blob: 1a6a5850d46ac317a188710c8cbafe66dc93b3c6 [file] [log] [blame]
Damien Miller62e9c4f2013-04-23 15:15:49 +10001/* $OpenBSD: sshconnect2.c,v 1.193 2013/03/05 20:16:09 markus Exp $ */
Damien Millereba71ba2000-04-29 23:57:08 +10002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Miller01ed2272008-11-05 16:20:46 +11004 * Copyright (c) 2008 Damien Miller. All rights reserved.
Damien Millereba71ba2000-04-29 23:57:08 +10005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100014 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
Damien Miller2eb63402006-03-15 11:09:42 +110028
Damien Miller9cf6d072006-03-15 11:29:24 +110029#include <sys/types.h>
Damien Millerd7834352006-08-05 12:39:39 +100030#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110031#include <sys/wait.h>
Damien Millerf17883e2006-03-15 11:45:54 +110032#include <sys/stat.h>
Damien Millereba71ba2000-04-29 23:57:08 +100033
Darren Tucker39972492006-07-12 22:22:46 +100034#include <errno.h>
Darren Tucker6e7fe1c2010-01-08 17:07:22 +110035#include <fcntl.h>
Darren Tuckerf520ea12007-05-20 15:11:33 +100036#include <netdb.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <pwd.h>
38#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100039#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100040#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100041#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100042#include <unistd.h>
Damien Miller63b4bcd2013-03-20 12:55:14 +110043#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
Damien Miller7ba0ca72008-07-17 18:57:06 +100044#include <vis.h>
Damien Miller2e28d862008-07-17 19:15:43 +100045#endif
Darren Tucker39972492006-07-12 22:22:46 +100046
Damien Miller9c617692003-05-14 14:31:11 +100047#include "openbsd-compat/sys-queue.h"
48
Damien Millerd7834352006-08-05 12:39:39 +100049#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100050#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100052#include "buffer.h"
53#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100054#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100056#include "key.h"
Damien Millereba71ba2000-04-29 23:57:08 +100057#include "kex.h"
58#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100059#include "sshconnect.h"
60#include "authfile.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000061#include "dh.h"
Damien Millerad833b32000-08-23 10:46:23 +100062#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "log.h"
64#include "readconf.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100065#include "misc.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000066#include "match.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000067#include "dispatch.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000068#include "canohost.h"
Ben Lindstrom1bad2562002-06-06 19:57:33 +000069#include "msg.h"
70#include "pathnames.h"
Damien Miller6b4069a2006-06-13 13:05:15 +100071#include "uidswap.h"
Damien Millerd925dcd2010-12-01 12:21:51 +110072#include "hostfile.h"
Damien Millercee85232009-03-06 00:58:22 +110073#include "schnorr.h"
Damien Miller01ed2272008-11-05 16:20:46 +110074#include "jpake.h"
Damien Miller874d77b2000-10-14 16:23:11 +110075
Darren Tucker0efd1552003-08-26 11:49:55 +100076#ifdef GSSAPI
77#include "ssh-gss.h"
78#endif
79
Damien Millereba71ba2000-04-29 23:57:08 +100080/* import */
81extern char *client_version_string;
82extern char *server_version_string;
83extern Options options;
84
85/*
86 * SSH2 key exchange
87 */
88
Ben Lindstrom46c16222000-12-22 01:43:59 +000089u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +100090u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100091
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000092char *xxx_host;
93struct sockaddr *xxx_hostaddr;
94
Ben Lindstromf28f6342001-04-04 02:03:04 +000095Kex *xxx_kex = NULL;
96
Ben Lindstrombba81212001-06-25 05:01:22 +000097static int
Ben Lindstromd6481ea2001-06-25 04:37:41 +000098verify_host_key_callback(Key *hostkey)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000099{
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000100 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +1000101 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000102 return 0;
103}
104
Damien Millerd925dcd2010-12-01 12:21:51 +1100105static char *
106order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
107{
108 char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
109 size_t maxlen;
110 struct hostkeys *hostkeys;
111 int ktype;
Damien Miller295ee632011-05-29 21:42:31 +1000112 u_int i;
Damien Millerd925dcd2010-12-01 12:21:51 +1100113
114 /* Find all hostkeys for this hostname */
115 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
116 hostkeys = init_hostkeys();
Damien Miller295ee632011-05-29 21:42:31 +1000117 for (i = 0; i < options.num_user_hostfiles; i++)
118 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
119 for (i = 0; i < options.num_system_hostfiles; i++)
120 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
Damien Millerd925dcd2010-12-01 12:21:51 +1100121
122 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
123 maxlen = strlen(avail) + 1;
124 first = xmalloc(maxlen);
125 last = xmalloc(maxlen);
126 *first = *last = '\0';
127
128#define ALG_APPEND(to, from) \
129 do { \
130 if (*to != '\0') \
131 strlcat(to, ",", maxlen); \
132 strlcat(to, from, maxlen); \
133 } while (0)
134
135 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
136 if ((ktype = key_type_from_name(alg)) == KEY_UNSPEC)
137 fatal("%s: unknown alg %s", __func__, alg);
138 if (lookup_key_in_hostkeys_by_type(hostkeys,
139 key_type_plain(ktype), NULL))
140 ALG_APPEND(first, alg);
141 else
142 ALG_APPEND(last, alg);
143 }
144#undef ALG_APPEND
145 xasprintf(&ret, "%s%s%s", first, *first == '\0' ? "" : ",", last);
146 if (*first != '\0')
147 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
148
149 xfree(first);
150 xfree(last);
151 xfree(hostname);
152 xfree(oavail);
153 free_hostkeys(hostkeys);
154
155 return ret;
156}
157
Damien Millereba71ba2000-04-29 23:57:08 +1000158void
Damien Millerd925dcd2010-12-01 12:21:51 +1100159ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
Damien Miller874d77b2000-10-14 16:23:11 +1100160{
Damien Miller874d77b2000-10-14 16:23:11 +1100161 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000162
163 xxx_host = host;
164 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100165
Damien Millere39cacc2000-11-29 12:18:44 +1100166 if (options.ciphers == (char *)-1) {
Damien Miller996acd22003-04-09 20:59:48 +1000167 logit("No valid ciphers for protocol version 2 given, using defaults.");
Damien Millere39cacc2000-11-29 12:18:44 +1100168 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100169 }
170 if (options.ciphers != NULL) {
171 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
172 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
173 }
Damien Millera0ff4662001-03-30 10:49:35 +1000174 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
175 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
176 myproposal[PROPOSAL_ENC_ALGS_STOC] =
177 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100178 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000179 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000180 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100181 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000182 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000183 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100184 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000185 if (options.macs != NULL) {
186 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
187 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
188 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000189 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100190 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000191 options.hostkeyalgorithms;
Damien Millerd925dcd2010-12-01 12:21:51 +1100192 else {
193 /* Prefer algorithms that we already have keys for */
194 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
195 order_hostkeyalgs(host, hostaddr, port);
196 }
Damien Millerd5f62bf2010-09-24 22:11:14 +1000197 if (options.kex_algorithms != NULL)
198 myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
Damien Miller874d77b2000-10-14 16:23:11 +1100199
Damien Millera5539d22003-04-09 20:50:06 +1000200 if (options.rekey_limit)
Damien Miller3dff1762008-02-10 22:25:52 +1100201 packet_set_rekey_limit((u_int32_t)options.rekey_limit);
Damien Millera5539d22003-04-09 20:50:06 +1000202
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000203 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000204 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100205 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000206 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100207 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100208 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Damien Millereb8b60e2010-08-31 22:41:14 +1000209 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000210 kex->client_version_string=client_version_string;
211 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000212 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100213
Ben Lindstromf28f6342001-04-04 02:03:04 +0000214 xxx_kex = kex;
215
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000216 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100217
Darren Tucker36331b52010-01-08 16:50:41 +1100218 if (options.use_roaming && !kex->roaming) {
219 debug("Roaming not allowed by server");
220 options.use_roaming = 0;
221 }
222
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000223 session_id2 = kex->session_id;
224 session_id2_len = kex->session_id_len;
225
Damien Miller874d77b2000-10-14 16:23:11 +1100226#ifdef DEBUG_KEXDH
227 /* send 1st encrypted/maced/compressed message */
228 packet_start(SSH2_MSG_IGNORE);
229 packet_put_cstring("markus");
230 packet_send();
231 packet_write_wait();
232#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000233}
Damien Millerb1715dc2000-05-30 13:44:51 +1000234
Damien Millereba71ba2000-04-29 23:57:08 +1000235/*
236 * Authenticate user
237 */
Damien Miller62cee002000-09-23 17:15:56 +1100238
239typedef struct Authctxt Authctxt;
240typedef struct Authmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000241typedef struct identity Identity;
242typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100243
Damien Miller280ecfb2003-05-14 13:46:00 +1000244struct identity {
245 TAILQ_ENTRY(identity) next;
246 AuthenticationConnection *ac; /* set if agent supports key */
247 Key *key; /* public/private key */
248 char *filename; /* comment for agent-only keys */
249 int tried;
250 int isprivate; /* key points to the private key */
Damien Miller5ceddc32013-02-15 12:18:32 +1100251 int userprovided;
Damien Miller280ecfb2003-05-14 13:46:00 +1000252};
253TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100254
255struct Authctxt {
256 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000257 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100258 const char *host;
259 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100260 Authmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000261 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000262 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000263 /* pubkey */
Damien Miller280ecfb2003-05-14 13:46:00 +1000264 Idlist keys;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000265 AuthenticationConnection *agent;
266 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000267 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000268 /* kbd-interactive */
269 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000270 /* generic */
271 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100272};
273struct Authmethod {
274 char *name; /* string to compare against server's list */
275 int (*userauth)(Authctxt *authctxt);
Damien Miller01ed2272008-11-05 16:20:46 +1100276 void (*cleanup)(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100277 int *enabled; /* flag in option struct that enables method */
278 int *batch_flag; /* flag in option struct that disables method */
279};
280
Damien Miller630d6f42002-01-22 23:17:30 +1100281void input_userauth_success(int, u_int32_t, void *);
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100282void input_userauth_success_unexpected(int, u_int32_t, void *);
Damien Miller630d6f42002-01-22 23:17:30 +1100283void input_userauth_failure(int, u_int32_t, void *);
284void input_userauth_banner(int, u_int32_t, void *);
285void input_userauth_error(int, u_int32_t, void *);
286void input_userauth_info_req(int, u_int32_t, void *);
287void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000288void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller01ed2272008-11-05 16:20:46 +1100289void input_userauth_jpake_server_step1(int, u_int32_t, void *);
290void input_userauth_jpake_server_step2(int, u_int32_t, void *);
291void input_userauth_jpake_server_confirm(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100292
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000293int userauth_none(Authctxt *);
294int userauth_pubkey(Authctxt *);
295int userauth_passwd(Authctxt *);
296int userauth_kbdint(Authctxt *);
297int userauth_hostbased(Authctxt *);
Damien Miller01ed2272008-11-05 16:20:46 +1100298int userauth_jpake(Authctxt *);
299
300void userauth_jpake_cleanup(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100301
Darren Tucker0efd1552003-08-26 11:49:55 +1000302#ifdef GSSAPI
303int userauth_gssapi(Authctxt *authctxt);
304void input_gssapi_response(int type, u_int32_t, void *);
305void input_gssapi_token(int type, u_int32_t, void *);
306void input_gssapi_hash(int type, u_int32_t, void *);
307void input_gssapi_error(int, u_int32_t, void *);
308void input_gssapi_errtok(int, u_int32_t, void *);
309#endif
310
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000311void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000312
Damien Miller280ecfb2003-05-14 13:46:00 +1000313static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000314static void pubkey_prepare(Authctxt *);
315static void pubkey_cleanup(Authctxt *);
Damien Miller5ceddc32013-02-15 12:18:32 +1100316static Key *load_identity_file(char *, int);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000317
Ben Lindstrombba81212001-06-25 05:01:22 +0000318static Authmethod *authmethod_get(char *authlist);
319static Authmethod *authmethod_lookup(const char *name);
320static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100321
322Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000323#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100324 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000325 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100326 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000327 &options.gss_authentication,
328 NULL},
329#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000330 {"hostbased",
331 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100332 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000333 &options.hostbased_authentication,
334 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000335 {"publickey",
336 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100337 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000338 &options.pubkey_authentication,
339 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100340#ifdef JPAKE
341 {"jpake-01@openssh.com",
342 userauth_jpake,
343 userauth_jpake_cleanup,
344 &options.zero_knowledge_password_authentication,
345 &options.batch_mode},
346#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100347 {"keyboard-interactive",
348 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100349 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100350 &options.kbd_interactive_authentication,
351 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000352 {"password",
353 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100354 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000355 &options.password_authentication,
356 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100357 {"none",
358 userauth_none,
359 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100360 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100361 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100362 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100363};
364
365void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000366ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000367 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100368{
369 Authctxt authctxt;
370 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100371
Ben Lindstrom551ea372001-06-05 18:56:16 +0000372 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000373 options.kbd_interactive_authentication = 1;
374
Damien Miller62cee002000-09-23 17:15:56 +1100375 packet_start(SSH2_MSG_SERVICE_REQUEST);
376 packet_put_cstring("ssh-userauth");
377 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000378 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100379 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100380 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000381 if (type != SSH2_MSG_SERVICE_ACCEPT)
382 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100383 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100384 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000385 debug2("service_accept: %s", reply);
Damien Miller62cee002000-09-23 17:15:56 +1100386 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100387 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000388 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100389 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100390 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000391 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100392
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000393 if (options.preferred_authentications == NULL)
394 options.preferred_authentications = authmethods_get();
395
Damien Miller62cee002000-09-23 17:15:56 +1100396 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000397 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000398 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100399 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000400 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100401 authctxt.host = host;
402 authctxt.service = "ssh-connection"; /* service name */
403 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100404 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000405 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000406 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000407 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000408 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100409 if (authctxt.method == NULL)
410 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100411
412 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100413 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100414
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000415 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100416 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
417 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000418 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100419 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
420
Damien Miller280ecfb2003-05-14 13:46:00 +1000421 pubkey_cleanup(&authctxt);
Damien Millerf842fcb2003-05-15 12:01:28 +1000422 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
423
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000424 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100425}
Damien Millerf842fcb2003-05-15 12:01:28 +1000426
Damien Miller62cee002000-09-23 17:15:56 +1100427void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000428userauth(Authctxt *authctxt, char *authlist)
429{
Damien Miller01ed2272008-11-05 16:20:46 +1100430 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
431 authctxt->method->cleanup(authctxt);
432
Darren Tucker0efd1552003-08-26 11:49:55 +1000433 if (authctxt->methoddata) {
434 xfree(authctxt->methoddata);
435 authctxt->methoddata = NULL;
436 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000437 if (authlist == NULL) {
438 authlist = authctxt->authlist;
439 } else {
440 if (authctxt->authlist)
441 xfree(authctxt->authlist);
442 authctxt->authlist = authlist;
443 }
444 for (;;) {
445 Authmethod *method = authmethod_get(authlist);
446 if (method == NULL)
447 fatal("Permission denied (%s).", authlist);
448 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000449
450 /* reset the per method handler */
451 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
452 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
453
454 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000455 if (method->userauth(authctxt) != 0) {
456 debug2("we sent a %s packet, wait for reply", method->name);
457 break;
458 } else {
459 debug2("we did not send a packet, disable method");
460 method->enabled = NULL;
461 }
462 }
463}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000464
Damien Millerf7475d72008-11-03 19:26:18 +1100465/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000466void
Damien Miller630d6f42002-01-22 23:17:30 +1100467input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100468{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000469 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000470 "type %d", type);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000471}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000472
Damien Millerf7475d72008-11-03 19:26:18 +1100473/* ARGSUSED */
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000474void
Damien Miller630d6f42002-01-22 23:17:30 +1100475input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000476{
Damien Miller7ba0ca72008-07-17 18:57:06 +1000477 char *msg, *raw, *lang;
478 u_int len;
Darren Tucker79644822003-10-08 17:37:58 +1000479
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000480 debug3("input_userauth_banner");
Damien Miller7ba0ca72008-07-17 18:57:06 +1000481 raw = packet_get_string(&len);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000482 lang = packet_get_string(NULL);
Damien Millerc674d582008-11-03 19:16:57 +1100483 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
Damien Miller7ba0ca72008-07-17 18:57:06 +1000484 if (len > 65536)
485 len = 65536;
Damien Millerc4d1b362008-11-03 19:22:09 +1100486 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
Darren Tucker0c348f52010-01-08 18:58:05 +1100487 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
Darren Tucker046dff22003-10-08 17:32:02 +1000488 fprintf(stderr, "%s", msg);
Damien Miller7ba0ca72008-07-17 18:57:06 +1000489 xfree(msg);
490 }
491 xfree(raw);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000492 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100493}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000494
Damien Millerf7475d72008-11-03 19:26:18 +1100495/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100496void
Damien Miller630d6f42002-01-22 23:17:30 +1100497input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100498{
499 Authctxt *authctxt = ctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100500
Damien Miller62cee002000-09-23 17:15:56 +1100501 if (authctxt == NULL)
502 fatal("input_userauth_success: no authentication context");
Darren Tucker79644822003-10-08 17:37:58 +1000503 if (authctxt->authlist) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000504 xfree(authctxt->authlist);
Darren Tucker79644822003-10-08 17:37:58 +1000505 authctxt->authlist = NULL;
506 }
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100507 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
508 authctxt->method->cleanup(authctxt);
Darren Tucker79644822003-10-08 17:37:58 +1000509 if (authctxt->methoddata) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000510 xfree(authctxt->methoddata);
Darren Tucker79644822003-10-08 17:37:58 +1000511 authctxt->methoddata = NULL;
512 }
Damien Miller62cee002000-09-23 17:15:56 +1100513 authctxt->success = 1; /* break out */
514}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000515
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100516void
517input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
518{
519 Authctxt *authctxt = ctxt;
520
521 if (authctxt == NULL)
522 fatal("%s: no authentication context", __func__);
523
524 fatal("Unexpected authentication success during %s.",
525 authctxt->method->name);
526}
527
Damien Millerf7475d72008-11-03 19:26:18 +1100528/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100529void
Damien Miller630d6f42002-01-22 23:17:30 +1100530input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100531{
Damien Miller62cee002000-09-23 17:15:56 +1100532 Authctxt *authctxt = ctxt;
533 char *authlist = NULL;
534 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100535
536 if (authctxt == NULL)
537 fatal("input_userauth_failure: no authentication context");
538
Damien Miller874d77b2000-10-14 16:23:11 +1100539 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100540 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100541 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100542
Damien Miller62e9c4f2013-04-23 15:15:49 +1000543 if (partial != 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000544 logit("Authenticated with partial success.");
Damien Miller62e9c4f2013-04-23 15:15:49 +1000545 /* reset state */
546 pubkey_cleanup(authctxt);
547 pubkey_prepare(authctxt);
548 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000549 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100550
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000551 userauth(authctxt, authlist);
552}
Damien Millerf7475d72008-11-03 19:26:18 +1100553
554/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000555void
Damien Miller630d6f42002-01-22 23:17:30 +1100556input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000557{
558 Authctxt *authctxt = ctxt;
559 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000560 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000561 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000562 int pktype, sent = 0;
563 u_int alen, blen;
564 char *pkalg, *fp;
565 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000566
567 if (authctxt == NULL)
568 fatal("input_userauth_pk_ok: no authentication context");
569 if (datafellows & SSH_BUG_PKOK) {
570 /* this is similar to SSH_BUG_PKAUTH */
571 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
572 pkblob = packet_get_string(&blen);
573 buffer_init(&b);
574 buffer_append(&b, pkblob, blen);
575 pkalg = buffer_get_string(&b, &alen);
576 buffer_free(&b);
577 } else {
578 pkalg = packet_get_string(&alen);
579 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000580 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100581 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000582
Damien Miller280ecfb2003-05-14 13:46:00 +1000583 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000584
Damien Miller280ecfb2003-05-14 13:46:00 +1000585 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
586 debug("unknown pkalg %s", pkalg);
587 goto done;
588 }
589 if ((key = key_from_blob(pkblob, blen)) == NULL) {
590 debug("no key from blob. pkalg %s", pkalg);
591 goto done;
592 }
593 if (key->type != pktype) {
594 error("input_userauth_pk_ok: type mismatch "
595 "for decoded key (received %d, expected %d)",
596 key->type, pktype);
597 goto done;
598 }
599 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
600 debug2("input_userauth_pk_ok: fp %s", fp);
601 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000602
Darren Tuckerd05b6012003-10-15 15:55:59 +1000603 /*
604 * search keys in the reverse order, because last candidate has been
605 * moved to the end of the queue. this also avoids confusion by
606 * duplicate keys
607 */
Damien Miller0b51a522004-04-20 20:07:19 +1000608 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000609 if (key_equal(key, id->key)) {
610 sent = sign_and_send_pubkey(authctxt, id);
611 break;
612 }
613 }
614done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000615 if (key != NULL)
616 key_free(key);
617 xfree(pkalg);
618 xfree(pkblob);
619
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000620 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000621 if (sent == 0)
622 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100623}
624
Darren Tucker0efd1552003-08-26 11:49:55 +1000625#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100626int
Darren Tucker0efd1552003-08-26 11:49:55 +1000627userauth_gssapi(Authctxt *authctxt)
628{
629 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100630 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000631 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000632 OM_uint32 min;
633 int ok = 0;
634
635 /* Try one GSSAPI method at a time, rather than sending them all at
636 * once. */
637
Darren Tucker56afe142003-11-03 20:06:14 +1100638 if (gss_supported == NULL)
639 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000640
641 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100642 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000643 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100644 if (gss_supported->elements[mech].length < 128 &&
Damien Millera1cb9f32006-08-19 00:33:34 +1000645 ssh_gssapi_check_mechanism(&gssctxt,
646 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000647 ok = 1; /* Mechanism works */
648 } else {
649 mech++;
650 }
651 }
652
Damien Millera1cb9f32006-08-19 00:33:34 +1000653 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000654 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000655
656 authctxt->methoddata=(void *)gssctxt;
657
658 packet_start(SSH2_MSG_USERAUTH_REQUEST);
659 packet_put_cstring(authctxt->server_user);
660 packet_put_cstring(authctxt->service);
661 packet_put_cstring(authctxt->method->name);
662
663 packet_put_int(1);
664
Darren Tucker655a5e02003-11-03 20:09:03 +1100665 packet_put_int((gss_supported->elements[mech].length) + 2);
666 packet_put_char(SSH_GSS_OIDTYPE);
667 packet_put_char(gss_supported->elements[mech].length);
668 packet_put_raw(gss_supported->elements[mech].elements,
669 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000670
671 packet_send();
672
673 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
674 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
675 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
676 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
677
678 mech++; /* Move along to next candidate */
679
680 return 1;
681}
682
Damien Miller91c6aa42003-11-17 21:20:18 +1100683static OM_uint32
684process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
685{
686 Authctxt *authctxt = ctxt;
687 Gssctxt *gssctxt = authctxt->methoddata;
688 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000689 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
690 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100691 OM_uint32 status, ms, flags;
692 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100693
Damien Miller91c6aa42003-11-17 21:20:18 +1100694 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100695 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100696
697 if (send_tok.length > 0) {
698 if (GSS_ERROR(status))
699 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
700 else
701 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100702
Damien Miller91c6aa42003-11-17 21:20:18 +1100703 packet_put_string(send_tok.value, send_tok.length);
704 packet_send();
705 gss_release_buffer(&ms, &send_tok);
706 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100707
Damien Miller91c6aa42003-11-17 21:20:18 +1100708 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100709 /* send either complete or MIC, depending on mechanism */
710 if (!(flags & GSS_C_INTEG_FLAG)) {
711 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
712 packet_send();
713 } else {
714 ssh_gssapi_buildmic(&b, authctxt->server_user,
715 authctxt->service, "gssapi-with-mic");
716
717 gssbuf.value = buffer_ptr(&b);
718 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100719
Damien Miller0425d402003-11-17 22:18:21 +1100720 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100721
Damien Miller0425d402003-11-17 22:18:21 +1100722 if (!GSS_ERROR(status)) {
723 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
724 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100725
Damien Miller0425d402003-11-17 22:18:21 +1100726 packet_send();
727 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100728
Damien Miller0425d402003-11-17 22:18:21 +1100729 buffer_free(&b);
730 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100731 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100732 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100733
Damien Miller91c6aa42003-11-17 21:20:18 +1100734 return status;
735}
736
Damien Millerf7475d72008-11-03 19:26:18 +1100737/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000738void
739input_gssapi_response(int type, u_int32_t plen, void *ctxt)
740{
741 Authctxt *authctxt = ctxt;
742 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000743 int oidlen;
744 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000745
746 if (authctxt == NULL)
747 fatal("input_gssapi_response: no authentication context");
748 gssctxt = authctxt->methoddata;
749
750 /* Setup our OID */
751 oidv = packet_get_string(&oidlen);
752
Darren Tucker655a5e02003-11-03 20:09:03 +1100753 if (oidlen <= 2 ||
754 oidv[0] != SSH_GSS_OIDTYPE ||
755 oidv[1] != oidlen - 2) {
Damien Miller91c6aa42003-11-17 21:20:18 +1100756 xfree(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100757 debug("Badly encoded mechanism OID received");
758 userauth(authctxt, NULL);
Darren Tucker655a5e02003-11-03 20:09:03 +1100759 return;
Darren Tucker0efd1552003-08-26 11:49:55 +1000760 }
761
Darren Tucker655a5e02003-11-03 20:09:03 +1100762 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
763 fatal("Server returned different OID than expected");
764
Darren Tucker0efd1552003-08-26 11:49:55 +1000765 packet_check_eom();
766
767 xfree(oidv);
768
Damien Miller91c6aa42003-11-17 21:20:18 +1100769 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000770 /* Start again with next method on list */
771 debug("Trying to start again");
772 userauth(authctxt, NULL);
773 return;
774 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000775}
776
Damien Millerf7475d72008-11-03 19:26:18 +1100777/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000778void
779input_gssapi_token(int type, u_int32_t plen, void *ctxt)
780{
781 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000782 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100783 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000784 u_int slen;
785
786 if (authctxt == NULL)
787 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000788
789 recv_tok.value = packet_get_string(&slen);
790 recv_tok.length = slen; /* safe typecast */
791
792 packet_check_eom();
793
Damien Miller91c6aa42003-11-17 21:20:18 +1100794 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000795
796 xfree(recv_tok.value);
797
798 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000799 /* Start again with the next method in the list */
800 userauth(authctxt, NULL);
801 return;
802 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000803}
804
Damien Millerf7475d72008-11-03 19:26:18 +1100805/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000806void
807input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
808{
809 Authctxt *authctxt = ctxt;
810 Gssctxt *gssctxt;
811 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
812 gss_buffer_desc recv_tok;
813 OM_uint32 status, ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000814 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000815
816 if (authctxt == NULL)
817 fatal("input_gssapi_response: no authentication context");
818 gssctxt = authctxt->methoddata;
819
Darren Tucker600ad8d2003-08-26 12:10:48 +1000820 recv_tok.value = packet_get_string(&len);
821 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000822
823 packet_check_eom();
824
825 /* Stick it into GSSAPI and see what it says */
826 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000827 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000828
829 xfree(recv_tok.value);
830 gss_release_buffer(&ms, &send_tok);
831
832 /* Server will be returning a failed packet after this one */
833}
834
Damien Millerf7475d72008-11-03 19:26:18 +1100835/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000836void
837input_gssapi_error(int type, u_int32_t plen, void *ctxt)
838{
839 OM_uint32 maj, min;
840 char *msg;
841 char *lang;
842
843 maj=packet_get_int();
844 min=packet_get_int();
845 msg=packet_get_string(NULL);
846 lang=packet_get_string(NULL);
847
848 packet_check_eom();
849
Damien Miller15d72a02005-11-05 15:07:33 +1100850 debug("Server GSSAPI Error:\n%s", msg);
Darren Tucker0efd1552003-08-26 11:49:55 +1000851 xfree(msg);
852 xfree(lang);
853}
854#endif /* GSSAPI */
855
Damien Millereba71ba2000-04-29 23:57:08 +1000856int
Damien Miller874d77b2000-10-14 16:23:11 +1100857userauth_none(Authctxt *authctxt)
858{
859 /* initial userauth request */
860 packet_start(SSH2_MSG_USERAUTH_REQUEST);
861 packet_put_cstring(authctxt->server_user);
862 packet_put_cstring(authctxt->service);
863 packet_put_cstring(authctxt->method->name);
864 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100865 return 1;
866}
867
868int
Damien Miller62cee002000-09-23 17:15:56 +1100869userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000870{
Damien Millere247cc42000-05-07 12:03:14 +1000871 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000872 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000873 char *password;
Darren Tuckerab791692010-01-08 18:48:02 +1100874 const char *host = options.host_key_alias ? options.host_key_alias :
875 authctxt->host;
Damien Millereba71ba2000-04-29 23:57:08 +1000876
Damien Millerd3a18572000-06-07 19:55:44 +1000877 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000878 return 0;
879
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000880 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000881 error("Permission denied, please try again.");
882
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000883 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100884 authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +1000885 password = read_passphrase(prompt, 0);
886 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100887 packet_put_cstring(authctxt->server_user);
888 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100889 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000890 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000891 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000892 memset(password, 0, strlen(password));
893 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100894 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000895 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000896
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000897 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000898 &input_userauth_passwd_changereq);
899
Damien Millereba71ba2000-04-29 23:57:08 +1000900 return 1;
901}
Damien Millerf7475d72008-11-03 19:26:18 +1100902
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000903/*
904 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
905 */
Damien Millerf7475d72008-11-03 19:26:18 +1100906/* ARGSUSED */
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000907void
Tim Ricec8549622002-03-31 12:49:38 -0800908input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000909{
910 Authctxt *authctxt = ctxt;
911 char *info, *lang, *password = NULL, *retype = NULL;
912 char prompt[150];
Darren Tuckerab791692010-01-08 18:48:02 +1100913 const char *host = options.host_key_alias ? options.host_key_alias :
914 authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000915
916 debug2("input_userauth_passwd_changereq");
917
918 if (authctxt == NULL)
919 fatal("input_userauth_passwd_changereq: "
920 "no authentication context");
921
922 info = packet_get_string(NULL);
923 lang = packet_get_string(NULL);
924 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000925 logit("%s", info);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000926 xfree(info);
927 xfree(lang);
928 packet_start(SSH2_MSG_USERAUTH_REQUEST);
929 packet_put_cstring(authctxt->server_user);
930 packet_put_cstring(authctxt->service);
931 packet_put_cstring(authctxt->method->name);
932 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000933 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000934 "Enter %.30s@%.128s's old password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100935 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000936 password = read_passphrase(prompt, 0);
937 packet_put_cstring(password);
938 memset(password, 0, strlen(password));
939 xfree(password);
940 password = NULL;
941 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000942 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000943 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100944 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000945 password = read_passphrase(prompt, RP_ALLOW_EOF);
946 if (password == NULL) {
947 /* bail out */
948 return;
949 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000950 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000951 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100952 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000953 retype = read_passphrase(prompt, 0);
954 if (strcmp(password, retype) != 0) {
955 memset(password, 0, strlen(password));
956 xfree(password);
Damien Miller996acd22003-04-09 20:59:48 +1000957 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000958 password = NULL;
959 }
960 memset(retype, 0, strlen(retype));
961 xfree(retype);
962 }
963 packet_put_cstring(password);
964 memset(password, 0, strlen(password));
965 xfree(password);
966 packet_add_padding(64);
967 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000968
969 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000970 &input_userauth_passwd_changereq);
971}
Damien Millereba71ba2000-04-29 23:57:08 +1000972
Damien Miller01ed2272008-11-05 16:20:46 +1100973#ifdef JPAKE
974static char *
975pw_encrypt(const char *password, const char *crypt_scheme, const char *salt)
976{
977 /* OpenBSD crypt(3) handles all of these */
978 if (strcmp(crypt_scheme, "crypt") == 0 ||
979 strcmp(crypt_scheme, "bcrypt") == 0 ||
980 strcmp(crypt_scheme, "md5crypt") == 0 ||
981 strcmp(crypt_scheme, "crypt-extended") == 0)
982 return xstrdup(crypt(password, salt));
983 error("%s: unsupported password encryption scheme \"%.100s\"",
984 __func__, crypt_scheme);
985 return NULL;
986}
987
988static BIGNUM *
989jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme,
990 const char *salt)
991{
992 char prompt[256], *password, *crypted;
993 u_char *secret;
994 u_int secret_len;
995 BIGNUM *ret;
996
997 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ",
998 authctxt->server_user, authctxt->host);
999 password = read_passphrase(prompt, 0);
1000
1001 if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) {
1002 logit("Disabling %s authentication", authctxt->method->name);
1003 authctxt->method->enabled = NULL;
1004 /* Continue with an empty password to fail gracefully */
1005 crypted = xstrdup("");
1006 }
1007
1008#ifdef JPAKE_DEBUG
1009 debug3("%s: salt = %s", __func__, salt);
1010 debug3("%s: scheme = %s", __func__, crypt_scheme);
1011 debug3("%s: crypted = %s", __func__, crypted);
1012#endif
1013
1014 if (hash_buffer(crypted, strlen(crypted), EVP_sha256(),
1015 &secret, &secret_len) != 0)
1016 fatal("%s: hash_buffer", __func__);
1017
1018 bzero(password, strlen(password));
1019 bzero(crypted, strlen(crypted));
1020 xfree(password);
1021 xfree(crypted);
1022
1023 if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL)
1024 fatal("%s: BN_bin2bn (secret)", __func__);
1025 bzero(secret, secret_len);
1026 xfree(secret);
1027
1028 return ret;
1029}
1030
1031/* ARGSUSED */
1032void
1033input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt)
1034{
1035 Authctxt *authctxt = ctxt;
1036 struct jpake_ctx *pctx = authctxt->methoddata;
1037 u_char *x3_proof, *x4_proof, *x2_s_proof;
1038 u_int x3_proof_len, x4_proof_len, x2_s_proof_len;
1039 char *crypt_scheme, *salt;
1040
1041 /* Disable this message */
1042 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL);
1043
1044 if ((pctx->g_x3 = BN_new()) == NULL ||
1045 (pctx->g_x4 = BN_new()) == NULL)
1046 fatal("%s: BN_new", __func__);
1047
1048 /* Fetch step 1 values */
1049 crypt_scheme = packet_get_string(NULL);
1050 salt = packet_get_string(NULL);
1051 pctx->server_id = packet_get_string(&pctx->server_id_len);
1052 packet_get_bignum2(pctx->g_x3);
1053 packet_get_bignum2(pctx->g_x4);
1054 x3_proof = packet_get_string(&x3_proof_len);
1055 x4_proof = packet_get_string(&x4_proof_len);
1056 packet_check_eom();
1057
1058 JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__));
1059
1060 /* Obtain password and derive secret */
1061 pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt);
1062 bzero(crypt_scheme, strlen(crypt_scheme));
1063 bzero(salt, strlen(salt));
1064 xfree(crypt_scheme);
1065 xfree(salt);
1066 JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__));
1067
1068 /* Calculate step 2 values */
1069 jpake_step2(pctx->grp, pctx->s, pctx->g_x1,
1070 pctx->g_x3, pctx->g_x4, pctx->x2,
1071 pctx->server_id, pctx->server_id_len,
1072 pctx->client_id, pctx->client_id_len,
1073 x3_proof, x3_proof_len,
1074 x4_proof, x4_proof_len,
1075 &pctx->a,
1076 &x2_s_proof, &x2_s_proof_len);
1077
1078 bzero(x3_proof, x3_proof_len);
1079 bzero(x4_proof, x4_proof_len);
1080 xfree(x3_proof);
1081 xfree(x4_proof);
1082
1083 JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__));
1084
1085 /* Send values for step 2 */
1086 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2);
1087 packet_put_bignum2(pctx->a);
1088 packet_put_string(x2_s_proof, x2_s_proof_len);
1089 packet_send();
1090
1091 bzero(x2_s_proof, x2_s_proof_len);
1092 xfree(x2_s_proof);
1093
1094 /* Expect step 2 packet from peer */
1095 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2,
1096 input_userauth_jpake_server_step2);
1097}
1098
1099/* ARGSUSED */
1100void
1101input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt)
1102{
1103 Authctxt *authctxt = ctxt;
1104 struct jpake_ctx *pctx = authctxt->methoddata;
1105 u_char *x4_s_proof;
1106 u_int x4_s_proof_len;
1107
1108 /* Disable this message */
1109 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL);
1110
1111 if ((pctx->b = BN_new()) == NULL)
1112 fatal("%s: BN_new", __func__);
1113
1114 /* Fetch step 2 values */
1115 packet_get_bignum2(pctx->b);
1116 x4_s_proof = packet_get_string(&x4_s_proof_len);
1117 packet_check_eom();
1118
1119 JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__));
1120
1121 /* Derive shared key and calculate confirmation hash */
1122 jpake_key_confirm(pctx->grp, pctx->s, pctx->b,
1123 pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4,
1124 pctx->client_id, pctx->client_id_len,
1125 pctx->server_id, pctx->server_id_len,
1126 session_id2, session_id2_len,
1127 x4_s_proof, x4_s_proof_len,
1128 &pctx->k,
1129 &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len);
1130
1131 bzero(x4_s_proof, x4_s_proof_len);
1132 xfree(x4_s_proof);
1133
1134 JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__));
1135
1136 /* Send key confirmation proof */
1137 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM);
1138 packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
1139 packet_send();
1140
1141 /* Expect confirmation from peer */
1142 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM,
1143 input_userauth_jpake_server_confirm);
1144}
1145
1146/* ARGSUSED */
1147void
1148input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt)
1149{
1150 Authctxt *authctxt = ctxt;
1151 struct jpake_ctx *pctx = authctxt->methoddata;
1152
1153 /* Disable this message */
1154 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL);
1155
1156 pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len);
1157 packet_check_eom();
1158
1159 JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__));
1160
1161 /* Verify expected confirmation hash */
1162 if (jpake_check_confirm(pctx->k,
1163 pctx->server_id, pctx->server_id_len,
1164 session_id2, session_id2_len,
1165 pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1)
1166 debug("%s: %s success", __func__, authctxt->method->name);
1167 else {
1168 debug("%s: confirmation mismatch", __func__);
1169 /* XXX stash this so if auth succeeds then we can warn/kill */
1170 }
1171
1172 userauth_jpake_cleanup(authctxt);
1173}
1174#endif /* JPAKE */
1175
Ben Lindstrombba81212001-06-25 05:01:22 +00001176static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001177identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1178 u_char *data, u_int datalen)
1179{
1180 Key *prv;
1181 int ret;
1182
1183 /* the agent supports this key */
1184 if (id->ac)
1185 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1186 data, datalen));
1187 /*
1188 * we have already loaded the private key or
1189 * the private key is stored in external hardware
1190 */
1191 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1192 return (key_sign(id->key, sigp, lenp, data, datalen));
1193 /* load the private key from the file */
Damien Miller5ceddc32013-02-15 12:18:32 +11001194 if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL)
Damien Miller280ecfb2003-05-14 13:46:00 +10001195 return (-1);
1196 ret = key_sign(prv, sigp, lenp, data, datalen);
1197 key_free(prv);
1198 return (ret);
1199}
1200
1201static int
1202sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +10001203{
1204 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001205 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +00001206 u_int bloblen, slen;
Darren Tucker502d3842003-06-28 12:38:01 +10001207 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001208 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001209 int have_sig = 1;
Damien Miller4e270b02010-04-16 15:56:21 +10001210 char *fp;
Damien Millereba71ba2000-04-29 23:57:08 +10001211
Damien Miller4e270b02010-04-16 15:56:21 +10001212 fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
1213 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
1214 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001215
Damien Miller280ecfb2003-05-14 13:46:00 +10001216 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001217 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +00001218 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001219 return 0;
1220 }
Damien Millereba71ba2000-04-29 23:57:08 +10001221 /* data to be signed */
1222 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +11001223 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +10001224 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001225 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +11001226 } else {
1227 buffer_put_string(&b, session_id2, session_id2_len);
1228 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +10001229 }
Damien Millereba71ba2000-04-29 23:57:08 +10001230 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001231 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +10001232 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +00001233 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +10001234 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +11001235 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +00001236 if (datafellows & SSH_BUG_PKAUTH) {
1237 buffer_put_char(&b, have_sig);
1238 } else {
1239 buffer_put_cstring(&b, authctxt->method->name);
1240 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +10001241 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +00001242 }
Damien Millereba71ba2000-04-29 23:57:08 +10001243 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +10001244
1245 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +10001246 ret = identity_sign(id, &signature, &slen,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001247 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +10001248 if (ret == -1) {
1249 xfree(blob);
1250 buffer_free(&b);
1251 return 0;
1252 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001253#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +10001254 buffer_dump(&b);
1255#endif
Ben Lindstromd121f612000-12-03 17:00:47 +00001256 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +10001257 buffer_clear(&b);
1258 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001259 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +10001260 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001261 buffer_put_cstring(&b, authctxt->server_user);
1262 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +11001263 buffer_put_cstring(&b, authctxt->method->name);
1264 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +00001265 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001266 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +10001267 buffer_put_string(&b, blob, bloblen);
1268 }
1269 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001270
Damien Millereba71ba2000-04-29 23:57:08 +10001271 /* append signature */
1272 buffer_put_string(&b, signature, slen);
1273 xfree(signature);
1274
1275 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +10001276 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +11001277 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +10001278 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +10001279
1280 /* put remaining data from buffer into packet */
1281 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1282 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1283 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +10001284 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +10001285
1286 return 1;
Damien Miller994cf142000-07-21 10:19:44 +10001287}
1288
Ben Lindstrombba81212001-06-25 05:01:22 +00001289static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001290send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001291{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001292 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001293 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +10001294
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001295 debug3("send_pubkey_test");
1296
Damien Miller280ecfb2003-05-14 13:46:00 +10001297 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001298 /* we cannot handle this key */
1299 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +10001300 return 0;
1301 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001302 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001303 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001304
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001305 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1306 packet_put_cstring(authctxt->server_user);
1307 packet_put_cstring(authctxt->service);
1308 packet_put_cstring(authctxt->method->name);
1309 packet_put_char(have_sig);
1310 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001311 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001312 packet_put_string(blob, bloblen);
1313 xfree(blob);
1314 packet_send();
1315 return 1;
1316}
1317
Ben Lindstrombba81212001-06-25 05:01:22 +00001318static Key *
Damien Miller5ceddc32013-02-15 12:18:32 +11001319load_identity_file(char *filename, int userprovided)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001320{
1321 Key *private;
1322 char prompt[300], *passphrase;
Darren Tuckerd4c86b12010-01-12 19:41:22 +11001323 int perm_ok = 0, quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001324 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001325
Ben Lindstrom329782e2001-03-10 17:08:59 +00001326 if (stat(filename, &st) < 0) {
Damien Miller5ceddc32013-02-15 12:18:32 +11001327 (userprovided ? logit : debug3)("no such identity: %s: %s",
1328 filename, strerror(errno));
Ben Lindstrom329782e2001-03-10 17:08:59 +00001329 return NULL;
1330 }
Darren Tucker232b76f2006-05-06 17:41:51 +10001331 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1332 if (!perm_ok)
1333 return NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +00001334 if (private == NULL) {
1335 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001336 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +10001337 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +11001338 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +11001339 for (i = 0; i < options.number_of_password_prompts; i++) {
1340 passphrase = read_passphrase(prompt, 0);
1341 if (strcmp(passphrase, "") != 0) {
Darren Tucker232b76f2006-05-06 17:41:51 +10001342 private = key_load_private_type(KEY_UNSPEC,
1343 filename, passphrase, NULL, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001344 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +11001345 } else {
1346 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001347 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +11001348 }
1349 memset(passphrase, 0, strlen(passphrase));
1350 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001351 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +11001352 break;
1353 debug2("bad passphrase given, try again...");
1354 }
Damien Miller994cf142000-07-21 10:19:44 +10001355 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001356 return private;
1357}
1358
Damien Miller280ecfb2003-05-14 13:46:00 +10001359/*
1360 * try keys in the following order:
1361 * 1. agent keys that are found in the config file
1362 * 2. other agent keys
1363 * 3. keys that are only listed in the config file
1364 */
1365static void
1366pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001367{
Damien Millercb6b68b2012-12-03 09:49:52 +11001368 Identity *id, *id2, *tmp;
Damien Miller280ecfb2003-05-14 13:46:00 +10001369 Idlist agent, files, *preferred;
1370 Key *key;
1371 AuthenticationConnection *ac;
Damien Millerad833b32000-08-23 10:46:23 +10001372 char *comment;
Damien Miller280ecfb2003-05-14 13:46:00 +10001373 int i, found;
Damien Millerad833b32000-08-23 10:46:23 +10001374
Damien Miller280ecfb2003-05-14 13:46:00 +10001375 TAILQ_INIT(&agent); /* keys from the agent */
1376 TAILQ_INIT(&files); /* keys from the config file */
1377 preferred = &authctxt->keys;
1378 TAILQ_INIT(preferred); /* preferred order of keys */
1379
Damien Millercb6b68b2012-12-03 09:49:52 +11001380 /* list of keys stored in the filesystem and PKCS#11 */
Damien Miller280ecfb2003-05-14 13:46:00 +10001381 for (i = 0; i < options.num_identity_files; i++) {
1382 key = options.identity_keys[i];
1383 if (key && key->type == KEY_RSA1)
1384 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +11001385 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1386 continue;
Damien Miller280ecfb2003-05-14 13:46:00 +10001387 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001388 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001389 id->key = key;
1390 id->filename = xstrdup(options.identity_files[i]);
Darren Tucker19104782013-04-05 11:13:08 +11001391 id->userprovided = options.identity_file_userprovided[i];
Damien Miller280ecfb2003-05-14 13:46:00 +10001392 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001393 }
Damien Millercb6b68b2012-12-03 09:49:52 +11001394 /* Prefer PKCS11 keys that are explicitly listed */
1395 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1396 if (id->key == NULL || (id->key->flags & KEY_FLAG_EXT) == 0)
1397 continue;
1398 found = 0;
1399 TAILQ_FOREACH(id2, &files, next) {
1400 if (id2->key == NULL ||
1401 (id2->key->flags & KEY_FLAG_EXT) != 0)
1402 continue;
1403 if (key_equal(id->key, id2->key)) {
1404 TAILQ_REMOVE(&files, id, next);
1405 TAILQ_INSERT_TAIL(preferred, id, next);
1406 found = 1;
1407 break;
1408 }
1409 }
1410 /* If IdentitiesOnly set and key not found then don't use it */
1411 if (!found && options.identities_only) {
1412 TAILQ_REMOVE(&files, id, next);
1413 bzero(id, sizeof(id));
1414 free(id);
1415 }
1416 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001417 /* list of keys supported by the agent */
1418 if ((ac = ssh_get_authentication_connection())) {
1419 for (key = ssh_get_first_identity(ac, &comment, 2);
1420 key != NULL;
1421 key = ssh_get_next_identity(ac, &comment, 2)) {
1422 found = 0;
1423 TAILQ_FOREACH(id, &files, next) {
Damien Millera8e06ce2003-11-21 23:48:55 +11001424 /* agent keys from the config file are preferred */
Damien Miller280ecfb2003-05-14 13:46:00 +10001425 if (key_equal(key, id->key)) {
1426 key_free(key);
1427 xfree(comment);
1428 TAILQ_REMOVE(&files, id, next);
1429 TAILQ_INSERT_TAIL(preferred, id, next);
1430 id->ac = ac;
1431 found = 1;
1432 break;
1433 }
1434 }
Damien Millerbd394c32004-03-08 23:12:36 +11001435 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001436 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001437 id->key = key;
1438 id->filename = comment;
1439 id->ac = ac;
1440 TAILQ_INSERT_TAIL(&agent, id, next);
1441 }
1442 }
1443 /* append remaining agent keys */
1444 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1445 TAILQ_REMOVE(&agent, id, next);
1446 TAILQ_INSERT_TAIL(preferred, id, next);
1447 }
1448 authctxt->agent = ac;
Damien Miller62cee002000-09-23 17:15:56 +11001449 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001450 /* append remaining keys from the config file */
1451 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1452 TAILQ_REMOVE(&files, id, next);
1453 TAILQ_INSERT_TAIL(preferred, id, next);
1454 }
1455 TAILQ_FOREACH(id, preferred, next) {
Damien Miller5ceddc32013-02-15 12:18:32 +11001456 debug2("key: %s (%p),%s", id->filename, id->key,
1457 id->userprovided ? " explicit" : "");
Damien Miller280ecfb2003-05-14 13:46:00 +10001458 }
1459}
1460
1461static void
1462pubkey_cleanup(Authctxt *authctxt)
1463{
1464 Identity *id;
1465
1466 if (authctxt->agent != NULL)
1467 ssh_close_authentication_connection(authctxt->agent);
1468 for (id = TAILQ_FIRST(&authctxt->keys); id;
1469 id = TAILQ_FIRST(&authctxt->keys)) {
1470 TAILQ_REMOVE(&authctxt->keys, id, next);
1471 if (id->key)
1472 key_free(id->key);
1473 if (id->filename)
1474 xfree(id->filename);
1475 xfree(id);
1476 }
Damien Millereba71ba2000-04-29 23:57:08 +10001477}
1478
Damien Miller62cee002000-09-23 17:15:56 +11001479int
1480userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001481{
Damien Miller280ecfb2003-05-14 13:46:00 +10001482 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001483 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001484
Damien Miller280ecfb2003-05-14 13:46:00 +10001485 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1486 if (id->tried++)
1487 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001488 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001489 TAILQ_REMOVE(&authctxt->keys, id, next);
1490 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1491 /*
1492 * send a test message if we have the public key. for
1493 * encrypted keys we cannot do this and have to load the
1494 * private key instead
1495 */
1496 if (id->key && id->key->type != KEY_RSA1) {
Damien Miller88680652010-04-16 15:53:43 +10001497 debug("Offering %s public key: %s", key_type(id->key),
1498 id->filename);
Damien Miller280ecfb2003-05-14 13:46:00 +10001499 sent = send_pubkey_test(authctxt, id);
1500 } else if (id->key == NULL) {
1501 debug("Trying private key: %s", id->filename);
Damien Miller5ceddc32013-02-15 12:18:32 +11001502 id->key = load_identity_file(id->filename,
1503 id->userprovided);
Damien Miller280ecfb2003-05-14 13:46:00 +10001504 if (id->key != NULL) {
1505 id->isprivate = 1;
1506 sent = sign_and_send_pubkey(authctxt, id);
1507 key_free(id->key);
1508 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001509 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001510 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001511 if (sent)
1512 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001513 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001514 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001515}
Damien Millereba71ba2000-04-29 23:57:08 +10001516
Damien Miller874d77b2000-10-14 16:23:11 +11001517/*
1518 * Send userauth request message specifying keyboard-interactive method.
1519 */
1520int
1521userauth_kbdint(Authctxt *authctxt)
1522{
1523 static int attempt = 0;
1524
1525 if (attempt++ >= options.number_of_password_prompts)
1526 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001527 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1528 if (attempt > 1 && !authctxt->info_req_seen) {
1529 debug3("userauth_kbdint: disable: no info_req_seen");
1530 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1531 return 0;
1532 }
Damien Miller874d77b2000-10-14 16:23:11 +11001533
1534 debug2("userauth_kbdint");
1535 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1536 packet_put_cstring(authctxt->server_user);
1537 packet_put_cstring(authctxt->service);
1538 packet_put_cstring(authctxt->method->name);
1539 packet_put_cstring(""); /* lang */
1540 packet_put_cstring(options.kbd_interactive_devices ?
1541 options.kbd_interactive_devices : "");
1542 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001543
1544 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1545 return 1;
1546}
1547
1548/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001549 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001550 */
1551void
Damien Miller630d6f42002-01-22 23:17:30 +11001552input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001553{
1554 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001555 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001556 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001557 int echo = 0;
1558
1559 debug2("input_userauth_info_req");
1560
1561 if (authctxt == NULL)
1562 fatal("input_userauth_info_req: no authentication context");
1563
Ben Lindstrom7d199962001-09-12 18:29:00 +00001564 authctxt->info_req_seen = 1;
1565
Damien Miller874d77b2000-10-14 16:23:11 +11001566 name = packet_get_string(NULL);
1567 inst = packet_get_string(NULL);
1568 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001569 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001570 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001571 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001572 logit("%s", inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001573 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +11001574 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001575 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001576
1577 num_prompts = packet_get_int();
1578 /*
1579 * Begin to build info response packet based on prompts requested.
1580 * We commit to providing the correct number of responses, so if
1581 * further on we run into a problem that prevents this, we have to
1582 * be sure and clean this up and send a correct error response.
1583 */
1584 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1585 packet_put_int(num_prompts);
1586
Ben Lindstrom551ea372001-06-05 18:56:16 +00001587 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001588 for (i = 0; i < num_prompts; i++) {
1589 prompt = packet_get_string(NULL);
1590 echo = packet_get_char();
1591
Ben Lindstrom949974b2001-06-25 05:20:31 +00001592 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001593
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001594 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +11001595 memset(response, 0, strlen(response));
1596 xfree(response);
1597 xfree(prompt);
1598 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001599 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001600
Damien Miller9f643902001-11-12 11:02:52 +11001601 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001602 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001603}
Damien Miller62cee002000-09-23 17:15:56 +11001604
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001605static int
Ben Lindstrom5c385522002-06-23 21:23:20 +00001606ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001607 u_char *data, u_int datalen)
1608{
1609 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001610 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001611 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001612 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001613
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001614 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001615
Ben Lindstrom5206b952002-06-06 19:59:29 +00001616 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001617 error("ssh_keysign: not installed: %s", strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001618 return -1;
1619 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001620 if (fflush(stdout) != 0)
1621 error("ssh_keysign: fflush: %s", strerror(errno));
1622 if (pipe(to) < 0) {
1623 error("ssh_keysign: pipe: %s", strerror(errno));
1624 return -1;
1625 }
1626 if (pipe(from) < 0) {
1627 error("ssh_keysign: pipe: %s", strerror(errno));
1628 return -1;
1629 }
1630 if ((pid = fork()) < 0) {
1631 error("ssh_keysign: fork: %s", strerror(errno));
1632 return -1;
1633 }
1634 if (pid == 0) {
Darren Tucker6e7fe1c2010-01-08 17:07:22 +11001635 /* keep the socket on exec */
1636 fcntl(packet_get_connection_in(), F_SETFD, 0);
Damien Miller2e5fe882006-06-13 13:10:00 +10001637 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001638 close(from[0]);
1639 if (dup2(from[1], STDOUT_FILENO) < 0)
1640 fatal("ssh_keysign: dup2: %s", strerror(errno));
1641 close(to[1]);
1642 if (dup2(to[0], STDIN_FILENO) < 0)
1643 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001644 close(from[1]);
1645 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001646 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001647 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1648 strerror(errno));
1649 }
1650 close(from[1]);
1651 close(to[0]);
1652
1653 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001654 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001655 buffer_put_string(&b, data, datalen);
Damien Miller51bf11f2003-11-17 21:20:47 +11001656 if (ssh_msg_send(to[1], version, &b) == -1)
1657 fatal("ssh_keysign: couldn't send request");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001658
Damien Miller901119b2002-10-04 11:10:04 +10001659 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001660 error("ssh_keysign: no reply");
Damien Millerfb1310e2004-01-21 11:02:50 +11001661 buffer_free(&b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001662 return -1;
1663 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001664 close(from[0]);
1665 close(to[1]);
1666
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001667 while (waitpid(pid, &status, 0) < 0)
1668 if (errno != EINTR)
1669 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001670
Ben Lindstrom5206b952002-06-06 19:59:29 +00001671 if (buffer_get_char(&b) != version) {
1672 error("ssh_keysign: bad version");
Damien Millerfb1310e2004-01-21 11:02:50 +11001673 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001674 return -1;
1675 }
1676 *sigp = buffer_get_string(&b, lenp);
Damien Millerfb1310e2004-01-21 11:02:50 +11001677 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001678
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001679 return 0;
1680}
1681
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001682int
1683userauth_hostbased(Authctxt *authctxt)
1684{
1685 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001686 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001687 Buffer b;
1688 u_char *signature, *blob;
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001689 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001690 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001691 u_int blen, slen;
Darren Tucker5246df42010-01-08 18:50:46 +11001692 int ok, i, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001693
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001694 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001695 for (i = 0; i < sensitive->nkeys; i++) {
1696 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001697 if (private && private->type != KEY_RSA1) {
1698 found = 1;
1699 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001700 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001701 break;
1702 }
1703 }
1704 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001705 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001706 return 0;
1707 }
1708 if (key_to_blob(private, &blob, &blen) == 0) {
1709 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001710 return 0;
1711 }
Damien Miller91c18472001-11-12 11:02:03 +11001712 /* figure out a name for the client host */
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001713 p = get_local_name(packet_get_connection_in());
Damien Miller91c18472001-11-12 11:02:03 +11001714 if (p == NULL) {
1715 error("userauth_hostbased: cannot get local ipaddr/name");
1716 key_free(private);
Damien Miller5790b592006-03-26 13:54:03 +11001717 xfree(blob);
Damien Miller91c18472001-11-12 11:02:03 +11001718 return 0;
1719 }
Damien Miller07d86be2006-03-26 14:19:21 +11001720 xasprintf(&chost, "%s.", p);
Damien Miller91c18472001-11-12 11:02:03 +11001721 debug2("userauth_hostbased: chost %s", chost);
Damien Miller00111382003-03-10 11:21:17 +11001722 xfree(p);
Damien Miller91c18472001-11-12 11:02:03 +11001723
Ben Lindstrom671388f2001-04-19 20:40:45 +00001724 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1725 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001726 pkalg = xstrdup(key_ssh_name(private));
1727 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001728 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001729 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001730 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1731 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001732 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001733 buffer_put_cstring(&b, authctxt->method->name);
1734 buffer_put_cstring(&b, pkalg);
1735 buffer_put_string(&b, blob, blen);
1736 buffer_put_cstring(&b, chost);
1737 buffer_put_cstring(&b, authctxt->local_user);
1738#ifdef DEBUG_PK
1739 buffer_dump(&b);
1740#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001741 if (sensitive->external_keysign)
1742 ok = ssh_keysign(private, &signature, &slen,
1743 buffer_ptr(&b), buffer_len(&b));
1744 else
1745 ok = key_sign(private, &signature, &slen,
1746 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001747 key_free(private);
1748 buffer_free(&b);
1749 if (ok != 0) {
1750 error("key_sign failed");
1751 xfree(chost);
1752 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001753 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001754 return 0;
1755 }
1756 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1757 packet_put_cstring(authctxt->server_user);
1758 packet_put_cstring(authctxt->service);
1759 packet_put_cstring(authctxt->method->name);
1760 packet_put_cstring(pkalg);
1761 packet_put_string(blob, blen);
1762 packet_put_cstring(chost);
1763 packet_put_cstring(authctxt->local_user);
1764 packet_put_string(signature, slen);
1765 memset(signature, 's', slen);
1766 xfree(signature);
1767 xfree(chost);
1768 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001769 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001770
1771 packet_send();
1772 return 1;
1773}
1774
Damien Miller01ed2272008-11-05 16:20:46 +11001775#ifdef JPAKE
1776int
1777userauth_jpake(Authctxt *authctxt)
1778{
1779 struct jpake_ctx *pctx;
1780 u_char *x1_proof, *x2_proof;
1781 u_int x1_proof_len, x2_proof_len;
1782 static int attempt = 0; /* XXX share with userauth_password's? */
1783
1784 if (attempt++ >= options.number_of_password_prompts)
1785 return 0;
1786 if (attempt != 1)
1787 error("Permission denied, please try again.");
1788
1789 if (authctxt->methoddata != NULL)
1790 fatal("%s: authctxt->methoddata already set (%p)",
1791 __func__, authctxt->methoddata);
1792
1793 authctxt->methoddata = pctx = jpake_new();
1794
1795 /*
1796 * Send request immediately, to get the protocol going while
1797 * we do the initial computations.
1798 */
1799 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1800 packet_put_cstring(authctxt->server_user);
1801 packet_put_cstring(authctxt->service);
1802 packet_put_cstring(authctxt->method->name);
1803 packet_send();
1804 packet_write_wait();
1805
1806 jpake_step1(pctx->grp,
1807 &pctx->client_id, &pctx->client_id_len,
1808 &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2,
1809 &x1_proof, &x1_proof_len,
1810 &x2_proof, &x2_proof_len);
1811
1812 JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__));
1813
1814 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1);
1815 packet_put_string(pctx->client_id, pctx->client_id_len);
1816 packet_put_bignum2(pctx->g_x1);
1817 packet_put_bignum2(pctx->g_x2);
1818 packet_put_string(x1_proof, x1_proof_len);
1819 packet_put_string(x2_proof, x2_proof_len);
1820 packet_send();
1821
1822 bzero(x1_proof, x1_proof_len);
1823 bzero(x2_proof, x2_proof_len);
1824 xfree(x1_proof);
1825 xfree(x2_proof);
1826
1827 /* Expect step 1 packet from peer */
1828 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1,
1829 input_userauth_jpake_server_step1);
Darren Tucker2f29a8c2009-10-24 11:47:58 +11001830 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS,
1831 &input_userauth_success_unexpected);
Damien Miller01ed2272008-11-05 16:20:46 +11001832
1833 return 1;
1834}
1835
1836void
1837userauth_jpake_cleanup(Authctxt *authctxt)
1838{
1839 debug3("%s: clean up", __func__);
1840 if (authctxt->methoddata != NULL) {
1841 jpake_free(authctxt->methoddata);
1842 authctxt->methoddata = NULL;
1843 }
Darren Tucker2f29a8c2009-10-24 11:47:58 +11001844 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
Damien Miller01ed2272008-11-05 16:20:46 +11001845}
1846#endif /* JPAKE */
1847
Damien Miller62cee002000-09-23 17:15:56 +11001848/* find auth method */
1849
Damien Miller62cee002000-09-23 17:15:56 +11001850/*
1851 * given auth method name, if configurable options permit this method fill
1852 * in auth_ident field and return true, otherwise return false.
1853 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001854static int
Damien Miller62cee002000-09-23 17:15:56 +11001855authmethod_is_enabled(Authmethod *method)
1856{
1857 if (method == NULL)
1858 return 0;
1859 /* return false if options indicate this method is disabled */
1860 if (method->enabled == NULL || *method->enabled == 0)
1861 return 0;
1862 /* return false if batch mode is enabled but method needs interactive mode */
1863 if (method->batch_flag != NULL && *method->batch_flag != 0)
1864 return 0;
1865 return 1;
1866}
1867
Ben Lindstrombba81212001-06-25 05:01:22 +00001868static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001869authmethod_lookup(const char *name)
1870{
1871 Authmethod *method = NULL;
1872 if (name != NULL)
1873 for (method = authmethods; method->name != NULL; method++)
1874 if (strcmp(name, method->name) == 0)
1875 return method;
1876 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1877 return NULL;
1878}
1879
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001880/* XXX internal state */
1881static Authmethod *current = NULL;
1882static char *supported = NULL;
1883static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001884
Damien Miller62cee002000-09-23 17:15:56 +11001885/*
1886 * Given the authentication method list sent by the server, return the
1887 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001888 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001889 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001890static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001891authmethod_get(char *authlist)
1892{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001893 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001894 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001895
Damien Miller62cee002000-09-23 17:15:56 +11001896 /* Use a suitable default if we're passed a nil list. */
1897 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001898 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001899
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001900 if (supported == NULL || strcmp(authlist, supported) != 0) {
1901 debug3("start over, passed a different list %s", authlist);
1902 if (supported != NULL)
1903 xfree(supported);
1904 supported = xstrdup(authlist);
1905 preferred = options.preferred_authentications;
1906 debug3("preferred %s", preferred);
1907 current = NULL;
1908 } else if (current != NULL && authmethod_is_enabled(current))
1909 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001910
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001911 for (;;) {
1912 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001913 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001914 current = NULL;
1915 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001916 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001917 preferred += next;
1918 debug3("authmethod_lookup %s", name);
1919 debug3("remaining preferred: %s", preferred);
1920 if ((current = authmethod_lookup(name)) != NULL &&
1921 authmethod_is_enabled(current)) {
1922 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001923 debug("Next authentication method: %s", name);
Damien Miller78c40c32011-05-15 08:36:59 +10001924 xfree(name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001925 return current;
1926 }
Damien Millereba71ba2000-04-29 23:57:08 +10001927 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001928}
Damien Miller62cee002000-09-23 17:15:56 +11001929
Ben Lindstrom79073822001-07-04 03:42:30 +00001930static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001931authmethods_get(void)
1932{
1933 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001934 Buffer b;
1935 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001936
Damien Miller0e3b8722002-01-22 23:26:38 +11001937 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001938 for (method = authmethods; method->name != NULL; method++) {
1939 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001940 if (buffer_len(&b) > 0)
1941 buffer_append(&b, ",", 1);
1942 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001943 }
Damien Miller62cee002000-09-23 17:15:56 +11001944 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001945 buffer_append(&b, "\0", 1);
1946 list = xstrdup(buffer_ptr(&b));
1947 buffer_free(&b);
1948 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001949}
Damien Miller01ed2272008-11-05 16:20:46 +11001950