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;
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;
144 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
145 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
146 myproposal[PROPOSAL_ENC_ALGS_STOC] =
147 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
148 if (options.compression) {
149 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
150 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
152 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
153 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
155 if (options.macs != NULL) {
156 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
157 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
159 if (options.hostkeyalgorithms != NULL)
160 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
161 options.hostkeyalgorithms;
164 /* If we've got GSSAPI algorithms, then we also support the
165 * 'null' hostkey, as a last resort */
166 if (options.gss_keyex && gss) {
167 orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS];
168 xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS],
173 if (options.rekey_limit)
174 packet_set_rekey_limit(options.rekey_limit);
176 /* start key exchange */
177 kex = kex_setup(myproposal);
178 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
179 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
180 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
181 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
183 kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
184 kex->kex[KEX_GSS_GRP14_SHA1] = kexgss_client;
185 kex->kex[KEX_GSS_GEX_SHA1] = kexgss_client;
187 kex->client_version_string=client_version_string;
188 kex->server_version_string=server_version_string;
189 kex->verify_host_key=&verify_host_key_callback;
192 kex->gss_deleg_creds = options.gss_deleg_creds;
193 kex->gss_trust_dns = options.gss_trust_dns;
194 kex->gss_host = gss_host;
199 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
201 session_id2 = kex->session_id;
202 session_id2_len = kex->session_id_len;
205 /* send 1st encrypted/maced/compressed message */
206 packet_start(SSH2_MSG_IGNORE);
207 packet_put_cstring("markus");
217 typedef struct Authctxt Authctxt;
218 typedef struct Authmethod Authmethod;
219 typedef struct identity Identity;
220 typedef struct idlist Idlist;
223 TAILQ_ENTRY(identity) next;
224 AuthenticationConnection *ac; /* set if agent supports key */
225 Key *key; /* public/private key */
226 char *filename; /* comment for agent-only keys */
228 int isprivate; /* key points to the private key */
230 TAILQ_HEAD(idlist, identity);
233 const char *server_user;
234 const char *local_user;
242 AuthenticationConnection *agent;
244 Sensitive *sensitive;
245 /* kbd-interactive */
251 char *name; /* string to compare against server's list */
252 int (*userauth)(Authctxt *authctxt);
253 int *enabled; /* flag in option struct that enables method */
254 int *batch_flag; /* flag in option struct that disables method */
257 void input_userauth_success(int, u_int32_t, void *);
258 void input_userauth_failure(int, u_int32_t, void *);
259 void input_userauth_banner(int, u_int32_t, void *);
260 void input_userauth_error(int, u_int32_t, void *);
261 void input_userauth_info_req(int, u_int32_t, void *);
262 void input_userauth_pk_ok(int, u_int32_t, void *);
263 void input_userauth_passwd_changereq(int, u_int32_t, void *);
265 int userauth_none(Authctxt *);
266 int userauth_pubkey(Authctxt *);
267 int userauth_passwd(Authctxt *);
268 int userauth_kbdint(Authctxt *);
269 int userauth_hostbased(Authctxt *);
270 int userauth_kerberos(Authctxt *);
273 int userauth_external(Authctxt *authctxt);
274 int userauth_gssapi(Authctxt *authctxt);
275 int userauth_gssapi_with_mic(Authctxt *authctxt);
276 int userauth_gssapi_without_mic(Authctxt *authctxt);
277 void input_gssapi_response(int type, u_int32_t, void *);
278 void input_gssapi_token(int type, u_int32_t, void *);
279 void input_gssapi_hash(int type, u_int32_t, void *);
280 void input_gssapi_error(int, u_int32_t, void *);
281 void input_gssapi_errtok(int, u_int32_t, void *);
282 int userauth_gsskeyex(Authctxt *authctxt);
285 void userauth(Authctxt *, char *);
287 static int sign_and_send_pubkey(Authctxt *, Identity *);
288 static void pubkey_prepare(Authctxt *);
289 static void pubkey_cleanup(Authctxt *);
290 static Key *load_identity_file(char *);
292 static Authmethod *authmethod_get(char *authlist);
293 static Authmethod *authmethod_lookup(const char *name);
294 static char *authmethods_get(void);
296 Authmethod authmethods[] = {
300 &options.gss_authentication,
304 &options.gss_authentication,
308 &options.gss_authentication,
312 &options.gss_authentication,
317 &options.hostbased_authentication,
321 &options.pubkey_authentication,
323 {"keyboard-interactive",
325 &options.kbd_interactive_authentication,
326 &options.batch_mode},
329 &options.password_authentication,
330 &options.batch_mode},
335 {NULL, NULL, NULL, NULL}
339 ssh_userauth2(const char *local_user, const char *server_user, char *host,
340 Sensitive *sensitive)
345 if (options.challenge_response_authentication)
346 options.kbd_interactive_authentication = 1;
348 packet_start(SSH2_MSG_SERVICE_REQUEST);
349 packet_put_cstring("ssh-userauth");
351 debug("SSH2_MSG_SERVICE_REQUEST sent");
353 type = packet_read();
354 if (type != SSH2_MSG_SERVICE_ACCEPT)
355 fatal("Server denied authentication request: %d", type);
356 if (packet_remaining() > 0) {
357 char *reply = packet_get_string(NULL);
358 debug2("service_accept: %s", reply);
361 debug2("buggy server: service_accept w/o service");
364 debug("SSH2_MSG_SERVICE_ACCEPT received");
366 if (options.preferred_authentications == NULL)
367 options.preferred_authentications = authmethods_get();
369 /* setup authentication context */
370 memset(&authctxt, 0, sizeof(authctxt));
371 pubkey_prepare(&authctxt);
372 authctxt.server_user = server_user;
373 authctxt.local_user = local_user;
374 authctxt.host = host;
375 authctxt.service = "ssh-connection"; /* service name */
376 authctxt.success = 0;
377 authctxt.method = authmethod_lookup("none");
378 authctxt.authlist = NULL;
379 authctxt.methoddata = NULL;
380 authctxt.sensitive = sensitive;
381 authctxt.info_req_seen = 0;
382 if (authctxt.method == NULL)
383 fatal("ssh_userauth2: internal error: cannot send userauth none request");
385 /* initial userauth request */
386 userauth_none(&authctxt);
388 dispatch_init(&input_userauth_error);
389 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
390 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
391 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
392 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
394 pubkey_cleanup(&authctxt);
395 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
397 /* if the user wants to use the none cipher do it */
398 /* post authentication and only if the right conditions are met */
399 /* both of the NONE commands must be true and there must be no */
401 if ((options.none_switch == 1) && (options.none_enabled == 1))
403 if (!tty_flag) /* no null on tty sessions */
405 debug("Requesting none rekeying...");
406 myproposal[PROPOSAL_ENC_ALGS_STOC] = "none";
407 myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none";
408 kex_prop2buf(&xxx_kex->my,myproposal);
409 packet_request_rekeying();
410 fprintf(stderr, "WARNING: ENABLED NONE CIPHER\n");
414 /* requested NONE cipher when in a tty */
415 debug("Cannot switch to NONE cipher with tty allocated");
416 fprintf(stderr, "NONE cipher switch disabled when a TTY is allocated\n");
419 debug("Authentication succeeded (%s).", authctxt.method->name);
423 userauth(Authctxt *authctxt, char *authlist)
425 if (authctxt->methoddata) {
426 xfree(authctxt->methoddata);
427 authctxt->methoddata = NULL;
429 if (authlist == NULL) {
430 authlist = authctxt->authlist;
432 if (authctxt->authlist)
433 xfree(authctxt->authlist);
434 authctxt->authlist = authlist;
437 Authmethod *method = authmethod_get(authlist);
439 fatal("Permission denied (%s).", authlist);
440 authctxt->method = method;
442 /* reset the per method handler */
443 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
444 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
446 /* and try new method */
447 if (method->userauth(authctxt) != 0) {
448 debug2("we sent a %s packet, wait for reply", method->name);
451 debug2("we did not send a packet, disable method");
452 method->enabled = NULL;
458 input_userauth_error(int type, u_int32_t seq, void *ctxt)
460 fatal("input_userauth_error: bad message during authentication: "
465 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
469 debug3("input_userauth_banner");
470 msg = packet_get_string(NULL);
471 lang = packet_get_string(NULL);
472 if (options.log_level >= SYSLOG_LEVEL_INFO)
473 fprintf(stderr, "%s", msg);
479 input_userauth_success(int type, u_int32_t seq, void *ctxt)
481 Authctxt *authctxt = ctxt;
482 if (authctxt == NULL)
483 fatal("input_userauth_success: no authentication context");
484 if (authctxt->authlist) {
485 xfree(authctxt->authlist);
486 authctxt->authlist = NULL;
488 if (authctxt->methoddata) {
489 xfree(authctxt->methoddata);
490 authctxt->methoddata = NULL;
492 authctxt->success = 1; /* break out */
496 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
498 Authctxt *authctxt = ctxt;
499 char *authlist = NULL;
502 if (authctxt == NULL)
503 fatal("input_userauth_failure: no authentication context");
505 authlist = packet_get_string(NULL);
506 partial = packet_get_char();
510 logit("Authenticated with partial success.");
511 debug("Authentications that can continue: %s", authlist);
513 userauth(authctxt, authlist);
516 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
518 Authctxt *authctxt = ctxt;
522 int pktype, sent = 0;
527 if (authctxt == NULL)
528 fatal("input_userauth_pk_ok: no authentication context");
529 if (datafellows & SSH_BUG_PKOK) {
530 /* this is similar to SSH_BUG_PKAUTH */
531 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
532 pkblob = packet_get_string(&blen);
534 buffer_append(&b, pkblob, blen);
535 pkalg = buffer_get_string(&b, &alen);
538 pkalg = packet_get_string(&alen);
539 pkblob = packet_get_string(&blen);
543 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
545 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
546 debug("unknown pkalg %s", pkalg);
549 if ((key = key_from_blob(pkblob, blen)) == NULL) {
550 debug("no key from blob. pkalg %s", pkalg);
553 if (key->type != pktype) {
554 error("input_userauth_pk_ok: type mismatch "
555 "for decoded key (received %d, expected %d)",
559 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
560 debug2("input_userauth_pk_ok: fp %s", fp);
564 * search keys in the reverse order, because last candidate has been
565 * moved to the end of the queue. this also avoids confusion by
568 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
569 if (key_equal(key, id->key)) {
570 sent = sign_and_send_pubkey(authctxt, id);
580 /* try another method if we did not send a packet */
582 userauth(authctxt, NULL);
587 userauth_gssapi(Authctxt *authctxt)
589 Gssctxt *gssctxt = NULL;
590 static gss_OID_set gss_supported = NULL;
591 static u_int mech = 0;
594 char *gss_host = NULL;
596 if (!options.gss_authentication) {
597 verbose("GSSAPI authentication disabled.");
601 if (options.gss_trust_dns)
602 gss_host = (char *)get_canonical_hostname(1);
604 gss_host = (char *)authctxt->host;
606 /* Try one GSSAPI method at a time, rather than sending them all at
609 if (gss_supported == NULL)
610 gss_indicate_mechs(&min, &gss_supported);
612 /* Check to see if the mechanism is usable before we offer it */
613 while (mech < gss_supported->count && !ok) {
614 /* My DER encoding requires length<128 */
615 if (gss_supported->elements[mech].length < 128 &&
616 ssh_gssapi_check_mechanism(&gssctxt,
617 &gss_supported->elements[mech], gss_host)) {
618 ok = 1; /* Mechanism works */
627 authctxt->methoddata=(void *)gssctxt;
629 packet_start(SSH2_MSG_USERAUTH_REQUEST);
630 packet_put_cstring(authctxt->server_user);
631 packet_put_cstring(authctxt->service);
632 packet_put_cstring(authctxt->method->name);
636 packet_put_int((gss_supported->elements[mech].length) + 2);
637 packet_put_char(SSH_GSS_OIDTYPE);
638 packet_put_char(gss_supported->elements[mech].length);
639 packet_put_raw(gss_supported->elements[mech].elements,
640 gss_supported->elements[mech].length);
644 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
645 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
646 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
647 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
649 mech++; /* Move along to next candidate */
655 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
657 Authctxt *authctxt = ctxt;
658 Gssctxt *gssctxt = authctxt->methoddata;
659 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
660 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
661 gss_buffer_desc gssbuf;
662 OM_uint32 status, ms, flags;
665 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
666 recv_tok, &send_tok, &flags);
668 if (send_tok.length > 0) {
669 if (GSS_ERROR(status))
670 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
672 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
674 packet_put_string(send_tok.value, send_tok.length);
676 gss_release_buffer(&ms, &send_tok);
679 if (status == GSS_S_COMPLETE) {
680 /* send either complete or MIC, depending on mechanism */
681 if (strcmp(authctxt->method->name,"gssapi")==0 ||
682 (!(flags & GSS_C_INTEG_FLAG))) {
683 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
686 ssh_gssapi_buildmic(&b, authctxt->server_user,
687 authctxt->service, "gssapi-with-mic");
689 gssbuf.value = buffer_ptr(&b);
690 gssbuf.length = buffer_len(&b);
692 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
694 if (!GSS_ERROR(status)) {
695 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
696 packet_put_string(mic.value, mic.length);
702 gss_release_buffer(&ms, &mic);
710 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
712 Authctxt *authctxt = ctxt;
717 if (authctxt == NULL)
718 fatal("input_gssapi_response: no authentication context");
719 gssctxt = authctxt->methoddata;
722 oidv = packet_get_string(&oidlen);
725 oidv[0] != SSH_GSS_OIDTYPE ||
726 oidv[1] != oidlen - 2) {
728 debug("Badly encoded mechanism OID received");
729 userauth(authctxt, NULL);
733 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
734 fatal("Server returned different OID than expected");
740 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
741 /* Start again with next method on list */
742 debug("Trying to start again");
743 userauth(authctxt, NULL);
749 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
751 Authctxt *authctxt = ctxt;
752 gss_buffer_desc recv_tok;
756 if (authctxt == NULL)
757 fatal("input_gssapi_response: no authentication context");
759 recv_tok.value = packet_get_string(&slen);
760 recv_tok.length = slen; /* safe typecast */
764 status = process_gssapi_token(ctxt, &recv_tok);
766 xfree(recv_tok.value);
768 if (GSS_ERROR(status)) {
769 /* Start again with the next method in the list */
770 userauth(authctxt, NULL);
776 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
778 Authctxt *authctxt = ctxt;
780 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
781 gss_buffer_desc recv_tok;
782 OM_uint32 status, ms;
785 if (authctxt == NULL)
786 fatal("input_gssapi_response: no authentication context");
787 gssctxt = authctxt->methoddata;
789 recv_tok.value = packet_get_string(&len);
790 recv_tok.length = len;
794 /* Stick it into GSSAPI and see what it says */
795 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
796 &recv_tok, &send_tok, NULL);
798 xfree(recv_tok.value);
799 gss_release_buffer(&ms, &send_tok);
801 /* Server will be returning a failed packet after this one */
805 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
811 maj=packet_get_int();
812 min=packet_get_int();
813 msg=packet_get_string(NULL);
814 lang=packet_get_string(NULL);
818 debug("Server GSSAPI Error:\n%s", msg);
825 const gss_OID_desc * const gss_mech_globus_gssapi_openssl;
826 #define is_gsi_oid(oid) \
827 (oid->length == gss_mech_globus_gssapi_openssl->length && \
828 (memcmp(oid->elements, gss_mech_globus_gssapi_openssl->elements, \
833 userauth_external(Authctxt *authctxt)
835 static int attempt = 0;
840 /* The client MUST NOT try this method if initial key exchange
841 was not performed using a GSSAPI-based key exchange
843 if (gss_kex_context == NULL) {
844 debug2("gsskex not performed, skipping external-keyx");
848 debug2("userauth_external");
849 packet_start(SSH2_MSG_USERAUTH_REQUEST);
851 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
852 packet_put_cstring("");
855 packet_put_cstring(authctxt->server_user);
859 packet_put_cstring(authctxt->service);
860 packet_put_cstring(authctxt->method->name);
866 userauth_gsskeyex(Authctxt *authctxt)
869 gss_buffer_desc gssbuf;
870 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
873 static int attempt = 0;
877 if (gss_kex_context == NULL) {
878 debug("No valid Key exchange context");
883 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
884 ssh_gssapi_buildmic(&b, "", authctxt->service, "gssapi-keyex");
887 ssh_gssapi_buildmic(&b, authctxt->server_user, authctxt->service,
893 gssbuf.value = buffer_ptr(&b);
894 gssbuf.length = buffer_len(&b);
896 if (GSS_ERROR(ssh_gssapi_sign(gss_kex_context, &gssbuf, &mic))) {
901 packet_start(SSH2_MSG_USERAUTH_REQUEST);
903 if (options.implicit && is_gsi_oid(gss_kex_context->oid)) {
904 packet_put_cstring("");
907 packet_put_cstring(authctxt->server_user);
911 packet_put_cstring(authctxt->service);
912 packet_put_cstring(authctxt->method->name);
913 packet_put_string(mic.value, mic.length);
917 gss_release_buffer(&ms, &mic);
925 userauth_none(Authctxt *authctxt)
927 /* initial userauth request */
928 packet_start(SSH2_MSG_USERAUTH_REQUEST);
929 packet_put_cstring(authctxt->server_user);
930 packet_put_cstring(authctxt->service);
931 packet_put_cstring(authctxt->method->name);
937 userauth_passwd(Authctxt *authctxt)
939 static int attempt = 0;
943 if (attempt++ >= options.number_of_password_prompts)
947 error("Permission denied, please try again.");
949 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
950 authctxt->server_user, authctxt->host);
951 password = read_passphrase(prompt, 0);
952 packet_start(SSH2_MSG_USERAUTH_REQUEST);
953 packet_put_cstring(authctxt->server_user);
954 packet_put_cstring(authctxt->service);
955 packet_put_cstring(authctxt->method->name);
957 packet_put_cstring(password);
958 memset(password, 0, strlen(password));
960 packet_add_padding(64);
963 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
964 &input_userauth_passwd_changereq);
969 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
972 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
974 Authctxt *authctxt = ctxt;
975 char *info, *lang, *password = NULL, *retype = NULL;
978 debug2("input_userauth_passwd_changereq");
980 if (authctxt == NULL)
981 fatal("input_userauth_passwd_changereq: "
982 "no authentication context");
984 info = packet_get_string(NULL);
985 lang = packet_get_string(NULL);
986 if (strlen(info) > 0)
990 packet_start(SSH2_MSG_USERAUTH_REQUEST);
991 packet_put_cstring(authctxt->server_user);
992 packet_put_cstring(authctxt->service);
993 packet_put_cstring(authctxt->method->name);
994 packet_put_char(1); /* additional info */
995 snprintf(prompt, sizeof(prompt),
996 "Enter %.30s@%.128s's old password: ",
997 authctxt->server_user, authctxt->host);
998 password = read_passphrase(prompt, 0);
999 packet_put_cstring(password);
1000 memset(password, 0, strlen(password));
1003 while (password == NULL) {
1004 snprintf(prompt, sizeof(prompt),
1005 "Enter %.30s@%.128s's new password: ",
1006 authctxt->server_user, authctxt->host);
1007 password = read_passphrase(prompt, RP_ALLOW_EOF);
1008 if (password == NULL) {
1012 snprintf(prompt, sizeof(prompt),
1013 "Retype %.30s@%.128s's new password: ",
1014 authctxt->server_user, authctxt->host);
1015 retype = read_passphrase(prompt, 0);
1016 if (strcmp(password, retype) != 0) {
1017 memset(password, 0, strlen(password));
1019 logit("Mismatch; try again, EOF to quit.");
1022 memset(retype, 0, strlen(retype));
1025 packet_put_cstring(password);
1026 memset(password, 0, strlen(password));
1028 packet_add_padding(64);
1031 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1032 &input_userauth_passwd_changereq);
1036 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1037 u_char *data, u_int datalen)
1042 /* the agent supports this key */
1044 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1047 * we have already loaded the private key or
1048 * the private key is stored in external hardware
1050 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1051 return (key_sign(id->key, sigp, lenp, data, datalen));
1052 /* load the private key from the file */
1053 if ((prv = load_identity_file(id->filename)) == NULL)
1055 ret = key_sign(prv, sigp, lenp, data, datalen);
1061 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1064 u_char *blob, *signature;
1065 u_int bloblen, slen;
1070 debug3("sign_and_send_pubkey");
1072 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1073 /* we cannot handle this key */
1074 debug3("sign_and_send_pubkey: cannot handle key");
1077 /* data to be signed */
1079 if (datafellows & SSH_OLD_SESSIONID) {
1080 buffer_append(&b, session_id2, session_id2_len);
1081 skip = session_id2_len;
1083 buffer_put_string(&b, session_id2, session_id2_len);
1084 skip = buffer_len(&b);
1086 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1087 buffer_put_cstring(&b, authctxt->server_user);
1088 buffer_put_cstring(&b,
1089 datafellows & SSH_BUG_PKSERVICE ?
1092 if (datafellows & SSH_BUG_PKAUTH) {
1093 buffer_put_char(&b, have_sig);
1095 buffer_put_cstring(&b, authctxt->method->name);
1096 buffer_put_char(&b, have_sig);
1097 buffer_put_cstring(&b, key_ssh_name(id->key));
1099 buffer_put_string(&b, blob, bloblen);
1101 /* generate signature */
1102 ret = identity_sign(id, &signature, &slen,
1103 buffer_ptr(&b), buffer_len(&b));
1112 if (datafellows & SSH_BUG_PKSERVICE) {
1114 buffer_append(&b, session_id2, session_id2_len);
1115 skip = session_id2_len;
1116 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1117 buffer_put_cstring(&b, authctxt->server_user);
1118 buffer_put_cstring(&b, authctxt->service);
1119 buffer_put_cstring(&b, authctxt->method->name);
1120 buffer_put_char(&b, have_sig);
1121 if (!(datafellows & SSH_BUG_PKAUTH))
1122 buffer_put_cstring(&b, key_ssh_name(id->key));
1123 buffer_put_string(&b, blob, bloblen);
1127 /* append signature */
1128 buffer_put_string(&b, signature, slen);
1131 /* skip session id and packet type */
1132 if (buffer_len(&b) < skip + 1)
1133 fatal("userauth_pubkey: internal error");
1134 buffer_consume(&b, skip + 1);
1136 /* put remaining data from buffer into packet */
1137 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1138 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1146 send_pubkey_test(Authctxt *authctxt, Identity *id)
1149 u_int bloblen, have_sig = 0;
1151 debug3("send_pubkey_test");
1153 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1154 /* we cannot handle this key */
1155 debug3("send_pubkey_test: cannot handle key");
1158 /* register callback for USERAUTH_PK_OK message */
1159 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1161 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1162 packet_put_cstring(authctxt->server_user);
1163 packet_put_cstring(authctxt->service);
1164 packet_put_cstring(authctxt->method->name);
1165 packet_put_char(have_sig);
1166 if (!(datafellows & SSH_BUG_PKAUTH))
1167 packet_put_cstring(key_ssh_name(id->key));
1168 packet_put_string(blob, bloblen);
1175 load_identity_file(char *filename)
1178 char prompt[300], *passphrase;
1179 int perm_ok, quit, i;
1182 if (stat(filename, &st) < 0) {
1183 debug3("no such identity: %s", filename);
1186 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1189 if (private == NULL) {
1190 if (options.batch_mode)
1192 snprintf(prompt, sizeof prompt,
1193 "Enter passphrase for key '%.100s': ", filename);
1194 for (i = 0; i < options.number_of_password_prompts; i++) {
1195 passphrase = read_passphrase(prompt, 0);
1196 if (strcmp(passphrase, "") != 0) {
1197 private = key_load_private_type(KEY_UNSPEC,
1198 filename, passphrase, NULL, NULL);
1201 debug2("no passphrase given, try next key");
1204 memset(passphrase, 0, strlen(passphrase));
1206 if (private != NULL || quit)
1208 debug2("bad passphrase given, try again...");
1215 * try keys in the following order:
1216 * 1. agent keys that are found in the config file
1217 * 2. other agent keys
1218 * 3. keys that are only listed in the config file
1221 pubkey_prepare(Authctxt *authctxt)
1224 Idlist agent, files, *preferred;
1226 AuthenticationConnection *ac;
1230 TAILQ_INIT(&agent); /* keys from the agent */
1231 TAILQ_INIT(&files); /* keys from the config file */
1232 preferred = &authctxt->keys;
1233 TAILQ_INIT(preferred); /* preferred order of keys */
1235 /* list of keys stored in the filesystem */
1236 for (i = 0; i < options.num_identity_files; i++) {
1237 key = options.identity_keys[i];
1238 if (key && key->type == KEY_RSA1)
1240 options.identity_keys[i] = NULL;
1241 id = xcalloc(1, sizeof(*id));
1243 id->filename = xstrdup(options.identity_files[i]);
1244 TAILQ_INSERT_TAIL(&files, id, next);
1246 /* list of keys supported by the agent */
1247 if ((ac = ssh_get_authentication_connection())) {
1248 for (key = ssh_get_first_identity(ac, &comment, 2);
1250 key = ssh_get_next_identity(ac, &comment, 2)) {
1252 TAILQ_FOREACH(id, &files, next) {
1253 /* agent keys from the config file are preferred */
1254 if (key_equal(key, id->key)) {
1257 TAILQ_REMOVE(&files, id, next);
1258 TAILQ_INSERT_TAIL(preferred, id, next);
1264 if (!found && !options.identities_only) {
1265 id = xcalloc(1, sizeof(*id));
1267 id->filename = comment;
1269 TAILQ_INSERT_TAIL(&agent, id, next);
1272 /* append remaining agent keys */
1273 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1274 TAILQ_REMOVE(&agent, id, next);
1275 TAILQ_INSERT_TAIL(preferred, id, next);
1277 authctxt->agent = ac;
1279 /* append remaining keys from the config file */
1280 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1281 TAILQ_REMOVE(&files, id, next);
1282 TAILQ_INSERT_TAIL(preferred, id, next);
1284 TAILQ_FOREACH(id, preferred, next) {
1285 debug2("key: %s (%p)", id->filename, id->key);
1290 pubkey_cleanup(Authctxt *authctxt)
1294 if (authctxt->agent != NULL)
1295 ssh_close_authentication_connection(authctxt->agent);
1296 for (id = TAILQ_FIRST(&authctxt->keys); id;
1297 id = TAILQ_FIRST(&authctxt->keys)) {
1298 TAILQ_REMOVE(&authctxt->keys, id, next);
1302 xfree(id->filename);
1308 userauth_pubkey(Authctxt *authctxt)
1313 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1316 /* move key to the end of the queue */
1317 TAILQ_REMOVE(&authctxt->keys, id, next);
1318 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1320 * send a test message if we have the public key. for
1321 * encrypted keys we cannot do this and have to load the
1322 * private key instead
1324 if (id->key && id->key->type != KEY_RSA1) {
1325 debug("Offering public key: %s", id->filename);
1326 sent = send_pubkey_test(authctxt, id);
1327 } else if (id->key == NULL) {
1328 debug("Trying private key: %s", id->filename);
1329 id->key = load_identity_file(id->filename);
1330 if (id->key != NULL) {
1332 sent = sign_and_send_pubkey(authctxt, id);
1344 * Send userauth request message specifying keyboard-interactive method.
1347 userauth_kbdint(Authctxt *authctxt)
1349 static int attempt = 0;
1351 if (attempt++ >= options.number_of_password_prompts)
1353 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1354 if (attempt > 1 && !authctxt->info_req_seen) {
1355 debug3("userauth_kbdint: disable: no info_req_seen");
1356 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1360 debug2("userauth_kbdint");
1361 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1362 packet_put_cstring(authctxt->server_user);
1363 packet_put_cstring(authctxt->service);
1364 packet_put_cstring(authctxt->method->name);
1365 packet_put_cstring(""); /* lang */
1366 packet_put_cstring(options.kbd_interactive_devices ?
1367 options.kbd_interactive_devices : "");
1370 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1375 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1378 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1380 Authctxt *authctxt = ctxt;
1381 char *name, *inst, *lang, *prompt, *response;
1382 u_int num_prompts, i;
1385 debug2("input_userauth_info_req");
1387 if (authctxt == NULL)
1388 fatal("input_userauth_info_req: no authentication context");
1390 authctxt->info_req_seen = 1;
1392 name = packet_get_string(NULL);
1393 inst = packet_get_string(NULL);
1394 lang = packet_get_string(NULL);
1395 if (strlen(name) > 0)
1397 if (strlen(inst) > 0)
1403 num_prompts = packet_get_int();
1405 * Begin to build info response packet based on prompts requested.
1406 * We commit to providing the correct number of responses, so if
1407 * further on we run into a problem that prevents this, we have to
1408 * be sure and clean this up and send a correct error response.
1410 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1411 packet_put_int(num_prompts);
1413 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1414 for (i = 0; i < num_prompts; i++) {
1415 prompt = packet_get_string(NULL);
1416 echo = packet_get_char();
1418 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1420 packet_put_cstring(response);
1421 memset(response, 0, strlen(response));
1425 packet_check_eom(); /* done with parsing incoming message. */
1427 packet_add_padding(64);
1432 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1433 u_char *data, u_int datalen)
1438 int to[2], from[2], status, version = 2;
1440 debug2("ssh_keysign called");
1442 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1443 error("ssh_keysign: no installed: %s", strerror(errno));
1446 if (fflush(stdout) != 0)
1447 error("ssh_keysign: fflush: %s", strerror(errno));
1449 error("ssh_keysign: pipe: %s", strerror(errno));
1452 if (pipe(from) < 0) {
1453 error("ssh_keysign: pipe: %s", strerror(errno));
1456 if ((pid = fork()) < 0) {
1457 error("ssh_keysign: fork: %s", strerror(errno));
1461 permanently_drop_suid(getuid());
1463 if (dup2(from[1], STDOUT_FILENO) < 0)
1464 fatal("ssh_keysign: dup2: %s", strerror(errno));
1466 if (dup2(to[0], STDIN_FILENO) < 0)
1467 fatal("ssh_keysign: dup2: %s", strerror(errno));
1470 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1471 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1478 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1479 buffer_put_string(&b, data, datalen);
1480 if (ssh_msg_send(to[1], version, &b) == -1)
1481 fatal("ssh_keysign: couldn't send request");
1483 if (ssh_msg_recv(from[0], &b) < 0) {
1484 error("ssh_keysign: no reply");
1491 while (waitpid(pid, &status, 0) < 0)
1495 if (buffer_get_char(&b) != version) {
1496 error("ssh_keysign: bad version");
1500 *sigp = buffer_get_string(&b, lenp);
1507 userauth_hostbased(Authctxt *authctxt)
1509 Key *private = NULL;
1510 Sensitive *sensitive = authctxt->sensitive;
1512 u_char *signature, *blob;
1513 char *chost, *pkalg, *p;
1514 const char *service;
1516 int ok, i, len, found = 0;
1518 /* check for a useful key */
1519 for (i = 0; i < sensitive->nkeys; i++) {
1520 private = sensitive->keys[i];
1521 if (private && private->type != KEY_RSA1) {
1523 /* we take and free the key */
1524 sensitive->keys[i] = NULL;
1529 debug("No more client hostkeys for hostbased authentication.");
1532 if (key_to_blob(private, &blob, &blen) == 0) {
1536 /* figure out a name for the client host */
1537 p = get_local_name(packet_get_connection_in());
1539 error("userauth_hostbased: cannot get local ipaddr/name");
1544 len = strlen(p) + 2;
1545 xasprintf(&chost, "%s.", p);
1546 debug2("userauth_hostbased: chost %s", chost);
1549 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1551 pkalg = xstrdup(key_ssh_name(private));
1553 /* construct data */
1554 buffer_put_string(&b, session_id2, session_id2_len);
1555 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1556 buffer_put_cstring(&b, authctxt->server_user);
1557 buffer_put_cstring(&b, service);
1558 buffer_put_cstring(&b, authctxt->method->name);
1559 buffer_put_cstring(&b, pkalg);
1560 buffer_put_string(&b, blob, blen);
1561 buffer_put_cstring(&b, chost);
1562 buffer_put_cstring(&b, authctxt->local_user);
1566 if (sensitive->external_keysign)
1567 ok = ssh_keysign(private, &signature, &slen,
1568 buffer_ptr(&b), buffer_len(&b));
1570 ok = key_sign(private, &signature, &slen,
1571 buffer_ptr(&b), buffer_len(&b));
1575 error("key_sign failed");
1581 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1582 packet_put_cstring(authctxt->server_user);
1583 packet_put_cstring(authctxt->service);
1584 packet_put_cstring(authctxt->method->name);
1585 packet_put_cstring(pkalg);
1586 packet_put_string(blob, blen);
1587 packet_put_cstring(chost);
1588 packet_put_cstring(authctxt->local_user);
1589 packet_put_string(signature, slen);
1590 memset(signature, 's', slen);
1600 /* find auth method */
1603 * given auth method name, if configurable options permit this method fill
1604 * in auth_ident field and return true, otherwise return false.
1607 authmethod_is_enabled(Authmethod *method)
1611 /* return false if options indicate this method is disabled */
1612 if (method->enabled == NULL || *method->enabled == 0)
1614 /* return false if batch mode is enabled but method needs interactive mode */
1615 if (method->batch_flag != NULL && *method->batch_flag != 0)
1621 authmethod_lookup(const char *name)
1623 Authmethod *method = NULL;
1625 for (method = authmethods; method->name != NULL; method++)
1626 if (strcmp(name, method->name) == 0)
1628 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1632 /* XXX internal state */
1633 static Authmethod *current = NULL;
1634 static char *supported = NULL;
1635 static char *preferred = NULL;
1638 * Given the authentication method list sent by the server, return the
1639 * next method we should try. If the server initially sends a nil list,
1640 * use a built-in default list.
1643 authmethod_get(char *authlist)
1648 /* Use a suitable default if we're passed a nil list. */
1649 if (authlist == NULL || strlen(authlist) == 0)
1650 authlist = options.preferred_authentications;
1652 if (supported == NULL || strcmp(authlist, supported) != 0) {
1653 debug3("start over, passed a different list %s", authlist);
1654 if (supported != NULL)
1656 supported = xstrdup(authlist);
1657 preferred = options.preferred_authentications;
1658 debug3("preferred %s", preferred);
1660 } else if (current != NULL && authmethod_is_enabled(current))
1664 if ((name = match_list(preferred, supported, &next)) == NULL) {
1665 debug("No more authentication methods to try.");
1670 debug3("authmethod_lookup %s", name);
1671 debug3("remaining preferred: %s", preferred);
1672 if ((current = authmethod_lookup(name)) != NULL &&
1673 authmethod_is_enabled(current)) {
1674 debug3("authmethod_is_enabled %s", name);
1675 debug("Next authentication method: %s", name);
1682 authmethods_get(void)
1684 Authmethod *method = NULL;
1689 for (method = authmethods; method->name != NULL; method++) {
1690 if (authmethod_is_enabled(method)) {
1691 if (buffer_len(&b) > 0)
1692 buffer_append(&b, ",", 1);
1693 buffer_append(&b, method->name, strlen(method->name));
1696 buffer_append(&b, "\0", 1);
1697 list = xstrdup(buffer_ptr(&b));