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.52 2001/03/10 12:48:27 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"
55 void ssh_dh1_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
56 void ssh_dhgex_client(Kex *, char *, struct sockaddr *, Buffer *, Buffer *);
59 extern char *client_version_string;
60 extern char *server_version_string;
61 extern Options options;
67 u_char *session_id2 = NULL;
68 int session_id2_len = 0;
71 ssh_kex2(char *host, struct sockaddr *hostaddr)
75 Buffer *client_kexinit, *server_kexinit;
76 char *sprop[PROPOSAL_MAX];
78 if (options.ciphers == (char *)-1) {
79 log("No valid ciphers for protocol version 2 given, using defaults.");
80 options.ciphers = NULL;
82 if (options.ciphers != NULL) {
83 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
84 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
86 if (options.compression) {
87 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
88 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
90 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
91 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
93 if (options.macs != NULL) {
94 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
95 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
98 /* buffers with raw kexinit messages */
99 server_kexinit = xmalloc(sizeof(*server_kexinit));
100 buffer_init(server_kexinit);
101 client_kexinit = kex_init(myproposal);
103 /* algorithm negotiation */
104 kex_exchange_kexinit(client_kexinit, server_kexinit, sprop);
105 kex = kex_choose_conf(myproposal, sprop, 0);
106 for (i = 0; i < PROPOSAL_MAX; i++)
109 /* server authentication and session key agreement */
110 switch(kex->kex_type) {
112 ssh_dh1_client(kex, host, hostaddr,
113 client_kexinit, server_kexinit);
116 ssh_dhgex_client(kex, host, hostaddr, client_kexinit,
120 fatal("Unsupported key exchange %d", kex->kex_type);
123 buffer_free(client_kexinit);
124 buffer_free(server_kexinit);
125 xfree(client_kexinit);
126 xfree(server_kexinit);
128 debug("Wait SSH2_MSG_NEWKEYS.");
129 packet_read_expect(&plen, SSH2_MSG_NEWKEYS);
131 debug("GOT SSH2_MSG_NEWKEYS.");
133 debug("send SSH2_MSG_NEWKEYS.");
134 packet_start(SSH2_MSG_NEWKEYS);
137 debug("done: send SSH2_MSG_NEWKEYS.");
140 /* send 1st encrypted/maced/compressed message */
141 packet_start(SSH2_MSG_IGNORE);
142 packet_put_cstring("markus");
146 debug("done: KEX2.");
149 /* diffie-hellman-group1-sha1 */
152 ssh_dh1_client(Kex *kex, char *host, struct sockaddr *hostaddr,
153 Buffer *client_kexinit, Buffer *server_kexinit)
160 char *signature = NULL;
162 char *server_host_key_blob = NULL;
163 Key *server_host_key;
166 BIGNUM *dh_server_pub = 0;
167 BIGNUM *shared_secret = 0;
171 debug("Sending SSH2_MSG_KEXDH_INIT.");
172 /* generate and send 'e', client DH public key */
173 dh = dh_new_group1();
174 dh_gen_key(dh, kex->we_need * 8);
175 packet_start(SSH2_MSG_KEXDH_INIT);
176 packet_put_bignum2(dh->pub_key);
181 fprintf(stderr, "\np= ");
182 BN_print_fp(stderr, dh->p);
183 fprintf(stderr, "\ng= ");
184 BN_print_fp(stderr, dh->g);
185 fprintf(stderr, "\npub= ");
186 BN_print_fp(stderr, dh->pub_key);
187 fprintf(stderr, "\n");
188 DHparams_print_fp(stderr, dh);
191 debug("Wait SSH2_MSG_KEXDH_REPLY.");
193 packet_read_expect(&plen, SSH2_MSG_KEXDH_REPLY);
195 debug("Got SSH2_MSG_KEXDH_REPLY.");
198 server_host_key_blob = packet_get_string(&sbloblen);
199 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
200 if (server_host_key == NULL)
201 fatal("cannot decode server_host_key_blob");
203 check_host_key(host, hostaddr, server_host_key,
204 options.user_hostfile2, options.system_hostfile2);
206 /* DH paramter f, server public DH key */
207 dh_server_pub = BN_new();
208 if (dh_server_pub == NULL)
209 fatal("dh_server_pub == NULL");
210 packet_get_bignum2(dh_server_pub, &dlen);
213 fprintf(stderr, "\ndh_server_pub= ");
214 BN_print_fp(stderr, dh_server_pub);
215 fprintf(stderr, "\n");
216 debug("bits %d", BN_num_bits(dh_server_pub));
220 signature = packet_get_string(&slen);
223 if (!dh_pub_is_valid(dh, dh_server_pub))
224 packet_disconnect("bad server public DH value");
227 kbuf = xmalloc(klen);
228 kout = DH_compute_key(kbuf, dh_server_pub, dh);
230 debug("shared secret: len %d/%d", klen, kout);
231 fprintf(stderr, "shared secret == ");
232 for (i = 0; i< kout; i++)
233 fprintf(stderr, "%02x", (kbuf[i])&0xff);
234 fprintf(stderr, "\n");
236 shared_secret = BN_new();
238 BN_bin2bn(kbuf, kout, shared_secret);
239 memset(kbuf, 0, klen);
242 /* calc and verify H */
244 client_version_string,
245 server_version_string,
246 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
247 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
248 server_host_key_blob, sbloblen,
253 xfree(server_host_key_blob);
255 BN_free(dh_server_pub);
257 fprintf(stderr, "hash == ");
258 for (i = 0; i< 20; i++)
259 fprintf(stderr, "%02x", (hash[i])&0xff);
260 fprintf(stderr, "\n");
262 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
263 fatal("key_verify failed for server_host_key");
264 key_free(server_host_key);
267 kex_derive_keys(kex, hash, shared_secret);
268 BN_clear_free(shared_secret);
271 /* save session id */
272 session_id2_len = 20;
273 session_id2 = xmalloc(session_id2_len);
274 memcpy(session_id2, hash, session_id2_len);
277 /* diffie-hellman-group-exchange-sha1 */
280 * Estimates the group order for a Diffie-Hellman group that has an
281 * attack complexity approximately the same as O(2**bits). Estimate
282 * with: O(exp(1.9223 * (ln q)^(1/3) (ln ln q)^(2/3)))
286 dh_estimate(int bits)
290 return (512); /* O(2**63) */
292 return (1024); /* O(2**86) */
294 return (2048); /* O(2**116) */
295 return (4096); /* O(2**156) */
299 ssh_dhgex_client(Kex *kex, char *host, struct sockaddr *hostaddr,
300 Buffer *client_kexinit, Buffer *server_kexinit)
307 char *signature = NULL;
309 char *server_host_key_blob = NULL;
310 Key *server_host_key;
313 BIGNUM *dh_server_pub = 0;
314 BIGNUM *shared_secret = 0;
315 BIGNUM *p = 0, *g = 0;
319 nbits = dh_estimate(kex->we_need * 8);
321 debug("Sending SSH2_MSG_KEX_DH_GEX_REQUEST.");
322 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
323 packet_put_int(nbits);
328 fprintf(stderr, "\nnbits = %d", nbits);
331 debug("Wait SSH2_MSG_KEX_DH_GEX_GROUP.");
333 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_GROUP);
335 debug("Got SSH2_MSG_KEX_DH_GEX_GROUP.");
337 if ((p = BN_new()) == NULL)
339 packet_get_bignum2(p, &dlen);
340 if ((g = BN_new()) == NULL)
342 packet_get_bignum2(g, &dlen);
343 dh = dh_new_group(g, p);
345 dh_gen_key(dh, kex->we_need * 8);
348 fprintf(stderr, "\np= ");
349 BN_print_fp(stderr, dh->p);
350 fprintf(stderr, "\ng= ");
351 BN_print_fp(stderr, dh->g);
352 fprintf(stderr, "\npub= ");
353 BN_print_fp(stderr, dh->pub_key);
354 fprintf(stderr, "\n");
355 DHparams_print_fp(stderr, dh);
358 debug("Sending SSH2_MSG_KEX_DH_GEX_INIT.");
359 /* generate and send 'e', client DH public key */
360 packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
361 packet_put_bignum2(dh->pub_key);
365 debug("Wait SSH2_MSG_KEX_DH_GEX_REPLY.");
367 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_REPLY);
369 debug("Got SSH2_MSG_KEXDH_REPLY.");
372 server_host_key_blob = packet_get_string(&sbloblen);
373 server_host_key = key_from_blob(server_host_key_blob, sbloblen);
374 if (server_host_key == NULL)
375 fatal("cannot decode server_host_key_blob");
377 check_host_key(host, hostaddr, server_host_key,
378 options.user_hostfile2, options.system_hostfile2);
380 /* DH paramter f, server public DH key */
381 dh_server_pub = BN_new();
382 if (dh_server_pub == NULL)
383 fatal("dh_server_pub == NULL");
384 packet_get_bignum2(dh_server_pub, &dlen);
387 fprintf(stderr, "\ndh_server_pub= ");
388 BN_print_fp(stderr, dh_server_pub);
389 fprintf(stderr, "\n");
390 debug("bits %d", BN_num_bits(dh_server_pub));
394 signature = packet_get_string(&slen);
397 if (!dh_pub_is_valid(dh, dh_server_pub))
398 packet_disconnect("bad server public DH value");
401 kbuf = xmalloc(klen);
402 kout = DH_compute_key(kbuf, dh_server_pub, dh);
404 debug("shared secret: len %d/%d", klen, kout);
405 fprintf(stderr, "shared secret == ");
406 for (i = 0; i< kout; i++)
407 fprintf(stderr, "%02x", (kbuf[i])&0xff);
408 fprintf(stderr, "\n");
410 shared_secret = BN_new();
412 BN_bin2bn(kbuf, kout, shared_secret);
413 memset(kbuf, 0, klen);
416 /* calc and verify H */
418 client_version_string,
419 server_version_string,
420 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
421 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
422 server_host_key_blob, sbloblen,
428 xfree(server_host_key_blob);
430 BN_free(dh_server_pub);
432 fprintf(stderr, "hash == ");
433 for (i = 0; i< 20; i++)
434 fprintf(stderr, "%02x", (hash[i])&0xff);
435 fprintf(stderr, "\n");
437 if (key_verify(server_host_key, (u_char *)signature, slen, hash, 20) != 1)
438 fatal("key_verify failed for server_host_key");
439 key_free(server_host_key);
442 kex_derive_keys(kex, hash, shared_secret);
443 BN_clear_free(shared_secret);
446 /* save session id */
447 session_id2_len = 20;
448 session_id2 = xmalloc(session_id2_len);
449 memcpy(session_id2, hash, session_id2_len);
456 typedef struct Authctxt Authctxt;
457 typedef struct Authmethod Authmethod;
459 typedef int sign_cb_fn(
460 Authctxt *authctxt, Key *key,
461 u_char **sigp, int *lenp, u_char *data, int datalen);
464 const char *server_user;
467 AuthenticationConnection *agent;
472 sign_cb_fn *last_key_sign;
476 char *name; /* string to compare against server's list */
477 int (*userauth)(Authctxt *authctxt);
478 int *enabled; /* flag in option struct that enables method */
479 int *batch_flag; /* flag in option struct that disables method */
482 void input_userauth_success(int type, int plen, void *ctxt);
483 void input_userauth_failure(int type, int plen, void *ctxt);
484 void input_userauth_banner(int type, int plen, void *ctxt);
485 void input_userauth_error(int type, int plen, void *ctxt);
486 void input_userauth_info_req(int type, int plen, void *ctxt);
487 void input_userauth_pk_ok(int type, int plen, void *ctxt);
489 int userauth_none(Authctxt *authctxt);
490 int userauth_pubkey(Authctxt *authctxt);
491 int userauth_passwd(Authctxt *authctxt);
492 int userauth_kbdint(Authctxt *authctxt);
494 void userauth(Authctxt *authctxt, char *authlist);
497 sign_and_send_pubkey(Authctxt *authctxt, Key *k,
498 sign_cb_fn *sign_callback);
499 void clear_auth_state(Authctxt *authctxt);
501 void authmethod_clear(void);
502 Authmethod *authmethod_get(char *authlist);
503 Authmethod *authmethod_lookup(const char *name);
505 Authmethod authmethods[] = {
508 &options.pubkey_authentication,
512 &options.password_authentication,
513 &options.batch_mode},
514 {"keyboard-interactive",
516 &options.kbd_interactive_authentication,
517 &options.batch_mode},
522 {NULL, NULL, NULL, NULL}
526 ssh_userauth2(const char *server_user, char *host)
532 if (options.challenge_reponse_authentication)
533 options.kbd_interactive_authentication = 1;
535 debug("send SSH2_MSG_SERVICE_REQUEST");
536 packet_start(SSH2_MSG_SERVICE_REQUEST);
537 packet_put_cstring("ssh-userauth");
540 type = packet_read(&plen);
541 if (type != SSH2_MSG_SERVICE_ACCEPT) {
542 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
544 if (packet_remaining() > 0) {
545 char *reply = packet_get_string(&plen);
546 debug("service_accept: %s", reply);
549 debug("buggy server: service_accept w/o service");
552 debug("got SSH2_MSG_SERVICE_ACCEPT");
554 /* setup authentication context */
555 authctxt.agent = ssh_get_authentication_connection();
556 authctxt.server_user = server_user;
557 authctxt.host = host;
558 authctxt.service = "ssh-connection"; /* service name */
559 authctxt.success = 0;
560 authctxt.method = authmethod_lookup("none");
561 authctxt.authlist = NULL;
562 if (authctxt.method == NULL)
563 fatal("ssh_userauth2: internal error: cannot send userauth none request");
566 /* initial userauth request */
567 userauth_none(&authctxt);
569 dispatch_init(&input_userauth_error);
570 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
571 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
572 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
573 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
575 if (authctxt.agent != NULL)
576 ssh_close_authentication_connection(authctxt.agent);
578 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
581 userauth(Authctxt *authctxt, char *authlist)
583 if (authlist == NULL) {
584 authlist = authctxt->authlist;
586 if (authctxt->authlist)
587 xfree(authctxt->authlist);
588 authctxt->authlist = authlist;
591 Authmethod *method = authmethod_get(authlist);
593 fatal("Permission denied (%s).", authlist);
594 authctxt->method = method;
595 if (method->userauth(authctxt) != 0) {
596 debug2("we sent a %s packet, wait for reply", method->name);
599 debug2("we did not send a packet, disable method");
600 method->enabled = NULL;
605 input_userauth_error(int type, int plen, void *ctxt)
607 fatal("input_userauth_error: bad message during authentication: "
611 input_userauth_banner(int type, int plen, void *ctxt)
614 debug3("input_userauth_banner");
615 msg = packet_get_string(NULL);
616 lang = packet_get_string(NULL);
617 fprintf(stderr, "%s", msg);
622 input_userauth_success(int type, int plen, void *ctxt)
624 Authctxt *authctxt = ctxt;
625 if (authctxt == NULL)
626 fatal("input_userauth_success: no authentication context");
627 if (authctxt->authlist)
628 xfree(authctxt->authlist);
629 clear_auth_state(authctxt);
630 authctxt->success = 1; /* break out */
633 input_userauth_failure(int type, int plen, void *ctxt)
635 Authctxt *authctxt = ctxt;
636 char *authlist = NULL;
639 if (authctxt == NULL)
640 fatal("input_userauth_failure: no authentication context");
642 authlist = packet_get_string(NULL);
643 partial = packet_get_char();
647 log("Authenticated with partial success.");
648 debug("authentications that can continue: %s", authlist);
650 clear_auth_state(authctxt);
651 userauth(authctxt, authlist);
654 input_userauth_pk_ok(int type, int plen, void *ctxt)
656 Authctxt *authctxt = ctxt;
659 int alen, blen, pktype, sent = 0;
660 char *pkalg, *pkblob;
662 if (authctxt == NULL)
663 fatal("input_userauth_pk_ok: no authentication context");
664 if (datafellows & SSH_BUG_PKOK) {
665 /* this is similar to SSH_BUG_PKAUTH */
666 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
667 pkblob = packet_get_string(&blen);
669 buffer_append(&b, pkblob, blen);
670 pkalg = buffer_get_string(&b, &alen);
673 pkalg = packet_get_string(&alen);
674 pkblob = packet_get_string(&blen);
678 debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d",
679 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
682 if (authctxt->last_key == NULL ||
683 authctxt->last_key_sign == NULL) {
684 debug("no last key or no sign cb");
687 debug2("last_key %s", key_fingerprint(authctxt->last_key));
688 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
689 debug("unknown pkalg %s", pkalg);
692 if ((key = key_from_blob(pkblob, blen)) == NULL) {
693 debug("no key from blob. pkalg %s", pkalg);
696 debug2("input_userauth_pk_ok: fp %s", key_fingerprint(key));
697 if (!key_equal(key, authctxt->last_key)) {
698 debug("key != last_key");
701 sent = sign_and_send_pubkey(authctxt, key,
702 authctxt->last_key_sign);
711 clear_auth_state(authctxt);
712 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
714 /* try another method if we did not send a packet*/
716 userauth(authctxt, NULL);
721 userauth_none(Authctxt *authctxt)
723 /* initial userauth request */
724 packet_start(SSH2_MSG_USERAUTH_REQUEST);
725 packet_put_cstring(authctxt->server_user);
726 packet_put_cstring(authctxt->service);
727 packet_put_cstring(authctxt->method->name);
733 userauth_passwd(Authctxt *authctxt)
735 static int attempt = 0;
739 if (attempt++ >= options.number_of_password_prompts)
743 error("Permission denied, please try again.");
745 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
746 authctxt->server_user, authctxt->host);
747 password = read_passphrase(prompt, 0);
748 packet_start(SSH2_MSG_USERAUTH_REQUEST);
749 packet_put_cstring(authctxt->server_user);
750 packet_put_cstring(authctxt->service);
751 packet_put_cstring(authctxt->method->name);
753 packet_put_cstring(password);
754 memset(password, 0, strlen(password));
756 packet_inject_ignore(64);
762 clear_auth_state(Authctxt *authctxt)
764 /* XXX clear authentication state */
765 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
766 debug3("clear_auth_state: key_free %p", authctxt->last_key);
767 key_free(authctxt->last_key);
769 authctxt->last_key = NULL;
770 authctxt->last_key_hint = -2;
771 authctxt->last_key_sign = NULL;
775 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
778 u_char *blob, *signature;
784 debug3("sign_and_send_pubkey");
786 if (key_to_blob(k, &blob, &bloblen) == 0) {
787 /* we cannot handle this key */
788 debug3("sign_and_send_pubkey: cannot handle key");
791 /* data to be signed */
793 if (datafellows & SSH_OLD_SESSIONID) {
794 buffer_append(&b, session_id2, session_id2_len);
795 skip = session_id2_len;
797 buffer_put_string(&b, session_id2, session_id2_len);
798 skip = buffer_len(&b);
800 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
801 buffer_put_cstring(&b, authctxt->server_user);
802 buffer_put_cstring(&b,
803 datafellows & SSH_BUG_PKSERVICE ?
806 if (datafellows & SSH_BUG_PKAUTH) {
807 buffer_put_char(&b, have_sig);
809 buffer_put_cstring(&b, authctxt->method->name);
810 buffer_put_char(&b, have_sig);
811 buffer_put_cstring(&b, key_ssh_name(k));
813 buffer_put_string(&b, blob, bloblen);
815 /* generate signature */
816 ret = (*sign_callback)(authctxt, k, &signature, &slen,
817 buffer_ptr(&b), buffer_len(&b));
826 if (datafellows & SSH_BUG_PKSERVICE) {
828 buffer_append(&b, session_id2, session_id2_len);
829 skip = session_id2_len;
830 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
831 buffer_put_cstring(&b, authctxt->server_user);
832 buffer_put_cstring(&b, authctxt->service);
833 buffer_put_cstring(&b, authctxt->method->name);
834 buffer_put_char(&b, have_sig);
835 if (!(datafellows & SSH_BUG_PKAUTH))
836 buffer_put_cstring(&b, key_ssh_name(k));
837 buffer_put_string(&b, blob, bloblen);
841 /* append signature */
842 buffer_put_string(&b, signature, slen);
845 /* skip session id and packet type */
846 if (buffer_len(&b) < skip + 1)
847 fatal("userauth_pubkey: internal error");
848 buffer_consume(&b, skip + 1);
850 /* put remaining data from buffer into packet */
851 packet_start(SSH2_MSG_USERAUTH_REQUEST);
852 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
860 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
864 int bloblen, have_sig = 0;
866 debug3("send_pubkey_test");
868 if (key_to_blob(k, &blob, &bloblen) == 0) {
869 /* we cannot handle this key */
870 debug3("send_pubkey_test: cannot handle key");
873 /* register callback for USERAUTH_PK_OK message */
874 authctxt->last_key_sign = sign_callback;
875 authctxt->last_key_hint = hint;
876 authctxt->last_key = k;
877 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
879 packet_start(SSH2_MSG_USERAUTH_REQUEST);
880 packet_put_cstring(authctxt->server_user);
881 packet_put_cstring(authctxt->service);
882 packet_put_cstring(authctxt->method->name);
883 packet_put_char(have_sig);
884 if (!(datafellows & SSH_BUG_PKAUTH))
885 packet_put_cstring(key_ssh_name(k));
886 packet_put_string(blob, bloblen);
893 load_identity_file(char *filename)
896 char prompt[300], *passphrase;
897 int success = 0, quit, i;
900 if (stat(filename, &st) < 0) {
901 debug3("no such identity: %s", filename);
904 private = key_new(KEY_UNSPEC);
905 if (!load_private_key(filename, "", private, NULL)) {
906 if (options.batch_mode) {
910 snprintf(prompt, sizeof prompt,
911 "Enter passphrase for key '%.100s': ", filename);
912 for (i = 0; i < options.number_of_password_prompts; i++) {
913 passphrase = read_passphrase(prompt, 0);
914 if (strcmp(passphrase, "") != 0) {
915 success = load_private_key(filename,
916 passphrase, private, NULL);
919 debug2("no passphrase given, try next key");
922 memset(passphrase, 0, strlen(passphrase));
926 debug2("bad passphrase given, try again...");
937 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
938 u_char *data, int datalen)
943 idx = authctxt->last_key_hint;
946 private = load_identity_file(options.identity_files[idx]);
949 ret = key_sign(private, sigp, lenp, data, datalen);
954 int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
955 u_char *data, int datalen)
957 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
960 int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
961 u_char *data, int datalen)
963 return key_sign(key, sigp, lenp, data, datalen);
967 userauth_pubkey_agent(Authctxt *authctxt)
969 static int called = 0;
975 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
976 debug2("userauth_pubkey_agent: no keys at all");
979 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
981 debug2("userauth_pubkey_agent: no more keys");
983 debug("userauth_pubkey_agent: testing agent key %s", comment);
985 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
990 debug2("userauth_pubkey_agent: no message sent");
995 userauth_pubkey(Authctxt *authctxt)
1002 if (authctxt->agent != NULL) {
1004 sent = userauth_pubkey_agent(authctxt);
1005 } while(!sent && authctxt->agent->howmany > 0);
1007 while (!sent && idx < options.num_identity_files) {
1008 key = options.identity_keys[idx];
1009 filename = options.identity_files[idx];
1011 debug("try privkey: %s", filename);
1012 key = load_identity_file(filename);
1014 sent = sign_and_send_pubkey(authctxt, key,
1018 } else if (key->type != KEY_RSA1) {
1019 debug("try pubkey: %s", filename);
1020 sent = send_pubkey_test(authctxt, key,
1021 identity_sign_cb, idx);
1029 * Send userauth request message specifying keyboard-interactive method.
1032 userauth_kbdint(Authctxt *authctxt)
1034 static int attempt = 0;
1036 if (attempt++ >= options.number_of_password_prompts)
1039 debug2("userauth_kbdint");
1040 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1041 packet_put_cstring(authctxt->server_user);
1042 packet_put_cstring(authctxt->service);
1043 packet_put_cstring(authctxt->method->name);
1044 packet_put_cstring(""); /* lang */
1045 packet_put_cstring(options.kbd_interactive_devices ?
1046 options.kbd_interactive_devices : "");
1049 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1054 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1057 input_userauth_info_req(int type, int plen, void *ctxt)
1059 Authctxt *authctxt = ctxt;
1060 char *name, *inst, *lang, *prompt, *response;
1061 u_int num_prompts, i;
1064 debug2("input_userauth_info_req");
1066 if (authctxt == NULL)
1067 fatal("input_userauth_info_req: no authentication context");
1069 name = packet_get_string(NULL);
1070 inst = packet_get_string(NULL);
1071 lang = packet_get_string(NULL);
1072 if (strlen(name) > 0)
1074 if (strlen(inst) > 0)
1080 num_prompts = packet_get_int();
1082 * Begin to build info response packet based on prompts requested.
1083 * We commit to providing the correct number of responses, so if
1084 * further on we run into a problem that prevents this, we have to
1085 * be sure and clean this up and send a correct error response.
1087 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1088 packet_put_int(num_prompts);
1090 for (i = 0; i < num_prompts; i++) {
1091 prompt = packet_get_string(NULL);
1092 echo = packet_get_char();
1094 response = cli_prompt(prompt, echo);
1096 packet_put_cstring(response);
1097 memset(response, 0, strlen(response));
1101 packet_done(); /* done with parsing incoming message. */
1103 packet_inject_ignore(64);
1107 /* find auth method */
1111 static char *def_authlist = "publickey,password";
1112 static char *authlist_current = NULL; /* clean copy used for comparison */
1113 static char *authname_current = NULL; /* last used auth method */
1114 static char *authlist_working = NULL; /* copy that gets modified by strtok_r() */
1115 static char *authlist_state = NULL; /* state variable for strtok_r() */
1118 * Before starting to use a new authentication method list sent by the
1119 * server, reset internal variables. This should also be called when
1120 * finished processing server list to free resources.
1123 authmethod_clear(void)
1125 if (authlist_current != NULL) {
1126 xfree(authlist_current);
1127 authlist_current = NULL;
1129 if (authlist_working != NULL) {
1130 xfree(authlist_working);
1131 authlist_working = NULL;
1133 if (authname_current != NULL) {
1134 xfree(authname_current);
1135 authname_current = NULL;
1137 if (authlist_state != NULL)
1138 authlist_state = NULL;
1143 * given auth method name, if configurable options permit this method fill
1144 * in auth_ident field and return true, otherwise return false.
1147 authmethod_is_enabled(Authmethod *method)
1151 /* return false if options indicate this method is disabled */
1152 if (method->enabled == NULL || *method->enabled == 0)
1154 /* return false if batch mode is enabled but method needs interactive mode */
1155 if (method->batch_flag != NULL && *method->batch_flag != 0)
1161 authmethod_lookup(const char *name)
1163 Authmethod *method = NULL;
1165 for (method = authmethods; method->name != NULL; method++)
1166 if (strcmp(name, method->name) == 0)
1168 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1173 * Given the authentication method list sent by the server, return the
1174 * next method we should try. If the server initially sends a nil list,
1175 * use a built-in default list. If the server sends a nil list after
1176 * previously sending a valid list, continue using the list originally
1181 authmethod_get(char *authlist)
1183 char *name = NULL, *authname_old;
1184 Authmethod *method = NULL;
1186 /* Use a suitable default if we're passed a nil list. */
1187 if (authlist == NULL || strlen(authlist) == 0)
1188 authlist = def_authlist;
1190 if (authlist_current == NULL || strcmp(authlist, authlist_current) != 0) {
1191 /* start over if passed a different list */
1192 debug3("start over, passed a different list");
1194 authlist_current = xstrdup(authlist);
1195 authlist_working = xstrdup(authlist);
1196 name = strtok_r(authlist_working, DELIM, &authlist_state);
1199 * try to use previously used authentication method
1200 * or continue to use previously passed list
1202 name = (authname_current != NULL) ?
1203 authname_current : strtok_r(NULL, DELIM, &authlist_state);
1206 while (name != NULL) {
1207 debug3("authmethod_lookup %s", name);
1208 method = authmethod_lookup(name);
1209 if (method != NULL && authmethod_is_enabled(method)) {
1210 debug3("authmethod_is_enabled %s", name);
1213 name = strtok_r(NULL, DELIM, &authlist_state);
1217 authname_old = authname_current;
1218 if (method != NULL) {
1219 debug("next auth method to try is %s", name);
1220 authname_current = xstrdup(name);
1222 debug("no more auth methods to try");
1223 authname_current = NULL;
1226 if (authname_old != NULL)
1227 xfree(authname_old);