1 /* $OpenBSD: sshconnect2.c,v 1.361 2022/09/17 10:33:18 djm Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Copyright (c) 2008 Damien Miller. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
30 #include <sys/socket.h>
44 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
48 #include "openbsd-compat/sys-queue.h"
59 #include "myproposal.h"
60 #include "sshconnect.h"
71 #include "pathnames.h"
84 extern char *client_version_string
;
85 extern char *server_version_string
;
86 extern Options options
;
92 static char *xxx_host
;
93 static struct sockaddr
*xxx_hostaddr
;
94 static const struct ssh_conn_info
*xxx_conn_info
;
97 verify_host_key_callback(struct sshkey
*hostkey
, struct ssh
*ssh
)
101 if ((r
= sshkey_check_rsa_length(hostkey
,
102 options
.required_rsa_size
)) != 0)
103 fatal_r(r
, "Bad server host key");
104 if (verify_host_key(xxx_host
, xxx_hostaddr
, hostkey
,
105 xxx_conn_info
) == -1)
106 fatal("Host key verification failed.");
110 /* Returns the first item from a comma-separated algorithm list */
112 first_alg(const char *algs
)
117 if ((cp
= strchr(ret
, ',')) != NULL
)
123 order_hostkeyalgs(char *host
, struct sockaddr
*hostaddr
, u_short port
,
124 const struct ssh_conn_info
*cinfo
)
126 char *oavail
= NULL
, *avail
= NULL
, *first
= NULL
, *last
= NULL
;
127 char *alg
= NULL
, *hostname
= NULL
, *ret
= NULL
, *best
= NULL
;
129 struct hostkeys
*hostkeys
= NULL
;
133 /* Find all hostkeys for this hostname */
134 get_hostfile_hostname_ipaddr(host
, hostaddr
, port
, &hostname
, NULL
);
135 hostkeys
= init_hostkeys();
136 for (i
= 0; i
< options
.num_user_hostfiles
; i
++)
137 load_hostkeys(hostkeys
, hostname
, options
.user_hostfiles
[i
], 0);
138 for (i
= 0; i
< options
.num_system_hostfiles
; i
++) {
139 load_hostkeys(hostkeys
, hostname
,
140 options
.system_hostfiles
[i
], 0);
142 if (options
.known_hosts_command
!= NULL
) {
143 load_hostkeys_command(hostkeys
, options
.known_hosts_command
,
144 "ORDER", cinfo
, NULL
, host
);
147 * If a plain public key exists that matches the type of the best
148 * preference HostkeyAlgorithms, then use the whole list as is.
149 * Note that we ignore whether the best preference algorithm is a
150 * certificate type, as sshconnect.c will downgrade certs to
151 * plain keys if necessary.
153 best
= first_alg(options
.hostkeyalgorithms
);
154 if (lookup_key_in_hostkeys_by_type(hostkeys
,
155 sshkey_type_plain(sshkey_type_from_name(best
)),
156 sshkey_ecdsa_nid_from_name(best
), NULL
)) {
157 debug3_f("have matching best-preference key type %s, "
158 "using HostkeyAlgorithms verbatim", best
);
159 ret
= xstrdup(options
.hostkeyalgorithms
);
164 * Otherwise, prefer the host key algorithms that match known keys
165 * while keeping the ordering of HostkeyAlgorithms as much as possible.
167 oavail
= avail
= xstrdup(options
.hostkeyalgorithms
);
168 maxlen
= strlen(avail
) + 1;
169 first
= xmalloc(maxlen
);
170 last
= xmalloc(maxlen
);
171 *first
= *last
= '\0';
173 #define ALG_APPEND(to, from) \
176 strlcat(to, ",", maxlen); \
177 strlcat(to, from, maxlen); \
180 while ((alg
= strsep(&avail
, ",")) && *alg
!= '\0') {
181 if ((ktype
= sshkey_type_from_name(alg
)) == KEY_UNSPEC
)
182 fatal_f("unknown alg %s", alg
);
184 * If we have a @cert-authority marker in known_hosts then
185 * prefer all certificate algorithms.
187 if (sshkey_type_is_cert(ktype
) &&
188 lookup_marker_in_hostkeys(hostkeys
, MRK_CA
)) {
189 ALG_APPEND(first
, alg
);
192 /* If the key appears in known_hosts then prefer it */
193 if (lookup_key_in_hostkeys_by_type(hostkeys
,
194 sshkey_type_plain(ktype
),
195 sshkey_ecdsa_nid_from_name(alg
), NULL
)) {
196 ALG_APPEND(first
, alg
);
199 /* Otherwise, put it last */
200 ALG_APPEND(last
, alg
);
203 xasprintf(&ret
, "%s%s%s", first
,
204 (*first
== '\0' || *last
== '\0') ? "" : ",", last
);
206 debug3_f("prefer hostkeyalgs: %s", first
);
208 debug3_f("no algorithms matched; accept original");
215 free_hostkeys(hostkeys
);
221 ssh_kex2(struct ssh
*ssh
, char *host
, struct sockaddr
*hostaddr
, u_short port
,
222 const struct ssh_conn_info
*cinfo
)
224 char *myproposal
[PROPOSAL_MAX
] = { KEX_CLIENT
};
226 char *prop_kex
= NULL
, *prop_enc
= NULL
, *prop_hostkey
= NULL
;
227 int r
, use_known_hosts_order
= 0;
230 xxx_hostaddr
= hostaddr
;
231 xxx_conn_info
= cinfo
;
234 * If the user has not specified HostkeyAlgorithms, or has only
235 * appended or removed algorithms from that list then prefer algorithms
236 * that are in the list that are supported by known_hosts keys.
238 if (options
.hostkeyalgorithms
== NULL
||
239 options
.hostkeyalgorithms
[0] == '-' ||
240 options
.hostkeyalgorithms
[0] == '+')
241 use_known_hosts_order
= 1;
243 /* Expand or fill in HostkeyAlgorithms */
244 all_key
= sshkey_alg_list(0, 0, 1, ',');
245 if ((r
= kex_assemble_names(&options
.hostkeyalgorithms
,
246 kex_default_pk_alg(), all_key
)) != 0)
247 fatal_fr(r
, "kex_assemble_namelist");
250 if ((s
= kex_names_cat(options
.kex_algorithms
, "ext-info-c")) == NULL
)
251 fatal_f("kex_names_cat");
252 myproposal
[PROPOSAL_KEX_ALGS
] = prop_kex
= compat_kex_proposal(ssh
, s
);
253 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
254 myproposal
[PROPOSAL_ENC_ALGS_STOC
] = prop_enc
=
255 compat_cipher_proposal(ssh
, options
.ciphers
);
256 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
257 myproposal
[PROPOSAL_COMP_ALGS_STOC
] =
258 (char *)compression_alg_list(options
.compression
);
259 myproposal
[PROPOSAL_MAC_ALGS_CTOS
] =
260 myproposal
[PROPOSAL_MAC_ALGS_STOC
] = options
.macs
;
261 if (use_known_hosts_order
) {
262 /* Query known_hosts and prefer algorithms that appear there */
263 myproposal
[PROPOSAL_SERVER_HOST_KEY_ALGS
] = prop_hostkey
=
264 compat_pkalg_proposal(ssh
,
265 order_hostkeyalgs(host
, hostaddr
, port
, cinfo
));
267 /* Use specified HostkeyAlgorithms exactly */
268 myproposal
[PROPOSAL_SERVER_HOST_KEY_ALGS
] = prop_hostkey
=
269 compat_pkalg_proposal(ssh
, options
.hostkeyalgorithms
);
272 if (options
.rekey_limit
|| options
.rekey_interval
)
273 ssh_packet_set_rekey_limits(ssh
, options
.rekey_limit
,
274 options
.rekey_interval
);
276 /* start key exchange */
277 if ((r
= kex_setup(ssh
, myproposal
)) != 0)
278 fatal_r(r
, "kex_setup");
280 ssh
->kex
->kex
[KEX_DH_GRP1_SHA1
] = kex_gen_client
;
281 ssh
->kex
->kex
[KEX_DH_GRP14_SHA1
] = kex_gen_client
;
282 ssh
->kex
->kex
[KEX_DH_GRP14_SHA256
] = kex_gen_client
;
283 ssh
->kex
->kex
[KEX_DH_GRP16_SHA512
] = kex_gen_client
;
284 ssh
->kex
->kex
[KEX_DH_GRP18_SHA512
] = kex_gen_client
;
285 ssh
->kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_client
;
286 ssh
->kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_client
;
287 # ifdef OPENSSL_HAS_ECC
288 ssh
->kex
->kex
[KEX_ECDH_SHA2
] = kex_gen_client
;
291 ssh
->kex
->kex
[KEX_C25519_SHA256
] = kex_gen_client
;
292 ssh
->kex
->kex
[KEX_KEM_SNTRUP761X25519_SHA512
] = kex_gen_client
;
293 ssh
->kex
->verify_host_key
=&verify_host_key_callback
;
295 ssh_dispatch_run_fatal(ssh
, DISPATCH_BLOCK
, &ssh
->kex
->done
);
297 /* remove ext-info from the KEX proposals for rekeying */
298 myproposal
[PROPOSAL_KEX_ALGS
] =
299 compat_kex_proposal(ssh
, options
.kex_algorithms
);
300 if ((r
= kex_prop2buf(ssh
->kex
->my
, myproposal
)) != 0)
301 fatal_r(r
, "kex_prop2buf");
304 /* send 1st encrypted/maced/compressed message */
305 if ((r
= sshpkt_start(ssh
, SSH2_MSG_IGNORE
)) != 0 ||
306 (r
= sshpkt_put_cstring(ssh
, "markus")) != 0 ||
307 (r
= sshpkt_send(ssh
)) != 0 ||
308 (r
= ssh_packet_write_wait(ssh
)) != 0)
309 fatal_fr(r
, "send packet");
311 /* Free only parts of proposal that were dynamically allocated here. */
321 typedef struct cauthctxt Authctxt
;
322 typedef struct cauthmethod Authmethod
;
323 typedef struct identity Identity
;
324 typedef struct idlist Idlist
;
327 TAILQ_ENTRY(identity
) next
;
328 int agent_fd
; /* >=0 if agent supports key */
329 struct sshkey
*key
; /* public/private key */
330 char *filename
; /* comment for agent-only keys */
332 int isprivate
; /* key points to the private key */
335 TAILQ_HEAD(idlist
, identity
);
338 const char *server_user
;
339 const char *local_user
;
342 struct cauthmethod
*method
;
343 sig_atomic_t success
;
347 gss_OID_set gss_supported_mechs
;
354 Sensitive
*sensitive
;
355 char *oktypes
, *ktypes
;
356 const char *active_ktype
;
357 /* kbd-interactive */
367 char *name
; /* string to compare against server's list */
368 int (*userauth
)(struct ssh
*ssh
);
369 void (*cleanup
)(struct ssh
*ssh
);
370 int *enabled
; /* flag in option struct that enables method */
371 int *batch_flag
; /* flag in option struct that disables method */
374 static int input_userauth_service_accept(int, u_int32_t
, struct ssh
*);
375 static int input_userauth_ext_info(int, u_int32_t
, struct ssh
*);
376 static int input_userauth_success(int, u_int32_t
, struct ssh
*);
377 static int input_userauth_failure(int, u_int32_t
, struct ssh
*);
378 static int input_userauth_banner(int, u_int32_t
, struct ssh
*);
379 static int input_userauth_error(int, u_int32_t
, struct ssh
*);
380 static int input_userauth_info_req(int, u_int32_t
, struct ssh
*);
381 static int input_userauth_pk_ok(int, u_int32_t
, struct ssh
*);
382 static int input_userauth_passwd_changereq(int, u_int32_t
, struct ssh
*);
384 static int userauth_none(struct ssh
*);
385 static int userauth_pubkey(struct ssh
*);
386 static int userauth_passwd(struct ssh
*);
387 static int userauth_kbdint(struct ssh
*);
388 static int userauth_hostbased(struct ssh
*);
391 static int userauth_gssapi(struct ssh
*);
392 static void userauth_gssapi_cleanup(struct ssh
*);
393 static int input_gssapi_response(int type
, u_int32_t
, struct ssh
*);
394 static int input_gssapi_token(int type
, u_int32_t
, struct ssh
*);
395 static int input_gssapi_error(int, u_int32_t
, struct ssh
*);
396 static int input_gssapi_errtok(int, u_int32_t
, struct ssh
*);
399 void userauth(struct ssh
*, char *);
401 static void pubkey_cleanup(struct ssh
*);
402 static int sign_and_send_pubkey(struct ssh
*ssh
, Identity
*);
403 static void pubkey_prepare(struct ssh
*, Authctxt
*);
404 static void pubkey_reset(Authctxt
*);
405 static struct sshkey
*load_identity_file(Identity
*);
407 static Authmethod
*authmethod_get(char *authlist
);
408 static Authmethod
*authmethod_lookup(const char *name
);
409 static char *authmethods_get(void);
411 Authmethod authmethods
[] = {
415 userauth_gssapi_cleanup
,
416 &options
.gss_authentication
,
422 &options
.hostbased_authentication
,
427 &options
.pubkey_authentication
,
429 {"keyboard-interactive",
432 &options
.kbd_interactive_authentication
,
433 &options
.batch_mode
},
437 &options
.password_authentication
,
438 &options
.batch_mode
},
444 {NULL
, NULL
, NULL
, NULL
, NULL
}
448 ssh_userauth2(struct ssh
*ssh
, const char *local_user
,
449 const char *server_user
, char *host
, Sensitive
*sensitive
)
454 if (options
.preferred_authentications
== NULL
)
455 options
.preferred_authentications
= authmethods_get();
457 /* setup authentication context */
458 memset(&authctxt
, 0, sizeof(authctxt
));
459 authctxt
.server_user
= server_user
;
460 authctxt
.local_user
= local_user
;
461 authctxt
.host
= host
;
462 authctxt
.service
= "ssh-connection"; /* service name */
463 authctxt
.success
= 0;
464 authctxt
.method
= authmethod_lookup("none");
465 authctxt
.authlist
= NULL
;
466 authctxt
.methoddata
= NULL
;
467 authctxt
.sensitive
= sensitive
;
468 authctxt
.active_ktype
= authctxt
.oktypes
= authctxt
.ktypes
= NULL
;
469 authctxt
.info_req_seen
= 0;
470 authctxt
.attempt_kbdint
= 0;
471 authctxt
.attempt_passwd
= 0;
473 authctxt
.gss_supported_mechs
= NULL
;
474 authctxt
.mech_tried
= 0;
476 authctxt
.agent_fd
= -1;
477 pubkey_prepare(ssh
, &authctxt
);
478 if (authctxt
.method
== NULL
) {
479 fatal_f("internal error: cannot send userauth none request");
482 if ((r
= sshpkt_start(ssh
, SSH2_MSG_SERVICE_REQUEST
)) != 0 ||
483 (r
= sshpkt_put_cstring(ssh
, "ssh-userauth")) != 0 ||
484 (r
= sshpkt_send(ssh
)) != 0)
485 fatal_fr(r
, "send packet");
487 ssh
->authctxt
= &authctxt
;
488 ssh_dispatch_init(ssh
, &input_userauth_error
);
489 ssh_dispatch_set(ssh
, SSH2_MSG_EXT_INFO
, &input_userauth_ext_info
);
490 ssh_dispatch_set(ssh
, SSH2_MSG_SERVICE_ACCEPT
, &input_userauth_service_accept
);
491 ssh_dispatch_run_fatal(ssh
, DISPATCH_BLOCK
, &authctxt
.success
); /* loop until success */
493 ssh
->authctxt
= NULL
;
495 ssh_dispatch_range(ssh
, SSH2_MSG_USERAUTH_MIN
, SSH2_MSG_USERAUTH_MAX
, NULL
);
497 if (!authctxt
.success
)
498 fatal("Authentication failed.");
499 if (ssh_packet_connection_is_on_socket(ssh
)) {
500 verbose("Authenticated to %s ([%s]:%d) using \"%s\".", host
,
501 ssh_remote_ipaddr(ssh
), ssh_remote_port(ssh
),
502 authctxt
.method
->name
);
504 verbose("Authenticated to %s (via proxy) using \"%s\".", host
,
505 authctxt
.method
->name
);
511 input_userauth_service_accept(int type
, u_int32_t seq
, struct ssh
*ssh
)
515 if (ssh_packet_remaining(ssh
) > 0) {
518 if ((r
= sshpkt_get_cstring(ssh
, &reply
, NULL
)) != 0)
520 debug2("service_accept: %s", reply
);
523 debug2("buggy server: service_accept w/o service");
525 if ((r
= sshpkt_get_end(ssh
)) != 0)
527 debug("SSH2_MSG_SERVICE_ACCEPT received");
529 /* initial userauth request */
532 ssh_dispatch_set(ssh
, SSH2_MSG_EXT_INFO
, &input_userauth_error
);
533 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_SUCCESS
, &input_userauth_success
);
534 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_FAILURE
, &input_userauth_failure
);
535 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_BANNER
, &input_userauth_banner
);
543 input_userauth_ext_info(int type
, u_int32_t seqnr
, struct ssh
*ssh
)
545 return kex_input_ext_info(type
, seqnr
, ssh
);
549 userauth(struct ssh
*ssh
, char *authlist
)
551 Authctxt
*authctxt
= (Authctxt
*)ssh
->authctxt
;
553 if (authctxt
->method
!= NULL
&& authctxt
->method
->cleanup
!= NULL
)
554 authctxt
->method
->cleanup(ssh
);
556 free(authctxt
->methoddata
);
557 authctxt
->methoddata
= NULL
;
558 if (authlist
== NULL
) {
559 authlist
= authctxt
->authlist
;
561 free(authctxt
->authlist
);
562 authctxt
->authlist
= authlist
;
565 Authmethod
*method
= authmethod_get(authlist
);
567 fatal("%s@%s: Permission denied (%s).",
568 authctxt
->server_user
, authctxt
->host
, authlist
);
569 authctxt
->method
= method
;
571 /* reset the per method handler */
572 ssh_dispatch_range(ssh
, SSH2_MSG_USERAUTH_PER_METHOD_MIN
,
573 SSH2_MSG_USERAUTH_PER_METHOD_MAX
, NULL
);
575 /* and try new method */
576 if (method
->userauth(ssh
) != 0) {
577 debug2("we sent a %s packet, wait for reply", method
->name
);
580 debug2("we did not send a packet, disable method");
581 method
->enabled
= NULL
;
588 input_userauth_error(int type
, u_int32_t seq
, struct ssh
*ssh
)
590 fatal_f("bad message during authentication: type %d", type
);
596 input_userauth_banner(int type
, u_int32_t seq
, struct ssh
*ssh
)
602 debug3_f("entering");
603 if ((r
= sshpkt_get_cstring(ssh
, &msg
, &len
)) != 0 ||
604 (r
= sshpkt_get_cstring(ssh
, NULL
, NULL
)) != 0)
606 if (len
> 0 && options
.log_level
>= SYSLOG_LEVEL_INFO
)
607 fmprintf(stderr
, "%s", msg
);
616 input_userauth_success(int type
, u_int32_t seq
, struct ssh
*ssh
)
618 Authctxt
*authctxt
= ssh
->authctxt
;
620 if (authctxt
== NULL
)
621 fatal_f("no authentication context");
622 free(authctxt
->authlist
);
623 authctxt
->authlist
= NULL
;
624 if (authctxt
->method
!= NULL
&& authctxt
->method
->cleanup
!= NULL
)
625 authctxt
->method
->cleanup(ssh
);
626 free(authctxt
->methoddata
);
627 authctxt
->methoddata
= NULL
;
628 authctxt
->success
= 1; /* break out */
634 input_userauth_success_unexpected(int type
, u_int32_t seq
, struct ssh
*ssh
)
636 Authctxt
*authctxt
= ssh
->authctxt
;
638 if (authctxt
== NULL
)
639 fatal_f("no authentication context");
641 fatal("Unexpected authentication success during %s.",
642 authctxt
->method
->name
);
649 input_userauth_failure(int type
, u_int32_t seq
, struct ssh
*ssh
)
651 Authctxt
*authctxt
= ssh
->authctxt
;
652 char *authlist
= NULL
;
655 if (authctxt
== NULL
)
656 fatal("input_userauth_failure: no authentication context");
658 if (sshpkt_get_cstring(ssh
, &authlist
, NULL
) != 0 ||
659 sshpkt_get_u8(ssh
, &partial
) != 0 ||
660 sshpkt_get_end(ssh
) != 0)
664 verbose("Authenticated using \"%s\" with partial success.",
665 authctxt
->method
->name
);
667 pubkey_reset(authctxt
);
669 debug("Authentications that can continue: %s", authlist
);
671 userauth(ssh
, authlist
);
679 * Format an identity for logging including filename, key type, fingerprint
680 * and location (agent, etc.). Caller must free.
683 format_identity(Identity
*id
)
685 char *fp
= NULL
, *ret
= NULL
;
686 const char *note
= "";
688 if (id
->key
!= NULL
) {
689 fp
= sshkey_fingerprint(id
->key
, options
.fingerprint_hash
,
693 if ((id
->key
->flags
& SSHKEY_FLAG_EXT
) != 0)
695 else if (sshkey_is_sk(id
->key
))
696 note
= " authenticator";
698 xasprintf(&ret
, "%s %s%s%s%s%s%s",
700 id
->key
? sshkey_type(id
->key
) : "", id
->key
? " " : "",
702 id
->userprovided
? " explicit" : "", note
,
703 id
->agent_fd
!= -1 ? " agent" : "");
710 input_userauth_pk_ok(int type
, u_int32_t seq
, struct ssh
*ssh
)
712 Authctxt
*authctxt
= ssh
->authctxt
;
713 struct sshkey
*key
= NULL
;
715 int pktype
, found
= 0, sent
= 0;
717 char *pkalg
= NULL
, *fp
= NULL
, *ident
= NULL
;
718 u_char
*pkblob
= NULL
;
721 if (authctxt
== NULL
)
722 fatal("input_userauth_pk_ok: no authentication context");
724 if ((r
= sshpkt_get_cstring(ssh
, &pkalg
, NULL
)) != 0 ||
725 (r
= sshpkt_get_string(ssh
, &pkblob
, &blen
)) != 0 ||
726 (r
= sshpkt_get_end(ssh
)) != 0)
729 if ((pktype
= sshkey_type_from_name(pkalg
)) == KEY_UNSPEC
) {
730 debug_f("server sent unknown pkalg %s", pkalg
);
733 if ((r
= sshkey_from_blob(pkblob
, blen
, &key
)) != 0) {
734 debug_r(r
, "no key from blob. pkalg %s", pkalg
);
737 if (key
->type
!= pktype
) {
738 error("input_userauth_pk_ok: type mismatch "
739 "for decoded key (received %d, expected %d)",
745 * search keys in the reverse order, because last candidate has been
746 * moved to the end of the queue. this also avoids confusion by
749 TAILQ_FOREACH_REVERSE(id
, &authctxt
->keys
, idlist
, next
) {
750 if (sshkey_equal(key
, id
->key
)) {
755 if (!found
|| id
== NULL
) {
756 fp
= sshkey_fingerprint(key
, options
.fingerprint_hash
,
758 error_f("server replied with unknown key: %s %s",
759 sshkey_type(key
), fp
== NULL
? "<ERROR>" : fp
);
762 ident
= format_identity(id
);
763 debug("Server accepts key: %s", ident
);
764 sent
= sign_and_send_pubkey(ssh
, id
);
773 /* try another method if we did not send a packet */
774 if (r
== 0 && sent
== 0)
781 userauth_gssapi(struct ssh
*ssh
)
783 Authctxt
*authctxt
= (Authctxt
*)ssh
->authctxt
;
784 Gssctxt
*gssctxt
= NULL
;
789 /* Try one GSSAPI method at a time, rather than sending them all at
792 if (authctxt
->gss_supported_mechs
== NULL
)
793 gss_indicate_mechs(&min
, &authctxt
->gss_supported_mechs
);
795 /* Check to see whether the mechanism is usable before we offer it */
796 while (authctxt
->mech_tried
< authctxt
->gss_supported_mechs
->count
&&
798 mech
= &authctxt
->gss_supported_mechs
->
799 elements
[authctxt
->mech_tried
];
800 /* My DER encoding requires length<128 */
801 if (mech
->length
< 128 && ssh_gssapi_check_mechanism(&gssctxt
,
802 mech
, authctxt
->host
)) {
803 ok
= 1; /* Mechanism works */
805 authctxt
->mech_tried
++;
809 if (!ok
|| mech
== NULL
)
812 authctxt
->methoddata
=(void *)gssctxt
;
814 if ((r
= sshpkt_start(ssh
, SSH2_MSG_USERAUTH_REQUEST
)) != 0 ||
815 (r
= sshpkt_put_cstring(ssh
, authctxt
->server_user
)) != 0 ||
816 (r
= sshpkt_put_cstring(ssh
, authctxt
->service
)) != 0 ||
817 (r
= sshpkt_put_cstring(ssh
, authctxt
->method
->name
)) != 0 ||
818 (r
= sshpkt_put_u32(ssh
, 1)) != 0 ||
819 (r
= sshpkt_put_u32(ssh
, (mech
->length
) + 2)) != 0 ||
820 (r
= sshpkt_put_u8(ssh
, SSH_GSS_OIDTYPE
)) != 0 ||
821 (r
= sshpkt_put_u8(ssh
, mech
->length
)) != 0 ||
822 (r
= sshpkt_put(ssh
, mech
->elements
, mech
->length
)) != 0 ||
823 (r
= sshpkt_send(ssh
)) != 0)
824 fatal_fr(r
, "send packet");
826 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE
, &input_gssapi_response
);
827 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, &input_gssapi_token
);
828 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_GSSAPI_ERROR
, &input_gssapi_error
);
829 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
, &input_gssapi_errtok
);
831 authctxt
->mech_tried
++; /* Move along to next candidate */
837 userauth_gssapi_cleanup(struct ssh
*ssh
)
839 Authctxt
*authctxt
= (Authctxt
*)ssh
->authctxt
;
840 Gssctxt
*gssctxt
= (Gssctxt
*)authctxt
->methoddata
;
842 ssh_gssapi_delete_ctx(&gssctxt
);
843 authctxt
->methoddata
= NULL
;
845 free(authctxt
->gss_supported_mechs
);
846 authctxt
->gss_supported_mechs
= NULL
;
850 process_gssapi_token(struct ssh
*ssh
, gss_buffer_t recv_tok
)
852 Authctxt
*authctxt
= ssh
->authctxt
;
853 Gssctxt
*gssctxt
= authctxt
->methoddata
;
854 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
855 gss_buffer_desc mic
= GSS_C_EMPTY_BUFFER
;
856 gss_buffer_desc gssbuf
;
857 OM_uint32 status
, ms
, flags
;
860 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
861 recv_tok
, &send_tok
, &flags
);
863 if (send_tok
.length
> 0) {
864 u_char type
= GSS_ERROR(status
) ?
865 SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
:
866 SSH2_MSG_USERAUTH_GSSAPI_TOKEN
;
868 if ((r
= sshpkt_start(ssh
, type
)) != 0 ||
869 (r
= sshpkt_put_string(ssh
, send_tok
.value
,
870 send_tok
.length
)) != 0 ||
871 (r
= sshpkt_send(ssh
)) != 0)
872 fatal_fr(r
, "send %u packet", type
);
874 gss_release_buffer(&ms
, &send_tok
);
877 if (status
== GSS_S_COMPLETE
) {
878 /* send either complete or MIC, depending on mechanism */
879 if (!(flags
& GSS_C_INTEG_FLAG
)) {
880 if ((r
= sshpkt_start(ssh
,
881 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
)) != 0 ||
882 (r
= sshpkt_send(ssh
)) != 0)
883 fatal_fr(r
, "send completion");
887 if ((b
= sshbuf_new()) == NULL
)
888 fatal_f("sshbuf_new failed");
889 ssh_gssapi_buildmic(b
, authctxt
->server_user
,
890 authctxt
->service
, "gssapi-with-mic",
891 ssh
->kex
->session_id
);
893 if ((gssbuf
.value
= sshbuf_mutable_ptr(b
)) == NULL
)
894 fatal_f("sshbuf_mutable_ptr failed");
895 gssbuf
.length
= sshbuf_len(b
);
897 status
= ssh_gssapi_sign(gssctxt
, &gssbuf
, &mic
);
899 if (!GSS_ERROR(status
)) {
900 if ((r
= sshpkt_start(ssh
,
901 SSH2_MSG_USERAUTH_GSSAPI_MIC
)) != 0 ||
902 (r
= sshpkt_put_string(ssh
, mic
.value
,
904 (r
= sshpkt_send(ssh
)) != 0)
905 fatal_fr(r
, "send MIC");
909 gss_release_buffer(&ms
, &mic
);
918 input_gssapi_response(int type
, u_int32_t plen
, struct ssh
*ssh
)
920 Authctxt
*authctxt
= ssh
->authctxt
;
926 if (authctxt
== NULL
)
927 fatal("input_gssapi_response: no authentication context");
928 gssctxt
= authctxt
->methoddata
;
931 if ((r
= sshpkt_get_string(ssh
, &oidv
, &oidlen
)) != 0)
935 oidv
[0] != SSH_GSS_OIDTYPE
||
936 oidv
[1] != oidlen
- 2) {
937 debug("Badly encoded mechanism OID received");
942 if (!ssh_gssapi_check_oid(gssctxt
, oidv
+ 2, oidlen
- 2))
943 fatal("Server returned different OID than expected");
945 if ((r
= sshpkt_get_end(ssh
)) != 0)
948 if (GSS_ERROR(process_gssapi_token(ssh
, GSS_C_NO_BUFFER
))) {
949 /* Start again with next method on list */
950 debug("Trying to start again");
963 input_gssapi_token(int type
, u_int32_t plen
, struct ssh
*ssh
)
965 Authctxt
*authctxt
= ssh
->authctxt
;
966 gss_buffer_desc recv_tok
;
972 if (authctxt
== NULL
)
973 fatal("input_gssapi_response: no authentication context");
975 if ((r
= sshpkt_get_string(ssh
, &p
, &len
)) != 0 ||
976 (r
= sshpkt_get_end(ssh
)) != 0)
980 recv_tok
.length
= len
;
981 status
= process_gssapi_token(ssh
, &recv_tok
);
983 /* Start again with the next method in the list */
984 if (GSS_ERROR(status
)) {
996 input_gssapi_errtok(int type
, u_int32_t plen
, struct ssh
*ssh
)
998 Authctxt
*authctxt
= ssh
->authctxt
;
1000 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
1001 gss_buffer_desc recv_tok
;
1007 if (authctxt
== NULL
)
1008 fatal("input_gssapi_response: no authentication context");
1009 gssctxt
= authctxt
->methoddata
;
1011 if ((r
= sshpkt_get_string(ssh
, &p
, &len
)) != 0 ||
1012 (r
= sshpkt_get_end(ssh
)) != 0) {
1017 /* Stick it into GSSAPI and see what it says */
1019 recv_tok
.length
= len
;
1020 (void)ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
1021 &recv_tok
, &send_tok
, NULL
);
1023 gss_release_buffer(&ms
, &send_tok
);
1025 /* Server will be returning a failed packet after this one */
1031 input_gssapi_error(int type
, u_int32_t plen
, struct ssh
*ssh
)
1037 if ((r
= sshpkt_get_u32(ssh
, NULL
)) != 0 || /* maj */
1038 (r
= sshpkt_get_u32(ssh
, NULL
)) != 0 || /* min */
1039 (r
= sshpkt_get_cstring(ssh
, &msg
, NULL
)) != 0 ||
1040 (r
= sshpkt_get_cstring(ssh
, &lang
, NULL
)) != 0)
1042 r
= sshpkt_get_end(ssh
);
1043 debug("Server GSSAPI Error:\n%s", msg
);
1052 userauth_none(struct ssh
*ssh
)
1054 Authctxt
*authctxt
= (Authctxt
*)ssh
->authctxt
;
1057 /* initial userauth request */
1058 if ((r
= sshpkt_start(ssh
, SSH2_MSG_USERAUTH_REQUEST
)) != 0 ||
1059 (r
= sshpkt_put_cstring(ssh
, authctxt
->server_user
)) != 0 ||
1060 (r
= sshpkt_put_cstring(ssh
, authctxt
->service
)) != 0 ||
1061 (r
= sshpkt_put_cstring(ssh
, authctxt
->method
->name
)) != 0 ||
1062 (r
= sshpkt_send(ssh
)) != 0)
1063 fatal_fr(r
, "send packet");
1068 userauth_passwd(struct ssh
*ssh
)
1070 Authctxt
*authctxt
= (Authctxt
*)ssh
->authctxt
;
1071 char *password
, *prompt
= NULL
;
1072 const char *host
= options
.host_key_alias
? options
.host_key_alias
:
1076 if (authctxt
->attempt_passwd
++ >= options
.number_of_password_prompts
)
1079 if (authctxt
->attempt_passwd
!= 1)
1080 error("Permission denied, please try again.");
1082 xasprintf(&prompt
, "%s@%s's password: ", authctxt
->server_user
, host
);
1083 password
= read_passphrase(prompt
, 0);
1084 if ((r
= sshpkt_start(ssh
, SSH2_MSG_USERAUTH_REQUEST
)) != 0 ||
1085 (r
= sshpkt_put_cstring(ssh
, authctxt
->server_user
)) != 0 ||
1086 (r
= sshpkt_put_cstring(ssh
, authctxt
->service
)) != 0 ||
1087 (r
= sshpkt_put_cstring(ssh
, authctxt
->method
->name
)) != 0 ||
1088 (r
= sshpkt_put_u8(ssh
, 0)) != 0 ||
1089 (r
= sshpkt_put_cstring(ssh
, password
)) != 0 ||
1090 (r
= sshpkt_add_padding(ssh
, 64)) != 0 ||
1091 (r
= sshpkt_send(ssh
)) != 0)
1092 fatal_fr(r
, "send packet");
1095 if (password
!= NULL
)
1096 freezero(password
, strlen(password
));
1098 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
1099 &input_userauth_passwd_changereq
);
1105 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
1109 input_userauth_passwd_changereq(int type
, u_int32_t seqnr
, struct ssh
*ssh
)
1111 Authctxt
*authctxt
= ssh
->authctxt
;
1112 char *info
= NULL
, *lang
= NULL
, *password
= NULL
, *retype
= NULL
;
1117 debug2("input_userauth_passwd_changereq");
1119 if (authctxt
== NULL
)
1120 fatal("input_userauth_passwd_changereq: "
1121 "no authentication context");
1122 host
= options
.host_key_alias
? options
.host_key_alias
: authctxt
->host
;
1124 if ((r
= sshpkt_get_cstring(ssh
, &info
, NULL
)) != 0 ||
1125 (r
= sshpkt_get_cstring(ssh
, &lang
, NULL
)) != 0)
1127 if (strlen(info
) > 0)
1129 if ((r
= sshpkt_start(ssh
, SSH2_MSG_USERAUTH_REQUEST
)) != 0 ||
1130 (r
= sshpkt_put_cstring(ssh
, authctxt
->server_user
)) != 0 ||
1131 (r
= sshpkt_put_cstring(ssh
, authctxt
->service
)) != 0 ||
1132 (r
= sshpkt_put_cstring(ssh
, authctxt
->method
->name
)) != 0 ||
1133 (r
= sshpkt_put_u8(ssh
, 1)) != 0) /* additional info */
1136 snprintf(prompt
, sizeof(prompt
),
1137 "Enter %.30s@%.128s's old password: ",
1138 authctxt
->server_user
, host
);
1139 password
= read_passphrase(prompt
, 0);
1140 if ((r
= sshpkt_put_cstring(ssh
, password
)) != 0)
1143 freezero(password
, strlen(password
));
1145 while (password
== NULL
) {
1146 snprintf(prompt
, sizeof(prompt
),
1147 "Enter %.30s@%.128s's new password: ",
1148 authctxt
->server_user
, host
);
1149 password
= read_passphrase(prompt
, RP_ALLOW_EOF
);
1150 if (password
== NULL
) {
1155 snprintf(prompt
, sizeof(prompt
),
1156 "Retype %.30s@%.128s's new password: ",
1157 authctxt
->server_user
, host
);
1158 retype
= read_passphrase(prompt
, 0);
1159 if (strcmp(password
, retype
) != 0) {
1160 freezero(password
, strlen(password
));
1161 logit("Mismatch; try again, EOF to quit.");
1164 freezero(retype
, strlen(retype
));
1166 if ((r
= sshpkt_put_cstring(ssh
, password
)) != 0 ||
1167 (r
= sshpkt_add_padding(ssh
, 64)) != 0 ||
1168 (r
= sshpkt_send(ssh
)) != 0)
1171 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
1172 &input_userauth_passwd_changereq
);
1176 freezero(password
, strlen(password
));
1183 * Select an algorithm for publickey signatures.
1184 * Returns algorithm (caller must free) or NULL if no mutual algorithm found.
1186 * Call with ssh==NULL to ignore server-sig-algs extension list and
1187 * only attempt with the key's base signature type.
1190 key_sig_algorithm(struct ssh
*ssh
, const struct sshkey
*key
)
1192 char *allowed
, *oallowed
, *cp
, *tmp
, *alg
= NULL
;
1193 const char *server_sig_algs
;
1196 * The signature algorithm will only differ from the key algorithm
1197 * for RSA keys/certs and when the server advertises support for
1198 * newer (SHA2) algorithms.
1200 if (ssh
== NULL
|| ssh
->kex
->server_sig_algs
== NULL
||
1201 (key
->type
!= KEY_RSA
&& key
->type
!= KEY_RSA_CERT
) ||
1202 (key
->type
== KEY_RSA_CERT
&& (ssh
->compat
& SSH_BUG_SIGTYPE
))) {
1203 /* Filter base key signature alg against our configuration */
1204 return match_list(sshkey_ssh_name(key
),
1205 options
.pubkey_accepted_algos
, NULL
);
1209 * Workaround OpenSSH 7.4 bug: this version supports RSA/SHA-2 but
1210 * fails to advertise it via SSH2_MSG_EXT_INFO.
1212 server_sig_algs
= ssh
->kex
->server_sig_algs
;
1213 if (key
->type
== KEY_RSA
&& (ssh
->compat
& SSH_BUG_SIGTYPE74
))
1214 server_sig_algs
= "rsa-sha2-256,rsa-sha2-512";
1217 * For RSA keys/certs, since these might have a different sig type:
1218 * find the first entry in PubkeyAcceptedAlgorithms of the right type
1219 * that also appears in the supported signature algorithms list from
1222 oallowed
= allowed
= xstrdup(options
.pubkey_accepted_algos
);
1223 while ((cp
= strsep(&allowed
, ",")) != NULL
) {
1224 if (sshkey_type_from_name(cp
) != key
->type
)
1226 tmp
= match_list(sshkey_sigalg_by_name(cp
),
1227 server_sig_algs
, NULL
);
1239 identity_sign(struct identity
*id
, u_char
**sigp
, size_t *lenp
,
1240 const u_char
*data
, size_t datalen
, u_int compat
, const char *alg
)
1242 struct sshkey
*sign_key
= NULL
, *prv
= NULL
;
1243 int is_agent
= 0, retried
= 0, r
= SSH_ERR_INTERNAL_ERROR
;
1244 struct notifier_ctx
*notifier
= NULL
;
1245 char *fp
= NULL
, *pin
= NULL
, *prompt
= NULL
;
1250 /* The agent supports this key. */
1251 if (id
->key
!= NULL
&& id
->agent_fd
!= -1) {
1252 return ssh_agent_sign(id
->agent_fd
, id
->key
, sigp
, lenp
,
1253 data
, datalen
, alg
, compat
);
1257 * We have already loaded the private key or the private key is
1258 * stored in external hardware.
1260 if (id
->key
!= NULL
&&
1261 (id
->isprivate
|| (id
->key
->flags
& SSHKEY_FLAG_EXT
))) {
1265 /* Load the private key from the file. */
1266 if ((prv
= load_identity_file(id
)) == NULL
)
1267 return SSH_ERR_KEY_NOT_FOUND
;
1268 if (id
->key
!= NULL
&& !sshkey_equal_public(prv
, id
->key
)) {
1269 error_f("private key %s contents do not match public",
1271 r
= SSH_ERR_KEY_NOT_FOUND
;
1277 /* Prompt for touch for non-agent FIDO keys that request UP */
1278 if (!is_agent
&& sshkey_is_sk(sign_key
) &&
1279 (sign_key
->sk_flags
& SSH_SK_USER_PRESENCE_REQD
)) {
1280 /* XXX should batch mode just skip these? */
1281 if ((fp
= sshkey_fingerprint(sign_key
,
1282 options
.fingerprint_hash
, SSH_FP_DEFAULT
)) == NULL
)
1283 fatal_f("fingerprint failed");
1284 notifier
= notify_start(options
.batch_mode
,
1285 "Confirm user presence for key %s %s",
1286 sshkey_type(sign_key
), fp
);
1289 if ((r
= sshkey_sign(sign_key
, sigp
, lenp
, data
, datalen
,
1290 alg
, options
.sk_provider
, pin
, compat
)) != 0) {
1291 debug_fr(r
, "sshkey_sign");
1292 if (!retried
&& pin
== NULL
&& !is_agent
&&
1293 sshkey_is_sk(sign_key
) &&
1294 r
== SSH_ERR_KEY_WRONG_PASSPHRASE
) {
1295 notify_complete(notifier
, NULL
);
1297 xasprintf(&prompt
, "Enter PIN for %s key %s: ",
1298 sshkey_type(sign_key
), id
->filename
);
1299 pin
= read_passphrase(prompt
, 0);
1307 * PKCS#11 tokens may not support all signature algorithms,
1308 * so check what we get back.
1310 if ((r
= sshkey_check_sigtype(*sigp
, *lenp
, alg
)) != 0) {
1311 debug_fr(r
, "sshkey_check_sigtype");
1319 freezero(pin
, strlen(pin
));
1320 notify_complete(notifier
, r
== 0 ? "User presence confirmed" : NULL
);
1326 id_filename_matches(Identity
*id
, Identity
*private_id
)
1328 static const char * const suffixes
[] = { ".pub", "-cert.pub", NULL
};
1329 size_t len
= strlen(id
->filename
), plen
= strlen(private_id
->filename
);
1332 if (strcmp(id
->filename
, private_id
->filename
) == 0)
1334 for (i
= 0; suffixes
[i
]; i
++) {
1335 slen
= strlen(suffixes
[i
]);
1336 if (len
> slen
&& plen
== len
- slen
&&
1337 strcmp(id
->filename
+ (len
- slen
), suffixes
[i
]) == 0 &&
1338 memcmp(id
->filename
, private_id
->filename
, plen
) == 0)
1345 sign_and_send_pubkey(struct ssh
*ssh
, Identity
*id
)
1347 Authctxt
*authctxt
= (Authctxt
*)ssh
->authctxt
;
1348 struct sshbuf
*b
= NULL
;
1349 Identity
*private_id
, *sign_id
= NULL
;
1350 u_char
*signature
= NULL
;
1351 size_t slen
= 0, skip
= 0;
1352 int r
, fallback_sigtype
, sent
= 0;
1353 char *alg
= NULL
, *fp
= NULL
;
1354 const char *loc
= "", *method
= "publickey";
1357 /* prefer host-bound pubkey signatures if supported by server */
1358 if ((ssh
->kex
->flags
& KEX_HAS_PUBKEY_HOSTBOUND
) != 0 &&
1359 (options
.pubkey_authentication
& SSH_PUBKEY_AUTH_HBOUND
) != 0) {
1361 method
= "publickey-hostbound-v00@openssh.com";
1364 if ((fp
= sshkey_fingerprint(id
->key
, options
.fingerprint_hash
,
1365 SSH_FP_DEFAULT
)) == NULL
)
1368 debug3_f("using %s with %s %s", method
, sshkey_type(id
->key
), fp
);
1371 * If the key is an certificate, try to find a matching private key
1372 * and use it to complete the signature.
1373 * If no such private key exists, fall back to trying the certificate
1374 * key itself in case it has a private half already loaded.
1375 * This will try to set sign_id to the private key that will perform
1378 if (sshkey_is_cert(id
->key
)) {
1379 TAILQ_FOREACH(private_id
, &authctxt
->keys
, next
) {
1380 if (sshkey_equal_public(id
->key
, private_id
->key
) &&
1381 id
->key
->type
!= private_id
->key
->type
) {
1382 sign_id
= private_id
;
1387 * Exact key matches are preferred, but also allow
1388 * filename matches for non-PKCS#11/agent keys that
1389 * didn't load public keys. This supports the case
1390 * of keeping just a private key file and public
1391 * certificate on disk.
1393 if (sign_id
== NULL
&&
1394 !id
->isprivate
&& id
->agent_fd
== -1 &&
1395 (id
->key
->flags
& SSHKEY_FLAG_EXT
) == 0) {
1396 TAILQ_FOREACH(private_id
, &authctxt
->keys
, next
) {
1397 if (private_id
->key
== NULL
&&
1398 id_filename_matches(id
, private_id
)) {
1399 sign_id
= private_id
;
1404 if (sign_id
!= NULL
) {
1405 debug2_f("using private key \"%s\"%s for "
1406 "certificate", sign_id
->filename
,
1407 sign_id
->agent_fd
!= -1 ? " from agent" : "");
1409 debug_f("no separate private key for certificate "
1410 "\"%s\"", id
->filename
);
1415 * If the above didn't select another identity to do the signing
1416 * then default to the one we started with.
1418 if (sign_id
== NULL
)
1421 /* assemble and sign data */
1422 for (fallback_sigtype
= 0; fallback_sigtype
<= 1; fallback_sigtype
++) {
1426 if ((alg
= key_sig_algorithm(fallback_sigtype
? NULL
: ssh
,
1427 id
->key
)) == NULL
) {
1428 error_f("no mutual signature supported");
1431 debug3_f("signing using %s %s", alg
, fp
);
1434 if ((b
= sshbuf_new()) == NULL
)
1435 fatal_f("sshbuf_new failed");
1436 if (ssh
->compat
& SSH_OLD_SESSIONID
) {
1437 if ((r
= sshbuf_putb(b
, ssh
->kex
->session_id
)) != 0)
1438 fatal_fr(r
, "sshbuf_putb");
1440 if ((r
= sshbuf_put_stringb(b
,
1441 ssh
->kex
->session_id
)) != 0)
1442 fatal_fr(r
, "sshbuf_put_stringb");
1444 skip
= sshbuf_len(b
);
1445 if ((r
= sshbuf_put_u8(b
, SSH2_MSG_USERAUTH_REQUEST
)) != 0 ||
1446 (r
= sshbuf_put_cstring(b
, authctxt
->server_user
)) != 0 ||
1447 (r
= sshbuf_put_cstring(b
, authctxt
->service
)) != 0 ||
1448 (r
= sshbuf_put_cstring(b
, method
)) != 0 ||
1449 (r
= sshbuf_put_u8(b
, 1)) != 0 ||
1450 (r
= sshbuf_put_cstring(b
, alg
)) != 0 ||
1451 (r
= sshkey_puts(id
->key
, b
)) != 0) {
1452 fatal_fr(r
, "assemble signed data");
1455 if (ssh
->kex
->initial_hostkey
== NULL
) {
1456 fatal_f("internal error: initial hostkey "
1459 if ((r
= sshkey_puts(ssh
->kex
->initial_hostkey
, b
)) != 0)
1460 fatal_fr(r
, "assemble %s hostkey", method
);
1462 /* generate signature */
1463 r
= identity_sign(sign_id
, &signature
, &slen
,
1464 sshbuf_ptr(b
), sshbuf_len(b
), ssh
->compat
, alg
);
1467 else if (r
== SSH_ERR_KEY_NOT_FOUND
)
1468 goto out
; /* soft failure */
1469 else if (r
== SSH_ERR_SIGN_ALG_UNSUPPORTED
&&
1470 !fallback_sigtype
) {
1471 if (sign_id
->agent_fd
!= -1)
1473 else if ((sign_id
->key
->flags
& SSHKEY_FLAG_EXT
) != 0)
1475 logit("%skey %s %s returned incorrect signature type",
1476 loc
, sshkey_type(id
->key
), fp
);
1479 error_fr(r
, "signing failed for %s \"%s\"%s",
1480 sshkey_type(sign_id
->key
), sign_id
->filename
,
1481 id
->agent_fd
!= -1 ? " from agent" : "");
1484 if (slen
== 0 || signature
== NULL
) /* shouldn't happen */
1485 fatal_f("no signature");
1487 /* append signature */
1488 if ((r
= sshbuf_put_string(b
, signature
, slen
)) != 0)
1489 fatal_fr(r
, "append signature");
1492 sshbuf_dump(b
, stderr
);
1494 /* skip session id and packet type */
1495 if ((r
= sshbuf_consume(b
, skip
+ 1)) != 0)
1496 fatal_fr(r
, "consume");
1498 /* put remaining data from buffer into packet */
1499 if ((r
= sshpkt_start(ssh
, SSH2_MSG_USERAUTH_REQUEST
)) != 0 ||
1500 (r
= sshpkt_putb(ssh
, b
)) != 0 ||
1501 (r
= sshpkt_send(ssh
)) != 0)
1502 fatal_fr(r
, "enqueue request");
1511 freezero(signature
, slen
);
1516 send_pubkey_test(struct ssh
*ssh
, Identity
*id
)
1518 Authctxt
*authctxt
= (Authctxt
*)ssh
->authctxt
;
1519 u_char
*blob
= NULL
;
1525 if ((alg
= key_sig_algorithm(ssh
, id
->key
)) == NULL
) {
1526 debug_f("no mutual signature algorithm");
1530 if ((r
= sshkey_to_blob(id
->key
, &blob
, &bloblen
)) != 0) {
1531 /* we cannot handle this key */
1532 debug3_f("cannot handle key");
1535 /* register callback for USERAUTH_PK_OK message */
1536 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_PK_OK
, &input_userauth_pk_ok
);
1538 if ((r
= sshpkt_start(ssh
, SSH2_MSG_USERAUTH_REQUEST
)) != 0 ||
1539 (r
= sshpkt_put_cstring(ssh
, authctxt
->server_user
)) != 0 ||
1540 (r
= sshpkt_put_cstring(ssh
, authctxt
->service
)) != 0 ||
1541 (r
= sshpkt_put_cstring(ssh
, authctxt
->method
->name
)) != 0 ||
1542 (r
= sshpkt_put_u8(ssh
, have_sig
)) != 0 ||
1543 (r
= sshpkt_put_cstring(ssh
, alg
)) != 0 ||
1544 (r
= sshpkt_put_string(ssh
, blob
, bloblen
)) != 0 ||
1545 (r
= sshpkt_send(ssh
)) != 0)
1546 fatal_fr(r
, "send packet");
1555 static struct sshkey
*
1556 load_identity_file(Identity
*id
)
1558 struct sshkey
*private = NULL
;
1559 char prompt
[300], *passphrase
, *comment
;
1563 if (stat(id
->filename
, &st
) == -1) {
1564 do_log2(id
->userprovided
?
1565 SYSLOG_LEVEL_INFO
: SYSLOG_LEVEL_DEBUG3
,
1566 "no such identity: %s: %s", id
->filename
, strerror(errno
));
1569 snprintf(prompt
, sizeof prompt
,
1570 "Enter passphrase for key '%.100s': ", id
->filename
);
1571 for (i
= 0; i
<= options
.number_of_password_prompts
; i
++) {
1575 passphrase
= read_passphrase(prompt
, 0);
1576 if (*passphrase
== '\0') {
1577 debug2("no passphrase given, try next key");
1582 switch ((r
= sshkey_load_private_type(KEY_UNSPEC
, id
->filename
,
1583 passphrase
, &private, &comment
))) {
1586 case SSH_ERR_KEY_WRONG_PASSPHRASE
:
1587 if (options
.batch_mode
) {
1592 debug2("bad passphrase given, try again...");
1594 case SSH_ERR_SYSTEM_ERROR
:
1595 if (errno
== ENOENT
) {
1596 debug2_r(r
, "Load key \"%s\"", id
->filename
);
1602 error_r(r
, "Load key \"%s\"", id
->filename
);
1606 if (private != NULL
&& sshkey_is_sk(private) &&
1607 options
.sk_provider
== NULL
) {
1608 debug("key \"%s\" is an authenticator-hosted key, "
1609 "but no provider specified", id
->filename
);
1610 sshkey_free(private);
1614 if (!quit
&& (r
= sshkey_check_rsa_length(private,
1615 options
.required_rsa_size
)) != 0) {
1616 debug_fr(r
, "Skipping key %s", id
->filename
);
1617 sshkey_free(private);
1621 if (!quit
&& private != NULL
&& id
->agent_fd
== -1 &&
1622 !(id
->key
&& id
->isprivate
))
1623 maybe_add_key_to_agent(id
->filename
, private, comment
,
1626 freezero(passphrase
, strlen(passphrase
));
1628 if (private != NULL
|| quit
)
1635 key_type_allowed_by_config(struct sshkey
*key
)
1637 if (match_pattern_list(sshkey_ssh_name(key
),
1638 options
.pubkey_accepted_algos
, 0) == 1)
1641 /* RSA keys/certs might be allowed by alternate signature types */
1642 switch (key
->type
) {
1644 if (match_pattern_list("rsa-sha2-512",
1645 options
.pubkey_accepted_algos
, 0) == 1)
1647 if (match_pattern_list("rsa-sha2-256",
1648 options
.pubkey_accepted_algos
, 0) == 1)
1652 if (match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
1653 options
.pubkey_accepted_algos
, 0) == 1)
1655 if (match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
1656 options
.pubkey_accepted_algos
, 0) == 1)
1663 /* obtain a list of keys from the agent */
1665 get_agent_identities(struct ssh
*ssh
, int *agent_fdp
,
1666 struct ssh_identitylist
**idlistp
)
1669 struct ssh_identitylist
*idlist
;
1671 if ((r
= ssh_get_authentication_socket(&agent_fd
)) != 0) {
1672 if (r
!= SSH_ERR_AGENT_NOT_PRESENT
)
1673 debug_fr(r
, "ssh_get_authentication_socket");
1676 if ((r
= ssh_agent_bind_hostkey(agent_fd
, ssh
->kex
->initial_hostkey
,
1677 ssh
->kex
->session_id
, ssh
->kex
->initial_sig
, 0)) == 0)
1678 debug_f("bound agent to hostkey");
1680 debug2_fr(r
, "ssh_agent_bind_hostkey");
1682 if ((r
= ssh_fetch_identitylist(agent_fd
, &idlist
)) != 0) {
1683 debug_fr(r
, "ssh_fetch_identitylist");
1688 *agent_fdp
= agent_fd
;
1690 debug_f("agent returned %zu keys", idlist
->nkeys
);
1695 * try keys in the following order:
1696 * 1. certificates listed in the config file
1697 * 2. other input certificates
1698 * 3. agent keys that are found in the config file
1699 * 4. other agent keys
1700 * 5. keys that are only listed in the config file
1703 pubkey_prepare(struct ssh
*ssh
, Authctxt
*authctxt
)
1705 struct identity
*id
, *id2
, *tmp
;
1706 struct idlist agent
, files
, *preferred
;
1708 int agent_fd
= -1, i
, r
, found
;
1710 struct ssh_identitylist
*idlist
;
1713 TAILQ_INIT(&agent
); /* keys from the agent */
1714 TAILQ_INIT(&files
); /* keys from the config file */
1715 preferred
= &authctxt
->keys
;
1716 TAILQ_INIT(preferred
); /* preferred order of keys */
1718 /* list of keys stored in the filesystem and PKCS#11 */
1719 for (i
= 0; i
< options
.num_identity_files
; i
++) {
1720 key
= options
.identity_keys
[i
];
1721 if (key
&& key
->cert
&&
1722 key
->cert
->type
!= SSH2_CERT_TYPE_USER
) {
1723 debug_f("ignoring certificate %s: not a user "
1724 "certificate", options
.identity_files
[i
]);
1727 if (key
&& sshkey_is_sk(key
) && options
.sk_provider
== NULL
) {
1728 debug_f("ignoring authenticator-hosted key %s as no "
1729 "SecurityKeyProvider has been specified",
1730 options
.identity_files
[i
]);
1733 options
.identity_keys
[i
] = NULL
;
1734 id
= xcalloc(1, sizeof(*id
));
1737 id
->filename
= xstrdup(options
.identity_files
[i
]);
1738 id
->userprovided
= options
.identity_file_userprovided
[i
];
1739 TAILQ_INSERT_TAIL(&files
, id
, next
);
1741 /* list of certificates specified by user */
1742 for (i
= 0; i
< options
.num_certificate_files
; i
++) {
1743 key
= options
.certificates
[i
];
1744 if (!sshkey_is_cert(key
) || key
->cert
== NULL
||
1745 key
->cert
->type
!= SSH2_CERT_TYPE_USER
) {
1746 debug_f("ignoring certificate %s: not a user "
1747 "certificate", options
.identity_files
[i
]);
1750 if (key
&& sshkey_is_sk(key
) && options
.sk_provider
== NULL
) {
1751 debug_f("ignoring authenticator-hosted key "
1752 "certificate %s as no "
1753 "SecurityKeyProvider has been specified",
1754 options
.identity_files
[i
]);
1757 id
= xcalloc(1, sizeof(*id
));
1760 id
->filename
= xstrdup(options
.certificate_files
[i
]);
1761 id
->userprovided
= options
.certificate_file_userprovided
[i
];
1762 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1764 /* list of keys supported by the agent */
1765 if ((r
= get_agent_identities(ssh
, &agent_fd
, &idlist
)) == 0) {
1766 for (j
= 0; j
< idlist
->nkeys
; j
++) {
1767 if ((r
= sshkey_check_rsa_length(idlist
->keys
[j
],
1768 options
.required_rsa_size
)) != 0) {
1769 debug_fr(r
, "ignoring %s agent key",
1770 sshkey_ssh_name(idlist
->keys
[j
]));
1774 TAILQ_FOREACH(id
, &files
, next
) {
1776 * agent keys from the config file are
1779 if (sshkey_equal(idlist
->keys
[j
], id
->key
)) {
1780 TAILQ_REMOVE(&files
, id
, next
);
1781 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1782 id
->agent_fd
= agent_fd
;
1787 if (!found
&& !options
.identities_only
) {
1788 id
= xcalloc(1, sizeof(*id
));
1789 /* XXX "steals" key/comment from idlist */
1790 id
->key
= idlist
->keys
[j
];
1791 id
->filename
= idlist
->comments
[j
];
1792 idlist
->keys
[j
] = NULL
;
1793 idlist
->comments
[j
] = NULL
;
1794 id
->agent_fd
= agent_fd
;
1795 TAILQ_INSERT_TAIL(&agent
, id
, next
);
1798 ssh_free_identitylist(idlist
);
1799 /* append remaining agent keys */
1800 TAILQ_CONCAT(preferred
, &agent
, next
);
1801 authctxt
->agent_fd
= agent_fd
;
1803 /* Prefer PKCS11 keys that are explicitly listed */
1804 TAILQ_FOREACH_SAFE(id
, &files
, next
, tmp
) {
1805 if (id
->key
== NULL
|| (id
->key
->flags
& SSHKEY_FLAG_EXT
) == 0)
1808 TAILQ_FOREACH(id2
, &files
, next
) {
1809 if (id2
->key
== NULL
||
1810 (id2
->key
->flags
& SSHKEY_FLAG_EXT
) != 0)
1812 if (sshkey_equal(id
->key
, id2
->key
)) {
1813 TAILQ_REMOVE(&files
, id
, next
);
1814 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1819 /* If IdentitiesOnly set and key not found then don't use it */
1820 if (!found
&& options
.identities_only
) {
1821 TAILQ_REMOVE(&files
, id
, next
);
1822 freezero(id
, sizeof(*id
));
1825 /* append remaining keys from the config file */
1826 TAILQ_CONCAT(preferred
, &files
, next
);
1827 /* finally, filter by PubkeyAcceptedAlgorithms */
1828 TAILQ_FOREACH_SAFE(id
, preferred
, next
, id2
) {
1829 if (id
->key
!= NULL
&& !key_type_allowed_by_config(id
->key
)) {
1830 debug("Skipping %s key %s - "
1831 "corresponding algo not in PubkeyAcceptedAlgorithms",
1832 sshkey_ssh_name(id
->key
), id
->filename
);
1833 TAILQ_REMOVE(preferred
, id
, next
);
1834 sshkey_free(id
->key
);
1836 memset(id
, 0, sizeof(*id
));
1840 /* List the keys we plan on using */
1841 TAILQ_FOREACH_SAFE(id
, preferred
, next
, id2
) {
1842 ident
= format_identity(id
);
1843 debug("Will attempt key: %s", ident
);
1850 pubkey_cleanup(struct ssh
*ssh
)
1852 Authctxt
*authctxt
= (Authctxt
*)ssh
->authctxt
;
1855 if (authctxt
->agent_fd
!= -1) {
1856 ssh_close_authentication_socket(authctxt
->agent_fd
);
1857 authctxt
->agent_fd
= -1;
1859 for (id
= TAILQ_FIRST(&authctxt
->keys
); id
;
1860 id
= TAILQ_FIRST(&authctxt
->keys
)) {
1861 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1862 sshkey_free(id
->key
);
1869 pubkey_reset(Authctxt
*authctxt
)
1873 TAILQ_FOREACH(id
, &authctxt
->keys
, next
)
1878 try_identity(struct ssh
*ssh
, Identity
*id
)
1882 if (sshkey_type_plain(id
->key
->type
) == KEY_RSA
&&
1883 (ssh
->compat
& SSH_BUG_RSASIGMD5
) != 0) {
1884 debug("Skipped %s key %s for RSA/MD5 server",
1885 sshkey_type(id
->key
), id
->filename
);
1892 userauth_pubkey(struct ssh
*ssh
)
1894 Authctxt
*authctxt
= (Authctxt
*)ssh
->authctxt
;
1899 while ((id
= TAILQ_FIRST(&authctxt
->keys
))) {
1902 /* move key to the end of the queue */
1903 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1904 TAILQ_INSERT_TAIL(&authctxt
->keys
, id
, next
);
1906 * send a test message if we have the public key. for
1907 * encrypted keys we cannot do this and have to load the
1908 * private key instead
1910 if (id
->key
!= NULL
) {
1911 if (try_identity(ssh
, id
)) {
1912 ident
= format_identity(id
);
1913 debug("Offering public key: %s", ident
);
1915 sent
= send_pubkey_test(ssh
, id
);
1918 debug("Trying private key: %s", id
->filename
);
1919 id
->key
= load_identity_file(id
);
1920 if (id
->key
!= NULL
) {
1921 if (try_identity(ssh
, id
)) {
1923 sent
= sign_and_send_pubkey(ssh
, id
);
1925 sshkey_free(id
->key
);
1937 * Send userauth request message specifying keyboard-interactive method.
1940 userauth_kbdint(struct ssh
*ssh
)
1942 Authctxt
*authctxt
= (Authctxt
*)ssh
->authctxt
;
1945 if (authctxt
->attempt_kbdint
++ >= options
.number_of_password_prompts
)
1947 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1948 if (authctxt
->attempt_kbdint
> 1 && !authctxt
->info_req_seen
) {
1949 debug3("userauth_kbdint: disable: no info_req_seen");
1950 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_INFO_REQUEST
, NULL
);
1954 debug2("userauth_kbdint");
1955 if ((r
= sshpkt_start(ssh
, SSH2_MSG_USERAUTH_REQUEST
)) != 0 ||
1956 (r
= sshpkt_put_cstring(ssh
, authctxt
->server_user
)) != 0 ||
1957 (r
= sshpkt_put_cstring(ssh
, authctxt
->service
)) != 0 ||
1958 (r
= sshpkt_put_cstring(ssh
, authctxt
->method
->name
)) != 0 ||
1959 (r
= sshpkt_put_cstring(ssh
, "")) != 0 || /* lang */
1960 (r
= sshpkt_put_cstring(ssh
, options
.kbd_interactive_devices
?
1961 options
.kbd_interactive_devices
: "")) != 0 ||
1962 (r
= sshpkt_send(ssh
)) != 0)
1963 fatal_fr(r
, "send packet");
1965 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_INFO_REQUEST
, &input_userauth_info_req
);
1970 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1973 input_userauth_info_req(int type
, u_int32_t seq
, struct ssh
*ssh
)
1975 Authctxt
*authctxt
= ssh
->authctxt
;
1976 char *name
= NULL
, *inst
= NULL
, *lang
= NULL
, *prompt
= NULL
;
1977 char *display_prompt
= NULL
, *response
= NULL
;
1979 u_int num_prompts
, i
;
1982 debug2_f("entering");
1984 if (authctxt
== NULL
)
1985 fatal_f("no authentication context");
1987 authctxt
->info_req_seen
= 1;
1989 if ((r
= sshpkt_get_cstring(ssh
, &name
, NULL
)) != 0 ||
1990 (r
= sshpkt_get_cstring(ssh
, &inst
, NULL
)) != 0 ||
1991 (r
= sshpkt_get_cstring(ssh
, &lang
, NULL
)) != 0)
1993 if (strlen(name
) > 0)
1995 if (strlen(inst
) > 0)
1998 if ((r
= sshpkt_get_u32(ssh
, &num_prompts
)) != 0)
2001 * Begin to build info response packet based on prompts requested.
2002 * We commit to providing the correct number of responses, so if
2003 * further on we run into a problem that prevents this, we have to
2004 * be sure and clean this up and send a correct error response.
2006 if ((r
= sshpkt_start(ssh
, SSH2_MSG_USERAUTH_INFO_RESPONSE
)) != 0 ||
2007 (r
= sshpkt_put_u32(ssh
, num_prompts
)) != 0)
2010 debug2_f("num_prompts %d", num_prompts
);
2011 for (i
= 0; i
< num_prompts
; i
++) {
2012 if ((r
= sshpkt_get_cstring(ssh
, &prompt
, NULL
)) != 0 ||
2013 (r
= sshpkt_get_u8(ssh
, &echo
)) != 0)
2015 if (asmprintf(&display_prompt
, INT_MAX
, NULL
, "(%s@%s) %s",
2016 authctxt
->server_user
, options
.host_key_alias
?
2017 options
.host_key_alias
: authctxt
->host
, prompt
) == -1)
2018 fatal_f("asmprintf failed");
2019 response
= read_passphrase(display_prompt
, echo
? RP_ECHO
: 0);
2020 if ((r
= sshpkt_put_cstring(ssh
, response
)) != 0)
2022 freezero(response
, strlen(response
));
2024 free(display_prompt
);
2025 display_prompt
= response
= prompt
= NULL
;
2027 /* done with parsing incoming message. */
2028 if ((r
= sshpkt_get_end(ssh
)) != 0 ||
2029 (r
= sshpkt_add_padding(ssh
, 64)) != 0)
2031 r
= sshpkt_send(ssh
);
2034 freezero(response
, strlen(response
));
2036 free(display_prompt
);
2044 ssh_keysign(struct ssh
*ssh
, struct sshkey
*key
, u_char
**sigp
, size_t *lenp
,
2045 const u_char
*data
, size_t datalen
)
2050 int r
, to
[2], from
[2], status
;
2051 int sock
= ssh_packet_get_connection_in(ssh
);
2052 u_char rversion
= 0, version
= 2;
2053 void (*osigchld
)(int);
2058 if (stat(_PATH_SSH_KEY_SIGN
, &st
) == -1) {
2059 error_f("not installed: %s", strerror(errno
));
2062 if (fflush(stdout
) != 0) {
2063 error_f("fflush: %s", strerror(errno
));
2066 if (pipe(to
) == -1) {
2067 error_f("pipe: %s", strerror(errno
));
2070 if (pipe(from
) == -1) {
2071 error_f("pipe: %s", strerror(errno
));
2074 if ((pid
= fork()) == -1) {
2075 error_f("fork: %s", strerror(errno
));
2078 osigchld
= ssh_signal(SIGCHLD
, SIG_DFL
);
2081 if (dup2(from
[1], STDOUT_FILENO
) == -1)
2082 fatal_f("dup2: %s", strerror(errno
));
2084 if (dup2(to
[0], STDIN_FILENO
) == -1)
2085 fatal_f("dup2: %s", strerror(errno
));
2089 if (dup2(sock
, STDERR_FILENO
+ 1) == -1)
2090 fatal_f("dup2: %s", strerror(errno
));
2091 sock
= STDERR_FILENO
+ 1;
2092 fcntl(sock
, F_SETFD
, 0); /* keep the socket on exec */
2093 closefrom(sock
+ 1);
2095 debug3_f("[child] pid=%ld, exec %s",
2096 (long)getpid(), _PATH_SSH_KEY_SIGN
);
2097 execl(_PATH_SSH_KEY_SIGN
, _PATH_SSH_KEY_SIGN
, (char *)NULL
);
2098 fatal_f("exec(%s): %s", _PATH_SSH_KEY_SIGN
,
2103 sock
= STDERR_FILENO
+ 1;
2105 if ((b
= sshbuf_new()) == NULL
)
2106 fatal_f("sshbuf_new failed");
2107 /* send # of sock, data to be signed */
2108 if ((r
= sshbuf_put_u32(b
, sock
)) != 0 ||
2109 (r
= sshbuf_put_string(b
, data
, datalen
)) != 0)
2110 fatal_fr(r
, "buffer error");
2111 if (ssh_msg_send(to
[1], version
, b
) == -1)
2112 fatal_f("couldn't send request");
2114 r
= ssh_msg_recv(from
[0], b
);
2118 error_f("no reply");
2123 while (waitpid(pid
, &status
, 0) == -1) {
2124 if (errno
!= EINTR
) {
2125 error_f("waitpid %ld: %s", (long)pid
, strerror(errno
));
2129 if (!WIFEXITED(status
)) {
2130 error_f("exited abnormally");
2133 if (WEXITSTATUS(status
) != 0) {
2134 error_f("exited with status %d", WEXITSTATUS(status
));
2137 if ((r
= sshbuf_get_u8(b
, &rversion
)) != 0) {
2138 error_fr(r
, "buffer error");
2141 if (rversion
!= version
) {
2142 error_f("bad version");
2145 if ((r
= sshbuf_get_string(b
, sigp
, lenp
)) != 0) {
2146 error_fr(r
, "buffer error");
2148 ssh_signal(SIGCHLD
, osigchld
);
2152 ssh_signal(SIGCHLD
, osigchld
);
2159 userauth_hostbased(struct ssh
*ssh
)
2161 Authctxt
*authctxt
= (Authctxt
*)ssh
->authctxt
;
2162 struct sshkey
*private = NULL
;
2163 struct sshbuf
*b
= NULL
;
2164 u_char
*sig
= NULL
, *keyblob
= NULL
;
2165 char *fp
= NULL
, *chost
= NULL
, *lname
= NULL
;
2166 size_t siglen
= 0, keylen
= 0;
2167 int i
, r
, success
= 0;
2169 if (authctxt
->ktypes
== NULL
) {
2170 authctxt
->oktypes
= xstrdup(options
.hostbased_accepted_algos
);
2171 authctxt
->ktypes
= authctxt
->oktypes
;
2175 * Work through each listed type pattern in HostbasedAcceptedAlgorithms,
2176 * trying each hostkey that matches the type in turn.
2179 if (authctxt
->active_ktype
== NULL
)
2180 authctxt
->active_ktype
= strsep(&authctxt
->ktypes
, ",");
2181 if (authctxt
->active_ktype
== NULL
||
2182 *authctxt
->active_ktype
== '\0')
2184 debug3_f("trying key type %s", authctxt
->active_ktype
);
2186 /* check for a useful key */
2188 for (i
= 0; i
< authctxt
->sensitive
->nkeys
; i
++) {
2189 if (authctxt
->sensitive
->keys
[i
] == NULL
||
2190 authctxt
->sensitive
->keys
[i
]->type
== KEY_UNSPEC
)
2192 if (!sshkey_match_keyname_to_sigalgs(
2193 sshkey_ssh_name(authctxt
->sensitive
->keys
[i
]),
2194 authctxt
->active_ktype
))
2196 /* we take and free the key */
2197 private = authctxt
->sensitive
->keys
[i
];
2198 authctxt
->sensitive
->keys
[i
] = NULL
;
2202 if (private != NULL
)
2204 /* No more keys of this type; advance */
2205 authctxt
->active_ktype
= NULL
;
2207 if (private == NULL
) {
2208 free(authctxt
->oktypes
);
2209 authctxt
->oktypes
= authctxt
->ktypes
= NULL
;
2210 authctxt
->active_ktype
= NULL
;
2211 debug("No more client hostkeys for hostbased authentication.");
2215 if ((fp
= sshkey_fingerprint(private, options
.fingerprint_hash
,
2216 SSH_FP_DEFAULT
)) == NULL
) {
2217 error_f("sshkey_fingerprint failed");
2220 debug_f("trying hostkey %s %s using sigalg %s",
2221 sshkey_ssh_name(private), fp
, authctxt
->active_ktype
);
2223 /* figure out a name for the client host */
2224 lname
= get_local_name(ssh_packet_get_connection_in(ssh
));
2225 if (lname
== NULL
) {
2226 error_f("cannot get local ipaddr/name");
2230 /* XXX sshbuf_put_stringf? */
2231 xasprintf(&chost
, "%s.", lname
);
2232 debug2_f("chost %s", chost
);
2234 /* construct data */
2235 if ((b
= sshbuf_new()) == NULL
) {
2236 error_f("sshbuf_new failed");
2239 if ((r
= sshkey_to_blob(private, &keyblob
, &keylen
)) != 0) {
2240 error_fr(r
, "sshkey_to_blob");
2243 if ((r
= sshbuf_put_stringb(b
, ssh
->kex
->session_id
)) != 0 ||
2244 (r
= sshbuf_put_u8(b
, SSH2_MSG_USERAUTH_REQUEST
)) != 0 ||
2245 (r
= sshbuf_put_cstring(b
, authctxt
->server_user
)) != 0 ||
2246 (r
= sshbuf_put_cstring(b
, authctxt
->service
)) != 0 ||
2247 (r
= sshbuf_put_cstring(b
, authctxt
->method
->name
)) != 0 ||
2248 (r
= sshbuf_put_cstring(b
, authctxt
->active_ktype
)) != 0 ||
2249 (r
= sshbuf_put_string(b
, keyblob
, keylen
)) != 0 ||
2250 (r
= sshbuf_put_cstring(b
, chost
)) != 0 ||
2251 (r
= sshbuf_put_cstring(b
, authctxt
->local_user
)) != 0) {
2252 error_fr(r
, "buffer error");
2257 sshbuf_dump(b
, stderr
);
2259 if ((r
= ssh_keysign(ssh
, private, &sig
, &siglen
,
2260 sshbuf_ptr(b
), sshbuf_len(b
))) != 0) {
2261 error("sign using hostkey %s %s failed",
2262 sshkey_ssh_name(private), fp
);
2265 if ((r
= sshpkt_start(ssh
, SSH2_MSG_USERAUTH_REQUEST
)) != 0 ||
2266 (r
= sshpkt_put_cstring(ssh
, authctxt
->server_user
)) != 0 ||
2267 (r
= sshpkt_put_cstring(ssh
, authctxt
->service
)) != 0 ||
2268 (r
= sshpkt_put_cstring(ssh
, authctxt
->method
->name
)) != 0 ||
2269 (r
= sshpkt_put_cstring(ssh
, authctxt
->active_ktype
)) != 0 ||
2270 (r
= sshpkt_put_string(ssh
, keyblob
, keylen
)) != 0 ||
2271 (r
= sshpkt_put_cstring(ssh
, chost
)) != 0 ||
2272 (r
= sshpkt_put_cstring(ssh
, authctxt
->local_user
)) != 0 ||
2273 (r
= sshpkt_put_string(ssh
, sig
, siglen
)) != 0 ||
2274 (r
= sshpkt_send(ssh
)) != 0) {
2275 error_fr(r
, "packet error");
2282 freezero(sig
, siglen
);
2287 sshkey_free(private);
2293 /* find auth method */
2296 * given auth method name, if configurable options permit this method fill
2297 * in auth_ident field and return true, otherwise return false.
2300 authmethod_is_enabled(Authmethod
*method
)
2304 /* return false if options indicate this method is disabled */
2305 if (method
->enabled
== NULL
|| *method
->enabled
== 0)
2307 /* return false if batch mode is enabled but method needs interactive mode */
2308 if (method
->batch_flag
!= NULL
&& *method
->batch_flag
!= 0)
2314 authmethod_lookup(const char *name
)
2316 Authmethod
*method
= NULL
;
2318 for (method
= authmethods
; method
->name
!= NULL
; method
++)
2319 if (strcmp(name
, method
->name
) == 0)
2321 debug2("Unrecognized authentication method name: %s", name
? name
: "NULL");
2325 /* XXX internal state */
2326 static Authmethod
*current
= NULL
;
2327 static char *supported
= NULL
;
2328 static char *preferred
= NULL
;
2331 * Given the authentication method list sent by the server, return the
2332 * next method we should try. If the server initially sends a nil list,
2333 * use a built-in default list.
2336 authmethod_get(char *authlist
)
2341 /* Use a suitable default if we're passed a nil list. */
2342 if (authlist
== NULL
|| strlen(authlist
) == 0)
2343 authlist
= options
.preferred_authentications
;
2345 if (supported
== NULL
|| strcmp(authlist
, supported
) != 0) {
2346 debug3("start over, passed a different list %s", authlist
);
2348 supported
= xstrdup(authlist
);
2349 preferred
= options
.preferred_authentications
;
2350 debug3("preferred %s", preferred
);
2352 } else if (current
!= NULL
&& authmethod_is_enabled(current
))
2356 if ((name
= match_list(preferred
, supported
, &next
)) == NULL
) {
2357 debug("No more authentication methods to try.");
2362 debug3("authmethod_lookup %s", name
);
2363 debug3("remaining preferred: %s", preferred
);
2364 if ((current
= authmethod_lookup(name
)) != NULL
&&
2365 authmethod_is_enabled(current
)) {
2366 debug3("authmethod_is_enabled %s", name
);
2367 debug("Next authentication method: %s", name
);
2376 authmethods_get(void)
2378 Authmethod
*method
= NULL
;
2383 if ((b
= sshbuf_new()) == NULL
)
2384 fatal_f("sshbuf_new failed");
2385 for (method
= authmethods
; method
->name
!= NULL
; method
++) {
2386 if (authmethod_is_enabled(method
)) {
2387 if ((r
= sshbuf_putf(b
, "%s%s",
2388 sshbuf_len(b
) ? "," : "", method
->name
)) != 0)
2389 fatal_fr(r
, "buffer error");
2392 if ((list
= sshbuf_dup_string(b
)) == NULL
)
2393 fatal_f("sshbuf_dup_string failed");