1 /* $OpenBSD: sshconnect2.c,v 1.192 2013/02/17 23:16:57 dtucker Exp $ */
4 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Copyright (c) 2008 Damien Miller. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
31 #include <sys/socket.h>
44 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
48 #include "openbsd-compat/sys-queue.h"
59 #include "myproposal.h"
60 #include "sshconnect.h"
71 #include "pathnames.h"
82 extern char *client_version_string;
83 extern char *server_version_string;
84 extern Options options;
85 #ifdef NONE_CIPHER_ENABLED
89 * tty_flag is set in ssh.c so we can use it here. If set then prevent
90 * the switch to the null cipher.
100 u_char *session_id2 = NULL;
101 u_int session_id2_len = 0;
104 struct sockaddr *xxx_hostaddr;
109 verify_host_key_callback(Key *hostkey)
111 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
112 fatal("Host key verification failed.");
117 order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
119 char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
121 struct hostkeys *hostkeys;
125 /* Find all hostkeys for this hostname */
126 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
127 hostkeys = init_hostkeys();
128 for (i = 0; i < options.num_user_hostfiles; i++)
129 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
130 for (i = 0; i < options.num_system_hostfiles; i++)
131 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
133 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
134 maxlen = strlen(avail) + 1;
135 first = xmalloc(maxlen);
136 last = xmalloc(maxlen);
137 *first = *last = '\0';
139 #define ALG_APPEND(to, from) \
142 strlcat(to, ",", maxlen); \
143 strlcat(to, from, maxlen); \
146 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
147 if ((ktype = key_type_from_name(alg)) == KEY_UNSPEC)
148 fatal("%s: unknown alg %s", __func__, alg);
149 if (lookup_key_in_hostkeys_by_type(hostkeys,
150 key_type_plain(ktype), NULL))
151 ALG_APPEND(first, alg);
153 ALG_APPEND(last, alg);
156 xasprintf(&ret, "%s%s%s", first, *first == '\0' ? "" : ",", last);
158 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
164 free_hostkeys(hostkeys);
170 ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
175 xxx_hostaddr = hostaddr;
177 if (options.ciphers == (char *)-1) {
178 logit("No valid ciphers for protocol version 2 given, using defaults.");
179 options.ciphers = NULL;
181 if (options.ciphers != NULL) {
182 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
183 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
185 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
186 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
187 myproposal[PROPOSAL_ENC_ALGS_STOC] =
188 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
189 if (options.compression) {
190 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
191 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
193 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
194 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
196 if (options.macs != NULL) {
197 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
198 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
200 if (options.hostkeyalgorithms != NULL)
201 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
202 options.hostkeyalgorithms;
204 /* Prefer algorithms that we already have keys for */
205 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
206 order_hostkeyalgs(host, hostaddr, port);
208 if (options.kex_algorithms != NULL)
209 myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
211 if (options.rekey_limit)
212 packet_set_rekey_limit((u_int32_t)options.rekey_limit);
214 /* start key exchange */
215 kex = kex_setup(myproposal);
216 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
217 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
218 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
219 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
220 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
221 kex->client_version_string=client_version_string;
222 kex->server_version_string=server_version_string;
223 kex->verify_host_key=&verify_host_key_callback;
227 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
229 if (options.use_roaming && !kex->roaming) {
230 debug("Roaming not allowed by server");
231 options.use_roaming = 0;
234 session_id2 = kex->session_id;
235 session_id2_len = kex->session_id_len;
238 /* send 1st encrypted/maced/compressed message */
239 packet_start(SSH2_MSG_IGNORE);
240 packet_put_cstring("markus");
250 typedef struct Authctxt Authctxt;
251 typedef struct Authmethod Authmethod;
252 typedef struct identity Identity;
253 typedef struct idlist Idlist;
256 TAILQ_ENTRY(identity) next;
257 AuthenticationConnection *ac; /* set if agent supports key */
258 Key *key; /* public/private key */
259 char *filename; /* comment for agent-only keys */
261 int isprivate; /* key points to the private key */
264 TAILQ_HEAD(idlist, identity);
267 const char *server_user;
268 const char *local_user;
272 sig_atomic_t success;
276 AuthenticationConnection *agent;
278 Sensitive *sensitive;
279 /* kbd-interactive */
285 char *name; /* string to compare against server's list */
286 int (*userauth)(Authctxt *authctxt);
287 void (*cleanup)(Authctxt *authctxt);
288 int *enabled; /* flag in option struct that enables method */
289 int *batch_flag; /* flag in option struct that disables method */
292 void input_userauth_success(int, u_int32_t, void *);
293 void input_userauth_success_unexpected(int, u_int32_t, void *);
294 void input_userauth_failure(int, u_int32_t, void *);
295 void input_userauth_banner(int, u_int32_t, void *);
296 void input_userauth_error(int, u_int32_t, void *);
297 void input_userauth_info_req(int, u_int32_t, void *);
298 void input_userauth_pk_ok(int, u_int32_t, void *);
299 void input_userauth_passwd_changereq(int, u_int32_t, void *);
300 void input_userauth_jpake_server_step1(int, u_int32_t, void *);
301 void input_userauth_jpake_server_step2(int, u_int32_t, void *);
302 void input_userauth_jpake_server_confirm(int, u_int32_t, void *);
304 int userauth_none(Authctxt *);
305 int userauth_pubkey(Authctxt *);
306 int userauth_passwd(Authctxt *);
307 int userauth_kbdint(Authctxt *);
308 int userauth_hostbased(Authctxt *);
309 int userauth_jpake(Authctxt *);
311 void userauth_jpake_cleanup(Authctxt *);
314 int userauth_gssapi(Authctxt *authctxt);
315 void input_gssapi_response(int type, u_int32_t, void *);
316 void input_gssapi_token(int type, u_int32_t, void *);
317 void input_gssapi_hash(int type, u_int32_t, void *);
318 void input_gssapi_error(int, u_int32_t, void *);
319 void input_gssapi_errtok(int, u_int32_t, void *);
322 void userauth(Authctxt *, char *);
324 static int sign_and_send_pubkey(Authctxt *, Identity *);
325 static void pubkey_prepare(Authctxt *);
326 static void pubkey_cleanup(Authctxt *);
327 static Key *load_identity_file(char *, int);
329 static Authmethod *authmethod_get(char *authlist);
330 static Authmethod *authmethod_lookup(const char *name);
331 static char *authmethods_get(void);
333 Authmethod authmethods[] = {
338 &options.gss_authentication,
344 &options.hostbased_authentication,
349 &options.pubkey_authentication,
352 {"jpake-01@openssh.com",
354 userauth_jpake_cleanup,
355 &options.zero_knowledge_password_authentication,
356 &options.batch_mode},
358 {"keyboard-interactive",
361 &options.kbd_interactive_authentication,
362 &options.batch_mode},
366 &options.password_authentication,
367 &options.batch_mode},
373 {NULL, NULL, NULL, NULL, NULL}
377 ssh_userauth2(const char *local_user, const char *server_user, char *host,
378 Sensitive *sensitive)
383 if (options.challenge_response_authentication)
384 options.kbd_interactive_authentication = 1;
386 packet_start(SSH2_MSG_SERVICE_REQUEST);
387 packet_put_cstring("ssh-userauth");
389 debug("SSH2_MSG_SERVICE_REQUEST sent");
391 type = packet_read();
392 if (type != SSH2_MSG_SERVICE_ACCEPT)
393 fatal("Server denied authentication request: %d", type);
394 if (packet_remaining() > 0) {
395 char *reply = packet_get_string(NULL);
396 debug2("service_accept: %s", reply);
399 debug2("buggy server: service_accept w/o service");
402 debug("SSH2_MSG_SERVICE_ACCEPT received");
404 if (options.preferred_authentications == NULL)
405 options.preferred_authentications = authmethods_get();
407 /* setup authentication context */
408 memset(&authctxt, 0, sizeof(authctxt));
409 pubkey_prepare(&authctxt);
410 authctxt.server_user = server_user;
411 authctxt.local_user = local_user;
412 authctxt.host = host;
413 authctxt.service = "ssh-connection"; /* service name */
414 authctxt.success = 0;
415 authctxt.method = authmethod_lookup("none");
416 authctxt.authlist = NULL;
417 authctxt.methoddata = NULL;
418 authctxt.sensitive = sensitive;
419 authctxt.info_req_seen = 0;
420 if (authctxt.method == NULL)
421 fatal("ssh_userauth2: internal error: cannot send userauth none request");
423 /* initial userauth request */
424 userauth_none(&authctxt);
426 dispatch_init(&input_userauth_error);
427 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
428 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
429 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
430 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
432 pubkey_cleanup(&authctxt);
433 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
435 #ifdef NONE_CIPHER_ENABLED
437 * If the user explicitly requests to use the none cipher enable it
438 * post authentication and only if the right conditions are met: both
439 * of the NONE switches must be true and there must be no tty allocated.
441 if (options.none_switch == 1 && options.none_enabled == 1) {
443 debug("Requesting none cipher re-keying...");
444 myproposal[PROPOSAL_ENC_ALGS_STOC] = "none";
445 myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none";
446 kex_prop2buf(&xxx_kex->my, myproposal);
447 packet_request_rekeying();
448 fprintf(stderr, "WARNING: enabled NONE cipher\n");
450 /* Requested NONE cipher on an interactive session. */
451 debug("Cannot switch to NONE cipher with tty "
453 fprintf(stderr, "NONE cipher switch disabled given "
454 "a TTY is allocated\n");
458 debug("Authentication succeeded (%s).", authctxt.method->name);
462 userauth(Authctxt *authctxt, char *authlist)
464 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
465 authctxt->method->cleanup(authctxt);
467 if (authctxt->methoddata) {
468 xfree(authctxt->methoddata);
469 authctxt->methoddata = NULL;
471 if (authlist == NULL) {
472 authlist = authctxt->authlist;
474 if (authctxt->authlist)
475 xfree(authctxt->authlist);
476 authctxt->authlist = authlist;
479 Authmethod *method = authmethod_get(authlist);
481 fatal("Permission denied (%s).", authlist);
482 authctxt->method = method;
484 /* reset the per method handler */
485 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
486 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
488 /* and try new method */
489 if (method->userauth(authctxt) != 0) {
490 debug2("we sent a %s packet, wait for reply", method->name);
493 debug2("we did not send a packet, disable method");
494 method->enabled = NULL;
501 input_userauth_error(int type, u_int32_t seq, void *ctxt)
503 fatal("input_userauth_error: bad message during authentication: "
509 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
511 char *msg, *raw, *lang;
514 debug3("input_userauth_banner");
515 raw = packet_get_string(&len);
516 lang = packet_get_string(NULL);
517 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
520 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
521 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
522 fprintf(stderr, "%s", msg);
531 input_userauth_success(int type, u_int32_t seq, void *ctxt)
533 Authctxt *authctxt = ctxt;
535 if (authctxt == NULL)
536 fatal("input_userauth_success: no authentication context");
537 if (authctxt->authlist) {
538 xfree(authctxt->authlist);
539 authctxt->authlist = NULL;
541 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
542 authctxt->method->cleanup(authctxt);
543 if (authctxt->methoddata) {
544 xfree(authctxt->methoddata);
545 authctxt->methoddata = NULL;
547 authctxt->success = 1; /* break out */
551 input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
553 Authctxt *authctxt = ctxt;
555 if (authctxt == NULL)
556 fatal("%s: no authentication context", __func__);
558 fatal("Unexpected authentication success during %s.",
559 authctxt->method->name);
564 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
566 Authctxt *authctxt = ctxt;
567 char *authlist = NULL;
570 if (authctxt == NULL)
571 fatal("input_userauth_failure: no authentication context");
573 authlist = packet_get_string(NULL);
574 partial = packet_get_char();
578 logit("Authenticated with partial success.");
579 debug("Authentications that can continue: %s", authlist);
581 userauth(authctxt, authlist);
586 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
588 Authctxt *authctxt = ctxt;
592 int pktype, sent = 0;
597 if (authctxt == NULL)
598 fatal("input_userauth_pk_ok: no authentication context");
599 if (datafellows & SSH_BUG_PKOK) {
600 /* this is similar to SSH_BUG_PKAUTH */
601 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
602 pkblob = packet_get_string(&blen);
604 buffer_append(&b, pkblob, blen);
605 pkalg = buffer_get_string(&b, &alen);
608 pkalg = packet_get_string(&alen);
609 pkblob = packet_get_string(&blen);
613 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
615 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
616 debug("unknown pkalg %s", pkalg);
619 if ((key = key_from_blob(pkblob, blen)) == NULL) {
620 debug("no key from blob. pkalg %s", pkalg);
623 if (key->type != pktype) {
624 error("input_userauth_pk_ok: type mismatch "
625 "for decoded key (received %d, expected %d)",
629 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
630 debug2("input_userauth_pk_ok: fp %s", fp);
634 * search keys in the reverse order, because last candidate has been
635 * moved to the end of the queue. this also avoids confusion by
638 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
639 if (key_equal(key, id->key)) {
640 sent = sign_and_send_pubkey(authctxt, id);
650 /* try another method if we did not send a packet */
652 userauth(authctxt, NULL);
657 userauth_gssapi(Authctxt *authctxt)
659 Gssctxt *gssctxt = NULL;
660 static gss_OID_set gss_supported = NULL;
661 static u_int mech = 0;
665 /* Try one GSSAPI method at a time, rather than sending them all at
668 if (gss_supported == NULL)
669 gss_indicate_mechs(&min, &gss_supported);
671 /* Check to see if the mechanism is usable before we offer it */
672 while (mech < gss_supported->count && !ok) {
673 /* My DER encoding requires length<128 */
674 if (gss_supported->elements[mech].length < 128 &&
675 ssh_gssapi_check_mechanism(&gssctxt,
676 &gss_supported->elements[mech], authctxt->host)) {
677 ok = 1; /* Mechanism works */
686 authctxt->methoddata=(void *)gssctxt;
688 packet_start(SSH2_MSG_USERAUTH_REQUEST);
689 packet_put_cstring(authctxt->server_user);
690 packet_put_cstring(authctxt->service);
691 packet_put_cstring(authctxt->method->name);
695 packet_put_int((gss_supported->elements[mech].length) + 2);
696 packet_put_char(SSH_GSS_OIDTYPE);
697 packet_put_char(gss_supported->elements[mech].length);
698 packet_put_raw(gss_supported->elements[mech].elements,
699 gss_supported->elements[mech].length);
703 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
704 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
705 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
706 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
708 mech++; /* Move along to next candidate */
714 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
716 Authctxt *authctxt = ctxt;
717 Gssctxt *gssctxt = authctxt->methoddata;
718 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
719 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
720 gss_buffer_desc gssbuf;
721 OM_uint32 status, ms, flags;
724 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
725 recv_tok, &send_tok, &flags);
727 if (send_tok.length > 0) {
728 if (GSS_ERROR(status))
729 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
731 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
733 packet_put_string(send_tok.value, send_tok.length);
735 gss_release_buffer(&ms, &send_tok);
738 if (status == GSS_S_COMPLETE) {
739 /* send either complete or MIC, depending on mechanism */
740 if (!(flags & GSS_C_INTEG_FLAG)) {
741 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
744 ssh_gssapi_buildmic(&b, authctxt->server_user,
745 authctxt->service, "gssapi-with-mic");
747 gssbuf.value = buffer_ptr(&b);
748 gssbuf.length = buffer_len(&b);
750 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
752 if (!GSS_ERROR(status)) {
753 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
754 packet_put_string(mic.value, mic.length);
760 gss_release_buffer(&ms, &mic);
769 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
771 Authctxt *authctxt = ctxt;
776 if (authctxt == NULL)
777 fatal("input_gssapi_response: no authentication context");
778 gssctxt = authctxt->methoddata;
781 oidv = packet_get_string(&oidlen);
784 oidv[0] != SSH_GSS_OIDTYPE ||
785 oidv[1] != oidlen - 2) {
787 debug("Badly encoded mechanism OID received");
788 userauth(authctxt, NULL);
792 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
793 fatal("Server returned different OID than expected");
799 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
800 /* Start again with next method on list */
801 debug("Trying to start again");
802 userauth(authctxt, NULL);
809 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
811 Authctxt *authctxt = ctxt;
812 gss_buffer_desc recv_tok;
816 if (authctxt == NULL)
817 fatal("input_gssapi_response: no authentication context");
819 recv_tok.value = packet_get_string(&slen);
820 recv_tok.length = slen; /* safe typecast */
824 status = process_gssapi_token(ctxt, &recv_tok);
826 xfree(recv_tok.value);
828 if (GSS_ERROR(status)) {
829 /* Start again with the next method in the list */
830 userauth(authctxt, NULL);
837 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
839 Authctxt *authctxt = ctxt;
841 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
842 gss_buffer_desc recv_tok;
843 OM_uint32 status, ms;
846 if (authctxt == NULL)
847 fatal("input_gssapi_response: no authentication context");
848 gssctxt = authctxt->methoddata;
850 recv_tok.value = packet_get_string(&len);
851 recv_tok.length = len;
855 /* Stick it into GSSAPI and see what it says */
856 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
857 &recv_tok, &send_tok, NULL);
859 xfree(recv_tok.value);
860 gss_release_buffer(&ms, &send_tok);
862 /* Server will be returning a failed packet after this one */
867 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
873 maj=packet_get_int();
874 min=packet_get_int();
875 msg=packet_get_string(NULL);
876 lang=packet_get_string(NULL);
880 debug("Server GSSAPI Error:\n%s", msg);
887 userauth_none(Authctxt *authctxt)
889 /* initial userauth request */
890 packet_start(SSH2_MSG_USERAUTH_REQUEST);
891 packet_put_cstring(authctxt->server_user);
892 packet_put_cstring(authctxt->service);
893 packet_put_cstring(authctxt->method->name);
899 userauth_passwd(Authctxt *authctxt)
901 static int attempt = 0;
904 const char *host = options.host_key_alias ? options.host_key_alias :
907 if (attempt++ >= options.number_of_password_prompts)
911 error("Permission denied, please try again.");
913 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
914 authctxt->server_user, host);
915 password = read_passphrase(prompt, 0);
916 packet_start(SSH2_MSG_USERAUTH_REQUEST);
917 packet_put_cstring(authctxt->server_user);
918 packet_put_cstring(authctxt->service);
919 packet_put_cstring(authctxt->method->name);
921 packet_put_cstring(password);
922 memset(password, 0, strlen(password));
924 packet_add_padding(64);
927 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
928 &input_userauth_passwd_changereq);
934 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
938 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
940 Authctxt *authctxt = ctxt;
941 char *info, *lang, *password = NULL, *retype = NULL;
943 const char *host = options.host_key_alias ? options.host_key_alias :
946 debug2("input_userauth_passwd_changereq");
948 if (authctxt == NULL)
949 fatal("input_userauth_passwd_changereq: "
950 "no authentication context");
952 info = packet_get_string(NULL);
953 lang = packet_get_string(NULL);
954 if (strlen(info) > 0)
958 packet_start(SSH2_MSG_USERAUTH_REQUEST);
959 packet_put_cstring(authctxt->server_user);
960 packet_put_cstring(authctxt->service);
961 packet_put_cstring(authctxt->method->name);
962 packet_put_char(1); /* additional info */
963 snprintf(prompt, sizeof(prompt),
964 "Enter %.30s@%.128s's old password: ",
965 authctxt->server_user, host);
966 password = read_passphrase(prompt, 0);
967 packet_put_cstring(password);
968 memset(password, 0, strlen(password));
971 while (password == NULL) {
972 snprintf(prompt, sizeof(prompt),
973 "Enter %.30s@%.128s's new password: ",
974 authctxt->server_user, host);
975 password = read_passphrase(prompt, RP_ALLOW_EOF);
976 if (password == NULL) {
980 snprintf(prompt, sizeof(prompt),
981 "Retype %.30s@%.128s's new password: ",
982 authctxt->server_user, host);
983 retype = read_passphrase(prompt, 0);
984 if (strcmp(password, retype) != 0) {
985 memset(password, 0, strlen(password));
987 logit("Mismatch; try again, EOF to quit.");
990 memset(retype, 0, strlen(retype));
993 packet_put_cstring(password);
994 memset(password, 0, strlen(password));
996 packet_add_padding(64);
999 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1000 &input_userauth_passwd_changereq);
1005 pw_encrypt(const char *password, const char *crypt_scheme, const char *salt)
1007 /* OpenBSD crypt(3) handles all of these */
1008 if (strcmp(crypt_scheme, "crypt") == 0 ||
1009 strcmp(crypt_scheme, "bcrypt") == 0 ||
1010 strcmp(crypt_scheme, "md5crypt") == 0 ||
1011 strcmp(crypt_scheme, "crypt-extended") == 0)
1012 return xstrdup(crypt(password, salt));
1013 error("%s: unsupported password encryption scheme \"%.100s\"",
1014 __func__, crypt_scheme);
1019 jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme,
1022 char prompt[256], *password, *crypted;
1027 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ",
1028 authctxt->server_user, authctxt->host);
1029 password = read_passphrase(prompt, 0);
1031 if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) {
1032 logit("Disabling %s authentication", authctxt->method->name);
1033 authctxt->method->enabled = NULL;
1034 /* Continue with an empty password to fail gracefully */
1035 crypted = xstrdup("");
1039 debug3("%s: salt = %s", __func__, salt);
1040 debug3("%s: scheme = %s", __func__, crypt_scheme);
1041 debug3("%s: crypted = %s", __func__, crypted);
1044 if (hash_buffer(crypted, strlen(crypted), EVP_sha256(),
1045 &secret, &secret_len) != 0)
1046 fatal("%s: hash_buffer", __func__);
1048 bzero(password, strlen(password));
1049 bzero(crypted, strlen(crypted));
1053 if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL)
1054 fatal("%s: BN_bin2bn (secret)", __func__);
1055 bzero(secret, secret_len);
1063 input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt)
1065 Authctxt *authctxt = ctxt;
1066 struct jpake_ctx *pctx = authctxt->methoddata;
1067 u_char *x3_proof, *x4_proof, *x2_s_proof;
1068 u_int x3_proof_len, x4_proof_len, x2_s_proof_len;
1069 char *crypt_scheme, *salt;
1071 /* Disable this message */
1072 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL);
1074 if ((pctx->g_x3 = BN_new()) == NULL ||
1075 (pctx->g_x4 = BN_new()) == NULL)
1076 fatal("%s: BN_new", __func__);
1078 /* Fetch step 1 values */
1079 crypt_scheme = packet_get_string(NULL);
1080 salt = packet_get_string(NULL);
1081 pctx->server_id = packet_get_string(&pctx->server_id_len);
1082 packet_get_bignum2(pctx->g_x3);
1083 packet_get_bignum2(pctx->g_x4);
1084 x3_proof = packet_get_string(&x3_proof_len);
1085 x4_proof = packet_get_string(&x4_proof_len);
1088 JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__));
1090 /* Obtain password and derive secret */
1091 pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt);
1092 bzero(crypt_scheme, strlen(crypt_scheme));
1093 bzero(salt, strlen(salt));
1094 xfree(crypt_scheme);
1096 JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__));
1098 /* Calculate step 2 values */
1099 jpake_step2(pctx->grp, pctx->s, pctx->g_x1,
1100 pctx->g_x3, pctx->g_x4, pctx->x2,
1101 pctx->server_id, pctx->server_id_len,
1102 pctx->client_id, pctx->client_id_len,
1103 x3_proof, x3_proof_len,
1104 x4_proof, x4_proof_len,
1106 &x2_s_proof, &x2_s_proof_len);
1108 bzero(x3_proof, x3_proof_len);
1109 bzero(x4_proof, x4_proof_len);
1113 JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__));
1115 /* Send values for step 2 */
1116 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2);
1117 packet_put_bignum2(pctx->a);
1118 packet_put_string(x2_s_proof, x2_s_proof_len);
1121 bzero(x2_s_proof, x2_s_proof_len);
1124 /* Expect step 2 packet from peer */
1125 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2,
1126 input_userauth_jpake_server_step2);
1131 input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt)
1133 Authctxt *authctxt = ctxt;
1134 struct jpake_ctx *pctx = authctxt->methoddata;
1136 u_int x4_s_proof_len;
1138 /* Disable this message */
1139 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL);
1141 if ((pctx->b = BN_new()) == NULL)
1142 fatal("%s: BN_new", __func__);
1144 /* Fetch step 2 values */
1145 packet_get_bignum2(pctx->b);
1146 x4_s_proof = packet_get_string(&x4_s_proof_len);
1149 JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__));
1151 /* Derive shared key and calculate confirmation hash */
1152 jpake_key_confirm(pctx->grp, pctx->s, pctx->b,
1153 pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4,
1154 pctx->client_id, pctx->client_id_len,
1155 pctx->server_id, pctx->server_id_len,
1156 session_id2, session_id2_len,
1157 x4_s_proof, x4_s_proof_len,
1159 &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len);
1161 bzero(x4_s_proof, x4_s_proof_len);
1164 JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__));
1166 /* Send key confirmation proof */
1167 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM);
1168 packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
1171 /* Expect confirmation from peer */
1172 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM,
1173 input_userauth_jpake_server_confirm);
1178 input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt)
1180 Authctxt *authctxt = ctxt;
1181 struct jpake_ctx *pctx = authctxt->methoddata;
1183 /* Disable this message */
1184 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL);
1186 pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len);
1189 JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__));
1191 /* Verify expected confirmation hash */
1192 if (jpake_check_confirm(pctx->k,
1193 pctx->server_id, pctx->server_id_len,
1194 session_id2, session_id2_len,
1195 pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1)
1196 debug("%s: %s success", __func__, authctxt->method->name);
1198 debug("%s: confirmation mismatch", __func__);
1199 /* XXX stash this so if auth succeeds then we can warn/kill */
1202 userauth_jpake_cleanup(authctxt);
1207 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1208 u_char *data, u_int datalen)
1213 /* the agent supports this key */
1215 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1218 * we have already loaded the private key or
1219 * the private key is stored in external hardware
1221 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1222 return (key_sign(id->key, sigp, lenp, data, datalen));
1223 /* load the private key from the file */
1224 if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL)
1226 ret = key_sign(prv, sigp, lenp, data, datalen);
1232 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1235 u_char *blob, *signature;
1236 u_int bloblen, slen;
1242 fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
1243 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
1246 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1247 /* we cannot handle this key */
1248 debug3("sign_and_send_pubkey: cannot handle key");
1251 /* data to be signed */
1253 if (datafellows & SSH_OLD_SESSIONID) {
1254 buffer_append(&b, session_id2, session_id2_len);
1255 skip = session_id2_len;
1257 buffer_put_string(&b, session_id2, session_id2_len);
1258 skip = buffer_len(&b);
1260 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1261 buffer_put_cstring(&b, authctxt->server_user);
1262 buffer_put_cstring(&b,
1263 datafellows & SSH_BUG_PKSERVICE ?
1266 if (datafellows & SSH_BUG_PKAUTH) {
1267 buffer_put_char(&b, have_sig);
1269 buffer_put_cstring(&b, authctxt->method->name);
1270 buffer_put_char(&b, have_sig);
1271 buffer_put_cstring(&b, key_ssh_name(id->key));
1273 buffer_put_string(&b, blob, bloblen);
1275 /* generate signature */
1276 ret = identity_sign(id, &signature, &slen,
1277 buffer_ptr(&b), buffer_len(&b));
1286 if (datafellows & SSH_BUG_PKSERVICE) {
1288 buffer_append(&b, session_id2, session_id2_len);
1289 skip = session_id2_len;
1290 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1291 buffer_put_cstring(&b, authctxt->server_user);
1292 buffer_put_cstring(&b, authctxt->service);
1293 buffer_put_cstring(&b, authctxt->method->name);
1294 buffer_put_char(&b, have_sig);
1295 if (!(datafellows & SSH_BUG_PKAUTH))
1296 buffer_put_cstring(&b, key_ssh_name(id->key));
1297 buffer_put_string(&b, blob, bloblen);
1301 /* append signature */
1302 buffer_put_string(&b, signature, slen);
1305 /* skip session id and packet type */
1306 if (buffer_len(&b) < skip + 1)
1307 fatal("userauth_pubkey: internal error");
1308 buffer_consume(&b, skip + 1);
1310 /* put remaining data from buffer into packet */
1311 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1312 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1320 send_pubkey_test(Authctxt *authctxt, Identity *id)
1323 u_int bloblen, have_sig = 0;
1325 debug3("send_pubkey_test");
1327 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1328 /* we cannot handle this key */
1329 debug3("send_pubkey_test: cannot handle key");
1332 /* register callback for USERAUTH_PK_OK message */
1333 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1335 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1336 packet_put_cstring(authctxt->server_user);
1337 packet_put_cstring(authctxt->service);
1338 packet_put_cstring(authctxt->method->name);
1339 packet_put_char(have_sig);
1340 if (!(datafellows & SSH_BUG_PKAUTH))
1341 packet_put_cstring(key_ssh_name(id->key));
1342 packet_put_string(blob, bloblen);
1349 load_identity_file(char *filename, int userprovided)
1352 char prompt[300], *passphrase;
1353 int perm_ok = 0, quit, i;
1356 if (stat(filename, &st) < 0) {
1357 (userprovided ? logit : debug3)("no such identity: %s: %s",
1358 filename, strerror(errno));
1361 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1364 if (private == NULL) {
1365 if (options.batch_mode)
1367 snprintf(prompt, sizeof prompt,
1368 "Enter passphrase for key '%.100s': ", filename);
1369 for (i = 0; i < options.number_of_password_prompts; i++) {
1370 passphrase = read_passphrase(prompt, 0);
1371 if (strcmp(passphrase, "") != 0) {
1372 private = key_load_private_type(KEY_UNSPEC,
1373 filename, passphrase, NULL, NULL);
1376 debug2("no passphrase given, try next key");
1379 memset(passphrase, 0, strlen(passphrase));
1381 if (private != NULL || quit)
1383 debug2("bad passphrase given, try again...");
1390 * try keys in the following order:
1391 * 1. agent keys that are found in the config file
1392 * 2. other agent keys
1393 * 3. keys that are only listed in the config file
1396 pubkey_prepare(Authctxt *authctxt)
1398 Identity *id, *id2, *tmp;
1399 Idlist agent, files, *preferred;
1401 AuthenticationConnection *ac;
1405 TAILQ_INIT(&agent); /* keys from the agent */
1406 TAILQ_INIT(&files); /* keys from the config file */
1407 preferred = &authctxt->keys;
1408 TAILQ_INIT(preferred); /* preferred order of keys */
1410 /* list of keys stored in the filesystem and PKCS#11 */
1411 for (i = 0; i < options.num_identity_files; i++) {
1412 key = options.identity_keys[i];
1413 if (key && key->type == KEY_RSA1)
1415 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1417 options.identity_keys[i] = NULL;
1418 id = xcalloc(1, sizeof(*id));
1420 id->filename = xstrdup(options.identity_files[i]);
1421 id->userprovided = options.identity_file_userprovided[i];
1422 TAILQ_INSERT_TAIL(&files, id, next);
1424 /* Prefer PKCS11 keys that are explicitly listed */
1425 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1426 if (id->key == NULL || (id->key->flags & KEY_FLAG_EXT) == 0)
1429 TAILQ_FOREACH(id2, &files, next) {
1430 if (id2->key == NULL ||
1431 (id2->key->flags & KEY_FLAG_EXT) != 0)
1433 if (key_equal(id->key, id2->key)) {
1434 TAILQ_REMOVE(&files, id, next);
1435 TAILQ_INSERT_TAIL(preferred, id, next);
1440 /* If IdentitiesOnly set and key not found then don't use it */
1441 if (!found && options.identities_only) {
1442 TAILQ_REMOVE(&files, id, next);
1443 bzero(id, sizeof(id));
1447 /* list of keys supported by the agent */
1448 if ((ac = ssh_get_authentication_connection())) {
1449 for (key = ssh_get_first_identity(ac, &comment, 2);
1451 key = ssh_get_next_identity(ac, &comment, 2)) {
1453 TAILQ_FOREACH(id, &files, next) {
1454 /* agent keys from the config file are preferred */
1455 if (key_equal(key, id->key)) {
1458 TAILQ_REMOVE(&files, id, next);
1459 TAILQ_INSERT_TAIL(preferred, id, next);
1465 if (!found && !options.identities_only) {
1466 id = xcalloc(1, sizeof(*id));
1468 id->filename = comment;
1470 TAILQ_INSERT_TAIL(&agent, id, next);
1473 /* append remaining agent keys */
1474 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1475 TAILQ_REMOVE(&agent, id, next);
1476 TAILQ_INSERT_TAIL(preferred, id, next);
1478 authctxt->agent = ac;
1480 /* append remaining keys from the config file */
1481 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1482 TAILQ_REMOVE(&files, id, next);
1483 TAILQ_INSERT_TAIL(preferred, id, next);
1485 TAILQ_FOREACH(id, preferred, next) {
1486 debug2("key: %s (%p),%s", id->filename, id->key,
1487 id->userprovided ? " explicit" : "");
1492 pubkey_cleanup(Authctxt *authctxt)
1496 if (authctxt->agent != NULL)
1497 ssh_close_authentication_connection(authctxt->agent);
1498 for (id = TAILQ_FIRST(&authctxt->keys); id;
1499 id = TAILQ_FIRST(&authctxt->keys)) {
1500 TAILQ_REMOVE(&authctxt->keys, id, next);
1504 xfree(id->filename);
1510 userauth_pubkey(Authctxt *authctxt)
1515 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1518 /* move key to the end of the queue */
1519 TAILQ_REMOVE(&authctxt->keys, id, next);
1520 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1522 * send a test message if we have the public key. for
1523 * encrypted keys we cannot do this and have to load the
1524 * private key instead
1526 if (id->key && id->key->type != KEY_RSA1) {
1527 debug("Offering %s public key: %s", key_type(id->key),
1529 sent = send_pubkey_test(authctxt, id);
1530 } else if (id->key == NULL) {
1531 debug("Trying private key: %s", id->filename);
1532 id->key = load_identity_file(id->filename,
1534 if (id->key != NULL) {
1536 sent = sign_and_send_pubkey(authctxt, id);
1548 * Send userauth request message specifying keyboard-interactive method.
1551 userauth_kbdint(Authctxt *authctxt)
1553 static int attempt = 0;
1555 if (attempt++ >= options.number_of_password_prompts)
1557 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1558 if (attempt > 1 && !authctxt->info_req_seen) {
1559 debug3("userauth_kbdint: disable: no info_req_seen");
1560 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1564 debug2("userauth_kbdint");
1565 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1566 packet_put_cstring(authctxt->server_user);
1567 packet_put_cstring(authctxt->service);
1568 packet_put_cstring(authctxt->method->name);
1569 packet_put_cstring(""); /* lang */
1570 packet_put_cstring(options.kbd_interactive_devices ?
1571 options.kbd_interactive_devices : "");
1574 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1579 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1582 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1584 Authctxt *authctxt = ctxt;
1585 char *name, *inst, *lang, *prompt, *response;
1586 u_int num_prompts, i;
1589 debug2("input_userauth_info_req");
1591 if (authctxt == NULL)
1592 fatal("input_userauth_info_req: no authentication context");
1594 authctxt->info_req_seen = 1;
1596 name = packet_get_string(NULL);
1597 inst = packet_get_string(NULL);
1598 lang = packet_get_string(NULL);
1599 if (strlen(name) > 0)
1601 if (strlen(inst) > 0)
1607 num_prompts = packet_get_int();
1609 * Begin to build info response packet based on prompts requested.
1610 * We commit to providing the correct number of responses, so if
1611 * further on we run into a problem that prevents this, we have to
1612 * be sure and clean this up and send a correct error response.
1614 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1615 packet_put_int(num_prompts);
1617 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1618 for (i = 0; i < num_prompts; i++) {
1619 prompt = packet_get_string(NULL);
1620 echo = packet_get_char();
1622 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1624 packet_put_cstring(response);
1625 memset(response, 0, strlen(response));
1629 packet_check_eom(); /* done with parsing incoming message. */
1631 packet_add_padding(64);
1636 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1637 u_char *data, u_int datalen)
1642 int to[2], from[2], status, version = 2;
1644 debug2("ssh_keysign called");
1646 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1647 error("ssh_keysign: not installed: %s", strerror(errno));
1650 if (fflush(stdout) != 0)
1651 error("ssh_keysign: fflush: %s", strerror(errno));
1653 error("ssh_keysign: pipe: %s", strerror(errno));
1656 if (pipe(from) < 0) {
1657 error("ssh_keysign: pipe: %s", strerror(errno));
1660 if ((pid = fork()) < 0) {
1661 error("ssh_keysign: fork: %s", strerror(errno));
1665 /* keep the socket on exec */
1666 fcntl(packet_get_connection_in(), F_SETFD, 0);
1667 permanently_drop_suid(getuid());
1669 if (dup2(from[1], STDOUT_FILENO) < 0)
1670 fatal("ssh_keysign: dup2: %s", strerror(errno));
1672 if (dup2(to[0], STDIN_FILENO) < 0)
1673 fatal("ssh_keysign: dup2: %s", strerror(errno));
1676 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1677 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1684 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1685 buffer_put_string(&b, data, datalen);
1686 if (ssh_msg_send(to[1], version, &b) == -1)
1687 fatal("ssh_keysign: couldn't send request");
1689 if (ssh_msg_recv(from[0], &b) < 0) {
1690 error("ssh_keysign: no reply");
1697 while (waitpid(pid, &status, 0) < 0)
1701 if (buffer_get_char(&b) != version) {
1702 error("ssh_keysign: bad version");
1706 *sigp = buffer_get_string(&b, lenp);
1713 userauth_hostbased(Authctxt *authctxt)
1715 Key *private = NULL;
1716 Sensitive *sensitive = authctxt->sensitive;
1718 u_char *signature, *blob;
1719 char *chost, *pkalg, *p;
1720 const char *service;
1722 int ok, i, found = 0;
1724 /* check for a useful key */
1725 for (i = 0; i < sensitive->nkeys; i++) {
1726 private = sensitive->keys[i];
1727 if (private && private->type != KEY_RSA1) {
1729 /* we take and free the key */
1730 sensitive->keys[i] = NULL;
1735 debug("No more client hostkeys for hostbased authentication.");
1738 if (key_to_blob(private, &blob, &blen) == 0) {
1742 /* figure out a name for the client host */
1743 p = get_local_name(packet_get_connection_in());
1745 error("userauth_hostbased: cannot get local ipaddr/name");
1750 xasprintf(&chost, "%s.", p);
1751 debug2("userauth_hostbased: chost %s", chost);
1754 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1756 pkalg = xstrdup(key_ssh_name(private));
1758 /* construct data */
1759 buffer_put_string(&b, session_id2, session_id2_len);
1760 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1761 buffer_put_cstring(&b, authctxt->server_user);
1762 buffer_put_cstring(&b, service);
1763 buffer_put_cstring(&b, authctxt->method->name);
1764 buffer_put_cstring(&b, pkalg);
1765 buffer_put_string(&b, blob, blen);
1766 buffer_put_cstring(&b, chost);
1767 buffer_put_cstring(&b, authctxt->local_user);
1771 if (sensitive->external_keysign)
1772 ok = ssh_keysign(private, &signature, &slen,
1773 buffer_ptr(&b), buffer_len(&b));
1775 ok = key_sign(private, &signature, &slen,
1776 buffer_ptr(&b), buffer_len(&b));
1780 error("key_sign failed");
1786 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1787 packet_put_cstring(authctxt->server_user);
1788 packet_put_cstring(authctxt->service);
1789 packet_put_cstring(authctxt->method->name);
1790 packet_put_cstring(pkalg);
1791 packet_put_string(blob, blen);
1792 packet_put_cstring(chost);
1793 packet_put_cstring(authctxt->local_user);
1794 packet_put_string(signature, slen);
1795 memset(signature, 's', slen);
1807 userauth_jpake(Authctxt *authctxt)
1809 struct jpake_ctx *pctx;
1810 u_char *x1_proof, *x2_proof;
1811 u_int x1_proof_len, x2_proof_len;
1812 static int attempt = 0; /* XXX share with userauth_password's? */
1814 if (attempt++ >= options.number_of_password_prompts)
1817 error("Permission denied, please try again.");
1819 if (authctxt->methoddata != NULL)
1820 fatal("%s: authctxt->methoddata already set (%p)",
1821 __func__, authctxt->methoddata);
1823 authctxt->methoddata = pctx = jpake_new();
1826 * Send request immediately, to get the protocol going while
1827 * we do the initial computations.
1829 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1830 packet_put_cstring(authctxt->server_user);
1831 packet_put_cstring(authctxt->service);
1832 packet_put_cstring(authctxt->method->name);
1834 packet_write_wait();
1836 jpake_step1(pctx->grp,
1837 &pctx->client_id, &pctx->client_id_len,
1838 &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2,
1839 &x1_proof, &x1_proof_len,
1840 &x2_proof, &x2_proof_len);
1842 JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__));
1844 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1);
1845 packet_put_string(pctx->client_id, pctx->client_id_len);
1846 packet_put_bignum2(pctx->g_x1);
1847 packet_put_bignum2(pctx->g_x2);
1848 packet_put_string(x1_proof, x1_proof_len);
1849 packet_put_string(x2_proof, x2_proof_len);
1852 bzero(x1_proof, x1_proof_len);
1853 bzero(x2_proof, x2_proof_len);
1857 /* Expect step 1 packet from peer */
1858 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1,
1859 input_userauth_jpake_server_step1);
1860 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS,
1861 &input_userauth_success_unexpected);
1867 userauth_jpake_cleanup(Authctxt *authctxt)
1869 debug3("%s: clean up", __func__);
1870 if (authctxt->methoddata != NULL) {
1871 jpake_free(authctxt->methoddata);
1872 authctxt->methoddata = NULL;
1874 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
1878 /* find auth method */
1881 * given auth method name, if configurable options permit this method fill
1882 * in auth_ident field and return true, otherwise return false.
1885 authmethod_is_enabled(Authmethod *method)
1889 /* return false if options indicate this method is disabled */
1890 if (method->enabled == NULL || *method->enabled == 0)
1892 /* return false if batch mode is enabled but method needs interactive mode */
1893 if (method->batch_flag != NULL && *method->batch_flag != 0)
1899 authmethod_lookup(const char *name)
1901 Authmethod *method = NULL;
1903 for (method = authmethods; method->name != NULL; method++)
1904 if (strcmp(name, method->name) == 0)
1906 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1910 /* XXX internal state */
1911 static Authmethod *current = NULL;
1912 static char *supported = NULL;
1913 static char *preferred = NULL;
1916 * Given the authentication method list sent by the server, return the
1917 * next method we should try. If the server initially sends a nil list,
1918 * use a built-in default list.
1921 authmethod_get(char *authlist)
1926 /* Use a suitable default if we're passed a nil list. */
1927 if (authlist == NULL || strlen(authlist) == 0)
1928 authlist = options.preferred_authentications;
1930 if (supported == NULL || strcmp(authlist, supported) != 0) {
1931 debug3("start over, passed a different list %s", authlist);
1932 if (supported != NULL)
1934 supported = xstrdup(authlist);
1935 preferred = options.preferred_authentications;
1936 debug3("preferred %s", preferred);
1938 } else if (current != NULL && authmethod_is_enabled(current))
1942 if ((name = match_list(preferred, supported, &next)) == NULL) {
1943 debug("No more authentication methods to try.");
1948 debug3("authmethod_lookup %s", name);
1949 debug3("remaining preferred: %s", preferred);
1950 if ((current = authmethod_lookup(name)) != NULL &&
1951 authmethod_is_enabled(current)) {
1952 debug3("authmethod_is_enabled %s", name);
1953 debug("Next authentication method: %s", name);
1961 authmethods_get(void)
1963 Authmethod *method = NULL;
1968 for (method = authmethods; method->name != NULL; method++) {
1969 if (authmethod_is_enabled(method)) {
1970 if (buffer_len(&b) > 0)
1971 buffer_append(&b, ",", 1);
1972 buffer_append(&b, method->name, strlen(method->name));
1975 buffer_append(&b, "\0", 1);
1976 list = xstrdup(buffer_ptr(&b));