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.54 2001/03/12 22:02:02 markus 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"
56 void ssh_dh1_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
57 void ssh_dhgex_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
60 extern char *client_version_string;
61 extern char *server_version_string;
62 extern Options options;
68 u_char *session_id2 = NULL;
69 int session_id2_len = 0;
72 ssh_kex2(char *host, struct sockaddr *hostaddr)
76 Buffer *client_kexinit, *server_kexinit;
77 char *sprop[PROPOSAL_MAX];
79 if (options.ciphers == (char *)-1) {
80 log("No valid ciphers for protocol version 2 given, using defaults.");
81 options.ciphers = NULL;
83 if (options.ciphers != NULL) {
84 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
85 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
87 if (options.compression) {
88 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
89 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
91 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
92 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
94 if (options.macs != NULL) {
95 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
96 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
99 /* buffers with raw kexinit messages */
100 server_kexinit = xmalloc(sizeof(*server_kexinit));
101 buffer_init(server_kexinit);
102 client_kexinit = kex_init(myproposal);
104 /* algorithm negotiation */
105 kex_exchange_kexinit(client_kexinit, server_kexinit, sprop);
106 kex = kex_choose_conf(myproposal, sprop, 0);
107 for (i = 0; i < PROPOSAL_MAX; i++)
110 /* server authentication and session key agreement */
111 switch(kex->kex_type) {
113 ssh_dh1_client(kex, host, hostaddr,
114 client_kexinit, server_kexinit);
117 ssh_dhgex_client(kex, host, hostaddr, client_kexinit,
121 fatal("Unsupported key exchange %d", kex->kex_type);
124 buffer_free(client_kexinit);
125 buffer_free(server_kexinit);
126 xfree(client_kexinit);
127 xfree(server_kexinit);
129 debug("Wait SSH2_MSG_NEWKEYS.");
130 packet_read_expect(&plen, SSH2_MSG_NEWKEYS);
132 debug("GOT SSH2_MSG_NEWKEYS.");
134 debug("send SSH2_MSG_NEWKEYS.");
135 packet_start(SSH2_MSG_NEWKEYS);
138 debug("done: send SSH2_MSG_NEWKEYS.");
141 /* send 1st encrypted/maced/compressed message */
142 packet_start(SSH2_MSG_IGNORE);
143 packet_put_cstring("markus");
147 debug("done: KEX2.");
150 /* diffie-hellman-group1-sha1 */
153 ssh_dh1_client(Kex *kex, char *host, struct sockaddr *hostaddr,
154 Buffer *client_kexinit, Buffer *server_kexinit)
161 char *signature = NULL;
163 char *server_host_key_blob = NULL;
164 Key *server_host_key;
167 BIGNUM *dh_server_pub = 0;
168 BIGNUM *shared_secret = 0;
172 debug("Sending SSH2_MSG_KEXDH_INIT.");
173 /* generate and send 'e', client DH public key */
174 dh = dh_new_group1();
175 dh_gen_key(dh, kex->we_need * 8);
176 packet_start(SSH2_MSG_KEXDH_INIT);
177 packet_put_bignum2(dh->pub_key);
182 fprintf(stderr, "\np= ");
183 BN_print_fp(stderr, dh->p);
184 fprintf(stderr, "\ng= ");
185 BN_print_fp(stderr, dh->g);
186 fprintf(stderr, "\npub= ");
187 BN_print_fp(stderr, dh->pub_key);
188 fprintf(stderr, "\n");
189 DHparams_print_fp(stderr, dh);
192 debug("Wait SSH2_MSG_KEXDH_REPLY.");
194 packet_read_expect(&plen, SSH2_MSG_KEXDH_REPLY);
196 debug("Got SSH2_MSG_KEXDH_REPLY.");
199 server_host_key_blob = packet_get_string(&sbloblen);
200 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
201 if (server_host_key == NULL)
202 fatal("cannot decode server_host_key_blob");
204 check_host_key(host, hostaddr, server_host_key,
205 options.user_hostfile2, options.system_hostfile2);
207 /* DH paramter f, server public DH key */
208 dh_server_pub = BN_new();
209 if (dh_server_pub == NULL)
210 fatal("dh_server_pub == NULL");
211 packet_get_bignum2(dh_server_pub, &dlen);
214 fprintf(stderr, "\ndh_server_pub= ");
215 BN_print_fp(stderr, dh_server_pub);
216 fprintf(stderr, "\n");
217 debug("bits %d", BN_num_bits(dh_server_pub));
221 signature = packet_get_string(&slen);
224 if (!dh_pub_is_valid(dh, dh_server_pub))
225 packet_disconnect("bad server public DH value");
228 kbuf = xmalloc(klen);
229 kout = DH_compute_key(kbuf, dh_server_pub, dh);
231 debug("shared secret: len %d/%d", klen, kout);
232 fprintf(stderr, "shared secret == ");
233 for (i = 0; i< kout; i++)
234 fprintf(stderr, "%02x", (kbuf[i])&0xff);
235 fprintf(stderr, "\n");
237 shared_secret = BN_new();
239 BN_bin2bn(kbuf, kout, shared_secret);
240 memset(kbuf, 0, klen);
243 /* calc and verify H */
245 client_version_string,
246 server_version_string,
247 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
248 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
249 server_host_key_blob, sbloblen,
254 xfree(server_host_key_blob);
256 BN_free(dh_server_pub);
258 fprintf(stderr, "hash == ");
259 for (i = 0; i< 20; i++)
260 fprintf(stderr, "%02x", (hash[i])&0xff);
261 fprintf(stderr, "\n");
263 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
264 fatal("key_verify failed for server_host_key");
265 key_free(server_host_key);
268 kex_derive_keys(kex, hash, shared_secret);
269 BN_clear_free(shared_secret);
272 /* save session id */
273 session_id2_len = 20;
274 session_id2 = xmalloc(session_id2_len);
275 memcpy(session_id2, hash, session_id2_len);
278 /* diffie-hellman-group-exchange-sha1 */
281 * Estimates the group order for a Diffie-Hellman group that has an
282 * attack complexity approximately the same as O(2**bits). Estimate
283 * with: O(exp(1.9223 * (ln q)^(1/3) (ln ln q)^(2/3)))
287 dh_estimate(int bits)
291 return (512); /* O(2**63) */
293 return (1024); /* O(2**86) */
295 return (2048); /* O(2**116) */
296 return (4096); /* O(2**156) */
300 ssh_dhgex_client(Kex *kex, char *host, struct sockaddr *hostaddr,
301 Buffer *client_kexinit, Buffer *server_kexinit)
308 char *signature = NULL;
310 char *server_host_key_blob = NULL;
311 Key *server_host_key;
314 BIGNUM *dh_server_pub = 0;
315 BIGNUM *shared_secret = 0;
316 BIGNUM *p = 0, *g = 0;
320 nbits = dh_estimate(kex->we_need * 8);
322 debug("Sending SSH2_MSG_KEX_DH_GEX_REQUEST.");
323 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
324 packet_put_int(nbits);
329 fprintf(stderr, "\nnbits = %d", nbits);
332 debug("Wait SSH2_MSG_KEX_DH_GEX_GROUP.");
334 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_GROUP);
336 debug("Got SSH2_MSG_KEX_DH_GEX_GROUP.");
338 if ((p = BN_new()) == NULL)
340 packet_get_bignum2(p, &dlen);
341 if ((g = BN_new()) == NULL)
343 packet_get_bignum2(g, &dlen);
344 dh = dh_new_group(g, p);
346 dh_gen_key(dh, kex->we_need * 8);
349 fprintf(stderr, "\np= ");
350 BN_print_fp(stderr, dh->p);
351 fprintf(stderr, "\ng= ");
352 BN_print_fp(stderr, dh->g);
353 fprintf(stderr, "\npub= ");
354 BN_print_fp(stderr, dh->pub_key);
355 fprintf(stderr, "\n");
356 DHparams_print_fp(stderr, dh);
359 debug("Sending SSH2_MSG_KEX_DH_GEX_INIT.");
360 /* generate and send 'e', client DH public key */
361 packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
362 packet_put_bignum2(dh->pub_key);
366 debug("Wait SSH2_MSG_KEX_DH_GEX_REPLY.");
368 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_REPLY);
370 debug("Got SSH2_MSG_KEXDH_REPLY.");
373 server_host_key_blob = packet_get_string(&sbloblen);
374 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
375 if (server_host_key == NULL)
376 fatal("cannot decode server_host_key_blob");
378 check_host_key(host, hostaddr, server_host_key,
379 options.user_hostfile2, options.system_hostfile2);
381 /* DH paramter f, server public DH key */
382 dh_server_pub = BN_new();
383 if (dh_server_pub == NULL)
384 fatal("dh_server_pub == NULL");
385 packet_get_bignum2(dh_server_pub, &dlen);
388 fprintf(stderr, "\ndh_server_pub= ");
389 BN_print_fp(stderr, dh_server_pub);
390 fprintf(stderr, "\n");
391 debug("bits %d", BN_num_bits(dh_server_pub));
395 signature = packet_get_string(&slen);
398 if (!dh_pub_is_valid(dh, dh_server_pub))
399 packet_disconnect("bad server public DH value");
402 kbuf = xmalloc(klen);
403 kout = DH_compute_key(kbuf, dh_server_pub, dh);
405 debug("shared secret: len %d/%d", klen, kout);
406 fprintf(stderr, "shared secret == ");
407 for (i = 0; i< kout; i++)
408 fprintf(stderr, "%02x", (kbuf[i])&0xff);
409 fprintf(stderr, "\n");
411 shared_secret = BN_new();
413 BN_bin2bn(kbuf, kout, shared_secret);
414 memset(kbuf, 0, klen);
417 /* calc and verify H */
419 client_version_string,
420 server_version_string,
421 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
422 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
423 server_host_key_blob, sbloblen,
429 xfree(server_host_key_blob);
431 BN_free(dh_server_pub);
433 fprintf(stderr, "hash == ");
434 for (i = 0; i< 20; i++)
435 fprintf(stderr, "%02x", (hash[i])&0xff);
436 fprintf(stderr, "\n");
438 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
439 fatal("key_verify failed for server_host_key");
440 key_free(server_host_key);
443 kex_derive_keys(kex, hash, shared_secret);
444 BN_clear_free(shared_secret);
447 /* save session id */
448 session_id2_len = 20;
449 session_id2 = xmalloc(session_id2_len);
450 memcpy(session_id2, hash, session_id2_len);
457 typedef struct Authctxt Authctxt;
458 typedef struct Authmethod Authmethod;
460 typedef int sign_cb_fn(
461 Authctxt *authctxt, Key *key,
462 u_char **sigp, int *lenp, u_char *data, int datalen);
465 const char *server_user;
468 AuthenticationConnection *agent;
473 sign_cb_fn *last_key_sign;
477 char *name; /* string to compare against server's list */
478 int (*userauth)(Authctxt *authctxt);
479 int *enabled; /* flag in option struct that enables method */
480 int *batch_flag; /* flag in option struct that disables method */
483 void input_userauth_success(int type, int plen, void *ctxt);
484 void input_userauth_failure(int type, int plen, void *ctxt);
485 void input_userauth_banner(int type, int plen, void *ctxt);
486 void input_userauth_error(int type, int plen, void *ctxt);
487 void input_userauth_info_req(int type, int plen, void *ctxt);
488 void input_userauth_pk_ok(int type, int plen, void *ctxt);
490 int userauth_none(Authctxt *authctxt);
491 int userauth_pubkey(Authctxt *authctxt);
492 int userauth_passwd(Authctxt *authctxt);
493 int userauth_kbdint(Authctxt *authctxt);
495 void userauth(Authctxt *authctxt, char *authlist);
498 sign_and_send_pubkey(Authctxt *authctxt, Key *k,
499 sign_cb_fn *sign_callback);
500 void clear_auth_state(Authctxt *authctxt);
502 Authmethod *authmethod_get(char *authlist);
503 Authmethod *authmethod_lookup(const char *name);
504 char *authmethods_get(void);
506 Authmethod authmethods[] = {
509 &options.pubkey_authentication,
513 &options.password_authentication,
514 &options.batch_mode},
515 {"keyboard-interactive",
517 &options.kbd_interactive_authentication,
518 &options.batch_mode},
523 {NULL, NULL, NULL, NULL}
527 ssh_userauth2(const char *server_user, char *host)
533 if (options.challenge_reponse_authentication)
534 options.kbd_interactive_authentication = 1;
536 debug("send SSH2_MSG_SERVICE_REQUEST");
537 packet_start(SSH2_MSG_SERVICE_REQUEST);
538 packet_put_cstring("ssh-userauth");
541 type = packet_read(&plen);
542 if (type != SSH2_MSG_SERVICE_ACCEPT) {
543 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
545 if (packet_remaining() > 0) {
546 char *reply = packet_get_string(&plen);
547 debug("service_accept: %s", reply);
550 debug("buggy server: service_accept w/o service");
553 debug("got SSH2_MSG_SERVICE_ACCEPT");
555 if (options.preferred_authentications == NULL)
556 options.preferred_authentications = authmethods_get();
558 /* setup authentication context */
559 authctxt.agent = ssh_get_authentication_connection();
560 authctxt.server_user = server_user;
561 authctxt.host = host;
562 authctxt.service = "ssh-connection"; /* service name */
563 authctxt.success = 0;
564 authctxt.method = authmethod_lookup("none");
565 authctxt.authlist = NULL;
566 if (authctxt.method == NULL)
567 fatal("ssh_userauth2: internal error: cannot send userauth none request");
569 /* initial userauth request */
570 userauth_none(&authctxt);
572 dispatch_init(&input_userauth_error);
573 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
574 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
575 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
576 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
578 if (authctxt.agent != NULL)
579 ssh_close_authentication_connection(authctxt.agent);
581 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
584 userauth(Authctxt *authctxt, char *authlist)
586 if (authlist == NULL) {
587 authlist = authctxt->authlist;
589 if (authctxt->authlist)
590 xfree(authctxt->authlist);
591 authctxt->authlist = authlist;
594 Authmethod *method = authmethod_get(authlist);
596 fatal("Permission denied (%s).", authlist);
597 authctxt->method = method;
598 if (method->userauth(authctxt) != 0) {
599 debug2("we sent a %s packet, wait for reply", method->name);
602 debug2("we did not send a packet, disable method");
603 method->enabled = NULL;
608 input_userauth_error(int type, int plen, void *ctxt)
610 fatal("input_userauth_error: bad message during authentication: "
614 input_userauth_banner(int type, int plen, void *ctxt)
617 debug3("input_userauth_banner");
618 msg = packet_get_string(NULL);
619 lang = packet_get_string(NULL);
620 fprintf(stderr, "%s", msg);
625 input_userauth_success(int type, int plen, void *ctxt)
627 Authctxt *authctxt = ctxt;
628 if (authctxt == NULL)
629 fatal("input_userauth_success: no authentication context");
630 if (authctxt->authlist)
631 xfree(authctxt->authlist);
632 clear_auth_state(authctxt);
633 authctxt->success = 1; /* break out */
636 input_userauth_failure(int type, int plen, void *ctxt)
638 Authctxt *authctxt = ctxt;
639 char *authlist = NULL;
642 if (authctxt == NULL)
643 fatal("input_userauth_failure: no authentication context");
645 authlist = packet_get_string(NULL);
646 partial = packet_get_char();
650 log("Authenticated with partial success.");
651 debug("authentications that can continue: %s", authlist);
653 clear_auth_state(authctxt);
654 userauth(authctxt, authlist);
657 input_userauth_pk_ok(int type, int plen, void *ctxt)
659 Authctxt *authctxt = ctxt;
662 int alen, blen, pktype, sent = 0;
663 char *pkalg, *pkblob, *fp;
665 if (authctxt == NULL)
666 fatal("input_userauth_pk_ok: no authentication context");
667 if (datafellows & SSH_BUG_PKOK) {
668 /* this is similar to SSH_BUG_PKAUTH */
669 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
670 pkblob = packet_get_string(&blen);
672 buffer_append(&b, pkblob, blen);
673 pkalg = buffer_get_string(&b, &alen);
676 pkalg = packet_get_string(&alen);
677 pkblob = packet_get_string(&blen);
681 debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d",
682 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
685 if (authctxt->last_key == NULL ||
686 authctxt->last_key_sign == NULL) {
687 debug("no last key or no sign cb");
690 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
691 debug("unknown pkalg %s", pkalg);
694 if ((key = key_from_blob(pkblob, blen)) == NULL) {
695 debug("no key from blob. pkalg %s", pkalg);
698 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
699 debug2("input_userauth_pk_ok: fp %s", fp);
701 if (!key_equal(key, authctxt->last_key)) {
702 debug("key != last_key");
705 sent = sign_and_send_pubkey(authctxt, key,
706 authctxt->last_key_sign);
715 clear_auth_state(authctxt);
716 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
718 /* try another method if we did not send a packet*/
720 userauth(authctxt, NULL);
725 userauth_none(Authctxt *authctxt)
727 /* initial userauth request */
728 packet_start(SSH2_MSG_USERAUTH_REQUEST);
729 packet_put_cstring(authctxt->server_user);
730 packet_put_cstring(authctxt->service);
731 packet_put_cstring(authctxt->method->name);
737 userauth_passwd(Authctxt *authctxt)
739 static int attempt = 0;
743 if (attempt++ >= options.number_of_password_prompts)
747 error("Permission denied, please try again.");
749 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
750 authctxt->server_user, authctxt->host);
751 password = read_passphrase(prompt, 0);
752 packet_start(SSH2_MSG_USERAUTH_REQUEST);
753 packet_put_cstring(authctxt->server_user);
754 packet_put_cstring(authctxt->service);
755 packet_put_cstring(authctxt->method->name);
757 packet_put_cstring(password);
758 memset(password, 0, strlen(password));
760 packet_inject_ignore(64);
766 clear_auth_state(Authctxt *authctxt)
768 /* XXX clear authentication state */
769 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
770 debug3("clear_auth_state: key_free %p", authctxt->last_key);
771 key_free(authctxt->last_key);
773 authctxt->last_key = NULL;
774 authctxt->last_key_hint = -2;
775 authctxt->last_key_sign = NULL;
779 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
782 u_char *blob, *signature;
788 debug3("sign_and_send_pubkey");
790 if (key_to_blob(k, &blob, &bloblen) == 0) {
791 /* we cannot handle this key */
792 debug3("sign_and_send_pubkey: cannot handle key");
795 /* data to be signed */
797 if (datafellows & SSH_OLD_SESSIONID) {
798 buffer_append(&b, session_id2, session_id2_len);
799 skip = session_id2_len;
801 buffer_put_string(&b, session_id2, session_id2_len);
802 skip = buffer_len(&b);
804 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
805 buffer_put_cstring(&b, authctxt->server_user);
806 buffer_put_cstring(&b,
807 datafellows & SSH_BUG_PKSERVICE ?
810 if (datafellows & SSH_BUG_PKAUTH) {
811 buffer_put_char(&b, have_sig);
813 buffer_put_cstring(&b, authctxt->method->name);
814 buffer_put_char(&b, have_sig);
815 buffer_put_cstring(&b, key_ssh_name(k));
817 buffer_put_string(&b, blob, bloblen);
819 /* generate signature */
820 ret = (*sign_callback)(authctxt, k, &signature, &slen,
821 buffer_ptr(&b), buffer_len(&b));
830 if (datafellows & SSH_BUG_PKSERVICE) {
832 buffer_append(&b, session_id2, session_id2_len);
833 skip = session_id2_len;
834 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
835 buffer_put_cstring(&b, authctxt->server_user);
836 buffer_put_cstring(&b, authctxt->service);
837 buffer_put_cstring(&b, authctxt->method->name);
838 buffer_put_char(&b, have_sig);
839 if (!(datafellows & SSH_BUG_PKAUTH))
840 buffer_put_cstring(&b, key_ssh_name(k));
841 buffer_put_string(&b, blob, bloblen);
845 /* append signature */
846 buffer_put_string(&b, signature, slen);
849 /* skip session id and packet type */
850 if (buffer_len(&b) < skip + 1)
851 fatal("userauth_pubkey: internal error");
852 buffer_consume(&b, skip + 1);
854 /* put remaining data from buffer into packet */
855 packet_start(SSH2_MSG_USERAUTH_REQUEST);
856 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
864 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
868 int bloblen, have_sig = 0;
870 debug3("send_pubkey_test");
872 if (key_to_blob(k, &blob, &bloblen) == 0) {
873 /* we cannot handle this key */
874 debug3("send_pubkey_test: cannot handle key");
877 /* register callback for USERAUTH_PK_OK message */
878 authctxt->last_key_sign = sign_callback;
879 authctxt->last_key_hint = hint;
880 authctxt->last_key = k;
881 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
883 packet_start(SSH2_MSG_USERAUTH_REQUEST);
884 packet_put_cstring(authctxt->server_user);
885 packet_put_cstring(authctxt->service);
886 packet_put_cstring(authctxt->method->name);
887 packet_put_char(have_sig);
888 if (!(datafellows & SSH_BUG_PKAUTH))
889 packet_put_cstring(key_ssh_name(k));
890 packet_put_string(blob, bloblen);
897 load_identity_file(char *filename)
900 char prompt[300], *passphrase;
901 int success = 0, quit, i;
904 if (stat(filename, &st) < 0) {
905 debug3("no such identity: %s", filename);
908 private = key_new(KEY_UNSPEC);
909 if (!load_private_key(filename, "", private, NULL)) {
910 if (options.batch_mode) {
914 snprintf(prompt, sizeof prompt,
915 "Enter passphrase for key '%.100s': ", filename);
916 for (i = 0; i < options.number_of_password_prompts; i++) {
917 passphrase = read_passphrase(prompt, 0);
918 if (strcmp(passphrase, "") != 0) {
919 success = load_private_key(filename,
920 passphrase, private, NULL);
923 debug2("no passphrase given, try next key");
926 memset(passphrase, 0, strlen(passphrase));
930 debug2("bad passphrase given, try again...");
941 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
942 u_char *data, int datalen)
947 idx = authctxt->last_key_hint;
950 private = load_identity_file(options.identity_files[idx]);
953 ret = key_sign(private, sigp, lenp, data, datalen);
958 int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
959 u_char *data, int datalen)
961 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
964 int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
965 u_char *data, int datalen)
967 return key_sign(key, sigp, lenp, data, datalen);
971 userauth_pubkey_agent(Authctxt *authctxt)
973 static int called = 0;
979 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
980 debug2("userauth_pubkey_agent: no keys at all");
983 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
985 debug2("userauth_pubkey_agent: no more keys");
987 debug("userauth_pubkey_agent: testing agent key %s", comment);
989 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
994 debug2("userauth_pubkey_agent: no message sent");
999 userauth_pubkey(Authctxt *authctxt)
1006 if (authctxt->agent != NULL) {
1008 sent = userauth_pubkey_agent(authctxt);
1009 } while(!sent && authctxt->agent->howmany > 0);
1011 while (!sent && idx < options.num_identity_files) {
1012 key = options.identity_keys[idx];
1013 filename = options.identity_files[idx];
1015 debug("try privkey: %s", filename);
1016 key = load_identity_file(filename);
1018 sent = sign_and_send_pubkey(authctxt, key,
1022 } else if (key->type != KEY_RSA1) {
1023 debug("try pubkey: %s", filename);
1024 sent = send_pubkey_test(authctxt, key,
1025 identity_sign_cb, idx);
1033 * Send userauth request message specifying keyboard-interactive method.
1036 userauth_kbdint(Authctxt *authctxt)
1038 static int attempt = 0;
1040 if (attempt++ >= options.number_of_password_prompts)
1043 debug2("userauth_kbdint");
1044 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1045 packet_put_cstring(authctxt->server_user);
1046 packet_put_cstring(authctxt->service);
1047 packet_put_cstring(authctxt->method->name);
1048 packet_put_cstring(""); /* lang */
1049 packet_put_cstring(options.kbd_interactive_devices ?
1050 options.kbd_interactive_devices : "");
1053 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1058 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1061 input_userauth_info_req(int type, int plen, void *ctxt)
1063 Authctxt *authctxt = ctxt;
1064 char *name, *inst, *lang, *prompt, *response;
1065 u_int num_prompts, i;
1068 debug2("input_userauth_info_req");
1070 if (authctxt == NULL)
1071 fatal("input_userauth_info_req: no authentication context");
1073 name = packet_get_string(NULL);
1074 inst = packet_get_string(NULL);
1075 lang = packet_get_string(NULL);
1076 if (strlen(name) > 0)
1078 if (strlen(inst) > 0)
1084 num_prompts = packet_get_int();
1086 * Begin to build info response packet based on prompts requested.
1087 * We commit to providing the correct number of responses, so if
1088 * further on we run into a problem that prevents this, we have to
1089 * be sure and clean this up and send a correct error response.
1091 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1092 packet_put_int(num_prompts);
1094 for (i = 0; i < num_prompts; i++) {
1095 prompt = packet_get_string(NULL);
1096 echo = packet_get_char();
1098 response = cli_prompt(prompt, echo);
1100 packet_put_cstring(response);
1101 memset(response, 0, strlen(response));
1105 packet_done(); /* done with parsing incoming message. */
1107 packet_inject_ignore(64);
1111 /* find auth method */
1114 * given auth method name, if configurable options permit this method fill
1115 * in auth_ident field and return true, otherwise return false.
1118 authmethod_is_enabled(Authmethod *method)
1122 /* return false if options indicate this method is disabled */
1123 if (method->enabled == NULL || *method->enabled == 0)
1125 /* return false if batch mode is enabled but method needs interactive mode */
1126 if (method->batch_flag != NULL && *method->batch_flag != 0)
1132 authmethod_lookup(const char *name)
1134 Authmethod *method = NULL;
1136 for (method = authmethods; method->name != NULL; method++)
1137 if (strcmp(name, method->name) == 0)
1139 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1143 /* XXX internal state */
1144 static Authmethod *current = NULL;
1145 static char *supported = NULL;
1146 static char *preferred = NULL;
1148 * Given the authentication method list sent by the server, return the
1149 * next method we should try. If the server initially sends a nil list,
1150 * use a built-in default list.
1153 authmethod_get(char *authlist)
1159 /* Use a suitable default if we're passed a nil list. */
1160 if (authlist == NULL || strlen(authlist) == 0)
1161 authlist = options.preferred_authentications;
1163 if (supported == NULL || strcmp(authlist, supported) != 0) {
1164 debug3("start over, passed a different list %s", authlist);
1165 if (supported != NULL)
1167 supported = xstrdup(authlist);
1168 preferred = options.preferred_authentications;
1169 debug3("preferred %s", preferred);
1171 } else if (current != NULL && authmethod_is_enabled(current))
1175 if ((name = match_list(preferred, supported, &next)) == NULL) {
1176 debug("no more auth methods to try");
1181 debug3("authmethod_lookup %s", name);
1182 debug3("remaining preferred: %s", preferred);
1183 if ((current = authmethod_lookup(name)) != NULL &&
1184 authmethod_is_enabled(current)) {
1185 debug3("authmethod_is_enabled %s", name);
1186 debug("next auth method to try is %s", name);
1195 authmethods_get(void)
1197 Authmethod *method = NULL;
1201 for (method = authmethods; method->name != NULL; method++) {
1202 if (authmethod_is_enabled(method)) {
1204 strlcat(buf, DELIM, sizeof buf);
1205 strlcat(buf, method->name, sizeof buf);
1208 return xstrdup(buf);