blob: 775103185eddc7c1e932b8a06faea4643423e355 [file] [log] [blame]
djm@openbsd.orgf9eca242015-07-30 00:01:34 +00001/* $OpenBSD: sshconnect2.c,v 1.226 2015/07/30 00:01:34 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"
Darren Tuckere608ca22004-05-13 16:15:47 +100064#include "misc.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100065#include "readconf.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"
djm@openbsd.org141efe42015-01-14 20:05:27 +000073#include "ssherr.h"
Damien Miller874d77b2000-10-14 16:23:11 +110074
Darren Tucker0efd1552003-08-26 11:49:55 +100075#ifdef GSSAPI
76#include "ssh-gss.h"
77#endif
78
Damien Millereba71ba2000-04-29 23:57:08 +100079/* import */
80extern char *client_version_string;
81extern char *server_version_string;
82extern Options options;
83
84/*
85 * SSH2 key exchange
86 */
87
Ben Lindstrom46c16222000-12-22 01:43:59 +000088u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +100089u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100090
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000091char *xxx_host;
92struct sockaddr *xxx_hostaddr;
93
Ben Lindstrombba81212001-06-25 05:01:22 +000094static int
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000095verify_host_key_callback(Key *hostkey, struct ssh *ssh)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000096{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000097 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +100098 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000099 return 0;
100}
101
Damien Millerd925dcd2010-12-01 12:21:51 +1100102static char *
103order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
104{
105 char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
106 size_t maxlen;
107 struct hostkeys *hostkeys;
108 int ktype;
Damien Miller295ee632011-05-29 21:42:31 +1000109 u_int i;
Damien Millerd925dcd2010-12-01 12:21:51 +1100110
111 /* Find all hostkeys for this hostname */
112 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
113 hostkeys = init_hostkeys();
Damien Miller295ee632011-05-29 21:42:31 +1000114 for (i = 0; i < options.num_user_hostfiles; i++)
115 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
116 for (i = 0; i < options.num_system_hostfiles; i++)
117 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
Damien Millerd925dcd2010-12-01 12:21:51 +1100118
119 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
120 maxlen = strlen(avail) + 1;
121 first = xmalloc(maxlen);
122 last = xmalloc(maxlen);
123 *first = *last = '\0';
124
125#define ALG_APPEND(to, from) \
126 do { \
127 if (*to != '\0') \
128 strlcat(to, ",", maxlen); \
129 strlcat(to, from, maxlen); \
130 } while (0)
131
132 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000133 if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
Damien Millerd925dcd2010-12-01 12:21:51 +1100134 fatal("%s: unknown alg %s", __func__, alg);
135 if (lookup_key_in_hostkeys_by_type(hostkeys,
djm@openbsd.org141efe42015-01-14 20:05:27 +0000136 sshkey_type_plain(ktype), NULL))
Damien Millerd925dcd2010-12-01 12:21:51 +1100137 ALG_APPEND(first, alg);
138 else
139 ALG_APPEND(last, alg);
140 }
141#undef ALG_APPEND
djm@openbsd.org35d60222015-01-18 13:33:34 +0000142 xasprintf(&ret, "%s%s%s", first,
143 (*first == '\0' || *last == '\0') ? "" : ",", last);
Damien Millerd925dcd2010-12-01 12:21:51 +1100144 if (*first != '\0')
145 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
146
Darren Tuckera627d422013-06-02 07:31:17 +1000147 free(first);
148 free(last);
149 free(hostname);
150 free(oavail);
Damien Millerd925dcd2010-12-01 12:21:51 +1100151 free_hostkeys(hostkeys);
152
153 return ret;
154}
155
Damien Millereba71ba2000-04-29 23:57:08 +1000156void
Damien Millerd925dcd2010-12-01 12:21:51 +1100157ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
Damien Miller874d77b2000-10-14 16:23:11 +1100158{
Damien Miller9235a032014-04-20 13:17:20 +1000159 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000160 struct kex *kex;
markus@openbsd.org57e783c2015-01-20 20:16:21 +0000161 int r;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000162
163 xxx_host = host;
164 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100165
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000166 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(
167 options.kex_algorithms);
Damien Millera0ff4662001-03-30 10:49:35 +1000168 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000169 compat_cipher_proposal(options.ciphers);
Damien Millera0ff4662001-03-30 10:49:35 +1000170 myproposal[PROPOSAL_ENC_ALGS_STOC] =
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000171 compat_cipher_proposal(options.ciphers);
Damien Miller874d77b2000-10-14 16:23:11 +1100172 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000173 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000174 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100175 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000176 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000177 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100178 }
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000179 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
180 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
181 if (options.hostkeyalgorithms != NULL) {
182 if (kex_assemble_names(KEX_DEFAULT_PK_ALG,
183 &options.hostkeyalgorithms) != 0)
184 fatal("%s: kex_assemble_namelist", __func__);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100185 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100186 compat_pkalg_proposal(options.hostkeyalgorithms);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000187 } else {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000188 /* Enforce default */
189 options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG);
Damien Millerd925dcd2010-12-01 12:21:51 +1100190 /* Prefer algorithms that we already have keys for */
191 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100192 compat_pkalg_proposal(
193 order_hostkeyalgs(host, hostaddr, port));
Damien Millerd925dcd2010-12-01 12:21:51 +1100194 }
Damien Miller874d77b2000-10-14 16:23:11 +1100195
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000196 if (options.rekey_limit || options.rekey_interval)
197 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
198 (time_t)options.rekey_interval);
Damien Millera5539d22003-04-09 20:50:06 +1000199
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000200 /* start key exchange */
markus@openbsd.org57e783c2015-01-20 20:16:21 +0000201 if ((r = kex_setup(active_state, myproposal)) != 0)
202 fatal("kex_setup: %s", ssh_err(r));
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000203 kex = active_state->kex;
Damien Miller1f0311c2014-05-15 14:24:09 +1000204#ifdef WITH_OPENSSL
Damien Miller8e7fb332003-02-24 12:03:03 +1100205 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000206 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100207 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100208 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100209# ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000210 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100211# endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000212#endif
Damien Miller1e124262013-11-04 08:26:52 +1100213 kex->kex[KEX_C25519_SHA256] = kexc25519_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000214 kex->client_version_string=client_version_string;
215 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000216 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100217
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000218 dispatch_run(DISPATCH_BLOCK, &kex->done, active_state);
Damien Miller874d77b2000-10-14 16:23:11 +1100219
Darren Tucker36331b52010-01-08 16:50:41 +1100220 if (options.use_roaming && !kex->roaming) {
221 debug("Roaming not allowed by server");
222 options.use_roaming = 0;
223 }
224
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000225 session_id2 = kex->session_id;
226 session_id2_len = kex->session_id_len;
227
Damien Miller874d77b2000-10-14 16:23:11 +1100228#ifdef DEBUG_KEXDH
229 /* send 1st encrypted/maced/compressed message */
230 packet_start(SSH2_MSG_IGNORE);
231 packet_put_cstring("markus");
232 packet_send();
233 packet_write_wait();
234#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000235}
Damien Millerb1715dc2000-05-30 13:44:51 +1000236
Damien Millereba71ba2000-04-29 23:57:08 +1000237/*
238 * Authenticate user
239 */
Damien Miller62cee002000-09-23 17:15:56 +1100240
djm@openbsd.org141efe42015-01-14 20:05:27 +0000241typedef struct cauthctxt Authctxt;
242typedef struct cauthmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000243typedef struct identity Identity;
244typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100245
Damien Miller280ecfb2003-05-14 13:46:00 +1000246struct identity {
247 TAILQ_ENTRY(identity) next;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000248 int agent_fd; /* >=0 if agent supports key */
249 struct sshkey *key; /* public/private key */
Damien Miller280ecfb2003-05-14 13:46:00 +1000250 char *filename; /* comment for agent-only keys */
251 int tried;
252 int isprivate; /* key points to the private key */
Damien Miller5ceddc32013-02-15 12:18:32 +1100253 int userprovided;
Damien Miller280ecfb2003-05-14 13:46:00 +1000254};
255TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100256
djm@openbsd.org141efe42015-01-14 20:05:27 +0000257struct cauthctxt {
Damien Miller62cee002000-09-23 17:15:56 +1100258 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000259 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100260 const char *host;
261 const char *service;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000262 struct cauthmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000263 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000264 char *authlist;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000265 int attempt;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000266 /* pubkey */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000267 struct idlist keys;
268 int agent_fd;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000269 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000270 Sensitive *sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000271 char *oktypes, *ktypes;
272 const char *active_ktype;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000273 /* kbd-interactive */
274 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000275 /* generic */
276 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100277};
djm@openbsd.org141efe42015-01-14 20:05:27 +0000278
279struct cauthmethod {
Damien Miller62cee002000-09-23 17:15:56 +1100280 char *name; /* string to compare against server's list */
281 int (*userauth)(Authctxt *authctxt);
Damien Miller01ed2272008-11-05 16:20:46 +1100282 void (*cleanup)(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100283 int *enabled; /* flag in option struct that enables method */
284 int *batch_flag; /* flag in option struct that disables method */
285};
286
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000287int input_userauth_success(int, u_int32_t, void *);
288int input_userauth_success_unexpected(int, u_int32_t, void *);
289int input_userauth_failure(int, u_int32_t, void *);
290int input_userauth_banner(int, u_int32_t, void *);
291int input_userauth_error(int, u_int32_t, void *);
292int input_userauth_info_req(int, u_int32_t, void *);
293int input_userauth_pk_ok(int, u_int32_t, void *);
294int input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100295
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000296int userauth_none(Authctxt *);
297int userauth_pubkey(Authctxt *);
298int userauth_passwd(Authctxt *);
299int userauth_kbdint(Authctxt *);
300int userauth_hostbased(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100301
Darren Tucker0efd1552003-08-26 11:49:55 +1000302#ifdef GSSAPI
303int userauth_gssapi(Authctxt *authctxt);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000304int input_gssapi_response(int type, u_int32_t, void *);
305int input_gssapi_token(int type, u_int32_t, void *);
306int input_gssapi_hash(int type, u_int32_t, void *);
307int input_gssapi_error(int, u_int32_t, void *);
308int input_gssapi_errtok(int, u_int32_t, void *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000309#endif
310
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000311void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000312
Damien Miller280ecfb2003-05-14 13:46:00 +1000313static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000314static void pubkey_prepare(Authctxt *);
315static void pubkey_cleanup(Authctxt *);
Damien Miller5ceddc32013-02-15 12:18:32 +1100316static Key *load_identity_file(char *, int);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000317
Ben Lindstrombba81212001-06-25 05:01:22 +0000318static Authmethod *authmethod_get(char *authlist);
319static Authmethod *authmethod_lookup(const char *name);
320static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100321
322Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000323#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100324 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000325 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100326 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000327 &options.gss_authentication,
328 NULL},
329#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000330 {"hostbased",
331 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100332 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000333 &options.hostbased_authentication,
334 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000335 {"publickey",
336 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100337 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000338 &options.pubkey_authentication,
339 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100340 {"keyboard-interactive",
341 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100342 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100343 &options.kbd_interactive_authentication,
344 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000345 {"password",
346 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100347 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000348 &options.password_authentication,
349 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100350 {"none",
351 userauth_none,
352 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100353 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100354 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100355 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100356};
357
358void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000359ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000360 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100361{
362 Authctxt authctxt;
363 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100364
Ben Lindstrom551ea372001-06-05 18:56:16 +0000365 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000366 options.kbd_interactive_authentication = 1;
367
Damien Miller62cee002000-09-23 17:15:56 +1100368 packet_start(SSH2_MSG_SERVICE_REQUEST);
369 packet_put_cstring("ssh-userauth");
370 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000371 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100372 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100373 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000374 if (type != SSH2_MSG_SERVICE_ACCEPT)
375 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100376 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100377 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000378 debug2("service_accept: %s", reply);
Darren Tuckera627d422013-06-02 07:31:17 +1000379 free(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100380 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000381 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100382 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100383 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000384 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100385
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000386 if (options.preferred_authentications == NULL)
387 options.preferred_authentications = authmethods_get();
388
Damien Miller62cee002000-09-23 17:15:56 +1100389 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000390 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000391 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100392 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000393 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100394 authctxt.host = host;
395 authctxt.service = "ssh-connection"; /* service name */
396 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100397 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000398 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000399 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000400 authctxt.sensitive = sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000401 authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000402 authctxt.info_req_seen = 0;
djm@openbsd.orgf14564c2015-01-15 11:04:36 +0000403 authctxt.agent_fd = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100404 if (authctxt.method == NULL)
405 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100406
407 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100408 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100409
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000410 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100411 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
412 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000413 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100414 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
415
Damien Miller280ecfb2003-05-14 13:46:00 +1000416 pubkey_cleanup(&authctxt);
Damien Millerf842fcb2003-05-15 12:01:28 +1000417 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
418
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000419 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100420}
Damien Millerf842fcb2003-05-15 12:01:28 +1000421
Damien Miller62cee002000-09-23 17:15:56 +1100422void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000423userauth(Authctxt *authctxt, char *authlist)
424{
Damien Miller01ed2272008-11-05 16:20:46 +1100425 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
426 authctxt->method->cleanup(authctxt);
427
Darren Tuckera627d422013-06-02 07:31:17 +1000428 free(authctxt->methoddata);
429 authctxt->methoddata = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000430 if (authlist == NULL) {
431 authlist = authctxt->authlist;
432 } else {
Darren Tuckera627d422013-06-02 07:31:17 +1000433 free(authctxt->authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000434 authctxt->authlist = authlist;
435 }
436 for (;;) {
437 Authmethod *method = authmethod_get(authlist);
438 if (method == NULL)
439 fatal("Permission denied (%s).", authlist);
440 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000441
442 /* reset the per method handler */
443 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
444 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
445
446 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000447 if (method->userauth(authctxt) != 0) {
448 debug2("we sent a %s packet, wait for reply", method->name);
449 break;
450 } else {
451 debug2("we did not send a packet, disable method");
452 method->enabled = NULL;
453 }
454 }
455}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000456
Damien Millerf7475d72008-11-03 19:26:18 +1100457/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000458int
Damien Miller630d6f42002-01-22 23:17:30 +1100459input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100460{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000461 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000462 "type %d", type);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000463 return 0;
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000464}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000465
Damien Millerf7475d72008-11-03 19:26:18 +1100466/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000467int
Damien Miller630d6f42002-01-22 23:17:30 +1100468input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000469{
Damien Miller7ba0ca72008-07-17 18:57:06 +1000470 char *msg, *raw, *lang;
471 u_int len;
Darren Tucker79644822003-10-08 17:37:58 +1000472
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000473 debug3("input_userauth_banner");
Damien Miller7ba0ca72008-07-17 18:57:06 +1000474 raw = packet_get_string(&len);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000475 lang = packet_get_string(NULL);
Damien Millerc674d582008-11-03 19:16:57 +1100476 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
Damien Miller7ba0ca72008-07-17 18:57:06 +1000477 if (len > 65536)
478 len = 65536;
Damien Millerc4d1b362008-11-03 19:22:09 +1100479 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
Darren Tucker0c348f52010-01-08 18:58:05 +1100480 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
Darren Tucker046dff22003-10-08 17:32:02 +1000481 fprintf(stderr, "%s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000482 free(msg);
Damien Miller7ba0ca72008-07-17 18:57:06 +1000483 }
Darren Tuckera627d422013-06-02 07:31:17 +1000484 free(raw);
485 free(lang);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000486 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100487}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000488
Damien Millerf7475d72008-11-03 19:26:18 +1100489/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000490int
Damien Miller630d6f42002-01-22 23:17:30 +1100491input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100492{
493 Authctxt *authctxt = ctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100494
Damien Miller62cee002000-09-23 17:15:56 +1100495 if (authctxt == NULL)
496 fatal("input_userauth_success: no authentication context");
Darren Tuckera627d422013-06-02 07:31:17 +1000497 free(authctxt->authlist);
498 authctxt->authlist = NULL;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100499 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
500 authctxt->method->cleanup(authctxt);
Darren Tuckera627d422013-06-02 07:31:17 +1000501 free(authctxt->methoddata);
502 authctxt->methoddata = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100503 authctxt->success = 1; /* break out */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000504 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100505}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000506
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000507int
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100508input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
509{
510 Authctxt *authctxt = ctxt;
511
512 if (authctxt == NULL)
513 fatal("%s: no authentication context", __func__);
514
515 fatal("Unexpected authentication success during %s.",
516 authctxt->method->name);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000517 return 0;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100518}
519
Damien Millerf7475d72008-11-03 19:26:18 +1100520/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000521int
Damien Miller630d6f42002-01-22 23:17:30 +1100522input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100523{
Damien Miller62cee002000-09-23 17:15:56 +1100524 Authctxt *authctxt = ctxt;
525 char *authlist = NULL;
526 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100527
528 if (authctxt == NULL)
529 fatal("input_userauth_failure: no authentication context");
530
Damien Miller874d77b2000-10-14 16:23:11 +1100531 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100532 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100533 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100534
Damien Miller62e9c4f2013-04-23 15:15:49 +1000535 if (partial != 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000536 logit("Authenticated with partial success.");
Damien Miller62e9c4f2013-04-23 15:15:49 +1000537 /* reset state */
538 pubkey_cleanup(authctxt);
539 pubkey_prepare(authctxt);
540 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000541 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100542
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000543 userauth(authctxt, authlist);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000544 return 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000545}
Damien Millerf7475d72008-11-03 19:26:18 +1100546
547/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000548int
Damien Miller630d6f42002-01-22 23:17:30 +1100549input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000550{
551 Authctxt *authctxt = ctxt;
552 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000553 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000554 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000555 int pktype, sent = 0;
556 u_int alen, blen;
557 char *pkalg, *fp;
558 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000559
560 if (authctxt == NULL)
561 fatal("input_userauth_pk_ok: no authentication context");
562 if (datafellows & SSH_BUG_PKOK) {
563 /* this is similar to SSH_BUG_PKAUTH */
564 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
565 pkblob = packet_get_string(&blen);
566 buffer_init(&b);
567 buffer_append(&b, pkblob, blen);
568 pkalg = buffer_get_string(&b, &alen);
569 buffer_free(&b);
570 } else {
571 pkalg = packet_get_string(&alen);
572 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000573 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100574 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000575
Damien Miller280ecfb2003-05-14 13:46:00 +1000576 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000577
Damien Miller280ecfb2003-05-14 13:46:00 +1000578 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
579 debug("unknown pkalg %s", pkalg);
580 goto done;
581 }
582 if ((key = key_from_blob(pkblob, blen)) == NULL) {
583 debug("no key from blob. pkalg %s", pkalg);
584 goto done;
585 }
586 if (key->type != pktype) {
587 error("input_userauth_pk_ok: type mismatch "
588 "for decoded key (received %d, expected %d)",
589 key->type, pktype);
590 goto done;
591 }
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000592 if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
593 SSH_FP_DEFAULT)) == NULL)
594 goto done;
Damien Miller280ecfb2003-05-14 13:46:00 +1000595 debug2("input_userauth_pk_ok: fp %s", fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000596 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000597
Darren Tuckerd05b6012003-10-15 15:55:59 +1000598 /*
599 * search keys in the reverse order, because last candidate has been
600 * moved to the end of the queue. this also avoids confusion by
601 * duplicate keys
602 */
Damien Miller0b51a522004-04-20 20:07:19 +1000603 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000604 if (key_equal(key, id->key)) {
605 sent = sign_and_send_pubkey(authctxt, id);
606 break;
607 }
608 }
609done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000610 if (key != NULL)
611 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000612 free(pkalg);
613 free(pkblob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000614
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000615 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000616 if (sent == 0)
617 userauth(authctxt, NULL);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000618 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100619}
620
Darren Tucker0efd1552003-08-26 11:49:55 +1000621#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100622int
Darren Tucker0efd1552003-08-26 11:49:55 +1000623userauth_gssapi(Authctxt *authctxt)
624{
625 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100626 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000627 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000628 OM_uint32 min;
629 int ok = 0;
630
631 /* Try one GSSAPI method at a time, rather than sending them all at
632 * once. */
633
Darren Tucker56afe142003-11-03 20:06:14 +1100634 if (gss_supported == NULL)
635 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000636
637 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100638 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000639 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100640 if (gss_supported->elements[mech].length < 128 &&
Damien Millera1cb9f32006-08-19 00:33:34 +1000641 ssh_gssapi_check_mechanism(&gssctxt,
642 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000643 ok = 1; /* Mechanism works */
644 } else {
645 mech++;
646 }
647 }
648
Damien Millera1cb9f32006-08-19 00:33:34 +1000649 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000650 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000651
652 authctxt->methoddata=(void *)gssctxt;
653
654 packet_start(SSH2_MSG_USERAUTH_REQUEST);
655 packet_put_cstring(authctxt->server_user);
656 packet_put_cstring(authctxt->service);
657 packet_put_cstring(authctxt->method->name);
658
659 packet_put_int(1);
660
Darren Tucker655a5e02003-11-03 20:09:03 +1100661 packet_put_int((gss_supported->elements[mech].length) + 2);
662 packet_put_char(SSH_GSS_OIDTYPE);
663 packet_put_char(gss_supported->elements[mech].length);
664 packet_put_raw(gss_supported->elements[mech].elements,
665 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000666
667 packet_send();
668
669 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
670 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
671 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
672 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
673
674 mech++; /* Move along to next candidate */
675
676 return 1;
677}
678
Damien Miller91c6aa42003-11-17 21:20:18 +1100679static OM_uint32
680process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
681{
682 Authctxt *authctxt = ctxt;
683 Gssctxt *gssctxt = authctxt->methoddata;
684 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000685 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
686 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100687 OM_uint32 status, ms, flags;
688 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100689
Damien Miller91c6aa42003-11-17 21:20:18 +1100690 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100691 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100692
693 if (send_tok.length > 0) {
694 if (GSS_ERROR(status))
695 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
696 else
697 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100698
Damien Miller91c6aa42003-11-17 21:20:18 +1100699 packet_put_string(send_tok.value, send_tok.length);
700 packet_send();
701 gss_release_buffer(&ms, &send_tok);
702 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100703
Damien Miller91c6aa42003-11-17 21:20:18 +1100704 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100705 /* send either complete or MIC, depending on mechanism */
706 if (!(flags & GSS_C_INTEG_FLAG)) {
707 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
708 packet_send();
709 } else {
710 ssh_gssapi_buildmic(&b, authctxt->server_user,
711 authctxt->service, "gssapi-with-mic");
712
713 gssbuf.value = buffer_ptr(&b);
714 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100715
Damien Miller0425d402003-11-17 22:18:21 +1100716 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100717
Damien Miller0425d402003-11-17 22:18:21 +1100718 if (!GSS_ERROR(status)) {
719 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
720 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100721
Damien Miller0425d402003-11-17 22:18:21 +1100722 packet_send();
723 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100724
Damien Miller0425d402003-11-17 22:18:21 +1100725 buffer_free(&b);
726 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100727 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100728 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100729
Damien Miller91c6aa42003-11-17 21:20:18 +1100730 return status;
731}
732
Damien Millerf7475d72008-11-03 19:26:18 +1100733/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000734int
Darren Tucker0efd1552003-08-26 11:49:55 +1000735input_gssapi_response(int type, u_int32_t plen, void *ctxt)
736{
737 Authctxt *authctxt = ctxt;
738 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000739 int oidlen;
740 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000741
742 if (authctxt == NULL)
743 fatal("input_gssapi_response: no authentication context");
744 gssctxt = authctxt->methoddata;
745
746 /* Setup our OID */
747 oidv = packet_get_string(&oidlen);
748
Darren Tucker655a5e02003-11-03 20:09:03 +1100749 if (oidlen <= 2 ||
750 oidv[0] != SSH_GSS_OIDTYPE ||
751 oidv[1] != oidlen - 2) {
Darren Tuckera627d422013-06-02 07:31:17 +1000752 free(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100753 debug("Badly encoded mechanism OID received");
754 userauth(authctxt, NULL);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000755 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000756 }
757
Darren Tucker655a5e02003-11-03 20:09:03 +1100758 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
759 fatal("Server returned different OID than expected");
760
Darren Tucker0efd1552003-08-26 11:49:55 +1000761 packet_check_eom();
762
Darren Tuckera627d422013-06-02 07:31:17 +1000763 free(oidv);
Darren Tucker0efd1552003-08-26 11:49:55 +1000764
Damien Miller91c6aa42003-11-17 21:20:18 +1100765 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000766 /* Start again with next method on list */
767 debug("Trying to start again");
768 userauth(authctxt, NULL);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000769 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000770 }
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000771 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000772}
773
Damien Millerf7475d72008-11-03 19:26:18 +1100774/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000775int
Darren Tucker0efd1552003-08-26 11:49:55 +1000776input_gssapi_token(int type, u_int32_t plen, void *ctxt)
777{
778 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000779 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100780 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000781 u_int slen;
782
783 if (authctxt == NULL)
784 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000785
786 recv_tok.value = packet_get_string(&slen);
787 recv_tok.length = slen; /* safe typecast */
788
789 packet_check_eom();
790
Damien Miller91c6aa42003-11-17 21:20:18 +1100791 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000792
Darren Tuckera627d422013-06-02 07:31:17 +1000793 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000794
795 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000796 /* Start again with the next method in the list */
797 userauth(authctxt, NULL);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000798 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000799 }
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000800 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000801}
802
Damien Millerf7475d72008-11-03 19:26:18 +1100803/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000804int
Darren Tucker0efd1552003-08-26 11:49:55 +1000805input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
806{
807 Authctxt *authctxt = ctxt;
808 Gssctxt *gssctxt;
809 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
810 gss_buffer_desc recv_tok;
Damien Millerd677ad12013-04-23 15:18:51 +1000811 OM_uint32 ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000812 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000813
814 if (authctxt == NULL)
815 fatal("input_gssapi_response: no authentication context");
816 gssctxt = authctxt->methoddata;
817
Darren Tucker600ad8d2003-08-26 12:10:48 +1000818 recv_tok.value = packet_get_string(&len);
819 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000820
821 packet_check_eom();
822
823 /* Stick it into GSSAPI and see what it says */
Damien Millerd677ad12013-04-23 15:18:51 +1000824 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000825 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000826
Darren Tuckera627d422013-06-02 07:31:17 +1000827 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000828 gss_release_buffer(&ms, &send_tok);
829
830 /* Server will be returning a failed packet after this one */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000831 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000832}
833
Damien Millerf7475d72008-11-03 19:26:18 +1100834/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000835int
Darren Tucker0efd1552003-08-26 11:49:55 +1000836input_gssapi_error(int type, u_int32_t plen, void *ctxt)
837{
Darren Tucker0efd1552003-08-26 11:49:55 +1000838 char *msg;
839 char *lang;
840
Damien Millerd677ad12013-04-23 15:18:51 +1000841 /* maj */(void)packet_get_int();
842 /* min */(void)packet_get_int();
Darren Tucker0efd1552003-08-26 11:49:55 +1000843 msg=packet_get_string(NULL);
844 lang=packet_get_string(NULL);
845
846 packet_check_eom();
847
Damien Miller15d72a02005-11-05 15:07:33 +1100848 debug("Server GSSAPI Error:\n%s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000849 free(msg);
850 free(lang);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000851 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000852}
853#endif /* GSSAPI */
854
Damien Millereba71ba2000-04-29 23:57:08 +1000855int
Damien Miller874d77b2000-10-14 16:23:11 +1100856userauth_none(Authctxt *authctxt)
857{
858 /* initial userauth request */
859 packet_start(SSH2_MSG_USERAUTH_REQUEST);
860 packet_put_cstring(authctxt->server_user);
861 packet_put_cstring(authctxt->service);
862 packet_put_cstring(authctxt->method->name);
863 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100864 return 1;
865}
866
867int
Damien Miller62cee002000-09-23 17:15:56 +1100868userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000869{
Damien Millere247cc42000-05-07 12:03:14 +1000870 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000871 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000872 char *password;
Darren Tuckerab791692010-01-08 18:48:02 +1100873 const char *host = options.host_key_alias ? options.host_key_alias :
874 authctxt->host;
Damien Millereba71ba2000-04-29 23:57:08 +1000875
Damien Millerd3a18572000-06-07 19:55:44 +1000876 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000877 return 0;
878
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000879 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000880 error("Permission denied, please try again.");
881
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000882 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100883 authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +1000884 password = read_passphrase(prompt, 0);
885 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100886 packet_put_cstring(authctxt->server_user);
887 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100888 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000889 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000890 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100891 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000892 free(password);
Damien Miller9f643902001-11-12 11:02:52 +1100893 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000894 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000895
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000896 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000897 &input_userauth_passwd_changereq);
898
Damien Millereba71ba2000-04-29 23:57:08 +1000899 return 1;
900}
Damien Millerf7475d72008-11-03 19:26:18 +1100901
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000902/*
903 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
904 */
Damien Millerf7475d72008-11-03 19:26:18 +1100905/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000906int
Tim Ricec8549622002-03-31 12:49:38 -0800907input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000908{
909 Authctxt *authctxt = ctxt;
910 char *info, *lang, *password = NULL, *retype = NULL;
911 char prompt[150];
Darren Tuckerab791692010-01-08 18:48:02 +1100912 const char *host = options.host_key_alias ? options.host_key_alias :
913 authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000914
915 debug2("input_userauth_passwd_changereq");
916
917 if (authctxt == NULL)
918 fatal("input_userauth_passwd_changereq: "
919 "no authentication context");
920
921 info = packet_get_string(NULL);
922 lang = packet_get_string(NULL);
923 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000924 logit("%s", info);
Darren Tuckera627d422013-06-02 07:31:17 +1000925 free(info);
926 free(lang);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000927 packet_start(SSH2_MSG_USERAUTH_REQUEST);
928 packet_put_cstring(authctxt->server_user);
929 packet_put_cstring(authctxt->service);
930 packet_put_cstring(authctxt->method->name);
931 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000932 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000933 "Enter %.30s@%.128s's old password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100934 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000935 password = read_passphrase(prompt, 0);
936 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100937 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000938 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000939 password = NULL;
940 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000941 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000942 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100943 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000944 password = read_passphrase(prompt, RP_ALLOW_EOF);
945 if (password == NULL) {
946 /* bail out */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000947 return 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000948 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000949 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000950 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100951 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000952 retype = read_passphrase(prompt, 0);
953 if (strcmp(password, retype) != 0) {
Damien Millera5103f42014-02-04 11:20:14 +1100954 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000955 free(password);
Damien Miller996acd22003-04-09 20:59:48 +1000956 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000957 password = NULL;
958 }
Damien Millera5103f42014-02-04 11:20:14 +1100959 explicit_bzero(retype, strlen(retype));
Darren Tuckera627d422013-06-02 07:31:17 +1000960 free(retype);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000961 }
962 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100963 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000964 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000965 packet_add_padding(64);
966 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000967
968 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000969 &input_userauth_passwd_changereq);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000970 return 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000971}
Damien Millereba71ba2000-04-29 23:57:08 +1000972
Ben Lindstrombba81212001-06-25 05:01:22 +0000973static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000974identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
975 const u_char *data, size_t datalen, u_int compat)
Damien Miller280ecfb2003-05-14 13:46:00 +1000976{
977 Key *prv;
978 int ret;
979
980 /* the agent supports this key */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000981 if (id->agent_fd)
982 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
983 data, datalen, compat);
984
Damien Miller280ecfb2003-05-14 13:46:00 +1000985 /*
986 * we have already loaded the private key or
987 * the private key is stored in external hardware
988 */
Damien Miller86687062014-07-02 15:28:02 +1000989 if (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))
djm@openbsd.org141efe42015-01-14 20:05:27 +0000990 return (sshkey_sign(id->key, sigp, lenp, data, datalen,
991 compat));
Damien Miller280ecfb2003-05-14 13:46:00 +1000992 /* load the private key from the file */
Damien Miller5ceddc32013-02-15 12:18:32 +1100993 if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000994 return (-1); /* XXX return decent error code */
995 ret = sshkey_sign(prv, sigp, lenp, data, datalen, compat);
996 sshkey_free(prv);
Damien Miller280ecfb2003-05-14 13:46:00 +1000997 return (ret);
998}
999
1000static int
1001sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +10001002{
1003 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001004 u_char *blob, *signature;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001005 u_int bloblen;
1006 size_t slen;
Darren Tucker502d3842003-06-28 12:38:01 +10001007 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001008 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001009 int have_sig = 1;
Damien Miller4e270b02010-04-16 15:56:21 +10001010 char *fp;
Damien Millereba71ba2000-04-29 23:57:08 +10001011
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001012 if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1013 SSH_FP_DEFAULT)) == NULL)
1014 return 0;
Damien Miller4e270b02010-04-16 15:56:21 +10001015 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
Darren Tuckera627d422013-06-02 07:31:17 +10001016 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001017
Damien Miller280ecfb2003-05-14 13:46:00 +10001018 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001019 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +00001020 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001021 return 0;
1022 }
Damien Millereba71ba2000-04-29 23:57:08 +10001023 /* data to be signed */
1024 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +11001025 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +10001026 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001027 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +11001028 } else {
1029 buffer_put_string(&b, session_id2, session_id2_len);
1030 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +10001031 }
Damien Millereba71ba2000-04-29 23:57:08 +10001032 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001033 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +10001034 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +00001035 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +10001036 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +11001037 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +00001038 if (datafellows & SSH_BUG_PKAUTH) {
1039 buffer_put_char(&b, have_sig);
1040 } else {
1041 buffer_put_cstring(&b, authctxt->method->name);
1042 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +10001043 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +00001044 }
Damien Millereba71ba2000-04-29 23:57:08 +10001045 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +10001046
1047 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +10001048 ret = identity_sign(id, &signature, &slen,
djm@openbsd.org141efe42015-01-14 20:05:27 +00001049 buffer_ptr(&b), buffer_len(&b), datafellows);
1050 if (ret != 0) {
Darren Tuckera627d422013-06-02 07:31:17 +10001051 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +10001052 buffer_free(&b);
1053 return 0;
1054 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001055#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +10001056 buffer_dump(&b);
1057#endif
Ben Lindstromd121f612000-12-03 17:00:47 +00001058 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +10001059 buffer_clear(&b);
1060 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001061 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +10001062 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001063 buffer_put_cstring(&b, authctxt->server_user);
1064 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +11001065 buffer_put_cstring(&b, authctxt->method->name);
1066 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +00001067 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001068 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +10001069 buffer_put_string(&b, blob, bloblen);
1070 }
Darren Tuckera627d422013-06-02 07:31:17 +10001071 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001072
Damien Millereba71ba2000-04-29 23:57:08 +10001073 /* append signature */
1074 buffer_put_string(&b, signature, slen);
Darren Tuckera627d422013-06-02 07:31:17 +10001075 free(signature);
Damien Millereba71ba2000-04-29 23:57:08 +10001076
1077 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +10001078 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +11001079 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +10001080 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +10001081
1082 /* put remaining data from buffer into packet */
1083 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1084 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1085 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +10001086 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +10001087
1088 return 1;
Damien Miller994cf142000-07-21 10:19:44 +10001089}
1090
Ben Lindstrombba81212001-06-25 05:01:22 +00001091static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001092send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001093{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001094 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001095 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +10001096
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001097 debug3("send_pubkey_test");
1098
Damien Miller280ecfb2003-05-14 13:46:00 +10001099 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001100 /* we cannot handle this key */
1101 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +10001102 return 0;
1103 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001104 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001105 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001106
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001107 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1108 packet_put_cstring(authctxt->server_user);
1109 packet_put_cstring(authctxt->service);
1110 packet_put_cstring(authctxt->method->name);
1111 packet_put_char(have_sig);
1112 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001113 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001114 packet_put_string(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001115 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001116 packet_send();
1117 return 1;
1118}
1119
Ben Lindstrombba81212001-06-25 05:01:22 +00001120static Key *
Damien Miller5ceddc32013-02-15 12:18:32 +11001121load_identity_file(char *filename, int userprovided)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001122{
1123 Key *private;
1124 char prompt[300], *passphrase;
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001125 int r, perm_ok = 0, quit = 0, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001126 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001127
Ben Lindstrom329782e2001-03-10 17:08:59 +00001128 if (stat(filename, &st) < 0) {
Damien Miller5ceddc32013-02-15 12:18:32 +11001129 (userprovided ? logit : debug3)("no such identity: %s: %s",
1130 filename, strerror(errno));
Ben Lindstrom329782e2001-03-10 17:08:59 +00001131 return NULL;
1132 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001133 snprintf(prompt, sizeof prompt,
1134 "Enter passphrase for key '%.100s': ", filename);
1135 for (i = 0; i <= options.number_of_password_prompts; i++) {
1136 if (i == 0)
1137 passphrase = "";
1138 else {
Damien Miller62cee002000-09-23 17:15:56 +11001139 passphrase = read_passphrase(prompt, 0);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001140 if (*passphrase == '\0') {
Damien Miller62cee002000-09-23 17:15:56 +11001141 debug2("no passphrase given, try next key");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001142 free(passphrase);
1143 break;
Damien Miller62cee002000-09-23 17:15:56 +11001144 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001145 }
1146 switch ((r = sshkey_load_private_type(KEY_UNSPEC, filename,
1147 passphrase, &private, NULL, &perm_ok))) {
1148 case 0:
1149 break;
1150 case SSH_ERR_KEY_WRONG_PASSPHRASE:
1151 if (options.batch_mode) {
1152 quit = 1;
1153 break;
1154 }
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001155 if (i != 0)
1156 debug2("bad passphrase given, try again...");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001157 break;
1158 case SSH_ERR_SYSTEM_ERROR:
1159 if (errno == ENOENT) {
1160 debug2("Load key \"%s\": %s",
1161 filename, ssh_err(r));
1162 quit = 1;
1163 break;
1164 }
1165 /* FALLTHROUGH */
1166 default:
1167 error("Load key \"%s\": %s", filename, ssh_err(r));
1168 quit = 1;
1169 break;
1170 }
1171 if (i > 0) {
Damien Millera5103f42014-02-04 11:20:14 +11001172 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001173 free(passphrase);
Damien Miller62cee002000-09-23 17:15:56 +11001174 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001175 if (private != NULL || quit)
1176 break;
Damien Miller994cf142000-07-21 10:19:44 +10001177 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001178 return private;
1179}
1180
Damien Miller280ecfb2003-05-14 13:46:00 +10001181/*
1182 * try keys in the following order:
1183 * 1. agent keys that are found in the config file
1184 * 2. other agent keys
1185 * 3. keys that are only listed in the config file
1186 */
1187static void
1188pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001189{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001190 struct identity *id, *id2, *tmp;
1191 struct idlist agent, files, *preferred;
1192 struct sshkey *key;
1193 int agent_fd, i, r, found;
1194 size_t j;
1195 struct ssh_identitylist *idlist;
Damien Millerad833b32000-08-23 10:46:23 +10001196
Damien Miller280ecfb2003-05-14 13:46:00 +10001197 TAILQ_INIT(&agent); /* keys from the agent */
1198 TAILQ_INIT(&files); /* keys from the config file */
1199 preferred = &authctxt->keys;
1200 TAILQ_INIT(preferred); /* preferred order of keys */
1201
Damien Millercb6b68b2012-12-03 09:49:52 +11001202 /* list of keys stored in the filesystem and PKCS#11 */
Damien Miller280ecfb2003-05-14 13:46:00 +10001203 for (i = 0; i < options.num_identity_files; i++) {
1204 key = options.identity_keys[i];
1205 if (key && key->type == KEY_RSA1)
1206 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +11001207 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1208 continue;
Damien Miller280ecfb2003-05-14 13:46:00 +10001209 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001210 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001211 id->key = key;
1212 id->filename = xstrdup(options.identity_files[i]);
Darren Tucker19104782013-04-05 11:13:08 +11001213 id->userprovided = options.identity_file_userprovided[i];
Damien Miller280ecfb2003-05-14 13:46:00 +10001214 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001215 }
Damien Millercb6b68b2012-12-03 09:49:52 +11001216 /* Prefer PKCS11 keys that are explicitly listed */
1217 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
Damien Miller86687062014-07-02 15:28:02 +10001218 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
Damien Millercb6b68b2012-12-03 09:49:52 +11001219 continue;
1220 found = 0;
1221 TAILQ_FOREACH(id2, &files, next) {
1222 if (id2->key == NULL ||
Damien Miller86687062014-07-02 15:28:02 +10001223 (id2->key->flags & SSHKEY_FLAG_EXT) == 0)
Damien Millercb6b68b2012-12-03 09:49:52 +11001224 continue;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001225 if (sshkey_equal(id->key, id2->key)) {
Damien Millercb6b68b2012-12-03 09:49:52 +11001226 TAILQ_REMOVE(&files, id, next);
1227 TAILQ_INSERT_TAIL(preferred, id, next);
1228 found = 1;
1229 break;
1230 }
1231 }
1232 /* If IdentitiesOnly set and key not found then don't use it */
1233 if (!found && options.identities_only) {
1234 TAILQ_REMOVE(&files, id, next);
Damien Miller1d2c4562014-02-04 11:18:20 +11001235 explicit_bzero(id, sizeof(*id));
Damien Millercb6b68b2012-12-03 09:49:52 +11001236 free(id);
1237 }
1238 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001239 /* list of keys supported by the agent */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001240 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1241 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1242 debug("%s: ssh_get_authentication_socket: %s",
1243 __func__, ssh_err(r));
1244 } else if ((r = ssh_fetch_identitylist(agent_fd, 2, &idlist)) != 0) {
1245 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1246 debug("%s: ssh_fetch_identitylist: %s",
1247 __func__, ssh_err(r));
1248 } else {
1249 for (j = 0; j < idlist->nkeys; j++) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001250 found = 0;
1251 TAILQ_FOREACH(id, &files, next) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001252 /*
1253 * agent keys from the config file are
1254 * preferred
1255 */
1256 if (sshkey_equal(idlist->keys[j], id->key)) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001257 TAILQ_REMOVE(&files, id, next);
1258 TAILQ_INSERT_TAIL(preferred, id, next);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001259 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001260 found = 1;
1261 break;
1262 }
1263 }
Damien Millerbd394c32004-03-08 23:12:36 +11001264 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001265 id = xcalloc(1, sizeof(*id));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001266 /* XXX "steals" key/comment from idlist */
1267 id->key = idlist->keys[j];
1268 id->filename = idlist->comments[j];
1269 idlist->keys[j] = NULL;
1270 idlist->comments[j] = NULL;
1271 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001272 TAILQ_INSERT_TAIL(&agent, id, next);
1273 }
1274 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001275 ssh_free_identitylist(idlist);
Damien Miller280ecfb2003-05-14 13:46:00 +10001276 /* append remaining agent keys */
1277 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1278 TAILQ_REMOVE(&agent, id, next);
1279 TAILQ_INSERT_TAIL(preferred, id, next);
1280 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001281 authctxt->agent_fd = agent_fd;
Damien Miller62cee002000-09-23 17:15:56 +11001282 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001283 /* append remaining keys from the config file */
1284 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1285 TAILQ_REMOVE(&files, id, next);
1286 TAILQ_INSERT_TAIL(preferred, id, next);
1287 }
1288 TAILQ_FOREACH(id, preferred, next) {
Damien Miller5ceddc32013-02-15 12:18:32 +11001289 debug2("key: %s (%p),%s", id->filename, id->key,
1290 id->userprovided ? " explicit" : "");
Damien Miller280ecfb2003-05-14 13:46:00 +10001291 }
1292}
1293
1294static void
1295pubkey_cleanup(Authctxt *authctxt)
1296{
1297 Identity *id;
1298
djm@openbsd.org141efe42015-01-14 20:05:27 +00001299 if (authctxt->agent_fd != -1)
1300 ssh_close_authentication_socket(authctxt->agent_fd);
Damien Miller280ecfb2003-05-14 13:46:00 +10001301 for (id = TAILQ_FIRST(&authctxt->keys); id;
1302 id = TAILQ_FIRST(&authctxt->keys)) {
1303 TAILQ_REMOVE(&authctxt->keys, id, next);
1304 if (id->key)
djm@openbsd.org141efe42015-01-14 20:05:27 +00001305 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +10001306 free(id->filename);
1307 free(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001308 }
Damien Millereba71ba2000-04-29 23:57:08 +10001309}
1310
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001311static int
1312try_identity(Identity *id)
1313{
1314 if (!id->key)
1315 return (0);
1316 if (match_pattern_list(sshkey_ssh_name(id->key),
1317 options.pubkey_key_types, 0) != 1) {
1318 debug("Skipping %s key %s for not in PubkeyAcceptedKeyTypes",
1319 sshkey_ssh_name(id->key), id->filename);
1320 return (0);
1321 }
1322 if (key_type_plain(id->key->type) == KEY_RSA &&
1323 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1324 debug("Skipped %s key %s for RSA/MD5 server",
1325 key_type(id->key), id->filename);
1326 return (0);
1327 }
1328 return (id->key->type != KEY_RSA1);
1329}
1330
Damien Miller62cee002000-09-23 17:15:56 +11001331int
1332userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001333{
Damien Miller280ecfb2003-05-14 13:46:00 +10001334 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001335 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001336
Damien Miller280ecfb2003-05-14 13:46:00 +10001337 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1338 if (id->tried++)
1339 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001340 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001341 TAILQ_REMOVE(&authctxt->keys, id, next);
1342 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1343 /*
1344 * send a test message if we have the public key. for
1345 * encrypted keys we cannot do this and have to load the
1346 * private key instead
1347 */
Damien Miller324541e2013-12-31 12:25:40 +11001348 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001349 if (try_identity(id)) {
Damien Miller324541e2013-12-31 12:25:40 +11001350 debug("Offering %s public key: %s",
1351 key_type(id->key), id->filename);
1352 sent = send_pubkey_test(authctxt, id);
1353 }
1354 } else {
Damien Miller280ecfb2003-05-14 13:46:00 +10001355 debug("Trying private key: %s", id->filename);
Damien Miller5ceddc32013-02-15 12:18:32 +11001356 id->key = load_identity_file(id->filename,
1357 id->userprovided);
Damien Miller280ecfb2003-05-14 13:46:00 +10001358 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001359 if (try_identity(id)) {
1360 id->isprivate = 1;
Damien Miller324541e2013-12-31 12:25:40 +11001361 sent = sign_and_send_pubkey(
1362 authctxt, id);
1363 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001364 key_free(id->key);
1365 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001366 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001367 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001368 if (sent)
1369 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001370 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001371 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001372}
Damien Millereba71ba2000-04-29 23:57:08 +10001373
Damien Miller874d77b2000-10-14 16:23:11 +11001374/*
1375 * Send userauth request message specifying keyboard-interactive method.
1376 */
1377int
1378userauth_kbdint(Authctxt *authctxt)
1379{
1380 static int attempt = 0;
1381
1382 if (attempt++ >= options.number_of_password_prompts)
1383 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001384 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1385 if (attempt > 1 && !authctxt->info_req_seen) {
1386 debug3("userauth_kbdint: disable: no info_req_seen");
1387 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1388 return 0;
1389 }
Damien Miller874d77b2000-10-14 16:23:11 +11001390
1391 debug2("userauth_kbdint");
1392 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1393 packet_put_cstring(authctxt->server_user);
1394 packet_put_cstring(authctxt->service);
1395 packet_put_cstring(authctxt->method->name);
1396 packet_put_cstring(""); /* lang */
1397 packet_put_cstring(options.kbd_interactive_devices ?
1398 options.kbd_interactive_devices : "");
1399 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001400
1401 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1402 return 1;
1403}
1404
1405/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001406 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001407 */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001408int
Damien Miller630d6f42002-01-22 23:17:30 +11001409input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001410{
1411 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001412 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001413 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001414 int echo = 0;
1415
1416 debug2("input_userauth_info_req");
1417
1418 if (authctxt == NULL)
1419 fatal("input_userauth_info_req: no authentication context");
1420
Ben Lindstrom7d199962001-09-12 18:29:00 +00001421 authctxt->info_req_seen = 1;
1422
Damien Miller874d77b2000-10-14 16:23:11 +11001423 name = packet_get_string(NULL);
1424 inst = packet_get_string(NULL);
1425 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001426 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001427 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001428 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001429 logit("%s", inst);
Darren Tuckera627d422013-06-02 07:31:17 +10001430 free(name);
1431 free(inst);
1432 free(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001433
1434 num_prompts = packet_get_int();
1435 /*
1436 * Begin to build info response packet based on prompts requested.
1437 * We commit to providing the correct number of responses, so if
1438 * further on we run into a problem that prevents this, we have to
1439 * be sure and clean this up and send a correct error response.
1440 */
1441 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1442 packet_put_int(num_prompts);
1443
Ben Lindstrom551ea372001-06-05 18:56:16 +00001444 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001445 for (i = 0; i < num_prompts; i++) {
1446 prompt = packet_get_string(NULL);
1447 echo = packet_get_char();
1448
Ben Lindstrom949974b2001-06-25 05:20:31 +00001449 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001450
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001451 packet_put_cstring(response);
Damien Millera5103f42014-02-04 11:20:14 +11001452 explicit_bzero(response, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +10001453 free(response);
1454 free(prompt);
Damien Miller874d77b2000-10-14 16:23:11 +11001455 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001456 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001457
Damien Miller9f643902001-11-12 11:02:52 +11001458 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001459 packet_send();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001460 return 0;
Damien Miller874d77b2000-10-14 16:23:11 +11001461}
Damien Miller62cee002000-09-23 17:15:56 +11001462
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001463static int
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001464ssh_keysign(struct sshkey *key, u_char **sigp, size_t *lenp,
1465 const u_char *data, size_t datalen)
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001466{
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001467 struct sshbuf *b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001468 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001469 pid_t pid;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001470 int i, r, to[2], from[2], status, sock = packet_get_connection_in();
1471 u_char rversion = 0, version = 2;
1472 void (*osigchld)(int);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001473
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001474 *sigp = NULL;
1475 *lenp = 0;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001476
Ben Lindstrom5206b952002-06-06 19:59:29 +00001477 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001478 error("%s: not installed: %s", __func__, strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001479 return -1;
1480 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001481 if (fflush(stdout) != 0) {
1482 error("%s: fflush: %s", __func__, strerror(errno));
1483 return -1;
1484 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001485 if (pipe(to) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001486 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001487 return -1;
1488 }
1489 if (pipe(from) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001490 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001491 return -1;
1492 }
1493 if ((pid = fork()) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001494 error("%s: fork: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001495 return -1;
1496 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001497 osigchld = signal(SIGCHLD, SIG_DFL);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001498 if (pid == 0) {
Darren Tucker6e7fe1c2010-01-08 17:07:22 +11001499 /* keep the socket on exec */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001500 fcntl(sock, F_SETFD, 0);
Damien Miller2e5fe882006-06-13 13:10:00 +10001501 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001502 close(from[0]);
1503 if (dup2(from[1], STDOUT_FILENO) < 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001504 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001505 close(to[1]);
1506 if (dup2(to[0], STDIN_FILENO) < 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001507 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001508 close(from[1]);
1509 close(to[0]);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001510 /* Close everything but stdio and the socket */
1511 for (i = STDERR_FILENO + 1; i < sock; i++)
1512 close(i);
1513 closefrom(sock + 1);
1514 debug3("%s: [child] pid=%ld, exec %s",
1515 __func__, (long)getpid(), _PATH_SSH_KEY_SIGN);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001516 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001517 fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001518 strerror(errno));
1519 }
1520 close(from[1]);
1521 close(to[0]);
1522
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001523 if ((b = sshbuf_new()) == NULL)
1524 fatal("%s: sshbuf_new failed", __func__);
1525 /* send # of sock, data to be signed */
1526 if ((r = sshbuf_put_u32(b, sock) != 0) ||
1527 (r = sshbuf_put_string(b, data, datalen)) != 0)
1528 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1529 if (ssh_msg_send(to[1], version, b) == -1)
1530 fatal("%s: couldn't send request", __func__);
1531 sshbuf_reset(b);
1532 r = ssh_msg_recv(from[0], b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001533 close(from[0]);
1534 close(to[1]);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001535 if (r < 0) {
1536 error("%s: no reply", __func__);
1537 goto fail;
1538 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001539
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001540 errno = 0;
1541 while (waitpid(pid, &status, 0) < 0) {
1542 if (errno != EINTR) {
1543 error("%s: waitpid %ld: %s",
1544 __func__, (long)pid, strerror(errno));
1545 goto fail;
1546 }
1547 }
1548 if (!WIFEXITED(status)) {
1549 error("%s: exited abnormally", __func__);
1550 goto fail;
1551 }
1552 if (WEXITSTATUS(status) != 0) {
1553 error("%s: exited with status %d",
1554 __func__, WEXITSTATUS(status));
1555 goto fail;
1556 }
1557 if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
1558 error("%s: buffer error: %s", __func__, ssh_err(r));
1559 goto fail;
1560 }
1561 if (rversion != version) {
1562 error("%s: bad version", __func__);
1563 goto fail;
1564 }
1565 if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
1566 error("%s: buffer error: %s", __func__, ssh_err(r));
1567 fail:
1568 signal(SIGCHLD, osigchld);
1569 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001570 return -1;
1571 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001572 signal(SIGCHLD, osigchld);
1573 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001574
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001575 return 0;
1576}
1577
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001578int
1579userauth_hostbased(Authctxt *authctxt)
1580{
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001581 struct ssh *ssh = active_state;
1582 struct sshkey *private = NULL;
1583 struct sshbuf *b = NULL;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001584 const char *service;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001585 u_char *sig = NULL, *keyblob = NULL;
1586 char *fp = NULL, *chost = NULL, *lname = NULL;
1587 size_t siglen = 0, keylen = 0;
1588 int i, r, success = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001589
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001590 if (authctxt->ktypes == NULL) {
1591 authctxt->oktypes = xstrdup(options.hostbased_key_types);
1592 authctxt->ktypes = authctxt->oktypes;
1593 }
djm@openbsd.org1195f4c2015-01-08 10:14:08 +00001594
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001595 /*
1596 * Work through each listed type pattern in HostbasedKeyTypes,
1597 * trying each hostkey that matches the type in turn.
1598 */
1599 for (;;) {
1600 if (authctxt->active_ktype == NULL)
1601 authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
1602 if (authctxt->active_ktype == NULL ||
1603 *authctxt->active_ktype == '\0')
1604 break;
1605 debug3("%s: trying key type %s", __func__,
1606 authctxt->active_ktype);
1607
1608 /* check for a useful key */
1609 private = NULL;
1610 for (i = 0; i < authctxt->sensitive->nkeys; i++) {
1611 if (authctxt->sensitive->keys[i] == NULL ||
1612 authctxt->sensitive->keys[i]->type == KEY_RSA1 ||
1613 authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
1614 continue;
1615 if (match_pattern_list(
1616 sshkey_ssh_name(authctxt->sensitive->keys[i]),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001617 authctxt->active_ktype, 0) != 1)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001618 continue;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001619 /* we take and free the key */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001620 private = authctxt->sensitive->keys[i];
1621 authctxt->sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001622 break;
1623 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001624 /* Found one */
1625 if (private != NULL)
1626 break;
1627 /* No more keys of this type; advance */
1628 authctxt->active_ktype = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001629 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001630 if (private == NULL) {
1631 free(authctxt->oktypes);
1632 authctxt->oktypes = authctxt->ktypes = NULL;
1633 authctxt->active_ktype = NULL;
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001634 debug("No more client hostkeys for hostbased authentication.");
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001635 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001636 }
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001637
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001638 if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
1639 SSH_FP_DEFAULT)) == NULL) {
1640 error("%s: sshkey_fingerprint failed", __func__);
1641 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001642 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001643 debug("%s: trying hostkey %s %s",
1644 __func__, sshkey_ssh_name(private), fp);
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001645
Damien Miller91c18472001-11-12 11:02:03 +11001646 /* figure out a name for the client host */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001647 if ((lname = get_local_name(packet_get_connection_in())) == NULL) {
1648 error("%s: cannot get local ipaddr/name", __func__);
1649 goto out;
Damien Miller91c18472001-11-12 11:02:03 +11001650 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001651
1652 /* XXX sshbuf_put_stringf? */
1653 xasprintf(&chost, "%s.", lname);
1654 debug2("%s: chost %s", __func__, chost);
Damien Miller91c18472001-11-12 11:02:03 +11001655
Ben Lindstrom671388f2001-04-19 20:40:45 +00001656 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1657 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001658
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001659 /* construct data */
1660 if ((b = sshbuf_new()) == NULL) {
1661 error("%s: sshbuf_new failed", __func__);
1662 goto out;
1663 }
1664 if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
1665 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
1666 goto out;
1667 }
1668 if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
1669 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1670 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1671 (r = sshbuf_put_cstring(b, service)) != 0 ||
1672 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1673 (r = sshbuf_put_cstring(b, key_ssh_name(private))) != 0 ||
1674 (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
1675 (r = sshbuf_put_cstring(b, chost)) != 0 ||
1676 (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
1677 error("%s: buffer error: %s", __func__, ssh_err(r));
1678 goto out;
1679 }
1680
1681#ifdef DEBUG_PK
1682 sshbuf_dump(b, stderr);
1683#endif
1684 if (authctxt->sensitive->external_keysign)
1685 r = ssh_keysign(private, &sig, &siglen,
1686 sshbuf_ptr(b), sshbuf_len(b));
1687 else if ((r = sshkey_sign(private, &sig, &siglen,
1688 sshbuf_ptr(b), sshbuf_len(b), datafellows)) != 0)
1689 debug("%s: sshkey_sign: %s", __func__, ssh_err(r));
1690 if (r != 0) {
1691 error("sign using hostkey %s %s failed",
1692 sshkey_ssh_name(private), fp);
1693 goto out;
1694 }
1695 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1696 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1697 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1698 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1699 (r = sshpkt_put_cstring(ssh, key_ssh_name(private))) != 0 ||
1700 (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
1701 (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
1702 (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
1703 (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
1704 (r = sshpkt_send(ssh)) != 0) {
1705 error("%s: packet error: %s", __func__, ssh_err(r));
1706 goto out;
1707 }
1708 success = 1;
1709
1710 out:
1711 if (sig != NULL) {
1712 explicit_bzero(sig, siglen);
1713 free(sig);
1714 }
1715 free(keyblob);
1716 free(lname);
1717 free(fp);
1718 free(chost);
1719 sshkey_free(private);
1720 sshbuf_free(b);
1721
1722 return success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001723}
1724
Damien Miller62cee002000-09-23 17:15:56 +11001725/* find auth method */
1726
Damien Miller62cee002000-09-23 17:15:56 +11001727/*
1728 * given auth method name, if configurable options permit this method fill
1729 * in auth_ident field and return true, otherwise return false.
1730 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001731static int
Damien Miller62cee002000-09-23 17:15:56 +11001732authmethod_is_enabled(Authmethod *method)
1733{
1734 if (method == NULL)
1735 return 0;
1736 /* return false if options indicate this method is disabled */
1737 if (method->enabled == NULL || *method->enabled == 0)
1738 return 0;
1739 /* return false if batch mode is enabled but method needs interactive mode */
1740 if (method->batch_flag != NULL && *method->batch_flag != 0)
1741 return 0;
1742 return 1;
1743}
1744
Ben Lindstrombba81212001-06-25 05:01:22 +00001745static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001746authmethod_lookup(const char *name)
1747{
1748 Authmethod *method = NULL;
1749 if (name != NULL)
1750 for (method = authmethods; method->name != NULL; method++)
1751 if (strcmp(name, method->name) == 0)
1752 return method;
1753 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1754 return NULL;
1755}
1756
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001757/* XXX internal state */
1758static Authmethod *current = NULL;
1759static char *supported = NULL;
1760static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001761
Damien Miller62cee002000-09-23 17:15:56 +11001762/*
1763 * Given the authentication method list sent by the server, return the
1764 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001765 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001766 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001767static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001768authmethod_get(char *authlist)
1769{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001770 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001771 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001772
Damien Miller62cee002000-09-23 17:15:56 +11001773 /* Use a suitable default if we're passed a nil list. */
1774 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001775 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001776
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001777 if (supported == NULL || strcmp(authlist, supported) != 0) {
1778 debug3("start over, passed a different list %s", authlist);
Darren Tuckera627d422013-06-02 07:31:17 +10001779 free(supported);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001780 supported = xstrdup(authlist);
1781 preferred = options.preferred_authentications;
1782 debug3("preferred %s", preferred);
1783 current = NULL;
1784 } else if (current != NULL && authmethod_is_enabled(current))
1785 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001786
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001787 for (;;) {
1788 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001789 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001790 current = NULL;
1791 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001792 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001793 preferred += next;
1794 debug3("authmethod_lookup %s", name);
1795 debug3("remaining preferred: %s", preferred);
1796 if ((current = authmethod_lookup(name)) != NULL &&
1797 authmethod_is_enabled(current)) {
1798 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001799 debug("Next authentication method: %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10001800 free(name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001801 return current;
1802 }
Darren Tuckere52a2602013-06-06 08:22:05 +10001803 free(name);
Damien Millereba71ba2000-04-29 23:57:08 +10001804 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001805}
Damien Miller62cee002000-09-23 17:15:56 +11001806
Ben Lindstrom79073822001-07-04 03:42:30 +00001807static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001808authmethods_get(void)
1809{
1810 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001811 Buffer b;
1812 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001813
Damien Miller0e3b8722002-01-22 23:26:38 +11001814 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001815 for (method = authmethods; method->name != NULL; method++) {
1816 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001817 if (buffer_len(&b) > 0)
1818 buffer_append(&b, ",", 1);
1819 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001820 }
Damien Miller62cee002000-09-23 17:15:56 +11001821 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001822 buffer_append(&b, "\0", 1);
1823 list = xstrdup(buffer_ptr(&b));
1824 buffer_free(&b);
1825 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001826}
Damien Miller01ed2272008-11-05 16:20:46 +11001827