blob: 3cb9b101cb88905279c3ff6f1f9e83fd371d821a [file] [log] [blame]
Damien Millerd925dcd2010-12-01 12:21:51 +11001/* $OpenBSD: sshconnect2.c,v 1.186 2010/11/29 23:45:51 djm 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 Miller2e28d862008-07-17 19:15:43 +100043#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
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;
112
113 /* Find all hostkeys for this hostname */
114 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
115 hostkeys = init_hostkeys();
116 load_hostkeys(hostkeys, hostname, options.user_hostfile2);
117 load_hostkeys(hostkeys, hostname, options.system_hostfile2);
118 load_hostkeys(hostkeys, hostname, options.user_hostfile);
119 load_hostkeys(hostkeys, hostname, options.system_hostfile);
120
121 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
122 maxlen = strlen(avail) + 1;
123 first = xmalloc(maxlen);
124 last = xmalloc(maxlen);
125 *first = *last = '\0';
126
127#define ALG_APPEND(to, from) \
128 do { \
129 if (*to != '\0') \
130 strlcat(to, ",", maxlen); \
131 strlcat(to, from, maxlen); \
132 } while (0)
133
134 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
135 if ((ktype = key_type_from_name(alg)) == KEY_UNSPEC)
136 fatal("%s: unknown alg %s", __func__, alg);
137 if (lookup_key_in_hostkeys_by_type(hostkeys,
138 key_type_plain(ktype), NULL))
139 ALG_APPEND(first, alg);
140 else
141 ALG_APPEND(last, alg);
142 }
143#undef ALG_APPEND
144 xasprintf(&ret, "%s%s%s", first, *first == '\0' ? "" : ",", last);
145 if (*first != '\0')
146 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
147
148 xfree(first);
149 xfree(last);
150 xfree(hostname);
151 xfree(oavail);
152 free_hostkeys(hostkeys);
153
154 return ret;
155}
156
Damien Millereba71ba2000-04-29 23:57:08 +1000157void
Damien Millerd925dcd2010-12-01 12:21:51 +1100158ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
Damien Miller874d77b2000-10-14 16:23:11 +1100159{
Damien Miller874d77b2000-10-14 16:23:11 +1100160 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000161
162 xxx_host = host;
163 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100164
Damien Millere39cacc2000-11-29 12:18:44 +1100165 if (options.ciphers == (char *)-1) {
Damien Miller996acd22003-04-09 20:59:48 +1000166 logit("No valid ciphers for protocol version 2 given, using defaults.");
Damien Millere39cacc2000-11-29 12:18:44 +1100167 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100168 }
169 if (options.ciphers != NULL) {
170 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
171 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
172 }
Damien Millera0ff4662001-03-30 10:49:35 +1000173 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
174 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
175 myproposal[PROPOSAL_ENC_ALGS_STOC] =
176 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100177 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000178 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000179 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100180 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000181 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000182 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100183 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000184 if (options.macs != NULL) {
185 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
186 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
187 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000188 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100189 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000190 options.hostkeyalgorithms;
Damien Millerd925dcd2010-12-01 12:21:51 +1100191 else {
192 /* Prefer algorithms that we already have keys for */
193 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
194 order_hostkeyalgs(host, hostaddr, port);
195 }
Damien Millerd5f62bf2010-09-24 22:11:14 +1000196 if (options.kex_algorithms != NULL)
197 myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
Damien Miller874d77b2000-10-14 16:23:11 +1100198
Damien Millera5539d22003-04-09 20:50:06 +1000199 if (options.rekey_limit)
Damien Miller3dff1762008-02-10 22:25:52 +1100200 packet_set_rekey_limit((u_int32_t)options.rekey_limit);
Damien Millera5539d22003-04-09 20:50:06 +1000201
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000202 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000203 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100204 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000205 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100206 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100207 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Damien Millereb8b60e2010-08-31 22:41:14 +1000208 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000209 kex->client_version_string=client_version_string;
210 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000211 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100212
Ben Lindstromf28f6342001-04-04 02:03:04 +0000213 xxx_kex = kex;
214
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000215 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100216
Darren Tucker36331b52010-01-08 16:50:41 +1100217 if (options.use_roaming && !kex->roaming) {
218 debug("Roaming not allowed by server");
219 options.use_roaming = 0;
220 }
221
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000222 session_id2 = kex->session_id;
223 session_id2_len = kex->session_id_len;
224
Damien Miller874d77b2000-10-14 16:23:11 +1100225#ifdef DEBUG_KEXDH
226 /* send 1st encrypted/maced/compressed message */
227 packet_start(SSH2_MSG_IGNORE);
228 packet_put_cstring("markus");
229 packet_send();
230 packet_write_wait();
231#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000232}
Damien Millerb1715dc2000-05-30 13:44:51 +1000233
Damien Millereba71ba2000-04-29 23:57:08 +1000234/*
235 * Authenticate user
236 */
Damien Miller62cee002000-09-23 17:15:56 +1100237
238typedef struct Authctxt Authctxt;
239typedef struct Authmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000240typedef struct identity Identity;
241typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100242
Damien Miller280ecfb2003-05-14 13:46:00 +1000243struct identity {
244 TAILQ_ENTRY(identity) next;
245 AuthenticationConnection *ac; /* set if agent supports key */
246 Key *key; /* public/private key */
247 char *filename; /* comment for agent-only keys */
248 int tried;
249 int isprivate; /* key points to the private key */
250};
251TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100252
253struct Authctxt {
254 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000255 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100256 const char *host;
257 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100258 Authmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000259 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000260 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000261 /* pubkey */
Damien Miller280ecfb2003-05-14 13:46:00 +1000262 Idlist keys;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000263 AuthenticationConnection *agent;
264 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000265 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000266 /* kbd-interactive */
267 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000268 /* generic */
269 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100270};
271struct Authmethod {
272 char *name; /* string to compare against server's list */
273 int (*userauth)(Authctxt *authctxt);
Damien Miller01ed2272008-11-05 16:20:46 +1100274 void (*cleanup)(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100275 int *enabled; /* flag in option struct that enables method */
276 int *batch_flag; /* flag in option struct that disables method */
277};
278
Damien Miller630d6f42002-01-22 23:17:30 +1100279void input_userauth_success(int, u_int32_t, void *);
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100280void input_userauth_success_unexpected(int, u_int32_t, void *);
Damien Miller630d6f42002-01-22 23:17:30 +1100281void input_userauth_failure(int, u_int32_t, void *);
282void input_userauth_banner(int, u_int32_t, void *);
283void input_userauth_error(int, u_int32_t, void *);
284void input_userauth_info_req(int, u_int32_t, void *);
285void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000286void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller01ed2272008-11-05 16:20:46 +1100287void input_userauth_jpake_server_step1(int, u_int32_t, void *);
288void input_userauth_jpake_server_step2(int, u_int32_t, void *);
289void input_userauth_jpake_server_confirm(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100290
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000291int userauth_none(Authctxt *);
292int userauth_pubkey(Authctxt *);
293int userauth_passwd(Authctxt *);
294int userauth_kbdint(Authctxt *);
295int userauth_hostbased(Authctxt *);
Damien Miller01ed2272008-11-05 16:20:46 +1100296int userauth_jpake(Authctxt *);
297
298void userauth_jpake_cleanup(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100299
Darren Tucker0efd1552003-08-26 11:49:55 +1000300#ifdef GSSAPI
301int userauth_gssapi(Authctxt *authctxt);
302void input_gssapi_response(int type, u_int32_t, void *);
303void input_gssapi_token(int type, u_int32_t, void *);
304void input_gssapi_hash(int type, u_int32_t, void *);
305void input_gssapi_error(int, u_int32_t, void *);
306void input_gssapi_errtok(int, u_int32_t, void *);
307#endif
308
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000309void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000310
Damien Miller280ecfb2003-05-14 13:46:00 +1000311static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000312static void pubkey_prepare(Authctxt *);
313static void pubkey_cleanup(Authctxt *);
314static Key *load_identity_file(char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000315
Ben Lindstrombba81212001-06-25 05:01:22 +0000316static Authmethod *authmethod_get(char *authlist);
317static Authmethod *authmethod_lookup(const char *name);
318static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100319
320Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000321#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100322 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000323 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100324 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000325 &options.gss_authentication,
326 NULL},
327#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000328 {"hostbased",
329 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100330 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000331 &options.hostbased_authentication,
332 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000333 {"publickey",
334 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100335 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000336 &options.pubkey_authentication,
337 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100338#ifdef JPAKE
339 {"jpake-01@openssh.com",
340 userauth_jpake,
341 userauth_jpake_cleanup,
342 &options.zero_knowledge_password_authentication,
343 &options.batch_mode},
344#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100345 {"keyboard-interactive",
346 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100347 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100348 &options.kbd_interactive_authentication,
349 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000350 {"password",
351 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100352 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000353 &options.password_authentication,
354 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100355 {"none",
356 userauth_none,
357 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100358 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100359 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100360 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100361};
362
363void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000364ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000365 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100366{
367 Authctxt authctxt;
368 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100369
Ben Lindstrom551ea372001-06-05 18:56:16 +0000370 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000371 options.kbd_interactive_authentication = 1;
372
Damien Miller62cee002000-09-23 17:15:56 +1100373 packet_start(SSH2_MSG_SERVICE_REQUEST);
374 packet_put_cstring("ssh-userauth");
375 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000376 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100377 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100378 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000379 if (type != SSH2_MSG_SERVICE_ACCEPT)
380 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100381 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100382 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000383 debug2("service_accept: %s", reply);
Damien Miller62cee002000-09-23 17:15:56 +1100384 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100385 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000386 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100387 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100388 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000389 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100390
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000391 if (options.preferred_authentications == NULL)
392 options.preferred_authentications = authmethods_get();
393
Damien Miller62cee002000-09-23 17:15:56 +1100394 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000395 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000396 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100397 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000398 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100399 authctxt.host = host;
400 authctxt.service = "ssh-connection"; /* service name */
401 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100402 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000403 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000404 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000405 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000406 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100407 if (authctxt.method == NULL)
408 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100409
410 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100411 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100412
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000413 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100414 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
415 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000416 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100417 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
418
Damien Miller280ecfb2003-05-14 13:46:00 +1000419 pubkey_cleanup(&authctxt);
Damien Millerf842fcb2003-05-15 12:01:28 +1000420 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
421
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000422 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100423}
Damien Millerf842fcb2003-05-15 12:01:28 +1000424
Damien Miller62cee002000-09-23 17:15:56 +1100425void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000426userauth(Authctxt *authctxt, char *authlist)
427{
Damien Miller01ed2272008-11-05 16:20:46 +1100428 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
429 authctxt->method->cleanup(authctxt);
430
Darren Tucker0efd1552003-08-26 11:49:55 +1000431 if (authctxt->methoddata) {
432 xfree(authctxt->methoddata);
433 authctxt->methoddata = NULL;
434 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000435 if (authlist == NULL) {
436 authlist = authctxt->authlist;
437 } else {
438 if (authctxt->authlist)
439 xfree(authctxt->authlist);
440 authctxt->authlist = authlist;
441 }
442 for (;;) {
443 Authmethod *method = authmethod_get(authlist);
444 if (method == NULL)
445 fatal("Permission denied (%s).", authlist);
446 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000447
448 /* reset the per method handler */
449 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
450 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
451
452 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000453 if (method->userauth(authctxt) != 0) {
454 debug2("we sent a %s packet, wait for reply", method->name);
455 break;
456 } else {
457 debug2("we did not send a packet, disable method");
458 method->enabled = NULL;
459 }
460 }
461}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000462
Damien Millerf7475d72008-11-03 19:26:18 +1100463/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000464void
Damien Miller630d6f42002-01-22 23:17:30 +1100465input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100466{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000467 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000468 "type %d", type);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000469}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000470
Damien Millerf7475d72008-11-03 19:26:18 +1100471/* ARGSUSED */
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000472void
Damien Miller630d6f42002-01-22 23:17:30 +1100473input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000474{
Damien Miller7ba0ca72008-07-17 18:57:06 +1000475 char *msg, *raw, *lang;
476 u_int len;
Darren Tucker79644822003-10-08 17:37:58 +1000477
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000478 debug3("input_userauth_banner");
Damien Miller7ba0ca72008-07-17 18:57:06 +1000479 raw = packet_get_string(&len);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000480 lang = packet_get_string(NULL);
Damien Millerc674d582008-11-03 19:16:57 +1100481 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
Damien Miller7ba0ca72008-07-17 18:57:06 +1000482 if (len > 65536)
483 len = 65536;
Damien Millerc4d1b362008-11-03 19:22:09 +1100484 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
Darren Tucker0c348f52010-01-08 18:58:05 +1100485 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
Darren Tucker046dff22003-10-08 17:32:02 +1000486 fprintf(stderr, "%s", msg);
Damien Miller7ba0ca72008-07-17 18:57:06 +1000487 xfree(msg);
488 }
489 xfree(raw);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000490 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100491}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000492
Damien Millerf7475d72008-11-03 19:26:18 +1100493/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100494void
Damien Miller630d6f42002-01-22 23:17:30 +1100495input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100496{
497 Authctxt *authctxt = ctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100498
Damien Miller62cee002000-09-23 17:15:56 +1100499 if (authctxt == NULL)
500 fatal("input_userauth_success: no authentication context");
Darren Tucker79644822003-10-08 17:37:58 +1000501 if (authctxt->authlist) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000502 xfree(authctxt->authlist);
Darren Tucker79644822003-10-08 17:37:58 +1000503 authctxt->authlist = NULL;
504 }
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100505 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
506 authctxt->method->cleanup(authctxt);
Darren Tucker79644822003-10-08 17:37:58 +1000507 if (authctxt->methoddata) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000508 xfree(authctxt->methoddata);
Darren Tucker79644822003-10-08 17:37:58 +1000509 authctxt->methoddata = NULL;
510 }
Damien Miller62cee002000-09-23 17:15:56 +1100511 authctxt->success = 1; /* break out */
512}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000513
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100514void
515input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
516{
517 Authctxt *authctxt = ctxt;
518
519 if (authctxt == NULL)
520 fatal("%s: no authentication context", __func__);
521
522 fatal("Unexpected authentication success during %s.",
523 authctxt->method->name);
524}
525
Damien Millerf7475d72008-11-03 19:26:18 +1100526/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100527void
Damien Miller630d6f42002-01-22 23:17:30 +1100528input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100529{
Damien Miller62cee002000-09-23 17:15:56 +1100530 Authctxt *authctxt = ctxt;
531 char *authlist = NULL;
532 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100533
534 if (authctxt == NULL)
535 fatal("input_userauth_failure: no authentication context");
536
Damien Miller874d77b2000-10-14 16:23:11 +1100537 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100538 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100539 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100540
541 if (partial != 0)
Damien Miller996acd22003-04-09 20:59:48 +1000542 logit("Authenticated with partial success.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000543 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100544
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000545 userauth(authctxt, authlist);
546}
Damien Millerf7475d72008-11-03 19:26:18 +1100547
548/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000549void
Damien Miller630d6f42002-01-22 23:17:30 +1100550input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000551{
552 Authctxt *authctxt = ctxt;
553 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000554 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000555 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000556 int pktype, sent = 0;
557 u_int alen, blen;
558 char *pkalg, *fp;
559 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000560
561 if (authctxt == NULL)
562 fatal("input_userauth_pk_ok: no authentication context");
563 if (datafellows & SSH_BUG_PKOK) {
564 /* this is similar to SSH_BUG_PKAUTH */
565 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
566 pkblob = packet_get_string(&blen);
567 buffer_init(&b);
568 buffer_append(&b, pkblob, blen);
569 pkalg = buffer_get_string(&b, &alen);
570 buffer_free(&b);
571 } else {
572 pkalg = packet_get_string(&alen);
573 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000574 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100575 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000576
Damien Miller280ecfb2003-05-14 13:46:00 +1000577 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000578
Damien Miller280ecfb2003-05-14 13:46:00 +1000579 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
580 debug("unknown pkalg %s", pkalg);
581 goto done;
582 }
583 if ((key = key_from_blob(pkblob, blen)) == NULL) {
584 debug("no key from blob. pkalg %s", pkalg);
585 goto done;
586 }
587 if (key->type != pktype) {
588 error("input_userauth_pk_ok: type mismatch "
589 "for decoded key (received %d, expected %d)",
590 key->type, pktype);
591 goto done;
592 }
593 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
594 debug2("input_userauth_pk_ok: fp %s", fp);
595 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000596
Darren Tuckerd05b6012003-10-15 15:55:59 +1000597 /*
598 * search keys in the reverse order, because last candidate has been
599 * moved to the end of the queue. this also avoids confusion by
600 * duplicate keys
601 */
Damien Miller0b51a522004-04-20 20:07:19 +1000602 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000603 if (key_equal(key, id->key)) {
604 sent = sign_and_send_pubkey(authctxt, id);
605 break;
606 }
607 }
608done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000609 if (key != NULL)
610 key_free(key);
611 xfree(pkalg);
612 xfree(pkblob);
613
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000614 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000615 if (sent == 0)
616 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100617}
618
Darren Tucker0efd1552003-08-26 11:49:55 +1000619#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100620int
Darren Tucker0efd1552003-08-26 11:49:55 +1000621userauth_gssapi(Authctxt *authctxt)
622{
623 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100624 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000625 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000626 OM_uint32 min;
627 int ok = 0;
628
629 /* Try one GSSAPI method at a time, rather than sending them all at
630 * once. */
631
Darren Tucker56afe142003-11-03 20:06:14 +1100632 if (gss_supported == NULL)
633 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000634
635 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100636 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000637 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100638 if (gss_supported->elements[mech].length < 128 &&
Damien Millera1cb9f32006-08-19 00:33:34 +1000639 ssh_gssapi_check_mechanism(&gssctxt,
640 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000641 ok = 1; /* Mechanism works */
642 } else {
643 mech++;
644 }
645 }
646
Damien Millera1cb9f32006-08-19 00:33:34 +1000647 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000648 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000649
650 authctxt->methoddata=(void *)gssctxt;
651
652 packet_start(SSH2_MSG_USERAUTH_REQUEST);
653 packet_put_cstring(authctxt->server_user);
654 packet_put_cstring(authctxt->service);
655 packet_put_cstring(authctxt->method->name);
656
657 packet_put_int(1);
658
Darren Tucker655a5e02003-11-03 20:09:03 +1100659 packet_put_int((gss_supported->elements[mech].length) + 2);
660 packet_put_char(SSH_GSS_OIDTYPE);
661 packet_put_char(gss_supported->elements[mech].length);
662 packet_put_raw(gss_supported->elements[mech].elements,
663 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000664
665 packet_send();
666
667 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
668 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
669 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
670 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
671
672 mech++; /* Move along to next candidate */
673
674 return 1;
675}
676
Damien Miller91c6aa42003-11-17 21:20:18 +1100677static OM_uint32
678process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
679{
680 Authctxt *authctxt = ctxt;
681 Gssctxt *gssctxt = authctxt->methoddata;
682 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000683 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
684 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100685 OM_uint32 status, ms, flags;
686 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100687
Damien Miller91c6aa42003-11-17 21:20:18 +1100688 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100689 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100690
691 if (send_tok.length > 0) {
692 if (GSS_ERROR(status))
693 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
694 else
695 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100696
Damien Miller91c6aa42003-11-17 21:20:18 +1100697 packet_put_string(send_tok.value, send_tok.length);
698 packet_send();
699 gss_release_buffer(&ms, &send_tok);
700 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100701
Damien Miller91c6aa42003-11-17 21:20:18 +1100702 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100703 /* send either complete or MIC, depending on mechanism */
704 if (!(flags & GSS_C_INTEG_FLAG)) {
705 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
706 packet_send();
707 } else {
708 ssh_gssapi_buildmic(&b, authctxt->server_user,
709 authctxt->service, "gssapi-with-mic");
710
711 gssbuf.value = buffer_ptr(&b);
712 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100713
Damien Miller0425d402003-11-17 22:18:21 +1100714 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100715
Damien Miller0425d402003-11-17 22:18:21 +1100716 if (!GSS_ERROR(status)) {
717 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
718 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100719
Damien Miller0425d402003-11-17 22:18:21 +1100720 packet_send();
721 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100722
Damien Miller0425d402003-11-17 22:18:21 +1100723 buffer_free(&b);
724 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100725 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100726 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100727
Damien Miller91c6aa42003-11-17 21:20:18 +1100728 return status;
729}
730
Damien Millerf7475d72008-11-03 19:26:18 +1100731/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000732void
733input_gssapi_response(int type, u_int32_t plen, void *ctxt)
734{
735 Authctxt *authctxt = ctxt;
736 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000737 int oidlen;
738 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000739
740 if (authctxt == NULL)
741 fatal("input_gssapi_response: no authentication context");
742 gssctxt = authctxt->methoddata;
743
744 /* Setup our OID */
745 oidv = packet_get_string(&oidlen);
746
Darren Tucker655a5e02003-11-03 20:09:03 +1100747 if (oidlen <= 2 ||
748 oidv[0] != SSH_GSS_OIDTYPE ||
749 oidv[1] != oidlen - 2) {
Damien Miller91c6aa42003-11-17 21:20:18 +1100750 xfree(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100751 debug("Badly encoded mechanism OID received");
752 userauth(authctxt, NULL);
Darren Tucker655a5e02003-11-03 20:09:03 +1100753 return;
Darren Tucker0efd1552003-08-26 11:49:55 +1000754 }
755
Darren Tucker655a5e02003-11-03 20:09:03 +1100756 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
757 fatal("Server returned different OID than expected");
758
Darren Tucker0efd1552003-08-26 11:49:55 +1000759 packet_check_eom();
760
761 xfree(oidv);
762
Damien Miller91c6aa42003-11-17 21:20:18 +1100763 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000764 /* Start again with next method on list */
765 debug("Trying to start again");
766 userauth(authctxt, NULL);
767 return;
768 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000769}
770
Damien Millerf7475d72008-11-03 19:26:18 +1100771/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000772void
773input_gssapi_token(int type, u_int32_t plen, void *ctxt)
774{
775 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000776 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100777 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000778 u_int slen;
779
780 if (authctxt == NULL)
781 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000782
783 recv_tok.value = packet_get_string(&slen);
784 recv_tok.length = slen; /* safe typecast */
785
786 packet_check_eom();
787
Damien Miller91c6aa42003-11-17 21:20:18 +1100788 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000789
790 xfree(recv_tok.value);
791
792 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000793 /* Start again with the next method in the list */
794 userauth(authctxt, NULL);
795 return;
796 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000797}
798
Damien Millerf7475d72008-11-03 19:26:18 +1100799/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000800void
801input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
802{
803 Authctxt *authctxt = ctxt;
804 Gssctxt *gssctxt;
805 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
806 gss_buffer_desc recv_tok;
807 OM_uint32 status, ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000808 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000809
810 if (authctxt == NULL)
811 fatal("input_gssapi_response: no authentication context");
812 gssctxt = authctxt->methoddata;
813
Darren Tucker600ad8d2003-08-26 12:10:48 +1000814 recv_tok.value = packet_get_string(&len);
815 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000816
817 packet_check_eom();
818
819 /* Stick it into GSSAPI and see what it says */
820 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000821 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000822
823 xfree(recv_tok.value);
824 gss_release_buffer(&ms, &send_tok);
825
826 /* Server will be returning a failed packet after this one */
827}
828
Damien Millerf7475d72008-11-03 19:26:18 +1100829/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000830void
831input_gssapi_error(int type, u_int32_t plen, void *ctxt)
832{
833 OM_uint32 maj, min;
834 char *msg;
835 char *lang;
836
837 maj=packet_get_int();
838 min=packet_get_int();
839 msg=packet_get_string(NULL);
840 lang=packet_get_string(NULL);
841
842 packet_check_eom();
843
Damien Miller15d72a02005-11-05 15:07:33 +1100844 debug("Server GSSAPI Error:\n%s", msg);
Darren Tucker0efd1552003-08-26 11:49:55 +1000845 xfree(msg);
846 xfree(lang);
847}
848#endif /* GSSAPI */
849
Damien Millereba71ba2000-04-29 23:57:08 +1000850int
Damien Miller874d77b2000-10-14 16:23:11 +1100851userauth_none(Authctxt *authctxt)
852{
853 /* initial userauth request */
854 packet_start(SSH2_MSG_USERAUTH_REQUEST);
855 packet_put_cstring(authctxt->server_user);
856 packet_put_cstring(authctxt->service);
857 packet_put_cstring(authctxt->method->name);
858 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100859 return 1;
860}
861
862int
Damien Miller62cee002000-09-23 17:15:56 +1100863userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000864{
Damien Millere247cc42000-05-07 12:03:14 +1000865 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000866 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000867 char *password;
Darren Tuckerab791692010-01-08 18:48:02 +1100868 const char *host = options.host_key_alias ? options.host_key_alias :
869 authctxt->host;
Damien Millereba71ba2000-04-29 23:57:08 +1000870
Damien Millerd3a18572000-06-07 19:55:44 +1000871 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000872 return 0;
873
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000874 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000875 error("Permission denied, please try again.");
876
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000877 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100878 authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +1000879 password = read_passphrase(prompt, 0);
880 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100881 packet_put_cstring(authctxt->server_user);
882 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100883 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000884 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000885 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000886 memset(password, 0, strlen(password));
887 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100888 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000889 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000890
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000891 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000892 &input_userauth_passwd_changereq);
893
Damien Millereba71ba2000-04-29 23:57:08 +1000894 return 1;
895}
Damien Millerf7475d72008-11-03 19:26:18 +1100896
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000897/*
898 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
899 */
Damien Millerf7475d72008-11-03 19:26:18 +1100900/* ARGSUSED */
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000901void
Tim Ricec8549622002-03-31 12:49:38 -0800902input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000903{
904 Authctxt *authctxt = ctxt;
905 char *info, *lang, *password = NULL, *retype = NULL;
906 char prompt[150];
Darren Tuckerab791692010-01-08 18:48:02 +1100907 const char *host = options.host_key_alias ? options.host_key_alias :
908 authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000909
910 debug2("input_userauth_passwd_changereq");
911
912 if (authctxt == NULL)
913 fatal("input_userauth_passwd_changereq: "
914 "no authentication context");
915
916 info = packet_get_string(NULL);
917 lang = packet_get_string(NULL);
918 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000919 logit("%s", info);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000920 xfree(info);
921 xfree(lang);
922 packet_start(SSH2_MSG_USERAUTH_REQUEST);
923 packet_put_cstring(authctxt->server_user);
924 packet_put_cstring(authctxt->service);
925 packet_put_cstring(authctxt->method->name);
926 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000927 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000928 "Enter %.30s@%.128s's old password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100929 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000930 password = read_passphrase(prompt, 0);
931 packet_put_cstring(password);
932 memset(password, 0, strlen(password));
933 xfree(password);
934 password = NULL;
935 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000936 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000937 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100938 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000939 password = read_passphrase(prompt, RP_ALLOW_EOF);
940 if (password == NULL) {
941 /* bail out */
942 return;
943 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000944 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000945 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100946 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000947 retype = read_passphrase(prompt, 0);
948 if (strcmp(password, retype) != 0) {
949 memset(password, 0, strlen(password));
950 xfree(password);
Damien Miller996acd22003-04-09 20:59:48 +1000951 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000952 password = NULL;
953 }
954 memset(retype, 0, strlen(retype));
955 xfree(retype);
956 }
957 packet_put_cstring(password);
958 memset(password, 0, strlen(password));
959 xfree(password);
960 packet_add_padding(64);
961 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000962
963 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000964 &input_userauth_passwd_changereq);
965}
Damien Millereba71ba2000-04-29 23:57:08 +1000966
Damien Miller01ed2272008-11-05 16:20:46 +1100967#ifdef JPAKE
968static char *
969pw_encrypt(const char *password, const char *crypt_scheme, const char *salt)
970{
971 /* OpenBSD crypt(3) handles all of these */
972 if (strcmp(crypt_scheme, "crypt") == 0 ||
973 strcmp(crypt_scheme, "bcrypt") == 0 ||
974 strcmp(crypt_scheme, "md5crypt") == 0 ||
975 strcmp(crypt_scheme, "crypt-extended") == 0)
976 return xstrdup(crypt(password, salt));
977 error("%s: unsupported password encryption scheme \"%.100s\"",
978 __func__, crypt_scheme);
979 return NULL;
980}
981
982static BIGNUM *
983jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme,
984 const char *salt)
985{
986 char prompt[256], *password, *crypted;
987 u_char *secret;
988 u_int secret_len;
989 BIGNUM *ret;
990
991 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ",
992 authctxt->server_user, authctxt->host);
993 password = read_passphrase(prompt, 0);
994
995 if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) {
996 logit("Disabling %s authentication", authctxt->method->name);
997 authctxt->method->enabled = NULL;
998 /* Continue with an empty password to fail gracefully */
999 crypted = xstrdup("");
1000 }
1001
1002#ifdef JPAKE_DEBUG
1003 debug3("%s: salt = %s", __func__, salt);
1004 debug3("%s: scheme = %s", __func__, crypt_scheme);
1005 debug3("%s: crypted = %s", __func__, crypted);
1006#endif
1007
1008 if (hash_buffer(crypted, strlen(crypted), EVP_sha256(),
1009 &secret, &secret_len) != 0)
1010 fatal("%s: hash_buffer", __func__);
1011
1012 bzero(password, strlen(password));
1013 bzero(crypted, strlen(crypted));
1014 xfree(password);
1015 xfree(crypted);
1016
1017 if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL)
1018 fatal("%s: BN_bin2bn (secret)", __func__);
1019 bzero(secret, secret_len);
1020 xfree(secret);
1021
1022 return ret;
1023}
1024
1025/* ARGSUSED */
1026void
1027input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt)
1028{
1029 Authctxt *authctxt = ctxt;
1030 struct jpake_ctx *pctx = authctxt->methoddata;
1031 u_char *x3_proof, *x4_proof, *x2_s_proof;
1032 u_int x3_proof_len, x4_proof_len, x2_s_proof_len;
1033 char *crypt_scheme, *salt;
1034
1035 /* Disable this message */
1036 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL);
1037
1038 if ((pctx->g_x3 = BN_new()) == NULL ||
1039 (pctx->g_x4 = BN_new()) == NULL)
1040 fatal("%s: BN_new", __func__);
1041
1042 /* Fetch step 1 values */
1043 crypt_scheme = packet_get_string(NULL);
1044 salt = packet_get_string(NULL);
1045 pctx->server_id = packet_get_string(&pctx->server_id_len);
1046 packet_get_bignum2(pctx->g_x3);
1047 packet_get_bignum2(pctx->g_x4);
1048 x3_proof = packet_get_string(&x3_proof_len);
1049 x4_proof = packet_get_string(&x4_proof_len);
1050 packet_check_eom();
1051
1052 JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__));
1053
1054 /* Obtain password and derive secret */
1055 pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt);
1056 bzero(crypt_scheme, strlen(crypt_scheme));
1057 bzero(salt, strlen(salt));
1058 xfree(crypt_scheme);
1059 xfree(salt);
1060 JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__));
1061
1062 /* Calculate step 2 values */
1063 jpake_step2(pctx->grp, pctx->s, pctx->g_x1,
1064 pctx->g_x3, pctx->g_x4, pctx->x2,
1065 pctx->server_id, pctx->server_id_len,
1066 pctx->client_id, pctx->client_id_len,
1067 x3_proof, x3_proof_len,
1068 x4_proof, x4_proof_len,
1069 &pctx->a,
1070 &x2_s_proof, &x2_s_proof_len);
1071
1072 bzero(x3_proof, x3_proof_len);
1073 bzero(x4_proof, x4_proof_len);
1074 xfree(x3_proof);
1075 xfree(x4_proof);
1076
1077 JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__));
1078
1079 /* Send values for step 2 */
1080 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2);
1081 packet_put_bignum2(pctx->a);
1082 packet_put_string(x2_s_proof, x2_s_proof_len);
1083 packet_send();
1084
1085 bzero(x2_s_proof, x2_s_proof_len);
1086 xfree(x2_s_proof);
1087
1088 /* Expect step 2 packet from peer */
1089 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2,
1090 input_userauth_jpake_server_step2);
1091}
1092
1093/* ARGSUSED */
1094void
1095input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt)
1096{
1097 Authctxt *authctxt = ctxt;
1098 struct jpake_ctx *pctx = authctxt->methoddata;
1099 u_char *x4_s_proof;
1100 u_int x4_s_proof_len;
1101
1102 /* Disable this message */
1103 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL);
1104
1105 if ((pctx->b = BN_new()) == NULL)
1106 fatal("%s: BN_new", __func__);
1107
1108 /* Fetch step 2 values */
1109 packet_get_bignum2(pctx->b);
1110 x4_s_proof = packet_get_string(&x4_s_proof_len);
1111 packet_check_eom();
1112
1113 JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__));
1114
1115 /* Derive shared key and calculate confirmation hash */
1116 jpake_key_confirm(pctx->grp, pctx->s, pctx->b,
1117 pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4,
1118 pctx->client_id, pctx->client_id_len,
1119 pctx->server_id, pctx->server_id_len,
1120 session_id2, session_id2_len,
1121 x4_s_proof, x4_s_proof_len,
1122 &pctx->k,
1123 &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len);
1124
1125 bzero(x4_s_proof, x4_s_proof_len);
1126 xfree(x4_s_proof);
1127
1128 JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__));
1129
1130 /* Send key confirmation proof */
1131 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM);
1132 packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
1133 packet_send();
1134
1135 /* Expect confirmation from peer */
1136 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM,
1137 input_userauth_jpake_server_confirm);
1138}
1139
1140/* ARGSUSED */
1141void
1142input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt)
1143{
1144 Authctxt *authctxt = ctxt;
1145 struct jpake_ctx *pctx = authctxt->methoddata;
1146
1147 /* Disable this message */
1148 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL);
1149
1150 pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len);
1151 packet_check_eom();
1152
1153 JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__));
1154
1155 /* Verify expected confirmation hash */
1156 if (jpake_check_confirm(pctx->k,
1157 pctx->server_id, pctx->server_id_len,
1158 session_id2, session_id2_len,
1159 pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1)
1160 debug("%s: %s success", __func__, authctxt->method->name);
1161 else {
1162 debug("%s: confirmation mismatch", __func__);
1163 /* XXX stash this so if auth succeeds then we can warn/kill */
1164 }
1165
1166 userauth_jpake_cleanup(authctxt);
1167}
1168#endif /* JPAKE */
1169
Ben Lindstrombba81212001-06-25 05:01:22 +00001170static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001171identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1172 u_char *data, u_int datalen)
1173{
1174 Key *prv;
1175 int ret;
1176
1177 /* the agent supports this key */
1178 if (id->ac)
1179 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1180 data, datalen));
1181 /*
1182 * we have already loaded the private key or
1183 * the private key is stored in external hardware
1184 */
1185 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1186 return (key_sign(id->key, sigp, lenp, data, datalen));
1187 /* load the private key from the file */
1188 if ((prv = load_identity_file(id->filename)) == NULL)
1189 return (-1);
1190 ret = key_sign(prv, sigp, lenp, data, datalen);
1191 key_free(prv);
1192 return (ret);
1193}
1194
1195static int
1196sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +10001197{
1198 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001199 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +00001200 u_int bloblen, slen;
Darren Tucker502d3842003-06-28 12:38:01 +10001201 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001202 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001203 int have_sig = 1;
Damien Miller4e270b02010-04-16 15:56:21 +10001204 char *fp;
Damien Millereba71ba2000-04-29 23:57:08 +10001205
Damien Miller4e270b02010-04-16 15:56:21 +10001206 fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
1207 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
1208 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001209
Damien Miller280ecfb2003-05-14 13:46:00 +10001210 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001211 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +00001212 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001213 return 0;
1214 }
Damien Millereba71ba2000-04-29 23:57:08 +10001215 /* data to be signed */
1216 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +11001217 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +10001218 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001219 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +11001220 } else {
1221 buffer_put_string(&b, session_id2, session_id2_len);
1222 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +10001223 }
Damien Millereba71ba2000-04-29 23:57:08 +10001224 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001225 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +10001226 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +00001227 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +10001228 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +11001229 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +00001230 if (datafellows & SSH_BUG_PKAUTH) {
1231 buffer_put_char(&b, have_sig);
1232 } else {
1233 buffer_put_cstring(&b, authctxt->method->name);
1234 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +10001235 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +00001236 }
Damien Millereba71ba2000-04-29 23:57:08 +10001237 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +10001238
1239 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +10001240 ret = identity_sign(id, &signature, &slen,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001241 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +10001242 if (ret == -1) {
1243 xfree(blob);
1244 buffer_free(&b);
1245 return 0;
1246 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001247#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +10001248 buffer_dump(&b);
1249#endif
Ben Lindstromd121f612000-12-03 17:00:47 +00001250 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +10001251 buffer_clear(&b);
1252 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001253 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +10001254 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001255 buffer_put_cstring(&b, authctxt->server_user);
1256 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +11001257 buffer_put_cstring(&b, authctxt->method->name);
1258 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +00001259 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001260 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +10001261 buffer_put_string(&b, blob, bloblen);
1262 }
1263 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001264
Damien Millereba71ba2000-04-29 23:57:08 +10001265 /* append signature */
1266 buffer_put_string(&b, signature, slen);
1267 xfree(signature);
1268
1269 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +10001270 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +11001271 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +10001272 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +10001273
1274 /* put remaining data from buffer into packet */
1275 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1276 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1277 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +10001278 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +10001279
1280 return 1;
Damien Miller994cf142000-07-21 10:19:44 +10001281}
1282
Ben Lindstrombba81212001-06-25 05:01:22 +00001283static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001284send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001285{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001286 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001287 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +10001288
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001289 debug3("send_pubkey_test");
1290
Damien Miller280ecfb2003-05-14 13:46:00 +10001291 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001292 /* we cannot handle this key */
1293 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +10001294 return 0;
1295 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001296 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001297 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001298
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001299 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1300 packet_put_cstring(authctxt->server_user);
1301 packet_put_cstring(authctxt->service);
1302 packet_put_cstring(authctxt->method->name);
1303 packet_put_char(have_sig);
1304 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001305 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001306 packet_put_string(blob, bloblen);
1307 xfree(blob);
1308 packet_send();
1309 return 1;
1310}
1311
Ben Lindstrombba81212001-06-25 05:01:22 +00001312static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001313load_identity_file(char *filename)
1314{
1315 Key *private;
1316 char prompt[300], *passphrase;
Darren Tuckerd4c86b12010-01-12 19:41:22 +11001317 int perm_ok = 0, quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001318 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001319
Ben Lindstrom329782e2001-03-10 17:08:59 +00001320 if (stat(filename, &st) < 0) {
1321 debug3("no such identity: %s", filename);
1322 return NULL;
1323 }
Darren Tucker232b76f2006-05-06 17:41:51 +10001324 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1325 if (!perm_ok)
1326 return NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +00001327 if (private == NULL) {
1328 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001329 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +10001330 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +11001331 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +11001332 for (i = 0; i < options.number_of_password_prompts; i++) {
1333 passphrase = read_passphrase(prompt, 0);
1334 if (strcmp(passphrase, "") != 0) {
Darren Tucker232b76f2006-05-06 17:41:51 +10001335 private = key_load_private_type(KEY_UNSPEC,
1336 filename, passphrase, NULL, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001337 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +11001338 } else {
1339 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001340 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +11001341 }
1342 memset(passphrase, 0, strlen(passphrase));
1343 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001344 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +11001345 break;
1346 debug2("bad passphrase given, try again...");
1347 }
Damien Miller994cf142000-07-21 10:19:44 +10001348 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001349 return private;
1350}
1351
Damien Miller280ecfb2003-05-14 13:46:00 +10001352/*
1353 * try keys in the following order:
1354 * 1. agent keys that are found in the config file
1355 * 2. other agent keys
1356 * 3. keys that are only listed in the config file
1357 */
1358static void
1359pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001360{
Damien Miller280ecfb2003-05-14 13:46:00 +10001361 Identity *id;
1362 Idlist agent, files, *preferred;
1363 Key *key;
1364 AuthenticationConnection *ac;
Damien Millerad833b32000-08-23 10:46:23 +10001365 char *comment;
Damien Miller280ecfb2003-05-14 13:46:00 +10001366 int i, found;
Damien Millerad833b32000-08-23 10:46:23 +10001367
Damien Miller280ecfb2003-05-14 13:46:00 +10001368 TAILQ_INIT(&agent); /* keys from the agent */
1369 TAILQ_INIT(&files); /* keys from the config file */
1370 preferred = &authctxt->keys;
1371 TAILQ_INIT(preferred); /* preferred order of keys */
1372
1373 /* list of keys stored in the filesystem */
1374 for (i = 0; i < options.num_identity_files; i++) {
1375 key = options.identity_keys[i];
1376 if (key && key->type == KEY_RSA1)
1377 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +11001378 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1379 continue;
Damien Miller280ecfb2003-05-14 13:46:00 +10001380 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001381 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001382 id->key = key;
1383 id->filename = xstrdup(options.identity_files[i]);
1384 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001385 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001386 /* list of keys supported by the agent */
1387 if ((ac = ssh_get_authentication_connection())) {
1388 for (key = ssh_get_first_identity(ac, &comment, 2);
1389 key != NULL;
1390 key = ssh_get_next_identity(ac, &comment, 2)) {
1391 found = 0;
1392 TAILQ_FOREACH(id, &files, next) {
Damien Millera8e06ce2003-11-21 23:48:55 +11001393 /* agent keys from the config file are preferred */
Damien Miller280ecfb2003-05-14 13:46:00 +10001394 if (key_equal(key, id->key)) {
1395 key_free(key);
1396 xfree(comment);
1397 TAILQ_REMOVE(&files, id, next);
1398 TAILQ_INSERT_TAIL(preferred, id, next);
1399 id->ac = ac;
1400 found = 1;
1401 break;
1402 }
1403 }
Damien Millerbd394c32004-03-08 23:12:36 +11001404 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001405 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001406 id->key = key;
1407 id->filename = comment;
1408 id->ac = ac;
1409 TAILQ_INSERT_TAIL(&agent, id, next);
1410 }
1411 }
1412 /* append remaining agent keys */
1413 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1414 TAILQ_REMOVE(&agent, id, next);
1415 TAILQ_INSERT_TAIL(preferred, id, next);
1416 }
1417 authctxt->agent = ac;
Damien Miller62cee002000-09-23 17:15:56 +11001418 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001419 /* append remaining keys from the config file */
1420 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1421 TAILQ_REMOVE(&files, id, next);
1422 TAILQ_INSERT_TAIL(preferred, id, next);
1423 }
1424 TAILQ_FOREACH(id, preferred, next) {
1425 debug2("key: %s (%p)", id->filename, id->key);
1426 }
1427}
1428
1429static void
1430pubkey_cleanup(Authctxt *authctxt)
1431{
1432 Identity *id;
1433
1434 if (authctxt->agent != NULL)
1435 ssh_close_authentication_connection(authctxt->agent);
1436 for (id = TAILQ_FIRST(&authctxt->keys); id;
1437 id = TAILQ_FIRST(&authctxt->keys)) {
1438 TAILQ_REMOVE(&authctxt->keys, id, next);
1439 if (id->key)
1440 key_free(id->key);
1441 if (id->filename)
1442 xfree(id->filename);
1443 xfree(id);
1444 }
Damien Millereba71ba2000-04-29 23:57:08 +10001445}
1446
Damien Miller62cee002000-09-23 17:15:56 +11001447int
1448userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001449{
Damien Miller280ecfb2003-05-14 13:46:00 +10001450 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001451 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001452
Damien Miller280ecfb2003-05-14 13:46:00 +10001453 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1454 if (id->tried++)
1455 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001456 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001457 TAILQ_REMOVE(&authctxt->keys, id, next);
1458 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1459 /*
1460 * send a test message if we have the public key. for
1461 * encrypted keys we cannot do this and have to load the
1462 * private key instead
1463 */
1464 if (id->key && id->key->type != KEY_RSA1) {
Damien Miller88680652010-04-16 15:53:43 +10001465 debug("Offering %s public key: %s", key_type(id->key),
1466 id->filename);
Damien Miller280ecfb2003-05-14 13:46:00 +10001467 sent = send_pubkey_test(authctxt, id);
1468 } else if (id->key == NULL) {
1469 debug("Trying private key: %s", id->filename);
1470 id->key = load_identity_file(id->filename);
1471 if (id->key != NULL) {
1472 id->isprivate = 1;
1473 sent = sign_and_send_pubkey(authctxt, id);
1474 key_free(id->key);
1475 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001476 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001477 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001478 if (sent)
1479 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001480 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001481 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001482}
Damien Millereba71ba2000-04-29 23:57:08 +10001483
Damien Miller874d77b2000-10-14 16:23:11 +11001484/*
1485 * Send userauth request message specifying keyboard-interactive method.
1486 */
1487int
1488userauth_kbdint(Authctxt *authctxt)
1489{
1490 static int attempt = 0;
1491
1492 if (attempt++ >= options.number_of_password_prompts)
1493 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001494 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1495 if (attempt > 1 && !authctxt->info_req_seen) {
1496 debug3("userauth_kbdint: disable: no info_req_seen");
1497 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1498 return 0;
1499 }
Damien Miller874d77b2000-10-14 16:23:11 +11001500
1501 debug2("userauth_kbdint");
1502 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1503 packet_put_cstring(authctxt->server_user);
1504 packet_put_cstring(authctxt->service);
1505 packet_put_cstring(authctxt->method->name);
1506 packet_put_cstring(""); /* lang */
1507 packet_put_cstring(options.kbd_interactive_devices ?
1508 options.kbd_interactive_devices : "");
1509 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001510
1511 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1512 return 1;
1513}
1514
1515/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001516 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001517 */
1518void
Damien Miller630d6f42002-01-22 23:17:30 +11001519input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001520{
1521 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001522 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001523 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001524 int echo = 0;
1525
1526 debug2("input_userauth_info_req");
1527
1528 if (authctxt == NULL)
1529 fatal("input_userauth_info_req: no authentication context");
1530
Ben Lindstrom7d199962001-09-12 18:29:00 +00001531 authctxt->info_req_seen = 1;
1532
Damien Miller874d77b2000-10-14 16:23:11 +11001533 name = packet_get_string(NULL);
1534 inst = packet_get_string(NULL);
1535 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001536 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001537 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001538 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001539 logit("%s", inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001540 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +11001541 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001542 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001543
1544 num_prompts = packet_get_int();
1545 /*
1546 * Begin to build info response packet based on prompts requested.
1547 * We commit to providing the correct number of responses, so if
1548 * further on we run into a problem that prevents this, we have to
1549 * be sure and clean this up and send a correct error response.
1550 */
1551 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1552 packet_put_int(num_prompts);
1553
Ben Lindstrom551ea372001-06-05 18:56:16 +00001554 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001555 for (i = 0; i < num_prompts; i++) {
1556 prompt = packet_get_string(NULL);
1557 echo = packet_get_char();
1558
Ben Lindstrom949974b2001-06-25 05:20:31 +00001559 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001560
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001561 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +11001562 memset(response, 0, strlen(response));
1563 xfree(response);
1564 xfree(prompt);
1565 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001566 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001567
Damien Miller9f643902001-11-12 11:02:52 +11001568 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001569 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001570}
Damien Miller62cee002000-09-23 17:15:56 +11001571
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001572static int
Ben Lindstrom5c385522002-06-23 21:23:20 +00001573ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001574 u_char *data, u_int datalen)
1575{
1576 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001577 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001578 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001579 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001580
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001581 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001582
Ben Lindstrom5206b952002-06-06 19:59:29 +00001583 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001584 error("ssh_keysign: not installed: %s", strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001585 return -1;
1586 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001587 if (fflush(stdout) != 0)
1588 error("ssh_keysign: fflush: %s", strerror(errno));
1589 if (pipe(to) < 0) {
1590 error("ssh_keysign: pipe: %s", strerror(errno));
1591 return -1;
1592 }
1593 if (pipe(from) < 0) {
1594 error("ssh_keysign: pipe: %s", strerror(errno));
1595 return -1;
1596 }
1597 if ((pid = fork()) < 0) {
1598 error("ssh_keysign: fork: %s", strerror(errno));
1599 return -1;
1600 }
1601 if (pid == 0) {
Darren Tucker6e7fe1c2010-01-08 17:07:22 +11001602 /* keep the socket on exec */
1603 fcntl(packet_get_connection_in(), F_SETFD, 0);
Damien Miller2e5fe882006-06-13 13:10:00 +10001604 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001605 close(from[0]);
1606 if (dup2(from[1], STDOUT_FILENO) < 0)
1607 fatal("ssh_keysign: dup2: %s", strerror(errno));
1608 close(to[1]);
1609 if (dup2(to[0], STDIN_FILENO) < 0)
1610 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001611 close(from[1]);
1612 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001613 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001614 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1615 strerror(errno));
1616 }
1617 close(from[1]);
1618 close(to[0]);
1619
1620 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001621 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001622 buffer_put_string(&b, data, datalen);
Damien Miller51bf11f2003-11-17 21:20:47 +11001623 if (ssh_msg_send(to[1], version, &b) == -1)
1624 fatal("ssh_keysign: couldn't send request");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001625
Damien Miller901119b2002-10-04 11:10:04 +10001626 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001627 error("ssh_keysign: no reply");
Damien Millerfb1310e2004-01-21 11:02:50 +11001628 buffer_free(&b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001629 return -1;
1630 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001631 close(from[0]);
1632 close(to[1]);
1633
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001634 while (waitpid(pid, &status, 0) < 0)
1635 if (errno != EINTR)
1636 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001637
Ben Lindstrom5206b952002-06-06 19:59:29 +00001638 if (buffer_get_char(&b) != version) {
1639 error("ssh_keysign: bad version");
Damien Millerfb1310e2004-01-21 11:02:50 +11001640 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001641 return -1;
1642 }
1643 *sigp = buffer_get_string(&b, lenp);
Damien Millerfb1310e2004-01-21 11:02:50 +11001644 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001645
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001646 return 0;
1647}
1648
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001649int
1650userauth_hostbased(Authctxt *authctxt)
1651{
1652 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001653 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001654 Buffer b;
1655 u_char *signature, *blob;
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001656 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001657 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001658 u_int blen, slen;
Darren Tucker5246df42010-01-08 18:50:46 +11001659 int ok, i, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001660
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001661 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001662 for (i = 0; i < sensitive->nkeys; i++) {
1663 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001664 if (private && private->type != KEY_RSA1) {
1665 found = 1;
1666 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001667 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001668 break;
1669 }
1670 }
1671 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001672 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001673 return 0;
1674 }
1675 if (key_to_blob(private, &blob, &blen) == 0) {
1676 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001677 return 0;
1678 }
Damien Miller91c18472001-11-12 11:02:03 +11001679 /* figure out a name for the client host */
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001680 p = get_local_name(packet_get_connection_in());
Damien Miller91c18472001-11-12 11:02:03 +11001681 if (p == NULL) {
1682 error("userauth_hostbased: cannot get local ipaddr/name");
1683 key_free(private);
Damien Miller5790b592006-03-26 13:54:03 +11001684 xfree(blob);
Damien Miller91c18472001-11-12 11:02:03 +11001685 return 0;
1686 }
Damien Miller07d86be2006-03-26 14:19:21 +11001687 xasprintf(&chost, "%s.", p);
Damien Miller91c18472001-11-12 11:02:03 +11001688 debug2("userauth_hostbased: chost %s", chost);
Damien Miller00111382003-03-10 11:21:17 +11001689 xfree(p);
Damien Miller91c18472001-11-12 11:02:03 +11001690
Ben Lindstrom671388f2001-04-19 20:40:45 +00001691 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1692 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001693 pkalg = xstrdup(key_ssh_name(private));
1694 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001695 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001696 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001697 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1698 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001699 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001700 buffer_put_cstring(&b, authctxt->method->name);
1701 buffer_put_cstring(&b, pkalg);
1702 buffer_put_string(&b, blob, blen);
1703 buffer_put_cstring(&b, chost);
1704 buffer_put_cstring(&b, authctxt->local_user);
1705#ifdef DEBUG_PK
1706 buffer_dump(&b);
1707#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001708 if (sensitive->external_keysign)
1709 ok = ssh_keysign(private, &signature, &slen,
1710 buffer_ptr(&b), buffer_len(&b));
1711 else
1712 ok = key_sign(private, &signature, &slen,
1713 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001714 key_free(private);
1715 buffer_free(&b);
1716 if (ok != 0) {
1717 error("key_sign failed");
1718 xfree(chost);
1719 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001720 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001721 return 0;
1722 }
1723 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1724 packet_put_cstring(authctxt->server_user);
1725 packet_put_cstring(authctxt->service);
1726 packet_put_cstring(authctxt->method->name);
1727 packet_put_cstring(pkalg);
1728 packet_put_string(blob, blen);
1729 packet_put_cstring(chost);
1730 packet_put_cstring(authctxt->local_user);
1731 packet_put_string(signature, slen);
1732 memset(signature, 's', slen);
1733 xfree(signature);
1734 xfree(chost);
1735 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001736 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001737
1738 packet_send();
1739 return 1;
1740}
1741
Damien Miller01ed2272008-11-05 16:20:46 +11001742#ifdef JPAKE
1743int
1744userauth_jpake(Authctxt *authctxt)
1745{
1746 struct jpake_ctx *pctx;
1747 u_char *x1_proof, *x2_proof;
1748 u_int x1_proof_len, x2_proof_len;
1749 static int attempt = 0; /* XXX share with userauth_password's? */
1750
1751 if (attempt++ >= options.number_of_password_prompts)
1752 return 0;
1753 if (attempt != 1)
1754 error("Permission denied, please try again.");
1755
1756 if (authctxt->methoddata != NULL)
1757 fatal("%s: authctxt->methoddata already set (%p)",
1758 __func__, authctxt->methoddata);
1759
1760 authctxt->methoddata = pctx = jpake_new();
1761
1762 /*
1763 * Send request immediately, to get the protocol going while
1764 * we do the initial computations.
1765 */
1766 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1767 packet_put_cstring(authctxt->server_user);
1768 packet_put_cstring(authctxt->service);
1769 packet_put_cstring(authctxt->method->name);
1770 packet_send();
1771 packet_write_wait();
1772
1773 jpake_step1(pctx->grp,
1774 &pctx->client_id, &pctx->client_id_len,
1775 &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2,
1776 &x1_proof, &x1_proof_len,
1777 &x2_proof, &x2_proof_len);
1778
1779 JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__));
1780
1781 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1);
1782 packet_put_string(pctx->client_id, pctx->client_id_len);
1783 packet_put_bignum2(pctx->g_x1);
1784 packet_put_bignum2(pctx->g_x2);
1785 packet_put_string(x1_proof, x1_proof_len);
1786 packet_put_string(x2_proof, x2_proof_len);
1787 packet_send();
1788
1789 bzero(x1_proof, x1_proof_len);
1790 bzero(x2_proof, x2_proof_len);
1791 xfree(x1_proof);
1792 xfree(x2_proof);
1793
1794 /* Expect step 1 packet from peer */
1795 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1,
1796 input_userauth_jpake_server_step1);
Darren Tucker2f29a8c2009-10-24 11:47:58 +11001797 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS,
1798 &input_userauth_success_unexpected);
Damien Miller01ed2272008-11-05 16:20:46 +11001799
1800 return 1;
1801}
1802
1803void
1804userauth_jpake_cleanup(Authctxt *authctxt)
1805{
1806 debug3("%s: clean up", __func__);
1807 if (authctxt->methoddata != NULL) {
1808 jpake_free(authctxt->methoddata);
1809 authctxt->methoddata = NULL;
1810 }
Darren Tucker2f29a8c2009-10-24 11:47:58 +11001811 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
Damien Miller01ed2272008-11-05 16:20:46 +11001812}
1813#endif /* JPAKE */
1814
Damien Miller62cee002000-09-23 17:15:56 +11001815/* find auth method */
1816
Damien Miller62cee002000-09-23 17:15:56 +11001817/*
1818 * given auth method name, if configurable options permit this method fill
1819 * in auth_ident field and return true, otherwise return false.
1820 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001821static int
Damien Miller62cee002000-09-23 17:15:56 +11001822authmethod_is_enabled(Authmethod *method)
1823{
1824 if (method == NULL)
1825 return 0;
1826 /* return false if options indicate this method is disabled */
1827 if (method->enabled == NULL || *method->enabled == 0)
1828 return 0;
1829 /* return false if batch mode is enabled but method needs interactive mode */
1830 if (method->batch_flag != NULL && *method->batch_flag != 0)
1831 return 0;
1832 return 1;
1833}
1834
Ben Lindstrombba81212001-06-25 05:01:22 +00001835static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001836authmethod_lookup(const char *name)
1837{
1838 Authmethod *method = NULL;
1839 if (name != NULL)
1840 for (method = authmethods; method->name != NULL; method++)
1841 if (strcmp(name, method->name) == 0)
1842 return method;
1843 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1844 return NULL;
1845}
1846
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001847/* XXX internal state */
1848static Authmethod *current = NULL;
1849static char *supported = NULL;
1850static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001851
Damien Miller62cee002000-09-23 17:15:56 +11001852/*
1853 * Given the authentication method list sent by the server, return the
1854 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001855 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001856 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001857static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001858authmethod_get(char *authlist)
1859{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001860 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001861 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001862
Damien Miller62cee002000-09-23 17:15:56 +11001863 /* Use a suitable default if we're passed a nil list. */
1864 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001865 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001866
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001867 if (supported == NULL || strcmp(authlist, supported) != 0) {
1868 debug3("start over, passed a different list %s", authlist);
1869 if (supported != NULL)
1870 xfree(supported);
1871 supported = xstrdup(authlist);
1872 preferred = options.preferred_authentications;
1873 debug3("preferred %s", preferred);
1874 current = NULL;
1875 } else if (current != NULL && authmethod_is_enabled(current))
1876 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001877
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001878 for (;;) {
1879 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001880 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001881 current = NULL;
1882 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001883 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001884 preferred += next;
1885 debug3("authmethod_lookup %s", name);
1886 debug3("remaining preferred: %s", preferred);
1887 if ((current = authmethod_lookup(name)) != NULL &&
1888 authmethod_is_enabled(current)) {
1889 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001890 debug("Next authentication method: %s", name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001891 return current;
1892 }
Damien Millereba71ba2000-04-29 23:57:08 +10001893 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001894}
Damien Miller62cee002000-09-23 17:15:56 +11001895
Ben Lindstrom79073822001-07-04 03:42:30 +00001896static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001897authmethods_get(void)
1898{
1899 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001900 Buffer b;
1901 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001902
Damien Miller0e3b8722002-01-22 23:26:38 +11001903 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001904 for (method = authmethods; method->name != NULL; method++) {
1905 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001906 if (buffer_len(&b) > 0)
1907 buffer_append(&b, ",", 1);
1908 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001909 }
Damien Miller62cee002000-09-23 17:15:56 +11001910 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001911 buffer_append(&b, "\0", 1);
1912 list = xstrdup(buffer_ptr(&b));
1913 buffer_free(&b);
1914 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001915}
Damien Miller01ed2272008-11-05 16:20:46 +11001916