1 /* $OpenBSD: sshconnect2.c,v 1.367 2023/08/01 08:15:04 dtucker 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, host);
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_ext_info(int, u_int32_t, struct ssh *);
362 static int input_userauth_success(int, u_int32_t, struct ssh *);
363 static int input_userauth_failure(int, u_int32_t, struct ssh *);
364 static int input_userauth_banner(int, u_int32_t, struct ssh *);
365 static int input_userauth_error(int, u_int32_t, struct ssh *);
366 static int input_userauth_info_req(int, u_int32_t, struct ssh *);
367 static int input_userauth_pk_ok(int, u_int32_t, struct ssh *);
368 static int input_userauth_passwd_changereq(int, u_int32_t, struct ssh *);
370 static int userauth_none(struct ssh *);
371 static int userauth_pubkey(struct ssh *);
372 static int userauth_passwd(struct ssh *);
373 static int userauth_kbdint(struct ssh *);
374 static int userauth_hostbased(struct ssh *);
377 static int userauth_gssapi(struct ssh *);
378 static void userauth_gssapi_cleanup(struct ssh *);
379 static int input_gssapi_response(int type, u_int32_t, struct ssh *);
380 static int input_gssapi_token(int type, u_int32_t, struct ssh *);
381 static int input_gssapi_error(int, u_int32_t, struct ssh *);
382 static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
385 void userauth(struct ssh *, char *);
387 static void pubkey_cleanup(struct ssh *);
388 static int sign_and_send_pubkey(struct ssh *ssh, Identity *);
389 static void pubkey_prepare(struct ssh *, Authctxt *);
390 static void pubkey_reset(Authctxt *);
391 static struct sshkey *load_identity_file(Identity *);
393 static Authmethod *authmethod_get(char *authlist);
394 static Authmethod *authmethod_lookup(const char *name);
395 static char *authmethods_get(void);
397 Authmethod authmethods[] = {
401 userauth_gssapi_cleanup,
402 &options.gss_authentication,
408 &options.hostbased_authentication,
413 &options.pubkey_authentication,
415 {"keyboard-interactive",
418 &options.kbd_interactive_authentication,
419 &options.batch_mode},
423 &options.password_authentication,
424 &options.batch_mode},
430 {NULL, NULL, NULL, NULL, NULL}
434 ssh_userauth2(struct ssh *ssh, const char *local_user,
435 const char *server_user, char *host, Sensitive *sensitive)
440 if (options.preferred_authentications == NULL)
441 options.preferred_authentications = authmethods_get();
443 /* setup authentication context */
444 memset(&authctxt, 0, sizeof(authctxt));
445 authctxt.server_user = server_user;
446 authctxt.local_user = local_user;
447 authctxt.host = host;
448 authctxt.service = "ssh-connection"; /* service name */
449 authctxt.success = 0;
450 authctxt.method = authmethod_lookup("none");
451 authctxt.authlist = NULL;
452 authctxt.methoddata = NULL;
453 authctxt.sensitive = sensitive;
454 authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
455 authctxt.info_req_seen = 0;
456 authctxt.attempt_kbdint = 0;
457 authctxt.attempt_passwd = 0;
459 authctxt.gss_supported_mechs = NULL;
460 authctxt.mech_tried = 0;
462 authctxt.agent_fd = -1;
463 pubkey_prepare(ssh, &authctxt);
464 if (authctxt.method == NULL) {
465 fatal_f("internal error: cannot send userauth none request");
468 if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
469 (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
470 (r = sshpkt_send(ssh)) != 0)
471 fatal_fr(r, "send packet");
473 ssh->authctxt = &authctxt;
474 ssh_dispatch_init(ssh, &input_userauth_error);
475 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
476 ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
477 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt.success); /* loop until success */
479 ssh->authctxt = NULL;
481 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
483 if (!authctxt.success)
484 fatal("Authentication failed.");
485 if (ssh_packet_connection_is_on_socket(ssh)) {
486 verbose("Authenticated to %s ([%s]:%d) using \"%s\".", host,
487 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
488 authctxt.method->name);
490 verbose("Authenticated to %s (via proxy) using \"%s\".", host,
491 authctxt.method->name);
496 input_userauth_service_accept(int type, u_int32_t seq, struct ssh *ssh)
500 if (ssh_packet_remaining(ssh) > 0) {
503 if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
505 debug2("service_accept: %s", reply);
508 debug2("buggy server: service_accept w/o service");
510 if ((r = sshpkt_get_end(ssh)) != 0)
512 debug("SSH2_MSG_SERVICE_ACCEPT received");
514 /* initial userauth request */
517 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error);
518 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
519 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
520 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
527 input_userauth_ext_info(int type, u_int32_t seqnr, struct ssh *ssh)
529 return kex_input_ext_info(type, seqnr, ssh);
533 userauth(struct ssh *ssh, char *authlist)
535 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
537 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
538 authctxt->method->cleanup(ssh);
540 free(authctxt->methoddata);
541 authctxt->methoddata = NULL;
542 if (authlist == NULL) {
543 authlist = authctxt->authlist;
545 free(authctxt->authlist);
546 authctxt->authlist = authlist;
549 Authmethod *method = authmethod_get(authlist);
551 fatal("%s@%s: Permission denied (%s).",
552 authctxt->server_user, authctxt->host, authlist);
553 authctxt->method = method;
555 /* reset the per method handler */
556 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_PER_METHOD_MIN,
557 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
559 /* and try new method */
560 if (method->userauth(ssh) != 0) {
561 debug2("we sent a %s packet, wait for reply", method->name);
564 debug2("we did not send a packet, disable method");
565 method->enabled = NULL;
571 input_userauth_error(int type, u_int32_t seq, struct ssh *ssh)
573 fatal_f("bad message during authentication: type %d", type);
578 input_userauth_banner(int type, u_int32_t seq, struct ssh *ssh)
584 debug3_f("entering");
585 if ((r = sshpkt_get_cstring(ssh, &msg, &len)) != 0 ||
586 (r = sshpkt_get_cstring(ssh, NULL, NULL)) != 0)
588 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO)
589 fmprintf(stderr, "%s", msg);
597 input_userauth_success(int type, u_int32_t seq, struct ssh *ssh)
599 Authctxt *authctxt = ssh->authctxt;
601 if (authctxt == NULL)
602 fatal_f("no authentication context");
603 free(authctxt->authlist);
604 authctxt->authlist = NULL;
605 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
606 authctxt->method->cleanup(ssh);
607 free(authctxt->methoddata);
608 authctxt->methoddata = NULL;
609 authctxt->success = 1; /* break out */
615 input_userauth_success_unexpected(int type, u_int32_t seq, struct ssh *ssh)
617 Authctxt *authctxt = ssh->authctxt;
619 if (authctxt == NULL)
620 fatal_f("no authentication context");
622 fatal("Unexpected authentication success during %s.",
623 authctxt->method->name);
629 input_userauth_failure(int type, u_int32_t seq, struct ssh *ssh)
631 Authctxt *authctxt = ssh->authctxt;
632 char *authlist = NULL;
635 if (authctxt == NULL)
636 fatal("input_userauth_failure: no authentication context");
638 if (sshpkt_get_cstring(ssh, &authlist, NULL) != 0 ||
639 sshpkt_get_u8(ssh, &partial) != 0 ||
640 sshpkt_get_end(ssh) != 0)
644 verbose("Authenticated using \"%s\" with partial success.",
645 authctxt->method->name);
647 pubkey_reset(authctxt);
649 debug("Authentications that can continue: %s", authlist);
651 userauth(ssh, authlist);
659 * Format an identity for logging including filename, key type, fingerprint
660 * and location (agent, etc.). Caller must free.
663 format_identity(Identity *id)
665 char *fp = NULL, *ret = NULL;
666 const char *note = "";
668 if (id->key != NULL) {
669 fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
673 if ((id->key->flags & SSHKEY_FLAG_EXT) != 0)
675 else if (sshkey_is_sk(id->key))
676 note = " authenticator";
678 xasprintf(&ret, "%s %s%s%s%s%s%s",
680 id->key ? sshkey_type(id->key) : "", id->key ? " " : "",
682 id->userprovided ? " explicit" : "", note,
683 id->agent_fd != -1 ? " agent" : "");
689 input_userauth_pk_ok(int type, u_int32_t seq, struct ssh *ssh)
691 Authctxt *authctxt = ssh->authctxt;
692 struct sshkey *key = NULL;
694 int pktype, found = 0, sent = 0;
696 char *pkalg = NULL, *fp = NULL, *ident = NULL;
697 u_char *pkblob = NULL;
700 if (authctxt == NULL)
701 fatal("input_userauth_pk_ok: no authentication context");
703 if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
704 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
705 (r = sshpkt_get_end(ssh)) != 0)
708 if ((pktype = sshkey_type_from_name(pkalg)) == KEY_UNSPEC) {
709 debug_f("server sent unknown pkalg %s", pkalg);
712 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
713 debug_r(r, "no key from blob. pkalg %s", pkalg);
716 if (key->type != pktype) {
717 error("input_userauth_pk_ok: type mismatch "
718 "for decoded key (received %d, expected %d)",
724 * search keys in the reverse order, because last candidate has been
725 * moved to the end of the queue. this also avoids confusion by
728 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
729 if (sshkey_equal(key, id->key)) {
734 if (!found || id == NULL) {
735 fp = sshkey_fingerprint(key, options.fingerprint_hash,
737 error_f("server replied with unknown key: %s %s",
738 sshkey_type(key), fp == NULL ? "<ERROR>" : fp);
741 ident = format_identity(id);
742 debug("Server accepts key: %s", ident);
743 sent = sign_and_send_pubkey(ssh, id);
752 /* try another method if we did not send a packet */
753 if (r == 0 && sent == 0)
760 userauth_gssapi(struct ssh *ssh)
762 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
763 Gssctxt *gssctxt = NULL;
768 /* Try one GSSAPI method at a time, rather than sending them all at
771 if (authctxt->gss_supported_mechs == NULL)
772 gss_indicate_mechs(&min, &authctxt->gss_supported_mechs);
774 /* Check to see whether the mechanism is usable before we offer it */
775 while (authctxt->mech_tried < authctxt->gss_supported_mechs->count &&
777 mech = &authctxt->gss_supported_mechs->
778 elements[authctxt->mech_tried];
779 /* My DER encoding requires length<128 */
780 if (mech->length < 128 && ssh_gssapi_check_mechanism(&gssctxt,
781 mech, authctxt->host)) {
782 ok = 1; /* Mechanism works */
784 authctxt->mech_tried++;
788 if (!ok || mech == NULL)
791 authctxt->methoddata=(void *)gssctxt;
793 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
794 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
795 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
796 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
797 (r = sshpkt_put_u32(ssh, 1)) != 0 ||
798 (r = sshpkt_put_u32(ssh, (mech->length) + 2)) != 0 ||
799 (r = sshpkt_put_u8(ssh, SSH_GSS_OIDTYPE)) != 0 ||
800 (r = sshpkt_put_u8(ssh, mech->length)) != 0 ||
801 (r = sshpkt_put(ssh, mech->elements, mech->length)) != 0 ||
802 (r = sshpkt_send(ssh)) != 0)
803 fatal_fr(r, "send packet");
805 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
806 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
807 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
808 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
810 authctxt->mech_tried++; /* Move along to next candidate */
816 userauth_gssapi_cleanup(struct ssh *ssh)
818 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
819 Gssctxt *gssctxt = (Gssctxt *)authctxt->methoddata;
821 ssh_gssapi_delete_ctx(&gssctxt);
822 authctxt->methoddata = NULL;
824 free(authctxt->gss_supported_mechs);
825 authctxt->gss_supported_mechs = NULL;
829 process_gssapi_token(struct ssh *ssh, gss_buffer_t recv_tok)
831 Authctxt *authctxt = ssh->authctxt;
832 Gssctxt *gssctxt = authctxt->methoddata;
833 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
834 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
835 gss_buffer_desc gssbuf;
836 OM_uint32 status, ms, flags;
839 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
840 recv_tok, &send_tok, &flags);
842 if (send_tok.length > 0) {
843 u_char type = GSS_ERROR(status) ?
844 SSH2_MSG_USERAUTH_GSSAPI_ERRTOK :
845 SSH2_MSG_USERAUTH_GSSAPI_TOKEN;
847 if ((r = sshpkt_start(ssh, type)) != 0 ||
848 (r = sshpkt_put_string(ssh, send_tok.value,
849 send_tok.length)) != 0 ||
850 (r = sshpkt_send(ssh)) != 0)
851 fatal_fr(r, "send %u packet", type);
853 gss_release_buffer(&ms, &send_tok);
856 if (status == GSS_S_COMPLETE) {
857 /* send either complete or MIC, depending on mechanism */
858 if (!(flags & GSS_C_INTEG_FLAG)) {
859 if ((r = sshpkt_start(ssh,
860 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE)) != 0 ||
861 (r = sshpkt_send(ssh)) != 0)
862 fatal_fr(r, "send completion");
866 if ((b = sshbuf_new()) == NULL)
867 fatal_f("sshbuf_new failed");
868 ssh_gssapi_buildmic(b, authctxt->server_user,
869 authctxt->service, "gssapi-with-mic",
870 ssh->kex->session_id);
872 if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
873 fatal_f("sshbuf_mutable_ptr failed");
874 gssbuf.length = sshbuf_len(b);
876 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
878 if (!GSS_ERROR(status)) {
879 if ((r = sshpkt_start(ssh,
880 SSH2_MSG_USERAUTH_GSSAPI_MIC)) != 0 ||
881 (r = sshpkt_put_string(ssh, mic.value,
883 (r = sshpkt_send(ssh)) != 0)
884 fatal_fr(r, "send MIC");
888 gss_release_buffer(&ms, &mic);
896 input_gssapi_response(int type, u_int32_t plen, struct ssh *ssh)
898 Authctxt *authctxt = ssh->authctxt;
904 if (authctxt == NULL)
905 fatal("input_gssapi_response: no authentication context");
906 gssctxt = authctxt->methoddata;
909 if ((r = sshpkt_get_string(ssh, &oidv, &oidlen)) != 0)
913 oidv[0] != SSH_GSS_OIDTYPE ||
914 oidv[1] != oidlen - 2) {
915 debug("Badly encoded mechanism OID received");
920 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
921 fatal("Server returned different OID than expected");
923 if ((r = sshpkt_get_end(ssh)) != 0)
926 if (GSS_ERROR(process_gssapi_token(ssh, GSS_C_NO_BUFFER))) {
927 /* Start again with next method on list */
928 debug("Trying to start again");
940 input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
942 Authctxt *authctxt = ssh->authctxt;
943 gss_buffer_desc recv_tok;
949 if (authctxt == NULL)
950 fatal("input_gssapi_response: no authentication context");
952 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
953 (r = sshpkt_get_end(ssh)) != 0)
957 recv_tok.length = len;
958 status = process_gssapi_token(ssh, &recv_tok);
960 /* Start again with the next method in the list */
961 if (GSS_ERROR(status)) {
972 input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
974 Authctxt *authctxt = ssh->authctxt;
976 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
977 gss_buffer_desc recv_tok;
983 if (authctxt == NULL)
984 fatal("input_gssapi_response: no authentication context");
985 gssctxt = authctxt->methoddata;
987 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
988 (r = sshpkt_get_end(ssh)) != 0) {
993 /* Stick it into GSSAPI and see what it says */
995 recv_tok.length = len;
996 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
997 &recv_tok, &send_tok, NULL);
999 gss_release_buffer(&ms, &send_tok);
1001 /* Server will be returning a failed packet after this one */
1006 input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh)
1012 if ((r = sshpkt_get_u32(ssh, NULL)) != 0 || /* maj */
1013 (r = sshpkt_get_u32(ssh, NULL)) != 0 || /* min */
1014 (r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
1015 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1017 r = sshpkt_get_end(ssh);
1018 debug("Server GSSAPI Error:\n%s", msg);
1027 userauth_none(struct ssh *ssh)
1029 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1032 /* initial userauth request */
1033 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1034 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1035 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1036 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1037 (r = sshpkt_send(ssh)) != 0)
1038 fatal_fr(r, "send packet");
1043 userauth_passwd(struct ssh *ssh)
1045 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1046 char *password, *prompt = NULL;
1047 const char *host = options.host_key_alias ? options.host_key_alias :
1051 if (authctxt->attempt_passwd++ >= options.number_of_password_prompts)
1054 if (authctxt->attempt_passwd != 1)
1055 error("Permission denied, please try again.");
1057 xasprintf(&prompt, "%s@%s's password: ", authctxt->server_user, host);
1058 password = read_passphrase(prompt, 0);
1059 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1060 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1061 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1062 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1063 (r = sshpkt_put_u8(ssh, 0)) != 0 ||
1064 (r = sshpkt_put_cstring(ssh, password)) != 0 ||
1065 (r = sshpkt_add_padding(ssh, 64)) != 0 ||
1066 (r = sshpkt_send(ssh)) != 0)
1067 fatal_fr(r, "send packet");
1070 if (password != NULL)
1071 freezero(password, strlen(password));
1073 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1074 &input_userauth_passwd_changereq);
1080 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
1083 input_userauth_passwd_changereq(int type, u_int32_t seqnr, struct ssh *ssh)
1085 Authctxt *authctxt = ssh->authctxt;
1086 char *info = NULL, *lang = NULL, *password = NULL, *retype = NULL;
1091 debug2("input_userauth_passwd_changereq");
1093 if (authctxt == NULL)
1094 fatal("input_userauth_passwd_changereq: "
1095 "no authentication context");
1096 host = options.host_key_alias ? options.host_key_alias : authctxt->host;
1098 if ((r = sshpkt_get_cstring(ssh, &info, NULL)) != 0 ||
1099 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1101 if (strlen(info) > 0)
1103 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1104 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1105 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1106 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1107 (r = sshpkt_put_u8(ssh, 1)) != 0) /* additional info */
1110 snprintf(prompt, sizeof(prompt),
1111 "Enter %.30s@%.128s's old password: ",
1112 authctxt->server_user, host);
1113 password = read_passphrase(prompt, 0);
1114 if ((r = sshpkt_put_cstring(ssh, password)) != 0)
1117 freezero(password, strlen(password));
1119 while (password == NULL) {
1120 snprintf(prompt, sizeof(prompt),
1121 "Enter %.30s@%.128s's new password: ",
1122 authctxt->server_user, host);
1123 password = read_passphrase(prompt, RP_ALLOW_EOF);
1124 if (password == NULL) {
1129 snprintf(prompt, sizeof(prompt),
1130 "Retype %.30s@%.128s's new password: ",
1131 authctxt->server_user, host);
1132 retype = read_passphrase(prompt, 0);
1133 if (strcmp(password, retype) != 0) {
1134 freezero(password, strlen(password));
1135 logit("Mismatch; try again, EOF to quit.");
1138 freezero(retype, strlen(retype));
1140 if ((r = sshpkt_put_cstring(ssh, password)) != 0 ||
1141 (r = sshpkt_add_padding(ssh, 64)) != 0 ||
1142 (r = sshpkt_send(ssh)) != 0)
1145 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1146 &input_userauth_passwd_changereq);
1150 freezero(password, strlen(password));
1157 * Select an algorithm for publickey signatures.
1158 * Returns algorithm (caller must free) or NULL if no mutual algorithm found.
1160 * Call with ssh==NULL to ignore server-sig-algs extension list and
1161 * only attempt with the key's base signature type.
1164 key_sig_algorithm(struct ssh *ssh, const struct sshkey *key)
1166 char *allowed, *oallowed, *cp, *tmp, *alg = NULL;
1167 const char *server_sig_algs;
1170 * The signature algorithm will only differ from the key algorithm
1171 * for RSA keys/certs and when the server advertises support for
1172 * newer (SHA2) algorithms.
1174 if (ssh == NULL || ssh->kex->server_sig_algs == NULL ||
1175 (key->type != KEY_RSA && key->type != KEY_RSA_CERT) ||
1176 (key->type == KEY_RSA_CERT && (ssh->compat & SSH_BUG_SIGTYPE))) {
1177 /* Filter base key signature alg against our configuration */
1178 return match_list(sshkey_ssh_name(key),
1179 options.pubkey_accepted_algos, NULL);
1183 * Workaround OpenSSH 7.4 bug: this version supports RSA/SHA-2 but
1184 * fails to advertise it via SSH2_MSG_EXT_INFO.
1186 server_sig_algs = ssh->kex->server_sig_algs;
1187 if (key->type == KEY_RSA && (ssh->compat & SSH_BUG_SIGTYPE74))
1188 server_sig_algs = "rsa-sha2-256,rsa-sha2-512";
1191 * For RSA keys/certs, since these might have a different sig type:
1192 * find the first entry in PubkeyAcceptedAlgorithms of the right type
1193 * that also appears in the supported signature algorithms list from
1196 oallowed = allowed = xstrdup(options.pubkey_accepted_algos);
1197 while ((cp = strsep(&allowed, ",")) != NULL) {
1198 if (sshkey_type_from_name(cp) != key->type)
1200 tmp = match_list(sshkey_sigalg_by_name(cp),
1201 server_sig_algs, NULL);
1213 identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
1214 const u_char *data, size_t datalen, u_int compat, const char *alg)
1216 struct sshkey *sign_key = NULL, *prv = NULL;
1217 int is_agent = 0, retried = 0, r = SSH_ERR_INTERNAL_ERROR;
1218 struct notifier_ctx *notifier = NULL;
1219 char *fp = NULL, *pin = NULL, *prompt = NULL;
1224 /* The agent supports this key. */
1225 if (id->key != NULL && id->agent_fd != -1) {
1226 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
1227 data, datalen, alg, compat);
1231 * We have already loaded the private key or the private key is
1232 * stored in external hardware.
1234 if (id->key != NULL &&
1235 (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))) {
1239 /* Load the private key from the file. */
1240 if ((prv = load_identity_file(id)) == NULL)
1241 return SSH_ERR_KEY_NOT_FOUND;
1242 if (id->key != NULL && !sshkey_equal_public(prv, id->key)) {
1243 error_f("private key %s contents do not match public",
1245 r = SSH_ERR_KEY_NOT_FOUND;
1251 /* Prompt for touch for non-agent FIDO keys that request UP */
1252 if (!is_agent && sshkey_is_sk(sign_key) &&
1253 (sign_key->sk_flags & SSH_SK_USER_PRESENCE_REQD)) {
1254 /* XXX should batch mode just skip these? */
1255 if ((fp = sshkey_fingerprint(sign_key,
1256 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1257 fatal_f("fingerprint failed");
1258 notifier = notify_start(options.batch_mode,
1259 "Confirm user presence for key %s %s",
1260 sshkey_type(sign_key), fp);
1263 if ((r = sshkey_sign(sign_key, sigp, lenp, data, datalen,
1264 alg, options.sk_provider, pin, compat)) != 0) {
1265 debug_fr(r, "sshkey_sign");
1266 if (!retried && pin == NULL && !is_agent &&
1267 sshkey_is_sk(sign_key) &&
1268 r == SSH_ERR_KEY_WRONG_PASSPHRASE) {
1269 notify_complete(notifier, NULL);
1271 xasprintf(&prompt, "Enter PIN for %s key %s: ",
1272 sshkey_type(sign_key), id->filename);
1273 pin = read_passphrase(prompt, 0);
1281 * PKCS#11 tokens may not support all signature algorithms,
1282 * so check what we get back.
1284 if ((r = sshkey_check_sigtype(*sigp, *lenp, alg)) != 0) {
1285 debug_fr(r, "sshkey_check_sigtype");
1293 freezero(pin, strlen(pin));
1294 notify_complete(notifier, r == 0 ? "User presence confirmed" : NULL);
1300 id_filename_matches(Identity *id, Identity *private_id)
1302 static const char * const suffixes[] = { ".pub", "-cert.pub", NULL };
1303 size_t len = strlen(id->filename), plen = strlen(private_id->filename);
1306 if (strcmp(id->filename, private_id->filename) == 0)
1308 for (i = 0; suffixes[i]; i++) {
1309 slen = strlen(suffixes[i]);
1310 if (len > slen && plen == len - slen &&
1311 strcmp(id->filename + (len - slen), suffixes[i]) == 0 &&
1312 memcmp(id->filename, private_id->filename, plen) == 0)
1319 sign_and_send_pubkey(struct ssh *ssh, Identity *id)
1321 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1322 struct sshbuf *b = NULL;
1323 Identity *private_id, *sign_id = NULL;
1324 u_char *signature = NULL;
1325 size_t slen = 0, skip = 0;
1326 int r, fallback_sigtype, sent = 0;
1327 char *alg = NULL, *fp = NULL;
1328 const char *loc = "", *method = "publickey";
1331 /* prefer host-bound pubkey signatures if supported by server */
1332 if ((ssh->kex->flags & KEX_HAS_PUBKEY_HOSTBOUND) != 0 &&
1333 (options.pubkey_authentication & SSH_PUBKEY_AUTH_HBOUND) != 0) {
1335 method = "publickey-hostbound-v00@openssh.com";
1338 if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1339 SSH_FP_DEFAULT)) == NULL)
1342 debug3_f("using %s with %s %s", method, sshkey_type(id->key), fp);
1345 * If the key is an certificate, try to find a matching private key
1346 * and use it to complete the signature.
1347 * If no such private key exists, fall back to trying the certificate
1348 * key itself in case it has a private half already loaded.
1349 * This will try to set sign_id to the private key that will perform
1352 if (sshkey_is_cert(id->key)) {
1353 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1354 if (sshkey_equal_public(id->key, private_id->key) &&
1355 id->key->type != private_id->key->type) {
1356 sign_id = private_id;
1361 * Exact key matches are preferred, but also allow
1362 * filename matches for non-PKCS#11/agent keys that
1363 * didn't load public keys. This supports the case
1364 * of keeping just a private key file and public
1365 * certificate on disk.
1367 if (sign_id == NULL &&
1368 !id->isprivate && id->agent_fd == -1 &&
1369 (id->key->flags & SSHKEY_FLAG_EXT) == 0) {
1370 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1371 if (private_id->key == NULL &&
1372 id_filename_matches(id, private_id)) {
1373 sign_id = private_id;
1378 if (sign_id != NULL) {
1379 debug2_f("using private key \"%s\"%s for "
1380 "certificate", sign_id->filename,
1381 sign_id->agent_fd != -1 ? " from agent" : "");
1383 debug_f("no separate private key for certificate "
1384 "\"%s\"", id->filename);
1389 * If the above didn't select another identity to do the signing
1390 * then default to the one we started with.
1392 if (sign_id == NULL)
1395 /* assemble and sign data */
1396 for (fallback_sigtype = 0; fallback_sigtype <= 1; fallback_sigtype++) {
1400 if ((alg = key_sig_algorithm(fallback_sigtype ? NULL : ssh,
1401 id->key)) == NULL) {
1402 error_f("no mutual signature supported");
1405 debug3_f("signing using %s %s", alg, fp);
1408 if ((b = sshbuf_new()) == NULL)
1409 fatal_f("sshbuf_new failed");
1410 if (ssh->compat & SSH_OLD_SESSIONID) {
1411 if ((r = sshbuf_putb(b, ssh->kex->session_id)) != 0)
1412 fatal_fr(r, "sshbuf_putb");
1414 if ((r = sshbuf_put_stringb(b,
1415 ssh->kex->session_id)) != 0)
1416 fatal_fr(r, "sshbuf_put_stringb");
1418 skip = sshbuf_len(b);
1419 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1420 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1421 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1422 (r = sshbuf_put_cstring(b, method)) != 0 ||
1423 (r = sshbuf_put_u8(b, 1)) != 0 ||
1424 (r = sshbuf_put_cstring(b, alg)) != 0 ||
1425 (r = sshkey_puts(id->key, b)) != 0) {
1426 fatal_fr(r, "assemble signed data");
1429 if (ssh->kex->initial_hostkey == NULL) {
1430 fatal_f("internal error: initial hostkey "
1433 if ((r = sshkey_puts(ssh->kex->initial_hostkey, b)) != 0)
1434 fatal_fr(r, "assemble %s hostkey", method);
1436 /* generate signature */
1437 r = identity_sign(sign_id, &signature, &slen,
1438 sshbuf_ptr(b), sshbuf_len(b), ssh->compat, alg);
1441 else if (r == SSH_ERR_KEY_NOT_FOUND)
1442 goto out; /* soft failure */
1443 else if (r == SSH_ERR_SIGN_ALG_UNSUPPORTED &&
1444 !fallback_sigtype) {
1445 if (sign_id->agent_fd != -1)
1447 else if ((sign_id->key->flags & SSHKEY_FLAG_EXT) != 0)
1449 logit("%skey %s %s returned incorrect signature type",
1450 loc, sshkey_type(id->key), fp);
1453 error_fr(r, "signing failed for %s \"%s\"%s",
1454 sshkey_type(sign_id->key), sign_id->filename,
1455 id->agent_fd != -1 ? " from agent" : "");
1458 if (slen == 0 || signature == NULL) /* shouldn't happen */
1459 fatal_f("no signature");
1461 /* append signature */
1462 if ((r = sshbuf_put_string(b, signature, slen)) != 0)
1463 fatal_fr(r, "append signature");
1466 sshbuf_dump(b, stderr);
1468 /* skip session id and packet type */
1469 if ((r = sshbuf_consume(b, skip + 1)) != 0)
1470 fatal_fr(r, "consume");
1472 /* put remaining data from buffer into packet */
1473 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1474 (r = sshpkt_putb(ssh, b)) != 0 ||
1475 (r = sshpkt_send(ssh)) != 0)
1476 fatal_fr(r, "enqueue request");
1485 freezero(signature, slen);
1490 send_pubkey_test(struct ssh *ssh, Identity *id)
1492 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1493 u_char *blob = NULL;
1499 if ((alg = key_sig_algorithm(ssh, id->key)) == NULL) {
1500 debug_f("no mutual signature algorithm");
1504 if ((r = sshkey_to_blob(id->key, &blob, &bloblen)) != 0) {
1505 /* we cannot handle this key */
1506 debug3_f("cannot handle key");
1509 /* register callback for USERAUTH_PK_OK message */
1510 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1512 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1513 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1514 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1515 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1516 (r = sshpkt_put_u8(ssh, have_sig)) != 0 ||
1517 (r = sshpkt_put_cstring(ssh, alg)) != 0 ||
1518 (r = sshpkt_put_string(ssh, blob, bloblen)) != 0 ||
1519 (r = sshpkt_send(ssh)) != 0)
1520 fatal_fr(r, "send packet");
1529 static struct sshkey *
1530 load_identity_file(Identity *id)
1532 struct sshkey *private = NULL;
1533 char prompt[300], *passphrase, *comment;
1537 if (stat(id->filename, &st) == -1) {
1538 do_log2(id->userprovided ?
1539 SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_DEBUG3,
1540 "no such identity: %s: %s", id->filename, strerror(errno));
1543 snprintf(prompt, sizeof prompt,
1544 "Enter passphrase for key '%.100s': ", id->filename);
1545 for (i = 0; i <= options.number_of_password_prompts; i++) {
1549 passphrase = read_passphrase(prompt, 0);
1550 if (*passphrase == '\0') {
1551 debug2("no passphrase given, try next key");
1556 switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
1557 passphrase, &private, &comment))) {
1560 case SSH_ERR_KEY_WRONG_PASSPHRASE:
1561 if (options.batch_mode) {
1566 debug2("bad passphrase given, try again...");
1568 case SSH_ERR_SYSTEM_ERROR:
1569 if (errno == ENOENT) {
1570 debug2_r(r, "Load key \"%s\"", id->filename);
1576 error_r(r, "Load key \"%s\"", id->filename);
1580 if (private != NULL && sshkey_is_sk(private) &&
1581 options.sk_provider == NULL) {
1582 debug("key \"%s\" is an authenticator-hosted key, "
1583 "but no provider specified", id->filename);
1584 sshkey_free(private);
1588 if (!quit && (r = sshkey_check_rsa_length(private,
1589 options.required_rsa_size)) != 0) {
1590 debug_fr(r, "Skipping key %s", id->filename);
1591 sshkey_free(private);
1595 if (!quit && private != NULL && id->agent_fd == -1 &&
1596 !(id->key && id->isprivate))
1597 maybe_add_key_to_agent(id->filename, private, comment,
1600 freezero(passphrase, strlen(passphrase));
1602 if (private != NULL || quit)
1609 key_type_allowed_by_config(struct sshkey *key)
1611 if (match_pattern_list(sshkey_ssh_name(key),
1612 options.pubkey_accepted_algos, 0) == 1)
1615 /* RSA keys/certs might be allowed by alternate signature types */
1616 switch (key->type) {
1618 if (match_pattern_list("rsa-sha2-512",
1619 options.pubkey_accepted_algos, 0) == 1)
1621 if (match_pattern_list("rsa-sha2-256",
1622 options.pubkey_accepted_algos, 0) == 1)
1626 if (match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
1627 options.pubkey_accepted_algos, 0) == 1)
1629 if (match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
1630 options.pubkey_accepted_algos, 0) == 1)
1637 /* obtain a list of keys from the agent */
1639 get_agent_identities(struct ssh *ssh, int *agent_fdp,
1640 struct ssh_identitylist **idlistp)
1643 struct ssh_identitylist *idlist;
1645 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1646 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1647 debug_fr(r, "ssh_get_authentication_socket");
1650 if ((r = ssh_agent_bind_hostkey(agent_fd, ssh->kex->initial_hostkey,
1651 ssh->kex->session_id, ssh->kex->initial_sig, 0)) == 0)
1652 debug_f("bound agent to hostkey");
1654 debug2_fr(r, "ssh_agent_bind_hostkey");
1656 if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
1657 debug_fr(r, "ssh_fetch_identitylist");
1662 *agent_fdp = agent_fd;
1664 debug_f("agent returned %zu keys", idlist->nkeys);
1669 * try keys in the following order:
1670 * 1. certificates listed in the config file
1671 * 2. other input certificates
1672 * 3. agent keys that are found in the config file
1673 * 4. other agent keys
1674 * 5. keys that are only listed in the config file
1677 pubkey_prepare(struct ssh *ssh, Authctxt *authctxt)
1679 struct identity *id, *id2, *tmp;
1680 struct idlist agent, files, *preferred;
1682 int agent_fd = -1, i, r, found;
1684 struct ssh_identitylist *idlist;
1687 TAILQ_INIT(&agent); /* keys from the agent */
1688 TAILQ_INIT(&files); /* keys from the config file */
1689 preferred = &authctxt->keys;
1690 TAILQ_INIT(preferred); /* preferred order of keys */
1692 /* list of keys stored in the filesystem and PKCS#11 */
1693 for (i = 0; i < options.num_identity_files; i++) {
1694 key = options.identity_keys[i];
1695 if (key && key->cert &&
1696 key->cert->type != SSH2_CERT_TYPE_USER) {
1697 debug_f("ignoring certificate %s: not a user "
1698 "certificate", options.identity_files[i]);
1701 if (key && sshkey_is_sk(key) && options.sk_provider == NULL) {
1702 debug_f("ignoring authenticator-hosted key %s as no "
1703 "SecurityKeyProvider has been specified",
1704 options.identity_files[i]);
1707 options.identity_keys[i] = NULL;
1708 id = xcalloc(1, sizeof(*id));
1711 id->filename = xstrdup(options.identity_files[i]);
1712 id->userprovided = options.identity_file_userprovided[i];
1713 TAILQ_INSERT_TAIL(&files, id, next);
1715 /* list of certificates specified by user */
1716 for (i = 0; i < options.num_certificate_files; i++) {
1717 key = options.certificates[i];
1718 if (!sshkey_is_cert(key) || key->cert == NULL ||
1719 key->cert->type != SSH2_CERT_TYPE_USER) {
1720 debug_f("ignoring certificate %s: not a user "
1721 "certificate", options.identity_files[i]);
1724 if (key && sshkey_is_sk(key) && options.sk_provider == NULL) {
1725 debug_f("ignoring authenticator-hosted key "
1726 "certificate %s as no "
1727 "SecurityKeyProvider has been specified",
1728 options.identity_files[i]);
1731 id = xcalloc(1, sizeof(*id));
1734 id->filename = xstrdup(options.certificate_files[i]);
1735 id->userprovided = options.certificate_file_userprovided[i];
1736 TAILQ_INSERT_TAIL(preferred, id, next);
1738 /* list of keys supported by the agent */
1739 if ((r = get_agent_identities(ssh, &agent_fd, &idlist)) == 0) {
1740 for (j = 0; j < idlist->nkeys; j++) {
1741 if ((r = sshkey_check_rsa_length(idlist->keys[j],
1742 options.required_rsa_size)) != 0) {
1743 debug_fr(r, "ignoring %s agent key",
1744 sshkey_ssh_name(idlist->keys[j]));
1748 TAILQ_FOREACH(id, &files, next) {
1750 * agent keys from the config file are
1753 if (sshkey_equal(idlist->keys[j], id->key)) {
1754 TAILQ_REMOVE(&files, id, next);
1755 TAILQ_INSERT_TAIL(preferred, id, next);
1756 id->agent_fd = agent_fd;
1761 if (!found && !options.identities_only) {
1762 id = xcalloc(1, sizeof(*id));
1763 /* XXX "steals" key/comment from idlist */
1764 id->key = idlist->keys[j];
1765 id->filename = idlist->comments[j];
1766 idlist->keys[j] = NULL;
1767 idlist->comments[j] = NULL;
1768 id->agent_fd = agent_fd;
1769 TAILQ_INSERT_TAIL(&agent, id, next);
1772 ssh_free_identitylist(idlist);
1773 /* append remaining agent keys */
1774 TAILQ_CONCAT(preferred, &agent, next);
1775 authctxt->agent_fd = agent_fd;
1777 /* Prefer PKCS11 keys that are explicitly listed */
1778 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1779 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
1782 TAILQ_FOREACH(id2, &files, next) {
1783 if (id2->key == NULL ||
1784 (id2->key->flags & SSHKEY_FLAG_EXT) != 0)
1786 if (sshkey_equal(id->key, id2->key)) {
1787 TAILQ_REMOVE(&files, id, next);
1788 TAILQ_INSERT_TAIL(preferred, id, next);
1793 /* If IdentitiesOnly set and key not found then don't use it */
1794 if (!found && options.identities_only) {
1795 TAILQ_REMOVE(&files, id, next);
1796 freezero(id, sizeof(*id));
1799 /* append remaining keys from the config file */
1800 TAILQ_CONCAT(preferred, &files, next);
1801 /* finally, filter by PubkeyAcceptedAlgorithms */
1802 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1803 if (id->key != NULL && !key_type_allowed_by_config(id->key)) {
1804 debug("Skipping %s key %s - "
1805 "corresponding algo not in PubkeyAcceptedAlgorithms",
1806 sshkey_ssh_name(id->key), id->filename);
1807 TAILQ_REMOVE(preferred, id, next);
1808 sshkey_free(id->key);
1810 memset(id, 0, sizeof(*id));
1814 /* List the keys we plan on using */
1815 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1816 ident = format_identity(id);
1817 debug("Will attempt key: %s", ident);
1824 pubkey_cleanup(struct ssh *ssh)
1826 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1829 if (authctxt->agent_fd != -1) {
1830 ssh_close_authentication_socket(authctxt->agent_fd);
1831 authctxt->agent_fd = -1;
1833 for (id = TAILQ_FIRST(&authctxt->keys); id;
1834 id = TAILQ_FIRST(&authctxt->keys)) {
1835 TAILQ_REMOVE(&authctxt->keys, id, next);
1836 sshkey_free(id->key);
1843 pubkey_reset(Authctxt *authctxt)
1847 TAILQ_FOREACH(id, &authctxt->keys, next)
1852 userauth_pubkey(struct ssh *ssh)
1854 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1859 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1862 /* move key to the end of the queue */
1863 TAILQ_REMOVE(&authctxt->keys, id, next);
1864 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1866 * send a test message if we have the public key. for
1867 * encrypted keys we cannot do this and have to load the
1868 * private key instead
1870 if (id->key != NULL) {
1871 ident = format_identity(id);
1872 debug("Offering public key: %s", ident);
1874 sent = send_pubkey_test(ssh, id);
1876 debug("Trying private key: %s", id->filename);
1877 id->key = load_identity_file(id);
1878 if (id->key != NULL) {
1879 if (id->key != NULL) {
1881 sent = sign_and_send_pubkey(ssh, id);
1883 sshkey_free(id->key);
1895 * Send userauth request message specifying keyboard-interactive method.
1898 userauth_kbdint(struct ssh *ssh)
1900 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1903 if (authctxt->attempt_kbdint++ >= options.number_of_password_prompts)
1905 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1906 if (authctxt->attempt_kbdint > 1 && !authctxt->info_req_seen) {
1907 debug3("userauth_kbdint: disable: no info_req_seen");
1908 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1912 debug2("userauth_kbdint");
1913 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1914 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1915 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1916 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1917 (r = sshpkt_put_cstring(ssh, "")) != 0 || /* lang */
1918 (r = sshpkt_put_cstring(ssh, options.kbd_interactive_devices ?
1919 options.kbd_interactive_devices : "")) != 0 ||
1920 (r = sshpkt_send(ssh)) != 0)
1921 fatal_fr(r, "send packet");
1923 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1928 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1931 input_userauth_info_req(int type, u_int32_t seq, struct ssh *ssh)
1933 Authctxt *authctxt = ssh->authctxt;
1934 char *name = NULL, *inst = NULL, *lang = NULL, *prompt = NULL;
1935 char *display_prompt = NULL, *response = NULL;
1937 u_int num_prompts, i;
1940 debug2_f("entering");
1942 if (authctxt == NULL)
1943 fatal_f("no authentication context");
1945 authctxt->info_req_seen = 1;
1947 if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 ||
1948 (r = sshpkt_get_cstring(ssh, &inst, NULL)) != 0 ||
1949 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1951 if (strlen(name) > 0)
1953 if (strlen(inst) > 0)
1956 if ((r = sshpkt_get_u32(ssh, &num_prompts)) != 0)
1959 * Begin to build info response packet based on prompts requested.
1960 * We commit to providing the correct number of responses, so if
1961 * further on we run into a problem that prevents this, we have to
1962 * be sure and clean this up and send a correct error response.
1964 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE)) != 0 ||
1965 (r = sshpkt_put_u32(ssh, num_prompts)) != 0)
1968 debug2_f("num_prompts %d", num_prompts);
1969 for (i = 0; i < num_prompts; i++) {
1970 if ((r = sshpkt_get_cstring(ssh, &prompt, NULL)) != 0 ||
1971 (r = sshpkt_get_u8(ssh, &echo)) != 0)
1973 if (asmprintf(&display_prompt, INT_MAX, NULL, "(%s@%s) %s",
1974 authctxt->server_user, options.host_key_alias ?
1975 options.host_key_alias : authctxt->host, prompt) == -1)
1976 fatal_f("asmprintf failed");
1977 response = read_passphrase(display_prompt, echo ? RP_ECHO : 0);
1978 if ((r = sshpkt_put_cstring(ssh, response)) != 0)
1980 freezero(response, strlen(response));
1982 free(display_prompt);
1983 display_prompt = response = prompt = NULL;
1985 /* done with parsing incoming message. */
1986 if ((r = sshpkt_get_end(ssh)) != 0 ||
1987 (r = sshpkt_add_padding(ssh, 64)) != 0)
1989 r = sshpkt_send(ssh);
1992 freezero(response, strlen(response));
1994 free(display_prompt);
2002 ssh_keysign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
2003 const u_char *data, size_t datalen)
2008 int r, to[2], from[2], status;
2009 int sock = ssh_packet_get_connection_in(ssh);
2010 u_char rversion = 0, version = 2;
2011 void (*osigchld)(int);
2016 if (stat(_PATH_SSH_KEY_SIGN, &st) == -1) {
2017 error_f("not installed: %s", strerror(errno));
2020 if (fflush(stdout) != 0) {
2021 error_f("fflush: %s", strerror(errno));
2024 if (pipe(to) == -1) {
2025 error_f("pipe: %s", strerror(errno));
2028 if (pipe(from) == -1) {
2029 error_f("pipe: %s", strerror(errno));
2032 if ((pid = fork()) == -1) {
2033 error_f("fork: %s", strerror(errno));
2036 osigchld = ssh_signal(SIGCHLD, SIG_DFL);
2039 if (dup2(from[1], STDOUT_FILENO) == -1)
2040 fatal_f("dup2: %s", strerror(errno));
2042 if (dup2(to[0], STDIN_FILENO) == -1)
2043 fatal_f("dup2: %s", strerror(errno));
2047 if (dup2(sock, STDERR_FILENO + 1) == -1)
2048 fatal_f("dup2: %s", strerror(errno));
2049 sock = STDERR_FILENO + 1;
2050 if (fcntl(sock, F_SETFD, 0) == -1) /* keep the socket on exec */
2051 debug3_f("fcntl F_SETFD: %s", strerror(errno));
2052 closefrom(sock + 1);
2054 debug3_f("[child] pid=%ld, exec %s",
2055 (long)getpid(), _PATH_SSH_KEY_SIGN);
2056 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
2057 fatal_f("exec(%s): %s", _PATH_SSH_KEY_SIGN,
2062 sock = STDERR_FILENO + 1;
2064 if ((b = sshbuf_new()) == NULL)
2065 fatal_f("sshbuf_new failed");
2066 /* send # of sock, data to be signed */
2067 if ((r = sshbuf_put_u32(b, sock)) != 0 ||
2068 (r = sshbuf_put_string(b, data, datalen)) != 0)
2069 fatal_fr(r, "buffer error");
2070 if (ssh_msg_send(to[1], version, b) == -1)
2071 fatal_f("couldn't send request");
2073 r = ssh_msg_recv(from[0], b);
2077 error_f("no reply");
2082 while (waitpid(pid, &status, 0) == -1) {
2083 if (errno != EINTR) {
2084 error_f("waitpid %ld: %s", (long)pid, strerror(errno));
2088 if (!WIFEXITED(status)) {
2089 error_f("exited abnormally");
2092 if (WEXITSTATUS(status) != 0) {
2093 error_f("exited with status %d", WEXITSTATUS(status));
2096 if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
2097 error_fr(r, "buffer error");
2100 if (rversion != version) {
2101 error_f("bad version");
2104 if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
2105 error_fr(r, "buffer error");
2107 ssh_signal(SIGCHLD, osigchld);
2111 ssh_signal(SIGCHLD, osigchld);
2118 userauth_hostbased(struct ssh *ssh)
2120 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
2121 struct sshkey *private = NULL;
2122 struct sshbuf *b = NULL;
2123 u_char *sig = NULL, *keyblob = NULL;
2124 char *fp = NULL, *chost = NULL, *lname = NULL;
2125 size_t siglen = 0, keylen = 0;
2126 int i, r, success = 0;
2128 if (authctxt->ktypes == NULL) {
2129 authctxt->oktypes = xstrdup(options.hostbased_accepted_algos);
2130 authctxt->ktypes = authctxt->oktypes;
2134 * Work through each listed type pattern in HostbasedAcceptedAlgorithms,
2135 * trying each hostkey that matches the type in turn.
2138 if (authctxt->active_ktype == NULL)
2139 authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
2140 if (authctxt->active_ktype == NULL ||
2141 *authctxt->active_ktype == '\0')
2143 debug3_f("trying key type %s", authctxt->active_ktype);
2145 /* check for a useful key */
2147 for (i = 0; i < authctxt->sensitive->nkeys; i++) {
2148 if (authctxt->sensitive->keys[i] == NULL ||
2149 authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
2151 if (!sshkey_match_keyname_to_sigalgs(
2152 sshkey_ssh_name(authctxt->sensitive->keys[i]),
2153 authctxt->active_ktype))
2155 /* we take and free the key */
2156 private = authctxt->sensitive->keys[i];
2157 authctxt->sensitive->keys[i] = NULL;
2161 if (private != NULL)
2163 /* No more keys of this type; advance */
2164 authctxt->active_ktype = NULL;
2166 if (private == NULL) {
2167 free(authctxt->oktypes);
2168 authctxt->oktypes = authctxt->ktypes = NULL;
2169 authctxt->active_ktype = NULL;
2170 debug("No more client hostkeys for hostbased authentication.");
2174 if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
2175 SSH_FP_DEFAULT)) == NULL) {
2176 error_f("sshkey_fingerprint failed");
2179 debug_f("trying hostkey %s %s using sigalg %s",
2180 sshkey_ssh_name(private), fp, authctxt->active_ktype);
2182 /* figure out a name for the client host */
2183 lname = get_local_name(ssh_packet_get_connection_in(ssh));
2184 if (lname == NULL) {
2185 error_f("cannot get local ipaddr/name");
2189 /* XXX sshbuf_put_stringf? */
2190 xasprintf(&chost, "%s.", lname);
2191 debug2_f("chost %s", chost);
2193 /* construct data */
2194 if ((b = sshbuf_new()) == NULL) {
2195 error_f("sshbuf_new failed");
2198 if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
2199 error_fr(r, "sshkey_to_blob");
2202 if ((r = sshbuf_put_stringb(b, ssh->kex->session_id)) != 0 ||
2203 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2204 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
2205 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
2206 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
2207 (r = sshbuf_put_cstring(b, authctxt->active_ktype)) != 0 ||
2208 (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
2209 (r = sshbuf_put_cstring(b, chost)) != 0 ||
2210 (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
2211 error_fr(r, "buffer error");
2216 sshbuf_dump(b, stderr);
2218 if ((r = ssh_keysign(ssh, private, &sig, &siglen,
2219 sshbuf_ptr(b), sshbuf_len(b))) != 0) {
2220 error("sign using hostkey %s %s failed",
2221 sshkey_ssh_name(private), fp);
2224 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2225 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
2226 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
2227 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
2228 (r = sshpkt_put_cstring(ssh, authctxt->active_ktype)) != 0 ||
2229 (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
2230 (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
2231 (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
2232 (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
2233 (r = sshpkt_send(ssh)) != 0) {
2234 error_fr(r, "packet error");
2241 freezero(sig, siglen);
2246 sshkey_free(private);
2252 /* find auth method */
2255 * given auth method name, if configurable options permit this method fill
2256 * in auth_ident field and return true, otherwise return false.
2259 authmethod_is_enabled(Authmethod *method)
2263 /* return false if options indicate this method is disabled */
2264 if (method->enabled == NULL || *method->enabled == 0)
2266 /* return false if batch mode is enabled but method needs interactive mode */
2267 if (method->batch_flag != NULL && *method->batch_flag != 0)
2273 authmethod_lookup(const char *name)
2275 Authmethod *method = NULL;
2277 for (method = authmethods; method->name != NULL; method++)
2278 if (strcmp(name, method->name) == 0)
2280 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
2284 /* XXX internal state */
2285 static Authmethod *current = NULL;
2286 static char *supported = NULL;
2287 static char *preferred = NULL;
2290 * Given the authentication method list sent by the server, return the
2291 * next method we should try. If the server initially sends a nil list,
2292 * use a built-in default list.
2295 authmethod_get(char *authlist)
2300 /* Use a suitable default if we're passed a nil list. */
2301 if (authlist == NULL || strlen(authlist) == 0)
2302 authlist = options.preferred_authentications;
2304 if (supported == NULL || strcmp(authlist, supported) != 0) {
2305 debug3("start over, passed a different list %s", authlist);
2307 supported = xstrdup(authlist);
2308 preferred = options.preferred_authentications;
2309 debug3("preferred %s", preferred);
2311 } else if (current != NULL && authmethod_is_enabled(current))
2315 if ((name = match_list(preferred, supported, &next)) == NULL) {
2316 debug("No more authentication methods to try.");
2321 debug3("authmethod_lookup %s", name);
2322 debug3("remaining preferred: %s", preferred);
2323 if ((current = authmethod_lookup(name)) != NULL &&
2324 authmethod_is_enabled(current)) {
2325 debug3("authmethod_is_enabled %s", name);
2326 debug("Next authentication method: %s", name);
2335 authmethods_get(void)
2337 Authmethod *method = NULL;
2342 if ((b = sshbuf_new()) == NULL)
2343 fatal_f("sshbuf_new failed");
2344 for (method = authmethods; method->name != NULL; method++) {
2345 if (authmethod_is_enabled(method)) {
2346 if ((r = sshbuf_putf(b, "%s%s",
2347 sshbuf_len(b) ? "," : "", method->name)) != 0)
2348 fatal_fr(r, "buffer error");
2351 if ((list = sshbuf_dup_string(b)) == NULL)
2352 fatal_f("sshbuf_dup_string failed");