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.57 2001/03/27 17:46:49 provos Exp $");
28 #include <openssl/bn.h>
29 #include <openssl/md5.h>
30 #include <openssl/dh.h>
31 #include <openssl/hmac.h>
44 #include "myproposal.h"
46 #include "sshconnect.h"
57 void ssh_dh1_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
58 void ssh_dhgex_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
61 extern char *client_version_string;
62 extern char *server_version_string;
63 extern Options options;
69 u_char *session_id2 = NULL;
70 int session_id2_len = 0;
73 ssh_kex2(char *host, struct sockaddr *hostaddr)
77 Buffer *client_kexinit, *server_kexinit;
78 char *sprop[PROPOSAL_MAX];
80 if (options.ciphers == (char *)-1) {
81 log("No valid ciphers for protocol version 2 given, using defaults.");
82 options.ciphers = NULL;
84 if (options.ciphers != NULL) {
85 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
86 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
88 if (options.compression) {
89 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
90 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
92 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
93 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
95 if (options.macs != NULL) {
96 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
97 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
100 myproposal[PROPOSAL_ENC_ALGS_STOC] =
101 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
103 /* buffers with raw kexinit messages */
104 server_kexinit = xmalloc(sizeof(*server_kexinit));
105 buffer_init(server_kexinit);
106 client_kexinit = kex_init(myproposal);
108 /* algorithm negotiation */
109 kex_exchange_kexinit(client_kexinit, server_kexinit, sprop);
110 kex = kex_choose_conf(myproposal, sprop, 0);
111 for (i = 0; i < PROPOSAL_MAX; i++)
114 /* server authentication and session key agreement */
115 switch(kex->kex_type) {
117 ssh_dh1_client(kex, host, hostaddr,
118 client_kexinit, server_kexinit);
121 ssh_dhgex_client(kex, host, hostaddr, client_kexinit,
125 fatal("Unsupported key exchange %d", kex->kex_type);
128 buffer_free(client_kexinit);
129 buffer_free(server_kexinit);
130 xfree(client_kexinit);
131 xfree(server_kexinit);
133 debug("Wait SSH2_MSG_NEWKEYS.");
134 packet_read_expect(&plen, SSH2_MSG_NEWKEYS);
136 debug("GOT SSH2_MSG_NEWKEYS.");
138 debug("send SSH2_MSG_NEWKEYS.");
139 packet_start(SSH2_MSG_NEWKEYS);
142 debug("done: send SSH2_MSG_NEWKEYS.");
145 /* send 1st encrypted/maced/compressed message */
146 packet_start(SSH2_MSG_IGNORE);
147 packet_put_cstring("markus");
151 debug("done: KEX2.");
154 /* diffie-hellman-group1-sha1 */
157 ssh_dh1_client(Kex *kex, char *host, struct sockaddr *hostaddr,
158 Buffer *client_kexinit, Buffer *server_kexinit)
165 char *signature = NULL;
167 char *server_host_key_blob = NULL;
168 Key *server_host_key;
171 BIGNUM *dh_server_pub = 0;
172 BIGNUM *shared_secret = 0;
176 debug("Sending SSH2_MSG_KEXDH_INIT.");
177 /* generate and send 'e', client DH public key */
178 dh = dh_new_group1();
179 dh_gen_key(dh, kex->we_need * 8);
180 packet_start(SSH2_MSG_KEXDH_INIT);
181 packet_put_bignum2(dh->pub_key);
186 fprintf(stderr, "\np= ");
187 BN_print_fp(stderr, dh->p);
188 fprintf(stderr, "\ng= ");
189 BN_print_fp(stderr, dh->g);
190 fprintf(stderr, "\npub= ");
191 BN_print_fp(stderr, dh->pub_key);
192 fprintf(stderr, "\n");
193 DHparams_print_fp(stderr, dh);
196 debug("Wait SSH2_MSG_KEXDH_REPLY.");
198 packet_read_expect(&plen, SSH2_MSG_KEXDH_REPLY);
200 debug("Got SSH2_MSG_KEXDH_REPLY.");
203 server_host_key_blob = packet_get_string(&sbloblen);
204 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
205 if (server_host_key == NULL)
206 fatal("cannot decode server_host_key_blob");
208 check_host_key(host, hostaddr, server_host_key,
209 options.user_hostfile2, options.system_hostfile2);
211 /* DH paramter f, server public DH key */
212 dh_server_pub = BN_new();
213 if (dh_server_pub == NULL)
214 fatal("dh_server_pub == NULL");
215 packet_get_bignum2(dh_server_pub, &dlen);
218 fprintf(stderr, "\ndh_server_pub= ");
219 BN_print_fp(stderr, dh_server_pub);
220 fprintf(stderr, "\n");
221 debug("bits %d", BN_num_bits(dh_server_pub));
225 signature = packet_get_string(&slen);
228 if (!dh_pub_is_valid(dh, dh_server_pub))
229 packet_disconnect("bad server public DH value");
232 kbuf = xmalloc(klen);
233 kout = DH_compute_key(kbuf, dh_server_pub, dh);
235 debug("shared secret: len %d/%d", klen, kout);
236 fprintf(stderr, "shared secret == ");
237 for (i = 0; i< kout; i++)
238 fprintf(stderr, "%02x", (kbuf[i])&0xff);
239 fprintf(stderr, "\n");
241 shared_secret = BN_new();
243 BN_bin2bn(kbuf, kout, shared_secret);
244 memset(kbuf, 0, klen);
247 /* calc and verify H */
249 client_version_string,
250 server_version_string,
251 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
252 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
253 server_host_key_blob, sbloblen,
258 xfree(server_host_key_blob);
260 BN_free(dh_server_pub);
262 fprintf(stderr, "hash == ");
263 for (i = 0; i< 20; i++)
264 fprintf(stderr, "%02x", (hash[i])&0xff);
265 fprintf(stderr, "\n");
267 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
268 fatal("key_verify failed for server_host_key");
269 key_free(server_host_key);
272 kex_derive_keys(kex, hash, shared_secret);
273 BN_clear_free(shared_secret);
276 /* save session id */
277 session_id2_len = 20;
278 session_id2 = xmalloc(session_id2_len);
279 memcpy(session_id2, hash, session_id2_len);
282 /* diffie-hellman-group-exchange-sha1 */
285 * Estimates the group order for a Diffie-Hellman group that has an
286 * attack complexity approximately the same as O(2**bits). Estimate
287 * with: O(exp(1.9223 * (ln q)^(1/3) (ln ln q)^(2/3)))
291 dh_estimate(int bits)
295 return (512); /* O(2**63) */
297 return (1024); /* O(2**86) */
299 return (2048); /* O(2**116) */
300 return (4096); /* O(2**156) */
304 ssh_dhgex_client(Kex *kex, char *host, struct sockaddr *hostaddr,
305 Buffer *client_kexinit, Buffer *server_kexinit)
312 char *signature = NULL;
313 u_int slen, nbits, min, max;
314 char *server_host_key_blob = NULL;
315 Key *server_host_key;
318 BIGNUM *dh_server_pub = 0;
319 BIGNUM *shared_secret = 0;
320 BIGNUM *p = 0, *g = 0;
324 nbits = dh_estimate(kex->we_need * 8);
326 if (datafellows & SSH_OLD_DHGEX) {
327 debug("Sending SSH2_MSG_KEX_DH_GEX_REQUEST_OLD.");
329 /* Old GEX request */
330 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST_OLD);
331 packet_put_int(nbits);
335 debug("Sending SSH2_MSG_KEX_DH_GEX_REQUEST.");
337 /* New GEX request */
339 max = MIN(DH_GRP_MAX, nbits * 1.25);
341 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
343 packet_put_int(nbits);
350 fprintf(stderr, "\nmin = %d, nbits = %d, max = %d", min, nbits, max);
353 debug("Wait SSH2_MSG_KEX_DH_GEX_GROUP.");
355 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_GROUP);
357 debug("Got SSH2_MSG_KEX_DH_GEX_GROUP.");
359 if ((p = BN_new()) == NULL)
361 packet_get_bignum2(p, &dlen);
362 if ((g = BN_new()) == NULL)
364 packet_get_bignum2(g, &dlen);
366 if (BN_num_bits(p) < min || BN_num_bits(p) > max)
367 fatal("DH_GEX group out of range: %d !< %d !< %d",
368 min, BN_num_bits(p), max);
370 dh = dh_new_group(g, p);
372 dh_gen_key(dh, kex->we_need * 8);
375 fprintf(stderr, "\np= ");
376 BN_print_fp(stderr, dh->p);
377 fprintf(stderr, "\ng= ");
378 BN_print_fp(stderr, dh->g);
379 fprintf(stderr, "\npub= ");
380 BN_print_fp(stderr, dh->pub_key);
381 fprintf(stderr, "\n");
382 DHparams_print_fp(stderr, dh);
385 debug("Sending SSH2_MSG_KEX_DH_GEX_INIT.");
386 /* generate and send 'e', client DH public key */
387 packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
388 packet_put_bignum2(dh->pub_key);
392 debug("Wait SSH2_MSG_KEX_DH_GEX_REPLY.");
394 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_REPLY);
396 debug("Got SSH2_MSG_KEXDH_REPLY.");
399 server_host_key_blob = packet_get_string(&sbloblen);
400 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
401 if (server_host_key == NULL)
402 fatal("cannot decode server_host_key_blob");
404 check_host_key(host, hostaddr, server_host_key,
405 options.user_hostfile2, options.system_hostfile2);
407 /* DH paramter f, server public DH key */
408 dh_server_pub = BN_new();
409 if (dh_server_pub == NULL)
410 fatal("dh_server_pub == NULL");
411 packet_get_bignum2(dh_server_pub, &dlen);
414 fprintf(stderr, "\ndh_server_pub= ");
415 BN_print_fp(stderr, dh_server_pub);
416 fprintf(stderr, "\n");
417 debug("bits %d", BN_num_bits(dh_server_pub));
421 signature = packet_get_string(&slen);
424 if (!dh_pub_is_valid(dh, dh_server_pub))
425 packet_disconnect("bad server public DH value");
428 kbuf = xmalloc(klen);
429 kout = DH_compute_key(kbuf, dh_server_pub, dh);
431 debug("shared secret: len %d/%d", klen, kout);
432 fprintf(stderr, "shared secret == ");
433 for (i = 0; i< kout; i++)
434 fprintf(stderr, "%02x", (kbuf[i])&0xff);
435 fprintf(stderr, "\n");
437 shared_secret = BN_new();
439 BN_bin2bn(kbuf, kout, shared_secret);
440 memset(kbuf, 0, klen);
443 /* calc and verify H */
445 client_version_string,
446 server_version_string,
447 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
448 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
449 server_host_key_blob, sbloblen,
455 xfree(server_host_key_blob);
457 BN_free(dh_server_pub);
459 fprintf(stderr, "hash == ");
460 for (i = 0; i< 20; i++)
461 fprintf(stderr, "%02x", (hash[i])&0xff);
462 fprintf(stderr, "\n");
464 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
465 fatal("key_verify failed for server_host_key");
466 key_free(server_host_key);
469 kex_derive_keys(kex, hash, shared_secret);
470 BN_clear_free(shared_secret);
473 /* save session id */
474 session_id2_len = 20;
475 session_id2 = xmalloc(session_id2_len);
476 memcpy(session_id2, hash, session_id2_len);
483 typedef struct Authctxt Authctxt;
484 typedef struct Authmethod Authmethod;
486 typedef int sign_cb_fn(
487 Authctxt *authctxt, Key *key,
488 u_char **sigp, int *lenp, u_char *data, int datalen);
491 const char *server_user;
494 AuthenticationConnection *agent;
499 sign_cb_fn *last_key_sign;
503 char *name; /* string to compare against server's list */
504 int (*userauth)(Authctxt *authctxt);
505 int *enabled; /* flag in option struct that enables method */
506 int *batch_flag; /* flag in option struct that disables method */
509 void input_userauth_success(int type, int plen, void *ctxt);
510 void input_userauth_failure(int type, int plen, void *ctxt);
511 void input_userauth_banner(int type, int plen, void *ctxt);
512 void input_userauth_error(int type, int plen, void *ctxt);
513 void input_userauth_info_req(int type, int plen, void *ctxt);
514 void input_userauth_pk_ok(int type, int plen, void *ctxt);
516 int userauth_none(Authctxt *authctxt);
517 int userauth_pubkey(Authctxt *authctxt);
518 int userauth_passwd(Authctxt *authctxt);
519 int userauth_kbdint(Authctxt *authctxt);
521 void userauth(Authctxt *authctxt, char *authlist);
524 sign_and_send_pubkey(Authctxt *authctxt, Key *k,
525 sign_cb_fn *sign_callback);
526 void clear_auth_state(Authctxt *authctxt);
528 Authmethod *authmethod_get(char *authlist);
529 Authmethod *authmethod_lookup(const char *name);
530 char *authmethods_get(void);
532 Authmethod authmethods[] = {
535 &options.pubkey_authentication,
539 &options.password_authentication,
540 &options.batch_mode},
541 {"keyboard-interactive",
543 &options.kbd_interactive_authentication,
544 &options.batch_mode},
549 {NULL, NULL, NULL, NULL}
553 ssh_userauth2(const char *server_user, char *host)
559 if (options.challenge_reponse_authentication)
560 options.kbd_interactive_authentication = 1;
562 debug("send SSH2_MSG_SERVICE_REQUEST");
563 packet_start(SSH2_MSG_SERVICE_REQUEST);
564 packet_put_cstring("ssh-userauth");
567 type = packet_read(&plen);
568 if (type != SSH2_MSG_SERVICE_ACCEPT) {
569 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
571 if (packet_remaining() > 0) {
572 char *reply = packet_get_string(&plen);
573 debug("service_accept: %s", reply);
576 debug("buggy server: service_accept w/o service");
579 debug("got SSH2_MSG_SERVICE_ACCEPT");
581 if (options.preferred_authentications == NULL)
582 options.preferred_authentications = authmethods_get();
584 /* setup authentication context */
585 authctxt.agent = ssh_get_authentication_connection();
586 authctxt.server_user = server_user;
587 authctxt.host = host;
588 authctxt.service = "ssh-connection"; /* service name */
589 authctxt.success = 0;
590 authctxt.method = authmethod_lookup("none");
591 authctxt.authlist = NULL;
592 if (authctxt.method == NULL)
593 fatal("ssh_userauth2: internal error: cannot send userauth none request");
595 /* initial userauth request */
596 userauth_none(&authctxt);
598 dispatch_init(&input_userauth_error);
599 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
600 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
601 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
602 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
604 if (authctxt.agent != NULL)
605 ssh_close_authentication_connection(authctxt.agent);
607 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
610 userauth(Authctxt *authctxt, char *authlist)
612 if (authlist == NULL) {
613 authlist = authctxt->authlist;
615 if (authctxt->authlist)
616 xfree(authctxt->authlist);
617 authctxt->authlist = authlist;
620 Authmethod *method = authmethod_get(authlist);
622 fatal("Permission denied (%s).", authlist);
623 authctxt->method = method;
624 if (method->userauth(authctxt) != 0) {
625 debug2("we sent a %s packet, wait for reply", method->name);
628 debug2("we did not send a packet, disable method");
629 method->enabled = NULL;
634 input_userauth_error(int type, int plen, void *ctxt)
636 fatal("input_userauth_error: bad message during authentication: "
640 input_userauth_banner(int type, int plen, void *ctxt)
643 debug3("input_userauth_banner");
644 msg = packet_get_string(NULL);
645 lang = packet_get_string(NULL);
646 fprintf(stderr, "%s", msg);
651 input_userauth_success(int type, int plen, void *ctxt)
653 Authctxt *authctxt = ctxt;
654 if (authctxt == NULL)
655 fatal("input_userauth_success: no authentication context");
656 if (authctxt->authlist)
657 xfree(authctxt->authlist);
658 clear_auth_state(authctxt);
659 authctxt->success = 1; /* break out */
662 input_userauth_failure(int type, int plen, void *ctxt)
664 Authctxt *authctxt = ctxt;
665 char *authlist = NULL;
668 if (authctxt == NULL)
669 fatal("input_userauth_failure: no authentication context");
671 authlist = packet_get_string(NULL);
672 partial = packet_get_char();
676 log("Authenticated with partial success.");
677 debug("authentications that can continue: %s", authlist);
679 clear_auth_state(authctxt);
680 userauth(authctxt, authlist);
683 input_userauth_pk_ok(int type, int plen, void *ctxt)
685 Authctxt *authctxt = ctxt;
688 int alen, blen, pktype, sent = 0;
689 char *pkalg, *pkblob, *fp;
691 if (authctxt == NULL)
692 fatal("input_userauth_pk_ok: no authentication context");
693 if (datafellows & SSH_BUG_PKOK) {
694 /* this is similar to SSH_BUG_PKAUTH */
695 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
696 pkblob = packet_get_string(&blen);
698 buffer_append(&b, pkblob, blen);
699 pkalg = buffer_get_string(&b, &alen);
702 pkalg = packet_get_string(&alen);
703 pkblob = packet_get_string(&blen);
707 debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d",
708 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
711 if (authctxt->last_key == NULL ||
712 authctxt->last_key_sign == NULL) {
713 debug("no last key or no sign cb");
716 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
717 debug("unknown pkalg %s", pkalg);
720 if ((key = key_from_blob(pkblob, blen)) == NULL) {
721 debug("no key from blob. pkalg %s", pkalg);
724 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
725 debug2("input_userauth_pk_ok: fp %s", fp);
727 if (!key_equal(key, authctxt->last_key)) {
728 debug("key != last_key");
731 sent = sign_and_send_pubkey(authctxt, key,
732 authctxt->last_key_sign);
741 clear_auth_state(authctxt);
742 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
744 /* try another method if we did not send a packet*/
746 userauth(authctxt, NULL);
751 userauth_none(Authctxt *authctxt)
753 /* initial userauth request */
754 packet_start(SSH2_MSG_USERAUTH_REQUEST);
755 packet_put_cstring(authctxt->server_user);
756 packet_put_cstring(authctxt->service);
757 packet_put_cstring(authctxt->method->name);
763 userauth_passwd(Authctxt *authctxt)
765 static int attempt = 0;
769 if (attempt++ >= options.number_of_password_prompts)
773 error("Permission denied, please try again.");
775 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
776 authctxt->server_user, authctxt->host);
777 password = read_passphrase(prompt, 0);
778 packet_start(SSH2_MSG_USERAUTH_REQUEST);
779 packet_put_cstring(authctxt->server_user);
780 packet_put_cstring(authctxt->service);
781 packet_put_cstring(authctxt->method->name);
783 packet_put_cstring(password);
784 memset(password, 0, strlen(password));
786 packet_inject_ignore(64);
792 clear_auth_state(Authctxt *authctxt)
794 /* XXX clear authentication state */
795 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
796 debug3("clear_auth_state: key_free %p", authctxt->last_key);
797 key_free(authctxt->last_key);
799 authctxt->last_key = NULL;
800 authctxt->last_key_hint = -2;
801 authctxt->last_key_sign = NULL;
805 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
808 u_char *blob, *signature;
814 debug3("sign_and_send_pubkey");
816 if (key_to_blob(k, &blob, &bloblen) == 0) {
817 /* we cannot handle this key */
818 debug3("sign_and_send_pubkey: cannot handle key");
821 /* data to be signed */
823 if (datafellows & SSH_OLD_SESSIONID) {
824 buffer_append(&b, session_id2, session_id2_len);
825 skip = session_id2_len;
827 buffer_put_string(&b, session_id2, session_id2_len);
828 skip = buffer_len(&b);
830 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
831 buffer_put_cstring(&b, authctxt->server_user);
832 buffer_put_cstring(&b,
833 datafellows & SSH_BUG_PKSERVICE ?
836 if (datafellows & SSH_BUG_PKAUTH) {
837 buffer_put_char(&b, have_sig);
839 buffer_put_cstring(&b, authctxt->method->name);
840 buffer_put_char(&b, have_sig);
841 buffer_put_cstring(&b, key_ssh_name(k));
843 buffer_put_string(&b, blob, bloblen);
845 /* generate signature */
846 ret = (*sign_callback)(authctxt, k, &signature, &slen,
847 buffer_ptr(&b), buffer_len(&b));
856 if (datafellows & SSH_BUG_PKSERVICE) {
858 buffer_append(&b, session_id2, session_id2_len);
859 skip = session_id2_len;
860 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
861 buffer_put_cstring(&b, authctxt->server_user);
862 buffer_put_cstring(&b, authctxt->service);
863 buffer_put_cstring(&b, authctxt->method->name);
864 buffer_put_char(&b, have_sig);
865 if (!(datafellows & SSH_BUG_PKAUTH))
866 buffer_put_cstring(&b, key_ssh_name(k));
867 buffer_put_string(&b, blob, bloblen);
871 /* append signature */
872 buffer_put_string(&b, signature, slen);
875 /* skip session id and packet type */
876 if (buffer_len(&b) < skip + 1)
877 fatal("userauth_pubkey: internal error");
878 buffer_consume(&b, skip + 1);
880 /* put remaining data from buffer into packet */
881 packet_start(SSH2_MSG_USERAUTH_REQUEST);
882 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
890 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
894 int bloblen, have_sig = 0;
896 debug3("send_pubkey_test");
898 if (key_to_blob(k, &blob, &bloblen) == 0) {
899 /* we cannot handle this key */
900 debug3("send_pubkey_test: cannot handle key");
903 /* register callback for USERAUTH_PK_OK message */
904 authctxt->last_key_sign = sign_callback;
905 authctxt->last_key_hint = hint;
906 authctxt->last_key = k;
907 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
909 packet_start(SSH2_MSG_USERAUTH_REQUEST);
910 packet_put_cstring(authctxt->server_user);
911 packet_put_cstring(authctxt->service);
912 packet_put_cstring(authctxt->method->name);
913 packet_put_char(have_sig);
914 if (!(datafellows & SSH_BUG_PKAUTH))
915 packet_put_cstring(key_ssh_name(k));
916 packet_put_string(blob, bloblen);
923 load_identity_file(char *filename)
926 char prompt[300], *passphrase;
930 if (stat(filename, &st) < 0) {
931 debug3("no such identity: %s", filename);
934 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
935 if (private == NULL) {
936 if (options.batch_mode)
938 snprintf(prompt, sizeof prompt,
939 "Enter passphrase for key '%.100s': ", filename);
940 for (i = 0; i < options.number_of_password_prompts; i++) {
941 passphrase = read_passphrase(prompt, 0);
942 if (strcmp(passphrase, "") != 0) {
943 private = key_load_private_type(KEY_UNSPEC, filename,
947 debug2("no passphrase given, try next key");
950 memset(passphrase, 0, strlen(passphrase));
952 if (private != NULL || quit)
954 debug2("bad passphrase given, try again...");
961 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
962 u_char *data, int datalen)
967 idx = authctxt->last_key_hint;
970 private = load_identity_file(options.identity_files[idx]);
973 ret = key_sign(private, sigp, lenp, data, datalen);
978 int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
979 u_char *data, int datalen)
981 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
984 int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
985 u_char *data, int datalen)
987 return key_sign(key, sigp, lenp, data, datalen);
991 userauth_pubkey_agent(Authctxt *authctxt)
993 static int called = 0;
999 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
1000 debug2("userauth_pubkey_agent: no keys at all");
1003 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
1005 debug2("userauth_pubkey_agent: no more keys");
1007 debug("userauth_pubkey_agent: testing agent key %s", comment);
1009 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
1014 debug2("userauth_pubkey_agent: no message sent");
1019 userauth_pubkey(Authctxt *authctxt)
1026 if (authctxt->agent != NULL) {
1028 sent = userauth_pubkey_agent(authctxt);
1029 } while(!sent && authctxt->agent->howmany > 0);
1031 while (!sent && idx < options.num_identity_files) {
1032 key = options.identity_keys[idx];
1033 filename = options.identity_files[idx];
1035 debug("try privkey: %s", filename);
1036 key = load_identity_file(filename);
1038 sent = sign_and_send_pubkey(authctxt, key,
1042 } else if (key->type != KEY_RSA1) {
1043 debug("try pubkey: %s", filename);
1044 sent = send_pubkey_test(authctxt, key,
1045 identity_sign_cb, idx);
1053 * Send userauth request message specifying keyboard-interactive method.
1056 userauth_kbdint(Authctxt *authctxt)
1058 static int attempt = 0;
1060 if (attempt++ >= options.number_of_password_prompts)
1063 debug2("userauth_kbdint");
1064 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1065 packet_put_cstring(authctxt->server_user);
1066 packet_put_cstring(authctxt->service);
1067 packet_put_cstring(authctxt->method->name);
1068 packet_put_cstring(""); /* lang */
1069 packet_put_cstring(options.kbd_interactive_devices ?
1070 options.kbd_interactive_devices : "");
1073 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1078 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1081 input_userauth_info_req(int type, int plen, void *ctxt)
1083 Authctxt *authctxt = ctxt;
1084 char *name, *inst, *lang, *prompt, *response;
1085 u_int num_prompts, i;
1088 debug2("input_userauth_info_req");
1090 if (authctxt == NULL)
1091 fatal("input_userauth_info_req: no authentication context");
1093 name = packet_get_string(NULL);
1094 inst = packet_get_string(NULL);
1095 lang = packet_get_string(NULL);
1096 if (strlen(name) > 0)
1098 if (strlen(inst) > 0)
1104 num_prompts = packet_get_int();
1106 * Begin to build info response packet based on prompts requested.
1107 * We commit to providing the correct number of responses, so if
1108 * further on we run into a problem that prevents this, we have to
1109 * be sure and clean this up and send a correct error response.
1111 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1112 packet_put_int(num_prompts);
1114 for (i = 0; i < num_prompts; i++) {
1115 prompt = packet_get_string(NULL);
1116 echo = packet_get_char();
1118 response = cli_prompt(prompt, echo);
1120 packet_put_cstring(response);
1121 memset(response, 0, strlen(response));
1125 packet_done(); /* done with parsing incoming message. */
1127 packet_inject_ignore(64);
1131 /* find auth method */
1134 * given auth method name, if configurable options permit this method fill
1135 * in auth_ident field and return true, otherwise return false.
1138 authmethod_is_enabled(Authmethod *method)
1142 /* return false if options indicate this method is disabled */
1143 if (method->enabled == NULL || *method->enabled == 0)
1145 /* return false if batch mode is enabled but method needs interactive mode */
1146 if (method->batch_flag != NULL && *method->batch_flag != 0)
1152 authmethod_lookup(const char *name)
1154 Authmethod *method = NULL;
1156 for (method = authmethods; method->name != NULL; method++)
1157 if (strcmp(name, method->name) == 0)
1159 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1163 /* XXX internal state */
1164 static Authmethod *current = NULL;
1165 static char *supported = NULL;
1166 static char *preferred = NULL;
1168 * Given the authentication method list sent by the server, return the
1169 * next method we should try. If the server initially sends a nil list,
1170 * use a built-in default list.
1173 authmethod_get(char *authlist)
1179 /* Use a suitable default if we're passed a nil list. */
1180 if (authlist == NULL || strlen(authlist) == 0)
1181 authlist = options.preferred_authentications;
1183 if (supported == NULL || strcmp(authlist, supported) != 0) {
1184 debug3("start over, passed a different list %s", authlist);
1185 if (supported != NULL)
1187 supported = xstrdup(authlist);
1188 preferred = options.preferred_authentications;
1189 debug3("preferred %s", preferred);
1191 } else if (current != NULL && authmethod_is_enabled(current))
1195 if ((name = match_list(preferred, supported, &next)) == NULL) {
1196 debug("no more auth methods to try");
1201 debug3("authmethod_lookup %s", name);
1202 debug3("remaining preferred: %s", preferred);
1203 if ((current = authmethod_lookup(name)) != NULL &&
1204 authmethod_is_enabled(current)) {
1205 debug3("authmethod_is_enabled %s", name);
1206 debug("next auth method to try is %s", name);
1215 authmethods_get(void)
1217 Authmethod *method = NULL;
1221 for (method = authmethods; method->name != NULL; method++) {
1222 if (authmethod_is_enabled(method)) {
1224 strlcat(buf, DELIM, sizeof buf);
1225 strlcat(buf, method->name, sizeof buf);
1228 return xstrdup(buf);