1 /* $OpenBSD: sshconnect2.c,v 1.251 2016/12/04 23:54:02 djm Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Copyright (c) 2008 Damien Miller. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
30 #include <sys/socket.h>
43 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
47 #include "openbsd-compat/sys-queue.h"
58 #include "myproposal.h"
59 #include "sshconnect.h"
70 #include "pathnames.h"
81 extern char *client_version_string;
82 extern char *server_version_string;
83 extern Options options;
89 u_char *session_id2 = NULL;
90 u_int session_id2_len = 0;
93 struct sockaddr *xxx_hostaddr;
96 verify_host_key_callback(Key *hostkey, struct ssh *ssh)
98 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
99 fatal("Host key verification failed.");
104 order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
106 char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
108 struct hostkeys *hostkeys;
112 /* Find all hostkeys for this hostname */
113 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
114 hostkeys = init_hostkeys();
115 for (i = 0; i < options.num_user_hostfiles; i++)
116 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
117 for (i = 0; i < options.num_system_hostfiles; i++)
118 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
120 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
121 maxlen = strlen(avail) + 1;
122 first = xmalloc(maxlen);
123 last = xmalloc(maxlen);
124 *first = *last = '\0';
126 #define ALG_APPEND(to, from) \
129 strlcat(to, ",", maxlen); \
130 strlcat(to, from, maxlen); \
133 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
134 if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
135 fatal("%s: unknown alg %s", __func__, alg);
136 if (lookup_key_in_hostkeys_by_type(hostkeys,
137 sshkey_type_plain(ktype), NULL))
138 ALG_APPEND(first, alg);
140 ALG_APPEND(last, alg);
143 xasprintf(&ret, "%s%s%s", first,
144 (*first == '\0' || *last == '\0') ? "" : ",", last);
146 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
152 free_hostkeys(hostkeys);
158 ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
160 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
166 xxx_hostaddr = hostaddr;
168 if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
169 fatal("%s: kex_names_cat", __func__);
170 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(s);
171 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
172 compat_cipher_proposal(options.ciphers);
173 myproposal[PROPOSAL_ENC_ALGS_STOC] =
174 compat_cipher_proposal(options.ciphers);
175 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
176 myproposal[PROPOSAL_COMP_ALGS_STOC] = options.compression ?
177 "zlib@openssh.com,zlib,none" : "none,zlib@openssh.com,zlib";
178 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
179 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
180 if (options.hostkeyalgorithms != NULL) {
181 if (kex_assemble_names(KEX_DEFAULT_PK_ALG,
182 &options.hostkeyalgorithms) != 0)
183 fatal("%s: kex_assemble_namelist", __func__);
184 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
185 compat_pkalg_proposal(options.hostkeyalgorithms);
187 /* Enforce default */
188 options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG);
189 /* Prefer algorithms that we already have keys for */
190 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
191 compat_pkalg_proposal(
192 order_hostkeyalgs(host, hostaddr, port));
195 if (options.rekey_limit || options.rekey_interval)
196 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
197 (time_t)options.rekey_interval);
199 /* start key exchange */
200 if ((r = kex_setup(active_state, myproposal)) != 0)
201 fatal("kex_setup: %s", ssh_err(r));
202 kex = active_state->kex;
204 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
205 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
206 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_client;
207 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_client;
208 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_client;
209 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
210 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
211 # ifdef OPENSSL_HAS_ECC
212 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
215 kex->kex[KEX_C25519_SHA256] = kexc25519_client;
216 kex->client_version_string=client_version_string;
217 kex->server_version_string=server_version_string;
218 kex->verify_host_key=&verify_host_key_callback;
220 dispatch_run(DISPATCH_BLOCK, &kex->done, active_state);
222 /* remove ext-info from the KEX proposals for rekeying */
223 myproposal[PROPOSAL_KEX_ALGS] =
224 compat_kex_proposal(options.kex_algorithms);
225 if ((r = kex_prop2buf(kex->my, myproposal)) != 0)
226 fatal("kex_prop2buf: %s", ssh_err(r));
228 session_id2 = kex->session_id;
229 session_id2_len = kex->session_id_len;
232 /* send 1st encrypted/maced/compressed message */
233 packet_start(SSH2_MSG_IGNORE);
234 packet_put_cstring("markus");
244 typedef struct cauthctxt Authctxt;
245 typedef struct cauthmethod Authmethod;
246 typedef struct identity Identity;
247 typedef struct idlist Idlist;
250 TAILQ_ENTRY(identity) next;
251 int agent_fd; /* >=0 if agent supports key */
252 struct sshkey *key; /* public/private key */
253 char *filename; /* comment for agent-only keys */
255 int isprivate; /* key points to the private key */
258 TAILQ_HEAD(idlist, identity);
261 const char *server_user;
262 const char *local_user;
265 struct cauthmethod *method;
266 sig_atomic_t success;
273 Sensitive *sensitive;
274 char *oktypes, *ktypes;
275 const char *active_ktype;
276 /* kbd-interactive */
283 char *name; /* string to compare against server's list */
284 int (*userauth)(Authctxt *authctxt);
285 void (*cleanup)(Authctxt *authctxt);
286 int *enabled; /* flag in option struct that enables method */
287 int *batch_flag; /* flag in option struct that disables method */
290 int input_userauth_service_accept(int, u_int32_t, void *);
291 int input_userauth_ext_info(int, u_int32_t, void *);
292 int input_userauth_success(int, u_int32_t, void *);
293 int input_userauth_success_unexpected(int, u_int32_t, void *);
294 int input_userauth_failure(int, u_int32_t, void *);
295 int input_userauth_banner(int, u_int32_t, void *);
296 int input_userauth_error(int, u_int32_t, void *);
297 int input_userauth_info_req(int, u_int32_t, void *);
298 int input_userauth_pk_ok(int, u_int32_t, void *);
299 int input_userauth_passwd_changereq(int, u_int32_t, void *);
301 int userauth_none(Authctxt *);
302 int userauth_pubkey(Authctxt *);
303 int userauth_passwd(Authctxt *);
304 int userauth_kbdint(Authctxt *);
305 int userauth_hostbased(Authctxt *);
308 int userauth_gssapi(Authctxt *authctxt);
309 int input_gssapi_response(int type, u_int32_t, void *);
310 int input_gssapi_token(int type, u_int32_t, void *);
311 int input_gssapi_hash(int type, u_int32_t, void *);
312 int input_gssapi_error(int, u_int32_t, void *);
313 int input_gssapi_errtok(int, u_int32_t, void *);
316 void userauth(Authctxt *, char *);
318 static int sign_and_send_pubkey(Authctxt *, Identity *);
319 static void pubkey_prepare(Authctxt *);
320 static void pubkey_cleanup(Authctxt *);
321 static void pubkey_reset(Authctxt *);
322 static Key *load_identity_file(Identity *);
324 static Authmethod *authmethod_get(char *authlist);
325 static Authmethod *authmethod_lookup(const char *name);
326 static char *authmethods_get(void);
328 Authmethod authmethods[] = {
333 &options.gss_authentication,
339 &options.hostbased_authentication,
344 &options.pubkey_authentication,
346 {"keyboard-interactive",
349 &options.kbd_interactive_authentication,
350 &options.batch_mode},
354 &options.password_authentication,
355 &options.batch_mode},
361 {NULL, NULL, NULL, NULL, NULL}
365 ssh_userauth2(const char *local_user, const char *server_user, char *host,
366 Sensitive *sensitive)
368 struct ssh *ssh = active_state;
372 if (options.challenge_response_authentication)
373 options.kbd_interactive_authentication = 1;
374 if (options.preferred_authentications == NULL)
375 options.preferred_authentications = authmethods_get();
377 /* setup authentication context */
378 memset(&authctxt, 0, sizeof(authctxt));
379 pubkey_prepare(&authctxt);
380 authctxt.server_user = server_user;
381 authctxt.local_user = local_user;
382 authctxt.host = host;
383 authctxt.service = "ssh-connection"; /* service name */
384 authctxt.success = 0;
385 authctxt.method = authmethod_lookup("none");
386 authctxt.authlist = NULL;
387 authctxt.methoddata = NULL;
388 authctxt.sensitive = sensitive;
389 authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
390 authctxt.info_req_seen = 0;
391 authctxt.agent_fd = -1;
392 if (authctxt.method == NULL)
393 fatal("ssh_userauth2: internal error: cannot send userauth none request");
395 if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
396 (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
397 (r = sshpkt_send(ssh)) != 0)
398 fatal("%s: %s", __func__, ssh_err(r));
400 ssh_dispatch_init(ssh, &input_userauth_error);
401 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
402 ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
403 ssh_dispatch_run(ssh, DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
405 pubkey_cleanup(&authctxt);
406 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
408 if (!authctxt.success)
409 fatal("Authentication failed.");
410 debug("Authentication succeeded (%s).", authctxt.method->name);
415 input_userauth_service_accept(int type, u_int32_t seqnr, void *ctxt)
417 Authctxt *authctxt = ctxt;
418 struct ssh *ssh = active_state;
421 if (ssh_packet_remaining(ssh) > 0) {
424 if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
426 debug2("service_accept: %s", reply);
429 debug2("buggy server: service_accept w/o service");
431 if ((r = sshpkt_get_end(ssh)) != 0)
433 debug("SSH2_MSG_SERVICE_ACCEPT received");
435 /* initial userauth request */
436 userauth_none(authctxt);
438 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error);
439 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
440 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
441 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
449 input_userauth_ext_info(int type, u_int32_t seqnr, void *ctxt)
451 return kex_input_ext_info(type, seqnr, active_state);
455 userauth(Authctxt *authctxt, char *authlist)
457 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
458 authctxt->method->cleanup(authctxt);
460 free(authctxt->methoddata);
461 authctxt->methoddata = NULL;
462 if (authlist == NULL) {
463 authlist = authctxt->authlist;
465 free(authctxt->authlist);
466 authctxt->authlist = authlist;
469 Authmethod *method = authmethod_get(authlist);
471 fatal("Permission denied (%s).", authlist);
472 authctxt->method = method;
474 /* reset the per method handler */
475 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
476 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
478 /* and try new method */
479 if (method->userauth(authctxt) != 0) {
480 debug2("we sent a %s packet, wait for reply", method->name);
483 debug2("we did not send a packet, disable method");
484 method->enabled = NULL;
491 input_userauth_error(int type, u_int32_t seq, void *ctxt)
493 fatal("input_userauth_error: bad message during authentication: "
500 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
505 debug3("%s", __func__);
506 msg = packet_get_string(&len);
507 lang = packet_get_string(NULL);
508 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO)
509 fmprintf(stderr, "%s", msg);
517 input_userauth_success(int type, u_int32_t seq, void *ctxt)
519 Authctxt *authctxt = ctxt;
521 if (authctxt == NULL)
522 fatal("input_userauth_success: no authentication context");
523 free(authctxt->authlist);
524 authctxt->authlist = NULL;
525 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
526 authctxt->method->cleanup(authctxt);
527 free(authctxt->methoddata);
528 authctxt->methoddata = NULL;
529 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);
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 verbose("Authenticated with partial success.");
564 pubkey_reset(authctxt);
566 debug("Authentications that can continue: %s", authlist);
568 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 if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
618 SSH_FP_DEFAULT)) == NULL)
620 debug2("input_userauth_pk_ok: fp %s", fp);
624 * search keys in the reverse order, because last candidate has been
625 * moved to the end of the queue. this also avoids confusion by
628 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
629 if (key_equal(key, id->key)) {
630 sent = sign_and_send_pubkey(authctxt, id);
640 /* try another method if we did not send a packet */
642 userauth(authctxt, NULL);
648 userauth_gssapi(Authctxt *authctxt)
650 Gssctxt *gssctxt = NULL;
651 static gss_OID_set gss_supported = NULL;
652 static u_int mech = 0;
656 /* Try one GSSAPI method at a time, rather than sending them all at
659 if (gss_supported == NULL)
660 gss_indicate_mechs(&min, &gss_supported);
662 /* Check to see if the mechanism is usable before we offer it */
663 while (mech < gss_supported->count && !ok) {
664 /* My DER encoding requires length<128 */
665 if (gss_supported->elements[mech].length < 128 &&
666 ssh_gssapi_check_mechanism(&gssctxt,
667 &gss_supported->elements[mech], authctxt->host)) {
668 ok = 1; /* Mechanism works */
677 authctxt->methoddata=(void *)gssctxt;
679 packet_start(SSH2_MSG_USERAUTH_REQUEST);
680 packet_put_cstring(authctxt->server_user);
681 packet_put_cstring(authctxt->service);
682 packet_put_cstring(authctxt->method->name);
686 packet_put_int((gss_supported->elements[mech].length) + 2);
687 packet_put_char(SSH_GSS_OIDTYPE);
688 packet_put_char(gss_supported->elements[mech].length);
689 packet_put_raw(gss_supported->elements[mech].elements,
690 gss_supported->elements[mech].length);
694 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
695 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
696 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
697 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
699 mech++; /* Move along to next candidate */
705 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
707 Authctxt *authctxt = ctxt;
708 Gssctxt *gssctxt = authctxt->methoddata;
709 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
710 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
711 gss_buffer_desc gssbuf;
712 OM_uint32 status, ms, flags;
715 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
716 recv_tok, &send_tok, &flags);
718 if (send_tok.length > 0) {
719 if (GSS_ERROR(status))
720 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
722 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
724 packet_put_string(send_tok.value, send_tok.length);
726 gss_release_buffer(&ms, &send_tok);
729 if (status == GSS_S_COMPLETE) {
730 /* send either complete or MIC, depending on mechanism */
731 if (!(flags & GSS_C_INTEG_FLAG)) {
732 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
735 ssh_gssapi_buildmic(&b, authctxt->server_user,
736 authctxt->service, "gssapi-with-mic");
738 gssbuf.value = buffer_ptr(&b);
739 gssbuf.length = buffer_len(&b);
741 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
743 if (!GSS_ERROR(status)) {
744 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
745 packet_put_string(mic.value, mic.length);
751 gss_release_buffer(&ms, &mic);
760 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
762 Authctxt *authctxt = ctxt;
767 if (authctxt == NULL)
768 fatal("input_gssapi_response: no authentication context");
769 gssctxt = authctxt->methoddata;
772 oidv = packet_get_string(&oidlen);
775 oidv[0] != SSH_GSS_OIDTYPE ||
776 oidv[1] != oidlen - 2) {
778 debug("Badly encoded mechanism OID received");
779 userauth(authctxt, NULL);
783 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
784 fatal("Server returned different OID than expected");
790 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
791 /* Start again with next method on list */
792 debug("Trying to start again");
793 userauth(authctxt, NULL);
801 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
803 Authctxt *authctxt = ctxt;
804 gss_buffer_desc recv_tok;
808 if (authctxt == NULL)
809 fatal("input_gssapi_response: no authentication context");
811 recv_tok.value = packet_get_string(&slen);
812 recv_tok.length = slen; /* safe typecast */
816 status = process_gssapi_token(ctxt, &recv_tok);
818 free(recv_tok.value);
820 if (GSS_ERROR(status)) {
821 /* Start again with the next method in the list */
822 userauth(authctxt, NULL);
830 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
832 Authctxt *authctxt = ctxt;
834 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
835 gss_buffer_desc recv_tok;
839 if (authctxt == NULL)
840 fatal("input_gssapi_response: no authentication context");
841 gssctxt = authctxt->methoddata;
843 recv_tok.value = packet_get_string(&len);
844 recv_tok.length = len;
848 /* Stick it into GSSAPI and see what it says */
849 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
850 &recv_tok, &send_tok, NULL);
852 free(recv_tok.value);
853 gss_release_buffer(&ms, &send_tok);
855 /* Server will be returning a failed packet after this one */
861 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
866 /* maj */(void)packet_get_int();
867 /* min */(void)packet_get_int();
868 msg=packet_get_string(NULL);
869 lang=packet_get_string(NULL);
873 debug("Server GSSAPI Error:\n%s", msg);
881 userauth_none(Authctxt *authctxt)
883 /* initial userauth request */
884 packet_start(SSH2_MSG_USERAUTH_REQUEST);
885 packet_put_cstring(authctxt->server_user);
886 packet_put_cstring(authctxt->service);
887 packet_put_cstring(authctxt->method->name);
893 userauth_passwd(Authctxt *authctxt)
895 static int attempt = 0;
898 const char *host = options.host_key_alias ? options.host_key_alias :
901 if (attempt++ >= options.number_of_password_prompts)
905 error("Permission denied, please try again.");
907 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
908 authctxt->server_user, host);
909 password = read_passphrase(prompt, 0);
910 packet_start(SSH2_MSG_USERAUTH_REQUEST);
911 packet_put_cstring(authctxt->server_user);
912 packet_put_cstring(authctxt->service);
913 packet_put_cstring(authctxt->method->name);
915 packet_put_cstring(password);
916 explicit_bzero(password, strlen(password));
918 packet_add_padding(64);
921 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
922 &input_userauth_passwd_changereq);
928 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
932 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
934 Authctxt *authctxt = ctxt;
935 char *info, *lang, *password = NULL, *retype = NULL;
937 const char *host = options.host_key_alias ? options.host_key_alias :
940 debug2("input_userauth_passwd_changereq");
942 if (authctxt == NULL)
943 fatal("input_userauth_passwd_changereq: "
944 "no authentication context");
946 info = packet_get_string(NULL);
947 lang = packet_get_string(NULL);
948 if (strlen(info) > 0)
952 packet_start(SSH2_MSG_USERAUTH_REQUEST);
953 packet_put_cstring(authctxt->server_user);
954 packet_put_cstring(authctxt->service);
955 packet_put_cstring(authctxt->method->name);
956 packet_put_char(1); /* additional info */
957 snprintf(prompt, sizeof(prompt),
958 "Enter %.30s@%.128s's old password: ",
959 authctxt->server_user, host);
960 password = read_passphrase(prompt, 0);
961 packet_put_cstring(password);
962 explicit_bzero(password, strlen(password));
965 while (password == NULL) {
966 snprintf(prompt, sizeof(prompt),
967 "Enter %.30s@%.128s's new password: ",
968 authctxt->server_user, host);
969 password = read_passphrase(prompt, RP_ALLOW_EOF);
970 if (password == NULL) {
974 snprintf(prompt, sizeof(prompt),
975 "Retype %.30s@%.128s's new password: ",
976 authctxt->server_user, host);
977 retype = read_passphrase(prompt, 0);
978 if (strcmp(password, retype) != 0) {
979 explicit_bzero(password, strlen(password));
981 logit("Mismatch; try again, EOF to quit.");
984 explicit_bzero(retype, strlen(retype));
987 packet_put_cstring(password);
988 explicit_bzero(password, strlen(password));
990 packet_add_padding(64);
993 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
994 &input_userauth_passwd_changereq);
999 identity_sign_encode(struct identity *id)
1001 struct ssh *ssh = active_state;
1003 if (id->key->type == KEY_RSA) {
1004 switch (ssh->kex->rsa_sha2) {
1006 return "rsa-sha2-256";
1008 return "rsa-sha2-512";
1011 return key_ssh_name(id->key);
1015 identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
1016 const u_char *data, size_t datalen, u_int compat)
1022 alg = identity_sign_encode(id);
1024 /* the agent supports this key */
1025 if (id->agent_fd != -1)
1026 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
1027 data, datalen, alg, compat);
1030 * we have already loaded the private key or
1031 * the private key is stored in external hardware
1033 if (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))
1034 return (sshkey_sign(id->key, sigp, lenp, data, datalen, alg,
1036 /* load the private key from the file */
1037 if ((prv = load_identity_file(id)) == NULL)
1038 return SSH_ERR_KEY_NOT_FOUND;
1039 ret = sshkey_sign(prv, sigp, lenp, data, datalen, alg, compat);
1045 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1048 Identity *private_id;
1049 u_char *blob, *signature;
1051 u_int bloblen, skip = 0;
1052 int matched, ret = -1, have_sig = 1;
1055 if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1056 SSH_FP_DEFAULT)) == NULL)
1058 debug3("%s: %s %s", __func__, key_type(id->key), fp);
1061 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1062 /* we cannot handle this key */
1063 debug3("sign_and_send_pubkey: cannot handle key");
1066 /* data to be signed */
1068 if (datafellows & SSH_OLD_SESSIONID) {
1069 buffer_append(&b, session_id2, session_id2_len);
1070 skip = session_id2_len;
1072 buffer_put_string(&b, session_id2, session_id2_len);
1073 skip = buffer_len(&b);
1075 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1076 buffer_put_cstring(&b, authctxt->server_user);
1077 buffer_put_cstring(&b,
1078 datafellows & SSH_BUG_PKSERVICE ?
1081 if (datafellows & SSH_BUG_PKAUTH) {
1082 buffer_put_char(&b, have_sig);
1084 buffer_put_cstring(&b, authctxt->method->name);
1085 buffer_put_char(&b, have_sig);
1086 buffer_put_cstring(&b, identity_sign_encode(id));
1088 buffer_put_string(&b, blob, bloblen);
1091 * If the key is an certificate, try to find a matching private key
1092 * and use it to complete the signature.
1093 * If no such private key exists, fall back to trying the certificate
1094 * key itself in case it has a private half already loaded.
1096 if (key_is_cert(id->key)) {
1098 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1099 if (sshkey_equal_public(id->key, private_id->key) &&
1100 id->key->type != private_id->key->type) {
1107 debug2("%s: using private key \"%s\"%s for "
1108 "certificate", __func__, id->filename,
1109 id->agent_fd != -1 ? " from agent" : "");
1111 debug("%s: no separate private key for certificate "
1112 "\"%s\"", __func__, id->filename);
1116 /* generate signature */
1117 ret = identity_sign(id, &signature, &slen,
1118 buffer_ptr(&b), buffer_len(&b), datafellows);
1120 if (ret != SSH_ERR_KEY_NOT_FOUND)
1121 error("%s: signing failed: %s", __func__, ssh_err(ret));
1129 if (datafellows & SSH_BUG_PKSERVICE) {
1131 buffer_append(&b, session_id2, session_id2_len);
1132 skip = session_id2_len;
1133 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1134 buffer_put_cstring(&b, authctxt->server_user);
1135 buffer_put_cstring(&b, authctxt->service);
1136 buffer_put_cstring(&b, authctxt->method->name);
1137 buffer_put_char(&b, have_sig);
1138 if (!(datafellows & SSH_BUG_PKAUTH))
1139 buffer_put_cstring(&b, key_ssh_name(id->key));
1140 buffer_put_string(&b, blob, bloblen);
1144 /* append signature */
1145 buffer_put_string(&b, signature, slen);
1148 /* skip session id and packet type */
1149 if (buffer_len(&b) < skip + 1)
1150 fatal("userauth_pubkey: internal error");
1151 buffer_consume(&b, skip + 1);
1153 /* put remaining data from buffer into packet */
1154 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1155 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1163 send_pubkey_test(Authctxt *authctxt, Identity *id)
1166 u_int bloblen, have_sig = 0;
1168 debug3("send_pubkey_test");
1170 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1171 /* we cannot handle this key */
1172 debug3("send_pubkey_test: cannot handle key");
1175 /* register callback for USERAUTH_PK_OK message */
1176 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1178 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1179 packet_put_cstring(authctxt->server_user);
1180 packet_put_cstring(authctxt->service);
1181 packet_put_cstring(authctxt->method->name);
1182 packet_put_char(have_sig);
1183 if (!(datafellows & SSH_BUG_PKAUTH))
1184 packet_put_cstring(identity_sign_encode(id));
1185 packet_put_string(blob, bloblen);
1192 load_identity_file(Identity *id)
1194 Key *private = NULL;
1195 char prompt[300], *passphrase, *comment;
1196 int r, perm_ok = 0, quit = 0, i;
1199 if (stat(id->filename, &st) < 0) {
1200 (id->userprovided ? logit : debug3)("no such identity: %s: %s",
1201 id->filename, strerror(errno));
1204 snprintf(prompt, sizeof prompt,
1205 "Enter passphrase for key '%.100s': ", id->filename);
1206 for (i = 0; i <= options.number_of_password_prompts; i++) {
1210 passphrase = read_passphrase(prompt, 0);
1211 if (*passphrase == '\0') {
1212 debug2("no passphrase given, try next key");
1217 switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
1218 passphrase, &private, &comment, &perm_ok))) {
1221 case SSH_ERR_KEY_WRONG_PASSPHRASE:
1222 if (options.batch_mode) {
1227 debug2("bad passphrase given, try again...");
1229 case SSH_ERR_SYSTEM_ERROR:
1230 if (errno == ENOENT) {
1231 debug2("Load key \"%s\": %s",
1232 id->filename, ssh_err(r));
1238 error("Load key \"%s\": %s", id->filename, ssh_err(r));
1242 if (!quit && private != NULL && id->agent_fd == -1 &&
1243 !(id->key && id->isprivate))
1244 maybe_add_key_to_agent(id->filename, private, comment,
1247 explicit_bzero(passphrase, strlen(passphrase));
1251 if (private != NULL || quit)
1258 * try keys in the following order:
1259 * 1. certificates listed in the config file
1260 * 2. other input certificates
1261 * 3. agent keys that are found in the config file
1262 * 4. other agent keys
1263 * 5. keys that are only listed in the config file
1266 pubkey_prepare(Authctxt *authctxt)
1268 struct identity *id, *id2, *tmp;
1269 struct idlist agent, files, *preferred;
1271 int agent_fd = -1, i, r, found;
1273 struct ssh_identitylist *idlist;
1275 TAILQ_INIT(&agent); /* keys from the agent */
1276 TAILQ_INIT(&files); /* keys from the config file */
1277 preferred = &authctxt->keys;
1278 TAILQ_INIT(preferred); /* preferred order of keys */
1280 /* list of keys stored in the filesystem and PKCS#11 */
1281 for (i = 0; i < options.num_identity_files; i++) {
1282 key = options.identity_keys[i];
1283 if (key && key->type == KEY_RSA1)
1285 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1287 options.identity_keys[i] = NULL;
1288 id = xcalloc(1, sizeof(*id));
1291 id->filename = xstrdup(options.identity_files[i]);
1292 id->userprovided = options.identity_file_userprovided[i];
1293 TAILQ_INSERT_TAIL(&files, id, next);
1295 /* list of certificates specified by user */
1296 for (i = 0; i < options.num_certificate_files; i++) {
1297 key = options.certificates[i];
1298 if (!key_is_cert(key) || key->cert == NULL ||
1299 key->cert->type != SSH2_CERT_TYPE_USER)
1301 id = xcalloc(1, sizeof(*id));
1304 id->filename = xstrdup(options.certificate_files[i]);
1305 id->userprovided = options.certificate_file_userprovided[i];
1306 TAILQ_INSERT_TAIL(preferred, id, next);
1308 /* list of keys supported by the agent */
1309 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1310 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1311 debug("%s: ssh_get_authentication_socket: %s",
1312 __func__, ssh_err(r));
1313 } else if ((r = ssh_fetch_identitylist(agent_fd, 2, &idlist)) != 0) {
1314 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1315 debug("%s: ssh_fetch_identitylist: %s",
1316 __func__, ssh_err(r));
1319 for (j = 0; j < idlist->nkeys; j++) {
1321 TAILQ_FOREACH(id, &files, next) {
1323 * agent keys from the config file are
1326 if (sshkey_equal(idlist->keys[j], id->key)) {
1327 TAILQ_REMOVE(&files, id, next);
1328 TAILQ_INSERT_TAIL(preferred, id, next);
1329 id->agent_fd = agent_fd;
1334 if (!found && !options.identities_only) {
1335 id = xcalloc(1, sizeof(*id));
1336 /* XXX "steals" key/comment from idlist */
1337 id->key = idlist->keys[j];
1338 id->filename = idlist->comments[j];
1339 idlist->keys[j] = NULL;
1340 idlist->comments[j] = NULL;
1341 id->agent_fd = agent_fd;
1342 TAILQ_INSERT_TAIL(&agent, id, next);
1345 ssh_free_identitylist(idlist);
1346 /* append remaining agent keys */
1347 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1348 TAILQ_REMOVE(&agent, id, next);
1349 TAILQ_INSERT_TAIL(preferred, id, next);
1351 authctxt->agent_fd = agent_fd;
1353 /* Prefer PKCS11 keys that are explicitly listed */
1354 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1355 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
1358 TAILQ_FOREACH(id2, &files, next) {
1359 if (id2->key == NULL ||
1360 (id2->key->flags & SSHKEY_FLAG_EXT) == 0)
1362 if (sshkey_equal(id->key, id2->key)) {
1363 TAILQ_REMOVE(&files, id, next);
1364 TAILQ_INSERT_TAIL(preferred, id, next);
1369 /* If IdentitiesOnly set and key not found then don't use it */
1370 if (!found && options.identities_only) {
1371 TAILQ_REMOVE(&files, id, next);
1372 explicit_bzero(id, sizeof(*id));
1376 /* append remaining keys from the config file */
1377 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1378 TAILQ_REMOVE(&files, id, next);
1379 TAILQ_INSERT_TAIL(preferred, id, next);
1381 /* finally, filter by PubkeyAcceptedKeyTypes */
1382 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1383 if (id->key != NULL &&
1384 match_pattern_list(sshkey_ssh_name(id->key),
1385 options.pubkey_key_types, 0) != 1) {
1386 debug("Skipping %s key %s - "
1387 "not in PubkeyAcceptedKeyTypes",
1388 sshkey_ssh_name(id->key), id->filename);
1389 TAILQ_REMOVE(preferred, id, next);
1390 sshkey_free(id->key);
1392 memset(id, 0, sizeof(*id));
1395 debug2("key: %s (%p)%s%s", id->filename, id->key,
1396 id->userprovided ? ", explicit" : "",
1397 id->agent_fd != -1 ? ", agent" : "");
1402 pubkey_cleanup(Authctxt *authctxt)
1406 if (authctxt->agent_fd != -1)
1407 ssh_close_authentication_socket(authctxt->agent_fd);
1408 for (id = TAILQ_FIRST(&authctxt->keys); id;
1409 id = TAILQ_FIRST(&authctxt->keys)) {
1410 TAILQ_REMOVE(&authctxt->keys, id, next);
1411 sshkey_free(id->key);
1418 pubkey_reset(Authctxt *authctxt)
1422 TAILQ_FOREACH(id, &authctxt->keys, next)
1427 try_identity(Identity *id)
1431 if (key_type_plain(id->key->type) == KEY_RSA &&
1432 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1433 debug("Skipped %s key %s for RSA/MD5 server",
1434 key_type(id->key), id->filename);
1437 return (id->key->type != KEY_RSA1);
1441 userauth_pubkey(Authctxt *authctxt)
1446 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1449 /* move key to the end of the queue */
1450 TAILQ_REMOVE(&authctxt->keys, id, next);
1451 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1453 * send a test message if we have the public key. for
1454 * encrypted keys we cannot do this and have to load the
1455 * private key instead
1457 if (id->key != NULL) {
1458 if (try_identity(id)) {
1459 debug("Offering %s public key: %s",
1460 key_type(id->key), id->filename);
1461 sent = send_pubkey_test(authctxt, id);
1464 debug("Trying private key: %s", id->filename);
1465 id->key = load_identity_file(id);
1466 if (id->key != NULL) {
1467 if (try_identity(id)) {
1469 sent = sign_and_send_pubkey(
1484 * Send userauth request message specifying keyboard-interactive method.
1487 userauth_kbdint(Authctxt *authctxt)
1489 static int attempt = 0;
1491 if (attempt++ >= options.number_of_password_prompts)
1493 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1494 if (attempt > 1 && !authctxt->info_req_seen) {
1495 debug3("userauth_kbdint: disable: no info_req_seen");
1496 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1500 debug2("userauth_kbdint");
1501 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1502 packet_put_cstring(authctxt->server_user);
1503 packet_put_cstring(authctxt->service);
1504 packet_put_cstring(authctxt->method->name);
1505 packet_put_cstring(""); /* lang */
1506 packet_put_cstring(options.kbd_interactive_devices ?
1507 options.kbd_interactive_devices : "");
1510 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1515 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1518 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1520 Authctxt *authctxt = ctxt;
1521 char *name, *inst, *lang, *prompt, *response;
1522 u_int num_prompts, i;
1525 debug2("input_userauth_info_req");
1527 if (authctxt == NULL)
1528 fatal("input_userauth_info_req: no authentication context");
1530 authctxt->info_req_seen = 1;
1532 name = packet_get_string(NULL);
1533 inst = packet_get_string(NULL);
1534 lang = packet_get_string(NULL);
1535 if (strlen(name) > 0)
1537 if (strlen(inst) > 0)
1543 num_prompts = packet_get_int();
1545 * Begin to build info response packet based on prompts requested.
1546 * We commit to providing the correct number of responses, so if
1547 * further on we run into a problem that prevents this, we have to
1548 * be sure and clean this up and send a correct error response.
1550 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1551 packet_put_int(num_prompts);
1553 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1554 for (i = 0; i < num_prompts; i++) {
1555 prompt = packet_get_string(NULL);
1556 echo = packet_get_char();
1558 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1560 packet_put_cstring(response);
1561 explicit_bzero(response, strlen(response));
1565 packet_check_eom(); /* done with parsing incoming message. */
1567 packet_add_padding(64);
1573 ssh_keysign(struct sshkey *key, u_char **sigp, size_t *lenp,
1574 const u_char *data, size_t datalen)
1579 int i, r, to[2], from[2], status, sock = packet_get_connection_in();
1580 u_char rversion = 0, version = 2;
1581 void (*osigchld)(int);
1586 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1587 error("%s: not installed: %s", __func__, strerror(errno));
1590 if (fflush(stdout) != 0) {
1591 error("%s: fflush: %s", __func__, strerror(errno));
1595 error("%s: pipe: %s", __func__, strerror(errno));
1598 if (pipe(from) < 0) {
1599 error("%s: pipe: %s", __func__, strerror(errno));
1602 if ((pid = fork()) < 0) {
1603 error("%s: fork: %s", __func__, strerror(errno));
1606 osigchld = signal(SIGCHLD, SIG_DFL);
1608 /* keep the socket on exec */
1609 fcntl(sock, F_SETFD, 0);
1610 permanently_drop_suid(getuid());
1612 if (dup2(from[1], STDOUT_FILENO) < 0)
1613 fatal("%s: dup2: %s", __func__, strerror(errno));
1615 if (dup2(to[0], STDIN_FILENO) < 0)
1616 fatal("%s: dup2: %s", __func__, strerror(errno));
1619 /* Close everything but stdio and the socket */
1620 for (i = STDERR_FILENO + 1; i < sock; i++)
1622 closefrom(sock + 1);
1623 debug3("%s: [child] pid=%ld, exec %s",
1624 __func__, (long)getpid(), _PATH_SSH_KEY_SIGN);
1625 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
1626 fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN,
1632 if ((b = sshbuf_new()) == NULL)
1633 fatal("%s: sshbuf_new failed", __func__);
1634 /* send # of sock, data to be signed */
1635 if ((r = sshbuf_put_u32(b, sock) != 0) ||
1636 (r = sshbuf_put_string(b, data, datalen)) != 0)
1637 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1638 if (ssh_msg_send(to[1], version, b) == -1)
1639 fatal("%s: couldn't send request", __func__);
1641 r = ssh_msg_recv(from[0], b);
1645 error("%s: no reply", __func__);
1650 while (waitpid(pid, &status, 0) < 0) {
1651 if (errno != EINTR) {
1652 error("%s: waitpid %ld: %s",
1653 __func__, (long)pid, strerror(errno));
1657 if (!WIFEXITED(status)) {
1658 error("%s: exited abnormally", __func__);
1661 if (WEXITSTATUS(status) != 0) {
1662 error("%s: exited with status %d",
1663 __func__, WEXITSTATUS(status));
1666 if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
1667 error("%s: buffer error: %s", __func__, ssh_err(r));
1670 if (rversion != version) {
1671 error("%s: bad version", __func__);
1674 if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
1675 error("%s: buffer error: %s", __func__, ssh_err(r));
1677 signal(SIGCHLD, osigchld);
1681 signal(SIGCHLD, osigchld);
1688 userauth_hostbased(Authctxt *authctxt)
1690 struct ssh *ssh = active_state;
1691 struct sshkey *private = NULL;
1692 struct sshbuf *b = NULL;
1693 const char *service;
1694 u_char *sig = NULL, *keyblob = NULL;
1695 char *fp = NULL, *chost = NULL, *lname = NULL;
1696 size_t siglen = 0, keylen = 0;
1697 int i, r, success = 0;
1699 if (authctxt->ktypes == NULL) {
1700 authctxt->oktypes = xstrdup(options.hostbased_key_types);
1701 authctxt->ktypes = authctxt->oktypes;
1705 * Work through each listed type pattern in HostbasedKeyTypes,
1706 * trying each hostkey that matches the type in turn.
1709 if (authctxt->active_ktype == NULL)
1710 authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
1711 if (authctxt->active_ktype == NULL ||
1712 *authctxt->active_ktype == '\0')
1714 debug3("%s: trying key type %s", __func__,
1715 authctxt->active_ktype);
1717 /* check for a useful key */
1719 for (i = 0; i < authctxt->sensitive->nkeys; i++) {
1720 if (authctxt->sensitive->keys[i] == NULL ||
1721 authctxt->sensitive->keys[i]->type == KEY_RSA1 ||
1722 authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
1724 if (match_pattern_list(
1725 sshkey_ssh_name(authctxt->sensitive->keys[i]),
1726 authctxt->active_ktype, 0) != 1)
1728 /* we take and free the key */
1729 private = authctxt->sensitive->keys[i];
1730 authctxt->sensitive->keys[i] = NULL;
1734 if (private != NULL)
1736 /* No more keys of this type; advance */
1737 authctxt->active_ktype = NULL;
1739 if (private == NULL) {
1740 free(authctxt->oktypes);
1741 authctxt->oktypes = authctxt->ktypes = NULL;
1742 authctxt->active_ktype = NULL;
1743 debug("No more client hostkeys for hostbased authentication.");
1747 if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
1748 SSH_FP_DEFAULT)) == NULL) {
1749 error("%s: sshkey_fingerprint failed", __func__);
1752 debug("%s: trying hostkey %s %s",
1753 __func__, sshkey_ssh_name(private), fp);
1755 /* figure out a name for the client host */
1756 if ((lname = get_local_name(packet_get_connection_in())) == NULL) {
1757 error("%s: cannot get local ipaddr/name", __func__);
1761 /* XXX sshbuf_put_stringf? */
1762 xasprintf(&chost, "%s.", lname);
1763 debug2("%s: chost %s", __func__, chost);
1765 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1768 /* construct data */
1769 if ((b = sshbuf_new()) == NULL) {
1770 error("%s: sshbuf_new failed", __func__);
1773 if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
1774 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
1777 if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
1778 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1779 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1780 (r = sshbuf_put_cstring(b, service)) != 0 ||
1781 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1782 (r = sshbuf_put_cstring(b, key_ssh_name(private))) != 0 ||
1783 (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
1784 (r = sshbuf_put_cstring(b, chost)) != 0 ||
1785 (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
1786 error("%s: buffer error: %s", __func__, ssh_err(r));
1791 sshbuf_dump(b, stderr);
1793 if (authctxt->sensitive->external_keysign)
1794 r = ssh_keysign(private, &sig, &siglen,
1795 sshbuf_ptr(b), sshbuf_len(b));
1796 else if ((r = sshkey_sign(private, &sig, &siglen,
1797 sshbuf_ptr(b), sshbuf_len(b), NULL, datafellows)) != 0)
1798 debug("%s: sshkey_sign: %s", __func__, ssh_err(r));
1800 error("sign using hostkey %s %s failed",
1801 sshkey_ssh_name(private), fp);
1804 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1805 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1806 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1807 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1808 (r = sshpkt_put_cstring(ssh, key_ssh_name(private))) != 0 ||
1809 (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
1810 (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
1811 (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
1812 (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
1813 (r = sshpkt_send(ssh)) != 0) {
1814 error("%s: packet error: %s", __func__, ssh_err(r));
1821 explicit_bzero(sig, siglen);
1828 sshkey_free(private);
1834 /* find auth method */
1837 * given auth method name, if configurable options permit this method fill
1838 * in auth_ident field and return true, otherwise return false.
1841 authmethod_is_enabled(Authmethod *method)
1845 /* return false if options indicate this method is disabled */
1846 if (method->enabled == NULL || *method->enabled == 0)
1848 /* return false if batch mode is enabled but method needs interactive mode */
1849 if (method->batch_flag != NULL && *method->batch_flag != 0)
1855 authmethod_lookup(const char *name)
1857 Authmethod *method = NULL;
1859 for (method = authmethods; method->name != NULL; method++)
1860 if (strcmp(name, method->name) == 0)
1862 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1866 /* XXX internal state */
1867 static Authmethod *current = NULL;
1868 static char *supported = NULL;
1869 static char *preferred = NULL;
1872 * Given the authentication method list sent by the server, return the
1873 * next method we should try. If the server initially sends a nil list,
1874 * use a built-in default list.
1877 authmethod_get(char *authlist)
1882 /* Use a suitable default if we're passed a nil list. */
1883 if (authlist == NULL || strlen(authlist) == 0)
1884 authlist = options.preferred_authentications;
1886 if (supported == NULL || strcmp(authlist, supported) != 0) {
1887 debug3("start over, passed a different list %s", authlist);
1889 supported = xstrdup(authlist);
1890 preferred = options.preferred_authentications;
1891 debug3("preferred %s", preferred);
1893 } else if (current != NULL && authmethod_is_enabled(current))
1897 if ((name = match_list(preferred, supported, &next)) == NULL) {
1898 debug("No more authentication methods to try.");
1903 debug3("authmethod_lookup %s", name);
1904 debug3("remaining preferred: %s", preferred);
1905 if ((current = authmethod_lookup(name)) != NULL &&
1906 authmethod_is_enabled(current)) {
1907 debug3("authmethod_is_enabled %s", name);
1908 debug("Next authentication method: %s", name);
1917 authmethods_get(void)
1919 Authmethod *method = NULL;
1924 for (method = authmethods; method->name != NULL; method++) {
1925 if (authmethod_is_enabled(method)) {
1926 if (buffer_len(&b) > 0)
1927 buffer_append(&b, ",", 1);
1928 buffer_append(&b, method->name, strlen(method->name));
1931 if ((list = sshbuf_dup_string(&b)) == NULL)
1932 fatal("%s: sshbuf_dup_string failed", __func__);