1 /* $OpenBSD: sshconnect2.c,v 1.166 2008/07/17 08:48:00 djm Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/types.h>
29 #include <sys/socket.h>
41 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
45 #include "openbsd-compat/sys-queue.h"
56 #include "myproposal.h"
57 #include "sshconnect.h"
68 #include "pathnames.h"
76 extern char *client_version_string;
77 extern char *server_version_string;
78 extern Options options;
81 /* tty_flag is set in ssh.c. use this in ssh_userauth2 */
82 /* if it is set then prevent the switch to the null cipher */
90 u_char *session_id2 = NULL;
91 u_int session_id2_len = 0;
94 struct sockaddr *xxx_hostaddr;
99 verify_host_key_callback(Key *hostkey)
101 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
102 fatal("Host key verification failed.");
107 ssh_kex2(char *host, struct sockaddr *hostaddr)
112 xxx_hostaddr = hostaddr;
114 if (options.ciphers == (char *)-1) {
115 logit("No valid ciphers for protocol version 2 given, using defaults.");
116 options.ciphers = NULL;
118 if (options.ciphers != NULL) {
119 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
120 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
122 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
123 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
124 myproposal[PROPOSAL_ENC_ALGS_STOC] =
125 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
126 if (options.compression) {
127 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
128 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
130 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
131 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
133 if (options.macs != NULL) {
134 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
135 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
137 if (options.hostkeyalgorithms != NULL)
138 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
139 options.hostkeyalgorithms;
141 if (options.rekey_limit)
142 packet_set_rekey_limit((u_int32_t)options.rekey_limit);
144 /* start key exchange */
145 kex = kex_setup(myproposal);
146 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
147 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
148 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
149 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
150 kex->client_version_string=client_version_string;
151 kex->server_version_string=server_version_string;
152 kex->verify_host_key=&verify_host_key_callback;
156 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
158 session_id2 = kex->session_id;
159 session_id2_len = kex->session_id_len;
162 /* send 1st encrypted/maced/compressed message */
163 packet_start(SSH2_MSG_IGNORE);
164 packet_put_cstring("markus");
174 typedef struct Authctxt Authctxt;
175 typedef struct Authmethod Authmethod;
176 typedef struct identity Identity;
177 typedef struct idlist Idlist;
180 TAILQ_ENTRY(identity) next;
181 AuthenticationConnection *ac; /* set if agent supports key */
182 Key *key; /* public/private key */
183 char *filename; /* comment for agent-only keys */
185 int isprivate; /* key points to the private key */
187 TAILQ_HEAD(idlist, identity);
190 const char *server_user;
191 const char *local_user;
199 AuthenticationConnection *agent;
201 Sensitive *sensitive;
202 /* kbd-interactive */
208 char *name; /* string to compare against server's list */
209 int (*userauth)(Authctxt *authctxt);
210 int *enabled; /* flag in option struct that enables method */
211 int *batch_flag; /* flag in option struct that disables method */
214 void input_userauth_success(int, u_int32_t, void *);
215 void input_userauth_failure(int, u_int32_t, void *);
216 void input_userauth_banner(int, u_int32_t, void *);
217 void input_userauth_error(int, u_int32_t, void *);
218 void input_userauth_info_req(int, u_int32_t, void *);
219 void input_userauth_pk_ok(int, u_int32_t, void *);
220 void input_userauth_passwd_changereq(int, u_int32_t, void *);
222 int userauth_none(Authctxt *);
223 int userauth_pubkey(Authctxt *);
224 int userauth_passwd(Authctxt *);
225 int userauth_kbdint(Authctxt *);
226 int userauth_hostbased(Authctxt *);
227 int userauth_kerberos(Authctxt *);
230 int userauth_gssapi(Authctxt *authctxt);
231 void input_gssapi_response(int type, u_int32_t, void *);
232 void input_gssapi_token(int type, u_int32_t, void *);
233 void input_gssapi_hash(int type, u_int32_t, void *);
234 void input_gssapi_error(int, u_int32_t, void *);
235 void input_gssapi_errtok(int, u_int32_t, void *);
238 void userauth(Authctxt *, char *);
240 static int sign_and_send_pubkey(Authctxt *, Identity *);
241 static void pubkey_prepare(Authctxt *);
242 static void pubkey_cleanup(Authctxt *);
243 static Key *load_identity_file(char *);
245 static Authmethod *authmethod_get(char *authlist);
246 static Authmethod *authmethod_lookup(const char *name);
247 static char *authmethods_get(void);
249 Authmethod authmethods[] = {
253 &options.gss_authentication,
258 &options.hostbased_authentication,
262 &options.pubkey_authentication,
264 {"keyboard-interactive",
266 &options.kbd_interactive_authentication,
267 &options.batch_mode},
270 &options.password_authentication,
271 &options.batch_mode},
276 {NULL, NULL, NULL, NULL}
280 ssh_userauth2(const char *local_user, const char *server_user, char *host,
281 Sensitive *sensitive)
286 if (options.challenge_response_authentication)
287 options.kbd_interactive_authentication = 1;
289 packet_start(SSH2_MSG_SERVICE_REQUEST);
290 packet_put_cstring("ssh-userauth");
292 debug("SSH2_MSG_SERVICE_REQUEST sent");
294 type = packet_read();
295 if (type != SSH2_MSG_SERVICE_ACCEPT)
296 fatal("Server denied authentication request: %d", type);
297 if (packet_remaining() > 0) {
298 char *reply = packet_get_string(NULL);
299 debug2("service_accept: %s", reply);
302 debug2("buggy server: service_accept w/o service");
305 debug("SSH2_MSG_SERVICE_ACCEPT received");
307 if (options.preferred_authentications == NULL)
308 options.preferred_authentications = authmethods_get();
310 /* setup authentication context */
311 memset(&authctxt, 0, sizeof(authctxt));
312 pubkey_prepare(&authctxt);
313 authctxt.server_user = server_user;
314 authctxt.local_user = local_user;
315 authctxt.host = host;
316 authctxt.service = "ssh-connection"; /* service name */
317 authctxt.success = 0;
318 authctxt.method = authmethod_lookup("none");
319 authctxt.authlist = NULL;
320 authctxt.methoddata = NULL;
321 authctxt.sensitive = sensitive;
322 authctxt.info_req_seen = 0;
323 if (authctxt.method == NULL)
324 fatal("ssh_userauth2: internal error: cannot send userauth none request");
326 /* initial userauth request */
327 userauth_none(&authctxt);
329 dispatch_init(&input_userauth_error);
330 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
331 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
332 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
333 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
335 pubkey_cleanup(&authctxt);
336 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
338 /* if the user wants to use the none cipher do it */
339 /* post authentication and only if the right conditions are met */
340 /* both of the NONE commands must be true and there must be no */
342 if ((options.none_switch == 1) && (options.none_enabled == 1))
344 if (!tty_flag) /* no null on tty sessions */
346 debug("Requesting none rekeying...");
347 myproposal[PROPOSAL_ENC_ALGS_STOC] = "none";
348 myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none";
349 kex_prop2buf(&xxx_kex->my,myproposal);
350 packet_request_rekeying();
351 fprintf(stderr, "WARNING: ENABLED NONE CIPHER\n");
355 /* requested NONE cipher when in a tty */
356 debug("Cannot switch to NONE cipher with tty allocated");
357 fprintf(stderr, "NONE cipher switch disabled when a TTY is allocated\n");
360 debug("Authentication succeeded (%s).", authctxt.method->name);
364 userauth(Authctxt *authctxt, char *authlist)
366 if (authctxt->methoddata) {
367 xfree(authctxt->methoddata);
368 authctxt->methoddata = NULL;
370 if (authlist == NULL) {
371 authlist = authctxt->authlist;
373 if (authctxt->authlist)
374 xfree(authctxt->authlist);
375 authctxt->authlist = authlist;
378 Authmethod *method = authmethod_get(authlist);
380 fatal("Permission denied (%s).", authlist);
381 authctxt->method = method;
383 /* reset the per method handler */
384 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
385 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
387 /* and try new method */
388 if (method->userauth(authctxt) != 0) {
389 debug2("we sent a %s packet, wait for reply", method->name);
392 debug2("we did not send a packet, disable method");
393 method->enabled = NULL;
399 input_userauth_error(int type, u_int32_t seq, void *ctxt)
401 fatal("input_userauth_error: bad message during authentication: "
406 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
408 char *msg, *raw, *lang;
411 debug3("input_userauth_banner");
412 raw = packet_get_string(&len);
413 lang = packet_get_string(NULL);
414 if (options.log_level >= SYSLOG_LEVEL_INFO) {
417 msg = xmalloc(len * 4); /* max expansion from strnvis() */
418 strnvis(msg, raw, len * 4, VIS_SAFE|VIS_OCTAL);
419 fprintf(stderr, "%s", msg);
427 input_userauth_success(int type, u_int32_t seq, void *ctxt)
429 Authctxt *authctxt = ctxt;
430 if (authctxt == NULL)
431 fatal("input_userauth_success: no authentication context");
432 if (authctxt->authlist) {
433 xfree(authctxt->authlist);
434 authctxt->authlist = NULL;
436 if (authctxt->methoddata) {
437 xfree(authctxt->methoddata);
438 authctxt->methoddata = NULL;
440 authctxt->success = 1; /* break out */
444 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
446 Authctxt *authctxt = ctxt;
447 char *authlist = NULL;
450 if (authctxt == NULL)
451 fatal("input_userauth_failure: no authentication context");
453 authlist = packet_get_string(NULL);
454 partial = packet_get_char();
458 logit("Authenticated with partial success.");
459 debug("Authentications that can continue: %s", authlist);
461 userauth(authctxt, authlist);
464 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
466 Authctxt *authctxt = ctxt;
470 int pktype, sent = 0;
475 if (authctxt == NULL)
476 fatal("input_userauth_pk_ok: no authentication context");
477 if (datafellows & SSH_BUG_PKOK) {
478 /* this is similar to SSH_BUG_PKAUTH */
479 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
480 pkblob = packet_get_string(&blen);
482 buffer_append(&b, pkblob, blen);
483 pkalg = buffer_get_string(&b, &alen);
486 pkalg = packet_get_string(&alen);
487 pkblob = packet_get_string(&blen);
491 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
493 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
494 debug("unknown pkalg %s", pkalg);
497 if ((key = key_from_blob(pkblob, blen)) == NULL) {
498 debug("no key from blob. pkalg %s", pkalg);
501 if (key->type != pktype) {
502 error("input_userauth_pk_ok: type mismatch "
503 "for decoded key (received %d, expected %d)",
507 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
508 debug2("input_userauth_pk_ok: fp %s", fp);
512 * search keys in the reverse order, because last candidate has been
513 * moved to the end of the queue. this also avoids confusion by
516 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
517 if (key_equal(key, id->key)) {
518 sent = sign_and_send_pubkey(authctxt, id);
528 /* try another method if we did not send a packet */
530 userauth(authctxt, NULL);
535 userauth_gssapi(Authctxt *authctxt)
537 Gssctxt *gssctxt = NULL;
538 static gss_OID_set gss_supported = NULL;
539 static u_int mech = 0;
543 /* Try one GSSAPI method at a time, rather than sending them all at
546 if (gss_supported == NULL)
547 gss_indicate_mechs(&min, &gss_supported);
549 /* Check to see if the mechanism is usable before we offer it */
550 while (mech < gss_supported->count && !ok) {
551 /* My DER encoding requires length<128 */
552 if (gss_supported->elements[mech].length < 128 &&
553 ssh_gssapi_check_mechanism(&gssctxt,
554 &gss_supported->elements[mech], authctxt->host)) {
555 ok = 1; /* Mechanism works */
564 authctxt->methoddata=(void *)gssctxt;
566 packet_start(SSH2_MSG_USERAUTH_REQUEST);
567 packet_put_cstring(authctxt->server_user);
568 packet_put_cstring(authctxt->service);
569 packet_put_cstring(authctxt->method->name);
573 packet_put_int((gss_supported->elements[mech].length) + 2);
574 packet_put_char(SSH_GSS_OIDTYPE);
575 packet_put_char(gss_supported->elements[mech].length);
576 packet_put_raw(gss_supported->elements[mech].elements,
577 gss_supported->elements[mech].length);
581 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
582 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
583 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
584 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
586 mech++; /* Move along to next candidate */
592 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
594 Authctxt *authctxt = ctxt;
595 Gssctxt *gssctxt = authctxt->methoddata;
596 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
597 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
598 gss_buffer_desc gssbuf;
599 OM_uint32 status, ms, flags;
602 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
603 recv_tok, &send_tok, &flags);
605 if (send_tok.length > 0) {
606 if (GSS_ERROR(status))
607 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
609 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
611 packet_put_string(send_tok.value, send_tok.length);
613 gss_release_buffer(&ms, &send_tok);
616 if (status == GSS_S_COMPLETE) {
617 /* send either complete or MIC, depending on mechanism */
618 if (!(flags & GSS_C_INTEG_FLAG)) {
619 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
622 ssh_gssapi_buildmic(&b, authctxt->server_user,
623 authctxt->service, "gssapi-with-mic");
625 gssbuf.value = buffer_ptr(&b);
626 gssbuf.length = buffer_len(&b);
628 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
630 if (!GSS_ERROR(status)) {
631 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
632 packet_put_string(mic.value, mic.length);
638 gss_release_buffer(&ms, &mic);
646 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
648 Authctxt *authctxt = ctxt;
653 if (authctxt == NULL)
654 fatal("input_gssapi_response: no authentication context");
655 gssctxt = authctxt->methoddata;
658 oidv = packet_get_string(&oidlen);
661 oidv[0] != SSH_GSS_OIDTYPE ||
662 oidv[1] != oidlen - 2) {
664 debug("Badly encoded mechanism OID received");
665 userauth(authctxt, NULL);
669 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
670 fatal("Server returned different OID than expected");
676 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
677 /* Start again with next method on list */
678 debug("Trying to start again");
679 userauth(authctxt, NULL);
685 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
687 Authctxt *authctxt = ctxt;
688 gss_buffer_desc recv_tok;
692 if (authctxt == NULL)
693 fatal("input_gssapi_response: no authentication context");
695 recv_tok.value = packet_get_string(&slen);
696 recv_tok.length = slen; /* safe typecast */
700 status = process_gssapi_token(ctxt, &recv_tok);
702 xfree(recv_tok.value);
704 if (GSS_ERROR(status)) {
705 /* Start again with the next method in the list */
706 userauth(authctxt, NULL);
712 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
714 Authctxt *authctxt = ctxt;
716 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
717 gss_buffer_desc recv_tok;
718 OM_uint32 status, ms;
721 if (authctxt == NULL)
722 fatal("input_gssapi_response: no authentication context");
723 gssctxt = authctxt->methoddata;
725 recv_tok.value = packet_get_string(&len);
726 recv_tok.length = len;
730 /* Stick it into GSSAPI and see what it says */
731 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
732 &recv_tok, &send_tok, NULL);
734 xfree(recv_tok.value);
735 gss_release_buffer(&ms, &send_tok);
737 /* Server will be returning a failed packet after this one */
741 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
747 maj=packet_get_int();
748 min=packet_get_int();
749 msg=packet_get_string(NULL);
750 lang=packet_get_string(NULL);
754 debug("Server GSSAPI Error:\n%s", msg);
761 userauth_none(Authctxt *authctxt)
763 /* initial userauth request */
764 packet_start(SSH2_MSG_USERAUTH_REQUEST);
765 packet_put_cstring(authctxt->server_user);
766 packet_put_cstring(authctxt->service);
767 packet_put_cstring(authctxt->method->name);
773 userauth_passwd(Authctxt *authctxt)
775 static int attempt = 0;
779 if (attempt++ >= options.number_of_password_prompts)
783 error("Permission denied, please try again.");
785 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
786 authctxt->server_user, authctxt->host);
787 password = read_passphrase(prompt, 0);
788 packet_start(SSH2_MSG_USERAUTH_REQUEST);
789 packet_put_cstring(authctxt->server_user);
790 packet_put_cstring(authctxt->service);
791 packet_put_cstring(authctxt->method->name);
793 packet_put_cstring(password);
794 memset(password, 0, strlen(password));
796 packet_add_padding(64);
799 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
800 &input_userauth_passwd_changereq);
805 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
808 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
810 Authctxt *authctxt = ctxt;
811 char *info, *lang, *password = NULL, *retype = NULL;
814 debug2("input_userauth_passwd_changereq");
816 if (authctxt == NULL)
817 fatal("input_userauth_passwd_changereq: "
818 "no authentication context");
820 info = packet_get_string(NULL);
821 lang = packet_get_string(NULL);
822 if (strlen(info) > 0)
826 packet_start(SSH2_MSG_USERAUTH_REQUEST);
827 packet_put_cstring(authctxt->server_user);
828 packet_put_cstring(authctxt->service);
829 packet_put_cstring(authctxt->method->name);
830 packet_put_char(1); /* additional info */
831 snprintf(prompt, sizeof(prompt),
832 "Enter %.30s@%.128s's old password: ",
833 authctxt->server_user, authctxt->host);
834 password = read_passphrase(prompt, 0);
835 packet_put_cstring(password);
836 memset(password, 0, strlen(password));
839 while (password == NULL) {
840 snprintf(prompt, sizeof(prompt),
841 "Enter %.30s@%.128s's new password: ",
842 authctxt->server_user, authctxt->host);
843 password = read_passphrase(prompt, RP_ALLOW_EOF);
844 if (password == NULL) {
848 snprintf(prompt, sizeof(prompt),
849 "Retype %.30s@%.128s's new password: ",
850 authctxt->server_user, authctxt->host);
851 retype = read_passphrase(prompt, 0);
852 if (strcmp(password, retype) != 0) {
853 memset(password, 0, strlen(password));
855 logit("Mismatch; try again, EOF to quit.");
858 memset(retype, 0, strlen(retype));
861 packet_put_cstring(password);
862 memset(password, 0, strlen(password));
864 packet_add_padding(64);
867 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
868 &input_userauth_passwd_changereq);
872 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
873 u_char *data, u_int datalen)
878 /* the agent supports this key */
880 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
883 * we have already loaded the private key or
884 * the private key is stored in external hardware
886 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
887 return (key_sign(id->key, sigp, lenp, data, datalen));
888 /* load the private key from the file */
889 if ((prv = load_identity_file(id->filename)) == NULL)
891 ret = key_sign(prv, sigp, lenp, data, datalen);
897 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
900 u_char *blob, *signature;
906 debug3("sign_and_send_pubkey");
908 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
909 /* we cannot handle this key */
910 debug3("sign_and_send_pubkey: cannot handle key");
913 /* data to be signed */
915 if (datafellows & SSH_OLD_SESSIONID) {
916 buffer_append(&b, session_id2, session_id2_len);
917 skip = session_id2_len;
919 buffer_put_string(&b, session_id2, session_id2_len);
920 skip = buffer_len(&b);
922 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
923 buffer_put_cstring(&b, authctxt->server_user);
924 buffer_put_cstring(&b,
925 datafellows & SSH_BUG_PKSERVICE ?
928 if (datafellows & SSH_BUG_PKAUTH) {
929 buffer_put_char(&b, have_sig);
931 buffer_put_cstring(&b, authctxt->method->name);
932 buffer_put_char(&b, have_sig);
933 buffer_put_cstring(&b, key_ssh_name(id->key));
935 buffer_put_string(&b, blob, bloblen);
937 /* generate signature */
938 ret = identity_sign(id, &signature, &slen,
939 buffer_ptr(&b), buffer_len(&b));
948 if (datafellows & SSH_BUG_PKSERVICE) {
950 buffer_append(&b, session_id2, session_id2_len);
951 skip = session_id2_len;
952 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
953 buffer_put_cstring(&b, authctxt->server_user);
954 buffer_put_cstring(&b, authctxt->service);
955 buffer_put_cstring(&b, authctxt->method->name);
956 buffer_put_char(&b, have_sig);
957 if (!(datafellows & SSH_BUG_PKAUTH))
958 buffer_put_cstring(&b, key_ssh_name(id->key));
959 buffer_put_string(&b, blob, bloblen);
963 /* append signature */
964 buffer_put_string(&b, signature, slen);
967 /* skip session id and packet type */
968 if (buffer_len(&b) < skip + 1)
969 fatal("userauth_pubkey: internal error");
970 buffer_consume(&b, skip + 1);
972 /* put remaining data from buffer into packet */
973 packet_start(SSH2_MSG_USERAUTH_REQUEST);
974 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
982 send_pubkey_test(Authctxt *authctxt, Identity *id)
985 u_int bloblen, have_sig = 0;
987 debug3("send_pubkey_test");
989 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
990 /* we cannot handle this key */
991 debug3("send_pubkey_test: cannot handle key");
994 /* register callback for USERAUTH_PK_OK message */
995 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
997 packet_start(SSH2_MSG_USERAUTH_REQUEST);
998 packet_put_cstring(authctxt->server_user);
999 packet_put_cstring(authctxt->service);
1000 packet_put_cstring(authctxt->method->name);
1001 packet_put_char(have_sig);
1002 if (!(datafellows & SSH_BUG_PKAUTH))
1003 packet_put_cstring(key_ssh_name(id->key));
1004 packet_put_string(blob, bloblen);
1011 load_identity_file(char *filename)
1014 char prompt[300], *passphrase;
1015 int perm_ok, quit, i;
1018 if (stat(filename, &st) < 0) {
1019 debug3("no such identity: %s", filename);
1022 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1025 if (private == NULL) {
1026 if (options.batch_mode)
1028 snprintf(prompt, sizeof prompt,
1029 "Enter passphrase for key '%.100s': ", filename);
1030 for (i = 0; i < options.number_of_password_prompts; i++) {
1031 passphrase = read_passphrase(prompt, 0);
1032 if (strcmp(passphrase, "") != 0) {
1033 private = key_load_private_type(KEY_UNSPEC,
1034 filename, passphrase, NULL, NULL);
1037 debug2("no passphrase given, try next key");
1040 memset(passphrase, 0, strlen(passphrase));
1042 if (private != NULL || quit)
1044 debug2("bad passphrase given, try again...");
1051 * try keys in the following order:
1052 * 1. agent keys that are found in the config file
1053 * 2. other agent keys
1054 * 3. keys that are only listed in the config file
1057 pubkey_prepare(Authctxt *authctxt)
1060 Idlist agent, files, *preferred;
1062 AuthenticationConnection *ac;
1066 TAILQ_INIT(&agent); /* keys from the agent */
1067 TAILQ_INIT(&files); /* keys from the config file */
1068 preferred = &authctxt->keys;
1069 TAILQ_INIT(preferred); /* preferred order of keys */
1071 /* list of keys stored in the filesystem */
1072 for (i = 0; i < options.num_identity_files; i++) {
1073 key = options.identity_keys[i];
1074 if (key && key->type == KEY_RSA1)
1076 options.identity_keys[i] = NULL;
1077 id = xcalloc(1, sizeof(*id));
1079 id->filename = xstrdup(options.identity_files[i]);
1080 TAILQ_INSERT_TAIL(&files, id, next);
1082 /* list of keys supported by the agent */
1083 if ((ac = ssh_get_authentication_connection())) {
1084 for (key = ssh_get_first_identity(ac, &comment, 2);
1086 key = ssh_get_next_identity(ac, &comment, 2)) {
1088 TAILQ_FOREACH(id, &files, next) {
1089 /* agent keys from the config file are preferred */
1090 if (key_equal(key, id->key)) {
1093 TAILQ_REMOVE(&files, id, next);
1094 TAILQ_INSERT_TAIL(preferred, id, next);
1100 if (!found && !options.identities_only) {
1101 id = xcalloc(1, sizeof(*id));
1103 id->filename = comment;
1105 TAILQ_INSERT_TAIL(&agent, id, next);
1108 /* append remaining agent keys */
1109 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1110 TAILQ_REMOVE(&agent, id, next);
1111 TAILQ_INSERT_TAIL(preferred, id, next);
1113 authctxt->agent = ac;
1115 /* append remaining keys from the config file */
1116 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1117 TAILQ_REMOVE(&files, id, next);
1118 TAILQ_INSERT_TAIL(preferred, id, next);
1120 TAILQ_FOREACH(id, preferred, next) {
1121 debug2("key: %s (%p)", id->filename, id->key);
1126 pubkey_cleanup(Authctxt *authctxt)
1130 if (authctxt->agent != NULL)
1131 ssh_close_authentication_connection(authctxt->agent);
1132 for (id = TAILQ_FIRST(&authctxt->keys); id;
1133 id = TAILQ_FIRST(&authctxt->keys)) {
1134 TAILQ_REMOVE(&authctxt->keys, id, next);
1138 xfree(id->filename);
1144 userauth_pubkey(Authctxt *authctxt)
1149 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1152 /* move key to the end of the queue */
1153 TAILQ_REMOVE(&authctxt->keys, id, next);
1154 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1156 * send a test message if we have the public key. for
1157 * encrypted keys we cannot do this and have to load the
1158 * private key instead
1160 if (id->key && id->key->type != KEY_RSA1) {
1161 debug("Offering public key: %s", id->filename);
1162 sent = send_pubkey_test(authctxt, id);
1163 } else if (id->key == NULL) {
1164 debug("Trying private key: %s", id->filename);
1165 id->key = load_identity_file(id->filename);
1166 if (id->key != NULL) {
1168 sent = sign_and_send_pubkey(authctxt, id);
1180 * Send userauth request message specifying keyboard-interactive method.
1183 userauth_kbdint(Authctxt *authctxt)
1185 static int attempt = 0;
1187 if (attempt++ >= options.number_of_password_prompts)
1189 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1190 if (attempt > 1 && !authctxt->info_req_seen) {
1191 debug3("userauth_kbdint: disable: no info_req_seen");
1192 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1196 debug2("userauth_kbdint");
1197 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1198 packet_put_cstring(authctxt->server_user);
1199 packet_put_cstring(authctxt->service);
1200 packet_put_cstring(authctxt->method->name);
1201 packet_put_cstring(""); /* lang */
1202 packet_put_cstring(options.kbd_interactive_devices ?
1203 options.kbd_interactive_devices : "");
1206 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1211 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1214 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1216 Authctxt *authctxt = ctxt;
1217 char *name, *inst, *lang, *prompt, *response;
1218 u_int num_prompts, i;
1221 debug2("input_userauth_info_req");
1223 if (authctxt == NULL)
1224 fatal("input_userauth_info_req: no authentication context");
1226 authctxt->info_req_seen = 1;
1228 name = packet_get_string(NULL);
1229 inst = packet_get_string(NULL);
1230 lang = packet_get_string(NULL);
1231 if (strlen(name) > 0)
1233 if (strlen(inst) > 0)
1239 num_prompts = packet_get_int();
1241 * Begin to build info response packet based on prompts requested.
1242 * We commit to providing the correct number of responses, so if
1243 * further on we run into a problem that prevents this, we have to
1244 * be sure and clean this up and send a correct error response.
1246 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1247 packet_put_int(num_prompts);
1249 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1250 for (i = 0; i < num_prompts; i++) {
1251 prompt = packet_get_string(NULL);
1252 echo = packet_get_char();
1254 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1256 packet_put_cstring(response);
1257 memset(response, 0, strlen(response));
1261 packet_check_eom(); /* done with parsing incoming message. */
1263 packet_add_padding(64);
1268 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1269 u_char *data, u_int datalen)
1274 int to[2], from[2], status, version = 2;
1276 debug2("ssh_keysign called");
1278 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1279 error("ssh_keysign: no installed: %s", strerror(errno));
1282 if (fflush(stdout) != 0)
1283 error("ssh_keysign: fflush: %s", strerror(errno));
1285 error("ssh_keysign: pipe: %s", strerror(errno));
1288 if (pipe(from) < 0) {
1289 error("ssh_keysign: pipe: %s", strerror(errno));
1292 if ((pid = fork()) < 0) {
1293 error("ssh_keysign: fork: %s", strerror(errno));
1297 permanently_drop_suid(getuid());
1299 if (dup2(from[1], STDOUT_FILENO) < 0)
1300 fatal("ssh_keysign: dup2: %s", strerror(errno));
1302 if (dup2(to[0], STDIN_FILENO) < 0)
1303 fatal("ssh_keysign: dup2: %s", strerror(errno));
1306 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1307 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1314 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1315 buffer_put_string(&b, data, datalen);
1316 if (ssh_msg_send(to[1], version, &b) == -1)
1317 fatal("ssh_keysign: couldn't send request");
1319 if (ssh_msg_recv(from[0], &b) < 0) {
1320 error("ssh_keysign: no reply");
1327 while (waitpid(pid, &status, 0) < 0)
1331 if (buffer_get_char(&b) != version) {
1332 error("ssh_keysign: bad version");
1336 *sigp = buffer_get_string(&b, lenp);
1343 userauth_hostbased(Authctxt *authctxt)
1345 Key *private = NULL;
1346 Sensitive *sensitive = authctxt->sensitive;
1348 u_char *signature, *blob;
1349 char *chost, *pkalg, *p, myname[NI_MAXHOST];
1350 const char *service;
1352 int ok, i, len, found = 0;
1354 /* check for a useful key */
1355 for (i = 0; i < sensitive->nkeys; i++) {
1356 private = sensitive->keys[i];
1357 if (private && private->type != KEY_RSA1) {
1359 /* we take and free the key */
1360 sensitive->keys[i] = NULL;
1365 debug("No more client hostkeys for hostbased authentication.");
1368 if (key_to_blob(private, &blob, &blen) == 0) {
1372 /* figure out a name for the client host */
1374 if (packet_connection_is_on_socket())
1375 p = get_local_name(packet_get_connection_in());
1377 if (gethostname(myname, sizeof(myname)) == -1) {
1378 verbose("userauth_hostbased: gethostname: %s",
1381 p = xstrdup(myname);
1384 error("userauth_hostbased: cannot get local ipaddr/name");
1389 len = strlen(p) + 2;
1390 xasprintf(&chost, "%s.", p);
1391 debug2("userauth_hostbased: chost %s", chost);
1394 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1396 pkalg = xstrdup(key_ssh_name(private));
1398 /* construct data */
1399 buffer_put_string(&b, session_id2, session_id2_len);
1400 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1401 buffer_put_cstring(&b, authctxt->server_user);
1402 buffer_put_cstring(&b, service);
1403 buffer_put_cstring(&b, authctxt->method->name);
1404 buffer_put_cstring(&b, pkalg);
1405 buffer_put_string(&b, blob, blen);
1406 buffer_put_cstring(&b, chost);
1407 buffer_put_cstring(&b, authctxt->local_user);
1411 if (sensitive->external_keysign)
1412 ok = ssh_keysign(private, &signature, &slen,
1413 buffer_ptr(&b), buffer_len(&b));
1415 ok = key_sign(private, &signature, &slen,
1416 buffer_ptr(&b), buffer_len(&b));
1420 error("key_sign failed");
1426 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1427 packet_put_cstring(authctxt->server_user);
1428 packet_put_cstring(authctxt->service);
1429 packet_put_cstring(authctxt->method->name);
1430 packet_put_cstring(pkalg);
1431 packet_put_string(blob, blen);
1432 packet_put_cstring(chost);
1433 packet_put_cstring(authctxt->local_user);
1434 packet_put_string(signature, slen);
1435 memset(signature, 's', slen);
1445 /* find auth method */
1448 * given auth method name, if configurable options permit this method fill
1449 * in auth_ident field and return true, otherwise return false.
1452 authmethod_is_enabled(Authmethod *method)
1456 /* return false if options indicate this method is disabled */
1457 if (method->enabled == NULL || *method->enabled == 0)
1459 /* return false if batch mode is enabled but method needs interactive mode */
1460 if (method->batch_flag != NULL && *method->batch_flag != 0)
1466 authmethod_lookup(const char *name)
1468 Authmethod *method = NULL;
1470 for (method = authmethods; method->name != NULL; method++)
1471 if (strcmp(name, method->name) == 0)
1473 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1477 /* XXX internal state */
1478 static Authmethod *current = NULL;
1479 static char *supported = NULL;
1480 static char *preferred = NULL;
1483 * Given the authentication method list sent by the server, return the
1484 * next method we should try. If the server initially sends a nil list,
1485 * use a built-in default list.
1488 authmethod_get(char *authlist)
1493 /* Use a suitable default if we're passed a nil list. */
1494 if (authlist == NULL || strlen(authlist) == 0)
1495 authlist = options.preferred_authentications;
1497 if (supported == NULL || strcmp(authlist, supported) != 0) {
1498 debug3("start over, passed a different list %s", authlist);
1499 if (supported != NULL)
1501 supported = xstrdup(authlist);
1502 preferred = options.preferred_authentications;
1503 debug3("preferred %s", preferred);
1505 } else if (current != NULL && authmethod_is_enabled(current))
1509 if ((name = match_list(preferred, supported, &next)) == NULL) {
1510 debug("No more authentication methods to try.");
1515 debug3("authmethod_lookup %s", name);
1516 debug3("remaining preferred: %s", preferred);
1517 if ((current = authmethod_lookup(name)) != NULL &&
1518 authmethod_is_enabled(current)) {
1519 debug3("authmethod_is_enabled %s", name);
1520 debug("Next authentication method: %s", name);
1527 authmethods_get(void)
1529 Authmethod *method = NULL;
1534 for (method = authmethods; method->name != NULL; method++) {
1535 if (authmethod_is_enabled(method)) {
1536 if (buffer_len(&b) > 0)
1537 buffer_append(&b, ",", 1);
1538 buffer_append(&b, method->name, strlen(method->name));
1541 buffer_append(&b, "\0", 1);
1542 list = xstrdup(buffer_ptr(&b));