1 /* $OpenBSD: sshconnect2.c,v 1.204 2014/02/02 03:44:32 djm Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Copyright (c) 2008 Damien Miller. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
30 #include <sys/socket.h>
43 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
47 #include "openbsd-compat/sys-queue.h"
58 #include "myproposal.h"
59 #include "sshconnect.h"
70 #include "pathnames.h"
79 extern char *client_version_string;
80 extern char *server_version_string;
81 extern Options options;
87 u_char *session_id2 = NULL;
88 u_int session_id2_len = 0;
91 struct sockaddr *xxx_hostaddr;
96 verify_host_key_callback(Key *hostkey)
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 = key_type_from_name(alg)) == KEY_UNSPEC)
135 fatal("%s: unknown alg %s", __func__, alg);
136 if (lookup_key_in_hostkeys_by_type(hostkeys,
137 key_type_plain(ktype), NULL))
138 ALG_APPEND(first, alg);
140 ALG_APPEND(last, alg);
143 xasprintf(&ret, "%s%s%s", first, *first == '\0' ? "" : ",", last);
145 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
151 free_hostkeys(hostkeys);
157 ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
162 xxx_hostaddr = hostaddr;
164 if (options.ciphers == (char *)-1) {
165 logit("No valid ciphers for protocol version 2 given, using defaults.");
166 options.ciphers = NULL;
168 if (options.ciphers != NULL) {
169 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
170 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
172 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
173 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
174 myproposal[PROPOSAL_ENC_ALGS_STOC] =
175 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
176 if (options.compression) {
177 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
178 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
180 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
181 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
183 if (options.macs != NULL) {
184 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
185 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
187 if (options.hostkeyalgorithms != NULL)
188 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
189 compat_pkalg_proposal(options.hostkeyalgorithms);
191 /* Prefer algorithms that we already have keys for */
192 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
193 compat_pkalg_proposal(
194 order_hostkeyalgs(host, hostaddr, port));
196 if (options.kex_algorithms != NULL)
197 myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
198 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(
199 myproposal[PROPOSAL_KEX_ALGS]);
201 if (options.rekey_limit || options.rekey_interval)
202 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
203 (time_t)options.rekey_interval);
205 /* start key exchange */
206 kex = kex_setup(myproposal);
207 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
208 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
209 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
210 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
211 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
212 kex->kex[KEX_C25519_SHA256] = kexc25519_client;
213 kex->client_version_string=client_version_string;
214 kex->server_version_string=server_version_string;
215 kex->verify_host_key=&verify_host_key_callback;
219 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
221 if (options.use_roaming && !kex->roaming) {
222 debug("Roaming not allowed by server");
223 options.use_roaming = 0;
226 session_id2 = kex->session_id;
227 session_id2_len = kex->session_id_len;
230 /* send 1st encrypted/maced/compressed message */
231 packet_start(SSH2_MSG_IGNORE);
232 packet_put_cstring("markus");
242 typedef struct Authctxt Authctxt;
243 typedef struct Authmethod Authmethod;
244 typedef struct identity Identity;
245 typedef struct idlist Idlist;
248 TAILQ_ENTRY(identity) next;
249 AuthenticationConnection *ac; /* set if agent supports key */
250 Key *key; /* public/private key */
251 char *filename; /* comment for agent-only keys */
253 int isprivate; /* key points to the private key */
256 TAILQ_HEAD(idlist, identity);
259 const char *server_user;
260 const char *local_user;
264 sig_atomic_t success;
268 AuthenticationConnection *agent;
270 Sensitive *sensitive;
271 /* kbd-interactive */
277 char *name; /* string to compare against server's list */
278 int (*userauth)(Authctxt *authctxt);
279 void (*cleanup)(Authctxt *authctxt);
280 int *enabled; /* flag in option struct that enables method */
281 int *batch_flag; /* flag in option struct that disables method */
284 void input_userauth_success(int, u_int32_t, void *);
285 void input_userauth_success_unexpected(int, u_int32_t, void *);
286 void input_userauth_failure(int, u_int32_t, void *);
287 void input_userauth_banner(int, u_int32_t, void *);
288 void input_userauth_error(int, u_int32_t, void *);
289 void input_userauth_info_req(int, u_int32_t, void *);
290 void input_userauth_pk_ok(int, u_int32_t, void *);
291 void input_userauth_passwd_changereq(int, u_int32_t, void *);
293 int userauth_none(Authctxt *);
294 int userauth_pubkey(Authctxt *);
295 int userauth_passwd(Authctxt *);
296 int userauth_kbdint(Authctxt *);
297 int userauth_hostbased(Authctxt *);
300 int userauth_gssapi(Authctxt *authctxt);
301 void input_gssapi_response(int type, u_int32_t, void *);
302 void input_gssapi_token(int type, u_int32_t, void *);
303 void input_gssapi_hash(int type, u_int32_t, void *);
304 void input_gssapi_error(int, u_int32_t, void *);
305 void input_gssapi_errtok(int, u_int32_t, void *);
308 void userauth(Authctxt *, char *);
310 static int sign_and_send_pubkey(Authctxt *, Identity *);
311 static void pubkey_prepare(Authctxt *);
312 static void pubkey_cleanup(Authctxt *);
313 static Key *load_identity_file(char *, int);
315 static Authmethod *authmethod_get(char *authlist);
316 static Authmethod *authmethod_lookup(const char *name);
317 static char *authmethods_get(void);
319 Authmethod authmethods[] = {
324 &options.gss_authentication,
330 &options.hostbased_authentication,
335 &options.pubkey_authentication,
337 {"keyboard-interactive",
340 &options.kbd_interactive_authentication,
341 &options.batch_mode},
345 &options.password_authentication,
346 &options.batch_mode},
352 {NULL, NULL, NULL, NULL, NULL}
356 ssh_userauth2(const char *local_user, const char *server_user, char *host,
357 Sensitive *sensitive)
362 if (options.challenge_response_authentication)
363 options.kbd_interactive_authentication = 1;
365 packet_start(SSH2_MSG_SERVICE_REQUEST);
366 packet_put_cstring("ssh-userauth");
368 debug("SSH2_MSG_SERVICE_REQUEST sent");
370 type = packet_read();
371 if (type != SSH2_MSG_SERVICE_ACCEPT)
372 fatal("Server denied authentication request: %d", type);
373 if (packet_remaining() > 0) {
374 char *reply = packet_get_string(NULL);
375 debug2("service_accept: %s", reply);
378 debug2("buggy server: service_accept w/o service");
381 debug("SSH2_MSG_SERVICE_ACCEPT received");
383 if (options.preferred_authentications == NULL)
384 options.preferred_authentications = authmethods_get();
386 /* setup authentication context */
387 memset(&authctxt, 0, sizeof(authctxt));
388 pubkey_prepare(&authctxt);
389 authctxt.server_user = server_user;
390 authctxt.local_user = local_user;
391 authctxt.host = host;
392 authctxt.service = "ssh-connection"; /* service name */
393 authctxt.success = 0;
394 authctxt.method = authmethod_lookup("none");
395 authctxt.authlist = NULL;
396 authctxt.methoddata = NULL;
397 authctxt.sensitive = sensitive;
398 authctxt.info_req_seen = 0;
399 if (authctxt.method == NULL)
400 fatal("ssh_userauth2: internal error: cannot send userauth none request");
402 /* initial userauth request */
403 userauth_none(&authctxt);
405 dispatch_init(&input_userauth_error);
406 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
407 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
408 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
409 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
411 pubkey_cleanup(&authctxt);
412 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
414 debug("Authentication succeeded (%s).", authctxt.method->name);
418 userauth(Authctxt *authctxt, char *authlist)
420 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
421 authctxt->method->cleanup(authctxt);
423 free(authctxt->methoddata);
424 authctxt->methoddata = NULL;
425 if (authlist == NULL) {
426 authlist = authctxt->authlist;
428 free(authctxt->authlist);
429 authctxt->authlist = authlist;
432 Authmethod *method = authmethod_get(authlist);
434 fatal("Permission denied (%s).", authlist);
435 authctxt->method = method;
437 /* reset the per method handler */
438 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
439 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
441 /* and try new method */
442 if (method->userauth(authctxt) != 0) {
443 debug2("we sent a %s packet, wait for reply", method->name);
446 debug2("we did not send a packet, disable method");
447 method->enabled = NULL;
454 input_userauth_error(int type, u_int32_t seq, void *ctxt)
456 fatal("input_userauth_error: bad message during authentication: "
462 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
464 char *msg, *raw, *lang;
467 debug3("input_userauth_banner");
468 raw = packet_get_string(&len);
469 lang = packet_get_string(NULL);
470 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
473 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
474 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
475 fprintf(stderr, "%s", msg);
484 input_userauth_success(int type, u_int32_t seq, void *ctxt)
486 Authctxt *authctxt = ctxt;
488 if (authctxt == NULL)
489 fatal("input_userauth_success: no authentication context");
490 free(authctxt->authlist);
491 authctxt->authlist = NULL;
492 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
493 authctxt->method->cleanup(authctxt);
494 free(authctxt->methoddata);
495 authctxt->methoddata = NULL;
496 authctxt->success = 1; /* break out */
500 input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
502 Authctxt *authctxt = ctxt;
504 if (authctxt == NULL)
505 fatal("%s: no authentication context", __func__);
507 fatal("Unexpected authentication success during %s.",
508 authctxt->method->name);
513 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
515 Authctxt *authctxt = ctxt;
516 char *authlist = NULL;
519 if (authctxt == NULL)
520 fatal("input_userauth_failure: no authentication context");
522 authlist = packet_get_string(NULL);
523 partial = packet_get_char();
527 logit("Authenticated with partial success.");
529 pubkey_cleanup(authctxt);
530 pubkey_prepare(authctxt);
532 debug("Authentications that can continue: %s", authlist);
534 userauth(authctxt, authlist);
539 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
541 Authctxt *authctxt = ctxt;
545 int pktype, sent = 0;
550 if (authctxt == NULL)
551 fatal("input_userauth_pk_ok: no authentication context");
552 if (datafellows & SSH_BUG_PKOK) {
553 /* this is similar to SSH_BUG_PKAUTH */
554 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
555 pkblob = packet_get_string(&blen);
557 buffer_append(&b, pkblob, blen);
558 pkalg = buffer_get_string(&b, &alen);
561 pkalg = packet_get_string(&alen);
562 pkblob = packet_get_string(&blen);
566 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
568 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
569 debug("unknown pkalg %s", pkalg);
572 if ((key = key_from_blob(pkblob, blen)) == NULL) {
573 debug("no key from blob. pkalg %s", pkalg);
576 if (key->type != pktype) {
577 error("input_userauth_pk_ok: type mismatch "
578 "for decoded key (received %d, expected %d)",
582 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
583 debug2("input_userauth_pk_ok: fp %s", fp);
587 * search keys in the reverse order, because last candidate has been
588 * moved to the end of the queue. this also avoids confusion by
591 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
592 if (key_equal(key, id->key)) {
593 sent = sign_and_send_pubkey(authctxt, id);
603 /* try another method if we did not send a packet */
605 userauth(authctxt, NULL);
610 userauth_gssapi(Authctxt *authctxt)
612 Gssctxt *gssctxt = NULL;
613 static gss_OID_set gss_supported = NULL;
614 static u_int mech = 0;
618 /* Try one GSSAPI method at a time, rather than sending them all at
621 if (gss_supported == NULL)
622 gss_indicate_mechs(&min, &gss_supported);
624 /* Check to see if the mechanism is usable before we offer it */
625 while (mech < gss_supported->count && !ok) {
626 /* My DER encoding requires length<128 */
627 if (gss_supported->elements[mech].length < 128 &&
628 ssh_gssapi_check_mechanism(&gssctxt,
629 &gss_supported->elements[mech], authctxt->host)) {
630 ok = 1; /* Mechanism works */
639 authctxt->methoddata=(void *)gssctxt;
641 packet_start(SSH2_MSG_USERAUTH_REQUEST);
642 packet_put_cstring(authctxt->server_user);
643 packet_put_cstring(authctxt->service);
644 packet_put_cstring(authctxt->method->name);
648 packet_put_int((gss_supported->elements[mech].length) + 2);
649 packet_put_char(SSH_GSS_OIDTYPE);
650 packet_put_char(gss_supported->elements[mech].length);
651 packet_put_raw(gss_supported->elements[mech].elements,
652 gss_supported->elements[mech].length);
656 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
657 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
658 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
659 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
661 mech++; /* Move along to next candidate */
667 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
669 Authctxt *authctxt = ctxt;
670 Gssctxt *gssctxt = authctxt->methoddata;
671 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
672 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
673 gss_buffer_desc gssbuf;
674 OM_uint32 status, ms, flags;
677 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
678 recv_tok, &send_tok, &flags);
680 if (send_tok.length > 0) {
681 if (GSS_ERROR(status))
682 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
684 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
686 packet_put_string(send_tok.value, send_tok.length);
688 gss_release_buffer(&ms, &send_tok);
691 if (status == GSS_S_COMPLETE) {
692 /* send either complete or MIC, depending on mechanism */
693 if (!(flags & GSS_C_INTEG_FLAG)) {
694 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
697 ssh_gssapi_buildmic(&b, authctxt->server_user,
698 authctxt->service, "gssapi-with-mic");
700 gssbuf.value = buffer_ptr(&b);
701 gssbuf.length = buffer_len(&b);
703 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
705 if (!GSS_ERROR(status)) {
706 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
707 packet_put_string(mic.value, mic.length);
713 gss_release_buffer(&ms, &mic);
722 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
724 Authctxt *authctxt = ctxt;
729 if (authctxt == NULL)
730 fatal("input_gssapi_response: no authentication context");
731 gssctxt = authctxt->methoddata;
734 oidv = packet_get_string(&oidlen);
737 oidv[0] != SSH_GSS_OIDTYPE ||
738 oidv[1] != oidlen - 2) {
740 debug("Badly encoded mechanism OID received");
741 userauth(authctxt, NULL);
745 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
746 fatal("Server returned different OID than expected");
752 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
753 /* Start again with next method on list */
754 debug("Trying to start again");
755 userauth(authctxt, NULL);
762 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
764 Authctxt *authctxt = ctxt;
765 gss_buffer_desc recv_tok;
769 if (authctxt == NULL)
770 fatal("input_gssapi_response: no authentication context");
772 recv_tok.value = packet_get_string(&slen);
773 recv_tok.length = slen; /* safe typecast */
777 status = process_gssapi_token(ctxt, &recv_tok);
779 free(recv_tok.value);
781 if (GSS_ERROR(status)) {
782 /* Start again with the next method in the list */
783 userauth(authctxt, NULL);
790 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
792 Authctxt *authctxt = ctxt;
794 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
795 gss_buffer_desc recv_tok;
799 if (authctxt == NULL)
800 fatal("input_gssapi_response: no authentication context");
801 gssctxt = authctxt->methoddata;
803 recv_tok.value = packet_get_string(&len);
804 recv_tok.length = len;
808 /* Stick it into GSSAPI and see what it says */
809 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
810 &recv_tok, &send_tok, NULL);
812 free(recv_tok.value);
813 gss_release_buffer(&ms, &send_tok);
815 /* Server will be returning a failed packet after this one */
820 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
825 /* maj */(void)packet_get_int();
826 /* min */(void)packet_get_int();
827 msg=packet_get_string(NULL);
828 lang=packet_get_string(NULL);
832 debug("Server GSSAPI Error:\n%s", msg);
839 userauth_none(Authctxt *authctxt)
841 /* initial userauth request */
842 packet_start(SSH2_MSG_USERAUTH_REQUEST);
843 packet_put_cstring(authctxt->server_user);
844 packet_put_cstring(authctxt->service);
845 packet_put_cstring(authctxt->method->name);
851 userauth_passwd(Authctxt *authctxt)
853 static int attempt = 0;
856 const char *host = options.host_key_alias ? options.host_key_alias :
859 if (attempt++ >= options.number_of_password_prompts)
863 error("Permission denied, please try again.");
865 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
866 authctxt->server_user, host);
867 password = read_passphrase(prompt, 0);
868 packet_start(SSH2_MSG_USERAUTH_REQUEST);
869 packet_put_cstring(authctxt->server_user);
870 packet_put_cstring(authctxt->service);
871 packet_put_cstring(authctxt->method->name);
873 packet_put_cstring(password);
874 explicit_bzero(password, strlen(password));
876 packet_add_padding(64);
879 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
880 &input_userauth_passwd_changereq);
886 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
890 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
892 Authctxt *authctxt = ctxt;
893 char *info, *lang, *password = NULL, *retype = NULL;
895 const char *host = options.host_key_alias ? options.host_key_alias :
898 debug2("input_userauth_passwd_changereq");
900 if (authctxt == NULL)
901 fatal("input_userauth_passwd_changereq: "
902 "no authentication context");
904 info = packet_get_string(NULL);
905 lang = packet_get_string(NULL);
906 if (strlen(info) > 0)
910 packet_start(SSH2_MSG_USERAUTH_REQUEST);
911 packet_put_cstring(authctxt->server_user);
912 packet_put_cstring(authctxt->service);
913 packet_put_cstring(authctxt->method->name);
914 packet_put_char(1); /* additional info */
915 snprintf(prompt, sizeof(prompt),
916 "Enter %.30s@%.128s's old password: ",
917 authctxt->server_user, host);
918 password = read_passphrase(prompt, 0);
919 packet_put_cstring(password);
920 explicit_bzero(password, strlen(password));
923 while (password == NULL) {
924 snprintf(prompt, sizeof(prompt),
925 "Enter %.30s@%.128s's new password: ",
926 authctxt->server_user, host);
927 password = read_passphrase(prompt, RP_ALLOW_EOF);
928 if (password == NULL) {
932 snprintf(prompt, sizeof(prompt),
933 "Retype %.30s@%.128s's new password: ",
934 authctxt->server_user, host);
935 retype = read_passphrase(prompt, 0);
936 if (strcmp(password, retype) != 0) {
937 explicit_bzero(password, strlen(password));
939 logit("Mismatch; try again, EOF to quit.");
942 explicit_bzero(retype, strlen(retype));
945 packet_put_cstring(password);
946 explicit_bzero(password, strlen(password));
948 packet_add_padding(64);
951 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
952 &input_userauth_passwd_changereq);
956 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
957 u_char *data, u_int datalen)
962 /* the agent supports this key */
964 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
967 * we have already loaded the private key or
968 * the private key is stored in external hardware
970 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
971 return (key_sign(id->key, sigp, lenp, data, datalen));
972 /* load the private key from the file */
973 if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL)
975 ret = key_sign(prv, sigp, lenp, data, datalen);
981 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
984 u_char *blob, *signature;
991 fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
992 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
995 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
996 /* we cannot handle this key */
997 debug3("sign_and_send_pubkey: cannot handle key");
1000 /* data to be signed */
1002 if (datafellows & SSH_OLD_SESSIONID) {
1003 buffer_append(&b, session_id2, session_id2_len);
1004 skip = session_id2_len;
1006 buffer_put_string(&b, session_id2, session_id2_len);
1007 skip = buffer_len(&b);
1009 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1010 buffer_put_cstring(&b, authctxt->server_user);
1011 buffer_put_cstring(&b,
1012 datafellows & SSH_BUG_PKSERVICE ?
1015 if (datafellows & SSH_BUG_PKAUTH) {
1016 buffer_put_char(&b, have_sig);
1018 buffer_put_cstring(&b, authctxt->method->name);
1019 buffer_put_char(&b, have_sig);
1020 buffer_put_cstring(&b, key_ssh_name(id->key));
1022 buffer_put_string(&b, blob, bloblen);
1024 /* generate signature */
1025 ret = identity_sign(id, &signature, &slen,
1026 buffer_ptr(&b), buffer_len(&b));
1035 if (datafellows & SSH_BUG_PKSERVICE) {
1037 buffer_append(&b, session_id2, session_id2_len);
1038 skip = session_id2_len;
1039 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1040 buffer_put_cstring(&b, authctxt->server_user);
1041 buffer_put_cstring(&b, authctxt->service);
1042 buffer_put_cstring(&b, authctxt->method->name);
1043 buffer_put_char(&b, have_sig);
1044 if (!(datafellows & SSH_BUG_PKAUTH))
1045 buffer_put_cstring(&b, key_ssh_name(id->key));
1046 buffer_put_string(&b, blob, bloblen);
1050 /* append signature */
1051 buffer_put_string(&b, signature, slen);
1054 /* skip session id and packet type */
1055 if (buffer_len(&b) < skip + 1)
1056 fatal("userauth_pubkey: internal error");
1057 buffer_consume(&b, skip + 1);
1059 /* put remaining data from buffer into packet */
1060 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1061 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1069 send_pubkey_test(Authctxt *authctxt, Identity *id)
1072 u_int bloblen, have_sig = 0;
1074 debug3("send_pubkey_test");
1076 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1077 /* we cannot handle this key */
1078 debug3("send_pubkey_test: cannot handle key");
1081 /* register callback for USERAUTH_PK_OK message */
1082 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1084 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1085 packet_put_cstring(authctxt->server_user);
1086 packet_put_cstring(authctxt->service);
1087 packet_put_cstring(authctxt->method->name);
1088 packet_put_char(have_sig);
1089 if (!(datafellows & SSH_BUG_PKAUTH))
1090 packet_put_cstring(key_ssh_name(id->key));
1091 packet_put_string(blob, bloblen);
1098 load_identity_file(char *filename, int userprovided)
1101 char prompt[300], *passphrase;
1102 int perm_ok = 0, quit, i;
1105 if (stat(filename, &st) < 0) {
1106 (userprovided ? logit : debug3)("no such identity: %s: %s",
1107 filename, strerror(errno));
1110 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1112 if (private != NULL)
1116 if (private == NULL) {
1117 if (options.batch_mode)
1119 snprintf(prompt, sizeof prompt,
1120 "Enter passphrase for key '%.100s': ", filename);
1121 for (i = 0; i < options.number_of_password_prompts; i++) {
1122 passphrase = read_passphrase(prompt, 0);
1123 if (strcmp(passphrase, "") != 0) {
1124 private = key_load_private_type(KEY_UNSPEC,
1125 filename, passphrase, NULL, NULL);
1128 debug2("no passphrase given, try next key");
1131 explicit_bzero(passphrase, strlen(passphrase));
1133 if (private != NULL || quit)
1135 debug2("bad passphrase given, try again...");
1142 * try keys in the following order:
1143 * 1. agent keys that are found in the config file
1144 * 2. other agent keys
1145 * 3. keys that are only listed in the config file
1148 pubkey_prepare(Authctxt *authctxt)
1150 Identity *id, *id2, *tmp;
1151 Idlist agent, files, *preferred;
1153 AuthenticationConnection *ac;
1157 TAILQ_INIT(&agent); /* keys from the agent */
1158 TAILQ_INIT(&files); /* keys from the config file */
1159 preferred = &authctxt->keys;
1160 TAILQ_INIT(preferred); /* preferred order of keys */
1162 /* list of keys stored in the filesystem and PKCS#11 */
1163 for (i = 0; i < options.num_identity_files; i++) {
1164 key = options.identity_keys[i];
1165 if (key && key->type == KEY_RSA1)
1167 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1169 options.identity_keys[i] = NULL;
1170 id = xcalloc(1, sizeof(*id));
1172 id->filename = xstrdup(options.identity_files[i]);
1173 id->userprovided = options.identity_file_userprovided[i];
1174 TAILQ_INSERT_TAIL(&files, id, next);
1176 /* Prefer PKCS11 keys that are explicitly listed */
1177 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1178 if (id->key == NULL || (id->key->flags & KEY_FLAG_EXT) == 0)
1181 TAILQ_FOREACH(id2, &files, next) {
1182 if (id2->key == NULL ||
1183 (id2->key->flags & KEY_FLAG_EXT) != 0)
1185 if (key_equal(id->key, id2->key)) {
1186 TAILQ_REMOVE(&files, id, next);
1187 TAILQ_INSERT_TAIL(preferred, id, next);
1192 /* If IdentitiesOnly set and key not found then don't use it */
1193 if (!found && options.identities_only) {
1194 TAILQ_REMOVE(&files, id, next);
1195 explicit_bzero(id, sizeof(*id));
1199 /* list of keys supported by the agent */
1200 if ((ac = ssh_get_authentication_connection())) {
1201 for (key = ssh_get_first_identity(ac, &comment, 2);
1203 key = ssh_get_next_identity(ac, &comment, 2)) {
1205 TAILQ_FOREACH(id, &files, next) {
1206 /* agent keys from the config file are preferred */
1207 if (key_equal(key, id->key)) {
1210 TAILQ_REMOVE(&files, id, next);
1211 TAILQ_INSERT_TAIL(preferred, id, next);
1217 if (!found && !options.identities_only) {
1218 id = xcalloc(1, sizeof(*id));
1220 id->filename = comment;
1222 TAILQ_INSERT_TAIL(&agent, id, next);
1225 /* append remaining agent keys */
1226 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1227 TAILQ_REMOVE(&agent, id, next);
1228 TAILQ_INSERT_TAIL(preferred, id, next);
1230 authctxt->agent = ac;
1232 /* append remaining keys from the config file */
1233 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1234 TAILQ_REMOVE(&files, id, next);
1235 TAILQ_INSERT_TAIL(preferred, id, next);
1237 TAILQ_FOREACH(id, preferred, next) {
1238 debug2("key: %s (%p),%s", id->filename, id->key,
1239 id->userprovided ? " explicit" : "");
1244 pubkey_cleanup(Authctxt *authctxt)
1248 if (authctxt->agent != NULL)
1249 ssh_close_authentication_connection(authctxt->agent);
1250 for (id = TAILQ_FIRST(&authctxt->keys); id;
1251 id = TAILQ_FIRST(&authctxt->keys)) {
1252 TAILQ_REMOVE(&authctxt->keys, id, next);
1261 userauth_pubkey(Authctxt *authctxt)
1266 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1269 /* move key to the end of the queue */
1270 TAILQ_REMOVE(&authctxt->keys, id, next);
1271 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1273 * send a test message if we have the public key. for
1274 * encrypted keys we cannot do this and have to load the
1275 * private key instead
1277 if (id->key != NULL) {
1278 if (key_type_plain(id->key->type) == KEY_RSA &&
1279 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1280 debug("Skipped %s key %s for RSA/MD5 server",
1281 key_type(id->key), id->filename);
1282 } else if (id->key->type != KEY_RSA1) {
1283 debug("Offering %s public key: %s",
1284 key_type(id->key), id->filename);
1285 sent = send_pubkey_test(authctxt, id);
1288 debug("Trying private key: %s", id->filename);
1289 id->key = load_identity_file(id->filename,
1291 if (id->key != NULL) {
1293 if (key_type_plain(id->key->type) == KEY_RSA &&
1294 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1295 debug("Skipped %s key %s for RSA/MD5 "
1296 "server", key_type(id->key),
1299 sent = sign_and_send_pubkey(
1313 * Send userauth request message specifying keyboard-interactive method.
1316 userauth_kbdint(Authctxt *authctxt)
1318 static int attempt = 0;
1320 if (attempt++ >= options.number_of_password_prompts)
1322 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1323 if (attempt > 1 && !authctxt->info_req_seen) {
1324 debug3("userauth_kbdint: disable: no info_req_seen");
1325 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1329 debug2("userauth_kbdint");
1330 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1331 packet_put_cstring(authctxt->server_user);
1332 packet_put_cstring(authctxt->service);
1333 packet_put_cstring(authctxt->method->name);
1334 packet_put_cstring(""); /* lang */
1335 packet_put_cstring(options.kbd_interactive_devices ?
1336 options.kbd_interactive_devices : "");
1339 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1344 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1347 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1349 Authctxt *authctxt = ctxt;
1350 char *name, *inst, *lang, *prompt, *response;
1351 u_int num_prompts, i;
1354 debug2("input_userauth_info_req");
1356 if (authctxt == NULL)
1357 fatal("input_userauth_info_req: no authentication context");
1359 authctxt->info_req_seen = 1;
1361 name = packet_get_string(NULL);
1362 inst = packet_get_string(NULL);
1363 lang = packet_get_string(NULL);
1364 if (strlen(name) > 0)
1366 if (strlen(inst) > 0)
1372 num_prompts = packet_get_int();
1374 * Begin to build info response packet based on prompts requested.
1375 * We commit to providing the correct number of responses, so if
1376 * further on we run into a problem that prevents this, we have to
1377 * be sure and clean this up and send a correct error response.
1379 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1380 packet_put_int(num_prompts);
1382 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1383 for (i = 0; i < num_prompts; i++) {
1384 prompt = packet_get_string(NULL);
1385 echo = packet_get_char();
1387 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1389 packet_put_cstring(response);
1390 explicit_bzero(response, strlen(response));
1394 packet_check_eom(); /* done with parsing incoming message. */
1396 packet_add_padding(64);
1401 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1402 u_char *data, u_int datalen)
1407 int to[2], from[2], status, version = 2;
1409 debug2("ssh_keysign called");
1411 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1412 error("ssh_keysign: not installed: %s", strerror(errno));
1415 if (fflush(stdout) != 0)
1416 error("ssh_keysign: fflush: %s", strerror(errno));
1418 error("ssh_keysign: pipe: %s", strerror(errno));
1421 if (pipe(from) < 0) {
1422 error("ssh_keysign: pipe: %s", strerror(errno));
1425 if ((pid = fork()) < 0) {
1426 error("ssh_keysign: fork: %s", strerror(errno));
1430 /* keep the socket on exec */
1431 fcntl(packet_get_connection_in(), F_SETFD, 0);
1432 permanently_drop_suid(getuid());
1434 if (dup2(from[1], STDOUT_FILENO) < 0)
1435 fatal("ssh_keysign: dup2: %s", strerror(errno));
1437 if (dup2(to[0], STDIN_FILENO) < 0)
1438 fatal("ssh_keysign: dup2: %s", strerror(errno));
1441 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1442 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1449 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1450 buffer_put_string(&b, data, datalen);
1451 if (ssh_msg_send(to[1], version, &b) == -1)
1452 fatal("ssh_keysign: couldn't send request");
1454 if (ssh_msg_recv(from[0], &b) < 0) {
1455 error("ssh_keysign: no reply");
1462 while (waitpid(pid, &status, 0) < 0)
1466 if (buffer_get_char(&b) != version) {
1467 error("ssh_keysign: bad version");
1471 *sigp = buffer_get_string(&b, lenp);
1478 userauth_hostbased(Authctxt *authctxt)
1480 Key *private = NULL;
1481 Sensitive *sensitive = authctxt->sensitive;
1483 u_char *signature, *blob;
1484 char *chost, *pkalg, *p;
1485 const char *service;
1487 int ok, i, found = 0;
1489 /* check for a useful key */
1490 for (i = 0; i < sensitive->nkeys; i++) {
1491 private = sensitive->keys[i];
1492 if (private && private->type != KEY_RSA1) {
1494 /* we take and free the key */
1495 sensitive->keys[i] = NULL;
1500 debug("No more client hostkeys for hostbased authentication.");
1503 if (key_to_blob(private, &blob, &blen) == 0) {
1507 /* figure out a name for the client host */
1508 p = get_local_name(packet_get_connection_in());
1510 error("userauth_hostbased: cannot get local ipaddr/name");
1515 xasprintf(&chost, "%s.", p);
1516 debug2("userauth_hostbased: chost %s", chost);
1519 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1521 pkalg = xstrdup(key_ssh_name(private));
1523 /* construct data */
1524 buffer_put_string(&b, session_id2, session_id2_len);
1525 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1526 buffer_put_cstring(&b, authctxt->server_user);
1527 buffer_put_cstring(&b, service);
1528 buffer_put_cstring(&b, authctxt->method->name);
1529 buffer_put_cstring(&b, pkalg);
1530 buffer_put_string(&b, blob, blen);
1531 buffer_put_cstring(&b, chost);
1532 buffer_put_cstring(&b, authctxt->local_user);
1536 if (sensitive->external_keysign)
1537 ok = ssh_keysign(private, &signature, &slen,
1538 buffer_ptr(&b), buffer_len(&b));
1540 ok = key_sign(private, &signature, &slen,
1541 buffer_ptr(&b), buffer_len(&b));
1545 error("key_sign failed");
1551 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1552 packet_put_cstring(authctxt->server_user);
1553 packet_put_cstring(authctxt->service);
1554 packet_put_cstring(authctxt->method->name);
1555 packet_put_cstring(pkalg);
1556 packet_put_string(blob, blen);
1557 packet_put_cstring(chost);
1558 packet_put_cstring(authctxt->local_user);
1559 packet_put_string(signature, slen);
1560 explicit_bzero(signature, slen);
1570 /* find auth method */
1573 * given auth method name, if configurable options permit this method fill
1574 * in auth_ident field and return true, otherwise return false.
1577 authmethod_is_enabled(Authmethod *method)
1581 /* return false if options indicate this method is disabled */
1582 if (method->enabled == NULL || *method->enabled == 0)
1584 /* return false if batch mode is enabled but method needs interactive mode */
1585 if (method->batch_flag != NULL && *method->batch_flag != 0)
1591 authmethod_lookup(const char *name)
1593 Authmethod *method = NULL;
1595 for (method = authmethods; method->name != NULL; method++)
1596 if (strcmp(name, method->name) == 0)
1598 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1602 /* XXX internal state */
1603 static Authmethod *current = NULL;
1604 static char *supported = NULL;
1605 static char *preferred = NULL;
1608 * Given the authentication method list sent by the server, return the
1609 * next method we should try. If the server initially sends a nil list,
1610 * use a built-in default list.
1613 authmethod_get(char *authlist)
1618 /* Use a suitable default if we're passed a nil list. */
1619 if (authlist == NULL || strlen(authlist) == 0)
1620 authlist = options.preferred_authentications;
1622 if (supported == NULL || strcmp(authlist, supported) != 0) {
1623 debug3("start over, passed a different list %s", authlist);
1625 supported = xstrdup(authlist);
1626 preferred = options.preferred_authentications;
1627 debug3("preferred %s", preferred);
1629 } else if (current != NULL && authmethod_is_enabled(current))
1633 if ((name = match_list(preferred, supported, &next)) == NULL) {
1634 debug("No more authentication methods to try.");
1639 debug3("authmethod_lookup %s", name);
1640 debug3("remaining preferred: %s", preferred);
1641 if ((current = authmethod_lookup(name)) != NULL &&
1642 authmethod_is_enabled(current)) {
1643 debug3("authmethod_is_enabled %s", name);
1644 debug("Next authentication method: %s", name);
1653 authmethods_get(void)
1655 Authmethod *method = NULL;
1660 for (method = authmethods; method->name != NULL; method++) {
1661 if (authmethod_is_enabled(method)) {
1662 if (buffer_len(&b) > 0)
1663 buffer_append(&b, ",", 1);
1664 buffer_append(&b, method->name, strlen(method->name));
1667 buffer_append(&b, "\0", 1);
1668 list = xstrdup(buffer_ptr(&b));