2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 RCSID("$OpenBSD: sshconnect2.c,v 1.143 2005/10/14 02:17:59 stevesk Exp $");
28 #include "openbsd-compat/sys-queue.h"
39 #include "myproposal.h"
40 #include "sshconnect.h"
51 #include "pathnames.h"
58 extern char *client_version_string;
59 extern char *server_version_string;
60 extern Options options;
63 /* tty_flag is set in ssh.c. use this in ssh_userauth2 */
64 /* if it is set then prevent the switch to the null cipher */
72 u_char *session_id2 = NULL;
73 u_int session_id2_len = 0;
76 struct sockaddr *xxx_hostaddr;
81 verify_host_key_callback(Key *hostkey)
83 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
84 fatal("Host key verification failed.");
89 ssh_kex2(char *host, struct sockaddr *hostaddr)
94 char *orig = NULL, *gss = NULL;
96 char *gss_host = NULL;
100 xxx_hostaddr = hostaddr;
103 if (options.gss_keyex) {
104 /* Add the GSSAPI mechanisms currently supported on this client to
105 * the key exchange algorithm proposal */
106 orig = myproposal[PROPOSAL_KEX_ALGS];
107 if (options.gss_trust_dns)
108 gss_host = (char *)get_canonical_hostname(1);
112 gss = ssh_gssapi_client_mechanisms(gss_host);
114 debug("Offering GSSAPI proposal: %s", gss);
115 len = strlen(orig) + strlen(gss) + 2;
116 myproposal[PROPOSAL_KEX_ALGS] = xmalloc(len);
117 snprintf(myproposal[PROPOSAL_KEX_ALGS], len, "%s,%s", gss,
123 if (options.ciphers == (char *)-1) {
124 logit("No valid ciphers for protocol version 2 given, using defaults.");
125 options.ciphers = NULL;
127 if (options.ciphers != NULL) {
128 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
129 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
131 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
132 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
133 myproposal[PROPOSAL_ENC_ALGS_STOC] =
134 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
135 if (options.compression) {
136 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
137 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
139 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
140 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
142 if (options.macs != NULL) {
143 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
144 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
146 if (options.hostkeyalgorithms != NULL)
147 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
148 options.hostkeyalgorithms;
151 /* If we've got GSSAPI algorithms, then we also support the
152 * 'null' hostkey, as a last resort */
153 if (options.gss_keyex && gss) {
154 orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
155 len = strlen(orig) + sizeof(",null");
156 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = xmalloc(len);
157 snprintf(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS], len,
162 if (options.rekey_limit)
163 packet_set_rekey_limit(options.rekey_limit);
165 /* start key exchange */
166 kex = kex_setup(myproposal);
167 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
168 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
169 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
171 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
172 kex->kex[KEX_GSS_GEX_SHA1] = kexgss_client;
174 kex->client_version_string=client_version_string;
175 kex->server_version_string=server_version_string;
176 kex->verify_host_key=&verify_host_key_callback;
179 kex->gss_deleg_creds = options.gss_deleg_creds;
180 kex->gss_trust_dns = options.gss_trust_dns;
181 kex->gss_host = gss_host;
186 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
188 session_id2 = kex->session_id;
189 session_id2_len = kex->session_id_len;
192 /* send 1st encrypted/maced/compressed message */
193 packet_start(SSH2_MSG_IGNORE);
194 packet_put_cstring("markus");
204 typedef struct Authctxt Authctxt;
205 typedef struct Authmethod Authmethod;
206 typedef struct identity Identity;
207 typedef struct idlist Idlist;
210 TAILQ_ENTRY(identity) next;
211 AuthenticationConnection *ac; /* set if agent supports key */
212 Key *key; /* public/private key */
213 char *filename; /* comment for agent-only keys */
215 int isprivate; /* key points to the private key */
217 TAILQ_HEAD(idlist, identity);
220 const char *server_user;
221 const char *local_user;
229 AuthenticationConnection *agent;
231 Sensitive *sensitive;
232 /* kbd-interactive */
238 char *name; /* string to compare against server's list */
239 int (*userauth)(Authctxt *authctxt);
240 int *enabled; /* flag in option struct that enables method */
241 int *batch_flag; /* flag in option struct that disables method */
244 void input_userauth_success(int, u_int32_t, void *);
245 void input_userauth_failure(int, u_int32_t, void *);
246 void input_userauth_banner(int, u_int32_t, void *);
247 void input_userauth_error(int, u_int32_t, void *);
248 void input_userauth_info_req(int, u_int32_t, void *);
249 void input_userauth_pk_ok(int, u_int32_t, void *);
250 void input_userauth_passwd_changereq(int, u_int32_t, void *);
252 int userauth_none(Authctxt *);
253 int userauth_pubkey(Authctxt *);
254 int userauth_passwd(Authctxt *);
255 int userauth_kbdint(Authctxt *);
256 int userauth_hostbased(Authctxt *);
257 int userauth_kerberos(Authctxt *);
260 int userauth_external(Authctxt *authctxt);
261 int userauth_gssapi(Authctxt *authctxt);
262 int userauth_gssapi_with_mic(Authctxt *authctxt);
263 int userauth_gssapi_without_mic(Authctxt *authctxt);
264 void input_gssapi_response(int type, u_int32_t, void *);
265 void input_gssapi_token(int type, u_int32_t, void *);
266 void input_gssapi_hash(int type, u_int32_t, void *);
267 void input_gssapi_error(int, u_int32_t, void *);
268 void input_gssapi_errtok(int, u_int32_t, void *);
269 int userauth_gsskeyex(Authctxt *authctxt);
272 void userauth(Authctxt *, char *);
274 static int sign_and_send_pubkey(Authctxt *, Identity *);
275 static void pubkey_prepare(Authctxt *);
276 static void pubkey_cleanup(Authctxt *);
277 static Key *load_identity_file(char *);
279 static Authmethod *authmethod_get(char *authlist);
280 static Authmethod *authmethod_lookup(const char *name);
281 static char *authmethods_get(void);
283 Authmethod authmethods[] = {
287 &options.gss_authentication,
291 &options.gss_authentication,
295 &options.gss_authentication,
299 &options.gss_authentication,
304 &options.hostbased_authentication,
308 &options.pubkey_authentication,
310 {"keyboard-interactive",
312 &options.kbd_interactive_authentication,
313 &options.batch_mode},
316 &options.password_authentication,
317 &options.batch_mode},
322 {NULL, NULL, NULL, NULL}
326 ssh_userauth2(const char *local_user, const char *server_user, char *host,
327 Sensitive *sensitive)
332 if (options.challenge_response_authentication)
333 options.kbd_interactive_authentication = 1;
335 packet_start(SSH2_MSG_SERVICE_REQUEST);
336 packet_put_cstring("ssh-userauth");
338 debug("SSH2_MSG_SERVICE_REQUEST sent");
340 type = packet_read();
341 if (type != SSH2_MSG_SERVICE_ACCEPT)
342 fatal("Server denied authentication request: %d", type);
343 if (packet_remaining() > 0) {
344 char *reply = packet_get_string(NULL);
345 debug2("service_accept: %s", reply);
348 debug2("buggy server: service_accept w/o service");
351 debug("SSH2_MSG_SERVICE_ACCEPT received");
353 if (options.preferred_authentications == NULL)
354 options.preferred_authentications = authmethods_get();
356 /* setup authentication context */
357 memset(&authctxt, 0, sizeof(authctxt));
358 pubkey_prepare(&authctxt);
359 authctxt.server_user = server_user;
360 authctxt.local_user = local_user;
361 authctxt.host = host;
362 authctxt.service = "ssh-connection"; /* service name */
363 authctxt.success = 0;
364 authctxt.method = authmethod_lookup("none");
365 authctxt.authlist = NULL;
366 authctxt.methoddata = NULL;
367 authctxt.sensitive = sensitive;
368 authctxt.info_req_seen = 0;
369 if (authctxt.method == NULL)
370 fatal("ssh_userauth2: internal error: cannot send userauth none request");
372 /* initial userauth request */
373 userauth_none(&authctxt);
375 dispatch_init(&input_userauth_error);
376 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
377 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
378 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
379 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
381 pubkey_cleanup(&authctxt);
382 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
383 if ((options.none_switch == 1) && !tty_flag) /* no null on tty sessions */
385 debug("Requesting none rekeying...");
386 myproposal[PROPOSAL_ENC_ALGS_STOC] = "none";
387 myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none";
388 kex_prop2buf(&xxx_kex->my,myproposal);
389 packet_request_rekeying();
390 fprintf(stderr, "WARNING: ENABLED NULL CIPHER\n");
392 debug("Authentication succeeded (%s).", authctxt.method->name);
396 userauth(Authctxt *authctxt, char *authlist)
398 if (authctxt->methoddata) {
399 xfree(authctxt->methoddata);
400 authctxt->methoddata = NULL;
402 if (authlist == NULL) {
403 authlist = authctxt->authlist;
405 if (authctxt->authlist)
406 xfree(authctxt->authlist);
407 authctxt->authlist = authlist;
410 Authmethod *method = authmethod_get(authlist);
412 fatal("Permission denied (%s).", authlist);
413 authctxt->method = method;
415 /* reset the per method handler */
416 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
417 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
419 /* and try new method */
420 if (method->userauth(authctxt) != 0) {
421 debug2("we sent a %s packet, wait for reply", method->name);
424 debug2("we did not send a packet, disable method");
425 method->enabled = NULL;
431 input_userauth_error(int type, u_int32_t seq, void *ctxt)
433 fatal("input_userauth_error: bad message during authentication: "
438 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
442 debug3("input_userauth_banner");
443 msg = packet_get_string(NULL);
444 lang = packet_get_string(NULL);
445 if (options.log_level > SYSLOG_LEVEL_QUIET)
446 fprintf(stderr, "%s", msg);
452 input_userauth_success(int type, u_int32_t seq, void *ctxt)
454 Authctxt *authctxt = ctxt;
455 if (authctxt == NULL)
456 fatal("input_userauth_success: no authentication context");
457 if (authctxt->authlist) {
458 xfree(authctxt->authlist);
459 authctxt->authlist = NULL;
461 if (authctxt->methoddata) {
462 xfree(authctxt->methoddata);
463 authctxt->methoddata = NULL;
465 authctxt->success = 1; /* break out */
469 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
471 Authctxt *authctxt = ctxt;
472 char *authlist = NULL;
475 if (authctxt == NULL)
476 fatal("input_userauth_failure: no authentication context");
478 authlist = packet_get_string(NULL);
479 partial = packet_get_char();
483 logit("Authenticated with partial success.");
484 debug("Authentications that can continue: %s", authlist);
486 userauth(authctxt, authlist);
489 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
491 Authctxt *authctxt = ctxt;
495 int pktype, sent = 0;
500 if (authctxt == NULL)
501 fatal("input_userauth_pk_ok: no authentication context");
502 if (datafellows & SSH_BUG_PKOK) {
503 /* this is similar to SSH_BUG_PKAUTH */
504 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
505 pkblob = packet_get_string(&blen);
507 buffer_append(&b, pkblob, blen);
508 pkalg = buffer_get_string(&b, &alen);
511 pkalg = packet_get_string(&alen);
512 pkblob = packet_get_string(&blen);
516 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
518 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
519 debug("unknown pkalg %s", pkalg);
522 if ((key = key_from_blob(pkblob, blen)) == NULL) {
523 debug("no key from blob. pkalg %s", pkalg);
526 if (key->type != pktype) {
527 error("input_userauth_pk_ok: type mismatch "
528 "for decoded key (received %d, expected %d)",
532 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
533 debug2("input_userauth_pk_ok: fp %s", fp);
537 * search keys in the reverse order, because last candidate has been
538 * moved to the end of the queue. this also avoids confusion by
541 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
542 if (key_equal(key, id->key)) {
543 sent = sign_and_send_pubkey(authctxt, id);
553 /* try another method if we did not send a packet */
555 userauth(authctxt, NULL);
560 userauth_gssapi(Authctxt *authctxt)
562 Gssctxt *gssctxt = NULL;
563 static gss_OID_set gss_supported = NULL;
564 static u_int mech = 0;
567 char *gss_host = NULL;
569 if (!options.gss_authentication) {
570 verbose("GSSAPI authentication disabled.");
574 if (options.gss_trust_dns)
575 gss_host = (char *)get_canonical_hostname(1);
577 gss_host = (char *)authctxt->host;
579 /* Try one GSSAPI method at a time, rather than sending them all at
582 if (gss_supported == NULL)
583 gss_indicate_mechs(&min, &gss_supported);
585 /* Check to see if the mechanism is usable before we offer it */
586 while (mech < gss_supported->count && !ok) {
588 ssh_gssapi_delete_ctx(&gssctxt);
589 ssh_gssapi_build_ctx(&gssctxt);
590 ssh_gssapi_set_oid(gssctxt, &gss_supported->elements[mech]);
592 /* My DER encoding requires length<128 */
593 if (gss_supported->elements[mech].length < 128 &&
594 ssh_gssapi_check_mechanism(&gss_supported->elements[mech],
596 !GSS_ERROR(ssh_gssapi_import_name(gssctxt,
598 ok = 1; /* Mechanism works */
607 authctxt->methoddata=(void *)gssctxt;
609 packet_start(SSH2_MSG_USERAUTH_REQUEST);
610 packet_put_cstring(authctxt->server_user);
611 packet_put_cstring(authctxt->service);
612 packet_put_cstring(authctxt->method->name);
616 packet_put_int((gss_supported->elements[mech].length) + 2);
617 packet_put_char(SSH_GSS_OIDTYPE);
618 packet_put_char(gss_supported->elements[mech].length);
619 packet_put_raw(gss_supported->elements[mech].elements,
620 gss_supported->elements[mech].length);
624 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
625 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
626 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
627 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
629 mech++; /* Move along to next candidate */
635 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
637 Authctxt *authctxt = ctxt;
638 Gssctxt *gssctxt = authctxt->methoddata;
639 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
640 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
641 gss_buffer_desc gssbuf;
642 OM_uint32 status, ms, flags;
645 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
646 recv_tok, &send_tok, &flags);
648 if (send_tok.length > 0) {
649 if (GSS_ERROR(status))
650 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
652 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
654 packet_put_string(send_tok.value, send_tok.length);
656 gss_release_buffer(&ms, &send_tok);
659 if (status == GSS_S_COMPLETE) {
660 /* send either complete or MIC, depending on mechanism */
661 if (strcmp(authctxt->method->name,"gssapi")==0 ||
662 (!(flags & GSS_C_INTEG_FLAG))) {
663 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
666 ssh_gssapi_buildmic(&b, authctxt->server_user,
667 authctxt->service, "gssapi-with-mic");
669 gssbuf.value = buffer_ptr(&b);
670 gssbuf.length = buffer_len(&b);
672 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
674 if (!GSS_ERROR(status)) {
675 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
676 packet_put_string(mic.value, mic.length);
682 gss_release_buffer(&ms, &mic);
690 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
692 Authctxt *authctxt = ctxt;
697 if (authctxt == NULL)
698 fatal("input_gssapi_response: no authentication context");
699 gssctxt = authctxt->methoddata;
702 oidv = packet_get_string(&oidlen);
705 oidv[0] != SSH_GSS_OIDTYPE ||
706 oidv[1] != oidlen - 2) {
708 debug("Badly encoded mechanism OID received");
709 userauth(authctxt, NULL);
713 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
714 fatal("Server returned different OID than expected");
720 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
721 /* Start again with next method on list */
722 debug("Trying to start again");
723 userauth(authctxt, NULL);
729 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
731 Authctxt *authctxt = ctxt;
732 gss_buffer_desc recv_tok;
736 if (authctxt == NULL)
737 fatal("input_gssapi_response: no authentication context");
739 recv_tok.value = packet_get_string(&slen);
740 recv_tok.length = slen; /* safe typecast */
744 status = process_gssapi_token(ctxt, &recv_tok);
746 xfree(recv_tok.value);
748 if (GSS_ERROR(status)) {
749 /* Start again with the next method in the list */
750 userauth(authctxt, NULL);
756 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
758 Authctxt *authctxt = ctxt;
760 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
761 gss_buffer_desc recv_tok;
762 OM_uint32 status, ms;
765 if (authctxt == NULL)
766 fatal("input_gssapi_response: no authentication context");
767 gssctxt = authctxt->methoddata;
769 recv_tok.value = packet_get_string(&len);
770 recv_tok.length = len;
774 /* Stick it into GSSAPI and see what it says */
775 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
776 &recv_tok, &send_tok, NULL);
778 xfree(recv_tok.value);
779 gss_release_buffer(&ms, &send_tok);
781 /* Server will be returning a failed packet after this one */
785 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
791 maj=packet_get_int();
792 min=packet_get_int();
793 msg=packet_get_string(NULL);
794 lang=packet_get_string(NULL);
798 debug("Server GSSAPI Error:\n%s", msg);
805 const gss_OID_desc * const gss_mech_globus_gssapi_openssl;
806 #define is_gsi_oid(oid) \
807 (oid->length == gss_mech_globus_gssapi_openssl->length && \
808 (memcmp(oid->elements, gss_mech_globus_gssapi_openssl->elements, \
813 userauth_external(Authctxt *authctxt)
815 static int attempt = 0;
820 /* The client MUST NOT try this method if initial key exchange
821 was not performed using a GSSAPI-based key exchange
823 if (gss_kex_context == NULL) {
824 debug2("gsskex not performed, skipping external-keyx");
828 debug2("userauth_external");
829 packet_start(SSH2_MSG_USERAUTH_REQUEST);
831 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
832 packet_put_cstring("");
835 packet_put_cstring(authctxt->server_user);
839 packet_put_cstring(authctxt->service);
840 packet_put_cstring(authctxt->method->name);
846 userauth_gsskeyex(Authctxt *authctxt)
849 gss_buffer_desc gssbuf;
850 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
853 static int attempt = 0;
857 if (gss_kex_context == NULL) {
858 debug("No valid Key exchange context");
863 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
864 ssh_gssapi_buildmic(&b, "", authctxt->service, "gssapi-keyex");
867 ssh_gssapi_buildmic(&b, authctxt->server_user, authctxt->service,
873 gssbuf.value = buffer_ptr(&b);
874 gssbuf.length = buffer_len(&b);
876 if (GSS_ERROR(ssh_gssapi_sign(gss_kex_context, &gssbuf, &mic))) {
881 packet_start(SSH2_MSG_USERAUTH_REQUEST);
883 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
884 packet_put_cstring("");
887 packet_put_cstring(authctxt->server_user);
891 packet_put_cstring(authctxt->service);
892 packet_put_cstring(authctxt->method->name);
893 packet_put_string(mic.value, mic.length);
897 gss_release_buffer(&ms, &mic);
905 userauth_none(Authctxt *authctxt)
907 /* initial userauth request */
908 packet_start(SSH2_MSG_USERAUTH_REQUEST);
909 packet_put_cstring(authctxt->server_user);
910 packet_put_cstring(authctxt->service);
911 packet_put_cstring(authctxt->method->name);
917 userauth_passwd(Authctxt *authctxt)
919 static int attempt = 0;
923 if (attempt++ >= options.number_of_password_prompts)
927 error("Permission denied, please try again.");
929 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
930 authctxt->server_user, authctxt->host);
931 password = read_passphrase(prompt, 0);
932 packet_start(SSH2_MSG_USERAUTH_REQUEST);
933 packet_put_cstring(authctxt->server_user);
934 packet_put_cstring(authctxt->service);
935 packet_put_cstring(authctxt->method->name);
937 packet_put_cstring(password);
938 memset(password, 0, strlen(password));
940 packet_add_padding(64);
943 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
944 &input_userauth_passwd_changereq);
949 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
952 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
954 Authctxt *authctxt = ctxt;
955 char *info, *lang, *password = NULL, *retype = NULL;
958 debug2("input_userauth_passwd_changereq");
960 if (authctxt == NULL)
961 fatal("input_userauth_passwd_changereq: "
962 "no authentication context");
964 info = packet_get_string(NULL);
965 lang = packet_get_string(NULL);
966 if (strlen(info) > 0)
970 packet_start(SSH2_MSG_USERAUTH_REQUEST);
971 packet_put_cstring(authctxt->server_user);
972 packet_put_cstring(authctxt->service);
973 packet_put_cstring(authctxt->method->name);
974 packet_put_char(1); /* additional info */
975 snprintf(prompt, sizeof(prompt),
976 "Enter %.30s@%.128s's old password: ",
977 authctxt->server_user, authctxt->host);
978 password = read_passphrase(prompt, 0);
979 packet_put_cstring(password);
980 memset(password, 0, strlen(password));
983 while (password == NULL) {
984 snprintf(prompt, sizeof(prompt),
985 "Enter %.30s@%.128s's new password: ",
986 authctxt->server_user, authctxt->host);
987 password = read_passphrase(prompt, RP_ALLOW_EOF);
988 if (password == NULL) {
992 snprintf(prompt, sizeof(prompt),
993 "Retype %.30s@%.128s's new password: ",
994 authctxt->server_user, authctxt->host);
995 retype = read_passphrase(prompt, 0);
996 if (strcmp(password, retype) != 0) {
997 memset(password, 0, strlen(password));
999 logit("Mismatch; try again, EOF to quit.");
1002 memset(retype, 0, strlen(retype));
1005 packet_put_cstring(password);
1006 memset(password, 0, strlen(password));
1008 packet_add_padding(64);
1011 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1012 &input_userauth_passwd_changereq);
1016 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1017 u_char *data, u_int datalen)
1022 /* the agent supports this key */
1024 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1027 * we have already loaded the private key or
1028 * the private key is stored in external hardware
1030 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1031 return (key_sign(id->key, sigp, lenp, data, datalen));
1032 /* load the private key from the file */
1033 if ((prv = load_identity_file(id->filename)) == NULL)
1035 ret = key_sign(prv, sigp, lenp, data, datalen);
1041 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1044 u_char *blob, *signature;
1045 u_int bloblen, slen;
1050 debug3("sign_and_send_pubkey");
1052 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1053 /* we cannot handle this key */
1054 debug3("sign_and_send_pubkey: cannot handle key");
1057 /* data to be signed */
1059 if (datafellows & SSH_OLD_SESSIONID) {
1060 buffer_append(&b, session_id2, session_id2_len);
1061 skip = session_id2_len;
1063 buffer_put_string(&b, session_id2, session_id2_len);
1064 skip = buffer_len(&b);
1066 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1067 buffer_put_cstring(&b, authctxt->server_user);
1068 buffer_put_cstring(&b,
1069 datafellows & SSH_BUG_PKSERVICE ?
1072 if (datafellows & SSH_BUG_PKAUTH) {
1073 buffer_put_char(&b, have_sig);
1075 buffer_put_cstring(&b, authctxt->method->name);
1076 buffer_put_char(&b, have_sig);
1077 buffer_put_cstring(&b, key_ssh_name(id->key));
1079 buffer_put_string(&b, blob, bloblen);
1081 /* generate signature */
1082 ret = identity_sign(id, &signature, &slen,
1083 buffer_ptr(&b), buffer_len(&b));
1092 if (datafellows & SSH_BUG_PKSERVICE) {
1094 buffer_append(&b, session_id2, session_id2_len);
1095 skip = session_id2_len;
1096 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1097 buffer_put_cstring(&b, authctxt->server_user);
1098 buffer_put_cstring(&b, authctxt->service);
1099 buffer_put_cstring(&b, authctxt->method->name);
1100 buffer_put_char(&b, have_sig);
1101 if (!(datafellows & SSH_BUG_PKAUTH))
1102 buffer_put_cstring(&b, key_ssh_name(id->key));
1103 buffer_put_string(&b, blob, bloblen);
1107 /* append signature */
1108 buffer_put_string(&b, signature, slen);
1111 /* skip session id and packet type */
1112 if (buffer_len(&b) < skip + 1)
1113 fatal("userauth_pubkey: internal error");
1114 buffer_consume(&b, skip + 1);
1116 /* put remaining data from buffer into packet */
1117 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1118 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1126 send_pubkey_test(Authctxt *authctxt, Identity *id)
1129 u_int bloblen, have_sig = 0;
1131 debug3("send_pubkey_test");
1133 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1134 /* we cannot handle this key */
1135 debug3("send_pubkey_test: cannot handle key");
1138 /* register callback for USERAUTH_PK_OK message */
1139 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1141 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1142 packet_put_cstring(authctxt->server_user);
1143 packet_put_cstring(authctxt->service);
1144 packet_put_cstring(authctxt->method->name);
1145 packet_put_char(have_sig);
1146 if (!(datafellows & SSH_BUG_PKAUTH))
1147 packet_put_cstring(key_ssh_name(id->key));
1148 packet_put_string(blob, bloblen);
1155 load_identity_file(char *filename)
1158 char prompt[300], *passphrase;
1162 if (stat(filename, &st) < 0) {
1163 debug3("no such identity: %s", filename);
1166 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
1167 if (private == NULL) {
1168 if (options.batch_mode)
1170 snprintf(prompt, sizeof prompt,
1171 "Enter passphrase for key '%.100s': ", filename);
1172 for (i = 0; i < options.number_of_password_prompts; i++) {
1173 passphrase = read_passphrase(prompt, 0);
1174 if (strcmp(passphrase, "") != 0) {
1175 private = key_load_private_type(KEY_UNSPEC, filename,
1179 debug2("no passphrase given, try next key");
1182 memset(passphrase, 0, strlen(passphrase));
1184 if (private != NULL || quit)
1186 debug2("bad passphrase given, try again...");
1193 * try keys in the following order:
1194 * 1. agent keys that are found in the config file
1195 * 2. other agent keys
1196 * 3. keys that are only listed in the config file
1199 pubkey_prepare(Authctxt *authctxt)
1202 Idlist agent, files, *preferred;
1204 AuthenticationConnection *ac;
1208 TAILQ_INIT(&agent); /* keys from the agent */
1209 TAILQ_INIT(&files); /* keys from the config file */
1210 preferred = &authctxt->keys;
1211 TAILQ_INIT(preferred); /* preferred order of keys */
1213 /* list of keys stored in the filesystem */
1214 for (i = 0; i < options.num_identity_files; i++) {
1215 key = options.identity_keys[i];
1216 if (key && key->type == KEY_RSA1)
1218 options.identity_keys[i] = NULL;
1219 id = xmalloc(sizeof(*id));
1220 memset(id, 0, sizeof(*id));
1222 id->filename = xstrdup(options.identity_files[i]);
1223 TAILQ_INSERT_TAIL(&files, id, next);
1225 /* list of keys supported by the agent */
1226 if ((ac = ssh_get_authentication_connection())) {
1227 for (key = ssh_get_first_identity(ac, &comment, 2);
1229 key = ssh_get_next_identity(ac, &comment, 2)) {
1231 TAILQ_FOREACH(id, &files, next) {
1232 /* agent keys from the config file are preferred */
1233 if (key_equal(key, id->key)) {
1236 TAILQ_REMOVE(&files, id, next);
1237 TAILQ_INSERT_TAIL(preferred, id, next);
1243 if (!found && !options.identities_only) {
1244 id = xmalloc(sizeof(*id));
1245 memset(id, 0, sizeof(*id));
1247 id->filename = comment;
1249 TAILQ_INSERT_TAIL(&agent, id, next);
1252 /* append remaining agent keys */
1253 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1254 TAILQ_REMOVE(&agent, id, next);
1255 TAILQ_INSERT_TAIL(preferred, id, next);
1257 authctxt->agent = ac;
1259 /* append remaining keys from the config file */
1260 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1261 TAILQ_REMOVE(&files, id, next);
1262 TAILQ_INSERT_TAIL(preferred, id, next);
1264 TAILQ_FOREACH(id, preferred, next) {
1265 debug2("key: %s (%p)", id->filename, id->key);
1270 pubkey_cleanup(Authctxt *authctxt)
1274 if (authctxt->agent != NULL)
1275 ssh_close_authentication_connection(authctxt->agent);
1276 for (id = TAILQ_FIRST(&authctxt->keys); id;
1277 id = TAILQ_FIRST(&authctxt->keys)) {
1278 TAILQ_REMOVE(&authctxt->keys, id, next);
1282 xfree(id->filename);
1288 userauth_pubkey(Authctxt *authctxt)
1293 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1296 /* move key to the end of the queue */
1297 TAILQ_REMOVE(&authctxt->keys, id, next);
1298 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1300 * send a test message if we have the public key. for
1301 * encrypted keys we cannot do this and have to load the
1302 * private key instead
1304 if (id->key && id->key->type != KEY_RSA1) {
1305 debug("Offering public key: %s", id->filename);
1306 sent = send_pubkey_test(authctxt, id);
1307 } else if (id->key == NULL) {
1308 debug("Trying private key: %s", id->filename);
1309 id->key = load_identity_file(id->filename);
1310 if (id->key != NULL) {
1312 sent = sign_and_send_pubkey(authctxt, id);
1324 * Send userauth request message specifying keyboard-interactive method.
1327 userauth_kbdint(Authctxt *authctxt)
1329 static int attempt = 0;
1331 if (attempt++ >= options.number_of_password_prompts)
1333 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1334 if (attempt > 1 && !authctxt->info_req_seen) {
1335 debug3("userauth_kbdint: disable: no info_req_seen");
1336 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1340 debug2("userauth_kbdint");
1341 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1342 packet_put_cstring(authctxt->server_user);
1343 packet_put_cstring(authctxt->service);
1344 packet_put_cstring(authctxt->method->name);
1345 packet_put_cstring(""); /* lang */
1346 packet_put_cstring(options.kbd_interactive_devices ?
1347 options.kbd_interactive_devices : "");
1350 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1355 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1358 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1360 Authctxt *authctxt = ctxt;
1361 char *name, *inst, *lang, *prompt, *response;
1362 u_int num_prompts, i;
1365 debug2("input_userauth_info_req");
1367 if (authctxt == NULL)
1368 fatal("input_userauth_info_req: no authentication context");
1370 authctxt->info_req_seen = 1;
1372 name = packet_get_string(NULL);
1373 inst = packet_get_string(NULL);
1374 lang = packet_get_string(NULL);
1375 if (strlen(name) > 0)
1377 if (strlen(inst) > 0)
1383 num_prompts = packet_get_int();
1385 * Begin to build info response packet based on prompts requested.
1386 * We commit to providing the correct number of responses, so if
1387 * further on we run into a problem that prevents this, we have to
1388 * be sure and clean this up and send a correct error response.
1390 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1391 packet_put_int(num_prompts);
1393 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1394 for (i = 0; i < num_prompts; i++) {
1395 prompt = packet_get_string(NULL);
1396 echo = packet_get_char();
1398 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1400 packet_put_cstring(response);
1401 memset(response, 0, strlen(response));
1405 packet_check_eom(); /* done with parsing incoming message. */
1407 packet_add_padding(64);
1412 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1413 u_char *data, u_int datalen)
1418 int to[2], from[2], status, version = 2;
1420 debug2("ssh_keysign called");
1422 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1423 error("ssh_keysign: no installed: %s", strerror(errno));
1426 if (fflush(stdout) != 0)
1427 error("ssh_keysign: fflush: %s", strerror(errno));
1429 error("ssh_keysign: pipe: %s", strerror(errno));
1432 if (pipe(from) < 0) {
1433 error("ssh_keysign: pipe: %s", strerror(errno));
1436 if ((pid = fork()) < 0) {
1437 error("ssh_keysign: fork: %s", strerror(errno));
1444 if (dup2(from[1], STDOUT_FILENO) < 0)
1445 fatal("ssh_keysign: dup2: %s", strerror(errno));
1447 if (dup2(to[0], STDIN_FILENO) < 0)
1448 fatal("ssh_keysign: dup2: %s", strerror(errno));
1451 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1452 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1459 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1460 buffer_put_string(&b, data, datalen);
1461 if (ssh_msg_send(to[1], version, &b) == -1)
1462 fatal("ssh_keysign: couldn't send request");
1464 if (ssh_msg_recv(from[0], &b) < 0) {
1465 error("ssh_keysign: no reply");
1472 while (waitpid(pid, &status, 0) < 0)
1476 if (buffer_get_char(&b) != version) {
1477 error("ssh_keysign: bad version");
1481 *sigp = buffer_get_string(&b, lenp);
1488 userauth_hostbased(Authctxt *authctxt)
1490 Key *private = NULL;
1491 Sensitive *sensitive = authctxt->sensitive;
1493 u_char *signature, *blob;
1494 char *chost, *pkalg, *p;
1495 const char *service;
1497 int ok, i, len, found = 0;
1499 /* check for a useful key */
1500 for (i = 0; i < sensitive->nkeys; i++) {
1501 private = sensitive->keys[i];
1502 if (private && private->type != KEY_RSA1) {
1504 /* we take and free the key */
1505 sensitive->keys[i] = NULL;
1510 debug("No more client hostkeys for hostbased authentication.");
1513 if (key_to_blob(private, &blob, &blen) == 0) {
1517 /* figure out a name for the client host */
1518 p = get_local_name(packet_get_connection_in());
1520 error("userauth_hostbased: cannot get local ipaddr/name");
1524 len = strlen(p) + 2;
1525 chost = xmalloc(len);
1526 strlcpy(chost, p, len);
1527 strlcat(chost, ".", len);
1528 debug2("userauth_hostbased: chost %s", chost);
1531 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1533 pkalg = xstrdup(key_ssh_name(private));
1535 /* construct data */
1536 buffer_put_string(&b, session_id2, session_id2_len);
1537 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1538 buffer_put_cstring(&b, authctxt->server_user);
1539 buffer_put_cstring(&b, service);
1540 buffer_put_cstring(&b, authctxt->method->name);
1541 buffer_put_cstring(&b, pkalg);
1542 buffer_put_string(&b, blob, blen);
1543 buffer_put_cstring(&b, chost);
1544 buffer_put_cstring(&b, authctxt->local_user);
1548 if (sensitive->external_keysign)
1549 ok = ssh_keysign(private, &signature, &slen,
1550 buffer_ptr(&b), buffer_len(&b));
1552 ok = key_sign(private, &signature, &slen,
1553 buffer_ptr(&b), buffer_len(&b));
1557 error("key_sign failed");
1562 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1563 packet_put_cstring(authctxt->server_user);
1564 packet_put_cstring(authctxt->service);
1565 packet_put_cstring(authctxt->method->name);
1566 packet_put_cstring(pkalg);
1567 packet_put_string(blob, blen);
1568 packet_put_cstring(chost);
1569 packet_put_cstring(authctxt->local_user);
1570 packet_put_string(signature, slen);
1571 memset(signature, 's', slen);
1580 /* find auth method */
1583 * given auth method name, if configurable options permit this method fill
1584 * in auth_ident field and return true, otherwise return false.
1587 authmethod_is_enabled(Authmethod *method)
1591 /* return false if options indicate this method is disabled */
1592 if (method->enabled == NULL || *method->enabled == 0)
1594 /* return false if batch mode is enabled but method needs interactive mode */
1595 if (method->batch_flag != NULL && *method->batch_flag != 0)
1601 authmethod_lookup(const char *name)
1603 Authmethod *method = NULL;
1605 for (method = authmethods; method->name != NULL; method++)
1606 if (strcmp(name, method->name) == 0)
1608 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1612 /* XXX internal state */
1613 static Authmethod *current = NULL;
1614 static char *supported = NULL;
1615 static char *preferred = NULL;
1618 * Given the authentication method list sent by the server, return the
1619 * next method we should try. If the server initially sends a nil list,
1620 * use a built-in default list.
1623 authmethod_get(char *authlist)
1628 /* Use a suitable default if we're passed a nil list. */
1629 if (authlist == NULL || strlen(authlist) == 0)
1630 authlist = options.preferred_authentications;
1632 if (supported == NULL || strcmp(authlist, supported) != 0) {
1633 debug3("start over, passed a different list %s", authlist);
1634 if (supported != NULL)
1636 supported = xstrdup(authlist);
1637 preferred = options.preferred_authentications;
1638 debug3("preferred %s", preferred);
1640 } else if (current != NULL && authmethod_is_enabled(current))
1644 if ((name = match_list(preferred, supported, &next)) == NULL) {
1645 debug("No more authentication methods to try.");
1650 debug3("authmethod_lookup %s", name);
1651 debug3("remaining preferred: %s", preferred);
1652 if ((current = authmethod_lookup(name)) != NULL &&
1653 authmethod_is_enabled(current)) {
1654 debug3("authmethod_is_enabled %s", name);
1655 debug("Next authentication method: %s", name);
1662 authmethods_get(void)
1664 Authmethod *method = NULL;
1669 for (method = authmethods; method->name != NULL; method++) {
1670 if (authmethod_is_enabled(method)) {
1671 if (buffer_len(&b) > 0)
1672 buffer_append(&b, ",", 1);
1673 buffer_append(&b, method->name, strlen(method->name));
1676 buffer_append(&b, "\0", 1);
1677 list = xstrdup(buffer_ptr(&b));