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[] = {
316 &options.gss_authentication,
321 &options.gss_authentication,
326 &options.gss_authentication,
331 &options.gss_authentication,
337 &options.hostbased_authentication,
342 &options.pubkey_authentication,
345 {"jpake-01@openssh.com",
347 userauth_jpake_cleanup,
348 &options.zero_knowledge_password_authentication,
349 &options.batch_mode},
351 {"keyboard-interactive",
354 &options.kbd_interactive_authentication,
355 &options.batch_mode},
359 &options.password_authentication,
360 &options.batch_mode},
366 {NULL, NULL, NULL, NULL, NULL}
370 ssh_userauth2(const char *local_user, const char *server_user, char *host,
371 Sensitive *sensitive)
376 if (options.challenge_response_authentication)
377 options.kbd_interactive_authentication = 1;
379 packet_start(SSH2_MSG_SERVICE_REQUEST);
380 packet_put_cstring("ssh-userauth");
382 debug("SSH2_MSG_SERVICE_REQUEST sent");
384 type = packet_read();
385 if (type != SSH2_MSG_SERVICE_ACCEPT)
386 fatal("Server denied authentication request: %d", type);
387 if (packet_remaining() > 0) {
388 char *reply = packet_get_string(NULL);
389 debug2("service_accept: %s", reply);
392 debug2("buggy server: service_accept w/o service");
395 debug("SSH2_MSG_SERVICE_ACCEPT received");
397 if (options.preferred_authentications == NULL)
398 options.preferred_authentications = authmethods_get();
400 /* setup authentication context */
401 memset(&authctxt, 0, sizeof(authctxt));
402 pubkey_prepare(&authctxt);
403 authctxt.server_user = server_user;
404 authctxt.local_user = local_user;
405 authctxt.host = host;
406 authctxt.service = "ssh-connection"; /* service name */
407 authctxt.success = 0;
408 authctxt.method = authmethod_lookup("none");
409 authctxt.authlist = NULL;
410 authctxt.methoddata = NULL;
411 authctxt.sensitive = sensitive;
412 authctxt.info_req_seen = 0;
413 if (authctxt.method == NULL)
414 fatal("ssh_userauth2: internal error: cannot send userauth none request");
416 /* initial userauth request */
417 userauth_none(&authctxt);
419 dispatch_init(&input_userauth_error);
420 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
421 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
422 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
423 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
425 pubkey_cleanup(&authctxt);
426 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
428 /* if the user wants to use the none cipher do it */
429 /* post authentication and only if the right conditions are met */
430 /* both of the NONE commands must be true and there must be no */
432 if ((options.none_switch == 1) && (options.none_enabled == 1))
434 if (!tty_flag) /* no null on tty sessions */
436 debug("Requesting none rekeying...");
437 myproposal[PROPOSAL_ENC_ALGS_STOC] = "none";
438 myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none";
439 kex_prop2buf(&xxx_kex->my,myproposal);
440 packet_request_rekeying();
441 fprintf(stderr, "WARNING: ENABLED NONE CIPHER\n");
445 /* requested NONE cipher when in a tty */
446 debug("Cannot switch to NONE cipher with tty allocated");
447 fprintf(stderr, "NONE cipher switch disabled when a TTY is allocated\n");
450 debug("Authentication succeeded (%s).", authctxt.method->name);
454 userauth(Authctxt *authctxt, char *authlist)
456 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
457 authctxt->method->cleanup(authctxt);
459 if (authctxt->methoddata) {
460 xfree(authctxt->methoddata);
461 authctxt->methoddata = NULL;
463 if (authlist == NULL) {
464 authlist = authctxt->authlist;
466 if (authctxt->authlist)
467 xfree(authctxt->authlist);
468 authctxt->authlist = authlist;
471 Authmethod *method = authmethod_get(authlist);
473 fatal("Permission denied (%s).", authlist);
474 authctxt->method = method;
476 /* reset the per method handler */
477 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
478 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
480 /* and try new method */
481 if (method->userauth(authctxt) != 0) {
482 debug2("we sent a %s packet, wait for reply", method->name);
485 debug2("we did not send a packet, disable method");
486 method->enabled = NULL;
493 input_userauth_error(int type, u_int32_t seq, void *ctxt)
495 fatal("input_userauth_error: bad message during authentication: "
501 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
503 char *msg, *raw, *lang;
506 debug3("input_userauth_banner");
507 raw = packet_get_string(&len);
508 lang = packet_get_string(NULL);
509 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
512 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
513 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL);
514 fprintf(stderr, "%s", msg);
523 input_userauth_success(int type, u_int32_t seq, void *ctxt)
525 Authctxt *authctxt = ctxt;
526 if (authctxt == NULL)
527 fatal("input_userauth_success: no authentication context");
528 if (authctxt->authlist) {
529 xfree(authctxt->authlist);
530 authctxt->authlist = NULL;
532 if (authctxt->methoddata) {
533 xfree(authctxt->methoddata);
534 authctxt->methoddata = NULL;
536 authctxt->success = 1; /* break out */
541 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
543 Authctxt *authctxt = ctxt;
544 char *authlist = NULL;
547 if (authctxt == NULL)
548 fatal("input_userauth_failure: no authentication context");
550 authlist = packet_get_string(NULL);
551 partial = packet_get_char();
555 logit("Authenticated with partial success.");
556 debug("Authentications that can continue: %s", authlist);
558 userauth(authctxt, authlist);
563 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
565 Authctxt *authctxt = ctxt;
569 int pktype, sent = 0;
574 if (authctxt == NULL)
575 fatal("input_userauth_pk_ok: no authentication context");
576 if (datafellows & SSH_BUG_PKOK) {
577 /* this is similar to SSH_BUG_PKAUTH */
578 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
579 pkblob = packet_get_string(&blen);
581 buffer_append(&b, pkblob, blen);
582 pkalg = buffer_get_string(&b, &alen);
585 pkalg = packet_get_string(&alen);
586 pkblob = packet_get_string(&blen);
590 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
592 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
593 debug("unknown pkalg %s", pkalg);
596 if ((key = key_from_blob(pkblob, blen)) == NULL) {
597 debug("no key from blob. pkalg %s", pkalg);
600 if (key->type != pktype) {
601 error("input_userauth_pk_ok: type mismatch "
602 "for decoded key (received %d, expected %d)",
606 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
607 debug2("input_userauth_pk_ok: fp %s", fp);
611 * search keys in the reverse order, because last candidate has been
612 * moved to the end of the queue. this also avoids confusion by
615 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
616 if (key_equal(key, id->key)) {
617 sent = sign_and_send_pubkey(authctxt, id);
627 /* try another method if we did not send a packet */
629 userauth(authctxt, NULL);
634 userauth_gssapi(Authctxt *authctxt)
636 Gssctxt *gssctxt = NULL;
637 static gss_OID_set gss_supported = NULL;
638 static u_int mech = 0;
641 char *gss_host = NULL;
643 if (!options.gss_authentication) {
644 verbose("GSSAPI authentication disabled.");
648 if (options.gss_trust_dns)
649 gss_host = (char *)get_canonical_hostname(1);
651 gss_host = (char *)authctxt->host;
653 /* Try one GSSAPI method at a time, rather than sending them all at
656 if (gss_supported == NULL)
657 gss_indicate_mechs(&min, &gss_supported);
659 /* Check to see if the mechanism is usable before we offer it */
660 while (mech < gss_supported->count && !ok) {
661 /* My DER encoding requires length<128 */
662 if (gss_supported->elements[mech].length < 128 &&
663 ssh_gssapi_check_mechanism(&gssctxt,
664 &gss_supported->elements[mech], gss_host)) {
665 ok = 1; /* Mechanism works */
674 authctxt->methoddata=(void *)gssctxt;
676 packet_start(SSH2_MSG_USERAUTH_REQUEST);
677 packet_put_cstring(authctxt->server_user);
678 packet_put_cstring(authctxt->service);
679 packet_put_cstring(authctxt->method->name);
683 packet_put_int((gss_supported->elements[mech].length) + 2);
684 packet_put_char(SSH_GSS_OIDTYPE);
685 packet_put_char(gss_supported->elements[mech].length);
686 packet_put_raw(gss_supported->elements[mech].elements,
687 gss_supported->elements[mech].length);
691 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
692 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
693 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
694 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
696 mech++; /* Move along to next candidate */
702 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
704 Authctxt *authctxt = ctxt;
705 Gssctxt *gssctxt = authctxt->methoddata;
706 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
707 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
708 gss_buffer_desc gssbuf;
709 OM_uint32 status, ms, flags;
712 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
713 recv_tok, &send_tok, &flags);
715 if (send_tok.length > 0) {
716 if (GSS_ERROR(status))
717 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
719 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
721 packet_put_string(send_tok.value, send_tok.length);
723 gss_release_buffer(&ms, &send_tok);
726 if (status == GSS_S_COMPLETE) {
727 /* send either complete or MIC, depending on mechanism */
728 if (strcmp(authctxt->method->name,"gssapi")==0 ||
729 (!(flags & GSS_C_INTEG_FLAG))) {
730 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
733 ssh_gssapi_buildmic(&b, authctxt->server_user,
734 authctxt->service, "gssapi-with-mic");
736 gssbuf.value = buffer_ptr(&b);
737 gssbuf.length = buffer_len(&b);
739 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
741 if (!GSS_ERROR(status)) {
742 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
743 packet_put_string(mic.value, mic.length);
749 gss_release_buffer(&ms, &mic);
758 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
760 Authctxt *authctxt = ctxt;
765 if (authctxt == NULL)
766 fatal("input_gssapi_response: no authentication context");
767 gssctxt = authctxt->methoddata;
770 oidv = packet_get_string(&oidlen);
773 oidv[0] != SSH_GSS_OIDTYPE ||
774 oidv[1] != oidlen - 2) {
776 debug("Badly encoded mechanism OID received");
777 userauth(authctxt, NULL);
781 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
782 fatal("Server returned different OID than expected");
788 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
789 /* Start again with next method on list */
790 debug("Trying to start again");
791 userauth(authctxt, NULL);
798 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
800 Authctxt *authctxt = ctxt;
801 gss_buffer_desc recv_tok;
805 if (authctxt == NULL)
806 fatal("input_gssapi_response: no authentication context");
808 recv_tok.value = packet_get_string(&slen);
809 recv_tok.length = slen; /* safe typecast */
813 status = process_gssapi_token(ctxt, &recv_tok);
815 xfree(recv_tok.value);
817 if (GSS_ERROR(status)) {
818 /* Start again with the next method in the list */
819 userauth(authctxt, NULL);
826 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
828 Authctxt *authctxt = ctxt;
830 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
831 gss_buffer_desc recv_tok;
832 OM_uint32 status, ms;
835 if (authctxt == NULL)
836 fatal("input_gssapi_response: no authentication context");
837 gssctxt = authctxt->methoddata;
839 recv_tok.value = packet_get_string(&len);
840 recv_tok.length = len;
844 /* Stick it into GSSAPI and see what it says */
845 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
846 &recv_tok, &send_tok, NULL);
848 xfree(recv_tok.value);
849 gss_release_buffer(&ms, &send_tok);
851 /* Server will be returning a failed packet after this one */
856 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
862 maj=packet_get_int();
863 min=packet_get_int();
864 msg=packet_get_string(NULL);
865 lang=packet_get_string(NULL);
869 debug("Server GSSAPI Error:\n%s", msg);
876 const gss_OID_desc * const gss_mech_globus_gssapi_openssl;
877 #define is_gsi_oid(oid) \
878 (oid->length == gss_mech_globus_gssapi_openssl->length && \
879 (memcmp(oid->elements, gss_mech_globus_gssapi_openssl->elements, \
884 userauth_external(Authctxt *authctxt)
886 static int attempt = 0;
891 /* The client MUST NOT try this method if initial key exchange
892 was not performed using a GSSAPI-based key exchange
894 if (gss_kex_context == NULL) {
895 debug2("gsskex not performed, skipping external-keyx");
899 debug2("userauth_external");
900 packet_start(SSH2_MSG_USERAUTH_REQUEST);
902 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
903 packet_put_cstring("");
906 packet_put_cstring(authctxt->server_user);
910 packet_put_cstring(authctxt->service);
911 packet_put_cstring(authctxt->method->name);
917 userauth_gsskeyex(Authctxt *authctxt)
920 gss_buffer_desc gssbuf;
921 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
924 static int attempt = 0;
928 if (gss_kex_context == NULL) {
929 debug("No valid Key exchange context");
934 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
935 ssh_gssapi_buildmic(&b, "", authctxt->service, "gssapi-keyex");
938 ssh_gssapi_buildmic(&b, authctxt->server_user, authctxt->service,
944 gssbuf.value = buffer_ptr(&b);
945 gssbuf.length = buffer_len(&b);
947 if (GSS_ERROR(ssh_gssapi_sign(gss_kex_context, &gssbuf, &mic))) {
952 packet_start(SSH2_MSG_USERAUTH_REQUEST);
954 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
955 packet_put_cstring("");
958 packet_put_cstring(authctxt->server_user);
962 packet_put_cstring(authctxt->service);
963 packet_put_cstring(authctxt->method->name);
964 packet_put_string(mic.value, mic.length);
968 gss_release_buffer(&ms, &mic);
976 userauth_none(Authctxt *authctxt)
978 /* initial userauth request */
979 packet_start(SSH2_MSG_USERAUTH_REQUEST);
980 packet_put_cstring(authctxt->server_user);
981 packet_put_cstring(authctxt->service);
982 packet_put_cstring(authctxt->method->name);
988 userauth_passwd(Authctxt *authctxt)
990 static int attempt = 0;
994 if (attempt++ >= options.number_of_password_prompts)
998 error("Permission denied, please try again.");
1000 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
1001 authctxt->server_user, authctxt->host);
1002 password = read_passphrase(prompt, 0);
1003 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1004 packet_put_cstring(authctxt->server_user);
1005 packet_put_cstring(authctxt->service);
1006 packet_put_cstring(authctxt->method->name);
1008 packet_put_cstring(password);
1009 memset(password, 0, strlen(password));
1011 packet_add_padding(64);
1014 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1015 &input_userauth_passwd_changereq);
1021 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
1025 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
1027 Authctxt *authctxt = ctxt;
1028 char *info, *lang, *password = NULL, *retype = NULL;
1031 debug2("input_userauth_passwd_changereq");
1033 if (authctxt == NULL)
1034 fatal("input_userauth_passwd_changereq: "
1035 "no authentication context");
1037 info = packet_get_string(NULL);
1038 lang = packet_get_string(NULL);
1039 if (strlen(info) > 0)
1043 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1044 packet_put_cstring(authctxt->server_user);
1045 packet_put_cstring(authctxt->service);
1046 packet_put_cstring(authctxt->method->name);
1047 packet_put_char(1); /* additional info */
1048 snprintf(prompt, sizeof(prompt),
1049 "Enter %.30s@%.128s's old password: ",
1050 authctxt->server_user, authctxt->host);
1051 password = read_passphrase(prompt, 0);
1052 packet_put_cstring(password);
1053 memset(password, 0, strlen(password));
1056 while (password == NULL) {
1057 snprintf(prompt, sizeof(prompt),
1058 "Enter %.30s@%.128s's new password: ",
1059 authctxt->server_user, authctxt->host);
1060 password = read_passphrase(prompt, RP_ALLOW_EOF);
1061 if (password == NULL) {
1065 snprintf(prompt, sizeof(prompt),
1066 "Retype %.30s@%.128s's new password: ",
1067 authctxt->server_user, authctxt->host);
1068 retype = read_passphrase(prompt, 0);
1069 if (strcmp(password, retype) != 0) {
1070 memset(password, 0, strlen(password));
1072 logit("Mismatch; try again, EOF to quit.");
1075 memset(retype, 0, strlen(retype));
1078 packet_put_cstring(password);
1079 memset(password, 0, strlen(password));
1081 packet_add_padding(64);
1084 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1085 &input_userauth_passwd_changereq);
1090 pw_encrypt(const char *password, const char *crypt_scheme, const char *salt)
1092 /* OpenBSD crypt(3) handles all of these */
1093 if (strcmp(crypt_scheme, "crypt") == 0 ||
1094 strcmp(crypt_scheme, "bcrypt") == 0 ||
1095 strcmp(crypt_scheme, "md5crypt") == 0 ||
1096 strcmp(crypt_scheme, "crypt-extended") == 0)
1097 return xstrdup(crypt(password, salt));
1098 error("%s: unsupported password encryption scheme \"%.100s\"",
1099 __func__, crypt_scheme);
1104 jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme,
1107 char prompt[256], *password, *crypted;
1112 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ",
1113 authctxt->server_user, authctxt->host);
1114 password = read_passphrase(prompt, 0);
1116 if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) {
1117 logit("Disabling %s authentication", authctxt->method->name);
1118 authctxt->method->enabled = NULL;
1119 /* Continue with an empty password to fail gracefully */
1120 crypted = xstrdup("");
1124 debug3("%s: salt = %s", __func__, salt);
1125 debug3("%s: scheme = %s", __func__, crypt_scheme);
1126 debug3("%s: crypted = %s", __func__, crypted);
1129 if (hash_buffer(crypted, strlen(crypted), EVP_sha256(),
1130 &secret, &secret_len) != 0)
1131 fatal("%s: hash_buffer", __func__);
1133 bzero(password, strlen(password));
1134 bzero(crypted, strlen(crypted));
1138 if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL)
1139 fatal("%s: BN_bin2bn (secret)", __func__);
1140 bzero(secret, secret_len);
1148 input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt)
1150 Authctxt *authctxt = ctxt;
1151 struct jpake_ctx *pctx = authctxt->methoddata;
1152 u_char *x3_proof, *x4_proof, *x2_s_proof;
1153 u_int x3_proof_len, x4_proof_len, x2_s_proof_len;
1154 char *crypt_scheme, *salt;
1156 /* Disable this message */
1157 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL);
1159 if ((pctx->g_x3 = BN_new()) == NULL ||
1160 (pctx->g_x4 = BN_new()) == NULL)
1161 fatal("%s: BN_new", __func__);
1163 /* Fetch step 1 values */
1164 crypt_scheme = packet_get_string(NULL);
1165 salt = packet_get_string(NULL);
1166 pctx->server_id = packet_get_string(&pctx->server_id_len);
1167 packet_get_bignum2(pctx->g_x3);
1168 packet_get_bignum2(pctx->g_x4);
1169 x3_proof = packet_get_string(&x3_proof_len);
1170 x4_proof = packet_get_string(&x4_proof_len);
1173 JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__));
1175 /* Obtain password and derive secret */
1176 pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt);
1177 bzero(crypt_scheme, strlen(crypt_scheme));
1178 bzero(salt, strlen(salt));
1179 xfree(crypt_scheme);
1181 JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__));
1183 /* Calculate step 2 values */
1184 jpake_step2(pctx->grp, pctx->s, pctx->g_x1,
1185 pctx->g_x3, pctx->g_x4, pctx->x2,
1186 pctx->server_id, pctx->server_id_len,
1187 pctx->client_id, pctx->client_id_len,
1188 x3_proof, x3_proof_len,
1189 x4_proof, x4_proof_len,
1191 &x2_s_proof, &x2_s_proof_len);
1193 bzero(x3_proof, x3_proof_len);
1194 bzero(x4_proof, x4_proof_len);
1198 JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__));
1200 /* Send values for step 2 */
1201 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2);
1202 packet_put_bignum2(pctx->a);
1203 packet_put_string(x2_s_proof, x2_s_proof_len);
1206 bzero(x2_s_proof, x2_s_proof_len);
1209 /* Expect step 2 packet from peer */
1210 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2,
1211 input_userauth_jpake_server_step2);
1216 input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt)
1218 Authctxt *authctxt = ctxt;
1219 struct jpake_ctx *pctx = authctxt->methoddata;
1221 u_int x4_s_proof_len;
1223 /* Disable this message */
1224 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL);
1226 if ((pctx->b = BN_new()) == NULL)
1227 fatal("%s: BN_new", __func__);
1229 /* Fetch step 2 values */
1230 packet_get_bignum2(pctx->b);
1231 x4_s_proof = packet_get_string(&x4_s_proof_len);
1234 JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__));
1236 /* Derive shared key and calculate confirmation hash */
1237 jpake_key_confirm(pctx->grp, pctx->s, pctx->b,
1238 pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4,
1239 pctx->client_id, pctx->client_id_len,
1240 pctx->server_id, pctx->server_id_len,
1241 session_id2, session_id2_len,
1242 x4_s_proof, x4_s_proof_len,
1244 &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len);
1246 bzero(x4_s_proof, x4_s_proof_len);
1249 JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__));
1251 /* Send key confirmation proof */
1252 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM);
1253 packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
1256 /* Expect confirmation from peer */
1257 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM,
1258 input_userauth_jpake_server_confirm);
1263 input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt)
1265 Authctxt *authctxt = ctxt;
1266 struct jpake_ctx *pctx = authctxt->methoddata;
1268 /* Disable this message */
1269 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL);
1271 pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len);
1274 JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__));
1276 /* Verify expected confirmation hash */
1277 if (jpake_check_confirm(pctx->k,
1278 pctx->server_id, pctx->server_id_len,
1279 session_id2, session_id2_len,
1280 pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1)
1281 debug("%s: %s success", __func__, authctxt->method->name);
1283 debug("%s: confirmation mismatch", __func__);
1284 /* XXX stash this so if auth succeeds then we can warn/kill */
1287 userauth_jpake_cleanup(authctxt);
1292 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1293 u_char *data, u_int datalen)
1298 /* the agent supports this key */
1300 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1303 * we have already loaded the private key or
1304 * the private key is stored in external hardware
1306 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1307 return (key_sign(id->key, sigp, lenp, data, datalen));
1308 /* load the private key from the file */
1309 if ((prv = load_identity_file(id->filename)) == NULL)
1311 ret = key_sign(prv, sigp, lenp, data, datalen);
1317 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1320 u_char *blob, *signature;
1321 u_int bloblen, slen;
1326 debug3("sign_and_send_pubkey");
1328 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1329 /* we cannot handle this key */
1330 debug3("sign_and_send_pubkey: cannot handle key");
1333 /* data to be signed */
1335 if (datafellows & SSH_OLD_SESSIONID) {
1336 buffer_append(&b, session_id2, session_id2_len);
1337 skip = session_id2_len;
1339 buffer_put_string(&b, session_id2, session_id2_len);
1340 skip = buffer_len(&b);
1342 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1343 buffer_put_cstring(&b, authctxt->server_user);
1344 buffer_put_cstring(&b,
1345 datafellows & SSH_BUG_PKSERVICE ?
1348 if (datafellows & SSH_BUG_PKAUTH) {
1349 buffer_put_char(&b, have_sig);
1351 buffer_put_cstring(&b, authctxt->method->name);
1352 buffer_put_char(&b, have_sig);
1353 buffer_put_cstring(&b, key_ssh_name(id->key));
1355 buffer_put_string(&b, blob, bloblen);
1357 /* generate signature */
1358 ret = identity_sign(id, &signature, &slen,
1359 buffer_ptr(&b), buffer_len(&b));
1368 if (datafellows & SSH_BUG_PKSERVICE) {
1370 buffer_append(&b, session_id2, session_id2_len);
1371 skip = session_id2_len;
1372 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1373 buffer_put_cstring(&b, authctxt->server_user);
1374 buffer_put_cstring(&b, authctxt->service);
1375 buffer_put_cstring(&b, authctxt->method->name);
1376 buffer_put_char(&b, have_sig);
1377 if (!(datafellows & SSH_BUG_PKAUTH))
1378 buffer_put_cstring(&b, key_ssh_name(id->key));
1379 buffer_put_string(&b, blob, bloblen);
1383 /* append signature */
1384 buffer_put_string(&b, signature, slen);
1387 /* skip session id and packet type */
1388 if (buffer_len(&b) < skip + 1)
1389 fatal("userauth_pubkey: internal error");
1390 buffer_consume(&b, skip + 1);
1392 /* put remaining data from buffer into packet */
1393 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1394 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1402 send_pubkey_test(Authctxt *authctxt, Identity *id)
1405 u_int bloblen, have_sig = 0;
1407 debug3("send_pubkey_test");
1409 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1410 /* we cannot handle this key */
1411 debug3("send_pubkey_test: cannot handle key");
1414 /* register callback for USERAUTH_PK_OK message */
1415 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1417 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1418 packet_put_cstring(authctxt->server_user);
1419 packet_put_cstring(authctxt->service);
1420 packet_put_cstring(authctxt->method->name);
1421 packet_put_char(have_sig);
1422 if (!(datafellows & SSH_BUG_PKAUTH))
1423 packet_put_cstring(key_ssh_name(id->key));
1424 packet_put_string(blob, bloblen);
1431 load_identity_file(char *filename)
1434 char prompt[300], *passphrase;
1435 int perm_ok, quit, i;
1438 if (stat(filename, &st) < 0) {
1439 debug3("no such identity: %s", filename);
1442 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1445 if (private == NULL) {
1446 if (options.batch_mode)
1448 snprintf(prompt, sizeof prompt,
1449 "Enter passphrase for key '%.100s': ", filename);
1450 for (i = 0; i < options.number_of_password_prompts; i++) {
1451 passphrase = read_passphrase(prompt, 0);
1452 if (strcmp(passphrase, "") != 0) {
1453 private = key_load_private_type(KEY_UNSPEC,
1454 filename, passphrase, NULL, NULL);
1457 debug2("no passphrase given, try next key");
1460 memset(passphrase, 0, strlen(passphrase));
1462 if (private != NULL || quit)
1464 debug2("bad passphrase given, try again...");
1471 * try keys in the following order:
1472 * 1. agent keys that are found in the config file
1473 * 2. other agent keys
1474 * 3. keys that are only listed in the config file
1477 pubkey_prepare(Authctxt *authctxt)
1480 Idlist agent, files, *preferred;
1482 AuthenticationConnection *ac;
1486 TAILQ_INIT(&agent); /* keys from the agent */
1487 TAILQ_INIT(&files); /* keys from the config file */
1488 preferred = &authctxt->keys;
1489 TAILQ_INIT(preferred); /* preferred order of keys */
1491 /* list of keys stored in the filesystem */
1492 for (i = 0; i < options.num_identity_files; i++) {
1493 key = options.identity_keys[i];
1494 if (key && key->type == KEY_RSA1)
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 public key: %s", id->filename);
1582 sent = send_pubkey_test(authctxt, id);
1583 } else if (id->key == NULL) {
1584 debug("Trying private key: %s", id->filename);
1585 id->key = load_identity_file(id->filename);
1586 if (id->key != NULL) {
1588 sent = sign_and_send_pubkey(authctxt, id);
1600 * Send userauth request message specifying keyboard-interactive method.
1603 userauth_kbdint(Authctxt *authctxt)
1605 static int attempt = 0;
1607 if (attempt++ >= options.number_of_password_prompts)
1609 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1610 if (attempt > 1 && !authctxt->info_req_seen) {
1611 debug3("userauth_kbdint: disable: no info_req_seen");
1612 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1616 debug2("userauth_kbdint");
1617 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1618 packet_put_cstring(authctxt->server_user);
1619 packet_put_cstring(authctxt->service);
1620 packet_put_cstring(authctxt->method->name);
1621 packet_put_cstring(""); /* lang */
1622 packet_put_cstring(options.kbd_interactive_devices ?
1623 options.kbd_interactive_devices : "");
1626 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1631 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1634 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1636 Authctxt *authctxt = ctxt;
1637 char *name, *inst, *lang, *prompt, *response;
1638 u_int num_prompts, i;
1641 debug2("input_userauth_info_req");
1643 if (authctxt == NULL)
1644 fatal("input_userauth_info_req: no authentication context");
1646 authctxt->info_req_seen = 1;
1648 name = packet_get_string(NULL);
1649 inst = packet_get_string(NULL);
1650 lang = packet_get_string(NULL);
1651 if (strlen(name) > 0)
1653 if (strlen(inst) > 0)
1659 num_prompts = packet_get_int();
1661 * Begin to build info response packet based on prompts requested.
1662 * We commit to providing the correct number of responses, so if
1663 * further on we run into a problem that prevents this, we have to
1664 * be sure and clean this up and send a correct error response.
1666 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1667 packet_put_int(num_prompts);
1669 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1670 for (i = 0; i < num_prompts; i++) {
1671 prompt = packet_get_string(NULL);
1672 echo = packet_get_char();
1674 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1676 packet_put_cstring(response);
1677 memset(response, 0, strlen(response));
1681 packet_check_eom(); /* done with parsing incoming message. */
1683 packet_add_padding(64);
1688 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1689 u_char *data, u_int datalen)
1694 int to[2], from[2], status, version = 2;
1696 debug2("ssh_keysign called");
1698 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1699 error("ssh_keysign: no installed: %s", strerror(errno));
1702 if (fflush(stdout) != 0)
1703 error("ssh_keysign: fflush: %s", strerror(errno));
1705 error("ssh_keysign: pipe: %s", strerror(errno));
1708 if (pipe(from) < 0) {
1709 error("ssh_keysign: pipe: %s", strerror(errno));
1712 if ((pid = fork()) < 0) {
1713 error("ssh_keysign: fork: %s", strerror(errno));
1717 permanently_drop_suid(getuid());
1719 if (dup2(from[1], STDOUT_FILENO) < 0)
1720 fatal("ssh_keysign: dup2: %s", strerror(errno));
1722 if (dup2(to[0], STDIN_FILENO) < 0)
1723 fatal("ssh_keysign: dup2: %s", strerror(errno));
1726 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1727 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1734 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1735 buffer_put_string(&b, data, datalen);
1736 if (ssh_msg_send(to[1], version, &b) == -1)
1737 fatal("ssh_keysign: couldn't send request");
1739 if (ssh_msg_recv(from[0], &b) < 0) {
1740 error("ssh_keysign: no reply");
1747 while (waitpid(pid, &status, 0) < 0)
1751 if (buffer_get_char(&b) != version) {
1752 error("ssh_keysign: bad version");
1756 *sigp = buffer_get_string(&b, lenp);
1763 userauth_hostbased(Authctxt *authctxt)
1765 Key *private = NULL;
1766 Sensitive *sensitive = authctxt->sensitive;
1768 u_char *signature, *blob;
1769 char *chost, *pkalg, *p, myname[NI_MAXHOST];
1770 const char *service;
1772 int ok, i, len, found = 0;
1774 /* check for a useful key */
1775 for (i = 0; i < sensitive->nkeys; i++) {
1776 private = sensitive->keys[i];
1777 if (private && private->type != KEY_RSA1) {
1779 /* we take and free the key */
1780 sensitive->keys[i] = NULL;
1785 debug("No more client hostkeys for hostbased authentication.");
1788 if (key_to_blob(private, &blob, &blen) == 0) {
1792 /* figure out a name for the client host */
1794 if (packet_connection_is_on_socket())
1795 p = get_local_name(packet_get_connection_in());
1797 if (gethostname(myname, sizeof(myname)) == -1) {
1798 verbose("userauth_hostbased: gethostname: %s",
1801 p = xstrdup(myname);
1804 error("userauth_hostbased: cannot get local ipaddr/name");
1809 len = strlen(p) + 2;
1810 xasprintf(&chost, "%s.", p);
1811 debug2("userauth_hostbased: chost %s", chost);
1814 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1816 pkalg = xstrdup(key_ssh_name(private));
1818 /* construct data */
1819 buffer_put_string(&b, session_id2, session_id2_len);
1820 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1821 buffer_put_cstring(&b, authctxt->server_user);
1822 buffer_put_cstring(&b, service);
1823 buffer_put_cstring(&b, authctxt->method->name);
1824 buffer_put_cstring(&b, pkalg);
1825 buffer_put_string(&b, blob, blen);
1826 buffer_put_cstring(&b, chost);
1827 buffer_put_cstring(&b, authctxt->local_user);
1831 if (sensitive->external_keysign)
1832 ok = ssh_keysign(private, &signature, &slen,
1833 buffer_ptr(&b), buffer_len(&b));
1835 ok = key_sign(private, &signature, &slen,
1836 buffer_ptr(&b), buffer_len(&b));
1840 error("key_sign failed");
1846 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1847 packet_put_cstring(authctxt->server_user);
1848 packet_put_cstring(authctxt->service);
1849 packet_put_cstring(authctxt->method->name);
1850 packet_put_cstring(pkalg);
1851 packet_put_string(blob, blen);
1852 packet_put_cstring(chost);
1853 packet_put_cstring(authctxt->local_user);
1854 packet_put_string(signature, slen);
1855 memset(signature, 's', slen);
1867 userauth_jpake(Authctxt *authctxt)
1869 struct jpake_ctx *pctx;
1870 u_char *x1_proof, *x2_proof;
1871 u_int x1_proof_len, x2_proof_len;
1872 static int attempt = 0; /* XXX share with userauth_password's? */
1874 if (attempt++ >= options.number_of_password_prompts)
1877 error("Permission denied, please try again.");
1879 if (authctxt->methoddata != NULL)
1880 fatal("%s: authctxt->methoddata already set (%p)",
1881 __func__, authctxt->methoddata);
1883 authctxt->methoddata = pctx = jpake_new();
1886 * Send request immediately, to get the protocol going while
1887 * we do the initial computations.
1889 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1890 packet_put_cstring(authctxt->server_user);
1891 packet_put_cstring(authctxt->service);
1892 packet_put_cstring(authctxt->method->name);
1894 packet_write_wait();
1896 jpake_step1(pctx->grp,
1897 &pctx->client_id, &pctx->client_id_len,
1898 &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2,
1899 &x1_proof, &x1_proof_len,
1900 &x2_proof, &x2_proof_len);
1902 JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__));
1904 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1);
1905 packet_put_string(pctx->client_id, pctx->client_id_len);
1906 packet_put_bignum2(pctx->g_x1);
1907 packet_put_bignum2(pctx->g_x2);
1908 packet_put_string(x1_proof, x1_proof_len);
1909 packet_put_string(x2_proof, x2_proof_len);
1912 bzero(x1_proof, x1_proof_len);
1913 bzero(x2_proof, x2_proof_len);
1917 /* Expect step 1 packet from peer */
1918 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1,
1919 input_userauth_jpake_server_step1);
1925 userauth_jpake_cleanup(Authctxt *authctxt)
1927 debug3("%s: clean up", __func__);
1928 if (authctxt->methoddata != NULL) {
1929 jpake_free(authctxt->methoddata);
1930 authctxt->methoddata = NULL;
1935 /* find auth method */
1938 * given auth method name, if configurable options permit this method fill
1939 * in auth_ident field and return true, otherwise return false.
1942 authmethod_is_enabled(Authmethod *method)
1946 /* return false if options indicate this method is disabled */
1947 if (method->enabled == NULL || *method->enabled == 0)
1949 /* return false if batch mode is enabled but method needs interactive mode */
1950 if (method->batch_flag != NULL && *method->batch_flag != 0)
1956 authmethod_lookup(const char *name)
1958 Authmethod *method = NULL;
1960 for (method = authmethods; method->name != NULL; method++)
1961 if (strcmp(name, method->name) == 0)
1963 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1967 /* XXX internal state */
1968 static Authmethod *current = NULL;
1969 static char *supported = NULL;
1970 static char *preferred = NULL;
1973 * Given the authentication method list sent by the server, return the
1974 * next method we should try. If the server initially sends a nil list,
1975 * use a built-in default list.
1978 authmethod_get(char *authlist)
1983 /* Use a suitable default if we're passed a nil list. */
1984 if (authlist == NULL || strlen(authlist) == 0)
1985 authlist = options.preferred_authentications;
1987 if (supported == NULL || strcmp(authlist, supported) != 0) {
1988 debug3("start over, passed a different list %s", authlist);
1989 if (supported != NULL)
1991 supported = xstrdup(authlist);
1992 preferred = options.preferred_authentications;
1993 debug3("preferred %s", preferred);
1995 } else if (current != NULL && authmethod_is_enabled(current))
1999 if ((name = match_list(preferred, supported, &next)) == NULL) {
2000 debug("No more authentication methods to try.");
2005 debug3("authmethod_lookup %s", name);
2006 debug3("remaining preferred: %s", preferred);
2007 if ((current = authmethod_lookup(name)) != NULL &&
2008 authmethod_is_enabled(current)) {
2009 debug3("authmethod_is_enabled %s", name);
2010 debug("Next authentication method: %s", name);
2017 authmethods_get(void)
2019 Authmethod *method = NULL;
2024 for (method = authmethods; method->name != NULL; method++) {
2025 if (authmethod_is_enabled(method)) {
2026 if (buffer_len(&b) > 0)
2027 buffer_append(&b, ",", 1);
2028 buffer_append(&b, method->name, strlen(method->name));
2031 buffer_append(&b, "\0", 1);
2032 list = xstrdup(buffer_ptr(&b));