blob: af00fb30c39b8e26adfc958c5588371ae54a66bb [file] [log] [blame]
djm@openbsd.org96bd8952020-02-06 22:48:23 +00001/* $OpenBSD: sshconnect2.c,v 1.320 2020/02/06 22:48:23 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 Millera7a73ee2006-08-05 11:37:59 +100039#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100040#include <string.h>
deraadt@openbsd.org72687c82019-11-13 04:47:52 +000041#include <stdarg.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"
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +000052#include "sshbuf.h"
Damien Millereba71ba2000-04-29 23:57:08 +100053#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"
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +000056#include "sshkey.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"
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +000074#include "utf8.h"
djm@openbsd.org884416b2019-10-31 21:18:28 +000075#include "ssh-sk.h"
djm@openbsd.orge44bb612019-11-12 22:36:44 +000076#include "sk-api.h"
Damien Miller874d77b2000-10-14 16:23:11 +110077
Darren Tucker0efd1552003-08-26 11:49:55 +100078#ifdef GSSAPI
79#include "ssh-gss.h"
80#endif
81
Damien Millereba71ba2000-04-29 23:57:08 +100082/* import */
83extern char *client_version_string;
84extern char *server_version_string;
85extern Options options;
86
87/*
88 * SSH2 key exchange
89 */
90
Ben Lindstrom46c16222000-12-22 01:43:59 +000091u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +100092u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100093
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000094char *xxx_host;
95struct sockaddr *xxx_hostaddr;
96
Ben Lindstrombba81212001-06-25 05:01:22 +000097static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +000098verify_host_key_callback(struct sshkey *hostkey, struct ssh *ssh)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000099{
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000100 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +1000101 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000102 return 0;
103}
104
Damien Millerd925dcd2010-12-01 12:21:51 +1100105static char *
106order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
107{
108 char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
109 size_t maxlen;
110 struct hostkeys *hostkeys;
111 int ktype;
Damien Miller295ee632011-05-29 21:42:31 +1000112 u_int i;
Damien Millerd925dcd2010-12-01 12:21:51 +1100113
114 /* Find all hostkeys for this hostname */
115 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
116 hostkeys = init_hostkeys();
Damien Miller295ee632011-05-29 21:42:31 +1000117 for (i = 0; i < options.num_user_hostfiles; i++)
118 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
119 for (i = 0; i < options.num_system_hostfiles; i++)
120 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
Damien Millerd925dcd2010-12-01 12:21:51 +1100121
djm@openbsd.org96bd8952020-02-06 22:48:23 +0000122 oavail = avail = xstrdup(options.hostkeyalgorithms);
Damien Millerd925dcd2010-12-01 12:21:51 +1100123 maxlen = strlen(avail) + 1;
124 first = xmalloc(maxlen);
125 last = xmalloc(maxlen);
126 *first = *last = '\0';
127
128#define ALG_APPEND(to, from) \
129 do { \
130 if (*to != '\0') \
131 strlcat(to, ",", maxlen); \
132 strlcat(to, from, maxlen); \
133 } while (0)
134
135 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000136 if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
Damien Millerd925dcd2010-12-01 12:21:51 +1100137 fatal("%s: unknown alg %s", __func__, alg);
138 if (lookup_key_in_hostkeys_by_type(hostkeys,
djm@openbsd.org141efe42015-01-14 20:05:27 +0000139 sshkey_type_plain(ktype), NULL))
Damien Millerd925dcd2010-12-01 12:21:51 +1100140 ALG_APPEND(first, alg);
141 else
142 ALG_APPEND(last, alg);
143 }
144#undef ALG_APPEND
djm@openbsd.org35d60222015-01-18 13:33:34 +0000145 xasprintf(&ret, "%s%s%s", first,
146 (*first == '\0' || *last == '\0') ? "" : ",", last);
Damien Millerd925dcd2010-12-01 12:21:51 +1100147 if (*first != '\0')
148 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
149
Darren Tuckera627d422013-06-02 07:31:17 +1000150 free(first);
151 free(last);
152 free(hostname);
153 free(oavail);
Damien Millerd925dcd2010-12-01 12:21:51 +1100154 free_hostkeys(hostkeys);
155
156 return ret;
157}
158
Damien Millereba71ba2000-04-29 23:57:08 +1000159void
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000160ssh_kex2(struct ssh *ssh, char *host, struct sockaddr *hostaddr, u_short port)
Damien Miller874d77b2000-10-14 16:23:11 +1100161{
Damien Miller9235a032014-04-20 13:17:20 +1000162 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
djm@openbsd.org312d2f22018-07-04 13:49:31 +0000163 char *s, *all_key;
djm@openbsd.org96bd8952020-02-06 22:48:23 +0000164 int r, use_known_hosts_order = 0;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000165
166 xxx_host = host;
167 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100168
djm@openbsd.org96bd8952020-02-06 22:48:23 +0000169 /*
170 * If the user has not specified HostkeyAlgorithms, or has only
171 * appended or removed algorithms from that list then prefer algorithms
172 * that are in the list that are supported by known_hosts keys.
173 */
174 if (options.hostkeyalgorithms == NULL ||
175 options.hostkeyalgorithms[0] == '-' ||
176 options.hostkeyalgorithms[0] == '+')
177 use_known_hosts_order = 1;
178
179 /* Expand or fill in HostkeyAlgorithms */
180 all_key = sshkey_alg_list(0, 0, 1, ',');
181 if (kex_assemble_names(&options.hostkeyalgorithms,
182 kex_default_pk_alg(), all_key) != 0)
183 fatal("%s: kex_assemble_namelist", __func__);
184 free(all_key);
185
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000186 if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
187 fatal("%s: kex_names_cat", __func__);
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000188 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(s);
Damien Millera0ff4662001-03-30 10:49:35 +1000189 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000190 compat_cipher_proposal(options.ciphers);
Damien Millera0ff4662001-03-30 10:49:35 +1000191 myproposal[PROPOSAL_ENC_ALGS_STOC] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000192 compat_cipher_proposal(options.ciphers);
dtucker@openbsd.org8c02e362016-05-24 04:43:45 +0000193 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
dtucker@openbsd.org7f8e66f2020-01-23 10:24:29 +0000194 myproposal[PROPOSAL_COMP_ALGS_STOC] =
195 (char *)compression_alg_list(options.compression);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000196 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
197 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
djm@openbsd.org96bd8952020-02-06 22:48:23 +0000198 if (use_known_hosts_order) {
199 /* Query known_hosts and prefer algorithms that appear there */
Damien Millerd925dcd2010-12-01 12:21:51 +1100200 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100201 compat_pkalg_proposal(
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000202 order_hostkeyalgs(host, hostaddr, port));
djm@openbsd.org96bd8952020-02-06 22:48:23 +0000203 } else {
204 /* Use specified HostkeyAlgorithms exactly */
205 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
206 compat_pkalg_proposal(options.hostkeyalgorithms);
Damien Millerd925dcd2010-12-01 12:21:51 +1100207 }
Damien Miller874d77b2000-10-14 16:23:11 +1100208
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000209 if (options.rekey_limit || options.rekey_interval)
djm@openbsd.orged1df722019-01-19 21:34:45 +0000210 ssh_packet_set_rekey_limits(ssh, options.rekey_limit,
dtucker@openbsd.orgc998bf02017-02-03 02:56:00 +0000211 options.rekey_interval);
Damien Millera5539d22003-04-09 20:50:06 +1000212
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000213 /* start key exchange */
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000214 if ((r = kex_setup(ssh, myproposal)) != 0)
markus@openbsd.org57e783c2015-01-20 20:16:21 +0000215 fatal("kex_setup: %s", ssh_err(r));
Damien Miller1f0311c2014-05-15 14:24:09 +1000216#ifdef WITH_OPENSSL
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000217 ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client;
218 ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client;
219 ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client;
220 ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client;
221 ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000222 ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
223 ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100224# ifdef OPENSSL_HAS_ECC
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000225 ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100226# endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000227#endif
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000228 ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client;
229 ssh->kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_client;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000230 ssh->kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100231
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000232 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &ssh->kex->done);
Damien Miller874d77b2000-10-14 16:23:11 +1100233
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000234 /* remove ext-info from the KEX proposals for rekeying */
235 myproposal[PROPOSAL_KEX_ALGS] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000236 compat_kex_proposal(options.kex_algorithms);
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000237 if ((r = kex_prop2buf(ssh->kex->my, myproposal)) != 0)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000238 fatal("kex_prop2buf: %s", ssh_err(r));
Darren Tucker36331b52010-01-08 16:50:41 +1100239
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000240 session_id2 = ssh->kex->session_id;
241 session_id2_len = ssh->kex->session_id_len;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000242
Damien Miller874d77b2000-10-14 16:23:11 +1100243#ifdef DEBUG_KEXDH
244 /* send 1st encrypted/maced/compressed message */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000245 if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 ||
246 (r = sshpkt_put_cstring(ssh, "markus")) != 0 ||
markus@openbsd.org394a8422018-07-11 18:55:11 +0000247 (r = sshpkt_send(ssh)) != 0 ||
248 (r = ssh_packet_write_wait(ssh)) != 0)
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000249 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100250#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000251}
Damien Millerb1715dc2000-05-30 13:44:51 +1000252
Damien Millereba71ba2000-04-29 23:57:08 +1000253/*
254 * Authenticate user
255 */
Damien Miller62cee002000-09-23 17:15:56 +1100256
djm@openbsd.org141efe42015-01-14 20:05:27 +0000257typedef struct cauthctxt Authctxt;
258typedef struct cauthmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000259typedef struct identity Identity;
260typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100261
Damien Miller280ecfb2003-05-14 13:46:00 +1000262struct identity {
263 TAILQ_ENTRY(identity) next;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000264 int agent_fd; /* >=0 if agent supports key */
265 struct sshkey *key; /* public/private key */
Damien Miller280ecfb2003-05-14 13:46:00 +1000266 char *filename; /* comment for agent-only keys */
267 int tried;
268 int isprivate; /* key points to the private key */
Damien Miller5ceddc32013-02-15 12:18:32 +1100269 int userprovided;
Damien Miller280ecfb2003-05-14 13:46:00 +1000270};
271TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100272
djm@openbsd.org141efe42015-01-14 20:05:27 +0000273struct cauthctxt {
Damien Miller62cee002000-09-23 17:15:56 +1100274 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000275 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100276 const char *host;
277 const char *service;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000278 struct cauthmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000279 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000280 char *authlist;
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000281#ifdef GSSAPI
282 /* gssapi */
283 gss_OID_set gss_supported_mechs;
284 u_int mech_tried;
285#endif
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000286 /* pubkey */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000287 struct idlist keys;
288 int agent_fd;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000289 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000290 Sensitive *sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000291 char *oktypes, *ktypes;
292 const char *active_ktype;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000293 /* kbd-interactive */
294 int info_req_seen;
djm@openbsd.org4a526942019-01-04 03:27:50 +0000295 int attempt_kbdint;
296 /* password */
297 int attempt_passwd;
Darren Tucker0efd1552003-08-26 11:49:55 +1000298 /* generic */
299 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100300};
djm@openbsd.org141efe42015-01-14 20:05:27 +0000301
302struct cauthmethod {
Damien Miller62cee002000-09-23 17:15:56 +1100303 char *name; /* string to compare against server's list */
djm@openbsd.org64c95982019-01-19 21:40:21 +0000304 int (*userauth)(struct ssh *ssh);
305 void (*cleanup)(struct ssh *ssh);
Damien Miller62cee002000-09-23 17:15:56 +1100306 int *enabled; /* flag in option struct that enables method */
307 int *batch_flag; /* flag in option struct that disables method */
308};
309
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000310static int input_userauth_service_accept(int, u_int32_t, struct ssh *);
311static int input_userauth_ext_info(int, u_int32_t, struct ssh *);
312static int input_userauth_success(int, u_int32_t, struct ssh *);
313static int input_userauth_failure(int, u_int32_t, struct ssh *);
314static int input_userauth_banner(int, u_int32_t, struct ssh *);
315static int input_userauth_error(int, u_int32_t, struct ssh *);
316static int input_userauth_info_req(int, u_int32_t, struct ssh *);
317static int input_userauth_pk_ok(int, u_int32_t, struct ssh *);
318static int input_userauth_passwd_changereq(int, u_int32_t, struct ssh *);
Damien Miller874d77b2000-10-14 16:23:11 +1100319
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000320static int userauth_none(struct ssh *);
321static int userauth_pubkey(struct ssh *);
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000322static int userauth_passwd(struct ssh *);
323static int userauth_kbdint(struct ssh *);
324static int userauth_hostbased(struct ssh *);
Damien Miller62cee002000-09-23 17:15:56 +1100325
Darren Tucker0efd1552003-08-26 11:49:55 +1000326#ifdef GSSAPI
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000327static int userauth_gssapi(struct ssh *);
328static void userauth_gssapi_cleanup(struct ssh *);
329static int input_gssapi_response(int type, u_int32_t, struct ssh *);
330static int input_gssapi_token(int type, u_int32_t, struct ssh *);
331static int input_gssapi_error(int, u_int32_t, struct ssh *);
332static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000333#endif
334
djm@openbsd.org64c95982019-01-19 21:40:21 +0000335void userauth(struct ssh *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000336
djm@openbsd.org38e83e42019-02-12 23:53:10 +0000337static void pubkey_cleanup(struct ssh *);
djm@openbsd.org64c95982019-01-19 21:40:21 +0000338static int sign_and_send_pubkey(struct ssh *ssh, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000339static void pubkey_prepare(Authctxt *);
djm@openbsd.orgb9844a42016-12-04 23:54:02 +0000340static void pubkey_reset(Authctxt *);
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000341static struct sshkey *load_identity_file(Identity *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000342
Ben Lindstrombba81212001-06-25 05:01:22 +0000343static Authmethod *authmethod_get(char *authlist);
344static Authmethod *authmethod_lookup(const char *name);
345static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100346
347Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000348#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100349 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000350 userauth_gssapi,
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000351 userauth_gssapi_cleanup,
Darren Tucker0efd1552003-08-26 11:49:55 +1000352 &options.gss_authentication,
353 NULL},
354#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000355 {"hostbased",
356 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100357 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000358 &options.hostbased_authentication,
359 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000360 {"publickey",
361 userauth_pubkey,
djm@openbsd.org38e83e42019-02-12 23:53:10 +0000362 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000363 &options.pubkey_authentication,
364 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100365 {"keyboard-interactive",
366 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100367 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100368 &options.kbd_interactive_authentication,
369 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000370 {"password",
371 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100372 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000373 &options.password_authentication,
374 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100375 {"none",
376 userauth_none,
377 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100378 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100379 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100380 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100381};
382
383void
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000384ssh_userauth2(struct ssh *ssh, const char *local_user,
385 const char *server_user, char *host, Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100386{
387 Authctxt authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000388 int r;
Damien Miller62cee002000-09-23 17:15:56 +1100389
Ben Lindstrom551ea372001-06-05 18:56:16 +0000390 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000391 options.kbd_interactive_authentication = 1;
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000392 if (options.preferred_authentications == NULL)
393 options.preferred_authentications = authmethods_get();
394
Damien Miller62cee002000-09-23 17:15:56 +1100395 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000396 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller62cee002000-09-23 17:15:56 +1100397 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000398 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100399 authctxt.host = host;
400 authctxt.service = "ssh-connection"; /* service name */
401 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100402 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000403 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000404 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000405 authctxt.sensitive = sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000406 authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000407 authctxt.info_req_seen = 0;
djm@openbsd.org4a526942019-01-04 03:27:50 +0000408 authctxt.attempt_kbdint = 0;
409 authctxt.attempt_passwd = 0;
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000410#if GSSAPI
411 authctxt.gss_supported_mechs = NULL;
412 authctxt.mech_tried = 0;
413#endif
djm@openbsd.orgf14564c2015-01-15 11:04:36 +0000414 authctxt.agent_fd = -1;
djm@openbsd.orgaaed6352018-11-16 02:46:20 +0000415 pubkey_prepare(&authctxt);
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000416 if (authctxt.method == NULL) {
417 fatal("%s: internal error: cannot send userauth none request",
418 __func__);
419 }
Damien Miller62cee002000-09-23 17:15:56 +1100420
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000421 if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
422 (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
423 (r = sshpkt_send(ssh)) != 0)
424 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller62cee002000-09-23 17:15:56 +1100425
markus@openbsd.org94583be2017-05-30 14:19:15 +0000426 ssh->authctxt = &authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000427 ssh_dispatch_init(ssh, &input_userauth_error);
428 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
429 ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
markus@openbsd.org92e9fe62017-05-31 07:00:13 +0000430 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt.success); /* loop until success */
djm@openbsd.org38e83e42019-02-12 23:53:10 +0000431 pubkey_cleanup(ssh);
markus@openbsd.org94583be2017-05-30 14:19:15 +0000432 ssh->authctxt = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100433
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000434 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
Damien Millerf842fcb2003-05-15 12:01:28 +1000435
dtucker@openbsd.orgf31c6542016-09-22 02:29:57 +0000436 if (!authctxt.success)
437 fatal("Authentication failed.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000438 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100439}
Damien Millerf842fcb2003-05-15 12:01:28 +1000440
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000441/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000442static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000443input_userauth_service_accept(int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000444{
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000445 int r;
446
447 if (ssh_packet_remaining(ssh) > 0) {
448 char *reply;
449
450 if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
451 goto out;
452 debug2("service_accept: %s", reply);
453 free(reply);
454 } else {
455 debug2("buggy server: service_accept w/o service");
456 }
457 if ((r = sshpkt_get_end(ssh)) != 0)
458 goto out;
459 debug("SSH2_MSG_SERVICE_ACCEPT received");
460
461 /* initial userauth request */
djm@openbsd.org64c95982019-01-19 21:40:21 +0000462 userauth_none(ssh);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000463
464 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error);
465 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
466 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
467 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
468 r = 0;
469 out:
470 return r;
471}
472
473/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000474static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000475input_userauth_ext_info(int type, u_int32_t seqnr, struct ssh *ssh)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000476{
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000477 return kex_input_ext_info(type, seqnr, ssh);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000478}
479
Damien Miller62cee002000-09-23 17:15:56 +1100480void
djm@openbsd.org64c95982019-01-19 21:40:21 +0000481userauth(struct ssh *ssh, char *authlist)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000482{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000483 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000484
Damien Miller01ed2272008-11-05 16:20:46 +1100485 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
djm@openbsd.org64c95982019-01-19 21:40:21 +0000486 authctxt->method->cleanup(ssh);
Damien Miller01ed2272008-11-05 16:20:46 +1100487
Darren Tuckera627d422013-06-02 07:31:17 +1000488 free(authctxt->methoddata);
489 authctxt->methoddata = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000490 if (authlist == NULL) {
491 authlist = authctxt->authlist;
492 } else {
Darren Tuckera627d422013-06-02 07:31:17 +1000493 free(authctxt->authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000494 authctxt->authlist = authlist;
495 }
496 for (;;) {
497 Authmethod *method = authmethod_get(authlist);
498 if (method == NULL)
dtucker@openbsd.org4626e392017-06-14 00:31:38 +0000499 fatal("%s@%s: Permission denied (%s).",
500 authctxt->server_user, authctxt->host, authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000501 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000502
503 /* reset the per method handler */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000504 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_PER_METHOD_MIN,
Damien Millerf842fcb2003-05-15 12:01:28 +1000505 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
506
507 /* and try new method */
djm@openbsd.org64c95982019-01-19 21:40:21 +0000508 if (method->userauth(ssh) != 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000509 debug2("we sent a %s packet, wait for reply", method->name);
510 break;
511 } else {
512 debug2("we did not send a packet, disable method");
513 method->enabled = NULL;
514 }
515 }
516}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000517
Damien Millerf7475d72008-11-03 19:26:18 +1100518/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000519static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000520input_userauth_error(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100521{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000522 fatal("%s: bad message during authentication: type %d", __func__, type);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000523 return 0;
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000524}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000525
Damien Millerf7475d72008-11-03 19:26:18 +1100526/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000527static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000528input_userauth_banner(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000529{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000530 char *msg = NULL;
djm@openbsd.orged1df722019-01-19 21:34:45 +0000531 size_t len;
532 int r;
Darren Tucker79644822003-10-08 17:37:58 +1000533
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000534 debug3("%s", __func__);
djm@openbsd.orged1df722019-01-19 21:34:45 +0000535 if ((r = sshpkt_get_cstring(ssh, &msg, &len)) != 0 ||
djm@openbsd.org64c95982019-01-19 21:40:21 +0000536 (r = sshpkt_get_cstring(ssh, NULL, NULL)) != 0)
djm@openbsd.orged1df722019-01-19 21:34:45 +0000537 goto out;
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000538 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO)
539 fmprintf(stderr, "%s", msg);
djm@openbsd.orged1df722019-01-19 21:34:45 +0000540 r = 0;
541 out:
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000542 free(msg);
djm@openbsd.orged1df722019-01-19 21:34:45 +0000543 return r;
Damien Miller62cee002000-09-23 17:15:56 +1100544}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000545
Damien Millerf7475d72008-11-03 19:26:18 +1100546/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000547static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000548input_userauth_success(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100549{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000550 Authctxt *authctxt = ssh->authctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100551
Damien Miller62cee002000-09-23 17:15:56 +1100552 if (authctxt == NULL)
djm@openbsd.org64c95982019-01-19 21:40:21 +0000553 fatal("%s: no authentication context", __func__);
Darren Tuckera627d422013-06-02 07:31:17 +1000554 free(authctxt->authlist);
555 authctxt->authlist = NULL;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100556 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
djm@openbsd.org64c95982019-01-19 21:40:21 +0000557 authctxt->method->cleanup(ssh);
Darren Tuckera627d422013-06-02 07:31:17 +1000558 free(authctxt->methoddata);
559 authctxt->methoddata = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100560 authctxt->success = 1; /* break out */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000561 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100562}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000563
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000564#if 0
565static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000566input_userauth_success_unexpected(int type, u_int32_t seq, struct ssh *ssh)
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100567{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000568 Authctxt *authctxt = ssh->authctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100569
570 if (authctxt == NULL)
571 fatal("%s: no authentication context", __func__);
572
573 fatal("Unexpected authentication success during %s.",
574 authctxt->method->name);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000575 return 0;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100576}
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000577#endif
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100578
Damien Millerf7475d72008-11-03 19:26:18 +1100579/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000580static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000581input_userauth_failure(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100582{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000583 Authctxt *authctxt = ssh->authctxt;
Damien Miller62cee002000-09-23 17:15:56 +1100584 char *authlist = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000585 u_char partial;
Damien Miller62cee002000-09-23 17:15:56 +1100586
587 if (authctxt == NULL)
588 fatal("input_userauth_failure: no authentication context");
589
dtucker@openbsd.org696fb422019-07-07 01:05:00 +0000590 if (sshpkt_get_cstring(ssh, &authlist, NULL) != 0 ||
591 sshpkt_get_u8(ssh, &partial) != 0 ||
592 sshpkt_get_end(ssh) != 0)
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000593 goto out;
Damien Miller62cee002000-09-23 17:15:56 +1100594
Damien Miller62e9c4f2013-04-23 15:15:49 +1000595 if (partial != 0) {
dtucker@openbsd.org83cbca62016-07-22 05:46:11 +0000596 verbose("Authenticated with partial success.");
Damien Miller62e9c4f2013-04-23 15:15:49 +1000597 /* reset state */
djm@openbsd.orgb9844a42016-12-04 23:54:02 +0000598 pubkey_reset(authctxt);
Damien Miller62e9c4f2013-04-23 15:15:49 +1000599 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000600 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100601
djm@openbsd.org64c95982019-01-19 21:40:21 +0000602 userauth(ssh, authlist);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000603 authlist = NULL;
604 out:
605 free(authlist);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000606 return 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000607}
Damien Millerf7475d72008-11-03 19:26:18 +1100608
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000609/*
610 * Format an identity for logging including filename, key type, fingerprint
611 * and location (agent, etc.). Caller must free.
612 */
613static char *
614format_identity(Identity *id)
615{
616 char *fp = NULL, *ret = NULL;
djm@openbsd.org884416b2019-10-31 21:18:28 +0000617 const char *note = "";
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000618
619 if (id->key != NULL) {
620 fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
621 SSH_FP_DEFAULT);
622 }
djm@openbsd.org884416b2019-10-31 21:18:28 +0000623 if (id->key) {
624 if ((id->key->flags & SSHKEY_FLAG_EXT) != 0)
625 note = " token";
markus@openbsd.org2c557442019-11-12 19:33:08 +0000626 else if (sshkey_is_sk(id->key))
naddy@openbsd.orga47f6a62020-02-06 22:30:54 +0000627 note = " authenticator";
djm@openbsd.org884416b2019-10-31 21:18:28 +0000628 }
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000629 xasprintf(&ret, "%s %s%s%s%s%s%s",
630 id->filename,
631 id->key ? sshkey_type(id->key) : "", id->key ? " " : "",
632 fp ? fp : "",
djm@openbsd.org884416b2019-10-31 21:18:28 +0000633 id->userprovided ? " explicit" : "", note,
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000634 id->agent_fd != -1 ? " agent" : "");
635 free(fp);
636 return ret;
637}
638
Damien Millerf7475d72008-11-03 19:26:18 +1100639/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000640static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000641input_userauth_pk_ok(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000642{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000643 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000644 struct sshkey *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000645 Identity *id = NULL;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000646 int pktype, found = 0, sent = 0;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000647 size_t blen;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000648 char *pkalg = NULL, *fp = NULL, *ident = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000649 u_char *pkblob = NULL;
650 int r;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000651
652 if (authctxt == NULL)
653 fatal("input_userauth_pk_ok: no authentication context");
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000654
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000655 if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
656 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
657 (r = sshpkt_get_end(ssh)) != 0)
658 goto done;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000659
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000660 if ((pktype = sshkey_type_from_name(pkalg)) == KEY_UNSPEC) {
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000661 debug("%s: server sent unknown pkalg %s", __func__, pkalg);
Damien Miller280ecfb2003-05-14 13:46:00 +1000662 goto done;
663 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000664 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
665 debug("no key from blob. pkalg %s: %s", pkalg, ssh_err(r));
Damien Miller280ecfb2003-05-14 13:46:00 +1000666 goto done;
667 }
668 if (key->type != pktype) {
669 error("input_userauth_pk_ok: type mismatch "
670 "for decoded key (received %d, expected %d)",
671 key->type, pktype);
672 goto done;
673 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000674
Darren Tuckerd05b6012003-10-15 15:55:59 +1000675 /*
676 * search keys in the reverse order, because last candidate has been
677 * moved to the end of the queue. this also avoids confusion by
678 * duplicate keys
679 */
Damien Miller0b51a522004-04-20 20:07:19 +1000680 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000681 if (sshkey_equal(key, id->key)) {
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000682 found = 1;
Damien Miller280ecfb2003-05-14 13:46:00 +1000683 break;
684 }
685 }
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000686 if (!found || id == NULL) {
687 fp = sshkey_fingerprint(key, options.fingerprint_hash,
688 SSH_FP_DEFAULT);
689 error("%s: server replied with unknown key: %s %s", __func__,
690 sshkey_type(key), fp == NULL ? "<ERROR>" : fp);
691 goto done;
692 }
693 ident = format_identity(id);
694 debug("Server accepts key: %s", ident);
djm@openbsd.org64c95982019-01-19 21:40:21 +0000695 sent = sign_and_send_pubkey(ssh, id);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000696 r = 0;
697 done:
698 sshkey_free(key);
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000699 free(ident);
700 free(fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000701 free(pkalg);
702 free(pkblob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000703
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000704 /* try another method if we did not send a packet */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000705 if (r == 0 && sent == 0)
djm@openbsd.org64c95982019-01-19 21:40:21 +0000706 userauth(ssh, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000707 return r;
Damien Miller62cee002000-09-23 17:15:56 +1100708}
709
Darren Tucker0efd1552003-08-26 11:49:55 +1000710#ifdef GSSAPI
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000711static int
djm@openbsd.org64c95982019-01-19 21:40:21 +0000712userauth_gssapi(struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000713{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000714 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000715 Gssctxt *gssctxt = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000716 OM_uint32 min;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000717 int r, ok = 0;
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000718 gss_OID mech = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000719
720 /* Try one GSSAPI method at a time, rather than sending them all at
721 * once. */
722
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000723 if (authctxt->gss_supported_mechs == NULL)
724 gss_indicate_mechs(&min, &authctxt->gss_supported_mechs);
Darren Tucker0efd1552003-08-26 11:49:55 +1000725
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000726 /* Check to see whether the mechanism is usable before we offer it */
727 while (authctxt->mech_tried < authctxt->gss_supported_mechs->count &&
728 !ok) {
729 mech = &authctxt->gss_supported_mechs->
730 elements[authctxt->mech_tried];
Darren Tucker0efd1552003-08-26 11:49:55 +1000731 /* My DER encoding requires length<128 */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000732 if (mech->length < 128 && ssh_gssapi_check_mechanism(&gssctxt,
733 mech, authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000734 ok = 1; /* Mechanism works */
735 } else {
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000736 authctxt->mech_tried++;
Darren Tucker0efd1552003-08-26 11:49:55 +1000737 }
738 }
739
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000740 if (!ok || mech == NULL)
Damien Millereccb9de2005-06-17 12:59:34 +1000741 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000742
743 authctxt->methoddata=(void *)gssctxt;
744
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000745 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
746 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
747 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
748 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
749 (r = sshpkt_put_u32(ssh, 1)) != 0 ||
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000750 (r = sshpkt_put_u32(ssh, (mech->length) + 2)) != 0 ||
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000751 (r = sshpkt_put_u8(ssh, SSH_GSS_OIDTYPE)) != 0 ||
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000752 (r = sshpkt_put_u8(ssh, mech->length)) != 0 ||
753 (r = sshpkt_put(ssh, mech->elements, mech->length)) != 0 ||
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000754 (r = sshpkt_send(ssh)) != 0)
755 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +1000756
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000757 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
758 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
759 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
760 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000761
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000762 authctxt->mech_tried++; /* Move along to next candidate */
Darren Tucker0efd1552003-08-26 11:49:55 +1000763
764 return 1;
765}
766
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000767static void
768userauth_gssapi_cleanup(struct ssh *ssh)
769{
770 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
771 Gssctxt *gssctxt = (Gssctxt *)authctxt->methoddata;
772
773 ssh_gssapi_delete_ctx(&gssctxt);
774 authctxt->methoddata = NULL;
775
776 free(authctxt->gss_supported_mechs);
777 authctxt->gss_supported_mechs = NULL;
778}
779
Damien Miller91c6aa42003-11-17 21:20:18 +1100780static OM_uint32
djm@openbsd.org39896b72017-05-31 05:08:46 +0000781process_gssapi_token(struct ssh *ssh, gss_buffer_t recv_tok)
Damien Miller91c6aa42003-11-17 21:20:18 +1100782{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000783 Authctxt *authctxt = ssh->authctxt;
Damien Miller91c6aa42003-11-17 21:20:18 +1100784 Gssctxt *gssctxt = authctxt->methoddata;
785 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000786 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
787 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100788 OM_uint32 status, ms, flags;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000789 int r;
Damien Miller787b2ec2003-11-21 23:56:47 +1100790
Damien Miller91c6aa42003-11-17 21:20:18 +1100791 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100792 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100793
794 if (send_tok.length > 0) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000795 u_char type = GSS_ERROR(status) ?
796 SSH2_MSG_USERAUTH_GSSAPI_ERRTOK :
797 SSH2_MSG_USERAUTH_GSSAPI_TOKEN;
Damien Miller787b2ec2003-11-21 23:56:47 +1100798
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000799 if ((r = sshpkt_start(ssh, type)) != 0 ||
800 (r = sshpkt_put_string(ssh, send_tok.value,
801 send_tok.length)) != 0 ||
802 (r = sshpkt_send(ssh)) != 0)
803 fatal("%s: %s", __func__, ssh_err(r));
804
Damien Miller91c6aa42003-11-17 21:20:18 +1100805 gss_release_buffer(&ms, &send_tok);
806 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100807
Damien Miller91c6aa42003-11-17 21:20:18 +1100808 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100809 /* send either complete or MIC, depending on mechanism */
810 if (!(flags & GSS_C_INTEG_FLAG)) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000811 if ((r = sshpkt_start(ssh,
812 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE)) != 0 ||
813 (r = sshpkt_send(ssh)) != 0)
814 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0425d402003-11-17 22:18:21 +1100815 } else {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000816 struct sshbuf *b;
817
818 if ((b = sshbuf_new()) == NULL)
819 fatal("%s: sshbuf_new failed", __func__);
820 ssh_gssapi_buildmic(b, authctxt->server_user,
Damien Miller0425d402003-11-17 22:18:21 +1100821 authctxt->service, "gssapi-with-mic");
822
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000823 if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
824 fatal("%s: sshbuf_mutable_ptr failed", __func__);
825 gssbuf.length = sshbuf_len(b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100826
Damien Miller0425d402003-11-17 22:18:21 +1100827 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100828
Damien Miller0425d402003-11-17 22:18:21 +1100829 if (!GSS_ERROR(status)) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000830 if ((r = sshpkt_start(ssh,
831 SSH2_MSG_USERAUTH_GSSAPI_MIC)) != 0 ||
832 (r = sshpkt_put_string(ssh, mic.value,
833 mic.length)) != 0 ||
834 (r = sshpkt_send(ssh)) != 0)
835 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0425d402003-11-17 22:18:21 +1100836 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100837
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000838 sshbuf_free(b);
Damien Miller0425d402003-11-17 22:18:21 +1100839 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100840 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100841 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100842
Damien Miller91c6aa42003-11-17 21:20:18 +1100843 return status;
844}
845
Damien Millerf7475d72008-11-03 19:26:18 +1100846/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000847static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000848input_gssapi_response(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000849{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000850 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000851 Gssctxt *gssctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000852 size_t oidlen;
853 u_char *oidv = NULL;
854 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000855
856 if (authctxt == NULL)
857 fatal("input_gssapi_response: no authentication context");
858 gssctxt = authctxt->methoddata;
859
860 /* Setup our OID */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000861 if ((r = sshpkt_get_string(ssh, &oidv, &oidlen)) != 0)
862 goto done;
Darren Tucker0efd1552003-08-26 11:49:55 +1000863
Darren Tucker655a5e02003-11-03 20:09:03 +1100864 if (oidlen <= 2 ||
865 oidv[0] != SSH_GSS_OIDTYPE ||
866 oidv[1] != oidlen - 2) {
867 debug("Badly encoded mechanism OID received");
djm@openbsd.org0a5f2ea2019-01-21 01:05:00 +0000868 userauth(ssh, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000869 goto ok;
Darren Tucker0efd1552003-08-26 11:49:55 +1000870 }
871
Darren Tucker655a5e02003-11-03 20:09:03 +1100872 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
873 fatal("Server returned different OID than expected");
874
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000875 if ((r = sshpkt_get_end(ssh)) != 0)
876 goto done;
Darren Tucker0efd1552003-08-26 11:49:55 +1000877
djm@openbsd.org39896b72017-05-31 05:08:46 +0000878 if (GSS_ERROR(process_gssapi_token(ssh, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000879 /* Start again with next method on list */
880 debug("Trying to start again");
djm@openbsd.org0a5f2ea2019-01-21 01:05:00 +0000881 userauth(ssh, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000882 goto ok;
Darren Tucker0efd1552003-08-26 11:49:55 +1000883 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000884 ok:
885 r = 0;
886 done:
887 free(oidv);
888 return r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000889}
890
Damien Millerf7475d72008-11-03 19:26:18 +1100891/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000892static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000893input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000894{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000895 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000896 gss_buffer_desc recv_tok;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000897 u_char *p = NULL;
898 size_t len;
Damien Miller91c6aa42003-11-17 21:20:18 +1100899 OM_uint32 status;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000900 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000901
902 if (authctxt == NULL)
903 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000904
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000905 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
906 (r = sshpkt_get_end(ssh)) != 0)
907 goto out;
Darren Tucker0efd1552003-08-26 11:49:55 +1000908
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000909 recv_tok.value = p;
910 recv_tok.length = len;
djm@openbsd.org39896b72017-05-31 05:08:46 +0000911 status = process_gssapi_token(ssh, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000912
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000913 /* Start again with the next method in the list */
Darren Tucker0efd1552003-08-26 11:49:55 +1000914 if (GSS_ERROR(status)) {
djm@openbsd.org0a5f2ea2019-01-21 01:05:00 +0000915 userauth(ssh, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000916 /* ok */
Darren Tucker0efd1552003-08-26 11:49:55 +1000917 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000918 r = 0;
919 out:
920 free(p);
921 return r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000922}
923
Damien Millerf7475d72008-11-03 19:26:18 +1100924/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000925static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000926input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000927{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000928 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000929 Gssctxt *gssctxt;
930 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
931 gss_buffer_desc recv_tok;
Damien Millerd677ad12013-04-23 15:18:51 +1000932 OM_uint32 ms;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000933 u_char *p = NULL;
934 size_t len;
935 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000936
937 if (authctxt == NULL)
938 fatal("input_gssapi_response: no authentication context");
939 gssctxt = authctxt->methoddata;
940
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000941 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
942 (r = sshpkt_get_end(ssh)) != 0) {
943 free(p);
944 return r;
945 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000946
947 /* Stick it into GSSAPI and see what it says */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000948 recv_tok.value = p;
949 recv_tok.length = len;
Damien Millerd677ad12013-04-23 15:18:51 +1000950 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000951 &recv_tok, &send_tok, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000952 free(p);
Darren Tucker0efd1552003-08-26 11:49:55 +1000953 gss_release_buffer(&ms, &send_tok);
954
955 /* Server will be returning a failed packet after this one */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000956 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000957}
958
Damien Millerf7475d72008-11-03 19:26:18 +1100959/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000960static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000961input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000962{
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000963 char *msg = NULL;
964 char *lang = NULL;
965 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000966
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000967 if ((r = sshpkt_get_u32(ssh, NULL)) != 0 || /* maj */
968 (r = sshpkt_get_u32(ssh, NULL)) != 0 || /* min */
969 (r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
970 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
971 goto out;
972 r = sshpkt_get_end(ssh);
Damien Miller15d72a02005-11-05 15:07:33 +1100973 debug("Server GSSAPI Error:\n%s", msg);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000974 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000975 free(msg);
976 free(lang);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000977 return r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000978}
979#endif /* GSSAPI */
980
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000981static int
djm@openbsd.org64c95982019-01-19 21:40:21 +0000982userauth_none(struct ssh *ssh)
Damien Miller874d77b2000-10-14 16:23:11 +1100983{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000984 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000985 int r;
986
Damien Miller874d77b2000-10-14 16:23:11 +1100987 /* initial userauth request */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000988 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
989 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
990 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
991 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
992 (r = sshpkt_send(ssh)) != 0)
993 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100994 return 1;
995}
996
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000997static int
djm@openbsd.org64c95982019-01-19 21:40:21 +0000998userauth_passwd(struct ssh *ssh)
Damien Millereba71ba2000-04-29 23:57:08 +1000999{
djm@openbsd.org64c95982019-01-19 21:40:21 +00001000 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
djm@openbsd.org91b19192018-11-28 06:00:38 +00001001 char *password, *prompt = NULL;
Darren Tuckerab791692010-01-08 18:48:02 +11001002 const char *host = options.host_key_alias ? options.host_key_alias :
1003 authctxt->host;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001004 int r;
Damien Millereba71ba2000-04-29 23:57:08 +10001005
djm@openbsd.org4a526942019-01-04 03:27:50 +00001006 if (authctxt->attempt_passwd++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +10001007 return 0;
1008
djm@openbsd.org4a526942019-01-04 03:27:50 +00001009 if (authctxt->attempt_passwd != 1)
Damien Millerd3a18572000-06-07 19:55:44 +10001010 error("Permission denied, please try again.");
1011
djm@openbsd.org91b19192018-11-28 06:00:38 +00001012 xasprintf(&prompt, "%s@%s's password: ", authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +10001013 password = read_passphrase(prompt, 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001014 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1015 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1016 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1017 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1018 (r = sshpkt_put_u8(ssh, 0)) != 0 ||
1019 (r = sshpkt_put_cstring(ssh, password)) != 0 ||
1020 (r = sshpkt_add_padding(ssh, 64)) != 0 ||
1021 (r = sshpkt_send(ssh)) != 0)
1022 fatal("%s: %s", __func__, ssh_err(r));
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001023
djm@openbsd.org91b19192018-11-28 06:00:38 +00001024 free(prompt);
1025 if (password != NULL)
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001026 freezero(password, strlen(password));
1027
1028 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001029 &input_userauth_passwd_changereq);
1030
Damien Millereba71ba2000-04-29 23:57:08 +10001031 return 1;
1032}
Damien Millerf7475d72008-11-03 19:26:18 +11001033
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001034/*
1035 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
1036 */
Damien Millerf7475d72008-11-03 19:26:18 +11001037/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +00001038static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001039input_userauth_passwd_changereq(int type, u_int32_t seqnr, struct ssh *ssh)
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001040{
markus@openbsd.org94583be2017-05-30 14:19:15 +00001041 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001042 char *info = NULL, *lang = NULL, *password = NULL, *retype = NULL;
dtucker@openbsd.orga54eb272017-08-27 00:38:41 +00001043 char prompt[256];
djm@openbsd.org3e032a92017-01-30 00:32:03 +00001044 const char *host;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001045 int r;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001046
1047 debug2("input_userauth_passwd_changereq");
1048
1049 if (authctxt == NULL)
1050 fatal("input_userauth_passwd_changereq: "
1051 "no authentication context");
djm@openbsd.org3e032a92017-01-30 00:32:03 +00001052 host = options.host_key_alias ? options.host_key_alias : authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001053
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001054 if ((r = sshpkt_get_cstring(ssh, &info, NULL)) != 0 ||
1055 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1056 goto out;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001057 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001058 logit("%s", info);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001059 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1060 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1061 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1062 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1063 (r = sshpkt_put_u8(ssh, 1)) != 0) /* additional info */
1064 goto out;
1065
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001066 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001067 "Enter %.30s@%.128s's old password: ",
Darren Tuckerab791692010-01-08 18:48:02 +11001068 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001069 password = read_passphrase(prompt, 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001070 if ((r = sshpkt_put_cstring(ssh, password)) != 0)
1071 goto out;
1072
1073 freezero(password, strlen(password));
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001074 password = NULL;
1075 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001076 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001077 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +11001078 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001079 password = read_passphrase(prompt, RP_ALLOW_EOF);
1080 if (password == NULL) {
1081 /* bail out */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001082 r = 0;
1083 goto out;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001084 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001085 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001086 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +11001087 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001088 retype = read_passphrase(prompt, 0);
1089 if (strcmp(password, retype) != 0) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001090 freezero(password, strlen(password));
Damien Miller996acd22003-04-09 20:59:48 +10001091 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001092 password = NULL;
1093 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001094 freezero(retype, strlen(retype));
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001095 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001096 if ((r = sshpkt_put_cstring(ssh, password)) != 0 ||
1097 (r = sshpkt_add_padding(ssh, 64)) != 0 ||
1098 (r = sshpkt_send(ssh)) != 0)
1099 goto out;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001100
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001101 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001102 &input_userauth_passwd_changereq);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001103 r = 0;
1104 out:
1105 if (password)
1106 freezero(password, strlen(password));
1107 free(info);
1108 free(lang);
1109 return r;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001110}
Damien Millereba71ba2000-04-29 23:57:08 +10001111
djm@openbsd.org26074382018-03-03 03:01:50 +00001112/*
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001113 * Select an algorithm for publickey signatures.
1114 * Returns algorithm (caller must free) or NULL if no mutual algorithm found.
1115 *
1116 * Call with ssh==NULL to ignore server-sig-algs extension list and
1117 * only attempt with the key's base signature type.
djm@openbsd.org26074382018-03-03 03:01:50 +00001118 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001119static char *
1120key_sig_algorithm(struct ssh *ssh, const struct sshkey *key)
djm@openbsd.org26074382018-03-03 03:01:50 +00001121{
djm@openbsd.orgb4d4eda2018-07-03 13:20:25 +00001122 char *allowed, *oallowed, *cp, *tmp, *alg = NULL;
djm@openbsd.org26074382018-03-03 03:01:50 +00001123
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001124 /*
1125 * The signature algorithm will only differ from the key algorithm
1126 * for RSA keys/certs and when the server advertises support for
1127 * newer (SHA2) algorithms.
1128 */
1129 if (ssh == NULL || ssh->kex->server_sig_algs == NULL ||
djm@openbsd.org1a4a9cf2018-10-11 03:48:04 +00001130 (key->type != KEY_RSA && key->type != KEY_RSA_CERT) ||
1131 (key->type == KEY_RSA_CERT && (datafellows & SSH_BUG_SIGTYPE))) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001132 /* Filter base key signature alg against our configuration */
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001133 return match_list(sshkey_ssh_name(key),
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001134 options.pubkey_key_types, NULL);
djm@openbsd.org26074382018-03-03 03:01:50 +00001135 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001136
1137 /*
1138 * For RSA keys/certs, since these might have a different sig type:
1139 * find the first entry in PubkeyAcceptedKeyTypes of the right type
1140 * that also appears in the supported signature algorithms list from
1141 * the server.
1142 */
1143 oallowed = allowed = xstrdup(options.pubkey_key_types);
1144 while ((cp = strsep(&allowed, ",")) != NULL) {
1145 if (sshkey_type_from_name(cp) != key->type)
1146 continue;
djm@openbsd.orgb4d4eda2018-07-03 13:20:25 +00001147 tmp = match_list(sshkey_sigalg_by_name(cp), ssh->kex->server_sig_algs, NULL);
1148 if (tmp != NULL)
1149 alg = xstrdup(cp);
1150 free(tmp);
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001151 if (alg != NULL)
1152 break;
1153 }
1154 free(oallowed);
1155 return alg;
djm@openbsd.org26074382018-03-03 03:01:50 +00001156}
1157
Ben Lindstrombba81212001-06-25 05:01:22 +00001158static int
djm@openbsd.org141efe42015-01-14 20:05:27 +00001159identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001160 const u_char *data, size_t datalen, u_int compat, const char *alg)
Damien Miller280ecfb2003-05-14 13:46:00 +10001161{
djm@openbsd.org884416b2019-10-31 21:18:28 +00001162 struct sshkey *sign_key = NULL, *prv = NULL;
1163 int r = SSH_ERR_INTERNAL_ERROR;
djm@openbsd.orge44bb612019-11-12 22:36:44 +00001164 struct notifier_ctx *notifier = NULL;
1165 char *fp = NULL;
djm@openbsd.org884416b2019-10-31 21:18:28 +00001166
1167 *sigp = NULL;
1168 *lenp = 0;
Damien Miller280ecfb2003-05-14 13:46:00 +10001169
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001170 /* The agent supports this key. */
djm@openbsd.org26074382018-03-03 03:01:50 +00001171 if (id->key != NULL && id->agent_fd != -1) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001172 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
1173 data, datalen, alg, compat);
1174 }
1175
1176 /*
1177 * We have already loaded the private key or the private key is
1178 * stored in external hardware.
1179 */
1180 if (id->key != NULL &&
1181 (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))) {
djm@openbsd.org884416b2019-10-31 21:18:28 +00001182 sign_key = id->key;
1183 } else {
1184 /* Load the private key from the file. */
1185 if ((prv = load_identity_file(id)) == NULL)
1186 return SSH_ERR_KEY_NOT_FOUND;
1187 if (id->key != NULL && !sshkey_equal_public(prv, id->key)) {
1188 error("%s: private key %s contents do not match public",
1189 __func__, id->filename);
1190 r = SSH_ERR_KEY_NOT_FOUND;
1191 goto out;
1192 }
1193 sign_key = prv;
djm@openbsd.orge44bb612019-11-12 22:36:44 +00001194 if (sshkey_is_sk(sign_key) &&
1195 (sign_key->sk_flags & SSH_SK_USER_PRESENCE_REQD)) {
1196 /* XXX match batch mode should just skip these keys? */
1197 if ((fp = sshkey_fingerprint(sign_key,
1198 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1199 fatal("%s: sshkey_fingerprint", __func__);
1200 notifier = notify_start(options.batch_mode,
1201 "Confirm user presence for key %s %s",
1202 sshkey_type(sign_key), fp);
1203 free(fp);
1204 }
djm@openbsd.org26074382018-03-03 03:01:50 +00001205 }
djm@openbsd.org9a14c642019-10-31 21:23:19 +00001206 if ((r = sshkey_sign(sign_key, sigp, lenp, data, datalen,
1207 alg, options.sk_provider, compat)) != 0) {
djm@openbsd.org884416b2019-10-31 21:18:28 +00001208 debug("%s: sshkey_sign: %s", __func__, ssh_err(r));
1209 goto out;
djm@openbsd.orgc4972d02017-08-11 04:47:12 +00001210 }
djm@openbsd.org884416b2019-10-31 21:18:28 +00001211 /*
1212 * PKCS#11 tokens may not support all signature algorithms,
1213 * so check what we get back.
1214 */
1215 if ((r = sshkey_check_sigtype(*sigp, *lenp, alg)) != 0) {
1216 debug("%s: sshkey_check_sigtype: %s", __func__, ssh_err(r));
1217 goto out;
1218 }
1219 /* success */
1220 r = 0;
1221 out:
djm@openbsd.org49dc9fa2019-11-15 02:37:24 +00001222 notify_complete(notifier);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001223 sshkey_free(prv);
djm@openbsd.org26074382018-03-03 03:01:50 +00001224 return r;
Damien Miller280ecfb2003-05-14 13:46:00 +10001225}
1226
1227static int
djm@openbsd.org1e245522017-03-11 23:40:26 +00001228id_filename_matches(Identity *id, Identity *private_id)
1229{
1230 const char *suffixes[] = { ".pub", "-cert.pub", NULL };
1231 size_t len = strlen(id->filename), plen = strlen(private_id->filename);
1232 size_t i, slen;
1233
1234 if (strcmp(id->filename, private_id->filename) == 0)
1235 return 1;
1236 for (i = 0; suffixes[i]; i++) {
1237 slen = strlen(suffixes[i]);
1238 if (len > slen && plen == len - slen &&
1239 strcmp(id->filename + (len - slen), suffixes[i]) == 0 &&
1240 memcmp(id->filename, private_id->filename, plen) == 0)
1241 return 1;
1242 }
1243 return 0;
1244}
1245
1246static int
djm@openbsd.org64c95982019-01-19 21:40:21 +00001247sign_and_send_pubkey(struct ssh *ssh, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +10001248{
djm@openbsd.org64c95982019-01-19 21:40:21 +00001249 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001250 struct sshbuf *b = NULL;
1251 Identity *private_id, *sign_id = NULL;
1252 u_char *signature = NULL;
1253 size_t slen = 0, skip = 0;
1254 int r, fallback_sigtype, sent = 0;
1255 char *alg = NULL, *fp = NULL;
1256 const char *loc = "";
Damien Millereba71ba2000-04-29 23:57:08 +10001257
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001258 if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1259 SSH_FP_DEFAULT)) == NULL)
1260 return 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001261
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001262 debug3("%s: %s %s", __func__, sshkey_type(id->key), fp);
Damien Millereba71ba2000-04-29 23:57:08 +10001263
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001264 /*
1265 * If the key is an certificate, try to find a matching private key
1266 * and use it to complete the signature.
djm@openbsd.org9ee692f2016-04-28 14:30:21 +00001267 * If no such private key exists, fall back to trying the certificate
1268 * key itself in case it has a private half already loaded.
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001269 * This will try to set sign_id to the private key that will perform
1270 * the signature.
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001271 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001272 if (sshkey_is_cert(id->key)) {
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001273 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1274 if (sshkey_equal_public(id->key, private_id->key) &&
1275 id->key->type != private_id->key->type) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001276 sign_id = private_id;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001277 break;
1278 }
1279 }
djm@openbsd.org1e245522017-03-11 23:40:26 +00001280 /*
1281 * Exact key matches are preferred, but also allow
1282 * filename matches for non-PKCS#11/agent keys that
1283 * didn't load public keys. This supports the case
1284 * of keeping just a private key file and public
1285 * certificate on disk.
1286 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001287 if (sign_id == NULL &&
1288 !id->isprivate && id->agent_fd == -1 &&
djm@openbsd.org1e245522017-03-11 23:40:26 +00001289 (id->key->flags & SSHKEY_FLAG_EXT) == 0) {
1290 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1291 if (private_id->key == NULL &&
1292 id_filename_matches(id, private_id)) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001293 sign_id = private_id;
djm@openbsd.org1e245522017-03-11 23:40:26 +00001294 break;
1295 }
1296 }
1297 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001298 if (sign_id != NULL) {
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001299 debug2("%s: using private key \"%s\"%s for "
1300 "certificate", __func__, id->filename,
1301 id->agent_fd != -1 ? " from agent" : "");
1302 } else {
djm@openbsd.orgc38905b2016-03-14 16:20:54 +00001303 debug("%s: no separate private key for certificate "
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001304 "\"%s\"", __func__, id->filename);
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001305 }
1306 }
1307
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001308 /*
1309 * If the above didn't select another identity to do the signing
1310 * then default to the one we started with.
1311 */
1312 if (sign_id == NULL)
1313 sign_id = id;
1314
1315 /* assemble and sign data */
1316 for (fallback_sigtype = 0; fallback_sigtype <= 1; fallback_sigtype++) {
1317 free(alg);
1318 slen = 0;
1319 signature = NULL;
1320 if ((alg = key_sig_algorithm(fallback_sigtype ? NULL : ssh,
1321 id->key)) == NULL) {
1322 error("%s: no mutual signature supported", __func__);
1323 goto out;
1324 }
djm@openbsd.org881aded2020-01-21 05:56:27 +00001325 debug3("%s: signing using %s %s", __func__, alg, fp);
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001326
1327 sshbuf_free(b);
1328 if ((b = sshbuf_new()) == NULL)
1329 fatal("%s: sshbuf_new failed", __func__);
1330 if (datafellows & SSH_OLD_SESSIONID) {
1331 if ((r = sshbuf_put(b, session_id2,
1332 session_id2_len)) != 0) {
1333 fatal("%s: sshbuf_put: %s",
1334 __func__, ssh_err(r));
1335 }
1336 } else {
1337 if ((r = sshbuf_put_string(b, session_id2,
1338 session_id2_len)) != 0) {
1339 fatal("%s: sshbuf_put_string: %s",
1340 __func__, ssh_err(r));
1341 }
1342 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001343 skip = sshbuf_len(b);
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001344 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1345 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1346 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1347 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1348 (r = sshbuf_put_u8(b, 1)) != 0 ||
1349 (r = sshbuf_put_cstring(b, alg)) != 0 ||
1350 (r = sshkey_puts(id->key, b)) != 0) {
1351 fatal("%s: assemble signed data: %s",
1352 __func__, ssh_err(r));
1353 }
1354
1355 /* generate signature */
1356 r = identity_sign(sign_id, &signature, &slen,
1357 sshbuf_ptr(b), sshbuf_len(b), datafellows, alg);
1358 if (r == 0)
1359 break;
1360 else if (r == SSH_ERR_KEY_NOT_FOUND)
1361 goto out; /* soft failure */
1362 else if (r == SSH_ERR_SIGN_ALG_UNSUPPORTED &&
1363 !fallback_sigtype) {
1364 if (sign_id->agent_fd != -1)
1365 loc = "agent ";
1366 else if ((sign_id->key->flags & SSHKEY_FLAG_EXT) != 0)
1367 loc = "token ";
1368 logit("%skey %s %s returned incorrect signature type",
1369 loc, sshkey_type(id->key), fp);
1370 continue;
1371 }
djm@openbsd.org881aded2020-01-21 05:56:27 +00001372 error("%s: signing failed for %s \"%s\"%s: %s", __func__,
1373 sshkey_type(sign_id->key), sign_id->filename,
1374 id->agent_fd != -1 ? " from agent" : "", ssh_err(r));
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001375 goto out;
Damien Millerad833b32000-08-23 10:46:23 +10001376 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001377 if (slen == 0 || signature == NULL) /* shouldn't happen */
1378 fatal("%s: no signature", __func__);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001379
Damien Millereba71ba2000-04-29 23:57:08 +10001380 /* append signature */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001381 if ((r = sshbuf_put_string(b, signature, slen)) != 0)
1382 fatal("%s: append signature: %s", __func__, ssh_err(r));
Damien Millereba71ba2000-04-29 23:57:08 +10001383
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001384#ifdef DEBUG_PK
1385 sshbuf_dump(b, stderr);
1386#endif
Damien Millereba71ba2000-04-29 23:57:08 +10001387 /* skip session id and packet type */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001388 if ((r = sshbuf_consume(b, skip + 1)) != 0)
1389 fatal("%s: consume: %s", __func__, ssh_err(r));
Damien Millereba71ba2000-04-29 23:57:08 +10001390
1391 /* put remaining data from buffer into packet */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001392 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1393 (r = sshpkt_putb(ssh, b)) != 0 ||
1394 (r = sshpkt_send(ssh)) != 0)
1395 fatal("%s: enqueue request: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001396
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001397 /* success */
1398 sent = 1;
1399
1400 out:
1401 free(fp);
1402 free(alg);
1403 sshbuf_free(b);
1404 freezero(signature, slen);
1405 return sent;
Damien Miller994cf142000-07-21 10:19:44 +10001406}
1407
Ben Lindstrombba81212001-06-25 05:01:22 +00001408static int
djm@openbsd.org64c95982019-01-19 21:40:21 +00001409send_pubkey_test(struct ssh *ssh, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001410{
djm@openbsd.org64c95982019-01-19 21:40:21 +00001411 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001412 u_char *blob = NULL;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001413 char *alg = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001414 size_t bloblen;
1415 u_int have_sig = 0;
1416 int sent = 0, r;
Damien Miller994cf142000-07-21 10:19:44 +10001417
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001418 if ((alg = key_sig_algorithm(ssh, id->key)) == NULL) {
1419 debug("%s: no mutual signature algorithm", __func__);
1420 goto out;
1421 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001422
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001423 if ((r = sshkey_to_blob(id->key, &blob, &bloblen)) != 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001424 /* we cannot handle this key */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001425 debug3("%s: cannot handle key", __func__);
1426 goto out;
Damien Miller994cf142000-07-21 10:19:44 +10001427 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001428 /* register callback for USERAUTH_PK_OK message */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001429 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001430
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001431 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1432 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1433 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1434 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1435 (r = sshpkt_put_u8(ssh, have_sig)) != 0 ||
1436 (r = sshpkt_put_cstring(ssh, alg)) != 0 ||
1437 (r = sshpkt_put_string(ssh, blob, bloblen)) != 0 ||
1438 (r = sshpkt_send(ssh)) != 0)
1439 fatal("%s: %s", __func__, ssh_err(r));
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001440 sent = 1;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001441
1442 out:
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001443 free(alg);
1444 free(blob);
1445 return sent;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001446}
1447
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001448static struct sshkey *
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001449load_identity_file(Identity *id)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001450{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001451 struct sshkey *private = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001452 char prompt[300], *passphrase, *comment;
dtucker@openbsd.org6b39a7b2019-08-05 11:50:33 +00001453 int r, quit = 0, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001454 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001455
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001456 if (stat(id->filename, &st) == -1) {
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001457 (id->userprovided ? logit : debug3)("no such identity: %s: %s",
1458 id->filename, strerror(errno));
Ben Lindstrom329782e2001-03-10 17:08:59 +00001459 return NULL;
1460 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001461 snprintf(prompt, sizeof prompt,
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001462 "Enter passphrase for key '%.100s': ", id->filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001463 for (i = 0; i <= options.number_of_password_prompts; i++) {
1464 if (i == 0)
1465 passphrase = "";
1466 else {
Damien Miller62cee002000-09-23 17:15:56 +11001467 passphrase = read_passphrase(prompt, 0);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001468 if (*passphrase == '\0') {
Damien Miller62cee002000-09-23 17:15:56 +11001469 debug2("no passphrase given, try next key");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001470 free(passphrase);
1471 break;
Damien Miller62cee002000-09-23 17:15:56 +11001472 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001473 }
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001474 switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
dtucker@openbsd.org6b39a7b2019-08-05 11:50:33 +00001475 passphrase, &private, &comment))) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001476 case 0:
1477 break;
1478 case SSH_ERR_KEY_WRONG_PASSPHRASE:
1479 if (options.batch_mode) {
1480 quit = 1;
1481 break;
1482 }
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001483 if (i != 0)
1484 debug2("bad passphrase given, try again...");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001485 break;
1486 case SSH_ERR_SYSTEM_ERROR:
1487 if (errno == ENOENT) {
1488 debug2("Load key \"%s\": %s",
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001489 id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001490 quit = 1;
1491 break;
1492 }
1493 /* FALLTHROUGH */
1494 default:
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001495 error("Load key \"%s\": %s", id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001496 quit = 1;
1497 break;
1498 }
markus@openbsd.org2c557442019-11-12 19:33:08 +00001499 if (private != NULL && sshkey_is_sk(private) &&
djm@openbsd.org884416b2019-10-31 21:18:28 +00001500 options.sk_provider == NULL) {
naddy@openbsd.orga47f6a62020-02-06 22:30:54 +00001501 debug("key \"%s\" is an authenticator-hosted key, "
1502 "but no provider specified", id->filename);
djm@openbsd.org884416b2019-10-31 21:18:28 +00001503 sshkey_free(private);
1504 private = NULL;
1505 quit = 1;
1506 }
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001507 if (!quit && private != NULL && id->agent_fd == -1 &&
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001508 !(id->key && id->isprivate))
1509 maybe_add_key_to_agent(id->filename, private, comment,
1510 passphrase);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001511 if (i > 0)
1512 freezero(passphrase, strlen(passphrase));
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001513 free(comment);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001514 if (private != NULL || quit)
1515 break;
Damien Miller994cf142000-07-21 10:19:44 +10001516 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001517 return private;
1518}
1519
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001520static int
1521key_type_allowed_by_config(struct sshkey *key)
1522{
1523 if (match_pattern_list(sshkey_ssh_name(key),
1524 options.pubkey_key_types, 0) == 1)
1525 return 1;
1526
1527 /* RSA keys/certs might be allowed by alternate signature types */
1528 switch (key->type) {
1529 case KEY_RSA:
1530 if (match_pattern_list("rsa-sha2-512",
1531 options.pubkey_key_types, 0) == 1)
1532 return 1;
1533 if (match_pattern_list("rsa-sha2-256",
1534 options.pubkey_key_types, 0) == 1)
1535 return 1;
1536 break;
1537 case KEY_RSA_CERT:
1538 if (match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
1539 options.pubkey_key_types, 0) == 1)
1540 return 1;
1541 if (match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
1542 options.pubkey_key_types, 0) == 1)
1543 return 1;
1544 break;
1545 }
1546 return 0;
1547}
1548
1549
Damien Miller280ecfb2003-05-14 13:46:00 +10001550/*
1551 * try keys in the following order:
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001552 * 1. certificates listed in the config file
1553 * 2. other input certificates
1554 * 3. agent keys that are found in the config file
1555 * 4. other agent keys
1556 * 5. keys that are only listed in the config file
Damien Miller280ecfb2003-05-14 13:46:00 +10001557 */
1558static void
1559pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001560{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001561 struct identity *id, *id2, *tmp;
1562 struct idlist agent, files, *preferred;
1563 struct sshkey *key;
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001564 int agent_fd = -1, i, r, found;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001565 size_t j;
1566 struct ssh_identitylist *idlist;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001567 char *ident;
Damien Millerad833b32000-08-23 10:46:23 +10001568
Damien Miller280ecfb2003-05-14 13:46:00 +10001569 TAILQ_INIT(&agent); /* keys from the agent */
1570 TAILQ_INIT(&files); /* keys from the config file */
1571 preferred = &authctxt->keys;
1572 TAILQ_INIT(preferred); /* preferred order of keys */
1573
Damien Millercb6b68b2012-12-03 09:49:52 +11001574 /* list of keys stored in the filesystem and PKCS#11 */
Damien Miller280ecfb2003-05-14 13:46:00 +10001575 for (i = 0; i < options.num_identity_files; i++) {
1576 key = options.identity_keys[i];
djm@openbsd.org884416b2019-10-31 21:18:28 +00001577 if (key && key->cert &&
1578 key->cert->type != SSH2_CERT_TYPE_USER) {
1579 debug("%s: ignoring certificate %s: not a user "
1580 "certificate", __func__,
1581 options.identity_files[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +11001582 continue;
djm@openbsd.org884416b2019-10-31 21:18:28 +00001583 }
markus@openbsd.org2c557442019-11-12 19:33:08 +00001584 if (key && sshkey_is_sk(key) && options.sk_provider == NULL) {
naddy@openbsd.orga47f6a62020-02-06 22:30:54 +00001585 debug("%s: ignoring authenticator-hosted key %s as no "
djm@openbsd.org884416b2019-10-31 21:18:28 +00001586 "SecurityKeyProvider has been specified",
1587 __func__, options.identity_files[i]);
1588 continue;
1589 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001590 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001591 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001592 id->agent_fd = -1;
Damien Miller280ecfb2003-05-14 13:46:00 +10001593 id->key = key;
1594 id->filename = xstrdup(options.identity_files[i]);
Darren Tucker19104782013-04-05 11:13:08 +11001595 id->userprovided = options.identity_file_userprovided[i];
Damien Miller280ecfb2003-05-14 13:46:00 +10001596 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001597 }
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001598 /* list of certificates specified by user */
1599 for (i = 0; i < options.num_certificate_files; i++) {
1600 key = options.certificates[i];
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001601 if (!sshkey_is_cert(key) || key->cert == NULL ||
djm@openbsd.org884416b2019-10-31 21:18:28 +00001602 key->cert->type != SSH2_CERT_TYPE_USER) {
1603 debug("%s: ignoring certificate %s: not a user "
1604 "certificate", __func__,
1605 options.identity_files[i]);
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001606 continue;
djm@openbsd.org884416b2019-10-31 21:18:28 +00001607 }
markus@openbsd.org2c557442019-11-12 19:33:08 +00001608 if (key && sshkey_is_sk(key) && options.sk_provider == NULL) {
naddy@openbsd.orga47f6a62020-02-06 22:30:54 +00001609 debug("%s: ignoring authenticator-hosted key "
1610 "certificate %s as no "
djm@openbsd.org884416b2019-10-31 21:18:28 +00001611 "SecurityKeyProvider has been specified",
1612 __func__, options.identity_files[i]);
1613 continue;
1614 }
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001615 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001616 id->agent_fd = -1;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001617 id->key = key;
1618 id->filename = xstrdup(options.certificate_files[i]);
1619 id->userprovided = options.certificate_file_userprovided[i];
1620 TAILQ_INSERT_TAIL(preferred, id, next);
1621 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001622 /* list of keys supported by the agent */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001623 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1624 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1625 debug("%s: ssh_get_authentication_socket: %s",
1626 __func__, ssh_err(r));
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +00001627 } else if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001628 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1629 debug("%s: ssh_fetch_identitylist: %s",
1630 __func__, ssh_err(r));
markus@openbsd.orgfc77ccd2016-01-14 22:56:56 +00001631 close(agent_fd);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001632 } else {
1633 for (j = 0; j < idlist->nkeys; j++) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001634 found = 0;
1635 TAILQ_FOREACH(id, &files, next) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001636 /*
1637 * agent keys from the config file are
1638 * preferred
1639 */
1640 if (sshkey_equal(idlist->keys[j], id->key)) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001641 TAILQ_REMOVE(&files, id, next);
1642 TAILQ_INSERT_TAIL(preferred, id, next);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001643 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001644 found = 1;
1645 break;
1646 }
1647 }
Damien Millerbd394c32004-03-08 23:12:36 +11001648 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001649 id = xcalloc(1, sizeof(*id));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001650 /* XXX "steals" key/comment from idlist */
1651 id->key = idlist->keys[j];
1652 id->filename = idlist->comments[j];
1653 idlist->keys[j] = NULL;
1654 idlist->comments[j] = NULL;
1655 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001656 TAILQ_INSERT_TAIL(&agent, id, next);
1657 }
1658 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001659 ssh_free_identitylist(idlist);
Damien Miller280ecfb2003-05-14 13:46:00 +10001660 /* append remaining agent keys */
1661 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1662 TAILQ_REMOVE(&agent, id, next);
1663 TAILQ_INSERT_TAIL(preferred, id, next);
1664 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001665 authctxt->agent_fd = agent_fd;
Damien Miller62cee002000-09-23 17:15:56 +11001666 }
djm@openbsd.org82f24c32016-05-23 23:30:50 +00001667 /* Prefer PKCS11 keys that are explicitly listed */
1668 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1669 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
1670 continue;
1671 found = 0;
1672 TAILQ_FOREACH(id2, &files, next) {
1673 if (id2->key == NULL ||
1674 (id2->key->flags & SSHKEY_FLAG_EXT) == 0)
1675 continue;
1676 if (sshkey_equal(id->key, id2->key)) {
1677 TAILQ_REMOVE(&files, id, next);
1678 TAILQ_INSERT_TAIL(preferred, id, next);
1679 found = 1;
1680 break;
1681 }
1682 }
1683 /* If IdentitiesOnly set and key not found then don't use it */
1684 if (!found && options.identities_only) {
1685 TAILQ_REMOVE(&files, id, next);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001686 freezero(id, sizeof(*id));
djm@openbsd.org82f24c32016-05-23 23:30:50 +00001687 }
1688 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001689 /* append remaining keys from the config file */
1690 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1691 TAILQ_REMOVE(&files, id, next);
1692 TAILQ_INSERT_TAIL(preferred, id, next);
1693 }
djm@openbsd.orge679c092015-10-13 16:15:21 +00001694 /* finally, filter by PubkeyAcceptedKeyTypes */
1695 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
djm@openbsd.orgd78b75d2018-07-03 13:07:58 +00001696 if (id->key != NULL && !key_type_allowed_by_config(id->key)) {
djm@openbsd.orge679c092015-10-13 16:15:21 +00001697 debug("Skipping %s key %s - "
1698 "not in PubkeyAcceptedKeyTypes",
1699 sshkey_ssh_name(id->key), id->filename);
1700 TAILQ_REMOVE(preferred, id, next);
1701 sshkey_free(id->key);
1702 free(id->filename);
1703 memset(id, 0, sizeof(*id));
1704 continue;
1705 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001706 }
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001707 /* List the keys we plan on using */
1708 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1709 ident = format_identity(id);
1710 debug("Will attempt key: %s", ident);
1711 free(ident);
1712 }
1713 debug2("%s: done", __func__);
Damien Miller280ecfb2003-05-14 13:46:00 +10001714}
1715
1716static void
djm@openbsd.org38e83e42019-02-12 23:53:10 +00001717pubkey_cleanup(struct ssh *ssh)
Damien Miller280ecfb2003-05-14 13:46:00 +10001718{
djm@openbsd.org5c68ea82019-02-11 09:44:42 +00001719 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
Damien Miller280ecfb2003-05-14 13:46:00 +10001720 Identity *id;
1721
djm@openbsd.orgaaed6352018-11-16 02:46:20 +00001722 if (authctxt->agent_fd != -1) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001723 ssh_close_authentication_socket(authctxt->agent_fd);
djm@openbsd.orgaaed6352018-11-16 02:46:20 +00001724 authctxt->agent_fd = -1;
1725 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001726 for (id = TAILQ_FIRST(&authctxt->keys); id;
1727 id = TAILQ_FIRST(&authctxt->keys)) {
1728 TAILQ_REMOVE(&authctxt->keys, id, next);
mmcc@openbsd.org89540b62015-12-11 02:31:47 +00001729 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +10001730 free(id->filename);
1731 free(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001732 }
Damien Millereba71ba2000-04-29 23:57:08 +10001733}
1734
djm@openbsd.orgb9844a42016-12-04 23:54:02 +00001735static void
1736pubkey_reset(Authctxt *authctxt)
1737{
1738 Identity *id;
1739
1740 TAILQ_FOREACH(id, &authctxt->keys, next)
1741 id->tried = 0;
1742}
1743
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001744static int
1745try_identity(Identity *id)
1746{
1747 if (!id->key)
1748 return (0);
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001749 if (sshkey_type_plain(id->key->type) == KEY_RSA &&
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001750 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1751 debug("Skipped %s key %s for RSA/MD5 server",
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001752 sshkey_type(id->key), id->filename);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001753 return (0);
1754 }
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001755 return 1;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001756}
1757
djm@openbsd.org5c68ea82019-02-11 09:44:42 +00001758static int
djm@openbsd.org64c95982019-01-19 21:40:21 +00001759userauth_pubkey(struct ssh *ssh)
Damien Millereba71ba2000-04-29 23:57:08 +10001760{
djm@openbsd.org64c95982019-01-19 21:40:21 +00001761 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
Damien Miller280ecfb2003-05-14 13:46:00 +10001762 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001763 int sent = 0;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001764 char *ident;
Damien Millereba71ba2000-04-29 23:57:08 +10001765
Damien Miller280ecfb2003-05-14 13:46:00 +10001766 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1767 if (id->tried++)
1768 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001769 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001770 TAILQ_REMOVE(&authctxt->keys, id, next);
1771 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1772 /*
1773 * send a test message if we have the public key. for
1774 * encrypted keys we cannot do this and have to load the
1775 * private key instead
1776 */
Damien Miller324541e2013-12-31 12:25:40 +11001777 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001778 if (try_identity(id)) {
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001779 ident = format_identity(id);
1780 debug("Offering public key: %s", ident);
1781 free(ident);
djm@openbsd.org64c95982019-01-19 21:40:21 +00001782 sent = send_pubkey_test(ssh, id);
Damien Miller324541e2013-12-31 12:25:40 +11001783 }
1784 } else {
Damien Miller280ecfb2003-05-14 13:46:00 +10001785 debug("Trying private key: %s", id->filename);
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001786 id->key = load_identity_file(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001787 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001788 if (try_identity(id)) {
1789 id->isprivate = 1;
djm@openbsd.org64c95982019-01-19 21:40:21 +00001790 sent = sign_and_send_pubkey(ssh, id);
Damien Miller324541e2013-12-31 12:25:40 +11001791 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001792 sshkey_free(id->key);
Damien Miller280ecfb2003-05-14 13:46:00 +10001793 id->key = NULL;
djm@openbsd.orgb9844a42016-12-04 23:54:02 +00001794 id->isprivate = 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001795 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001796 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001797 if (sent)
1798 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001799 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001800 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001801}
Damien Millereba71ba2000-04-29 23:57:08 +10001802
Damien Miller874d77b2000-10-14 16:23:11 +11001803/*
1804 * Send userauth request message specifying keyboard-interactive method.
1805 */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +00001806static int
djm@openbsd.org64c95982019-01-19 21:40:21 +00001807userauth_kbdint(struct ssh *ssh)
Damien Miller874d77b2000-10-14 16:23:11 +11001808{
djm@openbsd.org64c95982019-01-19 21:40:21 +00001809 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001810 int r;
Damien Miller874d77b2000-10-14 16:23:11 +11001811
djm@openbsd.org4a526942019-01-04 03:27:50 +00001812 if (authctxt->attempt_kbdint++ >= options.number_of_password_prompts)
Damien Miller874d77b2000-10-14 16:23:11 +11001813 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001814 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
djm@openbsd.org4a526942019-01-04 03:27:50 +00001815 if (authctxt->attempt_kbdint > 1 && !authctxt->info_req_seen) {
Ben Lindstrom7d199962001-09-12 18:29:00 +00001816 debug3("userauth_kbdint: disable: no info_req_seen");
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001817 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
Ben Lindstrom7d199962001-09-12 18:29:00 +00001818 return 0;
1819 }
Damien Miller874d77b2000-10-14 16:23:11 +11001820
1821 debug2("userauth_kbdint");
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001822 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1823 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1824 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1825 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1826 (r = sshpkt_put_cstring(ssh, "")) != 0 || /* lang */
1827 (r = sshpkt_put_cstring(ssh, options.kbd_interactive_devices ?
1828 options.kbd_interactive_devices : "")) != 0 ||
1829 (r = sshpkt_send(ssh)) != 0)
1830 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +11001831
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001832 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
Damien Miller874d77b2000-10-14 16:23:11 +11001833 return 1;
1834}
1835
1836/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001837 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001838 */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +00001839static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001840input_userauth_info_req(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller874d77b2000-10-14 16:23:11 +11001841{
markus@openbsd.org94583be2017-05-30 14:19:15 +00001842 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001843 char *name = NULL, *inst = NULL, *lang = NULL, *prompt = NULL;
1844 char *response = NULL;
1845 u_char echo = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001846 u_int num_prompts, i;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001847 int r;
Damien Miller874d77b2000-10-14 16:23:11 +11001848
1849 debug2("input_userauth_info_req");
1850
1851 if (authctxt == NULL)
1852 fatal("input_userauth_info_req: no authentication context");
1853
Ben Lindstrom7d199962001-09-12 18:29:00 +00001854 authctxt->info_req_seen = 1;
1855
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001856 if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 ||
1857 (r = sshpkt_get_cstring(ssh, &inst, NULL)) != 0 ||
1858 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1859 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +11001860 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001861 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001862 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001863 logit("%s", inst);
Damien Miller874d77b2000-10-14 16:23:11 +11001864
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001865 if ((r = sshpkt_get_u32(ssh, &num_prompts)) != 0)
1866 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +11001867 /*
1868 * Begin to build info response packet based on prompts requested.
1869 * We commit to providing the correct number of responses, so if
1870 * further on we run into a problem that prevents this, we have to
1871 * be sure and clean this up and send a correct error response.
1872 */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001873 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE)) != 0 ||
1874 (r = sshpkt_put_u32(ssh, num_prompts)) != 0)
1875 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +11001876
Ben Lindstrom551ea372001-06-05 18:56:16 +00001877 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001878 for (i = 0; i < num_prompts; i++) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001879 if ((r = sshpkt_get_cstring(ssh, &prompt, NULL)) != 0 ||
1880 (r = sshpkt_get_u8(ssh, &echo)) != 0)
1881 goto out;
Ben Lindstrom949974b2001-06-25 05:20:31 +00001882 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001883 if ((r = sshpkt_put_cstring(ssh, response)) != 0)
1884 goto out;
1885 freezero(response, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +10001886 free(prompt);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001887 response = prompt = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001888 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001889 /* done with parsing incoming message. */
1890 if ((r = sshpkt_get_end(ssh)) != 0 ||
1891 (r = sshpkt_add_padding(ssh, 64)) != 0)
1892 goto out;
1893 r = sshpkt_send(ssh);
1894 out:
1895 if (response)
1896 freezero(response, strlen(response));
1897 free(prompt);
1898 free(name);
1899 free(inst);
1900 free(lang);
1901 return r;
Damien Miller874d77b2000-10-14 16:23:11 +11001902}
Damien Miller62cee002000-09-23 17:15:56 +11001903
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001904static int
djm@openbsd.orged1df722019-01-19 21:34:45 +00001905ssh_keysign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001906 const u_char *data, size_t datalen)
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001907{
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001908 struct sshbuf *b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001909 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001910 pid_t pid;
deraadt@openbsd.orga1d29cc2019-05-15 04:43:31 +00001911 int r, to[2], from[2], status;
djm@openbsd.org64c95982019-01-19 21:40:21 +00001912 int sock = ssh_packet_get_connection_in(ssh);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001913 u_char rversion = 0, version = 2;
1914 void (*osigchld)(int);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001915
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001916 *sigp = NULL;
1917 *lenp = 0;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001918
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001919 if (stat(_PATH_SSH_KEY_SIGN, &st) == -1) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001920 error("%s: not installed: %s", __func__, strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001921 return -1;
1922 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001923 if (fflush(stdout) != 0) {
1924 error("%s: fflush: %s", __func__, strerror(errno));
1925 return -1;
1926 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001927 if (pipe(to) == -1) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001928 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001929 return -1;
1930 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001931 if (pipe(from) == -1) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001932 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001933 return -1;
1934 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001935 if ((pid = fork()) == -1) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001936 error("%s: fork: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001937 return -1;
1938 }
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +00001939 osigchld = ssh_signal(SIGCHLD, SIG_DFL);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001940 if (pid == 0) {
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001941 close(from[0]);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001942 if (dup2(from[1], STDOUT_FILENO) == -1)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001943 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001944 close(to[1]);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001945 if (dup2(to[0], STDIN_FILENO) == -1)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001946 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001947 close(from[1]);
1948 close(to[0]);
deraadt@openbsd.orga1d29cc2019-05-15 04:43:31 +00001949
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001950 if (dup2(sock, STDERR_FILENO + 1) == -1)
deraadt@openbsd.orga1d29cc2019-05-15 04:43:31 +00001951 fatal("%s: dup2: %s", __func__, strerror(errno));
1952 sock = STDERR_FILENO + 1;
1953 fcntl(sock, F_SETFD, 0); /* keep the socket on exec */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001954 closefrom(sock + 1);
deraadt@openbsd.orga1d29cc2019-05-15 04:43:31 +00001955
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001956 debug3("%s: [child] pid=%ld, exec %s",
1957 __func__, (long)getpid(), _PATH_SSH_KEY_SIGN);
mmcc@openbsd.org94141b72015-12-11 00:20:04 +00001958 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001959 fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001960 strerror(errno));
1961 }
1962 close(from[1]);
1963 close(to[0]);
djm@openbsd.orgc586d2d2019-05-31 03:20:07 +00001964 sock = STDERR_FILENO + 1;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001965
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001966 if ((b = sshbuf_new()) == NULL)
1967 fatal("%s: sshbuf_new failed", __func__);
1968 /* send # of sock, data to be signed */
djm@openbsd.org326e2fa2017-01-30 00:32:28 +00001969 if ((r = sshbuf_put_u32(b, sock)) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001970 (r = sshbuf_put_string(b, data, datalen)) != 0)
1971 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1972 if (ssh_msg_send(to[1], version, b) == -1)
1973 fatal("%s: couldn't send request", __func__);
1974 sshbuf_reset(b);
1975 r = ssh_msg_recv(from[0], b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001976 close(from[0]);
1977 close(to[1]);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001978 if (r < 0) {
1979 error("%s: no reply", __func__);
1980 goto fail;
1981 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001982
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001983 errno = 0;
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001984 while (waitpid(pid, &status, 0) == -1) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001985 if (errno != EINTR) {
1986 error("%s: waitpid %ld: %s",
1987 __func__, (long)pid, strerror(errno));
1988 goto fail;
1989 }
1990 }
1991 if (!WIFEXITED(status)) {
1992 error("%s: exited abnormally", __func__);
1993 goto fail;
1994 }
1995 if (WEXITSTATUS(status) != 0) {
1996 error("%s: exited with status %d",
1997 __func__, WEXITSTATUS(status));
1998 goto fail;
1999 }
2000 if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
2001 error("%s: buffer error: %s", __func__, ssh_err(r));
2002 goto fail;
2003 }
2004 if (rversion != version) {
2005 error("%s: bad version", __func__);
2006 goto fail;
2007 }
2008 if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
2009 error("%s: buffer error: %s", __func__, ssh_err(r));
2010 fail:
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +00002011 ssh_signal(SIGCHLD, osigchld);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002012 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00002013 return -1;
2014 }
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +00002015 ssh_signal(SIGCHLD, osigchld);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002016 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00002017
Ben Lindstrom1bad2562002-06-06 19:57:33 +00002018 return 0;
2019}
2020
djm@openbsd.org5c68ea82019-02-11 09:44:42 +00002021static int
djm@openbsd.org64c95982019-01-19 21:40:21 +00002022userauth_hostbased(struct ssh *ssh)
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002023{
djm@openbsd.org64c95982019-01-19 21:40:21 +00002024 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002025 struct sshkey *private = NULL;
2026 struct sshbuf *b = NULL;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002027 u_char *sig = NULL, *keyblob = NULL;
2028 char *fp = NULL, *chost = NULL, *lname = NULL;
2029 size_t siglen = 0, keylen = 0;
2030 int i, r, success = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002031
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002032 if (authctxt->ktypes == NULL) {
2033 authctxt->oktypes = xstrdup(options.hostbased_key_types);
2034 authctxt->ktypes = authctxt->oktypes;
2035 }
djm@openbsd.org1195f4c2015-01-08 10:14:08 +00002036
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002037 /*
2038 * Work through each listed type pattern in HostbasedKeyTypes,
2039 * trying each hostkey that matches the type in turn.
2040 */
2041 for (;;) {
2042 if (authctxt->active_ktype == NULL)
2043 authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
2044 if (authctxt->active_ktype == NULL ||
2045 *authctxt->active_ktype == '\0')
2046 break;
2047 debug3("%s: trying key type %s", __func__,
2048 authctxt->active_ktype);
2049
2050 /* check for a useful key */
2051 private = NULL;
2052 for (i = 0; i < authctxt->sensitive->nkeys; i++) {
2053 if (authctxt->sensitive->keys[i] == NULL ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002054 authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
2055 continue;
2056 if (match_pattern_list(
2057 sshkey_ssh_name(authctxt->sensitive->keys[i]),
djm@openbsd.orge661a862015-05-04 06:10:48 +00002058 authctxt->active_ktype, 0) != 1)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002059 continue;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002060 /* we take and free the key */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002061 private = authctxt->sensitive->keys[i];
2062 authctxt->sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002063 break;
2064 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002065 /* Found one */
2066 if (private != NULL)
2067 break;
2068 /* No more keys of this type; advance */
2069 authctxt->active_ktype = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002070 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002071 if (private == NULL) {
2072 free(authctxt->oktypes);
2073 authctxt->oktypes = authctxt->ktypes = NULL;
2074 authctxt->active_ktype = NULL;
Ben Lindstrom1d568f92002-12-23 02:44:36 +00002075 debug("No more client hostkeys for hostbased authentication.");
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002076 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002077 }
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00002078
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002079 if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
2080 SSH_FP_DEFAULT)) == NULL) {
2081 error("%s: sshkey_fingerprint failed", __func__);
2082 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002083 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002084 debug("%s: trying hostkey %s %s",
2085 __func__, sshkey_ssh_name(private), fp);
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00002086
Damien Miller91c18472001-11-12 11:02:03 +11002087 /* figure out a name for the client host */
djm@openbsd.org64c95982019-01-19 21:40:21 +00002088 lname = get_local_name(ssh_packet_get_connection_in(ssh));
2089 if (lname == NULL) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002090 error("%s: cannot get local ipaddr/name", __func__);
2091 goto out;
Damien Miller91c18472001-11-12 11:02:03 +11002092 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002093
2094 /* XXX sshbuf_put_stringf? */
2095 xasprintf(&chost, "%s.", lname);
2096 debug2("%s: chost %s", __func__, chost);
Damien Miller91c18472001-11-12 11:02:03 +11002097
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002098 /* construct data */
2099 if ((b = sshbuf_new()) == NULL) {
2100 error("%s: sshbuf_new failed", __func__);
2101 goto out;
2102 }
2103 if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
2104 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
2105 goto out;
2106 }
2107 if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
2108 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2109 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +00002110 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002111 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002112 (r = sshbuf_put_cstring(b, sshkey_ssh_name(private))) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002113 (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
2114 (r = sshbuf_put_cstring(b, chost)) != 0 ||
2115 (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
2116 error("%s: buffer error: %s", __func__, ssh_err(r));
2117 goto out;
2118 }
2119
2120#ifdef DEBUG_PK
2121 sshbuf_dump(b, stderr);
2122#endif
djm@openbsd.orged1df722019-01-19 21:34:45 +00002123 if ((r = ssh_keysign(ssh, private, &sig, &siglen,
2124 sshbuf_ptr(b), sshbuf_len(b))) != 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002125 error("sign using hostkey %s %s failed",
2126 sshkey_ssh_name(private), fp);
2127 goto out;
2128 }
2129 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2130 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
2131 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
2132 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002133 (r = sshpkt_put_cstring(ssh, sshkey_ssh_name(private))) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002134 (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
2135 (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
2136 (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
2137 (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
2138 (r = sshpkt_send(ssh)) != 0) {
2139 error("%s: packet error: %s", __func__, ssh_err(r));
2140 goto out;
2141 }
2142 success = 1;
2143
2144 out:
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002145 if (sig != NULL)
2146 freezero(sig, siglen);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002147 free(keyblob);
2148 free(lname);
2149 free(fp);
2150 free(chost);
2151 sshkey_free(private);
2152 sshbuf_free(b);
2153
2154 return success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002155}
2156
Damien Miller62cee002000-09-23 17:15:56 +11002157/* find auth method */
2158
Damien Miller62cee002000-09-23 17:15:56 +11002159/*
2160 * given auth method name, if configurable options permit this method fill
2161 * in auth_ident field and return true, otherwise return false.
2162 */
Ben Lindstrombba81212001-06-25 05:01:22 +00002163static int
Damien Miller62cee002000-09-23 17:15:56 +11002164authmethod_is_enabled(Authmethod *method)
2165{
2166 if (method == NULL)
2167 return 0;
2168 /* return false if options indicate this method is disabled */
2169 if (method->enabled == NULL || *method->enabled == 0)
2170 return 0;
2171 /* return false if batch mode is enabled but method needs interactive mode */
2172 if (method->batch_flag != NULL && *method->batch_flag != 0)
2173 return 0;
2174 return 1;
2175}
2176
Ben Lindstrombba81212001-06-25 05:01:22 +00002177static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11002178authmethod_lookup(const char *name)
2179{
2180 Authmethod *method = NULL;
2181 if (name != NULL)
2182 for (method = authmethods; method->name != NULL; method++)
2183 if (strcmp(name, method->name) == 0)
2184 return method;
2185 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
2186 return NULL;
2187}
2188
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002189/* XXX internal state */
2190static Authmethod *current = NULL;
2191static char *supported = NULL;
2192static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00002193
Damien Miller62cee002000-09-23 17:15:56 +11002194/*
2195 * Given the authentication method list sent by the server, return the
2196 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00002197 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00002198 */
Ben Lindstrombba81212001-06-25 05:01:22 +00002199static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11002200authmethod_get(char *authlist)
2201{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002202 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00002203 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00002204
Damien Miller62cee002000-09-23 17:15:56 +11002205 /* Use a suitable default if we're passed a nil list. */
2206 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002207 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11002208
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002209 if (supported == NULL || strcmp(authlist, supported) != 0) {
2210 debug3("start over, passed a different list %s", authlist);
Darren Tuckera627d422013-06-02 07:31:17 +10002211 free(supported);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002212 supported = xstrdup(authlist);
2213 preferred = options.preferred_authentications;
2214 debug3("preferred %s", preferred);
2215 current = NULL;
2216 } else if (current != NULL && authmethod_is_enabled(current))
2217 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10002218
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002219 for (;;) {
2220 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00002221 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002222 current = NULL;
2223 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11002224 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002225 preferred += next;
2226 debug3("authmethod_lookup %s", name);
2227 debug3("remaining preferred: %s", preferred);
2228 if ((current = authmethod_lookup(name)) != NULL &&
2229 authmethod_is_enabled(current)) {
2230 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00002231 debug("Next authentication method: %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002232 free(name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002233 return current;
2234 }
Darren Tuckere52a2602013-06-06 08:22:05 +10002235 free(name);
Damien Millereba71ba2000-04-29 23:57:08 +10002236 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002237}
Damien Miller62cee002000-09-23 17:15:56 +11002238
Ben Lindstrom79073822001-07-04 03:42:30 +00002239static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002240authmethods_get(void)
2241{
2242 Authmethod *method = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002243 struct sshbuf *b;
Damien Miller0e3b8722002-01-22 23:26:38 +11002244 char *list;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002245 int r;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002246
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002247 if ((b = sshbuf_new()) == NULL)
2248 fatal("%s: sshbuf_new failed", __func__);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002249 for (method = authmethods; method->name != NULL; method++) {
2250 if (authmethod_is_enabled(method)) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002251 if ((r = sshbuf_putf(b, "%s%s",
2252 sshbuf_len(b) ? "," : "", method->name)) != 0)
2253 fatal("%s: buffer error: %s",
2254 __func__, ssh_err(r));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002255 }
Damien Miller62cee002000-09-23 17:15:56 +11002256 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002257 if ((list = sshbuf_dup_string(b)) == NULL)
djm@openbsd.org1a31d022016-05-02 08:49:03 +00002258 fatal("%s: sshbuf_dup_string failed", __func__);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002259 sshbuf_free(b);
Damien Miller0e3b8722002-01-22 23:26:38 +11002260 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10002261}