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.114 2003/04/01 10:22:21 markus Exp $");
37 #include "myproposal.h"
38 #include "sshconnect.h"
49 #include "pathnames.h"
56 extern char *client_version_string;
57 extern char *server_version_string;
58 extern Options options;
64 u_char *session_id2 = NULL;
65 int session_id2_len = 0;
68 struct sockaddr *xxx_hostaddr;
73 verify_host_key_callback(Key *hostkey)
75 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
76 fatal("Host key verification failed.");
81 ssh_kex2(char *host, struct sockaddr *hostaddr)
90 xxx_hostaddr = hostaddr;
93 if (options.gss_keyex) {
95 /* Add the GSSAPI mechanisms currently supported on this client to
96 * the key exchange algorithm proposal */
97 orig = myproposal[PROPOSAL_KEX_ALGS];
98 canonhost = xstrdup(get_canonical_hostname(1));
99 resolve_localhost(&canonhost);
100 gss = ssh_gssapi_client_mechanisms(canonhost);
104 len = strlen(orig)+strlen(gss)+2;
105 myproposal[PROPOSAL_KEX_ALGS]=xmalloc(len);
106 snprintf(myproposal[PROPOSAL_KEX_ALGS],len,"%s,%s",gss,orig);
111 if (options.ciphers == (char *)-1) {
112 log("No valid ciphers for protocol version 2 given, using defaults.");
113 options.ciphers = NULL;
115 if (options.ciphers != NULL) {
116 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
117 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
119 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
120 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
121 myproposal[PROPOSAL_ENC_ALGS_STOC] =
122 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
123 if (options.compression) {
124 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
125 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
127 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
128 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
130 if (options.macs != NULL) {
131 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
132 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
134 if (options.hostkeyalgorithms != NULL)
135 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
136 options.hostkeyalgorithms;
139 /* If we've got GSSAPI algorithms, then we also support the
140 * 'null' hostkey, as a last resort */
141 if (options.gss_keyex && gss) {
142 orig=myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
143 len = strlen(orig)+sizeof(",null");
144 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]=xmalloc(len);
145 snprintf(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],len,"%s,null",orig);
148 /* start key exchange */
149 kex = kex_setup(myproposal);
150 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
151 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
153 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
155 kex->client_version_string=client_version_string;
156 kex->server_version_string=server_version_string;
157 kex->verify_host_key=&verify_host_key_callback;
160 kex->options.gss_deleg_creds=options.gss_deleg_creds;
164 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
166 session_id2 = kex->session_id;
167 session_id2_len = kex->session_id_len;
170 /* send 1st encrypted/maced/compressed message */
171 packet_start(SSH2_MSG_IGNORE);
172 packet_put_cstring("markus");
182 typedef struct Authctxt Authctxt;
183 typedef struct Authmethod Authmethod;
185 typedef int sign_cb_fn(
186 Authctxt *authctxt, Key *key,
187 u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
190 const char *server_user;
191 const char *local_user;
199 sign_cb_fn *last_key_sign;
201 AuthenticationConnection *agent;
203 Sensitive *sensitive;
204 /* kbd-interactive */
210 char *name; /* string to compare against server's list */
211 int (*userauth)(Authctxt *authctxt);
212 int *enabled; /* flag in option struct that enables method */
213 int *batch_flag; /* flag in option struct that disables method */
216 void input_userauth_success(int, u_int32_t, void *);
217 void input_userauth_failure(int, u_int32_t, void *);
218 void input_userauth_banner(int, u_int32_t, void *);
219 void input_userauth_error(int, u_int32_t, void *);
220 void input_userauth_info_req(int, u_int32_t, void *);
221 void input_userauth_pk_ok(int, u_int32_t, void *);
222 void input_userauth_passwd_changereq(int, u_int32_t, void *);
224 int userauth_none(Authctxt *);
225 int userauth_pubkey(Authctxt *);
226 int userauth_passwd(Authctxt *);
227 int userauth_kbdint(Authctxt *);
228 int userauth_hostbased(Authctxt *);
231 int userauth_external(Authctxt *authctxt);
232 int userauth_gssapi(Authctxt *authctxt);
233 void input_gssapi_response(int type, u_int32_t plen, void *ctxt);
234 void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
235 void input_gssapi_hash(int type, u_int32_t plen, void *ctxt);
236 void input_gssapi_error(int, u_int32_t, void *);
239 void userauth(Authctxt *, char *);
241 static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
242 static void clear_auth_state(Authctxt *);
244 static Authmethod *authmethod_get(char *authlist);
245 static Authmethod *authmethod_lookup(const char *name);
246 static char *authmethods_get(void);
248 Authmethod authmethods[] = {
252 &options.gss_authentication,
256 &options.gss_authentication,
261 &options.hostbased_authentication,
265 &options.pubkey_authentication,
267 {"keyboard-interactive",
269 &options.kbd_interactive_authentication,
270 &options.batch_mode},
273 &options.password_authentication,
274 &options.batch_mode},
279 {NULL, NULL, NULL, NULL}
283 ssh_userauth2(const char *local_user, const char *server_user, char *host,
284 Sensitive *sensitive)
289 if (options.challenge_response_authentication)
290 options.kbd_interactive_authentication = 1;
292 packet_start(SSH2_MSG_SERVICE_REQUEST);
293 packet_put_cstring("ssh-userauth");
295 debug("SSH2_MSG_SERVICE_REQUEST sent");
297 type = packet_read();
298 if (type != SSH2_MSG_SERVICE_ACCEPT)
299 fatal("Server denied authentication request: %d", type);
300 if (packet_remaining() > 0) {
301 char *reply = packet_get_string(NULL);
302 debug2("service_accept: %s", reply);
305 debug2("buggy server: service_accept w/o service");
308 debug("SSH2_MSG_SERVICE_ACCEPT received");
310 if (options.preferred_authentications == NULL)
311 options.preferred_authentications = authmethods_get();
313 /* setup authentication context */
314 memset(&authctxt, 0, sizeof(authctxt));
315 authctxt.agent = ssh_get_authentication_connection();
316 authctxt.server_user = server_user;
317 authctxt.local_user = local_user;
318 authctxt.host = host;
319 authctxt.service = "ssh-connection"; /* service name */
320 authctxt.success = 0;
321 authctxt.method = authmethod_lookup("none");
322 authctxt.authlist = NULL;
323 authctxt.methoddata = NULL;
324 authctxt.sensitive = sensitive;
325 authctxt.info_req_seen = 0;
326 if (authctxt.method == NULL)
327 fatal("ssh_userauth2: internal error: cannot send userauth none request");
329 /* initial userauth request */
330 userauth_none(&authctxt);
332 dispatch_init(&input_userauth_error);
333 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
334 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
335 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
336 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
338 if (authctxt.agent != NULL)
339 ssh_close_authentication_connection(authctxt.agent);
341 debug("Authentication succeeded (%s).", authctxt.method->name);
344 userauth(Authctxt *authctxt, char *authlist)
346 if (authctxt->methoddata!=NULL) {
347 xfree(authctxt->methoddata);
348 authctxt->methoddata=NULL;
351 if (authlist == NULL) {
352 authlist = authctxt->authlist;
354 if (authctxt->authlist)
355 xfree(authctxt->authlist);
356 authctxt->authlist = authlist;
359 Authmethod *method = authmethod_get(authlist);
361 fatal("Permission denied (%s).", authlist);
362 authctxt->method = method;
363 if (method->userauth(authctxt) != 0) {
364 debug2("we sent a %s packet, wait for reply", method->name);
367 debug2("we did not send a packet, disable method");
368 method->enabled = NULL;
374 input_userauth_error(int type, u_int32_t seq, void *ctxt)
376 fatal("input_userauth_error: bad message during authentication: "
381 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
384 debug3("input_userauth_banner");
385 msg = packet_get_string(NULL);
386 lang = packet_get_string(NULL);
387 fprintf(stderr, "%s", msg);
393 input_userauth_success(int type, u_int32_t seq, void *ctxt)
395 Authctxt *authctxt = ctxt;
396 if (authctxt == NULL)
397 fatal("input_userauth_success: no authentication context");
398 if (authctxt->authlist)
399 xfree(authctxt->authlist);
400 if (authctxt->methoddata)
401 xfree(authctxt->methoddata);
402 clear_auth_state(authctxt);
403 authctxt->success = 1; /* break out */
407 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
409 Authctxt *authctxt = ctxt;
410 char *authlist = NULL;
413 if (authctxt == NULL)
414 fatal("input_userauth_failure: no authentication context");
416 authlist = packet_get_string(NULL);
417 partial = packet_get_char();
421 log("Authenticated with partial success.");
422 debug("Authentications that can continue: %s", authlist);
424 clear_auth_state(authctxt);
425 userauth(authctxt, authlist);
428 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
430 Authctxt *authctxt = ctxt;
433 int pktype, sent = 0;
438 if (authctxt == NULL)
439 fatal("input_userauth_pk_ok: no authentication context");
440 if (datafellows & SSH_BUG_PKOK) {
441 /* this is similar to SSH_BUG_PKAUTH */
442 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
443 pkblob = packet_get_string(&blen);
445 buffer_append(&b, pkblob, blen);
446 pkalg = buffer_get_string(&b, &alen);
449 pkalg = packet_get_string(&alen);
450 pkblob = packet_get_string(&blen);
454 debug("Server accepts key: pkalg %s blen %u lastkey %p hint %d",
455 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
458 if (authctxt->last_key == NULL ||
459 authctxt->last_key_sign == NULL) {
460 debug("no last key or no sign cb");
463 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
464 debug("unknown pkalg %s", pkalg);
467 if ((key = key_from_blob(pkblob, blen)) == NULL) {
468 debug("no key from blob. pkalg %s", pkalg);
471 if (key->type != pktype) {
472 error("input_userauth_pk_ok: type mismatch "
473 "for decoded key (received %d, expected %d)",
477 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
478 debug2("input_userauth_pk_ok: fp %s", fp);
480 if (!key_equal(key, authctxt->last_key)) {
481 debug("key != last_key");
484 sent = sign_and_send_pubkey(authctxt, key,
485 authctxt->last_key_sign);
494 clear_auth_state(authctxt);
495 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
497 /* try another method if we did not send a packet */
499 userauth(authctxt, NULL);
505 userauth_gssapi(Authctxt *authctxt)
507 Gssctxt *gssctxt = NULL;
508 static gss_OID_set supported = NULL;
513 /* Things work better if we send one mechanism at a time, rather
514 * than them all at once. This means that if we fail at some point
515 * in the middle of a negotiation, we can come back and try something
518 if (datafellows & SSH_OLD_GSSAPI) return 0;
520 /* Before we offer a mechanism, check that we can support it. Don't
521 * bother trying to get credentials - as the standard fallback will
522 * deal with that kind of failure.
525 if (supported==NULL) gss_indicate_mechs(&min, &supported);
527 while (mech<supported->count && !ok) {
528 if (gssctxt) ssh_gssapi_delete_ctx(&gssctxt);
529 ssh_gssapi_build_ctx(&gssctxt);
530 ssh_gssapi_set_oid(gssctxt,&supported->elements[mech]);
532 /* The DER encoding below only works for lengths<128,
535 if (supported->elements[mech].length<128 &&
536 !GSS_ERROR(ssh_gssapi_import_name(gssctxt,
538 ok = 1; /* Mechanism works */
546 authctxt->methoddata=(void *)gssctxt;
548 packet_start(SSH2_MSG_USERAUTH_REQUEST);
549 packet_put_cstring(authctxt->server_user);
550 packet_put_cstring(authctxt->service);
551 packet_put_cstring(authctxt->method->name);
555 /* The newest gsskeyex draft stipulates that OIDs should
556 * be DER encoded, so we need to add the object type and
557 * length information back on */
558 if (datafellows & SSH_BUG_GSSAPI_BER) {
559 packet_put_string(supported->elements[mech].elements,
560 supported->elements[mech].length);
562 packet_put_int((supported->elements[mech].length)+2);
563 packet_put_char(0x06);
564 packet_put_char(supported->elements[mech].length);
565 packet_put_raw(supported->elements[mech].elements,
566 supported->elements[mech].length);
572 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,&input_gssapi_response);
573 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,&input_gssapi_token);
574 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR,&input_gssapi_error);
576 mech++; /* Move along to next candidate */
582 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
584 Authctxt *authctxt = ctxt;
589 gss_buffer_desc send_tok;
591 if (authctxt == NULL)
592 fatal("input_gssapi_response: no authentication context");
593 gssctxt = authctxt->methoddata;
596 oidv=packet_get_string(&oidlen);
598 if (datafellows & SSH_BUG_GSSAPI_BER) {
599 if (!ssh_gssapi_check_oid(gssctxt,oidv,oidlen)) {
600 fatal("Server returned different OID than expected");
602 ssh_gssapi_set_oid_data(gssctxt,oidv,oidlen);
604 if(oidv[0]!=0x06 || oidv[1]!=oidlen-2) {
605 debug("Badly encoded mechanism OID received");
606 clear_auth_state(authctxt);
607 userauth(authctxt,NULL);
610 if (!ssh_gssapi_check_oid(gssctxt,oidv+2,oidlen-2)) {
611 fatal("Server returned different OID than expected");
613 ssh_gssapi_set_oid_data(gssctxt,oidv+2,oidlen-2);
618 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
619 GSS_C_NO_BUFFER, &send_tok,
621 if (GSS_ERROR(status)) {
622 /* Start again with next method on list */
623 debug("Trying to start again");
624 clear_auth_state(authctxt);
625 userauth(authctxt,NULL);
629 /* We must have data to send */
630 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
631 packet_put_string(send_tok.value,send_tok.length);
634 gss_release_buffer(&ms, &send_tok);
638 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
640 Authctxt *authctxt = ctxt;
642 gss_buffer_desc send_tok,recv_tok;
646 if (authctxt == NULL)
647 fatal("input_gssapi_token: no authentication context");
648 gssctxt = authctxt->methoddata;
650 recv_tok.value=packet_get_string(&slen);
651 recv_tok.length=slen; /* safe typecast */
653 status=ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
654 &recv_tok, &send_tok, NULL);
658 if (GSS_ERROR(status)) {
659 /* Start again with the next method in the list */
660 clear_auth_state(authctxt);
661 userauth(authctxt,NULL);
665 if (send_tok.length>0) {
666 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
667 packet_put_string(send_tok.value,send_tok.length);
672 if (status == GSS_S_COMPLETE) {
673 /* If that succeeded, send a exchange complete message */
674 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
681 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
687 maj=packet_get_int();
688 min=packet_get_int();
689 msg=packet_get_string(NULL);
690 lang=packet_get_string(NULL);
694 fprintf(stderr, "Server GSSAPI Error:\n%s\n", msg);
700 userauth_external(Authctxt *authctxt)
702 static int attempt =0;
707 debug2("userauth_external");
708 packet_start(SSH2_MSG_USERAUTH_REQUEST);
710 if(options.implicit && !(datafellows & SSH_BUG_GSS_EMPTYUSER)) {
711 packet_put_cstring("");
714 packet_put_cstring(authctxt->server_user);
718 packet_put_cstring(authctxt->service);
719 packet_put_cstring(authctxt->method->name);
727 userauth_none(Authctxt *authctxt)
729 /* initial userauth request */
730 packet_start(SSH2_MSG_USERAUTH_REQUEST);
731 packet_put_cstring(authctxt->server_user);
732 packet_put_cstring(authctxt->service);
733 packet_put_cstring(authctxt->method->name);
740 userauth_passwd(Authctxt *authctxt)
742 static int attempt = 0;
746 if (attempt++ >= options.number_of_password_prompts)
750 error("Permission denied, please try again.");
752 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
753 authctxt->server_user, authctxt->host);
754 password = read_passphrase(prompt, 0);
755 packet_start(SSH2_MSG_USERAUTH_REQUEST);
756 packet_put_cstring(authctxt->server_user);
757 packet_put_cstring(authctxt->service);
758 packet_put_cstring(authctxt->method->name);
760 packet_put_cstring(password);
761 memset(password, 0, strlen(password));
763 packet_add_padding(64);
766 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
767 &input_userauth_passwd_changereq);
772 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
775 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
777 Authctxt *authctxt = ctxt;
778 char *info, *lang, *password = NULL, *retype = NULL;
781 debug2("input_userauth_passwd_changereq");
783 if (authctxt == NULL)
784 fatal("input_userauth_passwd_changereq: "
785 "no authentication context");
787 info = packet_get_string(NULL);
788 lang = packet_get_string(NULL);
789 if (strlen(info) > 0)
793 packet_start(SSH2_MSG_USERAUTH_REQUEST);
794 packet_put_cstring(authctxt->server_user);
795 packet_put_cstring(authctxt->service);
796 packet_put_cstring(authctxt->method->name);
797 packet_put_char(1); /* additional info */
798 snprintf(prompt, sizeof(prompt),
799 "Enter %.30s@%.128s's old password: ",
800 authctxt->server_user, authctxt->host);
801 password = read_passphrase(prompt, 0);
802 packet_put_cstring(password);
803 memset(password, 0, strlen(password));
806 while (password == NULL) {
807 snprintf(prompt, sizeof(prompt),
808 "Enter %.30s@%.128s's new password: ",
809 authctxt->server_user, authctxt->host);
810 password = read_passphrase(prompt, RP_ALLOW_EOF);
811 if (password == NULL) {
815 snprintf(prompt, sizeof(prompt),
816 "Retype %.30s@%.128s's new password: ",
817 authctxt->server_user, authctxt->host);
818 retype = read_passphrase(prompt, 0);
819 if (strcmp(password, retype) != 0) {
820 memset(password, 0, strlen(password));
822 log("Mismatch; try again, EOF to quit.");
825 memset(retype, 0, strlen(retype));
828 packet_put_cstring(password);
829 memset(password, 0, strlen(password));
831 packet_add_padding(64);
834 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
835 &input_userauth_passwd_changereq);
839 clear_auth_state(Authctxt *authctxt)
841 /* XXX clear authentication state */
842 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
844 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,NULL);
845 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,NULL);
846 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR,NULL);
849 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
850 debug3("clear_auth_state: key_free %p", authctxt->last_key);
851 key_free(authctxt->last_key);
853 authctxt->last_key = NULL;
854 authctxt->last_key_hint = -2;
855 authctxt->last_key_sign = NULL;
859 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
862 u_char *blob, *signature;
868 debug3("sign_and_send_pubkey");
870 if (key_to_blob(k, &blob, &bloblen) == 0) {
871 /* we cannot handle this key */
872 debug3("sign_and_send_pubkey: cannot handle key");
875 /* data to be signed */
877 if (datafellows & SSH_OLD_SESSIONID) {
878 buffer_append(&b, session_id2, session_id2_len);
879 skip = session_id2_len;
881 buffer_put_string(&b, session_id2, session_id2_len);
882 skip = buffer_len(&b);
884 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
885 buffer_put_cstring(&b, authctxt->server_user);
886 buffer_put_cstring(&b,
887 datafellows & SSH_BUG_PKSERVICE ?
890 if (datafellows & SSH_BUG_PKAUTH) {
891 buffer_put_char(&b, have_sig);
893 buffer_put_cstring(&b, authctxt->method->name);
894 buffer_put_char(&b, have_sig);
895 buffer_put_cstring(&b, key_ssh_name(k));
897 buffer_put_string(&b, blob, bloblen);
899 /* generate signature */
900 ret = (*sign_callback)(authctxt, k, &signature, &slen,
901 buffer_ptr(&b), buffer_len(&b));
910 if (datafellows & SSH_BUG_PKSERVICE) {
912 buffer_append(&b, session_id2, session_id2_len);
913 skip = session_id2_len;
914 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
915 buffer_put_cstring(&b, authctxt->server_user);
916 buffer_put_cstring(&b, authctxt->service);
917 buffer_put_cstring(&b, authctxt->method->name);
918 buffer_put_char(&b, have_sig);
919 if (!(datafellows & SSH_BUG_PKAUTH))
920 buffer_put_cstring(&b, key_ssh_name(k));
921 buffer_put_string(&b, blob, bloblen);
925 /* append signature */
926 buffer_put_string(&b, signature, slen);
929 /* skip session id and packet type */
930 if (buffer_len(&b) < skip + 1)
931 fatal("userauth_pubkey: internal error");
932 buffer_consume(&b, skip + 1);
934 /* put remaining data from buffer into packet */
935 packet_start(SSH2_MSG_USERAUTH_REQUEST);
936 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
944 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
948 u_int bloblen, have_sig = 0;
950 debug3("send_pubkey_test");
952 if (key_to_blob(k, &blob, &bloblen) == 0) {
953 /* we cannot handle this key */
954 debug3("send_pubkey_test: cannot handle key");
957 /* register callback for USERAUTH_PK_OK message */
958 authctxt->last_key_sign = sign_callback;
959 authctxt->last_key_hint = hint;
960 authctxt->last_key = k;
961 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
963 packet_start(SSH2_MSG_USERAUTH_REQUEST);
964 packet_put_cstring(authctxt->server_user);
965 packet_put_cstring(authctxt->service);
966 packet_put_cstring(authctxt->method->name);
967 packet_put_char(have_sig);
968 if (!(datafellows & SSH_BUG_PKAUTH))
969 packet_put_cstring(key_ssh_name(k));
970 packet_put_string(blob, bloblen);
977 load_identity_file(char *filename)
980 char prompt[300], *passphrase;
984 if (stat(filename, &st) < 0) {
985 debug3("no such identity: %s", filename);
988 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
989 if (private == NULL) {
990 if (options.batch_mode)
992 snprintf(prompt, sizeof prompt,
993 "Enter passphrase for key '%.100s': ", filename);
994 for (i = 0; i < options.number_of_password_prompts; i++) {
995 passphrase = read_passphrase(prompt, 0);
996 if (strcmp(passphrase, "") != 0) {
997 private = key_load_private_type(KEY_UNSPEC, filename,
1001 debug2("no passphrase given, try next key");
1004 memset(passphrase, 0, strlen(passphrase));
1006 if (private != NULL || quit)
1008 debug2("bad passphrase given, try again...");
1015 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
1016 u_char *data, u_int datalen)
1021 idx = authctxt->last_key_hint;
1025 /* private key is stored in external hardware */
1026 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
1027 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
1029 private = load_identity_file(options.identity_files[idx]);
1030 if (private == NULL)
1032 ret = key_sign(private, sigp, lenp, data, datalen);
1038 agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
1039 u_char *data, u_int datalen)
1041 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
1045 key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
1046 u_char *data, u_int datalen)
1048 return key_sign(key, sigp, lenp, data, datalen);
1052 userauth_pubkey_agent(Authctxt *authctxt)
1054 static int called = 0;
1060 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
1061 debug2("userauth_pubkey_agent: no keys at all");
1064 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
1066 debug2("userauth_pubkey_agent: no more keys");
1068 debug("Offering agent key: %s", comment);
1070 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
1075 debug2("userauth_pubkey_agent: no message sent");
1080 userauth_pubkey(Authctxt *authctxt)
1087 if (authctxt->agent != NULL) {
1089 sent = userauth_pubkey_agent(authctxt);
1090 } while (!sent && authctxt->agent->howmany > 0);
1092 while (!sent && idx < options.num_identity_files) {
1093 key = options.identity_keys[idx];
1094 filename = options.identity_files[idx];
1096 debug("Trying private key: %s", filename);
1097 key = load_identity_file(filename);
1099 sent = sign_and_send_pubkey(authctxt, key,
1103 } else if (key->type != KEY_RSA1) {
1104 debug("Offering public key: %s", filename);
1105 sent = send_pubkey_test(authctxt, key,
1106 identity_sign_cb, idx);
1114 * Send userauth request message specifying keyboard-interactive method.
1117 userauth_kbdint(Authctxt *authctxt)
1119 static int attempt = 0;
1121 if (attempt++ >= options.number_of_password_prompts)
1123 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1124 if (attempt > 1 && !authctxt->info_req_seen) {
1125 debug3("userauth_kbdint: disable: no info_req_seen");
1126 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1130 debug2("userauth_kbdint");
1131 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1132 packet_put_cstring(authctxt->server_user);
1133 packet_put_cstring(authctxt->service);
1134 packet_put_cstring(authctxt->method->name);
1135 packet_put_cstring(""); /* lang */
1136 packet_put_cstring(options.kbd_interactive_devices ?
1137 options.kbd_interactive_devices : "");
1140 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1145 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1148 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1150 Authctxt *authctxt = ctxt;
1151 char *name, *inst, *lang, *prompt, *response;
1152 u_int num_prompts, i;
1155 debug2("input_userauth_info_req");
1157 if (authctxt == NULL)
1158 fatal("input_userauth_info_req: no authentication context");
1160 authctxt->info_req_seen = 1;
1162 name = packet_get_string(NULL);
1163 inst = packet_get_string(NULL);
1164 lang = packet_get_string(NULL);
1165 if (strlen(name) > 0)
1167 if (strlen(inst) > 0)
1173 num_prompts = packet_get_int();
1175 * Begin to build info response packet based on prompts requested.
1176 * We commit to providing the correct number of responses, so if
1177 * further on we run into a problem that prevents this, we have to
1178 * be sure and clean this up and send a correct error response.
1180 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1181 packet_put_int(num_prompts);
1183 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1184 for (i = 0; i < num_prompts; i++) {
1185 prompt = packet_get_string(NULL);
1186 echo = packet_get_char();
1188 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1190 packet_put_cstring(response);
1191 memset(response, 0, strlen(response));
1195 packet_check_eom(); /* done with parsing incoming message. */
1197 packet_add_padding(64);
1202 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1203 u_char *data, u_int datalen)
1208 int to[2], from[2], status, version = 2;
1210 debug2("ssh_keysign called");
1212 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1213 error("ssh_keysign: no installed: %s", strerror(errno));
1216 if (fflush(stdout) != 0)
1217 error("ssh_keysign: fflush: %s", strerror(errno));
1219 error("ssh_keysign: pipe: %s", strerror(errno));
1222 if (pipe(from) < 0) {
1223 error("ssh_keysign: pipe: %s", strerror(errno));
1226 if ((pid = fork()) < 0) {
1227 error("ssh_keysign: fork: %s", strerror(errno));
1234 if (dup2(from[1], STDOUT_FILENO) < 0)
1235 fatal("ssh_keysign: dup2: %s", strerror(errno));
1237 if (dup2(to[0], STDIN_FILENO) < 0)
1238 fatal("ssh_keysign: dup2: %s", strerror(errno));
1241 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1242 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1249 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1250 buffer_put_string(&b, data, datalen);
1251 ssh_msg_send(to[1], version, &b);
1253 if (ssh_msg_recv(from[0], &b) < 0) {
1254 error("ssh_keysign: no reply");
1261 while (waitpid(pid, &status, 0) < 0)
1265 if (buffer_get_char(&b) != version) {
1266 error("ssh_keysign: bad version");
1270 *sigp = buffer_get_string(&b, lenp);
1277 userauth_hostbased(Authctxt *authctxt)
1279 Key *private = NULL;
1280 Sensitive *sensitive = authctxt->sensitive;
1282 u_char *signature, *blob;
1283 char *chost, *pkalg, *p;
1284 const char *service;
1286 int ok, i, len, found = 0;
1288 /* check for a useful key */
1289 for (i = 0; i < sensitive->nkeys; i++) {
1290 private = sensitive->keys[i];
1291 if (private && private->type != KEY_RSA1) {
1293 /* we take and free the key */
1294 sensitive->keys[i] = NULL;
1299 debug("No more client hostkeys for hostbased authentication.");
1302 if (key_to_blob(private, &blob, &blen) == 0) {
1306 /* figure out a name for the client host */
1307 p = get_local_name(packet_get_connection_in());
1309 error("userauth_hostbased: cannot get local ipaddr/name");
1313 len = strlen(p) + 2;
1314 chost = xmalloc(len);
1315 strlcpy(chost, p, len);
1316 strlcat(chost, ".", len);
1317 debug2("userauth_hostbased: chost %s", chost);
1320 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1322 pkalg = xstrdup(key_ssh_name(private));
1324 /* construct data */
1325 buffer_put_string(&b, session_id2, session_id2_len);
1326 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1327 buffer_put_cstring(&b, authctxt->server_user);
1328 buffer_put_cstring(&b, service);
1329 buffer_put_cstring(&b, authctxt->method->name);
1330 buffer_put_cstring(&b, pkalg);
1331 buffer_put_string(&b, blob, blen);
1332 buffer_put_cstring(&b, chost);
1333 buffer_put_cstring(&b, authctxt->local_user);
1337 if (sensitive->external_keysign)
1338 ok = ssh_keysign(private, &signature, &slen,
1339 buffer_ptr(&b), buffer_len(&b));
1341 ok = key_sign(private, &signature, &slen,
1342 buffer_ptr(&b), buffer_len(&b));
1346 error("key_sign failed");
1351 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1352 packet_put_cstring(authctxt->server_user);
1353 packet_put_cstring(authctxt->service);
1354 packet_put_cstring(authctxt->method->name);
1355 packet_put_cstring(pkalg);
1356 packet_put_string(blob, blen);
1357 packet_put_cstring(chost);
1358 packet_put_cstring(authctxt->local_user);
1359 packet_put_string(signature, slen);
1360 memset(signature, 's', slen);
1369 /* find auth method */
1372 * given auth method name, if configurable options permit this method fill
1373 * in auth_ident field and return true, otherwise return false.
1376 authmethod_is_enabled(Authmethod *method)
1380 /* return false if options indicate this method is disabled */
1381 if (method->enabled == NULL || *method->enabled == 0)
1383 /* return false if batch mode is enabled but method needs interactive mode */
1384 if (method->batch_flag != NULL && *method->batch_flag != 0)
1390 authmethod_lookup(const char *name)
1392 Authmethod *method = NULL;
1394 for (method = authmethods; method->name != NULL; method++)
1395 if (strcmp(name, method->name) == 0)
1397 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1401 /* XXX internal state */
1402 static Authmethod *current = NULL;
1403 static char *supported = NULL;
1404 static char *preferred = NULL;
1407 * Given the authentication method list sent by the server, return the
1408 * next method we should try. If the server initially sends a nil list,
1409 * use a built-in default list.
1412 authmethod_get(char *authlist)
1417 /* Use a suitable default if we're passed a nil list. */
1418 if (authlist == NULL || strlen(authlist) == 0)
1419 authlist = options.preferred_authentications;
1421 if (supported == NULL || strcmp(authlist, supported) != 0) {
1422 debug3("start over, passed a different list %s", authlist);
1423 if (supported != NULL)
1425 supported = xstrdup(authlist);
1426 preferred = options.preferred_authentications;
1427 debug3("preferred %s", preferred);
1429 } else if (current != NULL && authmethod_is_enabled(current))
1433 if ((name = match_list(preferred, supported, &next)) == NULL) {
1434 debug("No more authentication methods to try.");
1439 debug3("authmethod_lookup %s", name);
1440 debug3("remaining preferred: %s", preferred);
1441 if ((current = authmethod_lookup(name)) != NULL &&
1442 authmethod_is_enabled(current)) {
1443 debug3("authmethod_is_enabled %s", name);
1444 debug("Next authentication method: %s", name);
1451 authmethods_get(void)
1453 Authmethod *method = NULL;
1458 for (method = authmethods; method->name != NULL; method++) {
1459 if (authmethod_is_enabled(method)) {
1460 if (buffer_len(&b) > 0)
1461 buffer_append(&b, ",", 1);
1462 buffer_append(&b, method->name, strlen(method->name));
1465 buffer_append(&b, "\0", 1);
1466 list = xstrdup(buffer_ptr(&b));