1 /* $OpenBSD: sshconnect2.c,v 1.266 2017/08/27 00:38:41 dtucker Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Copyright (c) 2008 Damien Miller. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
30 #include <sys/socket.h>
43 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
47 #include "openbsd-compat/sys-queue.h"
58 #include "myproposal.h"
59 #include "sshconnect.h"
70 #include "pathnames.h"
81 extern char *client_version_string
;
82 extern char *server_version_string
;
83 extern Options options
;
89 u_char
*session_id2
= NULL
;
90 u_int session_id2_len
= 0;
93 struct sockaddr
*xxx_hostaddr
;
96 verify_host_key_callback(struct sshkey
*hostkey
, struct ssh
*ssh
)
98 if (verify_host_key(xxx_host
, xxx_hostaddr
, hostkey
) == -1)
99 fatal("Host key verification failed.");
104 order_hostkeyalgs(char *host
, struct sockaddr
*hostaddr
, u_short port
)
106 char *oavail
, *avail
, *first
, *last
, *alg
, *hostname
, *ret
;
108 struct hostkeys
*hostkeys
;
112 /* Find all hostkeys for this hostname */
113 get_hostfile_hostname_ipaddr(host
, hostaddr
, port
, &hostname
, NULL
);
114 hostkeys
= init_hostkeys();
115 for (i
= 0; i
< options
.num_user_hostfiles
; i
++)
116 load_hostkeys(hostkeys
, hostname
, options
.user_hostfiles
[i
]);
117 for (i
= 0; i
< options
.num_system_hostfiles
; i
++)
118 load_hostkeys(hostkeys
, hostname
, options
.system_hostfiles
[i
]);
120 oavail
= avail
= xstrdup(KEX_DEFAULT_PK_ALG
);
121 maxlen
= strlen(avail
) + 1;
122 first
= xmalloc(maxlen
);
123 last
= xmalloc(maxlen
);
124 *first
= *last
= '\0';
126 #define ALG_APPEND(to, from) \
129 strlcat(to, ",", maxlen); \
130 strlcat(to, from, maxlen); \
133 while ((alg
= strsep(&avail
, ",")) && *alg
!= '\0') {
134 if ((ktype
= sshkey_type_from_name(alg
)) == KEY_UNSPEC
)
135 fatal("%s: unknown alg %s", __func__
, alg
);
136 if (lookup_key_in_hostkeys_by_type(hostkeys
,
137 sshkey_type_plain(ktype
), NULL
))
138 ALG_APPEND(first
, alg
);
140 ALG_APPEND(last
, alg
);
143 xasprintf(&ret
, "%s%s%s", first
,
144 (*first
== '\0' || *last
== '\0') ? "" : ",", last
);
146 debug3("%s: prefer hostkeyalgs: %s", __func__
, first
);
152 free_hostkeys(hostkeys
);
158 ssh_kex2(char *host
, struct sockaddr
*hostaddr
, u_short port
)
160 char *myproposal
[PROPOSAL_MAX
] = { KEX_CLIENT
};
166 xxx_hostaddr
= hostaddr
;
168 if ((s
= kex_names_cat(options
.kex_algorithms
, "ext-info-c")) == NULL
)
169 fatal("%s: kex_names_cat", __func__
);
170 myproposal
[PROPOSAL_KEX_ALGS
] = compat_kex_proposal(s
);
171 myproposal
[PROPOSAL_ENC_ALGS_CTOS
] =
172 compat_cipher_proposal(options
.ciphers
);
173 myproposal
[PROPOSAL_ENC_ALGS_STOC
] =
174 compat_cipher_proposal(options
.ciphers
);
175 myproposal
[PROPOSAL_COMP_ALGS_CTOS
] =
176 myproposal
[PROPOSAL_COMP_ALGS_STOC
] = options
.compression
?
177 "zlib@openssh.com,zlib,none" : "none,zlib@openssh.com,zlib";
178 myproposal
[PROPOSAL_MAC_ALGS_CTOS
] =
179 myproposal
[PROPOSAL_MAC_ALGS_STOC
] = options
.macs
;
180 if (options
.hostkeyalgorithms
!= NULL
) {
181 if (kex_assemble_names(KEX_DEFAULT_PK_ALG
,
182 &options
.hostkeyalgorithms
) != 0)
183 fatal("%s: kex_assemble_namelist", __func__
);
184 myproposal
[PROPOSAL_SERVER_HOST_KEY_ALGS
] =
185 compat_pkalg_proposal(options
.hostkeyalgorithms
);
187 /* Enforce default */
188 options
.hostkeyalgorithms
= xstrdup(KEX_DEFAULT_PK_ALG
);
189 /* Prefer algorithms that we already have keys for */
190 myproposal
[PROPOSAL_SERVER_HOST_KEY_ALGS
] =
191 compat_pkalg_proposal(
192 order_hostkeyalgs(host
, hostaddr
, port
));
195 if (options
.rekey_limit
|| options
.rekey_interval
)
196 packet_set_rekey_limits(options
.rekey_limit
,
197 options
.rekey_interval
);
199 /* start key exchange */
200 if ((r
= kex_setup(active_state
, myproposal
)) != 0)
201 fatal("kex_setup: %s", ssh_err(r
));
202 kex
= active_state
->kex
;
204 kex
->kex
[KEX_DH_GRP1_SHA1
] = kexdh_client
;
205 kex
->kex
[KEX_DH_GRP14_SHA1
] = kexdh_client
;
206 kex
->kex
[KEX_DH_GRP14_SHA256
] = kexdh_client
;
207 kex
->kex
[KEX_DH_GRP16_SHA512
] = kexdh_client
;
208 kex
->kex
[KEX_DH_GRP18_SHA512
] = kexdh_client
;
209 kex
->kex
[KEX_DH_GEX_SHA1
] = kexgex_client
;
210 kex
->kex
[KEX_DH_GEX_SHA256
] = kexgex_client
;
211 # ifdef OPENSSL_HAS_ECC
212 kex
->kex
[KEX_ECDH_SHA2
] = kexecdh_client
;
215 kex
->kex
[KEX_C25519_SHA256
] = kexc25519_client
;
216 kex
->client_version_string
=client_version_string
;
217 kex
->server_version_string
=server_version_string
;
218 kex
->verify_host_key
=&verify_host_key_callback
;
220 ssh_dispatch_run_fatal(active_state
, DISPATCH_BLOCK
, &kex
->done
);
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
, struct ssh
*);
291 int input_userauth_ext_info(int, u_int32_t
, struct ssh
*);
292 int input_userauth_success(int, u_int32_t
, struct ssh
*);
293 int input_userauth_success_unexpected(int, u_int32_t
, struct ssh
*);
294 int input_userauth_failure(int, u_int32_t
, struct ssh
*);
295 int input_userauth_banner(int, u_int32_t
, struct ssh
*);
296 int input_userauth_error(int, u_int32_t
, struct ssh
*);
297 int input_userauth_info_req(int, u_int32_t
, struct ssh
*);
298 int input_userauth_pk_ok(int, u_int32_t
, struct ssh
*);
299 int input_userauth_passwd_changereq(int, u_int32_t
, struct ssh
*);
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
, struct ssh
*);
310 int input_gssapi_token(int type
, u_int32_t
, struct ssh
*);
311 int input_gssapi_hash(int type
, u_int32_t
, struct ssh
*);
312 int input_gssapi_error(int, u_int32_t
, struct ssh
*);
313 int input_gssapi_errtok(int, u_int32_t
, struct ssh
*);
316 void userauth(Authctxt
*, char *);
318 static int sign_and_send_pubkey(Authctxt
*, Identity
*);
319 static void pubkey_prepare(Authctxt
*);
320 static void pubkey_cleanup(Authctxt
*);
321 static void pubkey_reset(Authctxt
*);
322 static struct sshkey
*load_identity_file(Identity
*);
324 static Authmethod
*authmethod_get(char *authlist
);
325 static Authmethod
*authmethod_lookup(const char *name
);
326 static char *authmethods_get(void);
328 Authmethod authmethods
[] = {
333 &options
.gss_authentication
,
339 &options
.hostbased_authentication
,
344 &options
.pubkey_authentication
,
346 {"keyboard-interactive",
349 &options
.kbd_interactive_authentication
,
350 &options
.batch_mode
},
354 &options
.password_authentication
,
355 &options
.batch_mode
},
361 {NULL
, NULL
, NULL
, NULL
, NULL
}
365 ssh_userauth2(const char *local_user
, const char *server_user
, char *host
,
366 Sensitive
*sensitive
)
368 struct ssh
*ssh
= active_state
;
372 if (options
.challenge_response_authentication
)
373 options
.kbd_interactive_authentication
= 1;
374 if (options
.preferred_authentications
== NULL
)
375 options
.preferred_authentications
= authmethods_get();
377 /* setup authentication context */
378 memset(&authctxt
, 0, sizeof(authctxt
));
379 pubkey_prepare(&authctxt
);
380 authctxt
.server_user
= server_user
;
381 authctxt
.local_user
= local_user
;
382 authctxt
.host
= host
;
383 authctxt
.service
= "ssh-connection"; /* service name */
384 authctxt
.success
= 0;
385 authctxt
.method
= authmethod_lookup("none");
386 authctxt
.authlist
= NULL
;
387 authctxt
.methoddata
= NULL
;
388 authctxt
.sensitive
= sensitive
;
389 authctxt
.active_ktype
= authctxt
.oktypes
= authctxt
.ktypes
= NULL
;
390 authctxt
.info_req_seen
= 0;
391 authctxt
.agent_fd
= -1;
392 if (authctxt
.method
== NULL
)
393 fatal("ssh_userauth2: internal error: cannot send userauth none request");
395 if ((r
= sshpkt_start(ssh
, SSH2_MSG_SERVICE_REQUEST
)) != 0 ||
396 (r
= sshpkt_put_cstring(ssh
, "ssh-userauth")) != 0 ||
397 (r
= sshpkt_send(ssh
)) != 0)
398 fatal("%s: %s", __func__
, ssh_err(r
));
400 ssh
->authctxt
= &authctxt
;
401 ssh_dispatch_init(ssh
, &input_userauth_error
);
402 ssh_dispatch_set(ssh
, SSH2_MSG_EXT_INFO
, &input_userauth_ext_info
);
403 ssh_dispatch_set(ssh
, SSH2_MSG_SERVICE_ACCEPT
, &input_userauth_service_accept
);
404 ssh_dispatch_run_fatal(ssh
, DISPATCH_BLOCK
, &authctxt
.success
); /* loop until success */
405 ssh
->authctxt
= NULL
;
407 pubkey_cleanup(&authctxt
);
408 ssh_dispatch_range(ssh
, SSH2_MSG_USERAUTH_MIN
, SSH2_MSG_USERAUTH_MAX
, NULL
);
410 if (!authctxt
.success
)
411 fatal("Authentication failed.");
412 debug("Authentication succeeded (%s).", authctxt
.method
->name
);
417 input_userauth_service_accept(int type
, u_int32_t seq
, struct ssh
*ssh
)
419 Authctxt
*authctxt
= ssh
->authctxt
;
422 if (ssh_packet_remaining(ssh
) > 0) {
425 if ((r
= sshpkt_get_cstring(ssh
, &reply
, NULL
)) != 0)
427 debug2("service_accept: %s", reply
);
430 debug2("buggy server: service_accept w/o service");
432 if ((r
= sshpkt_get_end(ssh
)) != 0)
434 debug("SSH2_MSG_SERVICE_ACCEPT received");
436 /* initial userauth request */
437 userauth_none(authctxt
);
439 ssh_dispatch_set(ssh
, SSH2_MSG_EXT_INFO
, &input_userauth_error
);
440 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_SUCCESS
, &input_userauth_success
);
441 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_FAILURE
, &input_userauth_failure
);
442 ssh_dispatch_set(ssh
, SSH2_MSG_USERAUTH_BANNER
, &input_userauth_banner
);
450 input_userauth_ext_info(int type
, u_int32_t seqnr
, struct ssh
*ssh
)
452 return kex_input_ext_info(type
, seqnr
, ssh
);
456 userauth(Authctxt
*authctxt
, char *authlist
)
458 if (authctxt
->method
!= NULL
&& authctxt
->method
->cleanup
!= NULL
)
459 authctxt
->method
->cleanup(authctxt
);
461 free(authctxt
->methoddata
);
462 authctxt
->methoddata
= NULL
;
463 if (authlist
== NULL
) {
464 authlist
= authctxt
->authlist
;
466 free(authctxt
->authlist
);
467 authctxt
->authlist
= authlist
;
470 Authmethod
*method
= authmethod_get(authlist
);
472 fatal("%s@%s: Permission denied (%s).",
473 authctxt
->server_user
, authctxt
->host
, authlist
);
474 authctxt
->method
= method
;
476 /* reset the per method handler */
477 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN
,
478 SSH2_MSG_USERAUTH_PER_METHOD_MAX
, NULL
);
480 /* and try new method */
481 if (method
->userauth(authctxt
) != 0) {
482 debug2("we sent a %s packet, wait for reply", method
->name
);
485 debug2("we did not send a packet, disable method");
486 method
->enabled
= NULL
;
493 input_userauth_error(int type
, u_int32_t seq
, struct ssh
*ssh
)
495 fatal("input_userauth_error: bad message during authentication: "
502 input_userauth_banner(int type
, u_int32_t seq
, struct ssh
*ssh
)
507 debug3("%s", __func__
);
508 msg
= packet_get_string(&len
);
509 lang
= packet_get_string(NULL
);
510 if (len
> 0 && options
.log_level
>= SYSLOG_LEVEL_INFO
)
511 fmprintf(stderr
, "%s", msg
);
519 input_userauth_success(int type
, u_int32_t seq
, struct ssh
*ssh
)
521 Authctxt
*authctxt
= ssh
->authctxt
;
523 if (authctxt
== NULL
)
524 fatal("input_userauth_success: no authentication context");
525 free(authctxt
->authlist
);
526 authctxt
->authlist
= NULL
;
527 if (authctxt
->method
!= NULL
&& authctxt
->method
->cleanup
!= NULL
)
528 authctxt
->method
->cleanup(authctxt
);
529 free(authctxt
->methoddata
);
530 authctxt
->methoddata
= NULL
;
531 authctxt
->success
= 1; /* break out */
536 input_userauth_success_unexpected(int type
, u_int32_t seq
, struct ssh
*ssh
)
538 Authctxt
*authctxt
= ssh
->authctxt
;
540 if (authctxt
== NULL
)
541 fatal("%s: no authentication context", __func__
);
543 fatal("Unexpected authentication success during %s.",
544 authctxt
->method
->name
);
550 input_userauth_failure(int type
, u_int32_t seq
, struct ssh
*ssh
)
552 Authctxt
*authctxt
= ssh
->authctxt
;
553 char *authlist
= NULL
;
556 if (authctxt
== NULL
)
557 fatal("input_userauth_failure: no authentication context");
559 authlist
= packet_get_string(NULL
);
560 partial
= packet_get_char();
564 verbose("Authenticated with partial success.");
566 pubkey_reset(authctxt
);
568 debug("Authentications that can continue: %s", authlist
);
570 userauth(authctxt
, authlist
);
576 input_userauth_pk_ok(int type
, u_int32_t seq
, struct ssh
*ssh
)
578 Authctxt
*authctxt
= ssh
->authctxt
;
579 struct sshkey
*key
= NULL
;
582 int pktype
, sent
= 0;
587 if (authctxt
== NULL
)
588 fatal("input_userauth_pk_ok: no authentication context");
589 if (datafellows
& SSH_BUG_PKOK
) {
590 /* this is similar to SSH_BUG_PKAUTH */
591 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
592 pkblob
= packet_get_string(&blen
);
594 buffer_append(&b
, pkblob
, blen
);
595 pkalg
= buffer_get_string(&b
, &alen
);
598 pkalg
= packet_get_string(&alen
);
599 pkblob
= packet_get_string(&blen
);
603 debug("Server accepts key: pkalg %s blen %u", pkalg
, blen
);
605 if ((pktype
= key_type_from_name(pkalg
)) == KEY_UNSPEC
) {
606 debug("unknown pkalg %s", pkalg
);
609 if ((key
= key_from_blob(pkblob
, blen
)) == NULL
) {
610 debug("no key from blob. pkalg %s", pkalg
);
613 if (key
->type
!= pktype
) {
614 error("input_userauth_pk_ok: type mismatch "
615 "for decoded key (received %d, expected %d)",
619 if ((fp
= sshkey_fingerprint(key
, options
.fingerprint_hash
,
620 SSH_FP_DEFAULT
)) == NULL
)
622 debug2("input_userauth_pk_ok: fp %s", fp
);
626 * search keys in the reverse order, because last candidate has been
627 * moved to the end of the queue. this also avoids confusion by
630 TAILQ_FOREACH_REVERSE(id
, &authctxt
->keys
, idlist
, next
) {
631 if (key_equal(key
, id
->key
)) {
632 sent
= sign_and_send_pubkey(authctxt
, id
);
642 /* try another method if we did not send a packet */
644 userauth(authctxt
, NULL
);
650 userauth_gssapi(Authctxt
*authctxt
)
652 Gssctxt
*gssctxt
= NULL
;
653 static gss_OID_set gss_supported
= NULL
;
654 static u_int mech
= 0;
658 /* Try one GSSAPI method at a time, rather than sending them all at
661 if (gss_supported
== NULL
)
662 gss_indicate_mechs(&min
, &gss_supported
);
664 /* Check to see if the mechanism is usable before we offer it */
665 while (mech
< gss_supported
->count
&& !ok
) {
666 /* My DER encoding requires length<128 */
667 if (gss_supported
->elements
[mech
].length
< 128 &&
668 ssh_gssapi_check_mechanism(&gssctxt
,
669 &gss_supported
->elements
[mech
], authctxt
->host
)) {
670 ok
= 1; /* Mechanism works */
679 authctxt
->methoddata
=(void *)gssctxt
;
681 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
682 packet_put_cstring(authctxt
->server_user
);
683 packet_put_cstring(authctxt
->service
);
684 packet_put_cstring(authctxt
->method
->name
);
688 packet_put_int((gss_supported
->elements
[mech
].length
) + 2);
689 packet_put_char(SSH_GSS_OIDTYPE
);
690 packet_put_char(gss_supported
->elements
[mech
].length
);
691 packet_put_raw(gss_supported
->elements
[mech
].elements
,
692 gss_supported
->elements
[mech
].length
);
696 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE
, &input_gssapi_response
);
697 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
, &input_gssapi_token
);
698 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR
, &input_gssapi_error
);
699 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
, &input_gssapi_errtok
);
701 mech
++; /* Move along to next candidate */
707 process_gssapi_token(struct ssh
*ssh
, gss_buffer_t recv_tok
)
709 Authctxt
*authctxt
= ssh
->authctxt
;
710 Gssctxt
*gssctxt
= authctxt
->methoddata
;
711 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
712 gss_buffer_desc mic
= GSS_C_EMPTY_BUFFER
;
713 gss_buffer_desc gssbuf
;
714 OM_uint32 status
, ms
, flags
;
717 status
= ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
718 recv_tok
, &send_tok
, &flags
);
720 if (send_tok
.length
> 0) {
721 if (GSS_ERROR(status
))
722 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK
);
724 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN
);
726 packet_put_string(send_tok
.value
, send_tok
.length
);
728 gss_release_buffer(&ms
, &send_tok
);
731 if (status
== GSS_S_COMPLETE
) {
732 /* send either complete or MIC, depending on mechanism */
733 if (!(flags
& GSS_C_INTEG_FLAG
)) {
734 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE
);
737 ssh_gssapi_buildmic(&b
, authctxt
->server_user
,
738 authctxt
->service
, "gssapi-with-mic");
740 gssbuf
.value
= buffer_ptr(&b
);
741 gssbuf
.length
= buffer_len(&b
);
743 status
= ssh_gssapi_sign(gssctxt
, &gssbuf
, &mic
);
745 if (!GSS_ERROR(status
)) {
746 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC
);
747 packet_put_string(mic
.value
, mic
.length
);
753 gss_release_buffer(&ms
, &mic
);
762 input_gssapi_response(int type
, u_int32_t plen
, struct ssh
*ssh
)
764 Authctxt
*authctxt
= ssh
->authctxt
;
769 if (authctxt
== NULL
)
770 fatal("input_gssapi_response: no authentication context");
771 gssctxt
= authctxt
->methoddata
;
774 oidv
= packet_get_string(&oidlen
);
777 oidv
[0] != SSH_GSS_OIDTYPE
||
778 oidv
[1] != oidlen
- 2) {
780 debug("Badly encoded mechanism OID received");
781 userauth(authctxt
, NULL
);
785 if (!ssh_gssapi_check_oid(gssctxt
, oidv
+ 2, oidlen
- 2))
786 fatal("Server returned different OID than expected");
792 if (GSS_ERROR(process_gssapi_token(ssh
, GSS_C_NO_BUFFER
))) {
793 /* Start again with next method on list */
794 debug("Trying to start again");
795 userauth(authctxt
, NULL
);
803 input_gssapi_token(int type
, u_int32_t plen
, struct ssh
*ssh
)
805 Authctxt
*authctxt
= ssh
->authctxt
;
806 gss_buffer_desc recv_tok
;
810 if (authctxt
== NULL
)
811 fatal("input_gssapi_response: no authentication context");
813 recv_tok
.value
= packet_get_string(&slen
);
814 recv_tok
.length
= slen
; /* safe typecast */
818 status
= process_gssapi_token(ssh
, &recv_tok
);
820 free(recv_tok
.value
);
822 if (GSS_ERROR(status
)) {
823 /* Start again with the next method in the list */
824 userauth(authctxt
, NULL
);
832 input_gssapi_errtok(int type
, u_int32_t plen
, struct ssh
*ssh
)
834 Authctxt
*authctxt
= ssh
->authctxt
;
836 gss_buffer_desc send_tok
= GSS_C_EMPTY_BUFFER
;
837 gss_buffer_desc recv_tok
;
841 if (authctxt
== NULL
)
842 fatal("input_gssapi_response: no authentication context");
843 gssctxt
= authctxt
->methoddata
;
845 recv_tok
.value
= packet_get_string(&len
);
846 recv_tok
.length
= len
;
850 /* Stick it into GSSAPI and see what it says */
851 (void)ssh_gssapi_init_ctx(gssctxt
, options
.gss_deleg_creds
,
852 &recv_tok
, &send_tok
, NULL
);
854 free(recv_tok
.value
);
855 gss_release_buffer(&ms
, &send_tok
);
857 /* Server will be returning a failed packet after this one */
863 input_gssapi_error(int type
, u_int32_t plen
, struct ssh
*ssh
)
868 /* maj */(void)packet_get_int();
869 /* min */(void)packet_get_int();
870 msg
=packet_get_string(NULL
);
871 lang
=packet_get_string(NULL
);
875 debug("Server GSSAPI Error:\n%s", msg
);
883 userauth_none(Authctxt
*authctxt
)
885 /* initial userauth request */
886 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
887 packet_put_cstring(authctxt
->server_user
);
888 packet_put_cstring(authctxt
->service
);
889 packet_put_cstring(authctxt
->method
->name
);
895 userauth_passwd(Authctxt
*authctxt
)
897 static int attempt
= 0;
900 const char *host
= options
.host_key_alias
? options
.host_key_alias
:
903 if (attempt
++ >= options
.number_of_password_prompts
)
907 error("Permission denied, please try again.");
909 snprintf(prompt
, sizeof(prompt
), "%.30s@%.128s's password: ",
910 authctxt
->server_user
, host
);
911 password
= read_passphrase(prompt
, 0);
912 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
913 packet_put_cstring(authctxt
->server_user
);
914 packet_put_cstring(authctxt
->service
);
915 packet_put_cstring(authctxt
->method
->name
);
917 packet_put_cstring(password
);
918 explicit_bzero(password
, strlen(password
));
920 packet_add_padding(64);
923 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
924 &input_userauth_passwd_changereq
);
930 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
934 input_userauth_passwd_changereq(int type
, u_int32_t seqnr
, struct ssh
*ssh
)
936 Authctxt
*authctxt
= ssh
->authctxt
;
937 char *info
, *lang
, *password
= NULL
, *retype
= NULL
;
941 debug2("input_userauth_passwd_changereq");
943 if (authctxt
== NULL
)
944 fatal("input_userauth_passwd_changereq: "
945 "no authentication context");
946 host
= options
.host_key_alias
? options
.host_key_alias
: authctxt
->host
;
948 info
= packet_get_string(NULL
);
949 lang
= packet_get_string(NULL
);
950 if (strlen(info
) > 0)
954 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
955 packet_put_cstring(authctxt
->server_user
);
956 packet_put_cstring(authctxt
->service
);
957 packet_put_cstring(authctxt
->method
->name
);
958 packet_put_char(1); /* additional info */
959 snprintf(prompt
, sizeof(prompt
),
960 "Enter %.30s@%.128s's old password: ",
961 authctxt
->server_user
, host
);
962 password
= read_passphrase(prompt
, 0);
963 packet_put_cstring(password
);
964 explicit_bzero(password
, strlen(password
));
967 while (password
== NULL
) {
968 snprintf(prompt
, sizeof(prompt
),
969 "Enter %.30s@%.128s's new password: ",
970 authctxt
->server_user
, host
);
971 password
= read_passphrase(prompt
, RP_ALLOW_EOF
);
972 if (password
== NULL
) {
976 snprintf(prompt
, sizeof(prompt
),
977 "Retype %.30s@%.128s's new password: ",
978 authctxt
->server_user
, host
);
979 retype
= read_passphrase(prompt
, 0);
980 if (strcmp(password
, retype
) != 0) {
981 explicit_bzero(password
, strlen(password
));
983 logit("Mismatch; try again, EOF to quit.");
986 explicit_bzero(retype
, strlen(retype
));
989 packet_put_cstring(password
);
990 explicit_bzero(password
, strlen(password
));
992 packet_add_padding(64);
995 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ
,
996 &input_userauth_passwd_changereq
);
1001 key_sign_encode(const struct sshkey
*key
)
1003 struct ssh
*ssh
= active_state
;
1005 if (key
->type
== KEY_RSA
) {
1006 switch (ssh
->kex
->rsa_sha2
) {
1008 return "rsa-sha2-256";
1010 return "rsa-sha2-512";
1013 return key_ssh_name(key
);
1017 identity_sign(struct identity
*id
, u_char
**sigp
, size_t *lenp
,
1018 const u_char
*data
, size_t datalen
, u_int compat
)
1023 /* the agent supports this key */
1024 if (id
->key
!= NULL
&& id
->agent_fd
!= -1)
1025 return ssh_agent_sign(id
->agent_fd
, id
->key
, sigp
, lenp
,
1026 data
, datalen
, key_sign_encode(id
->key
), compat
);
1029 * we have already loaded the private key or
1030 * the private key is stored in external hardware
1032 if (id
->key
!= NULL
&&
1033 (id
->isprivate
|| (id
->key
->flags
& SSHKEY_FLAG_EXT
)))
1034 return (sshkey_sign(id
->key
, sigp
, lenp
, data
, datalen
,
1035 key_sign_encode(id
->key
), compat
));
1037 /* load the private key from the file */
1038 if ((prv
= load_identity_file(id
)) == NULL
)
1039 return SSH_ERR_KEY_NOT_FOUND
;
1040 if (id
->key
!= NULL
&& !sshkey_equal_public(prv
, id
->key
)) {
1041 error("%s: private key %s contents do not match public",
1042 __func__
, id
->filename
);
1043 return SSH_ERR_KEY_NOT_FOUND
;
1045 ret
= sshkey_sign(prv
, sigp
, lenp
, data
, datalen
,
1046 key_sign_encode(prv
), compat
);
1052 id_filename_matches(Identity
*id
, Identity
*private_id
)
1054 const char *suffixes
[] = { ".pub", "-cert.pub", NULL
};
1055 size_t len
= strlen(id
->filename
), plen
= strlen(private_id
->filename
);
1058 if (strcmp(id
->filename
, private_id
->filename
) == 0)
1060 for (i
= 0; suffixes
[i
]; i
++) {
1061 slen
= strlen(suffixes
[i
]);
1062 if (len
> slen
&& plen
== len
- slen
&&
1063 strcmp(id
->filename
+ (len
- slen
), suffixes
[i
]) == 0 &&
1064 memcmp(id
->filename
, private_id
->filename
, plen
) == 0)
1071 sign_and_send_pubkey(Authctxt
*authctxt
, Identity
*id
)
1074 Identity
*private_id
;
1075 u_char
*blob
, *signature
;
1077 u_int bloblen
, skip
= 0;
1078 int matched
, ret
= -1, have_sig
= 1;
1081 if ((fp
= sshkey_fingerprint(id
->key
, options
.fingerprint_hash
,
1082 SSH_FP_DEFAULT
)) == NULL
)
1084 debug3("%s: %s %s", __func__
, key_type(id
->key
), fp
);
1087 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
1088 /* we cannot handle this key */
1089 debug3("sign_and_send_pubkey: cannot handle key");
1092 /* data to be signed */
1094 if (datafellows
& SSH_OLD_SESSIONID
) {
1095 buffer_append(&b
, session_id2
, session_id2_len
);
1096 skip
= session_id2_len
;
1098 buffer_put_string(&b
, session_id2
, session_id2_len
);
1099 skip
= buffer_len(&b
);
1101 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1102 buffer_put_cstring(&b
, authctxt
->server_user
);
1103 buffer_put_cstring(&b
,
1104 datafellows
& SSH_BUG_PKSERVICE
?
1107 if (datafellows
& SSH_BUG_PKAUTH
) {
1108 buffer_put_char(&b
, have_sig
);
1110 buffer_put_cstring(&b
, authctxt
->method
->name
);
1111 buffer_put_char(&b
, have_sig
);
1112 buffer_put_cstring(&b
, key_sign_encode(id
->key
));
1114 buffer_put_string(&b
, blob
, bloblen
);
1117 * If the key is an certificate, try to find a matching private key
1118 * and use it to complete the signature.
1119 * If no such private key exists, fall back to trying the certificate
1120 * key itself in case it has a private half already loaded.
1122 if (key_is_cert(id
->key
)) {
1124 TAILQ_FOREACH(private_id
, &authctxt
->keys
, next
) {
1125 if (sshkey_equal_public(id
->key
, private_id
->key
) &&
1126 id
->key
->type
!= private_id
->key
->type
) {
1133 * Exact key matches are preferred, but also allow
1134 * filename matches for non-PKCS#11/agent keys that
1135 * didn't load public keys. This supports the case
1136 * of keeping just a private key file and public
1137 * certificate on disk.
1139 if (!matched
&& !id
->isprivate
&& id
->agent_fd
== -1 &&
1140 (id
->key
->flags
& SSHKEY_FLAG_EXT
) == 0) {
1141 TAILQ_FOREACH(private_id
, &authctxt
->keys
, next
) {
1142 if (private_id
->key
== NULL
&&
1143 id_filename_matches(id
, private_id
)) {
1151 debug2("%s: using private key \"%s\"%s for "
1152 "certificate", __func__
, id
->filename
,
1153 id
->agent_fd
!= -1 ? " from agent" : "");
1155 debug("%s: no separate private key for certificate "
1156 "\"%s\"", __func__
, id
->filename
);
1160 /* generate signature */
1161 ret
= identity_sign(id
, &signature
, &slen
,
1162 buffer_ptr(&b
), buffer_len(&b
), datafellows
);
1164 if (ret
!= SSH_ERR_KEY_NOT_FOUND
)
1165 error("%s: signing failed: %s", __func__
, ssh_err(ret
));
1173 if (datafellows
& SSH_BUG_PKSERVICE
) {
1175 buffer_append(&b
, session_id2
, session_id2_len
);
1176 skip
= session_id2_len
;
1177 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
1178 buffer_put_cstring(&b
, authctxt
->server_user
);
1179 buffer_put_cstring(&b
, authctxt
->service
);
1180 buffer_put_cstring(&b
, authctxt
->method
->name
);
1181 buffer_put_char(&b
, have_sig
);
1182 if (!(datafellows
& SSH_BUG_PKAUTH
))
1183 buffer_put_cstring(&b
, key_ssh_name(id
->key
));
1184 buffer_put_string(&b
, blob
, bloblen
);
1188 /* append signature */
1189 buffer_put_string(&b
, signature
, slen
);
1192 /* skip session id and packet type */
1193 if (buffer_len(&b
) < skip
+ 1)
1194 fatal("userauth_pubkey: internal error");
1195 buffer_consume(&b
, skip
+ 1);
1197 /* put remaining data from buffer into packet */
1198 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1199 packet_put_raw(buffer_ptr(&b
), buffer_len(&b
));
1207 send_pubkey_test(Authctxt
*authctxt
, Identity
*id
)
1210 u_int bloblen
, have_sig
= 0;
1212 debug3("send_pubkey_test");
1214 if (key_to_blob(id
->key
, &blob
, &bloblen
) == 0) {
1215 /* we cannot handle this key */
1216 debug3("send_pubkey_test: cannot handle key");
1219 /* register callback for USERAUTH_PK_OK message */
1220 dispatch_set(SSH2_MSG_USERAUTH_PK_OK
, &input_userauth_pk_ok
);
1222 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1223 packet_put_cstring(authctxt
->server_user
);
1224 packet_put_cstring(authctxt
->service
);
1225 packet_put_cstring(authctxt
->method
->name
);
1226 packet_put_char(have_sig
);
1227 if (!(datafellows
& SSH_BUG_PKAUTH
))
1228 packet_put_cstring(key_sign_encode(id
->key
));
1229 packet_put_string(blob
, bloblen
);
1235 static struct sshkey
*
1236 load_identity_file(Identity
*id
)
1238 struct sshkey
*private = NULL
;
1239 char prompt
[300], *passphrase
, *comment
;
1240 int r
, perm_ok
= 0, quit
= 0, i
;
1243 if (stat(id
->filename
, &st
) < 0) {
1244 (id
->userprovided
? logit
: debug3
)("no such identity: %s: %s",
1245 id
->filename
, strerror(errno
));
1248 snprintf(prompt
, sizeof prompt
,
1249 "Enter passphrase for key '%.100s': ", id
->filename
);
1250 for (i
= 0; i
<= options
.number_of_password_prompts
; i
++) {
1254 passphrase
= read_passphrase(prompt
, 0);
1255 if (*passphrase
== '\0') {
1256 debug2("no passphrase given, try next key");
1261 switch ((r
= sshkey_load_private_type(KEY_UNSPEC
, id
->filename
,
1262 passphrase
, &private, &comment
, &perm_ok
))) {
1265 case SSH_ERR_KEY_WRONG_PASSPHRASE
:
1266 if (options
.batch_mode
) {
1271 debug2("bad passphrase given, try again...");
1273 case SSH_ERR_SYSTEM_ERROR
:
1274 if (errno
== ENOENT
) {
1275 debug2("Load key \"%s\": %s",
1276 id
->filename
, ssh_err(r
));
1282 error("Load key \"%s\": %s", id
->filename
, ssh_err(r
));
1286 if (!quit
&& private != NULL
&& id
->agent_fd
== -1 &&
1287 !(id
->key
&& id
->isprivate
))
1288 maybe_add_key_to_agent(id
->filename
, private, comment
,
1291 explicit_bzero(passphrase
, strlen(passphrase
));
1295 if (private != NULL
|| quit
)
1302 * try keys in the following order:
1303 * 1. certificates listed in the config file
1304 * 2. other input certificates
1305 * 3. agent keys that are found in the config file
1306 * 4. other agent keys
1307 * 5. keys that are only listed in the config file
1310 pubkey_prepare(Authctxt
*authctxt
)
1312 struct identity
*id
, *id2
, *tmp
;
1313 struct idlist agent
, files
, *preferred
;
1315 int agent_fd
= -1, i
, r
, found
;
1317 struct ssh_identitylist
*idlist
;
1319 TAILQ_INIT(&agent
); /* keys from the agent */
1320 TAILQ_INIT(&files
); /* keys from the config file */
1321 preferred
= &authctxt
->keys
;
1322 TAILQ_INIT(preferred
); /* preferred order of keys */
1324 /* list of keys stored in the filesystem and PKCS#11 */
1325 for (i
= 0; i
< options
.num_identity_files
; i
++) {
1326 key
= options
.identity_keys
[i
];
1327 if (key
&& key
->cert
&& key
->cert
->type
!= SSH2_CERT_TYPE_USER
)
1329 options
.identity_keys
[i
] = NULL
;
1330 id
= xcalloc(1, sizeof(*id
));
1333 id
->filename
= xstrdup(options
.identity_files
[i
]);
1334 id
->userprovided
= options
.identity_file_userprovided
[i
];
1335 TAILQ_INSERT_TAIL(&files
, id
, next
);
1337 /* list of certificates specified by user */
1338 for (i
= 0; i
< options
.num_certificate_files
; i
++) {
1339 key
= options
.certificates
[i
];
1340 if (!key_is_cert(key
) || key
->cert
== NULL
||
1341 key
->cert
->type
!= SSH2_CERT_TYPE_USER
)
1343 id
= xcalloc(1, sizeof(*id
));
1346 id
->filename
= xstrdup(options
.certificate_files
[i
]);
1347 id
->userprovided
= options
.certificate_file_userprovided
[i
];
1348 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1350 /* list of keys supported by the agent */
1351 if ((r
= ssh_get_authentication_socket(&agent_fd
)) != 0) {
1352 if (r
!= SSH_ERR_AGENT_NOT_PRESENT
)
1353 debug("%s: ssh_get_authentication_socket: %s",
1354 __func__
, ssh_err(r
));
1355 } else if ((r
= ssh_fetch_identitylist(agent_fd
, &idlist
)) != 0) {
1356 if (r
!= SSH_ERR_AGENT_NO_IDENTITIES
)
1357 debug("%s: ssh_fetch_identitylist: %s",
1358 __func__
, ssh_err(r
));
1361 for (j
= 0; j
< idlist
->nkeys
; j
++) {
1363 TAILQ_FOREACH(id
, &files
, next
) {
1365 * agent keys from the config file are
1368 if (sshkey_equal(idlist
->keys
[j
], id
->key
)) {
1369 TAILQ_REMOVE(&files
, id
, next
);
1370 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1371 id
->agent_fd
= agent_fd
;
1376 if (!found
&& !options
.identities_only
) {
1377 id
= xcalloc(1, sizeof(*id
));
1378 /* XXX "steals" key/comment from idlist */
1379 id
->key
= idlist
->keys
[j
];
1380 id
->filename
= idlist
->comments
[j
];
1381 idlist
->keys
[j
] = NULL
;
1382 idlist
->comments
[j
] = NULL
;
1383 id
->agent_fd
= agent_fd
;
1384 TAILQ_INSERT_TAIL(&agent
, id
, next
);
1387 ssh_free_identitylist(idlist
);
1388 /* append remaining agent keys */
1389 for (id
= TAILQ_FIRST(&agent
); id
; id
= TAILQ_FIRST(&agent
)) {
1390 TAILQ_REMOVE(&agent
, id
, next
);
1391 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1393 authctxt
->agent_fd
= agent_fd
;
1395 /* Prefer PKCS11 keys that are explicitly listed */
1396 TAILQ_FOREACH_SAFE(id
, &files
, next
, tmp
) {
1397 if (id
->key
== NULL
|| (id
->key
->flags
& SSHKEY_FLAG_EXT
) == 0)
1400 TAILQ_FOREACH(id2
, &files
, next
) {
1401 if (id2
->key
== NULL
||
1402 (id2
->key
->flags
& SSHKEY_FLAG_EXT
) == 0)
1404 if (sshkey_equal(id
->key
, id2
->key
)) {
1405 TAILQ_REMOVE(&files
, id
, next
);
1406 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1411 /* If IdentitiesOnly set and key not found then don't use it */
1412 if (!found
&& options
.identities_only
) {
1413 TAILQ_REMOVE(&files
, id
, next
);
1414 explicit_bzero(id
, sizeof(*id
));
1418 /* append remaining keys from the config file */
1419 for (id
= TAILQ_FIRST(&files
); id
; id
= TAILQ_FIRST(&files
)) {
1420 TAILQ_REMOVE(&files
, id
, next
);
1421 TAILQ_INSERT_TAIL(preferred
, id
, next
);
1423 /* finally, filter by PubkeyAcceptedKeyTypes */
1424 TAILQ_FOREACH_SAFE(id
, preferred
, next
, id2
) {
1425 if (id
->key
!= NULL
&&
1426 match_pattern_list(sshkey_ssh_name(id
->key
),
1427 options
.pubkey_key_types
, 0) != 1) {
1428 debug("Skipping %s key %s - "
1429 "not in PubkeyAcceptedKeyTypes",
1430 sshkey_ssh_name(id
->key
), id
->filename
);
1431 TAILQ_REMOVE(preferred
, id
, next
);
1432 sshkey_free(id
->key
);
1434 memset(id
, 0, sizeof(*id
));
1437 debug2("key: %s (%p)%s%s", id
->filename
, id
->key
,
1438 id
->userprovided
? ", explicit" : "",
1439 id
->agent_fd
!= -1 ? ", agent" : "");
1444 pubkey_cleanup(Authctxt
*authctxt
)
1448 if (authctxt
->agent_fd
!= -1)
1449 ssh_close_authentication_socket(authctxt
->agent_fd
);
1450 for (id
= TAILQ_FIRST(&authctxt
->keys
); id
;
1451 id
= TAILQ_FIRST(&authctxt
->keys
)) {
1452 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1453 sshkey_free(id
->key
);
1460 pubkey_reset(Authctxt
*authctxt
)
1464 TAILQ_FOREACH(id
, &authctxt
->keys
, next
)
1469 try_identity(Identity
*id
)
1473 if (key_type_plain(id
->key
->type
) == KEY_RSA
&&
1474 (datafellows
& SSH_BUG_RSASIGMD5
) != 0) {
1475 debug("Skipped %s key %s for RSA/MD5 server",
1476 key_type(id
->key
), id
->filename
);
1483 userauth_pubkey(Authctxt
*authctxt
)
1489 while ((id
= TAILQ_FIRST(&authctxt
->keys
))) {
1492 /* move key to the end of the queue */
1493 TAILQ_REMOVE(&authctxt
->keys
, id
, next
);
1494 TAILQ_INSERT_TAIL(&authctxt
->keys
, id
, next
);
1496 * send a test message if we have the public key. for
1497 * encrypted keys we cannot do this and have to load the
1498 * private key instead
1500 if (id
->key
!= NULL
) {
1501 if (try_identity(id
)) {
1502 if ((fp
= sshkey_fingerprint(id
->key
,
1503 options
.fingerprint_hash
,
1504 SSH_FP_DEFAULT
)) == NULL
) {
1505 error("%s: sshkey_fingerprint failed",
1509 debug("Offering public key: %s %s %s",
1510 sshkey_type(id
->key
), fp
, id
->filename
);
1512 sent
= send_pubkey_test(authctxt
, id
);
1515 debug("Trying private key: %s", id
->filename
);
1516 id
->key
= load_identity_file(id
);
1517 if (id
->key
!= NULL
) {
1518 if (try_identity(id
)) {
1520 sent
= sign_and_send_pubkey(
1535 * Send userauth request message specifying keyboard-interactive method.
1538 userauth_kbdint(Authctxt
*authctxt
)
1540 static int attempt
= 0;
1542 if (attempt
++ >= options
.number_of_password_prompts
)
1544 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1545 if (attempt
> 1 && !authctxt
->info_req_seen
) {
1546 debug3("userauth_kbdint: disable: no info_req_seen");
1547 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, NULL
);
1551 debug2("userauth_kbdint");
1552 packet_start(SSH2_MSG_USERAUTH_REQUEST
);
1553 packet_put_cstring(authctxt
->server_user
);
1554 packet_put_cstring(authctxt
->service
);
1555 packet_put_cstring(authctxt
->method
->name
);
1556 packet_put_cstring(""); /* lang */
1557 packet_put_cstring(options
.kbd_interactive_devices
?
1558 options
.kbd_interactive_devices
: "");
1561 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST
, &input_userauth_info_req
);
1566 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1569 input_userauth_info_req(int type
, u_int32_t seq
, struct ssh
*ssh
)
1571 Authctxt
*authctxt
= ssh
->authctxt
;
1572 char *name
, *inst
, *lang
, *prompt
, *response
;
1573 u_int num_prompts
, i
;
1576 debug2("input_userauth_info_req");
1578 if (authctxt
== NULL
)
1579 fatal("input_userauth_info_req: no authentication context");
1581 authctxt
->info_req_seen
= 1;
1583 name
= packet_get_string(NULL
);
1584 inst
= packet_get_string(NULL
);
1585 lang
= packet_get_string(NULL
);
1586 if (strlen(name
) > 0)
1588 if (strlen(inst
) > 0)
1594 num_prompts
= packet_get_int();
1596 * Begin to build info response packet based on prompts requested.
1597 * We commit to providing the correct number of responses, so if
1598 * further on we run into a problem that prevents this, we have to
1599 * be sure and clean this up and send a correct error response.
1601 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE
);
1602 packet_put_int(num_prompts
);
1604 debug2("input_userauth_info_req: num_prompts %d", num_prompts
);
1605 for (i
= 0; i
< num_prompts
; i
++) {
1606 prompt
= packet_get_string(NULL
);
1607 echo
= packet_get_char();
1609 response
= read_passphrase(prompt
, echo
? RP_ECHO
: 0);
1611 packet_put_cstring(response
);
1612 explicit_bzero(response
, strlen(response
));
1616 packet_check_eom(); /* done with parsing incoming message. */
1618 packet_add_padding(64);
1624 ssh_keysign(struct sshkey
*key
, u_char
**sigp
, size_t *lenp
,
1625 const u_char
*data
, size_t datalen
)
1630 int i
, r
, to
[2], from
[2], status
, sock
= packet_get_connection_in();
1631 u_char rversion
= 0, version
= 2;
1632 void (*osigchld
)(int);
1637 if (stat(_PATH_SSH_KEY_SIGN
, &st
) < 0) {
1638 error("%s: not installed: %s", __func__
, strerror(errno
));
1641 if (fflush(stdout
) != 0) {
1642 error("%s: fflush: %s", __func__
, strerror(errno
));
1646 error("%s: pipe: %s", __func__
, strerror(errno
));
1649 if (pipe(from
) < 0) {
1650 error("%s: pipe: %s", __func__
, strerror(errno
));
1653 if ((pid
= fork()) < 0) {
1654 error("%s: fork: %s", __func__
, strerror(errno
));
1657 osigchld
= signal(SIGCHLD
, SIG_DFL
);
1659 /* keep the socket on exec */
1660 fcntl(sock
, F_SETFD
, 0);
1661 permanently_drop_suid(getuid());
1663 if (dup2(from
[1], STDOUT_FILENO
) < 0)
1664 fatal("%s: dup2: %s", __func__
, strerror(errno
));
1666 if (dup2(to
[0], STDIN_FILENO
) < 0)
1667 fatal("%s: dup2: %s", __func__
, strerror(errno
));
1670 /* Close everything but stdio and the socket */
1671 for (i
= STDERR_FILENO
+ 1; i
< sock
; i
++)
1673 closefrom(sock
+ 1);
1674 debug3("%s: [child] pid=%ld, exec %s",
1675 __func__
, (long)getpid(), _PATH_SSH_KEY_SIGN
);
1676 execl(_PATH_SSH_KEY_SIGN
, _PATH_SSH_KEY_SIGN
, (char *)NULL
);
1677 fatal("%s: exec(%s): %s", __func__
, _PATH_SSH_KEY_SIGN
,
1683 if ((b
= sshbuf_new()) == NULL
)
1684 fatal("%s: sshbuf_new failed", __func__
);
1685 /* send # of sock, data to be signed */
1686 if ((r
= sshbuf_put_u32(b
, sock
)) != 0 ||
1687 (r
= sshbuf_put_string(b
, data
, datalen
)) != 0)
1688 fatal("%s: buffer error: %s", __func__
, ssh_err(r
));
1689 if (ssh_msg_send(to
[1], version
, b
) == -1)
1690 fatal("%s: couldn't send request", __func__
);
1692 r
= ssh_msg_recv(from
[0], b
);
1696 error("%s: no reply", __func__
);
1701 while (waitpid(pid
, &status
, 0) < 0) {
1702 if (errno
!= EINTR
) {
1703 error("%s: waitpid %ld: %s",
1704 __func__
, (long)pid
, strerror(errno
));
1708 if (!WIFEXITED(status
)) {
1709 error("%s: exited abnormally", __func__
);
1712 if (WEXITSTATUS(status
) != 0) {
1713 error("%s: exited with status %d",
1714 __func__
, WEXITSTATUS(status
));
1717 if ((r
= sshbuf_get_u8(b
, &rversion
)) != 0) {
1718 error("%s: buffer error: %s", __func__
, ssh_err(r
));
1721 if (rversion
!= version
) {
1722 error("%s: bad version", __func__
);
1725 if ((r
= sshbuf_get_string(b
, sigp
, lenp
)) != 0) {
1726 error("%s: buffer error: %s", __func__
, ssh_err(r
));
1728 signal(SIGCHLD
, osigchld
);
1732 signal(SIGCHLD
, osigchld
);
1739 userauth_hostbased(Authctxt
*authctxt
)
1741 struct ssh
*ssh
= active_state
;
1742 struct sshkey
*private = NULL
;
1743 struct sshbuf
*b
= NULL
;
1744 const char *service
;
1745 u_char
*sig
= NULL
, *keyblob
= NULL
;
1746 char *fp
= NULL
, *chost
= NULL
, *lname
= NULL
;
1747 size_t siglen
= 0, keylen
= 0;
1748 int i
, r
, success
= 0;
1750 if (authctxt
->ktypes
== NULL
) {
1751 authctxt
->oktypes
= xstrdup(options
.hostbased_key_types
);
1752 authctxt
->ktypes
= authctxt
->oktypes
;
1756 * Work through each listed type pattern in HostbasedKeyTypes,
1757 * trying each hostkey that matches the type in turn.
1760 if (authctxt
->active_ktype
== NULL
)
1761 authctxt
->active_ktype
= strsep(&authctxt
->ktypes
, ",");
1762 if (authctxt
->active_ktype
== NULL
||
1763 *authctxt
->active_ktype
== '\0')
1765 debug3("%s: trying key type %s", __func__
,
1766 authctxt
->active_ktype
);
1768 /* check for a useful key */
1770 for (i
= 0; i
< authctxt
->sensitive
->nkeys
; i
++) {
1771 if (authctxt
->sensitive
->keys
[i
] == NULL
||
1772 authctxt
->sensitive
->keys
[i
]->type
== KEY_UNSPEC
)
1774 if (match_pattern_list(
1775 sshkey_ssh_name(authctxt
->sensitive
->keys
[i
]),
1776 authctxt
->active_ktype
, 0) != 1)
1778 /* we take and free the key */
1779 private = authctxt
->sensitive
->keys
[i
];
1780 authctxt
->sensitive
->keys
[i
] = NULL
;
1784 if (private != NULL
)
1786 /* No more keys of this type; advance */
1787 authctxt
->active_ktype
= NULL
;
1789 if (private == NULL
) {
1790 free(authctxt
->oktypes
);
1791 authctxt
->oktypes
= authctxt
->ktypes
= NULL
;
1792 authctxt
->active_ktype
= NULL
;
1793 debug("No more client hostkeys for hostbased authentication.");
1797 if ((fp
= sshkey_fingerprint(private, options
.fingerprint_hash
,
1798 SSH_FP_DEFAULT
)) == NULL
) {
1799 error("%s: sshkey_fingerprint failed", __func__
);
1802 debug("%s: trying hostkey %s %s",
1803 __func__
, sshkey_ssh_name(private), fp
);
1805 /* figure out a name for the client host */
1806 if ((lname
= get_local_name(packet_get_connection_in())) == NULL
) {
1807 error("%s: cannot get local ipaddr/name", __func__
);
1811 /* XXX sshbuf_put_stringf? */
1812 xasprintf(&chost
, "%s.", lname
);
1813 debug2("%s: chost %s", __func__
, chost
);
1815 service
= datafellows
& SSH_BUG_HBSERVICE
? "ssh-userauth" :
1818 /* construct data */
1819 if ((b
= sshbuf_new()) == NULL
) {
1820 error("%s: sshbuf_new failed", __func__
);
1823 if ((r
= sshkey_to_blob(private, &keyblob
, &keylen
)) != 0) {
1824 error("%s: sshkey_to_blob: %s", __func__
, ssh_err(r
));
1827 if ((r
= sshbuf_put_string(b
, session_id2
, session_id2_len
)) != 0 ||
1828 (r
= sshbuf_put_u8(b
, SSH2_MSG_USERAUTH_REQUEST
)) != 0 ||
1829 (r
= sshbuf_put_cstring(b
, authctxt
->server_user
)) != 0 ||
1830 (r
= sshbuf_put_cstring(b
, service
)) != 0 ||
1831 (r
= sshbuf_put_cstring(b
, authctxt
->method
->name
)) != 0 ||
1832 (r
= sshbuf_put_cstring(b
, key_ssh_name(private))) != 0 ||
1833 (r
= sshbuf_put_string(b
, keyblob
, keylen
)) != 0 ||
1834 (r
= sshbuf_put_cstring(b
, chost
)) != 0 ||
1835 (r
= sshbuf_put_cstring(b
, authctxt
->local_user
)) != 0) {
1836 error("%s: buffer error: %s", __func__
, ssh_err(r
));
1841 sshbuf_dump(b
, stderr
);
1843 if (authctxt
->sensitive
->external_keysign
)
1844 r
= ssh_keysign(private, &sig
, &siglen
,
1845 sshbuf_ptr(b
), sshbuf_len(b
));
1846 else if ((r
= sshkey_sign(private, &sig
, &siglen
,
1847 sshbuf_ptr(b
), sshbuf_len(b
), NULL
, datafellows
)) != 0)
1848 debug("%s: sshkey_sign: %s", __func__
, ssh_err(r
));
1850 error("sign using hostkey %s %s failed",
1851 sshkey_ssh_name(private), fp
);
1854 if ((r
= sshpkt_start(ssh
, SSH2_MSG_USERAUTH_REQUEST
)) != 0 ||
1855 (r
= sshpkt_put_cstring(ssh
, authctxt
->server_user
)) != 0 ||
1856 (r
= sshpkt_put_cstring(ssh
, authctxt
->service
)) != 0 ||
1857 (r
= sshpkt_put_cstring(ssh
, authctxt
->method
->name
)) != 0 ||
1858 (r
= sshpkt_put_cstring(ssh
, key_ssh_name(private))) != 0 ||
1859 (r
= sshpkt_put_string(ssh
, keyblob
, keylen
)) != 0 ||
1860 (r
= sshpkt_put_cstring(ssh
, chost
)) != 0 ||
1861 (r
= sshpkt_put_cstring(ssh
, authctxt
->local_user
)) != 0 ||
1862 (r
= sshpkt_put_string(ssh
, sig
, siglen
)) != 0 ||
1863 (r
= sshpkt_send(ssh
)) != 0) {
1864 error("%s: packet error: %s", __func__
, ssh_err(r
));
1871 explicit_bzero(sig
, siglen
);
1878 sshkey_free(private);
1884 /* find auth method */
1887 * given auth method name, if configurable options permit this method fill
1888 * in auth_ident field and return true, otherwise return false.
1891 authmethod_is_enabled(Authmethod
*method
)
1895 /* return false if options indicate this method is disabled */
1896 if (method
->enabled
== NULL
|| *method
->enabled
== 0)
1898 /* return false if batch mode is enabled but method needs interactive mode */
1899 if (method
->batch_flag
!= NULL
&& *method
->batch_flag
!= 0)
1905 authmethod_lookup(const char *name
)
1907 Authmethod
*method
= NULL
;
1909 for (method
= authmethods
; method
->name
!= NULL
; method
++)
1910 if (strcmp(name
, method
->name
) == 0)
1912 debug2("Unrecognized authentication method name: %s", name
? name
: "NULL");
1916 /* XXX internal state */
1917 static Authmethod
*current
= NULL
;
1918 static char *supported
= NULL
;
1919 static char *preferred
= NULL
;
1922 * Given the authentication method list sent by the server, return the
1923 * next method we should try. If the server initially sends a nil list,
1924 * use a built-in default list.
1927 authmethod_get(char *authlist
)
1932 /* Use a suitable default if we're passed a nil list. */
1933 if (authlist
== NULL
|| strlen(authlist
) == 0)
1934 authlist
= options
.preferred_authentications
;
1936 if (supported
== NULL
|| strcmp(authlist
, supported
) != 0) {
1937 debug3("start over, passed a different list %s", authlist
);
1939 supported
= xstrdup(authlist
);
1940 preferred
= options
.preferred_authentications
;
1941 debug3("preferred %s", preferred
);
1943 } else if (current
!= NULL
&& authmethod_is_enabled(current
))
1947 if ((name
= match_list(preferred
, supported
, &next
)) == NULL
) {
1948 debug("No more authentication methods to try.");
1953 debug3("authmethod_lookup %s", name
);
1954 debug3("remaining preferred: %s", preferred
);
1955 if ((current
= authmethod_lookup(name
)) != NULL
&&
1956 authmethod_is_enabled(current
)) {
1957 debug3("authmethod_is_enabled %s", name
);
1958 debug("Next authentication method: %s", name
);
1967 authmethods_get(void)
1969 Authmethod
*method
= NULL
;
1974 for (method
= authmethods
; method
->name
!= NULL
; method
++) {
1975 if (authmethod_is_enabled(method
)) {
1976 if (buffer_len(&b
) > 0)
1977 buffer_append(&b
, ",", 1);
1978 buffer_append(&b
, method
->name
, strlen(method
->name
));
1981 if ((list
= sshbuf_dup_string(&b
)) == NULL
)
1982 fatal("%s: sshbuf_dup_string failed", __func__
);