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.
30 #include <sys/types.h>
31 #include <sys/socket.h>
44 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
48 #include "openbsd-compat/sys-queue.h"
59 #include "myproposal.h"
60 #include "sshconnect.h"
71 #include "pathnames.h"
80 extern char *client_version_string;
81 extern char *server_version_string;
82 extern Options options;
83 #ifdef NONE_CIPHER_ENABLED
87 * tty_flag is set in ssh.c so we can use it here. If set then prevent
88 * the switch to the null cipher.
98 u_char *session_id2 = NULL;
99 u_int session_id2_len = 0;
102 struct sockaddr *xxx_hostaddr;
107 verify_host_key_callback(Key *hostkey)
109 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
110 fatal("Host key verification failed.");
115 order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
117 char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
119 struct hostkeys *hostkeys;
123 /* Find all hostkeys for this hostname */
124 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
125 hostkeys = init_hostkeys();
126 for (i = 0; i < options.num_user_hostfiles; i++)
127 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
128 for (i = 0; i < options.num_system_hostfiles; i++)
129 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
131 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
132 maxlen = strlen(avail) + 1;
133 first = xmalloc(maxlen);
134 last = xmalloc(maxlen);
135 *first = *last = '\0';
137 #define ALG_APPEND(to, from) \
140 strlcat(to, ",", maxlen); \
141 strlcat(to, from, maxlen); \
144 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
145 if ((ktype = key_type_from_name(alg)) == KEY_UNSPEC)
146 fatal("%s: unknown alg %s", __func__, alg);
147 if (lookup_key_in_hostkeys_by_type(hostkeys,
148 key_type_plain(ktype), NULL))
149 ALG_APPEND(first, alg);
151 ALG_APPEND(last, alg);
154 xasprintf(&ret, "%s%s%s", first, *first == '\0' ? "" : ",", last);
156 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
162 free_hostkeys(hostkeys);
168 ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
173 xxx_hostaddr = hostaddr;
175 if (options.ciphers == (char *)-1) {
176 logit("No valid ciphers for protocol version 2 given, using defaults.");
177 options.ciphers = NULL;
179 if (options.ciphers != NULL) {
180 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
181 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
183 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
184 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
185 myproposal[PROPOSAL_ENC_ALGS_STOC] =
186 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
187 if (options.compression) {
188 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
189 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
191 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
192 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
194 if (options.macs != NULL) {
195 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
196 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
198 if (options.hostkeyalgorithms != NULL)
199 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
200 compat_pkalg_proposal(options.hostkeyalgorithms);
202 /* Prefer algorithms that we already have keys for */
203 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
204 compat_pkalg_proposal(
205 order_hostkeyalgs(host, hostaddr, port));
207 if (options.kex_algorithms != NULL)
208 myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
209 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(
210 myproposal[PROPOSAL_KEX_ALGS]);
212 if (options.rekey_limit || options.rekey_interval)
213 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
214 (time_t)options.rekey_interval);
216 /* start key exchange */
217 kex = kex_setup(myproposal);
218 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
219 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
220 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
221 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
222 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
223 kex->kex[KEX_C25519_SHA256] = kexc25519_client;
224 kex->client_version_string=client_version_string;
225 kex->server_version_string=server_version_string;
226 kex->verify_host_key=&verify_host_key_callback;
230 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
232 if (options.use_roaming && !kex->roaming) {
233 debug("Roaming not allowed by server");
234 options.use_roaming = 0;
237 session_id2 = kex->session_id;
238 session_id2_len = kex->session_id_len;
241 /* send 1st encrypted/maced/compressed message */
242 packet_start(SSH2_MSG_IGNORE);
243 packet_put_cstring("markus");
253 typedef struct Authctxt Authctxt;
254 typedef struct Authmethod Authmethod;
255 typedef struct identity Identity;
256 typedef struct idlist Idlist;
259 TAILQ_ENTRY(identity) next;
260 AuthenticationConnection *ac; /* set if agent supports key */
261 Key *key; /* public/private key */
262 char *filename; /* comment for agent-only keys */
264 int isprivate; /* key points to the private key */
267 TAILQ_HEAD(idlist, identity);
270 const char *server_user;
271 const char *local_user;
275 sig_atomic_t success;
279 AuthenticationConnection *agent;
281 Sensitive *sensitive;
282 /* kbd-interactive */
288 char *name; /* string to compare against server's list */
289 int (*userauth)(Authctxt *authctxt);
290 void (*cleanup)(Authctxt *authctxt);
291 int *enabled; /* flag in option struct that enables method */
292 int *batch_flag; /* flag in option struct that disables method */
295 void input_userauth_success(int, u_int32_t, void *);
296 void input_userauth_success_unexpected(int, u_int32_t, void *);
297 void input_userauth_failure(int, u_int32_t, void *);
298 void input_userauth_banner(int, u_int32_t, void *);
299 void input_userauth_error(int, u_int32_t, void *);
300 void input_userauth_info_req(int, u_int32_t, void *);
301 void input_userauth_pk_ok(int, u_int32_t, void *);
302 void input_userauth_passwd_changereq(int, u_int32_t, void *);
304 int userauth_none(Authctxt *);
305 int userauth_pubkey(Authctxt *);
306 int userauth_passwd(Authctxt *);
307 int userauth_kbdint(Authctxt *);
308 int userauth_hostbased(Authctxt *);
311 int userauth_gssapi(Authctxt *authctxt);
312 void input_gssapi_response(int type, u_int32_t, void *);
313 void input_gssapi_token(int type, u_int32_t, void *);
314 void input_gssapi_hash(int type, u_int32_t, void *);
315 void input_gssapi_error(int, u_int32_t, void *);
316 void input_gssapi_errtok(int, u_int32_t, void *);
319 void userauth(Authctxt *, char *);
321 static int sign_and_send_pubkey(Authctxt *, Identity *);
322 static void pubkey_prepare(Authctxt *);
323 static void pubkey_cleanup(Authctxt *);
324 static Key *load_identity_file(char *, int);
326 static Authmethod *authmethod_get(char *authlist);
327 static Authmethod *authmethod_lookup(const char *name);
328 static char *authmethods_get(void);
330 Authmethod authmethods[] = {
335 &options.gss_authentication,
341 &options.hostbased_authentication,
346 &options.pubkey_authentication,
348 {"keyboard-interactive",
351 &options.kbd_interactive_authentication,
352 &options.batch_mode},
356 &options.password_authentication,
357 &options.batch_mode},
363 {NULL, NULL, NULL, NULL, NULL}
367 ssh_userauth2(const char *local_user, const char *server_user, char *host,
368 Sensitive *sensitive)
373 if (options.challenge_response_authentication)
374 options.kbd_interactive_authentication = 1;
376 packet_start(SSH2_MSG_SERVICE_REQUEST);
377 packet_put_cstring("ssh-userauth");
379 debug("SSH2_MSG_SERVICE_REQUEST sent");
381 type = packet_read();
382 if (type != SSH2_MSG_SERVICE_ACCEPT)
383 fatal("Server denied authentication request: %d", type);
384 if (packet_remaining() > 0) {
385 char *reply = packet_get_string(NULL);
386 debug2("service_accept: %s", reply);
389 debug2("buggy server: service_accept w/o service");
392 debug("SSH2_MSG_SERVICE_ACCEPT received");
394 if (options.preferred_authentications == NULL)
395 options.preferred_authentications = authmethods_get();
397 /* setup authentication context */
398 memset(&authctxt, 0, sizeof(authctxt));
399 pubkey_prepare(&authctxt);
400 authctxt.server_user = server_user;
401 authctxt.local_user = local_user;
402 authctxt.host = host;
403 authctxt.service = "ssh-connection"; /* service name */
404 authctxt.success = 0;
405 authctxt.method = authmethod_lookup("none");
406 authctxt.authlist = NULL;
407 authctxt.methoddata = NULL;
408 authctxt.sensitive = sensitive;
409 authctxt.info_req_seen = 0;
410 if (authctxt.method == NULL)
411 fatal("ssh_userauth2: internal error: cannot send userauth none request");
413 /* initial userauth request */
414 userauth_none(&authctxt);
416 dispatch_init(&input_userauth_error);
417 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
418 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
419 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
420 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
422 pubkey_cleanup(&authctxt);
423 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
425 #ifdef NONE_CIPHER_ENABLED
427 * If the user explicitly requests to use the none cipher enable it
428 * post authentication and only if the right conditions are met: both
429 * of the NONE switches must be true and there must be no tty allocated.
431 if (options.none_switch == 1 && options.none_enabled == 1) {
433 debug("Requesting none cipher re-keying...");
434 myproposal[PROPOSAL_ENC_ALGS_STOC] = "none";
435 myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none";
436 kex_prop2buf(&xxx_kex->my, myproposal);
437 packet_request_rekeying();
438 fprintf(stderr, "WARNING: enabled NONE cipher\n");
440 /* Requested NONE cipher on an interactive session. */
441 debug("Cannot switch to NONE cipher with tty "
443 fprintf(stderr, "NONE cipher switch disabled given "
444 "a TTY is allocated\n");
448 debug("Authentication succeeded (%s).", authctxt.method->name);
452 userauth(Authctxt *authctxt, char *authlist)
454 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
455 authctxt->method->cleanup(authctxt);
457 free(authctxt->methoddata);
458 authctxt->methoddata = NULL;
459 if (authlist == NULL) {
460 authlist = authctxt->authlist;
462 free(authctxt->authlist);
463 authctxt->authlist = authlist;
466 Authmethod *method = authmethod_get(authlist);
468 fatal("Permission denied (%s).", authlist);
469 authctxt->method = method;
471 /* reset the per method handler */
472 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
473 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
475 /* and try new method */
476 if (method->userauth(authctxt) != 0) {
477 debug2("we sent a %s packet, wait for reply", method->name);
480 debug2("we did not send a packet, disable method");
481 method->enabled = NULL;
488 input_userauth_error(int type, u_int32_t seq, void *ctxt)
490 fatal("input_userauth_error: bad message during authentication: "
496 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
498 char *msg, *raw, *lang;
501 debug3("input_userauth_banner");
502 raw = packet_get_string(&len);
503 lang = packet_get_string(NULL);
504 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
507 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
508 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
509 fprintf(stderr, "%s", msg);
518 input_userauth_success(int type, u_int32_t seq, void *ctxt)
520 Authctxt *authctxt = ctxt;
522 if (authctxt == NULL)
523 fatal("input_userauth_success: no authentication context");
524 free(authctxt->authlist);
525 authctxt->authlist = NULL;
526 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
527 authctxt->method->cleanup(authctxt);
528 free(authctxt->methoddata);
529 authctxt->methoddata = NULL;
530 authctxt->success = 1; /* break out */
534 input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
536 Authctxt *authctxt = ctxt;
538 if (authctxt == NULL)
539 fatal("%s: no authentication context", __func__);
541 fatal("Unexpected authentication success during %s.",
542 authctxt->method->name);
547 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
549 Authctxt *authctxt = ctxt;
550 char *authlist = NULL;
553 if (authctxt == NULL)
554 fatal("input_userauth_failure: no authentication context");
556 authlist = packet_get_string(NULL);
557 partial = packet_get_char();
561 logit("Authenticated with partial success.");
563 pubkey_cleanup(authctxt);
564 pubkey_prepare(authctxt);
566 debug("Authentications that can continue: %s", authlist);
568 userauth(authctxt, authlist);
573 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
575 Authctxt *authctxt = ctxt;
579 int pktype, sent = 0;
584 if (authctxt == NULL)
585 fatal("input_userauth_pk_ok: no authentication context");
586 if (datafellows & SSH_BUG_PKOK) {
587 /* this is similar to SSH_BUG_PKAUTH */
588 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
589 pkblob = packet_get_string(&blen);
591 buffer_append(&b, pkblob, blen);
592 pkalg = buffer_get_string(&b, &alen);
595 pkalg = packet_get_string(&alen);
596 pkblob = packet_get_string(&blen);
600 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
602 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
603 debug("unknown pkalg %s", pkalg);
606 if ((key = key_from_blob(pkblob, blen)) == NULL) {
607 debug("no key from blob. pkalg %s", pkalg);
610 if (key->type != pktype) {
611 error("input_userauth_pk_ok: type mismatch "
612 "for decoded key (received %d, expected %d)",
616 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
617 debug2("input_userauth_pk_ok: fp %s", fp);
621 * search keys in the reverse order, because last candidate has been
622 * moved to the end of the queue. this also avoids confusion by
625 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
626 if (key_equal(key, id->key)) {
627 sent = sign_and_send_pubkey(authctxt, id);
637 /* try another method if we did not send a packet */
639 userauth(authctxt, NULL);
644 userauth_gssapi(Authctxt *authctxt)
646 Gssctxt *gssctxt = NULL;
647 static gss_OID_set gss_supported = NULL;
648 static u_int mech = 0;
652 /* Try one GSSAPI method at a time, rather than sending them all at
655 if (gss_supported == NULL)
656 gss_indicate_mechs(&min, &gss_supported);
658 /* Check to see if the mechanism is usable before we offer it */
659 while (mech < gss_supported->count && !ok) {
660 /* My DER encoding requires length<128 */
661 if (gss_supported->elements[mech].length < 128 &&
662 ssh_gssapi_check_mechanism(&gssctxt,
663 &gss_supported->elements[mech], authctxt->host)) {
664 ok = 1; /* Mechanism works */
673 authctxt->methoddata=(void *)gssctxt;
675 packet_start(SSH2_MSG_USERAUTH_REQUEST);
676 packet_put_cstring(authctxt->server_user);
677 packet_put_cstring(authctxt->service);
678 packet_put_cstring(authctxt->method->name);
682 packet_put_int((gss_supported->elements[mech].length) + 2);
683 packet_put_char(SSH_GSS_OIDTYPE);
684 packet_put_char(gss_supported->elements[mech].length);
685 packet_put_raw(gss_supported->elements[mech].elements,
686 gss_supported->elements[mech].length);
690 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
691 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
692 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
693 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
695 mech++; /* Move along to next candidate */
701 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
703 Authctxt *authctxt = ctxt;
704 Gssctxt *gssctxt = authctxt->methoddata;
705 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
706 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
707 gss_buffer_desc gssbuf;
708 OM_uint32 status, ms, flags;
711 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
712 recv_tok, &send_tok, &flags);
714 if (send_tok.length > 0) {
715 if (GSS_ERROR(status))
716 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
718 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
720 packet_put_string(send_tok.value, send_tok.length);
722 gss_release_buffer(&ms, &send_tok);
725 if (status == GSS_S_COMPLETE) {
726 /* send either complete or MIC, depending on mechanism */
727 if (!(flags & GSS_C_INTEG_FLAG)) {
728 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
731 ssh_gssapi_buildmic(&b, authctxt->server_user,
732 authctxt->service, "gssapi-with-mic");
734 gssbuf.value = buffer_ptr(&b);
735 gssbuf.length = buffer_len(&b);
737 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
739 if (!GSS_ERROR(status)) {
740 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
741 packet_put_string(mic.value, mic.length);
747 gss_release_buffer(&ms, &mic);
756 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
758 Authctxt *authctxt = ctxt;
763 if (authctxt == NULL)
764 fatal("input_gssapi_response: no authentication context");
765 gssctxt = authctxt->methoddata;
768 oidv = packet_get_string(&oidlen);
771 oidv[0] != SSH_GSS_OIDTYPE ||
772 oidv[1] != oidlen - 2) {
774 debug("Badly encoded mechanism OID received");
775 userauth(authctxt, NULL);
779 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
780 fatal("Server returned different OID than expected");
786 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
787 /* Start again with next method on list */
788 debug("Trying to start again");
789 userauth(authctxt, NULL);
796 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
798 Authctxt *authctxt = ctxt;
799 gss_buffer_desc recv_tok;
803 if (authctxt == NULL)
804 fatal("input_gssapi_response: no authentication context");
806 recv_tok.value = packet_get_string(&slen);
807 recv_tok.length = slen; /* safe typecast */
811 status = process_gssapi_token(ctxt, &recv_tok);
813 free(recv_tok.value);
815 if (GSS_ERROR(status)) {
816 /* Start again with the next method in the list */
817 userauth(authctxt, NULL);
824 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
826 Authctxt *authctxt = ctxt;
828 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
829 gss_buffer_desc recv_tok;
833 if (authctxt == NULL)
834 fatal("input_gssapi_response: no authentication context");
835 gssctxt = authctxt->methoddata;
837 recv_tok.value = packet_get_string(&len);
838 recv_tok.length = len;
842 /* Stick it into GSSAPI and see what it says */
843 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
844 &recv_tok, &send_tok, NULL);
846 free(recv_tok.value);
847 gss_release_buffer(&ms, &send_tok);
849 /* Server will be returning a failed packet after this one */
854 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
859 /* maj */(void)packet_get_int();
860 /* min */(void)packet_get_int();
861 msg=packet_get_string(NULL);
862 lang=packet_get_string(NULL);
866 debug("Server GSSAPI Error:\n%s", msg);
873 userauth_none(Authctxt *authctxt)
875 /* initial userauth request */
876 packet_start(SSH2_MSG_USERAUTH_REQUEST);
877 packet_put_cstring(authctxt->server_user);
878 packet_put_cstring(authctxt->service);
879 packet_put_cstring(authctxt->method->name);
885 userauth_passwd(Authctxt *authctxt)
887 static int attempt = 0;
890 const char *host = options.host_key_alias ? options.host_key_alias :
893 if (attempt++ >= options.number_of_password_prompts)
897 error("Permission denied, please try again.");
899 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
900 authctxt->server_user, host);
901 password = read_passphrase(prompt, 0);
902 packet_start(SSH2_MSG_USERAUTH_REQUEST);
903 packet_put_cstring(authctxt->server_user);
904 packet_put_cstring(authctxt->service);
905 packet_put_cstring(authctxt->method->name);
907 packet_put_cstring(password);
908 explicit_bzero(password, strlen(password));
910 packet_add_padding(64);
913 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
914 &input_userauth_passwd_changereq);
920 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
924 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
926 Authctxt *authctxt = ctxt;
927 char *info, *lang, *password = NULL, *retype = NULL;
929 const char *host = options.host_key_alias ? options.host_key_alias :
932 debug2("input_userauth_passwd_changereq");
934 if (authctxt == NULL)
935 fatal("input_userauth_passwd_changereq: "
936 "no authentication context");
938 info = packet_get_string(NULL);
939 lang = packet_get_string(NULL);
940 if (strlen(info) > 0)
944 packet_start(SSH2_MSG_USERAUTH_REQUEST);
945 packet_put_cstring(authctxt->server_user);
946 packet_put_cstring(authctxt->service);
947 packet_put_cstring(authctxt->method->name);
948 packet_put_char(1); /* additional info */
949 snprintf(prompt, sizeof(prompt),
950 "Enter %.30s@%.128s's old password: ",
951 authctxt->server_user, host);
952 password = read_passphrase(prompt, 0);
953 packet_put_cstring(password);
954 explicit_bzero(password, strlen(password));
957 while (password == NULL) {
958 snprintf(prompt, sizeof(prompt),
959 "Enter %.30s@%.128s's new password: ",
960 authctxt->server_user, host);
961 password = read_passphrase(prompt, RP_ALLOW_EOF);
962 if (password == NULL) {
966 snprintf(prompt, sizeof(prompt),
967 "Retype %.30s@%.128s's new password: ",
968 authctxt->server_user, host);
969 retype = read_passphrase(prompt, 0);
970 if (strcmp(password, retype) != 0) {
971 explicit_bzero(password, strlen(password));
973 logit("Mismatch; try again, EOF to quit.");
976 explicit_bzero(retype, strlen(retype));
979 packet_put_cstring(password);
980 explicit_bzero(password, strlen(password));
982 packet_add_padding(64);
985 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
986 &input_userauth_passwd_changereq);
990 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
991 u_char *data, u_int datalen)
996 /* the agent supports this key */
998 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1001 * we have already loaded the private key or
1002 * the private key is stored in external hardware
1004 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1005 return (key_sign(id->key, sigp, lenp, data, datalen));
1006 /* load the private key from the file */
1007 if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL)
1009 ret = key_sign(prv, sigp, lenp, data, datalen);
1015 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1018 u_char *blob, *signature;
1019 u_int bloblen, slen;
1025 fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
1026 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
1029 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1030 /* we cannot handle this key */
1031 debug3("sign_and_send_pubkey: cannot handle key");
1034 /* data to be signed */
1036 if (datafellows & SSH_OLD_SESSIONID) {
1037 buffer_append(&b, session_id2, session_id2_len);
1038 skip = session_id2_len;
1040 buffer_put_string(&b, session_id2, session_id2_len);
1041 skip = buffer_len(&b);
1043 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1044 buffer_put_cstring(&b, authctxt->server_user);
1045 buffer_put_cstring(&b,
1046 datafellows & SSH_BUG_PKSERVICE ?
1049 if (datafellows & SSH_BUG_PKAUTH) {
1050 buffer_put_char(&b, have_sig);
1052 buffer_put_cstring(&b, authctxt->method->name);
1053 buffer_put_char(&b, have_sig);
1054 buffer_put_cstring(&b, key_ssh_name(id->key));
1056 buffer_put_string(&b, blob, bloblen);
1058 /* generate signature */
1059 ret = identity_sign(id, &signature, &slen,
1060 buffer_ptr(&b), buffer_len(&b));
1069 if (datafellows & SSH_BUG_PKSERVICE) {
1071 buffer_append(&b, session_id2, session_id2_len);
1072 skip = session_id2_len;
1073 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1074 buffer_put_cstring(&b, authctxt->server_user);
1075 buffer_put_cstring(&b, authctxt->service);
1076 buffer_put_cstring(&b, authctxt->method->name);
1077 buffer_put_char(&b, have_sig);
1078 if (!(datafellows & SSH_BUG_PKAUTH))
1079 buffer_put_cstring(&b, key_ssh_name(id->key));
1080 buffer_put_string(&b, blob, bloblen);
1084 /* append signature */
1085 buffer_put_string(&b, signature, slen);
1088 /* skip session id and packet type */
1089 if (buffer_len(&b) < skip + 1)
1090 fatal("userauth_pubkey: internal error");
1091 buffer_consume(&b, skip + 1);
1093 /* put remaining data from buffer into packet */
1094 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1095 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1103 send_pubkey_test(Authctxt *authctxt, Identity *id)
1106 u_int bloblen, have_sig = 0;
1108 debug3("send_pubkey_test");
1110 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1111 /* we cannot handle this key */
1112 debug3("send_pubkey_test: cannot handle key");
1115 /* register callback for USERAUTH_PK_OK message */
1116 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1118 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1119 packet_put_cstring(authctxt->server_user);
1120 packet_put_cstring(authctxt->service);
1121 packet_put_cstring(authctxt->method->name);
1122 packet_put_char(have_sig);
1123 if (!(datafellows & SSH_BUG_PKAUTH))
1124 packet_put_cstring(key_ssh_name(id->key));
1125 packet_put_string(blob, bloblen);
1132 load_identity_file(char *filename, int userprovided)
1135 char prompt[300], *passphrase;
1136 int perm_ok = 0, quit, i;
1139 if (stat(filename, &st) < 0) {
1140 (userprovided ? logit : debug3)("no such identity: %s: %s",
1141 filename, strerror(errno));
1144 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1146 if (private != NULL)
1150 if (private == NULL) {
1151 if (options.batch_mode)
1153 snprintf(prompt, sizeof prompt,
1154 "Enter passphrase for key '%.100s': ", filename);
1155 for (i = 0; i < options.number_of_password_prompts; i++) {
1156 passphrase = read_passphrase(prompt, 0);
1157 if (strcmp(passphrase, "") != 0) {
1158 private = key_load_private_type(KEY_UNSPEC,
1159 filename, passphrase, NULL, NULL);
1162 debug2("no passphrase given, try next key");
1165 explicit_bzero(passphrase, strlen(passphrase));
1167 if (private != NULL || quit)
1169 debug2("bad passphrase given, try again...");
1176 * try keys in the following order:
1177 * 1. agent keys that are found in the config file
1178 * 2. other agent keys
1179 * 3. keys that are only listed in the config file
1182 pubkey_prepare(Authctxt *authctxt)
1184 Identity *id, *id2, *tmp;
1185 Idlist agent, files, *preferred;
1187 AuthenticationConnection *ac;
1191 TAILQ_INIT(&agent); /* keys from the agent */
1192 TAILQ_INIT(&files); /* keys from the config file */
1193 preferred = &authctxt->keys;
1194 TAILQ_INIT(preferred); /* preferred order of keys */
1196 /* list of keys stored in the filesystem and PKCS#11 */
1197 for (i = 0; i < options.num_identity_files; i++) {
1198 key = options.identity_keys[i];
1199 if (key && key->type == KEY_RSA1)
1201 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1203 options.identity_keys[i] = NULL;
1204 id = xcalloc(1, sizeof(*id));
1206 id->filename = xstrdup(options.identity_files[i]);
1207 id->userprovided = options.identity_file_userprovided[i];
1208 TAILQ_INSERT_TAIL(&files, id, next);
1210 /* Prefer PKCS11 keys that are explicitly listed */
1211 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1212 if (id->key == NULL || (id->key->flags & KEY_FLAG_EXT) == 0)
1215 TAILQ_FOREACH(id2, &files, next) {
1216 if (id2->key == NULL ||
1217 (id2->key->flags & KEY_FLAG_EXT) != 0)
1219 if (key_equal(id->key, id2->key)) {
1220 TAILQ_REMOVE(&files, id, next);
1221 TAILQ_INSERT_TAIL(preferred, id, next);
1226 /* If IdentitiesOnly set and key not found then don't use it */
1227 if (!found && options.identities_only) {
1228 TAILQ_REMOVE(&files, id, next);
1229 explicit_bzero(id, sizeof(*id));
1233 /* list of keys supported by the agent */
1234 if ((ac = ssh_get_authentication_connection())) {
1235 for (key = ssh_get_first_identity(ac, &comment, 2);
1237 key = ssh_get_next_identity(ac, &comment, 2)) {
1239 TAILQ_FOREACH(id, &files, next) {
1240 /* agent keys from the config file are preferred */
1241 if (key_equal(key, id->key)) {
1244 TAILQ_REMOVE(&files, id, next);
1245 TAILQ_INSERT_TAIL(preferred, id, next);
1251 if (!found && !options.identities_only) {
1252 id = xcalloc(1, sizeof(*id));
1254 id->filename = comment;
1256 TAILQ_INSERT_TAIL(&agent, id, next);
1259 /* append remaining agent keys */
1260 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1261 TAILQ_REMOVE(&agent, id, next);
1262 TAILQ_INSERT_TAIL(preferred, id, next);
1264 authctxt->agent = ac;
1266 /* append remaining keys from the config file */
1267 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1268 TAILQ_REMOVE(&files, id, next);
1269 TAILQ_INSERT_TAIL(preferred, id, next);
1271 TAILQ_FOREACH(id, preferred, next) {
1272 debug2("key: %s (%p),%s", id->filename, id->key,
1273 id->userprovided ? " explicit" : "");
1278 pubkey_cleanup(Authctxt *authctxt)
1282 if (authctxt->agent != NULL)
1283 ssh_close_authentication_connection(authctxt->agent);
1284 for (id = TAILQ_FIRST(&authctxt->keys); id;
1285 id = TAILQ_FIRST(&authctxt->keys)) {
1286 TAILQ_REMOVE(&authctxt->keys, id, next);
1295 userauth_pubkey(Authctxt *authctxt)
1300 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1303 /* move key to the end of the queue */
1304 TAILQ_REMOVE(&authctxt->keys, id, next);
1305 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1307 * send a test message if we have the public key. for
1308 * encrypted keys we cannot do this and have to load the
1309 * private key instead
1311 if (id->key != NULL) {
1312 if (key_type_plain(id->key->type) == KEY_RSA &&
1313 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1314 debug("Skipped %s key %s for RSA/MD5 server",
1315 key_type(id->key), id->filename);
1316 } else if (id->key->type != KEY_RSA1) {
1317 debug("Offering %s public key: %s",
1318 key_type(id->key), id->filename);
1319 sent = send_pubkey_test(authctxt, id);
1322 debug("Trying private key: %s", id->filename);
1323 id->key = load_identity_file(id->filename,
1325 if (id->key != NULL) {
1327 if (key_type_plain(id->key->type) == KEY_RSA &&
1328 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1329 debug("Skipped %s key %s for RSA/MD5 "
1330 "server", key_type(id->key),
1333 sent = sign_and_send_pubkey(
1347 * Send userauth request message specifying keyboard-interactive method.
1350 userauth_kbdint(Authctxt *authctxt)
1352 static int attempt = 0;
1354 if (attempt++ >= options.number_of_password_prompts)
1356 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1357 if (attempt > 1 && !authctxt->info_req_seen) {
1358 debug3("userauth_kbdint: disable: no info_req_seen");
1359 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1363 debug2("userauth_kbdint");
1364 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1365 packet_put_cstring(authctxt->server_user);
1366 packet_put_cstring(authctxt->service);
1367 packet_put_cstring(authctxt->method->name);
1368 packet_put_cstring(""); /* lang */
1369 packet_put_cstring(options.kbd_interactive_devices ?
1370 options.kbd_interactive_devices : "");
1373 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1378 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1381 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1383 Authctxt *authctxt = ctxt;
1384 char *name, *inst, *lang, *prompt, *response;
1385 u_int num_prompts, i;
1388 debug2("input_userauth_info_req");
1390 if (authctxt == NULL)
1391 fatal("input_userauth_info_req: no authentication context");
1393 authctxt->info_req_seen = 1;
1395 name = packet_get_string(NULL);
1396 inst = packet_get_string(NULL);
1397 lang = packet_get_string(NULL);
1398 if (strlen(name) > 0)
1400 if (strlen(inst) > 0)
1406 num_prompts = packet_get_int();
1408 * Begin to build info response packet based on prompts requested.
1409 * We commit to providing the correct number of responses, so if
1410 * further on we run into a problem that prevents this, we have to
1411 * be sure and clean this up and send a correct error response.
1413 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1414 packet_put_int(num_prompts);
1416 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1417 for (i = 0; i < num_prompts; i++) {
1418 prompt = packet_get_string(NULL);
1419 echo = packet_get_char();
1421 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1423 packet_put_cstring(response);
1424 explicit_bzero(response, strlen(response));
1428 packet_check_eom(); /* done with parsing incoming message. */
1430 packet_add_padding(64);
1435 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1436 u_char *data, u_int datalen)
1441 int to[2], from[2], status, version = 2;
1443 debug2("ssh_keysign called");
1445 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1446 error("ssh_keysign: not installed: %s", strerror(errno));
1449 if (fflush(stdout) != 0)
1450 error("ssh_keysign: fflush: %s", strerror(errno));
1452 error("ssh_keysign: pipe: %s", strerror(errno));
1455 if (pipe(from) < 0) {
1456 error("ssh_keysign: pipe: %s", strerror(errno));
1459 if ((pid = fork()) < 0) {
1460 error("ssh_keysign: fork: %s", strerror(errno));
1464 /* keep the socket on exec */
1465 fcntl(packet_get_connection_in(), F_SETFD, 0);
1466 permanently_drop_suid(getuid());
1468 if (dup2(from[1], STDOUT_FILENO) < 0)
1469 fatal("ssh_keysign: dup2: %s", strerror(errno));
1471 if (dup2(to[0], STDIN_FILENO) < 0)
1472 fatal("ssh_keysign: dup2: %s", strerror(errno));
1475 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1476 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1483 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1484 buffer_put_string(&b, data, datalen);
1485 if (ssh_msg_send(to[1], version, &b) == -1)
1486 fatal("ssh_keysign: couldn't send request");
1488 if (ssh_msg_recv(from[0], &b) < 0) {
1489 error("ssh_keysign: no reply");
1496 while (waitpid(pid, &status, 0) < 0)
1500 if (buffer_get_char(&b) != version) {
1501 error("ssh_keysign: bad version");
1505 *sigp = buffer_get_string(&b, lenp);
1512 userauth_hostbased(Authctxt *authctxt)
1514 Key *private = NULL;
1515 Sensitive *sensitive = authctxt->sensitive;
1517 u_char *signature, *blob;
1518 char *chost, *pkalg, *p;
1519 const char *service;
1521 int ok, i, found = 0;
1523 /* check for a useful key */
1524 for (i = 0; i < sensitive->nkeys; i++) {
1525 private = sensitive->keys[i];
1526 if (private && private->type != KEY_RSA1) {
1528 /* we take and free the key */
1529 sensitive->keys[i] = NULL;
1534 debug("No more client hostkeys for hostbased authentication.");
1537 if (key_to_blob(private, &blob, &blen) == 0) {
1541 /* figure out a name for the client host */
1542 p = get_local_name(packet_get_connection_in());
1544 error("userauth_hostbased: cannot get local ipaddr/name");
1549 xasprintf(&chost, "%s.", p);
1550 debug2("userauth_hostbased: chost %s", chost);
1553 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1555 pkalg = xstrdup(key_ssh_name(private));
1557 /* construct data */
1558 buffer_put_string(&b, session_id2, session_id2_len);
1559 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1560 buffer_put_cstring(&b, authctxt->server_user);
1561 buffer_put_cstring(&b, service);
1562 buffer_put_cstring(&b, authctxt->method->name);
1563 buffer_put_cstring(&b, pkalg);
1564 buffer_put_string(&b, blob, blen);
1565 buffer_put_cstring(&b, chost);
1566 buffer_put_cstring(&b, authctxt->local_user);
1570 if (sensitive->external_keysign)
1571 ok = ssh_keysign(private, &signature, &slen,
1572 buffer_ptr(&b), buffer_len(&b));
1574 ok = key_sign(private, &signature, &slen,
1575 buffer_ptr(&b), buffer_len(&b));
1579 error("key_sign failed");
1585 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1586 packet_put_cstring(authctxt->server_user);
1587 packet_put_cstring(authctxt->service);
1588 packet_put_cstring(authctxt->method->name);
1589 packet_put_cstring(pkalg);
1590 packet_put_string(blob, blen);
1591 packet_put_cstring(chost);
1592 packet_put_cstring(authctxt->local_user);
1593 packet_put_string(signature, slen);
1594 explicit_bzero(signature, slen);
1604 /* find auth method */
1607 * given auth method name, if configurable options permit this method fill
1608 * in auth_ident field and return true, otherwise return false.
1611 authmethod_is_enabled(Authmethod *method)
1615 /* return false if options indicate this method is disabled */
1616 if (method->enabled == NULL || *method->enabled == 0)
1618 /* return false if batch mode is enabled but method needs interactive mode */
1619 if (method->batch_flag != NULL && *method->batch_flag != 0)
1625 authmethod_lookup(const char *name)
1627 Authmethod *method = NULL;
1629 for (method = authmethods; method->name != NULL; method++)
1630 if (strcmp(name, method->name) == 0)
1632 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1636 /* XXX internal state */
1637 static Authmethod *current = NULL;
1638 static char *supported = NULL;
1639 static char *preferred = NULL;
1642 * Given the authentication method list sent by the server, return the
1643 * next method we should try. If the server initially sends a nil list,
1644 * use a built-in default list.
1647 authmethod_get(char *authlist)
1652 /* Use a suitable default if we're passed a nil list. */
1653 if (authlist == NULL || strlen(authlist) == 0)
1654 authlist = options.preferred_authentications;
1656 if (supported == NULL || strcmp(authlist, supported) != 0) {
1657 debug3("start over, passed a different list %s", authlist);
1659 supported = xstrdup(authlist);
1660 preferred = options.preferred_authentications;
1661 debug3("preferred %s", preferred);
1663 } else if (current != NULL && authmethod_is_enabled(current))
1667 if ((name = match_list(preferred, supported, &next)) == NULL) {
1668 debug("No more authentication methods to try.");
1673 debug3("authmethod_lookup %s", name);
1674 debug3("remaining preferred: %s", preferred);
1675 if ((current = authmethod_lookup(name)) != NULL &&
1676 authmethod_is_enabled(current)) {
1677 debug3("authmethod_is_enabled %s", name);
1678 debug("Next authentication method: %s", name);
1687 authmethods_get(void)
1689 Authmethod *method = NULL;
1694 for (method = authmethods; method->name != NULL; method++) {
1695 if (authmethod_is_enabled(method)) {
1696 if (buffer_len(&b) > 0)
1697 buffer_append(&b, ",", 1);
1698 buffer_append(&b, method->name, strlen(method->name));
1701 buffer_append(&b, "\0", 1);
1702 list = xstrdup(buffer_ptr(&b));