1 /* $OpenBSD: sshconnect2.c,v 1.170 2008/11/04 08:22:13 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>
42 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
46 #include "openbsd-compat/sys-queue.h"
57 #include "myproposal.h"
58 #include "sshconnect.h"
69 #include "pathnames.h"
78 extern char *client_version_string;
79 extern char *server_version_string;
80 extern Options options;
82 /* tty_flag is set in ssh.c. use this in ssh_userauth2 */
83 /* if it is set then prevent the switch to the null cipher */
87 /* tty_flag is set in ssh.c. use this in ssh_userauth2 */
88 /* if it is set then prevent the switch to the null cipher */
96 u_char *session_id2 = NULL;
97 u_int session_id2_len = 0;
100 struct sockaddr *xxx_hostaddr;
105 verify_host_key_callback(Key *hostkey)
107 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
108 fatal("Host key verification failed.");
113 ssh_kex2(char *host, struct sockaddr *hostaddr)
118 char *orig = NULL, *gss = NULL;
119 char *gss_host = NULL;
123 xxx_hostaddr = hostaddr;
126 if (options.gss_keyex) {
127 /* Add the GSSAPI mechanisms currently supported on this
128 * client to the key exchange algorithm proposal */
129 orig = myproposal[PROPOSAL_KEX_ALGS];
131 if (options.gss_trust_dns)
132 gss_host = (char *)get_canonical_hostname(1);
136 gss = ssh_gssapi_client_mechanisms(gss_host);
138 debug("Offering GSSAPI proposal: %s", gss);
139 xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
145 if (options.ciphers == (char *)-1) {
146 logit("No valid ciphers for protocol version 2 given, using defaults.");
147 options.ciphers = NULL;
149 if (options.ciphers != NULL) {
150 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
151 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
153 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
154 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
155 myproposal[PROPOSAL_ENC_ALGS_STOC] =
156 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
157 if (options.compression) {
158 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
159 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
161 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
162 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
164 if (options.macs != NULL) {
165 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
166 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
168 if (options.hostkeyalgorithms != NULL)
169 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
170 options.hostkeyalgorithms;
173 /* If we've got GSSAPI algorithms, then we also support the
174 * 'null' hostkey, as a last resort */
175 if (options.gss_keyex && gss) {
176 orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
177 xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
182 if (options.rekey_limit)
183 packet_set_rekey_limit((u_int32_t)options.rekey_limit);
185 /* start key exchange */
186 kex = kex_setup(myproposal);
187 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
188 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
189 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
190 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
192 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
193 kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_client;
194 kex->kex[KEX_GSS_GEX_SHA1] = kexgss_client;
196 kex->client_version_string=client_version_string;
197 kex->server_version_string=server_version_string;
198 kex->verify_host_key=&verify_host_key_callback;
201 kex->gss_deleg_creds = options.gss_deleg_creds;
202 kex->gss_trust_dns = options.gss_trust_dns;
203 kex->gss_host = gss_host;
208 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
210 session_id2 = kex->session_id;
211 session_id2_len = kex->session_id_len;
214 /* send 1st encrypted/maced/compressed message */
215 packet_start(SSH2_MSG_IGNORE);
216 packet_put_cstring("markus");
226 typedef struct Authctxt Authctxt;
227 typedef struct Authmethod Authmethod;
228 typedef struct identity Identity;
229 typedef struct idlist Idlist;
232 TAILQ_ENTRY(identity) next;
233 AuthenticationConnection *ac; /* set if agent supports key */
234 Key *key; /* public/private key */
235 char *filename; /* comment for agent-only keys */
237 int isprivate; /* key points to the private key */
239 TAILQ_HEAD(idlist, identity);
242 const char *server_user;
243 const char *local_user;
251 AuthenticationConnection *agent;
253 Sensitive *sensitive;
254 /* kbd-interactive */
260 char *name; /* string to compare against server's list */
261 int (*userauth)(Authctxt *authctxt);
262 void (*cleanup)(Authctxt *authctxt);
263 int *enabled; /* flag in option struct that enables method */
264 int *batch_flag; /* flag in option struct that disables method */
267 void input_userauth_success(int, u_int32_t, void *);
268 void input_userauth_failure(int, u_int32_t, void *);
269 void input_userauth_banner(int, u_int32_t, void *);
270 void input_userauth_error(int, u_int32_t, void *);
271 void input_userauth_info_req(int, u_int32_t, void *);
272 void input_userauth_pk_ok(int, u_int32_t, void *);
273 void input_userauth_passwd_changereq(int, u_int32_t, void *);
274 void input_userauth_jpake_server_step1(int, u_int32_t, void *);
275 void input_userauth_jpake_server_step2(int, u_int32_t, void *);
276 void input_userauth_jpake_server_confirm(int, u_int32_t, void *);
278 int userauth_none(Authctxt *);
279 int userauth_pubkey(Authctxt *);
280 int userauth_passwd(Authctxt *);
281 int userauth_kbdint(Authctxt *);
282 int userauth_hostbased(Authctxt *);
283 int userauth_jpake(Authctxt *);
285 void userauth_jpake_cleanup(Authctxt *);
288 int userauth_external(Authctxt *authctxt);
289 int userauth_gssapi(Authctxt *authctxt);
290 int userauth_gssapi_with_mic(Authctxt *authctxt);
291 int userauth_gssapi_without_mic(Authctxt *authctxt);
292 void input_gssapi_response(int type, u_int32_t, void *);
293 void input_gssapi_token(int type, u_int32_t, void *);
294 void input_gssapi_hash(int type, u_int32_t, void *);
295 void input_gssapi_error(int, u_int32_t, void *);
296 void input_gssapi_errtok(int, u_int32_t, void *);
297 int userauth_gsskeyex(Authctxt *authctxt);
300 void userauth(Authctxt *, char *);
302 static int sign_and_send_pubkey(Authctxt *, Identity *);
303 static void pubkey_prepare(Authctxt *);
304 static void pubkey_cleanup(Authctxt *);
305 static Key *load_identity_file(char *);
307 static Authmethod *authmethod_get(char *authlist);
308 static Authmethod *authmethod_lookup(const char *name);
309 static char *authmethods_get(void);
311 Authmethod authmethods[] = {
315 &options.gss_authentication,
319 &options.gss_authentication,
324 &options.gss_authentication,
328 &options.gss_authentication,
334 &options.hostbased_authentication,
339 &options.pubkey_authentication,
342 {"jpake-01@openssh.com",
344 userauth_jpake_cleanup,
345 &options.zero_knowledge_password_authentication,
346 &options.batch_mode},
348 {"keyboard-interactive",
351 &options.kbd_interactive_authentication,
352 &options.batch_mode},
356 &options.password_authentication,
357 &options.batch_mode},
363 {NULL, NULL, NULL, NULL, NULL}
367 ssh_userauth2(const char *local_user, const char *server_user, char *host,
368 Sensitive *sensitive)
373 if (options.challenge_response_authentication)
374 options.kbd_interactive_authentication = 1;
376 packet_start(SSH2_MSG_SERVICE_REQUEST);
377 packet_put_cstring("ssh-userauth");
379 debug("SSH2_MSG_SERVICE_REQUEST sent");
381 type = packet_read();
382 if (type != SSH2_MSG_SERVICE_ACCEPT)
383 fatal("Server denied authentication request: %d", type);
384 if (packet_remaining() > 0) {
385 char *reply = packet_get_string(NULL);
386 debug2("service_accept: %s", reply);
389 debug2("buggy server: service_accept w/o service");
392 debug("SSH2_MSG_SERVICE_ACCEPT received");
394 if (options.preferred_authentications == NULL)
395 options.preferred_authentications = authmethods_get();
397 /* setup authentication context */
398 memset(&authctxt, 0, sizeof(authctxt));
399 pubkey_prepare(&authctxt);
400 authctxt.server_user = server_user;
401 authctxt.local_user = local_user;
402 authctxt.host = host;
403 authctxt.service = "ssh-connection"; /* service name */
404 authctxt.success = 0;
405 authctxt.method = authmethod_lookup("none");
406 authctxt.authlist = NULL;
407 authctxt.methoddata = NULL;
408 authctxt.sensitive = sensitive;
409 authctxt.info_req_seen = 0;
410 if (authctxt.method == NULL)
411 fatal("ssh_userauth2: internal error: cannot send userauth none request");
413 /* initial userauth request */
414 userauth_none(&authctxt);
416 dispatch_init(&input_userauth_error);
417 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
418 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
419 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
420 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
422 pubkey_cleanup(&authctxt);
423 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
425 /* if the user wants to use the none cipher do it */
426 /* post authentication and only if the right conditions are met */
427 /* both of the NONE commands must be true and there must be no */
429 if ((options.none_switch == 1) && (options.none_enabled == 1))
431 if (!tty_flag) /* no null on tty sessions */
433 debug("Requesting none rekeying...");
434 myproposal[PROPOSAL_ENC_ALGS_STOC] = "none";
435 myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none";
436 kex_prop2buf(&xxx_kex->my,myproposal);
437 packet_request_rekeying();
438 fprintf(stderr, "WARNING: ENABLED NONE CIPHER\n");
442 /* requested NONE cipher when in a tty */
443 debug("Cannot switch to NONE cipher with tty allocated");
444 fprintf(stderr, "NONE cipher switch disabled when a TTY is allocated\n");
447 debug("Authentication succeeded (%s).", authctxt.method->name);
451 userauth(Authctxt *authctxt, char *authlist)
453 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
454 authctxt->method->cleanup(authctxt);
456 if (authctxt->methoddata) {
457 xfree(authctxt->methoddata);
458 authctxt->methoddata = NULL;
460 if (authlist == NULL) {
461 authlist = authctxt->authlist;
463 if (authctxt->authlist)
464 xfree(authctxt->authlist);
465 authctxt->authlist = authlist;
468 Authmethod *method = authmethod_get(authlist);
470 fatal("Permission denied (%s).", authlist);
471 authctxt->method = method;
473 /* reset the per method handler */
474 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
475 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
477 /* and try new method */
478 if (method->userauth(authctxt) != 0) {
479 debug2("we sent a %s packet, wait for reply", method->name);
482 debug2("we did not send a packet, disable method");
483 method->enabled = NULL;
490 input_userauth_error(int type, u_int32_t seq, void *ctxt)
492 fatal("input_userauth_error: bad message during authentication: "
498 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
500 char *msg, *raw, *lang;
503 debug3("input_userauth_banner");
504 raw = packet_get_string(&len);
505 lang = packet_get_string(NULL);
506 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
509 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
510 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL);
511 fprintf(stderr, "%s", msg);
520 input_userauth_success(int type, u_int32_t seq, void *ctxt)
522 Authctxt *authctxt = ctxt;
523 if (authctxt == NULL)
524 fatal("input_userauth_success: no authentication context");
525 if (authctxt->authlist) {
526 xfree(authctxt->authlist);
527 authctxt->authlist = NULL;
529 if (authctxt->methoddata) {
530 xfree(authctxt->methoddata);
531 authctxt->methoddata = NULL;
533 authctxt->success = 1; /* break out */
538 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
540 Authctxt *authctxt = ctxt;
541 char *authlist = NULL;
544 if (authctxt == NULL)
545 fatal("input_userauth_failure: no authentication context");
547 authlist = packet_get_string(NULL);
548 partial = packet_get_char();
552 logit("Authenticated with partial success.");
553 debug("Authentications that can continue: %s", authlist);
555 userauth(authctxt, authlist);
560 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
562 Authctxt *authctxt = ctxt;
566 int pktype, sent = 0;
571 if (authctxt == NULL)
572 fatal("input_userauth_pk_ok: no authentication context");
573 if (datafellows & SSH_BUG_PKOK) {
574 /* this is similar to SSH_BUG_PKAUTH */
575 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
576 pkblob = packet_get_string(&blen);
578 buffer_append(&b, pkblob, blen);
579 pkalg = buffer_get_string(&b, &alen);
582 pkalg = packet_get_string(&alen);
583 pkblob = packet_get_string(&blen);
587 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
589 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
590 debug("unknown pkalg %s", pkalg);
593 if ((key = key_from_blob(pkblob, blen)) == NULL) {
594 debug("no key from blob. pkalg %s", pkalg);
597 if (key->type != pktype) {
598 error("input_userauth_pk_ok: type mismatch "
599 "for decoded key (received %d, expected %d)",
603 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
604 debug2("input_userauth_pk_ok: fp %s", fp);
608 * search keys in the reverse order, because last candidate has been
609 * moved to the end of the queue. this also avoids confusion by
612 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
613 if (key_equal(key, id->key)) {
614 sent = sign_and_send_pubkey(authctxt, id);
624 /* try another method if we did not send a packet */
626 userauth(authctxt, NULL);
631 userauth_gssapi(Authctxt *authctxt)
633 Gssctxt *gssctxt = NULL;
634 static gss_OID_set gss_supported = NULL;
635 static u_int mech = 0;
638 char *gss_host = NULL;
640 if (!options.gss_authentication) {
641 verbose("GSSAPI authentication disabled.");
645 if (options.gss_trust_dns)
646 gss_host = (char *)get_canonical_hostname(1);
648 gss_host = (char *)authctxt->host;
650 /* Try one GSSAPI method at a time, rather than sending them all at
653 if (gss_supported == NULL)
654 gss_indicate_mechs(&min, &gss_supported);
656 /* Check to see if the mechanism is usable before we offer it */
657 while (mech < gss_supported->count && !ok) {
658 /* My DER encoding requires length<128 */
659 if (gss_supported->elements[mech].length < 128 &&
660 ssh_gssapi_check_mechanism(&gssctxt,
661 &gss_supported->elements[mech], gss_host)) {
662 ok = 1; /* Mechanism works */
671 authctxt->methoddata=(void *)gssctxt;
673 packet_start(SSH2_MSG_USERAUTH_REQUEST);
674 packet_put_cstring(authctxt->server_user);
675 packet_put_cstring(authctxt->service);
676 packet_put_cstring(authctxt->method->name);
680 packet_put_int((gss_supported->elements[mech].length) + 2);
681 packet_put_char(SSH_GSS_OIDTYPE);
682 packet_put_char(gss_supported->elements[mech].length);
683 packet_put_raw(gss_supported->elements[mech].elements,
684 gss_supported->elements[mech].length);
688 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
689 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
690 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
691 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
693 mech++; /* Move along to next candidate */
699 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
701 Authctxt *authctxt = ctxt;
702 Gssctxt *gssctxt = authctxt->methoddata;
703 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
704 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
705 gss_buffer_desc gssbuf;
706 OM_uint32 status, ms, flags;
709 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
710 recv_tok, &send_tok, &flags);
712 if (send_tok.length > 0) {
713 if (GSS_ERROR(status))
714 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
716 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
718 packet_put_string(send_tok.value, send_tok.length);
720 gss_release_buffer(&ms, &send_tok);
723 if (status == GSS_S_COMPLETE) {
724 /* send either complete or MIC, depending on mechanism */
725 if (strcmp(authctxt->method->name,"gssapi")==0 ||
726 (!(flags & GSS_C_INTEG_FLAG))) {
727 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
730 ssh_gssapi_buildmic(&b, authctxt->server_user,
731 authctxt->service, "gssapi-with-mic");
733 gssbuf.value = buffer_ptr(&b);
734 gssbuf.length = buffer_len(&b);
736 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
738 if (!GSS_ERROR(status)) {
739 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
740 packet_put_string(mic.value, mic.length);
746 gss_release_buffer(&ms, &mic);
755 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
757 Authctxt *authctxt = ctxt;
762 if (authctxt == NULL)
763 fatal("input_gssapi_response: no authentication context");
764 gssctxt = authctxt->methoddata;
767 oidv = packet_get_string(&oidlen);
770 oidv[0] != SSH_GSS_OIDTYPE ||
771 oidv[1] != oidlen - 2) {
773 debug("Badly encoded mechanism OID received");
774 userauth(authctxt, NULL);
778 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
779 fatal("Server returned different OID than expected");
785 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
786 /* Start again with next method on list */
787 debug("Trying to start again");
788 userauth(authctxt, NULL);
795 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
797 Authctxt *authctxt = ctxt;
798 gss_buffer_desc recv_tok;
802 if (authctxt == NULL)
803 fatal("input_gssapi_response: no authentication context");
805 recv_tok.value = packet_get_string(&slen);
806 recv_tok.length = slen; /* safe typecast */
810 status = process_gssapi_token(ctxt, &recv_tok);
812 xfree(recv_tok.value);
814 if (GSS_ERROR(status)) {
815 /* Start again with the next method in the list */
816 userauth(authctxt, NULL);
823 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
825 Authctxt *authctxt = ctxt;
827 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
828 gss_buffer_desc recv_tok;
829 OM_uint32 status, ms;
832 if (authctxt == NULL)
833 fatal("input_gssapi_response: no authentication context");
834 gssctxt = authctxt->methoddata;
836 recv_tok.value = packet_get_string(&len);
837 recv_tok.length = len;
841 /* Stick it into GSSAPI and see what it says */
842 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
843 &recv_tok, &send_tok, NULL);
845 xfree(recv_tok.value);
846 gss_release_buffer(&ms, &send_tok);
848 /* Server will be returning a failed packet after this one */
853 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
859 maj=packet_get_int();
860 min=packet_get_int();
861 msg=packet_get_string(NULL);
862 lang=packet_get_string(NULL);
866 debug("Server GSSAPI Error:\n%s", msg);
873 const gss_OID_desc * const gss_mech_globus_gssapi_openssl;
874 #define is_gsi_oid(oid) \
875 (oid->length == gss_mech_globus_gssapi_openssl->length && \
876 (memcmp(oid->elements, gss_mech_globus_gssapi_openssl->elements, \
881 userauth_external(Authctxt *authctxt)
883 static int attempt = 0;
888 /* The client MUST NOT try this method if initial key exchange
889 was not performed using a GSSAPI-based key exchange
891 if (gss_kex_context == NULL) {
892 debug2("gsskex not performed, skipping external-keyx");
896 debug2("userauth_external");
897 packet_start(SSH2_MSG_USERAUTH_REQUEST);
899 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
900 packet_put_cstring("");
903 packet_put_cstring(authctxt->server_user);
907 packet_put_cstring(authctxt->service);
908 packet_put_cstring(authctxt->method->name);
914 userauth_gsskeyex(Authctxt *authctxt)
917 gss_buffer_desc gssbuf;
918 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
921 static int attempt = 0;
925 if (gss_kex_context == NULL) {
926 debug("No valid Key exchange context");
931 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
932 ssh_gssapi_buildmic(&b, "", authctxt->service, "gssapi-keyex");
935 ssh_gssapi_buildmic(&b, authctxt->server_user, authctxt->service,
941 gssbuf.value = buffer_ptr(&b);
942 gssbuf.length = buffer_len(&b);
944 if (GSS_ERROR(ssh_gssapi_sign(gss_kex_context, &gssbuf, &mic))) {
949 packet_start(SSH2_MSG_USERAUTH_REQUEST);
951 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
952 packet_put_cstring("");
955 packet_put_cstring(authctxt->server_user);
959 packet_put_cstring(authctxt->service);
960 packet_put_cstring(authctxt->method->name);
961 packet_put_string(mic.value, mic.length);
965 gss_release_buffer(&ms, &mic);
973 userauth_none(Authctxt *authctxt)
975 /* initial userauth request */
976 packet_start(SSH2_MSG_USERAUTH_REQUEST);
977 packet_put_cstring(authctxt->server_user);
978 packet_put_cstring(authctxt->service);
979 packet_put_cstring(authctxt->method->name);
985 userauth_passwd(Authctxt *authctxt)
987 static int attempt = 0;
991 if (attempt++ >= options.number_of_password_prompts)
995 error("Permission denied, please try again.");
997 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
998 authctxt->server_user, authctxt->host);
999 password = read_passphrase(prompt, 0);
1000 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1001 packet_put_cstring(authctxt->server_user);
1002 packet_put_cstring(authctxt->service);
1003 packet_put_cstring(authctxt->method->name);
1005 packet_put_cstring(password);
1006 memset(password, 0, strlen(password));
1008 packet_add_padding(64);
1011 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1012 &input_userauth_passwd_changereq);
1018 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
1022 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
1024 Authctxt *authctxt = ctxt;
1025 char *info, *lang, *password = NULL, *retype = NULL;
1028 debug2("input_userauth_passwd_changereq");
1030 if (authctxt == NULL)
1031 fatal("input_userauth_passwd_changereq: "
1032 "no authentication context");
1034 info = packet_get_string(NULL);
1035 lang = packet_get_string(NULL);
1036 if (strlen(info) > 0)
1040 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1041 packet_put_cstring(authctxt->server_user);
1042 packet_put_cstring(authctxt->service);
1043 packet_put_cstring(authctxt->method->name);
1044 packet_put_char(1); /* additional info */
1045 snprintf(prompt, sizeof(prompt),
1046 "Enter %.30s@%.128s's old password: ",
1047 authctxt->server_user, authctxt->host);
1048 password = read_passphrase(prompt, 0);
1049 packet_put_cstring(password);
1050 memset(password, 0, strlen(password));
1053 while (password == NULL) {
1054 snprintf(prompt, sizeof(prompt),
1055 "Enter %.30s@%.128s's new password: ",
1056 authctxt->server_user, authctxt->host);
1057 password = read_passphrase(prompt, RP_ALLOW_EOF);
1058 if (password == NULL) {
1062 snprintf(prompt, sizeof(prompt),
1063 "Retype %.30s@%.128s's new password: ",
1064 authctxt->server_user, authctxt->host);
1065 retype = read_passphrase(prompt, 0);
1066 if (strcmp(password, retype) != 0) {
1067 memset(password, 0, strlen(password));
1069 logit("Mismatch; try again, EOF to quit.");
1072 memset(retype, 0, strlen(retype));
1075 packet_put_cstring(password);
1076 memset(password, 0, strlen(password));
1078 packet_add_padding(64);
1081 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1082 &input_userauth_passwd_changereq);
1087 pw_encrypt(const char *password, const char *crypt_scheme, const char *salt)
1089 /* OpenBSD crypt(3) handles all of these */
1090 if (strcmp(crypt_scheme, "crypt") == 0 ||
1091 strcmp(crypt_scheme, "bcrypt") == 0 ||
1092 strcmp(crypt_scheme, "md5crypt") == 0 ||
1093 strcmp(crypt_scheme, "crypt-extended") == 0)
1094 return xstrdup(crypt(password, salt));
1095 error("%s: unsupported password encryption scheme \"%.100s\"",
1096 __func__, crypt_scheme);
1101 jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme,
1104 char prompt[256], *password, *crypted;
1109 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ",
1110 authctxt->server_user, authctxt->host);
1111 password = read_passphrase(prompt, 0);
1113 if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) {
1114 logit("Disabling %s authentication", authctxt->method->name);
1115 authctxt->method->enabled = NULL;
1116 /* Continue with an empty password to fail gracefully */
1117 crypted = xstrdup("");
1121 debug3("%s: salt = %s", __func__, salt);
1122 debug3("%s: scheme = %s", __func__, crypt_scheme);
1123 debug3("%s: crypted = %s", __func__, crypted);
1126 if (hash_buffer(crypted, strlen(crypted), EVP_sha256(),
1127 &secret, &secret_len) != 0)
1128 fatal("%s: hash_buffer", __func__);
1130 bzero(password, strlen(password));
1131 bzero(crypted, strlen(crypted));
1135 if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL)
1136 fatal("%s: BN_bin2bn (secret)", __func__);
1137 bzero(secret, secret_len);
1145 input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt)
1147 Authctxt *authctxt = ctxt;
1148 struct jpake_ctx *pctx = authctxt->methoddata;
1149 u_char *x3_proof, *x4_proof, *x2_s_proof;
1150 u_int x3_proof_len, x4_proof_len, x2_s_proof_len;
1151 char *crypt_scheme, *salt;
1153 /* Disable this message */
1154 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL);
1156 if ((pctx->g_x3 = BN_new()) == NULL ||
1157 (pctx->g_x4 = BN_new()) == NULL)
1158 fatal("%s: BN_new", __func__);
1160 /* Fetch step 1 values */
1161 crypt_scheme = packet_get_string(NULL);
1162 salt = packet_get_string(NULL);
1163 pctx->server_id = packet_get_string(&pctx->server_id_len);
1164 packet_get_bignum2(pctx->g_x3);
1165 packet_get_bignum2(pctx->g_x4);
1166 x3_proof = packet_get_string(&x3_proof_len);
1167 x4_proof = packet_get_string(&x4_proof_len);
1170 JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__));
1172 /* Obtain password and derive secret */
1173 pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt);
1174 bzero(crypt_scheme, strlen(crypt_scheme));
1175 bzero(salt, strlen(salt));
1176 xfree(crypt_scheme);
1178 JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__));
1180 /* Calculate step 2 values */
1181 jpake_step2(pctx->grp, pctx->s, pctx->g_x1,
1182 pctx->g_x3, pctx->g_x4, pctx->x2,
1183 pctx->server_id, pctx->server_id_len,
1184 pctx->client_id, pctx->client_id_len,
1185 x3_proof, x3_proof_len,
1186 x4_proof, x4_proof_len,
1188 &x2_s_proof, &x2_s_proof_len);
1190 bzero(x3_proof, x3_proof_len);
1191 bzero(x4_proof, x4_proof_len);
1195 JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__));
1197 /* Send values for step 2 */
1198 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2);
1199 packet_put_bignum2(pctx->a);
1200 packet_put_string(x2_s_proof, x2_s_proof_len);
1203 bzero(x2_s_proof, x2_s_proof_len);
1206 /* Expect step 2 packet from peer */
1207 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2,
1208 input_userauth_jpake_server_step2);
1213 input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt)
1215 Authctxt *authctxt = ctxt;
1216 struct jpake_ctx *pctx = authctxt->methoddata;
1218 u_int x4_s_proof_len;
1220 /* Disable this message */
1221 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL);
1223 if ((pctx->b = BN_new()) == NULL)
1224 fatal("%s: BN_new", __func__);
1226 /* Fetch step 2 values */
1227 packet_get_bignum2(pctx->b);
1228 x4_s_proof = packet_get_string(&x4_s_proof_len);
1231 JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__));
1233 /* Derive shared key and calculate confirmation hash */
1234 jpake_key_confirm(pctx->grp, pctx->s, pctx->b,
1235 pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4,
1236 pctx->client_id, pctx->client_id_len,
1237 pctx->server_id, pctx->server_id_len,
1238 session_id2, session_id2_len,
1239 x4_s_proof, x4_s_proof_len,
1241 &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len);
1243 bzero(x4_s_proof, x4_s_proof_len);
1246 JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__));
1248 /* Send key confirmation proof */
1249 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM);
1250 packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
1253 /* Expect confirmation from peer */
1254 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM,
1255 input_userauth_jpake_server_confirm);
1260 input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt)
1262 Authctxt *authctxt = ctxt;
1263 struct jpake_ctx *pctx = authctxt->methoddata;
1265 /* Disable this message */
1266 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL);
1268 pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len);
1271 JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__));
1273 /* Verify expected confirmation hash */
1274 if (jpake_check_confirm(pctx->k,
1275 pctx->server_id, pctx->server_id_len,
1276 session_id2, session_id2_len,
1277 pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1)
1278 debug("%s: %s success", __func__, authctxt->method->name);
1280 debug("%s: confirmation mismatch", __func__);
1281 /* XXX stash this so if auth succeeds then we can warn/kill */
1284 userauth_jpake_cleanup(authctxt);
1289 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1290 u_char *data, u_int datalen)
1295 /* the agent supports this key */
1297 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1300 * we have already loaded the private key or
1301 * the private key is stored in external hardware
1303 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1304 return (key_sign(id->key, sigp, lenp, data, datalen));
1305 /* load the private key from the file */
1306 if ((prv = load_identity_file(id->filename)) == NULL)
1308 ret = key_sign(prv, sigp, lenp, data, datalen);
1314 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1317 u_char *blob, *signature;
1318 u_int bloblen, slen;
1323 debug3("sign_and_send_pubkey");
1325 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1326 /* we cannot handle this key */
1327 debug3("sign_and_send_pubkey: cannot handle key");
1330 /* data to be signed */
1332 if (datafellows & SSH_OLD_SESSIONID) {
1333 buffer_append(&b, session_id2, session_id2_len);
1334 skip = session_id2_len;
1336 buffer_put_string(&b, session_id2, session_id2_len);
1337 skip = buffer_len(&b);
1339 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1340 buffer_put_cstring(&b, authctxt->server_user);
1341 buffer_put_cstring(&b,
1342 datafellows & SSH_BUG_PKSERVICE ?
1345 if (datafellows & SSH_BUG_PKAUTH) {
1346 buffer_put_char(&b, have_sig);
1348 buffer_put_cstring(&b, authctxt->method->name);
1349 buffer_put_char(&b, have_sig);
1350 buffer_put_cstring(&b, key_ssh_name(id->key));
1352 buffer_put_string(&b, blob, bloblen);
1354 /* generate signature */
1355 ret = identity_sign(id, &signature, &slen,
1356 buffer_ptr(&b), buffer_len(&b));
1365 if (datafellows & SSH_BUG_PKSERVICE) {
1367 buffer_append(&b, session_id2, session_id2_len);
1368 skip = session_id2_len;
1369 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1370 buffer_put_cstring(&b, authctxt->server_user);
1371 buffer_put_cstring(&b, authctxt->service);
1372 buffer_put_cstring(&b, authctxt->method->name);
1373 buffer_put_char(&b, have_sig);
1374 if (!(datafellows & SSH_BUG_PKAUTH))
1375 buffer_put_cstring(&b, key_ssh_name(id->key));
1376 buffer_put_string(&b, blob, bloblen);
1380 /* append signature */
1381 buffer_put_string(&b, signature, slen);
1384 /* skip session id and packet type */
1385 if (buffer_len(&b) < skip + 1)
1386 fatal("userauth_pubkey: internal error");
1387 buffer_consume(&b, skip + 1);
1389 /* put remaining data from buffer into packet */
1390 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1391 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1399 send_pubkey_test(Authctxt *authctxt, Identity *id)
1402 u_int bloblen, have_sig = 0;
1404 debug3("send_pubkey_test");
1406 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1407 /* we cannot handle this key */
1408 debug3("send_pubkey_test: cannot handle key");
1411 /* register callback for USERAUTH_PK_OK message */
1412 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1414 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1415 packet_put_cstring(authctxt->server_user);
1416 packet_put_cstring(authctxt->service);
1417 packet_put_cstring(authctxt->method->name);
1418 packet_put_char(have_sig);
1419 if (!(datafellows & SSH_BUG_PKAUTH))
1420 packet_put_cstring(key_ssh_name(id->key));
1421 packet_put_string(blob, bloblen);
1428 load_identity_file(char *filename)
1431 char prompt[300], *passphrase;
1432 int perm_ok, quit, i;
1435 if (stat(filename, &st) < 0) {
1436 debug3("no such identity: %s", filename);
1439 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1442 if (private == NULL) {
1443 if (options.batch_mode)
1445 snprintf(prompt, sizeof prompt,
1446 "Enter passphrase for key '%.100s': ", filename);
1447 for (i = 0; i < options.number_of_password_prompts; i++) {
1448 passphrase = read_passphrase(prompt, 0);
1449 if (strcmp(passphrase, "") != 0) {
1450 private = key_load_private_type(KEY_UNSPEC,
1451 filename, passphrase, NULL, NULL);
1454 debug2("no passphrase given, try next key");
1457 memset(passphrase, 0, strlen(passphrase));
1459 if (private != NULL || quit)
1461 debug2("bad passphrase given, try again...");
1468 * try keys in the following order:
1469 * 1. agent keys that are found in the config file
1470 * 2. other agent keys
1471 * 3. keys that are only listed in the config file
1474 pubkey_prepare(Authctxt *authctxt)
1477 Idlist agent, files, *preferred;
1479 AuthenticationConnection *ac;
1483 TAILQ_INIT(&agent); /* keys from the agent */
1484 TAILQ_INIT(&files); /* keys from the config file */
1485 preferred = &authctxt->keys;
1486 TAILQ_INIT(preferred); /* preferred order of keys */
1488 /* list of keys stored in the filesystem */
1489 for (i = 0; i < options.num_identity_files; i++) {
1490 key = options.identity_keys[i];
1491 if (key && key->type == KEY_RSA1)
1493 options.identity_keys[i] = NULL;
1494 id = xcalloc(1, sizeof(*id));
1496 id->filename = xstrdup(options.identity_files[i]);
1497 TAILQ_INSERT_TAIL(&files, id, next);
1499 /* list of keys supported by the agent */
1500 if ((ac = ssh_get_authentication_connection())) {
1501 for (key = ssh_get_first_identity(ac, &comment, 2);
1503 key = ssh_get_next_identity(ac, &comment, 2)) {
1505 TAILQ_FOREACH(id, &files, next) {
1506 /* agent keys from the config file are preferred */
1507 if (key_equal(key, id->key)) {
1510 TAILQ_REMOVE(&files, id, next);
1511 TAILQ_INSERT_TAIL(preferred, id, next);
1517 if (!found && !options.identities_only) {
1518 id = xcalloc(1, sizeof(*id));
1520 id->filename = comment;
1522 TAILQ_INSERT_TAIL(&agent, id, next);
1525 /* append remaining agent keys */
1526 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1527 TAILQ_REMOVE(&agent, id, next);
1528 TAILQ_INSERT_TAIL(preferred, id, next);
1530 authctxt->agent = ac;
1532 /* append remaining keys from the config file */
1533 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1534 TAILQ_REMOVE(&files, id, next);
1535 TAILQ_INSERT_TAIL(preferred, id, next);
1537 TAILQ_FOREACH(id, preferred, next) {
1538 debug2("key: %s (%p)", id->filename, id->key);
1543 pubkey_cleanup(Authctxt *authctxt)
1547 if (authctxt->agent != NULL)
1548 ssh_close_authentication_connection(authctxt->agent);
1549 for (id = TAILQ_FIRST(&authctxt->keys); id;
1550 id = TAILQ_FIRST(&authctxt->keys)) {
1551 TAILQ_REMOVE(&authctxt->keys, id, next);
1555 xfree(id->filename);
1561 userauth_pubkey(Authctxt *authctxt)
1566 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1569 /* move key to the end of the queue */
1570 TAILQ_REMOVE(&authctxt->keys, id, next);
1571 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1573 * send a test message if we have the public key. for
1574 * encrypted keys we cannot do this and have to load the
1575 * private key instead
1577 if (id->key && id->key->type != KEY_RSA1) {
1578 debug("Offering public key: %s", id->filename);
1579 sent = send_pubkey_test(authctxt, id);
1580 } else if (id->key == NULL) {
1581 debug("Trying private key: %s", id->filename);
1582 id->key = load_identity_file(id->filename);
1583 if (id->key != NULL) {
1585 sent = sign_and_send_pubkey(authctxt, id);
1597 * Send userauth request message specifying keyboard-interactive method.
1600 userauth_kbdint(Authctxt *authctxt)
1602 static int attempt = 0;
1604 if (attempt++ >= options.number_of_password_prompts)
1606 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1607 if (attempt > 1 && !authctxt->info_req_seen) {
1608 debug3("userauth_kbdint: disable: no info_req_seen");
1609 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1613 debug2("userauth_kbdint");
1614 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1615 packet_put_cstring(authctxt->server_user);
1616 packet_put_cstring(authctxt->service);
1617 packet_put_cstring(authctxt->method->name);
1618 packet_put_cstring(""); /* lang */
1619 packet_put_cstring(options.kbd_interactive_devices ?
1620 options.kbd_interactive_devices : "");
1623 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1628 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1631 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1633 Authctxt *authctxt = ctxt;
1634 char *name, *inst, *lang, *prompt, *response;
1635 u_int num_prompts, i;
1638 debug2("input_userauth_info_req");
1640 if (authctxt == NULL)
1641 fatal("input_userauth_info_req: no authentication context");
1643 authctxt->info_req_seen = 1;
1645 name = packet_get_string(NULL);
1646 inst = packet_get_string(NULL);
1647 lang = packet_get_string(NULL);
1648 if (strlen(name) > 0)
1650 if (strlen(inst) > 0)
1656 num_prompts = packet_get_int();
1658 * Begin to build info response packet based on prompts requested.
1659 * We commit to providing the correct number of responses, so if
1660 * further on we run into a problem that prevents this, we have to
1661 * be sure and clean this up and send a correct error response.
1663 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1664 packet_put_int(num_prompts);
1666 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1667 for (i = 0; i < num_prompts; i++) {
1668 prompt = packet_get_string(NULL);
1669 echo = packet_get_char();
1671 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1673 packet_put_cstring(response);
1674 memset(response, 0, strlen(response));
1678 packet_check_eom(); /* done with parsing incoming message. */
1680 packet_add_padding(64);
1685 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1686 u_char *data, u_int datalen)
1691 int to[2], from[2], status, version = 2;
1693 debug2("ssh_keysign called");
1695 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1696 error("ssh_keysign: no installed: %s", strerror(errno));
1699 if (fflush(stdout) != 0)
1700 error("ssh_keysign: fflush: %s", strerror(errno));
1702 error("ssh_keysign: pipe: %s", strerror(errno));
1705 if (pipe(from) < 0) {
1706 error("ssh_keysign: pipe: %s", strerror(errno));
1709 if ((pid = fork()) < 0) {
1710 error("ssh_keysign: fork: %s", strerror(errno));
1714 permanently_drop_suid(getuid());
1716 if (dup2(from[1], STDOUT_FILENO) < 0)
1717 fatal("ssh_keysign: dup2: %s", strerror(errno));
1719 if (dup2(to[0], STDIN_FILENO) < 0)
1720 fatal("ssh_keysign: dup2: %s", strerror(errno));
1723 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1724 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1731 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1732 buffer_put_string(&b, data, datalen);
1733 if (ssh_msg_send(to[1], version, &b) == -1)
1734 fatal("ssh_keysign: couldn't send request");
1736 if (ssh_msg_recv(from[0], &b) < 0) {
1737 error("ssh_keysign: no reply");
1744 while (waitpid(pid, &status, 0) < 0)
1748 if (buffer_get_char(&b) != version) {
1749 error("ssh_keysign: bad version");
1753 *sigp = buffer_get_string(&b, lenp);
1760 userauth_hostbased(Authctxt *authctxt)
1762 Key *private = NULL;
1763 Sensitive *sensitive = authctxt->sensitive;
1765 u_char *signature, *blob;
1766 char *chost, *pkalg, *p, myname[NI_MAXHOST];
1767 const char *service;
1769 int ok, i, len, found = 0;
1771 /* check for a useful key */
1772 for (i = 0; i < sensitive->nkeys; i++) {
1773 private = sensitive->keys[i];
1774 if (private && private->type != KEY_RSA1) {
1776 /* we take and free the key */
1777 sensitive->keys[i] = NULL;
1782 debug("No more client hostkeys for hostbased authentication.");
1785 if (key_to_blob(private, &blob, &blen) == 0) {
1789 /* figure out a name for the client host */
1791 if (packet_connection_is_on_socket())
1792 p = get_local_name(packet_get_connection_in());
1794 if (gethostname(myname, sizeof(myname)) == -1) {
1795 verbose("userauth_hostbased: gethostname: %s",
1798 p = xstrdup(myname);
1801 error("userauth_hostbased: cannot get local ipaddr/name");
1806 len = strlen(p) + 2;
1807 xasprintf(&chost, "%s.", p);
1808 debug2("userauth_hostbased: chost %s", chost);
1811 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1813 pkalg = xstrdup(key_ssh_name(private));
1815 /* construct data */
1816 buffer_put_string(&b, session_id2, session_id2_len);
1817 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1818 buffer_put_cstring(&b, authctxt->server_user);
1819 buffer_put_cstring(&b, service);
1820 buffer_put_cstring(&b, authctxt->method->name);
1821 buffer_put_cstring(&b, pkalg);
1822 buffer_put_string(&b, blob, blen);
1823 buffer_put_cstring(&b, chost);
1824 buffer_put_cstring(&b, authctxt->local_user);
1828 if (sensitive->external_keysign)
1829 ok = ssh_keysign(private, &signature, &slen,
1830 buffer_ptr(&b), buffer_len(&b));
1832 ok = key_sign(private, &signature, &slen,
1833 buffer_ptr(&b), buffer_len(&b));
1837 error("key_sign failed");
1843 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1844 packet_put_cstring(authctxt->server_user);
1845 packet_put_cstring(authctxt->service);
1846 packet_put_cstring(authctxt->method->name);
1847 packet_put_cstring(pkalg);
1848 packet_put_string(blob, blen);
1849 packet_put_cstring(chost);
1850 packet_put_cstring(authctxt->local_user);
1851 packet_put_string(signature, slen);
1852 memset(signature, 's', slen);
1864 userauth_jpake(Authctxt *authctxt)
1866 struct jpake_ctx *pctx;
1867 u_char *x1_proof, *x2_proof;
1868 u_int x1_proof_len, x2_proof_len;
1869 static int attempt = 0; /* XXX share with userauth_password's? */
1871 if (attempt++ >= options.number_of_password_prompts)
1874 error("Permission denied, please try again.");
1876 if (authctxt->methoddata != NULL)
1877 fatal("%s: authctxt->methoddata already set (%p)",
1878 __func__, authctxt->methoddata);
1880 authctxt->methoddata = pctx = jpake_new();
1883 * Send request immediately, to get the protocol going while
1884 * we do the initial computations.
1886 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1887 packet_put_cstring(authctxt->server_user);
1888 packet_put_cstring(authctxt->service);
1889 packet_put_cstring(authctxt->method->name);
1891 packet_write_wait();
1893 jpake_step1(pctx->grp,
1894 &pctx->client_id, &pctx->client_id_len,
1895 &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2,
1896 &x1_proof, &x1_proof_len,
1897 &x2_proof, &x2_proof_len);
1899 JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__));
1901 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1);
1902 packet_put_string(pctx->client_id, pctx->client_id_len);
1903 packet_put_bignum2(pctx->g_x1);
1904 packet_put_bignum2(pctx->g_x2);
1905 packet_put_string(x1_proof, x1_proof_len);
1906 packet_put_string(x2_proof, x2_proof_len);
1909 bzero(x1_proof, x1_proof_len);
1910 bzero(x2_proof, x2_proof_len);
1914 /* Expect step 1 packet from peer */
1915 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1,
1916 input_userauth_jpake_server_step1);
1922 userauth_jpake_cleanup(Authctxt *authctxt)
1924 debug3("%s: clean up", __func__);
1925 if (authctxt->methoddata != NULL) {
1926 jpake_free(authctxt->methoddata);
1927 authctxt->methoddata = NULL;
1932 /* find auth method */
1935 * given auth method name, if configurable options permit this method fill
1936 * in auth_ident field and return true, otherwise return false.
1939 authmethod_is_enabled(Authmethod *method)
1943 /* return false if options indicate this method is disabled */
1944 if (method->enabled == NULL || *method->enabled == 0)
1946 /* return false if batch mode is enabled but method needs interactive mode */
1947 if (method->batch_flag != NULL && *method->batch_flag != 0)
1953 authmethod_lookup(const char *name)
1955 Authmethod *method = NULL;
1957 for (method = authmethods; method->name != NULL; method++)
1958 if (strcmp(name, method->name) == 0)
1960 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1964 /* XXX internal state */
1965 static Authmethod *current = NULL;
1966 static char *supported = NULL;
1967 static char *preferred = NULL;
1970 * Given the authentication method list sent by the server, return the
1971 * next method we should try. If the server initially sends a nil list,
1972 * use a built-in default list.
1975 authmethod_get(char *authlist)
1980 /* Use a suitable default if we're passed a nil list. */
1981 if (authlist == NULL || strlen(authlist) == 0)
1982 authlist = options.preferred_authentications;
1984 if (supported == NULL || strcmp(authlist, supported) != 0) {
1985 debug3("start over, passed a different list %s", authlist);
1986 if (supported != NULL)
1988 supported = xstrdup(authlist);
1989 preferred = options.preferred_authentications;
1990 debug3("preferred %s", preferred);
1992 } else if (current != NULL && authmethod_is_enabled(current))
1996 if ((name = match_list(preferred, supported, &next)) == NULL) {
1997 debug("No more authentication methods to try.");
2002 debug3("authmethod_lookup %s", name);
2003 debug3("remaining preferred: %s", preferred);
2004 if ((current = authmethod_lookup(name)) != NULL &&
2005 authmethod_is_enabled(current)) {
2006 debug3("authmethod_is_enabled %s", name);
2007 debug("Next authentication method: %s", name);
2014 authmethods_get(void)
2016 Authmethod *method = NULL;
2021 for (method = authmethods; method->name != NULL; method++) {
2022 if (authmethod_is_enabled(method)) {
2023 if (buffer_len(&b) > 0)
2024 buffer_append(&b, ",", 1);
2025 buffer_append(&b, method->name, strlen(method->name));
2028 buffer_append(&b, "\0", 1);
2029 list = xstrdup(buffer_ptr(&b));