1 /* $OpenBSD: sshconnect2.c,v 1.201 2014/01/09 23:20:00 djm Exp $ */
4 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Copyright (c) 2008 Damien Miller. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
31 #include <sys/socket.h>
44 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
48 #include "openbsd-compat/sys-queue.h"
59 #include "myproposal.h"
60 #include "sshconnect.h"
71 #include "pathnames.h"
82 extern char *client_version_string;
83 extern char *server_version_string;
84 extern Options options;
85 #ifdef NONE_CIPHER_ENABLED
89 * tty_flag is set in ssh.c so we can use it here. If set then prevent
90 * the switch to the null cipher.
100 u_char *session_id2 = NULL;
101 u_int session_id2_len = 0;
104 struct sockaddr *xxx_hostaddr;
109 verify_host_key_callback(Key *hostkey)
111 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
112 fatal("Host key verification failed.");
117 order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
119 char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
121 struct hostkeys *hostkeys;
125 /* Find all hostkeys for this hostname */
126 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
127 hostkeys = init_hostkeys();
128 for (i = 0; i < options.num_user_hostfiles; i++)
129 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
130 for (i = 0; i < options.num_system_hostfiles; i++)
131 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
133 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
134 maxlen = strlen(avail) + 1;
135 first = xmalloc(maxlen);
136 last = xmalloc(maxlen);
137 *first = *last = '\0';
139 #define ALG_APPEND(to, from) \
142 strlcat(to, ",", maxlen); \
143 strlcat(to, from, maxlen); \
146 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
147 if ((ktype = key_type_from_name(alg)) == KEY_UNSPEC)
148 fatal("%s: unknown alg %s", __func__, alg);
149 if (lookup_key_in_hostkeys_by_type(hostkeys,
150 key_type_plain(ktype), NULL))
151 ALG_APPEND(first, alg);
153 ALG_APPEND(last, alg);
156 xasprintf(&ret, "%s%s%s", first, *first == '\0' ? "" : ",", last);
158 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
164 free_hostkeys(hostkeys);
170 ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
175 xxx_hostaddr = hostaddr;
177 if (options.ciphers == (char *)-1) {
178 logit("No valid ciphers for protocol version 2 given, using defaults.");
179 options.ciphers = NULL;
181 if (options.ciphers != NULL) {
182 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
183 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
185 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
186 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
187 myproposal[PROPOSAL_ENC_ALGS_STOC] =
188 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
189 if (options.compression) {
190 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
191 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
193 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
194 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
196 if (options.macs != NULL) {
197 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
198 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
200 if (options.hostkeyalgorithms != NULL)
201 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
202 compat_pkalg_proposal(options.hostkeyalgorithms);
204 /* Prefer algorithms that we already have keys for */
205 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
206 compat_pkalg_proposal(
207 order_hostkeyalgs(host, hostaddr, port));
209 if (options.kex_algorithms != NULL)
210 myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
212 if (options.rekey_limit || options.rekey_interval)
213 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
214 (time_t)options.rekey_interval);
216 /* start key exchange */
217 kex = kex_setup(myproposal);
218 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
219 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
220 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
221 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
222 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
223 kex->kex[KEX_C25519_SHA256] = kexc25519_client;
224 kex->client_version_string=client_version_string;
225 kex->server_version_string=server_version_string;
226 kex->verify_host_key=&verify_host_key_callback;
230 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
232 if (options.use_roaming && !kex->roaming) {
233 debug("Roaming not allowed by server");
234 options.use_roaming = 0;
237 session_id2 = kex->session_id;
238 session_id2_len = kex->session_id_len;
241 /* send 1st encrypted/maced/compressed message */
242 packet_start(SSH2_MSG_IGNORE);
243 packet_put_cstring("markus");
253 typedef struct Authctxt Authctxt;
254 typedef struct Authmethod Authmethod;
255 typedef struct identity Identity;
256 typedef struct idlist Idlist;
259 TAILQ_ENTRY(identity) next;
260 AuthenticationConnection *ac; /* set if agent supports key */
261 Key *key; /* public/private key */
262 char *filename; /* comment for agent-only keys */
264 int isprivate; /* key points to the private key */
267 TAILQ_HEAD(idlist, identity);
270 const char *server_user;
271 const char *local_user;
275 sig_atomic_t success;
279 AuthenticationConnection *agent;
281 Sensitive *sensitive;
282 /* kbd-interactive */
288 char *name; /* string to compare against server's list */
289 int (*userauth)(Authctxt *authctxt);
290 void (*cleanup)(Authctxt *authctxt);
291 int *enabled; /* flag in option struct that enables method */
292 int *batch_flag; /* flag in option struct that disables method */
295 void input_userauth_success(int, u_int32_t, void *);
296 void input_userauth_success_unexpected(int, u_int32_t, void *);
297 void input_userauth_failure(int, u_int32_t, void *);
298 void input_userauth_banner(int, u_int32_t, void *);
299 void input_userauth_error(int, u_int32_t, void *);
300 void input_userauth_info_req(int, u_int32_t, void *);
301 void input_userauth_pk_ok(int, u_int32_t, void *);
302 void input_userauth_passwd_changereq(int, u_int32_t, void *);
303 void input_userauth_jpake_server_step1(int, u_int32_t, void *);
304 void input_userauth_jpake_server_step2(int, u_int32_t, void *);
305 void input_userauth_jpake_server_confirm(int, u_int32_t, void *);
307 int userauth_none(Authctxt *);
308 int userauth_pubkey(Authctxt *);
309 int userauth_passwd(Authctxt *);
310 int userauth_kbdint(Authctxt *);
311 int userauth_hostbased(Authctxt *);
312 int userauth_jpake(Authctxt *);
314 void userauth_jpake_cleanup(Authctxt *);
317 int userauth_gssapi(Authctxt *authctxt);
318 void input_gssapi_response(int type, u_int32_t, void *);
319 void input_gssapi_token(int type, u_int32_t, void *);
320 void input_gssapi_hash(int type, u_int32_t, void *);
321 void input_gssapi_error(int, u_int32_t, void *);
322 void input_gssapi_errtok(int, u_int32_t, void *);
325 void userauth(Authctxt *, char *);
327 static int sign_and_send_pubkey(Authctxt *, Identity *);
328 static void pubkey_prepare(Authctxt *);
329 static void pubkey_cleanup(Authctxt *);
330 static Key *load_identity_file(char *, int);
332 static Authmethod *authmethod_get(char *authlist);
333 static Authmethod *authmethod_lookup(const char *name);
334 static char *authmethods_get(void);
336 Authmethod authmethods[] = {
341 &options.gss_authentication,
347 &options.hostbased_authentication,
352 &options.pubkey_authentication,
355 {"jpake-01@openssh.com",
357 userauth_jpake_cleanup,
358 &options.zero_knowledge_password_authentication,
359 &options.batch_mode},
361 {"keyboard-interactive",
364 &options.kbd_interactive_authentication,
365 &options.batch_mode},
369 &options.password_authentication,
370 &options.batch_mode},
376 {NULL, NULL, NULL, NULL, NULL}
380 ssh_userauth2(const char *local_user, const char *server_user, char *host,
381 Sensitive *sensitive)
386 if (options.challenge_response_authentication)
387 options.kbd_interactive_authentication = 1;
389 packet_start(SSH2_MSG_SERVICE_REQUEST);
390 packet_put_cstring("ssh-userauth");
392 debug("SSH2_MSG_SERVICE_REQUEST sent");
394 type = packet_read();
395 if (type != SSH2_MSG_SERVICE_ACCEPT)
396 fatal("Server denied authentication request: %d", type);
397 if (packet_remaining() > 0) {
398 char *reply = packet_get_string(NULL);
399 debug2("service_accept: %s", reply);
402 debug2("buggy server: service_accept w/o service");
405 debug("SSH2_MSG_SERVICE_ACCEPT received");
407 if (options.preferred_authentications == NULL)
408 options.preferred_authentications = authmethods_get();
410 /* setup authentication context */
411 memset(&authctxt, 0, sizeof(authctxt));
412 pubkey_prepare(&authctxt);
413 authctxt.server_user = server_user;
414 authctxt.local_user = local_user;
415 authctxt.host = host;
416 authctxt.service = "ssh-connection"; /* service name */
417 authctxt.success = 0;
418 authctxt.method = authmethod_lookup("none");
419 authctxt.authlist = NULL;
420 authctxt.methoddata = NULL;
421 authctxt.sensitive = sensitive;
422 authctxt.info_req_seen = 0;
423 if (authctxt.method == NULL)
424 fatal("ssh_userauth2: internal error: cannot send userauth none request");
426 /* initial userauth request */
427 userauth_none(&authctxt);
429 dispatch_init(&input_userauth_error);
430 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
431 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
432 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
433 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
435 pubkey_cleanup(&authctxt);
436 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
438 #ifdef NONE_CIPHER_ENABLED
440 * If the user explicitly requests to use the none cipher enable it
441 * post authentication and only if the right conditions are met: both
442 * of the NONE switches must be true and there must be no tty allocated.
444 if (options.none_switch == 1 && options.none_enabled == 1) {
446 debug("Requesting none cipher re-keying...");
447 myproposal[PROPOSAL_ENC_ALGS_STOC] = "none";
448 myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none";
449 kex_prop2buf(&xxx_kex->my, myproposal);
450 packet_request_rekeying();
451 fprintf(stderr, "WARNING: enabled NONE cipher\n");
453 /* Requested NONE cipher on an interactive session. */
454 debug("Cannot switch to NONE cipher with tty "
456 fprintf(stderr, "NONE cipher switch disabled given "
457 "a TTY is allocated\n");
461 debug("Authentication succeeded (%s).", authctxt.method->name);
465 userauth(Authctxt *authctxt, char *authlist)
467 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
468 authctxt->method->cleanup(authctxt);
470 free(authctxt->methoddata);
471 authctxt->methoddata = NULL;
472 if (authlist == NULL) {
473 authlist = authctxt->authlist;
475 free(authctxt->authlist);
476 authctxt->authlist = authlist;
479 Authmethod *method = authmethod_get(authlist);
481 fatal("Permission denied (%s).", authlist);
482 authctxt->method = method;
484 /* reset the per method handler */
485 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
486 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
488 /* and try new method */
489 if (method->userauth(authctxt) != 0) {
490 debug2("we sent a %s packet, wait for reply", method->name);
493 debug2("we did not send a packet, disable method");
494 method->enabled = NULL;
501 input_userauth_error(int type, u_int32_t seq, void *ctxt)
503 fatal("input_userauth_error: bad message during authentication: "
509 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
511 char *msg, *raw, *lang;
514 debug3("input_userauth_banner");
515 raw = packet_get_string(&len);
516 lang = packet_get_string(NULL);
517 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
520 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
521 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
522 fprintf(stderr, "%s", msg);
531 input_userauth_success(int type, u_int32_t seq, void *ctxt)
533 Authctxt *authctxt = ctxt;
535 if (authctxt == NULL)
536 fatal("input_userauth_success: no authentication context");
537 free(authctxt->authlist);
538 authctxt->authlist = NULL;
539 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
540 authctxt->method->cleanup(authctxt);
541 free(authctxt->methoddata);
542 authctxt->methoddata = NULL;
543 authctxt->success = 1; /* break out */
547 input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
549 Authctxt *authctxt = ctxt;
551 if (authctxt == NULL)
552 fatal("%s: no authentication context", __func__);
554 fatal("Unexpected authentication success during %s.",
555 authctxt->method->name);
560 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
562 Authctxt *authctxt = ctxt;
563 char *authlist = NULL;
566 if (authctxt == NULL)
567 fatal("input_userauth_failure: no authentication context");
569 authlist = packet_get_string(NULL);
570 partial = packet_get_char();
574 logit("Authenticated with partial success.");
576 pubkey_cleanup(authctxt);
577 pubkey_prepare(authctxt);
579 debug("Authentications that can continue: %s", authlist);
581 userauth(authctxt, authlist);
586 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
588 Authctxt *authctxt = ctxt;
592 int pktype, sent = 0;
597 if (authctxt == NULL)
598 fatal("input_userauth_pk_ok: no authentication context");
599 if (datafellows & SSH_BUG_PKOK) {
600 /* this is similar to SSH_BUG_PKAUTH */
601 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
602 pkblob = packet_get_string(&blen);
604 buffer_append(&b, pkblob, blen);
605 pkalg = buffer_get_string(&b, &alen);
608 pkalg = packet_get_string(&alen);
609 pkblob = packet_get_string(&blen);
613 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
615 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
616 debug("unknown pkalg %s", pkalg);
619 if ((key = key_from_blob(pkblob, blen)) == NULL) {
620 debug("no key from blob. pkalg %s", pkalg);
623 if (key->type != pktype) {
624 error("input_userauth_pk_ok: type mismatch "
625 "for decoded key (received %d, expected %d)",
629 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
630 debug2("input_userauth_pk_ok: fp %s", fp);
634 * search keys in the reverse order, because last candidate has been
635 * moved to the end of the queue. this also avoids confusion by
638 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
639 if (key_equal(key, id->key)) {
640 sent = sign_and_send_pubkey(authctxt, id);
650 /* try another method if we did not send a packet */
652 userauth(authctxt, NULL);
657 userauth_gssapi(Authctxt *authctxt)
659 Gssctxt *gssctxt = NULL;
660 static gss_OID_set gss_supported = NULL;
661 static u_int mech = 0;
665 /* Try one GSSAPI method at a time, rather than sending them all at
668 if (gss_supported == NULL)
669 gss_indicate_mechs(&min, &gss_supported);
671 /* Check to see if the mechanism is usable before we offer it */
672 while (mech < gss_supported->count && !ok) {
673 /* My DER encoding requires length<128 */
674 if (gss_supported->elements[mech].length < 128 &&
675 ssh_gssapi_check_mechanism(&gssctxt,
676 &gss_supported->elements[mech], authctxt->host)) {
677 ok = 1; /* Mechanism works */
686 authctxt->methoddata=(void *)gssctxt;
688 packet_start(SSH2_MSG_USERAUTH_REQUEST);
689 packet_put_cstring(authctxt->server_user);
690 packet_put_cstring(authctxt->service);
691 packet_put_cstring(authctxt->method->name);
695 packet_put_int((gss_supported->elements[mech].length) + 2);
696 packet_put_char(SSH_GSS_OIDTYPE);
697 packet_put_char(gss_supported->elements[mech].length);
698 packet_put_raw(gss_supported->elements[mech].elements,
699 gss_supported->elements[mech].length);
703 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
704 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
705 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
706 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
708 mech++; /* Move along to next candidate */
714 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
716 Authctxt *authctxt = ctxt;
717 Gssctxt *gssctxt = authctxt->methoddata;
718 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
719 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
720 gss_buffer_desc gssbuf;
721 OM_uint32 status, ms, flags;
724 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
725 recv_tok, &send_tok, &flags);
727 if (send_tok.length > 0) {
728 if (GSS_ERROR(status))
729 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
731 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
733 packet_put_string(send_tok.value, send_tok.length);
735 gss_release_buffer(&ms, &send_tok);
738 if (status == GSS_S_COMPLETE) {
739 /* send either complete or MIC, depending on mechanism */
740 if (!(flags & GSS_C_INTEG_FLAG)) {
741 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
744 ssh_gssapi_buildmic(&b, authctxt->server_user,
745 authctxt->service, "gssapi-with-mic");
747 gssbuf.value = buffer_ptr(&b);
748 gssbuf.length = buffer_len(&b);
750 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
752 if (!GSS_ERROR(status)) {
753 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
754 packet_put_string(mic.value, mic.length);
760 gss_release_buffer(&ms, &mic);
769 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
771 Authctxt *authctxt = ctxt;
776 if (authctxt == NULL)
777 fatal("input_gssapi_response: no authentication context");
778 gssctxt = authctxt->methoddata;
781 oidv = packet_get_string(&oidlen);
784 oidv[0] != SSH_GSS_OIDTYPE ||
785 oidv[1] != oidlen - 2) {
787 debug("Badly encoded mechanism OID received");
788 userauth(authctxt, NULL);
792 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
793 fatal("Server returned different OID than expected");
799 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
800 /* Start again with next method on list */
801 debug("Trying to start again");
802 userauth(authctxt, NULL);
809 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
811 Authctxt *authctxt = ctxt;
812 gss_buffer_desc recv_tok;
816 if (authctxt == NULL)
817 fatal("input_gssapi_response: no authentication context");
819 recv_tok.value = packet_get_string(&slen);
820 recv_tok.length = slen; /* safe typecast */
824 status = process_gssapi_token(ctxt, &recv_tok);
826 free(recv_tok.value);
828 if (GSS_ERROR(status)) {
829 /* Start again with the next method in the list */
830 userauth(authctxt, NULL);
837 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
839 Authctxt *authctxt = ctxt;
841 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
842 gss_buffer_desc recv_tok;
846 if (authctxt == NULL)
847 fatal("input_gssapi_response: no authentication context");
848 gssctxt = authctxt->methoddata;
850 recv_tok.value = packet_get_string(&len);
851 recv_tok.length = len;
855 /* Stick it into GSSAPI and see what it says */
856 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
857 &recv_tok, &send_tok, NULL);
859 free(recv_tok.value);
860 gss_release_buffer(&ms, &send_tok);
862 /* Server will be returning a failed packet after this one */
867 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
872 /* maj */(void)packet_get_int();
873 /* min */(void)packet_get_int();
874 msg=packet_get_string(NULL);
875 lang=packet_get_string(NULL);
879 debug("Server GSSAPI Error:\n%s", msg);
886 userauth_none(Authctxt *authctxt)
888 /* initial userauth request */
889 packet_start(SSH2_MSG_USERAUTH_REQUEST);
890 packet_put_cstring(authctxt->server_user);
891 packet_put_cstring(authctxt->service);
892 packet_put_cstring(authctxt->method->name);
898 userauth_passwd(Authctxt *authctxt)
900 static int attempt = 0;
903 const char *host = options.host_key_alias ? options.host_key_alias :
906 if (attempt++ >= options.number_of_password_prompts)
910 error("Permission denied, please try again.");
912 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
913 authctxt->server_user, host);
914 password = read_passphrase(prompt, 0);
915 packet_start(SSH2_MSG_USERAUTH_REQUEST);
916 packet_put_cstring(authctxt->server_user);
917 packet_put_cstring(authctxt->service);
918 packet_put_cstring(authctxt->method->name);
920 packet_put_cstring(password);
921 memset(password, 0, strlen(password));
923 packet_add_padding(64);
926 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
927 &input_userauth_passwd_changereq);
933 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
937 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
939 Authctxt *authctxt = ctxt;
940 char *info, *lang, *password = NULL, *retype = NULL;
942 const char *host = options.host_key_alias ? options.host_key_alias :
945 debug2("input_userauth_passwd_changereq");
947 if (authctxt == NULL)
948 fatal("input_userauth_passwd_changereq: "
949 "no authentication context");
951 info = packet_get_string(NULL);
952 lang = packet_get_string(NULL);
953 if (strlen(info) > 0)
957 packet_start(SSH2_MSG_USERAUTH_REQUEST);
958 packet_put_cstring(authctxt->server_user);
959 packet_put_cstring(authctxt->service);
960 packet_put_cstring(authctxt->method->name);
961 packet_put_char(1); /* additional info */
962 snprintf(prompt, sizeof(prompt),
963 "Enter %.30s@%.128s's old password: ",
964 authctxt->server_user, host);
965 password = read_passphrase(prompt, 0);
966 packet_put_cstring(password);
967 memset(password, 0, strlen(password));
970 while (password == NULL) {
971 snprintf(prompt, sizeof(prompt),
972 "Enter %.30s@%.128s's new password: ",
973 authctxt->server_user, host);
974 password = read_passphrase(prompt, RP_ALLOW_EOF);
975 if (password == NULL) {
979 snprintf(prompt, sizeof(prompt),
980 "Retype %.30s@%.128s's new password: ",
981 authctxt->server_user, host);
982 retype = read_passphrase(prompt, 0);
983 if (strcmp(password, retype) != 0) {
984 memset(password, 0, strlen(password));
986 logit("Mismatch; try again, EOF to quit.");
989 memset(retype, 0, strlen(retype));
992 packet_put_cstring(password);
993 memset(password, 0, strlen(password));
995 packet_add_padding(64);
998 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
999 &input_userauth_passwd_changereq);
1004 pw_encrypt(const char *password, const char *crypt_scheme, const char *salt)
1006 /* OpenBSD crypt(3) handles all of these */
1007 if (strcmp(crypt_scheme, "crypt") == 0 ||
1008 strcmp(crypt_scheme, "bcrypt") == 0 ||
1009 strcmp(crypt_scheme, "md5crypt") == 0 ||
1010 strcmp(crypt_scheme, "crypt-extended") == 0)
1011 return xstrdup(crypt(password, salt));
1012 error("%s: unsupported password encryption scheme \"%.100s\"",
1013 __func__, crypt_scheme);
1018 jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme,
1021 char prompt[256], *password, *crypted;
1026 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ",
1027 authctxt->server_user, authctxt->host);
1028 password = read_passphrase(prompt, 0);
1030 if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) {
1031 logit("Disabling %s authentication", authctxt->method->name);
1032 authctxt->method->enabled = NULL;
1033 /* Continue with an empty password to fail gracefully */
1034 crypted = xstrdup("");
1038 debug3("%s: salt = %s", __func__, salt);
1039 debug3("%s: scheme = %s", __func__, crypt_scheme);
1040 debug3("%s: crypted = %s", __func__, crypted);
1043 if (hash_buffer(crypted, strlen(crypted), SSH_DIGEST_SHA1,
1044 &secret, &secret_len) != 0)
1045 fatal("%s: hash_buffer", __func__);
1047 bzero(password, strlen(password));
1048 bzero(crypted, strlen(crypted));
1052 if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL)
1053 fatal("%s: BN_bin2bn (secret)", __func__);
1054 bzero(secret, secret_len);
1062 input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt)
1064 Authctxt *authctxt = ctxt;
1065 struct jpake_ctx *pctx = authctxt->methoddata;
1066 u_char *x3_proof, *x4_proof, *x2_s_proof;
1067 u_int x3_proof_len, x4_proof_len, x2_s_proof_len;
1068 char *crypt_scheme, *salt;
1070 /* Disable this message */
1071 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL);
1073 if ((pctx->g_x3 = BN_new()) == NULL ||
1074 (pctx->g_x4 = BN_new()) == NULL)
1075 fatal("%s: BN_new", __func__);
1077 /* Fetch step 1 values */
1078 crypt_scheme = packet_get_string(NULL);
1079 salt = packet_get_string(NULL);
1080 pctx->server_id = packet_get_string(&pctx->server_id_len);
1081 packet_get_bignum2(pctx->g_x3);
1082 packet_get_bignum2(pctx->g_x4);
1083 x3_proof = packet_get_string(&x3_proof_len);
1084 x4_proof = packet_get_string(&x4_proof_len);
1087 JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__));
1089 /* Obtain password and derive secret */
1090 pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt);
1091 bzero(crypt_scheme, strlen(crypt_scheme));
1092 bzero(salt, strlen(salt));
1095 JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__));
1097 /* Calculate step 2 values */
1098 jpake_step2(pctx->grp, pctx->s, pctx->g_x1,
1099 pctx->g_x3, pctx->g_x4, pctx->x2,
1100 pctx->server_id, pctx->server_id_len,
1101 pctx->client_id, pctx->client_id_len,
1102 x3_proof, x3_proof_len,
1103 x4_proof, x4_proof_len,
1105 &x2_s_proof, &x2_s_proof_len);
1107 bzero(x3_proof, x3_proof_len);
1108 bzero(x4_proof, x4_proof_len);
1112 JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__));
1114 /* Send values for step 2 */
1115 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2);
1116 packet_put_bignum2(pctx->a);
1117 packet_put_string(x2_s_proof, x2_s_proof_len);
1120 bzero(x2_s_proof, x2_s_proof_len);
1123 /* Expect step 2 packet from peer */
1124 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2,
1125 input_userauth_jpake_server_step2);
1130 input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt)
1132 Authctxt *authctxt = ctxt;
1133 struct jpake_ctx *pctx = authctxt->methoddata;
1135 u_int x4_s_proof_len;
1137 /* Disable this message */
1138 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL);
1140 if ((pctx->b = BN_new()) == NULL)
1141 fatal("%s: BN_new", __func__);
1143 /* Fetch step 2 values */
1144 packet_get_bignum2(pctx->b);
1145 x4_s_proof = packet_get_string(&x4_s_proof_len);
1148 JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__));
1150 /* Derive shared key and calculate confirmation hash */
1151 jpake_key_confirm(pctx->grp, pctx->s, pctx->b,
1152 pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4,
1153 pctx->client_id, pctx->client_id_len,
1154 pctx->server_id, pctx->server_id_len,
1155 session_id2, session_id2_len,
1156 x4_s_proof, x4_s_proof_len,
1158 &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len);
1160 bzero(x4_s_proof, x4_s_proof_len);
1163 JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__));
1165 /* Send key confirmation proof */
1166 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM);
1167 packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
1170 /* Expect confirmation from peer */
1171 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM,
1172 input_userauth_jpake_server_confirm);
1177 input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt)
1179 Authctxt *authctxt = ctxt;
1180 struct jpake_ctx *pctx = authctxt->methoddata;
1182 /* Disable this message */
1183 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL);
1185 pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len);
1188 JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__));
1190 /* Verify expected confirmation hash */
1191 if (jpake_check_confirm(pctx->k,
1192 pctx->server_id, pctx->server_id_len,
1193 session_id2, session_id2_len,
1194 pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1)
1195 debug("%s: %s success", __func__, authctxt->method->name);
1197 debug("%s: confirmation mismatch", __func__);
1198 /* XXX stash this so if auth succeeds then we can warn/kill */
1201 userauth_jpake_cleanup(authctxt);
1206 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1207 u_char *data, u_int datalen)
1212 /* the agent supports this key */
1214 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1217 * we have already loaded the private key or
1218 * the private key is stored in external hardware
1220 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1221 return (key_sign(id->key, sigp, lenp, data, datalen));
1222 /* load the private key from the file */
1223 if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL)
1225 ret = key_sign(prv, sigp, lenp, data, datalen);
1231 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1234 u_char *blob, *signature;
1235 u_int bloblen, slen;
1241 fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
1242 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
1245 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1246 /* we cannot handle this key */
1247 debug3("sign_and_send_pubkey: cannot handle key");
1250 /* data to be signed */
1252 if (datafellows & SSH_OLD_SESSIONID) {
1253 buffer_append(&b, session_id2, session_id2_len);
1254 skip = session_id2_len;
1256 buffer_put_string(&b, session_id2, session_id2_len);
1257 skip = buffer_len(&b);
1259 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1260 buffer_put_cstring(&b, authctxt->server_user);
1261 buffer_put_cstring(&b,
1262 datafellows & SSH_BUG_PKSERVICE ?
1265 if (datafellows & SSH_BUG_PKAUTH) {
1266 buffer_put_char(&b, have_sig);
1268 buffer_put_cstring(&b, authctxt->method->name);
1269 buffer_put_char(&b, have_sig);
1270 buffer_put_cstring(&b, key_ssh_name(id->key));
1272 buffer_put_string(&b, blob, bloblen);
1274 /* generate signature */
1275 ret = identity_sign(id, &signature, &slen,
1276 buffer_ptr(&b), buffer_len(&b));
1285 if (datafellows & SSH_BUG_PKSERVICE) {
1287 buffer_append(&b, session_id2, session_id2_len);
1288 skip = session_id2_len;
1289 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1290 buffer_put_cstring(&b, authctxt->server_user);
1291 buffer_put_cstring(&b, authctxt->service);
1292 buffer_put_cstring(&b, authctxt->method->name);
1293 buffer_put_char(&b, have_sig);
1294 if (!(datafellows & SSH_BUG_PKAUTH))
1295 buffer_put_cstring(&b, key_ssh_name(id->key));
1296 buffer_put_string(&b, blob, bloblen);
1300 /* append signature */
1301 buffer_put_string(&b, signature, slen);
1304 /* skip session id and packet type */
1305 if (buffer_len(&b) < skip + 1)
1306 fatal("userauth_pubkey: internal error");
1307 buffer_consume(&b, skip + 1);
1309 /* put remaining data from buffer into packet */
1310 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1311 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1319 send_pubkey_test(Authctxt *authctxt, Identity *id)
1322 u_int bloblen, have_sig = 0;
1324 debug3("send_pubkey_test");
1326 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1327 /* we cannot handle this key */
1328 debug3("send_pubkey_test: cannot handle key");
1331 /* register callback for USERAUTH_PK_OK message */
1332 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1334 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1335 packet_put_cstring(authctxt->server_user);
1336 packet_put_cstring(authctxt->service);
1337 packet_put_cstring(authctxt->method->name);
1338 packet_put_char(have_sig);
1339 if (!(datafellows & SSH_BUG_PKAUTH))
1340 packet_put_cstring(key_ssh_name(id->key));
1341 packet_put_string(blob, bloblen);
1348 load_identity_file(char *filename, int userprovided)
1351 char prompt[300], *passphrase;
1352 int perm_ok = 0, quit, i;
1355 if (stat(filename, &st) < 0) {
1356 (userprovided ? logit : debug3)("no such identity: %s: %s",
1357 filename, strerror(errno));
1360 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1362 if (private != NULL)
1366 if (private == NULL) {
1367 if (options.batch_mode)
1369 snprintf(prompt, sizeof prompt,
1370 "Enter passphrase for key '%.100s': ", filename);
1371 for (i = 0; i < options.number_of_password_prompts; i++) {
1372 passphrase = read_passphrase(prompt, 0);
1373 if (strcmp(passphrase, "") != 0) {
1374 private = key_load_private_type(KEY_UNSPEC,
1375 filename, passphrase, NULL, NULL);
1378 debug2("no passphrase given, try next key");
1381 memset(passphrase, 0, strlen(passphrase));
1383 if (private != NULL || quit)
1385 debug2("bad passphrase given, try again...");
1392 * try keys in the following order:
1393 * 1. agent keys that are found in the config file
1394 * 2. other agent keys
1395 * 3. keys that are only listed in the config file
1398 pubkey_prepare(Authctxt *authctxt)
1400 Identity *id, *id2, *tmp;
1401 Idlist agent, files, *preferred;
1403 AuthenticationConnection *ac;
1407 TAILQ_INIT(&agent); /* keys from the agent */
1408 TAILQ_INIT(&files); /* keys from the config file */
1409 preferred = &authctxt->keys;
1410 TAILQ_INIT(preferred); /* preferred order of keys */
1412 /* list of keys stored in the filesystem and PKCS#11 */
1413 for (i = 0; i < options.num_identity_files; i++) {
1414 key = options.identity_keys[i];
1415 if (key && key->type == KEY_RSA1)
1417 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1419 options.identity_keys[i] = NULL;
1420 id = xcalloc(1, sizeof(*id));
1422 id->filename = xstrdup(options.identity_files[i]);
1423 id->userprovided = options.identity_file_userprovided[i];
1424 TAILQ_INSERT_TAIL(&files, id, next);
1426 /* Prefer PKCS11 keys that are explicitly listed */
1427 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1428 if (id->key == NULL || (id->key->flags & KEY_FLAG_EXT) == 0)
1431 TAILQ_FOREACH(id2, &files, next) {
1432 if (id2->key == NULL ||
1433 (id2->key->flags & KEY_FLAG_EXT) != 0)
1435 if (key_equal(id->key, id2->key)) {
1436 TAILQ_REMOVE(&files, id, next);
1437 TAILQ_INSERT_TAIL(preferred, id, next);
1442 /* If IdentitiesOnly set and key not found then don't use it */
1443 if (!found && options.identities_only) {
1444 TAILQ_REMOVE(&files, id, next);
1445 bzero(id, sizeof(*id));
1449 /* list of keys supported by the agent */
1450 if ((ac = ssh_get_authentication_connection())) {
1451 for (key = ssh_get_first_identity(ac, &comment, 2);
1453 key = ssh_get_next_identity(ac, &comment, 2)) {
1455 TAILQ_FOREACH(id, &files, next) {
1456 /* agent keys from the config file are preferred */
1457 if (key_equal(key, id->key)) {
1460 TAILQ_REMOVE(&files, id, next);
1461 TAILQ_INSERT_TAIL(preferred, id, next);
1467 if (!found && !options.identities_only) {
1468 id = xcalloc(1, sizeof(*id));
1470 id->filename = comment;
1472 TAILQ_INSERT_TAIL(&agent, id, next);
1475 /* append remaining agent keys */
1476 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1477 TAILQ_REMOVE(&agent, id, next);
1478 TAILQ_INSERT_TAIL(preferred, id, next);
1480 authctxt->agent = ac;
1482 /* append remaining keys from the config file */
1483 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1484 TAILQ_REMOVE(&files, id, next);
1485 TAILQ_INSERT_TAIL(preferred, id, next);
1487 TAILQ_FOREACH(id, preferred, next) {
1488 debug2("key: %s (%p),%s", id->filename, id->key,
1489 id->userprovided ? " explicit" : "");
1494 pubkey_cleanup(Authctxt *authctxt)
1498 if (authctxt->agent != NULL)
1499 ssh_close_authentication_connection(authctxt->agent);
1500 for (id = TAILQ_FIRST(&authctxt->keys); id;
1501 id = TAILQ_FIRST(&authctxt->keys)) {
1502 TAILQ_REMOVE(&authctxt->keys, id, next);
1511 userauth_pubkey(Authctxt *authctxt)
1516 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1519 /* move key to the end of the queue */
1520 TAILQ_REMOVE(&authctxt->keys, id, next);
1521 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1523 * send a test message if we have the public key. for
1524 * encrypted keys we cannot do this and have to load the
1525 * private key instead
1527 if (id->key != NULL) {
1528 if (key_type_plain(id->key->type) == KEY_RSA &&
1529 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1530 debug("Skipped %s key %s for RSA/MD5 server",
1531 key_type(id->key), id->filename);
1532 } else if (id->key->type != KEY_RSA1) {
1533 debug("Offering %s public key: %s",
1534 key_type(id->key), id->filename);
1535 sent = send_pubkey_test(authctxt, id);
1538 debug("Trying private key: %s", id->filename);
1539 id->key = load_identity_file(id->filename,
1541 if (id->key != NULL) {
1543 if (key_type_plain(id->key->type) == KEY_RSA &&
1544 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1545 debug("Skipped %s key %s for RSA/MD5 "
1546 "server", key_type(id->key),
1549 sent = sign_and_send_pubkey(
1563 * Send userauth request message specifying keyboard-interactive method.
1566 userauth_kbdint(Authctxt *authctxt)
1568 static int attempt = 0;
1570 if (attempt++ >= options.number_of_password_prompts)
1572 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1573 if (attempt > 1 && !authctxt->info_req_seen) {
1574 debug3("userauth_kbdint: disable: no info_req_seen");
1575 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1579 debug2("userauth_kbdint");
1580 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1581 packet_put_cstring(authctxt->server_user);
1582 packet_put_cstring(authctxt->service);
1583 packet_put_cstring(authctxt->method->name);
1584 packet_put_cstring(""); /* lang */
1585 packet_put_cstring(options.kbd_interactive_devices ?
1586 options.kbd_interactive_devices : "");
1589 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1594 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1597 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1599 Authctxt *authctxt = ctxt;
1600 char *name, *inst, *lang, *prompt, *response;
1601 u_int num_prompts, i;
1604 debug2("input_userauth_info_req");
1606 if (authctxt == NULL)
1607 fatal("input_userauth_info_req: no authentication context");
1609 authctxt->info_req_seen = 1;
1611 name = packet_get_string(NULL);
1612 inst = packet_get_string(NULL);
1613 lang = packet_get_string(NULL);
1614 if (strlen(name) > 0)
1616 if (strlen(inst) > 0)
1622 num_prompts = packet_get_int();
1624 * Begin to build info response packet based on prompts requested.
1625 * We commit to providing the correct number of responses, so if
1626 * further on we run into a problem that prevents this, we have to
1627 * be sure and clean this up and send a correct error response.
1629 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1630 packet_put_int(num_prompts);
1632 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1633 for (i = 0; i < num_prompts; i++) {
1634 prompt = packet_get_string(NULL);
1635 echo = packet_get_char();
1637 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1639 packet_put_cstring(response);
1640 memset(response, 0, strlen(response));
1644 packet_check_eom(); /* done with parsing incoming message. */
1646 packet_add_padding(64);
1651 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1652 u_char *data, u_int datalen)
1657 int to[2], from[2], status, version = 2;
1659 debug2("ssh_keysign called");
1661 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1662 error("ssh_keysign: not installed: %s", strerror(errno));
1665 if (fflush(stdout) != 0)
1666 error("ssh_keysign: fflush: %s", strerror(errno));
1668 error("ssh_keysign: pipe: %s", strerror(errno));
1671 if (pipe(from) < 0) {
1672 error("ssh_keysign: pipe: %s", strerror(errno));
1675 if ((pid = fork()) < 0) {
1676 error("ssh_keysign: fork: %s", strerror(errno));
1680 /* keep the socket on exec */
1681 fcntl(packet_get_connection_in(), F_SETFD, 0);
1682 permanently_drop_suid(getuid());
1684 if (dup2(from[1], STDOUT_FILENO) < 0)
1685 fatal("ssh_keysign: dup2: %s", strerror(errno));
1687 if (dup2(to[0], STDIN_FILENO) < 0)
1688 fatal("ssh_keysign: dup2: %s", strerror(errno));
1691 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1692 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1699 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1700 buffer_put_string(&b, data, datalen);
1701 if (ssh_msg_send(to[1], version, &b) == -1)
1702 fatal("ssh_keysign: couldn't send request");
1704 if (ssh_msg_recv(from[0], &b) < 0) {
1705 error("ssh_keysign: no reply");
1712 while (waitpid(pid, &status, 0) < 0)
1716 if (buffer_get_char(&b) != version) {
1717 error("ssh_keysign: bad version");
1721 *sigp = buffer_get_string(&b, lenp);
1728 userauth_hostbased(Authctxt *authctxt)
1730 Key *private = NULL;
1731 Sensitive *sensitive = authctxt->sensitive;
1733 u_char *signature, *blob;
1734 char *chost, *pkalg, *p;
1735 const char *service;
1737 int ok, i, found = 0;
1739 /* check for a useful key */
1740 for (i = 0; i < sensitive->nkeys; i++) {
1741 private = sensitive->keys[i];
1742 if (private && private->type != KEY_RSA1) {
1744 /* we take and free the key */
1745 sensitive->keys[i] = NULL;
1750 debug("No more client hostkeys for hostbased authentication.");
1753 if (key_to_blob(private, &blob, &blen) == 0) {
1757 /* figure out a name for the client host */
1758 p = get_local_name(packet_get_connection_in());
1760 error("userauth_hostbased: cannot get local ipaddr/name");
1765 xasprintf(&chost, "%s.", p);
1766 debug2("userauth_hostbased: chost %s", chost);
1769 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1771 pkalg = xstrdup(key_ssh_name(private));
1773 /* construct data */
1774 buffer_put_string(&b, session_id2, session_id2_len);
1775 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1776 buffer_put_cstring(&b, authctxt->server_user);
1777 buffer_put_cstring(&b, service);
1778 buffer_put_cstring(&b, authctxt->method->name);
1779 buffer_put_cstring(&b, pkalg);
1780 buffer_put_string(&b, blob, blen);
1781 buffer_put_cstring(&b, chost);
1782 buffer_put_cstring(&b, authctxt->local_user);
1786 if (sensitive->external_keysign)
1787 ok = ssh_keysign(private, &signature, &slen,
1788 buffer_ptr(&b), buffer_len(&b));
1790 ok = key_sign(private, &signature, &slen,
1791 buffer_ptr(&b), buffer_len(&b));
1795 error("key_sign failed");
1801 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1802 packet_put_cstring(authctxt->server_user);
1803 packet_put_cstring(authctxt->service);
1804 packet_put_cstring(authctxt->method->name);
1805 packet_put_cstring(pkalg);
1806 packet_put_string(blob, blen);
1807 packet_put_cstring(chost);
1808 packet_put_cstring(authctxt->local_user);
1809 packet_put_string(signature, slen);
1810 memset(signature, 's', slen);
1822 userauth_jpake(Authctxt *authctxt)
1824 struct jpake_ctx *pctx;
1825 u_char *x1_proof, *x2_proof;
1826 u_int x1_proof_len, x2_proof_len;
1827 static int attempt = 0; /* XXX share with userauth_password's? */
1829 if (attempt++ >= options.number_of_password_prompts)
1832 error("Permission denied, please try again.");
1834 if (authctxt->methoddata != NULL)
1835 fatal("%s: authctxt->methoddata already set (%p)",
1836 __func__, authctxt->methoddata);
1838 authctxt->methoddata = pctx = jpake_new();
1841 * Send request immediately, to get the protocol going while
1842 * we do the initial computations.
1844 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1845 packet_put_cstring(authctxt->server_user);
1846 packet_put_cstring(authctxt->service);
1847 packet_put_cstring(authctxt->method->name);
1849 packet_write_wait();
1851 jpake_step1(pctx->grp,
1852 &pctx->client_id, &pctx->client_id_len,
1853 &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2,
1854 &x1_proof, &x1_proof_len,
1855 &x2_proof, &x2_proof_len);
1857 JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__));
1859 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1);
1860 packet_put_string(pctx->client_id, pctx->client_id_len);
1861 packet_put_bignum2(pctx->g_x1);
1862 packet_put_bignum2(pctx->g_x2);
1863 packet_put_string(x1_proof, x1_proof_len);
1864 packet_put_string(x2_proof, x2_proof_len);
1867 bzero(x1_proof, x1_proof_len);
1868 bzero(x2_proof, x2_proof_len);
1872 /* Expect step 1 packet from peer */
1873 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1,
1874 input_userauth_jpake_server_step1);
1875 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS,
1876 &input_userauth_success_unexpected);
1882 userauth_jpake_cleanup(Authctxt *authctxt)
1884 debug3("%s: clean up", __func__);
1885 if (authctxt->methoddata != NULL) {
1886 jpake_free(authctxt->methoddata);
1887 authctxt->methoddata = NULL;
1889 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
1893 /* find auth method */
1896 * given auth method name, if configurable options permit this method fill
1897 * in auth_ident field and return true, otherwise return false.
1900 authmethod_is_enabled(Authmethod *method)
1904 /* return false if options indicate this method is disabled */
1905 if (method->enabled == NULL || *method->enabled == 0)
1907 /* return false if batch mode is enabled but method needs interactive mode */
1908 if (method->batch_flag != NULL && *method->batch_flag != 0)
1914 authmethod_lookup(const char *name)
1916 Authmethod *method = NULL;
1918 for (method = authmethods; method->name != NULL; method++)
1919 if (strcmp(name, method->name) == 0)
1921 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1925 /* XXX internal state */
1926 static Authmethod *current = NULL;
1927 static char *supported = NULL;
1928 static char *preferred = NULL;
1931 * Given the authentication method list sent by the server, return the
1932 * next method we should try. If the server initially sends a nil list,
1933 * use a built-in default list.
1936 authmethod_get(char *authlist)
1941 /* Use a suitable default if we're passed a nil list. */
1942 if (authlist == NULL || strlen(authlist) == 0)
1943 authlist = options.preferred_authentications;
1945 if (supported == NULL || strcmp(authlist, supported) != 0) {
1946 debug3("start over, passed a different list %s", authlist);
1948 supported = xstrdup(authlist);
1949 preferred = options.preferred_authentications;
1950 debug3("preferred %s", preferred);
1952 } else if (current != NULL && authmethod_is_enabled(current))
1956 if ((name = match_list(preferred, supported, &next)) == NULL) {
1957 debug("No more authentication methods to try.");
1962 debug3("authmethod_lookup %s", name);
1963 debug3("remaining preferred: %s", preferred);
1964 if ((current = authmethod_lookup(name)) != NULL &&
1965 authmethod_is_enabled(current)) {
1966 debug3("authmethod_is_enabled %s", name);
1967 debug("Next authentication method: %s", name);
1976 authmethods_get(void)
1978 Authmethod *method = NULL;
1983 for (method = authmethods; method->name != NULL; method++) {
1984 if (authmethod_is_enabled(method)) {
1985 if (buffer_len(&b) > 0)
1986 buffer_append(&b, ",", 1);
1987 buffer_append(&b, method->name, strlen(method->name));
1990 buffer_append(&b, "\0", 1);
1991 list = xstrdup(buffer_ptr(&b));