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.65 2001/04/04 14:34:58 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"
58 extern char *client_version_string;
59 extern char *server_version_string;
60 extern Options options;
66 u_char *session_id2 = NULL;
67 int session_id2_len = 0;
70 struct sockaddr *xxx_hostaddr;
75 check_host_key_callback(Key *hostkey)
77 check_host_key(xxx_host, xxx_hostaddr, hostkey,
78 options.user_hostfile2, options.system_hostfile2);
83 ssh_kex2(char *host, struct sockaddr *hostaddr)
88 xxx_hostaddr = hostaddr;
90 if (options.ciphers == (char *)-1) {
91 log("No valid ciphers for protocol version 2 given, using defaults.");
92 options.ciphers = NULL;
94 if (options.ciphers != NULL) {
95 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
96 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
98 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
99 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
100 myproposal[PROPOSAL_ENC_ALGS_STOC] =
101 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
102 if (options.compression) {
103 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
104 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
106 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
107 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
109 if (options.macs != NULL) {
110 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
111 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
114 /* start key exchange */
115 kex = kex_setup(myproposal);
116 kex->client_version_string=client_version_string;
117 kex->server_version_string=server_version_string;
118 kex->check_host_key=&check_host_key_callback;
122 dispatch_run(DISPATCH_BLOCK, &kex->newkeys, kex);
124 session_id2 = kex->session_id;
125 session_id2_len = kex->session_id_len;
128 /* send 1st encrypted/maced/compressed message */
129 packet_start(SSH2_MSG_IGNORE);
130 packet_put_cstring("markus");
134 debug("done: ssh_kex2.");
141 typedef struct Authctxt Authctxt;
142 typedef struct Authmethod Authmethod;
144 typedef int sign_cb_fn(
145 Authctxt *authctxt, Key *key,
146 u_char **sigp, int *lenp, u_char *data, int datalen);
149 const char *server_user;
152 AuthenticationConnection *agent;
157 sign_cb_fn *last_key_sign;
161 char *name; /* string to compare against server's list */
162 int (*userauth)(Authctxt *authctxt);
163 int *enabled; /* flag in option struct that enables method */
164 int *batch_flag; /* flag in option struct that disables method */
167 void input_userauth_success(int type, int plen, void *ctxt);
168 void input_userauth_failure(int type, int plen, void *ctxt);
169 void input_userauth_banner(int type, int plen, void *ctxt);
170 void input_userauth_error(int type, int plen, void *ctxt);
171 void input_userauth_info_req(int type, int plen, void *ctxt);
172 void input_userauth_pk_ok(int type, int plen, void *ctxt);
174 int userauth_none(Authctxt *authctxt);
175 int userauth_pubkey(Authctxt *authctxt);
176 int userauth_passwd(Authctxt *authctxt);
177 int userauth_kbdint(Authctxt *authctxt);
179 void userauth(Authctxt *authctxt, char *authlist);
182 sign_and_send_pubkey(Authctxt *authctxt, Key *k,
183 sign_cb_fn *sign_callback);
184 void clear_auth_state(Authctxt *authctxt);
186 Authmethod *authmethod_get(char *authlist);
187 Authmethod *authmethod_lookup(const char *name);
188 char *authmethods_get(void);
190 Authmethod authmethods[] = {
193 &options.pubkey_authentication,
197 &options.password_authentication,
198 &options.batch_mode},
199 {"keyboard-interactive",
201 &options.kbd_interactive_authentication,
202 &options.batch_mode},
207 {NULL, NULL, NULL, NULL}
211 ssh_userauth2(const char *server_user, char *host)
217 if (options.challenge_reponse_authentication)
218 options.kbd_interactive_authentication = 1;
220 debug("send SSH2_MSG_SERVICE_REQUEST");
221 packet_start(SSH2_MSG_SERVICE_REQUEST);
222 packet_put_cstring("ssh-userauth");
225 type = packet_read(&plen);
226 if (type != SSH2_MSG_SERVICE_ACCEPT) {
227 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
229 if (packet_remaining() > 0) {
230 char *reply = packet_get_string(&plen);
231 debug("service_accept: %s", reply);
234 debug("buggy server: service_accept w/o service");
237 debug("got SSH2_MSG_SERVICE_ACCEPT");
239 if (options.preferred_authentications == NULL)
240 options.preferred_authentications = authmethods_get();
242 /* setup authentication context */
243 authctxt.agent = ssh_get_authentication_connection();
244 authctxt.server_user = server_user;
245 authctxt.host = host;
246 authctxt.service = "ssh-connection"; /* service name */
247 authctxt.success = 0;
248 authctxt.method = authmethod_lookup("none");
249 authctxt.authlist = NULL;
250 if (authctxt.method == NULL)
251 fatal("ssh_userauth2: internal error: cannot send userauth none request");
253 /* initial userauth request */
254 userauth_none(&authctxt);
256 dispatch_init(&input_userauth_error);
257 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
258 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
259 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
260 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
262 if (authctxt.agent != NULL)
263 ssh_close_authentication_connection(authctxt.agent);
265 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
268 userauth(Authctxt *authctxt, char *authlist)
270 if (authlist == NULL) {
271 authlist = authctxt->authlist;
273 if (authctxt->authlist)
274 xfree(authctxt->authlist);
275 authctxt->authlist = authlist;
278 Authmethod *method = authmethod_get(authlist);
280 fatal("Permission denied (%s).", authlist);
281 authctxt->method = method;
282 if (method->userauth(authctxt) != 0) {
283 debug2("we sent a %s packet, wait for reply", method->name);
286 debug2("we did not send a packet, disable method");
287 method->enabled = NULL;
292 input_userauth_error(int type, int plen, void *ctxt)
294 fatal("input_userauth_error: bad message during authentication: "
298 input_userauth_banner(int type, int plen, void *ctxt)
301 debug3("input_userauth_banner");
302 msg = packet_get_string(NULL);
303 lang = packet_get_string(NULL);
304 fprintf(stderr, "%s", msg);
309 input_userauth_success(int type, int plen, void *ctxt)
311 Authctxt *authctxt = ctxt;
312 if (authctxt == NULL)
313 fatal("input_userauth_success: no authentication context");
314 if (authctxt->authlist)
315 xfree(authctxt->authlist);
316 clear_auth_state(authctxt);
317 authctxt->success = 1; /* break out */
320 input_userauth_failure(int type, int plen, void *ctxt)
322 Authctxt *authctxt = ctxt;
323 char *authlist = NULL;
326 if (authctxt == NULL)
327 fatal("input_userauth_failure: no authentication context");
329 authlist = packet_get_string(NULL);
330 partial = packet_get_char();
334 log("Authenticated with partial success.");
335 debug("authentications that can continue: %s", authlist);
337 clear_auth_state(authctxt);
338 userauth(authctxt, authlist);
341 input_userauth_pk_ok(int type, int plen, void *ctxt)
343 Authctxt *authctxt = ctxt;
346 int alen, blen, pktype, sent = 0;
347 char *pkalg, *pkblob, *fp;
349 if (authctxt == NULL)
350 fatal("input_userauth_pk_ok: no authentication context");
351 if (datafellows & SSH_BUG_PKOK) {
352 /* this is similar to SSH_BUG_PKAUTH */
353 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
354 pkblob = packet_get_string(&blen);
356 buffer_append(&b, pkblob, blen);
357 pkalg = buffer_get_string(&b, &alen);
360 pkalg = packet_get_string(&alen);
361 pkblob = packet_get_string(&blen);
365 debug("input_userauth_pk_ok: pkalg %s blen %d lastkey %p hint %d",
366 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
369 if (authctxt->last_key == NULL ||
370 authctxt->last_key_sign == NULL) {
371 debug("no last key or no sign cb");
374 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
375 debug("unknown pkalg %s", pkalg);
378 if ((key = key_from_blob(pkblob, blen)) == NULL) {
379 debug("no key from blob. pkalg %s", pkalg);
382 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
383 debug2("input_userauth_pk_ok: fp %s", fp);
385 if (!key_equal(key, authctxt->last_key)) {
386 debug("key != last_key");
389 sent = sign_and_send_pubkey(authctxt, key,
390 authctxt->last_key_sign);
399 clear_auth_state(authctxt);
400 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
402 /* try another method if we did not send a packet*/
404 userauth(authctxt, NULL);
409 userauth_none(Authctxt *authctxt)
411 /* initial userauth request */
412 packet_start(SSH2_MSG_USERAUTH_REQUEST);
413 packet_put_cstring(authctxt->server_user);
414 packet_put_cstring(authctxt->service);
415 packet_put_cstring(authctxt->method->name);
421 userauth_passwd(Authctxt *authctxt)
423 static int attempt = 0;
427 if (attempt++ >= options.number_of_password_prompts)
431 error("Permission denied, please try again.");
433 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
434 authctxt->server_user, authctxt->host);
435 password = read_passphrase(prompt, 0);
436 packet_start(SSH2_MSG_USERAUTH_REQUEST);
437 packet_put_cstring(authctxt->server_user);
438 packet_put_cstring(authctxt->service);
439 packet_put_cstring(authctxt->method->name);
441 packet_put_cstring(password);
442 memset(password, 0, strlen(password));
444 packet_inject_ignore(64);
450 clear_auth_state(Authctxt *authctxt)
452 /* XXX clear authentication state */
453 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
454 debug3("clear_auth_state: key_free %p", authctxt->last_key);
455 key_free(authctxt->last_key);
457 authctxt->last_key = NULL;
458 authctxt->last_key_hint = -2;
459 authctxt->last_key_sign = NULL;
463 sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
466 u_char *blob, *signature;
472 debug3("sign_and_send_pubkey");
474 if (key_to_blob(k, &blob, &bloblen) == 0) {
475 /* we cannot handle this key */
476 debug3("sign_and_send_pubkey: cannot handle key");
479 /* data to be signed */
481 if (datafellows & SSH_OLD_SESSIONID) {
482 buffer_append(&b, session_id2, session_id2_len);
483 skip = session_id2_len;
485 buffer_put_string(&b, session_id2, session_id2_len);
486 skip = buffer_len(&b);
488 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
489 buffer_put_cstring(&b, authctxt->server_user);
490 buffer_put_cstring(&b,
491 datafellows & SSH_BUG_PKSERVICE ?
494 if (datafellows & SSH_BUG_PKAUTH) {
495 buffer_put_char(&b, have_sig);
497 buffer_put_cstring(&b, authctxt->method->name);
498 buffer_put_char(&b, have_sig);
499 buffer_put_cstring(&b, key_ssh_name(k));
501 buffer_put_string(&b, blob, bloblen);
503 /* generate signature */
504 ret = (*sign_callback)(authctxt, k, &signature, &slen,
505 buffer_ptr(&b), buffer_len(&b));
514 if (datafellows & SSH_BUG_PKSERVICE) {
516 buffer_append(&b, session_id2, session_id2_len);
517 skip = session_id2_len;
518 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
519 buffer_put_cstring(&b, authctxt->server_user);
520 buffer_put_cstring(&b, authctxt->service);
521 buffer_put_cstring(&b, authctxt->method->name);
522 buffer_put_char(&b, have_sig);
523 if (!(datafellows & SSH_BUG_PKAUTH))
524 buffer_put_cstring(&b, key_ssh_name(k));
525 buffer_put_string(&b, blob, bloblen);
529 /* append signature */
530 buffer_put_string(&b, signature, slen);
533 /* skip session id and packet type */
534 if (buffer_len(&b) < skip + 1)
535 fatal("userauth_pubkey: internal error");
536 buffer_consume(&b, skip + 1);
538 /* put remaining data from buffer into packet */
539 packet_start(SSH2_MSG_USERAUTH_REQUEST);
540 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
548 send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
552 int bloblen, have_sig = 0;
554 debug3("send_pubkey_test");
556 if (key_to_blob(k, &blob, &bloblen) == 0) {
557 /* we cannot handle this key */
558 debug3("send_pubkey_test: cannot handle key");
561 /* register callback for USERAUTH_PK_OK message */
562 authctxt->last_key_sign = sign_callback;
563 authctxt->last_key_hint = hint;
564 authctxt->last_key = k;
565 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
567 packet_start(SSH2_MSG_USERAUTH_REQUEST);
568 packet_put_cstring(authctxt->server_user);
569 packet_put_cstring(authctxt->service);
570 packet_put_cstring(authctxt->method->name);
571 packet_put_char(have_sig);
572 if (!(datafellows & SSH_BUG_PKAUTH))
573 packet_put_cstring(key_ssh_name(k));
574 packet_put_string(blob, bloblen);
581 load_identity_file(char *filename)
584 char prompt[300], *passphrase;
588 if (stat(filename, &st) < 0) {
589 debug3("no such identity: %s", filename);
592 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
593 if (private == NULL) {
594 if (options.batch_mode)
596 snprintf(prompt, sizeof prompt,
597 "Enter passphrase for key '%.100s': ", filename);
598 for (i = 0; i < options.number_of_password_prompts; i++) {
599 passphrase = read_passphrase(prompt, 0);
600 if (strcmp(passphrase, "") != 0) {
601 private = key_load_private_type(KEY_UNSPEC, filename,
605 debug2("no passphrase given, try next key");
608 memset(passphrase, 0, strlen(passphrase));
610 if (private != NULL || quit)
612 debug2("bad passphrase given, try again...");
619 identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
620 u_char *data, int datalen)
625 idx = authctxt->last_key_hint;
628 private = load_identity_file(options.identity_files[idx]);
631 ret = key_sign(private, sigp, lenp, data, datalen);
636 int agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
637 u_char *data, int datalen)
639 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
642 int key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, int *lenp,
643 u_char *data, int datalen)
645 return key_sign(key, sigp, lenp, data, datalen);
649 userauth_pubkey_agent(Authctxt *authctxt)
651 static int called = 0;
657 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
658 debug2("userauth_pubkey_agent: no keys at all");
661 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
663 debug2("userauth_pubkey_agent: no more keys");
665 debug("userauth_pubkey_agent: testing agent key %s", comment);
667 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
672 debug2("userauth_pubkey_agent: no message sent");
677 userauth_pubkey(Authctxt *authctxt)
684 if (authctxt->agent != NULL) {
686 sent = userauth_pubkey_agent(authctxt);
687 } while(!sent && authctxt->agent->howmany > 0);
689 while (!sent && idx < options.num_identity_files) {
690 key = options.identity_keys[idx];
691 filename = options.identity_files[idx];
693 debug("try privkey: %s", filename);
694 key = load_identity_file(filename);
696 sent = sign_and_send_pubkey(authctxt, key,
700 } else if (key->type != KEY_RSA1) {
701 debug("try pubkey: %s", filename);
702 sent = send_pubkey_test(authctxt, key,
703 identity_sign_cb, idx);
711 * Send userauth request message specifying keyboard-interactive method.
714 userauth_kbdint(Authctxt *authctxt)
716 static int attempt = 0;
718 if (attempt++ >= options.number_of_password_prompts)
721 debug2("userauth_kbdint");
722 packet_start(SSH2_MSG_USERAUTH_REQUEST);
723 packet_put_cstring(authctxt->server_user);
724 packet_put_cstring(authctxt->service);
725 packet_put_cstring(authctxt->method->name);
726 packet_put_cstring(""); /* lang */
727 packet_put_cstring(options.kbd_interactive_devices ?
728 options.kbd_interactive_devices : "");
731 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
736 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
739 input_userauth_info_req(int type, int plen, void *ctxt)
741 Authctxt *authctxt = ctxt;
742 char *name, *inst, *lang, *prompt, *response;
743 u_int num_prompts, i;
746 debug2("input_userauth_info_req");
748 if (authctxt == NULL)
749 fatal("input_userauth_info_req: no authentication context");
751 name = packet_get_string(NULL);
752 inst = packet_get_string(NULL);
753 lang = packet_get_string(NULL);
754 if (strlen(name) > 0)
756 if (strlen(inst) > 0)
762 num_prompts = packet_get_int();
764 * Begin to build info response packet based on prompts requested.
765 * We commit to providing the correct number of responses, so if
766 * further on we run into a problem that prevents this, we have to
767 * be sure and clean this up and send a correct error response.
769 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
770 packet_put_int(num_prompts);
772 for (i = 0; i < num_prompts; i++) {
773 prompt = packet_get_string(NULL);
774 echo = packet_get_char();
776 response = cli_prompt(prompt, echo);
778 packet_put_cstring(response);
779 memset(response, 0, strlen(response));
783 packet_done(); /* done with parsing incoming message. */
785 packet_inject_ignore(64);
789 /* find auth method */
792 * given auth method name, if configurable options permit this method fill
793 * in auth_ident field and return true, otherwise return false.
796 authmethod_is_enabled(Authmethod *method)
800 /* return false if options indicate this method is disabled */
801 if (method->enabled == NULL || *method->enabled == 0)
803 /* return false if batch mode is enabled but method needs interactive mode */
804 if (method->batch_flag != NULL && *method->batch_flag != 0)
810 authmethod_lookup(const char *name)
812 Authmethod *method = NULL;
814 for (method = authmethods; method->name != NULL; method++)
815 if (strcmp(name, method->name) == 0)
817 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
821 /* XXX internal state */
822 static Authmethod *current = NULL;
823 static char *supported = NULL;
824 static char *preferred = NULL;
826 * Given the authentication method list sent by the server, return the
827 * next method we should try. If the server initially sends a nil list,
828 * use a built-in default list.
831 authmethod_get(char *authlist)
837 /* Use a suitable default if we're passed a nil list. */
838 if (authlist == NULL || strlen(authlist) == 0)
839 authlist = options.preferred_authentications;
841 if (supported == NULL || strcmp(authlist, supported) != 0) {
842 debug3("start over, passed a different list %s", authlist);
843 if (supported != NULL)
845 supported = xstrdup(authlist);
846 preferred = options.preferred_authentications;
847 debug3("preferred %s", preferred);
849 } else if (current != NULL && authmethod_is_enabled(current))
853 if ((name = match_list(preferred, supported, &next)) == NULL) {
854 debug("no more auth methods to try");
859 debug3("authmethod_lookup %s", name);
860 debug3("remaining preferred: %s", preferred);
861 if ((current = authmethod_lookup(name)) != NULL &&
862 authmethod_is_enabled(current)) {
863 debug3("authmethod_is_enabled %s", name);
864 debug("next auth method to try is %s", name);
873 authmethods_get(void)
875 Authmethod *method = NULL;
879 for (method = authmethods; method->name != NULL; method++) {
880 if (authmethod_is_enabled(method)) {
882 strlcat(buf, DELIM, sizeof buf);
883 strlcat(buf, method->name, sizeof buf);