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.124 2003/08/25 10:33:33 djm 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) {
97 /* Add the GSSAPI mechanisms currently supported on this client to
98 * the key exchange algorithm proposal */
99 orig = myproposal[PROPOSAL_KEX_ALGS];
100 canonhost = xstrdup(get_canonical_hostname(1));
101 resolve_localhost(&canonhost);
102 gss = ssh_gssapi_client_mechanisms(canonhost);
106 len = strlen(orig)+strlen(gss)+2;
107 myproposal[PROPOSAL_KEX_ALGS]=xmalloc(len);
108 snprintf(myproposal[PROPOSAL_KEX_ALGS],len,"%s,%s",gss,orig);
113 if (options.ciphers == (char *)-1) {
114 logit("No valid ciphers for protocol version 2 given, using defaults.");
115 options.ciphers = NULL;
117 if (options.ciphers != NULL) {
118 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
119 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
121 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
122 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
123 myproposal[PROPOSAL_ENC_ALGS_STOC] =
124 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
125 if (options.compression) {
126 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
127 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
129 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
130 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
132 if (options.macs != NULL) {
133 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
134 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
136 if (options.hostkeyalgorithms != NULL)
137 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
138 options.hostkeyalgorithms;
141 /* If we've got GSSAPI algorithms, then we also support the
142 * 'null' hostkey, as a last resort */
143 if (options.gss_keyex && gss) {
144 orig=myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
145 len = strlen(orig)+sizeof(",null");
146 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]=xmalloc(len);
147 snprintf(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],len,"%s,null",orig);
151 if (options.rekey_limit)
152 packet_set_rekey_limit(options.rekey_limit);
154 /* start key exchange */
155 kex = kex_setup(myproposal);
156 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
157 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
159 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
161 kex->client_version_string=client_version_string;
162 kex->server_version_string=server_version_string;
163 kex->verify_host_key=&verify_host_key_callback;
165 kex->options.gss_deleg_creds=options.gss_deleg_creds;
170 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
172 session_id2 = kex->session_id;
173 session_id2_len = kex->session_id_len;
176 /* send 1st encrypted/maced/compressed message */
177 packet_start(SSH2_MSG_IGNORE);
178 packet_put_cstring("markus");
188 typedef struct Authctxt Authctxt;
189 typedef struct Authmethod Authmethod;
190 typedef struct identity Identity;
191 typedef struct idlist Idlist;
194 TAILQ_ENTRY(identity) next;
195 AuthenticationConnection *ac; /* set if agent supports key */
196 Key *key; /* public/private key */
197 char *filename; /* comment for agent-only keys */
199 int isprivate; /* key points to the private key */
201 TAILQ_HEAD(idlist, identity);
204 const char *server_user;
205 const char *local_user;
213 AuthenticationConnection *agent;
215 Sensitive *sensitive;
216 /* kbd-interactive */
222 char *name; /* string to compare against server's list */
223 int (*userauth)(Authctxt *authctxt);
224 int *enabled; /* flag in option struct that enables method */
225 int *batch_flag; /* flag in option struct that disables method */
228 void input_userauth_success(int, u_int32_t, void *);
229 void input_userauth_failure(int, u_int32_t, void *);
230 void input_userauth_banner(int, u_int32_t, void *);
231 void input_userauth_error(int, u_int32_t, void *);
232 void input_userauth_info_req(int, u_int32_t, void *);
233 void input_userauth_pk_ok(int, u_int32_t, void *);
234 void input_userauth_passwd_changereq(int, u_int32_t, void *);
236 int userauth_none(Authctxt *);
237 int userauth_pubkey(Authctxt *);
238 int userauth_passwd(Authctxt *);
239 int userauth_kbdint(Authctxt *);
240 int userauth_hostbased(Authctxt *);
241 int userauth_kerberos(Authctxt *);
244 int userauth_gssapi(Authctxt *authctxt);
245 void input_gssapi_response(int type, u_int32_t, void *);
246 void input_gssapi_token(int type, u_int32_t, void *);
247 void input_gssapi_hash(int type, u_int32_t, void *);
248 void input_gssapi_error(int, u_int32_t, void *);
249 void input_gssapi_errtok(int, u_int32_t, void *);
253 int userauth_external(Authctxt *authctxt);
254 int userauth_gssapi(Authctxt *authctxt);
255 void input_gssapi_response(int type, u_int32_t, void *);
256 void input_gssapi_token(int type, u_int32_t, void *);
257 void input_gssapi_hash(int type, u_int32_t, void *);
258 void input_gssapi_error(int, u_int32_t, void *);
259 void input_gssapi_errtok(int, u_int32_t, void *);
262 void userauth(Authctxt *, char *);
264 static int sign_and_send_pubkey(Authctxt *, Identity *);
265 static void pubkey_prepare(Authctxt *);
266 static void pubkey_cleanup(Authctxt *);
267 static Key *load_identity_file(char *);
269 static Authmethod *authmethod_get(char *authlist);
270 static Authmethod *authmethod_lookup(const char *name);
271 static char *authmethods_get(void);
273 Authmethod authmethods[] = {
277 &options.gss_authentication,
281 &options.gss_authentication,
286 &options.hostbased_authentication,
290 &options.pubkey_authentication,
292 {"keyboard-interactive",
294 &options.kbd_interactive_authentication,
295 &options.batch_mode},
298 &options.password_authentication,
299 &options.batch_mode},
304 {NULL, NULL, NULL, NULL}
308 ssh_userauth2(const char *local_user, const char *server_user, char *host,
309 Sensitive *sensitive)
314 if (options.challenge_response_authentication)
315 options.kbd_interactive_authentication = 1;
317 packet_start(SSH2_MSG_SERVICE_REQUEST);
318 packet_put_cstring("ssh-userauth");
320 debug("SSH2_MSG_SERVICE_REQUEST sent");
322 type = packet_read();
323 if (type != SSH2_MSG_SERVICE_ACCEPT)
324 fatal("Server denied authentication request: %d", type);
325 if (packet_remaining() > 0) {
326 char *reply = packet_get_string(NULL);
327 debug2("service_accept: %s", reply);
330 debug2("buggy server: service_accept w/o service");
333 debug("SSH2_MSG_SERVICE_ACCEPT received");
335 if (options.preferred_authentications == NULL)
336 options.preferred_authentications = authmethods_get();
338 /* setup authentication context */
339 memset(&authctxt, 0, sizeof(authctxt));
340 pubkey_prepare(&authctxt);
341 authctxt.server_user = server_user;
342 authctxt.local_user = local_user;
343 authctxt.host = host;
344 authctxt.service = "ssh-connection"; /* service name */
345 authctxt.success = 0;
346 authctxt.method = authmethod_lookup("none");
347 authctxt.authlist = NULL;
348 authctxt.methoddata = NULL;
349 authctxt.sensitive = sensitive;
350 authctxt.info_req_seen = 0;
351 if (authctxt.method == NULL)
352 fatal("ssh_userauth2: internal error: cannot send userauth none request");
354 /* initial userauth request */
355 userauth_none(&authctxt);
357 dispatch_init(&input_userauth_error);
358 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
359 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
360 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
361 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
363 pubkey_cleanup(&authctxt);
364 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
366 debug("Authentication succeeded (%s).", authctxt.method->name);
370 userauth(Authctxt *authctxt, char *authlist)
372 if (authctxt->methoddata) {
373 xfree(authctxt->methoddata);
374 authctxt->methoddata = NULL;
376 if (authlist == NULL) {
377 authlist = authctxt->authlist;
379 if (authctxt->authlist)
380 xfree(authctxt->authlist);
381 authctxt->authlist = authlist;
384 Authmethod *method = authmethod_get(authlist);
386 fatal("Permission denied (%s).", authlist);
387 authctxt->method = method;
389 /* reset the per method handler */
390 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
391 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
393 /* and try new method */
394 if (method->userauth(authctxt) != 0) {
395 debug2("we sent a %s packet, wait for reply", method->name);
398 debug2("we did not send a packet, disable method");
399 method->enabled = NULL;
405 input_userauth_error(int type, u_int32_t seq, void *ctxt)
407 fatal("input_userauth_error: bad message during authentication: "
412 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
415 debug3("input_userauth_banner");
416 msg = packet_get_string(NULL);
417 lang = packet_get_string(NULL);
424 input_userauth_success(int type, u_int32_t seq, void *ctxt)
426 Authctxt *authctxt = ctxt;
427 if (authctxt == NULL)
428 fatal("input_userauth_success: no authentication context");
429 if (authctxt->authlist)
430 xfree(authctxt->authlist);
431 if (authctxt->methoddata)
432 xfree(authctxt->methoddata);
433 authctxt->success = 1; /* break out */
437 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
439 Authctxt *authctxt = ctxt;
440 char *authlist = NULL;
443 if (authctxt == NULL)
444 fatal("input_userauth_failure: no authentication context");
446 authlist = packet_get_string(NULL);
447 partial = packet_get_char();
451 logit("Authenticated with partial success.");
452 debug("Authentications that can continue: %s", authlist);
454 userauth(authctxt, authlist);
457 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
459 Authctxt *authctxt = ctxt;
463 int pktype, sent = 0;
468 if (authctxt == NULL)
469 fatal("input_userauth_pk_ok: no authentication context");
470 if (datafellows & SSH_BUG_PKOK) {
471 /* this is similar to SSH_BUG_PKAUTH */
472 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
473 pkblob = packet_get_string(&blen);
475 buffer_append(&b, pkblob, blen);
476 pkalg = buffer_get_string(&b, &alen);
479 pkalg = packet_get_string(&alen);
480 pkblob = packet_get_string(&blen);
484 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
486 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
487 debug("unknown pkalg %s", pkalg);
490 if ((key = key_from_blob(pkblob, blen)) == NULL) {
491 debug("no key from blob. pkalg %s", pkalg);
494 if (key->type != pktype) {
495 error("input_userauth_pk_ok: type mismatch "
496 "for decoded key (received %d, expected %d)",
500 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
501 debug2("input_userauth_pk_ok: fp %s", fp);
504 TAILQ_FOREACH(id, &authctxt->keys, next) {
505 if (key_equal(key, id->key)) {
506 sent = sign_and_send_pubkey(authctxt, id);
516 /* try another method if we did not send a packet */
518 userauth(authctxt, NULL);
523 userauth_gssapi(Authctxt *authctxt)
525 Gssctxt *gssctxt = NULL;
526 static gss_OID_set supported = NULL;
531 if (!options.gss_authentication) {
532 verbose("GSSAPI authentication disabled.");
536 /* Try one GSSAPI method at a time, rather than sending them all at
539 if (supported == NULL)
540 gss_indicate_mechs(&min, &supported);
542 /* Check to see if the mechanism is usable before we offer it */
543 while (mech<supported->count && !ok) {
545 ssh_gssapi_delete_ctx(&gssctxt);
546 ssh_gssapi_build_ctx(&gssctxt);
547 ssh_gssapi_set_oid(gssctxt, &supported->elements[mech]);
549 /* My DER encoding requires length<128 */
550 if (supported->elements[mech].length < 128 &&
551 !GSS_ERROR(ssh_gssapi_import_name(gssctxt,
553 ok = 1; /* Mechanism works */
561 authctxt->methoddata=(void *)gssctxt;
563 packet_start(SSH2_MSG_USERAUTH_REQUEST);
564 packet_put_cstring(authctxt->server_user);
565 packet_put_cstring(authctxt->service);
566 packet_put_cstring(authctxt->method->name);
570 /* Some servers encode the OID incorrectly (as we used to) */
571 if (datafellows & SSH_BUG_GSSAPI_BER) {
572 packet_put_string(supported->elements[mech].elements,
573 supported->elements[mech].length);
575 packet_put_int((supported->elements[mech].length)+2);
576 packet_put_char(SSH_GSS_OIDTYPE);
577 packet_put_char(supported->elements[mech].length);
578 packet_put_raw(supported->elements[mech].elements,
579 supported->elements[mech].length);
584 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
585 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
586 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
587 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
589 mech++; /* Move along to next candidate */
595 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
597 Authctxt *authctxt = ctxt;
599 OM_uint32 status, ms;
602 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
604 if (authctxt == NULL)
605 fatal("input_gssapi_response: no authentication context");
606 gssctxt = authctxt->methoddata;
609 oidv = packet_get_string(&oidlen);
611 if (datafellows & SSH_BUG_GSSAPI_BER) {
612 if (!ssh_gssapi_check_oid(gssctxt, oidv, oidlen))
613 fatal("Server returned different OID than expected");
615 if(oidv[0] != SSH_GSS_OIDTYPE || oidv[1] != oidlen-2) {
616 debug("Badly encoded mechanism OID received");
617 userauth(authctxt, NULL);
621 if (!ssh_gssapi_check_oid(gssctxt, oidv+2, oidlen-2))
622 fatal("Server returned different OID than expected");
629 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
630 GSS_C_NO_BUFFER, &send_tok, NULL);
631 if (GSS_ERROR(status)) {
632 if (send_tok.length > 0) {
633 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
634 packet_put_string(send_tok.value, send_tok.length);
636 gss_release_buffer(&ms, &send_tok);
638 /* Start again with next method on list */
639 debug("Trying to start again");
640 userauth(authctxt, NULL);
644 /* We must have data to send */
645 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
646 packet_put_string(send_tok.value, send_tok.length);
648 gss_release_buffer(&ms, &send_tok);
652 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
654 Authctxt *authctxt = ctxt;
656 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
657 gss_buffer_desc recv_tok;
658 OM_uint32 status, ms;
661 if (authctxt == NULL)
662 fatal("input_gssapi_response: no authentication context");
663 gssctxt = authctxt->methoddata;
665 recv_tok.value = packet_get_string(&slen);
666 recv_tok.length = slen; /* safe typecast */
670 status=ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
671 &recv_tok, &send_tok, NULL);
673 xfree(recv_tok.value);
675 if (GSS_ERROR(status)) {
676 if (send_tok.length > 0) {
677 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
678 packet_put_string(send_tok.value, send_tok.length);
680 gss_release_buffer(&ms, &send_tok);
682 /* Start again with the next method in the list */
683 userauth(authctxt, NULL);
687 if (send_tok.length > 0) {
688 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
689 packet_put_string(send_tok.value, send_tok.length);
691 gss_release_buffer(&ms, &send_tok);
694 if (status == GSS_S_COMPLETE) {
695 /* If that succeeded, send a exchange complete message */
696 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
702 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
704 Authctxt *authctxt = ctxt;
706 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
707 gss_buffer_desc recv_tok;
708 OM_uint32 status, ms;
711 if (authctxt == NULL)
712 fatal("input_gssapi_response: no authentication context");
713 gssctxt = authctxt->methoddata;
715 recv_tok.value = packet_get_string(&len);
716 recv_tok.length = len;
720 /* Stick it into GSSAPI and see what it says */
721 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
722 &recv_tok, &send_tok, NULL);
724 xfree(recv_tok.value);
725 gss_release_buffer(&ms, &send_tok);
727 /* Server will be returning a failed packet after this one */
731 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
737 maj=packet_get_int();
738 min=packet_get_int();
739 msg=packet_get_string(NULL);
740 lang=packet_get_string(NULL);
744 debug("Server GSSAPI Error:\n%s\n", msg);
750 userauth_external(Authctxt *authctxt)
752 static int attempt = 0;
757 /* The client MUST NOT try this method if initial key exchange
758 was not performed using a GSSAPI-based key exchange
760 if (xxx_kex->kex_type != KEX_GSS_GRP1_SHA1) {
761 debug2("gsskex not performed, skipping external-keyx");
765 debug2("userauth_external");
766 packet_start(SSH2_MSG_USERAUTH_REQUEST);
768 if(options.implicit && !(datafellows & SSH_BUG_GSS_EMPTYUSER)) {
769 packet_put_cstring("");
772 packet_put_cstring(authctxt->server_user);
776 packet_put_cstring(authctxt->service);
777 packet_put_cstring(authctxt->method->name);
785 userauth_none(Authctxt *authctxt)
787 /* initial userauth request */
788 packet_start(SSH2_MSG_USERAUTH_REQUEST);
789 packet_put_cstring(authctxt->server_user);
790 packet_put_cstring(authctxt->service);
791 packet_put_cstring(authctxt->method->name);
797 userauth_passwd(Authctxt *authctxt)
799 static int attempt = 0;
803 if (attempt++ >= options.number_of_password_prompts)
807 error("Permission denied, please try again.");
809 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
810 authctxt->server_user, authctxt->host);
811 password = read_passphrase(prompt, 0);
812 packet_start(SSH2_MSG_USERAUTH_REQUEST);
813 packet_put_cstring(authctxt->server_user);
814 packet_put_cstring(authctxt->service);
815 packet_put_cstring(authctxt->method->name);
817 packet_put_cstring(password);
818 memset(password, 0, strlen(password));
820 packet_add_padding(64);
823 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
824 &input_userauth_passwd_changereq);
829 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
832 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
834 Authctxt *authctxt = ctxt;
835 char *info, *lang, *password = NULL, *retype = NULL;
838 debug2("input_userauth_passwd_changereq");
840 if (authctxt == NULL)
841 fatal("input_userauth_passwd_changereq: "
842 "no authentication context");
844 info = packet_get_string(NULL);
845 lang = packet_get_string(NULL);
846 if (strlen(info) > 0)
850 packet_start(SSH2_MSG_USERAUTH_REQUEST);
851 packet_put_cstring(authctxt->server_user);
852 packet_put_cstring(authctxt->service);
853 packet_put_cstring(authctxt->method->name);
854 packet_put_char(1); /* additional info */
855 snprintf(prompt, sizeof(prompt),
856 "Enter %.30s@%.128s's old password: ",
857 authctxt->server_user, authctxt->host);
858 password = read_passphrase(prompt, 0);
859 packet_put_cstring(password);
860 memset(password, 0, strlen(password));
863 while (password == NULL) {
864 snprintf(prompt, sizeof(prompt),
865 "Enter %.30s@%.128s's new password: ",
866 authctxt->server_user, authctxt->host);
867 password = read_passphrase(prompt, RP_ALLOW_EOF);
868 if (password == NULL) {
872 snprintf(prompt, sizeof(prompt),
873 "Retype %.30s@%.128s's new password: ",
874 authctxt->server_user, authctxt->host);
875 retype = read_passphrase(prompt, 0);
876 if (strcmp(password, retype) != 0) {
877 memset(password, 0, strlen(password));
879 logit("Mismatch; try again, EOF to quit.");
882 memset(retype, 0, strlen(retype));
885 packet_put_cstring(password);
886 memset(password, 0, strlen(password));
888 packet_add_padding(64);
891 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
892 &input_userauth_passwd_changereq);
896 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
897 u_char *data, u_int datalen)
902 /* the agent supports this key */
904 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
907 * we have already loaded the private key or
908 * the private key is stored in external hardware
910 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
911 return (key_sign(id->key, sigp, lenp, data, datalen));
912 /* load the private key from the file */
913 if ((prv = load_identity_file(id->filename)) == NULL)
915 ret = key_sign(prv, sigp, lenp, data, datalen);
921 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
924 u_char *blob, *signature;
930 debug3("sign_and_send_pubkey");
932 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
933 /* we cannot handle this key */
934 debug3("sign_and_send_pubkey: cannot handle key");
937 /* data to be signed */
939 if (datafellows & SSH_OLD_SESSIONID) {
940 buffer_append(&b, session_id2, session_id2_len);
941 skip = session_id2_len;
943 buffer_put_string(&b, session_id2, session_id2_len);
944 skip = buffer_len(&b);
946 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
947 buffer_put_cstring(&b, authctxt->server_user);
948 buffer_put_cstring(&b,
949 datafellows & SSH_BUG_PKSERVICE ?
952 if (datafellows & SSH_BUG_PKAUTH) {
953 buffer_put_char(&b, have_sig);
955 buffer_put_cstring(&b, authctxt->method->name);
956 buffer_put_char(&b, have_sig);
957 buffer_put_cstring(&b, key_ssh_name(id->key));
959 buffer_put_string(&b, blob, bloblen);
961 /* generate signature */
962 ret = identity_sign(id, &signature, &slen,
963 buffer_ptr(&b), buffer_len(&b));
972 if (datafellows & SSH_BUG_PKSERVICE) {
974 buffer_append(&b, session_id2, session_id2_len);
975 skip = session_id2_len;
976 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
977 buffer_put_cstring(&b, authctxt->server_user);
978 buffer_put_cstring(&b, authctxt->service);
979 buffer_put_cstring(&b, authctxt->method->name);
980 buffer_put_char(&b, have_sig);
981 if (!(datafellows & SSH_BUG_PKAUTH))
982 buffer_put_cstring(&b, key_ssh_name(id->key));
983 buffer_put_string(&b, blob, bloblen);
987 /* append signature */
988 buffer_put_string(&b, signature, slen);
991 /* skip session id and packet type */
992 if (buffer_len(&b) < skip + 1)
993 fatal("userauth_pubkey: internal error");
994 buffer_consume(&b, skip + 1);
996 /* put remaining data from buffer into packet */
997 packet_start(SSH2_MSG_USERAUTH_REQUEST);
998 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1006 send_pubkey_test(Authctxt *authctxt, Identity *id)
1009 u_int bloblen, have_sig = 0;
1011 debug3("send_pubkey_test");
1013 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1014 /* we cannot handle this key */
1015 debug3("send_pubkey_test: cannot handle key");
1018 /* register callback for USERAUTH_PK_OK message */
1019 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1021 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1022 packet_put_cstring(authctxt->server_user);
1023 packet_put_cstring(authctxt->service);
1024 packet_put_cstring(authctxt->method->name);
1025 packet_put_char(have_sig);
1026 if (!(datafellows & SSH_BUG_PKAUTH))
1027 packet_put_cstring(key_ssh_name(id->key));
1028 packet_put_string(blob, bloblen);
1035 load_identity_file(char *filename)
1038 char prompt[300], *passphrase;
1042 if (stat(filename, &st) < 0) {
1043 debug3("no such identity: %s", filename);
1046 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
1047 if (private == NULL) {
1048 if (options.batch_mode)
1050 snprintf(prompt, sizeof prompt,
1051 "Enter passphrase for key '%.100s': ", filename);
1052 for (i = 0; i < options.number_of_password_prompts; i++) {
1053 passphrase = read_passphrase(prompt, 0);
1054 if (strcmp(passphrase, "") != 0) {
1055 private = key_load_private_type(KEY_UNSPEC, filename,
1059 debug2("no passphrase given, try next key");
1062 memset(passphrase, 0, strlen(passphrase));
1064 if (private != NULL || quit)
1066 debug2("bad passphrase given, try again...");
1073 * try keys in the following order:
1074 * 1. agent keys that are found in the config file
1075 * 2. other agent keys
1076 * 3. keys that are only listed in the config file
1079 pubkey_prepare(Authctxt *authctxt)
1082 Idlist agent, files, *preferred;
1084 AuthenticationConnection *ac;
1088 TAILQ_INIT(&agent); /* keys from the agent */
1089 TAILQ_INIT(&files); /* keys from the config file */
1090 preferred = &authctxt->keys;
1091 TAILQ_INIT(preferred); /* preferred order of keys */
1093 /* list of keys stored in the filesystem */
1094 for (i = 0; i < options.num_identity_files; i++) {
1095 key = options.identity_keys[i];
1096 if (key && key->type == KEY_RSA1)
1098 options.identity_keys[i] = NULL;
1099 id = xmalloc(sizeof(*id));
1100 memset(id, 0, sizeof(*id));
1102 id->filename = xstrdup(options.identity_files[i]);
1103 TAILQ_INSERT_TAIL(&files, id, next);
1105 /* list of keys supported by the agent */
1106 if ((ac = ssh_get_authentication_connection())) {
1107 for (key = ssh_get_first_identity(ac, &comment, 2);
1109 key = ssh_get_next_identity(ac, &comment, 2)) {
1111 TAILQ_FOREACH(id, &files, next) {
1112 /* agent keys from the config file are preferred */
1113 if (key_equal(key, id->key)) {
1116 TAILQ_REMOVE(&files, id, next);
1117 TAILQ_INSERT_TAIL(preferred, id, next);
1124 id = xmalloc(sizeof(*id));
1125 memset(id, 0, sizeof(*id));
1127 id->filename = comment;
1129 TAILQ_INSERT_TAIL(&agent, id, next);
1132 /* append remaining agent keys */
1133 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1134 TAILQ_REMOVE(&agent, id, next);
1135 TAILQ_INSERT_TAIL(preferred, id, next);
1137 authctxt->agent = ac;
1139 /* append remaining keys from the config file */
1140 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1141 TAILQ_REMOVE(&files, id, next);
1142 TAILQ_INSERT_TAIL(preferred, id, next);
1144 TAILQ_FOREACH(id, preferred, next) {
1145 debug2("key: %s (%p)", id->filename, id->key);
1150 pubkey_cleanup(Authctxt *authctxt)
1154 if (authctxt->agent != NULL)
1155 ssh_close_authentication_connection(authctxt->agent);
1156 for (id = TAILQ_FIRST(&authctxt->keys); id;
1157 id = TAILQ_FIRST(&authctxt->keys)) {
1158 TAILQ_REMOVE(&authctxt->keys, id, next);
1162 xfree(id->filename);
1168 userauth_pubkey(Authctxt *authctxt)
1173 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1176 TAILQ_REMOVE(&authctxt->keys, id, next);
1177 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1179 * send a test message if we have the public key. for
1180 * encrypted keys we cannot do this and have to load the
1181 * private key instead
1183 if (id->key && id->key->type != KEY_RSA1) {
1184 debug("Offering public key: %s", id->filename);
1185 sent = send_pubkey_test(authctxt, id);
1186 } else if (id->key == NULL) {
1187 debug("Trying private key: %s", id->filename);
1188 id->key = load_identity_file(id->filename);
1189 if (id->key != NULL) {
1191 sent = sign_and_send_pubkey(authctxt, id);
1203 * Send userauth request message specifying keyboard-interactive method.
1206 userauth_kbdint(Authctxt *authctxt)
1208 static int attempt = 0;
1210 if (attempt++ >= options.number_of_password_prompts)
1212 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1213 if (attempt > 1 && !authctxt->info_req_seen) {
1214 debug3("userauth_kbdint: disable: no info_req_seen");
1215 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1219 debug2("userauth_kbdint");
1220 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1221 packet_put_cstring(authctxt->server_user);
1222 packet_put_cstring(authctxt->service);
1223 packet_put_cstring(authctxt->method->name);
1224 packet_put_cstring(""); /* lang */
1225 packet_put_cstring(options.kbd_interactive_devices ?
1226 options.kbd_interactive_devices : "");
1229 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1234 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1237 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1239 Authctxt *authctxt = ctxt;
1240 char *name, *inst, *lang, *prompt, *response;
1241 u_int num_prompts, i;
1244 debug2("input_userauth_info_req");
1246 if (authctxt == NULL)
1247 fatal("input_userauth_info_req: no authentication context");
1249 authctxt->info_req_seen = 1;
1251 name = packet_get_string(NULL);
1252 inst = packet_get_string(NULL);
1253 lang = packet_get_string(NULL);
1254 if (strlen(name) > 0)
1256 if (strlen(inst) > 0)
1262 num_prompts = packet_get_int();
1264 * Begin to build info response packet based on prompts requested.
1265 * We commit to providing the correct number of responses, so if
1266 * further on we run into a problem that prevents this, we have to
1267 * be sure and clean this up and send a correct error response.
1269 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1270 packet_put_int(num_prompts);
1272 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1273 for (i = 0; i < num_prompts; i++) {
1274 prompt = packet_get_string(NULL);
1275 echo = packet_get_char();
1277 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1279 packet_put_cstring(response);
1280 memset(response, 0, strlen(response));
1284 packet_check_eom(); /* done with parsing incoming message. */
1286 packet_add_padding(64);
1291 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1292 u_char *data, u_int datalen)
1297 int to[2], from[2], status, version = 2;
1299 debug2("ssh_keysign called");
1301 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1302 error("ssh_keysign: no installed: %s", strerror(errno));
1305 if (fflush(stdout) != 0)
1306 error("ssh_keysign: fflush: %s", strerror(errno));
1308 error("ssh_keysign: pipe: %s", strerror(errno));
1311 if (pipe(from) < 0) {
1312 error("ssh_keysign: pipe: %s", strerror(errno));
1315 if ((pid = fork()) < 0) {
1316 error("ssh_keysign: fork: %s", strerror(errno));
1323 if (dup2(from[1], STDOUT_FILENO) < 0)
1324 fatal("ssh_keysign: dup2: %s", strerror(errno));
1326 if (dup2(to[0], STDIN_FILENO) < 0)
1327 fatal("ssh_keysign: dup2: %s", strerror(errno));
1330 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1331 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1338 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1339 buffer_put_string(&b, data, datalen);
1340 ssh_msg_send(to[1], version, &b);
1342 if (ssh_msg_recv(from[0], &b) < 0) {
1343 error("ssh_keysign: no reply");
1350 while (waitpid(pid, &status, 0) < 0)
1354 if (buffer_get_char(&b) != version) {
1355 error("ssh_keysign: bad version");
1359 *sigp = buffer_get_string(&b, lenp);
1366 userauth_hostbased(Authctxt *authctxt)
1368 Key *private = NULL;
1369 Sensitive *sensitive = authctxt->sensitive;
1371 u_char *signature, *blob;
1372 char *chost, *pkalg, *p;
1373 const char *service;
1375 int ok, i, len, found = 0;
1377 /* check for a useful key */
1378 for (i = 0; i < sensitive->nkeys; i++) {
1379 private = sensitive->keys[i];
1380 if (private && private->type != KEY_RSA1) {
1382 /* we take and free the key */
1383 sensitive->keys[i] = NULL;
1388 debug("No more client hostkeys for hostbased authentication.");
1391 if (key_to_blob(private, &blob, &blen) == 0) {
1395 /* figure out a name for the client host */
1396 p = get_local_name(packet_get_connection_in());
1398 error("userauth_hostbased: cannot get local ipaddr/name");
1402 len = strlen(p) + 2;
1403 chost = xmalloc(len);
1404 strlcpy(chost, p, len);
1405 strlcat(chost, ".", len);
1406 debug2("userauth_hostbased: chost %s", chost);
1409 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1411 pkalg = xstrdup(key_ssh_name(private));
1413 /* construct data */
1414 buffer_put_string(&b, session_id2, session_id2_len);
1415 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1416 buffer_put_cstring(&b, authctxt->server_user);
1417 buffer_put_cstring(&b, service);
1418 buffer_put_cstring(&b, authctxt->method->name);
1419 buffer_put_cstring(&b, pkalg);
1420 buffer_put_string(&b, blob, blen);
1421 buffer_put_cstring(&b, chost);
1422 buffer_put_cstring(&b, authctxt->local_user);
1426 if (sensitive->external_keysign)
1427 ok = ssh_keysign(private, &signature, &slen,
1428 buffer_ptr(&b), buffer_len(&b));
1430 ok = key_sign(private, &signature, &slen,
1431 buffer_ptr(&b), buffer_len(&b));
1435 error("key_sign failed");
1440 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1441 packet_put_cstring(authctxt->server_user);
1442 packet_put_cstring(authctxt->service);
1443 packet_put_cstring(authctxt->method->name);
1444 packet_put_cstring(pkalg);
1445 packet_put_string(blob, blen);
1446 packet_put_cstring(chost);
1447 packet_put_cstring(authctxt->local_user);
1448 packet_put_string(signature, slen);
1449 memset(signature, 's', slen);
1458 /* find auth method */
1461 * given auth method name, if configurable options permit this method fill
1462 * in auth_ident field and return true, otherwise return false.
1465 authmethod_is_enabled(Authmethod *method)
1469 /* return false if options indicate this method is disabled */
1470 if (method->enabled == NULL || *method->enabled == 0)
1472 /* return false if batch mode is enabled but method needs interactive mode */
1473 if (method->batch_flag != NULL && *method->batch_flag != 0)
1479 authmethod_lookup(const char *name)
1481 Authmethod *method = NULL;
1483 for (method = authmethods; method->name != NULL; method++)
1484 if (strcmp(name, method->name) == 0)
1486 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1490 /* XXX internal state */
1491 static Authmethod *current = NULL;
1492 static char *supported = NULL;
1493 static char *preferred = NULL;
1496 * Given the authentication method list sent by the server, return the
1497 * next method we should try. If the server initially sends a nil list,
1498 * use a built-in default list.
1501 authmethod_get(char *authlist)
1506 /* Use a suitable default if we're passed a nil list. */
1507 if (authlist == NULL || strlen(authlist) == 0)
1508 authlist = options.preferred_authentications;
1510 if (supported == NULL || strcmp(authlist, supported) != 0) {
1511 debug3("start over, passed a different list %s", authlist);
1512 if (supported != NULL)
1514 supported = xstrdup(authlist);
1515 preferred = options.preferred_authentications;
1516 debug3("preferred %s", preferred);
1518 } else if (current != NULL && authmethod_is_enabled(current))
1522 if ((name = match_list(preferred, supported, &next)) == NULL) {
1523 debug("No more authentication methods to try.");
1528 debug3("authmethod_lookup %s", name);
1529 debug3("remaining preferred: %s", preferred);
1530 if ((current = authmethod_lookup(name)) != NULL &&
1531 authmethod_is_enabled(current)) {
1532 debug3("authmethod_is_enabled %s", name);
1533 debug("Next authentication method: %s", name);
1540 authmethods_get(void)
1542 Authmethod *method = NULL;
1547 for (method = authmethods; method->name != NULL; method++) {
1548 if (authmethod_is_enabled(method)) {
1549 if (buffer_len(&b) > 0)
1550 buffer_append(&b, ",", 1);
1551 buffer_append(&b, method->name, strlen(method->name));
1554 buffer_append(&b, "\0", 1);
1555 list = xstrdup(buffer_ptr(&b));