1 /* $OpenBSD: sshconnect2.c,v 1.371 2023/12/18 14:45:49 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>
44 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
48 #include "openbsd-compat/sys-queue.h"
59 #include "sshconnect.h"
70 #include "pathnames.h"
83 extern char *client_version_string;
84 extern char *server_version_string;
85 extern Options options;
91 static char *xxx_host;
92 static struct sockaddr *xxx_hostaddr;
93 static const struct ssh_conn_info *xxx_conn_info;
96 verify_host_key_callback(struct sshkey *hostkey, struct ssh *ssh)
100 if ((r = sshkey_check_rsa_length(hostkey,
101 options.required_rsa_size)) != 0)
102 fatal_r(r, "Bad server host key");
103 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey,
104 xxx_conn_info) == -1)
105 fatal("Host key verification failed.");
109 /* Returns the first item from a comma-separated algorithm list */
111 first_alg(const char *algs)
116 if ((cp = strchr(ret, ',')) != NULL)
122 order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port,
123 const struct ssh_conn_info *cinfo)
125 char *oavail = NULL, *avail = NULL, *first = NULL, *last = NULL;
126 char *alg = NULL, *hostname = NULL, *ret = NULL, *best = NULL;
128 struct hostkeys *hostkeys = NULL;
132 /* Find all hostkeys for this hostname */
133 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
134 hostkeys = init_hostkeys();
135 for (i = 0; i < options.num_user_hostfiles; i++)
136 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i], 0);
137 for (i = 0; i < options.num_system_hostfiles; i++) {
138 load_hostkeys(hostkeys, hostname,
139 options.system_hostfiles[i], 0);
141 if (options.known_hosts_command != NULL) {
142 load_hostkeys_command(hostkeys, options.known_hosts_command,
143 "ORDER", cinfo, NULL, hostname);
146 * If a plain public key exists that matches the type of the best
147 * preference HostkeyAlgorithms, then use the whole list as is.
148 * Note that we ignore whether the best preference algorithm is a
149 * certificate type, as sshconnect.c will downgrade certs to
150 * plain keys if necessary.
152 best = first_alg(options.hostkeyalgorithms);
153 if (lookup_key_in_hostkeys_by_type(hostkeys,
154 sshkey_type_plain(sshkey_type_from_name(best)),
155 sshkey_ecdsa_nid_from_name(best), NULL)) {
156 debug3_f("have matching best-preference key type %s, "
157 "using HostkeyAlgorithms verbatim", best);
158 ret = xstrdup(options.hostkeyalgorithms);
163 * Otherwise, prefer the host key algorithms that match known keys
164 * while keeping the ordering of HostkeyAlgorithms as much as possible.
166 oavail = avail = xstrdup(options.hostkeyalgorithms);
167 maxlen = strlen(avail) + 1;
168 first = xmalloc(maxlen);
169 last = xmalloc(maxlen);
170 *first = *last = '\0';
172 #define ALG_APPEND(to, from) \
175 strlcat(to, ",", maxlen); \
176 strlcat(to, from, maxlen); \
179 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
180 if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
181 fatal_f("unknown alg %s", alg);
183 * If we have a @cert-authority marker in known_hosts then
184 * prefer all certificate algorithms.
186 if (sshkey_type_is_cert(ktype) &&
187 lookup_marker_in_hostkeys(hostkeys, MRK_CA)) {
188 ALG_APPEND(first, alg);
191 /* If the key appears in known_hosts then prefer it */
192 if (lookup_key_in_hostkeys_by_type(hostkeys,
193 sshkey_type_plain(ktype),
194 sshkey_ecdsa_nid_from_name(alg), NULL)) {
195 ALG_APPEND(first, alg);
198 /* Otherwise, put it last */
199 ALG_APPEND(last, alg);
202 xasprintf(&ret, "%s%s%s", first,
203 (*first == '\0' || *last == '\0') ? "" : ",", last);
205 debug3_f("prefer hostkeyalgs: %s", first);
207 debug3_f("no algorithms matched; accept original");
214 free_hostkeys(hostkeys);
220 ssh_kex2(struct ssh *ssh, char *host, struct sockaddr *hostaddr, u_short port,
221 const struct ssh_conn_info *cinfo)
223 char *myproposal[PROPOSAL_MAX];
224 char *s, *all_key, *hkalgs = NULL;
225 int r, use_known_hosts_order = 0;
228 xxx_hostaddr = hostaddr;
229 xxx_conn_info = cinfo;
231 if (options.rekey_limit || options.rekey_interval)
232 ssh_packet_set_rekey_limits(ssh, options.rekey_limit,
233 options.rekey_interval);
236 * If the user has not specified HostkeyAlgorithms, or has only
237 * appended or removed algorithms from that list then prefer algorithms
238 * that are in the list that are supported by known_hosts keys.
240 if (options.hostkeyalgorithms == NULL ||
241 options.hostkeyalgorithms[0] == '-' ||
242 options.hostkeyalgorithms[0] == '+')
243 use_known_hosts_order = 1;
245 /* Expand or fill in HostkeyAlgorithms */
246 all_key = sshkey_alg_list(0, 0, 1, ',');
247 if ((r = kex_assemble_names(&options.hostkeyalgorithms,
248 kex_default_pk_alg(), all_key)) != 0)
249 fatal_fr(r, "kex_assemble_namelist");
252 if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
253 fatal_f("kex_names_cat");
255 if (use_known_hosts_order)
256 hkalgs = order_hostkeyalgs(host, hostaddr, port, cinfo);
258 kex_proposal_populate_entries(ssh, myproposal, s, options.ciphers,
259 options.macs, compression_alg_list(options.compression),
260 hkalgs ? hkalgs : options.hostkeyalgorithms);
264 /* start key exchange */
265 if ((r = kex_setup(ssh, myproposal)) != 0)
266 fatal_r(r, "kex_setup");
268 ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client;
269 ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client;
270 ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client;
271 ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client;
272 ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client;
273 ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
274 ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
275 # ifdef OPENSSL_HAS_ECC
276 ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client;
279 ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client;
280 ssh->kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_client;
281 ssh->kex->verify_host_key=&verify_host_key_callback;
283 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &ssh->kex->done);
285 /* remove ext-info from the KEX proposals for rekeying */
286 free(myproposal[PROPOSAL_KEX_ALGS]);
287 myproposal[PROPOSAL_KEX_ALGS] =
288 compat_kex_proposal(ssh, options.kex_algorithms);
289 if ((r = kex_prop2buf(ssh->kex->my, myproposal)) != 0)
290 fatal_r(r, "kex_prop2buf");
293 /* send 1st encrypted/maced/compressed message */
294 if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 ||
295 (r = sshpkt_put_cstring(ssh, "markus")) != 0 ||
296 (r = sshpkt_send(ssh)) != 0 ||
297 (r = ssh_packet_write_wait(ssh)) != 0)
298 fatal_fr(r, "send packet");
300 kex_proposal_free_entries(myproposal);
307 typedef struct cauthctxt Authctxt;
308 typedef struct cauthmethod Authmethod;
309 typedef struct identity Identity;
310 typedef struct idlist Idlist;
313 TAILQ_ENTRY(identity) next;
314 int agent_fd; /* >=0 if agent supports key */
315 struct sshkey *key; /* public/private key */
316 char *filename; /* comment for agent-only keys */
318 int isprivate; /* key points to the private key */
321 TAILQ_HEAD(idlist, identity);
324 const char *server_user;
325 const char *local_user;
328 struct cauthmethod *method;
329 sig_atomic_t success;
333 gss_OID_set gss_supported_mechs;
340 Sensitive *sensitive;
341 char *oktypes, *ktypes;
342 const char *active_ktype;
343 /* kbd-interactive */
353 char *name; /* string to compare against server's list */
354 int (*userauth)(struct ssh *ssh);
355 void (*cleanup)(struct ssh *ssh);
356 int *enabled; /* flag in option struct that enables method */
357 int *batch_flag; /* flag in option struct that disables method */
360 static int input_userauth_service_accept(int, u_int32_t, struct ssh *);
361 static int input_userauth_success(int, u_int32_t, struct ssh *);
362 static int input_userauth_failure(int, u_int32_t, struct ssh *);
363 static int input_userauth_banner(int, u_int32_t, struct ssh *);
364 static int input_userauth_error(int, u_int32_t, struct ssh *);
365 static int input_userauth_info_req(int, u_int32_t, struct ssh *);
366 static int input_userauth_pk_ok(int, u_int32_t, struct ssh *);
367 static int input_userauth_passwd_changereq(int, u_int32_t, struct ssh *);
369 static int userauth_none(struct ssh *);
370 static int userauth_pubkey(struct ssh *);
371 static int userauth_passwd(struct ssh *);
372 static int userauth_kbdint(struct ssh *);
373 static int userauth_hostbased(struct ssh *);
376 static int userauth_gssapi(struct ssh *);
377 static void userauth_gssapi_cleanup(struct ssh *);
378 static int input_gssapi_response(int type, u_int32_t, struct ssh *);
379 static int input_gssapi_token(int type, u_int32_t, struct ssh *);
380 static int input_gssapi_error(int, u_int32_t, struct ssh *);
381 static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
384 void userauth(struct ssh *, char *);
386 static void pubkey_cleanup(struct ssh *);
387 static int sign_and_send_pubkey(struct ssh *ssh, Identity *);
388 static void pubkey_prepare(struct ssh *, Authctxt *);
389 static void pubkey_reset(Authctxt *);
390 static struct sshkey *load_identity_file(Identity *);
392 static Authmethod *authmethod_get(char *authlist);
393 static Authmethod *authmethod_lookup(const char *name);
394 static char *authmethods_get(void);
396 Authmethod authmethods[] = {
400 userauth_gssapi_cleanup,
401 &options.gss_authentication,
407 &options.hostbased_authentication,
412 &options.pubkey_authentication,
414 {"keyboard-interactive",
417 &options.kbd_interactive_authentication,
418 &options.batch_mode},
422 &options.password_authentication,
423 &options.batch_mode},
429 {NULL, NULL, NULL, NULL, NULL}
433 ssh_userauth2(struct ssh *ssh, const char *local_user,
434 const char *server_user, char *host, Sensitive *sensitive)
439 if (options.preferred_authentications == NULL)
440 options.preferred_authentications = authmethods_get();
442 /* setup authentication context */
443 memset(&authctxt, 0, sizeof(authctxt));
444 authctxt.server_user = server_user;
445 authctxt.local_user = local_user;
446 authctxt.host = host;
447 authctxt.service = "ssh-connection"; /* service name */
448 authctxt.success = 0;
449 authctxt.method = authmethod_lookup("none");
450 authctxt.authlist = NULL;
451 authctxt.methoddata = NULL;
452 authctxt.sensitive = sensitive;
453 authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
454 authctxt.info_req_seen = 0;
455 authctxt.attempt_kbdint = 0;
456 authctxt.attempt_passwd = 0;
458 authctxt.gss_supported_mechs = NULL;
459 authctxt.mech_tried = 0;
461 authctxt.agent_fd = -1;
462 if (authctxt.method == NULL)
463 fatal_f("internal error: cannot send userauth none request");
465 if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
466 (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
467 (r = sshpkt_send(ssh)) != 0)
468 fatal_fr(r, "send packet");
470 ssh->authctxt = &authctxt;
471 ssh_dispatch_init(ssh, &input_userauth_error);
472 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, kex_input_ext_info);
473 ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
474 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt.success); /* loop until success */
477 if (authctxt.gss_supported_mechs != NULL) {
480 gss_release_oid_set(&ms, &authctxt.gss_supported_mechs);
481 authctxt.gss_supported_mechs = NULL;
484 ssh->authctxt = NULL;
486 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
488 if (!authctxt.success)
489 fatal("Authentication failed.");
490 if (ssh_packet_connection_is_on_socket(ssh)) {
491 verbose("Authenticated to %s ([%s]:%d) using \"%s\".", host,
492 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
493 authctxt.method->name);
495 verbose("Authenticated to %s (via proxy) using \"%s\".", host,
496 authctxt.method->name);
501 input_userauth_service_accept(int type, u_int32_t seq, struct ssh *ssh)
505 if (ssh_packet_remaining(ssh) > 0) {
508 if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
510 debug2("service_accept: %s", reply);
513 debug2("buggy server: service_accept w/o service");
515 if ((r = sshpkt_get_end(ssh)) != 0)
517 debug("SSH2_MSG_SERVICE_ACCEPT received");
519 /* initial userauth request */
522 /* accept EXT_INFO at any time during userauth */
523 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, ssh->kex->ext_info_s ?
524 &kex_input_ext_info : &input_userauth_error);
525 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
526 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
527 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
534 userauth(struct ssh *ssh, char *authlist)
536 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
538 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
539 authctxt->method->cleanup(ssh);
541 free(authctxt->methoddata);
542 authctxt->methoddata = NULL;
543 if (authlist == NULL) {
544 authlist = authctxt->authlist;
546 free(authctxt->authlist);
547 authctxt->authlist = authlist;
550 Authmethod *method = authmethod_get(authlist);
552 fatal("%s@%s: Permission denied (%s).",
553 authctxt->server_user, authctxt->host, authlist);
554 authctxt->method = method;
556 /* reset the per method handler */
557 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_PER_METHOD_MIN,
558 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
560 /* and try new method */
561 if (method->userauth(ssh) != 0) {
562 debug2("we sent a %s packet, wait for reply", method->name);
565 debug2("we did not send a packet, disable method");
566 method->enabled = NULL;
572 input_userauth_error(int type, u_int32_t seq, struct ssh *ssh)
574 fatal_f("bad message during authentication: type %d", type);
579 input_userauth_banner(int type, u_int32_t seq, struct ssh *ssh)
585 debug3_f("entering");
586 if ((r = sshpkt_get_cstring(ssh, &msg, &len)) != 0 ||
587 (r = sshpkt_get_cstring(ssh, NULL, NULL)) != 0)
589 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO)
590 fmprintf(stderr, "%s", msg);
598 input_userauth_success(int type, u_int32_t seq, struct ssh *ssh)
600 Authctxt *authctxt = ssh->authctxt;
602 if (authctxt == NULL)
603 fatal_f("no authentication context");
604 free(authctxt->authlist);
605 authctxt->authlist = NULL;
606 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
607 authctxt->method->cleanup(ssh);
608 free(authctxt->methoddata);
609 authctxt->methoddata = NULL;
610 authctxt->success = 1; /* break out */
611 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, dispatch_protocol_error);
617 input_userauth_success_unexpected(int type, u_int32_t seq, struct ssh *ssh)
619 Authctxt *authctxt = ssh->authctxt;
621 if (authctxt == NULL)
622 fatal_f("no authentication context");
624 fatal("Unexpected authentication success during %s.",
625 authctxt->method->name);
631 input_userauth_failure(int type, u_int32_t seq, struct ssh *ssh)
633 Authctxt *authctxt = ssh->authctxt;
634 char *authlist = NULL;
637 if (authctxt == NULL)
638 fatal("input_userauth_failure: no authentication context");
640 if (sshpkt_get_cstring(ssh, &authlist, NULL) != 0 ||
641 sshpkt_get_u8(ssh, &partial) != 0 ||
642 sshpkt_get_end(ssh) != 0)
646 verbose("Authenticated using \"%s\" with partial success.",
647 authctxt->method->name);
649 pubkey_reset(authctxt);
651 debug("Authentications that can continue: %s", authlist);
653 userauth(ssh, authlist);
661 * Format an identity for logging including filename, key type, fingerprint
662 * and location (agent, etc.). Caller must free.
665 format_identity(Identity *id)
667 char *fp = NULL, *ret = NULL;
668 const char *note = "";
670 if (id->key != NULL) {
671 fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
675 if ((id->key->flags & SSHKEY_FLAG_EXT) != 0)
677 else if (sshkey_is_sk(id->key))
678 note = " authenticator";
680 xasprintf(&ret, "%s %s%s%s%s%s%s",
682 id->key ? sshkey_type(id->key) : "", id->key ? " " : "",
684 id->userprovided ? " explicit" : "", note,
685 id->agent_fd != -1 ? " agent" : "");
691 input_userauth_pk_ok(int type, u_int32_t seq, struct ssh *ssh)
693 Authctxt *authctxt = ssh->authctxt;
694 struct sshkey *key = NULL;
696 int pktype, found = 0, sent = 0;
698 char *pkalg = NULL, *fp = NULL, *ident = NULL;
699 u_char *pkblob = NULL;
702 if (authctxt == NULL)
703 fatal("input_userauth_pk_ok: no authentication context");
705 if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
706 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
707 (r = sshpkt_get_end(ssh)) != 0)
710 if ((pktype = sshkey_type_from_name(pkalg)) == KEY_UNSPEC) {
711 debug_f("server sent unknown pkalg %s", pkalg);
714 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
715 debug_r(r, "no key from blob. pkalg %s", pkalg);
718 if (key->type != pktype) {
719 error("input_userauth_pk_ok: type mismatch "
720 "for decoded key (received %d, expected %d)",
726 * search keys in the reverse order, because last candidate has been
727 * moved to the end of the queue. this also avoids confusion by
730 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
731 if (sshkey_equal(key, id->key)) {
736 if (!found || id == NULL) {
737 fp = sshkey_fingerprint(key, options.fingerprint_hash,
739 error_f("server replied with unknown key: %s %s",
740 sshkey_type(key), fp == NULL ? "<ERROR>" : fp);
743 ident = format_identity(id);
744 debug("Server accepts key: %s", ident);
745 sent = sign_and_send_pubkey(ssh, id);
754 /* try another method if we did not send a packet */
755 if (r == 0 && sent == 0)
762 userauth_gssapi(struct ssh *ssh)
764 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
765 Gssctxt *gssctxt = NULL;
770 /* Try one GSSAPI method at a time, rather than sending them all at
773 if (authctxt->gss_supported_mechs == NULL)
774 gss_indicate_mechs(&min, &authctxt->gss_supported_mechs);
776 /* Check to see whether the mechanism is usable before we offer it */
777 while (authctxt->mech_tried < authctxt->gss_supported_mechs->count &&
779 mech = &authctxt->gss_supported_mechs->
780 elements[authctxt->mech_tried];
781 /* My DER encoding requires length<128 */
782 if (mech->length < 128 && ssh_gssapi_check_mechanism(&gssctxt,
783 mech, authctxt->host)) {
784 ok = 1; /* Mechanism works */
786 authctxt->mech_tried++;
790 if (!ok || mech == NULL)
793 authctxt->methoddata=(void *)gssctxt;
795 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
796 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
797 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
798 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
799 (r = sshpkt_put_u32(ssh, 1)) != 0 ||
800 (r = sshpkt_put_u32(ssh, (mech->length) + 2)) != 0 ||
801 (r = sshpkt_put_u8(ssh, SSH_GSS_OIDTYPE)) != 0 ||
802 (r = sshpkt_put_u8(ssh, mech->length)) != 0 ||
803 (r = sshpkt_put(ssh, mech->elements, mech->length)) != 0 ||
804 (r = sshpkt_send(ssh)) != 0)
805 fatal_fr(r, "send packet");
807 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
808 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
809 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
810 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
812 authctxt->mech_tried++; /* Move along to next candidate */
818 userauth_gssapi_cleanup(struct ssh *ssh)
820 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
821 Gssctxt *gssctxt = (Gssctxt *)authctxt->methoddata;
823 ssh_gssapi_delete_ctx(&gssctxt);
824 authctxt->methoddata = NULL;
828 process_gssapi_token(struct ssh *ssh, gss_buffer_t recv_tok)
830 Authctxt *authctxt = ssh->authctxt;
831 Gssctxt *gssctxt = authctxt->methoddata;
832 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
833 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
834 gss_buffer_desc gssbuf;
835 OM_uint32 status, ms, flags;
838 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
839 recv_tok, &send_tok, &flags);
841 if (send_tok.length > 0) {
842 u_char type = GSS_ERROR(status) ?
843 SSH2_MSG_USERAUTH_GSSAPI_ERRTOK :
844 SSH2_MSG_USERAUTH_GSSAPI_TOKEN;
846 if ((r = sshpkt_start(ssh, type)) != 0 ||
847 (r = sshpkt_put_string(ssh, send_tok.value,
848 send_tok.length)) != 0 ||
849 (r = sshpkt_send(ssh)) != 0)
850 fatal_fr(r, "send %u packet", type);
852 gss_release_buffer(&ms, &send_tok);
855 if (status == GSS_S_COMPLETE) {
856 /* send either complete or MIC, depending on mechanism */
857 if (!(flags & GSS_C_INTEG_FLAG)) {
858 if ((r = sshpkt_start(ssh,
859 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE)) != 0 ||
860 (r = sshpkt_send(ssh)) != 0)
861 fatal_fr(r, "send completion");
865 if ((b = sshbuf_new()) == NULL)
866 fatal_f("sshbuf_new failed");
867 ssh_gssapi_buildmic(b, authctxt->server_user,
868 authctxt->service, "gssapi-with-mic",
869 ssh->kex->session_id);
871 if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
872 fatal_f("sshbuf_mutable_ptr failed");
873 gssbuf.length = sshbuf_len(b);
875 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
877 if (!GSS_ERROR(status)) {
878 if ((r = sshpkt_start(ssh,
879 SSH2_MSG_USERAUTH_GSSAPI_MIC)) != 0 ||
880 (r = sshpkt_put_string(ssh, mic.value,
882 (r = sshpkt_send(ssh)) != 0)
883 fatal_fr(r, "send MIC");
887 gss_release_buffer(&ms, &mic);
895 input_gssapi_response(int type, u_int32_t plen, struct ssh *ssh)
897 Authctxt *authctxt = ssh->authctxt;
903 if (authctxt == NULL)
904 fatal("input_gssapi_response: no authentication context");
905 gssctxt = authctxt->methoddata;
908 if ((r = sshpkt_get_string(ssh, &oidv, &oidlen)) != 0)
912 oidv[0] != SSH_GSS_OIDTYPE ||
913 oidv[1] != oidlen - 2) {
914 debug("Badly encoded mechanism OID received");
919 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
920 fatal("Server returned different OID than expected");
922 if ((r = sshpkt_get_end(ssh)) != 0)
925 if (GSS_ERROR(process_gssapi_token(ssh, GSS_C_NO_BUFFER))) {
926 /* Start again with next method on list */
927 debug("Trying to start again");
939 input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
941 Authctxt *authctxt = ssh->authctxt;
942 gss_buffer_desc recv_tok;
948 if (authctxt == NULL)
949 fatal("input_gssapi_response: no authentication context");
951 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
952 (r = sshpkt_get_end(ssh)) != 0)
956 recv_tok.length = len;
957 status = process_gssapi_token(ssh, &recv_tok);
959 /* Start again with the next method in the list */
960 if (GSS_ERROR(status)) {
971 input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
973 Authctxt *authctxt = ssh->authctxt;
975 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
976 gss_buffer_desc recv_tok;
982 if (authctxt == NULL)
983 fatal("input_gssapi_response: no authentication context");
984 gssctxt = authctxt->methoddata;
986 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
987 (r = sshpkt_get_end(ssh)) != 0) {
992 /* Stick it into GSSAPI and see what it says */
994 recv_tok.length = len;
995 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
996 &recv_tok, &send_tok, NULL);
998 gss_release_buffer(&ms, &send_tok);
1000 /* Server will be returning a failed packet after this one */
1005 input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh)
1011 if ((r = sshpkt_get_u32(ssh, NULL)) != 0 || /* maj */
1012 (r = sshpkt_get_u32(ssh, NULL)) != 0 || /* min */
1013 (r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
1014 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1016 r = sshpkt_get_end(ssh);
1017 debug("Server GSSAPI Error:\n%s", msg);
1026 userauth_none(struct ssh *ssh)
1028 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1031 /* initial userauth request */
1032 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1033 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1034 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1035 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1036 (r = sshpkt_send(ssh)) != 0)
1037 fatal_fr(r, "send packet");
1042 userauth_passwd(struct ssh *ssh)
1044 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1045 char *password, *prompt = NULL;
1046 const char *host = options.host_key_alias ? options.host_key_alias :
1050 if (authctxt->attempt_passwd++ >= options.number_of_password_prompts)
1053 if (authctxt->attempt_passwd != 1)
1054 error("Permission denied, please try again.");
1056 xasprintf(&prompt, "%s@%s's password: ", authctxt->server_user, host);
1057 password = read_passphrase(prompt, 0);
1058 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1059 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1060 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1061 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1062 (r = sshpkt_put_u8(ssh, 0)) != 0 ||
1063 (r = sshpkt_put_cstring(ssh, password)) != 0 ||
1064 (r = sshpkt_add_padding(ssh, 64)) != 0 ||
1065 (r = sshpkt_send(ssh)) != 0)
1066 fatal_fr(r, "send packet");
1069 if (password != NULL)
1070 freezero(password, strlen(password));
1072 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1073 &input_userauth_passwd_changereq);
1079 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
1082 input_userauth_passwd_changereq(int type, u_int32_t seqnr, struct ssh *ssh)
1084 Authctxt *authctxt = ssh->authctxt;
1085 char *info = NULL, *lang = NULL, *password = NULL, *retype = NULL;
1090 debug2("input_userauth_passwd_changereq");
1092 if (authctxt == NULL)
1093 fatal("input_userauth_passwd_changereq: "
1094 "no authentication context");
1095 host = options.host_key_alias ? options.host_key_alias : authctxt->host;
1097 if ((r = sshpkt_get_cstring(ssh, &info, NULL)) != 0 ||
1098 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1100 if (strlen(info) > 0)
1102 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1103 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1104 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1105 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1106 (r = sshpkt_put_u8(ssh, 1)) != 0) /* additional info */
1109 snprintf(prompt, sizeof(prompt),
1110 "Enter %.30s@%.128s's old password: ",
1111 authctxt->server_user, host);
1112 password = read_passphrase(prompt, 0);
1113 if ((r = sshpkt_put_cstring(ssh, password)) != 0)
1116 freezero(password, strlen(password));
1118 while (password == NULL) {
1119 snprintf(prompt, sizeof(prompt),
1120 "Enter %.30s@%.128s's new password: ",
1121 authctxt->server_user, host);
1122 password = read_passphrase(prompt, RP_ALLOW_EOF);
1123 if (password == NULL) {
1128 snprintf(prompt, sizeof(prompt),
1129 "Retype %.30s@%.128s's new password: ",
1130 authctxt->server_user, host);
1131 retype = read_passphrase(prompt, 0);
1132 if (strcmp(password, retype) != 0) {
1133 freezero(password, strlen(password));
1134 logit("Mismatch; try again, EOF to quit.");
1137 freezero(retype, strlen(retype));
1139 if ((r = sshpkt_put_cstring(ssh, password)) != 0 ||
1140 (r = sshpkt_add_padding(ssh, 64)) != 0 ||
1141 (r = sshpkt_send(ssh)) != 0)
1144 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1145 &input_userauth_passwd_changereq);
1149 freezero(password, strlen(password));
1156 * Select an algorithm for publickey signatures.
1157 * Returns algorithm (caller must free) or NULL if no mutual algorithm found.
1159 * Call with ssh==NULL to ignore server-sig-algs extension list and
1160 * only attempt with the key's base signature type.
1163 key_sig_algorithm(struct ssh *ssh, const struct sshkey *key)
1165 char *allowed, *oallowed, *cp, *tmp, *alg = NULL;
1166 const char *server_sig_algs;
1169 * The signature algorithm will only differ from the key algorithm
1170 * for RSA keys/certs and when the server advertises support for
1171 * newer (SHA2) algorithms.
1173 if (ssh == NULL || ssh->kex->server_sig_algs == NULL ||
1174 (key->type != KEY_RSA && key->type != KEY_RSA_CERT) ||
1175 (key->type == KEY_RSA_CERT && (ssh->compat & SSH_BUG_SIGTYPE))) {
1176 /* Filter base key signature alg against our configuration */
1177 return match_list(sshkey_ssh_name(key),
1178 options.pubkey_accepted_algos, NULL);
1182 * Workaround OpenSSH 7.4 bug: this version supports RSA/SHA-2 but
1183 * fails to advertise it via SSH2_MSG_EXT_INFO.
1185 server_sig_algs = ssh->kex->server_sig_algs;
1186 if (key->type == KEY_RSA && (ssh->compat & SSH_BUG_SIGTYPE74))
1187 server_sig_algs = "rsa-sha2-256,rsa-sha2-512";
1190 * For RSA keys/certs, since these might have a different sig type:
1191 * find the first entry in PubkeyAcceptedAlgorithms of the right type
1192 * that also appears in the supported signature algorithms list from
1195 oallowed = allowed = xstrdup(options.pubkey_accepted_algos);
1196 while ((cp = strsep(&allowed, ",")) != NULL) {
1197 if (sshkey_type_from_name(cp) != key->type)
1199 tmp = match_list(sshkey_sigalg_by_name(cp),
1200 server_sig_algs, NULL);
1212 identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
1213 const u_char *data, size_t datalen, u_int compat, const char *alg)
1215 struct sshkey *sign_key = NULL, *prv = NULL;
1216 int is_agent = 0, retried = 0, r = SSH_ERR_INTERNAL_ERROR;
1217 struct notifier_ctx *notifier = NULL;
1218 char *fp = NULL, *pin = NULL, *prompt = NULL;
1223 /* The agent supports this key. */
1224 if (id->key != NULL && id->agent_fd != -1) {
1225 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
1226 data, datalen, alg, compat);
1230 * We have already loaded the private key or the private key is
1231 * stored in external hardware.
1233 if (id->key != NULL &&
1234 (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))) {
1238 /* Load the private key from the file. */
1239 if ((prv = load_identity_file(id)) == NULL)
1240 return SSH_ERR_KEY_NOT_FOUND;
1241 if (id->key != NULL && !sshkey_equal_public(prv, id->key)) {
1242 error_f("private key %s contents do not match public",
1244 r = SSH_ERR_KEY_NOT_FOUND;
1250 /* Prompt for touch for non-agent FIDO keys that request UP */
1251 if (!is_agent && sshkey_is_sk(sign_key) &&
1252 (sign_key->sk_flags & SSH_SK_USER_PRESENCE_REQD)) {
1253 /* XXX should batch mode just skip these? */
1254 if ((fp = sshkey_fingerprint(sign_key,
1255 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1256 fatal_f("fingerprint failed");
1257 notifier = notify_start(options.batch_mode,
1258 "Confirm user presence for key %s %s",
1259 sshkey_type(sign_key), fp);
1262 if ((r = sshkey_sign(sign_key, sigp, lenp, data, datalen,
1263 alg, options.sk_provider, pin, compat)) != 0) {
1264 debug_fr(r, "sshkey_sign");
1265 if (!retried && pin == NULL && !is_agent &&
1266 sshkey_is_sk(sign_key) &&
1267 r == SSH_ERR_KEY_WRONG_PASSPHRASE) {
1268 notify_complete(notifier, NULL);
1270 xasprintf(&prompt, "Enter PIN for %s key %s: ",
1271 sshkey_type(sign_key), id->filename);
1272 pin = read_passphrase(prompt, 0);
1280 * PKCS#11 tokens may not support all signature algorithms,
1281 * so check what we get back.
1283 if ((r = sshkey_check_sigtype(*sigp, *lenp, alg)) != 0) {
1284 debug_fr(r, "sshkey_check_sigtype");
1292 freezero(pin, strlen(pin));
1293 notify_complete(notifier, r == 0 ? "User presence confirmed" : NULL);
1299 id_filename_matches(Identity *id, Identity *private_id)
1301 static const char * const suffixes[] = { ".pub", "-cert.pub", NULL };
1302 size_t len = strlen(id->filename), plen = strlen(private_id->filename);
1305 if (strcmp(id->filename, private_id->filename) == 0)
1307 for (i = 0; suffixes[i]; i++) {
1308 slen = strlen(suffixes[i]);
1309 if (len > slen && plen == len - slen &&
1310 strcmp(id->filename + (len - slen), suffixes[i]) == 0 &&
1311 memcmp(id->filename, private_id->filename, plen) == 0)
1318 sign_and_send_pubkey(struct ssh *ssh, Identity *id)
1320 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1321 struct sshbuf *b = NULL;
1322 Identity *private_id, *sign_id = NULL;
1323 u_char *signature = NULL;
1324 size_t slen = 0, skip = 0;
1325 int r, fallback_sigtype, sent = 0;
1326 char *alg = NULL, *fp = NULL;
1327 const char *loc = "", *method = "publickey";
1330 /* prefer host-bound pubkey signatures if supported by server */
1331 if ((ssh->kex->flags & KEX_HAS_PUBKEY_HOSTBOUND) != 0 &&
1332 (options.pubkey_authentication & SSH_PUBKEY_AUTH_HBOUND) != 0) {
1334 method = "publickey-hostbound-v00@openssh.com";
1337 if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1338 SSH_FP_DEFAULT)) == NULL)
1341 debug3_f("using %s with %s %s", method, sshkey_type(id->key), fp);
1344 * If the key is an certificate, try to find a matching private key
1345 * and use it to complete the signature.
1346 * If no such private key exists, fall back to trying the certificate
1347 * key itself in case it has a private half already loaded.
1348 * This will try to set sign_id to the private key that will perform
1351 if (sshkey_is_cert(id->key)) {
1352 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1353 if (sshkey_equal_public(id->key, private_id->key) &&
1354 id->key->type != private_id->key->type) {
1355 sign_id = private_id;
1360 * Exact key matches are preferred, but also allow
1361 * filename matches for non-PKCS#11/agent keys that
1362 * didn't load public keys. This supports the case
1363 * of keeping just a private key file and public
1364 * certificate on disk.
1366 if (sign_id == NULL &&
1367 !id->isprivate && id->agent_fd == -1 &&
1368 (id->key->flags & SSHKEY_FLAG_EXT) == 0) {
1369 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1370 if (private_id->key == NULL &&
1371 id_filename_matches(id, private_id)) {
1372 sign_id = private_id;
1377 if (sign_id != NULL) {
1378 debug2_f("using private key \"%s\"%s for "
1379 "certificate", sign_id->filename,
1380 sign_id->agent_fd != -1 ? " from agent" : "");
1382 debug_f("no separate private key for certificate "
1383 "\"%s\"", id->filename);
1388 * If the above didn't select another identity to do the signing
1389 * then default to the one we started with.
1391 if (sign_id == NULL)
1394 /* assemble and sign data */
1395 for (fallback_sigtype = 0; fallback_sigtype <= 1; fallback_sigtype++) {
1399 if ((alg = key_sig_algorithm(fallback_sigtype ? NULL : ssh,
1400 id->key)) == NULL) {
1401 error_f("no mutual signature supported");
1404 debug3_f("signing using %s %s", alg, fp);
1407 if ((b = sshbuf_new()) == NULL)
1408 fatal_f("sshbuf_new failed");
1409 if (ssh->compat & SSH_OLD_SESSIONID) {
1410 if ((r = sshbuf_putb(b, ssh->kex->session_id)) != 0)
1411 fatal_fr(r, "sshbuf_putb");
1413 if ((r = sshbuf_put_stringb(b,
1414 ssh->kex->session_id)) != 0)
1415 fatal_fr(r, "sshbuf_put_stringb");
1417 skip = sshbuf_len(b);
1418 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1419 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1420 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1421 (r = sshbuf_put_cstring(b, method)) != 0 ||
1422 (r = sshbuf_put_u8(b, 1)) != 0 ||
1423 (r = sshbuf_put_cstring(b, alg)) != 0 ||
1424 (r = sshkey_puts(id->key, b)) != 0) {
1425 fatal_fr(r, "assemble signed data");
1428 if (ssh->kex->initial_hostkey == NULL) {
1429 fatal_f("internal error: initial hostkey "
1432 if ((r = sshkey_puts(ssh->kex->initial_hostkey, b)) != 0)
1433 fatal_fr(r, "assemble %s hostkey", method);
1435 /* generate signature */
1436 r = identity_sign(sign_id, &signature, &slen,
1437 sshbuf_ptr(b), sshbuf_len(b), ssh->compat, alg);
1440 else if (r == SSH_ERR_KEY_NOT_FOUND)
1441 goto out; /* soft failure */
1442 else if (r == SSH_ERR_SIGN_ALG_UNSUPPORTED &&
1443 !fallback_sigtype) {
1444 if (sign_id->agent_fd != -1)
1446 else if ((sign_id->key->flags & SSHKEY_FLAG_EXT) != 0)
1448 logit("%skey %s %s returned incorrect signature type",
1449 loc, sshkey_type(id->key), fp);
1452 error_fr(r, "signing failed for %s \"%s\"%s",
1453 sshkey_type(sign_id->key), sign_id->filename,
1454 id->agent_fd != -1 ? " from agent" : "");
1457 if (slen == 0 || signature == NULL) /* shouldn't happen */
1458 fatal_f("no signature");
1460 /* append signature */
1461 if ((r = sshbuf_put_string(b, signature, slen)) != 0)
1462 fatal_fr(r, "append signature");
1465 sshbuf_dump(b, stderr);
1467 /* skip session id and packet type */
1468 if ((r = sshbuf_consume(b, skip + 1)) != 0)
1469 fatal_fr(r, "consume");
1471 /* put remaining data from buffer into packet */
1472 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1473 (r = sshpkt_putb(ssh, b)) != 0 ||
1474 (r = sshpkt_send(ssh)) != 0)
1475 fatal_fr(r, "enqueue request");
1484 freezero(signature, slen);
1489 send_pubkey_test(struct ssh *ssh, Identity *id)
1491 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1492 u_char *blob = NULL;
1498 if ((alg = key_sig_algorithm(ssh, id->key)) == NULL) {
1499 debug_f("no mutual signature algorithm");
1503 if ((r = sshkey_to_blob(id->key, &blob, &bloblen)) != 0) {
1504 /* we cannot handle this key */
1505 debug3_f("cannot handle key");
1508 /* register callback for USERAUTH_PK_OK message */
1509 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1511 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1512 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1513 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1514 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1515 (r = sshpkt_put_u8(ssh, have_sig)) != 0 ||
1516 (r = sshpkt_put_cstring(ssh, alg)) != 0 ||
1517 (r = sshpkt_put_string(ssh, blob, bloblen)) != 0 ||
1518 (r = sshpkt_send(ssh)) != 0)
1519 fatal_fr(r, "send packet");
1528 static struct sshkey *
1529 load_identity_file(Identity *id)
1531 struct sshkey *private = NULL;
1532 char prompt[300], *passphrase, *comment;
1536 if (stat(id->filename, &st) == -1) {
1537 do_log2(id->userprovided ?
1538 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_DEBUG3,
1539 "no such identity: %s: %s", id->filename, strerror(errno));
1542 snprintf(prompt, sizeof prompt,
1543 "Enter passphrase for key '%.100s': ", id->filename);
1544 for (i = 0; i <= options.number_of_password_prompts; i++) {
1548 passphrase = read_passphrase(prompt, 0);
1549 if (*passphrase == '\0') {
1550 debug2("no passphrase given, try next key");
1555 switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
1556 passphrase, &private, &comment))) {
1559 case SSH_ERR_KEY_WRONG_PASSPHRASE:
1560 if (options.batch_mode) {
1565 debug2("bad passphrase given, try again...");
1567 case SSH_ERR_SYSTEM_ERROR:
1568 if (errno == ENOENT) {
1569 debug2_r(r, "Load key \"%s\"", id->filename);
1575 error_r(r, "Load key \"%s\"", id->filename);
1579 if (private != NULL && sshkey_is_sk(private) &&
1580 options.sk_provider == NULL) {
1581 debug("key \"%s\" is an authenticator-hosted key, "
1582 "but no provider specified", id->filename);
1583 sshkey_free(private);
1587 if (!quit && (r = sshkey_check_rsa_length(private,
1588 options.required_rsa_size)) != 0) {
1589 debug_fr(r, "Skipping key %s", id->filename);
1590 sshkey_free(private);
1594 if (!quit && private != NULL && id->agent_fd == -1 &&
1595 !(id->key && id->isprivate))
1596 maybe_add_key_to_agent(id->filename, private, comment,
1599 freezero(passphrase, strlen(passphrase));
1601 if (private != NULL || quit)
1608 key_type_allowed_by_config(struct sshkey *key)
1610 if (match_pattern_list(sshkey_ssh_name(key),
1611 options.pubkey_accepted_algos, 0) == 1)
1614 /* RSA keys/certs might be allowed by alternate signature types */
1615 switch (key->type) {
1617 if (match_pattern_list("rsa-sha2-512",
1618 options.pubkey_accepted_algos, 0) == 1)
1620 if (match_pattern_list("rsa-sha2-256",
1621 options.pubkey_accepted_algos, 0) == 1)
1625 if (match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
1626 options.pubkey_accepted_algos, 0) == 1)
1628 if (match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
1629 options.pubkey_accepted_algos, 0) == 1)
1636 /* obtain a list of keys from the agent */
1638 get_agent_identities(struct ssh *ssh, int *agent_fdp,
1639 struct ssh_identitylist **idlistp)
1642 struct ssh_identitylist *idlist;
1644 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1645 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1646 debug_fr(r, "ssh_get_authentication_socket");
1649 if ((r = ssh_agent_bind_hostkey(agent_fd, ssh->kex->initial_hostkey,
1650 ssh->kex->session_id, ssh->kex->initial_sig, 0)) == 0)
1651 debug_f("bound agent to hostkey");
1653 debug2_fr(r, "ssh_agent_bind_hostkey");
1655 if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
1656 debug_fr(r, "ssh_fetch_identitylist");
1661 *agent_fdp = agent_fd;
1663 debug_f("agent returned %zu keys", idlist->nkeys);
1668 * try keys in the following order:
1669 * 1. certificates listed in the config file
1670 * 2. other input certificates
1671 * 3. agent keys that are found in the config file
1672 * 4. other agent keys
1673 * 5. keys that are only listed in the config file
1676 pubkey_prepare(struct ssh *ssh, Authctxt *authctxt)
1678 struct identity *id, *id2, *tmp;
1679 struct idlist agent, files, *preferred;
1681 int disallowed, agent_fd = -1, i, r, found;
1683 struct ssh_identitylist *idlist;
1686 TAILQ_INIT(&agent); /* keys from the agent */
1687 TAILQ_INIT(&files); /* keys from the config file */
1688 preferred = &authctxt->keys;
1689 TAILQ_INIT(preferred); /* preferred order of keys */
1691 /* list of keys stored in the filesystem and PKCS#11 */
1692 for (i = 0; i < options.num_identity_files; i++) {
1693 key = options.identity_keys[i];
1694 if (key && key->cert &&
1695 key->cert->type != SSH2_CERT_TYPE_USER) {
1696 debug_f("ignoring certificate %s: not a user "
1697 "certificate", options.identity_files[i]);
1700 if (key && sshkey_is_sk(key) && options.sk_provider == NULL) {
1701 debug_f("ignoring authenticator-hosted key %s as no "
1702 "SecurityKeyProvider has been specified",
1703 options.identity_files[i]);
1706 options.identity_keys[i] = NULL;
1707 id = xcalloc(1, sizeof(*id));
1710 id->filename = xstrdup(options.identity_files[i]);
1711 id->userprovided = options.identity_file_userprovided[i];
1712 TAILQ_INSERT_TAIL(&files, id, next);
1714 /* list of certificates specified by user */
1715 for (i = 0; i < options.num_certificate_files; i++) {
1716 key = options.certificates[i];
1717 if (!sshkey_is_cert(key) || key->cert == NULL ||
1718 key->cert->type != SSH2_CERT_TYPE_USER) {
1719 debug_f("ignoring certificate %s: not a user "
1720 "certificate", options.identity_files[i]);
1723 if (key && sshkey_is_sk(key) && options.sk_provider == NULL) {
1724 debug_f("ignoring authenticator-hosted key "
1725 "certificate %s as no "
1726 "SecurityKeyProvider has been specified",
1727 options.identity_files[i]);
1730 id = xcalloc(1, sizeof(*id));
1733 id->filename = xstrdup(options.certificate_files[i]);
1734 id->userprovided = options.certificate_file_userprovided[i];
1735 TAILQ_INSERT_TAIL(preferred, id, next);
1737 /* list of keys supported by the agent */
1738 if ((r = get_agent_identities(ssh, &agent_fd, &idlist)) == 0) {
1739 for (j = 0; j < idlist->nkeys; j++) {
1740 if ((r = sshkey_check_rsa_length(idlist->keys[j],
1741 options.required_rsa_size)) != 0) {
1742 debug_fr(r, "ignoring %s agent key",
1743 sshkey_ssh_name(idlist->keys[j]));
1747 TAILQ_FOREACH(id, &files, next) {
1749 * agent keys from the config file are
1752 if (sshkey_equal(idlist->keys[j], id->key)) {
1753 TAILQ_REMOVE(&files, id, next);
1754 TAILQ_INSERT_TAIL(preferred, id, next);
1755 id->agent_fd = agent_fd;
1760 if (!found && !options.identities_only) {
1761 id = xcalloc(1, sizeof(*id));
1762 /* XXX "steals" key/comment from idlist */
1763 id->key = idlist->keys[j];
1764 id->filename = idlist->comments[j];
1765 idlist->keys[j] = NULL;
1766 idlist->comments[j] = NULL;
1767 id->agent_fd = agent_fd;
1768 TAILQ_INSERT_TAIL(&agent, id, next);
1771 ssh_free_identitylist(idlist);
1772 /* append remaining agent keys */
1773 TAILQ_CONCAT(preferred, &agent, next);
1774 authctxt->agent_fd = agent_fd;
1776 /* Prefer PKCS11 keys that are explicitly listed */
1777 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1778 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
1781 TAILQ_FOREACH(id2, &files, next) {
1782 if (id2->key == NULL ||
1783 (id2->key->flags & SSHKEY_FLAG_EXT) != 0)
1785 if (sshkey_equal(id->key, id2->key)) {
1786 TAILQ_REMOVE(&files, id, next);
1787 TAILQ_INSERT_TAIL(preferred, id, next);
1792 /* If IdentitiesOnly set and key not found then don't use it */
1793 if (!found && options.identities_only) {
1794 TAILQ_REMOVE(&files, id, next);
1795 freezero(id, sizeof(*id));
1798 /* append remaining keys from the config file */
1799 TAILQ_CONCAT(preferred, &files, next);
1800 /* finally, filter by PubkeyAcceptedAlgorithms */
1801 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1804 if (id->key == NULL)
1806 if (!key_type_allowed_by_config(id->key)) {
1807 debug("Skipping %s key %s - corresponding algorithm "
1808 "not in PubkeyAcceptedAlgorithms",
1809 sshkey_ssh_name(id->key), id->filename);
1811 } else if (ssh->kex->server_sig_algs != NULL &&
1812 (cp = key_sig_algorithm(ssh, id->key)) == NULL) {
1813 debug("Skipping %s key %s - corresponding algorithm "
1814 "not supported by server",
1815 sshkey_ssh_name(id->key), id->filename);
1822 TAILQ_REMOVE(preferred, id, next);
1823 sshkey_free(id->key);
1825 memset(id, 0, sizeof(*id));
1827 /* List the keys we plan on using */
1828 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1829 ident = format_identity(id);
1830 debug("Will attempt key: %s", ident);
1837 pubkey_cleanup(struct ssh *ssh)
1839 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1842 if (authctxt->agent_fd != -1) {
1843 ssh_close_authentication_socket(authctxt->agent_fd);
1844 authctxt->agent_fd = -1;
1846 for (id = TAILQ_FIRST(&authctxt->keys); id;
1847 id = TAILQ_FIRST(&authctxt->keys)) {
1848 TAILQ_REMOVE(&authctxt->keys, id, next);
1849 sshkey_free(id->key);
1856 pubkey_reset(Authctxt *authctxt)
1860 TAILQ_FOREACH(id, &authctxt->keys, next)
1865 userauth_pubkey(struct ssh *ssh)
1867 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1871 static int prepared;
1874 pubkey_prepare(ssh, authctxt);
1878 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1881 /* move key to the end of the queue */
1882 TAILQ_REMOVE(&authctxt->keys, id, next);
1883 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1885 * send a test message if we have the public key. for
1886 * encrypted keys we cannot do this and have to load the
1887 * private key instead
1889 if (id->key != NULL) {
1890 ident = format_identity(id);
1891 debug("Offering public key: %s", ident);
1893 sent = send_pubkey_test(ssh, id);
1895 debug("Trying private key: %s", id->filename);
1896 id->key = load_identity_file(id);
1897 if (id->key != NULL) {
1898 if (id->key != NULL) {
1900 sent = sign_and_send_pubkey(ssh, id);
1902 sshkey_free(id->key);
1914 * Send userauth request message specifying keyboard-interactive method.
1917 userauth_kbdint(struct ssh *ssh)
1919 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1922 if (authctxt->attempt_kbdint++ >= options.number_of_password_prompts)
1924 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1925 if (authctxt->attempt_kbdint > 1 && !authctxt->info_req_seen) {
1926 debug3("userauth_kbdint: disable: no info_req_seen");
1927 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1931 debug2("userauth_kbdint");
1932 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1933 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1934 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1935 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1936 (r = sshpkt_put_cstring(ssh, "")) != 0 || /* lang */
1937 (r = sshpkt_put_cstring(ssh, options.kbd_interactive_devices ?
1938 options.kbd_interactive_devices : "")) != 0 ||
1939 (r = sshpkt_send(ssh)) != 0)
1940 fatal_fr(r, "send packet");
1942 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1947 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1950 input_userauth_info_req(int type, u_int32_t seq, struct ssh *ssh)
1952 Authctxt *authctxt = ssh->authctxt;
1953 char *name = NULL, *inst = NULL, *lang = NULL, *prompt = NULL;
1954 char *display_prompt = NULL, *response = NULL;
1956 u_int num_prompts, i;
1959 debug2_f("entering");
1961 if (authctxt == NULL)
1962 fatal_f("no authentication context");
1964 authctxt->info_req_seen = 1;
1966 if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 ||
1967 (r = sshpkt_get_cstring(ssh, &inst, NULL)) != 0 ||
1968 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1970 if (strlen(name) > 0)
1972 if (strlen(inst) > 0)
1975 if ((r = sshpkt_get_u32(ssh, &num_prompts)) != 0)
1978 * Begin to build info response packet based on prompts requested.
1979 * We commit to providing the correct number of responses, so if
1980 * further on we run into a problem that prevents this, we have to
1981 * be sure and clean this up and send a correct error response.
1983 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE)) != 0 ||
1984 (r = sshpkt_put_u32(ssh, num_prompts)) != 0)
1987 debug2_f("num_prompts %d", num_prompts);
1988 for (i = 0; i < num_prompts; i++) {
1989 if ((r = sshpkt_get_cstring(ssh, &prompt, NULL)) != 0 ||
1990 (r = sshpkt_get_u8(ssh, &echo)) != 0)
1992 if (asmprintf(&display_prompt, INT_MAX, NULL, "(%s@%s) %s",
1993 authctxt->server_user, options.host_key_alias ?
1994 options.host_key_alias : authctxt->host, prompt) == -1)
1995 fatal_f("asmprintf failed");
1996 response = read_passphrase(display_prompt, echo ? RP_ECHO : 0);
1997 if ((r = sshpkt_put_cstring(ssh, response)) != 0)
1999 freezero(response, strlen(response));
2001 free(display_prompt);
2002 display_prompt = response = prompt = NULL;
2004 /* done with parsing incoming message. */
2005 if ((r = sshpkt_get_end(ssh)) != 0 ||
2006 (r = sshpkt_add_padding(ssh, 64)) != 0)
2008 r = sshpkt_send(ssh);
2011 freezero(response, strlen(response));
2013 free(display_prompt);
2021 ssh_keysign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
2022 const u_char *data, size_t datalen)
2027 int r, to[2], from[2], status;
2028 int sock = ssh_packet_get_connection_in(ssh);
2029 u_char rversion = 0, version = 2;
2030 void (*osigchld)(int);
2035 if (stat(_PATH_SSH_KEY_SIGN, &st) == -1) {
2036 error_f("not installed: %s", strerror(errno));
2039 if (fflush(stdout) != 0) {
2040 error_f("fflush: %s", strerror(errno));
2043 if (pipe(to) == -1) {
2044 error_f("pipe: %s", strerror(errno));
2047 if (pipe(from) == -1) {
2048 error_f("pipe: %s", strerror(errno));
2051 if ((pid = fork()) == -1) {
2052 error_f("fork: %s", strerror(errno));
2055 osigchld = ssh_signal(SIGCHLD, SIG_DFL);
2058 if (dup2(from[1], STDOUT_FILENO) == -1)
2059 fatal_f("dup2: %s", strerror(errno));
2061 if (dup2(to[0], STDIN_FILENO) == -1)
2062 fatal_f("dup2: %s", strerror(errno));
2066 if (dup2(sock, STDERR_FILENO + 1) == -1)
2067 fatal_f("dup2: %s", strerror(errno));
2068 sock = STDERR_FILENO + 1;
2069 if (fcntl(sock, F_SETFD, 0) == -1) /* keep the socket on exec */
2070 debug3_f("fcntl F_SETFD: %s", strerror(errno));
2071 closefrom(sock + 1);
2073 debug3_f("[child] pid=%ld, exec %s",
2074 (long)getpid(), _PATH_SSH_KEY_SIGN);
2075 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
2076 fatal_f("exec(%s): %s", _PATH_SSH_KEY_SIGN,
2081 sock = STDERR_FILENO + 1;
2083 if ((b = sshbuf_new()) == NULL)
2084 fatal_f("sshbuf_new failed");
2085 /* send # of sock, data to be signed */
2086 if ((r = sshbuf_put_u32(b, sock)) != 0 ||
2087 (r = sshbuf_put_string(b, data, datalen)) != 0)
2088 fatal_fr(r, "buffer error");
2089 if (ssh_msg_send(to[1], version, b) == -1)
2090 fatal_f("couldn't send request");
2092 r = ssh_msg_recv(from[0], b);
2096 error_f("no reply");
2101 while (waitpid(pid, &status, 0) == -1) {
2102 if (errno != EINTR) {
2103 error_f("waitpid %ld: %s", (long)pid, strerror(errno));
2107 if (!WIFEXITED(status)) {
2108 error_f("exited abnormally");
2111 if (WEXITSTATUS(status) != 0) {
2112 error_f("exited with status %d", WEXITSTATUS(status));
2115 if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
2116 error_fr(r, "buffer error");
2119 if (rversion != version) {
2120 error_f("bad version");
2123 if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
2124 error_fr(r, "buffer error");
2126 ssh_signal(SIGCHLD, osigchld);
2130 ssh_signal(SIGCHLD, osigchld);
2137 userauth_hostbased(struct ssh *ssh)
2139 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
2140 struct sshkey *private = NULL;
2141 struct sshbuf *b = NULL;
2142 u_char *sig = NULL, *keyblob = NULL;
2143 char *fp = NULL, *chost = NULL, *lname = NULL;
2144 size_t siglen = 0, keylen = 0;
2145 int i, r, success = 0;
2147 if (authctxt->ktypes == NULL) {
2148 authctxt->oktypes = xstrdup(options.hostbased_accepted_algos);
2149 authctxt->ktypes = authctxt->oktypes;
2153 * Work through each listed type pattern in HostbasedAcceptedAlgorithms,
2154 * trying each hostkey that matches the type in turn.
2157 if (authctxt->active_ktype == NULL)
2158 authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
2159 if (authctxt->active_ktype == NULL ||
2160 *authctxt->active_ktype == '\0')
2162 debug3_f("trying key type %s", authctxt->active_ktype);
2164 /* check for a useful key */
2166 for (i = 0; i < authctxt->sensitive->nkeys; i++) {
2167 if (authctxt->sensitive->keys[i] == NULL ||
2168 authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
2170 if (!sshkey_match_keyname_to_sigalgs(
2171 sshkey_ssh_name(authctxt->sensitive->keys[i]),
2172 authctxt->active_ktype))
2174 /* we take and free the key */
2175 private = authctxt->sensitive->keys[i];
2176 authctxt->sensitive->keys[i] = NULL;
2180 if (private != NULL)
2182 /* No more keys of this type; advance */
2183 authctxt->active_ktype = NULL;
2185 if (private == NULL) {
2186 free(authctxt->oktypes);
2187 authctxt->oktypes = authctxt->ktypes = NULL;
2188 authctxt->active_ktype = NULL;
2189 debug("No more client hostkeys for hostbased authentication.");
2193 if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
2194 SSH_FP_DEFAULT)) == NULL) {
2195 error_f("sshkey_fingerprint failed");
2198 debug_f("trying hostkey %s %s using sigalg %s",
2199 sshkey_ssh_name(private), fp, authctxt->active_ktype);
2201 /* figure out a name for the client host */
2202 lname = get_local_name(ssh_packet_get_connection_in(ssh));
2203 if (lname == NULL) {
2204 error_f("cannot get local ipaddr/name");
2208 /* XXX sshbuf_put_stringf? */
2209 xasprintf(&chost, "%s.", lname);
2210 debug2_f("chost %s", chost);
2212 /* construct data */
2213 if ((b = sshbuf_new()) == NULL) {
2214 error_f("sshbuf_new failed");
2217 if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
2218 error_fr(r, "sshkey_to_blob");
2221 if ((r = sshbuf_put_stringb(b, ssh->kex->session_id)) != 0 ||
2222 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2223 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
2224 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
2225 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
2226 (r = sshbuf_put_cstring(b, authctxt->active_ktype)) != 0 ||
2227 (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
2228 (r = sshbuf_put_cstring(b, chost)) != 0 ||
2229 (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
2230 error_fr(r, "buffer error");
2235 sshbuf_dump(b, stderr);
2237 if ((r = ssh_keysign(ssh, private, &sig, &siglen,
2238 sshbuf_ptr(b), sshbuf_len(b))) != 0) {
2239 error("sign using hostkey %s %s failed",
2240 sshkey_ssh_name(private), fp);
2243 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2244 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
2245 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
2246 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
2247 (r = sshpkt_put_cstring(ssh, authctxt->active_ktype)) != 0 ||
2248 (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
2249 (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
2250 (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
2251 (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
2252 (r = sshpkt_send(ssh)) != 0) {
2253 error_fr(r, "packet error");
2260 freezero(sig, siglen);
2265 sshkey_free(private);
2271 /* find auth method */
2274 * given auth method name, if configurable options permit this method fill
2275 * in auth_ident field and return true, otherwise return false.
2278 authmethod_is_enabled(Authmethod *method)
2282 /* return false if options indicate this method is disabled */
2283 if (method->enabled == NULL || *method->enabled == 0)
2285 /* return false if batch mode is enabled but method needs interactive mode */
2286 if (method->batch_flag != NULL && *method->batch_flag != 0)
2292 authmethod_lookup(const char *name)
2294 Authmethod *method = NULL;
2296 for (method = authmethods; method->name != NULL; method++)
2297 if (strcmp(name, method->name) == 0)
2299 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
2303 /* XXX internal state */
2304 static Authmethod *current = NULL;
2305 static char *supported = NULL;
2306 static char *preferred = NULL;
2309 * Given the authentication method list sent by the server, return the
2310 * next method we should try. If the server initially sends a nil list,
2311 * use a built-in default list.
2314 authmethod_get(char *authlist)
2319 /* Use a suitable default if we're passed a nil list. */
2320 if (authlist == NULL || strlen(authlist) == 0)
2321 authlist = options.preferred_authentications;
2323 if (supported == NULL || strcmp(authlist, supported) != 0) {
2324 debug3("start over, passed a different list %s", authlist);
2326 supported = xstrdup(authlist);
2327 preferred = options.preferred_authentications;
2328 debug3("preferred %s", preferred);
2330 } else if (current != NULL && authmethod_is_enabled(current))
2334 if ((name = match_list(preferred, supported, &next)) == NULL) {
2335 debug("No more authentication methods to try.");
2340 debug3("authmethod_lookup %s", name);
2341 debug3("remaining preferred: %s", preferred);
2342 if ((current = authmethod_lookup(name)) != NULL &&
2343 authmethod_is_enabled(current)) {
2344 debug3("authmethod_is_enabled %s", name);
2345 debug("Next authentication method: %s", name);
2354 authmethods_get(void)
2356 Authmethod *method = NULL;
2361 if ((b = sshbuf_new()) == NULL)
2362 fatal_f("sshbuf_new failed");
2363 for (method = authmethods; method->name != NULL; method++) {
2364 if (authmethod_is_enabled(method)) {
2365 if ((r = sshbuf_putf(b, "%s%s",
2366 sshbuf_len(b) ? "," : "", method->name)) != 0)
2367 fatal_fr(r, "buffer error");
2370 if ((list = sshbuf_dup_string(b)) == NULL)
2371 fatal_f("sshbuf_dup_string failed");