blob: 19ed3459f407cb3976e4cb54353520ded5d9cbaf [file] [log] [blame]
Darren Tuckera627d422013-06-02 07:31:17 +10001/* $OpenBSD: sshconnect2.c,v 1.197 2013/05/17 00:13:14 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 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
Darren Tuckera627d422013-06-02 07:31:17 +1000149 free(first);
150 free(last);
151 free(hostname);
152 free(oavail);
Damien Millerd925dcd2010-12-01 12:21:51 +1100153 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
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000200 if (options.rekey_limit || options.rekey_interval)
201 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
202 (time_t)options.rekey_interval);
Damien Millera5539d22003-04-09 20:50:06 +1000203
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000204 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000205 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100206 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000207 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100208 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100209 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Damien Millereb8b60e2010-08-31 22:41:14 +1000210 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000211 kex->client_version_string=client_version_string;
212 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000213 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100214
Ben Lindstromf28f6342001-04-04 02:03:04 +0000215 xxx_kex = kex;
216
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000217 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100218
Darren Tucker36331b52010-01-08 16:50:41 +1100219 if (options.use_roaming && !kex->roaming) {
220 debug("Roaming not allowed by server");
221 options.use_roaming = 0;
222 }
223
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000224 session_id2 = kex->session_id;
225 session_id2_len = kex->session_id_len;
226
Damien Miller874d77b2000-10-14 16:23:11 +1100227#ifdef DEBUG_KEXDH
228 /* send 1st encrypted/maced/compressed message */
229 packet_start(SSH2_MSG_IGNORE);
230 packet_put_cstring("markus");
231 packet_send();
232 packet_write_wait();
233#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000234}
Damien Millerb1715dc2000-05-30 13:44:51 +1000235
Damien Millereba71ba2000-04-29 23:57:08 +1000236/*
237 * Authenticate user
238 */
Damien Miller62cee002000-09-23 17:15:56 +1100239
240typedef struct Authctxt Authctxt;
241typedef struct Authmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000242typedef struct identity Identity;
243typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100244
Damien Miller280ecfb2003-05-14 13:46:00 +1000245struct identity {
246 TAILQ_ENTRY(identity) next;
247 AuthenticationConnection *ac; /* set if agent supports key */
248 Key *key; /* public/private key */
249 char *filename; /* comment for agent-only keys */
250 int tried;
251 int isprivate; /* key points to the private key */
Damien Miller5ceddc32013-02-15 12:18:32 +1100252 int userprovided;
Damien Miller280ecfb2003-05-14 13:46:00 +1000253};
254TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100255
256struct Authctxt {
257 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000258 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100259 const char *host;
260 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100261 Authmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000262 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000263 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000264 /* pubkey */
Damien Miller280ecfb2003-05-14 13:46:00 +1000265 Idlist keys;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000266 AuthenticationConnection *agent;
267 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000268 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000269 /* kbd-interactive */
270 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000271 /* generic */
272 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100273};
274struct Authmethod {
275 char *name; /* string to compare against server's list */
276 int (*userauth)(Authctxt *authctxt);
Damien Miller01ed2272008-11-05 16:20:46 +1100277 void (*cleanup)(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100278 int *enabled; /* flag in option struct that enables method */
279 int *batch_flag; /* flag in option struct that disables method */
280};
281
Damien Miller630d6f42002-01-22 23:17:30 +1100282void input_userauth_success(int, u_int32_t, void *);
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100283void input_userauth_success_unexpected(int, u_int32_t, void *);
Damien Miller630d6f42002-01-22 23:17:30 +1100284void input_userauth_failure(int, u_int32_t, void *);
285void input_userauth_banner(int, u_int32_t, void *);
286void input_userauth_error(int, u_int32_t, void *);
287void input_userauth_info_req(int, u_int32_t, void *);
288void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000289void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller01ed2272008-11-05 16:20:46 +1100290void input_userauth_jpake_server_step1(int, u_int32_t, void *);
291void input_userauth_jpake_server_step2(int, u_int32_t, void *);
292void input_userauth_jpake_server_confirm(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100293
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000294int userauth_none(Authctxt *);
295int userauth_pubkey(Authctxt *);
296int userauth_passwd(Authctxt *);
297int userauth_kbdint(Authctxt *);
298int userauth_hostbased(Authctxt *);
Damien Miller01ed2272008-11-05 16:20:46 +1100299int userauth_jpake(Authctxt *);
300
301void userauth_jpake_cleanup(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100302
Darren Tucker0efd1552003-08-26 11:49:55 +1000303#ifdef GSSAPI
304int userauth_gssapi(Authctxt *authctxt);
305void input_gssapi_response(int type, u_int32_t, void *);
306void input_gssapi_token(int type, u_int32_t, void *);
307void input_gssapi_hash(int type, u_int32_t, void *);
308void input_gssapi_error(int, u_int32_t, void *);
309void input_gssapi_errtok(int, u_int32_t, void *);
310#endif
311
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000312void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000313
Damien Miller280ecfb2003-05-14 13:46:00 +1000314static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000315static void pubkey_prepare(Authctxt *);
316static void pubkey_cleanup(Authctxt *);
Damien Miller5ceddc32013-02-15 12:18:32 +1100317static Key *load_identity_file(char *, int);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000318
Ben Lindstrombba81212001-06-25 05:01:22 +0000319static Authmethod *authmethod_get(char *authlist);
320static Authmethod *authmethod_lookup(const char *name);
321static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100322
323Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000324#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100325 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000326 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100327 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000328 &options.gss_authentication,
329 NULL},
330#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000331 {"hostbased",
332 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100333 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000334 &options.hostbased_authentication,
335 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000336 {"publickey",
337 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100338 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000339 &options.pubkey_authentication,
340 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100341#ifdef JPAKE
342 {"jpake-01@openssh.com",
343 userauth_jpake,
344 userauth_jpake_cleanup,
345 &options.zero_knowledge_password_authentication,
346 &options.batch_mode},
347#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100348 {"keyboard-interactive",
349 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100350 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100351 &options.kbd_interactive_authentication,
352 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000353 {"password",
354 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100355 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000356 &options.password_authentication,
357 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100358 {"none",
359 userauth_none,
360 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100361 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100362 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100363 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100364};
365
366void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000367ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000368 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100369{
370 Authctxt authctxt;
371 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100372
Ben Lindstrom551ea372001-06-05 18:56:16 +0000373 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000374 options.kbd_interactive_authentication = 1;
375
Damien Miller62cee002000-09-23 17:15:56 +1100376 packet_start(SSH2_MSG_SERVICE_REQUEST);
377 packet_put_cstring("ssh-userauth");
378 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000379 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100380 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100381 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000382 if (type != SSH2_MSG_SERVICE_ACCEPT)
383 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100384 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100385 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000386 debug2("service_accept: %s", reply);
Darren Tuckera627d422013-06-02 07:31:17 +1000387 free(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100388 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000389 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100390 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100391 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000392 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100393
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000394 if (options.preferred_authentications == NULL)
395 options.preferred_authentications = authmethods_get();
396
Damien Miller62cee002000-09-23 17:15:56 +1100397 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000398 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000399 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100400 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000401 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100402 authctxt.host = host;
403 authctxt.service = "ssh-connection"; /* service name */
404 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100405 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000406 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000407 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000408 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000409 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100410 if (authctxt.method == NULL)
411 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100412
413 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100414 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100415
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000416 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100417 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
418 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000419 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100420 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
421
Damien Miller280ecfb2003-05-14 13:46:00 +1000422 pubkey_cleanup(&authctxt);
Damien Millerf842fcb2003-05-15 12:01:28 +1000423 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
424
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000425 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100426}
Damien Millerf842fcb2003-05-15 12:01:28 +1000427
Damien Miller62cee002000-09-23 17:15:56 +1100428void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000429userauth(Authctxt *authctxt, char *authlist)
430{
Damien Miller01ed2272008-11-05 16:20:46 +1100431 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
432 authctxt->method->cleanup(authctxt);
433
Darren Tuckera627d422013-06-02 07:31:17 +1000434 free(authctxt->methoddata);
435 authctxt->methoddata = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000436 if (authlist == NULL) {
437 authlist = authctxt->authlist;
438 } else {
Darren Tuckera627d422013-06-02 07:31:17 +1000439 free(authctxt->authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000440 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);
Darren Tuckera627d422013-06-02 07:31:17 +1000487 free(msg);
Damien Miller7ba0ca72008-07-17 18:57:06 +1000488 }
Darren Tuckera627d422013-06-02 07:31:17 +1000489 free(raw);
490 free(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 Tuckera627d422013-06-02 07:31:17 +1000501 free(authctxt->authlist);
502 authctxt->authlist = NULL;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100503 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
504 authctxt->method->cleanup(authctxt);
Darren Tuckera627d422013-06-02 07:31:17 +1000505 free(authctxt->methoddata);
506 authctxt->methoddata = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100507 authctxt->success = 1; /* break out */
508}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000509
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100510void
511input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
512{
513 Authctxt *authctxt = ctxt;
514
515 if (authctxt == NULL)
516 fatal("%s: no authentication context", __func__);
517
518 fatal("Unexpected authentication success during %s.",
519 authctxt->method->name);
520}
521
Damien Millerf7475d72008-11-03 19:26:18 +1100522/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100523void
Damien Miller630d6f42002-01-22 23:17:30 +1100524input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100525{
Damien Miller62cee002000-09-23 17:15:56 +1100526 Authctxt *authctxt = ctxt;
527 char *authlist = NULL;
528 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100529
530 if (authctxt == NULL)
531 fatal("input_userauth_failure: no authentication context");
532
Damien Miller874d77b2000-10-14 16:23:11 +1100533 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100534 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100535 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100536
Damien Miller62e9c4f2013-04-23 15:15:49 +1000537 if (partial != 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000538 logit("Authenticated with partial success.");
Damien Miller62e9c4f2013-04-23 15:15:49 +1000539 /* reset state */
540 pubkey_cleanup(authctxt);
541 pubkey_prepare(authctxt);
542 }
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);
Darren Tuckera627d422013-06-02 07:31:17 +1000595 free(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);
Darren Tuckera627d422013-06-02 07:31:17 +1000611 free(pkalg);
612 free(pkblob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000613
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) {
Darren Tuckera627d422013-06-02 07:31:17 +1000750 free(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
Darren Tuckera627d422013-06-02 07:31:17 +1000761 free(oidv);
Darren Tucker0efd1552003-08-26 11:49:55 +1000762
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
Darren Tuckera627d422013-06-02 07:31:17 +1000790 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000791
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;
Damien Millerd677ad12013-04-23 15:18:51 +1000807 OM_uint32 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 */
Damien Millerd677ad12013-04-23 15:18:51 +1000820 (void)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
Darren Tuckera627d422013-06-02 07:31:17 +1000823 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000824 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{
Darren Tucker0efd1552003-08-26 11:49:55 +1000833 char *msg;
834 char *lang;
835
Damien Millerd677ad12013-04-23 15:18:51 +1000836 /* maj */(void)packet_get_int();
837 /* min */(void)packet_get_int();
Darren Tucker0efd1552003-08-26 11:49:55 +1000838 msg=packet_get_string(NULL);
839 lang=packet_get_string(NULL);
840
841 packet_check_eom();
842
Damien Miller15d72a02005-11-05 15:07:33 +1100843 debug("Server GSSAPI Error:\n%s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000844 free(msg);
845 free(lang);
Darren Tucker0efd1552003-08-26 11:49:55 +1000846}
847#endif /* GSSAPI */
848
Damien Millereba71ba2000-04-29 23:57:08 +1000849int
Damien Miller874d77b2000-10-14 16:23:11 +1100850userauth_none(Authctxt *authctxt)
851{
852 /* initial userauth request */
853 packet_start(SSH2_MSG_USERAUTH_REQUEST);
854 packet_put_cstring(authctxt->server_user);
855 packet_put_cstring(authctxt->service);
856 packet_put_cstring(authctxt->method->name);
857 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100858 return 1;
859}
860
861int
Damien Miller62cee002000-09-23 17:15:56 +1100862userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000863{
Damien Millere247cc42000-05-07 12:03:14 +1000864 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000865 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000866 char *password;
Darren Tuckerab791692010-01-08 18:48:02 +1100867 const char *host = options.host_key_alias ? options.host_key_alias :
868 authctxt->host;
Damien Millereba71ba2000-04-29 23:57:08 +1000869
Damien Millerd3a18572000-06-07 19:55:44 +1000870 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000871 return 0;
872
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000873 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000874 error("Permission denied, please try again.");
875
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000876 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100877 authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +1000878 password = read_passphrase(prompt, 0);
879 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100880 packet_put_cstring(authctxt->server_user);
881 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100882 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000883 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000884 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000885 memset(password, 0, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000886 free(password);
Damien Miller9f643902001-11-12 11:02:52 +1100887 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000888 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000889
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000890 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000891 &input_userauth_passwd_changereq);
892
Damien Millereba71ba2000-04-29 23:57:08 +1000893 return 1;
894}
Damien Millerf7475d72008-11-03 19:26:18 +1100895
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000896/*
897 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
898 */
Damien Millerf7475d72008-11-03 19:26:18 +1100899/* ARGSUSED */
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000900void
Tim Ricec8549622002-03-31 12:49:38 -0800901input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000902{
903 Authctxt *authctxt = ctxt;
904 char *info, *lang, *password = NULL, *retype = NULL;
905 char prompt[150];
Darren Tuckerab791692010-01-08 18:48:02 +1100906 const char *host = options.host_key_alias ? options.host_key_alias :
907 authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000908
909 debug2("input_userauth_passwd_changereq");
910
911 if (authctxt == NULL)
912 fatal("input_userauth_passwd_changereq: "
913 "no authentication context");
914
915 info = packet_get_string(NULL);
916 lang = packet_get_string(NULL);
917 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000918 logit("%s", info);
Darren Tuckera627d422013-06-02 07:31:17 +1000919 free(info);
920 free(lang);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000921 packet_start(SSH2_MSG_USERAUTH_REQUEST);
922 packet_put_cstring(authctxt->server_user);
923 packet_put_cstring(authctxt->service);
924 packet_put_cstring(authctxt->method->name);
925 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000926 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000927 "Enter %.30s@%.128s's old password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100928 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000929 password = read_passphrase(prompt, 0);
930 packet_put_cstring(password);
931 memset(password, 0, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000932 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000933 password = NULL;
934 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000935 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000936 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100937 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000938 password = read_passphrase(prompt, RP_ALLOW_EOF);
939 if (password == NULL) {
940 /* bail out */
941 return;
942 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000943 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000944 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100945 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000946 retype = read_passphrase(prompt, 0);
947 if (strcmp(password, retype) != 0) {
948 memset(password, 0, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000949 free(password);
Damien Miller996acd22003-04-09 20:59:48 +1000950 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000951 password = NULL;
952 }
953 memset(retype, 0, strlen(retype));
Darren Tuckera627d422013-06-02 07:31:17 +1000954 free(retype);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000955 }
956 packet_put_cstring(password);
957 memset(password, 0, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000958 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000959 packet_add_padding(64);
960 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000961
962 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000963 &input_userauth_passwd_changereq);
964}
Damien Millereba71ba2000-04-29 23:57:08 +1000965
Damien Miller01ed2272008-11-05 16:20:46 +1100966#ifdef JPAKE
967static char *
968pw_encrypt(const char *password, const char *crypt_scheme, const char *salt)
969{
970 /* OpenBSD crypt(3) handles all of these */
971 if (strcmp(crypt_scheme, "crypt") == 0 ||
972 strcmp(crypt_scheme, "bcrypt") == 0 ||
973 strcmp(crypt_scheme, "md5crypt") == 0 ||
974 strcmp(crypt_scheme, "crypt-extended") == 0)
975 return xstrdup(crypt(password, salt));
976 error("%s: unsupported password encryption scheme \"%.100s\"",
977 __func__, crypt_scheme);
978 return NULL;
979}
980
981static BIGNUM *
982jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme,
983 const char *salt)
984{
985 char prompt[256], *password, *crypted;
986 u_char *secret;
987 u_int secret_len;
988 BIGNUM *ret;
989
990 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ",
991 authctxt->server_user, authctxt->host);
992 password = read_passphrase(prompt, 0);
993
994 if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) {
995 logit("Disabling %s authentication", authctxt->method->name);
996 authctxt->method->enabled = NULL;
997 /* Continue with an empty password to fail gracefully */
998 crypted = xstrdup("");
999 }
1000
1001#ifdef JPAKE_DEBUG
1002 debug3("%s: salt = %s", __func__, salt);
1003 debug3("%s: scheme = %s", __func__, crypt_scheme);
1004 debug3("%s: crypted = %s", __func__, crypted);
1005#endif
1006
1007 if (hash_buffer(crypted, strlen(crypted), EVP_sha256(),
1008 &secret, &secret_len) != 0)
1009 fatal("%s: hash_buffer", __func__);
1010
1011 bzero(password, strlen(password));
1012 bzero(crypted, strlen(crypted));
Darren Tuckera627d422013-06-02 07:31:17 +10001013 free(password);
1014 free(crypted);
Damien Miller01ed2272008-11-05 16:20:46 +11001015
1016 if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL)
1017 fatal("%s: BN_bin2bn (secret)", __func__);
1018 bzero(secret, secret_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001019 free(secret);
Damien Miller01ed2272008-11-05 16:20:46 +11001020
1021 return ret;
1022}
1023
1024/* ARGSUSED */
1025void
1026input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt)
1027{
1028 Authctxt *authctxt = ctxt;
1029 struct jpake_ctx *pctx = authctxt->methoddata;
1030 u_char *x3_proof, *x4_proof, *x2_s_proof;
1031 u_int x3_proof_len, x4_proof_len, x2_s_proof_len;
1032 char *crypt_scheme, *salt;
1033
1034 /* Disable this message */
1035 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL);
1036
1037 if ((pctx->g_x3 = BN_new()) == NULL ||
1038 (pctx->g_x4 = BN_new()) == NULL)
1039 fatal("%s: BN_new", __func__);
1040
1041 /* Fetch step 1 values */
1042 crypt_scheme = packet_get_string(NULL);
1043 salt = packet_get_string(NULL);
1044 pctx->server_id = packet_get_string(&pctx->server_id_len);
1045 packet_get_bignum2(pctx->g_x3);
1046 packet_get_bignum2(pctx->g_x4);
1047 x3_proof = packet_get_string(&x3_proof_len);
1048 x4_proof = packet_get_string(&x4_proof_len);
1049 packet_check_eom();
1050
1051 JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__));
1052
1053 /* Obtain password and derive secret */
1054 pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt);
1055 bzero(crypt_scheme, strlen(crypt_scheme));
1056 bzero(salt, strlen(salt));
Darren Tuckera627d422013-06-02 07:31:17 +10001057 free(crypt_scheme);
1058 free(salt);
Damien Miller01ed2272008-11-05 16:20:46 +11001059 JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__));
1060
1061 /* Calculate step 2 values */
1062 jpake_step2(pctx->grp, pctx->s, pctx->g_x1,
1063 pctx->g_x3, pctx->g_x4, pctx->x2,
1064 pctx->server_id, pctx->server_id_len,
1065 pctx->client_id, pctx->client_id_len,
1066 x3_proof, x3_proof_len,
1067 x4_proof, x4_proof_len,
1068 &pctx->a,
1069 &x2_s_proof, &x2_s_proof_len);
1070
1071 bzero(x3_proof, x3_proof_len);
1072 bzero(x4_proof, x4_proof_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001073 free(x3_proof);
1074 free(x4_proof);
Damien Miller01ed2272008-11-05 16:20:46 +11001075
1076 JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__));
1077
1078 /* Send values for step 2 */
1079 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2);
1080 packet_put_bignum2(pctx->a);
1081 packet_put_string(x2_s_proof, x2_s_proof_len);
1082 packet_send();
1083
1084 bzero(x2_s_proof, x2_s_proof_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001085 free(x2_s_proof);
Damien Miller01ed2272008-11-05 16:20:46 +11001086
1087 /* Expect step 2 packet from peer */
1088 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2,
1089 input_userauth_jpake_server_step2);
1090}
1091
1092/* ARGSUSED */
1093void
1094input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt)
1095{
1096 Authctxt *authctxt = ctxt;
1097 struct jpake_ctx *pctx = authctxt->methoddata;
1098 u_char *x4_s_proof;
1099 u_int x4_s_proof_len;
1100
1101 /* Disable this message */
1102 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL);
1103
1104 if ((pctx->b = BN_new()) == NULL)
1105 fatal("%s: BN_new", __func__);
1106
1107 /* Fetch step 2 values */
1108 packet_get_bignum2(pctx->b);
1109 x4_s_proof = packet_get_string(&x4_s_proof_len);
1110 packet_check_eom();
1111
1112 JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__));
1113
1114 /* Derive shared key and calculate confirmation hash */
1115 jpake_key_confirm(pctx->grp, pctx->s, pctx->b,
1116 pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4,
1117 pctx->client_id, pctx->client_id_len,
1118 pctx->server_id, pctx->server_id_len,
1119 session_id2, session_id2_len,
1120 x4_s_proof, x4_s_proof_len,
1121 &pctx->k,
1122 &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len);
1123
1124 bzero(x4_s_proof, x4_s_proof_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001125 free(x4_s_proof);
Damien Miller01ed2272008-11-05 16:20:46 +11001126
1127 JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__));
1128
1129 /* Send key confirmation proof */
1130 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM);
1131 packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
1132 packet_send();
1133
1134 /* Expect confirmation from peer */
1135 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM,
1136 input_userauth_jpake_server_confirm);
1137}
1138
1139/* ARGSUSED */
1140void
1141input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt)
1142{
1143 Authctxt *authctxt = ctxt;
1144 struct jpake_ctx *pctx = authctxt->methoddata;
1145
1146 /* Disable this message */
1147 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL);
1148
1149 pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len);
1150 packet_check_eom();
1151
1152 JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__));
1153
1154 /* Verify expected confirmation hash */
1155 if (jpake_check_confirm(pctx->k,
1156 pctx->server_id, pctx->server_id_len,
1157 session_id2, session_id2_len,
1158 pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1)
1159 debug("%s: %s success", __func__, authctxt->method->name);
1160 else {
1161 debug("%s: confirmation mismatch", __func__);
1162 /* XXX stash this so if auth succeeds then we can warn/kill */
1163 }
1164
1165 userauth_jpake_cleanup(authctxt);
1166}
1167#endif /* JPAKE */
1168
Ben Lindstrombba81212001-06-25 05:01:22 +00001169static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001170identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1171 u_char *data, u_int datalen)
1172{
1173 Key *prv;
1174 int ret;
1175
1176 /* the agent supports this key */
1177 if (id->ac)
1178 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1179 data, datalen));
1180 /*
1181 * we have already loaded the private key or
1182 * the private key is stored in external hardware
1183 */
1184 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1185 return (key_sign(id->key, sigp, lenp, data, datalen));
1186 /* load the private key from the file */
Damien Miller5ceddc32013-02-15 12:18:32 +11001187 if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL)
Damien Miller280ecfb2003-05-14 13:46:00 +10001188 return (-1);
1189 ret = key_sign(prv, sigp, lenp, data, datalen);
1190 key_free(prv);
1191 return (ret);
1192}
1193
1194static int
1195sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +10001196{
1197 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001198 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +00001199 u_int bloblen, slen;
Darren Tucker502d3842003-06-28 12:38:01 +10001200 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001201 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001202 int have_sig = 1;
Damien Miller4e270b02010-04-16 15:56:21 +10001203 char *fp;
Damien Millereba71ba2000-04-29 23:57:08 +10001204
Damien Miller4e270b02010-04-16 15:56:21 +10001205 fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
1206 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
Darren Tuckera627d422013-06-02 07:31:17 +10001207 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001208
Damien Miller280ecfb2003-05-14 13:46:00 +10001209 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001210 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +00001211 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001212 return 0;
1213 }
Damien Millereba71ba2000-04-29 23:57:08 +10001214 /* data to be signed */
1215 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +11001216 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +10001217 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001218 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +11001219 } else {
1220 buffer_put_string(&b, session_id2, session_id2_len);
1221 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +10001222 }
Damien Millereba71ba2000-04-29 23:57:08 +10001223 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001224 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +10001225 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +00001226 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +10001227 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +11001228 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +00001229 if (datafellows & SSH_BUG_PKAUTH) {
1230 buffer_put_char(&b, have_sig);
1231 } else {
1232 buffer_put_cstring(&b, authctxt->method->name);
1233 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +10001234 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +00001235 }
Damien Millereba71ba2000-04-29 23:57:08 +10001236 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +10001237
1238 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +10001239 ret = identity_sign(id, &signature, &slen,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001240 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +10001241 if (ret == -1) {
Darren Tuckera627d422013-06-02 07:31:17 +10001242 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +10001243 buffer_free(&b);
1244 return 0;
1245 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001246#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +10001247 buffer_dump(&b);
1248#endif
Ben Lindstromd121f612000-12-03 17:00:47 +00001249 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +10001250 buffer_clear(&b);
1251 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001252 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +10001253 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001254 buffer_put_cstring(&b, authctxt->server_user);
1255 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +11001256 buffer_put_cstring(&b, authctxt->method->name);
1257 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +00001258 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001259 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +10001260 buffer_put_string(&b, blob, bloblen);
1261 }
Darren Tuckera627d422013-06-02 07:31:17 +10001262 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001263
Damien Millereba71ba2000-04-29 23:57:08 +10001264 /* append signature */
1265 buffer_put_string(&b, signature, slen);
Darren Tuckera627d422013-06-02 07:31:17 +10001266 free(signature);
Damien Millereba71ba2000-04-29 23:57:08 +10001267
1268 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +10001269 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +11001270 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +10001271 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +10001272
1273 /* put remaining data from buffer into packet */
1274 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1275 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1276 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +10001277 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +10001278
1279 return 1;
Damien Miller994cf142000-07-21 10:19:44 +10001280}
1281
Ben Lindstrombba81212001-06-25 05:01:22 +00001282static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001283send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001284{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001285 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001286 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +10001287
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001288 debug3("send_pubkey_test");
1289
Damien Miller280ecfb2003-05-14 13:46:00 +10001290 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001291 /* we cannot handle this key */
1292 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +10001293 return 0;
1294 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001295 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001296 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001297
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001298 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1299 packet_put_cstring(authctxt->server_user);
1300 packet_put_cstring(authctxt->service);
1301 packet_put_cstring(authctxt->method->name);
1302 packet_put_char(have_sig);
1303 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001304 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001305 packet_put_string(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001306 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001307 packet_send();
1308 return 1;
1309}
1310
Ben Lindstrombba81212001-06-25 05:01:22 +00001311static Key *
Damien Miller5ceddc32013-02-15 12:18:32 +11001312load_identity_file(char *filename, int userprovided)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001313{
1314 Key *private;
1315 char prompt[300], *passphrase;
Darren Tuckerd4c86b12010-01-12 19:41:22 +11001316 int perm_ok = 0, quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001317 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001318
Ben Lindstrom329782e2001-03-10 17:08:59 +00001319 if (stat(filename, &st) < 0) {
Damien Miller5ceddc32013-02-15 12:18:32 +11001320 (userprovided ? logit : debug3)("no such identity: %s: %s",
1321 filename, strerror(errno));
Ben Lindstrom329782e2001-03-10 17:08:59 +00001322 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));
Darren Tuckera627d422013-06-02 07:31:17 +10001343 free(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 Millercb6b68b2012-12-03 09:49:52 +11001361 Identity *id, *id2, *tmp;
Damien Miller280ecfb2003-05-14 13:46:00 +10001362 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
Damien Millercb6b68b2012-12-03 09:49:52 +11001373 /* list of keys stored in the filesystem and PKCS#11 */
Damien Miller280ecfb2003-05-14 13:46:00 +10001374 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]);
Darren Tucker19104782013-04-05 11:13:08 +11001384 id->userprovided = options.identity_file_userprovided[i];
Damien Miller280ecfb2003-05-14 13:46:00 +10001385 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001386 }
Damien Millercb6b68b2012-12-03 09:49:52 +11001387 /* Prefer PKCS11 keys that are explicitly listed */
1388 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1389 if (id->key == NULL || (id->key->flags & KEY_FLAG_EXT) == 0)
1390 continue;
1391 found = 0;
1392 TAILQ_FOREACH(id2, &files, next) {
1393 if (id2->key == NULL ||
1394 (id2->key->flags & KEY_FLAG_EXT) != 0)
1395 continue;
1396 if (key_equal(id->key, id2->key)) {
1397 TAILQ_REMOVE(&files, id, next);
1398 TAILQ_INSERT_TAIL(preferred, id, next);
1399 found = 1;
1400 break;
1401 }
1402 }
1403 /* If IdentitiesOnly set and key not found then don't use it */
1404 if (!found && options.identities_only) {
1405 TAILQ_REMOVE(&files, id, next);
Darren Tucker54da6be2013-05-16 20:25:04 +10001406 bzero(id, sizeof(*id));
Damien Millercb6b68b2012-12-03 09:49:52 +11001407 free(id);
1408 }
1409 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001410 /* list of keys supported by the agent */
1411 if ((ac = ssh_get_authentication_connection())) {
1412 for (key = ssh_get_first_identity(ac, &comment, 2);
1413 key != NULL;
1414 key = ssh_get_next_identity(ac, &comment, 2)) {
1415 found = 0;
1416 TAILQ_FOREACH(id, &files, next) {
Damien Millera8e06ce2003-11-21 23:48:55 +11001417 /* agent keys from the config file are preferred */
Damien Miller280ecfb2003-05-14 13:46:00 +10001418 if (key_equal(key, id->key)) {
1419 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001420 free(comment);
Damien Miller280ecfb2003-05-14 13:46:00 +10001421 TAILQ_REMOVE(&files, id, next);
1422 TAILQ_INSERT_TAIL(preferred, id, next);
1423 id->ac = ac;
1424 found = 1;
1425 break;
1426 }
1427 }
Damien Millerbd394c32004-03-08 23:12:36 +11001428 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001429 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001430 id->key = key;
1431 id->filename = comment;
1432 id->ac = ac;
1433 TAILQ_INSERT_TAIL(&agent, id, next);
1434 }
1435 }
1436 /* append remaining agent keys */
1437 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1438 TAILQ_REMOVE(&agent, id, next);
1439 TAILQ_INSERT_TAIL(preferred, id, next);
1440 }
1441 authctxt->agent = ac;
Damien Miller62cee002000-09-23 17:15:56 +11001442 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001443 /* append remaining keys from the config file */
1444 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1445 TAILQ_REMOVE(&files, id, next);
1446 TAILQ_INSERT_TAIL(preferred, id, next);
1447 }
1448 TAILQ_FOREACH(id, preferred, next) {
Damien Miller5ceddc32013-02-15 12:18:32 +11001449 debug2("key: %s (%p),%s", id->filename, id->key,
1450 id->userprovided ? " explicit" : "");
Damien Miller280ecfb2003-05-14 13:46:00 +10001451 }
1452}
1453
1454static void
1455pubkey_cleanup(Authctxt *authctxt)
1456{
1457 Identity *id;
1458
1459 if (authctxt->agent != NULL)
1460 ssh_close_authentication_connection(authctxt->agent);
1461 for (id = TAILQ_FIRST(&authctxt->keys); id;
1462 id = TAILQ_FIRST(&authctxt->keys)) {
1463 TAILQ_REMOVE(&authctxt->keys, id, next);
1464 if (id->key)
1465 key_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +10001466 free(id->filename);
1467 free(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001468 }
Damien Millereba71ba2000-04-29 23:57:08 +10001469}
1470
Damien Miller62cee002000-09-23 17:15:56 +11001471int
1472userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001473{
Damien Miller280ecfb2003-05-14 13:46:00 +10001474 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001475 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001476
Damien Miller280ecfb2003-05-14 13:46:00 +10001477 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1478 if (id->tried++)
1479 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001480 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001481 TAILQ_REMOVE(&authctxt->keys, id, next);
1482 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1483 /*
1484 * send a test message if we have the public key. for
1485 * encrypted keys we cannot do this and have to load the
1486 * private key instead
1487 */
1488 if (id->key && id->key->type != KEY_RSA1) {
Damien Miller88680652010-04-16 15:53:43 +10001489 debug("Offering %s public key: %s", key_type(id->key),
1490 id->filename);
Damien Miller280ecfb2003-05-14 13:46:00 +10001491 sent = send_pubkey_test(authctxt, id);
1492 } else if (id->key == NULL) {
1493 debug("Trying private key: %s", id->filename);
Damien Miller5ceddc32013-02-15 12:18:32 +11001494 id->key = load_identity_file(id->filename,
1495 id->userprovided);
Damien Miller280ecfb2003-05-14 13:46:00 +10001496 if (id->key != NULL) {
1497 id->isprivate = 1;
1498 sent = sign_and_send_pubkey(authctxt, id);
1499 key_free(id->key);
1500 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001501 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001502 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001503 if (sent)
1504 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001505 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001506 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001507}
Damien Millereba71ba2000-04-29 23:57:08 +10001508
Damien Miller874d77b2000-10-14 16:23:11 +11001509/*
1510 * Send userauth request message specifying keyboard-interactive method.
1511 */
1512int
1513userauth_kbdint(Authctxt *authctxt)
1514{
1515 static int attempt = 0;
1516
1517 if (attempt++ >= options.number_of_password_prompts)
1518 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001519 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1520 if (attempt > 1 && !authctxt->info_req_seen) {
1521 debug3("userauth_kbdint: disable: no info_req_seen");
1522 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1523 return 0;
1524 }
Damien Miller874d77b2000-10-14 16:23:11 +11001525
1526 debug2("userauth_kbdint");
1527 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1528 packet_put_cstring(authctxt->server_user);
1529 packet_put_cstring(authctxt->service);
1530 packet_put_cstring(authctxt->method->name);
1531 packet_put_cstring(""); /* lang */
1532 packet_put_cstring(options.kbd_interactive_devices ?
1533 options.kbd_interactive_devices : "");
1534 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001535
1536 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1537 return 1;
1538}
1539
1540/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001541 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001542 */
1543void
Damien Miller630d6f42002-01-22 23:17:30 +11001544input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001545{
1546 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001547 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001548 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001549 int echo = 0;
1550
1551 debug2("input_userauth_info_req");
1552
1553 if (authctxt == NULL)
1554 fatal("input_userauth_info_req: no authentication context");
1555
Ben Lindstrom7d199962001-09-12 18:29:00 +00001556 authctxt->info_req_seen = 1;
1557
Damien Miller874d77b2000-10-14 16:23:11 +11001558 name = packet_get_string(NULL);
1559 inst = packet_get_string(NULL);
1560 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001561 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001562 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001563 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001564 logit("%s", inst);
Darren Tuckera627d422013-06-02 07:31:17 +10001565 free(name);
1566 free(inst);
1567 free(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001568
1569 num_prompts = packet_get_int();
1570 /*
1571 * Begin to build info response packet based on prompts requested.
1572 * We commit to providing the correct number of responses, so if
1573 * further on we run into a problem that prevents this, we have to
1574 * be sure and clean this up and send a correct error response.
1575 */
1576 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1577 packet_put_int(num_prompts);
1578
Ben Lindstrom551ea372001-06-05 18:56:16 +00001579 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001580 for (i = 0; i < num_prompts; i++) {
1581 prompt = packet_get_string(NULL);
1582 echo = packet_get_char();
1583
Ben Lindstrom949974b2001-06-25 05:20:31 +00001584 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001585
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001586 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +11001587 memset(response, 0, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +10001588 free(response);
1589 free(prompt);
Damien Miller874d77b2000-10-14 16:23:11 +11001590 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001591 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001592
Damien Miller9f643902001-11-12 11:02:52 +11001593 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001594 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001595}
Damien Miller62cee002000-09-23 17:15:56 +11001596
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001597static int
Ben Lindstrom5c385522002-06-23 21:23:20 +00001598ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001599 u_char *data, u_int datalen)
1600{
1601 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001602 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001603 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001604 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001605
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001606 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001607
Ben Lindstrom5206b952002-06-06 19:59:29 +00001608 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001609 error("ssh_keysign: not installed: %s", strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001610 return -1;
1611 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001612 if (fflush(stdout) != 0)
1613 error("ssh_keysign: fflush: %s", strerror(errno));
1614 if (pipe(to) < 0) {
1615 error("ssh_keysign: pipe: %s", strerror(errno));
1616 return -1;
1617 }
1618 if (pipe(from) < 0) {
1619 error("ssh_keysign: pipe: %s", strerror(errno));
1620 return -1;
1621 }
1622 if ((pid = fork()) < 0) {
1623 error("ssh_keysign: fork: %s", strerror(errno));
1624 return -1;
1625 }
1626 if (pid == 0) {
Darren Tucker6e7fe1c2010-01-08 17:07:22 +11001627 /* keep the socket on exec */
1628 fcntl(packet_get_connection_in(), F_SETFD, 0);
Damien Miller2e5fe882006-06-13 13:10:00 +10001629 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001630 close(from[0]);
1631 if (dup2(from[1], STDOUT_FILENO) < 0)
1632 fatal("ssh_keysign: dup2: %s", strerror(errno));
1633 close(to[1]);
1634 if (dup2(to[0], STDIN_FILENO) < 0)
1635 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001636 close(from[1]);
1637 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001638 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001639 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1640 strerror(errno));
1641 }
1642 close(from[1]);
1643 close(to[0]);
1644
1645 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001646 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001647 buffer_put_string(&b, data, datalen);
Damien Miller51bf11f2003-11-17 21:20:47 +11001648 if (ssh_msg_send(to[1], version, &b) == -1)
1649 fatal("ssh_keysign: couldn't send request");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001650
Damien Miller901119b2002-10-04 11:10:04 +10001651 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001652 error("ssh_keysign: no reply");
Damien Millerfb1310e2004-01-21 11:02:50 +11001653 buffer_free(&b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001654 return -1;
1655 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001656 close(from[0]);
1657 close(to[1]);
1658
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001659 while (waitpid(pid, &status, 0) < 0)
1660 if (errno != EINTR)
1661 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001662
Ben Lindstrom5206b952002-06-06 19:59:29 +00001663 if (buffer_get_char(&b) != version) {
1664 error("ssh_keysign: bad version");
Damien Millerfb1310e2004-01-21 11:02:50 +11001665 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001666 return -1;
1667 }
1668 *sigp = buffer_get_string(&b, lenp);
Damien Millerfb1310e2004-01-21 11:02:50 +11001669 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001670
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001671 return 0;
1672}
1673
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001674int
1675userauth_hostbased(Authctxt *authctxt)
1676{
1677 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001678 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001679 Buffer b;
1680 u_char *signature, *blob;
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001681 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001682 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001683 u_int blen, slen;
Darren Tucker5246df42010-01-08 18:50:46 +11001684 int ok, i, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001685
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001686 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001687 for (i = 0; i < sensitive->nkeys; i++) {
1688 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001689 if (private && private->type != KEY_RSA1) {
1690 found = 1;
1691 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001692 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001693 break;
1694 }
1695 }
1696 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001697 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001698 return 0;
1699 }
1700 if (key_to_blob(private, &blob, &blen) == 0) {
1701 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001702 return 0;
1703 }
Damien Miller91c18472001-11-12 11:02:03 +11001704 /* figure out a name for the client host */
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001705 p = get_local_name(packet_get_connection_in());
Damien Miller91c18472001-11-12 11:02:03 +11001706 if (p == NULL) {
1707 error("userauth_hostbased: cannot get local ipaddr/name");
1708 key_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001709 free(blob);
Damien Miller91c18472001-11-12 11:02:03 +11001710 return 0;
1711 }
Damien Miller07d86be2006-03-26 14:19:21 +11001712 xasprintf(&chost, "%s.", p);
Damien Miller91c18472001-11-12 11:02:03 +11001713 debug2("userauth_hostbased: chost %s", chost);
Darren Tuckera627d422013-06-02 07:31:17 +10001714 free(p);
Damien Miller91c18472001-11-12 11:02:03 +11001715
Ben Lindstrom671388f2001-04-19 20:40:45 +00001716 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1717 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001718 pkalg = xstrdup(key_ssh_name(private));
1719 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001720 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001721 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001722 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1723 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001724 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001725 buffer_put_cstring(&b, authctxt->method->name);
1726 buffer_put_cstring(&b, pkalg);
1727 buffer_put_string(&b, blob, blen);
1728 buffer_put_cstring(&b, chost);
1729 buffer_put_cstring(&b, authctxt->local_user);
1730#ifdef DEBUG_PK
1731 buffer_dump(&b);
1732#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001733 if (sensitive->external_keysign)
1734 ok = ssh_keysign(private, &signature, &slen,
1735 buffer_ptr(&b), buffer_len(&b));
1736 else
1737 ok = key_sign(private, &signature, &slen,
1738 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001739 key_free(private);
1740 buffer_free(&b);
1741 if (ok != 0) {
1742 error("key_sign failed");
Darren Tuckera627d422013-06-02 07:31:17 +10001743 free(chost);
1744 free(pkalg);
1745 free(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001746 return 0;
1747 }
1748 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1749 packet_put_cstring(authctxt->server_user);
1750 packet_put_cstring(authctxt->service);
1751 packet_put_cstring(authctxt->method->name);
1752 packet_put_cstring(pkalg);
1753 packet_put_string(blob, blen);
1754 packet_put_cstring(chost);
1755 packet_put_cstring(authctxt->local_user);
1756 packet_put_string(signature, slen);
1757 memset(signature, 's', slen);
Darren Tuckera627d422013-06-02 07:31:17 +10001758 free(signature);
1759 free(chost);
1760 free(pkalg);
1761 free(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001762
1763 packet_send();
1764 return 1;
1765}
1766
Damien Miller01ed2272008-11-05 16:20:46 +11001767#ifdef JPAKE
1768int
1769userauth_jpake(Authctxt *authctxt)
1770{
1771 struct jpake_ctx *pctx;
1772 u_char *x1_proof, *x2_proof;
1773 u_int x1_proof_len, x2_proof_len;
1774 static int attempt = 0; /* XXX share with userauth_password's? */
1775
1776 if (attempt++ >= options.number_of_password_prompts)
1777 return 0;
1778 if (attempt != 1)
1779 error("Permission denied, please try again.");
1780
1781 if (authctxt->methoddata != NULL)
1782 fatal("%s: authctxt->methoddata already set (%p)",
1783 __func__, authctxt->methoddata);
1784
1785 authctxt->methoddata = pctx = jpake_new();
1786
1787 /*
1788 * Send request immediately, to get the protocol going while
1789 * we do the initial computations.
1790 */
1791 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1792 packet_put_cstring(authctxt->server_user);
1793 packet_put_cstring(authctxt->service);
1794 packet_put_cstring(authctxt->method->name);
1795 packet_send();
1796 packet_write_wait();
1797
1798 jpake_step1(pctx->grp,
1799 &pctx->client_id, &pctx->client_id_len,
1800 &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2,
1801 &x1_proof, &x1_proof_len,
1802 &x2_proof, &x2_proof_len);
1803
1804 JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__));
1805
1806 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1);
1807 packet_put_string(pctx->client_id, pctx->client_id_len);
1808 packet_put_bignum2(pctx->g_x1);
1809 packet_put_bignum2(pctx->g_x2);
1810 packet_put_string(x1_proof, x1_proof_len);
1811 packet_put_string(x2_proof, x2_proof_len);
1812 packet_send();
1813
1814 bzero(x1_proof, x1_proof_len);
1815 bzero(x2_proof, x2_proof_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001816 free(x1_proof);
1817 free(x2_proof);
Damien Miller01ed2272008-11-05 16:20:46 +11001818
1819 /* Expect step 1 packet from peer */
1820 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1,
1821 input_userauth_jpake_server_step1);
Darren Tucker2f29a8c2009-10-24 11:47:58 +11001822 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS,
1823 &input_userauth_success_unexpected);
Damien Miller01ed2272008-11-05 16:20:46 +11001824
1825 return 1;
1826}
1827
1828void
1829userauth_jpake_cleanup(Authctxt *authctxt)
1830{
1831 debug3("%s: clean up", __func__);
1832 if (authctxt->methoddata != NULL) {
1833 jpake_free(authctxt->methoddata);
1834 authctxt->methoddata = NULL;
1835 }
Darren Tucker2f29a8c2009-10-24 11:47:58 +11001836 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
Damien Miller01ed2272008-11-05 16:20:46 +11001837}
1838#endif /* JPAKE */
1839
Damien Miller62cee002000-09-23 17:15:56 +11001840/* find auth method */
1841
Damien Miller62cee002000-09-23 17:15:56 +11001842/*
1843 * given auth method name, if configurable options permit this method fill
1844 * in auth_ident field and return true, otherwise return false.
1845 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001846static int
Damien Miller62cee002000-09-23 17:15:56 +11001847authmethod_is_enabled(Authmethod *method)
1848{
1849 if (method == NULL)
1850 return 0;
1851 /* return false if options indicate this method is disabled */
1852 if (method->enabled == NULL || *method->enabled == 0)
1853 return 0;
1854 /* return false if batch mode is enabled but method needs interactive mode */
1855 if (method->batch_flag != NULL && *method->batch_flag != 0)
1856 return 0;
1857 return 1;
1858}
1859
Ben Lindstrombba81212001-06-25 05:01:22 +00001860static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001861authmethod_lookup(const char *name)
1862{
1863 Authmethod *method = NULL;
1864 if (name != NULL)
1865 for (method = authmethods; method->name != NULL; method++)
1866 if (strcmp(name, method->name) == 0)
1867 return method;
1868 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1869 return NULL;
1870}
1871
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001872/* XXX internal state */
1873static Authmethod *current = NULL;
1874static char *supported = NULL;
1875static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001876
Damien Miller62cee002000-09-23 17:15:56 +11001877/*
1878 * Given the authentication method list sent by the server, return the
1879 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001880 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001881 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001882static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001883authmethod_get(char *authlist)
1884{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001885 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001886 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001887
Damien Miller62cee002000-09-23 17:15:56 +11001888 /* Use a suitable default if we're passed a nil list. */
1889 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001890 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001891
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001892 if (supported == NULL || strcmp(authlist, supported) != 0) {
1893 debug3("start over, passed a different list %s", authlist);
Darren Tuckera627d422013-06-02 07:31:17 +10001894 free(supported);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001895 supported = xstrdup(authlist);
1896 preferred = options.preferred_authentications;
1897 debug3("preferred %s", preferred);
1898 current = NULL;
1899 } else if (current != NULL && authmethod_is_enabled(current))
1900 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001901
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001902 for (;;) {
1903 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001904 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001905 current = NULL;
1906 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001907 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001908 preferred += next;
1909 debug3("authmethod_lookup %s", name);
1910 debug3("remaining preferred: %s", preferred);
1911 if ((current = authmethod_lookup(name)) != NULL &&
1912 authmethod_is_enabled(current)) {
1913 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001914 debug("Next authentication method: %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10001915 free(name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001916 return current;
1917 }
Damien Millereba71ba2000-04-29 23:57:08 +10001918 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001919}
Damien Miller62cee002000-09-23 17:15:56 +11001920
Ben Lindstrom79073822001-07-04 03:42:30 +00001921static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001922authmethods_get(void)
1923{
1924 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001925 Buffer b;
1926 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001927
Damien Miller0e3b8722002-01-22 23:26:38 +11001928 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001929 for (method = authmethods; method->name != NULL; method++) {
1930 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001931 if (buffer_len(&b) > 0)
1932 buffer_append(&b, ",", 1);
1933 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001934 }
Damien Miller62cee002000-09-23 17:15:56 +11001935 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001936 buffer_append(&b, "\0", 1);
1937 list = xstrdup(buffer_ptr(&b));
1938 buffer_free(&b);
1939 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001940}
Damien Miller01ed2272008-11-05 16:20:46 +11001941