1 /* $OpenBSD: sshconnect2.c,v 1.164 2007/05/17 23:53:41 jolan 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>
42 #include "openbsd-compat/sys-queue.h"
53 #include "myproposal.h"
54 #include "sshconnect.h"
65 #include "pathnames.h"
73 extern char *client_version_string;
74 extern char *server_version_string;
75 extern Options options;
77 /* tty_flag is set in ssh.c. use this in ssh_userauth2 */
78 /* if it is set then prevent the switch to the null cipher */
86 u_char *session_id2 = NULL;
87 u_int session_id2_len = 0;
90 struct sockaddr *xxx_hostaddr;
95 verify_host_key_callback(Key *hostkey)
97 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
98 fatal("Host key verification failed.");
103 ssh_kex2(char *host, struct sockaddr *hostaddr)
108 char *orig = NULL, *gss = NULL;
109 char *gss_host = NULL;
113 xxx_hostaddr = hostaddr;
116 if (options.gss_keyex) {
117 /* Add the GSSAPI mechanisms currently supported on this
118 * client to the key exchange algorithm proposal */
119 orig = myproposal[PROPOSAL_KEX_ALGS];
121 if (options.gss_trust_dns)
122 gss_host = (char *)get_canonical_hostname(1);
126 gss = ssh_gssapi_client_mechanisms(gss_host);
128 debug("Offering GSSAPI proposal: %s", gss);
129 xasprintf(&myproposal[PROPOSAL_KEX_ALGS],
135 if (options.ciphers == (char *)-1) {
136 logit("No valid ciphers for protocol version 2 given, using defaults.");
137 options.ciphers = NULL;
139 if (options.ciphers != NULL) {
140 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
141 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
143 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
144 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
145 myproposal[PROPOSAL_ENC_ALGS_STOC] =
146 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
147 if (options.compression) {
148 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
149 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
151 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
152 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
154 if (options.macs != NULL) {
155 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
156 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
158 if (options.hostkeyalgorithms != NULL)
159 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
160 options.hostkeyalgorithms;
163 /* If we've got GSSAPI algorithms, then we also support the
164 * 'null' hostkey, as a last resort */
165 if (options.gss_keyex && gss) {
166 orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
167 xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
172 if (options.rekey_limit)
173 packet_set_rekey_limit(options.rekey_limit);
175 /* start key exchange */
176 kex = kex_setup(myproposal);
177 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
178 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
179 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
180 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
182 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
183 kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_client;
184 kex->kex[KEX_GSS_GEX_SHA1] = kexgss_client;
186 kex->client_version_string=client_version_string;
187 kex->server_version_string=server_version_string;
188 kex->verify_host_key=&verify_host_key_callback;
191 kex->gss_deleg_creds = options.gss_deleg_creds;
192 kex->gss_trust_dns = options.gss_trust_dns;
193 kex->gss_host = gss_host;
198 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
200 session_id2 = kex->session_id;
201 session_id2_len = kex->session_id_len;
204 /* send 1st encrypted/maced/compressed message */
205 packet_start(SSH2_MSG_IGNORE);
206 packet_put_cstring("markus");
216 typedef struct Authctxt Authctxt;
217 typedef struct Authmethod Authmethod;
218 typedef struct identity Identity;
219 typedef struct idlist Idlist;
222 TAILQ_ENTRY(identity) next;
223 AuthenticationConnection *ac; /* set if agent supports key */
224 Key *key; /* public/private key */
225 char *filename; /* comment for agent-only keys */
227 int isprivate; /* key points to the private key */
229 TAILQ_HEAD(idlist, identity);
232 const char *server_user;
233 const char *local_user;
241 AuthenticationConnection *agent;
243 Sensitive *sensitive;
244 /* kbd-interactive */
250 char *name; /* string to compare against server's list */
251 int (*userauth)(Authctxt *authctxt);
252 int *enabled; /* flag in option struct that enables method */
253 int *batch_flag; /* flag in option struct that disables method */
256 void input_userauth_success(int, u_int32_t, void *);
257 void input_userauth_failure(int, u_int32_t, void *);
258 void input_userauth_banner(int, u_int32_t, void *);
259 void input_userauth_error(int, u_int32_t, void *);
260 void input_userauth_info_req(int, u_int32_t, void *);
261 void input_userauth_pk_ok(int, u_int32_t, void *);
262 void input_userauth_passwd_changereq(int, u_int32_t, void *);
264 int userauth_none(Authctxt *);
265 int userauth_pubkey(Authctxt *);
266 int userauth_passwd(Authctxt *);
267 int userauth_kbdint(Authctxt *);
268 int userauth_hostbased(Authctxt *);
269 int userauth_kerberos(Authctxt *);
272 int userauth_external(Authctxt *authctxt);
273 int userauth_gssapi(Authctxt *authctxt);
274 int userauth_gssapi_with_mic(Authctxt *authctxt);
275 int userauth_gssapi_without_mic(Authctxt *authctxt);
276 void input_gssapi_response(int type, u_int32_t, void *);
277 void input_gssapi_token(int type, u_int32_t, void *);
278 void input_gssapi_hash(int type, u_int32_t, void *);
279 void input_gssapi_error(int, u_int32_t, void *);
280 void input_gssapi_errtok(int, u_int32_t, void *);
281 int userauth_gsskeyex(Authctxt *authctxt);
284 void userauth(Authctxt *, char *);
286 static int sign_and_send_pubkey(Authctxt *, Identity *);
287 static void pubkey_prepare(Authctxt *);
288 static void pubkey_cleanup(Authctxt *);
289 static Key *load_identity_file(char *);
291 static Authmethod *authmethod_get(char *authlist);
292 static Authmethod *authmethod_lookup(const char *name);
293 static char *authmethods_get(void);
295 Authmethod authmethods[] = {
299 &options.gss_authentication,
303 &options.gss_authentication,
307 &options.gss_authentication,
311 &options.gss_authentication,
316 &options.hostbased_authentication,
320 &options.pubkey_authentication,
322 {"keyboard-interactive",
324 &options.kbd_interactive_authentication,
325 &options.batch_mode},
328 &options.password_authentication,
329 &options.batch_mode},
334 {NULL, NULL, NULL, NULL}
338 ssh_userauth2(const char *local_user, const char *server_user, char *host,
339 Sensitive *sensitive)
344 if (options.challenge_response_authentication)
345 options.kbd_interactive_authentication = 1;
347 packet_start(SSH2_MSG_SERVICE_REQUEST);
348 packet_put_cstring("ssh-userauth");
350 debug("SSH2_MSG_SERVICE_REQUEST sent");
352 type = packet_read();
353 if (type != SSH2_MSG_SERVICE_ACCEPT)
354 fatal("Server denied authentication request: %d", type);
355 if (packet_remaining() > 0) {
356 char *reply = packet_get_string(NULL);
357 debug2("service_accept: %s", reply);
360 debug2("buggy server: service_accept w/o service");
363 debug("SSH2_MSG_SERVICE_ACCEPT received");
365 if (options.preferred_authentications == NULL)
366 options.preferred_authentications = authmethods_get();
368 /* setup authentication context */
369 memset(&authctxt, 0, sizeof(authctxt));
370 pubkey_prepare(&authctxt);
371 authctxt.server_user = server_user;
372 authctxt.local_user = local_user;
373 authctxt.host = host;
374 authctxt.service = "ssh-connection"; /* service name */
375 authctxt.success = 0;
376 authctxt.method = authmethod_lookup("none");
377 authctxt.authlist = NULL;
378 authctxt.methoddata = NULL;
379 authctxt.sensitive = sensitive;
380 authctxt.info_req_seen = 0;
381 if (authctxt.method == NULL)
382 fatal("ssh_userauth2: internal error: cannot send userauth none request");
384 /* initial userauth request */
385 userauth_none(&authctxt);
387 dispatch_init(&input_userauth_error);
388 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
389 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
390 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
391 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
393 pubkey_cleanup(&authctxt);
394 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
396 /* if the user wants to use the none cipher do it */
397 /* post authentication and only if the right conditions are met */
398 /* both of the NONE commands must be true and there must be no */
400 if ((options.none_switch == 1) && (options.none_enabled == 1))
402 if (!tty_flag) /* no null on tty sessions */
404 debug("Requesting none rekeying...");
405 myproposal[PROPOSAL_ENC_ALGS_STOC] = "none";
406 myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none";
407 kex_prop2buf(&xxx_kex->my,myproposal);
408 packet_request_rekeying();
409 fprintf(stderr, "WARNING: ENABLED NONE CIPHER\n");
413 /* requested NONE cipher when in a tty */
414 debug("Cannot switch to NONE cipher with tty allocated");
415 fprintf(stderr, "NONE cipher switch disabled when a TTY is allocated\n");
418 debug("Authentication succeeded (%s).", authctxt.method->name);
422 userauth(Authctxt *authctxt, char *authlist)
424 if (authctxt->methoddata) {
425 xfree(authctxt->methoddata);
426 authctxt->methoddata = NULL;
428 if (authlist == NULL) {
429 authlist = authctxt->authlist;
431 if (authctxt->authlist)
432 xfree(authctxt->authlist);
433 authctxt->authlist = authlist;
436 Authmethod *method = authmethod_get(authlist);
438 fatal("Permission denied (%s).", authlist);
439 authctxt->method = method;
441 /* reset the per method handler */
442 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
443 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
445 /* and try new method */
446 if (method->userauth(authctxt) != 0) {
447 debug2("we sent a %s packet, wait for reply", method->name);
450 debug2("we did not send a packet, disable method");
451 method->enabled = NULL;
457 input_userauth_error(int type, u_int32_t seq, void *ctxt)
459 fatal("input_userauth_error: bad message during authentication: "
464 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
468 debug3("input_userauth_banner");
469 msg = packet_get_string(NULL);
470 lang = packet_get_string(NULL);
471 if (options.log_level >= SYSLOG_LEVEL_INFO)
472 fprintf(stderr, "%s", msg);
478 input_userauth_success(int type, u_int32_t seq, void *ctxt)
480 Authctxt *authctxt = ctxt;
481 if (authctxt == NULL)
482 fatal("input_userauth_success: no authentication context");
483 if (authctxt->authlist) {
484 xfree(authctxt->authlist);
485 authctxt->authlist = NULL;
487 if (authctxt->methoddata) {
488 xfree(authctxt->methoddata);
489 authctxt->methoddata = NULL;
491 authctxt->success = 1; /* break out */
495 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
497 Authctxt *authctxt = ctxt;
498 char *authlist = NULL;
501 if (authctxt == NULL)
502 fatal("input_userauth_failure: no authentication context");
504 authlist = packet_get_string(NULL);
505 partial = packet_get_char();
509 logit("Authenticated with partial success.");
510 debug("Authentications that can continue: %s", authlist);
512 userauth(authctxt, authlist);
515 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
517 Authctxt *authctxt = ctxt;
521 int pktype, sent = 0;
526 if (authctxt == NULL)
527 fatal("input_userauth_pk_ok: no authentication context");
528 if (datafellows & SSH_BUG_PKOK) {
529 /* this is similar to SSH_BUG_PKAUTH */
530 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
531 pkblob = packet_get_string(&blen);
533 buffer_append(&b, pkblob, blen);
534 pkalg = buffer_get_string(&b, &alen);
537 pkalg = packet_get_string(&alen);
538 pkblob = packet_get_string(&blen);
542 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
544 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
545 debug("unknown pkalg %s", pkalg);
548 if ((key = key_from_blob(pkblob, blen)) == NULL) {
549 debug("no key from blob. pkalg %s", pkalg);
552 if (key->type != pktype) {
553 error("input_userauth_pk_ok: type mismatch "
554 "for decoded key (received %d, expected %d)",
558 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
559 debug2("input_userauth_pk_ok: fp %s", fp);
563 * search keys in the reverse order, because last candidate has been
564 * moved to the end of the queue. this also avoids confusion by
567 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
568 if (key_equal(key, id->key)) {
569 sent = sign_and_send_pubkey(authctxt, id);
579 /* try another method if we did not send a packet */
581 userauth(authctxt, NULL);
586 userauth_gssapi(Authctxt *authctxt)
588 Gssctxt *gssctxt = NULL;
589 static gss_OID_set gss_supported = NULL;
590 static u_int mech = 0;
593 char *gss_host = NULL;
595 if (!options.gss_authentication) {
596 verbose("GSSAPI authentication disabled.");
600 if (options.gss_trust_dns)
601 gss_host = (char *)get_canonical_hostname(1);
603 gss_host = (char *)authctxt->host;
605 /* Try one GSSAPI method at a time, rather than sending them all at
608 if (gss_supported == NULL)
609 gss_indicate_mechs(&min, &gss_supported);
611 /* Check to see if the mechanism is usable before we offer it */
612 while (mech < gss_supported->count && !ok) {
613 /* My DER encoding requires length<128 */
614 if (gss_supported->elements[mech].length < 128 &&
615 ssh_gssapi_check_mechanism(&gssctxt,
616 &gss_supported->elements[mech], gss_host)) {
617 ok = 1; /* Mechanism works */
626 authctxt->methoddata=(void *)gssctxt;
628 packet_start(SSH2_MSG_USERAUTH_REQUEST);
629 packet_put_cstring(authctxt->server_user);
630 packet_put_cstring(authctxt->service);
631 packet_put_cstring(authctxt->method->name);
635 packet_put_int((gss_supported->elements[mech].length) + 2);
636 packet_put_char(SSH_GSS_OIDTYPE);
637 packet_put_char(gss_supported->elements[mech].length);
638 packet_put_raw(gss_supported->elements[mech].elements,
639 gss_supported->elements[mech].length);
643 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
644 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
645 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
646 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
648 mech++; /* Move along to next candidate */
654 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
656 Authctxt *authctxt = ctxt;
657 Gssctxt *gssctxt = authctxt->methoddata;
658 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
659 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
660 gss_buffer_desc gssbuf;
661 OM_uint32 status, ms, flags;
664 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
665 recv_tok, &send_tok, &flags);
667 if (send_tok.length > 0) {
668 if (GSS_ERROR(status))
669 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
671 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
673 packet_put_string(send_tok.value, send_tok.length);
675 gss_release_buffer(&ms, &send_tok);
678 if (status == GSS_S_COMPLETE) {
679 /* send either complete or MIC, depending on mechanism */
680 if (strcmp(authctxt->method->name,"gssapi")==0 ||
681 (!(flags & GSS_C_INTEG_FLAG))) {
682 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
685 ssh_gssapi_buildmic(&b, authctxt->server_user,
686 authctxt->service, "gssapi-with-mic");
688 gssbuf.value = buffer_ptr(&b);
689 gssbuf.length = buffer_len(&b);
691 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
693 if (!GSS_ERROR(status)) {
694 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
695 packet_put_string(mic.value, mic.length);
701 gss_release_buffer(&ms, &mic);
709 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
711 Authctxt *authctxt = ctxt;
716 if (authctxt == NULL)
717 fatal("input_gssapi_response: no authentication context");
718 gssctxt = authctxt->methoddata;
721 oidv = packet_get_string(&oidlen);
724 oidv[0] != SSH_GSS_OIDTYPE ||
725 oidv[1] != oidlen - 2) {
727 debug("Badly encoded mechanism OID received");
728 userauth(authctxt, NULL);
732 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
733 fatal("Server returned different OID than expected");
739 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
740 /* Start again with next method on list */
741 debug("Trying to start again");
742 userauth(authctxt, NULL);
748 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
750 Authctxt *authctxt = ctxt;
751 gss_buffer_desc recv_tok;
755 if (authctxt == NULL)
756 fatal("input_gssapi_response: no authentication context");
758 recv_tok.value = packet_get_string(&slen);
759 recv_tok.length = slen; /* safe typecast */
763 status = process_gssapi_token(ctxt, &recv_tok);
765 xfree(recv_tok.value);
767 if (GSS_ERROR(status)) {
768 /* Start again with the next method in the list */
769 userauth(authctxt, NULL);
775 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
777 Authctxt *authctxt = ctxt;
779 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
780 gss_buffer_desc recv_tok;
781 OM_uint32 status, ms;
784 if (authctxt == NULL)
785 fatal("input_gssapi_response: no authentication context");
786 gssctxt = authctxt->methoddata;
788 recv_tok.value = packet_get_string(&len);
789 recv_tok.length = len;
793 /* Stick it into GSSAPI and see what it says */
794 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
795 &recv_tok, &send_tok, NULL);
797 xfree(recv_tok.value);
798 gss_release_buffer(&ms, &send_tok);
800 /* Server will be returning a failed packet after this one */
804 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
810 maj=packet_get_int();
811 min=packet_get_int();
812 msg=packet_get_string(NULL);
813 lang=packet_get_string(NULL);
817 debug("Server GSSAPI Error:\n%s", msg);
824 const gss_OID_desc * const gss_mech_globus_gssapi_openssl;
825 #define is_gsi_oid(oid) \
826 (oid->length == gss_mech_globus_gssapi_openssl->length && \
827 (memcmp(oid->elements, gss_mech_globus_gssapi_openssl->elements, \
832 userauth_external(Authctxt *authctxt)
834 static int attempt = 0;
839 /* The client MUST NOT try this method if initial key exchange
840 was not performed using a GSSAPI-based key exchange
842 if (gss_kex_context == NULL) {
843 debug2("gsskex not performed, skipping external-keyx");
847 debug2("userauth_external");
848 packet_start(SSH2_MSG_USERAUTH_REQUEST);
850 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
851 packet_put_cstring("");
854 packet_put_cstring(authctxt->server_user);
858 packet_put_cstring(authctxt->service);
859 packet_put_cstring(authctxt->method->name);
865 userauth_gsskeyex(Authctxt *authctxt)
868 gss_buffer_desc gssbuf;
869 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
872 static int attempt = 0;
876 if (gss_kex_context == NULL) {
877 debug("No valid Key exchange context");
882 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
883 ssh_gssapi_buildmic(&b, "", authctxt->service, "gssapi-keyex");
886 ssh_gssapi_buildmic(&b, authctxt->server_user, authctxt->service,
892 gssbuf.value = buffer_ptr(&b);
893 gssbuf.length = buffer_len(&b);
895 if (GSS_ERROR(ssh_gssapi_sign(gss_kex_context, &gssbuf, &mic))) {
900 packet_start(SSH2_MSG_USERAUTH_REQUEST);
902 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
903 packet_put_cstring("");
906 packet_put_cstring(authctxt->server_user);
910 packet_put_cstring(authctxt->service);
911 packet_put_cstring(authctxt->method->name);
912 packet_put_string(mic.value, mic.length);
916 gss_release_buffer(&ms, &mic);
924 userauth_none(Authctxt *authctxt)
926 /* initial userauth request */
927 packet_start(SSH2_MSG_USERAUTH_REQUEST);
928 packet_put_cstring(authctxt->server_user);
929 packet_put_cstring(authctxt->service);
930 packet_put_cstring(authctxt->method->name);
936 userauth_passwd(Authctxt *authctxt)
938 static int attempt = 0;
942 if (attempt++ >= options.number_of_password_prompts)
946 error("Permission denied, please try again.");
948 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
949 authctxt->server_user, authctxt->host);
950 password = read_passphrase(prompt, 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);
956 packet_put_cstring(password);
957 memset(password, 0, strlen(password));
959 packet_add_padding(64);
962 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
963 &input_userauth_passwd_changereq);
968 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
971 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
973 Authctxt *authctxt = ctxt;
974 char *info, *lang, *password = NULL, *retype = NULL;
977 debug2("input_userauth_passwd_changereq");
979 if (authctxt == NULL)
980 fatal("input_userauth_passwd_changereq: "
981 "no authentication context");
983 info = packet_get_string(NULL);
984 lang = packet_get_string(NULL);
985 if (strlen(info) > 0)
989 packet_start(SSH2_MSG_USERAUTH_REQUEST);
990 packet_put_cstring(authctxt->server_user);
991 packet_put_cstring(authctxt->service);
992 packet_put_cstring(authctxt->method->name);
993 packet_put_char(1); /* additional info */
994 snprintf(prompt, sizeof(prompt),
995 "Enter %.30s@%.128s's old password: ",
996 authctxt->server_user, authctxt->host);
997 password = read_passphrase(prompt, 0);
998 packet_put_cstring(password);
999 memset(password, 0, strlen(password));
1002 while (password == NULL) {
1003 snprintf(prompt, sizeof(prompt),
1004 "Enter %.30s@%.128s's new password: ",
1005 authctxt->server_user, authctxt->host);
1006 password = read_passphrase(prompt, RP_ALLOW_EOF);
1007 if (password == NULL) {
1011 snprintf(prompt, sizeof(prompt),
1012 "Retype %.30s@%.128s's new password: ",
1013 authctxt->server_user, authctxt->host);
1014 retype = read_passphrase(prompt, 0);
1015 if (strcmp(password, retype) != 0) {
1016 memset(password, 0, strlen(password));
1018 logit("Mismatch; try again, EOF to quit.");
1021 memset(retype, 0, strlen(retype));
1024 packet_put_cstring(password);
1025 memset(password, 0, strlen(password));
1027 packet_add_padding(64);
1030 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1031 &input_userauth_passwd_changereq);
1035 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1036 u_char *data, u_int datalen)
1041 /* the agent supports this key */
1043 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1046 * we have already loaded the private key or
1047 * the private key is stored in external hardware
1049 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1050 return (key_sign(id->key, sigp, lenp, data, datalen));
1051 /* load the private key from the file */
1052 if ((prv = load_identity_file(id->filename)) == NULL)
1054 ret = key_sign(prv, sigp, lenp, data, datalen);
1060 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1063 u_char *blob, *signature;
1064 u_int bloblen, slen;
1069 debug3("sign_and_send_pubkey");
1071 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1072 /* we cannot handle this key */
1073 debug3("sign_and_send_pubkey: cannot handle key");
1076 /* data to be signed */
1078 if (datafellows & SSH_OLD_SESSIONID) {
1079 buffer_append(&b, session_id2, session_id2_len);
1080 skip = session_id2_len;
1082 buffer_put_string(&b, session_id2, session_id2_len);
1083 skip = buffer_len(&b);
1085 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1086 buffer_put_cstring(&b, authctxt->server_user);
1087 buffer_put_cstring(&b,
1088 datafellows & SSH_BUG_PKSERVICE ?
1091 if (datafellows & SSH_BUG_PKAUTH) {
1092 buffer_put_char(&b, have_sig);
1094 buffer_put_cstring(&b, authctxt->method->name);
1095 buffer_put_char(&b, have_sig);
1096 buffer_put_cstring(&b, key_ssh_name(id->key));
1098 buffer_put_string(&b, blob, bloblen);
1100 /* generate signature */
1101 ret = identity_sign(id, &signature, &slen,
1102 buffer_ptr(&b), buffer_len(&b));
1111 if (datafellows & SSH_BUG_PKSERVICE) {
1113 buffer_append(&b, session_id2, session_id2_len);
1114 skip = session_id2_len;
1115 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1116 buffer_put_cstring(&b, authctxt->server_user);
1117 buffer_put_cstring(&b, authctxt->service);
1118 buffer_put_cstring(&b, authctxt->method->name);
1119 buffer_put_char(&b, have_sig);
1120 if (!(datafellows & SSH_BUG_PKAUTH))
1121 buffer_put_cstring(&b, key_ssh_name(id->key));
1122 buffer_put_string(&b, blob, bloblen);
1126 /* append signature */
1127 buffer_put_string(&b, signature, slen);
1130 /* skip session id and packet type */
1131 if (buffer_len(&b) < skip + 1)
1132 fatal("userauth_pubkey: internal error");
1133 buffer_consume(&b, skip + 1);
1135 /* put remaining data from buffer into packet */
1136 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1137 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1145 send_pubkey_test(Authctxt *authctxt, Identity *id)
1148 u_int bloblen, have_sig = 0;
1150 debug3("send_pubkey_test");
1152 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1153 /* we cannot handle this key */
1154 debug3("send_pubkey_test: cannot handle key");
1157 /* register callback for USERAUTH_PK_OK message */
1158 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1160 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1161 packet_put_cstring(authctxt->server_user);
1162 packet_put_cstring(authctxt->service);
1163 packet_put_cstring(authctxt->method->name);
1164 packet_put_char(have_sig);
1165 if (!(datafellows & SSH_BUG_PKAUTH))
1166 packet_put_cstring(key_ssh_name(id->key));
1167 packet_put_string(blob, bloblen);
1174 load_identity_file(char *filename)
1177 char prompt[300], *passphrase;
1178 int perm_ok, quit, i;
1181 if (stat(filename, &st) < 0) {
1182 debug3("no such identity: %s", filename);
1185 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1188 if (private == NULL) {
1189 if (options.batch_mode)
1191 snprintf(prompt, sizeof prompt,
1192 "Enter passphrase for key '%.100s': ", filename);
1193 for (i = 0; i < options.number_of_password_prompts; i++) {
1194 passphrase = read_passphrase(prompt, 0);
1195 if (strcmp(passphrase, "") != 0) {
1196 private = key_load_private_type(KEY_UNSPEC,
1197 filename, passphrase, NULL, NULL);
1200 debug2("no passphrase given, try next key");
1203 memset(passphrase, 0, strlen(passphrase));
1205 if (private != NULL || quit)
1207 debug2("bad passphrase given, try again...");
1214 * try keys in the following order:
1215 * 1. agent keys that are found in the config file
1216 * 2. other agent keys
1217 * 3. keys that are only listed in the config file
1220 pubkey_prepare(Authctxt *authctxt)
1223 Idlist agent, files, *preferred;
1225 AuthenticationConnection *ac;
1229 TAILQ_INIT(&agent); /* keys from the agent */
1230 TAILQ_INIT(&files); /* keys from the config file */
1231 preferred = &authctxt->keys;
1232 TAILQ_INIT(preferred); /* preferred order of keys */
1234 /* list of keys stored in the filesystem */
1235 for (i = 0; i < options.num_identity_files; i++) {
1236 key = options.identity_keys[i];
1237 if (key && key->type == KEY_RSA1)
1239 options.identity_keys[i] = NULL;
1240 id = xcalloc(1, sizeof(*id));
1242 id->filename = xstrdup(options.identity_files[i]);
1243 TAILQ_INSERT_TAIL(&files, id, next);
1245 /* list of keys supported by the agent */
1246 if ((ac = ssh_get_authentication_connection())) {
1247 for (key = ssh_get_first_identity(ac, &comment, 2);
1249 key = ssh_get_next_identity(ac, &comment, 2)) {
1251 TAILQ_FOREACH(id, &files, next) {
1252 /* agent keys from the config file are preferred */
1253 if (key_equal(key, id->key)) {
1256 TAILQ_REMOVE(&files, id, next);
1257 TAILQ_INSERT_TAIL(preferred, id, next);
1263 if (!found && !options.identities_only) {
1264 id = xcalloc(1, sizeof(*id));
1266 id->filename = comment;
1268 TAILQ_INSERT_TAIL(&agent, id, next);
1271 /* append remaining agent keys */
1272 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1273 TAILQ_REMOVE(&agent, id, next);
1274 TAILQ_INSERT_TAIL(preferred, id, next);
1276 authctxt->agent = ac;
1278 /* append remaining keys from the config file */
1279 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1280 TAILQ_REMOVE(&files, id, next);
1281 TAILQ_INSERT_TAIL(preferred, id, next);
1283 TAILQ_FOREACH(id, preferred, next) {
1284 debug2("key: %s (%p)", id->filename, id->key);
1289 pubkey_cleanup(Authctxt *authctxt)
1293 if (authctxt->agent != NULL)
1294 ssh_close_authentication_connection(authctxt->agent);
1295 for (id = TAILQ_FIRST(&authctxt->keys); id;
1296 id = TAILQ_FIRST(&authctxt->keys)) {
1297 TAILQ_REMOVE(&authctxt->keys, id, next);
1301 xfree(id->filename);
1307 userauth_pubkey(Authctxt *authctxt)
1312 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1315 /* move key to the end of the queue */
1316 TAILQ_REMOVE(&authctxt->keys, id, next);
1317 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1319 * send a test message if we have the public key. for
1320 * encrypted keys we cannot do this and have to load the
1321 * private key instead
1323 if (id->key && id->key->type != KEY_RSA1) {
1324 debug("Offering public key: %s", id->filename);
1325 sent = send_pubkey_test(authctxt, id);
1326 } else if (id->key == NULL) {
1327 debug("Trying private key: %s", id->filename);
1328 id->key = load_identity_file(id->filename);
1329 if (id->key != NULL) {
1331 sent = sign_and_send_pubkey(authctxt, id);
1343 * Send userauth request message specifying keyboard-interactive method.
1346 userauth_kbdint(Authctxt *authctxt)
1348 static int attempt = 0;
1350 if (attempt++ >= options.number_of_password_prompts)
1352 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1353 if (attempt > 1 && !authctxt->info_req_seen) {
1354 debug3("userauth_kbdint: disable: no info_req_seen");
1355 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1359 debug2("userauth_kbdint");
1360 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1361 packet_put_cstring(authctxt->server_user);
1362 packet_put_cstring(authctxt->service);
1363 packet_put_cstring(authctxt->method->name);
1364 packet_put_cstring(""); /* lang */
1365 packet_put_cstring(options.kbd_interactive_devices ?
1366 options.kbd_interactive_devices : "");
1369 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1374 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1377 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1379 Authctxt *authctxt = ctxt;
1380 char *name, *inst, *lang, *prompt, *response;
1381 u_int num_prompts, i;
1384 debug2("input_userauth_info_req");
1386 if (authctxt == NULL)
1387 fatal("input_userauth_info_req: no authentication context");
1389 authctxt->info_req_seen = 1;
1391 name = packet_get_string(NULL);
1392 inst = packet_get_string(NULL);
1393 lang = packet_get_string(NULL);
1394 if (strlen(name) > 0)
1396 if (strlen(inst) > 0)
1402 num_prompts = packet_get_int();
1404 * Begin to build info response packet based on prompts requested.
1405 * We commit to providing the correct number of responses, so if
1406 * further on we run into a problem that prevents this, we have to
1407 * be sure and clean this up and send a correct error response.
1409 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1410 packet_put_int(num_prompts);
1412 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1413 for (i = 0; i < num_prompts; i++) {
1414 prompt = packet_get_string(NULL);
1415 echo = packet_get_char();
1417 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1419 packet_put_cstring(response);
1420 memset(response, 0, strlen(response));
1424 packet_check_eom(); /* done with parsing incoming message. */
1426 packet_add_padding(64);
1431 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1432 u_char *data, u_int datalen)
1437 int to[2], from[2], status, version = 2;
1439 debug2("ssh_keysign called");
1441 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1442 error("ssh_keysign: no installed: %s", strerror(errno));
1445 if (fflush(stdout) != 0)
1446 error("ssh_keysign: fflush: %s", strerror(errno));
1448 error("ssh_keysign: pipe: %s", strerror(errno));
1451 if (pipe(from) < 0) {
1452 error("ssh_keysign: pipe: %s", strerror(errno));
1455 if ((pid = fork()) < 0) {
1456 error("ssh_keysign: fork: %s", strerror(errno));
1460 permanently_drop_suid(getuid());
1462 if (dup2(from[1], STDOUT_FILENO) < 0)
1463 fatal("ssh_keysign: dup2: %s", strerror(errno));
1465 if (dup2(to[0], STDIN_FILENO) < 0)
1466 fatal("ssh_keysign: dup2: %s", strerror(errno));
1469 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1470 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1477 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1478 buffer_put_string(&b, data, datalen);
1479 if (ssh_msg_send(to[1], version, &b) == -1)
1480 fatal("ssh_keysign: couldn't send request");
1482 if (ssh_msg_recv(from[0], &b) < 0) {
1483 error("ssh_keysign: no reply");
1490 while (waitpid(pid, &status, 0) < 0)
1494 if (buffer_get_char(&b) != version) {
1495 error("ssh_keysign: bad version");
1499 *sigp = buffer_get_string(&b, lenp);
1506 userauth_hostbased(Authctxt *authctxt)
1508 Key *private = NULL;
1509 Sensitive *sensitive = authctxt->sensitive;
1511 u_char *signature, *blob;
1512 char *chost, *pkalg, *p, myname[NI_MAXHOST];
1513 const char *service;
1515 int ok, i, len, found = 0;
1517 /* check for a useful key */
1518 for (i = 0; i < sensitive->nkeys; i++) {
1519 private = sensitive->keys[i];
1520 if (private && private->type != KEY_RSA1) {
1522 /* we take and free the key */
1523 sensitive->keys[i] = NULL;
1528 debug("No more client hostkeys for hostbased authentication.");
1531 if (key_to_blob(private, &blob, &blen) == 0) {
1535 /* figure out a name for the client host */
1537 if (packet_connection_is_on_socket())
1538 p = get_local_name(packet_get_connection_in());
1540 if (gethostname(myname, sizeof(myname)) == -1) {
1541 verbose("userauth_hostbased: gethostname: %s",
1544 p = xstrdup(myname);
1547 error("userauth_hostbased: cannot get local ipaddr/name");
1552 len = strlen(p) + 2;
1553 xasprintf(&chost, "%s.", p);
1554 debug2("userauth_hostbased: chost %s", chost);
1557 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1559 pkalg = xstrdup(key_ssh_name(private));
1561 /* construct data */
1562 buffer_put_string(&b, session_id2, session_id2_len);
1563 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1564 buffer_put_cstring(&b, authctxt->server_user);
1565 buffer_put_cstring(&b, service);
1566 buffer_put_cstring(&b, authctxt->method->name);
1567 buffer_put_cstring(&b, pkalg);
1568 buffer_put_string(&b, blob, blen);
1569 buffer_put_cstring(&b, chost);
1570 buffer_put_cstring(&b, authctxt->local_user);
1574 if (sensitive->external_keysign)
1575 ok = ssh_keysign(private, &signature, &slen,
1576 buffer_ptr(&b), buffer_len(&b));
1578 ok = key_sign(private, &signature, &slen,
1579 buffer_ptr(&b), buffer_len(&b));
1583 error("key_sign failed");
1589 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1590 packet_put_cstring(authctxt->server_user);
1591 packet_put_cstring(authctxt->service);
1592 packet_put_cstring(authctxt->method->name);
1593 packet_put_cstring(pkalg);
1594 packet_put_string(blob, blen);
1595 packet_put_cstring(chost);
1596 packet_put_cstring(authctxt->local_user);
1597 packet_put_string(signature, slen);
1598 memset(signature, 's', slen);
1608 /* find auth method */
1611 * given auth method name, if configurable options permit this method fill
1612 * in auth_ident field and return true, otherwise return false.
1615 authmethod_is_enabled(Authmethod *method)
1619 /* return false if options indicate this method is disabled */
1620 if (method->enabled == NULL || *method->enabled == 0)
1622 /* return false if batch mode is enabled but method needs interactive mode */
1623 if (method->batch_flag != NULL && *method->batch_flag != 0)
1629 authmethod_lookup(const char *name)
1631 Authmethod *method = NULL;
1633 for (method = authmethods; method->name != NULL; method++)
1634 if (strcmp(name, method->name) == 0)
1636 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1640 /* XXX internal state */
1641 static Authmethod *current = NULL;
1642 static char *supported = NULL;
1643 static char *preferred = NULL;
1646 * Given the authentication method list sent by the server, return the
1647 * next method we should try. If the server initially sends a nil list,
1648 * use a built-in default list.
1651 authmethod_get(char *authlist)
1656 /* Use a suitable default if we're passed a nil list. */
1657 if (authlist == NULL || strlen(authlist) == 0)
1658 authlist = options.preferred_authentications;
1660 if (supported == NULL || strcmp(authlist, supported) != 0) {
1661 debug3("start over, passed a different list %s", authlist);
1662 if (supported != NULL)
1664 supported = xstrdup(authlist);
1665 preferred = options.preferred_authentications;
1666 debug3("preferred %s", preferred);
1668 } else if (current != NULL && authmethod_is_enabled(current))
1672 if ((name = match_list(preferred, supported, &next)) == NULL) {
1673 debug("No more authentication methods to try.");
1678 debug3("authmethod_lookup %s", name);
1679 debug3("remaining preferred: %s", preferred);
1680 if ((current = authmethod_lookup(name)) != NULL &&
1681 authmethod_is_enabled(current)) {
1682 debug3("authmethod_is_enabled %s", name);
1683 debug("Next authentication method: %s", name);
1690 authmethods_get(void)
1692 Authmethod *method = NULL;
1697 for (method = authmethods; method->name != NULL; method++) {
1698 if (authmethod_is_enabled(method)) {
1699 if (buffer_len(&b) > 0)
1700 buffer_append(&b, ",", 1);
1701 buffer_append(&b, method->name, strlen(method->name));
1704 buffer_append(&b, "\0", 1);
1705 list = xstrdup(buffer_ptr(&b));