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;
199 if (options.rekey_limit || options.rekey_interval)
200 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
201 (time_t)options.rekey_interval);
203 /* start key exchange */
204 kex = kex_setup(myproposal);
205 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
206 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
207 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
208 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
209 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
210 kex->kex[KEX_C25519_SHA256] = kexc25519_client;
211 kex->client_version_string=client_version_string;
212 kex->server_version_string=server_version_string;
213 kex->verify_host_key=&verify_host_key_callback;
217 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
219 if (options.use_roaming && !kex->roaming) {
220 debug("Roaming not allowed by server");
221 options.use_roaming = 0;
224 session_id2 = kex->session_id;
225 session_id2_len = kex->session_id_len;
228 /* send 1st encrypted/maced/compressed message */
229 packet_start(SSH2_MSG_IGNORE);
230 packet_put_cstring("markus");
240 typedef struct Authctxt Authctxt;
241 typedef struct Authmethod Authmethod;
242 typedef struct identity Identity;
243 typedef struct idlist Idlist;
246 TAILQ_ENTRY(identity) next;
247 AuthenticationConnection *ac; /* set if agent supports key */
248 Key *key; /* public/private key */
249 char *filename; /* comment for agent-only keys */
251 int isprivate; /* key points to the private key */
254 TAILQ_HEAD(idlist, identity);
257 const char *server_user;
258 const char *local_user;
262 sig_atomic_t success;
266 AuthenticationConnection *agent;
268 Sensitive *sensitive;
269 /* kbd-interactive */
275 char *name; /* string to compare against server's list */
276 int (*userauth)(Authctxt *authctxt);
277 void (*cleanup)(Authctxt *authctxt);
278 int *enabled; /* flag in option struct that enables method */
279 int *batch_flag; /* flag in option struct that disables method */
282 void input_userauth_success(int, u_int32_t, void *);
283 void input_userauth_success_unexpected(int, u_int32_t, void *);
284 void input_userauth_failure(int, u_int32_t, void *);
285 void input_userauth_banner(int, u_int32_t, void *);
286 void input_userauth_error(int, u_int32_t, void *);
287 void input_userauth_info_req(int, u_int32_t, void *);
288 void input_userauth_pk_ok(int, u_int32_t, void *);
289 void input_userauth_passwd_changereq(int, u_int32_t, void *);
291 int userauth_none(Authctxt *);
292 int userauth_pubkey(Authctxt *);
293 int userauth_passwd(Authctxt *);
294 int userauth_kbdint(Authctxt *);
295 int userauth_hostbased(Authctxt *);
298 int userauth_gssapi(Authctxt *authctxt);
299 void input_gssapi_response(int type, u_int32_t, void *);
300 void input_gssapi_token(int type, u_int32_t, void *);
301 void input_gssapi_hash(int type, u_int32_t, void *);
302 void input_gssapi_error(int, u_int32_t, void *);
303 void input_gssapi_errtok(int, u_int32_t, void *);
306 void userauth(Authctxt *, char *);
308 static int sign_and_send_pubkey(Authctxt *, Identity *);
309 static void pubkey_prepare(Authctxt *);
310 static void pubkey_cleanup(Authctxt *);
311 static Key *load_identity_file(char *, int);
313 static Authmethod *authmethod_get(char *authlist);
314 static Authmethod *authmethod_lookup(const char *name);
315 static char *authmethods_get(void);
317 Authmethod authmethods[] = {
322 &options.gss_authentication,
328 &options.hostbased_authentication,
333 &options.pubkey_authentication,
335 {"keyboard-interactive",
338 &options.kbd_interactive_authentication,
339 &options.batch_mode},
343 &options.password_authentication,
344 &options.batch_mode},
350 {NULL, NULL, NULL, NULL, NULL}
354 ssh_userauth2(const char *local_user, const char *server_user, char *host,
355 Sensitive *sensitive)
360 if (options.challenge_response_authentication)
361 options.kbd_interactive_authentication = 1;
363 packet_start(SSH2_MSG_SERVICE_REQUEST);
364 packet_put_cstring("ssh-userauth");
366 debug("SSH2_MSG_SERVICE_REQUEST sent");
368 type = packet_read();
369 if (type != SSH2_MSG_SERVICE_ACCEPT)
370 fatal("Server denied authentication request: %d", type);
371 if (packet_remaining() > 0) {
372 char *reply = packet_get_string(NULL);
373 debug2("service_accept: %s", reply);
376 debug2("buggy server: service_accept w/o service");
379 debug("SSH2_MSG_SERVICE_ACCEPT received");
381 if (options.preferred_authentications == NULL)
382 options.preferred_authentications = authmethods_get();
384 /* setup authentication context */
385 memset(&authctxt, 0, sizeof(authctxt));
386 pubkey_prepare(&authctxt);
387 authctxt.server_user = server_user;
388 authctxt.local_user = local_user;
389 authctxt.host = host;
390 authctxt.service = "ssh-connection"; /* service name */
391 authctxt.success = 0;
392 authctxt.method = authmethod_lookup("none");
393 authctxt.authlist = NULL;
394 authctxt.methoddata = NULL;
395 authctxt.sensitive = sensitive;
396 authctxt.info_req_seen = 0;
397 if (authctxt.method == NULL)
398 fatal("ssh_userauth2: internal error: cannot send userauth none request");
400 /* initial userauth request */
401 userauth_none(&authctxt);
403 dispatch_init(&input_userauth_error);
404 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
405 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
406 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
407 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
409 pubkey_cleanup(&authctxt);
410 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
412 debug("Authentication succeeded (%s).", authctxt.method->name);
416 userauth(Authctxt *authctxt, char *authlist)
418 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
419 authctxt->method->cleanup(authctxt);
421 free(authctxt->methoddata);
422 authctxt->methoddata = NULL;
423 if (authlist == NULL) {
424 authlist = authctxt->authlist;
426 free(authctxt->authlist);
427 authctxt->authlist = authlist;
430 Authmethod *method = authmethod_get(authlist);
432 fatal("Permission denied (%s).", authlist);
433 authctxt->method = method;
435 /* reset the per method handler */
436 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
437 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
439 /* and try new method */
440 if (method->userauth(authctxt) != 0) {
441 debug2("we sent a %s packet, wait for reply", method->name);
444 debug2("we did not send a packet, disable method");
445 method->enabled = NULL;
452 input_userauth_error(int type, u_int32_t seq, void *ctxt)
454 fatal("input_userauth_error: bad message during authentication: "
460 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
462 char *msg, *raw, *lang;
465 debug3("input_userauth_banner");
466 raw = packet_get_string(&len);
467 lang = packet_get_string(NULL);
468 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
471 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
472 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
473 fprintf(stderr, "%s", msg);
482 input_userauth_success(int type, u_int32_t seq, void *ctxt)
484 Authctxt *authctxt = ctxt;
486 if (authctxt == NULL)
487 fatal("input_userauth_success: no authentication context");
488 free(authctxt->authlist);
489 authctxt->authlist = NULL;
490 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
491 authctxt->method->cleanup(authctxt);
492 free(authctxt->methoddata);
493 authctxt->methoddata = NULL;
494 authctxt->success = 1; /* break out */
498 input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
500 Authctxt *authctxt = ctxt;
502 if (authctxt == NULL)
503 fatal("%s: no authentication context", __func__);
505 fatal("Unexpected authentication success during %s.",
506 authctxt->method->name);
511 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
513 Authctxt *authctxt = ctxt;
514 char *authlist = NULL;
517 if (authctxt == NULL)
518 fatal("input_userauth_failure: no authentication context");
520 authlist = packet_get_string(NULL);
521 partial = packet_get_char();
525 logit("Authenticated with partial success.");
527 pubkey_cleanup(authctxt);
528 pubkey_prepare(authctxt);
530 debug("Authentications that can continue: %s", authlist);
532 userauth(authctxt, authlist);
537 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
539 Authctxt *authctxt = ctxt;
543 int pktype, sent = 0;
548 if (authctxt == NULL)
549 fatal("input_userauth_pk_ok: no authentication context");
550 if (datafellows & SSH_BUG_PKOK) {
551 /* this is similar to SSH_BUG_PKAUTH */
552 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
553 pkblob = packet_get_string(&blen);
555 buffer_append(&b, pkblob, blen);
556 pkalg = buffer_get_string(&b, &alen);
559 pkalg = packet_get_string(&alen);
560 pkblob = packet_get_string(&blen);
564 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
566 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
567 debug("unknown pkalg %s", pkalg);
570 if ((key = key_from_blob(pkblob, blen)) == NULL) {
571 debug("no key from blob. pkalg %s", pkalg);
574 if (key->type != pktype) {
575 error("input_userauth_pk_ok: type mismatch "
576 "for decoded key (received %d, expected %d)",
580 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
581 debug2("input_userauth_pk_ok: fp %s", fp);
585 * search keys in the reverse order, because last candidate has been
586 * moved to the end of the queue. this also avoids confusion by
589 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
590 if (key_equal(key, id->key)) {
591 sent = sign_and_send_pubkey(authctxt, id);
601 /* try another method if we did not send a packet */
603 userauth(authctxt, NULL);
608 userauth_gssapi(Authctxt *authctxt)
610 Gssctxt *gssctxt = NULL;
611 static gss_OID_set gss_supported = NULL;
612 static u_int mech = 0;
616 /* Try one GSSAPI method at a time, rather than sending them all at
619 if (gss_supported == NULL)
620 gss_indicate_mechs(&min, &gss_supported);
622 /* Check to see if the mechanism is usable before we offer it */
623 while (mech < gss_supported->count && !ok) {
624 /* My DER encoding requires length<128 */
625 if (gss_supported->elements[mech].length < 128 &&
626 ssh_gssapi_check_mechanism(&gssctxt,
627 &gss_supported->elements[mech], authctxt->host)) {
628 ok = 1; /* Mechanism works */
637 authctxt->methoddata=(void *)gssctxt;
639 packet_start(SSH2_MSG_USERAUTH_REQUEST);
640 packet_put_cstring(authctxt->server_user);
641 packet_put_cstring(authctxt->service);
642 packet_put_cstring(authctxt->method->name);
646 packet_put_int((gss_supported->elements[mech].length) + 2);
647 packet_put_char(SSH_GSS_OIDTYPE);
648 packet_put_char(gss_supported->elements[mech].length);
649 packet_put_raw(gss_supported->elements[mech].elements,
650 gss_supported->elements[mech].length);
654 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
655 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
656 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
657 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
659 mech++; /* Move along to next candidate */
665 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
667 Authctxt *authctxt = ctxt;
668 Gssctxt *gssctxt = authctxt->methoddata;
669 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
670 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
671 gss_buffer_desc gssbuf;
672 OM_uint32 status, ms, flags;
675 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
676 recv_tok, &send_tok, &flags);
678 if (send_tok.length > 0) {
679 if (GSS_ERROR(status))
680 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
682 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
684 packet_put_string(send_tok.value, send_tok.length);
686 gss_release_buffer(&ms, &send_tok);
689 if (status == GSS_S_COMPLETE) {
690 /* send either complete or MIC, depending on mechanism */
691 if (!(flags & GSS_C_INTEG_FLAG)) {
692 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
695 ssh_gssapi_buildmic(&b, authctxt->server_user,
696 authctxt->service, "gssapi-with-mic");
698 gssbuf.value = buffer_ptr(&b);
699 gssbuf.length = buffer_len(&b);
701 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
703 if (!GSS_ERROR(status)) {
704 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
705 packet_put_string(mic.value, mic.length);
711 gss_release_buffer(&ms, &mic);
720 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
722 Authctxt *authctxt = ctxt;
727 if (authctxt == NULL)
728 fatal("input_gssapi_response: no authentication context");
729 gssctxt = authctxt->methoddata;
732 oidv = packet_get_string(&oidlen);
735 oidv[0] != SSH_GSS_OIDTYPE ||
736 oidv[1] != oidlen - 2) {
738 debug("Badly encoded mechanism OID received");
739 userauth(authctxt, NULL);
743 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
744 fatal("Server returned different OID than expected");
750 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
751 /* Start again with next method on list */
752 debug("Trying to start again");
753 userauth(authctxt, NULL);
760 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
762 Authctxt *authctxt = ctxt;
763 gss_buffer_desc recv_tok;
767 if (authctxt == NULL)
768 fatal("input_gssapi_response: no authentication context");
770 recv_tok.value = packet_get_string(&slen);
771 recv_tok.length = slen; /* safe typecast */
775 status = process_gssapi_token(ctxt, &recv_tok);
777 free(recv_tok.value);
779 if (GSS_ERROR(status)) {
780 /* Start again with the next method in the list */
781 userauth(authctxt, NULL);
788 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
790 Authctxt *authctxt = ctxt;
792 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
793 gss_buffer_desc recv_tok;
797 if (authctxt == NULL)
798 fatal("input_gssapi_response: no authentication context");
799 gssctxt = authctxt->methoddata;
801 recv_tok.value = packet_get_string(&len);
802 recv_tok.length = len;
806 /* Stick it into GSSAPI and see what it says */
807 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
808 &recv_tok, &send_tok, NULL);
810 free(recv_tok.value);
811 gss_release_buffer(&ms, &send_tok);
813 /* Server will be returning a failed packet after this one */
818 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
823 /* maj */(void)packet_get_int();
824 /* min */(void)packet_get_int();
825 msg=packet_get_string(NULL);
826 lang=packet_get_string(NULL);
830 debug("Server GSSAPI Error:\n%s", msg);
837 userauth_none(Authctxt *authctxt)
839 /* initial userauth request */
840 packet_start(SSH2_MSG_USERAUTH_REQUEST);
841 packet_put_cstring(authctxt->server_user);
842 packet_put_cstring(authctxt->service);
843 packet_put_cstring(authctxt->method->name);
849 userauth_passwd(Authctxt *authctxt)
851 static int attempt = 0;
854 const char *host = options.host_key_alias ? options.host_key_alias :
857 if (attempt++ >= options.number_of_password_prompts)
861 error("Permission denied, please try again.");
863 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
864 authctxt->server_user, host);
865 password = read_passphrase(prompt, 0);
866 packet_start(SSH2_MSG_USERAUTH_REQUEST);
867 packet_put_cstring(authctxt->server_user);
868 packet_put_cstring(authctxt->service);
869 packet_put_cstring(authctxt->method->name);
871 packet_put_cstring(password);
872 explicit_bzero(password, strlen(password));
874 packet_add_padding(64);
877 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
878 &input_userauth_passwd_changereq);
884 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
888 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
890 Authctxt *authctxt = ctxt;
891 char *info, *lang, *password = NULL, *retype = NULL;
893 const char *host = options.host_key_alias ? options.host_key_alias :
896 debug2("input_userauth_passwd_changereq");
898 if (authctxt == NULL)
899 fatal("input_userauth_passwd_changereq: "
900 "no authentication context");
902 info = packet_get_string(NULL);
903 lang = packet_get_string(NULL);
904 if (strlen(info) > 0)
908 packet_start(SSH2_MSG_USERAUTH_REQUEST);
909 packet_put_cstring(authctxt->server_user);
910 packet_put_cstring(authctxt->service);
911 packet_put_cstring(authctxt->method->name);
912 packet_put_char(1); /* additional info */
913 snprintf(prompt, sizeof(prompt),
914 "Enter %.30s@%.128s's old password: ",
915 authctxt->server_user, host);
916 password = read_passphrase(prompt, 0);
917 packet_put_cstring(password);
918 explicit_bzero(password, strlen(password));
921 while (password == NULL) {
922 snprintf(prompt, sizeof(prompt),
923 "Enter %.30s@%.128s's new password: ",
924 authctxt->server_user, host);
925 password = read_passphrase(prompt, RP_ALLOW_EOF);
926 if (password == NULL) {
930 snprintf(prompt, sizeof(prompt),
931 "Retype %.30s@%.128s's new password: ",
932 authctxt->server_user, host);
933 retype = read_passphrase(prompt, 0);
934 if (strcmp(password, retype) != 0) {
935 explicit_bzero(password, strlen(password));
937 logit("Mismatch; try again, EOF to quit.");
940 explicit_bzero(retype, strlen(retype));
943 packet_put_cstring(password);
944 explicit_bzero(password, strlen(password));
946 packet_add_padding(64);
949 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
950 &input_userauth_passwd_changereq);
954 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
955 u_char *data, u_int datalen)
960 /* the agent supports this key */
962 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
965 * we have already loaded the private key or
966 * the private key is stored in external hardware
968 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
969 return (key_sign(id->key, sigp, lenp, data, datalen));
970 /* load the private key from the file */
971 if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL)
973 ret = key_sign(prv, sigp, lenp, data, datalen);
979 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
982 u_char *blob, *signature;
989 fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
990 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
993 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
994 /* we cannot handle this key */
995 debug3("sign_and_send_pubkey: cannot handle key");
998 /* data to be signed */
1000 if (datafellows & SSH_OLD_SESSIONID) {
1001 buffer_append(&b, session_id2, session_id2_len);
1002 skip = session_id2_len;
1004 buffer_put_string(&b, session_id2, session_id2_len);
1005 skip = buffer_len(&b);
1007 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1008 buffer_put_cstring(&b, authctxt->server_user);
1009 buffer_put_cstring(&b,
1010 datafellows & SSH_BUG_PKSERVICE ?
1013 if (datafellows & SSH_BUG_PKAUTH) {
1014 buffer_put_char(&b, have_sig);
1016 buffer_put_cstring(&b, authctxt->method->name);
1017 buffer_put_char(&b, have_sig);
1018 buffer_put_cstring(&b, key_ssh_name(id->key));
1020 buffer_put_string(&b, blob, bloblen);
1022 /* generate signature */
1023 ret = identity_sign(id, &signature, &slen,
1024 buffer_ptr(&b), buffer_len(&b));
1033 if (datafellows & SSH_BUG_PKSERVICE) {
1035 buffer_append(&b, session_id2, session_id2_len);
1036 skip = session_id2_len;
1037 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1038 buffer_put_cstring(&b, authctxt->server_user);
1039 buffer_put_cstring(&b, authctxt->service);
1040 buffer_put_cstring(&b, authctxt->method->name);
1041 buffer_put_char(&b, have_sig);
1042 if (!(datafellows & SSH_BUG_PKAUTH))
1043 buffer_put_cstring(&b, key_ssh_name(id->key));
1044 buffer_put_string(&b, blob, bloblen);
1048 /* append signature */
1049 buffer_put_string(&b, signature, slen);
1052 /* skip session id and packet type */
1053 if (buffer_len(&b) < skip + 1)
1054 fatal("userauth_pubkey: internal error");
1055 buffer_consume(&b, skip + 1);
1057 /* put remaining data from buffer into packet */
1058 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1059 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1067 send_pubkey_test(Authctxt *authctxt, Identity *id)
1070 u_int bloblen, have_sig = 0;
1072 debug3("send_pubkey_test");
1074 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1075 /* we cannot handle this key */
1076 debug3("send_pubkey_test: cannot handle key");
1079 /* register callback for USERAUTH_PK_OK message */
1080 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1082 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1083 packet_put_cstring(authctxt->server_user);
1084 packet_put_cstring(authctxt->service);
1085 packet_put_cstring(authctxt->method->name);
1086 packet_put_char(have_sig);
1087 if (!(datafellows & SSH_BUG_PKAUTH))
1088 packet_put_cstring(key_ssh_name(id->key));
1089 packet_put_string(blob, bloblen);
1096 load_identity_file(char *filename, int userprovided)
1099 char prompt[300], *passphrase;
1100 int perm_ok = 0, quit, i;
1103 if (stat(filename, &st) < 0) {
1104 (userprovided ? logit : debug3)("no such identity: %s: %s",
1105 filename, strerror(errno));
1108 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1110 if (private != NULL)
1114 if (private == NULL) {
1115 if (options.batch_mode)
1117 snprintf(prompt, sizeof prompt,
1118 "Enter passphrase for key '%.100s': ", filename);
1119 for (i = 0; i < options.number_of_password_prompts; i++) {
1120 passphrase = read_passphrase(prompt, 0);
1121 if (strcmp(passphrase, "") != 0) {
1122 private = key_load_private_type(KEY_UNSPEC,
1123 filename, passphrase, NULL, NULL);
1126 debug2("no passphrase given, try next key");
1129 explicit_bzero(passphrase, strlen(passphrase));
1131 if (private != NULL || quit)
1133 debug2("bad passphrase given, try again...");
1140 * try keys in the following order:
1141 * 1. agent keys that are found in the config file
1142 * 2. other agent keys
1143 * 3. keys that are only listed in the config file
1146 pubkey_prepare(Authctxt *authctxt)
1148 Identity *id, *id2, *tmp;
1149 Idlist agent, files, *preferred;
1151 AuthenticationConnection *ac;
1155 TAILQ_INIT(&agent); /* keys from the agent */
1156 TAILQ_INIT(&files); /* keys from the config file */
1157 preferred = &authctxt->keys;
1158 TAILQ_INIT(preferred); /* preferred order of keys */
1160 /* list of keys stored in the filesystem and PKCS#11 */
1161 for (i = 0; i < options.num_identity_files; i++) {
1162 key = options.identity_keys[i];
1163 if (key && key->type == KEY_RSA1)
1165 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1167 options.identity_keys[i] = NULL;
1168 id = xcalloc(1, sizeof(*id));
1170 id->filename = xstrdup(options.identity_files[i]);
1171 id->userprovided = options.identity_file_userprovided[i];
1172 TAILQ_INSERT_TAIL(&files, id, next);
1174 /* Prefer PKCS11 keys that are explicitly listed */
1175 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1176 if (id->key == NULL || (id->key->flags & KEY_FLAG_EXT) == 0)
1179 TAILQ_FOREACH(id2, &files, next) {
1180 if (id2->key == NULL ||
1181 (id2->key->flags & KEY_FLAG_EXT) != 0)
1183 if (key_equal(id->key, id2->key)) {
1184 TAILQ_REMOVE(&files, id, next);
1185 TAILQ_INSERT_TAIL(preferred, id, next);
1190 /* If IdentitiesOnly set and key not found then don't use it */
1191 if (!found && options.identities_only) {
1192 TAILQ_REMOVE(&files, id, next);
1193 explicit_bzero(id, sizeof(*id));
1197 /* list of keys supported by the agent */
1198 if ((ac = ssh_get_authentication_connection())) {
1199 for (key = ssh_get_first_identity(ac, &comment, 2);
1201 key = ssh_get_next_identity(ac, &comment, 2)) {
1203 TAILQ_FOREACH(id, &files, next) {
1204 /* agent keys from the config file are preferred */
1205 if (key_equal(key, id->key)) {
1208 TAILQ_REMOVE(&files, id, next);
1209 TAILQ_INSERT_TAIL(preferred, id, next);
1215 if (!found && !options.identities_only) {
1216 id = xcalloc(1, sizeof(*id));
1218 id->filename = comment;
1220 TAILQ_INSERT_TAIL(&agent, id, next);
1223 /* append remaining agent keys */
1224 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1225 TAILQ_REMOVE(&agent, id, next);
1226 TAILQ_INSERT_TAIL(preferred, id, next);
1228 authctxt->agent = ac;
1230 /* append remaining keys from the config file */
1231 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1232 TAILQ_REMOVE(&files, id, next);
1233 TAILQ_INSERT_TAIL(preferred, id, next);
1235 TAILQ_FOREACH(id, preferred, next) {
1236 debug2("key: %s (%p),%s", id->filename, id->key,
1237 id->userprovided ? " explicit" : "");
1242 pubkey_cleanup(Authctxt *authctxt)
1246 if (authctxt->agent != NULL)
1247 ssh_close_authentication_connection(authctxt->agent);
1248 for (id = TAILQ_FIRST(&authctxt->keys); id;
1249 id = TAILQ_FIRST(&authctxt->keys)) {
1250 TAILQ_REMOVE(&authctxt->keys, id, next);
1259 userauth_pubkey(Authctxt *authctxt)
1264 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1267 /* move key to the end of the queue */
1268 TAILQ_REMOVE(&authctxt->keys, id, next);
1269 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1271 * send a test message if we have the public key. for
1272 * encrypted keys we cannot do this and have to load the
1273 * private key instead
1275 if (id->key != NULL) {
1276 if (key_type_plain(id->key->type) == KEY_RSA &&
1277 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1278 debug("Skipped %s key %s for RSA/MD5 server",
1279 key_type(id->key), id->filename);
1280 } else if (id->key->type != KEY_RSA1) {
1281 debug("Offering %s public key: %s",
1282 key_type(id->key), id->filename);
1283 sent = send_pubkey_test(authctxt, id);
1286 debug("Trying private key: %s", id->filename);
1287 id->key = load_identity_file(id->filename,
1289 if (id->key != NULL) {
1291 if (key_type_plain(id->key->type) == KEY_RSA &&
1292 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1293 debug("Skipped %s key %s for RSA/MD5 "
1294 "server", key_type(id->key),
1297 sent = sign_and_send_pubkey(
1311 * Send userauth request message specifying keyboard-interactive method.
1314 userauth_kbdint(Authctxt *authctxt)
1316 static int attempt = 0;
1318 if (attempt++ >= options.number_of_password_prompts)
1320 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1321 if (attempt > 1 && !authctxt->info_req_seen) {
1322 debug3("userauth_kbdint: disable: no info_req_seen");
1323 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1327 debug2("userauth_kbdint");
1328 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1329 packet_put_cstring(authctxt->server_user);
1330 packet_put_cstring(authctxt->service);
1331 packet_put_cstring(authctxt->method->name);
1332 packet_put_cstring(""); /* lang */
1333 packet_put_cstring(options.kbd_interactive_devices ?
1334 options.kbd_interactive_devices : "");
1337 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1342 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1345 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1347 Authctxt *authctxt = ctxt;
1348 char *name, *inst, *lang, *prompt, *response;
1349 u_int num_prompts, i;
1352 debug2("input_userauth_info_req");
1354 if (authctxt == NULL)
1355 fatal("input_userauth_info_req: no authentication context");
1357 authctxt->info_req_seen = 1;
1359 name = packet_get_string(NULL);
1360 inst = packet_get_string(NULL);
1361 lang = packet_get_string(NULL);
1362 if (strlen(name) > 0)
1364 if (strlen(inst) > 0)
1370 num_prompts = packet_get_int();
1372 * Begin to build info response packet based on prompts requested.
1373 * We commit to providing the correct number of responses, so if
1374 * further on we run into a problem that prevents this, we have to
1375 * be sure and clean this up and send a correct error response.
1377 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1378 packet_put_int(num_prompts);
1380 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1381 for (i = 0; i < num_prompts; i++) {
1382 prompt = packet_get_string(NULL);
1383 echo = packet_get_char();
1385 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1387 packet_put_cstring(response);
1388 explicit_bzero(response, strlen(response));
1392 packet_check_eom(); /* done with parsing incoming message. */
1394 packet_add_padding(64);
1399 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1400 u_char *data, u_int datalen)
1405 int to[2], from[2], status, version = 2;
1407 debug2("ssh_keysign called");
1409 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1410 error("ssh_keysign: not installed: %s", strerror(errno));
1413 if (fflush(stdout) != 0)
1414 error("ssh_keysign: fflush: %s", strerror(errno));
1416 error("ssh_keysign: pipe: %s", strerror(errno));
1419 if (pipe(from) < 0) {
1420 error("ssh_keysign: pipe: %s", strerror(errno));
1423 if ((pid = fork()) < 0) {
1424 error("ssh_keysign: fork: %s", strerror(errno));
1428 /* keep the socket on exec */
1429 fcntl(packet_get_connection_in(), F_SETFD, 0);
1430 permanently_drop_suid(getuid());
1432 if (dup2(from[1], STDOUT_FILENO) < 0)
1433 fatal("ssh_keysign: dup2: %s", strerror(errno));
1435 if (dup2(to[0], STDIN_FILENO) < 0)
1436 fatal("ssh_keysign: dup2: %s", strerror(errno));
1439 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1440 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1447 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1448 buffer_put_string(&b, data, datalen);
1449 if (ssh_msg_send(to[1], version, &b) == -1)
1450 fatal("ssh_keysign: couldn't send request");
1452 if (ssh_msg_recv(from[0], &b) < 0) {
1453 error("ssh_keysign: no reply");
1460 while (waitpid(pid, &status, 0) < 0)
1464 if (buffer_get_char(&b) != version) {
1465 error("ssh_keysign: bad version");
1469 *sigp = buffer_get_string(&b, lenp);
1476 userauth_hostbased(Authctxt *authctxt)
1478 Key *private = NULL;
1479 Sensitive *sensitive = authctxt->sensitive;
1481 u_char *signature, *blob;
1482 char *chost, *pkalg, *p;
1483 const char *service;
1485 int ok, i, found = 0;
1487 /* check for a useful key */
1488 for (i = 0; i < sensitive->nkeys; i++) {
1489 private = sensitive->keys[i];
1490 if (private && private->type != KEY_RSA1) {
1492 /* we take and free the key */
1493 sensitive->keys[i] = NULL;
1498 debug("No more client hostkeys for hostbased authentication.");
1501 if (key_to_blob(private, &blob, &blen) == 0) {
1505 /* figure out a name for the client host */
1506 p = get_local_name(packet_get_connection_in());
1508 error("userauth_hostbased: cannot get local ipaddr/name");
1513 xasprintf(&chost, "%s.", p);
1514 debug2("userauth_hostbased: chost %s", chost);
1517 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1519 pkalg = xstrdup(key_ssh_name(private));
1521 /* construct data */
1522 buffer_put_string(&b, session_id2, session_id2_len);
1523 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1524 buffer_put_cstring(&b, authctxt->server_user);
1525 buffer_put_cstring(&b, service);
1526 buffer_put_cstring(&b, authctxt->method->name);
1527 buffer_put_cstring(&b, pkalg);
1528 buffer_put_string(&b, blob, blen);
1529 buffer_put_cstring(&b, chost);
1530 buffer_put_cstring(&b, authctxt->local_user);
1534 if (sensitive->external_keysign)
1535 ok = ssh_keysign(private, &signature, &slen,
1536 buffer_ptr(&b), buffer_len(&b));
1538 ok = key_sign(private, &signature, &slen,
1539 buffer_ptr(&b), buffer_len(&b));
1543 error("key_sign failed");
1549 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1550 packet_put_cstring(authctxt->server_user);
1551 packet_put_cstring(authctxt->service);
1552 packet_put_cstring(authctxt->method->name);
1553 packet_put_cstring(pkalg);
1554 packet_put_string(blob, blen);
1555 packet_put_cstring(chost);
1556 packet_put_cstring(authctxt->local_user);
1557 packet_put_string(signature, slen);
1558 explicit_bzero(signature, slen);
1568 /* find auth method */
1571 * given auth method name, if configurable options permit this method fill
1572 * in auth_ident field and return true, otherwise return false.
1575 authmethod_is_enabled(Authmethod *method)
1579 /* return false if options indicate this method is disabled */
1580 if (method->enabled == NULL || *method->enabled == 0)
1582 /* return false if batch mode is enabled but method needs interactive mode */
1583 if (method->batch_flag != NULL && *method->batch_flag != 0)
1589 authmethod_lookup(const char *name)
1591 Authmethod *method = NULL;
1593 for (method = authmethods; method->name != NULL; method++)
1594 if (strcmp(name, method->name) == 0)
1596 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1600 /* XXX internal state */
1601 static Authmethod *current = NULL;
1602 static char *supported = NULL;
1603 static char *preferred = NULL;
1606 * Given the authentication method list sent by the server, return the
1607 * next method we should try. If the server initially sends a nil list,
1608 * use a built-in default list.
1611 authmethod_get(char *authlist)
1616 /* Use a suitable default if we're passed a nil list. */
1617 if (authlist == NULL || strlen(authlist) == 0)
1618 authlist = options.preferred_authentications;
1620 if (supported == NULL || strcmp(authlist, supported) != 0) {
1621 debug3("start over, passed a different list %s", authlist);
1623 supported = xstrdup(authlist);
1624 preferred = options.preferred_authentications;
1625 debug3("preferred %s", preferred);
1627 } else if (current != NULL && authmethod_is_enabled(current))
1631 if ((name = match_list(preferred, supported, &next)) == NULL) {
1632 debug("No more authentication methods to try.");
1637 debug3("authmethod_lookup %s", name);
1638 debug3("remaining preferred: %s", preferred);
1639 if ((current = authmethod_lookup(name)) != NULL &&
1640 authmethod_is_enabled(current)) {
1641 debug3("authmethod_is_enabled %s", name);
1642 debug("Next authentication method: %s", name);
1651 authmethods_get(void)
1653 Authmethod *method = NULL;
1658 for (method = authmethods; method->name != NULL; method++) {
1659 if (authmethod_is_enabled(method)) {
1660 if (buffer_len(&b) > 0)
1661 buffer_append(&b, ",", 1);
1662 buffer_append(&b, method->name, strlen(method->name));
1665 buffer_append(&b, "\0", 1);
1666 list = xstrdup(buffer_ptr(&b));