1 /* $OpenBSD: sshconnect2.c,v 1.239 2016/02/23 01:34:14 djm Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Copyright (c) 2008 Damien Miller. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
30 #include <sys/socket.h>
43 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
47 #include "openbsd-compat/sys-queue.h"
58 #include "myproposal.h"
59 #include "sshconnect.h"
70 #include "pathnames.h"
80 extern char *client_version_string;
81 extern char *server_version_string;
82 extern Options options;
88 u_char *session_id2 = NULL;
89 u_int session_id2_len = 0;
92 struct sockaddr *xxx_hostaddr;
95 verify_host_key_callback(Key *hostkey, struct ssh *ssh)
97 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
98 fatal("Host key verification failed.");
103 order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
105 char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
107 struct hostkeys *hostkeys;
111 /* Find all hostkeys for this hostname */
112 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
113 hostkeys = init_hostkeys();
114 for (i = 0; i < options.num_user_hostfiles; i++)
115 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
116 for (i = 0; i < options.num_system_hostfiles; i++)
117 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
119 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
120 maxlen = strlen(avail) + 1;
121 first = xmalloc(maxlen);
122 last = xmalloc(maxlen);
123 *first = *last = '\0';
125 #define ALG_APPEND(to, from) \
128 strlcat(to, ",", maxlen); \
129 strlcat(to, from, maxlen); \
132 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
133 if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
134 fatal("%s: unknown alg %s", __func__, alg);
135 if (lookup_key_in_hostkeys_by_type(hostkeys,
136 sshkey_type_plain(ktype), NULL))
137 ALG_APPEND(first, alg);
139 ALG_APPEND(last, alg);
142 xasprintf(&ret, "%s%s%s", first,
143 (*first == '\0' || *last == '\0') ? "" : ",", last);
145 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
151 free_hostkeys(hostkeys);
157 ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
159 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
165 xxx_hostaddr = hostaddr;
167 if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
168 fatal("%s: kex_names_cat", __func__);
169 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(s);
170 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
171 compat_cipher_proposal(options.ciphers);
172 myproposal[PROPOSAL_ENC_ALGS_STOC] =
173 compat_cipher_proposal(options.ciphers);
174 if (options.compression) {
175 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
176 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
178 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
179 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
181 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
182 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
183 if (options.hostkeyalgorithms != NULL) {
184 if (kex_assemble_names(KEX_DEFAULT_PK_ALG,
185 &options.hostkeyalgorithms) != 0)
186 fatal("%s: kex_assemble_namelist", __func__);
187 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
188 compat_pkalg_proposal(options.hostkeyalgorithms);
190 /* Enforce default */
191 options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG);
192 /* Prefer algorithms that we already have keys for */
193 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
194 compat_pkalg_proposal(
195 order_hostkeyalgs(host, hostaddr, port));
198 if (options.rekey_limit || options.rekey_interval)
199 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
200 (time_t)options.rekey_interval);
202 /* start key exchange */
203 if ((r = kex_setup(active_state, myproposal)) != 0)
204 fatal("kex_setup: %s", ssh_err(r));
205 kex = active_state->kex;
207 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
208 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
209 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
210 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
211 # ifdef OPENSSL_HAS_ECC
212 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
215 kex->kex[KEX_C25519_SHA256] = kexc25519_client;
216 kex->client_version_string=client_version_string;
217 kex->server_version_string=server_version_string;
218 kex->verify_host_key=&verify_host_key_callback;
220 dispatch_run(DISPATCH_BLOCK, &kex->done, active_state);
222 /* remove ext-info from the KEX proposals for rekeying */
223 myproposal[PROPOSAL_KEX_ALGS] =
224 compat_kex_proposal(options.kex_algorithms);
225 if ((r = kex_prop2buf(kex->my, myproposal)) != 0)
226 fatal("kex_prop2buf: %s", ssh_err(r));
228 session_id2 = kex->session_id;
229 session_id2_len = kex->session_id_len;
232 /* send 1st encrypted/maced/compressed message */
233 packet_start(SSH2_MSG_IGNORE);
234 packet_put_cstring("markus");
244 typedef struct cauthctxt Authctxt;
245 typedef struct cauthmethod Authmethod;
246 typedef struct identity Identity;
247 typedef struct idlist Idlist;
250 TAILQ_ENTRY(identity) next;
251 int agent_fd; /* >=0 if agent supports key */
252 struct sshkey *key; /* public/private key */
253 char *filename; /* comment for agent-only keys */
255 int isprivate; /* key points to the private key */
258 TAILQ_HEAD(idlist, identity);
261 const char *server_user;
262 const char *local_user;
265 struct cauthmethod *method;
266 sig_atomic_t success;
273 Sensitive *sensitive;
274 char *oktypes, *ktypes;
275 const char *active_ktype;
276 /* kbd-interactive */
283 char *name; /* string to compare against server's list */
284 int (*userauth)(Authctxt *authctxt);
285 void (*cleanup)(Authctxt *authctxt);
286 int *enabled; /* flag in option struct that enables method */
287 int *batch_flag; /* flag in option struct that disables method */
290 int input_userauth_service_accept(int, u_int32_t, void *);
291 int input_userauth_ext_info(int, u_int32_t, void *);
292 int input_userauth_success(int, u_int32_t, void *);
293 int input_userauth_success_unexpected(int, u_int32_t, void *);
294 int input_userauth_failure(int, u_int32_t, void *);
295 int input_userauth_banner(int, u_int32_t, void *);
296 int input_userauth_error(int, u_int32_t, void *);
297 int input_userauth_info_req(int, u_int32_t, void *);
298 int input_userauth_pk_ok(int, u_int32_t, void *);
299 int input_userauth_passwd_changereq(int, u_int32_t, void *);
301 int userauth_none(Authctxt *);
302 int userauth_pubkey(Authctxt *);
303 int userauth_passwd(Authctxt *);
304 int userauth_kbdint(Authctxt *);
305 int userauth_hostbased(Authctxt *);
308 int userauth_gssapi(Authctxt *authctxt);
309 int input_gssapi_response(int type, u_int32_t, void *);
310 int input_gssapi_token(int type, u_int32_t, void *);
311 int input_gssapi_hash(int type, u_int32_t, void *);
312 int input_gssapi_error(int, u_int32_t, void *);
313 int input_gssapi_errtok(int, u_int32_t, void *);
316 void userauth(Authctxt *, char *);
318 static int sign_and_send_pubkey(Authctxt *, Identity *);
319 static void pubkey_prepare(Authctxt *);
320 static void pubkey_cleanup(Authctxt *);
321 static Key *load_identity_file(Identity *);
323 static Authmethod *authmethod_get(char *authlist);
324 static Authmethod *authmethod_lookup(const char *name);
325 static char *authmethods_get(void);
327 Authmethod authmethods[] = {
332 &options.gss_authentication,
338 &options.hostbased_authentication,
343 &options.pubkey_authentication,
345 {"keyboard-interactive",
348 &options.kbd_interactive_authentication,
349 &options.batch_mode},
353 &options.password_authentication,
354 &options.batch_mode},
360 {NULL, NULL, NULL, NULL, NULL}
364 ssh_userauth2(const char *local_user, const char *server_user, char *host,
365 Sensitive *sensitive)
367 struct ssh *ssh = active_state;
371 if (options.challenge_response_authentication)
372 options.kbd_interactive_authentication = 1;
373 if (options.preferred_authentications == NULL)
374 options.preferred_authentications = authmethods_get();
376 /* setup authentication context */
377 memset(&authctxt, 0, sizeof(authctxt));
378 pubkey_prepare(&authctxt);
379 authctxt.server_user = server_user;
380 authctxt.local_user = local_user;
381 authctxt.host = host;
382 authctxt.service = "ssh-connection"; /* service name */
383 authctxt.success = 0;
384 authctxt.method = authmethod_lookup("none");
385 authctxt.authlist = NULL;
386 authctxt.methoddata = NULL;
387 authctxt.sensitive = sensitive;
388 authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
389 authctxt.info_req_seen = 0;
390 authctxt.agent_fd = -1;
391 if (authctxt.method == NULL)
392 fatal("ssh_userauth2: internal error: cannot send userauth none request");
394 if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
395 (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
396 (r = sshpkt_send(ssh)) != 0)
397 fatal("%s: %s", __func__, ssh_err(r));
399 ssh_dispatch_init(ssh, &input_userauth_error);
400 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
401 ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
402 ssh_dispatch_run(ssh, DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
404 pubkey_cleanup(&authctxt);
405 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
407 debug("Authentication succeeded (%s).", authctxt.method->name);
412 input_userauth_service_accept(int type, u_int32_t seqnr, void *ctxt)
414 Authctxt *authctxt = ctxt;
415 struct ssh *ssh = active_state;
418 if (ssh_packet_remaining(ssh) > 0) {
421 if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
423 debug2("service_accept: %s", reply);
426 debug2("buggy server: service_accept w/o service");
428 if ((r = sshpkt_get_end(ssh)) != 0)
430 debug("SSH2_MSG_SERVICE_ACCEPT received");
432 /* initial userauth request */
433 userauth_none(authctxt);
435 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error);
436 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
437 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
438 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
446 input_userauth_ext_info(int type, u_int32_t seqnr, void *ctxt)
448 return kex_input_ext_info(type, seqnr, active_state);
452 userauth(Authctxt *authctxt, char *authlist)
454 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
455 authctxt->method->cleanup(authctxt);
457 free(authctxt->methoddata);
458 authctxt->methoddata = NULL;
459 if (authlist == NULL) {
460 authlist = authctxt->authlist;
462 free(authctxt->authlist);
463 authctxt->authlist = authlist;
466 Authmethod *method = authmethod_get(authlist);
468 fatal("Permission denied (%s).", authlist);
469 authctxt->method = method;
471 /* reset the per method handler */
472 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
473 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
475 /* and try new method */
476 if (method->userauth(authctxt) != 0) {
477 debug2("we sent a %s packet, wait for reply", method->name);
480 debug2("we did not send a packet, disable method");
481 method->enabled = NULL;
488 input_userauth_error(int type, u_int32_t seq, void *ctxt)
490 fatal("input_userauth_error: bad message during authentication: "
497 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
499 char *msg, *raw, *lang;
502 debug3("input_userauth_banner");
503 raw = packet_get_string(&len);
504 lang = packet_get_string(NULL);
505 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
508 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
509 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
510 fprintf(stderr, "%s", msg);
520 input_userauth_success(int type, u_int32_t seq, void *ctxt)
522 Authctxt *authctxt = ctxt;
524 if (authctxt == NULL)
525 fatal("input_userauth_success: no authentication context");
526 free(authctxt->authlist);
527 authctxt->authlist = NULL;
528 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
529 authctxt->method->cleanup(authctxt);
530 free(authctxt->methoddata);
531 authctxt->methoddata = NULL;
532 authctxt->success = 1; /* break out */
537 input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
539 Authctxt *authctxt = ctxt;
541 if (authctxt == NULL)
542 fatal("%s: no authentication context", __func__);
544 fatal("Unexpected authentication success during %s.",
545 authctxt->method->name);
551 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
553 Authctxt *authctxt = ctxt;
554 char *authlist = NULL;
557 if (authctxt == NULL)
558 fatal("input_userauth_failure: no authentication context");
560 authlist = packet_get_string(NULL);
561 partial = packet_get_char();
565 logit("Authenticated with partial success.");
567 pubkey_cleanup(authctxt);
568 pubkey_prepare(authctxt);
570 debug("Authentications that can continue: %s", authlist);
572 userauth(authctxt, authlist);
578 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
580 Authctxt *authctxt = ctxt;
584 int pktype, sent = 0;
589 if (authctxt == NULL)
590 fatal("input_userauth_pk_ok: no authentication context");
591 if (datafellows & SSH_BUG_PKOK) {
592 /* this is similar to SSH_BUG_PKAUTH */
593 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
594 pkblob = packet_get_string(&blen);
596 buffer_append(&b, pkblob, blen);
597 pkalg = buffer_get_string(&b, &alen);
600 pkalg = packet_get_string(&alen);
601 pkblob = packet_get_string(&blen);
605 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
607 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
608 debug("unknown pkalg %s", pkalg);
611 if ((key = key_from_blob(pkblob, blen)) == NULL) {
612 debug("no key from blob. pkalg %s", pkalg);
615 if (key->type != pktype) {
616 error("input_userauth_pk_ok: type mismatch "
617 "for decoded key (received %d, expected %d)",
621 if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
622 SSH_FP_DEFAULT)) == NULL)
624 debug2("input_userauth_pk_ok: fp %s", fp);
628 * search keys in the reverse order, because last candidate has been
629 * moved to the end of the queue. this also avoids confusion by
632 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
633 if (key_equal(key, id->key)) {
634 sent = sign_and_send_pubkey(authctxt, id);
644 /* try another method if we did not send a packet */
646 userauth(authctxt, NULL);
652 userauth_gssapi(Authctxt *authctxt)
654 Gssctxt *gssctxt = NULL;
655 static gss_OID_set gss_supported = NULL;
656 static u_int mech = 0;
660 /* Try one GSSAPI method at a time, rather than sending them all at
663 if (gss_supported == NULL)
664 gss_indicate_mechs(&min, &gss_supported);
666 /* Check to see if the mechanism is usable before we offer it */
667 while (mech < gss_supported->count && !ok) {
668 /* My DER encoding requires length<128 */
669 if (gss_supported->elements[mech].length < 128 &&
670 ssh_gssapi_check_mechanism(&gssctxt,
671 &gss_supported->elements[mech], authctxt->host)) {
672 ok = 1; /* Mechanism works */
681 authctxt->methoddata=(void *)gssctxt;
683 packet_start(SSH2_MSG_USERAUTH_REQUEST);
684 packet_put_cstring(authctxt->server_user);
685 packet_put_cstring(authctxt->service);
686 packet_put_cstring(authctxt->method->name);
690 packet_put_int((gss_supported->elements[mech].length) + 2);
691 packet_put_char(SSH_GSS_OIDTYPE);
692 packet_put_char(gss_supported->elements[mech].length);
693 packet_put_raw(gss_supported->elements[mech].elements,
694 gss_supported->elements[mech].length);
698 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
699 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
700 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
701 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
703 mech++; /* Move along to next candidate */
709 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
711 Authctxt *authctxt = ctxt;
712 Gssctxt *gssctxt = authctxt->methoddata;
713 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
714 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
715 gss_buffer_desc gssbuf;
716 OM_uint32 status, ms, flags;
719 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
720 recv_tok, &send_tok, &flags);
722 if (send_tok.length > 0) {
723 if (GSS_ERROR(status))
724 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
726 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
728 packet_put_string(send_tok.value, send_tok.length);
730 gss_release_buffer(&ms, &send_tok);
733 if (status == GSS_S_COMPLETE) {
734 /* send either complete or MIC, depending on mechanism */
735 if (!(flags & GSS_C_INTEG_FLAG)) {
736 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
739 ssh_gssapi_buildmic(&b, authctxt->server_user,
740 authctxt->service, "gssapi-with-mic");
742 gssbuf.value = buffer_ptr(&b);
743 gssbuf.length = buffer_len(&b);
745 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
747 if (!GSS_ERROR(status)) {
748 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
749 packet_put_string(mic.value, mic.length);
755 gss_release_buffer(&ms, &mic);
764 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
766 Authctxt *authctxt = ctxt;
771 if (authctxt == NULL)
772 fatal("input_gssapi_response: no authentication context");
773 gssctxt = authctxt->methoddata;
776 oidv = packet_get_string(&oidlen);
779 oidv[0] != SSH_GSS_OIDTYPE ||
780 oidv[1] != oidlen - 2) {
782 debug("Badly encoded mechanism OID received");
783 userauth(authctxt, NULL);
787 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
788 fatal("Server returned different OID than expected");
794 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
795 /* Start again with next method on list */
796 debug("Trying to start again");
797 userauth(authctxt, NULL);
805 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
807 Authctxt *authctxt = ctxt;
808 gss_buffer_desc recv_tok;
812 if (authctxt == NULL)
813 fatal("input_gssapi_response: no authentication context");
815 recv_tok.value = packet_get_string(&slen);
816 recv_tok.length = slen; /* safe typecast */
820 status = process_gssapi_token(ctxt, &recv_tok);
822 free(recv_tok.value);
824 if (GSS_ERROR(status)) {
825 /* Start again with the next method in the list */
826 userauth(authctxt, NULL);
834 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
836 Authctxt *authctxt = ctxt;
838 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
839 gss_buffer_desc recv_tok;
843 if (authctxt == NULL)
844 fatal("input_gssapi_response: no authentication context");
845 gssctxt = authctxt->methoddata;
847 recv_tok.value = packet_get_string(&len);
848 recv_tok.length = len;
852 /* Stick it into GSSAPI and see what it says */
853 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
854 &recv_tok, &send_tok, NULL);
856 free(recv_tok.value);
857 gss_release_buffer(&ms, &send_tok);
859 /* Server will be returning a failed packet after this one */
865 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
870 /* maj */(void)packet_get_int();
871 /* min */(void)packet_get_int();
872 msg=packet_get_string(NULL);
873 lang=packet_get_string(NULL);
877 debug("Server GSSAPI Error:\n%s", msg);
885 userauth_none(Authctxt *authctxt)
887 /* initial userauth request */
888 packet_start(SSH2_MSG_USERAUTH_REQUEST);
889 packet_put_cstring(authctxt->server_user);
890 packet_put_cstring(authctxt->service);
891 packet_put_cstring(authctxt->method->name);
897 userauth_passwd(Authctxt *authctxt)
899 static int attempt = 0;
902 const char *host = options.host_key_alias ? options.host_key_alias :
905 if (attempt++ >= options.number_of_password_prompts)
909 error("Permission denied, please try again.");
911 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
912 authctxt->server_user, host);
913 password = read_passphrase(prompt, 0);
914 packet_start(SSH2_MSG_USERAUTH_REQUEST);
915 packet_put_cstring(authctxt->server_user);
916 packet_put_cstring(authctxt->service);
917 packet_put_cstring(authctxt->method->name);
919 packet_put_cstring(password);
920 explicit_bzero(password, strlen(password));
922 packet_add_padding(64);
925 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
926 &input_userauth_passwd_changereq);
932 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
936 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
938 Authctxt *authctxt = ctxt;
939 char *info, *lang, *password = NULL, *retype = NULL;
941 const char *host = options.host_key_alias ? options.host_key_alias :
944 debug2("input_userauth_passwd_changereq");
946 if (authctxt == NULL)
947 fatal("input_userauth_passwd_changereq: "
948 "no authentication context");
950 info = packet_get_string(NULL);
951 lang = packet_get_string(NULL);
952 if (strlen(info) > 0)
956 packet_start(SSH2_MSG_USERAUTH_REQUEST);
957 packet_put_cstring(authctxt->server_user);
958 packet_put_cstring(authctxt->service);
959 packet_put_cstring(authctxt->method->name);
960 packet_put_char(1); /* additional info */
961 snprintf(prompt, sizeof(prompt),
962 "Enter %.30s@%.128s's old password: ",
963 authctxt->server_user, host);
964 password = read_passphrase(prompt, 0);
965 packet_put_cstring(password);
966 explicit_bzero(password, strlen(password));
969 while (password == NULL) {
970 snprintf(prompt, sizeof(prompt),
971 "Enter %.30s@%.128s's new password: ",
972 authctxt->server_user, host);
973 password = read_passphrase(prompt, RP_ALLOW_EOF);
974 if (password == NULL) {
978 snprintf(prompt, sizeof(prompt),
979 "Retype %.30s@%.128s's new password: ",
980 authctxt->server_user, host);
981 retype = read_passphrase(prompt, 0);
982 if (strcmp(password, retype) != 0) {
983 explicit_bzero(password, strlen(password));
985 logit("Mismatch; try again, EOF to quit.");
988 explicit_bzero(retype, strlen(retype));
991 packet_put_cstring(password);
992 explicit_bzero(password, strlen(password));
994 packet_add_padding(64);
997 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
998 &input_userauth_passwd_changereq);
1003 identity_sign_encode(struct identity *id)
1005 struct ssh *ssh = active_state;
1007 if (id->key->type == KEY_RSA) {
1008 switch (ssh->kex->rsa_sha2) {
1010 return "rsa-sha2-256";
1012 return "rsa-sha2-512";
1015 return key_ssh_name(id->key);
1019 identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
1020 const u_char *data, size_t datalen, u_int compat)
1026 alg = identity_sign_encode(id);
1028 /* the agent supports this key */
1029 if (id->agent_fd != -1)
1030 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
1031 data, datalen, alg, compat);
1034 * we have already loaded the private key or
1035 * the private key is stored in external hardware
1037 if (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))
1038 return (sshkey_sign(id->key, sigp, lenp, data, datalen, alg,
1040 /* load the private key from the file */
1041 if ((prv = load_identity_file(id)) == NULL)
1042 return SSH_ERR_KEY_NOT_FOUND;
1043 ret = sshkey_sign(prv, sigp, lenp, data, datalen, alg, compat);
1049 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1052 Identity *private_id;
1053 u_char *blob, *signature;
1055 u_int bloblen, skip = 0;
1056 int matched, ret = -1, have_sig = 1;
1059 if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1060 SSH_FP_DEFAULT)) == NULL)
1062 debug3("%s: %s %s", __func__, key_type(id->key), fp);
1065 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1066 /* we cannot handle this key */
1067 debug3("sign_and_send_pubkey: cannot handle key");
1070 /* data to be signed */
1072 if (datafellows & SSH_OLD_SESSIONID) {
1073 buffer_append(&b, session_id2, session_id2_len);
1074 skip = session_id2_len;
1076 buffer_put_string(&b, session_id2, session_id2_len);
1077 skip = buffer_len(&b);
1079 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1080 buffer_put_cstring(&b, authctxt->server_user);
1081 buffer_put_cstring(&b,
1082 datafellows & SSH_BUG_PKSERVICE ?
1085 if (datafellows & SSH_BUG_PKAUTH) {
1086 buffer_put_char(&b, have_sig);
1088 buffer_put_cstring(&b, authctxt->method->name);
1089 buffer_put_char(&b, have_sig);
1090 buffer_put_cstring(&b, identity_sign_encode(id));
1092 buffer_put_string(&b, blob, bloblen);
1095 * If the key is an certificate, try to find a matching private key
1096 * and use it to complete the signature.
1097 * If no such private key exists, return failure and continue with
1098 * other methods of authentication.
1100 if (key_is_cert(id->key)) {
1102 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1103 if (sshkey_equal_public(id->key, private_id->key) &&
1104 id->key->type != private_id->key->type) {
1111 debug2("%s: using private key \"%s\"%s for "
1112 "certificate", __func__, id->filename,
1113 id->agent_fd != -1 ? " from agent" : "");
1115 /* XXX maybe verbose/error? */
1116 debug("%s: no private key for certificate "
1117 "\"%s\"", __func__, id->filename);
1124 /* generate signature */
1125 ret = identity_sign(id, &signature, &slen,
1126 buffer_ptr(&b), buffer_len(&b), datafellows);
1128 if (ret != SSH_ERR_KEY_NOT_FOUND)
1129 error("%s: signing failed: %s", __func__, ssh_err(ret));
1137 if (datafellows & SSH_BUG_PKSERVICE) {
1139 buffer_append(&b, session_id2, session_id2_len);
1140 skip = session_id2_len;
1141 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1142 buffer_put_cstring(&b, authctxt->server_user);
1143 buffer_put_cstring(&b, authctxt->service);
1144 buffer_put_cstring(&b, authctxt->method->name);
1145 buffer_put_char(&b, have_sig);
1146 if (!(datafellows & SSH_BUG_PKAUTH))
1147 buffer_put_cstring(&b, key_ssh_name(id->key));
1148 buffer_put_string(&b, blob, bloblen);
1152 /* append signature */
1153 buffer_put_string(&b, signature, slen);
1156 /* skip session id and packet type */
1157 if (buffer_len(&b) < skip + 1)
1158 fatal("userauth_pubkey: internal error");
1159 buffer_consume(&b, skip + 1);
1161 /* put remaining data from buffer into packet */
1162 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1163 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1171 send_pubkey_test(Authctxt *authctxt, Identity *id)
1174 u_int bloblen, have_sig = 0;
1176 debug3("send_pubkey_test");
1178 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1179 /* we cannot handle this key */
1180 debug3("send_pubkey_test: cannot handle key");
1183 /* register callback for USERAUTH_PK_OK message */
1184 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1186 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1187 packet_put_cstring(authctxt->server_user);
1188 packet_put_cstring(authctxt->service);
1189 packet_put_cstring(authctxt->method->name);
1190 packet_put_char(have_sig);
1191 if (!(datafellows & SSH_BUG_PKAUTH))
1192 packet_put_cstring(identity_sign_encode(id));
1193 packet_put_string(blob, bloblen);
1200 load_identity_file(Identity *id)
1202 Key *private = NULL;
1203 char prompt[300], *passphrase, *comment;
1204 int r, perm_ok = 0, quit = 0, i;
1207 if (stat(id->filename, &st) < 0) {
1208 (id->userprovided ? logit : debug3)("no such identity: %s: %s",
1209 id->filename, strerror(errno));
1212 snprintf(prompt, sizeof prompt,
1213 "Enter passphrase for key '%.100s': ", id->filename);
1214 for (i = 0; i <= options.number_of_password_prompts; i++) {
1218 passphrase = read_passphrase(prompt, 0);
1219 if (*passphrase == '\0') {
1220 debug2("no passphrase given, try next key");
1225 switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
1226 passphrase, &private, &comment, &perm_ok))) {
1229 case SSH_ERR_KEY_WRONG_PASSPHRASE:
1230 if (options.batch_mode) {
1235 debug2("bad passphrase given, try again...");
1237 case SSH_ERR_SYSTEM_ERROR:
1238 if (errno == ENOENT) {
1239 debug2("Load key \"%s\": %s",
1240 id->filename, ssh_err(r));
1246 error("Load key \"%s\": %s", id->filename, ssh_err(r));
1250 if (!quit && private != NULL && id->agent_fd == -1 &&
1251 !(id->key && id->isprivate))
1252 maybe_add_key_to_agent(id->filename, private, comment,
1255 explicit_bzero(passphrase, strlen(passphrase));
1259 if (private != NULL || quit)
1266 * try keys in the following order:
1267 * 1. certificates listed in the config file
1268 * 2. other input certificates
1269 * 3. agent keys that are found in the config file
1270 * 4. other agent keys
1271 * 5. keys that are only listed in the config file
1274 pubkey_prepare(Authctxt *authctxt)
1276 struct identity *id, *id2, *tmp;
1277 struct idlist agent, files, *preferred;
1279 int agent_fd = -1, i, r, found;
1281 struct ssh_identitylist *idlist;
1283 TAILQ_INIT(&agent); /* keys from the agent */
1284 TAILQ_INIT(&files); /* keys from the config file */
1285 preferred = &authctxt->keys;
1286 TAILQ_INIT(preferred); /* preferred order of keys */
1288 /* list of keys stored in the filesystem and PKCS#11 */
1289 for (i = 0; i < options.num_identity_files; i++) {
1290 key = options.identity_keys[i];
1291 if (key && key->type == KEY_RSA1)
1293 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1295 options.identity_keys[i] = NULL;
1296 id = xcalloc(1, sizeof(*id));
1299 id->filename = xstrdup(options.identity_files[i]);
1300 id->userprovided = options.identity_file_userprovided[i];
1301 TAILQ_INSERT_TAIL(&files, id, next);
1303 /* Prefer PKCS11 keys that are explicitly listed */
1304 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1305 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
1308 TAILQ_FOREACH(id2, &files, next) {
1309 if (id2->key == NULL ||
1310 (id2->key->flags & SSHKEY_FLAG_EXT) == 0)
1312 if (sshkey_equal(id->key, id2->key)) {
1313 TAILQ_REMOVE(&files, id, next);
1314 TAILQ_INSERT_TAIL(preferred, id, next);
1319 /* If IdentitiesOnly set and key not found then don't use it */
1320 if (!found && options.identities_only) {
1321 TAILQ_REMOVE(&files, id, next);
1322 explicit_bzero(id, sizeof(*id));
1326 /* list of certificates specified by user */
1327 for (i = 0; i < options.num_certificate_files; i++) {
1328 key = options.certificates[i];
1329 if (!key_is_cert(key) || key->cert == NULL ||
1330 key->cert->type != SSH2_CERT_TYPE_USER)
1332 id = xcalloc(1, sizeof(*id));
1335 id->filename = xstrdup(options.certificate_files[i]);
1336 id->userprovided = options.certificate_file_userprovided[i];
1337 TAILQ_INSERT_TAIL(preferred, id, next);
1339 /* list of keys supported by the agent */
1340 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1341 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1342 debug("%s: ssh_get_authentication_socket: %s",
1343 __func__, ssh_err(r));
1344 } else if ((r = ssh_fetch_identitylist(agent_fd, 2, &idlist)) != 0) {
1345 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1346 debug("%s: ssh_fetch_identitylist: %s",
1347 __func__, ssh_err(r));
1350 for (j = 0; j < idlist->nkeys; j++) {
1352 TAILQ_FOREACH(id, &files, next) {
1354 * agent keys from the config file are
1357 if (sshkey_equal(idlist->keys[j], id->key)) {
1358 TAILQ_REMOVE(&files, id, next);
1359 TAILQ_INSERT_TAIL(preferred, id, next);
1360 id->agent_fd = agent_fd;
1365 if (!found && !options.identities_only) {
1366 id = xcalloc(1, sizeof(*id));
1367 /* XXX "steals" key/comment from idlist */
1368 id->key = idlist->keys[j];
1369 id->filename = idlist->comments[j];
1370 idlist->keys[j] = NULL;
1371 idlist->comments[j] = NULL;
1372 id->agent_fd = agent_fd;
1373 TAILQ_INSERT_TAIL(&agent, id, next);
1376 ssh_free_identitylist(idlist);
1377 /* append remaining agent keys */
1378 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1379 TAILQ_REMOVE(&agent, id, next);
1380 TAILQ_INSERT_TAIL(preferred, id, next);
1382 authctxt->agent_fd = agent_fd;
1384 /* append remaining keys from the config file */
1385 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1386 TAILQ_REMOVE(&files, id, next);
1387 TAILQ_INSERT_TAIL(preferred, id, next);
1389 /* finally, filter by PubkeyAcceptedKeyTypes */
1390 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1391 if (id->key != NULL &&
1392 match_pattern_list(sshkey_ssh_name(id->key),
1393 options.pubkey_key_types, 0) != 1) {
1394 debug("Skipping %s key %s - "
1395 "not in PubkeyAcceptedKeyTypes",
1396 sshkey_ssh_name(id->key), id->filename);
1397 TAILQ_REMOVE(preferred, id, next);
1398 sshkey_free(id->key);
1400 memset(id, 0, sizeof(*id));
1403 debug2("key: %s (%p)%s%s", id->filename, id->key,
1404 id->userprovided ? ", explicit" : "",
1405 id->agent_fd != -1 ? ", agent" : "");
1410 pubkey_cleanup(Authctxt *authctxt)
1414 if (authctxt->agent_fd != -1)
1415 ssh_close_authentication_socket(authctxt->agent_fd);
1416 for (id = TAILQ_FIRST(&authctxt->keys); id;
1417 id = TAILQ_FIRST(&authctxt->keys)) {
1418 TAILQ_REMOVE(&authctxt->keys, id, next);
1419 sshkey_free(id->key);
1426 try_identity(Identity *id)
1430 if (key_type_plain(id->key->type) == KEY_RSA &&
1431 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1432 debug("Skipped %s key %s for RSA/MD5 server",
1433 key_type(id->key), id->filename);
1436 return (id->key->type != KEY_RSA1);
1440 userauth_pubkey(Authctxt *authctxt)
1445 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1448 /* move key to the end of the queue */
1449 TAILQ_REMOVE(&authctxt->keys, id, next);
1450 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1452 * send a test message if we have the public key. for
1453 * encrypted keys we cannot do this and have to load the
1454 * private key instead
1456 if (id->key != NULL) {
1457 if (try_identity(id)) {
1458 debug("Offering %s public key: %s",
1459 key_type(id->key), id->filename);
1460 sent = send_pubkey_test(authctxt, id);
1463 debug("Trying private key: %s", id->filename);
1464 id->key = load_identity_file(id);
1465 if (id->key != NULL) {
1466 if (try_identity(id)) {
1468 sent = sign_and_send_pubkey(
1482 * Send userauth request message specifying keyboard-interactive method.
1485 userauth_kbdint(Authctxt *authctxt)
1487 static int attempt = 0;
1489 if (attempt++ >= options.number_of_password_prompts)
1491 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1492 if (attempt > 1 && !authctxt->info_req_seen) {
1493 debug3("userauth_kbdint: disable: no info_req_seen");
1494 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1498 debug2("userauth_kbdint");
1499 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1500 packet_put_cstring(authctxt->server_user);
1501 packet_put_cstring(authctxt->service);
1502 packet_put_cstring(authctxt->method->name);
1503 packet_put_cstring(""); /* lang */
1504 packet_put_cstring(options.kbd_interactive_devices ?
1505 options.kbd_interactive_devices : "");
1508 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1513 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1516 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1518 Authctxt *authctxt = ctxt;
1519 char *name, *inst, *lang, *prompt, *response;
1520 u_int num_prompts, i;
1523 debug2("input_userauth_info_req");
1525 if (authctxt == NULL)
1526 fatal("input_userauth_info_req: no authentication context");
1528 authctxt->info_req_seen = 1;
1530 name = packet_get_string(NULL);
1531 inst = packet_get_string(NULL);
1532 lang = packet_get_string(NULL);
1533 if (strlen(name) > 0)
1535 if (strlen(inst) > 0)
1541 num_prompts = packet_get_int();
1543 * Begin to build info response packet based on prompts requested.
1544 * We commit to providing the correct number of responses, so if
1545 * further on we run into a problem that prevents this, we have to
1546 * be sure and clean this up and send a correct error response.
1548 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1549 packet_put_int(num_prompts);
1551 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1552 for (i = 0; i < num_prompts; i++) {
1553 prompt = packet_get_string(NULL);
1554 echo = packet_get_char();
1556 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1558 packet_put_cstring(response);
1559 explicit_bzero(response, strlen(response));
1563 packet_check_eom(); /* done with parsing incoming message. */
1565 packet_add_padding(64);
1571 ssh_keysign(struct sshkey *key, u_char **sigp, size_t *lenp,
1572 const u_char *data, size_t datalen)
1577 int i, r, to[2], from[2], status, sock = packet_get_connection_in();
1578 u_char rversion = 0, version = 2;
1579 void (*osigchld)(int);
1584 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1585 error("%s: not installed: %s", __func__, strerror(errno));
1588 if (fflush(stdout) != 0) {
1589 error("%s: fflush: %s", __func__, strerror(errno));
1593 error("%s: pipe: %s", __func__, strerror(errno));
1596 if (pipe(from) < 0) {
1597 error("%s: pipe: %s", __func__, strerror(errno));
1600 if ((pid = fork()) < 0) {
1601 error("%s: fork: %s", __func__, strerror(errno));
1604 osigchld = signal(SIGCHLD, SIG_DFL);
1606 /* keep the socket on exec */
1607 fcntl(sock, F_SETFD, 0);
1608 permanently_drop_suid(getuid());
1610 if (dup2(from[1], STDOUT_FILENO) < 0)
1611 fatal("%s: dup2: %s", __func__, strerror(errno));
1613 if (dup2(to[0], STDIN_FILENO) < 0)
1614 fatal("%s: dup2: %s", __func__, strerror(errno));
1617 /* Close everything but stdio and the socket */
1618 for (i = STDERR_FILENO + 1; i < sock; i++)
1620 closefrom(sock + 1);
1621 debug3("%s: [child] pid=%ld, exec %s",
1622 __func__, (long)getpid(), _PATH_SSH_KEY_SIGN);
1623 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
1624 fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN,
1630 if ((b = sshbuf_new()) == NULL)
1631 fatal("%s: sshbuf_new failed", __func__);
1632 /* send # of sock, data to be signed */
1633 if ((r = sshbuf_put_u32(b, sock) != 0) ||
1634 (r = sshbuf_put_string(b, data, datalen)) != 0)
1635 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1636 if (ssh_msg_send(to[1], version, b) == -1)
1637 fatal("%s: couldn't send request", __func__);
1639 r = ssh_msg_recv(from[0], b);
1643 error("%s: no reply", __func__);
1648 while (waitpid(pid, &status, 0) < 0) {
1649 if (errno != EINTR) {
1650 error("%s: waitpid %ld: %s",
1651 __func__, (long)pid, strerror(errno));
1655 if (!WIFEXITED(status)) {
1656 error("%s: exited abnormally", __func__);
1659 if (WEXITSTATUS(status) != 0) {
1660 error("%s: exited with status %d",
1661 __func__, WEXITSTATUS(status));
1664 if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
1665 error("%s: buffer error: %s", __func__, ssh_err(r));
1668 if (rversion != version) {
1669 error("%s: bad version", __func__);
1672 if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
1673 error("%s: buffer error: %s", __func__, ssh_err(r));
1675 signal(SIGCHLD, osigchld);
1679 signal(SIGCHLD, osigchld);
1686 userauth_hostbased(Authctxt *authctxt)
1688 struct ssh *ssh = active_state;
1689 struct sshkey *private = NULL;
1690 struct sshbuf *b = NULL;
1691 const char *service;
1692 u_char *sig = NULL, *keyblob = NULL;
1693 char *fp = NULL, *chost = NULL, *lname = NULL;
1694 size_t siglen = 0, keylen = 0;
1695 int i, r, success = 0;
1697 if (authctxt->ktypes == NULL) {
1698 authctxt->oktypes = xstrdup(options.hostbased_key_types);
1699 authctxt->ktypes = authctxt->oktypes;
1703 * Work through each listed type pattern in HostbasedKeyTypes,
1704 * trying each hostkey that matches the type in turn.
1707 if (authctxt->active_ktype == NULL)
1708 authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
1709 if (authctxt->active_ktype == NULL ||
1710 *authctxt->active_ktype == '\0')
1712 debug3("%s: trying key type %s", __func__,
1713 authctxt->active_ktype);
1715 /* check for a useful key */
1717 for (i = 0; i < authctxt->sensitive->nkeys; i++) {
1718 if (authctxt->sensitive->keys[i] == NULL ||
1719 authctxt->sensitive->keys[i]->type == KEY_RSA1 ||
1720 authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
1722 if (match_pattern_list(
1723 sshkey_ssh_name(authctxt->sensitive->keys[i]),
1724 authctxt->active_ktype, 0) != 1)
1726 /* we take and free the key */
1727 private = authctxt->sensitive->keys[i];
1728 authctxt->sensitive->keys[i] = NULL;
1732 if (private != NULL)
1734 /* No more keys of this type; advance */
1735 authctxt->active_ktype = NULL;
1737 if (private == NULL) {
1738 free(authctxt->oktypes);
1739 authctxt->oktypes = authctxt->ktypes = NULL;
1740 authctxt->active_ktype = NULL;
1741 debug("No more client hostkeys for hostbased authentication.");
1745 if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
1746 SSH_FP_DEFAULT)) == NULL) {
1747 error("%s: sshkey_fingerprint failed", __func__);
1750 debug("%s: trying hostkey %s %s",
1751 __func__, sshkey_ssh_name(private), fp);
1753 /* figure out a name for the client host */
1754 if ((lname = get_local_name(packet_get_connection_in())) == NULL) {
1755 error("%s: cannot get local ipaddr/name", __func__);
1759 /* XXX sshbuf_put_stringf? */
1760 xasprintf(&chost, "%s.", lname);
1761 debug2("%s: chost %s", __func__, chost);
1763 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1766 /* construct data */
1767 if ((b = sshbuf_new()) == NULL) {
1768 error("%s: sshbuf_new failed", __func__);
1771 if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
1772 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
1775 if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
1776 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1777 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1778 (r = sshbuf_put_cstring(b, service)) != 0 ||
1779 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1780 (r = sshbuf_put_cstring(b, key_ssh_name(private))) != 0 ||
1781 (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
1782 (r = sshbuf_put_cstring(b, chost)) != 0 ||
1783 (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
1784 error("%s: buffer error: %s", __func__, ssh_err(r));
1789 sshbuf_dump(b, stderr);
1791 if (authctxt->sensitive->external_keysign)
1792 r = ssh_keysign(private, &sig, &siglen,
1793 sshbuf_ptr(b), sshbuf_len(b));
1794 else if ((r = sshkey_sign(private, &sig, &siglen,
1795 sshbuf_ptr(b), sshbuf_len(b), NULL, datafellows)) != 0)
1796 debug("%s: sshkey_sign: %s", __func__, ssh_err(r));
1798 error("sign using hostkey %s %s failed",
1799 sshkey_ssh_name(private), fp);
1802 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1803 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1804 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1805 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1806 (r = sshpkt_put_cstring(ssh, key_ssh_name(private))) != 0 ||
1807 (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
1808 (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
1809 (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
1810 (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
1811 (r = sshpkt_send(ssh)) != 0) {
1812 error("%s: packet error: %s", __func__, ssh_err(r));
1819 explicit_bzero(sig, siglen);
1826 sshkey_free(private);
1832 /* find auth method */
1835 * given auth method name, if configurable options permit this method fill
1836 * in auth_ident field and return true, otherwise return false.
1839 authmethod_is_enabled(Authmethod *method)
1843 /* return false if options indicate this method is disabled */
1844 if (method->enabled == NULL || *method->enabled == 0)
1846 /* return false if batch mode is enabled but method needs interactive mode */
1847 if (method->batch_flag != NULL && *method->batch_flag != 0)
1853 authmethod_lookup(const char *name)
1855 Authmethod *method = NULL;
1857 for (method = authmethods; method->name != NULL; method++)
1858 if (strcmp(name, method->name) == 0)
1860 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1864 /* XXX internal state */
1865 static Authmethod *current = NULL;
1866 static char *supported = NULL;
1867 static char *preferred = NULL;
1870 * Given the authentication method list sent by the server, return the
1871 * next method we should try. If the server initially sends a nil list,
1872 * use a built-in default list.
1875 authmethod_get(char *authlist)
1880 /* Use a suitable default if we're passed a nil list. */
1881 if (authlist == NULL || strlen(authlist) == 0)
1882 authlist = options.preferred_authentications;
1884 if (supported == NULL || strcmp(authlist, supported) != 0) {
1885 debug3("start over, passed a different list %s", authlist);
1887 supported = xstrdup(authlist);
1888 preferred = options.preferred_authentications;
1889 debug3("preferred %s", preferred);
1891 } else if (current != NULL && authmethod_is_enabled(current))
1895 if ((name = match_list(preferred, supported, &next)) == NULL) {
1896 debug("No more authentication methods to try.");
1901 debug3("authmethod_lookup %s", name);
1902 debug3("remaining preferred: %s", preferred);
1903 if ((current = authmethod_lookup(name)) != NULL &&
1904 authmethod_is_enabled(current)) {
1905 debug3("authmethod_is_enabled %s", name);
1906 debug("Next authentication method: %s", name);
1915 authmethods_get(void)
1917 Authmethod *method = NULL;
1922 for (method = authmethods; method->name != NULL; method++) {
1923 if (authmethod_is_enabled(method)) {
1924 if (buffer_len(&b) > 0)
1925 buffer_append(&b, ",", 1);
1926 buffer_append(&b, method->name, strlen(method->name));
1929 buffer_append(&b, "\0", 1);
1930 list = xstrdup(buffer_ptr(&b));