1 /* $OpenBSD: sshconnect2.c,v 1.189 2012/06/22 12:30:26 dtucker Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Copyright (c) 2008 Damien Miller. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
30 #include <sys/socket.h>
43 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
47 #include "openbsd-compat/sys-queue.h"
58 #include "myproposal.h"
59 #include "sshconnect.h"
70 #include "pathnames.h"
81 extern char *client_version_string;
82 extern char *server_version_string;
83 extern Options options;
86 /* tty_flag is set in ssh.c. use this in ssh_userauth2 */
87 /* if it is set then prevent the switch to the null cipher */
95 u_char *session_id2 = NULL;
96 u_int session_id2_len = 0;
99 struct sockaddr *xxx_hostaddr;
104 verify_host_key_callback(Key *hostkey)
106 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
107 fatal("Host key verification failed.");
112 order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
114 char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
116 struct hostkeys *hostkeys;
120 /* Find all hostkeys for this hostname */
121 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
122 hostkeys = init_hostkeys();
123 for (i = 0; i < options.num_user_hostfiles; i++)
124 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
125 for (i = 0; i < options.num_system_hostfiles; i++)
126 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
128 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
129 maxlen = strlen(avail) + 1;
130 first = xmalloc(maxlen);
131 last = xmalloc(maxlen);
132 *first = *last = '\0';
134 #define ALG_APPEND(to, from) \
137 strlcat(to, ",", maxlen); \
138 strlcat(to, from, maxlen); \
141 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
142 if ((ktype = key_type_from_name(alg)) == KEY_UNSPEC)
143 fatal("%s: unknown alg %s", __func__, alg);
144 if (lookup_key_in_hostkeys_by_type(hostkeys,
145 key_type_plain(ktype), NULL))
146 ALG_APPEND(first, alg);
148 ALG_APPEND(last, alg);
151 xasprintf(&ret, "%s%s%s", first, *first == '\0' ? "" : ",", last);
153 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
159 free_hostkeys(hostkeys);
165 ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
170 xxx_hostaddr = hostaddr;
172 if (options.ciphers == (char *)-1) {
173 logit("No valid ciphers for protocol version 2 given, using defaults.");
174 options.ciphers = NULL;
176 if (options.ciphers != NULL) {
177 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
178 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
180 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
181 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
182 myproposal[PROPOSAL_ENC_ALGS_STOC] =
183 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
184 if (options.compression) {
185 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
186 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
188 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
189 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
191 if (options.macs != NULL) {
192 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
193 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
195 if (options.hostkeyalgorithms != NULL)
196 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
197 options.hostkeyalgorithms;
199 /* Prefer algorithms that we already have keys for */
200 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
201 order_hostkeyalgs(host, hostaddr, port);
203 if (options.kex_algorithms != NULL)
204 myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
206 if (options.rekey_limit)
207 packet_set_rekey_limit((u_int32_t)options.rekey_limit);
209 /* start key exchange */
210 kex = kex_setup(myproposal);
211 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
212 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
213 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
214 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
215 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
216 kex->client_version_string=client_version_string;
217 kex->server_version_string=server_version_string;
218 kex->verify_host_key=&verify_host_key_callback;
222 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
224 if (options.use_roaming && !kex->roaming) {
225 debug("Roaming not allowed by server");
226 options.use_roaming = 0;
229 session_id2 = kex->session_id;
230 session_id2_len = kex->session_id_len;
233 /* send 1st encrypted/maced/compressed message */
234 packet_start(SSH2_MSG_IGNORE);
235 packet_put_cstring("markus");
245 typedef struct Authctxt Authctxt;
246 typedef struct Authmethod Authmethod;
247 typedef struct identity Identity;
248 typedef struct idlist Idlist;
251 TAILQ_ENTRY(identity) next;
252 AuthenticationConnection *ac; /* set if agent supports key */
253 Key *key; /* public/private key */
254 char *filename; /* comment for agent-only keys */
256 int isprivate; /* key points to the private key */
258 TAILQ_HEAD(idlist, identity);
261 const char *server_user;
262 const char *local_user;
266 sig_atomic_t success;
270 AuthenticationConnection *agent;
272 Sensitive *sensitive;
273 /* kbd-interactive */
279 char *name; /* string to compare against server's list */
280 int (*userauth)(Authctxt *authctxt);
281 void (*cleanup)(Authctxt *authctxt);
282 int *enabled; /* flag in option struct that enables method */
283 int *batch_flag; /* flag in option struct that disables method */
286 void input_userauth_success(int, u_int32_t, void *);
287 void input_userauth_success_unexpected(int, u_int32_t, void *);
288 void input_userauth_failure(int, u_int32_t, void *);
289 void input_userauth_banner(int, u_int32_t, void *);
290 void input_userauth_error(int, u_int32_t, void *);
291 void input_userauth_info_req(int, u_int32_t, void *);
292 void input_userauth_pk_ok(int, u_int32_t, void *);
293 void input_userauth_passwd_changereq(int, u_int32_t, void *);
294 void input_userauth_jpake_server_step1(int, u_int32_t, void *);
295 void input_userauth_jpake_server_step2(int, u_int32_t, void *);
296 void input_userauth_jpake_server_confirm(int, u_int32_t, void *);
298 int userauth_none(Authctxt *);
299 int userauth_pubkey(Authctxt *);
300 int userauth_passwd(Authctxt *);
301 int userauth_kbdint(Authctxt *);
302 int userauth_hostbased(Authctxt *);
303 int userauth_jpake(Authctxt *);
305 void userauth_jpake_cleanup(Authctxt *);
308 int userauth_gssapi(Authctxt *authctxt);
309 void input_gssapi_response(int type, u_int32_t, void *);
310 void input_gssapi_token(int type, u_int32_t, void *);
311 void input_gssapi_hash(int type, u_int32_t, void *);
312 void input_gssapi_error(int, u_int32_t, void *);
313 void input_gssapi_errtok(int, u_int32_t, void *);
316 void userauth(Authctxt *, char *);
318 static int sign_and_send_pubkey(Authctxt *, Identity *);
319 static void pubkey_prepare(Authctxt *);
320 static void pubkey_cleanup(Authctxt *);
321 static Key *load_identity_file(char *);
323 static Authmethod *authmethod_get(char *authlist);
324 static Authmethod *authmethod_lookup(const char *name);
325 static char *authmethods_get(void);
327 Authmethod authmethods[] = {
332 &options.gss_authentication,
338 &options.hostbased_authentication,
343 &options.pubkey_authentication,
346 {"jpake-01@openssh.com",
348 userauth_jpake_cleanup,
349 &options.zero_knowledge_password_authentication,
350 &options.batch_mode},
352 {"keyboard-interactive",
355 &options.kbd_interactive_authentication,
356 &options.batch_mode},
360 &options.password_authentication,
361 &options.batch_mode},
367 {NULL, NULL, NULL, NULL, NULL}
371 ssh_userauth2(const char *local_user, const char *server_user, char *host,
372 Sensitive *sensitive)
377 if (options.challenge_response_authentication)
378 options.kbd_interactive_authentication = 1;
380 packet_start(SSH2_MSG_SERVICE_REQUEST);
381 packet_put_cstring("ssh-userauth");
383 debug("SSH2_MSG_SERVICE_REQUEST sent");
385 type = packet_read();
386 if (type != SSH2_MSG_SERVICE_ACCEPT)
387 fatal("Server denied authentication request: %d", type);
388 if (packet_remaining() > 0) {
389 char *reply = packet_get_string(NULL);
390 debug2("service_accept: %s", reply);
393 debug2("buggy server: service_accept w/o service");
396 debug("SSH2_MSG_SERVICE_ACCEPT received");
398 if (options.preferred_authentications == NULL)
399 options.preferred_authentications = authmethods_get();
401 /* setup authentication context */
402 memset(&authctxt, 0, sizeof(authctxt));
403 pubkey_prepare(&authctxt);
404 authctxt.server_user = server_user;
405 authctxt.local_user = local_user;
406 authctxt.host = host;
407 authctxt.service = "ssh-connection"; /* service name */
408 authctxt.success = 0;
409 authctxt.method = authmethod_lookup("none");
410 authctxt.authlist = NULL;
411 authctxt.methoddata = NULL;
412 authctxt.sensitive = sensitive;
413 authctxt.info_req_seen = 0;
414 if (authctxt.method == NULL)
415 fatal("ssh_userauth2: internal error: cannot send userauth none request");
417 /* initial userauth request */
418 userauth_none(&authctxt);
420 dispatch_init(&input_userauth_error);
421 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
422 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
423 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
424 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
426 pubkey_cleanup(&authctxt);
427 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
429 /* if the user wants to use the none cipher do it */
430 /* post authentication and only if the right conditions are met */
431 /* both of the NONE commands must be true and there must be no */
433 if ((options.none_switch == 1) && (options.none_enabled == 1))
435 if (!tty_flag) /* no null on tty sessions */
437 debug("Requesting none rekeying...");
438 myproposal[PROPOSAL_ENC_ALGS_STOC] = "none";
439 myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none";
440 kex_prop2buf(&xxx_kex->my,myproposal);
441 packet_request_rekeying();
442 fprintf(stderr, "WARNING: ENABLED NONE CIPHER\n");
446 /* requested NONE cipher when in a tty */
447 debug("Cannot switch to NONE cipher with tty allocated");
448 fprintf(stderr, "NONE cipher switch disabled when a TTY is allocated\n");
451 debug("Authentication succeeded (%s).", authctxt.method->name);
455 userauth(Authctxt *authctxt, char *authlist)
457 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
458 authctxt->method->cleanup(authctxt);
460 if (authctxt->methoddata) {
461 xfree(authctxt->methoddata);
462 authctxt->methoddata = NULL;
464 if (authlist == NULL) {
465 authlist = authctxt->authlist;
467 if (authctxt->authlist)
468 xfree(authctxt->authlist);
469 authctxt->authlist = authlist;
472 Authmethod *method = authmethod_get(authlist);
474 fatal("Permission denied (%s).", authlist);
475 authctxt->method = method;
477 /* reset the per method handler */
478 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
479 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
481 /* and try new method */
482 if (method->userauth(authctxt) != 0) {
483 debug2("we sent a %s packet, wait for reply", method->name);
486 debug2("we did not send a packet, disable method");
487 method->enabled = NULL;
494 input_userauth_error(int type, u_int32_t seq, void *ctxt)
496 fatal("input_userauth_error: bad message during authentication: "
502 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
504 char *msg, *raw, *lang;
507 debug3("input_userauth_banner");
508 raw = packet_get_string(&len);
509 lang = packet_get_string(NULL);
510 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
513 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
514 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
515 fprintf(stderr, "%s", msg);
524 input_userauth_success(int type, u_int32_t seq, void *ctxt)
526 Authctxt *authctxt = ctxt;
528 if (authctxt == NULL)
529 fatal("input_userauth_success: no authentication context");
530 if (authctxt->authlist) {
531 xfree(authctxt->authlist);
532 authctxt->authlist = NULL;
534 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
535 authctxt->method->cleanup(authctxt);
536 if (authctxt->methoddata) {
537 xfree(authctxt->methoddata);
538 authctxt->methoddata = NULL;
540 authctxt->success = 1; /* break out */
544 input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
546 Authctxt *authctxt = ctxt;
548 if (authctxt == NULL)
549 fatal("%s: no authentication context", __func__);
551 fatal("Unexpected authentication success during %s.",
552 authctxt->method->name);
557 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
559 Authctxt *authctxt = ctxt;
560 char *authlist = NULL;
563 if (authctxt == NULL)
564 fatal("input_userauth_failure: no authentication context");
566 authlist = packet_get_string(NULL);
567 partial = packet_get_char();
571 logit("Authenticated with partial success.");
572 debug("Authentications that can continue: %s", authlist);
574 userauth(authctxt, authlist);
579 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
581 Authctxt *authctxt = ctxt;
585 int pktype, sent = 0;
590 if (authctxt == NULL)
591 fatal("input_userauth_pk_ok: no authentication context");
592 if (datafellows & SSH_BUG_PKOK) {
593 /* this is similar to SSH_BUG_PKAUTH */
594 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
595 pkblob = packet_get_string(&blen);
597 buffer_append(&b, pkblob, blen);
598 pkalg = buffer_get_string(&b, &alen);
601 pkalg = packet_get_string(&alen);
602 pkblob = packet_get_string(&blen);
606 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
608 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
609 debug("unknown pkalg %s", pkalg);
612 if ((key = key_from_blob(pkblob, blen)) == NULL) {
613 debug("no key from blob. pkalg %s", pkalg);
616 if (key->type != pktype) {
617 error("input_userauth_pk_ok: type mismatch "
618 "for decoded key (received %d, expected %d)",
622 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
623 debug2("input_userauth_pk_ok: fp %s", fp);
627 * search keys in the reverse order, because last candidate has been
628 * moved to the end of the queue. this also avoids confusion by
631 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
632 if (key_equal(key, id->key)) {
633 sent = sign_and_send_pubkey(authctxt, id);
643 /* try another method if we did not send a packet */
645 userauth(authctxt, NULL);
650 userauth_gssapi(Authctxt *authctxt)
652 Gssctxt *gssctxt = NULL;
653 static gss_OID_set gss_supported = NULL;
654 static u_int mech = 0;
658 /* Try one GSSAPI method at a time, rather than sending them all at
661 if (gss_supported == NULL)
662 gss_indicate_mechs(&min, &gss_supported);
664 /* Check to see if the mechanism is usable before we offer it */
665 while (mech < gss_supported->count && !ok) {
666 /* My DER encoding requires length<128 */
667 if (gss_supported->elements[mech].length < 128 &&
668 ssh_gssapi_check_mechanism(&gssctxt,
669 &gss_supported->elements[mech], authctxt->host)) {
670 ok = 1; /* Mechanism works */
679 authctxt->methoddata=(void *)gssctxt;
681 packet_start(SSH2_MSG_USERAUTH_REQUEST);
682 packet_put_cstring(authctxt->server_user);
683 packet_put_cstring(authctxt->service);
684 packet_put_cstring(authctxt->method->name);
688 packet_put_int((gss_supported->elements[mech].length) + 2);
689 packet_put_char(SSH_GSS_OIDTYPE);
690 packet_put_char(gss_supported->elements[mech].length);
691 packet_put_raw(gss_supported->elements[mech].elements,
692 gss_supported->elements[mech].length);
696 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
697 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
698 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
699 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
701 mech++; /* Move along to next candidate */
707 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
709 Authctxt *authctxt = ctxt;
710 Gssctxt *gssctxt = authctxt->methoddata;
711 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
712 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
713 gss_buffer_desc gssbuf;
714 OM_uint32 status, ms, flags;
717 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
718 recv_tok, &send_tok, &flags);
720 if (send_tok.length > 0) {
721 if (GSS_ERROR(status))
722 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
724 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
726 packet_put_string(send_tok.value, send_tok.length);
728 gss_release_buffer(&ms, &send_tok);
731 if (status == GSS_S_COMPLETE) {
732 /* send either complete or MIC, depending on mechanism */
733 if (!(flags & GSS_C_INTEG_FLAG)) {
734 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
737 ssh_gssapi_buildmic(&b, authctxt->server_user,
738 authctxt->service, "gssapi-with-mic");
740 gssbuf.value = buffer_ptr(&b);
741 gssbuf.length = buffer_len(&b);
743 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
745 if (!GSS_ERROR(status)) {
746 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
747 packet_put_string(mic.value, mic.length);
753 gss_release_buffer(&ms, &mic);
762 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
764 Authctxt *authctxt = ctxt;
769 if (authctxt == NULL)
770 fatal("input_gssapi_response: no authentication context");
771 gssctxt = authctxt->methoddata;
774 oidv = packet_get_string(&oidlen);
777 oidv[0] != SSH_GSS_OIDTYPE ||
778 oidv[1] != oidlen - 2) {
780 debug("Badly encoded mechanism OID received");
781 userauth(authctxt, NULL);
785 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
786 fatal("Server returned different OID than expected");
792 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
793 /* Start again with next method on list */
794 debug("Trying to start again");
795 userauth(authctxt, NULL);
802 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
804 Authctxt *authctxt = ctxt;
805 gss_buffer_desc recv_tok;
809 if (authctxt == NULL)
810 fatal("input_gssapi_response: no authentication context");
812 recv_tok.value = packet_get_string(&slen);
813 recv_tok.length = slen; /* safe typecast */
817 status = process_gssapi_token(ctxt, &recv_tok);
819 xfree(recv_tok.value);
821 if (GSS_ERROR(status)) {
822 /* Start again with the next method in the list */
823 userauth(authctxt, NULL);
830 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
832 Authctxt *authctxt = ctxt;
834 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
835 gss_buffer_desc recv_tok;
836 OM_uint32 status, ms;
839 if (authctxt == NULL)
840 fatal("input_gssapi_response: no authentication context");
841 gssctxt = authctxt->methoddata;
843 recv_tok.value = packet_get_string(&len);
844 recv_tok.length = len;
848 /* Stick it into GSSAPI and see what it says */
849 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
850 &recv_tok, &send_tok, NULL);
852 xfree(recv_tok.value);
853 gss_release_buffer(&ms, &send_tok);
855 /* Server will be returning a failed packet after this one */
860 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
866 maj=packet_get_int();
867 min=packet_get_int();
868 msg=packet_get_string(NULL);
869 lang=packet_get_string(NULL);
873 debug("Server GSSAPI Error:\n%s", msg);
880 userauth_none(Authctxt *authctxt)
882 /* initial userauth request */
883 packet_start(SSH2_MSG_USERAUTH_REQUEST);
884 packet_put_cstring(authctxt->server_user);
885 packet_put_cstring(authctxt->service);
886 packet_put_cstring(authctxt->method->name);
892 userauth_passwd(Authctxt *authctxt)
894 static int attempt = 0;
897 const char *host = options.host_key_alias ? options.host_key_alias :
900 if (attempt++ >= options.number_of_password_prompts)
904 error("Permission denied, please try again.");
906 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
907 authctxt->server_user, host);
908 password = read_passphrase(prompt, 0);
909 packet_start(SSH2_MSG_USERAUTH_REQUEST);
910 packet_put_cstring(authctxt->server_user);
911 packet_put_cstring(authctxt->service);
912 packet_put_cstring(authctxt->method->name);
914 packet_put_cstring(password);
915 memset(password, 0, strlen(password));
917 packet_add_padding(64);
920 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
921 &input_userauth_passwd_changereq);
927 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
931 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
933 Authctxt *authctxt = ctxt;
934 char *info, *lang, *password = NULL, *retype = NULL;
936 const char *host = options.host_key_alias ? options.host_key_alias :
939 debug2("input_userauth_passwd_changereq");
941 if (authctxt == NULL)
942 fatal("input_userauth_passwd_changereq: "
943 "no authentication context");
945 info = packet_get_string(NULL);
946 lang = packet_get_string(NULL);
947 if (strlen(info) > 0)
951 packet_start(SSH2_MSG_USERAUTH_REQUEST);
952 packet_put_cstring(authctxt->server_user);
953 packet_put_cstring(authctxt->service);
954 packet_put_cstring(authctxt->method->name);
955 packet_put_char(1); /* additional info */
956 snprintf(prompt, sizeof(prompt),
957 "Enter %.30s@%.128s's old password: ",
958 authctxt->server_user, host);
959 password = read_passphrase(prompt, 0);
960 packet_put_cstring(password);
961 memset(password, 0, strlen(password));
964 while (password == NULL) {
965 snprintf(prompt, sizeof(prompt),
966 "Enter %.30s@%.128s's new password: ",
967 authctxt->server_user, host);
968 password = read_passphrase(prompt, RP_ALLOW_EOF);
969 if (password == NULL) {
973 snprintf(prompt, sizeof(prompt),
974 "Retype %.30s@%.128s's new password: ",
975 authctxt->server_user, host);
976 retype = read_passphrase(prompt, 0);
977 if (strcmp(password, retype) != 0) {
978 memset(password, 0, strlen(password));
980 logit("Mismatch; try again, EOF to quit.");
983 memset(retype, 0, strlen(retype));
986 packet_put_cstring(password);
987 memset(password, 0, strlen(password));
989 packet_add_padding(64);
992 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
993 &input_userauth_passwd_changereq);
998 pw_encrypt(const char *password, const char *crypt_scheme, const char *salt)
1000 /* OpenBSD crypt(3) handles all of these */
1001 if (strcmp(crypt_scheme, "crypt") == 0 ||
1002 strcmp(crypt_scheme, "bcrypt") == 0 ||
1003 strcmp(crypt_scheme, "md5crypt") == 0 ||
1004 strcmp(crypt_scheme, "crypt-extended") == 0)
1005 return xstrdup(crypt(password, salt));
1006 error("%s: unsupported password encryption scheme \"%.100s\"",
1007 __func__, crypt_scheme);
1012 jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme,
1015 char prompt[256], *password, *crypted;
1020 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ",
1021 authctxt->server_user, authctxt->host);
1022 password = read_passphrase(prompt, 0);
1024 if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) {
1025 logit("Disabling %s authentication", authctxt->method->name);
1026 authctxt->method->enabled = NULL;
1027 /* Continue with an empty password to fail gracefully */
1028 crypted = xstrdup("");
1032 debug3("%s: salt = %s", __func__, salt);
1033 debug3("%s: scheme = %s", __func__, crypt_scheme);
1034 debug3("%s: crypted = %s", __func__, crypted);
1037 if (hash_buffer(crypted, strlen(crypted), EVP_sha256(),
1038 &secret, &secret_len) != 0)
1039 fatal("%s: hash_buffer", __func__);
1041 bzero(password, strlen(password));
1042 bzero(crypted, strlen(crypted));
1046 if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL)
1047 fatal("%s: BN_bin2bn (secret)", __func__);
1048 bzero(secret, secret_len);
1056 input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt)
1058 Authctxt *authctxt = ctxt;
1059 struct jpake_ctx *pctx = authctxt->methoddata;
1060 u_char *x3_proof, *x4_proof, *x2_s_proof;
1061 u_int x3_proof_len, x4_proof_len, x2_s_proof_len;
1062 char *crypt_scheme, *salt;
1064 /* Disable this message */
1065 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL);
1067 if ((pctx->g_x3 = BN_new()) == NULL ||
1068 (pctx->g_x4 = BN_new()) == NULL)
1069 fatal("%s: BN_new", __func__);
1071 /* Fetch step 1 values */
1072 crypt_scheme = packet_get_string(NULL);
1073 salt = packet_get_string(NULL);
1074 pctx->server_id = packet_get_string(&pctx->server_id_len);
1075 packet_get_bignum2(pctx->g_x3);
1076 packet_get_bignum2(pctx->g_x4);
1077 x3_proof = packet_get_string(&x3_proof_len);
1078 x4_proof = packet_get_string(&x4_proof_len);
1081 JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__));
1083 /* Obtain password and derive secret */
1084 pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt);
1085 bzero(crypt_scheme, strlen(crypt_scheme));
1086 bzero(salt, strlen(salt));
1087 xfree(crypt_scheme);
1089 JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__));
1091 /* Calculate step 2 values */
1092 jpake_step2(pctx->grp, pctx->s, pctx->g_x1,
1093 pctx->g_x3, pctx->g_x4, pctx->x2,
1094 pctx->server_id, pctx->server_id_len,
1095 pctx->client_id, pctx->client_id_len,
1096 x3_proof, x3_proof_len,
1097 x4_proof, x4_proof_len,
1099 &x2_s_proof, &x2_s_proof_len);
1101 bzero(x3_proof, x3_proof_len);
1102 bzero(x4_proof, x4_proof_len);
1106 JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__));
1108 /* Send values for step 2 */
1109 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2);
1110 packet_put_bignum2(pctx->a);
1111 packet_put_string(x2_s_proof, x2_s_proof_len);
1114 bzero(x2_s_proof, x2_s_proof_len);
1117 /* Expect step 2 packet from peer */
1118 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2,
1119 input_userauth_jpake_server_step2);
1124 input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt)
1126 Authctxt *authctxt = ctxt;
1127 struct jpake_ctx *pctx = authctxt->methoddata;
1129 u_int x4_s_proof_len;
1131 /* Disable this message */
1132 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL);
1134 if ((pctx->b = BN_new()) == NULL)
1135 fatal("%s: BN_new", __func__);
1137 /* Fetch step 2 values */
1138 packet_get_bignum2(pctx->b);
1139 x4_s_proof = packet_get_string(&x4_s_proof_len);
1142 JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__));
1144 /* Derive shared key and calculate confirmation hash */
1145 jpake_key_confirm(pctx->grp, pctx->s, pctx->b,
1146 pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4,
1147 pctx->client_id, pctx->client_id_len,
1148 pctx->server_id, pctx->server_id_len,
1149 session_id2, session_id2_len,
1150 x4_s_proof, x4_s_proof_len,
1152 &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len);
1154 bzero(x4_s_proof, x4_s_proof_len);
1157 JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__));
1159 /* Send key confirmation proof */
1160 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM);
1161 packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
1164 /* Expect confirmation from peer */
1165 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM,
1166 input_userauth_jpake_server_confirm);
1171 input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt)
1173 Authctxt *authctxt = ctxt;
1174 struct jpake_ctx *pctx = authctxt->methoddata;
1176 /* Disable this message */
1177 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL);
1179 pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len);
1182 JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__));
1184 /* Verify expected confirmation hash */
1185 if (jpake_check_confirm(pctx->k,
1186 pctx->server_id, pctx->server_id_len,
1187 session_id2, session_id2_len,
1188 pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1)
1189 debug("%s: %s success", __func__, authctxt->method->name);
1191 debug("%s: confirmation mismatch", __func__);
1192 /* XXX stash this so if auth succeeds then we can warn/kill */
1195 userauth_jpake_cleanup(authctxt);
1200 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1201 u_char *data, u_int datalen)
1206 /* the agent supports this key */
1208 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1211 * we have already loaded the private key or
1212 * the private key is stored in external hardware
1214 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1215 return (key_sign(id->key, sigp, lenp, data, datalen));
1216 /* load the private key from the file */
1217 if ((prv = load_identity_file(id->filename)) == NULL)
1219 ret = key_sign(prv, sigp, lenp, data, datalen);
1225 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1228 u_char *blob, *signature;
1229 u_int bloblen, slen;
1235 fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
1236 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
1239 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1240 /* we cannot handle this key */
1241 debug3("sign_and_send_pubkey: cannot handle key");
1244 /* data to be signed */
1246 if (datafellows & SSH_OLD_SESSIONID) {
1247 buffer_append(&b, session_id2, session_id2_len);
1248 skip = session_id2_len;
1250 buffer_put_string(&b, session_id2, session_id2_len);
1251 skip = buffer_len(&b);
1253 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1254 buffer_put_cstring(&b, authctxt->server_user);
1255 buffer_put_cstring(&b,
1256 datafellows & SSH_BUG_PKSERVICE ?
1259 if (datafellows & SSH_BUG_PKAUTH) {
1260 buffer_put_char(&b, have_sig);
1262 buffer_put_cstring(&b, authctxt->method->name);
1263 buffer_put_char(&b, have_sig);
1264 buffer_put_cstring(&b, key_ssh_name(id->key));
1266 buffer_put_string(&b, blob, bloblen);
1268 /* generate signature */
1269 ret = identity_sign(id, &signature, &slen,
1270 buffer_ptr(&b), buffer_len(&b));
1279 if (datafellows & SSH_BUG_PKSERVICE) {
1281 buffer_append(&b, session_id2, session_id2_len);
1282 skip = session_id2_len;
1283 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1284 buffer_put_cstring(&b, authctxt->server_user);
1285 buffer_put_cstring(&b, authctxt->service);
1286 buffer_put_cstring(&b, authctxt->method->name);
1287 buffer_put_char(&b, have_sig);
1288 if (!(datafellows & SSH_BUG_PKAUTH))
1289 buffer_put_cstring(&b, key_ssh_name(id->key));
1290 buffer_put_string(&b, blob, bloblen);
1294 /* append signature */
1295 buffer_put_string(&b, signature, slen);
1298 /* skip session id and packet type */
1299 if (buffer_len(&b) < skip + 1)
1300 fatal("userauth_pubkey: internal error");
1301 buffer_consume(&b, skip + 1);
1303 /* put remaining data from buffer into packet */
1304 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1305 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1313 send_pubkey_test(Authctxt *authctxt, Identity *id)
1316 u_int bloblen, have_sig = 0;
1318 debug3("send_pubkey_test");
1320 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1321 /* we cannot handle this key */
1322 debug3("send_pubkey_test: cannot handle key");
1325 /* register callback for USERAUTH_PK_OK message */
1326 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1328 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1329 packet_put_cstring(authctxt->server_user);
1330 packet_put_cstring(authctxt->service);
1331 packet_put_cstring(authctxt->method->name);
1332 packet_put_char(have_sig);
1333 if (!(datafellows & SSH_BUG_PKAUTH))
1334 packet_put_cstring(key_ssh_name(id->key));
1335 packet_put_string(blob, bloblen);
1342 load_identity_file(char *filename)
1345 char prompt[300], *passphrase;
1346 int perm_ok = 0, quit, i;
1349 if (stat(filename, &st) < 0) {
1350 debug3("no such identity: %s", filename);
1353 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1356 if (private == NULL) {
1357 if (options.batch_mode)
1359 snprintf(prompt, sizeof prompt,
1360 "Enter passphrase for key '%.100s': ", filename);
1361 for (i = 0; i < options.number_of_password_prompts; i++) {
1362 passphrase = read_passphrase(prompt, 0);
1363 if (strcmp(passphrase, "") != 0) {
1364 private = key_load_private_type(KEY_UNSPEC,
1365 filename, passphrase, NULL, NULL);
1368 debug2("no passphrase given, try next key");
1371 memset(passphrase, 0, strlen(passphrase));
1373 if (private != NULL || quit)
1375 debug2("bad passphrase given, try again...");
1382 * try keys in the following order:
1383 * 1. agent keys that are found in the config file
1384 * 2. other agent keys
1385 * 3. keys that are only listed in the config file
1388 pubkey_prepare(Authctxt *authctxt)
1391 Idlist agent, files, *preferred;
1393 AuthenticationConnection *ac;
1397 TAILQ_INIT(&agent); /* keys from the agent */
1398 TAILQ_INIT(&files); /* keys from the config file */
1399 preferred = &authctxt->keys;
1400 TAILQ_INIT(preferred); /* preferred order of keys */
1402 /* list of keys stored in the filesystem */
1403 for (i = 0; i < options.num_identity_files; i++) {
1404 key = options.identity_keys[i];
1405 if (key && key->type == KEY_RSA1)
1407 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1409 options.identity_keys[i] = NULL;
1410 id = xcalloc(1, sizeof(*id));
1412 id->filename = xstrdup(options.identity_files[i]);
1413 TAILQ_INSERT_TAIL(&files, id, next);
1415 /* list of keys supported by the agent */
1416 if ((ac = ssh_get_authentication_connection())) {
1417 for (key = ssh_get_first_identity(ac, &comment, 2);
1419 key = ssh_get_next_identity(ac, &comment, 2)) {
1421 TAILQ_FOREACH(id, &files, next) {
1422 /* agent keys from the config file are preferred */
1423 if (key_equal(key, id->key)) {
1426 TAILQ_REMOVE(&files, id, next);
1427 TAILQ_INSERT_TAIL(preferred, id, next);
1433 if (!found && !options.identities_only) {
1434 id = xcalloc(1, sizeof(*id));
1436 id->filename = comment;
1438 TAILQ_INSERT_TAIL(&agent, id, next);
1441 /* append remaining agent keys */
1442 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1443 TAILQ_REMOVE(&agent, id, next);
1444 TAILQ_INSERT_TAIL(preferred, id, next);
1446 authctxt->agent = ac;
1448 /* append remaining keys from the config file */
1449 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1450 TAILQ_REMOVE(&files, id, next);
1451 TAILQ_INSERT_TAIL(preferred, id, next);
1453 TAILQ_FOREACH(id, preferred, next) {
1454 debug2("key: %s (%p)", id->filename, id->key);
1459 pubkey_cleanup(Authctxt *authctxt)
1463 if (authctxt->agent != NULL)
1464 ssh_close_authentication_connection(authctxt->agent);
1465 for (id = TAILQ_FIRST(&authctxt->keys); id;
1466 id = TAILQ_FIRST(&authctxt->keys)) {
1467 TAILQ_REMOVE(&authctxt->keys, id, next);
1471 xfree(id->filename);
1477 userauth_pubkey(Authctxt *authctxt)
1482 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1485 /* move key to the end of the queue */
1486 TAILQ_REMOVE(&authctxt->keys, id, next);
1487 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1489 * send a test message if we have the public key. for
1490 * encrypted keys we cannot do this and have to load the
1491 * private key instead
1493 if (id->key && id->key->type != KEY_RSA1) {
1494 debug("Offering %s public key: %s", key_type(id->key),
1496 sent = send_pubkey_test(authctxt, id);
1497 } else if (id->key == NULL) {
1498 debug("Trying private key: %s", id->filename);
1499 id->key = load_identity_file(id->filename);
1500 if (id->key != NULL) {
1502 sent = sign_and_send_pubkey(authctxt, id);
1514 * Send userauth request message specifying keyboard-interactive method.
1517 userauth_kbdint(Authctxt *authctxt)
1519 static int attempt = 0;
1521 if (attempt++ >= options.number_of_password_prompts)
1523 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1524 if (attempt > 1 && !authctxt->info_req_seen) {
1525 debug3("userauth_kbdint: disable: no info_req_seen");
1526 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1530 debug2("userauth_kbdint");
1531 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1532 packet_put_cstring(authctxt->server_user);
1533 packet_put_cstring(authctxt->service);
1534 packet_put_cstring(authctxt->method->name);
1535 packet_put_cstring(""); /* lang */
1536 packet_put_cstring(options.kbd_interactive_devices ?
1537 options.kbd_interactive_devices : "");
1540 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1545 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1548 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1550 Authctxt *authctxt = ctxt;
1551 char *name, *inst, *lang, *prompt, *response;
1552 u_int num_prompts, i;
1555 debug2("input_userauth_info_req");
1557 if (authctxt == NULL)
1558 fatal("input_userauth_info_req: no authentication context");
1560 authctxt->info_req_seen = 1;
1562 name = packet_get_string(NULL);
1563 inst = packet_get_string(NULL);
1564 lang = packet_get_string(NULL);
1565 if (strlen(name) > 0)
1567 if (strlen(inst) > 0)
1573 num_prompts = packet_get_int();
1575 * Begin to build info response packet based on prompts requested.
1576 * We commit to providing the correct number of responses, so if
1577 * further on we run into a problem that prevents this, we have to
1578 * be sure and clean this up and send a correct error response.
1580 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1581 packet_put_int(num_prompts);
1583 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1584 for (i = 0; i < num_prompts; i++) {
1585 prompt = packet_get_string(NULL);
1586 echo = packet_get_char();
1588 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1590 packet_put_cstring(response);
1591 memset(response, 0, strlen(response));
1595 packet_check_eom(); /* done with parsing incoming message. */
1597 packet_add_padding(64);
1602 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1603 u_char *data, u_int datalen)
1608 int to[2], from[2], status, version = 2;
1610 debug2("ssh_keysign called");
1612 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1613 error("ssh_keysign: not installed: %s", strerror(errno));
1616 if (fflush(stdout) != 0)
1617 error("ssh_keysign: fflush: %s", strerror(errno));
1619 error("ssh_keysign: pipe: %s", strerror(errno));
1622 if (pipe(from) < 0) {
1623 error("ssh_keysign: pipe: %s", strerror(errno));
1626 if ((pid = fork()) < 0) {
1627 error("ssh_keysign: fork: %s", strerror(errno));
1631 /* keep the socket on exec */
1632 fcntl(packet_get_connection_in(), F_SETFD, 0);
1633 permanently_drop_suid(getuid());
1635 if (dup2(from[1], STDOUT_FILENO) < 0)
1636 fatal("ssh_keysign: dup2: %s", strerror(errno));
1638 if (dup2(to[0], STDIN_FILENO) < 0)
1639 fatal("ssh_keysign: dup2: %s", strerror(errno));
1642 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1643 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1650 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1651 buffer_put_string(&b, data, datalen);
1652 if (ssh_msg_send(to[1], version, &b) == -1)
1653 fatal("ssh_keysign: couldn't send request");
1655 if (ssh_msg_recv(from[0], &b) < 0) {
1656 error("ssh_keysign: no reply");
1663 while (waitpid(pid, &status, 0) < 0)
1667 if (buffer_get_char(&b) != version) {
1668 error("ssh_keysign: bad version");
1672 *sigp = buffer_get_string(&b, lenp);
1679 userauth_hostbased(Authctxt *authctxt)
1681 Key *private = NULL;
1682 Sensitive *sensitive = authctxt->sensitive;
1684 u_char *signature, *blob;
1685 char *chost, *pkalg, *p;
1686 const char *service;
1688 int ok, i, found = 0;
1690 /* check for a useful key */
1691 for (i = 0; i < sensitive->nkeys; i++) {
1692 private = sensitive->keys[i];
1693 if (private && private->type != KEY_RSA1) {
1695 /* we take and free the key */
1696 sensitive->keys[i] = NULL;
1701 debug("No more client hostkeys for hostbased authentication.");
1704 if (key_to_blob(private, &blob, &blen) == 0) {
1708 /* figure out a name for the client host */
1709 p = get_local_name(packet_get_connection_in());
1711 error("userauth_hostbased: cannot get local ipaddr/name");
1716 xasprintf(&chost, "%s.", p);
1717 debug2("userauth_hostbased: chost %s", chost);
1720 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1722 pkalg = xstrdup(key_ssh_name(private));
1724 /* construct data */
1725 buffer_put_string(&b, session_id2, session_id2_len);
1726 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1727 buffer_put_cstring(&b, authctxt->server_user);
1728 buffer_put_cstring(&b, service);
1729 buffer_put_cstring(&b, authctxt->method->name);
1730 buffer_put_cstring(&b, pkalg);
1731 buffer_put_string(&b, blob, blen);
1732 buffer_put_cstring(&b, chost);
1733 buffer_put_cstring(&b, authctxt->local_user);
1737 if (sensitive->external_keysign)
1738 ok = ssh_keysign(private, &signature, &slen,
1739 buffer_ptr(&b), buffer_len(&b));
1741 ok = key_sign(private, &signature, &slen,
1742 buffer_ptr(&b), buffer_len(&b));
1746 error("key_sign failed");
1752 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1753 packet_put_cstring(authctxt->server_user);
1754 packet_put_cstring(authctxt->service);
1755 packet_put_cstring(authctxt->method->name);
1756 packet_put_cstring(pkalg);
1757 packet_put_string(blob, blen);
1758 packet_put_cstring(chost);
1759 packet_put_cstring(authctxt->local_user);
1760 packet_put_string(signature, slen);
1761 memset(signature, 's', slen);
1773 userauth_jpake(Authctxt *authctxt)
1775 struct jpake_ctx *pctx;
1776 u_char *x1_proof, *x2_proof;
1777 u_int x1_proof_len, x2_proof_len;
1778 static int attempt = 0; /* XXX share with userauth_password's? */
1780 if (attempt++ >= options.number_of_password_prompts)
1783 error("Permission denied, please try again.");
1785 if (authctxt->methoddata != NULL)
1786 fatal("%s: authctxt->methoddata already set (%p)",
1787 __func__, authctxt->methoddata);
1789 authctxt->methoddata = pctx = jpake_new();
1792 * Send request immediately, to get the protocol going while
1793 * we do the initial computations.
1795 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1796 packet_put_cstring(authctxt->server_user);
1797 packet_put_cstring(authctxt->service);
1798 packet_put_cstring(authctxt->method->name);
1800 packet_write_wait();
1802 jpake_step1(pctx->grp,
1803 &pctx->client_id, &pctx->client_id_len,
1804 &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2,
1805 &x1_proof, &x1_proof_len,
1806 &x2_proof, &x2_proof_len);
1808 JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__));
1810 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1);
1811 packet_put_string(pctx->client_id, pctx->client_id_len);
1812 packet_put_bignum2(pctx->g_x1);
1813 packet_put_bignum2(pctx->g_x2);
1814 packet_put_string(x1_proof, x1_proof_len);
1815 packet_put_string(x2_proof, x2_proof_len);
1818 bzero(x1_proof, x1_proof_len);
1819 bzero(x2_proof, x2_proof_len);
1823 /* Expect step 1 packet from peer */
1824 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1,
1825 input_userauth_jpake_server_step1);
1826 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS,
1827 &input_userauth_success_unexpected);
1833 userauth_jpake_cleanup(Authctxt *authctxt)
1835 debug3("%s: clean up", __func__);
1836 if (authctxt->methoddata != NULL) {
1837 jpake_free(authctxt->methoddata);
1838 authctxt->methoddata = NULL;
1840 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
1844 /* find auth method */
1847 * given auth method name, if configurable options permit this method fill
1848 * in auth_ident field and return true, otherwise return false.
1851 authmethod_is_enabled(Authmethod *method)
1855 /* return false if options indicate this method is disabled */
1856 if (method->enabled == NULL || *method->enabled == 0)
1858 /* return false if batch mode is enabled but method needs interactive mode */
1859 if (method->batch_flag != NULL && *method->batch_flag != 0)
1865 authmethod_lookup(const char *name)
1867 Authmethod *method = NULL;
1869 for (method = authmethods; method->name != NULL; method++)
1870 if (strcmp(name, method->name) == 0)
1872 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1876 /* XXX internal state */
1877 static Authmethod *current = NULL;
1878 static char *supported = NULL;
1879 static char *preferred = NULL;
1882 * Given the authentication method list sent by the server, return the
1883 * next method we should try. If the server initially sends a nil list,
1884 * use a built-in default list.
1887 authmethod_get(char *authlist)
1892 /* Use a suitable default if we're passed a nil list. */
1893 if (authlist == NULL || strlen(authlist) == 0)
1894 authlist = options.preferred_authentications;
1896 if (supported == NULL || strcmp(authlist, supported) != 0) {
1897 debug3("start over, passed a different list %s", authlist);
1898 if (supported != NULL)
1900 supported = xstrdup(authlist);
1901 preferred = options.preferred_authentications;
1902 debug3("preferred %s", preferred);
1904 } else if (current != NULL && authmethod_is_enabled(current))
1908 if ((name = match_list(preferred, supported, &next)) == NULL) {
1909 debug("No more authentication methods to try.");
1914 debug3("authmethod_lookup %s", name);
1915 debug3("remaining preferred: %s", preferred);
1916 if ((current = authmethod_lookup(name)) != NULL &&
1917 authmethod_is_enabled(current)) {
1918 debug3("authmethod_is_enabled %s", name);
1919 debug("Next authentication method: %s", name);
1927 authmethods_get(void)
1929 Authmethod *method = NULL;
1934 for (method = authmethods; method->name != NULL; method++) {
1935 if (authmethod_is_enabled(method)) {
1936 if (buffer_len(&b) > 0)
1937 buffer_append(&b, ",", 1);
1938 buffer_append(&b, method->name, strlen(method->name));
1941 buffer_append(&b, "\0", 1);
1942 list = xstrdup(buffer_ptr(&b));