1 /* $OpenBSD: sshconnect2.c,v 1.204 2014/02/02 03:44:32 djm Exp $ */
4 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Copyright (c) 2008 Damien Miller. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/types.h>
32 #include <sys/socket.h>
45 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
49 #include "openbsd-compat/sys-queue.h"
60 #include "myproposal.h"
61 #include "sshconnect.h"
72 #include "pathnames.h"
81 extern char *client_version_string;
82 extern char *server_version_string;
83 extern Options options;
84 #ifdef NONE_CIPHER_ENABLED
88 * tty_flag is set in ssh.c so we can use it here. If set then prevent
89 * the switch to the null cipher.
99 u_char *session_id2 = NULL;
100 u_int session_id2_len = 0;
103 struct sockaddr *xxx_hostaddr;
108 verify_host_key_callback(Key *hostkey)
110 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
111 fatal("Host key verification failed.");
116 order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
118 char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
120 struct hostkeys *hostkeys;
124 /* Find all hostkeys for this hostname */
125 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
126 hostkeys = init_hostkeys();
127 for (i = 0; i < options.num_user_hostfiles; i++)
128 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
129 for (i = 0; i < options.num_system_hostfiles; i++)
130 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
132 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
133 maxlen = strlen(avail) + 1;
134 first = xmalloc(maxlen);
135 last = xmalloc(maxlen);
136 *first = *last = '\0';
138 #define ALG_APPEND(to, from) \
141 strlcat(to, ",", maxlen); \
142 strlcat(to, from, maxlen); \
145 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
146 if ((ktype = key_type_from_name(alg)) == KEY_UNSPEC)
147 fatal("%s: unknown alg %s", __func__, alg);
148 if (lookup_key_in_hostkeys_by_type(hostkeys,
149 key_type_plain(ktype), NULL))
150 ALG_APPEND(first, alg);
152 ALG_APPEND(last, alg);
155 xasprintf(&ret, "%s%s%s", first, *first == '\0' ? "" : ",", last);
157 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
163 free_hostkeys(hostkeys);
169 ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
174 xxx_hostaddr = hostaddr;
176 if (options.ciphers == (char *)-1) {
177 logit("No valid ciphers for protocol version 2 given, using defaults.");
178 options.ciphers = NULL;
180 if (options.ciphers != NULL) {
181 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
182 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
184 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
185 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
186 myproposal[PROPOSAL_ENC_ALGS_STOC] =
187 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
188 if (options.compression) {
189 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
190 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
192 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
193 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
195 if (options.macs != NULL) {
196 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
197 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
199 if (options.hostkeyalgorithms != NULL)
200 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
201 compat_pkalg_proposal(options.hostkeyalgorithms);
203 /* Prefer algorithms that we already have keys for */
204 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
205 compat_pkalg_proposal(
206 order_hostkeyalgs(host, hostaddr, port));
208 if (options.kex_algorithms != NULL)
209 myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
210 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(
211 myproposal[PROPOSAL_KEX_ALGS]);
213 if (options.rekey_limit || options.rekey_interval)
214 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
215 (time_t)options.rekey_interval);
217 /* start key exchange */
218 kex = kex_setup(myproposal);
219 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
220 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
221 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
222 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
223 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
224 kex->kex[KEX_C25519_SHA256] = kexc25519_client;
225 kex->client_version_string=client_version_string;
226 kex->server_version_string=server_version_string;
227 kex->verify_host_key=&verify_host_key_callback;
231 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
233 if (options.use_roaming && !kex->roaming) {
234 debug("Roaming not allowed by server");
235 options.use_roaming = 0;
238 session_id2 = kex->session_id;
239 session_id2_len = kex->session_id_len;
242 /* send 1st encrypted/maced/compressed message */
243 packet_start(SSH2_MSG_IGNORE);
244 packet_put_cstring("markus");
254 typedef struct Authctxt Authctxt;
255 typedef struct Authmethod Authmethod;
256 typedef struct identity Identity;
257 typedef struct idlist Idlist;
260 TAILQ_ENTRY(identity) next;
261 AuthenticationConnection *ac; /* set if agent supports key */
262 Key *key; /* public/private key */
263 char *filename; /* comment for agent-only keys */
265 int isprivate; /* key points to the private key */
268 TAILQ_HEAD(idlist, identity);
271 const char *server_user;
272 const char *local_user;
276 sig_atomic_t success;
280 AuthenticationConnection *agent;
282 Sensitive *sensitive;
283 /* kbd-interactive */
289 char *name; /* string to compare against server's list */
290 int (*userauth)(Authctxt *authctxt);
291 void (*cleanup)(Authctxt *authctxt);
292 int *enabled; /* flag in option struct that enables method */
293 int *batch_flag; /* flag in option struct that disables method */
296 void input_userauth_success(int, u_int32_t, void *);
297 void input_userauth_success_unexpected(int, u_int32_t, void *);
298 void input_userauth_failure(int, u_int32_t, void *);
299 void input_userauth_banner(int, u_int32_t, void *);
300 void input_userauth_error(int, u_int32_t, void *);
301 void input_userauth_info_req(int, u_int32_t, void *);
302 void input_userauth_pk_ok(int, u_int32_t, void *);
303 void input_userauth_passwd_changereq(int, u_int32_t, void *);
305 int userauth_none(Authctxt *);
306 int userauth_pubkey(Authctxt *);
307 int userauth_passwd(Authctxt *);
308 int userauth_kbdint(Authctxt *);
309 int userauth_hostbased(Authctxt *);
312 int userauth_gssapi(Authctxt *authctxt);
313 void input_gssapi_response(int type, u_int32_t, void *);
314 void input_gssapi_token(int type, u_int32_t, void *);
315 void input_gssapi_hash(int type, u_int32_t, void *);
316 void input_gssapi_error(int, u_int32_t, void *);
317 void input_gssapi_errtok(int, u_int32_t, void *);
320 void userauth(Authctxt *, char *);
322 static int sign_and_send_pubkey(Authctxt *, Identity *);
323 static void pubkey_prepare(Authctxt *);
324 static void pubkey_cleanup(Authctxt *);
325 static Key *load_identity_file(char *, int);
327 static Authmethod *authmethod_get(char *authlist);
328 static Authmethod *authmethod_lookup(const char *name);
329 static char *authmethods_get(void);
331 Authmethod authmethods[] = {
336 &options.gss_authentication,
342 &options.hostbased_authentication,
347 &options.pubkey_authentication,
349 {"keyboard-interactive",
352 &options.kbd_interactive_authentication,
353 &options.batch_mode},
357 &options.password_authentication,
358 &options.batch_mode},
364 {NULL, NULL, NULL, NULL, NULL}
368 ssh_userauth2(const char *local_user, const char *server_user, char *host,
369 Sensitive *sensitive)
374 if (options.challenge_response_authentication)
375 options.kbd_interactive_authentication = 1;
377 packet_start(SSH2_MSG_SERVICE_REQUEST);
378 packet_put_cstring("ssh-userauth");
380 debug("SSH2_MSG_SERVICE_REQUEST sent");
382 type = packet_read();
383 if (type != SSH2_MSG_SERVICE_ACCEPT)
384 fatal("Server denied authentication request: %d", type);
385 if (packet_remaining() > 0) {
386 char *reply = packet_get_string(NULL);
387 debug2("service_accept: %s", reply);
390 debug2("buggy server: service_accept w/o service");
393 debug("SSH2_MSG_SERVICE_ACCEPT received");
395 if (options.preferred_authentications == NULL)
396 options.preferred_authentications = authmethods_get();
398 /* setup authentication context */
399 memset(&authctxt, 0, sizeof(authctxt));
400 pubkey_prepare(&authctxt);
401 authctxt.server_user = server_user;
402 authctxt.local_user = local_user;
403 authctxt.host = host;
404 authctxt.service = "ssh-connection"; /* service name */
405 authctxt.success = 0;
406 authctxt.method = authmethod_lookup("none");
407 authctxt.authlist = NULL;
408 authctxt.methoddata = NULL;
409 authctxt.sensitive = sensitive;
410 authctxt.info_req_seen = 0;
411 if (authctxt.method == NULL)
412 fatal("ssh_userauth2: internal error: cannot send userauth none request");
414 /* initial userauth request */
415 userauth_none(&authctxt);
417 dispatch_init(&input_userauth_error);
418 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
419 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
420 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
421 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
423 pubkey_cleanup(&authctxt);
424 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
426 #ifdef NONE_CIPHER_ENABLED
428 * If the user explicitly requests to use the none cipher enable it
429 * post authentication and only if the right conditions are met: both
430 * of the NONE switches must be true and there must be no tty allocated.
432 if (options.none_switch == 1 && options.none_enabled == 1) {
434 debug("Requesting none cipher re-keying...");
435 myproposal[PROPOSAL_ENC_ALGS_STOC] = "none";
436 myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none";
437 kex_prop2buf(&xxx_kex->my, myproposal);
438 packet_request_rekeying();
439 fprintf(stderr, "WARNING: enabled NONE cipher\n");
441 /* Requested NONE cipher on an interactive session. */
442 debug("Cannot switch to NONE cipher with tty "
444 fprintf(stderr, "NONE cipher switch disabled given "
445 "a TTY is allocated\n");
449 debug("Authentication succeeded (%s).", authctxt.method->name);
453 userauth(Authctxt *authctxt, char *authlist)
455 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
456 authctxt->method->cleanup(authctxt);
458 free(authctxt->methoddata);
459 authctxt->methoddata = NULL;
460 if (authlist == NULL) {
461 authlist = authctxt->authlist;
463 free(authctxt->authlist);
464 authctxt->authlist = authlist;
467 Authmethod *method = authmethod_get(authlist);
469 fatal("Permission denied (%s).", authlist);
470 authctxt->method = method;
472 /* reset the per method handler */
473 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
474 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
476 /* and try new method */
477 if (method->userauth(authctxt) != 0) {
478 debug2("we sent a %s packet, wait for reply", method->name);
481 debug2("we did not send a packet, disable method");
482 method->enabled = NULL;
489 input_userauth_error(int type, u_int32_t seq, void *ctxt)
491 fatal("input_userauth_error: bad message during authentication: "
497 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
499 char *msg, *raw, *lang;
502 debug3("input_userauth_banner");
503 raw = packet_get_string(&len);
504 lang = packet_get_string(NULL);
505 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
508 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
509 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
510 fprintf(stderr, "%s", msg);
519 input_userauth_success(int type, u_int32_t seq, void *ctxt)
521 Authctxt *authctxt = ctxt;
523 if (authctxt == NULL)
524 fatal("input_userauth_success: no authentication context");
525 free(authctxt->authlist);
526 authctxt->authlist = NULL;
527 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
528 authctxt->method->cleanup(authctxt);
529 free(authctxt->methoddata);
530 authctxt->methoddata = NULL;
531 authctxt->success = 1; /* break out */
535 input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
537 Authctxt *authctxt = ctxt;
539 if (authctxt == NULL)
540 fatal("%s: no authentication context", __func__);
542 fatal("Unexpected authentication success during %s.",
543 authctxt->method->name);
548 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
550 Authctxt *authctxt = ctxt;
551 char *authlist = NULL;
554 if (authctxt == NULL)
555 fatal("input_userauth_failure: no authentication context");
557 authlist = packet_get_string(NULL);
558 partial = packet_get_char();
562 logit("Authenticated with partial success.");
564 pubkey_cleanup(authctxt);
565 pubkey_prepare(authctxt);
567 debug("Authentications that can continue: %s", authlist);
569 userauth(authctxt, authlist);
574 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
576 Authctxt *authctxt = ctxt;
580 int pktype, sent = 0;
585 if (authctxt == NULL)
586 fatal("input_userauth_pk_ok: no authentication context");
587 if (datafellows & SSH_BUG_PKOK) {
588 /* this is similar to SSH_BUG_PKAUTH */
589 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
590 pkblob = packet_get_string(&blen);
592 buffer_append(&b, pkblob, blen);
593 pkalg = buffer_get_string(&b, &alen);
596 pkalg = packet_get_string(&alen);
597 pkblob = packet_get_string(&blen);
601 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
603 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
604 debug("unknown pkalg %s", pkalg);
607 if ((key = key_from_blob(pkblob, blen)) == NULL) {
608 debug("no key from blob. pkalg %s", pkalg);
611 if (key->type != pktype) {
612 error("input_userauth_pk_ok: type mismatch "
613 "for decoded key (received %d, expected %d)",
617 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
618 debug2("input_userauth_pk_ok: fp %s", fp);
622 * search keys in the reverse order, because last candidate has been
623 * moved to the end of the queue. this also avoids confusion by
626 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
627 if (key_equal(key, id->key)) {
628 sent = sign_and_send_pubkey(authctxt, id);
638 /* try another method if we did not send a packet */
640 userauth(authctxt, NULL);
645 userauth_gssapi(Authctxt *authctxt)
647 Gssctxt *gssctxt = NULL;
648 static gss_OID_set gss_supported = NULL;
649 static u_int mech = 0;
653 /* Try one GSSAPI method at a time, rather than sending them all at
656 if (gss_supported == NULL)
657 gss_indicate_mechs(&min, &gss_supported);
659 /* Check to see if the mechanism is usable before we offer it */
660 while (mech < gss_supported->count && !ok) {
661 /* My DER encoding requires length<128 */
662 if (gss_supported->elements[mech].length < 128 &&
663 ssh_gssapi_check_mechanism(&gssctxt,
664 &gss_supported->elements[mech], authctxt->host)) {
665 ok = 1; /* Mechanism works */
674 authctxt->methoddata=(void *)gssctxt;
676 packet_start(SSH2_MSG_USERAUTH_REQUEST);
677 packet_put_cstring(authctxt->server_user);
678 packet_put_cstring(authctxt->service);
679 packet_put_cstring(authctxt->method->name);
683 packet_put_int((gss_supported->elements[mech].length) + 2);
684 packet_put_char(SSH_GSS_OIDTYPE);
685 packet_put_char(gss_supported->elements[mech].length);
686 packet_put_raw(gss_supported->elements[mech].elements,
687 gss_supported->elements[mech].length);
691 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
692 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
693 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
694 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
696 mech++; /* Move along to next candidate */
702 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
704 Authctxt *authctxt = ctxt;
705 Gssctxt *gssctxt = authctxt->methoddata;
706 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
707 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
708 gss_buffer_desc gssbuf;
709 OM_uint32 status, ms, flags;
712 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
713 recv_tok, &send_tok, &flags);
715 if (send_tok.length > 0) {
716 if (GSS_ERROR(status))
717 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
719 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
721 packet_put_string(send_tok.value, send_tok.length);
723 gss_release_buffer(&ms, &send_tok);
726 if (status == GSS_S_COMPLETE) {
727 /* send either complete or MIC, depending on mechanism */
728 if (!(flags & GSS_C_INTEG_FLAG)) {
729 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
732 ssh_gssapi_buildmic(&b, authctxt->server_user,
733 authctxt->service, "gssapi-with-mic");
735 gssbuf.value = buffer_ptr(&b);
736 gssbuf.length = buffer_len(&b);
738 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
740 if (!GSS_ERROR(status)) {
741 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
742 packet_put_string(mic.value, mic.length);
748 gss_release_buffer(&ms, &mic);
757 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
759 Authctxt *authctxt = ctxt;
764 if (authctxt == NULL)
765 fatal("input_gssapi_response: no authentication context");
766 gssctxt = authctxt->methoddata;
769 oidv = packet_get_string(&oidlen);
772 oidv[0] != SSH_GSS_OIDTYPE ||
773 oidv[1] != oidlen - 2) {
775 debug("Badly encoded mechanism OID received");
776 userauth(authctxt, NULL);
780 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
781 fatal("Server returned different OID than expected");
787 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
788 /* Start again with next method on list */
789 debug("Trying to start again");
790 userauth(authctxt, NULL);
797 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
799 Authctxt *authctxt = ctxt;
800 gss_buffer_desc recv_tok;
804 if (authctxt == NULL)
805 fatal("input_gssapi_response: no authentication context");
807 recv_tok.value = packet_get_string(&slen);
808 recv_tok.length = slen; /* safe typecast */
812 status = process_gssapi_token(ctxt, &recv_tok);
814 free(recv_tok.value);
816 if (GSS_ERROR(status)) {
817 /* Start again with the next method in the list */
818 userauth(authctxt, NULL);
825 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
827 Authctxt *authctxt = ctxt;
829 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
830 gss_buffer_desc recv_tok;
834 if (authctxt == NULL)
835 fatal("input_gssapi_response: no authentication context");
836 gssctxt = authctxt->methoddata;
838 recv_tok.value = packet_get_string(&len);
839 recv_tok.length = len;
843 /* Stick it into GSSAPI and see what it says */
844 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
845 &recv_tok, &send_tok, NULL);
847 free(recv_tok.value);
848 gss_release_buffer(&ms, &send_tok);
850 /* Server will be returning a failed packet after this one */
855 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
860 /* maj */(void)packet_get_int();
861 /* min */(void)packet_get_int();
862 msg=packet_get_string(NULL);
863 lang=packet_get_string(NULL);
867 debug("Server GSSAPI Error:\n%s", msg);
874 userauth_none(Authctxt *authctxt)
876 /* initial userauth request */
877 packet_start(SSH2_MSG_USERAUTH_REQUEST);
878 packet_put_cstring(authctxt->server_user);
879 packet_put_cstring(authctxt->service);
880 packet_put_cstring(authctxt->method->name);
886 userauth_passwd(Authctxt *authctxt)
888 static int attempt = 0;
891 const char *host = options.host_key_alias ? options.host_key_alias :
894 if (attempt++ >= options.number_of_password_prompts)
898 error("Permission denied, please try again.");
900 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
901 authctxt->server_user, host);
902 password = read_passphrase(prompt, 0);
903 packet_start(SSH2_MSG_USERAUTH_REQUEST);
904 packet_put_cstring(authctxt->server_user);
905 packet_put_cstring(authctxt->service);
906 packet_put_cstring(authctxt->method->name);
908 packet_put_cstring(password);
909 explicit_bzero(password, strlen(password));
911 packet_add_padding(64);
914 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
915 &input_userauth_passwd_changereq);
921 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
925 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
927 Authctxt *authctxt = ctxt;
928 char *info, *lang, *password = NULL, *retype = NULL;
930 const char *host = options.host_key_alias ? options.host_key_alias :
933 debug2("input_userauth_passwd_changereq");
935 if (authctxt == NULL)
936 fatal("input_userauth_passwd_changereq: "
937 "no authentication context");
939 info = packet_get_string(NULL);
940 lang = packet_get_string(NULL);
941 if (strlen(info) > 0)
945 packet_start(SSH2_MSG_USERAUTH_REQUEST);
946 packet_put_cstring(authctxt->server_user);
947 packet_put_cstring(authctxt->service);
948 packet_put_cstring(authctxt->method->name);
949 packet_put_char(1); /* additional info */
950 snprintf(prompt, sizeof(prompt),
951 "Enter %.30s@%.128s's old password: ",
952 authctxt->server_user, host);
953 password = read_passphrase(prompt, 0);
954 packet_put_cstring(password);
955 explicit_bzero(password, strlen(password));
958 while (password == NULL) {
959 snprintf(prompt, sizeof(prompt),
960 "Enter %.30s@%.128s's new password: ",
961 authctxt->server_user, host);
962 password = read_passphrase(prompt, RP_ALLOW_EOF);
963 if (password == NULL) {
967 snprintf(prompt, sizeof(prompt),
968 "Retype %.30s@%.128s's new password: ",
969 authctxt->server_user, host);
970 retype = read_passphrase(prompt, 0);
971 if (strcmp(password, retype) != 0) {
972 explicit_bzero(password, strlen(password));
974 logit("Mismatch; try again, EOF to quit.");
977 explicit_bzero(retype, strlen(retype));
980 packet_put_cstring(password);
981 explicit_bzero(password, strlen(password));
983 packet_add_padding(64);
986 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
987 &input_userauth_passwd_changereq);
991 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
992 u_char *data, u_int datalen)
997 /* the agent supports this key */
999 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1002 * we have already loaded the private key or
1003 * the private key is stored in external hardware
1005 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1006 return (key_sign(id->key, sigp, lenp, data, datalen));
1007 /* load the private key from the file */
1008 if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL)
1010 ret = key_sign(prv, sigp, lenp, data, datalen);
1016 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1019 u_char *blob, *signature;
1020 u_int bloblen, slen;
1026 fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
1027 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
1030 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1031 /* we cannot handle this key */
1032 debug3("sign_and_send_pubkey: cannot handle key");
1035 /* data to be signed */
1037 if (datafellows & SSH_OLD_SESSIONID) {
1038 buffer_append(&b, session_id2, session_id2_len);
1039 skip = session_id2_len;
1041 buffer_put_string(&b, session_id2, session_id2_len);
1042 skip = buffer_len(&b);
1044 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1045 buffer_put_cstring(&b, authctxt->server_user);
1046 buffer_put_cstring(&b,
1047 datafellows & SSH_BUG_PKSERVICE ?
1050 if (datafellows & SSH_BUG_PKAUTH) {
1051 buffer_put_char(&b, have_sig);
1053 buffer_put_cstring(&b, authctxt->method->name);
1054 buffer_put_char(&b, have_sig);
1055 buffer_put_cstring(&b, key_ssh_name(id->key));
1057 buffer_put_string(&b, blob, bloblen);
1059 /* generate signature */
1060 ret = identity_sign(id, &signature, &slen,
1061 buffer_ptr(&b), buffer_len(&b));
1070 if (datafellows & SSH_BUG_PKSERVICE) {
1072 buffer_append(&b, session_id2, session_id2_len);
1073 skip = session_id2_len;
1074 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1075 buffer_put_cstring(&b, authctxt->server_user);
1076 buffer_put_cstring(&b, authctxt->service);
1077 buffer_put_cstring(&b, authctxt->method->name);
1078 buffer_put_char(&b, have_sig);
1079 if (!(datafellows & SSH_BUG_PKAUTH))
1080 buffer_put_cstring(&b, key_ssh_name(id->key));
1081 buffer_put_string(&b, blob, bloblen);
1085 /* append signature */
1086 buffer_put_string(&b, signature, slen);
1089 /* skip session id and packet type */
1090 if (buffer_len(&b) < skip + 1)
1091 fatal("userauth_pubkey: internal error");
1092 buffer_consume(&b, skip + 1);
1094 /* put remaining data from buffer into packet */
1095 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1096 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1104 send_pubkey_test(Authctxt *authctxt, Identity *id)
1107 u_int bloblen, have_sig = 0;
1109 debug3("send_pubkey_test");
1111 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1112 /* we cannot handle this key */
1113 debug3("send_pubkey_test: cannot handle key");
1116 /* register callback for USERAUTH_PK_OK message */
1117 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1119 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1120 packet_put_cstring(authctxt->server_user);
1121 packet_put_cstring(authctxt->service);
1122 packet_put_cstring(authctxt->method->name);
1123 packet_put_char(have_sig);
1124 if (!(datafellows & SSH_BUG_PKAUTH))
1125 packet_put_cstring(key_ssh_name(id->key));
1126 packet_put_string(blob, bloblen);
1133 load_identity_file(char *filename, int userprovided)
1136 char prompt[300], *passphrase;
1137 int perm_ok = 0, quit, i;
1140 if (stat(filename, &st) < 0) {
1141 (userprovided ? logit : debug3)("no such identity: %s: %s",
1142 filename, strerror(errno));
1145 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1147 if (private != NULL)
1151 if (private == NULL) {
1152 if (options.batch_mode)
1154 snprintf(prompt, sizeof prompt,
1155 "Enter passphrase for key '%.100s': ", filename);
1156 for (i = 0; i < options.number_of_password_prompts; i++) {
1157 passphrase = read_passphrase(prompt, 0);
1158 if (strcmp(passphrase, "") != 0) {
1159 private = key_load_private_type(KEY_UNSPEC,
1160 filename, passphrase, NULL, NULL);
1163 debug2("no passphrase given, try next key");
1166 explicit_bzero(passphrase, strlen(passphrase));
1168 if (private != NULL || quit)
1170 debug2("bad passphrase given, try again...");
1177 * try keys in the following order:
1178 * 1. agent keys that are found in the config file
1179 * 2. other agent keys
1180 * 3. keys that are only listed in the config file
1183 pubkey_prepare(Authctxt *authctxt)
1185 Identity *id, *id2, *tmp;
1186 Idlist agent, files, *preferred;
1188 AuthenticationConnection *ac;
1192 TAILQ_INIT(&agent); /* keys from the agent */
1193 TAILQ_INIT(&files); /* keys from the config file */
1194 preferred = &authctxt->keys;
1195 TAILQ_INIT(preferred); /* preferred order of keys */
1197 /* list of keys stored in the filesystem and PKCS#11 */
1198 for (i = 0; i < options.num_identity_files; i++) {
1199 key = options.identity_keys[i];
1200 if (key && key->type == KEY_RSA1)
1202 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1204 options.identity_keys[i] = NULL;
1205 id = xcalloc(1, sizeof(*id));
1207 id->filename = xstrdup(options.identity_files[i]);
1208 id->userprovided = options.identity_file_userprovided[i];
1209 TAILQ_INSERT_TAIL(&files, id, next);
1211 /* Prefer PKCS11 keys that are explicitly listed */
1212 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1213 if (id->key == NULL || (id->key->flags & KEY_FLAG_EXT) == 0)
1216 TAILQ_FOREACH(id2, &files, next) {
1217 if (id2->key == NULL ||
1218 (id2->key->flags & KEY_FLAG_EXT) != 0)
1220 if (key_equal(id->key, id2->key)) {
1221 TAILQ_REMOVE(&files, id, next);
1222 TAILQ_INSERT_TAIL(preferred, id, next);
1227 /* If IdentitiesOnly set and key not found then don't use it */
1228 if (!found && options.identities_only) {
1229 TAILQ_REMOVE(&files, id, next);
1230 explicit_bzero(id, sizeof(*id));
1234 /* list of keys supported by the agent */
1235 if ((ac = ssh_get_authentication_connection())) {
1236 for (key = ssh_get_first_identity(ac, &comment, 2);
1238 key = ssh_get_next_identity(ac, &comment, 2)) {
1240 TAILQ_FOREACH(id, &files, next) {
1241 /* agent keys from the config file are preferred */
1242 if (key_equal(key, id->key)) {
1245 TAILQ_REMOVE(&files, id, next);
1246 TAILQ_INSERT_TAIL(preferred, id, next);
1252 if (!found && !options.identities_only) {
1253 id = xcalloc(1, sizeof(*id));
1255 id->filename = comment;
1257 TAILQ_INSERT_TAIL(&agent, id, next);
1260 /* append remaining agent keys */
1261 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1262 TAILQ_REMOVE(&agent, id, next);
1263 TAILQ_INSERT_TAIL(preferred, id, next);
1265 authctxt->agent = ac;
1267 /* append remaining keys from the config file */
1268 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1269 TAILQ_REMOVE(&files, id, next);
1270 TAILQ_INSERT_TAIL(preferred, id, next);
1272 TAILQ_FOREACH(id, preferred, next) {
1273 debug2("key: %s (%p),%s", id->filename, id->key,
1274 id->userprovided ? " explicit" : "");
1279 pubkey_cleanup(Authctxt *authctxt)
1283 if (authctxt->agent != NULL)
1284 ssh_close_authentication_connection(authctxt->agent);
1285 for (id = TAILQ_FIRST(&authctxt->keys); id;
1286 id = TAILQ_FIRST(&authctxt->keys)) {
1287 TAILQ_REMOVE(&authctxt->keys, id, next);
1296 userauth_pubkey(Authctxt *authctxt)
1301 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1304 /* move key to the end of the queue */
1305 TAILQ_REMOVE(&authctxt->keys, id, next);
1306 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1308 * send a test message if we have the public key. for
1309 * encrypted keys we cannot do this and have to load the
1310 * private key instead
1312 if (id->key != NULL) {
1313 if (key_type_plain(id->key->type) == KEY_RSA &&
1314 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1315 debug("Skipped %s key %s for RSA/MD5 server",
1316 key_type(id->key), id->filename);
1317 } else if (id->key->type != KEY_RSA1) {
1318 debug("Offering %s public key: %s",
1319 key_type(id->key), id->filename);
1320 sent = send_pubkey_test(authctxt, id);
1323 debug("Trying private key: %s", id->filename);
1324 id->key = load_identity_file(id->filename,
1326 if (id->key != NULL) {
1328 if (key_type_plain(id->key->type) == KEY_RSA &&
1329 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1330 debug("Skipped %s key %s for RSA/MD5 "
1331 "server", key_type(id->key),
1334 sent = sign_and_send_pubkey(
1348 * Send userauth request message specifying keyboard-interactive method.
1351 userauth_kbdint(Authctxt *authctxt)
1353 static int attempt = 0;
1355 if (attempt++ >= options.number_of_password_prompts)
1357 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1358 if (attempt > 1 && !authctxt->info_req_seen) {
1359 debug3("userauth_kbdint: disable: no info_req_seen");
1360 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1364 debug2("userauth_kbdint");
1365 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1366 packet_put_cstring(authctxt->server_user);
1367 packet_put_cstring(authctxt->service);
1368 packet_put_cstring(authctxt->method->name);
1369 packet_put_cstring(""); /* lang */
1370 packet_put_cstring(options.kbd_interactive_devices ?
1371 options.kbd_interactive_devices : "");
1374 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1379 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1382 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1384 Authctxt *authctxt = ctxt;
1385 char *name, *inst, *lang, *prompt, *response;
1386 u_int num_prompts, i;
1389 debug2("input_userauth_info_req");
1391 if (authctxt == NULL)
1392 fatal("input_userauth_info_req: no authentication context");
1394 authctxt->info_req_seen = 1;
1396 name = packet_get_string(NULL);
1397 inst = packet_get_string(NULL);
1398 lang = packet_get_string(NULL);
1399 if (strlen(name) > 0)
1401 if (strlen(inst) > 0)
1407 num_prompts = packet_get_int();
1409 * Begin to build info response packet based on prompts requested.
1410 * We commit to providing the correct number of responses, so if
1411 * further on we run into a problem that prevents this, we have to
1412 * be sure and clean this up and send a correct error response.
1414 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1415 packet_put_int(num_prompts);
1417 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1418 for (i = 0; i < num_prompts; i++) {
1419 prompt = packet_get_string(NULL);
1420 echo = packet_get_char();
1422 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1424 packet_put_cstring(response);
1425 explicit_bzero(response, strlen(response));
1429 packet_check_eom(); /* done with parsing incoming message. */
1431 packet_add_padding(64);
1436 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1437 u_char *data, u_int datalen)
1442 int to[2], from[2], status, version = 2;
1444 debug2("ssh_keysign called");
1446 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1447 error("ssh_keysign: not installed: %s", strerror(errno));
1450 if (fflush(stdout) != 0)
1451 error("ssh_keysign: fflush: %s", strerror(errno));
1453 error("ssh_keysign: pipe: %s", strerror(errno));
1456 if (pipe(from) < 0) {
1457 error("ssh_keysign: pipe: %s", strerror(errno));
1460 if ((pid = fork()) < 0) {
1461 error("ssh_keysign: fork: %s", strerror(errno));
1465 /* keep the socket on exec */
1466 fcntl(packet_get_connection_in(), F_SETFD, 0);
1467 permanently_drop_suid(getuid());
1469 if (dup2(from[1], STDOUT_FILENO) < 0)
1470 fatal("ssh_keysign: dup2: %s", strerror(errno));
1472 if (dup2(to[0], STDIN_FILENO) < 0)
1473 fatal("ssh_keysign: dup2: %s", strerror(errno));
1476 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1477 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1484 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1485 buffer_put_string(&b, data, datalen);
1486 if (ssh_msg_send(to[1], version, &b) == -1)
1487 fatal("ssh_keysign: couldn't send request");
1489 if (ssh_msg_recv(from[0], &b) < 0) {
1490 error("ssh_keysign: no reply");
1497 while (waitpid(pid, &status, 0) < 0)
1501 if (buffer_get_char(&b) != version) {
1502 error("ssh_keysign: bad version");
1506 *sigp = buffer_get_string(&b, lenp);
1513 userauth_hostbased(Authctxt *authctxt)
1515 Key *private = NULL;
1516 Sensitive *sensitive = authctxt->sensitive;
1518 u_char *signature, *blob;
1519 char *chost, *pkalg, *p;
1520 const char *service;
1522 int ok, i, found = 0;
1524 /* check for a useful key */
1525 for (i = 0; i < sensitive->nkeys; i++) {
1526 private = sensitive->keys[i];
1527 if (private && private->type != KEY_RSA1) {
1529 /* we take and free the key */
1530 sensitive->keys[i] = NULL;
1535 debug("No more client hostkeys for hostbased authentication.");
1538 if (key_to_blob(private, &blob, &blen) == 0) {
1542 /* figure out a name for the client host */
1543 p = get_local_name(packet_get_connection_in());
1545 error("userauth_hostbased: cannot get local ipaddr/name");
1550 xasprintf(&chost, "%s.", p);
1551 debug2("userauth_hostbased: chost %s", chost);
1554 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1556 pkalg = xstrdup(key_ssh_name(private));
1558 /* construct data */
1559 buffer_put_string(&b, session_id2, session_id2_len);
1560 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1561 buffer_put_cstring(&b, authctxt->server_user);
1562 buffer_put_cstring(&b, service);
1563 buffer_put_cstring(&b, authctxt->method->name);
1564 buffer_put_cstring(&b, pkalg);
1565 buffer_put_string(&b, blob, blen);
1566 buffer_put_cstring(&b, chost);
1567 buffer_put_cstring(&b, authctxt->local_user);
1571 if (sensitive->external_keysign)
1572 ok = ssh_keysign(private, &signature, &slen,
1573 buffer_ptr(&b), buffer_len(&b));
1575 ok = key_sign(private, &signature, &slen,
1576 buffer_ptr(&b), buffer_len(&b));
1580 error("key_sign failed");
1586 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1587 packet_put_cstring(authctxt->server_user);
1588 packet_put_cstring(authctxt->service);
1589 packet_put_cstring(authctxt->method->name);
1590 packet_put_cstring(pkalg);
1591 packet_put_string(blob, blen);
1592 packet_put_cstring(chost);
1593 packet_put_cstring(authctxt->local_user);
1594 packet_put_string(signature, slen);
1595 explicit_bzero(signature, slen);
1605 /* find auth method */
1608 * given auth method name, if configurable options permit this method fill
1609 * in auth_ident field and return true, otherwise return false.
1612 authmethod_is_enabled(Authmethod *method)
1616 /* return false if options indicate this method is disabled */
1617 if (method->enabled == NULL || *method->enabled == 0)
1619 /* return false if batch mode is enabled but method needs interactive mode */
1620 if (method->batch_flag != NULL && *method->batch_flag != 0)
1626 authmethod_lookup(const char *name)
1628 Authmethod *method = NULL;
1630 for (method = authmethods; method->name != NULL; method++)
1631 if (strcmp(name, method->name) == 0)
1633 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1637 /* XXX internal state */
1638 static Authmethod *current = NULL;
1639 static char *supported = NULL;
1640 static char *preferred = NULL;
1643 * Given the authentication method list sent by the server, return the
1644 * next method we should try. If the server initially sends a nil list,
1645 * use a built-in default list.
1648 authmethod_get(char *authlist)
1653 /* Use a suitable default if we're passed a nil list. */
1654 if (authlist == NULL || strlen(authlist) == 0)
1655 authlist = options.preferred_authentications;
1657 if (supported == NULL || strcmp(authlist, supported) != 0) {
1658 debug3("start over, passed a different list %s", authlist);
1660 supported = xstrdup(authlist);
1661 preferred = options.preferred_authentications;
1662 debug3("preferred %s", preferred);
1664 } else if (current != NULL && authmethod_is_enabled(current))
1668 if ((name = match_list(preferred, supported, &next)) == NULL) {
1669 debug("No more authentication methods to try.");
1674 debug3("authmethod_lookup %s", name);
1675 debug3("remaining preferred: %s", preferred);
1676 if ((current = authmethod_lookup(name)) != NULL &&
1677 authmethod_is_enabled(current)) {
1678 debug3("authmethod_is_enabled %s", name);
1679 debug("Next authentication method: %s", name);
1688 authmethods_get(void)
1690 Authmethod *method = NULL;
1695 for (method = authmethods; method->name != NULL; method++) {
1696 if (authmethod_is_enabled(method)) {
1697 if (buffer_len(&b) > 0)
1698 buffer_append(&b, ",", 1);
1699 buffer_append(&b, method->name, strlen(method->name));
1702 buffer_append(&b, "\0", 1);
1703 list = xstrdup(buffer_ptr(&b));