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.25 2000/10/12 09:59:19 markus Exp $");
28 #include <openssl/bn.h>
29 #include <openssl/rsa.h>
30 #include <openssl/dsa.h>
31 #include <openssl/md5.h>
32 #include <openssl/dh.h>
33 #include <openssl/hmac.h>
46 #include "myproposal.h"
49 #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 unsigned 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 == NULL) {
79 if (options.cipher == SSH_CIPHER_3DES) {
80 options.ciphers = "3des-cbc";
81 } else if (options.cipher == SSH_CIPHER_BLOWFISH) {
82 options.ciphers = "blowfish-cbc";
83 } else if (options.cipher == SSH_CIPHER_DES) {
84 fatal("cipher DES not supported for protocol version 2");
87 if (options.ciphers != NULL) {
88 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
89 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
91 if (options.compression) {
92 myproposal[PROPOSAL_COMP_ALGS_CTOS] = "zlib";
93 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
95 myproposal[PROPOSAL_COMP_ALGS_CTOS] = "none";
96 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
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)
160 unsigned int klen, kout;
161 char *signature = NULL;
163 char *server_host_key_blob = NULL;
164 Key *server_host_key;
165 unsigned int sbloblen;
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 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 = dsa_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);
256 fprintf(stderr, "hash == ");
257 for (i = 0; i< 20; i++)
258 fprintf(stderr, "%02x", (hash[i])&0xff);
259 fprintf(stderr, "\n");
261 if (dsa_verify(server_host_key, (unsigned char *)signature, slen, hash, 20) != 1)
262 fatal("dsa_verify failed for server_host_key");
263 key_free(server_host_key);
265 kex_derive_keys(kex, hash, shared_secret);
268 /* save session id */
269 session_id2_len = 20;
270 session_id2 = xmalloc(session_id2_len);
271 memcpy(session_id2, hash, session_id2_len);
274 /* diffie-hellman-group-exchange-sha1 */
277 * Estimates the group order for a Diffie-Hellman group that has an
278 * attack complexity approximately the same as O(2**bits). Estimate
279 * with: O(exp(1.9223 * (ln q)^(1/3) (ln ln q)^(2/3)))
283 dh_estimate(int bits)
287 return (512); /* O(2**63) */
289 return (1024); /* O(2**86) */
291 return (2048); /* O(2**116) */
292 return (4096); /* O(2**156) */
296 ssh_dhgex_client(Kex *kex, char *host, struct sockaddr *hostaddr,
297 Buffer *client_kexinit, Buffer *server_kexinit)
303 unsigned int klen, kout;
304 char *signature = NULL;
305 unsigned int slen, nbits;
306 char *server_host_key_blob = NULL;
307 Key *server_host_key;
308 unsigned int sbloblen;
310 BIGNUM *dh_server_pub = 0;
311 BIGNUM *shared_secret = 0;
312 BIGNUM *p = 0, *g = 0;
316 nbits = dh_estimate(kex->enc[MODE_OUT].cipher->key_len * 8);
318 debug("Sending SSH2_MSG_KEX_DH_GEX_REQUEST.");
319 packet_start(SSH2_MSG_KEX_DH_GEX_REQUEST);
320 packet_put_int(nbits);
325 fprintf(stderr, "\nnbits = %d", nbits);
328 debug("Wait SSH2_MSG_KEX_DH_GEX_GROUP.");
330 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_GROUP);
332 debug("Got SSH2_MSG_KEX_DH_GEX_GROUP.");
334 if ((p = BN_new()) == NULL)
336 packet_get_bignum2(p, &dlen);
337 if ((g = BN_new()) == NULL)
339 packet_get_bignum2(g, &dlen);
340 if ((dh = dh_new_group(g, p)) == NULL)
341 fatal("dh_new_group");
344 fprintf(stderr, "\np= ");
345 BN_print_fp(stderr, dh->p);
346 fprintf(stderr, "\ng= ");
347 BN_print_fp(stderr, dh->g);
348 fprintf(stderr, "\npub= ");
349 BN_print_fp(stderr, dh->pub_key);
350 fprintf(stderr, "\n");
351 DHparams_print_fp(stderr, dh);
354 debug("Sending SSH2_MSG_KEX_DH_GEX_INIT.");
355 /* generate and send 'e', client DH public key */
356 packet_start(SSH2_MSG_KEX_DH_GEX_INIT);
357 packet_put_bignum2(dh->pub_key);
361 debug("Wait SSH2_MSG_KEX_DH_GEX_REPLY.");
363 packet_read_expect(&plen, SSH2_MSG_KEX_DH_GEX_REPLY);
365 debug("Got SSH2_MSG_KEXDH_REPLY.");
368 server_host_key_blob = packet_get_string(&sbloblen);
369 server_host_key = dsa_key_from_blob(server_host_key_blob, sbloblen);
370 if (server_host_key == NULL)
371 fatal("cannot decode server_host_key_blob");
373 check_host_key(host, hostaddr, server_host_key,
374 options.user_hostfile2, options.system_hostfile2);
376 /* DH paramter f, server public DH key */
377 dh_server_pub = BN_new();
378 if (dh_server_pub == NULL)
379 fatal("dh_server_pub == NULL");
380 packet_get_bignum2(dh_server_pub, &dlen);
383 fprintf(stderr, "\ndh_server_pub= ");
384 BN_print_fp(stderr, dh_server_pub);
385 fprintf(stderr, "\n");
386 debug("bits %d", BN_num_bits(dh_server_pub));
390 signature = packet_get_string(&slen);
393 if (!dh_pub_is_valid(dh, dh_server_pub))
394 packet_disconnect("bad server public DH value");
397 kbuf = xmalloc(klen);
398 kout = DH_compute_key(kbuf, dh_server_pub, dh);
400 debug("shared secret: len %d/%d", klen, kout);
401 fprintf(stderr, "shared secret == ");
402 for (i = 0; i< kout; i++)
403 fprintf(stderr, "%02x", (kbuf[i])&0xff);
404 fprintf(stderr, "\n");
406 shared_secret = BN_new();
408 BN_bin2bn(kbuf, kout, shared_secret);
409 memset(kbuf, 0, klen);
412 /* calc and verify H */
414 client_version_string,
415 server_version_string,
416 buffer_ptr(client_kexinit), buffer_len(client_kexinit),
417 buffer_ptr(server_kexinit), buffer_len(server_kexinit),
418 server_host_key_blob, sbloblen,
424 xfree(server_host_key_blob);
427 fprintf(stderr, "hash == ");
428 for (i = 0; i< 20; i++)
429 fprintf(stderr, "%02x", (hash[i])&0xff);
430 fprintf(stderr, "\n");
432 if (dsa_verify(server_host_key, (unsigned char *)signature, slen, hash, 20) != 1)
433 fatal("dsa_verify failed for server_host_key");
434 key_free(server_host_key);
436 kex_derive_keys(kex, hash, shared_secret);
439 /* save session id */
440 session_id2_len = 20;
441 session_id2 = xmalloc(session_id2_len);
442 memcpy(session_id2, hash, session_id2_len);
449 typedef struct Authctxt Authctxt;
450 typedef struct Authmethod Authmethod;
452 typedef int sign_cb_fn(
453 Authctxt *authctxt, Key *key,
454 unsigned char **sigp, int *lenp, unsigned char *data, int datalen);
457 const char *server_user;
460 AuthenticationConnection *agent;
465 char *name; /* string to compare against server's list */
466 int (*userauth)(Authctxt *authctxt);
467 int *enabled; /* flag in option struct that enables method */
468 int *batch_flag; /* flag in option struct that disables method */
471 void input_userauth_success(int type, int plen, void *ctxt);
472 void input_userauth_failure(int type, int plen, void *ctxt);
473 void input_userauth_error(int type, int plen, void *ctxt);
474 void input_userauth_info_req(int type, int plen, void *ctxt);
476 int userauth_none(Authctxt *authctxt);
477 int userauth_pubkey(Authctxt *authctxt);
478 int userauth_passwd(Authctxt *authctxt);
479 int userauth_kbdint(Authctxt *authctxt);
481 void authmethod_clear();
482 Authmethod *authmethod_get(char *authlist);
483 Authmethod *authmethod_lookup(const char *name);
485 Authmethod authmethods[] = {
488 &options.dsa_authentication,
492 &options.password_authentication,
493 &options.batch_mode},
494 {"keyboard-interactive",
496 &options.kbd_interactive_authentication,
497 &options.batch_mode},
502 {NULL, NULL, NULL, NULL}
506 ssh_userauth2(const char *server_user, char *host)
512 debug("send SSH2_MSG_SERVICE_REQUEST");
513 packet_start(SSH2_MSG_SERVICE_REQUEST);
514 packet_put_cstring("ssh-userauth");
517 type = packet_read(&plen);
518 if (type != SSH2_MSG_SERVICE_ACCEPT) {
519 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
521 if (packet_remaining() > 0) {
522 char *reply = packet_get_string(&plen);
523 debug("service_accept: %s", reply);
527 debug("buggy server: service_accept w/o service");
530 debug("got SSH2_MSG_SERVICE_ACCEPT");
532 /* setup authentication context */
533 authctxt.agent = ssh_get_authentication_connection();
534 authctxt.server_user = server_user;
535 authctxt.host = host;
536 authctxt.service = "ssh-connection"; /* service name */
537 authctxt.success = 0;
538 authctxt.method = authmethod_lookup("none");
539 if (authctxt.method == NULL)
540 fatal("ssh_userauth2: internal error: cannot send userauth none request");
543 /* initial userauth request */
544 userauth_none(&authctxt);
546 dispatch_init(&input_userauth_error);
547 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
548 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
549 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
551 if (authctxt.agent != NULL)
552 ssh_close_authentication_connection(authctxt.agent);
554 debug("ssh-userauth2 successfull: method %s", authctxt.method->name);
557 input_userauth_error(int type, int plen, void *ctxt)
559 fatal("input_userauth_error: bad message during authentication");
562 input_userauth_success(int type, int plen, void *ctxt)
564 Authctxt *authctxt = ctxt;
565 if (authctxt == NULL)
566 fatal("input_userauth_success: no authentication context");
567 authctxt->success = 1; /* break out */
570 input_userauth_failure(int type, int plen, void *ctxt)
572 Authmethod *method = NULL;
573 Authctxt *authctxt = ctxt;
574 char *authlist = NULL;
577 if (authctxt == NULL)
578 fatal("input_userauth_failure: no authentication context");
580 authlist = packet_get_string(NULL);
581 partial = packet_get_char();
585 debug("partial success");
586 debug("authentications that can continue: %s", authlist);
589 method = authmethod_get(authlist);
591 fatal("Unable to find an authentication method");
592 authctxt->method = method;
593 if (method->userauth(authctxt) != 0) {
594 debug2("we sent a %s packet, wait for reply", method->name);
597 debug2("we did not send a packet, disable method");
598 method->enabled = NULL;
605 userauth_none(Authctxt *authctxt)
607 /* initial userauth request */
608 packet_start(SSH2_MSG_USERAUTH_REQUEST);
609 packet_put_cstring(authctxt->server_user);
610 packet_put_cstring(authctxt->service);
611 packet_put_cstring(authctxt->method->name);
618 userauth_passwd(Authctxt *authctxt)
620 static int attempt = 0;
624 if (attempt++ >= options.number_of_password_prompts)
628 error("Permission denied, please try again.");
630 snprintf(prompt, sizeof(prompt), "%.30s@%.40s's password: ",
631 authctxt->server_user, authctxt->host);
632 password = read_passphrase(prompt, 0);
633 packet_start(SSH2_MSG_USERAUTH_REQUEST);
634 packet_put_cstring(authctxt->server_user);
635 packet_put_cstring(authctxt->service);
636 packet_put_cstring(authctxt->method->name);
638 packet_put_cstring(password);
639 memset(password, 0, strlen(password));
647 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
650 unsigned char *blob, *signature;
656 dsa_make_key_blob(k, &blob, &bloblen);
658 /* data to be signed */
660 if (datafellows & SSH_COMPAT_SESSIONID_ENCODING) {
661 buffer_put_string(&b, session_id2, session_id2_len);
662 skip = buffer_len(&b);
664 buffer_append(&b, session_id2, session_id2_len);
665 skip = session_id2_len;
667 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
668 buffer_put_cstring(&b, authctxt->server_user);
669 buffer_put_cstring(&b,
670 datafellows & SSH_BUG_PUBKEYAUTH ?
673 buffer_put_cstring(&b, authctxt->method->name);
674 buffer_put_char(&b, have_sig);
675 buffer_put_cstring(&b, KEX_DSS);
676 buffer_put_string(&b, blob, bloblen);
678 /* generate signature */
679 ret = (*sign_callback)(authctxt, k, &signature, &slen, buffer_ptr(&b), buffer_len(&b));
688 if (datafellows & SSH_BUG_PUBKEYAUTH) {
690 buffer_append(&b, session_id2, session_id2_len);
691 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
692 buffer_put_cstring(&b, authctxt->server_user);
693 buffer_put_cstring(&b, authctxt->service);
694 buffer_put_cstring(&b, authctxt->method->name);
695 buffer_put_char(&b, have_sig);
696 buffer_put_cstring(&b, KEX_DSS);
697 buffer_put_string(&b, blob, bloblen);
700 /* append signature */
701 buffer_put_string(&b, signature, slen);
704 /* skip session id and packet type */
705 if (buffer_len(&b) < skip + 1)
706 fatal("userauth_pubkey: internal error");
707 buffer_consume(&b, skip + 1);
709 /* put remaining data from buffer into packet */
710 packet_start(SSH2_MSG_USERAUTH_REQUEST);
711 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
722 int dsa_sign_cb(Authctxt *authctxt, Key *key, unsigned char **sigp, int *lenp,
723 unsigned char *data, int datalen)
725 return dsa_sign(key, sigp, lenp, data, datalen);
729 userauth_pubkey_identity(Authctxt *authctxt, char *filename)
732 int i, ret, try_next;
735 if (stat(filename, &st) != 0) {
736 debug("key does not exist: %s", filename);
739 debug("try pubkey: %s", filename);
741 k = key_new(KEY_DSA);
742 if (!load_private_key(filename, "", k, NULL)) {
746 snprintf(prompt, sizeof prompt,
747 "Enter passphrase for %s key '%.100s': ",
748 key_type(k), filename);
749 for (i = 0; i < options.number_of_password_prompts; i++) {
750 passphrase = read_passphrase(prompt, 0);
751 if (strcmp(passphrase, "") != 0) {
752 success = load_private_key(filename, passphrase, k, NULL);
755 debug2("no passphrase given, try next key");
758 memset(passphrase, 0, strlen(passphrase));
760 if (success || try_next)
762 debug2("bad passphrase given, try again...");
769 ret = sign_and_send_pubkey(authctxt, k, dsa_sign_cb);
775 int agent_sign_cb(Authctxt *authctxt, Key *key, unsigned char **sigp, int *lenp,
776 unsigned char *data, int datalen)
778 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
782 userauth_pubkey_agent(Authctxt *authctxt)
784 static int called = 0;
790 k = ssh_get_first_identity(authctxt->agent, &comment, 2);
793 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
796 debug2("no more DSA keys from agent");
799 debug("trying DSA agent key %s", comment);
801 ret = sign_and_send_pubkey(authctxt, k, agent_sign_cb);
807 userauth_pubkey(Authctxt *authctxt)
812 if (authctxt->agent != NULL)
813 sent = userauth_pubkey_agent(authctxt);
814 while (sent == 0 && idx < options.num_identity_files2)
815 sent = userauth_pubkey_identity(authctxt, options.identity_files2[idx++]);
820 * Send userauth request message specifying keyboard-interactive method.
823 userauth_kbdint(Authctxt *authctxt)
825 static int attempt = 0;
827 if (attempt++ >= options.number_of_password_prompts)
830 debug2("userauth_kbdint");
831 packet_start(SSH2_MSG_USERAUTH_REQUEST);
832 packet_put_cstring(authctxt->server_user);
833 packet_put_cstring(authctxt->service);
834 packet_put_cstring(authctxt->method->name);
835 packet_put_cstring(""); /* lang */
836 packet_put_cstring(options.kbd_interactive_devices ?
837 options.kbd_interactive_devices : "");
841 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
846 * parse SSH2_MSG_USERAUTH_INFO_REQUEST, prompt user and send
847 * SSH2_MSG_USERAUTH_INFO_RESPONSE
850 input_userauth_info_req(int type, int plen, void *ctxt)
852 Authctxt *authctxt = ctxt;
857 char *response = NULL;
858 unsigned int num_prompts, i;
861 debug2("input_userauth_info_req");
863 if (authctxt == NULL)
864 fatal("input_userauth_info_req: no authentication context");
866 name = packet_get_string(NULL);
867 inst = packet_get_string(NULL);
868 lang = packet_get_string(NULL);
870 if (strlen(name) > 0)
874 if (strlen(inst) > 0)
877 xfree(lang); /* unused */
879 num_prompts = packet_get_int();
881 * Begin to build info response packet based on prompts requested.
882 * We commit to providing the correct number of responses, so if
883 * further on we run into a problem that prevents this, we have to
884 * be sure and clean this up and send a correct error response.
886 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
887 packet_put_int(num_prompts);
889 for (i = 0; i < num_prompts; i++) {
890 prompt = packet_get_string(NULL);
891 echo = packet_get_char();
893 response = cli_prompt(prompt, echo);
895 packet_put_cstring(response);
896 memset(response, 0, strlen(response));
900 packet_done(); /* done with parsing incoming message. */
906 /* find auth method */
910 static char *def_authlist = "publickey,password";
911 static char *authlist_current = NULL; /* clean copy used for comparison */
912 static char *authname_current = NULL; /* last used auth method */
913 static char *authlist_working = NULL; /* copy that gets modified by strtok_r() */
914 static char *authlist_state = NULL; /* state variable for strtok_r() */
917 * Before starting to use a new authentication method list sent by the
918 * server, reset internal variables. This should also be called when
919 * finished processing server list to free resources.
924 if (authlist_current != NULL) {
925 xfree(authlist_current);
926 authlist_current = NULL;
928 if (authlist_working != NULL) {
929 xfree(authlist_working);
930 authlist_working = NULL;
932 if (authname_current != NULL) {
933 xfree(authname_current);
934 authlist_state = NULL;
936 if (authlist_state != NULL)
937 authlist_state = NULL;
942 * given auth method name, if configurable options permit this method fill
943 * in auth_ident field and return true, otherwise return false.
946 authmethod_is_enabled(Authmethod *method)
950 /* return false if options indicate this method is disabled */
951 if (method->enabled == NULL || *method->enabled == 0)
953 /* return false if batch mode is enabled but method needs interactive mode */
954 if (method->batch_flag != NULL && *method->batch_flag != 0)
960 authmethod_lookup(const char *name)
962 Authmethod *method = NULL;
964 for (method = authmethods; method->name != NULL; method++)
965 if (strcmp(name, method->name) == 0)
967 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
972 * Given the authentication method list sent by the server, return the
973 * next method we should try. If the server initially sends a nil list,
974 * use a built-in default list. If the server sends a nil list after
975 * previously sending a valid list, continue using the list originally
980 authmethod_get(char *authlist)
983 Authmethod *method = NULL;
985 /* Use a suitable default if we're passed a nil list. */
986 if (authlist == NULL || strlen(authlist) == 0)
987 authlist = def_authlist;
989 if (authlist_current == NULL || strcmp(authlist, authlist_current) != 0) {
990 /* start over if passed a different list */
991 debug3("start over, passed a different list");
993 authlist_current = xstrdup(authlist);
994 authlist_working = xstrdup(authlist);
995 name = strtok_r(authlist_working, DELIM, &authlist_state);
998 * try to use previously used authentication method
999 * or continue to use previously passed list
1001 name = (authname_current != NULL) ?
1002 authname_current : strtok_r(NULL, DELIM, &authlist_state);
1005 while (name != NULL) {
1006 debug3("authmethod_lookup %s", name);
1007 method = authmethod_lookup(name);
1008 if (method != NULL && authmethod_is_enabled(method)) {
1009 debug3("authmethod_is_enabled %s", name);
1012 name = strtok_r(NULL, DELIM, &authlist_state);
1016 if (authname_current != NULL)
1017 xfree(authname_current);
1019 if (method != NULL) {
1020 debug("next auth method to try is %s", name);
1021 authname_current = xstrdup(name);
1024 debug("no more auth methods to try");
1025 authname_current = NULL;