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;
105 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
106 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
107 myproposal[PROPOSAL_ENC_ALGS_STOC] =
108 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
109 if (options.compression) {
110 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
111 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
113 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
114 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
116 if (options.macs != NULL) {
117 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
118 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
120 if (options.hostkeyalgorithms != NULL)
121 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
122 options.hostkeyalgorithms;
124 if (options.rekey_limit)
125 packet_set_rekey_limit(options.rekey_limit);
127 /* start key exchange */
128 kex = kex_setup(myproposal);
129 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
130 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
131 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
132 kex->client_version_string=client_version_string;
133 kex->server_version_string=server_version_string;
134 kex->verify_host_key=&verify_host_key_callback;
138 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
140 session_id2 = kex->session_id;
141 session_id2_len = kex->session_id_len;
144 /* send 1st encrypted/maced/compressed message */
145 packet_start(SSH2_MSG_IGNORE);
146 packet_put_cstring("markus");
156 typedef struct Authctxt Authctxt;
157 typedef struct Authmethod Authmethod;
158 typedef struct identity Identity;
159 typedef struct idlist Idlist;
162 TAILQ_ENTRY(identity) next;
163 AuthenticationConnection *ac; /* set if agent supports key */
164 Key *key; /* public/private key */
165 char *filename; /* comment for agent-only keys */
167 int isprivate; /* key points to the private key */
169 TAILQ_HEAD(idlist, identity);
172 const char *server_user;
173 const char *local_user;
181 AuthenticationConnection *agent;
183 Sensitive *sensitive;
184 /* kbd-interactive */
190 char *name; /* string to compare against server's list */
191 int (*userauth)(Authctxt *authctxt);
192 int *enabled; /* flag in option struct that enables method */
193 int *batch_flag; /* flag in option struct that disables method */
196 void input_userauth_success(int, u_int32_t, void *);
197 void input_userauth_failure(int, u_int32_t, void *);
198 void input_userauth_banner(int, u_int32_t, void *);
199 void input_userauth_error(int, u_int32_t, void *);
200 void input_userauth_info_req(int, u_int32_t, void *);
201 void input_userauth_pk_ok(int, u_int32_t, void *);
202 void input_userauth_passwd_changereq(int, u_int32_t, void *);
204 int userauth_none(Authctxt *);
205 int userauth_pubkey(Authctxt *);
206 int userauth_passwd(Authctxt *);
207 int userauth_kbdint(Authctxt *);
208 int userauth_hostbased(Authctxt *);
209 int userauth_kerberos(Authctxt *);
212 int userauth_gssapi(Authctxt *authctxt);
213 void input_gssapi_response(int type, u_int32_t, void *);
214 void input_gssapi_token(int type, u_int32_t, void *);
215 void input_gssapi_hash(int type, u_int32_t, void *);
216 void input_gssapi_error(int, u_int32_t, void *);
217 void input_gssapi_errtok(int, u_int32_t, void *);
220 void userauth(Authctxt *, char *);
222 static int sign_and_send_pubkey(Authctxt *, Identity *);
223 static void pubkey_prepare(Authctxt *);
224 static void pubkey_cleanup(Authctxt *);
225 static Key *load_identity_file(char *);
227 static Authmethod *authmethod_get(char *authlist);
228 static Authmethod *authmethod_lookup(const char *name);
229 static char *authmethods_get(void);
231 Authmethod authmethods[] = {
235 &options.gss_authentication,
240 &options.hostbased_authentication,
244 &options.pubkey_authentication,
246 {"keyboard-interactive",
248 &options.kbd_interactive_authentication,
249 &options.batch_mode},
252 &options.password_authentication,
253 &options.batch_mode},
258 {NULL, NULL, NULL, NULL}
262 ssh_userauth2(const char *local_user, const char *server_user, char *host,
263 Sensitive *sensitive)
268 if (options.challenge_response_authentication)
269 options.kbd_interactive_authentication = 1;
271 packet_start(SSH2_MSG_SERVICE_REQUEST);
272 packet_put_cstring("ssh-userauth");
274 debug("SSH2_MSG_SERVICE_REQUEST sent");
276 type = packet_read();
277 if (type != SSH2_MSG_SERVICE_ACCEPT)
278 fatal("Server denied authentication request: %d", type);
279 if (packet_remaining() > 0) {
280 char *reply = packet_get_string(NULL);
281 debug2("service_accept: %s", reply);
284 debug2("buggy server: service_accept w/o service");
287 debug("SSH2_MSG_SERVICE_ACCEPT received");
289 if (options.preferred_authentications == NULL)
290 options.preferred_authentications = authmethods_get();
292 /* setup authentication context */
293 memset(&authctxt, 0, sizeof(authctxt));
294 pubkey_prepare(&authctxt);
295 authctxt.server_user = server_user;
296 authctxt.local_user = local_user;
297 authctxt.host = host;
298 authctxt.service = "ssh-connection"; /* service name */
299 authctxt.success = 0;
300 authctxt.method = authmethod_lookup("none");
301 authctxt.authlist = NULL;
302 authctxt.methoddata = NULL;
303 authctxt.sensitive = sensitive;
304 authctxt.info_req_seen = 0;
305 if (authctxt.method == NULL)
306 fatal("ssh_userauth2: internal error: cannot send userauth none request");
308 /* initial userauth request */
309 userauth_none(&authctxt);
311 dispatch_init(&input_userauth_error);
312 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
313 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
314 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
315 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
317 pubkey_cleanup(&authctxt);
318 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
319 if ((options.none_switch == 1) && (options.none_enabled == 1) && !tty_flag) /* no null on tty sessions */
321 debug("Requesting none rekeying...");
322 myproposal[PROPOSAL_ENC_ALGS_STOC] = "none";
323 myproposal[PROPOSAL_ENC_ALGS_CTOS] = "none";
324 kex_prop2buf(&xxx_kex->my,myproposal);
325 packet_request_rekeying();
326 fprintf(stderr, "WARNING: ENABLED NULL CIPHER\n");
328 debug("Authentication succeeded (%s).", authctxt.method->name);
332 userauth(Authctxt *authctxt, char *authlist)
334 if (authctxt->methoddata) {
335 xfree(authctxt->methoddata);
336 authctxt->methoddata = NULL;
338 if (authlist == NULL) {
339 authlist = authctxt->authlist;
341 if (authctxt->authlist)
342 xfree(authctxt->authlist);
343 authctxt->authlist = authlist;
346 Authmethod *method = authmethod_get(authlist);
348 fatal("Permission denied (%s).", authlist);
349 authctxt->method = method;
351 /* reset the per method handler */
352 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
353 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
355 /* and try new method */
356 if (method->userauth(authctxt) != 0) {
357 debug2("we sent a %s packet, wait for reply", method->name);
360 debug2("we did not send a packet, disable method");
361 method->enabled = NULL;
367 input_userauth_error(int type, u_int32_t seq, void *ctxt)
369 fatal("input_userauth_error: bad message during authentication: "
374 input_userauth_banner(int type, u_int32_t seq, void *ctxt)
378 debug3("input_userauth_banner");
379 msg = packet_get_string(NULL);
380 lang = packet_get_string(NULL);
381 if (options.log_level > SYSLOG_LEVEL_QUIET)
382 fprintf(stderr, "%s", msg);
388 input_userauth_success(int type, u_int32_t seq, void *ctxt)
390 Authctxt *authctxt = ctxt;
391 if (authctxt == NULL)
392 fatal("input_userauth_success: no authentication context");
393 if (authctxt->authlist) {
394 xfree(authctxt->authlist);
395 authctxt->authlist = NULL;
397 if (authctxt->methoddata) {
398 xfree(authctxt->methoddata);
399 authctxt->methoddata = NULL;
401 authctxt->success = 1; /* break out */
405 input_userauth_failure(int type, u_int32_t seq, void *ctxt)
407 Authctxt *authctxt = ctxt;
408 char *authlist = NULL;
411 if (authctxt == NULL)
412 fatal("input_userauth_failure: no authentication context");
414 authlist = packet_get_string(NULL);
415 partial = packet_get_char();
419 logit("Authenticated with partial success.");
420 debug("Authentications that can continue: %s", authlist);
422 userauth(authctxt, authlist);
425 input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
427 Authctxt *authctxt = ctxt;
431 int pktype, sent = 0;
436 if (authctxt == NULL)
437 fatal("input_userauth_pk_ok: no authentication context");
438 if (datafellows & SSH_BUG_PKOK) {
439 /* this is similar to SSH_BUG_PKAUTH */
440 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
441 pkblob = packet_get_string(&blen);
443 buffer_append(&b, pkblob, blen);
444 pkalg = buffer_get_string(&b, &alen);
447 pkalg = packet_get_string(&alen);
448 pkblob = packet_get_string(&blen);
452 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
454 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
455 debug("unknown pkalg %s", pkalg);
458 if ((key = key_from_blob(pkblob, blen)) == NULL) {
459 debug("no key from blob. pkalg %s", pkalg);
462 if (key->type != pktype) {
463 error("input_userauth_pk_ok: type mismatch "
464 "for decoded key (received %d, expected %d)",
468 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
469 debug2("input_userauth_pk_ok: fp %s", fp);
473 * search keys in the reverse order, because last candidate has been
474 * moved to the end of the queue. this also avoids confusion by
477 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
478 if (key_equal(key, id->key)) {
479 sent = sign_and_send_pubkey(authctxt, id);
489 /* try another method if we did not send a packet */
491 userauth(authctxt, NULL);
496 userauth_gssapi(Authctxt *authctxt)
498 Gssctxt *gssctxt = NULL;
499 static gss_OID_set gss_supported = NULL;
500 static u_int mech = 0;
504 /* Try one GSSAPI method at a time, rather than sending them all at
507 if (gss_supported == NULL)
508 gss_indicate_mechs(&min, &gss_supported);
510 /* Check to see if the mechanism is usable before we offer it */
511 while (mech < gss_supported->count && !ok) {
513 ssh_gssapi_delete_ctx(&gssctxt);
514 ssh_gssapi_build_ctx(&gssctxt);
515 ssh_gssapi_set_oid(gssctxt, &gss_supported->elements[mech]);
517 /* My DER encoding requires length<128 */
518 if (gss_supported->elements[mech].length < 128 &&
519 !GSS_ERROR(ssh_gssapi_import_name(gssctxt,
521 ok = 1; /* Mechanism works */
530 authctxt->methoddata=(void *)gssctxt;
532 packet_start(SSH2_MSG_USERAUTH_REQUEST);
533 packet_put_cstring(authctxt->server_user);
534 packet_put_cstring(authctxt->service);
535 packet_put_cstring(authctxt->method->name);
539 packet_put_int((gss_supported->elements[mech].length) + 2);
540 packet_put_char(SSH_GSS_OIDTYPE);
541 packet_put_char(gss_supported->elements[mech].length);
542 packet_put_raw(gss_supported->elements[mech].elements,
543 gss_supported->elements[mech].length);
547 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
548 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
549 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
550 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
552 mech++; /* Move along to next candidate */
558 process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
560 Authctxt *authctxt = ctxt;
561 Gssctxt *gssctxt = authctxt->methoddata;
562 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
563 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
564 gss_buffer_desc gssbuf;
565 OM_uint32 status, ms, flags;
568 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
569 recv_tok, &send_tok, &flags);
571 if (send_tok.length > 0) {
572 if (GSS_ERROR(status))
573 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
575 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
577 packet_put_string(send_tok.value, send_tok.length);
579 gss_release_buffer(&ms, &send_tok);
582 if (status == GSS_S_COMPLETE) {
583 /* send either complete or MIC, depending on mechanism */
584 if (!(flags & GSS_C_INTEG_FLAG)) {
585 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
588 ssh_gssapi_buildmic(&b, authctxt->server_user,
589 authctxt->service, "gssapi-with-mic");
591 gssbuf.value = buffer_ptr(&b);
592 gssbuf.length = buffer_len(&b);
594 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
596 if (!GSS_ERROR(status)) {
597 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
598 packet_put_string(mic.value, mic.length);
604 gss_release_buffer(&ms, &mic);
612 input_gssapi_response(int type, u_int32_t plen, void *ctxt)
614 Authctxt *authctxt = ctxt;
619 if (authctxt == NULL)
620 fatal("input_gssapi_response: no authentication context");
621 gssctxt = authctxt->methoddata;
624 oidv = packet_get_string(&oidlen);
627 oidv[0] != SSH_GSS_OIDTYPE ||
628 oidv[1] != oidlen - 2) {
630 debug("Badly encoded mechanism OID received");
631 userauth(authctxt, NULL);
635 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
636 fatal("Server returned different OID than expected");
642 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
643 /* Start again with next method on list */
644 debug("Trying to start again");
645 userauth(authctxt, NULL);
651 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
653 Authctxt *authctxt = ctxt;
654 gss_buffer_desc recv_tok;
658 if (authctxt == NULL)
659 fatal("input_gssapi_response: no authentication context");
661 recv_tok.value = packet_get_string(&slen);
662 recv_tok.length = slen; /* safe typecast */
666 status = process_gssapi_token(ctxt, &recv_tok);
668 xfree(recv_tok.value);
670 if (GSS_ERROR(status)) {
671 /* Start again with the next method in the list */
672 userauth(authctxt, NULL);
678 input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
680 Authctxt *authctxt = ctxt;
682 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
683 gss_buffer_desc recv_tok;
684 OM_uint32 status, ms;
687 if (authctxt == NULL)
688 fatal("input_gssapi_response: no authentication context");
689 gssctxt = authctxt->methoddata;
691 recv_tok.value = packet_get_string(&len);
692 recv_tok.length = len;
696 /* Stick it into GSSAPI and see what it says */
697 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
698 &recv_tok, &send_tok, NULL);
700 xfree(recv_tok.value);
701 gss_release_buffer(&ms, &send_tok);
703 /* Server will be returning a failed packet after this one */
707 input_gssapi_error(int type, u_int32_t plen, void *ctxt)
713 maj=packet_get_int();
714 min=packet_get_int();
715 msg=packet_get_string(NULL);
716 lang=packet_get_string(NULL);
720 debug("Server GSSAPI Error:\n%s", msg);
727 userauth_none(Authctxt *authctxt)
729 /* initial userauth request */
730 packet_start(SSH2_MSG_USERAUTH_REQUEST);
731 packet_put_cstring(authctxt->server_user);
732 packet_put_cstring(authctxt->service);
733 packet_put_cstring(authctxt->method->name);
739 userauth_passwd(Authctxt *authctxt)
741 static int attempt = 0;
745 if (attempt++ >= options.number_of_password_prompts)
749 error("Permission denied, please try again.");
751 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
752 authctxt->server_user, authctxt->host);
753 password = read_passphrase(prompt, 0);
754 packet_start(SSH2_MSG_USERAUTH_REQUEST);
755 packet_put_cstring(authctxt->server_user);
756 packet_put_cstring(authctxt->service);
757 packet_put_cstring(authctxt->method->name);
759 packet_put_cstring(password);
760 memset(password, 0, strlen(password));
762 packet_add_padding(64);
765 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
766 &input_userauth_passwd_changereq);
771 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
774 input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
776 Authctxt *authctxt = ctxt;
777 char *info, *lang, *password = NULL, *retype = NULL;
780 debug2("input_userauth_passwd_changereq");
782 if (authctxt == NULL)
783 fatal("input_userauth_passwd_changereq: "
784 "no authentication context");
786 info = packet_get_string(NULL);
787 lang = packet_get_string(NULL);
788 if (strlen(info) > 0)
792 packet_start(SSH2_MSG_USERAUTH_REQUEST);
793 packet_put_cstring(authctxt->server_user);
794 packet_put_cstring(authctxt->service);
795 packet_put_cstring(authctxt->method->name);
796 packet_put_char(1); /* additional info */
797 snprintf(prompt, sizeof(prompt),
798 "Enter %.30s@%.128s's old password: ",
799 authctxt->server_user, authctxt->host);
800 password = read_passphrase(prompt, 0);
801 packet_put_cstring(password);
802 memset(password, 0, strlen(password));
805 while (password == NULL) {
806 snprintf(prompt, sizeof(prompt),
807 "Enter %.30s@%.128s's new password: ",
808 authctxt->server_user, authctxt->host);
809 password = read_passphrase(prompt, RP_ALLOW_EOF);
810 if (password == NULL) {
814 snprintf(prompt, sizeof(prompt),
815 "Retype %.30s@%.128s's new password: ",
816 authctxt->server_user, authctxt->host);
817 retype = read_passphrase(prompt, 0);
818 if (strcmp(password, retype) != 0) {
819 memset(password, 0, strlen(password));
821 logit("Mismatch; try again, EOF to quit.");
824 memset(retype, 0, strlen(retype));
827 packet_put_cstring(password);
828 memset(password, 0, strlen(password));
830 packet_add_padding(64);
833 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
834 &input_userauth_passwd_changereq);
838 identity_sign(Identity *id, u_char **sigp, u_int *lenp,
839 u_char *data, u_int datalen)
844 /* the agent supports this key */
846 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
849 * we have already loaded the private key or
850 * the private key is stored in external hardware
852 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
853 return (key_sign(id->key, sigp, lenp, data, datalen));
854 /* load the private key from the file */
855 if ((prv = load_identity_file(id->filename)) == NULL)
857 ret = key_sign(prv, sigp, lenp, data, datalen);
863 sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
866 u_char *blob, *signature;
872 debug3("sign_and_send_pubkey");
874 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
875 /* we cannot handle this key */
876 debug3("sign_and_send_pubkey: cannot handle key");
879 /* data to be signed */
881 if (datafellows & SSH_OLD_SESSIONID) {
882 buffer_append(&b, session_id2, session_id2_len);
883 skip = session_id2_len;
885 buffer_put_string(&b, session_id2, session_id2_len);
886 skip = buffer_len(&b);
888 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
889 buffer_put_cstring(&b, authctxt->server_user);
890 buffer_put_cstring(&b,
891 datafellows & SSH_BUG_PKSERVICE ?
894 if (datafellows & SSH_BUG_PKAUTH) {
895 buffer_put_char(&b, have_sig);
897 buffer_put_cstring(&b, authctxt->method->name);
898 buffer_put_char(&b, have_sig);
899 buffer_put_cstring(&b, key_ssh_name(id->key));
901 buffer_put_string(&b, blob, bloblen);
903 /* generate signature */
904 ret = identity_sign(id, &signature, &slen,
905 buffer_ptr(&b), buffer_len(&b));
914 if (datafellows & SSH_BUG_PKSERVICE) {
916 buffer_append(&b, session_id2, session_id2_len);
917 skip = session_id2_len;
918 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
919 buffer_put_cstring(&b, authctxt->server_user);
920 buffer_put_cstring(&b, authctxt->service);
921 buffer_put_cstring(&b, authctxt->method->name);
922 buffer_put_char(&b, have_sig);
923 if (!(datafellows & SSH_BUG_PKAUTH))
924 buffer_put_cstring(&b, key_ssh_name(id->key));
925 buffer_put_string(&b, blob, bloblen);
929 /* append signature */
930 buffer_put_string(&b, signature, slen);
933 /* skip session id and packet type */
934 if (buffer_len(&b) < skip + 1)
935 fatal("userauth_pubkey: internal error");
936 buffer_consume(&b, skip + 1);
938 /* put remaining data from buffer into packet */
939 packet_start(SSH2_MSG_USERAUTH_REQUEST);
940 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
948 send_pubkey_test(Authctxt *authctxt, Identity *id)
951 u_int bloblen, have_sig = 0;
953 debug3("send_pubkey_test");
955 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
956 /* we cannot handle this key */
957 debug3("send_pubkey_test: cannot handle key");
960 /* register callback for USERAUTH_PK_OK message */
961 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
963 packet_start(SSH2_MSG_USERAUTH_REQUEST);
964 packet_put_cstring(authctxt->server_user);
965 packet_put_cstring(authctxt->service);
966 packet_put_cstring(authctxt->method->name);
967 packet_put_char(have_sig);
968 if (!(datafellows & SSH_BUG_PKAUTH))
969 packet_put_cstring(key_ssh_name(id->key));
970 packet_put_string(blob, bloblen);
977 load_identity_file(char *filename)
980 char prompt[300], *passphrase;
984 if (stat(filename, &st) < 0) {
985 debug3("no such identity: %s", filename);
988 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
989 if (private == NULL) {
990 if (options.batch_mode)
992 snprintf(prompt, sizeof prompt,
993 "Enter passphrase for key '%.100s': ", filename);
994 for (i = 0; i < options.number_of_password_prompts; i++) {
995 passphrase = read_passphrase(prompt, 0);
996 if (strcmp(passphrase, "") != 0) {
997 private = key_load_private_type(KEY_UNSPEC, filename,
1001 debug2("no passphrase given, try next key");
1004 memset(passphrase, 0, strlen(passphrase));
1006 if (private != NULL || quit)
1008 debug2("bad passphrase given, try again...");
1015 * try keys in the following order:
1016 * 1. agent keys that are found in the config file
1017 * 2. other agent keys
1018 * 3. keys that are only listed in the config file
1021 pubkey_prepare(Authctxt *authctxt)
1024 Idlist agent, files, *preferred;
1026 AuthenticationConnection *ac;
1030 TAILQ_INIT(&agent); /* keys from the agent */
1031 TAILQ_INIT(&files); /* keys from the config file */
1032 preferred = &authctxt->keys;
1033 TAILQ_INIT(preferred); /* preferred order of keys */
1035 /* list of keys stored in the filesystem */
1036 for (i = 0; i < options.num_identity_files; i++) {
1037 key = options.identity_keys[i];
1038 if (key && key->type == KEY_RSA1)
1040 options.identity_keys[i] = NULL;
1041 id = xmalloc(sizeof(*id));
1042 memset(id, 0, sizeof(*id));
1044 id->filename = xstrdup(options.identity_files[i]);
1045 TAILQ_INSERT_TAIL(&files, id, next);
1047 /* list of keys supported by the agent */
1048 if ((ac = ssh_get_authentication_connection())) {
1049 for (key = ssh_get_first_identity(ac, &comment, 2);
1051 key = ssh_get_next_identity(ac, &comment, 2)) {
1053 TAILQ_FOREACH(id, &files, next) {
1054 /* agent keys from the config file are preferred */
1055 if (key_equal(key, id->key)) {
1058 TAILQ_REMOVE(&files, id, next);
1059 TAILQ_INSERT_TAIL(preferred, id, next);
1065 if (!found && !options.identities_only) {
1066 id = xmalloc(sizeof(*id));
1067 memset(id, 0, sizeof(*id));
1069 id->filename = comment;
1071 TAILQ_INSERT_TAIL(&agent, id, next);
1074 /* append remaining agent keys */
1075 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1076 TAILQ_REMOVE(&agent, id, next);
1077 TAILQ_INSERT_TAIL(preferred, id, next);
1079 authctxt->agent = ac;
1081 /* append remaining keys from the config file */
1082 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1083 TAILQ_REMOVE(&files, id, next);
1084 TAILQ_INSERT_TAIL(preferred, id, next);
1086 TAILQ_FOREACH(id, preferred, next) {
1087 debug2("key: %s (%p)", id->filename, id->key);
1092 pubkey_cleanup(Authctxt *authctxt)
1096 if (authctxt->agent != NULL)
1097 ssh_close_authentication_connection(authctxt->agent);
1098 for (id = TAILQ_FIRST(&authctxt->keys); id;
1099 id = TAILQ_FIRST(&authctxt->keys)) {
1100 TAILQ_REMOVE(&authctxt->keys, id, next);
1104 xfree(id->filename);
1110 userauth_pubkey(Authctxt *authctxt)
1115 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1118 /* move key to the end of the queue */
1119 TAILQ_REMOVE(&authctxt->keys, id, next);
1120 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1122 * send a test message if we have the public key. for
1123 * encrypted keys we cannot do this and have to load the
1124 * private key instead
1126 if (id->key && id->key->type != KEY_RSA1) {
1127 debug("Offering public key: %s", id->filename);
1128 sent = send_pubkey_test(authctxt, id);
1129 } else if (id->key == NULL) {
1130 debug("Trying private key: %s", id->filename);
1131 id->key = load_identity_file(id->filename);
1132 if (id->key != NULL) {
1134 sent = sign_and_send_pubkey(authctxt, id);
1146 * Send userauth request message specifying keyboard-interactive method.
1149 userauth_kbdint(Authctxt *authctxt)
1151 static int attempt = 0;
1153 if (attempt++ >= options.number_of_password_prompts)
1155 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1156 if (attempt > 1 && !authctxt->info_req_seen) {
1157 debug3("userauth_kbdint: disable: no info_req_seen");
1158 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1162 debug2("userauth_kbdint");
1163 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1164 packet_put_cstring(authctxt->server_user);
1165 packet_put_cstring(authctxt->service);
1166 packet_put_cstring(authctxt->method->name);
1167 packet_put_cstring(""); /* lang */
1168 packet_put_cstring(options.kbd_interactive_devices ?
1169 options.kbd_interactive_devices : "");
1172 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1177 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1180 input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1182 Authctxt *authctxt = ctxt;
1183 char *name, *inst, *lang, *prompt, *response;
1184 u_int num_prompts, i;
1187 debug2("input_userauth_info_req");
1189 if (authctxt == NULL)
1190 fatal("input_userauth_info_req: no authentication context");
1192 authctxt->info_req_seen = 1;
1194 name = packet_get_string(NULL);
1195 inst = packet_get_string(NULL);
1196 lang = packet_get_string(NULL);
1197 if (strlen(name) > 0)
1199 if (strlen(inst) > 0)
1205 num_prompts = packet_get_int();
1207 * Begin to build info response packet based on prompts requested.
1208 * We commit to providing the correct number of responses, so if
1209 * further on we run into a problem that prevents this, we have to
1210 * be sure and clean this up and send a correct error response.
1212 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1213 packet_put_int(num_prompts);
1215 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1216 for (i = 0; i < num_prompts; i++) {
1217 prompt = packet_get_string(NULL);
1218 echo = packet_get_char();
1220 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1222 packet_put_cstring(response);
1223 memset(response, 0, strlen(response));
1227 packet_check_eom(); /* done with parsing incoming message. */
1229 packet_add_padding(64);
1234 ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1235 u_char *data, u_int datalen)
1240 int to[2], from[2], status, version = 2;
1242 debug2("ssh_keysign called");
1244 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1245 error("ssh_keysign: no installed: %s", strerror(errno));
1248 if (fflush(stdout) != 0)
1249 error("ssh_keysign: fflush: %s", strerror(errno));
1251 error("ssh_keysign: pipe: %s", strerror(errno));
1254 if (pipe(from) < 0) {
1255 error("ssh_keysign: pipe: %s", strerror(errno));
1258 if ((pid = fork()) < 0) {
1259 error("ssh_keysign: fork: %s", strerror(errno));
1266 if (dup2(from[1], STDOUT_FILENO) < 0)
1267 fatal("ssh_keysign: dup2: %s", strerror(errno));
1269 if (dup2(to[0], STDIN_FILENO) < 0)
1270 fatal("ssh_keysign: dup2: %s", strerror(errno));
1273 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1274 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1281 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1282 buffer_put_string(&b, data, datalen);
1283 if (ssh_msg_send(to[1], version, &b) == -1)
1284 fatal("ssh_keysign: couldn't send request");
1286 if (ssh_msg_recv(from[0], &b) < 0) {
1287 error("ssh_keysign: no reply");
1294 while (waitpid(pid, &status, 0) < 0)
1298 if (buffer_get_char(&b) != version) {
1299 error("ssh_keysign: bad version");
1303 *sigp = buffer_get_string(&b, lenp);
1310 userauth_hostbased(Authctxt *authctxt)
1312 Key *private = NULL;
1313 Sensitive *sensitive = authctxt->sensitive;
1315 u_char *signature, *blob;
1316 char *chost, *pkalg, *p;
1317 const char *service;
1319 int ok, i, len, found = 0;
1321 /* check for a useful key */
1322 for (i = 0; i < sensitive->nkeys; i++) {
1323 private = sensitive->keys[i];
1324 if (private && private->type != KEY_RSA1) {
1326 /* we take and free the key */
1327 sensitive->keys[i] = NULL;
1332 debug("No more client hostkeys for hostbased authentication.");
1335 if (key_to_blob(private, &blob, &blen) == 0) {
1339 /* figure out a name for the client host */
1340 p = get_local_name(packet_get_connection_in());
1342 error("userauth_hostbased: cannot get local ipaddr/name");
1346 len = strlen(p) + 2;
1347 chost = xmalloc(len);
1348 strlcpy(chost, p, len);
1349 strlcat(chost, ".", len);
1350 debug2("userauth_hostbased: chost %s", chost);
1353 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1355 pkalg = xstrdup(key_ssh_name(private));
1357 /* construct data */
1358 buffer_put_string(&b, session_id2, session_id2_len);
1359 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1360 buffer_put_cstring(&b, authctxt->server_user);
1361 buffer_put_cstring(&b, service);
1362 buffer_put_cstring(&b, authctxt->method->name);
1363 buffer_put_cstring(&b, pkalg);
1364 buffer_put_string(&b, blob, blen);
1365 buffer_put_cstring(&b, chost);
1366 buffer_put_cstring(&b, authctxt->local_user);
1370 if (sensitive->external_keysign)
1371 ok = ssh_keysign(private, &signature, &slen,
1372 buffer_ptr(&b), buffer_len(&b));
1374 ok = key_sign(private, &signature, &slen,
1375 buffer_ptr(&b), buffer_len(&b));
1379 error("key_sign failed");
1384 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1385 packet_put_cstring(authctxt->server_user);
1386 packet_put_cstring(authctxt->service);
1387 packet_put_cstring(authctxt->method->name);
1388 packet_put_cstring(pkalg);
1389 packet_put_string(blob, blen);
1390 packet_put_cstring(chost);
1391 packet_put_cstring(authctxt->local_user);
1392 packet_put_string(signature, slen);
1393 memset(signature, 's', slen);
1402 /* find auth method */
1405 * given auth method name, if configurable options permit this method fill
1406 * in auth_ident field and return true, otherwise return false.
1409 authmethod_is_enabled(Authmethod *method)
1413 /* return false if options indicate this method is disabled */
1414 if (method->enabled == NULL || *method->enabled == 0)
1416 /* return false if batch mode is enabled but method needs interactive mode */
1417 if (method->batch_flag != NULL && *method->batch_flag != 0)
1423 authmethod_lookup(const char *name)
1425 Authmethod *method = NULL;
1427 for (method = authmethods; method->name != NULL; method++)
1428 if (strcmp(name, method->name) == 0)
1430 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1434 /* XXX internal state */
1435 static Authmethod *current = NULL;
1436 static char *supported = NULL;
1437 static char *preferred = NULL;
1440 * Given the authentication method list sent by the server, return the
1441 * next method we should try. If the server initially sends a nil list,
1442 * use a built-in default list.
1445 authmethod_get(char *authlist)
1450 /* Use a suitable default if we're passed a nil list. */
1451 if (authlist == NULL || strlen(authlist) == 0)
1452 authlist = options.preferred_authentications;
1454 if (supported == NULL || strcmp(authlist, supported) != 0) {
1455 debug3("start over, passed a different list %s", authlist);
1456 if (supported != NULL)
1458 supported = xstrdup(authlist);
1459 preferred = options.preferred_authentications;
1460 debug3("preferred %s", preferred);
1462 } else if (current != NULL && authmethod_is_enabled(current))
1466 if ((name = match_list(preferred, supported, &next)) == NULL) {
1467 debug("No more authentication methods to try.");
1472 debug3("authmethod_lookup %s", name);
1473 debug3("remaining preferred: %s", preferred);
1474 if ((current = authmethod_lookup(name)) != NULL &&
1475 authmethod_is_enabled(current)) {
1476 debug3("authmethod_is_enabled %s", name);
1477 debug("Next authentication method: %s", name);
1484 authmethods_get(void)
1486 Authmethod *method = NULL;
1491 for (method = authmethods; method->name != NULL; method++) {
1492 if (authmethod_is_enabled(method)) {
1493 if (buffer_len(&b) > 0)
1494 buffer_append(&b, ",", 1);
1495 buffer_append(&b, method->name, strlen(method->name));
1498 buffer_append(&b, "\0", 1);
1499 list = xstrdup(buffer_ptr(&b));