2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 RCSID("$OpenBSD: sshconnect2.c,v 1.134 2004/01/19 21:25:15 markus Exp $");
28 #include "openbsd-compat/sys-queue.h"
39 #include "myproposal.h"
40 #include "sshconnect.h"
51 #include "pathnames.h"
58 extern char *client_version_string;
59 extern char *server_version_string;
60 extern Options options;
66 u_char *session_id2 = NULL;
67 u_int session_id2_len = 0;
70 struct sockaddr *xxx_hostaddr;
75 verify_host_key_callback(Key *hostkey)
77 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
78 fatal("Host key verification failed.");
83 ssh_kex2(char *host, struct sockaddr *hostaddr)
92 xxx_hostaddr = hostaddr;
95 if (options.gss_keyex) {
96 /* Add the GSSAPI mechanisms currently supported on this client to
97 * the key exchange algorithm proposal */
98 orig = myproposal[PROPOSAL_KEX_ALGS];
99 gss = ssh_gssapi_client_mechanisms((char *)get_canonical_hostname(1));
101 len = strlen(orig)+strlen(gss)+2;
102 myproposal[PROPOSAL_KEX_ALGS]=xmalloc(len);
103 snprintf(myproposal[PROPOSAL_KEX_ALGS],len,"%s,%s",gss,orig);
108 if (options.ciphers == (char *)-1) {
109 logit("No valid ciphers for protocol version 2 given, using defaults.");
110 options.ciphers = NULL;
112 if (options.ciphers != NULL) {
113 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
114 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
116 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
117 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
118 myproposal[PROPOSAL_ENC_ALGS_STOC] =
119 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
120 if (options.compression) {
121 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
122 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
124 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
125 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
127 if (options.macs != NULL) {
128 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
129 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
131 if (options.hostkeyalgorithms != NULL)
132 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
133 options.hostkeyalgorithms;
136 /* If we've got GSSAPI algorithms, then we also support the
137 * 'null' hostkey, as a last resort */
138 if (options.gss_keyex && gss) {
139 orig=myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
140 len = strlen(orig)+sizeof(",null");
141 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]=xmalloc(len);
142 snprintf(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],len,"%s,null",orig);
146 if (options.rekey_limit)
147 packet_set_rekey_limit(options.rekey_limit);
149 /* start key exchange */
150 kex = kex_setup(myproposal);
151 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
152 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
154 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
156 kex->client_version_string=client_version_string;
157 kex->server_version_string=server_version_string;
158 kex->verify_host_key=&verify_host_key_callback;
160 kex->options.gss_deleg_creds=options.gss_deleg_creds;
165 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
167 session_id2 = kex->session_id;
168 session_id2_len = kex->session_id_len;
171 /* send 1st encrypted/maced/compressed message */
172 packet_start(SSH2_MSG_IGNORE);
173 packet_put_cstring("markus");
183 typedef struct Authctxt Authctxt;
184 typedef struct Authmethod Authmethod;
185 typedef struct identity Identity;
186 typedef struct idlist Idlist;
189 TAILQ_ENTRY(identity) next;
190 AuthenticationConnection *ac; /* set if agent supports key */
191 Key *key; /* public/private key */
192 char *filename; /* comment for agent-only keys */
194 int isprivate; /* key points to the private key */
196 TAILQ_HEAD(idlist, identity);
199 const char *server_user;
200 const char *local_user;
208 AuthenticationConnection *agent;
210 Sensitive *sensitive;
211 /* kbd-interactive */
217 char *name; /* string to compare against server's list */
218 int (*userauth)(Authctxt *authctxt);
219 int *enabled; /* flag in option struct that enables method */
220 int *batch_flag; /* flag in option struct that disables method */
223 void input_userauth_success(int, u_int32_t, void *);
224 void input_userauth_failure(int, u_int32_t, void *);
225 void input_userauth_banner(int, u_int32_t, void *);
226 void input_userauth_error(int, u_int32_t, void *);
227 void input_userauth_info_req(int, u_int32_t, void *);
228 void input_userauth_pk_ok(int, u_int32_t, void *);
229 void input_userauth_passwd_changereq(int, u_int32_t, void *);
231 int userauth_none(Authctxt *);
232 int userauth_pubkey(Authctxt *);
233 int userauth_passwd(Authctxt *);
234 int userauth_kbdint(Authctxt *);
235 int userauth_hostbased(Authctxt *);
236 int userauth_kerberos(Authctxt *);
239 int userauth_gssapi(Authctxt *authctxt);
240 void input_gssapi_response(int type, u_int32_t, void *);
241 void input_gssapi_token(int type, u_int32_t, void *);
242 void input_gssapi_hash(int type, u_int32_t, void *);
243 void input_gssapi_error(int, u_int32_t, void *);
244 void input_gssapi_errtok(int, u_int32_t, void *);
248 int userauth_external(Authctxt *authctxt);
249 int userauth_gssapi(Authctxt *authctxt);
250 int userauth_gssapi_with_mic(Authctxt *authctxt);
251 int userauth_gssapi_without_mic(Authctxt *authctxt);
252 void input_gssapi_response(int type, u_int32_t, void *);
253 void input_gssapi_token(int type, u_int32_t, void *);
254 void input_gssapi_hash(int type, u_int32_t, void *);
255 void input_gssapi_error(int, u_int32_t, void *);
256 void input_gssapi_errtok(int, u_int32_t, void *);
259 void userauth(Authctxt *, char *);
261 static int sign_and_send_pubkey(Authctxt *, Identity *);
262 static void pubkey_prepare(Authctxt *);
263 static void pubkey_cleanup(Authctxt *);
264 static Key *load_identity_file(char *);
266 static Authmethod *authmethod_get(char *authlist);
267 static Authmethod *authmethod_lookup(const char *name);
268 static char *authmethods_get(void);
270 static int gssapi_with_mic = 1; /* flag to toggle "gssapi-with-mic" vs.
273 Authmethod authmethods[] = {
277 &options.gss_authentication,
280 userauth_gssapi_with_mic,
281 &options.gss_authentication,
284 userauth_gssapi_without_mic,
285 &options.gss_authentication,
290 &options.hostbased_authentication,
294 &options.pubkey_authentication,
296 {"keyboard-interactive",
298 &options.kbd_interactive_authentication,
299 &options.batch_mode},
302 &options.password_authentication,
303 &options.batch_mode},
308 {NULL, NULL, NULL, NULL}
312 ssh_userauth2(const char *local_user, const char *server_user, char *host,
313 Sensitive *sensitive)
318 if (options.challenge_response_authentication)
319 options.kbd_interactive_authentication = 1;
321 packet_start(SSH2_MSG_SERVICE_REQUEST);
322 packet_put_cstring("ssh-userauth");
324 debug("SSH2_MSG_SERVICE_REQUEST sent");
326 type = packet_read();
327 if (type != SSH2_MSG_SERVICE_ACCEPT)
328 fatal("Server denied authentication request: %d", type);
329 if (packet_remaining() > 0) {
330 char *reply = packet_get_string(NULL);
331 debug2("service_accept: %s", reply);
334 debug2("buggy server: service_accept w/o service");
337 debug("SSH2_MSG_SERVICE_ACCEPT received");
339 if (options.preferred_authentications == NULL)
340 options.preferred_authentications = authmethods_get();
342 /* setup authentication context */
343 memset(&authctxt, 0, sizeof(authctxt));
344 pubkey_prepare(&authctxt);
345 authctxt.server_user = server_user;
346 authctxt.local_user = local_user;
347 authctxt.host = host;
348 authctxt.service = "ssh-connection"; /* service name */
349 authctxt.success = 0;
350 authctxt.method = authmethod_lookup("none");
351 authctxt.authlist = NULL;
352 authctxt.methoddata = NULL;
353 authctxt.sensitive = sensitive;
354 authctxt.info_req_seen = 0;
355 if (authctxt.method == NULL)
356 fatal("ssh_userauth2: internal error: cannot send userauth none request");
358 /* initial userauth request */
359 userauth_none(&authctxt);
361 dispatch_init(&input_userauth_error);
362 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
363 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
364 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
365 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
367 pubkey_cleanup(&authctxt);
368 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
370 debug("Authentication succeeded (%s).", authctxt.method->name);
374 userauth(Authctxt *authctxt, char *authlist)
376 if (authctxt->methoddata) {
377 xfree(authctxt->methoddata);
378 authctxt->methoddata = NULL;
380 if (authlist == NULL) {
381 authlist = authctxt->authlist;
383 if (authctxt->authlist)
384 xfree(authctxt->authlist);
385 authctxt->authlist = authlist;
388 Authmethod *method = authmethod_get(authlist);
390 fatal("Permission denied (%s).", authlist);
391 authctxt->method = method;
393 /* reset the per method handler */
394 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
395 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
397 /* and try new method */
398 if (method->userauth(authctxt) != 0) {
399 debug2("we sent a %s packet, wait for reply", method->name);
402 debug2("we did not send a packet, disable method");
403 method->enabled = NULL;
409 input_userauth_error(int type, u_int32_t seq, void *ctxt)
411 fatal("input_userauth_error: bad message during authentication: "
416 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
420 debug3("input_userauth_banner");
421 msg = packet_get_string(NULL);
422 lang = packet_get_string(NULL);
423 if (options.log_level > SYSLOG_LEVEL_QUIET)
424 fprintf(stderr, "%s", msg);
430 input_userauth_success(int type, u_int32_t seq, void *ctxt)
432 Authctxt *authctxt = ctxt;
433 if (authctxt == NULL)
434 fatal("input_userauth_success: no authentication context");
435 if (authctxt->authlist) {
436 xfree(authctxt->authlist);
437 authctxt->authlist = NULL;
439 if (authctxt->methoddata) {
440 xfree(authctxt->methoddata);
441 authctxt->methoddata = NULL;
443 authctxt->success = 1; /* break out */
447 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
449 Authctxt *authctxt = ctxt;
450 char *authlist = NULL;
453 if (authctxt == NULL)
454 fatal("input_userauth_failure: no authentication context");
456 authlist = packet_get_string(NULL);
457 partial = packet_get_char();
461 logit("Authenticated with partial success.");
462 debug("Authentications that can continue: %s", authlist);
464 userauth(authctxt, authlist);
467 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
469 Authctxt *authctxt = ctxt;
473 int pktype, sent = 0;
478 if (authctxt == NULL)
479 fatal("input_userauth_pk_ok: no authentication context");
480 if (datafellows & SSH_BUG_PKOK) {
481 /* this is similar to SSH_BUG_PKAUTH */
482 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
483 pkblob = packet_get_string(&blen);
485 buffer_append(&b, pkblob, blen);
486 pkalg = buffer_get_string(&b, &alen);
489 pkalg = packet_get_string(&alen);
490 pkblob = packet_get_string(&blen);
494 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
496 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
497 debug("unknown pkalg %s", pkalg);
500 if ((key = key_from_blob(pkblob, blen)) == NULL) {
501 debug("no key from blob. pkalg %s", pkalg);
504 if (key->type != pktype) {
505 error("input_userauth_pk_ok: type mismatch "
506 "for decoded key (received %d, expected %d)",
510 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
511 debug2("input_userauth_pk_ok: fp %s", fp);
515 * search keys in the reverse order, because last candidate has been
516 * moved to the end of the queue. this also avoids confusion by
519 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, next, idlist) {
520 if (key_equal(key, id->key)) {
521 sent = sign_and_send_pubkey(authctxt, id);
531 /* try another method if we did not send a packet */
533 userauth(authctxt, NULL);
538 userauth_gssapi(Authctxt *authctxt)
540 Gssctxt *gssctxt = NULL;
541 static gss_OID_set gss_supported = NULL;
546 if (!options.gss_authentication) {
547 verbose("GSSAPI authentication disabled.");
551 /* Try one GSSAPI method at a time, rather than sending them all at
554 if (gss_supported == NULL)
555 gss_indicate_mechs(&min, &gss_supported);
557 /* Check to see if the mechanism is usable before we offer it */
558 while (mech < gss_supported->count && !ok) {
560 ssh_gssapi_delete_ctx(&gssctxt);
561 ssh_gssapi_build_ctx(&gssctxt);
562 ssh_gssapi_set_oid(gssctxt, &gss_supported->elements[mech]);
564 /* My DER encoding requires length<128 */
565 if (gss_supported->elements[mech].length < 128 &&
566 !GSS_ERROR(ssh_gssapi_import_name(gssctxt,
568 ok = 1; /* Mechanism works */
576 authctxt->methoddata=(void *)gssctxt;
578 packet_start(SSH2_MSG_USERAUTH_REQUEST);
579 packet_put_cstring(authctxt->server_user);
580 packet_put_cstring(authctxt->service);
581 packet_put_cstring(authctxt->method->name);
585 /* Some servers encode the OID incorrectly (as we used to) */
586 if (datafellows & SSH_BUG_GSSAPI_BER) {
587 packet_put_string(gss_supported->elements[mech].elements,
588 gss_supported->elements[mech].length);
590 packet_put_int((gss_supported->elements[mech].length)+2);
591 packet_put_char(SSH_GSS_OIDTYPE);
592 packet_put_char(gss_supported->elements[mech].length);
593 packet_put_raw(gss_supported->elements[mech].elements,
594 gss_supported->elements[mech].length);
599 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
600 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
601 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
602 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
604 mech++; /* Move along to next candidate */
610 userauth_gssapi_with_mic(Authctxt *authctxt)
613 return userauth_gssapi(authctxt);
617 userauth_gssapi_without_mic(Authctxt *authctxt)
620 return userauth_gssapi(authctxt);
624 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
626 Authctxt *authctxt = ctxt;
627 Gssctxt *gssctxt = authctxt->methoddata;
628 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
629 gss_buffer_desc gssbuf, mic;
630 OM_uint32 status, ms, flags;
633 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
634 recv_tok, &send_tok, &flags);
636 if (send_tok.length > 0) {
637 if (GSS_ERROR(status))
638 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
640 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
642 packet_put_string(send_tok.value, send_tok.length);
644 gss_release_buffer(&ms, &send_tok);
647 if (status == GSS_S_COMPLETE) {
648 /* send either complete or MIC, depending on mechanism */
649 if (!(flags & GSS_C_INTEG_FLAG) || !gssapi_with_mic) {
650 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
653 ssh_gssapi_buildmic(&b, authctxt->server_user,
654 authctxt->service, "gssapi-with-mic");
656 gssbuf.value = buffer_ptr(&b);
657 gssbuf.length = buffer_len(&b);
659 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
661 if (!GSS_ERROR(status)) {
662 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
663 packet_put_string(mic.value, mic.length);
669 gss_release_buffer(&ms, &mic);
677 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
679 Authctxt *authctxt = ctxt;
684 if (authctxt == NULL)
685 fatal("input_gssapi_response: no authentication context");
686 gssctxt = authctxt->methoddata;
689 oidv = packet_get_string(&oidlen);
691 if (datafellows & SSH_BUG_GSSAPI_BER) {
692 if (!ssh_gssapi_check_oid(gssctxt, oidv, oidlen))
693 fatal("Server returned different OID than expected");
695 if(oidv[0] != SSH_GSS_OIDTYPE || oidv[1] != oidlen-2) {
696 debug("Badly encoded mechanism OID received");
697 userauth(authctxt, NULL);
701 if (!ssh_gssapi_check_oid(gssctxt, oidv+2, oidlen-2))
702 fatal("Server returned different OID than expected");
709 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
710 /* Start again with next method on list */
711 debug("Trying to start again");
712 userauth(authctxt, NULL);
718 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
720 Authctxt *authctxt = ctxt;
721 gss_buffer_desc recv_tok;
725 if (authctxt == NULL)
726 fatal("input_gssapi_response: no authentication context");
728 recv_tok.value = packet_get_string(&slen);
729 recv_tok.length = slen; /* safe typecast */
733 status = process_gssapi_token(ctxt, &recv_tok);
735 xfree(recv_tok.value);
737 if (GSS_ERROR(status)) {
738 /* Start again with the next method in the list */
739 userauth(authctxt, NULL);
745 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
747 Authctxt *authctxt = ctxt;
749 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
750 gss_buffer_desc recv_tok;
751 OM_uint32 status, ms;
754 if (authctxt == NULL)
755 fatal("input_gssapi_response: no authentication context");
756 gssctxt = authctxt->methoddata;
758 recv_tok.value = packet_get_string(&len);
759 recv_tok.length = len;
763 /* Stick it into GSSAPI and see what it says */
764 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
765 &recv_tok, &send_tok, NULL);
767 xfree(recv_tok.value);
768 gss_release_buffer(&ms, &send_tok);
770 /* Server will be returning a failed packet after this one */
774 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
780 maj=packet_get_int();
781 min=packet_get_int();
782 msg=packet_get_string(NULL);
783 lang=packet_get_string(NULL);
787 debug("Server GSSAPI Error:\n%s\n", msg);
793 userauth_external(Authctxt *authctxt)
795 static int attempt = 0;
800 /* The client MUST NOT try this method if initial key exchange
801 was not performed using a GSSAPI-based key exchange
803 if (xxx_kex->kex_type != KEX_GSS_GRP1_SHA1) {
804 debug2("gsskex not performed, skipping external-keyx");
808 debug2("userauth_external");
809 packet_start(SSH2_MSG_USERAUTH_REQUEST);
811 if(options.implicit && !(datafellows & SSH_BUG_GSS_EMPTYUSER)) {
812 packet_put_cstring("");
815 packet_put_cstring(authctxt->server_user);
819 packet_put_cstring(authctxt->service);
820 packet_put_cstring(authctxt->method->name);
828 userauth_none(Authctxt *authctxt)
830 /* initial userauth request */
831 packet_start(SSH2_MSG_USERAUTH_REQUEST);
832 packet_put_cstring(authctxt->server_user);
833 packet_put_cstring(authctxt->service);
834 packet_put_cstring(authctxt->method->name);
840 userauth_passwd(Authctxt *authctxt)
842 static int attempt = 0;
846 if (attempt++ >= options.number_of_password_prompts)
850 error("Permission denied, please try again.");
852 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
853 authctxt->server_user, authctxt->host);
854 password = read_passphrase(prompt, 0);
855 packet_start(SSH2_MSG_USERAUTH_REQUEST);
856 packet_put_cstring(authctxt->server_user);
857 packet_put_cstring(authctxt->service);
858 packet_put_cstring(authctxt->method->name);
860 packet_put_cstring(password);
861 memset(password, 0, strlen(password));
863 packet_add_padding(64);
866 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
867 &input_userauth_passwd_changereq);
872 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
875 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
877 Authctxt *authctxt = ctxt;
878 char *info, *lang, *password = NULL, *retype = NULL;
881 debug2("input_userauth_passwd_changereq");
883 if (authctxt == NULL)
884 fatal("input_userauth_passwd_changereq: "
885 "no authentication context");
887 info = packet_get_string(NULL);
888 lang = packet_get_string(NULL);
889 if (strlen(info) > 0)
893 packet_start(SSH2_MSG_USERAUTH_REQUEST);
894 packet_put_cstring(authctxt->server_user);
895 packet_put_cstring(authctxt->service);
896 packet_put_cstring(authctxt->method->name);
897 packet_put_char(1); /* additional info */
898 snprintf(prompt, sizeof(prompt),
899 "Enter %.30s@%.128s's old password: ",
900 authctxt->server_user, authctxt->host);
901 password = read_passphrase(prompt, 0);
902 packet_put_cstring(password);
903 memset(password, 0, strlen(password));
906 while (password == NULL) {
907 snprintf(prompt, sizeof(prompt),
908 "Enter %.30s@%.128s's new password: ",
909 authctxt->server_user, authctxt->host);
910 password = read_passphrase(prompt, RP_ALLOW_EOF);
911 if (password == NULL) {
915 snprintf(prompt, sizeof(prompt),
916 "Retype %.30s@%.128s's new password: ",
917 authctxt->server_user, authctxt->host);
918 retype = read_passphrase(prompt, 0);
919 if (strcmp(password, retype) != 0) {
920 memset(password, 0, strlen(password));
922 logit("Mismatch; try again, EOF to quit.");
925 memset(retype, 0, strlen(retype));
928 packet_put_cstring(password);
929 memset(password, 0, strlen(password));
931 packet_add_padding(64);
934 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
935 &input_userauth_passwd_changereq);
939 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
940 u_char *data, u_int datalen)
945 /* the agent supports this key */
947 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
950 * we have already loaded the private key or
951 * the private key is stored in external hardware
953 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
954 return (key_sign(id->key, sigp, lenp, data, datalen));
955 /* load the private key from the file */
956 if ((prv = load_identity_file(id->filename)) == NULL)
958 ret = key_sign(prv, sigp, lenp, data, datalen);
964 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
967 u_char *blob, *signature;
973 debug3("sign_and_send_pubkey");
975 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
976 /* we cannot handle this key */
977 debug3("sign_and_send_pubkey: cannot handle key");
980 /* data to be signed */
982 if (datafellows & SSH_OLD_SESSIONID) {
983 buffer_append(&b, session_id2, session_id2_len);
984 skip = session_id2_len;
986 buffer_put_string(&b, session_id2, session_id2_len);
987 skip = buffer_len(&b);
989 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
990 buffer_put_cstring(&b, authctxt->server_user);
991 buffer_put_cstring(&b,
992 datafellows & SSH_BUG_PKSERVICE ?
995 if (datafellows & SSH_BUG_PKAUTH) {
996 buffer_put_char(&b, have_sig);
998 buffer_put_cstring(&b, authctxt->method->name);
999 buffer_put_char(&b, have_sig);
1000 buffer_put_cstring(&b, key_ssh_name(id->key));
1002 buffer_put_string(&b, blob, bloblen);
1004 /* generate signature */
1005 ret = identity_sign(id, &signature, &slen,
1006 buffer_ptr(&b), buffer_len(&b));
1015 if (datafellows & SSH_BUG_PKSERVICE) {
1017 buffer_append(&b, session_id2, session_id2_len);
1018 skip = session_id2_len;
1019 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1020 buffer_put_cstring(&b, authctxt->server_user);
1021 buffer_put_cstring(&b, authctxt->service);
1022 buffer_put_cstring(&b, authctxt->method->name);
1023 buffer_put_char(&b, have_sig);
1024 if (!(datafellows & SSH_BUG_PKAUTH))
1025 buffer_put_cstring(&b, key_ssh_name(id->key));
1026 buffer_put_string(&b, blob, bloblen);
1030 /* append signature */
1031 buffer_put_string(&b, signature, slen);
1034 /* skip session id and packet type */
1035 if (buffer_len(&b) < skip + 1)
1036 fatal("userauth_pubkey: internal error");
1037 buffer_consume(&b, skip + 1);
1039 /* put remaining data from buffer into packet */
1040 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1041 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1049 send_pubkey_test(Authctxt *authctxt, Identity *id)
1052 u_int bloblen, have_sig = 0;
1054 debug3("send_pubkey_test");
1056 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1057 /* we cannot handle this key */
1058 debug3("send_pubkey_test: cannot handle key");
1061 /* register callback for USERAUTH_PK_OK message */
1062 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1064 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1065 packet_put_cstring(authctxt->server_user);
1066 packet_put_cstring(authctxt->service);
1067 packet_put_cstring(authctxt->method->name);
1068 packet_put_char(have_sig);
1069 if (!(datafellows & SSH_BUG_PKAUTH))
1070 packet_put_cstring(key_ssh_name(id->key));
1071 packet_put_string(blob, bloblen);
1078 load_identity_file(char *filename)
1081 char prompt[300], *passphrase;
1085 if (stat(filename, &st) < 0) {
1086 debug3("no such identity: %s", filename);
1089 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
1090 if (private == NULL) {
1091 if (options.batch_mode)
1093 snprintf(prompt, sizeof prompt,
1094 "Enter passphrase for key '%.100s': ", filename);
1095 for (i = 0; i < options.number_of_password_prompts; i++) {
1096 passphrase = read_passphrase(prompt, 0);
1097 if (strcmp(passphrase, "") != 0) {
1098 private = key_load_private_type(KEY_UNSPEC, filename,
1102 debug2("no passphrase given, try next key");
1105 memset(passphrase, 0, strlen(passphrase));
1107 if (private != NULL || quit)
1109 debug2("bad passphrase given, try again...");
1116 * try keys in the following order:
1117 * 1. agent keys that are found in the config file
1118 * 2. other agent keys
1119 * 3. keys that are only listed in the config file
1122 pubkey_prepare(Authctxt *authctxt)
1125 Idlist agent, files, *preferred;
1127 AuthenticationConnection *ac;
1131 TAILQ_INIT(&agent); /* keys from the agent */
1132 TAILQ_INIT(&files); /* keys from the config file */
1133 preferred = &authctxt->keys;
1134 TAILQ_INIT(preferred); /* preferred order of keys */
1136 /* list of keys stored in the filesystem */
1137 for (i = 0; i < options.num_identity_files; i++) {
1138 key = options.identity_keys[i];
1139 if (key && key->type == KEY_RSA1)
1141 options.identity_keys[i] = NULL;
1142 id = xmalloc(sizeof(*id));
1143 memset(id, 0, sizeof(*id));
1145 id->filename = xstrdup(options.identity_files[i]);
1146 TAILQ_INSERT_TAIL(&files, id, next);
1148 /* list of keys supported by the agent */
1149 if ((ac = ssh_get_authentication_connection())) {
1150 for (key = ssh_get_first_identity(ac, &comment, 2);
1152 key = ssh_get_next_identity(ac, &comment, 2)) {
1154 TAILQ_FOREACH(id, &files, next) {
1155 /* agent keys from the config file are preferred */
1156 if (key_equal(key, id->key)) {
1159 TAILQ_REMOVE(&files, id, next);
1160 TAILQ_INSERT_TAIL(preferred, id, next);
1167 id = xmalloc(sizeof(*id));
1168 memset(id, 0, sizeof(*id));
1170 id->filename = comment;
1172 TAILQ_INSERT_TAIL(&agent, id, next);
1175 /* append remaining agent keys */
1176 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1177 TAILQ_REMOVE(&agent, id, next);
1178 TAILQ_INSERT_TAIL(preferred, id, next);
1180 authctxt->agent = ac;
1182 /* append remaining keys from the config file */
1183 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1184 TAILQ_REMOVE(&files, id, next);
1185 TAILQ_INSERT_TAIL(preferred, id, next);
1187 TAILQ_FOREACH(id, preferred, next) {
1188 debug2("key: %s (%p)", id->filename, id->key);
1193 pubkey_cleanup(Authctxt *authctxt)
1197 if (authctxt->agent != NULL)
1198 ssh_close_authentication_connection(authctxt->agent);
1199 for (id = TAILQ_FIRST(&authctxt->keys); id;
1200 id = TAILQ_FIRST(&authctxt->keys)) {
1201 TAILQ_REMOVE(&authctxt->keys, id, next);
1205 xfree(id->filename);
1211 userauth_pubkey(Authctxt *authctxt)
1216 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1219 /* move key to the end of the queue */
1220 TAILQ_REMOVE(&authctxt->keys, id, next);
1221 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1223 * send a test message if we have the public key. for
1224 * encrypted keys we cannot do this and have to load the
1225 * private key instead
1227 if (id->key && id->key->type != KEY_RSA1) {
1228 debug("Offering public key: %s", id->filename);
1229 sent = send_pubkey_test(authctxt, id);
1230 } else if (id->key == NULL) {
1231 debug("Trying private key: %s", id->filename);
1232 id->key = load_identity_file(id->filename);
1233 if (id->key != NULL) {
1235 sent = sign_and_send_pubkey(authctxt, id);
1247 * Send userauth request message specifying keyboard-interactive method.
1250 userauth_kbdint(Authctxt *authctxt)
1252 static int attempt = 0;
1254 if (attempt++ >= options.number_of_password_prompts)
1256 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1257 if (attempt > 1 && !authctxt->info_req_seen) {
1258 debug3("userauth_kbdint: disable: no info_req_seen");
1259 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1263 debug2("userauth_kbdint");
1264 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1265 packet_put_cstring(authctxt->server_user);
1266 packet_put_cstring(authctxt->service);
1267 packet_put_cstring(authctxt->method->name);
1268 packet_put_cstring(""); /* lang */
1269 packet_put_cstring(options.kbd_interactive_devices ?
1270 options.kbd_interactive_devices : "");
1273 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1278 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1281 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1283 Authctxt *authctxt = ctxt;
1284 char *name, *inst, *lang, *prompt, *response;
1285 u_int num_prompts, i;
1288 debug2("input_userauth_info_req");
1290 if (authctxt == NULL)
1291 fatal("input_userauth_info_req: no authentication context");
1293 authctxt->info_req_seen = 1;
1295 name = packet_get_string(NULL);
1296 inst = packet_get_string(NULL);
1297 lang = packet_get_string(NULL);
1298 if (strlen(name) > 0)
1300 if (strlen(inst) > 0)
1306 num_prompts = packet_get_int();
1308 * Begin to build info response packet based on prompts requested.
1309 * We commit to providing the correct number of responses, so if
1310 * further on we run into a problem that prevents this, we have to
1311 * be sure and clean this up and send a correct error response.
1313 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1314 packet_put_int(num_prompts);
1316 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1317 for (i = 0; i < num_prompts; i++) {
1318 prompt = packet_get_string(NULL);
1319 echo = packet_get_char();
1321 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1323 packet_put_cstring(response);
1324 memset(response, 0, strlen(response));
1328 packet_check_eom(); /* done with parsing incoming message. */
1330 packet_add_padding(64);
1335 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1336 u_char *data, u_int datalen)
1341 int to[2], from[2], status, version = 2;
1343 debug2("ssh_keysign called");
1345 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1346 error("ssh_keysign: no installed: %s", strerror(errno));
1349 if (fflush(stdout) != 0)
1350 error("ssh_keysign: fflush: %s", strerror(errno));
1352 error("ssh_keysign: pipe: %s", strerror(errno));
1355 if (pipe(from) < 0) {
1356 error("ssh_keysign: pipe: %s", strerror(errno));
1359 if ((pid = fork()) < 0) {
1360 error("ssh_keysign: fork: %s", strerror(errno));
1367 if (dup2(from[1], STDOUT_FILENO) < 0)
1368 fatal("ssh_keysign: dup2: %s", strerror(errno));
1370 if (dup2(to[0], STDIN_FILENO) < 0)
1371 fatal("ssh_keysign: dup2: %s", strerror(errno));
1374 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1375 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1382 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1383 buffer_put_string(&b, data, datalen);
1384 if (ssh_msg_send(to[1], version, &b) == -1)
1385 fatal("ssh_keysign: couldn't send request");
1387 if (ssh_msg_recv(from[0], &b) < 0) {
1388 error("ssh_keysign: no reply");
1395 while (waitpid(pid, &status, 0) < 0)
1399 if (buffer_get_char(&b) != version) {
1400 error("ssh_keysign: bad version");
1404 *sigp = buffer_get_string(&b, lenp);
1411 userauth_hostbased(Authctxt *authctxt)
1413 Key *private = NULL;
1414 Sensitive *sensitive = authctxt->sensitive;
1416 u_char *signature, *blob;
1417 char *chost, *pkalg, *p;
1418 const char *service;
1420 int ok, i, len, found = 0;
1422 /* check for a useful key */
1423 for (i = 0; i < sensitive->nkeys; i++) {
1424 private = sensitive->keys[i];
1425 if (private && private->type != KEY_RSA1) {
1427 /* we take and free the key */
1428 sensitive->keys[i] = NULL;
1433 debug("No more client hostkeys for hostbased authentication.");
1436 if (key_to_blob(private, &blob, &blen) == 0) {
1440 /* figure out a name for the client host */
1441 p = get_local_name(packet_get_connection_in());
1443 error("userauth_hostbased: cannot get local ipaddr/name");
1447 len = strlen(p) + 2;
1448 chost = xmalloc(len);
1449 strlcpy(chost, p, len);
1450 strlcat(chost, ".", len);
1451 debug2("userauth_hostbased: chost %s", chost);
1454 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1456 pkalg = xstrdup(key_ssh_name(private));
1458 /* construct data */
1459 buffer_put_string(&b, session_id2, session_id2_len);
1460 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1461 buffer_put_cstring(&b, authctxt->server_user);
1462 buffer_put_cstring(&b, service);
1463 buffer_put_cstring(&b, authctxt->method->name);
1464 buffer_put_cstring(&b, pkalg);
1465 buffer_put_string(&b, blob, blen);
1466 buffer_put_cstring(&b, chost);
1467 buffer_put_cstring(&b, authctxt->local_user);
1471 if (sensitive->external_keysign)
1472 ok = ssh_keysign(private, &signature, &slen,
1473 buffer_ptr(&b), buffer_len(&b));
1475 ok = key_sign(private, &signature, &slen,
1476 buffer_ptr(&b), buffer_len(&b));
1480 error("key_sign failed");
1485 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1486 packet_put_cstring(authctxt->server_user);
1487 packet_put_cstring(authctxt->service);
1488 packet_put_cstring(authctxt->method->name);
1489 packet_put_cstring(pkalg);
1490 packet_put_string(blob, blen);
1491 packet_put_cstring(chost);
1492 packet_put_cstring(authctxt->local_user);
1493 packet_put_string(signature, slen);
1494 memset(signature, 's', slen);
1503 /* find auth method */
1506 * given auth method name, if configurable options permit this method fill
1507 * in auth_ident field and return true, otherwise return false.
1510 authmethod_is_enabled(Authmethod *method)
1514 /* return false if options indicate this method is disabled */
1515 if (method->enabled == NULL || *method->enabled == 0)
1517 /* return false if batch mode is enabled but method needs interactive mode */
1518 if (method->batch_flag != NULL && *method->batch_flag != 0)
1524 authmethod_lookup(const char *name)
1526 Authmethod *method = NULL;
1528 for (method = authmethods; method->name != NULL; method++)
1529 if (strcmp(name, method->name) == 0)
1531 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1535 /* XXX internal state */
1536 static Authmethod *current = NULL;
1537 static char *supported = NULL;
1538 static char *preferred = NULL;
1541 * Given the authentication method list sent by the server, return the
1542 * next method we should try. If the server initially sends a nil list,
1543 * use a built-in default list.
1546 authmethod_get(char *authlist)
1551 /* Use a suitable default if we're passed a nil list. */
1552 if (authlist == NULL || strlen(authlist) == 0)
1553 authlist = options.preferred_authentications;
1555 if (supported == NULL || strcmp(authlist, supported) != 0) {
1556 debug3("start over, passed a different list %s", authlist);
1557 if (supported != NULL)
1559 supported = xstrdup(authlist);
1560 preferred = options.preferred_authentications;
1561 debug3("preferred %s", preferred);
1563 } else if (current != NULL && authmethod_is_enabled(current))
1567 if ((name = match_list(preferred, supported, &next)) == NULL) {
1568 debug("No more authentication methods to try.");
1573 debug3("authmethod_lookup %s", name);
1574 debug3("remaining preferred: %s", preferred);
1575 if ((current = authmethod_lookup(name)) != NULL &&
1576 authmethod_is_enabled(current)) {
1577 debug3("authmethod_is_enabled %s", name);
1578 debug("Next authentication method: %s", name);
1585 authmethods_get(void)
1587 Authmethod *method = NULL;
1592 for (method = authmethods; method->name != NULL; method++) {
1593 if (authmethod_is_enabled(method)) {
1594 if (buffer_len(&b) > 0)
1595 buffer_append(&b, ",", 1);
1596 buffer_append(&b, method->name, strlen(method->name));
1599 buffer_append(&b, "\0", 1);
1600 list = xstrdup(buffer_ptr(&b));