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 Authmethod authmethods[] = {
274 &options.gss_authentication,
278 &options.gss_authentication,
282 &options.gss_authentication,
287 &options.hostbased_authentication,
291 &options.pubkey_authentication,
293 {"keyboard-interactive",
295 &options.kbd_interactive_authentication,
296 &options.batch_mode},
299 &options.password_authentication,
300 &options.batch_mode},
305 {NULL, NULL, NULL, NULL}
309 ssh_userauth2(const char *local_user, const char *server_user, char *host,
310 Sensitive *sensitive)
315 if (options.challenge_response_authentication)
316 options.kbd_interactive_authentication = 1;
318 packet_start(SSH2_MSG_SERVICE_REQUEST);
319 packet_put_cstring("ssh-userauth");
321 debug("SSH2_MSG_SERVICE_REQUEST sent");
323 type = packet_read();
324 if (type != SSH2_MSG_SERVICE_ACCEPT)
325 fatal("Server denied authentication request: %d", type);
326 if (packet_remaining() > 0) {
327 char *reply = packet_get_string(NULL);
328 debug2("service_accept: %s", reply);
331 debug2("buggy server: service_accept w/o service");
334 debug("SSH2_MSG_SERVICE_ACCEPT received");
336 if (options.preferred_authentications == NULL)
337 options.preferred_authentications = authmethods_get();
339 /* setup authentication context */
340 memset(&authctxt, 0, sizeof(authctxt));
341 pubkey_prepare(&authctxt);
342 authctxt.server_user = server_user;
343 authctxt.local_user = local_user;
344 authctxt.host = host;
345 authctxt.service = "ssh-connection"; /* service name */
346 authctxt.success = 0;
347 authctxt.method = authmethod_lookup("none");
348 authctxt.authlist = NULL;
349 authctxt.methoddata = NULL;
350 authctxt.sensitive = sensitive;
351 authctxt.info_req_seen = 0;
352 if (authctxt.method == NULL)
353 fatal("ssh_userauth2: internal error: cannot send userauth none request");
355 /* initial userauth request */
356 userauth_none(&authctxt);
358 dispatch_init(&input_userauth_error);
359 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
360 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
361 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
362 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
364 pubkey_cleanup(&authctxt);
365 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
367 debug("Authentication succeeded (%s).", authctxt.method->name);
371 userauth(Authctxt *authctxt, char *authlist)
373 if (authctxt->methoddata) {
374 xfree(authctxt->methoddata);
375 authctxt->methoddata = NULL;
377 if (authlist == NULL) {
378 authlist = authctxt->authlist;
380 if (authctxt->authlist)
381 xfree(authctxt->authlist);
382 authctxt->authlist = authlist;
385 Authmethod *method = authmethod_get(authlist);
387 fatal("Permission denied (%s).", authlist);
388 authctxt->method = method;
390 /* reset the per method handler */
391 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
392 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
394 /* and try new method */
395 if (method->userauth(authctxt) != 0) {
396 debug2("we sent a %s packet, wait for reply", method->name);
399 debug2("we did not send a packet, disable method");
400 method->enabled = NULL;
406 input_userauth_error(int type, u_int32_t seq, void *ctxt)
408 fatal("input_userauth_error: bad message during authentication: "
413 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
417 debug3("input_userauth_banner");
418 msg = packet_get_string(NULL);
419 lang = packet_get_string(NULL);
420 if (options.log_level > SYSLOG_LEVEL_QUIET)
421 fprintf(stderr, "%s", msg);
427 input_userauth_success(int type, u_int32_t seq, void *ctxt)
429 Authctxt *authctxt = ctxt;
430 if (authctxt == NULL)
431 fatal("input_userauth_success: no authentication context");
432 if (authctxt->authlist) {
433 xfree(authctxt->authlist);
434 authctxt->authlist = NULL;
436 if (authctxt->methoddata) {
437 xfree(authctxt->methoddata);
438 authctxt->methoddata = NULL;
440 authctxt->success = 1; /* break out */
444 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
446 Authctxt *authctxt = ctxt;
447 char *authlist = NULL;
450 if (authctxt == NULL)
451 fatal("input_userauth_failure: no authentication context");
453 authlist = packet_get_string(NULL);
454 partial = packet_get_char();
458 logit("Authenticated with partial success.");
459 debug("Authentications that can continue: %s", authlist);
461 userauth(authctxt, authlist);
464 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
466 Authctxt *authctxt = ctxt;
470 int pktype, sent = 0;
475 if (authctxt == NULL)
476 fatal("input_userauth_pk_ok: no authentication context");
477 if (datafellows & SSH_BUG_PKOK) {
478 /* this is similar to SSH_BUG_PKAUTH */
479 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
480 pkblob = packet_get_string(&blen);
482 buffer_append(&b, pkblob, blen);
483 pkalg = buffer_get_string(&b, &alen);
486 pkalg = packet_get_string(&alen);
487 pkblob = packet_get_string(&blen);
491 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
493 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
494 debug("unknown pkalg %s", pkalg);
497 if ((key = key_from_blob(pkblob, blen)) == NULL) {
498 debug("no key from blob. pkalg %s", pkalg);
501 if (key->type != pktype) {
502 error("input_userauth_pk_ok: type mismatch "
503 "for decoded key (received %d, expected %d)",
507 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
508 debug2("input_userauth_pk_ok: fp %s", fp);
512 * search keys in the reverse order, because last candidate has been
513 * moved to the end of the queue. this also avoids confusion by
516 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, next, idlist) {
517 if (key_equal(key, id->key)) {
518 sent = sign_and_send_pubkey(authctxt, id);
528 /* try another method if we did not send a packet */
530 userauth(authctxt, NULL);
535 userauth_gssapi(Authctxt *authctxt)
537 Gssctxt *gssctxt = NULL;
538 static gss_OID_set gss_supported = NULL;
543 if (!options.gss_authentication) {
544 verbose("GSSAPI authentication disabled.");
548 /* Try one GSSAPI method at a time, rather than sending them all at
551 if (gss_supported == NULL)
552 gss_indicate_mechs(&min, &gss_supported);
554 /* Check to see if the mechanism is usable before we offer it */
555 while (mech < gss_supported->count && !ok) {
557 ssh_gssapi_delete_ctx(&gssctxt);
558 ssh_gssapi_build_ctx(&gssctxt);
559 ssh_gssapi_set_oid(gssctxt, &gss_supported->elements[mech]);
561 /* My DER encoding requires length<128 */
562 if (gss_supported->elements[mech].length < 128 &&
563 !GSS_ERROR(ssh_gssapi_import_name(gssctxt,
565 ok = 1; /* Mechanism works */
573 authctxt->methoddata=(void *)gssctxt;
575 packet_start(SSH2_MSG_USERAUTH_REQUEST);
576 packet_put_cstring(authctxt->server_user);
577 packet_put_cstring(authctxt->service);
578 packet_put_cstring(authctxt->method->name);
582 /* Some servers encode the OID incorrectly (as we used to) */
583 if (datafellows & SSH_BUG_GSSAPI_BER) {
584 packet_put_string(gss_supported->elements[mech].elements,
585 gss_supported->elements[mech].length);
587 packet_put_int((gss_supported->elements[mech].length)+2);
588 packet_put_char(SSH_GSS_OIDTYPE);
589 packet_put_char(gss_supported->elements[mech].length);
590 packet_put_raw(gss_supported->elements[mech].elements,
591 gss_supported->elements[mech].length);
596 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
597 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
598 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
599 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
601 mech++; /* Move along to next candidate */
607 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
609 Authctxt *authctxt = ctxt;
610 Gssctxt *gssctxt = authctxt->methoddata;
611 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
612 gss_buffer_desc gssbuf, mic;
613 OM_uint32 status, ms, flags;
616 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
617 recv_tok, &send_tok, &flags);
619 if (send_tok.length > 0) {
620 if (GSS_ERROR(status))
621 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
623 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
625 packet_put_string(send_tok.value, send_tok.length);
627 gss_release_buffer(&ms, &send_tok);
630 if (status == GSS_S_COMPLETE) {
631 /* send either complete or MIC, depending on mechanism */
632 if (strcmp(authctxt->method->name,"gssapi")==0 ||
633 (!(flags & GSS_C_INTEG_FLAG))) {
634 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
637 ssh_gssapi_buildmic(&b, authctxt->server_user,
638 authctxt->service, "gssapi-with-mic");
640 gssbuf.value = buffer_ptr(&b);
641 gssbuf.length = buffer_len(&b);
643 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
645 if (!GSS_ERROR(status)) {
646 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
647 packet_put_string(mic.value, mic.length);
653 gss_release_buffer(&ms, &mic);
661 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
663 Authctxt *authctxt = ctxt;
668 if (authctxt == NULL)
669 fatal("input_gssapi_response: no authentication context");
670 gssctxt = authctxt->methoddata;
673 oidv = packet_get_string(&oidlen);
675 if (datafellows & SSH_BUG_GSSAPI_BER) {
676 if (!ssh_gssapi_check_oid(gssctxt, oidv, oidlen))
677 fatal("Server returned different OID than expected");
679 if(oidv[0] != SSH_GSS_OIDTYPE || oidv[1] != oidlen-2) {
680 debug("Badly encoded mechanism OID received");
681 userauth(authctxt, NULL);
685 if (!ssh_gssapi_check_oid(gssctxt, oidv+2, oidlen-2))
686 fatal("Server returned different OID than expected");
693 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
694 /* Start again with next method on list */
695 debug("Trying to start again");
696 userauth(authctxt, NULL);
702 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
704 Authctxt *authctxt = ctxt;
705 gss_buffer_desc recv_tok;
709 if (authctxt == NULL)
710 fatal("input_gssapi_response: no authentication context");
712 recv_tok.value = packet_get_string(&slen);
713 recv_tok.length = slen; /* safe typecast */
717 status = process_gssapi_token(ctxt, &recv_tok);
719 xfree(recv_tok.value);
721 if (GSS_ERROR(status)) {
722 /* Start again with the next method in the list */
723 userauth(authctxt, NULL);
729 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
731 Authctxt *authctxt = ctxt;
733 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
734 gss_buffer_desc recv_tok;
735 OM_uint32 status, ms;
738 if (authctxt == NULL)
739 fatal("input_gssapi_response: no authentication context");
740 gssctxt = authctxt->methoddata;
742 recv_tok.value = packet_get_string(&len);
743 recv_tok.length = len;
747 /* Stick it into GSSAPI and see what it says */
748 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
749 &recv_tok, &send_tok, NULL);
751 xfree(recv_tok.value);
752 gss_release_buffer(&ms, &send_tok);
754 /* Server will be returning a failed packet after this one */
758 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
764 maj=packet_get_int();
765 min=packet_get_int();
766 msg=packet_get_string(NULL);
767 lang=packet_get_string(NULL);
771 debug("Server GSSAPI Error:\n%s\n", msg);
777 userauth_external(Authctxt *authctxt)
779 static int attempt = 0;
784 /* The client MUST NOT try this method if initial key exchange
785 was not performed using a GSSAPI-based key exchange
787 if (xxx_kex->kex_type != KEX_GSS_GRP1_SHA1) {
788 debug2("gsskex not performed, skipping external-keyx");
792 debug2("userauth_external");
793 packet_start(SSH2_MSG_USERAUTH_REQUEST);
795 if(options.implicit && !(datafellows & SSH_BUG_GSS_EMPTYUSER)) {
796 packet_put_cstring("");
799 packet_put_cstring(authctxt->server_user);
803 packet_put_cstring(authctxt->service);
804 packet_put_cstring(authctxt->method->name);
812 userauth_none(Authctxt *authctxt)
814 /* initial userauth request */
815 packet_start(SSH2_MSG_USERAUTH_REQUEST);
816 packet_put_cstring(authctxt->server_user);
817 packet_put_cstring(authctxt->service);
818 packet_put_cstring(authctxt->method->name);
824 userauth_passwd(Authctxt *authctxt)
826 static int attempt = 0;
830 if (attempt++ >= options.number_of_password_prompts)
834 error("Permission denied, please try again.");
836 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
837 authctxt->server_user, authctxt->host);
838 password = read_passphrase(prompt, 0);
839 packet_start(SSH2_MSG_USERAUTH_REQUEST);
840 packet_put_cstring(authctxt->server_user);
841 packet_put_cstring(authctxt->service);
842 packet_put_cstring(authctxt->method->name);
844 packet_put_cstring(password);
845 memset(password, 0, strlen(password));
847 packet_add_padding(64);
850 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
851 &input_userauth_passwd_changereq);
856 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
859 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
861 Authctxt *authctxt = ctxt;
862 char *info, *lang, *password = NULL, *retype = NULL;
865 debug2("input_userauth_passwd_changereq");
867 if (authctxt == NULL)
868 fatal("input_userauth_passwd_changereq: "
869 "no authentication context");
871 info = packet_get_string(NULL);
872 lang = packet_get_string(NULL);
873 if (strlen(info) > 0)
877 packet_start(SSH2_MSG_USERAUTH_REQUEST);
878 packet_put_cstring(authctxt->server_user);
879 packet_put_cstring(authctxt->service);
880 packet_put_cstring(authctxt->method->name);
881 packet_put_char(1); /* additional info */
882 snprintf(prompt, sizeof(prompt),
883 "Enter %.30s@%.128s's old password: ",
884 authctxt->server_user, authctxt->host);
885 password = read_passphrase(prompt, 0);
886 packet_put_cstring(password);
887 memset(password, 0, strlen(password));
890 while (password == NULL) {
891 snprintf(prompt, sizeof(prompt),
892 "Enter %.30s@%.128s's new password: ",
893 authctxt->server_user, authctxt->host);
894 password = read_passphrase(prompt, RP_ALLOW_EOF);
895 if (password == NULL) {
899 snprintf(prompt, sizeof(prompt),
900 "Retype %.30s@%.128s's new password: ",
901 authctxt->server_user, authctxt->host);
902 retype = read_passphrase(prompt, 0);
903 if (strcmp(password, retype) != 0) {
904 memset(password, 0, strlen(password));
906 logit("Mismatch; try again, EOF to quit.");
909 memset(retype, 0, strlen(retype));
912 packet_put_cstring(password);
913 memset(password, 0, strlen(password));
915 packet_add_padding(64);
918 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
919 &input_userauth_passwd_changereq);
923 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
924 u_char *data, u_int datalen)
929 /* the agent supports this key */
931 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
934 * we have already loaded the private key or
935 * the private key is stored in external hardware
937 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
938 return (key_sign(id->key, sigp, lenp, data, datalen));
939 /* load the private key from the file */
940 if ((prv = load_identity_file(id->filename)) == NULL)
942 ret = key_sign(prv, sigp, lenp, data, datalen);
948 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
951 u_char *blob, *signature;
957 debug3("sign_and_send_pubkey");
959 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
960 /* we cannot handle this key */
961 debug3("sign_and_send_pubkey: cannot handle key");
964 /* data to be signed */
966 if (datafellows & SSH_OLD_SESSIONID) {
967 buffer_append(&b, session_id2, session_id2_len);
968 skip = session_id2_len;
970 buffer_put_string(&b, session_id2, session_id2_len);
971 skip = buffer_len(&b);
973 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
974 buffer_put_cstring(&b, authctxt->server_user);
975 buffer_put_cstring(&b,
976 datafellows & SSH_BUG_PKSERVICE ?
979 if (datafellows & SSH_BUG_PKAUTH) {
980 buffer_put_char(&b, have_sig);
982 buffer_put_cstring(&b, authctxt->method->name);
983 buffer_put_char(&b, have_sig);
984 buffer_put_cstring(&b, key_ssh_name(id->key));
986 buffer_put_string(&b, blob, bloblen);
988 /* generate signature */
989 ret = identity_sign(id, &signature, &slen,
990 buffer_ptr(&b), buffer_len(&b));
999 if (datafellows & SSH_BUG_PKSERVICE) {
1001 buffer_append(&b, session_id2, session_id2_len);
1002 skip = session_id2_len;
1003 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1004 buffer_put_cstring(&b, authctxt->server_user);
1005 buffer_put_cstring(&b, authctxt->service);
1006 buffer_put_cstring(&b, authctxt->method->name);
1007 buffer_put_char(&b, have_sig);
1008 if (!(datafellows & SSH_BUG_PKAUTH))
1009 buffer_put_cstring(&b, key_ssh_name(id->key));
1010 buffer_put_string(&b, blob, bloblen);
1014 /* append signature */
1015 buffer_put_string(&b, signature, slen);
1018 /* skip session id and packet type */
1019 if (buffer_len(&b) < skip + 1)
1020 fatal("userauth_pubkey: internal error");
1021 buffer_consume(&b, skip + 1);
1023 /* put remaining data from buffer into packet */
1024 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1025 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1033 send_pubkey_test(Authctxt *authctxt, Identity *id)
1036 u_int bloblen, have_sig = 0;
1038 debug3("send_pubkey_test");
1040 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1041 /* we cannot handle this key */
1042 debug3("send_pubkey_test: cannot handle key");
1045 /* register callback for USERAUTH_PK_OK message */
1046 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1048 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1049 packet_put_cstring(authctxt->server_user);
1050 packet_put_cstring(authctxt->service);
1051 packet_put_cstring(authctxt->method->name);
1052 packet_put_char(have_sig);
1053 if (!(datafellows & SSH_BUG_PKAUTH))
1054 packet_put_cstring(key_ssh_name(id->key));
1055 packet_put_string(blob, bloblen);
1062 load_identity_file(char *filename)
1065 char prompt[300], *passphrase;
1069 if (stat(filename, &st) < 0) {
1070 debug3("no such identity: %s", filename);
1073 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
1074 if (private == NULL) {
1075 if (options.batch_mode)
1077 snprintf(prompt, sizeof prompt,
1078 "Enter passphrase for key '%.100s': ", filename);
1079 for (i = 0; i < options.number_of_password_prompts; i++) {
1080 passphrase = read_passphrase(prompt, 0);
1081 if (strcmp(passphrase, "") != 0) {
1082 private = key_load_private_type(KEY_UNSPEC, filename,
1086 debug2("no passphrase given, try next key");
1089 memset(passphrase, 0, strlen(passphrase));
1091 if (private != NULL || quit)
1093 debug2("bad passphrase given, try again...");
1100 * try keys in the following order:
1101 * 1. agent keys that are found in the config file
1102 * 2. other agent keys
1103 * 3. keys that are only listed in the config file
1106 pubkey_prepare(Authctxt *authctxt)
1109 Idlist agent, files, *preferred;
1111 AuthenticationConnection *ac;
1115 TAILQ_INIT(&agent); /* keys from the agent */
1116 TAILQ_INIT(&files); /* keys from the config file */
1117 preferred = &authctxt->keys;
1118 TAILQ_INIT(preferred); /* preferred order of keys */
1120 /* list of keys stored in the filesystem */
1121 for (i = 0; i < options.num_identity_files; i++) {
1122 key = options.identity_keys[i];
1123 if (key && key->type == KEY_RSA1)
1125 options.identity_keys[i] = NULL;
1126 id = xmalloc(sizeof(*id));
1127 memset(id, 0, sizeof(*id));
1129 id->filename = xstrdup(options.identity_files[i]);
1130 TAILQ_INSERT_TAIL(&files, id, next);
1132 /* list of keys supported by the agent */
1133 if ((ac = ssh_get_authentication_connection())) {
1134 for (key = ssh_get_first_identity(ac, &comment, 2);
1136 key = ssh_get_next_identity(ac, &comment, 2)) {
1138 TAILQ_FOREACH(id, &files, next) {
1139 /* agent keys from the config file are preferred */
1140 if (key_equal(key, id->key)) {
1143 TAILQ_REMOVE(&files, id, next);
1144 TAILQ_INSERT_TAIL(preferred, id, next);
1151 id = xmalloc(sizeof(*id));
1152 memset(id, 0, sizeof(*id));
1154 id->filename = comment;
1156 TAILQ_INSERT_TAIL(&agent, id, next);
1159 /* append remaining agent keys */
1160 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1161 TAILQ_REMOVE(&agent, id, next);
1162 TAILQ_INSERT_TAIL(preferred, id, next);
1164 authctxt->agent = ac;
1166 /* append remaining keys from the config file */
1167 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1168 TAILQ_REMOVE(&files, id, next);
1169 TAILQ_INSERT_TAIL(preferred, id, next);
1171 TAILQ_FOREACH(id, preferred, next) {
1172 debug2("key: %s (%p)", id->filename, id->key);
1177 pubkey_cleanup(Authctxt *authctxt)
1181 if (authctxt->agent != NULL)
1182 ssh_close_authentication_connection(authctxt->agent);
1183 for (id = TAILQ_FIRST(&authctxt->keys); id;
1184 id = TAILQ_FIRST(&authctxt->keys)) {
1185 TAILQ_REMOVE(&authctxt->keys, id, next);
1189 xfree(id->filename);
1195 userauth_pubkey(Authctxt *authctxt)
1200 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1203 /* move key to the end of the queue */
1204 TAILQ_REMOVE(&authctxt->keys, id, next);
1205 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1207 * send a test message if we have the public key. for
1208 * encrypted keys we cannot do this and have to load the
1209 * private key instead
1211 if (id->key && id->key->type != KEY_RSA1) {
1212 debug("Offering public key: %s", id->filename);
1213 sent = send_pubkey_test(authctxt, id);
1214 } else if (id->key == NULL) {
1215 debug("Trying private key: %s", id->filename);
1216 id->key = load_identity_file(id->filename);
1217 if (id->key != NULL) {
1219 sent = sign_and_send_pubkey(authctxt, id);
1231 * Send userauth request message specifying keyboard-interactive method.
1234 userauth_kbdint(Authctxt *authctxt)
1236 static int attempt = 0;
1238 if (attempt++ >= options.number_of_password_prompts)
1240 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1241 if (attempt > 1 && !authctxt->info_req_seen) {
1242 debug3("userauth_kbdint: disable: no info_req_seen");
1243 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1247 debug2("userauth_kbdint");
1248 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1249 packet_put_cstring(authctxt->server_user);
1250 packet_put_cstring(authctxt->service);
1251 packet_put_cstring(authctxt->method->name);
1252 packet_put_cstring(""); /* lang */
1253 packet_put_cstring(options.kbd_interactive_devices ?
1254 options.kbd_interactive_devices : "");
1257 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1262 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1265 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1267 Authctxt *authctxt = ctxt;
1268 char *name, *inst, *lang, *prompt, *response;
1269 u_int num_prompts, i;
1272 debug2("input_userauth_info_req");
1274 if (authctxt == NULL)
1275 fatal("input_userauth_info_req: no authentication context");
1277 authctxt->info_req_seen = 1;
1279 name = packet_get_string(NULL);
1280 inst = packet_get_string(NULL);
1281 lang = packet_get_string(NULL);
1282 if (strlen(name) > 0)
1284 if (strlen(inst) > 0)
1290 num_prompts = packet_get_int();
1292 * Begin to build info response packet based on prompts requested.
1293 * We commit to providing the correct number of responses, so if
1294 * further on we run into a problem that prevents this, we have to
1295 * be sure and clean this up and send a correct error response.
1297 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1298 packet_put_int(num_prompts);
1300 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1301 for (i = 0; i < num_prompts; i++) {
1302 prompt = packet_get_string(NULL);
1303 echo = packet_get_char();
1305 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1307 packet_put_cstring(response);
1308 memset(response, 0, strlen(response));
1312 packet_check_eom(); /* done with parsing incoming message. */
1314 packet_add_padding(64);
1319 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1320 u_char *data, u_int datalen)
1325 int to[2], from[2], status, version = 2;
1327 debug2("ssh_keysign called");
1329 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1330 error("ssh_keysign: no installed: %s", strerror(errno));
1333 if (fflush(stdout) != 0)
1334 error("ssh_keysign: fflush: %s", strerror(errno));
1336 error("ssh_keysign: pipe: %s", strerror(errno));
1339 if (pipe(from) < 0) {
1340 error("ssh_keysign: pipe: %s", strerror(errno));
1343 if ((pid = fork()) < 0) {
1344 error("ssh_keysign: fork: %s", strerror(errno));
1351 if (dup2(from[1], STDOUT_FILENO) < 0)
1352 fatal("ssh_keysign: dup2: %s", strerror(errno));
1354 if (dup2(to[0], STDIN_FILENO) < 0)
1355 fatal("ssh_keysign: dup2: %s", strerror(errno));
1358 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1359 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1366 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1367 buffer_put_string(&b, data, datalen);
1368 if (ssh_msg_send(to[1], version, &b) == -1)
1369 fatal("ssh_keysign: couldn't send request");
1371 if (ssh_msg_recv(from[0], &b) < 0) {
1372 error("ssh_keysign: no reply");
1379 while (waitpid(pid, &status, 0) < 0)
1383 if (buffer_get_char(&b) != version) {
1384 error("ssh_keysign: bad version");
1388 *sigp = buffer_get_string(&b, lenp);
1395 userauth_hostbased(Authctxt *authctxt)
1397 Key *private = NULL;
1398 Sensitive *sensitive = authctxt->sensitive;
1400 u_char *signature, *blob;
1401 char *chost, *pkalg, *p;
1402 const char *service;
1404 int ok, i, len, found = 0;
1406 /* check for a useful key */
1407 for (i = 0; i < sensitive->nkeys; i++) {
1408 private = sensitive->keys[i];
1409 if (private && private->type != KEY_RSA1) {
1411 /* we take and free the key */
1412 sensitive->keys[i] = NULL;
1417 debug("No more client hostkeys for hostbased authentication.");
1420 if (key_to_blob(private, &blob, &blen) == 0) {
1424 /* figure out a name for the client host */
1425 p = get_local_name(packet_get_connection_in());
1427 error("userauth_hostbased: cannot get local ipaddr/name");
1431 len = strlen(p) + 2;
1432 chost = xmalloc(len);
1433 strlcpy(chost, p, len);
1434 strlcat(chost, ".", len);
1435 debug2("userauth_hostbased: chost %s", chost);
1438 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1440 pkalg = xstrdup(key_ssh_name(private));
1442 /* construct data */
1443 buffer_put_string(&b, session_id2, session_id2_len);
1444 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1445 buffer_put_cstring(&b, authctxt->server_user);
1446 buffer_put_cstring(&b, service);
1447 buffer_put_cstring(&b, authctxt->method->name);
1448 buffer_put_cstring(&b, pkalg);
1449 buffer_put_string(&b, blob, blen);
1450 buffer_put_cstring(&b, chost);
1451 buffer_put_cstring(&b, authctxt->local_user);
1455 if (sensitive->external_keysign)
1456 ok = ssh_keysign(private, &signature, &slen,
1457 buffer_ptr(&b), buffer_len(&b));
1459 ok = key_sign(private, &signature, &slen,
1460 buffer_ptr(&b), buffer_len(&b));
1464 error("key_sign failed");
1469 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1470 packet_put_cstring(authctxt->server_user);
1471 packet_put_cstring(authctxt->service);
1472 packet_put_cstring(authctxt->method->name);
1473 packet_put_cstring(pkalg);
1474 packet_put_string(blob, blen);
1475 packet_put_cstring(chost);
1476 packet_put_cstring(authctxt->local_user);
1477 packet_put_string(signature, slen);
1478 memset(signature, 's', slen);
1487 /* find auth method */
1490 * given auth method name, if configurable options permit this method fill
1491 * in auth_ident field and return true, otherwise return false.
1494 authmethod_is_enabled(Authmethod *method)
1498 /* return false if options indicate this method is disabled */
1499 if (method->enabled == NULL || *method->enabled == 0)
1501 /* return false if batch mode is enabled but method needs interactive mode */
1502 if (method->batch_flag != NULL && *method->batch_flag != 0)
1508 authmethod_lookup(const char *name)
1510 Authmethod *method = NULL;
1512 for (method = authmethods; method->name != NULL; method++)
1513 if (strcmp(name, method->name) == 0)
1515 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1519 /* XXX internal state */
1520 static Authmethod *current = NULL;
1521 static char *supported = NULL;
1522 static char *preferred = NULL;
1525 * Given the authentication method list sent by the server, return the
1526 * next method we should try. If the server initially sends a nil list,
1527 * use a built-in default list.
1530 authmethod_get(char *authlist)
1535 /* Use a suitable default if we're passed a nil list. */
1536 if (authlist == NULL || strlen(authlist) == 0)
1537 authlist = options.preferred_authentications;
1539 if (supported == NULL || strcmp(authlist, supported) != 0) {
1540 debug3("start over, passed a different list %s", authlist);
1541 if (supported != NULL)
1543 supported = xstrdup(authlist);
1544 preferred = options.preferred_authentications;
1545 debug3("preferred %s", preferred);
1547 } else if (current != NULL && authmethod_is_enabled(current))
1551 if ((name = match_list(preferred, supported, &next)) == NULL) {
1552 debug("No more authentication methods to try.");
1557 debug3("authmethod_lookup %s", name);
1558 debug3("remaining preferred: %s", preferred);
1559 if ((current = authmethod_lookup(name)) != NULL &&
1560 authmethod_is_enabled(current)) {
1561 debug3("authmethod_is_enabled %s", name);
1562 debug("Next authentication method: %s", name);
1569 authmethods_get(void)
1571 Authmethod *method = NULL;
1576 for (method = authmethods; method->name != NULL; method++) {
1577 if (authmethod_is_enabled(method)) {
1578 if (buffer_len(&b) > 0)
1579 buffer_append(&b, ",", 1);
1580 buffer_append(&b, method->name, strlen(method->name));
1583 buffer_append(&b, "\0", 1);
1584 list = xstrdup(buffer_ptr(&b));