1 /* $OpenBSD: sshconnect2.c,v 1.162 2006/08/30 00:06:51 dtucker 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 #include "openbsd-compat/sys-queue.h"
52 #include "myproposal.h"
53 #include "sshconnect.h"
64 #include "pathnames.h"
72 extern char *client_version_string;
73 extern char *server_version_string;
74 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;
110 char *gss_host = NULL;
114 xxx_hostaddr = hostaddr;
117 if (options.gss_keyex) {
118 /* Add the GSSAPI mechanisms currently supported on this client to
119 * the key exchange algorithm proposal */
120 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 len = strlen(orig) + strlen(gss) + 2;
130 myproposal[PROPOSAL_KEX_ALGS] = xmalloc(len);
131 snprintf(myproposal[PROPOSAL_KEX_ALGS], len, "%s,%s", gss,
137 if (options.ciphers == (char *)-1) {
138 logit("No valid ciphers for protocol version 2 given, using defaults.");
139 options.ciphers = NULL;
141 if (options.ciphers != NULL) {
142 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
143 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
145 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
146 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
147 myproposal[PROPOSAL_ENC_ALGS_STOC] =
148 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
149 if (options.compression) {
150 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
151 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
153 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
154 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
156 if (options.macs != NULL) {
157 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
158 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
160 if (options.hostkeyalgorithms != NULL)
161 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
162 options.hostkeyalgorithms;
165 /* If we've got GSSAPI algorithms, then we also support the
166 * 'null' hostkey, as a last resort */
167 if (options.gss_keyex && gss) {
168 orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
169 len = strlen(orig) + sizeof(",null");
170 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = xmalloc(len);
171 snprintf(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS], len,
176 if (options.rekey_limit)
177 packet_set_rekey_limit(options.rekey_limit);
179 /* start key exchange */
180 kex = kex_setup(myproposal);
181 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
182 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
183 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
185 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
186 kex->kex[KEX_GSS_GEX_SHA1] = kexgss_client;
188 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
189 kex->client_version_string=client_version_string;
190 kex->server_version_string=server_version_string;
191 kex->verify_host_key=&verify_host_key_callback;
194 kex->gss_deleg_creds = options.gss_deleg_creds;
195 kex->gss_trust_dns = options.gss_trust_dns;
196 kex->gss_host = gss_host;
201 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
203 session_id2 = kex->session_id;
204 session_id2_len = kex->session_id_len;
207 /* send 1st encrypted/maced/compressed message */
208 packet_start(SSH2_MSG_IGNORE);
209 packet_put_cstring("markus");
219 typedef struct Authctxt Authctxt;
220 typedef struct Authmethod Authmethod;
221 typedef struct identity Identity;
222 typedef struct idlist Idlist;
225 TAILQ_ENTRY(identity) next;
226 AuthenticationConnection *ac; /* set if agent supports key */
227 Key *key; /* public/private key */
228 char *filename; /* comment for agent-only keys */
230 int isprivate; /* key points to the private key */
232 TAILQ_HEAD(idlist, identity);
235 const char *server_user;
236 const char *local_user;
244 AuthenticationConnection *agent;
246 Sensitive *sensitive;
247 /* kbd-interactive */
253 char *name; /* string to compare against server's list */
254 int (*userauth)(Authctxt *authctxt);
255 int *enabled; /* flag in option struct that enables method */
256 int *batch_flag; /* flag in option struct that disables method */
259 void input_userauth_success(int, u_int32_t, void *);
260 void input_userauth_failure(int, u_int32_t, void *);
261 void input_userauth_banner(int, u_int32_t, void *);
262 void input_userauth_error(int, u_int32_t, void *);
263 void input_userauth_info_req(int, u_int32_t, void *);
264 void input_userauth_pk_ok(int, u_int32_t, void *);
265 void input_userauth_passwd_changereq(int, u_int32_t, void *);
267 int userauth_none(Authctxt *);
268 int userauth_pubkey(Authctxt *);
269 int userauth_passwd(Authctxt *);
270 int userauth_kbdint(Authctxt *);
271 int userauth_hostbased(Authctxt *);
272 int userauth_kerberos(Authctxt *);
275 int userauth_external(Authctxt *authctxt);
276 int userauth_gssapi(Authctxt *authctxt);
277 int userauth_gssapi_with_mic(Authctxt *authctxt);
278 int userauth_gssapi_without_mic(Authctxt *authctxt);
279 void input_gssapi_response(int type, u_int32_t, void *);
280 void input_gssapi_token(int type, u_int32_t, void *);
281 void input_gssapi_hash(int type, u_int32_t, void *);
282 void input_gssapi_error(int, u_int32_t, void *);
283 void input_gssapi_errtok(int, u_int32_t, void *);
284 int userauth_gsskeyex(Authctxt *authctxt);
287 void userauth(Authctxt *, char *);
289 static int sign_and_send_pubkey(Authctxt *, Identity *);
290 static void pubkey_prepare(Authctxt *);
291 static void pubkey_cleanup(Authctxt *);
292 static Key *load_identity_file(char *);
294 static Authmethod *authmethod_get(char *authlist);
295 static Authmethod *authmethod_lookup(const char *name);
296 static char *authmethods_get(void);
298 Authmethod authmethods[] = {
302 &options.gss_authentication,
306 &options.gss_authentication,
310 &options.gss_authentication,
314 &options.gss_authentication,
319 &options.hostbased_authentication,
323 &options.pubkey_authentication,
325 {"keyboard-interactive",
327 &options.kbd_interactive_authentication,
328 &options.batch_mode},
331 &options.password_authentication,
332 &options.batch_mode},
337 {NULL, NULL, NULL, NULL}
341 ssh_userauth2(const char *local_user, const char *server_user, char *host,
342 Sensitive *sensitive)
347 if (options.challenge_response_authentication)
348 options.kbd_interactive_authentication = 1;
350 packet_start(SSH2_MSG_SERVICE_REQUEST);
351 packet_put_cstring("ssh-userauth");
353 debug("SSH2_MSG_SERVICE_REQUEST sent");
355 type = packet_read();
356 if (type != SSH2_MSG_SERVICE_ACCEPT)
357 fatal("Server denied authentication request: %d", type);
358 if (packet_remaining() > 0) {
359 char *reply = packet_get_string(NULL);
360 debug2("service_accept: %s", reply);
363 debug2("buggy server: service_accept w/o service");
366 debug("SSH2_MSG_SERVICE_ACCEPT received");
368 if (options.preferred_authentications == NULL)
369 options.preferred_authentications = authmethods_get();
371 /* setup authentication context */
372 memset(&authctxt, 0, sizeof(authctxt));
373 pubkey_prepare(&authctxt);
374 authctxt.server_user = server_user;
375 authctxt.local_user = local_user;
376 authctxt.host = host;
377 authctxt.service = "ssh-connection"; /* service name */
378 authctxt.success = 0;
379 authctxt.method = authmethod_lookup("none");
380 authctxt.authlist = NULL;
381 authctxt.methoddata = NULL;
382 authctxt.sensitive = sensitive;
383 authctxt.info_req_seen = 0;
384 if (authctxt.method == NULL)
385 fatal("ssh_userauth2: internal error: cannot send userauth none request");
387 /* initial userauth request */
388 userauth_none(&authctxt);
390 dispatch_init(&input_userauth_error);
391 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
392 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
393 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
394 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
396 pubkey_cleanup(&authctxt);
397 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
398 if ((options.none_switch == 1) && (options.none_enabled == 1) && !tty_flag) /* no null on tty sessions */
400 debug("Requesting none rekeying...");
401 myproposal[PROPOSAL_ENC_ALGS_STOC] = "none";
402 myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none";
403 kex_prop2buf(&xxx_kex->my,myproposal);
404 packet_request_rekeying();
405 fprintf(stderr, "WARNING: ENABLED NULL CIPHER\n");
407 debug("Authentication succeeded (%s).", authctxt.method->name);
411 userauth(Authctxt *authctxt, char *authlist)
413 if (authctxt->methoddata) {
414 xfree(authctxt->methoddata);
415 authctxt->methoddata = NULL;
417 if (authlist == NULL) {
418 authlist = authctxt->authlist;
420 if (authctxt->authlist)
421 xfree(authctxt->authlist);
422 authctxt->authlist = authlist;
425 Authmethod *method = authmethod_get(authlist);
427 fatal("Permission denied (%s).", authlist);
428 authctxt->method = method;
430 /* reset the per method handler */
431 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
432 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
434 /* and try new method */
435 if (method->userauth(authctxt) != 0) {
436 debug2("we sent a %s packet, wait for reply", method->name);
439 debug2("we did not send a packet, disable method");
440 method->enabled = NULL;
446 input_userauth_error(int type, u_int32_t seq, void *ctxt)
448 fatal("input_userauth_error: bad message during authentication: "
453 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
457 debug3("input_userauth_banner");
458 msg = packet_get_string(NULL);
459 lang = packet_get_string(NULL);
460 if (options.log_level >= SYSLOG_LEVEL_INFO)
461 fprintf(stderr, "%s", msg);
467 input_userauth_success(int type, u_int32_t seq, void *ctxt)
469 Authctxt *authctxt = ctxt;
470 if (authctxt == NULL)
471 fatal("input_userauth_success: no authentication context");
472 if (authctxt->authlist) {
473 xfree(authctxt->authlist);
474 authctxt->authlist = NULL;
476 if (authctxt->methoddata) {
477 xfree(authctxt->methoddata);
478 authctxt->methoddata = NULL;
480 authctxt->success = 1; /* break out */
484 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
486 Authctxt *authctxt = ctxt;
487 char *authlist = NULL;
490 if (authctxt == NULL)
491 fatal("input_userauth_failure: no authentication context");
493 authlist = packet_get_string(NULL);
494 partial = packet_get_char();
498 logit("Authenticated with partial success.");
499 debug("Authentications that can continue: %s", authlist);
501 userauth(authctxt, authlist);
504 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
506 Authctxt *authctxt = ctxt;
510 int pktype, sent = 0;
515 if (authctxt == NULL)
516 fatal("input_userauth_pk_ok: no authentication context");
517 if (datafellows & SSH_BUG_PKOK) {
518 /* this is similar to SSH_BUG_PKAUTH */
519 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
520 pkblob = packet_get_string(&blen);
522 buffer_append(&b, pkblob, blen);
523 pkalg = buffer_get_string(&b, &alen);
526 pkalg = packet_get_string(&alen);
527 pkblob = packet_get_string(&blen);
531 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
533 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
534 debug("unknown pkalg %s", pkalg);
537 if ((key = key_from_blob(pkblob, blen)) == NULL) {
538 debug("no key from blob. pkalg %s", pkalg);
541 if (key->type != pktype) {
542 error("input_userauth_pk_ok: type mismatch "
543 "for decoded key (received %d, expected %d)",
547 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
548 debug2("input_userauth_pk_ok: fp %s", fp);
552 * search keys in the reverse order, because last candidate has been
553 * moved to the end of the queue. this also avoids confusion by
556 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
557 if (key_equal(key, id->key)) {
558 sent = sign_and_send_pubkey(authctxt, id);
568 /* try another method if we did not send a packet */
570 userauth(authctxt, NULL);
575 userauth_gssapi(Authctxt *authctxt)
577 Gssctxt *gssctxt = NULL;
578 static gss_OID_set gss_supported = NULL;
579 static u_int mech = 0;
582 char *gss_host = NULL;
584 if (!options.gss_authentication) {
585 verbose("GSSAPI authentication disabled.");
589 if (options.gss_trust_dns)
590 gss_host = (char *)get_canonical_hostname(1);
592 gss_host = (char *)authctxt->host;
594 /* Try one GSSAPI method at a time, rather than sending them all at
597 if (gss_supported == NULL)
598 gss_indicate_mechs(&min, &gss_supported);
600 /* Check to see if the mechanism is usable before we offer it */
601 while (mech < gss_supported->count && !ok) {
602 /* My DER encoding requires length<128 */
603 if (gss_supported->elements[mech].length < 128 &&
604 ssh_gssapi_check_mechanism(&gssctxt,
605 &gss_supported->elements[mech], gss_host)) {
606 ok = 1; /* Mechanism works */
615 authctxt->methoddata=(void *)gssctxt;
617 packet_start(SSH2_MSG_USERAUTH_REQUEST);
618 packet_put_cstring(authctxt->server_user);
619 packet_put_cstring(authctxt->service);
620 packet_put_cstring(authctxt->method->name);
624 packet_put_int((gss_supported->elements[mech].length) + 2);
625 packet_put_char(SSH_GSS_OIDTYPE);
626 packet_put_char(gss_supported->elements[mech].length);
627 packet_put_raw(gss_supported->elements[mech].elements,
628 gss_supported->elements[mech].length);
632 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
633 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
634 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
635 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
637 mech++; /* Move along to next candidate */
643 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
645 Authctxt *authctxt = ctxt;
646 Gssctxt *gssctxt = authctxt->methoddata;
647 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
648 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
649 gss_buffer_desc gssbuf;
650 OM_uint32 status, ms, flags;
653 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
654 recv_tok, &send_tok, &flags);
656 if (send_tok.length > 0) {
657 if (GSS_ERROR(status))
658 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
660 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
662 packet_put_string(send_tok.value, send_tok.length);
664 gss_release_buffer(&ms, &send_tok);
667 if (status == GSS_S_COMPLETE) {
668 /* send either complete or MIC, depending on mechanism */
669 if (strcmp(authctxt->method->name,"gssapi")==0 ||
670 (!(flags & GSS_C_INTEG_FLAG))) {
671 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
674 ssh_gssapi_buildmic(&b, authctxt->server_user,
675 authctxt->service, "gssapi-with-mic");
677 gssbuf.value = buffer_ptr(&b);
678 gssbuf.length = buffer_len(&b);
680 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
682 if (!GSS_ERROR(status)) {
683 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
684 packet_put_string(mic.value, mic.length);
690 gss_release_buffer(&ms, &mic);
698 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
700 Authctxt *authctxt = ctxt;
705 if (authctxt == NULL)
706 fatal("input_gssapi_response: no authentication context");
707 gssctxt = authctxt->methoddata;
710 oidv = packet_get_string(&oidlen);
713 oidv[0] != SSH_GSS_OIDTYPE ||
714 oidv[1] != oidlen - 2) {
716 debug("Badly encoded mechanism OID received");
717 userauth(authctxt, NULL);
721 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
722 fatal("Server returned different OID than expected");
728 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
729 /* Start again with next method on list */
730 debug("Trying to start again");
731 userauth(authctxt, NULL);
737 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
739 Authctxt *authctxt = ctxt;
740 gss_buffer_desc recv_tok;
744 if (authctxt == NULL)
745 fatal("input_gssapi_response: no authentication context");
747 recv_tok.value = packet_get_string(&slen);
748 recv_tok.length = slen; /* safe typecast */
752 status = process_gssapi_token(ctxt, &recv_tok);
754 xfree(recv_tok.value);
756 if (GSS_ERROR(status)) {
757 /* Start again with the next method in the list */
758 userauth(authctxt, NULL);
764 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
766 Authctxt *authctxt = ctxt;
768 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
769 gss_buffer_desc recv_tok;
770 OM_uint32 status, ms;
773 if (authctxt == NULL)
774 fatal("input_gssapi_response: no authentication context");
775 gssctxt = authctxt->methoddata;
777 recv_tok.value = packet_get_string(&len);
778 recv_tok.length = len;
782 /* Stick it into GSSAPI and see what it says */
783 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
784 &recv_tok, &send_tok, NULL);
786 xfree(recv_tok.value);
787 gss_release_buffer(&ms, &send_tok);
789 /* Server will be returning a failed packet after this one */
793 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
799 maj=packet_get_int();
800 min=packet_get_int();
801 msg=packet_get_string(NULL);
802 lang=packet_get_string(NULL);
806 debug("Server GSSAPI Error:\n%s", msg);
813 const gss_OID_desc * const gss_mech_globus_gssapi_openssl;
814 #define is_gsi_oid(oid) \
815 (oid->length == gss_mech_globus_gssapi_openssl->length && \
816 (memcmp(oid->elements, gss_mech_globus_gssapi_openssl->elements, \
821 userauth_external(Authctxt *authctxt)
823 static int attempt = 0;
828 /* The client MUST NOT try this method if initial key exchange
829 was not performed using a GSSAPI-based key exchange
831 if (gss_kex_context == NULL) {
832 debug2("gsskex not performed, skipping external-keyx");
836 debug2("userauth_external");
837 packet_start(SSH2_MSG_USERAUTH_REQUEST);
839 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
840 packet_put_cstring("");
843 packet_put_cstring(authctxt->server_user);
847 packet_put_cstring(authctxt->service);
848 packet_put_cstring(authctxt->method->name);
854 userauth_gsskeyex(Authctxt *authctxt)
857 gss_buffer_desc gssbuf;
858 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
861 static int attempt = 0;
865 if (gss_kex_context == NULL) {
866 debug("No valid Key exchange context");
871 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
872 ssh_gssapi_buildmic(&b, "", authctxt->service, "gssapi-keyex");
875 ssh_gssapi_buildmic(&b, authctxt->server_user, authctxt->service,
881 gssbuf.value = buffer_ptr(&b);
882 gssbuf.length = buffer_len(&b);
884 if (GSS_ERROR(ssh_gssapi_sign(gss_kex_context, &gssbuf, &mic))) {
889 packet_start(SSH2_MSG_USERAUTH_REQUEST);
891 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
892 packet_put_cstring("");
895 packet_put_cstring(authctxt->server_user);
899 packet_put_cstring(authctxt->service);
900 packet_put_cstring(authctxt->method->name);
901 packet_put_string(mic.value, mic.length);
905 gss_release_buffer(&ms, &mic);
913 userauth_none(Authctxt *authctxt)
915 /* initial userauth request */
916 packet_start(SSH2_MSG_USERAUTH_REQUEST);
917 packet_put_cstring(authctxt->server_user);
918 packet_put_cstring(authctxt->service);
919 packet_put_cstring(authctxt->method->name);
925 userauth_passwd(Authctxt *authctxt)
927 static int attempt = 0;
931 if (attempt++ >= options.number_of_password_prompts)
935 error("Permission denied, please try again.");
937 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
938 authctxt->server_user, authctxt->host);
939 password = read_passphrase(prompt, 0);
940 packet_start(SSH2_MSG_USERAUTH_REQUEST);
941 packet_put_cstring(authctxt->server_user);
942 packet_put_cstring(authctxt->service);
943 packet_put_cstring(authctxt->method->name);
945 packet_put_cstring(password);
946 memset(password, 0, strlen(password));
948 packet_add_padding(64);
951 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
952 &input_userauth_passwd_changereq);
957 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
960 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
962 Authctxt *authctxt = ctxt;
963 char *info, *lang, *password = NULL, *retype = NULL;
966 debug2("input_userauth_passwd_changereq");
968 if (authctxt == NULL)
969 fatal("input_userauth_passwd_changereq: "
970 "no authentication context");
972 info = packet_get_string(NULL);
973 lang = packet_get_string(NULL);
974 if (strlen(info) > 0)
978 packet_start(SSH2_MSG_USERAUTH_REQUEST);
979 packet_put_cstring(authctxt->server_user);
980 packet_put_cstring(authctxt->service);
981 packet_put_cstring(authctxt->method->name);
982 packet_put_char(1); /* additional info */
983 snprintf(prompt, sizeof(prompt),
984 "Enter %.30s@%.128s's old password: ",
985 authctxt->server_user, authctxt->host);
986 password = read_passphrase(prompt, 0);
987 packet_put_cstring(password);
988 memset(password, 0, strlen(password));
991 while (password == NULL) {
992 snprintf(prompt, sizeof(prompt),
993 "Enter %.30s@%.128s's new password: ",
994 authctxt->server_user, authctxt->host);
995 password = read_passphrase(prompt, RP_ALLOW_EOF);
996 if (password == NULL) {
1000 snprintf(prompt, sizeof(prompt),
1001 "Retype %.30s@%.128s's new password: ",
1002 authctxt->server_user, authctxt->host);
1003 retype = read_passphrase(prompt, 0);
1004 if (strcmp(password, retype) != 0) {
1005 memset(password, 0, strlen(password));
1007 logit("Mismatch; try again, EOF to quit.");
1010 memset(retype, 0, strlen(retype));
1013 packet_put_cstring(password);
1014 memset(password, 0, strlen(password));
1016 packet_add_padding(64);
1019 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1020 &input_userauth_passwd_changereq);
1024 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1025 u_char *data, u_int datalen)
1030 /* the agent supports this key */
1032 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1035 * we have already loaded the private key or
1036 * the private key is stored in external hardware
1038 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1039 return (key_sign(id->key, sigp, lenp, data, datalen));
1040 /* load the private key from the file */
1041 if ((prv = load_identity_file(id->filename)) == NULL)
1043 ret = key_sign(prv, sigp, lenp, data, datalen);
1049 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1052 u_char *blob, *signature;
1053 u_int bloblen, slen;
1058 debug3("sign_and_send_pubkey");
1060 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1061 /* we cannot handle this key */
1062 debug3("sign_and_send_pubkey: cannot handle key");
1065 /* data to be signed */
1067 if (datafellows & SSH_OLD_SESSIONID) {
1068 buffer_append(&b, session_id2, session_id2_len);
1069 skip = session_id2_len;
1071 buffer_put_string(&b, session_id2, session_id2_len);
1072 skip = buffer_len(&b);
1074 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1075 buffer_put_cstring(&b, authctxt->server_user);
1076 buffer_put_cstring(&b,
1077 datafellows & SSH_BUG_PKSERVICE ?
1080 if (datafellows & SSH_BUG_PKAUTH) {
1081 buffer_put_char(&b, have_sig);
1083 buffer_put_cstring(&b, authctxt->method->name);
1084 buffer_put_char(&b, have_sig);
1085 buffer_put_cstring(&b, key_ssh_name(id->key));
1087 buffer_put_string(&b, blob, bloblen);
1089 /* generate signature */
1090 ret = identity_sign(id, &signature, &slen,
1091 buffer_ptr(&b), buffer_len(&b));
1100 if (datafellows & SSH_BUG_PKSERVICE) {
1102 buffer_append(&b, session_id2, session_id2_len);
1103 skip = session_id2_len;
1104 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1105 buffer_put_cstring(&b, authctxt->server_user);
1106 buffer_put_cstring(&b, authctxt->service);
1107 buffer_put_cstring(&b, authctxt->method->name);
1108 buffer_put_char(&b, have_sig);
1109 if (!(datafellows & SSH_BUG_PKAUTH))
1110 buffer_put_cstring(&b, key_ssh_name(id->key));
1111 buffer_put_string(&b, blob, bloblen);
1115 /* append signature */
1116 buffer_put_string(&b, signature, slen);
1119 /* skip session id and packet type */
1120 if (buffer_len(&b) < skip + 1)
1121 fatal("userauth_pubkey: internal error");
1122 buffer_consume(&b, skip + 1);
1124 /* put remaining data from buffer into packet */
1125 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1126 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1134 send_pubkey_test(Authctxt *authctxt, Identity *id)
1137 u_int bloblen, have_sig = 0;
1139 debug3("send_pubkey_test");
1141 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1142 /* we cannot handle this key */
1143 debug3("send_pubkey_test: cannot handle key");
1146 /* register callback for USERAUTH_PK_OK message */
1147 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1149 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1150 packet_put_cstring(authctxt->server_user);
1151 packet_put_cstring(authctxt->service);
1152 packet_put_cstring(authctxt->method->name);
1153 packet_put_char(have_sig);
1154 if (!(datafellows & SSH_BUG_PKAUTH))
1155 packet_put_cstring(key_ssh_name(id->key));
1156 packet_put_string(blob, bloblen);
1163 load_identity_file(char *filename)
1166 char prompt[300], *passphrase;
1167 int perm_ok, quit, i;
1170 if (stat(filename, &st) < 0) {
1171 debug3("no such identity: %s", filename);
1174 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1177 if (private == NULL) {
1178 if (options.batch_mode)
1180 snprintf(prompt, sizeof prompt,
1181 "Enter passphrase for key '%.100s': ", filename);
1182 for (i = 0; i < options.number_of_password_prompts; i++) {
1183 passphrase = read_passphrase(prompt, 0);
1184 if (strcmp(passphrase, "") != 0) {
1185 private = key_load_private_type(KEY_UNSPEC,
1186 filename, passphrase, NULL, NULL);
1189 debug2("no passphrase given, try next key");
1192 memset(passphrase, 0, strlen(passphrase));
1194 if (private != NULL || quit)
1196 debug2("bad passphrase given, try again...");
1203 * try keys in the following order:
1204 * 1. agent keys that are found in the config file
1205 * 2. other agent keys
1206 * 3. keys that are only listed in the config file
1209 pubkey_prepare(Authctxt *authctxt)
1212 Idlist agent, files, *preferred;
1214 AuthenticationConnection *ac;
1218 TAILQ_INIT(&agent); /* keys from the agent */
1219 TAILQ_INIT(&files); /* keys from the config file */
1220 preferred = &authctxt->keys;
1221 TAILQ_INIT(preferred); /* preferred order of keys */
1223 /* list of keys stored in the filesystem */
1224 for (i = 0; i < options.num_identity_files; i++) {
1225 key = options.identity_keys[i];
1226 if (key && key->type == KEY_RSA1)
1228 options.identity_keys[i] = NULL;
1229 id = xcalloc(1, sizeof(*id));
1231 id->filename = xstrdup(options.identity_files[i]);
1232 TAILQ_INSERT_TAIL(&files, id, next);
1234 /* list of keys supported by the agent */
1235 if ((ac = ssh_get_authentication_connection())) {
1236 for (key = ssh_get_first_identity(ac, &comment, 2);
1238 key = ssh_get_next_identity(ac, &comment, 2)) {
1240 TAILQ_FOREACH(id, &files, next) {
1241 /* agent keys from the config file are preferred */
1242 if (key_equal(key, id->key)) {
1245 TAILQ_REMOVE(&files, id, next);
1246 TAILQ_INSERT_TAIL(preferred, id, next);
1252 if (!found && !options.identities_only) {
1253 id = xcalloc(1, sizeof(*id));
1255 id->filename = comment;
1257 TAILQ_INSERT_TAIL(&agent, id, next);
1260 /* append remaining agent keys */
1261 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1262 TAILQ_REMOVE(&agent, id, next);
1263 TAILQ_INSERT_TAIL(preferred, id, next);
1265 authctxt->agent = ac;
1267 /* append remaining keys from the config file */
1268 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1269 TAILQ_REMOVE(&files, id, next);
1270 TAILQ_INSERT_TAIL(preferred, id, next);
1272 TAILQ_FOREACH(id, preferred, next) {
1273 debug2("key: %s (%p)", id->filename, id->key);
1278 pubkey_cleanup(Authctxt *authctxt)
1282 if (authctxt->agent != NULL)
1283 ssh_close_authentication_connection(authctxt->agent);
1284 for (id = TAILQ_FIRST(&authctxt->keys); id;
1285 id = TAILQ_FIRST(&authctxt->keys)) {
1286 TAILQ_REMOVE(&authctxt->keys, id, next);
1290 xfree(id->filename);
1296 userauth_pubkey(Authctxt *authctxt)
1301 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1304 /* move key to the end of the queue */
1305 TAILQ_REMOVE(&authctxt->keys, id, next);
1306 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1308 * send a test message if we have the public key. for
1309 * encrypted keys we cannot do this and have to load the
1310 * private key instead
1312 if (id->key && id->key->type != KEY_RSA1) {
1313 debug("Offering public key: %s", id->filename);
1314 sent = send_pubkey_test(authctxt, id);
1315 } else if (id->key == NULL) {
1316 debug("Trying private key: %s", id->filename);
1317 id->key = load_identity_file(id->filename);
1318 if (id->key != NULL) {
1320 sent = sign_and_send_pubkey(authctxt, id);
1332 * Send userauth request message specifying keyboard-interactive method.
1335 userauth_kbdint(Authctxt *authctxt)
1337 static int attempt = 0;
1339 if (attempt++ >= options.number_of_password_prompts)
1341 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1342 if (attempt > 1 && !authctxt->info_req_seen) {
1343 debug3("userauth_kbdint: disable: no info_req_seen");
1344 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1348 debug2("userauth_kbdint");
1349 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1350 packet_put_cstring(authctxt->server_user);
1351 packet_put_cstring(authctxt->service);
1352 packet_put_cstring(authctxt->method->name);
1353 packet_put_cstring(""); /* lang */
1354 packet_put_cstring(options.kbd_interactive_devices ?
1355 options.kbd_interactive_devices : "");
1358 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1363 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1366 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1368 Authctxt *authctxt = ctxt;
1369 char *name, *inst, *lang, *prompt, *response;
1370 u_int num_prompts, i;
1373 debug2("input_userauth_info_req");
1375 if (authctxt == NULL)
1376 fatal("input_userauth_info_req: no authentication context");
1378 authctxt->info_req_seen = 1;
1380 name = packet_get_string(NULL);
1381 inst = packet_get_string(NULL);
1382 lang = packet_get_string(NULL);
1383 if (strlen(name) > 0)
1385 if (strlen(inst) > 0)
1391 num_prompts = packet_get_int();
1393 * Begin to build info response packet based on prompts requested.
1394 * We commit to providing the correct number of responses, so if
1395 * further on we run into a problem that prevents this, we have to
1396 * be sure and clean this up and send a correct error response.
1398 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1399 packet_put_int(num_prompts);
1401 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1402 for (i = 0; i < num_prompts; i++) {
1403 prompt = packet_get_string(NULL);
1404 echo = packet_get_char();
1406 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1408 packet_put_cstring(response);
1409 memset(response, 0, strlen(response));
1413 packet_check_eom(); /* done with parsing incoming message. */
1415 packet_add_padding(64);
1420 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1421 u_char *data, u_int datalen)
1426 int to[2], from[2], status, version = 2;
1428 debug2("ssh_keysign called");
1430 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1431 error("ssh_keysign: no installed: %s", strerror(errno));
1434 if (fflush(stdout) != 0)
1435 error("ssh_keysign: fflush: %s", strerror(errno));
1437 error("ssh_keysign: pipe: %s", strerror(errno));
1440 if (pipe(from) < 0) {
1441 error("ssh_keysign: pipe: %s", strerror(errno));
1444 if ((pid = fork()) < 0) {
1445 error("ssh_keysign: fork: %s", strerror(errno));
1449 permanently_drop_suid(getuid());
1451 if (dup2(from[1], STDOUT_FILENO) < 0)
1452 fatal("ssh_keysign: dup2: %s", strerror(errno));
1454 if (dup2(to[0], STDIN_FILENO) < 0)
1455 fatal("ssh_keysign: dup2: %s", strerror(errno));
1458 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1459 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1466 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1467 buffer_put_string(&b, data, datalen);
1468 if (ssh_msg_send(to[1], version, &b) == -1)
1469 fatal("ssh_keysign: couldn't send request");
1471 if (ssh_msg_recv(from[0], &b) < 0) {
1472 error("ssh_keysign: no reply");
1479 while (waitpid(pid, &status, 0) < 0)
1483 if (buffer_get_char(&b) != version) {
1484 error("ssh_keysign: bad version");
1488 *sigp = buffer_get_string(&b, lenp);
1495 userauth_hostbased(Authctxt *authctxt)
1497 Key *private = NULL;
1498 Sensitive *sensitive = authctxt->sensitive;
1500 u_char *signature, *blob;
1501 char *chost, *pkalg, *p;
1502 const char *service;
1504 int ok, i, len, found = 0;
1506 /* check for a useful key */
1507 for (i = 0; i < sensitive->nkeys; i++) {
1508 private = sensitive->keys[i];
1509 if (private && private->type != KEY_RSA1) {
1511 /* we take and free the key */
1512 sensitive->keys[i] = NULL;
1517 debug("No more client hostkeys for hostbased authentication.");
1520 if (key_to_blob(private, &blob, &blen) == 0) {
1524 /* figure out a name for the client host */
1525 p = get_local_name(packet_get_connection_in());
1527 error("userauth_hostbased: cannot get local ipaddr/name");
1532 len = strlen(p) + 2;
1533 xasprintf(&chost, "%s.", p);
1534 debug2("userauth_hostbased: chost %s", chost);
1537 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1539 pkalg = xstrdup(key_ssh_name(private));
1541 /* construct data */
1542 buffer_put_string(&b, session_id2, session_id2_len);
1543 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1544 buffer_put_cstring(&b, authctxt->server_user);
1545 buffer_put_cstring(&b, service);
1546 buffer_put_cstring(&b, authctxt->method->name);
1547 buffer_put_cstring(&b, pkalg);
1548 buffer_put_string(&b, blob, blen);
1549 buffer_put_cstring(&b, chost);
1550 buffer_put_cstring(&b, authctxt->local_user);
1554 if (sensitive->external_keysign)
1555 ok = ssh_keysign(private, &signature, &slen,
1556 buffer_ptr(&b), buffer_len(&b));
1558 ok = key_sign(private, &signature, &slen,
1559 buffer_ptr(&b), buffer_len(&b));
1563 error("key_sign failed");
1569 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1570 packet_put_cstring(authctxt->server_user);
1571 packet_put_cstring(authctxt->service);
1572 packet_put_cstring(authctxt->method->name);
1573 packet_put_cstring(pkalg);
1574 packet_put_string(blob, blen);
1575 packet_put_cstring(chost);
1576 packet_put_cstring(authctxt->local_user);
1577 packet_put_string(signature, slen);
1578 memset(signature, 's', slen);
1588 /* find auth method */
1591 * given auth method name, if configurable options permit this method fill
1592 * in auth_ident field and return true, otherwise return false.
1595 authmethod_is_enabled(Authmethod *method)
1599 /* return false if options indicate this method is disabled */
1600 if (method->enabled == NULL || *method->enabled == 0)
1602 /* return false if batch mode is enabled but method needs interactive mode */
1603 if (method->batch_flag != NULL && *method->batch_flag != 0)
1609 authmethod_lookup(const char *name)
1611 Authmethod *method = NULL;
1613 for (method = authmethods; method->name != NULL; method++)
1614 if (strcmp(name, method->name) == 0)
1616 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1620 /* XXX internal state */
1621 static Authmethod *current = NULL;
1622 static char *supported = NULL;
1623 static char *preferred = NULL;
1626 * Given the authentication method list sent by the server, return the
1627 * next method we should try. If the server initially sends a nil list,
1628 * use a built-in default list.
1631 authmethod_get(char *authlist)
1636 /* Use a suitable default if we're passed a nil list. */
1637 if (authlist == NULL || strlen(authlist) == 0)
1638 authlist = options.preferred_authentications;
1640 if (supported == NULL || strcmp(authlist, supported) != 0) {
1641 debug3("start over, passed a different list %s", authlist);
1642 if (supported != NULL)
1644 supported = xstrdup(authlist);
1645 preferred = options.preferred_authentications;
1646 debug3("preferred %s", preferred);
1648 } else if (current != NULL && authmethod_is_enabled(current))
1652 if ((name = match_list(preferred, supported, &next)) == NULL) {
1653 debug("No more authentication methods to try.");
1658 debug3("authmethod_lookup %s", name);
1659 debug3("remaining preferred: %s", preferred);
1660 if ((current = authmethod_lookup(name)) != NULL &&
1661 authmethod_is_enabled(current)) {
1662 debug3("authmethod_is_enabled %s", name);
1663 debug("Next authentication method: %s", name);
1670 authmethods_get(void)
1672 Authmethod *method = NULL;
1677 for (method = authmethods; method->name != NULL; method++) {
1678 if (authmethod_is_enabled(method)) {
1679 if (buffer_len(&b) > 0)
1680 buffer_append(&b, ",", 1);
1681 buffer_append(&b, method->name, strlen(method->name));
1684 buffer_append(&b, "\0", 1);
1685 list = xstrdup(buffer_ptr(&b));