1 /* $OpenBSD: sshconnect2.c,v 1.186 2010/11/29 23:45:51 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)
47 #include "openbsd-compat/sys-queue.h"
58 #include "myproposal.h"
59 #include "sshconnect.h"
70 #include "pathnames.h"
81 extern char *client_version_string;
82 extern char *server_version_string;
83 extern Options options;
89 u_char *session_id2 = NULL;
90 u_int session_id2_len = 0;
93 struct sockaddr *xxx_hostaddr;
98 verify_host_key_callback(Key *hostkey)
100 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
101 fatal("Host key verification failed.");
106 order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
108 char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
110 struct hostkeys *hostkeys;
113 /* Find all hostkeys for this hostname */
114 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
115 hostkeys = init_hostkeys();
116 load_hostkeys(hostkeys, hostname, options.user_hostfile2);
117 load_hostkeys(hostkeys, hostname, options.system_hostfile2);
118 load_hostkeys(hostkeys, hostname, options.user_hostfile);
119 load_hostkeys(hostkeys, hostname, options.system_hostfile);
121 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
122 maxlen = strlen(avail) + 1;
123 first = xmalloc(maxlen);
124 last = xmalloc(maxlen);
125 *first = *last = '\0';
127 #define ALG_APPEND(to, from) \
130 strlcat(to, ",", maxlen); \
131 strlcat(to, from, maxlen); \
134 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
135 if ((ktype = key_type_from_name(alg)) == KEY_UNSPEC)
136 fatal("%s: unknown alg %s", __func__, alg);
137 if (lookup_key_in_hostkeys_by_type(hostkeys,
138 key_type_plain(ktype), NULL))
139 ALG_APPEND(first, alg);
141 ALG_APPEND(last, alg);
144 xasprintf(&ret, "%s%s%s", first, *first == '\0' ? "" : ",", last);
146 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
152 free_hostkeys(hostkeys);
158 ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
163 char *orig = NULL, *gss = NULL;
164 char *gss_host = NULL;
168 xxx_hostaddr = hostaddr;
171 if (options.gss_keyex) {
172 /* Add the GSSAPI mechanisms currently supported on this
173 * client to the key exchange algorithm proposal */
174 orig = myproposal[PROPOSAL_KEX_ALGS];
176 if (options.gss_trust_dns)
177 gss_host = (char *)get_canonical_hostname(1);
181 gss = ssh_gssapi_client_mechanisms(gss_host, options.gss_client_identity);
183 debug("Offering GSSAPI proposal: %s", gss);
184 xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
190 if (options.ciphers == (char *)-1) {
191 logit("No valid ciphers for protocol version 2 given, using defaults.");
192 options.ciphers = NULL;
194 if (options.ciphers != NULL) {
195 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
196 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
198 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
199 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
200 myproposal[PROPOSAL_ENC_ALGS_STOC] =
201 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
202 if (options.compression) {
203 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
204 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
206 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
207 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
209 if (options.macs != NULL) {
210 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
211 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
213 if (options.hostkeyalgorithms != NULL)
214 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
215 options.hostkeyalgorithms;
217 /* Prefer algorithms that we already have keys for */
218 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
219 order_hostkeyalgs(host, hostaddr, port);
221 if (options.kex_algorithms != NULL)
222 myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
225 /* If we've got GSSAPI algorithms, then we also support the
226 * 'null' hostkey, as a last resort */
227 if (options.gss_keyex && gss) {
228 orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
229 xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
235 if (options.rekey_limit)
236 packet_set_rekey_limit((u_int32_t)options.rekey_limit);
238 /* start key exchange */
239 kex = kex_setup(myproposal);
240 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
241 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
242 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
243 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
244 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
246 if (options.gss_keyex) {
247 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
248 kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_client;
249 kex->kex[KEX_GSS_GEX_SHA1] = kexgss_client;
252 kex->client_version_string=client_version_string;
253 kex->server_version_string=server_version_string;
254 kex->verify_host_key=&verify_host_key_callback;
257 if (options.gss_keyex) {
258 kex->gss_deleg_creds = options.gss_deleg_creds;
259 kex->gss_trust_dns = options.gss_trust_dns;
260 kex->gss_client = options.gss_client_identity;
261 if (options.gss_server_identity) {
262 kex->gss_host = options.gss_server_identity;
264 kex->gss_host = gss_host;
271 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
273 if (options.use_roaming && !kex->roaming) {
274 debug("Roaming not allowed by server");
275 options.use_roaming = 0;
278 session_id2 = kex->session_id;
279 session_id2_len = kex->session_id_len;
282 /* send 1st encrypted/maced/compressed message */
283 packet_start(SSH2_MSG_IGNORE);
284 packet_put_cstring("markus");
294 typedef struct Authctxt Authctxt;
295 typedef struct Authmethod Authmethod;
296 typedef struct identity Identity;
297 typedef struct idlist Idlist;
300 TAILQ_ENTRY(identity) next;
301 AuthenticationConnection *ac; /* set if agent supports key */
302 Key *key; /* public/private key */
303 char *filename; /* comment for agent-only keys */
305 int isprivate; /* key points to the private key */
307 TAILQ_HEAD(idlist, identity);
310 const char *server_user;
311 const char *local_user;
315 sig_atomic_t success;
319 AuthenticationConnection *agent;
321 Sensitive *sensitive;
322 /* kbd-interactive */
328 char *name; /* string to compare against server's list */
329 int (*userauth)(Authctxt *authctxt);
330 void (*cleanup)(Authctxt *authctxt);
331 int *enabled; /* flag in option struct that enables method */
332 int *batch_flag; /* flag in option struct that disables method */
335 void input_userauth_success(int, u_int32_t, void *);
336 void input_userauth_success_unexpected(int, u_int32_t, void *);
337 void input_userauth_failure(int, u_int32_t, void *);
338 void input_userauth_banner(int, u_int32_t, void *);
339 void input_userauth_error(int, u_int32_t, void *);
340 void input_userauth_info_req(int, u_int32_t, void *);
341 void input_userauth_pk_ok(int, u_int32_t, void *);
342 void input_userauth_passwd_changereq(int, u_int32_t, void *);
343 void input_userauth_jpake_server_step1(int, u_int32_t, void *);
344 void input_userauth_jpake_server_step2(int, u_int32_t, void *);
345 void input_userauth_jpake_server_confirm(int, u_int32_t, void *);
347 int userauth_none(Authctxt *);
348 int userauth_pubkey(Authctxt *);
349 int userauth_passwd(Authctxt *);
350 int userauth_kbdint(Authctxt *);
351 int userauth_hostbased(Authctxt *);
352 int userauth_jpake(Authctxt *);
354 void userauth_jpake_cleanup(Authctxt *);
357 int userauth_gssapi(Authctxt *authctxt);
358 void input_gssapi_response(int type, u_int32_t, void *);
359 void input_gssapi_token(int type, u_int32_t, void *);
360 void input_gssapi_hash(int type, u_int32_t, void *);
361 void input_gssapi_error(int, u_int32_t, void *);
362 void input_gssapi_errtok(int, u_int32_t, void *);
363 int userauth_gsskeyex(Authctxt *authctxt);
366 void userauth(Authctxt *, char *);
368 static int sign_and_send_pubkey(Authctxt *, Identity *);
369 static void pubkey_prepare(Authctxt *);
370 static void pubkey_cleanup(Authctxt *);
371 static Key *load_identity_file(char *);
373 static Authmethod *authmethod_get(char *authlist);
374 static Authmethod *authmethod_lookup(const char *name);
375 static char *authmethods_get(void);
377 Authmethod authmethods[] = {
382 &options.gss_authentication,
387 &options.gss_authentication,
393 &options.hostbased_authentication,
398 &options.pubkey_authentication,
401 {"jpake-01@openssh.com",
403 userauth_jpake_cleanup,
404 &options.zero_knowledge_password_authentication,
405 &options.batch_mode},
407 {"keyboard-interactive",
410 &options.kbd_interactive_authentication,
411 &options.batch_mode},
415 &options.password_authentication,
416 &options.batch_mode},
422 {NULL, NULL, NULL, NULL, NULL}
426 ssh_userauth2(const char *local_user, const char *server_user, char *host,
427 Sensitive *sensitive)
432 if (options.challenge_response_authentication)
433 options.kbd_interactive_authentication = 1;
435 packet_start(SSH2_MSG_SERVICE_REQUEST);
436 packet_put_cstring("ssh-userauth");
438 debug("SSH2_MSG_SERVICE_REQUEST sent");
440 type = packet_read();
441 if (type != SSH2_MSG_SERVICE_ACCEPT)
442 fatal("Server denied authentication request: %d", type);
443 if (packet_remaining() > 0) {
444 char *reply = packet_get_string(NULL);
445 debug2("service_accept: %s", reply);
448 debug2("buggy server: service_accept w/o service");
451 debug("SSH2_MSG_SERVICE_ACCEPT received");
453 if (options.preferred_authentications == NULL)
454 options.preferred_authentications = authmethods_get();
456 /* setup authentication context */
457 memset(&authctxt, 0, sizeof(authctxt));
458 pubkey_prepare(&authctxt);
459 authctxt.server_user = server_user;
460 authctxt.local_user = local_user;
461 authctxt.host = host;
462 authctxt.service = "ssh-connection"; /* service name */
463 authctxt.success = 0;
464 authctxt.method = authmethod_lookup("none");
465 authctxt.authlist = NULL;
466 authctxt.methoddata = NULL;
467 authctxt.sensitive = sensitive;
468 authctxt.info_req_seen = 0;
469 if (authctxt.method == NULL)
470 fatal("ssh_userauth2: internal error: cannot send userauth none request");
472 /* initial userauth request */
473 userauth_none(&authctxt);
475 dispatch_init(&input_userauth_error);
476 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
477 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
478 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
479 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
481 pubkey_cleanup(&authctxt);
482 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
484 debug("Authentication succeeded (%s).", authctxt.method->name);
488 userauth(Authctxt *authctxt, char *authlist)
490 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
491 authctxt->method->cleanup(authctxt);
493 if (authctxt->methoddata) {
494 xfree(authctxt->methoddata);
495 authctxt->methoddata = NULL;
497 if (authlist == NULL) {
498 authlist = authctxt->authlist;
500 if (authctxt->authlist)
501 xfree(authctxt->authlist);
502 authctxt->authlist = authlist;
505 Authmethod *method = authmethod_get(authlist);
507 fatal("Permission denied (%s).", authlist);
508 authctxt->method = method;
510 /* reset the per method handler */
511 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
512 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
514 /* and try new method */
515 if (method->userauth(authctxt) != 0) {
516 debug2("we sent a %s packet, wait for reply", method->name);
519 debug2("we did not send a packet, disable method");
520 method->enabled = NULL;
527 input_userauth_error(int type, u_int32_t seq, void *ctxt)
529 fatal("input_userauth_error: bad message during authentication: "
535 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
537 char *msg, *raw, *lang;
540 debug3("input_userauth_banner");
541 raw = packet_get_string(&len);
542 lang = packet_get_string(NULL);
543 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
546 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
547 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
548 fprintf(stderr, "%s", msg);
557 input_userauth_success(int type, u_int32_t seq, void *ctxt)
559 Authctxt *authctxt = ctxt;
561 if (authctxt == NULL)
562 fatal("input_userauth_success: no authentication context");
563 if (authctxt->authlist) {
564 xfree(authctxt->authlist);
565 authctxt->authlist = NULL;
567 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
568 authctxt->method->cleanup(authctxt);
569 if (authctxt->methoddata) {
570 xfree(authctxt->methoddata);
571 authctxt->methoddata = NULL;
573 authctxt->success = 1; /* break out */
577 input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
579 Authctxt *authctxt = ctxt;
581 if (authctxt == NULL)
582 fatal("%s: no authentication context", __func__);
584 fatal("Unexpected authentication success during %s.",
585 authctxt->method->name);
590 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
592 Authctxt *authctxt = ctxt;
593 char *authlist = NULL;
596 if (authctxt == NULL)
597 fatal("input_userauth_failure: no authentication context");
599 authlist = packet_get_string(NULL);
600 partial = packet_get_char();
604 logit("Authenticated with partial success.");
605 debug("Authentications that can continue: %s", authlist);
607 userauth(authctxt, authlist);
612 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
614 Authctxt *authctxt = ctxt;
618 int pktype, sent = 0;
623 if (authctxt == NULL)
624 fatal("input_userauth_pk_ok: no authentication context");
625 if (datafellows & SSH_BUG_PKOK) {
626 /* this is similar to SSH_BUG_PKAUTH */
627 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
628 pkblob = packet_get_string(&blen);
630 buffer_append(&b, pkblob, blen);
631 pkalg = buffer_get_string(&b, &alen);
634 pkalg = packet_get_string(&alen);
635 pkblob = packet_get_string(&blen);
639 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
641 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
642 debug("unknown pkalg %s", pkalg);
645 if ((key = key_from_blob(pkblob, blen)) == NULL) {
646 debug("no key from blob. pkalg %s", pkalg);
649 if (key->type != pktype) {
650 error("input_userauth_pk_ok: type mismatch "
651 "for decoded key (received %d, expected %d)",
655 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
656 debug2("input_userauth_pk_ok: fp %s", fp);
660 * search keys in the reverse order, because last candidate has been
661 * moved to the end of the queue. this also avoids confusion by
664 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
665 if (key_equal(key, id->key)) {
666 sent = sign_and_send_pubkey(authctxt, id);
676 /* try another method if we did not send a packet */
678 userauth(authctxt, NULL);
683 userauth_gssapi(Authctxt *authctxt)
685 Gssctxt *gssctxt = NULL;
686 static gss_OID_set gss_supported = NULL;
687 static u_int mech = 0;
690 const char *gss_host;
692 if (options.gss_server_identity)
693 gss_host = options.gss_server_identity;
694 else if (options.gss_trust_dns)
695 gss_host = get_canonical_hostname(1);
697 gss_host = authctxt->host;
699 /* Try one GSSAPI method at a time, rather than sending them all at
702 if (gss_supported == NULL)
703 if (GSS_ERROR(gss_indicate_mechs(&min, &gss_supported))) {
704 gss_supported = NULL;
708 /* Check to see if the mechanism is usable before we offer it */
709 while (mech < gss_supported->count && !ok) {
710 /* My DER encoding requires length<128 */
711 if (gss_supported->elements[mech].length < 128 &&
712 ssh_gssapi_check_mechanism(&gssctxt,
713 &gss_supported->elements[mech], gss_host,
714 options.gss_client_identity)) {
715 ok = 1; /* Mechanism works */
724 authctxt->methoddata=(void *)gssctxt;
726 packet_start(SSH2_MSG_USERAUTH_REQUEST);
727 packet_put_cstring(authctxt->server_user);
728 packet_put_cstring(authctxt->service);
729 packet_put_cstring(authctxt->method->name);
733 packet_put_int((gss_supported->elements[mech].length) + 2);
734 packet_put_char(SSH_GSS_OIDTYPE);
735 packet_put_char(gss_supported->elements[mech].length);
736 packet_put_raw(gss_supported->elements[mech].elements,
737 gss_supported->elements[mech].length);
741 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
742 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
743 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
744 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
746 mech++; /* Move along to next candidate */
752 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
754 Authctxt *authctxt = ctxt;
755 Gssctxt *gssctxt = authctxt->methoddata;
756 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
757 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
758 gss_buffer_desc gssbuf;
759 OM_uint32 status, ms, flags;
762 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
763 recv_tok, &send_tok, &flags);
765 if (send_tok.length > 0) {
766 if (GSS_ERROR(status))
767 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
769 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
771 packet_put_string(send_tok.value, send_tok.length);
773 gss_release_buffer(&ms, &send_tok);
776 if (status == GSS_S_COMPLETE) {
777 /* send either complete or MIC, depending on mechanism */
778 if (!(flags & GSS_C_INTEG_FLAG)) {
779 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
782 ssh_gssapi_buildmic(&b, authctxt->server_user,
783 authctxt->service, "gssapi-with-mic");
785 gssbuf.value = buffer_ptr(&b);
786 gssbuf.length = buffer_len(&b);
788 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
790 if (!GSS_ERROR(status)) {
791 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
792 packet_put_string(mic.value, mic.length);
798 gss_release_buffer(&ms, &mic);
807 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
809 Authctxt *authctxt = ctxt;
814 if (authctxt == NULL)
815 fatal("input_gssapi_response: no authentication context");
816 gssctxt = authctxt->methoddata;
819 oidv = packet_get_string(&oidlen);
822 oidv[0] != SSH_GSS_OIDTYPE ||
823 oidv[1] != oidlen - 2) {
825 debug("Badly encoded mechanism OID received");
826 userauth(authctxt, NULL);
830 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
831 fatal("Server returned different OID than expected");
837 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
838 /* Start again with next method on list */
839 debug("Trying to start again");
840 userauth(authctxt, NULL);
847 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
849 Authctxt *authctxt = ctxt;
850 gss_buffer_desc recv_tok;
854 if (authctxt == NULL)
855 fatal("input_gssapi_response: no authentication context");
857 recv_tok.value = packet_get_string(&slen);
858 recv_tok.length = slen; /* safe typecast */
862 status = process_gssapi_token(ctxt, &recv_tok);
864 xfree(recv_tok.value);
866 if (GSS_ERROR(status)) {
867 /* Start again with the next method in the list */
868 userauth(authctxt, NULL);
875 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
877 Authctxt *authctxt = ctxt;
879 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
880 gss_buffer_desc recv_tok;
881 OM_uint32 status, ms;
884 if (authctxt == NULL)
885 fatal("input_gssapi_response: no authentication context");
886 gssctxt = authctxt->methoddata;
888 recv_tok.value = packet_get_string(&len);
889 recv_tok.length = len;
893 /* Stick it into GSSAPI and see what it says */
894 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
895 &recv_tok, &send_tok, NULL);
897 xfree(recv_tok.value);
898 gss_release_buffer(&ms, &send_tok);
900 /* Server will be returning a failed packet after this one */
905 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
911 maj=packet_get_int();
912 min=packet_get_int();
913 msg=packet_get_string(NULL);
914 lang=packet_get_string(NULL);
918 debug("Server GSSAPI Error:\n%s", msg);
924 userauth_gsskeyex(Authctxt *authctxt)
927 gss_buffer_desc gssbuf;
928 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
931 static int attempt = 0;
935 if (gss_kex_context == NULL) {
936 debug("No valid Key exchange context");
940 ssh_gssapi_buildmic(&b, authctxt->server_user, authctxt->service,
943 gssbuf.value = buffer_ptr(&b);
944 gssbuf.length = buffer_len(&b);
946 if (GSS_ERROR(ssh_gssapi_sign(gss_kex_context, &gssbuf, &mic))) {
951 packet_start(SSH2_MSG_USERAUTH_REQUEST);
952 packet_put_cstring(authctxt->server_user);
953 packet_put_cstring(authctxt->service);
954 packet_put_cstring(authctxt->method->name);
955 packet_put_string(mic.value, mic.length);
959 gss_release_buffer(&ms, &mic);
967 userauth_none(Authctxt *authctxt)
969 /* initial userauth request */
970 packet_start(SSH2_MSG_USERAUTH_REQUEST);
971 packet_put_cstring(authctxt->server_user);
972 packet_put_cstring(authctxt->service);
973 packet_put_cstring(authctxt->method->name);
979 userauth_passwd(Authctxt *authctxt)
981 static int attempt = 0;
984 const char *host = options.host_key_alias ? options.host_key_alias :
987 if (attempt++ >= options.number_of_password_prompts)
991 error("Permission denied, please try again.");
993 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
994 authctxt->server_user, host);
995 password = read_passphrase(prompt, 0);
996 packet_start(SSH2_MSG_USERAUTH_REQUEST);
997 packet_put_cstring(authctxt->server_user);
998 packet_put_cstring(authctxt->service);
999 packet_put_cstring(authctxt->method->name);
1001 packet_put_cstring(password);
1002 memset(password, 0, strlen(password));
1004 packet_add_padding(64);
1007 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1008 &input_userauth_passwd_changereq);
1014 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
1018 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
1020 Authctxt *authctxt = ctxt;
1021 char *info, *lang, *password = NULL, *retype = NULL;
1023 const char *host = options.host_key_alias ? options.host_key_alias :
1026 debug2("input_userauth_passwd_changereq");
1028 if (authctxt == NULL)
1029 fatal("input_userauth_passwd_changereq: "
1030 "no authentication context");
1032 info = packet_get_string(NULL);
1033 lang = packet_get_string(NULL);
1034 if (strlen(info) > 0)
1038 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1039 packet_put_cstring(authctxt->server_user);
1040 packet_put_cstring(authctxt->service);
1041 packet_put_cstring(authctxt->method->name);
1042 packet_put_char(1); /* additional info */
1043 snprintf(prompt, sizeof(prompt),
1044 "Enter %.30s@%.128s's old password: ",
1045 authctxt->server_user, host);
1046 password = read_passphrase(prompt, 0);
1047 packet_put_cstring(password);
1048 memset(password, 0, strlen(password));
1051 while (password == NULL) {
1052 snprintf(prompt, sizeof(prompt),
1053 "Enter %.30s@%.128s's new password: ",
1054 authctxt->server_user, host);
1055 password = read_passphrase(prompt, RP_ALLOW_EOF);
1056 if (password == NULL) {
1060 snprintf(prompt, sizeof(prompt),
1061 "Retype %.30s@%.128s's new password: ",
1062 authctxt->server_user, host);
1063 retype = read_passphrase(prompt, 0);
1064 if (strcmp(password, retype) != 0) {
1065 memset(password, 0, strlen(password));
1067 logit("Mismatch; try again, EOF to quit.");
1070 memset(retype, 0, strlen(retype));
1073 packet_put_cstring(password);
1074 memset(password, 0, strlen(password));
1076 packet_add_padding(64);
1079 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1080 &input_userauth_passwd_changereq);
1085 pw_encrypt(const char *password, const char *crypt_scheme, const char *salt)
1087 /* OpenBSD crypt(3) handles all of these */
1088 if (strcmp(crypt_scheme, "crypt") == 0 ||
1089 strcmp(crypt_scheme, "bcrypt") == 0 ||
1090 strcmp(crypt_scheme, "md5crypt") == 0 ||
1091 strcmp(crypt_scheme, "crypt-extended") == 0)
1092 return xstrdup(crypt(password, salt));
1093 error("%s: unsupported password encryption scheme \"%.100s\"",
1094 __func__, crypt_scheme);
1099 jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme,
1102 char prompt[256], *password, *crypted;
1107 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ",
1108 authctxt->server_user, authctxt->host);
1109 password = read_passphrase(prompt, 0);
1111 if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) {
1112 logit("Disabling %s authentication", authctxt->method->name);
1113 authctxt->method->enabled = NULL;
1114 /* Continue with an empty password to fail gracefully */
1115 crypted = xstrdup("");
1119 debug3("%s: salt = %s", __func__, salt);
1120 debug3("%s: scheme = %s", __func__, crypt_scheme);
1121 debug3("%s: crypted = %s", __func__, crypted);
1124 if (hash_buffer(crypted, strlen(crypted), EVP_sha256(),
1125 &secret, &secret_len) != 0)
1126 fatal("%s: hash_buffer", __func__);
1128 bzero(password, strlen(password));
1129 bzero(crypted, strlen(crypted));
1133 if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL)
1134 fatal("%s: BN_bin2bn (secret)", __func__);
1135 bzero(secret, secret_len);
1143 input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt)
1145 Authctxt *authctxt = ctxt;
1146 struct jpake_ctx *pctx = authctxt->methoddata;
1147 u_char *x3_proof, *x4_proof, *x2_s_proof;
1148 u_int x3_proof_len, x4_proof_len, x2_s_proof_len;
1149 char *crypt_scheme, *salt;
1151 /* Disable this message */
1152 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL);
1154 if ((pctx->g_x3 = BN_new()) == NULL ||
1155 (pctx->g_x4 = BN_new()) == NULL)
1156 fatal("%s: BN_new", __func__);
1158 /* Fetch step 1 values */
1159 crypt_scheme = packet_get_string(NULL);
1160 salt = packet_get_string(NULL);
1161 pctx->server_id = packet_get_string(&pctx->server_id_len);
1162 packet_get_bignum2(pctx->g_x3);
1163 packet_get_bignum2(pctx->g_x4);
1164 x3_proof = packet_get_string(&x3_proof_len);
1165 x4_proof = packet_get_string(&x4_proof_len);
1168 JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__));
1170 /* Obtain password and derive secret */
1171 pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt);
1172 bzero(crypt_scheme, strlen(crypt_scheme));
1173 bzero(salt, strlen(salt));
1174 xfree(crypt_scheme);
1176 JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__));
1178 /* Calculate step 2 values */
1179 jpake_step2(pctx->grp, pctx->s, pctx->g_x1,
1180 pctx->g_x3, pctx->g_x4, pctx->x2,
1181 pctx->server_id, pctx->server_id_len,
1182 pctx->client_id, pctx->client_id_len,
1183 x3_proof, x3_proof_len,
1184 x4_proof, x4_proof_len,
1186 &x2_s_proof, &x2_s_proof_len);
1188 bzero(x3_proof, x3_proof_len);
1189 bzero(x4_proof, x4_proof_len);
1193 JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__));
1195 /* Send values for step 2 */
1196 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2);
1197 packet_put_bignum2(pctx->a);
1198 packet_put_string(x2_s_proof, x2_s_proof_len);
1201 bzero(x2_s_proof, x2_s_proof_len);
1204 /* Expect step 2 packet from peer */
1205 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2,
1206 input_userauth_jpake_server_step2);
1211 input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt)
1213 Authctxt *authctxt = ctxt;
1214 struct jpake_ctx *pctx = authctxt->methoddata;
1216 u_int x4_s_proof_len;
1218 /* Disable this message */
1219 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL);
1221 if ((pctx->b = BN_new()) == NULL)
1222 fatal("%s: BN_new", __func__);
1224 /* Fetch step 2 values */
1225 packet_get_bignum2(pctx->b);
1226 x4_s_proof = packet_get_string(&x4_s_proof_len);
1229 JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__));
1231 /* Derive shared key and calculate confirmation hash */
1232 jpake_key_confirm(pctx->grp, pctx->s, pctx->b,
1233 pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4,
1234 pctx->client_id, pctx->client_id_len,
1235 pctx->server_id, pctx->server_id_len,
1236 session_id2, session_id2_len,
1237 x4_s_proof, x4_s_proof_len,
1239 &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len);
1241 bzero(x4_s_proof, x4_s_proof_len);
1244 JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__));
1246 /* Send key confirmation proof */
1247 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM);
1248 packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
1251 /* Expect confirmation from peer */
1252 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM,
1253 input_userauth_jpake_server_confirm);
1258 input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt)
1260 Authctxt *authctxt = ctxt;
1261 struct jpake_ctx *pctx = authctxt->methoddata;
1263 /* Disable this message */
1264 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL);
1266 pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len);
1269 JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__));
1271 /* Verify expected confirmation hash */
1272 if (jpake_check_confirm(pctx->k,
1273 pctx->server_id, pctx->server_id_len,
1274 session_id2, session_id2_len,
1275 pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1)
1276 debug("%s: %s success", __func__, authctxt->method->name);
1278 debug("%s: confirmation mismatch", __func__);
1279 /* XXX stash this so if auth succeeds then we can warn/kill */
1282 userauth_jpake_cleanup(authctxt);
1287 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1288 u_char *data, u_int datalen)
1293 /* the agent supports this key */
1295 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1298 * we have already loaded the private key or
1299 * the private key is stored in external hardware
1301 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1302 return (key_sign(id->key, sigp, lenp, data, datalen));
1303 /* load the private key from the file */
1304 if ((prv = load_identity_file(id->filename)) == NULL)
1306 ret = key_sign(prv, sigp, lenp, data, datalen);
1312 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1315 u_char *blob, *signature;
1316 u_int bloblen, slen;
1322 fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
1323 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
1326 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1327 /* we cannot handle this key */
1328 debug3("sign_and_send_pubkey: cannot handle key");
1331 /* data to be signed */
1333 if (datafellows & SSH_OLD_SESSIONID) {
1334 buffer_append(&b, session_id2, session_id2_len);
1335 skip = session_id2_len;
1337 buffer_put_string(&b, session_id2, session_id2_len);
1338 skip = buffer_len(&b);
1340 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1341 buffer_put_cstring(&b, authctxt->server_user);
1342 buffer_put_cstring(&b,
1343 datafellows & SSH_BUG_PKSERVICE ?
1346 if (datafellows & SSH_BUG_PKAUTH) {
1347 buffer_put_char(&b, have_sig);
1349 buffer_put_cstring(&b, authctxt->method->name);
1350 buffer_put_char(&b, have_sig);
1351 buffer_put_cstring(&b, key_ssh_name(id->key));
1353 buffer_put_string(&b, blob, bloblen);
1355 /* generate signature */
1356 ret = identity_sign(id, &signature, &slen,
1357 buffer_ptr(&b), buffer_len(&b));
1366 if (datafellows & SSH_BUG_PKSERVICE) {
1368 buffer_append(&b, session_id2, session_id2_len);
1369 skip = session_id2_len;
1370 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1371 buffer_put_cstring(&b, authctxt->server_user);
1372 buffer_put_cstring(&b, authctxt->service);
1373 buffer_put_cstring(&b, authctxt->method->name);
1374 buffer_put_char(&b, have_sig);
1375 if (!(datafellows & SSH_BUG_PKAUTH))
1376 buffer_put_cstring(&b, key_ssh_name(id->key));
1377 buffer_put_string(&b, blob, bloblen);
1381 /* append signature */
1382 buffer_put_string(&b, signature, slen);
1385 /* skip session id and packet type */
1386 if (buffer_len(&b) < skip + 1)
1387 fatal("userauth_pubkey: internal error");
1388 buffer_consume(&b, skip + 1);
1390 /* put remaining data from buffer into packet */
1391 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1392 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1400 send_pubkey_test(Authctxt *authctxt, Identity *id)
1403 u_int bloblen, have_sig = 0;
1405 debug3("send_pubkey_test");
1407 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1408 /* we cannot handle this key */
1409 debug3("send_pubkey_test: cannot handle key");
1412 /* register callback for USERAUTH_PK_OK message */
1413 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1415 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1416 packet_put_cstring(authctxt->server_user);
1417 packet_put_cstring(authctxt->service);
1418 packet_put_cstring(authctxt->method->name);
1419 packet_put_char(have_sig);
1420 if (!(datafellows & SSH_BUG_PKAUTH))
1421 packet_put_cstring(key_ssh_name(id->key));
1422 packet_put_string(blob, bloblen);
1429 load_identity_file(char *filename)
1432 char prompt[300], *passphrase;
1433 int perm_ok = 0, quit, i;
1436 if (stat(filename, &st) < 0) {
1437 debug3("no such identity: %s", filename);
1440 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1443 if (private == NULL) {
1444 if (options.batch_mode)
1446 snprintf(prompt, sizeof prompt,
1447 "Enter passphrase for key '%.100s': ", filename);
1448 for (i = 0; i < options.number_of_password_prompts; i++) {
1449 passphrase = read_passphrase(prompt, 0);
1450 if (strcmp(passphrase, "") != 0) {
1451 private = key_load_private_type(KEY_UNSPEC,
1452 filename, passphrase, NULL, NULL);
1455 debug2("no passphrase given, try next key");
1458 memset(passphrase, 0, strlen(passphrase));
1460 if (private != NULL || quit)
1462 debug2("bad passphrase given, try again...");
1469 * try keys in the following order:
1470 * 1. agent keys that are found in the config file
1471 * 2. other agent keys
1472 * 3. keys that are only listed in the config file
1475 pubkey_prepare(Authctxt *authctxt)
1478 Idlist agent, files, *preferred;
1480 AuthenticationConnection *ac;
1484 TAILQ_INIT(&agent); /* keys from the agent */
1485 TAILQ_INIT(&files); /* keys from the config file */
1486 preferred = &authctxt->keys;
1487 TAILQ_INIT(preferred); /* preferred order of keys */
1489 /* list of keys stored in the filesystem */
1490 for (i = 0; i < options.num_identity_files; i++) {
1491 key = options.identity_keys[i];
1492 if (key && key->type == KEY_RSA1)
1494 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1496 options.identity_keys[i] = NULL;
1497 id = xcalloc(1, sizeof(*id));
1499 id->filename = xstrdup(options.identity_files[i]);
1500 TAILQ_INSERT_TAIL(&files, id, next);
1502 /* list of keys supported by the agent */
1503 if ((ac = ssh_get_authentication_connection())) {
1504 for (key = ssh_get_first_identity(ac, &comment, 2);
1506 key = ssh_get_next_identity(ac, &comment, 2)) {
1508 TAILQ_FOREACH(id, &files, next) {
1509 /* agent keys from the config file are preferred */
1510 if (key_equal(key, id->key)) {
1513 TAILQ_REMOVE(&files, id, next);
1514 TAILQ_INSERT_TAIL(preferred, id, next);
1520 if (!found && !options.identities_only) {
1521 id = xcalloc(1, sizeof(*id));
1523 id->filename = comment;
1525 TAILQ_INSERT_TAIL(&agent, id, next);
1528 /* append remaining agent keys */
1529 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1530 TAILQ_REMOVE(&agent, id, next);
1531 TAILQ_INSERT_TAIL(preferred, id, next);
1533 authctxt->agent = ac;
1535 /* append remaining keys from the config file */
1536 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1537 TAILQ_REMOVE(&files, id, next);
1538 TAILQ_INSERT_TAIL(preferred, id, next);
1540 TAILQ_FOREACH(id, preferred, next) {
1541 debug2("key: %s (%p)", id->filename, id->key);
1546 pubkey_cleanup(Authctxt *authctxt)
1550 if (authctxt->agent != NULL)
1551 ssh_close_authentication_connection(authctxt->agent);
1552 for (id = TAILQ_FIRST(&authctxt->keys); id;
1553 id = TAILQ_FIRST(&authctxt->keys)) {
1554 TAILQ_REMOVE(&authctxt->keys, id, next);
1558 xfree(id->filename);
1564 userauth_pubkey(Authctxt *authctxt)
1569 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1572 /* move key to the end of the queue */
1573 TAILQ_REMOVE(&authctxt->keys, id, next);
1574 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1576 * send a test message if we have the public key. for
1577 * encrypted keys we cannot do this and have to load the
1578 * private key instead
1580 if (id->key && id->key->type != KEY_RSA1) {
1581 debug("Offering %s public key: %s", key_type(id->key),
1583 sent = send_pubkey_test(authctxt, id);
1584 } else if (id->key == NULL) {
1585 debug("Trying private key: %s", id->filename);
1586 id->key = load_identity_file(id->filename);
1587 if (id->key != NULL) {
1589 sent = sign_and_send_pubkey(authctxt, id);
1601 * Send userauth request message specifying keyboard-interactive method.
1604 userauth_kbdint(Authctxt *authctxt)
1606 static int attempt = 0;
1608 if (attempt++ >= options.number_of_password_prompts)
1610 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1611 if (attempt > 1 && !authctxt->info_req_seen) {
1612 debug3("userauth_kbdint: disable: no info_req_seen");
1613 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1617 debug2("userauth_kbdint");
1618 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1619 packet_put_cstring(authctxt->server_user);
1620 packet_put_cstring(authctxt->service);
1621 packet_put_cstring(authctxt->method->name);
1622 packet_put_cstring(""); /* lang */
1623 packet_put_cstring(options.kbd_interactive_devices ?
1624 options.kbd_interactive_devices : "");
1627 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1632 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1635 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1637 Authctxt *authctxt = ctxt;
1638 char *name, *inst, *lang, *prompt, *response;
1639 u_int num_prompts, i;
1642 debug2("input_userauth_info_req");
1644 if (authctxt == NULL)
1645 fatal("input_userauth_info_req: no authentication context");
1647 authctxt->info_req_seen = 1;
1649 name = packet_get_string(NULL);
1650 inst = packet_get_string(NULL);
1651 lang = packet_get_string(NULL);
1652 if (strlen(name) > 0)
1654 if (strlen(inst) > 0)
1660 num_prompts = packet_get_int();
1662 * Begin to build info response packet based on prompts requested.
1663 * We commit to providing the correct number of responses, so if
1664 * further on we run into a problem that prevents this, we have to
1665 * be sure and clean this up and send a correct error response.
1667 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1668 packet_put_int(num_prompts);
1670 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1671 for (i = 0; i < num_prompts; i++) {
1672 prompt = packet_get_string(NULL);
1673 echo = packet_get_char();
1675 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1677 packet_put_cstring(response);
1678 memset(response, 0, strlen(response));
1682 packet_check_eom(); /* done with parsing incoming message. */
1684 packet_add_padding(64);
1689 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1690 u_char *data, u_int datalen)
1695 int to[2], from[2], status, version = 2;
1697 debug2("ssh_keysign called");
1699 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1700 error("ssh_keysign: not installed: %s", strerror(errno));
1703 if (fflush(stdout) != 0)
1704 error("ssh_keysign: fflush: %s", strerror(errno));
1706 error("ssh_keysign: pipe: %s", strerror(errno));
1709 if (pipe(from) < 0) {
1710 error("ssh_keysign: pipe: %s", strerror(errno));
1713 if ((pid = fork()) < 0) {
1714 error("ssh_keysign: fork: %s", strerror(errno));
1718 /* keep the socket on exec */
1719 fcntl(packet_get_connection_in(), F_SETFD, 0);
1720 permanently_drop_suid(getuid());
1722 if (dup2(from[1], STDOUT_FILENO) < 0)
1723 fatal("ssh_keysign: dup2: %s", strerror(errno));
1725 if (dup2(to[0], STDIN_FILENO) < 0)
1726 fatal("ssh_keysign: dup2: %s", strerror(errno));
1729 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1730 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1737 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1738 buffer_put_string(&b, data, datalen);
1739 if (ssh_msg_send(to[1], version, &b) == -1)
1740 fatal("ssh_keysign: couldn't send request");
1742 if (ssh_msg_recv(from[0], &b) < 0) {
1743 error("ssh_keysign: no reply");
1750 while (waitpid(pid, &status, 0) < 0)
1754 if (buffer_get_char(&b) != version) {
1755 error("ssh_keysign: bad version");
1759 *sigp = buffer_get_string(&b, lenp);
1766 userauth_hostbased(Authctxt *authctxt)
1768 Key *private = NULL;
1769 Sensitive *sensitive = authctxt->sensitive;
1771 u_char *signature, *blob;
1772 char *chost, *pkalg, *p;
1773 const char *service;
1775 int ok, i, found = 0;
1777 /* check for a useful key */
1778 for (i = 0; i < sensitive->nkeys; i++) {
1779 private = sensitive->keys[i];
1780 if (private && private->type != KEY_RSA1) {
1782 /* we take and free the key */
1783 sensitive->keys[i] = NULL;
1788 debug("No more client hostkeys for hostbased authentication.");
1791 if (key_to_blob(private, &blob, &blen) == 0) {
1795 /* figure out a name for the client host */
1796 p = get_local_name(packet_get_connection_in());
1798 error("userauth_hostbased: cannot get local ipaddr/name");
1803 xasprintf(&chost, "%s.", p);
1804 debug2("userauth_hostbased: chost %s", chost);
1807 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1809 pkalg = xstrdup(key_ssh_name(private));
1811 /* construct data */
1812 buffer_put_string(&b, session_id2, session_id2_len);
1813 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1814 buffer_put_cstring(&b, authctxt->server_user);
1815 buffer_put_cstring(&b, service);
1816 buffer_put_cstring(&b, authctxt->method->name);
1817 buffer_put_cstring(&b, pkalg);
1818 buffer_put_string(&b, blob, blen);
1819 buffer_put_cstring(&b, chost);
1820 buffer_put_cstring(&b, authctxt->local_user);
1824 if (sensitive->external_keysign)
1825 ok = ssh_keysign(private, &signature, &slen,
1826 buffer_ptr(&b), buffer_len(&b));
1828 ok = key_sign(private, &signature, &slen,
1829 buffer_ptr(&b), buffer_len(&b));
1833 error("key_sign failed");
1839 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1840 packet_put_cstring(authctxt->server_user);
1841 packet_put_cstring(authctxt->service);
1842 packet_put_cstring(authctxt->method->name);
1843 packet_put_cstring(pkalg);
1844 packet_put_string(blob, blen);
1845 packet_put_cstring(chost);
1846 packet_put_cstring(authctxt->local_user);
1847 packet_put_string(signature, slen);
1848 memset(signature, 's', slen);
1860 userauth_jpake(Authctxt *authctxt)
1862 struct jpake_ctx *pctx;
1863 u_char *x1_proof, *x2_proof;
1864 u_int x1_proof_len, x2_proof_len;
1865 static int attempt = 0; /* XXX share with userauth_password's? */
1867 if (attempt++ >= options.number_of_password_prompts)
1870 error("Permission denied, please try again.");
1872 if (authctxt->methoddata != NULL)
1873 fatal("%s: authctxt->methoddata already set (%p)",
1874 __func__, authctxt->methoddata);
1876 authctxt->methoddata = pctx = jpake_new();
1879 * Send request immediately, to get the protocol going while
1880 * we do the initial computations.
1882 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1883 packet_put_cstring(authctxt->server_user);
1884 packet_put_cstring(authctxt->service);
1885 packet_put_cstring(authctxt->method->name);
1887 packet_write_wait();
1889 jpake_step1(pctx->grp,
1890 &pctx->client_id, &pctx->client_id_len,
1891 &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2,
1892 &x1_proof, &x1_proof_len,
1893 &x2_proof, &x2_proof_len);
1895 JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__));
1897 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1);
1898 packet_put_string(pctx->client_id, pctx->client_id_len);
1899 packet_put_bignum2(pctx->g_x1);
1900 packet_put_bignum2(pctx->g_x2);
1901 packet_put_string(x1_proof, x1_proof_len);
1902 packet_put_string(x2_proof, x2_proof_len);
1905 bzero(x1_proof, x1_proof_len);
1906 bzero(x2_proof, x2_proof_len);
1910 /* Expect step 1 packet from peer */
1911 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1,
1912 input_userauth_jpake_server_step1);
1913 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS,
1914 &input_userauth_success_unexpected);
1920 userauth_jpake_cleanup(Authctxt *authctxt)
1922 debug3("%s: clean up", __func__);
1923 if (authctxt->methoddata != NULL) {
1924 jpake_free(authctxt->methoddata);
1925 authctxt->methoddata = NULL;
1927 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
1931 /* find auth method */
1934 * given auth method name, if configurable options permit this method fill
1935 * in auth_ident field and return true, otherwise return false.
1938 authmethod_is_enabled(Authmethod *method)
1942 /* return false if options indicate this method is disabled */
1943 if (method->enabled == NULL || *method->enabled == 0)
1945 /* return false if batch mode is enabled but method needs interactive mode */
1946 if (method->batch_flag != NULL && *method->batch_flag != 0)
1952 authmethod_lookup(const char *name)
1954 Authmethod *method = NULL;
1956 for (method = authmethods; method->name != NULL; method++)
1957 if (strcmp(name, method->name) == 0)
1959 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1963 /* XXX internal state */
1964 static Authmethod *current = NULL;
1965 static char *supported = NULL;
1966 static char *preferred = NULL;
1969 * Given the authentication method list sent by the server, return the
1970 * next method we should try. If the server initially sends a nil list,
1971 * use a built-in default list.
1974 authmethod_get(char *authlist)
1979 /* Use a suitable default if we're passed a nil list. */
1980 if (authlist == NULL || strlen(authlist) == 0)
1981 authlist = options.preferred_authentications;
1983 if (supported == NULL || strcmp(authlist, supported) != 0) {
1984 debug3("start over, passed a different list %s", authlist);
1985 if (supported != NULL)
1987 supported = xstrdup(authlist);
1988 preferred = options.preferred_authentications;
1989 debug3("preferred %s", preferred);
1991 } else if (current != NULL && authmethod_is_enabled(current))
1995 if ((name = match_list(preferred, supported, &next)) == NULL) {
1996 debug("No more authentication methods to try.");
2001 debug3("authmethod_lookup %s", name);
2002 debug3("remaining preferred: %s", preferred);
2003 if ((current = authmethod_lookup(name)) != NULL &&
2004 authmethod_is_enabled(current)) {
2005 debug3("authmethod_is_enabled %s", name);
2006 debug("Next authentication method: %s", name);
2013 authmethods_get(void)
2015 Authmethod *method = NULL;
2020 for (method = authmethods; method->name != NULL; method++) {
2021 if (authmethod_is_enabled(method)) {
2022 if (buffer_len(&b) > 0)
2023 buffer_append(&b, ",", 1);
2024 buffer_append(&b, method->name, strlen(method->name));
2027 buffer_append(&b, "\0", 1);
2028 list = xstrdup(buffer_ptr(&b));