1 /* $OpenBSD: sshconnect2.c,v 1.166 2008/07/17 08:48:00 djm Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/types.h>
29 #include <sys/socket.h>
41 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
45 #include "openbsd-compat/sys-queue.h"
56 #include "myproposal.h"
57 #include "sshconnect.h"
68 #include "pathnames.h"
76 extern char *client_version_string;
77 extern char *server_version_string;
78 extern Options options;
80 /* tty_flag is set in ssh.c. use this in ssh_userauth2 */
81 /* if it is set then prevent the switch to the null cipher */
85 /* tty_flag is set in ssh.c. use this in ssh_userauth2 */
86 /* if it is set then prevent the switch to the null cipher */
94 u_char *session_id2 = NULL;
95 u_int session_id2_len = 0;
98 struct sockaddr *xxx_hostaddr;
103 verify_host_key_callback(Key *hostkey)
105 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
106 fatal("Host key verification failed.");
111 ssh_kex2(char *host, struct sockaddr *hostaddr)
116 char *orig = NULL, *gss = NULL;
117 char *gss_host = NULL;
121 xxx_hostaddr = hostaddr;
124 if (options.gss_keyex) {
125 /* Add the GSSAPI mechanisms currently supported on this
126 * client to the key exchange algorithm proposal */
127 orig = myproposal[PROPOSAL_KEX_ALGS];
129 if (options.gss_trust_dns)
130 gss_host = (char *)get_canonical_hostname(1);
134 gss = ssh_gssapi_client_mechanisms(gss_host);
136 debug("Offering GSSAPI proposal: %s", gss);
137 xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
143 if (options.ciphers == (char *)-1) {
144 logit("No valid ciphers for protocol version 2 given, using defaults.");
145 options.ciphers = NULL;
147 if (options.ciphers != NULL) {
148 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
149 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
151 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
152 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
153 myproposal[PROPOSAL_ENC_ALGS_STOC] =
154 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
155 if (options.compression) {
156 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
157 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
159 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
160 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
162 if (options.macs != NULL) {
163 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
164 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
166 if (options.hostkeyalgorithms != NULL)
167 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
168 options.hostkeyalgorithms;
171 /* If we've got GSSAPI algorithms, then we also support the
172 * 'null' hostkey, as a last resort */
173 if (options.gss_keyex && gss) {
174 orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
175 xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
180 if (options.rekey_limit)
181 packet_set_rekey_limit((u_int32_t)options.rekey_limit);
183 /* start key exchange */
184 kex = kex_setup(myproposal);
185 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
186 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
187 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
188 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
190 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
191 kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_client;
192 kex->kex[KEX_GSS_GEX_SHA1] = kexgss_client;
194 kex->client_version_string=client_version_string;
195 kex->server_version_string=server_version_string;
196 kex->verify_host_key=&verify_host_key_callback;
199 kex->gss_deleg_creds = options.gss_deleg_creds;
200 kex->gss_trust_dns = options.gss_trust_dns;
201 kex->gss_host = gss_host;
206 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
208 session_id2 = kex->session_id;
209 session_id2_len = kex->session_id_len;
212 /* send 1st encrypted/maced/compressed message */
213 packet_start(SSH2_MSG_IGNORE);
214 packet_put_cstring("markus");
224 typedef struct Authctxt Authctxt;
225 typedef struct Authmethod Authmethod;
226 typedef struct identity Identity;
227 typedef struct idlist Idlist;
230 TAILQ_ENTRY(identity) next;
231 AuthenticationConnection *ac; /* set if agent supports key */
232 Key *key; /* public/private key */
233 char *filename; /* comment for agent-only keys */
235 int isprivate; /* key points to the private key */
237 TAILQ_HEAD(idlist, identity);
240 const char *server_user;
241 const char *local_user;
249 AuthenticationConnection *agent;
251 Sensitive *sensitive;
252 /* kbd-interactive */
258 char *name; /* string to compare against server's list */
259 int (*userauth)(Authctxt *authctxt);
260 int *enabled; /* flag in option struct that enables method */
261 int *batch_flag; /* flag in option struct that disables method */
264 void input_userauth_success(int, u_int32_t, void *);
265 void input_userauth_failure(int, u_int32_t, void *);
266 void input_userauth_banner(int, u_int32_t, void *);
267 void input_userauth_error(int, u_int32_t, void *);
268 void input_userauth_info_req(int, u_int32_t, void *);
269 void input_userauth_pk_ok(int, u_int32_t, void *);
270 void input_userauth_passwd_changereq(int, u_int32_t, void *);
272 int userauth_none(Authctxt *);
273 int userauth_pubkey(Authctxt *);
274 int userauth_passwd(Authctxt *);
275 int userauth_kbdint(Authctxt *);
276 int userauth_hostbased(Authctxt *);
277 int userauth_kerberos(Authctxt *);
280 int userauth_external(Authctxt *authctxt);
281 int userauth_gssapi(Authctxt *authctxt);
282 int userauth_gssapi_with_mic(Authctxt *authctxt);
283 int userauth_gssapi_without_mic(Authctxt *authctxt);
284 void input_gssapi_response(int type, u_int32_t, void *);
285 void input_gssapi_token(int type, u_int32_t, void *);
286 void input_gssapi_hash(int type, u_int32_t, void *);
287 void input_gssapi_error(int, u_int32_t, void *);
288 void input_gssapi_errtok(int, u_int32_t, void *);
289 int userauth_gsskeyex(Authctxt *authctxt);
292 void userauth(Authctxt *, char *);
294 static int sign_and_send_pubkey(Authctxt *, Identity *);
295 static void pubkey_prepare(Authctxt *);
296 static void pubkey_cleanup(Authctxt *);
297 static Key *load_identity_file(char *);
299 static Authmethod *authmethod_get(char *authlist);
300 static Authmethod *authmethod_lookup(const char *name);
301 static char *authmethods_get(void);
303 Authmethod authmethods[] = {
307 &options.gss_authentication,
311 &options.gss_authentication,
315 &options.gss_authentication,
319 &options.gss_authentication,
324 &options.hostbased_authentication,
328 &options.pubkey_authentication,
330 {"keyboard-interactive",
332 &options.kbd_interactive_authentication,
333 &options.batch_mode},
336 &options.password_authentication,
337 &options.batch_mode},
342 {NULL, NULL, NULL, NULL}
346 ssh_userauth2(const char *local_user, const char *server_user, char *host,
347 Sensitive *sensitive)
352 if (options.challenge_response_authentication)
353 options.kbd_interactive_authentication = 1;
355 packet_start(SSH2_MSG_SERVICE_REQUEST);
356 packet_put_cstring("ssh-userauth");
358 debug("SSH2_MSG_SERVICE_REQUEST sent");
360 type = packet_read();
361 if (type != SSH2_MSG_SERVICE_ACCEPT)
362 fatal("Server denied authentication request: %d", type);
363 if (packet_remaining() > 0) {
364 char *reply = packet_get_string(NULL);
365 debug2("service_accept: %s", reply);
368 debug2("buggy server: service_accept w/o service");
371 debug("SSH2_MSG_SERVICE_ACCEPT received");
373 if (options.preferred_authentications == NULL)
374 options.preferred_authentications = authmethods_get();
376 /* setup authentication context */
377 memset(&authctxt, 0, sizeof(authctxt));
378 pubkey_prepare(&authctxt);
379 authctxt.server_user = server_user;
380 authctxt.local_user = local_user;
381 authctxt.host = host;
382 authctxt.service = "ssh-connection"; /* service name */
383 authctxt.success = 0;
384 authctxt.method = authmethod_lookup("none");
385 authctxt.authlist = NULL;
386 authctxt.methoddata = NULL;
387 authctxt.sensitive = sensitive;
388 authctxt.info_req_seen = 0;
389 if (authctxt.method == NULL)
390 fatal("ssh_userauth2: internal error: cannot send userauth none request");
392 /* initial userauth request */
393 userauth_none(&authctxt);
395 dispatch_init(&input_userauth_error);
396 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
397 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
398 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
399 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
401 pubkey_cleanup(&authctxt);
402 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
404 /* if the user wants to use the none cipher do it */
405 /* post authentication and only if the right conditions are met */
406 /* both of the NONE commands must be true and there must be no */
408 if ((options.none_switch == 1) && (options.none_enabled == 1))
410 if (!tty_flag) /* no null on tty sessions */
412 debug("Requesting none rekeying...");
413 myproposal[PROPOSAL_ENC_ALGS_STOC] = "none";
414 myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none";
415 kex_prop2buf(&xxx_kex->my,myproposal);
416 packet_request_rekeying();
417 fprintf(stderr, "WARNING: ENABLED NONE CIPHER\n");
421 /* requested NONE cipher when in a tty */
422 debug("Cannot switch to NONE cipher with tty allocated");
423 fprintf(stderr, "NONE cipher switch disabled when a TTY is allocated\n");
426 debug("Authentication succeeded (%s).", authctxt.method->name);
430 userauth(Authctxt *authctxt, char *authlist)
432 if (authctxt->methoddata) {
433 xfree(authctxt->methoddata);
434 authctxt->methoddata = NULL;
436 if (authlist == NULL) {
437 authlist = authctxt->authlist;
439 if (authctxt->authlist)
440 xfree(authctxt->authlist);
441 authctxt->authlist = authlist;
444 Authmethod *method = authmethod_get(authlist);
446 fatal("Permission denied (%s).", authlist);
447 authctxt->method = method;
449 /* reset the per method handler */
450 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
451 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
453 /* and try new method */
454 if (method->userauth(authctxt) != 0) {
455 debug2("we sent a %s packet, wait for reply", method->name);
458 debug2("we did not send a packet, disable method");
459 method->enabled = NULL;
465 input_userauth_error(int type, u_int32_t seq, void *ctxt)
467 fatal("input_userauth_error: bad message during authentication: "
472 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
474 char *msg, *raw, *lang;
477 debug3("input_userauth_banner");
478 raw = packet_get_string(&len);
479 lang = packet_get_string(NULL);
480 if (options.log_level >= SYSLOG_LEVEL_INFO) {
483 msg = xmalloc(len * 4); /* max expansion from strnvis() */
484 strnvis(msg, raw, len * 4, VIS_SAFE|VIS_OCTAL);
485 fprintf(stderr, "%s", msg);
493 input_userauth_success(int type, u_int32_t seq, void *ctxt)
495 Authctxt *authctxt = ctxt;
496 if (authctxt == NULL)
497 fatal("input_userauth_success: no authentication context");
498 if (authctxt->authlist) {
499 xfree(authctxt->authlist);
500 authctxt->authlist = NULL;
502 if (authctxt->methoddata) {
503 xfree(authctxt->methoddata);
504 authctxt->methoddata = NULL;
506 authctxt->success = 1; /* break out */
510 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
512 Authctxt *authctxt = ctxt;
513 char *authlist = NULL;
516 if (authctxt == NULL)
517 fatal("input_userauth_failure: no authentication context");
519 authlist = packet_get_string(NULL);
520 partial = packet_get_char();
524 logit("Authenticated with partial success.");
525 debug("Authentications that can continue: %s", authlist);
527 userauth(authctxt, authlist);
530 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
532 Authctxt *authctxt = ctxt;
536 int pktype, sent = 0;
541 if (authctxt == NULL)
542 fatal("input_userauth_pk_ok: no authentication context");
543 if (datafellows & SSH_BUG_PKOK) {
544 /* this is similar to SSH_BUG_PKAUTH */
545 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
546 pkblob = packet_get_string(&blen);
548 buffer_append(&b, pkblob, blen);
549 pkalg = buffer_get_string(&b, &alen);
552 pkalg = packet_get_string(&alen);
553 pkblob = packet_get_string(&blen);
557 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
559 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
560 debug("unknown pkalg %s", pkalg);
563 if ((key = key_from_blob(pkblob, blen)) == NULL) {
564 debug("no key from blob. pkalg %s", pkalg);
567 if (key->type != pktype) {
568 error("input_userauth_pk_ok: type mismatch "
569 "for decoded key (received %d, expected %d)",
573 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
574 debug2("input_userauth_pk_ok: fp %s", fp);
578 * search keys in the reverse order, because last candidate has been
579 * moved to the end of the queue. this also avoids confusion by
582 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
583 if (key_equal(key, id->key)) {
584 sent = sign_and_send_pubkey(authctxt, id);
594 /* try another method if we did not send a packet */
596 userauth(authctxt, NULL);
601 userauth_gssapi(Authctxt *authctxt)
603 Gssctxt *gssctxt = NULL;
604 static gss_OID_set gss_supported = NULL;
605 static u_int mech = 0;
608 char *gss_host = NULL;
610 if (!options.gss_authentication) {
611 verbose("GSSAPI authentication disabled.");
615 if (options.gss_trust_dns)
616 gss_host = (char *)get_canonical_hostname(1);
618 gss_host = (char *)authctxt->host;
620 /* Try one GSSAPI method at a time, rather than sending them all at
623 if (gss_supported == NULL)
624 gss_indicate_mechs(&min, &gss_supported);
626 /* Check to see if the mechanism is usable before we offer it */
627 while (mech < gss_supported->count && !ok) {
628 /* My DER encoding requires length<128 */
629 if (gss_supported->elements[mech].length < 128 &&
630 ssh_gssapi_check_mechanism(&gssctxt,
631 &gss_supported->elements[mech], gss_host)) {
632 ok = 1; /* Mechanism works */
641 authctxt->methoddata=(void *)gssctxt;
643 packet_start(SSH2_MSG_USERAUTH_REQUEST);
644 packet_put_cstring(authctxt->server_user);
645 packet_put_cstring(authctxt->service);
646 packet_put_cstring(authctxt->method->name);
650 packet_put_int((gss_supported->elements[mech].length) + 2);
651 packet_put_char(SSH_GSS_OIDTYPE);
652 packet_put_char(gss_supported->elements[mech].length);
653 packet_put_raw(gss_supported->elements[mech].elements,
654 gss_supported->elements[mech].length);
658 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
659 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
660 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
661 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
663 mech++; /* Move along to next candidate */
669 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
671 Authctxt *authctxt = ctxt;
672 Gssctxt *gssctxt = authctxt->methoddata;
673 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
674 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
675 gss_buffer_desc gssbuf;
676 OM_uint32 status, ms, flags;
679 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
680 recv_tok, &send_tok, &flags);
682 if (send_tok.length > 0) {
683 if (GSS_ERROR(status))
684 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
686 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
688 packet_put_string(send_tok.value, send_tok.length);
690 gss_release_buffer(&ms, &send_tok);
693 if (status == GSS_S_COMPLETE) {
694 /* send either complete or MIC, depending on mechanism */
695 if (strcmp(authctxt->method->name,"gssapi")==0 ||
696 (!(flags & GSS_C_INTEG_FLAG))) {
697 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
700 ssh_gssapi_buildmic(&b, authctxt->server_user,
701 authctxt->service, "gssapi-with-mic");
703 gssbuf.value = buffer_ptr(&b);
704 gssbuf.length = buffer_len(&b);
706 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
708 if (!GSS_ERROR(status)) {
709 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
710 packet_put_string(mic.value, mic.length);
716 gss_release_buffer(&ms, &mic);
724 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
726 Authctxt *authctxt = ctxt;
731 if (authctxt == NULL)
732 fatal("input_gssapi_response: no authentication context");
733 gssctxt = authctxt->methoddata;
736 oidv = packet_get_string(&oidlen);
739 oidv[0] != SSH_GSS_OIDTYPE ||
740 oidv[1] != oidlen - 2) {
742 debug("Badly encoded mechanism OID received");
743 userauth(authctxt, NULL);
747 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
748 fatal("Server returned different OID than expected");
754 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
755 /* Start again with next method on list */
756 debug("Trying to start again");
757 userauth(authctxt, NULL);
763 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
765 Authctxt *authctxt = ctxt;
766 gss_buffer_desc recv_tok;
770 if (authctxt == NULL)
771 fatal("input_gssapi_response: no authentication context");
773 recv_tok.value = packet_get_string(&slen);
774 recv_tok.length = slen; /* safe typecast */
778 status = process_gssapi_token(ctxt, &recv_tok);
780 xfree(recv_tok.value);
782 if (GSS_ERROR(status)) {
783 /* Start again with the next method in the list */
784 userauth(authctxt, NULL);
790 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
792 Authctxt *authctxt = ctxt;
794 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
795 gss_buffer_desc recv_tok;
796 OM_uint32 status, ms;
799 if (authctxt == NULL)
800 fatal("input_gssapi_response: no authentication context");
801 gssctxt = authctxt->methoddata;
803 recv_tok.value = packet_get_string(&len);
804 recv_tok.length = len;
808 /* Stick it into GSSAPI and see what it says */
809 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
810 &recv_tok, &send_tok, NULL);
812 xfree(recv_tok.value);
813 gss_release_buffer(&ms, &send_tok);
815 /* Server will be returning a failed packet after this one */
819 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
825 maj=packet_get_int();
826 min=packet_get_int();
827 msg=packet_get_string(NULL);
828 lang=packet_get_string(NULL);
832 debug("Server GSSAPI Error:\n%s", msg);
839 const gss_OID_desc * const gss_mech_globus_gssapi_openssl;
840 #define is_gsi_oid(oid) \
841 (oid->length == gss_mech_globus_gssapi_openssl->length && \
842 (memcmp(oid->elements, gss_mech_globus_gssapi_openssl->elements, \
847 userauth_external(Authctxt *authctxt)
849 static int attempt = 0;
854 /* The client MUST NOT try this method if initial key exchange
855 was not performed using a GSSAPI-based key exchange
857 if (gss_kex_context == NULL) {
858 debug2("gsskex not performed, skipping external-keyx");
862 debug2("userauth_external");
863 packet_start(SSH2_MSG_USERAUTH_REQUEST);
865 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
866 packet_put_cstring("");
869 packet_put_cstring(authctxt->server_user);
873 packet_put_cstring(authctxt->service);
874 packet_put_cstring(authctxt->method->name);
880 userauth_gsskeyex(Authctxt *authctxt)
883 gss_buffer_desc gssbuf;
884 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
887 static int attempt = 0;
891 if (gss_kex_context == NULL) {
892 debug("No valid Key exchange context");
897 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
898 ssh_gssapi_buildmic(&b, "", authctxt->service, "gssapi-keyex");
901 ssh_gssapi_buildmic(&b, authctxt->server_user, authctxt->service,
907 gssbuf.value = buffer_ptr(&b);
908 gssbuf.length = buffer_len(&b);
910 if (GSS_ERROR(ssh_gssapi_sign(gss_kex_context, &gssbuf, &mic))) {
915 packet_start(SSH2_MSG_USERAUTH_REQUEST);
917 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
918 packet_put_cstring("");
921 packet_put_cstring(authctxt->server_user);
925 packet_put_cstring(authctxt->service);
926 packet_put_cstring(authctxt->method->name);
927 packet_put_string(mic.value, mic.length);
931 gss_release_buffer(&ms, &mic);
939 userauth_none(Authctxt *authctxt)
941 /* initial userauth request */
942 packet_start(SSH2_MSG_USERAUTH_REQUEST);
943 packet_put_cstring(authctxt->server_user);
944 packet_put_cstring(authctxt->service);
945 packet_put_cstring(authctxt->method->name);
951 userauth_passwd(Authctxt *authctxt)
953 static int attempt = 0;
957 if (attempt++ >= options.number_of_password_prompts)
961 error("Permission denied, please try again.");
963 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
964 authctxt->server_user, authctxt->host);
965 password = read_passphrase(prompt, 0);
966 packet_start(SSH2_MSG_USERAUTH_REQUEST);
967 packet_put_cstring(authctxt->server_user);
968 packet_put_cstring(authctxt->service);
969 packet_put_cstring(authctxt->method->name);
971 packet_put_cstring(password);
972 memset(password, 0, strlen(password));
974 packet_add_padding(64);
977 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
978 &input_userauth_passwd_changereq);
983 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
986 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
988 Authctxt *authctxt = ctxt;
989 char *info, *lang, *password = NULL, *retype = NULL;
992 debug2("input_userauth_passwd_changereq");
994 if (authctxt == NULL)
995 fatal("input_userauth_passwd_changereq: "
996 "no authentication context");
998 info = packet_get_string(NULL);
999 lang = packet_get_string(NULL);
1000 if (strlen(info) > 0)
1004 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1005 packet_put_cstring(authctxt->server_user);
1006 packet_put_cstring(authctxt->service);
1007 packet_put_cstring(authctxt->method->name);
1008 packet_put_char(1); /* additional info */
1009 snprintf(prompt, sizeof(prompt),
1010 "Enter %.30s@%.128s's old password: ",
1011 authctxt->server_user, authctxt->host);
1012 password = read_passphrase(prompt, 0);
1013 packet_put_cstring(password);
1014 memset(password, 0, strlen(password));
1017 while (password == NULL) {
1018 snprintf(prompt, sizeof(prompt),
1019 "Enter %.30s@%.128s's new password: ",
1020 authctxt->server_user, authctxt->host);
1021 password = read_passphrase(prompt, RP_ALLOW_EOF);
1022 if (password == NULL) {
1026 snprintf(prompt, sizeof(prompt),
1027 "Retype %.30s@%.128s's new password: ",
1028 authctxt->server_user, authctxt->host);
1029 retype = read_passphrase(prompt, 0);
1030 if (strcmp(password, retype) != 0) {
1031 memset(password, 0, strlen(password));
1033 logit("Mismatch; try again, EOF to quit.");
1036 memset(retype, 0, strlen(retype));
1039 packet_put_cstring(password);
1040 memset(password, 0, strlen(password));
1042 packet_add_padding(64);
1045 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1046 &input_userauth_passwd_changereq);
1050 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1051 u_char *data, u_int datalen)
1056 /* the agent supports this key */
1058 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1061 * we have already loaded the private key or
1062 * the private key is stored in external hardware
1064 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1065 return (key_sign(id->key, sigp, lenp, data, datalen));
1066 /* load the private key from the file */
1067 if ((prv = load_identity_file(id->filename)) == NULL)
1069 ret = key_sign(prv, sigp, lenp, data, datalen);
1075 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1078 u_char *blob, *signature;
1079 u_int bloblen, slen;
1084 debug3("sign_and_send_pubkey");
1086 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1087 /* we cannot handle this key */
1088 debug3("sign_and_send_pubkey: cannot handle key");
1091 /* data to be signed */
1093 if (datafellows & SSH_OLD_SESSIONID) {
1094 buffer_append(&b, session_id2, session_id2_len);
1095 skip = session_id2_len;
1097 buffer_put_string(&b, session_id2, session_id2_len);
1098 skip = buffer_len(&b);
1100 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1101 buffer_put_cstring(&b, authctxt->server_user);
1102 buffer_put_cstring(&b,
1103 datafellows & SSH_BUG_PKSERVICE ?
1106 if (datafellows & SSH_BUG_PKAUTH) {
1107 buffer_put_char(&b, have_sig);
1109 buffer_put_cstring(&b, authctxt->method->name);
1110 buffer_put_char(&b, have_sig);
1111 buffer_put_cstring(&b, key_ssh_name(id->key));
1113 buffer_put_string(&b, blob, bloblen);
1115 /* generate signature */
1116 ret = identity_sign(id, &signature, &slen,
1117 buffer_ptr(&b), buffer_len(&b));
1126 if (datafellows & SSH_BUG_PKSERVICE) {
1128 buffer_append(&b, session_id2, session_id2_len);
1129 skip = session_id2_len;
1130 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1131 buffer_put_cstring(&b, authctxt->server_user);
1132 buffer_put_cstring(&b, authctxt->service);
1133 buffer_put_cstring(&b, authctxt->method->name);
1134 buffer_put_char(&b, have_sig);
1135 if (!(datafellows & SSH_BUG_PKAUTH))
1136 buffer_put_cstring(&b, key_ssh_name(id->key));
1137 buffer_put_string(&b, blob, bloblen);
1141 /* append signature */
1142 buffer_put_string(&b, signature, slen);
1145 /* skip session id and packet type */
1146 if (buffer_len(&b) < skip + 1)
1147 fatal("userauth_pubkey: internal error");
1148 buffer_consume(&b, skip + 1);
1150 /* put remaining data from buffer into packet */
1151 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1152 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1160 send_pubkey_test(Authctxt *authctxt, Identity *id)
1163 u_int bloblen, have_sig = 0;
1165 debug3("send_pubkey_test");
1167 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1168 /* we cannot handle this key */
1169 debug3("send_pubkey_test: cannot handle key");
1172 /* register callback for USERAUTH_PK_OK message */
1173 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1175 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1176 packet_put_cstring(authctxt->server_user);
1177 packet_put_cstring(authctxt->service);
1178 packet_put_cstring(authctxt->method->name);
1179 packet_put_char(have_sig);
1180 if (!(datafellows & SSH_BUG_PKAUTH))
1181 packet_put_cstring(key_ssh_name(id->key));
1182 packet_put_string(blob, bloblen);
1189 load_identity_file(char *filename)
1192 char prompt[300], *passphrase;
1193 int perm_ok, quit, i;
1196 if (stat(filename, &st) < 0) {
1197 debug3("no such identity: %s", filename);
1200 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1203 if (private == NULL) {
1204 if (options.batch_mode)
1206 snprintf(prompt, sizeof prompt,
1207 "Enter passphrase for key '%.100s': ", filename);
1208 for (i = 0; i < options.number_of_password_prompts; i++) {
1209 passphrase = read_passphrase(prompt, 0);
1210 if (strcmp(passphrase, "") != 0) {
1211 private = key_load_private_type(KEY_UNSPEC,
1212 filename, passphrase, NULL, NULL);
1215 debug2("no passphrase given, try next key");
1218 memset(passphrase, 0, strlen(passphrase));
1220 if (private != NULL || quit)
1222 debug2("bad passphrase given, try again...");
1229 * try keys in the following order:
1230 * 1. agent keys that are found in the config file
1231 * 2. other agent keys
1232 * 3. keys that are only listed in the config file
1235 pubkey_prepare(Authctxt *authctxt)
1238 Idlist agent, files, *preferred;
1240 AuthenticationConnection *ac;
1244 TAILQ_INIT(&agent); /* keys from the agent */
1245 TAILQ_INIT(&files); /* keys from the config file */
1246 preferred = &authctxt->keys;
1247 TAILQ_INIT(preferred); /* preferred order of keys */
1249 /* list of keys stored in the filesystem */
1250 for (i = 0; i < options.num_identity_files; i++) {
1251 key = options.identity_keys[i];
1252 if (key && key->type == KEY_RSA1)
1254 options.identity_keys[i] = NULL;
1255 id = xcalloc(1, sizeof(*id));
1257 id->filename = xstrdup(options.identity_files[i]);
1258 TAILQ_INSERT_TAIL(&files, id, next);
1260 /* list of keys supported by the agent */
1261 if ((ac = ssh_get_authentication_connection())) {
1262 for (key = ssh_get_first_identity(ac, &comment, 2);
1264 key = ssh_get_next_identity(ac, &comment, 2)) {
1266 TAILQ_FOREACH(id, &files, next) {
1267 /* agent keys from the config file are preferred */
1268 if (key_equal(key, id->key)) {
1271 TAILQ_REMOVE(&files, id, next);
1272 TAILQ_INSERT_TAIL(preferred, id, next);
1278 if (!found && !options.identities_only) {
1279 id = xcalloc(1, sizeof(*id));
1281 id->filename = comment;
1283 TAILQ_INSERT_TAIL(&agent, id, next);
1286 /* append remaining agent keys */
1287 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1288 TAILQ_REMOVE(&agent, id, next);
1289 TAILQ_INSERT_TAIL(preferred, id, next);
1291 authctxt->agent = ac;
1293 /* append remaining keys from the config file */
1294 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1295 TAILQ_REMOVE(&files, id, next);
1296 TAILQ_INSERT_TAIL(preferred, id, next);
1298 TAILQ_FOREACH(id, preferred, next) {
1299 debug2("key: %s (%p)", id->filename, id->key);
1304 pubkey_cleanup(Authctxt *authctxt)
1308 if (authctxt->agent != NULL)
1309 ssh_close_authentication_connection(authctxt->agent);
1310 for (id = TAILQ_FIRST(&authctxt->keys); id;
1311 id = TAILQ_FIRST(&authctxt->keys)) {
1312 TAILQ_REMOVE(&authctxt->keys, id, next);
1316 xfree(id->filename);
1322 userauth_pubkey(Authctxt *authctxt)
1327 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1330 /* move key to the end of the queue */
1331 TAILQ_REMOVE(&authctxt->keys, id, next);
1332 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1334 * send a test message if we have the public key. for
1335 * encrypted keys we cannot do this and have to load the
1336 * private key instead
1338 if (id->key && id->key->type != KEY_RSA1) {
1339 debug("Offering public key: %s", id->filename);
1340 sent = send_pubkey_test(authctxt, id);
1341 } else if (id->key == NULL) {
1342 debug("Trying private key: %s", id->filename);
1343 id->key = load_identity_file(id->filename);
1344 if (id->key != NULL) {
1346 sent = sign_and_send_pubkey(authctxt, id);
1358 * Send userauth request message specifying keyboard-interactive method.
1361 userauth_kbdint(Authctxt *authctxt)
1363 static int attempt = 0;
1365 if (attempt++ >= options.number_of_password_prompts)
1367 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1368 if (attempt > 1 && !authctxt->info_req_seen) {
1369 debug3("userauth_kbdint: disable: no info_req_seen");
1370 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1374 debug2("userauth_kbdint");
1375 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1376 packet_put_cstring(authctxt->server_user);
1377 packet_put_cstring(authctxt->service);
1378 packet_put_cstring(authctxt->method->name);
1379 packet_put_cstring(""); /* lang */
1380 packet_put_cstring(options.kbd_interactive_devices ?
1381 options.kbd_interactive_devices : "");
1384 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1389 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1392 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1394 Authctxt *authctxt = ctxt;
1395 char *name, *inst, *lang, *prompt, *response;
1396 u_int num_prompts, i;
1399 debug2("input_userauth_info_req");
1401 if (authctxt == NULL)
1402 fatal("input_userauth_info_req: no authentication context");
1404 authctxt->info_req_seen = 1;
1406 name = packet_get_string(NULL);
1407 inst = packet_get_string(NULL);
1408 lang = packet_get_string(NULL);
1409 if (strlen(name) > 0)
1411 if (strlen(inst) > 0)
1417 num_prompts = packet_get_int();
1419 * Begin to build info response packet based on prompts requested.
1420 * We commit to providing the correct number of responses, so if
1421 * further on we run into a problem that prevents this, we have to
1422 * be sure and clean this up and send a correct error response.
1424 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1425 packet_put_int(num_prompts);
1427 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1428 for (i = 0; i < num_prompts; i++) {
1429 prompt = packet_get_string(NULL);
1430 echo = packet_get_char();
1432 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1434 packet_put_cstring(response);
1435 memset(response, 0, strlen(response));
1439 packet_check_eom(); /* done with parsing incoming message. */
1441 packet_add_padding(64);
1446 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1447 u_char *data, u_int datalen)
1452 int to[2], from[2], status, version = 2;
1454 debug2("ssh_keysign called");
1456 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1457 error("ssh_keysign: no installed: %s", strerror(errno));
1460 if (fflush(stdout) != 0)
1461 error("ssh_keysign: fflush: %s", strerror(errno));
1463 error("ssh_keysign: pipe: %s", strerror(errno));
1466 if (pipe(from) < 0) {
1467 error("ssh_keysign: pipe: %s", strerror(errno));
1470 if ((pid = fork()) < 0) {
1471 error("ssh_keysign: fork: %s", strerror(errno));
1475 permanently_drop_suid(getuid());
1477 if (dup2(from[1], STDOUT_FILENO) < 0)
1478 fatal("ssh_keysign: dup2: %s", strerror(errno));
1480 if (dup2(to[0], STDIN_FILENO) < 0)
1481 fatal("ssh_keysign: dup2: %s", strerror(errno));
1484 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1485 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1492 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1493 buffer_put_string(&b, data, datalen);
1494 if (ssh_msg_send(to[1], version, &b) == -1)
1495 fatal("ssh_keysign: couldn't send request");
1497 if (ssh_msg_recv(from[0], &b) < 0) {
1498 error("ssh_keysign: no reply");
1505 while (waitpid(pid, &status, 0) < 0)
1509 if (buffer_get_char(&b) != version) {
1510 error("ssh_keysign: bad version");
1514 *sigp = buffer_get_string(&b, lenp);
1521 userauth_hostbased(Authctxt *authctxt)
1523 Key *private = NULL;
1524 Sensitive *sensitive = authctxt->sensitive;
1526 u_char *signature, *blob;
1527 char *chost, *pkalg, *p, myname[NI_MAXHOST];
1528 const char *service;
1530 int ok, i, len, found = 0;
1532 /* check for a useful key */
1533 for (i = 0; i < sensitive->nkeys; i++) {
1534 private = sensitive->keys[i];
1535 if (private && private->type != KEY_RSA1) {
1537 /* we take and free the key */
1538 sensitive->keys[i] = NULL;
1543 debug("No more client hostkeys for hostbased authentication.");
1546 if (key_to_blob(private, &blob, &blen) == 0) {
1550 /* figure out a name for the client host */
1552 if (packet_connection_is_on_socket())
1553 p = get_local_name(packet_get_connection_in());
1555 if (gethostname(myname, sizeof(myname)) == -1) {
1556 verbose("userauth_hostbased: gethostname: %s",
1559 p = xstrdup(myname);
1562 error("userauth_hostbased: cannot get local ipaddr/name");
1567 len = strlen(p) + 2;
1568 xasprintf(&chost, "%s.", p);
1569 debug2("userauth_hostbased: chost %s", chost);
1572 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1574 pkalg = xstrdup(key_ssh_name(private));
1576 /* construct data */
1577 buffer_put_string(&b, session_id2, session_id2_len);
1578 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1579 buffer_put_cstring(&b, authctxt->server_user);
1580 buffer_put_cstring(&b, service);
1581 buffer_put_cstring(&b, authctxt->method->name);
1582 buffer_put_cstring(&b, pkalg);
1583 buffer_put_string(&b, blob, blen);
1584 buffer_put_cstring(&b, chost);
1585 buffer_put_cstring(&b, authctxt->local_user);
1589 if (sensitive->external_keysign)
1590 ok = ssh_keysign(private, &signature, &slen,
1591 buffer_ptr(&b), buffer_len(&b));
1593 ok = key_sign(private, &signature, &slen,
1594 buffer_ptr(&b), buffer_len(&b));
1598 error("key_sign failed");
1604 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1605 packet_put_cstring(authctxt->server_user);
1606 packet_put_cstring(authctxt->service);
1607 packet_put_cstring(authctxt->method->name);
1608 packet_put_cstring(pkalg);
1609 packet_put_string(blob, blen);
1610 packet_put_cstring(chost);
1611 packet_put_cstring(authctxt->local_user);
1612 packet_put_string(signature, slen);
1613 memset(signature, 's', slen);
1623 /* find auth method */
1626 * given auth method name, if configurable options permit this method fill
1627 * in auth_ident field and return true, otherwise return false.
1630 authmethod_is_enabled(Authmethod *method)
1634 /* return false if options indicate this method is disabled */
1635 if (method->enabled == NULL || *method->enabled == 0)
1637 /* return false if batch mode is enabled but method needs interactive mode */
1638 if (method->batch_flag != NULL && *method->batch_flag != 0)
1644 authmethod_lookup(const char *name)
1646 Authmethod *method = NULL;
1648 for (method = authmethods; method->name != NULL; method++)
1649 if (strcmp(name, method->name) == 0)
1651 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1655 /* XXX internal state */
1656 static Authmethod *current = NULL;
1657 static char *supported = NULL;
1658 static char *preferred = NULL;
1661 * Given the authentication method list sent by the server, return the
1662 * next method we should try. If the server initially sends a nil list,
1663 * use a built-in default list.
1666 authmethod_get(char *authlist)
1671 /* Use a suitable default if we're passed a nil list. */
1672 if (authlist == NULL || strlen(authlist) == 0)
1673 authlist = options.preferred_authentications;
1675 if (supported == NULL || strcmp(authlist, supported) != 0) {
1676 debug3("start over, passed a different list %s", authlist);
1677 if (supported != NULL)
1679 supported = xstrdup(authlist);
1680 preferred = options.preferred_authentications;
1681 debug3("preferred %s", preferred);
1683 } else if (current != NULL && authmethod_is_enabled(current))
1687 if ((name = match_list(preferred, supported, &next)) == NULL) {
1688 debug("No more authentication methods to try.");
1693 debug3("authmethod_lookup %s", name);
1694 debug3("remaining preferred: %s", preferred);
1695 if ((current = authmethod_lookup(name)) != NULL &&
1696 authmethod_is_enabled(current)) {
1697 debug3("authmethod_is_enabled %s", name);
1698 debug("Next authentication method: %s", name);
1705 authmethods_get(void)
1707 Authmethod *method = NULL;
1712 for (method = authmethods; method->name != NULL; method++) {
1713 if (authmethod_is_enabled(method)) {
1714 if (buffer_len(&b) > 0)
1715 buffer_append(&b, ",", 1);
1716 buffer_append(&b, method->name, strlen(method->name));
1719 buffer_append(&b, "\0", 1);
1720 list = xstrdup(buffer_ptr(&b));