2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 RCSID("$OpenBSD: sshconnect2.c,v 1.143 2005/10/14 02:17:59 stevesk Exp $");
28 #include "openbsd-compat/sys-queue.h"
39 #include "myproposal.h"
40 #include "sshconnect.h"
51 #include "pathnames.h"
58 extern char *client_version_string;
59 extern char *server_version_string;
60 extern Options options;
63 /* tty_flag is set in ssh.c. use this in ssh_userauth2 */
64 /* if it is set then prevent the switch to the null cipher */
72 u_char *session_id2 = NULL;
73 u_int session_id2_len = 0;
76 struct sockaddr *xxx_hostaddr;
81 verify_host_key_callback(Key *hostkey)
83 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
84 fatal("Host key verification failed.");
89 ssh_kex2(char *host, struct sockaddr *hostaddr)
94 xxx_hostaddr = hostaddr;
96 if (options.ciphers == (char *)-1) {
97 logit("No valid ciphers for protocol version 2 given, using defaults.");
98 options.ciphers = NULL;
100 if (options.ciphers != NULL) {
101 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
102 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
104 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
105 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
106 myproposal[PROPOSAL_ENC_ALGS_STOC] =
107 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
108 if (options.compression) {
109 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
110 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
112 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
113 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
115 if (options.macs != NULL) {
116 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
117 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
119 if (options.hostkeyalgorithms != NULL)
120 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
121 options.hostkeyalgorithms;
123 if (options.rekey_limit)
124 packet_set_rekey_limit(options.rekey_limit);
126 /* start key exchange */
127 kex = kex_setup(myproposal);
128 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
129 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
130 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
131 kex->client_version_string=client_version_string;
132 kex->server_version_string=server_version_string;
133 kex->verify_host_key=&verify_host_key_callback;
137 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
139 session_id2 = kex->session_id;
140 session_id2_len = kex->session_id_len;
143 /* send 1st encrypted/maced/compressed message */
144 packet_start(SSH2_MSG_IGNORE);
145 packet_put_cstring("markus");
155 typedef struct Authctxt Authctxt;
156 typedef struct Authmethod Authmethod;
157 typedef struct identity Identity;
158 typedef struct idlist Idlist;
161 TAILQ_ENTRY(identity) next;
162 AuthenticationConnection *ac; /* set if agent supports key */
163 Key *key; /* public/private key */
164 char *filename; /* comment for agent-only keys */
166 int isprivate; /* key points to the private key */
168 TAILQ_HEAD(idlist, identity);
171 const char *server_user;
172 const char *local_user;
180 AuthenticationConnection *agent;
182 Sensitive *sensitive;
183 /* kbd-interactive */
189 char *name; /* string to compare against server's list */
190 int (*userauth)(Authctxt *authctxt);
191 int *enabled; /* flag in option struct that enables method */
192 int *batch_flag; /* flag in option struct that disables method */
195 void input_userauth_success(int, u_int32_t, void *);
196 void input_userauth_failure(int, u_int32_t, void *);
197 void input_userauth_banner(int, u_int32_t, void *);
198 void input_userauth_error(int, u_int32_t, void *);
199 void input_userauth_info_req(int, u_int32_t, void *);
200 void input_userauth_pk_ok(int, u_int32_t, void *);
201 void input_userauth_passwd_changereq(int, u_int32_t, void *);
203 int userauth_none(Authctxt *);
204 int userauth_pubkey(Authctxt *);
205 int userauth_passwd(Authctxt *);
206 int userauth_kbdint(Authctxt *);
207 int userauth_hostbased(Authctxt *);
208 int userauth_kerberos(Authctxt *);
211 int userauth_gssapi(Authctxt *authctxt);
212 void input_gssapi_response(int type, u_int32_t, void *);
213 void input_gssapi_token(int type, u_int32_t, void *);
214 void input_gssapi_hash(int type, u_int32_t, void *);
215 void input_gssapi_error(int, u_int32_t, void *);
216 void input_gssapi_errtok(int, u_int32_t, void *);
219 void userauth(Authctxt *, char *);
221 static int sign_and_send_pubkey(Authctxt *, Identity *);
222 static void pubkey_prepare(Authctxt *);
223 static void pubkey_cleanup(Authctxt *);
224 static Key *load_identity_file(char *);
226 static Authmethod *authmethod_get(char *authlist);
227 static Authmethod *authmethod_lookup(const char *name);
228 static char *authmethods_get(void);
230 Authmethod authmethods[] = {
234 &options.gss_authentication,
239 &options.hostbased_authentication,
243 &options.pubkey_authentication,
245 {"keyboard-interactive",
247 &options.kbd_interactive_authentication,
248 &options.batch_mode},
251 &options.password_authentication,
252 &options.batch_mode},
257 {NULL, NULL, NULL, NULL}
261 ssh_userauth2(const char *local_user, const char *server_user, char *host,
262 Sensitive *sensitive)
267 if (options.challenge_response_authentication)
268 options.kbd_interactive_authentication = 1;
270 packet_start(SSH2_MSG_SERVICE_REQUEST);
271 packet_put_cstring("ssh-userauth");
273 debug("SSH2_MSG_SERVICE_REQUEST sent");
275 type = packet_read();
276 if (type != SSH2_MSG_SERVICE_ACCEPT)
277 fatal("Server denied authentication request: %d", type);
278 if (packet_remaining() > 0) {
279 char *reply = packet_get_string(NULL);
280 debug2("service_accept: %s", reply);
283 debug2("buggy server: service_accept w/o service");
286 debug("SSH2_MSG_SERVICE_ACCEPT received");
288 if (options.preferred_authentications == NULL)
289 options.preferred_authentications = authmethods_get();
291 /* setup authentication context */
292 memset(&authctxt, 0, sizeof(authctxt));
293 pubkey_prepare(&authctxt);
294 authctxt.server_user = server_user;
295 authctxt.local_user = local_user;
296 authctxt.host = host;
297 authctxt.service = "ssh-connection"; /* service name */
298 authctxt.success = 0;
299 authctxt.method = authmethod_lookup("none");
300 authctxt.authlist = NULL;
301 authctxt.methoddata = NULL;
302 authctxt.sensitive = sensitive;
303 authctxt.info_req_seen = 0;
304 if (authctxt.method == NULL)
305 fatal("ssh_userauth2: internal error: cannot send userauth none request");
307 /* initial userauth request */
308 userauth_none(&authctxt);
310 dispatch_init(&input_userauth_error);
311 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
312 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
313 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
314 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
316 pubkey_cleanup(&authctxt);
317 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
318 if ((options.none_switch == 1) && !tty_flag) /* no null on tty sessions */
320 debug("Requesting none rekeying...");
321 myproposal[PROPOSAL_ENC_ALGS_STOC] = "none";
322 myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none";
323 kex_prop2buf(&xxx_kex->my,myproposal);
324 packet_request_rekeying();
325 fprintf(stderr, "WARNING: ENABLED NULL CIPHER\n");
327 debug("Authentication succeeded (%s).", authctxt.method->name);
331 userauth(Authctxt *authctxt, char *authlist)
333 if (authctxt->methoddata) {
334 xfree(authctxt->methoddata);
335 authctxt->methoddata = NULL;
337 if (authlist == NULL) {
338 authlist = authctxt->authlist;
340 if (authctxt->authlist)
341 xfree(authctxt->authlist);
342 authctxt->authlist = authlist;
345 Authmethod *method = authmethod_get(authlist);
347 fatal("Permission denied (%s).", authlist);
348 authctxt->method = method;
350 /* reset the per method handler */
351 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
352 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
354 /* and try new method */
355 if (method->userauth(authctxt) != 0) {
356 debug2("we sent a %s packet, wait for reply", method->name);
359 debug2("we did not send a packet, disable method");
360 method->enabled = NULL;
366 input_userauth_error(int type, u_int32_t seq, void *ctxt)
368 fatal("input_userauth_error: bad message during authentication: "
373 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
377 debug3("input_userauth_banner");
378 msg = packet_get_string(NULL);
379 lang = packet_get_string(NULL);
380 if (options.log_level > SYSLOG_LEVEL_QUIET)
381 fprintf(stderr, "%s", msg);
387 input_userauth_success(int type, u_int32_t seq, void *ctxt)
389 Authctxt *authctxt = ctxt;
390 if (authctxt == NULL)
391 fatal("input_userauth_success: no authentication context");
392 if (authctxt->authlist) {
393 xfree(authctxt->authlist);
394 authctxt->authlist = NULL;
396 if (authctxt->methoddata) {
397 xfree(authctxt->methoddata);
398 authctxt->methoddata = NULL;
400 authctxt->success = 1; /* break out */
404 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
406 Authctxt *authctxt = ctxt;
407 char *authlist = NULL;
410 if (authctxt == NULL)
411 fatal("input_userauth_failure: no authentication context");
413 authlist = packet_get_string(NULL);
414 partial = packet_get_char();
418 logit("Authenticated with partial success.");
419 debug("Authentications that can continue: %s", authlist);
421 userauth(authctxt, authlist);
424 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
426 Authctxt *authctxt = ctxt;
430 int pktype, sent = 0;
435 if (authctxt == NULL)
436 fatal("input_userauth_pk_ok: no authentication context");
437 if (datafellows & SSH_BUG_PKOK) {
438 /* this is similar to SSH_BUG_PKAUTH */
439 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
440 pkblob = packet_get_string(&blen);
442 buffer_append(&b, pkblob, blen);
443 pkalg = buffer_get_string(&b, &alen);
446 pkalg = packet_get_string(&alen);
447 pkblob = packet_get_string(&blen);
451 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
453 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
454 debug("unknown pkalg %s", pkalg);
457 if ((key = key_from_blob(pkblob, blen)) == NULL) {
458 debug("no key from blob. pkalg %s", pkalg);
461 if (key->type != pktype) {
462 error("input_userauth_pk_ok: type mismatch "
463 "for decoded key (received %d, expected %d)",
467 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
468 debug2("input_userauth_pk_ok: fp %s", fp);
472 * search keys in the reverse order, because last candidate has been
473 * moved to the end of the queue. this also avoids confusion by
476 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
477 if (key_equal(key, id->key)) {
478 sent = sign_and_send_pubkey(authctxt, id);
488 /* try another method if we did not send a packet */
490 userauth(authctxt, NULL);
495 userauth_gssapi(Authctxt *authctxt)
497 Gssctxt *gssctxt = NULL;
498 static gss_OID_set gss_supported = NULL;
499 static u_int mech = 0;
503 /* Try one GSSAPI method at a time, rather than sending them all at
506 if (gss_supported == NULL)
507 gss_indicate_mechs(&min, &gss_supported);
509 /* Check to see if the mechanism is usable before we offer it */
510 while (mech < gss_supported->count && !ok) {
512 ssh_gssapi_delete_ctx(&gssctxt);
513 ssh_gssapi_build_ctx(&gssctxt);
514 ssh_gssapi_set_oid(gssctxt, &gss_supported->elements[mech]);
516 /* My DER encoding requires length<128 */
517 if (gss_supported->elements[mech].length < 128 &&
518 !GSS_ERROR(ssh_gssapi_import_name(gssctxt,
520 ok = 1; /* Mechanism works */
529 authctxt->methoddata=(void *)gssctxt;
531 packet_start(SSH2_MSG_USERAUTH_REQUEST);
532 packet_put_cstring(authctxt->server_user);
533 packet_put_cstring(authctxt->service);
534 packet_put_cstring(authctxt->method->name);
538 packet_put_int((gss_supported->elements[mech].length) + 2);
539 packet_put_char(SSH_GSS_OIDTYPE);
540 packet_put_char(gss_supported->elements[mech].length);
541 packet_put_raw(gss_supported->elements[mech].elements,
542 gss_supported->elements[mech].length);
546 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
547 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
548 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
549 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
551 mech++; /* Move along to next candidate */
557 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
559 Authctxt *authctxt = ctxt;
560 Gssctxt *gssctxt = authctxt->methoddata;
561 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
562 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
563 gss_buffer_desc gssbuf;
564 OM_uint32 status, ms, flags;
567 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
568 recv_tok, &send_tok, &flags);
570 if (send_tok.length > 0) {
571 if (GSS_ERROR(status))
572 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
574 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
576 packet_put_string(send_tok.value, send_tok.length);
578 gss_release_buffer(&ms, &send_tok);
581 if (status == GSS_S_COMPLETE) {
582 /* send either complete or MIC, depending on mechanism */
583 if (!(flags & GSS_C_INTEG_FLAG)) {
584 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
587 ssh_gssapi_buildmic(&b, authctxt->server_user,
588 authctxt->service, "gssapi-with-mic");
590 gssbuf.value = buffer_ptr(&b);
591 gssbuf.length = buffer_len(&b);
593 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
595 if (!GSS_ERROR(status)) {
596 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
597 packet_put_string(mic.value, mic.length);
603 gss_release_buffer(&ms, &mic);
611 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
613 Authctxt *authctxt = ctxt;
618 if (authctxt == NULL)
619 fatal("input_gssapi_response: no authentication context");
620 gssctxt = authctxt->methoddata;
623 oidv = packet_get_string(&oidlen);
626 oidv[0] != SSH_GSS_OIDTYPE ||
627 oidv[1] != oidlen - 2) {
629 debug("Badly encoded mechanism OID received");
630 userauth(authctxt, NULL);
634 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
635 fatal("Server returned different OID than expected");
641 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
642 /* Start again with next method on list */
643 debug("Trying to start again");
644 userauth(authctxt, NULL);
650 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
652 Authctxt *authctxt = ctxt;
653 gss_buffer_desc recv_tok;
657 if (authctxt == NULL)
658 fatal("input_gssapi_response: no authentication context");
660 recv_tok.value = packet_get_string(&slen);
661 recv_tok.length = slen; /* safe typecast */
665 status = process_gssapi_token(ctxt, &recv_tok);
667 xfree(recv_tok.value);
669 if (GSS_ERROR(status)) {
670 /* Start again with the next method in the list */
671 userauth(authctxt, NULL);
677 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
679 Authctxt *authctxt = ctxt;
681 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
682 gss_buffer_desc recv_tok;
683 OM_uint32 status, ms;
686 if (authctxt == NULL)
687 fatal("input_gssapi_response: no authentication context");
688 gssctxt = authctxt->methoddata;
690 recv_tok.value = packet_get_string(&len);
691 recv_tok.length = len;
695 /* Stick it into GSSAPI and see what it says */
696 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
697 &recv_tok, &send_tok, NULL);
699 xfree(recv_tok.value);
700 gss_release_buffer(&ms, &send_tok);
702 /* Server will be returning a failed packet after this one */
706 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
712 maj=packet_get_int();
713 min=packet_get_int();
714 msg=packet_get_string(NULL);
715 lang=packet_get_string(NULL);
719 debug("Server GSSAPI Error:\n%s", msg);
726 userauth_none(Authctxt *authctxt)
728 /* initial userauth request */
729 packet_start(SSH2_MSG_USERAUTH_REQUEST);
730 packet_put_cstring(authctxt->server_user);
731 packet_put_cstring(authctxt->service);
732 packet_put_cstring(authctxt->method->name);
738 userauth_passwd(Authctxt *authctxt)
740 static int attempt = 0;
744 if (attempt++ >= options.number_of_password_prompts)
748 error("Permission denied, please try again.");
750 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
751 authctxt->server_user, authctxt->host);
752 password = read_passphrase(prompt, 0);
753 packet_start(SSH2_MSG_USERAUTH_REQUEST);
754 packet_put_cstring(authctxt->server_user);
755 packet_put_cstring(authctxt->service);
756 packet_put_cstring(authctxt->method->name);
758 packet_put_cstring(password);
759 memset(password, 0, strlen(password));
761 packet_add_padding(64);
764 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
765 &input_userauth_passwd_changereq);
770 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
773 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
775 Authctxt *authctxt = ctxt;
776 char *info, *lang, *password = NULL, *retype = NULL;
779 debug2("input_userauth_passwd_changereq");
781 if (authctxt == NULL)
782 fatal("input_userauth_passwd_changereq: "
783 "no authentication context");
785 info = packet_get_string(NULL);
786 lang = packet_get_string(NULL);
787 if (strlen(info) > 0)
791 packet_start(SSH2_MSG_USERAUTH_REQUEST);
792 packet_put_cstring(authctxt->server_user);
793 packet_put_cstring(authctxt->service);
794 packet_put_cstring(authctxt->method->name);
795 packet_put_char(1); /* additional info */
796 snprintf(prompt, sizeof(prompt),
797 "Enter %.30s@%.128s's old password: ",
798 authctxt->server_user, authctxt->host);
799 password = read_passphrase(prompt, 0);
800 packet_put_cstring(password);
801 memset(password, 0, strlen(password));
804 while (password == NULL) {
805 snprintf(prompt, sizeof(prompt),
806 "Enter %.30s@%.128s's new password: ",
807 authctxt->server_user, authctxt->host);
808 password = read_passphrase(prompt, RP_ALLOW_EOF);
809 if (password == NULL) {
813 snprintf(prompt, sizeof(prompt),
814 "Retype %.30s@%.128s's new password: ",
815 authctxt->server_user, authctxt->host);
816 retype = read_passphrase(prompt, 0);
817 if (strcmp(password, retype) != 0) {
818 memset(password, 0, strlen(password));
820 logit("Mismatch; try again, EOF to quit.");
823 memset(retype, 0, strlen(retype));
826 packet_put_cstring(password);
827 memset(password, 0, strlen(password));
829 packet_add_padding(64);
832 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
833 &input_userauth_passwd_changereq);
837 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
838 u_char *data, u_int datalen)
843 /* the agent supports this key */
845 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
848 * we have already loaded the private key or
849 * the private key is stored in external hardware
851 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
852 return (key_sign(id->key, sigp, lenp, data, datalen));
853 /* load the private key from the file */
854 if ((prv = load_identity_file(id->filename)) == NULL)
856 ret = key_sign(prv, sigp, lenp, data, datalen);
862 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
865 u_char *blob, *signature;
871 debug3("sign_and_send_pubkey");
873 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
874 /* we cannot handle this key */
875 debug3("sign_and_send_pubkey: cannot handle key");
878 /* data to be signed */
880 if (datafellows & SSH_OLD_SESSIONID) {
881 buffer_append(&b, session_id2, session_id2_len);
882 skip = session_id2_len;
884 buffer_put_string(&b, session_id2, session_id2_len);
885 skip = buffer_len(&b);
887 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
888 buffer_put_cstring(&b, authctxt->server_user);
889 buffer_put_cstring(&b,
890 datafellows & SSH_BUG_PKSERVICE ?
893 if (datafellows & SSH_BUG_PKAUTH) {
894 buffer_put_char(&b, have_sig);
896 buffer_put_cstring(&b, authctxt->method->name);
897 buffer_put_char(&b, have_sig);
898 buffer_put_cstring(&b, key_ssh_name(id->key));
900 buffer_put_string(&b, blob, bloblen);
902 /* generate signature */
903 ret = identity_sign(id, &signature, &slen,
904 buffer_ptr(&b), buffer_len(&b));
913 if (datafellows & SSH_BUG_PKSERVICE) {
915 buffer_append(&b, session_id2, session_id2_len);
916 skip = session_id2_len;
917 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
918 buffer_put_cstring(&b, authctxt->server_user);
919 buffer_put_cstring(&b, authctxt->service);
920 buffer_put_cstring(&b, authctxt->method->name);
921 buffer_put_char(&b, have_sig);
922 if (!(datafellows & SSH_BUG_PKAUTH))
923 buffer_put_cstring(&b, key_ssh_name(id->key));
924 buffer_put_string(&b, blob, bloblen);
928 /* append signature */
929 buffer_put_string(&b, signature, slen);
932 /* skip session id and packet type */
933 if (buffer_len(&b) < skip + 1)
934 fatal("userauth_pubkey: internal error");
935 buffer_consume(&b, skip + 1);
937 /* put remaining data from buffer into packet */
938 packet_start(SSH2_MSG_USERAUTH_REQUEST);
939 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
947 send_pubkey_test(Authctxt *authctxt, Identity *id)
950 u_int bloblen, have_sig = 0;
952 debug3("send_pubkey_test");
954 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
955 /* we cannot handle this key */
956 debug3("send_pubkey_test: cannot handle key");
959 /* register callback for USERAUTH_PK_OK message */
960 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
962 packet_start(SSH2_MSG_USERAUTH_REQUEST);
963 packet_put_cstring(authctxt->server_user);
964 packet_put_cstring(authctxt->service);
965 packet_put_cstring(authctxt->method->name);
966 packet_put_char(have_sig);
967 if (!(datafellows & SSH_BUG_PKAUTH))
968 packet_put_cstring(key_ssh_name(id->key));
969 packet_put_string(blob, bloblen);
976 load_identity_file(char *filename)
979 char prompt[300], *passphrase;
983 if (stat(filename, &st) < 0) {
984 debug3("no such identity: %s", filename);
987 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
988 if (private == NULL) {
989 if (options.batch_mode)
991 snprintf(prompt, sizeof prompt,
992 "Enter passphrase for key '%.100s': ", filename);
993 for (i = 0; i < options.number_of_password_prompts; i++) {
994 passphrase = read_passphrase(prompt, 0);
995 if (strcmp(passphrase, "") != 0) {
996 private = key_load_private_type(KEY_UNSPEC, filename,
1000 debug2("no passphrase given, try next key");
1003 memset(passphrase, 0, strlen(passphrase));
1005 if (private != NULL || quit)
1007 debug2("bad passphrase given, try again...");
1014 * try keys in the following order:
1015 * 1. agent keys that are found in the config file
1016 * 2. other agent keys
1017 * 3. keys that are only listed in the config file
1020 pubkey_prepare(Authctxt *authctxt)
1023 Idlist agent, files, *preferred;
1025 AuthenticationConnection *ac;
1029 TAILQ_INIT(&agent); /* keys from the agent */
1030 TAILQ_INIT(&files); /* keys from the config file */
1031 preferred = &authctxt->keys;
1032 TAILQ_INIT(preferred); /* preferred order of keys */
1034 /* list of keys stored in the filesystem */
1035 for (i = 0; i < options.num_identity_files; i++) {
1036 key = options.identity_keys[i];
1037 if (key && key->type == KEY_RSA1)
1039 options.identity_keys[i] = NULL;
1040 id = xmalloc(sizeof(*id));
1041 memset(id, 0, sizeof(*id));
1043 id->filename = xstrdup(options.identity_files[i]);
1044 TAILQ_INSERT_TAIL(&files, id, next);
1046 /* list of keys supported by the agent */
1047 if ((ac = ssh_get_authentication_connection())) {
1048 for (key = ssh_get_first_identity(ac, &comment, 2);
1050 key = ssh_get_next_identity(ac, &comment, 2)) {
1052 TAILQ_FOREACH(id, &files, next) {
1053 /* agent keys from the config file are preferred */
1054 if (key_equal(key, id->key)) {
1057 TAILQ_REMOVE(&files, id, next);
1058 TAILQ_INSERT_TAIL(preferred, id, next);
1064 if (!found && !options.identities_only) {
1065 id = xmalloc(sizeof(*id));
1066 memset(id, 0, sizeof(*id));
1068 id->filename = comment;
1070 TAILQ_INSERT_TAIL(&agent, id, next);
1073 /* append remaining agent keys */
1074 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1075 TAILQ_REMOVE(&agent, id, next);
1076 TAILQ_INSERT_TAIL(preferred, id, next);
1078 authctxt->agent = ac;
1080 /* append remaining keys from the config file */
1081 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1082 TAILQ_REMOVE(&files, id, next);
1083 TAILQ_INSERT_TAIL(preferred, id, next);
1085 TAILQ_FOREACH(id, preferred, next) {
1086 debug2("key: %s (%p)", id->filename, id->key);
1091 pubkey_cleanup(Authctxt *authctxt)
1095 if (authctxt->agent != NULL)
1096 ssh_close_authentication_connection(authctxt->agent);
1097 for (id = TAILQ_FIRST(&authctxt->keys); id;
1098 id = TAILQ_FIRST(&authctxt->keys)) {
1099 TAILQ_REMOVE(&authctxt->keys, id, next);
1103 xfree(id->filename);
1109 userauth_pubkey(Authctxt *authctxt)
1114 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1117 /* move key to the end of the queue */
1118 TAILQ_REMOVE(&authctxt->keys, id, next);
1119 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1121 * send a test message if we have the public key. for
1122 * encrypted keys we cannot do this and have to load the
1123 * private key instead
1125 if (id->key && id->key->type != KEY_RSA1) {
1126 debug("Offering public key: %s", id->filename);
1127 sent = send_pubkey_test(authctxt, id);
1128 } else if (id->key == NULL) {
1129 debug("Trying private key: %s", id->filename);
1130 id->key = load_identity_file(id->filename);
1131 if (id->key != NULL) {
1133 sent = sign_and_send_pubkey(authctxt, id);
1145 * Send userauth request message specifying keyboard-interactive method.
1148 userauth_kbdint(Authctxt *authctxt)
1150 static int attempt = 0;
1152 if (attempt++ >= options.number_of_password_prompts)
1154 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1155 if (attempt > 1 && !authctxt->info_req_seen) {
1156 debug3("userauth_kbdint: disable: no info_req_seen");
1157 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1161 debug2("userauth_kbdint");
1162 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1163 packet_put_cstring(authctxt->server_user);
1164 packet_put_cstring(authctxt->service);
1165 packet_put_cstring(authctxt->method->name);
1166 packet_put_cstring(""); /* lang */
1167 packet_put_cstring(options.kbd_interactive_devices ?
1168 options.kbd_interactive_devices : "");
1171 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1176 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1179 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1181 Authctxt *authctxt = ctxt;
1182 char *name, *inst, *lang, *prompt, *response;
1183 u_int num_prompts, i;
1186 debug2("input_userauth_info_req");
1188 if (authctxt == NULL)
1189 fatal("input_userauth_info_req: no authentication context");
1191 authctxt->info_req_seen = 1;
1193 name = packet_get_string(NULL);
1194 inst = packet_get_string(NULL);
1195 lang = packet_get_string(NULL);
1196 if (strlen(name) > 0)
1198 if (strlen(inst) > 0)
1204 num_prompts = packet_get_int();
1206 * Begin to build info response packet based on prompts requested.
1207 * We commit to providing the correct number of responses, so if
1208 * further on we run into a problem that prevents this, we have to
1209 * be sure and clean this up and send a correct error response.
1211 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1212 packet_put_int(num_prompts);
1214 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1215 for (i = 0; i < num_prompts; i++) {
1216 prompt = packet_get_string(NULL);
1217 echo = packet_get_char();
1219 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1221 packet_put_cstring(response);
1222 memset(response, 0, strlen(response));
1226 packet_check_eom(); /* done with parsing incoming message. */
1228 packet_add_padding(64);
1233 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1234 u_char *data, u_int datalen)
1239 int to[2], from[2], status, version = 2;
1241 debug2("ssh_keysign called");
1243 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1244 error("ssh_keysign: no installed: %s", strerror(errno));
1247 if (fflush(stdout) != 0)
1248 error("ssh_keysign: fflush: %s", strerror(errno));
1250 error("ssh_keysign: pipe: %s", strerror(errno));
1253 if (pipe(from) < 0) {
1254 error("ssh_keysign: pipe: %s", strerror(errno));
1257 if ((pid = fork()) < 0) {
1258 error("ssh_keysign: fork: %s", strerror(errno));
1265 if (dup2(from[1], STDOUT_FILENO) < 0)
1266 fatal("ssh_keysign: dup2: %s", strerror(errno));
1268 if (dup2(to[0], STDIN_FILENO) < 0)
1269 fatal("ssh_keysign: dup2: %s", strerror(errno));
1272 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1273 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1280 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1281 buffer_put_string(&b, data, datalen);
1282 if (ssh_msg_send(to[1], version, &b) == -1)
1283 fatal("ssh_keysign: couldn't send request");
1285 if (ssh_msg_recv(from[0], &b) < 0) {
1286 error("ssh_keysign: no reply");
1293 while (waitpid(pid, &status, 0) < 0)
1297 if (buffer_get_char(&b) != version) {
1298 error("ssh_keysign: bad version");
1302 *sigp = buffer_get_string(&b, lenp);
1309 userauth_hostbased(Authctxt *authctxt)
1311 Key *private = NULL;
1312 Sensitive *sensitive = authctxt->sensitive;
1314 u_char *signature, *blob;
1315 char *chost, *pkalg, *p;
1316 const char *service;
1318 int ok, i, len, found = 0;
1320 /* check for a useful key */
1321 for (i = 0; i < sensitive->nkeys; i++) {
1322 private = sensitive->keys[i];
1323 if (private && private->type != KEY_RSA1) {
1325 /* we take and free the key */
1326 sensitive->keys[i] = NULL;
1331 debug("No more client hostkeys for hostbased authentication.");
1334 if (key_to_blob(private, &blob, &blen) == 0) {
1338 /* figure out a name for the client host */
1339 p = get_local_name(packet_get_connection_in());
1341 error("userauth_hostbased: cannot get local ipaddr/name");
1345 len = strlen(p) + 2;
1346 chost = xmalloc(len);
1347 strlcpy(chost, p, len);
1348 strlcat(chost, ".", len);
1349 debug2("userauth_hostbased: chost %s", chost);
1352 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1354 pkalg = xstrdup(key_ssh_name(private));
1356 /* construct data */
1357 buffer_put_string(&b, session_id2, session_id2_len);
1358 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1359 buffer_put_cstring(&b, authctxt->server_user);
1360 buffer_put_cstring(&b, service);
1361 buffer_put_cstring(&b, authctxt->method->name);
1362 buffer_put_cstring(&b, pkalg);
1363 buffer_put_string(&b, blob, blen);
1364 buffer_put_cstring(&b, chost);
1365 buffer_put_cstring(&b, authctxt->local_user);
1369 if (sensitive->external_keysign)
1370 ok = ssh_keysign(private, &signature, &slen,
1371 buffer_ptr(&b), buffer_len(&b));
1373 ok = key_sign(private, &signature, &slen,
1374 buffer_ptr(&b), buffer_len(&b));
1378 error("key_sign failed");
1383 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1384 packet_put_cstring(authctxt->server_user);
1385 packet_put_cstring(authctxt->service);
1386 packet_put_cstring(authctxt->method->name);
1387 packet_put_cstring(pkalg);
1388 packet_put_string(blob, blen);
1389 packet_put_cstring(chost);
1390 packet_put_cstring(authctxt->local_user);
1391 packet_put_string(signature, slen);
1392 memset(signature, 's', slen);
1401 /* find auth method */
1404 * given auth method name, if configurable options permit this method fill
1405 * in auth_ident field and return true, otherwise return false.
1408 authmethod_is_enabled(Authmethod *method)
1412 /* return false if options indicate this method is disabled */
1413 if (method->enabled == NULL || *method->enabled == 0)
1415 /* return false if batch mode is enabled but method needs interactive mode */
1416 if (method->batch_flag != NULL && *method->batch_flag != 0)
1422 authmethod_lookup(const char *name)
1424 Authmethod *method = NULL;
1426 for (method = authmethods; method->name != NULL; method++)
1427 if (strcmp(name, method->name) == 0)
1429 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1433 /* XXX internal state */
1434 static Authmethod *current = NULL;
1435 static char *supported = NULL;
1436 static char *preferred = NULL;
1439 * Given the authentication method list sent by the server, return the
1440 * next method we should try. If the server initially sends a nil list,
1441 * use a built-in default list.
1444 authmethod_get(char *authlist)
1449 /* Use a suitable default if we're passed a nil list. */
1450 if (authlist == NULL || strlen(authlist) == 0)
1451 authlist = options.preferred_authentications;
1453 if (supported == NULL || strcmp(authlist, supported) != 0) {
1454 debug3("start over, passed a different list %s", authlist);
1455 if (supported != NULL)
1457 supported = xstrdup(authlist);
1458 preferred = options.preferred_authentications;
1459 debug3("preferred %s", preferred);
1461 } else if (current != NULL && authmethod_is_enabled(current))
1465 if ((name = match_list(preferred, supported, &next)) == NULL) {
1466 debug("No more authentication methods to try.");
1471 debug3("authmethod_lookup %s", name);
1472 debug3("remaining preferred: %s", preferred);
1473 if ((current = authmethod_lookup(name)) != NULL &&
1474 authmethod_is_enabled(current)) {
1475 debug3("authmethod_is_enabled %s", name);
1476 debug("Next authentication method: %s", name);
1483 authmethods_get(void)
1485 Authmethod *method = NULL;
1490 for (method = authmethods; method->name != NULL; method++) {
1491 if (authmethod_is_enabled(method)) {
1492 if (buffer_len(&b) > 0)
1493 buffer_append(&b, ",", 1);
1494 buffer_append(&b, method->name, strlen(method->name));
1497 buffer_append(&b, "\0", 1);
1498 list = xstrdup(buffer_ptr(&b));