1 /* $OpenBSD: sshconnect2.c,v 1.198 2013/06/05 12:52:38 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 || options.rekey_interval)
212 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
213 (time_t)options.rekey_interval);
215 /* start key exchange */
216 kex = kex_setup(myproposal);
217 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
218 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
219 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
220 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
221 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
222 kex->client_version_string=client_version_string;
223 kex->server_version_string=server_version_string;
224 kex->verify_host_key=&verify_host_key_callback;
228 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
230 if (options.use_roaming && !kex->roaming) {
231 debug("Roaming not allowed by server");
232 options.use_roaming = 0;
235 session_id2 = kex->session_id;
236 session_id2_len = kex->session_id_len;
239 /* send 1st encrypted/maced/compressed message */
240 packet_start(SSH2_MSG_IGNORE);
241 packet_put_cstring("markus");
251 typedef struct Authctxt Authctxt;
252 typedef struct Authmethod Authmethod;
253 typedef struct identity Identity;
254 typedef struct idlist Idlist;
257 TAILQ_ENTRY(identity) next;
258 AuthenticationConnection *ac; /* set if agent supports key */
259 Key *key; /* public/private key */
260 char *filename; /* comment for agent-only keys */
262 int isprivate; /* key points to the private key */
265 TAILQ_HEAD(idlist, identity);
268 const char *server_user;
269 const char *local_user;
273 sig_atomic_t success;
277 AuthenticationConnection *agent;
279 Sensitive *sensitive;
280 /* kbd-interactive */
286 char *name; /* string to compare against server's list */
287 int (*userauth)(Authctxt *authctxt);
288 void (*cleanup)(Authctxt *authctxt);
289 int *enabled; /* flag in option struct that enables method */
290 int *batch_flag; /* flag in option struct that disables method */
293 void input_userauth_success(int, u_int32_t, void *);
294 void input_userauth_success_unexpected(int, u_int32_t, void *);
295 void input_userauth_failure(int, u_int32_t, void *);
296 void input_userauth_banner(int, u_int32_t, void *);
297 void input_userauth_error(int, u_int32_t, void *);
298 void input_userauth_info_req(int, u_int32_t, void *);
299 void input_userauth_pk_ok(int, u_int32_t, void *);
300 void input_userauth_passwd_changereq(int, u_int32_t, void *);
301 void input_userauth_jpake_server_step1(int, u_int32_t, void *);
302 void input_userauth_jpake_server_step2(int, u_int32_t, void *);
303 void input_userauth_jpake_server_confirm(int, u_int32_t, void *);
305 int userauth_none(Authctxt *);
306 int userauth_pubkey(Authctxt *);
307 int userauth_passwd(Authctxt *);
308 int userauth_kbdint(Authctxt *);
309 int userauth_hostbased(Authctxt *);
310 int userauth_jpake(Authctxt *);
312 void userauth_jpake_cleanup(Authctxt *);
315 int userauth_gssapi(Authctxt *authctxt);
316 void input_gssapi_response(int type, u_int32_t, void *);
317 void input_gssapi_token(int type, u_int32_t, void *);
318 void input_gssapi_hash(int type, u_int32_t, void *);
319 void input_gssapi_error(int, u_int32_t, void *);
320 void input_gssapi_errtok(int, u_int32_t, void *);
323 void userauth(Authctxt *, char *);
325 static int sign_and_send_pubkey(Authctxt *, Identity *);
326 static void pubkey_prepare(Authctxt *);
327 static void pubkey_cleanup(Authctxt *);
328 static Key *load_identity_file(char *, int);
330 static Authmethod *authmethod_get(char *authlist);
331 static Authmethod *authmethod_lookup(const char *name);
332 static char *authmethods_get(void);
334 Authmethod authmethods[] = {
339 &options.gss_authentication,
345 &options.hostbased_authentication,
350 &options.pubkey_authentication,
353 {"jpake-01@openssh.com",
355 userauth_jpake_cleanup,
356 &options.zero_knowledge_password_authentication,
357 &options.batch_mode},
359 {"keyboard-interactive",
362 &options.kbd_interactive_authentication,
363 &options.batch_mode},
367 &options.password_authentication,
368 &options.batch_mode},
374 {NULL, NULL, NULL, NULL, NULL}
378 ssh_userauth2(const char *local_user, const char *server_user, char *host,
379 Sensitive *sensitive)
384 if (options.challenge_response_authentication)
385 options.kbd_interactive_authentication = 1;
387 packet_start(SSH2_MSG_SERVICE_REQUEST);
388 packet_put_cstring("ssh-userauth");
390 debug("SSH2_MSG_SERVICE_REQUEST sent");
392 type = packet_read();
393 if (type != SSH2_MSG_SERVICE_ACCEPT)
394 fatal("Server denied authentication request: %d", type);
395 if (packet_remaining() > 0) {
396 char *reply = packet_get_string(NULL);
397 debug2("service_accept: %s", reply);
400 debug2("buggy server: service_accept w/o service");
403 debug("SSH2_MSG_SERVICE_ACCEPT received");
405 if (options.preferred_authentications == NULL)
406 options.preferred_authentications = authmethods_get();
408 /* setup authentication context */
409 memset(&authctxt, 0, sizeof(authctxt));
410 pubkey_prepare(&authctxt);
411 authctxt.server_user = server_user;
412 authctxt.local_user = local_user;
413 authctxt.host = host;
414 authctxt.service = "ssh-connection"; /* service name */
415 authctxt.success = 0;
416 authctxt.method = authmethod_lookup("none");
417 authctxt.authlist = NULL;
418 authctxt.methoddata = NULL;
419 authctxt.sensitive = sensitive;
420 authctxt.info_req_seen = 0;
421 if (authctxt.method == NULL)
422 fatal("ssh_userauth2: internal error: cannot send userauth none request");
424 /* initial userauth request */
425 userauth_none(&authctxt);
427 dispatch_init(&input_userauth_error);
428 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
429 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
430 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
431 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
433 pubkey_cleanup(&authctxt);
434 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
436 #ifdef NONE_CIPHER_ENABLED
438 * If the user explicitly requests to use the none cipher enable it
439 * post authentication and only if the right conditions are met: both
440 * of the NONE switches must be true and there must be no tty allocated.
442 if (options.none_switch == 1 && options.none_enabled == 1) {
444 debug("Requesting none cipher re-keying...");
445 myproposal[PROPOSAL_ENC_ALGS_STOC] = "none";
446 myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none";
447 kex_prop2buf(&xxx_kex->my, myproposal);
448 packet_request_rekeying();
449 fprintf(stderr, "WARNING: enabled NONE cipher\n");
451 /* Requested NONE cipher on an interactive session. */
452 debug("Cannot switch to NONE cipher with tty "
454 fprintf(stderr, "NONE cipher switch disabled given "
455 "a TTY is allocated\n");
459 debug("Authentication succeeded (%s).", authctxt.method->name);
463 userauth(Authctxt *authctxt, char *authlist)
465 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
466 authctxt->method->cleanup(authctxt);
468 free(authctxt->methoddata);
469 authctxt->methoddata = NULL;
470 if (authlist == NULL) {
471 authlist = authctxt->authlist;
473 free(authctxt->authlist);
474 authctxt->authlist = authlist;
477 Authmethod *method = authmethod_get(authlist);
479 fatal("Permission denied (%s).", authlist);
480 authctxt->method = method;
482 /* reset the per method handler */
483 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
484 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
486 /* and try new method */
487 if (method->userauth(authctxt) != 0) {
488 debug2("we sent a %s packet, wait for reply", method->name);
491 debug2("we did not send a packet, disable method");
492 method->enabled = NULL;
499 input_userauth_error(int type, u_int32_t seq, void *ctxt)
501 fatal("input_userauth_error: bad message during authentication: "
507 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
509 char *msg, *raw, *lang;
512 debug3("input_userauth_banner");
513 raw = packet_get_string(&len);
514 lang = packet_get_string(NULL);
515 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
518 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
519 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
520 fprintf(stderr, "%s", msg);
529 input_userauth_success(int type, u_int32_t seq, void *ctxt)
531 Authctxt *authctxt = ctxt;
533 if (authctxt == NULL)
534 fatal("input_userauth_success: no authentication context");
535 free(authctxt->authlist);
536 authctxt->authlist = NULL;
537 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
538 authctxt->method->cleanup(authctxt);
539 free(authctxt->methoddata);
540 authctxt->methoddata = NULL;
541 authctxt->success = 1; /* break out */
545 input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
547 Authctxt *authctxt = ctxt;
549 if (authctxt == NULL)
550 fatal("%s: no authentication context", __func__);
552 fatal("Unexpected authentication success during %s.",
553 authctxt->method->name);
558 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
560 Authctxt *authctxt = ctxt;
561 char *authlist = NULL;
564 if (authctxt == NULL)
565 fatal("input_userauth_failure: no authentication context");
567 authlist = packet_get_string(NULL);
568 partial = packet_get_char();
572 logit("Authenticated with partial success.");
574 pubkey_cleanup(authctxt);
575 pubkey_prepare(authctxt);
577 debug("Authentications that can continue: %s", authlist);
579 userauth(authctxt, authlist);
584 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
586 Authctxt *authctxt = ctxt;
590 int pktype, sent = 0;
595 if (authctxt == NULL)
596 fatal("input_userauth_pk_ok: no authentication context");
597 if (datafellows & SSH_BUG_PKOK) {
598 /* this is similar to SSH_BUG_PKAUTH */
599 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
600 pkblob = packet_get_string(&blen);
602 buffer_append(&b, pkblob, blen);
603 pkalg = buffer_get_string(&b, &alen);
606 pkalg = packet_get_string(&alen);
607 pkblob = packet_get_string(&blen);
611 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
613 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
614 debug("unknown pkalg %s", pkalg);
617 if ((key = key_from_blob(pkblob, blen)) == NULL) {
618 debug("no key from blob. pkalg %s", pkalg);
621 if (key->type != pktype) {
622 error("input_userauth_pk_ok: type mismatch "
623 "for decoded key (received %d, expected %d)",
627 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
628 debug2("input_userauth_pk_ok: fp %s", fp);
632 * search keys in the reverse order, because last candidate has been
633 * moved to the end of the queue. this also avoids confusion by
636 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
637 if (key_equal(key, id->key)) {
638 sent = sign_and_send_pubkey(authctxt, id);
648 /* try another method if we did not send a packet */
650 userauth(authctxt, NULL);
655 userauth_gssapi(Authctxt *authctxt)
657 Gssctxt *gssctxt = NULL;
658 static gss_OID_set gss_supported = NULL;
659 static u_int mech = 0;
663 /* Try one GSSAPI method at a time, rather than sending them all at
666 if (gss_supported == NULL)
667 gss_indicate_mechs(&min, &gss_supported);
669 /* Check to see if the mechanism is usable before we offer it */
670 while (mech < gss_supported->count && !ok) {
671 /* My DER encoding requires length<128 */
672 if (gss_supported->elements[mech].length < 128 &&
673 ssh_gssapi_check_mechanism(&gssctxt,
674 &gss_supported->elements[mech], authctxt->host)) {
675 ok = 1; /* Mechanism works */
684 authctxt->methoddata=(void *)gssctxt;
686 packet_start(SSH2_MSG_USERAUTH_REQUEST);
687 packet_put_cstring(authctxt->server_user);
688 packet_put_cstring(authctxt->service);
689 packet_put_cstring(authctxt->method->name);
693 packet_put_int((gss_supported->elements[mech].length) + 2);
694 packet_put_char(SSH_GSS_OIDTYPE);
695 packet_put_char(gss_supported->elements[mech].length);
696 packet_put_raw(gss_supported->elements[mech].elements,
697 gss_supported->elements[mech].length);
701 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
702 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
703 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
704 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
706 mech++; /* Move along to next candidate */
712 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
714 Authctxt *authctxt = ctxt;
715 Gssctxt *gssctxt = authctxt->methoddata;
716 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
717 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
718 gss_buffer_desc gssbuf;
719 OM_uint32 status, ms, flags;
722 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
723 recv_tok, &send_tok, &flags);
725 if (send_tok.length > 0) {
726 if (GSS_ERROR(status))
727 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
729 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
731 packet_put_string(send_tok.value, send_tok.length);
733 gss_release_buffer(&ms, &send_tok);
736 if (status == GSS_S_COMPLETE) {
737 /* send either complete or MIC, depending on mechanism */
738 if (!(flags & GSS_C_INTEG_FLAG)) {
739 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
742 ssh_gssapi_buildmic(&b, authctxt->server_user,
743 authctxt->service, "gssapi-with-mic");
745 gssbuf.value = buffer_ptr(&b);
746 gssbuf.length = buffer_len(&b);
748 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
750 if (!GSS_ERROR(status)) {
751 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
752 packet_put_string(mic.value, mic.length);
758 gss_release_buffer(&ms, &mic);
767 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
769 Authctxt *authctxt = ctxt;
774 if (authctxt == NULL)
775 fatal("input_gssapi_response: no authentication context");
776 gssctxt = authctxt->methoddata;
779 oidv = packet_get_string(&oidlen);
782 oidv[0] != SSH_GSS_OIDTYPE ||
783 oidv[1] != oidlen - 2) {
785 debug("Badly encoded mechanism OID received");
786 userauth(authctxt, NULL);
790 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
791 fatal("Server returned different OID than expected");
797 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
798 /* Start again with next method on list */
799 debug("Trying to start again");
800 userauth(authctxt, NULL);
807 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
809 Authctxt *authctxt = ctxt;
810 gss_buffer_desc recv_tok;
814 if (authctxt == NULL)
815 fatal("input_gssapi_response: no authentication context");
817 recv_tok.value = packet_get_string(&slen);
818 recv_tok.length = slen; /* safe typecast */
822 status = process_gssapi_token(ctxt, &recv_tok);
824 free(recv_tok.value);
826 if (GSS_ERROR(status)) {
827 /* Start again with the next method in the list */
828 userauth(authctxt, NULL);
835 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
837 Authctxt *authctxt = ctxt;
839 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
840 gss_buffer_desc recv_tok;
844 if (authctxt == NULL)
845 fatal("input_gssapi_response: no authentication context");
846 gssctxt = authctxt->methoddata;
848 recv_tok.value = packet_get_string(&len);
849 recv_tok.length = len;
853 /* Stick it into GSSAPI and see what it says */
854 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
855 &recv_tok, &send_tok, NULL);
857 free(recv_tok.value);
858 gss_release_buffer(&ms, &send_tok);
860 /* Server will be returning a failed packet after this one */
865 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
870 /* maj */(void)packet_get_int();
871 /* min */(void)packet_get_int();
872 msg=packet_get_string(NULL);
873 lang=packet_get_string(NULL);
877 debug("Server GSSAPI Error:\n%s", msg);
884 userauth_none(Authctxt *authctxt)
886 /* initial userauth request */
887 packet_start(SSH2_MSG_USERAUTH_REQUEST);
888 packet_put_cstring(authctxt->server_user);
889 packet_put_cstring(authctxt->service);
890 packet_put_cstring(authctxt->method->name);
896 userauth_passwd(Authctxt *authctxt)
898 static int attempt = 0;
901 const char *host = options.host_key_alias ? options.host_key_alias :
904 if (attempt++ >= options.number_of_password_prompts)
908 error("Permission denied, please try again.");
910 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
911 authctxt->server_user, host);
912 password = read_passphrase(prompt, 0);
913 packet_start(SSH2_MSG_USERAUTH_REQUEST);
914 packet_put_cstring(authctxt->server_user);
915 packet_put_cstring(authctxt->service);
916 packet_put_cstring(authctxt->method->name);
918 packet_put_cstring(password);
919 memset(password, 0, strlen(password));
921 packet_add_padding(64);
924 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
925 &input_userauth_passwd_changereq);
931 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
935 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
937 Authctxt *authctxt = ctxt;
938 char *info, *lang, *password = NULL, *retype = NULL;
940 const char *host = options.host_key_alias ? options.host_key_alias :
943 debug2("input_userauth_passwd_changereq");
945 if (authctxt == NULL)
946 fatal("input_userauth_passwd_changereq: "
947 "no authentication context");
949 info = packet_get_string(NULL);
950 lang = packet_get_string(NULL);
951 if (strlen(info) > 0)
955 packet_start(SSH2_MSG_USERAUTH_REQUEST);
956 packet_put_cstring(authctxt->server_user);
957 packet_put_cstring(authctxt->service);
958 packet_put_cstring(authctxt->method->name);
959 packet_put_char(1); /* additional info */
960 snprintf(prompt, sizeof(prompt),
961 "Enter %.30s@%.128s's old password: ",
962 authctxt->server_user, host);
963 password = read_passphrase(prompt, 0);
964 packet_put_cstring(password);
965 memset(password, 0, strlen(password));
968 while (password == NULL) {
969 snprintf(prompt, sizeof(prompt),
970 "Enter %.30s@%.128s's new password: ",
971 authctxt->server_user, host);
972 password = read_passphrase(prompt, RP_ALLOW_EOF);
973 if (password == NULL) {
977 snprintf(prompt, sizeof(prompt),
978 "Retype %.30s@%.128s's new password: ",
979 authctxt->server_user, host);
980 retype = read_passphrase(prompt, 0);
981 if (strcmp(password, retype) != 0) {
982 memset(password, 0, strlen(password));
984 logit("Mismatch; try again, EOF to quit.");
987 memset(retype, 0, strlen(retype));
990 packet_put_cstring(password);
991 memset(password, 0, strlen(password));
993 packet_add_padding(64);
996 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
997 &input_userauth_passwd_changereq);
1002 pw_encrypt(const char *password, const char *crypt_scheme, const char *salt)
1004 /* OpenBSD crypt(3) handles all of these */
1005 if (strcmp(crypt_scheme, "crypt") == 0 ||
1006 strcmp(crypt_scheme, "bcrypt") == 0 ||
1007 strcmp(crypt_scheme, "md5crypt") == 0 ||
1008 strcmp(crypt_scheme, "crypt-extended") == 0)
1009 return xstrdup(crypt(password, salt));
1010 error("%s: unsupported password encryption scheme \"%.100s\"",
1011 __func__, crypt_scheme);
1016 jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme,
1019 char prompt[256], *password, *crypted;
1024 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ",
1025 authctxt->server_user, authctxt->host);
1026 password = read_passphrase(prompt, 0);
1028 if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) {
1029 logit("Disabling %s authentication", authctxt->method->name);
1030 authctxt->method->enabled = NULL;
1031 /* Continue with an empty password to fail gracefully */
1032 crypted = xstrdup("");
1036 debug3("%s: salt = %s", __func__, salt);
1037 debug3("%s: scheme = %s", __func__, crypt_scheme);
1038 debug3("%s: crypted = %s", __func__, crypted);
1041 if (hash_buffer(crypted, strlen(crypted), EVP_sha256(),
1042 &secret, &secret_len) != 0)
1043 fatal("%s: hash_buffer", __func__);
1045 bzero(password, strlen(password));
1046 bzero(crypted, strlen(crypted));
1050 if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL)
1051 fatal("%s: BN_bin2bn (secret)", __func__);
1052 bzero(secret, secret_len);
1060 input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt)
1062 Authctxt *authctxt = ctxt;
1063 struct jpake_ctx *pctx = authctxt->methoddata;
1064 u_char *x3_proof, *x4_proof, *x2_s_proof;
1065 u_int x3_proof_len, x4_proof_len, x2_s_proof_len;
1066 char *crypt_scheme, *salt;
1068 /* Disable this message */
1069 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL);
1071 if ((pctx->g_x3 = BN_new()) == NULL ||
1072 (pctx->g_x4 = BN_new()) == NULL)
1073 fatal("%s: BN_new", __func__);
1075 /* Fetch step 1 values */
1076 crypt_scheme = packet_get_string(NULL);
1077 salt = packet_get_string(NULL);
1078 pctx->server_id = packet_get_string(&pctx->server_id_len);
1079 packet_get_bignum2(pctx->g_x3);
1080 packet_get_bignum2(pctx->g_x4);
1081 x3_proof = packet_get_string(&x3_proof_len);
1082 x4_proof = packet_get_string(&x4_proof_len);
1085 JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__));
1087 /* Obtain password and derive secret */
1088 pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt);
1089 bzero(crypt_scheme, strlen(crypt_scheme));
1090 bzero(salt, strlen(salt));
1093 JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__));
1095 /* Calculate step 2 values */
1096 jpake_step2(pctx->grp, pctx->s, pctx->g_x1,
1097 pctx->g_x3, pctx->g_x4, pctx->x2,
1098 pctx->server_id, pctx->server_id_len,
1099 pctx->client_id, pctx->client_id_len,
1100 x3_proof, x3_proof_len,
1101 x4_proof, x4_proof_len,
1103 &x2_s_proof, &x2_s_proof_len);
1105 bzero(x3_proof, x3_proof_len);
1106 bzero(x4_proof, x4_proof_len);
1110 JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__));
1112 /* Send values for step 2 */
1113 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2);
1114 packet_put_bignum2(pctx->a);
1115 packet_put_string(x2_s_proof, x2_s_proof_len);
1118 bzero(x2_s_proof, x2_s_proof_len);
1121 /* Expect step 2 packet from peer */
1122 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2,
1123 input_userauth_jpake_server_step2);
1128 input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt)
1130 Authctxt *authctxt = ctxt;
1131 struct jpake_ctx *pctx = authctxt->methoddata;
1133 u_int x4_s_proof_len;
1135 /* Disable this message */
1136 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL);
1138 if ((pctx->b = BN_new()) == NULL)
1139 fatal("%s: BN_new", __func__);
1141 /* Fetch step 2 values */
1142 packet_get_bignum2(pctx->b);
1143 x4_s_proof = packet_get_string(&x4_s_proof_len);
1146 JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__));
1148 /* Derive shared key and calculate confirmation hash */
1149 jpake_key_confirm(pctx->grp, pctx->s, pctx->b,
1150 pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4,
1151 pctx->client_id, pctx->client_id_len,
1152 pctx->server_id, pctx->server_id_len,
1153 session_id2, session_id2_len,
1154 x4_s_proof, x4_s_proof_len,
1156 &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len);
1158 bzero(x4_s_proof, x4_s_proof_len);
1161 JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__));
1163 /* Send key confirmation proof */
1164 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM);
1165 packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
1168 /* Expect confirmation from peer */
1169 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM,
1170 input_userauth_jpake_server_confirm);
1175 input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt)
1177 Authctxt *authctxt = ctxt;
1178 struct jpake_ctx *pctx = authctxt->methoddata;
1180 /* Disable this message */
1181 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL);
1183 pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len);
1186 JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__));
1188 /* Verify expected confirmation hash */
1189 if (jpake_check_confirm(pctx->k,
1190 pctx->server_id, pctx->server_id_len,
1191 session_id2, session_id2_len,
1192 pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1)
1193 debug("%s: %s success", __func__, authctxt->method->name);
1195 debug("%s: confirmation mismatch", __func__);
1196 /* XXX stash this so if auth succeeds then we can warn/kill */
1199 userauth_jpake_cleanup(authctxt);
1204 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1205 u_char *data, u_int datalen)
1210 /* the agent supports this key */
1212 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1215 * we have already loaded the private key or
1216 * the private key is stored in external hardware
1218 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1219 return (key_sign(id->key, sigp, lenp, data, datalen));
1220 /* load the private key from the file */
1221 if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL)
1223 ret = key_sign(prv, sigp, lenp, data, datalen);
1229 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1232 u_char *blob, *signature;
1233 u_int bloblen, slen;
1239 fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
1240 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
1243 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1244 /* we cannot handle this key */
1245 debug3("sign_and_send_pubkey: cannot handle key");
1248 /* data to be signed */
1250 if (datafellows & SSH_OLD_SESSIONID) {
1251 buffer_append(&b, session_id2, session_id2_len);
1252 skip = session_id2_len;
1254 buffer_put_string(&b, session_id2, session_id2_len);
1255 skip = buffer_len(&b);
1257 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1258 buffer_put_cstring(&b, authctxt->server_user);
1259 buffer_put_cstring(&b,
1260 datafellows & SSH_BUG_PKSERVICE ?
1263 if (datafellows & SSH_BUG_PKAUTH) {
1264 buffer_put_char(&b, have_sig);
1266 buffer_put_cstring(&b, authctxt->method->name);
1267 buffer_put_char(&b, have_sig);
1268 buffer_put_cstring(&b, key_ssh_name(id->key));
1270 buffer_put_string(&b, blob, bloblen);
1272 /* generate signature */
1273 ret = identity_sign(id, &signature, &slen,
1274 buffer_ptr(&b), buffer_len(&b));
1283 if (datafellows & SSH_BUG_PKSERVICE) {
1285 buffer_append(&b, session_id2, session_id2_len);
1286 skip = session_id2_len;
1287 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1288 buffer_put_cstring(&b, authctxt->server_user);
1289 buffer_put_cstring(&b, authctxt->service);
1290 buffer_put_cstring(&b, authctxt->method->name);
1291 buffer_put_char(&b, have_sig);
1292 if (!(datafellows & SSH_BUG_PKAUTH))
1293 buffer_put_cstring(&b, key_ssh_name(id->key));
1294 buffer_put_string(&b, blob, bloblen);
1298 /* append signature */
1299 buffer_put_string(&b, signature, slen);
1302 /* skip session id and packet type */
1303 if (buffer_len(&b) < skip + 1)
1304 fatal("userauth_pubkey: internal error");
1305 buffer_consume(&b, skip + 1);
1307 /* put remaining data from buffer into packet */
1308 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1309 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1317 send_pubkey_test(Authctxt *authctxt, Identity *id)
1320 u_int bloblen, have_sig = 0;
1322 debug3("send_pubkey_test");
1324 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1325 /* we cannot handle this key */
1326 debug3("send_pubkey_test: cannot handle key");
1329 /* register callback for USERAUTH_PK_OK message */
1330 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1332 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1333 packet_put_cstring(authctxt->server_user);
1334 packet_put_cstring(authctxt->service);
1335 packet_put_cstring(authctxt->method->name);
1336 packet_put_char(have_sig);
1337 if (!(datafellows & SSH_BUG_PKAUTH))
1338 packet_put_cstring(key_ssh_name(id->key));
1339 packet_put_string(blob, bloblen);
1346 load_identity_file(char *filename, int userprovided)
1349 char prompt[300], *passphrase;
1350 int perm_ok = 0, quit, i;
1353 if (stat(filename, &st) < 0) {
1354 (userprovided ? logit : debug3)("no such identity: %s: %s",
1355 filename, strerror(errno));
1358 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1360 if (private != NULL)
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);
1509 userauth_pubkey(Authctxt *authctxt)
1514 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1517 /* move key to the end of the queue */
1518 TAILQ_REMOVE(&authctxt->keys, id, next);
1519 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1521 * send a test message if we have the public key. for
1522 * encrypted keys we cannot do this and have to load the
1523 * private key instead
1525 if (id->key && id->key->type != KEY_RSA1) {
1526 debug("Offering %s public key: %s", key_type(id->key),
1528 sent = send_pubkey_test(authctxt, id);
1529 } else if (id->key == NULL) {
1530 debug("Trying private key: %s", id->filename);
1531 id->key = load_identity_file(id->filename,
1533 if (id->key != NULL) {
1535 sent = sign_and_send_pubkey(authctxt, id);
1547 * Send userauth request message specifying keyboard-interactive method.
1550 userauth_kbdint(Authctxt *authctxt)
1552 static int attempt = 0;
1554 if (attempt++ >= options.number_of_password_prompts)
1556 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1557 if (attempt > 1 && !authctxt->info_req_seen) {
1558 debug3("userauth_kbdint: disable: no info_req_seen");
1559 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1563 debug2("userauth_kbdint");
1564 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1565 packet_put_cstring(authctxt->server_user);
1566 packet_put_cstring(authctxt->service);
1567 packet_put_cstring(authctxt->method->name);
1568 packet_put_cstring(""); /* lang */
1569 packet_put_cstring(options.kbd_interactive_devices ?
1570 options.kbd_interactive_devices : "");
1573 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1578 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1581 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1583 Authctxt *authctxt = ctxt;
1584 char *name, *inst, *lang, *prompt, *response;
1585 u_int num_prompts, i;
1588 debug2("input_userauth_info_req");
1590 if (authctxt == NULL)
1591 fatal("input_userauth_info_req: no authentication context");
1593 authctxt->info_req_seen = 1;
1595 name = packet_get_string(NULL);
1596 inst = packet_get_string(NULL);
1597 lang = packet_get_string(NULL);
1598 if (strlen(name) > 0)
1600 if (strlen(inst) > 0)
1606 num_prompts = packet_get_int();
1608 * Begin to build info response packet based on prompts requested.
1609 * We commit to providing the correct number of responses, so if
1610 * further on we run into a problem that prevents this, we have to
1611 * be sure and clean this up and send a correct error response.
1613 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1614 packet_put_int(num_prompts);
1616 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1617 for (i = 0; i < num_prompts; i++) {
1618 prompt = packet_get_string(NULL);
1619 echo = packet_get_char();
1621 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1623 packet_put_cstring(response);
1624 memset(response, 0, strlen(response));
1628 packet_check_eom(); /* done with parsing incoming message. */
1630 packet_add_padding(64);
1635 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1636 u_char *data, u_int datalen)
1641 int to[2], from[2], status, version = 2;
1643 debug2("ssh_keysign called");
1645 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1646 error("ssh_keysign: not installed: %s", strerror(errno));
1649 if (fflush(stdout) != 0)
1650 error("ssh_keysign: fflush: %s", strerror(errno));
1652 error("ssh_keysign: pipe: %s", strerror(errno));
1655 if (pipe(from) < 0) {
1656 error("ssh_keysign: pipe: %s", strerror(errno));
1659 if ((pid = fork()) < 0) {
1660 error("ssh_keysign: fork: %s", strerror(errno));
1664 /* keep the socket on exec */
1665 fcntl(packet_get_connection_in(), F_SETFD, 0);
1666 permanently_drop_suid(getuid());
1668 if (dup2(from[1], STDOUT_FILENO) < 0)
1669 fatal("ssh_keysign: dup2: %s", strerror(errno));
1671 if (dup2(to[0], STDIN_FILENO) < 0)
1672 fatal("ssh_keysign: dup2: %s", strerror(errno));
1675 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1676 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1683 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1684 buffer_put_string(&b, data, datalen);
1685 if (ssh_msg_send(to[1], version, &b) == -1)
1686 fatal("ssh_keysign: couldn't send request");
1688 if (ssh_msg_recv(from[0], &b) < 0) {
1689 error("ssh_keysign: no reply");
1696 while (waitpid(pid, &status, 0) < 0)
1700 if (buffer_get_char(&b) != version) {
1701 error("ssh_keysign: bad version");
1705 *sigp = buffer_get_string(&b, lenp);
1712 userauth_hostbased(Authctxt *authctxt)
1714 Key *private = NULL;
1715 Sensitive *sensitive = authctxt->sensitive;
1717 u_char *signature, *blob;
1718 char *chost, *pkalg, *p;
1719 const char *service;
1721 int ok, i, found = 0;
1723 /* check for a useful key */
1724 for (i = 0; i < sensitive->nkeys; i++) {
1725 private = sensitive->keys[i];
1726 if (private && private->type != KEY_RSA1) {
1728 /* we take and free the key */
1729 sensitive->keys[i] = NULL;
1734 debug("No more client hostkeys for hostbased authentication.");
1737 if (key_to_blob(private, &blob, &blen) == 0) {
1741 /* figure out a name for the client host */
1742 p = get_local_name(packet_get_connection_in());
1744 error("userauth_hostbased: cannot get local ipaddr/name");
1749 xasprintf(&chost, "%s.", p);
1750 debug2("userauth_hostbased: chost %s", chost);
1753 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1755 pkalg = xstrdup(key_ssh_name(private));
1757 /* construct data */
1758 buffer_put_string(&b, session_id2, session_id2_len);
1759 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1760 buffer_put_cstring(&b, authctxt->server_user);
1761 buffer_put_cstring(&b, service);
1762 buffer_put_cstring(&b, authctxt->method->name);
1763 buffer_put_cstring(&b, pkalg);
1764 buffer_put_string(&b, blob, blen);
1765 buffer_put_cstring(&b, chost);
1766 buffer_put_cstring(&b, authctxt->local_user);
1770 if (sensitive->external_keysign)
1771 ok = ssh_keysign(private, &signature, &slen,
1772 buffer_ptr(&b), buffer_len(&b));
1774 ok = key_sign(private, &signature, &slen,
1775 buffer_ptr(&b), buffer_len(&b));
1779 error("key_sign failed");
1785 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1786 packet_put_cstring(authctxt->server_user);
1787 packet_put_cstring(authctxt->service);
1788 packet_put_cstring(authctxt->method->name);
1789 packet_put_cstring(pkalg);
1790 packet_put_string(blob, blen);
1791 packet_put_cstring(chost);
1792 packet_put_cstring(authctxt->local_user);
1793 packet_put_string(signature, slen);
1794 memset(signature, 's', slen);
1806 userauth_jpake(Authctxt *authctxt)
1808 struct jpake_ctx *pctx;
1809 u_char *x1_proof, *x2_proof;
1810 u_int x1_proof_len, x2_proof_len;
1811 static int attempt = 0; /* XXX share with userauth_password's? */
1813 if (attempt++ >= options.number_of_password_prompts)
1816 error("Permission denied, please try again.");
1818 if (authctxt->methoddata != NULL)
1819 fatal("%s: authctxt->methoddata already set (%p)",
1820 __func__, authctxt->methoddata);
1822 authctxt->methoddata = pctx = jpake_new();
1825 * Send request immediately, to get the protocol going while
1826 * we do the initial computations.
1828 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1829 packet_put_cstring(authctxt->server_user);
1830 packet_put_cstring(authctxt->service);
1831 packet_put_cstring(authctxt->method->name);
1833 packet_write_wait();
1835 jpake_step1(pctx->grp,
1836 &pctx->client_id, &pctx->client_id_len,
1837 &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2,
1838 &x1_proof, &x1_proof_len,
1839 &x2_proof, &x2_proof_len);
1841 JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__));
1843 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1);
1844 packet_put_string(pctx->client_id, pctx->client_id_len);
1845 packet_put_bignum2(pctx->g_x1);
1846 packet_put_bignum2(pctx->g_x2);
1847 packet_put_string(x1_proof, x1_proof_len);
1848 packet_put_string(x2_proof, x2_proof_len);
1851 bzero(x1_proof, x1_proof_len);
1852 bzero(x2_proof, x2_proof_len);
1856 /* Expect step 1 packet from peer */
1857 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1,
1858 input_userauth_jpake_server_step1);
1859 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS,
1860 &input_userauth_success_unexpected);
1866 userauth_jpake_cleanup(Authctxt *authctxt)
1868 debug3("%s: clean up", __func__);
1869 if (authctxt->methoddata != NULL) {
1870 jpake_free(authctxt->methoddata);
1871 authctxt->methoddata = NULL;
1873 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
1877 /* find auth method */
1880 * given auth method name, if configurable options permit this method fill
1881 * in auth_ident field and return true, otherwise return false.
1884 authmethod_is_enabled(Authmethod *method)
1888 /* return false if options indicate this method is disabled */
1889 if (method->enabled == NULL || *method->enabled == 0)
1891 /* return false if batch mode is enabled but method needs interactive mode */
1892 if (method->batch_flag != NULL && *method->batch_flag != 0)
1898 authmethod_lookup(const char *name)
1900 Authmethod *method = NULL;
1902 for (method = authmethods; method->name != NULL; method++)
1903 if (strcmp(name, method->name) == 0)
1905 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1909 /* XXX internal state */
1910 static Authmethod *current = NULL;
1911 static char *supported = NULL;
1912 static char *preferred = NULL;
1915 * Given the authentication method list sent by the server, return the
1916 * next method we should try. If the server initially sends a nil list,
1917 * use a built-in default list.
1920 authmethod_get(char *authlist)
1925 /* Use a suitable default if we're passed a nil list. */
1926 if (authlist == NULL || strlen(authlist) == 0)
1927 authlist = options.preferred_authentications;
1929 if (supported == NULL || strcmp(authlist, supported) != 0) {
1930 debug3("start over, passed a different list %s", authlist);
1932 supported = xstrdup(authlist);
1933 preferred = options.preferred_authentications;
1934 debug3("preferred %s", preferred);
1936 } else if (current != NULL && authmethod_is_enabled(current))
1940 if ((name = match_list(preferred, supported, &next)) == NULL) {
1941 debug("No more authentication methods to try.");
1946 debug3("authmethod_lookup %s", name);
1947 debug3("remaining preferred: %s", preferred);
1948 if ((current = authmethod_lookup(name)) != NULL &&
1949 authmethod_is_enabled(current)) {
1950 debug3("authmethod_is_enabled %s", name);
1951 debug("Next authentication method: %s", name);
1960 authmethods_get(void)
1962 Authmethod *method = NULL;
1967 for (method = authmethods; method->name != NULL; method++) {
1968 if (authmethod_is_enabled(method)) {
1969 if (buffer_len(&b) > 0)
1970 buffer_append(&b, ",", 1);
1971 buffer_append(&b, method->name, strlen(method->name));
1974 buffer_append(&b, "\0", 1);
1975 list = xstrdup(buffer_ptr(&b));