blob: d658ed07b2132b8f0bfa61ed91c19005bc2c8e0e [file] [log] [blame]
djm@openbsd.org49dc9fa2019-11-15 02:37:24 +00001/* $OpenBSD: sshconnect2.c,v 1.314 2019/11/15 02:37:24 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
122 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
123 maxlen = strlen(avail) + 1;
124 first = xmalloc(maxlen);
125 last = xmalloc(maxlen);
126 *first = *last = '\0';
127
128#define ALG_APPEND(to, from) \
129 do { \
130 if (*to != '\0') \
131 strlcat(to, ",", maxlen); \
132 strlcat(to, from, maxlen); \
133 } while (0)
134
135 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
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;
markus@openbsd.org57e783c2015-01-20 20:16:21 +0000164 int r;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000165
166 xxx_host = host;
167 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100168
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000169 if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
170 fatal("%s: kex_names_cat", __func__);
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000171 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(s);
Damien Millera0ff4662001-03-30 10:49:35 +1000172 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000173 compat_cipher_proposal(options.ciphers);
Damien Millera0ff4662001-03-30 10:49:35 +1000174 myproposal[PROPOSAL_ENC_ALGS_STOC] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000175 compat_cipher_proposal(options.ciphers);
dtucker@openbsd.org8c02e362016-05-24 04:43:45 +0000176 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
177 myproposal[PROPOSAL_COMP_ALGS_STOC] = options.compression ?
sf@openbsd.org168b46f2018-07-09 13:37:10 +0000178 "zlib@openssh.com,zlib,none" : "none,zlib@openssh.com,zlib";
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000179 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
180 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
181 if (options.hostkeyalgorithms != NULL) {
djm@openbsd.org312d2f22018-07-04 13:49:31 +0000182 all_key = sshkey_alg_list(0, 0, 1, ',');
183 if (kex_assemble_names(&options.hostkeyalgorithms,
184 KEX_DEFAULT_PK_ALG, all_key) != 0)
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000185 fatal("%s: kex_assemble_namelist", __func__);
djm@openbsd.org312d2f22018-07-04 13:49:31 +0000186 free(all_key);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100187 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000188 compat_pkalg_proposal(options.hostkeyalgorithms);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000189 } else {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000190 /* Enforce default */
191 options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG);
Damien Millerd925dcd2010-12-01 12:21:51 +1100192 /* Prefer algorithms that we already have keys for */
193 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100194 compat_pkalg_proposal(
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000195 order_hostkeyalgs(host, hostaddr, port));
Damien Millerd925dcd2010-12-01 12:21:51 +1100196 }
Damien Miller874d77b2000-10-14 16:23:11 +1100197
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000198 if (options.rekey_limit || options.rekey_interval)
djm@openbsd.orged1df722019-01-19 21:34:45 +0000199 ssh_packet_set_rekey_limits(ssh, options.rekey_limit,
dtucker@openbsd.orgc998bf02017-02-03 02:56:00 +0000200 options.rekey_interval);
Damien Millera5539d22003-04-09 20:50:06 +1000201
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000202 /* start key exchange */
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000203 if ((r = kex_setup(ssh, myproposal)) != 0)
markus@openbsd.org57e783c2015-01-20 20:16:21 +0000204 fatal("kex_setup: %s", ssh_err(r));
Damien Miller1f0311c2014-05-15 14:24:09 +1000205#ifdef WITH_OPENSSL
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000206 ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client;
207 ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client;
208 ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client;
209 ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client;
210 ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000211 ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
212 ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100213# ifdef OPENSSL_HAS_ECC
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000214 ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100215# endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000216#endif
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000217 ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client;
218 ssh->kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_client;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000219 ssh->kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100220
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000221 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &ssh->kex->done);
Damien Miller874d77b2000-10-14 16:23:11 +1100222
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000223 /* remove ext-info from the KEX proposals for rekeying */
224 myproposal[PROPOSAL_KEX_ALGS] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000225 compat_kex_proposal(options.kex_algorithms);
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000226 if ((r = kex_prop2buf(ssh->kex->my, myproposal)) != 0)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000227 fatal("kex_prop2buf: %s", ssh_err(r));
Darren Tucker36331b52010-01-08 16:50:41 +1100228
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000229 session_id2 = ssh->kex->session_id;
230 session_id2_len = ssh->kex->session_id_len;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000231
Damien Miller874d77b2000-10-14 16:23:11 +1100232#ifdef DEBUG_KEXDH
233 /* send 1st encrypted/maced/compressed message */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000234 if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 ||
235 (r = sshpkt_put_cstring(ssh, "markus")) != 0 ||
markus@openbsd.org394a8422018-07-11 18:55:11 +0000236 (r = sshpkt_send(ssh)) != 0 ||
237 (r = ssh_packet_write_wait(ssh)) != 0)
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000238 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100239#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000240}
Damien Millerb1715dc2000-05-30 13:44:51 +1000241
Damien Millereba71ba2000-04-29 23:57:08 +1000242/*
243 * Authenticate user
244 */
Damien Miller62cee002000-09-23 17:15:56 +1100245
djm@openbsd.org141efe42015-01-14 20:05:27 +0000246typedef struct cauthctxt Authctxt;
247typedef struct cauthmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000248typedef struct identity Identity;
249typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100250
Damien Miller280ecfb2003-05-14 13:46:00 +1000251struct identity {
252 TAILQ_ENTRY(identity) next;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000253 int agent_fd; /* >=0 if agent supports key */
254 struct sshkey *key; /* public/private key */
Damien Miller280ecfb2003-05-14 13:46:00 +1000255 char *filename; /* comment for agent-only keys */
256 int tried;
257 int isprivate; /* key points to the private key */
Damien Miller5ceddc32013-02-15 12:18:32 +1100258 int userprovided;
Damien Miller280ecfb2003-05-14 13:46:00 +1000259};
260TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100261
djm@openbsd.org141efe42015-01-14 20:05:27 +0000262struct cauthctxt {
Damien Miller62cee002000-09-23 17:15:56 +1100263 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000264 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100265 const char *host;
266 const char *service;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000267 struct cauthmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000268 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000269 char *authlist;
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000270#ifdef GSSAPI
271 /* gssapi */
272 gss_OID_set gss_supported_mechs;
273 u_int mech_tried;
274#endif
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000275 /* pubkey */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000276 struct idlist keys;
277 int agent_fd;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000278 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000279 Sensitive *sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000280 char *oktypes, *ktypes;
281 const char *active_ktype;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000282 /* kbd-interactive */
283 int info_req_seen;
djm@openbsd.org4a526942019-01-04 03:27:50 +0000284 int attempt_kbdint;
285 /* password */
286 int attempt_passwd;
Darren Tucker0efd1552003-08-26 11:49:55 +1000287 /* generic */
288 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100289};
djm@openbsd.org141efe42015-01-14 20:05:27 +0000290
291struct cauthmethod {
Damien Miller62cee002000-09-23 17:15:56 +1100292 char *name; /* string to compare against server's list */
djm@openbsd.org64c95982019-01-19 21:40:21 +0000293 int (*userauth)(struct ssh *ssh);
294 void (*cleanup)(struct ssh *ssh);
Damien Miller62cee002000-09-23 17:15:56 +1100295 int *enabled; /* flag in option struct that enables method */
296 int *batch_flag; /* flag in option struct that disables method */
297};
298
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000299static int input_userauth_service_accept(int, u_int32_t, struct ssh *);
300static int input_userauth_ext_info(int, u_int32_t, struct ssh *);
301static int input_userauth_success(int, u_int32_t, struct ssh *);
302static int input_userauth_failure(int, u_int32_t, struct ssh *);
303static int input_userauth_banner(int, u_int32_t, struct ssh *);
304static int input_userauth_error(int, u_int32_t, struct ssh *);
305static int input_userauth_info_req(int, u_int32_t, struct ssh *);
306static int input_userauth_pk_ok(int, u_int32_t, struct ssh *);
307static int input_userauth_passwd_changereq(int, u_int32_t, struct ssh *);
Damien Miller874d77b2000-10-14 16:23:11 +1100308
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000309static int userauth_none(struct ssh *);
310static int userauth_pubkey(struct ssh *);
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000311static int userauth_passwd(struct ssh *);
312static int userauth_kbdint(struct ssh *);
313static int userauth_hostbased(struct ssh *);
Damien Miller62cee002000-09-23 17:15:56 +1100314
Darren Tucker0efd1552003-08-26 11:49:55 +1000315#ifdef GSSAPI
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000316static int userauth_gssapi(struct ssh *);
317static void userauth_gssapi_cleanup(struct ssh *);
318static int input_gssapi_response(int type, u_int32_t, struct ssh *);
319static int input_gssapi_token(int type, u_int32_t, struct ssh *);
320static int input_gssapi_error(int, u_int32_t, struct ssh *);
321static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000322#endif
323
djm@openbsd.org64c95982019-01-19 21:40:21 +0000324void userauth(struct ssh *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000325
djm@openbsd.org38e83e42019-02-12 23:53:10 +0000326static void pubkey_cleanup(struct ssh *);
djm@openbsd.org64c95982019-01-19 21:40:21 +0000327static int sign_and_send_pubkey(struct ssh *ssh, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000328static void pubkey_prepare(Authctxt *);
djm@openbsd.orgb9844a42016-12-04 23:54:02 +0000329static void pubkey_reset(Authctxt *);
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000330static struct sshkey *load_identity_file(Identity *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000331
Ben Lindstrombba81212001-06-25 05:01:22 +0000332static Authmethod *authmethod_get(char *authlist);
333static Authmethod *authmethod_lookup(const char *name);
334static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100335
336Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000337#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100338 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000339 userauth_gssapi,
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000340 userauth_gssapi_cleanup,
Darren Tucker0efd1552003-08-26 11:49:55 +1000341 &options.gss_authentication,
342 NULL},
343#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000344 {"hostbased",
345 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100346 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000347 &options.hostbased_authentication,
348 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000349 {"publickey",
350 userauth_pubkey,
djm@openbsd.org38e83e42019-02-12 23:53:10 +0000351 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000352 &options.pubkey_authentication,
353 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100354 {"keyboard-interactive",
355 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100356 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100357 &options.kbd_interactive_authentication,
358 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000359 {"password",
360 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100361 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000362 &options.password_authentication,
363 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100364 {"none",
365 userauth_none,
366 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100367 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100368 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100369 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100370};
371
372void
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000373ssh_userauth2(struct ssh *ssh, const char *local_user,
374 const char *server_user, char *host, Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100375{
376 Authctxt authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000377 int r;
Damien Miller62cee002000-09-23 17:15:56 +1100378
Ben Lindstrom551ea372001-06-05 18:56:16 +0000379 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000380 options.kbd_interactive_authentication = 1;
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000381 if (options.preferred_authentications == NULL)
382 options.preferred_authentications = authmethods_get();
383
Damien Miller62cee002000-09-23 17:15:56 +1100384 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000385 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller62cee002000-09-23 17:15:56 +1100386 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000387 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100388 authctxt.host = host;
389 authctxt.service = "ssh-connection"; /* service name */
390 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100391 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000392 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000393 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000394 authctxt.sensitive = sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000395 authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000396 authctxt.info_req_seen = 0;
djm@openbsd.org4a526942019-01-04 03:27:50 +0000397 authctxt.attempt_kbdint = 0;
398 authctxt.attempt_passwd = 0;
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000399#if GSSAPI
400 authctxt.gss_supported_mechs = NULL;
401 authctxt.mech_tried = 0;
402#endif
djm@openbsd.orgf14564c2015-01-15 11:04:36 +0000403 authctxt.agent_fd = -1;
djm@openbsd.orgaaed6352018-11-16 02:46:20 +0000404 pubkey_prepare(&authctxt);
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000405 if (authctxt.method == NULL) {
406 fatal("%s: internal error: cannot send userauth none request",
407 __func__);
408 }
Damien Miller62cee002000-09-23 17:15:56 +1100409
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000410 if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
411 (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
412 (r = sshpkt_send(ssh)) != 0)
413 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller62cee002000-09-23 17:15:56 +1100414
markus@openbsd.org94583be2017-05-30 14:19:15 +0000415 ssh->authctxt = &authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000416 ssh_dispatch_init(ssh, &input_userauth_error);
417 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
418 ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
markus@openbsd.org92e9fe62017-05-31 07:00:13 +0000419 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt.success); /* loop until success */
djm@openbsd.org38e83e42019-02-12 23:53:10 +0000420 pubkey_cleanup(ssh);
markus@openbsd.org94583be2017-05-30 14:19:15 +0000421 ssh->authctxt = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100422
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000423 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
Damien Millerf842fcb2003-05-15 12:01:28 +1000424
dtucker@openbsd.orgf31c6542016-09-22 02:29:57 +0000425 if (!authctxt.success)
426 fatal("Authentication failed.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000427 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100428}
Damien Millerf842fcb2003-05-15 12:01:28 +1000429
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000430/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000431static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000432input_userauth_service_accept(int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000433{
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000434 int r;
435
436 if (ssh_packet_remaining(ssh) > 0) {
437 char *reply;
438
439 if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
440 goto out;
441 debug2("service_accept: %s", reply);
442 free(reply);
443 } else {
444 debug2("buggy server: service_accept w/o service");
445 }
446 if ((r = sshpkt_get_end(ssh)) != 0)
447 goto out;
448 debug("SSH2_MSG_SERVICE_ACCEPT received");
449
450 /* initial userauth request */
djm@openbsd.org64c95982019-01-19 21:40:21 +0000451 userauth_none(ssh);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000452
453 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error);
454 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
455 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
456 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
457 r = 0;
458 out:
459 return r;
460}
461
462/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000463static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000464input_userauth_ext_info(int type, u_int32_t seqnr, struct ssh *ssh)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000465{
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000466 return kex_input_ext_info(type, seqnr, ssh);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000467}
468
Damien Miller62cee002000-09-23 17:15:56 +1100469void
djm@openbsd.org64c95982019-01-19 21:40:21 +0000470userauth(struct ssh *ssh, char *authlist)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000471{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000472 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000473
Damien Miller01ed2272008-11-05 16:20:46 +1100474 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
djm@openbsd.org64c95982019-01-19 21:40:21 +0000475 authctxt->method->cleanup(ssh);
Damien Miller01ed2272008-11-05 16:20:46 +1100476
Darren Tuckera627d422013-06-02 07:31:17 +1000477 free(authctxt->methoddata);
478 authctxt->methoddata = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000479 if (authlist == NULL) {
480 authlist = authctxt->authlist;
481 } else {
Darren Tuckera627d422013-06-02 07:31:17 +1000482 free(authctxt->authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000483 authctxt->authlist = authlist;
484 }
485 for (;;) {
486 Authmethod *method = authmethod_get(authlist);
487 if (method == NULL)
dtucker@openbsd.org4626e392017-06-14 00:31:38 +0000488 fatal("%s@%s: Permission denied (%s).",
489 authctxt->server_user, authctxt->host, authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000490 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000491
492 /* reset the per method handler */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000493 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_PER_METHOD_MIN,
Damien Millerf842fcb2003-05-15 12:01:28 +1000494 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
495
496 /* and try new method */
djm@openbsd.org64c95982019-01-19 21:40:21 +0000497 if (method->userauth(ssh) != 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000498 debug2("we sent a %s packet, wait for reply", method->name);
499 break;
500 } else {
501 debug2("we did not send a packet, disable method");
502 method->enabled = NULL;
503 }
504 }
505}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000506
Damien Millerf7475d72008-11-03 19:26:18 +1100507/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000508static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000509input_userauth_error(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100510{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000511 fatal("%s: bad message during authentication: type %d", __func__, type);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000512 return 0;
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000513}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000514
Damien Millerf7475d72008-11-03 19:26:18 +1100515/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000516static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000517input_userauth_banner(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000518{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000519 char *msg = NULL;
djm@openbsd.orged1df722019-01-19 21:34:45 +0000520 size_t len;
521 int r;
Darren Tucker79644822003-10-08 17:37:58 +1000522
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000523 debug3("%s", __func__);
djm@openbsd.orged1df722019-01-19 21:34:45 +0000524 if ((r = sshpkt_get_cstring(ssh, &msg, &len)) != 0 ||
djm@openbsd.org64c95982019-01-19 21:40:21 +0000525 (r = sshpkt_get_cstring(ssh, NULL, NULL)) != 0)
djm@openbsd.orged1df722019-01-19 21:34:45 +0000526 goto out;
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000527 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO)
528 fmprintf(stderr, "%s", msg);
djm@openbsd.orged1df722019-01-19 21:34:45 +0000529 r = 0;
530 out:
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000531 free(msg);
djm@openbsd.orged1df722019-01-19 21:34:45 +0000532 return r;
Damien Miller62cee002000-09-23 17:15:56 +1100533}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000534
Damien Millerf7475d72008-11-03 19:26:18 +1100535/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000536static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000537input_userauth_success(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100538{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000539 Authctxt *authctxt = ssh->authctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100540
Damien Miller62cee002000-09-23 17:15:56 +1100541 if (authctxt == NULL)
djm@openbsd.org64c95982019-01-19 21:40:21 +0000542 fatal("%s: no authentication context", __func__);
Darren Tuckera627d422013-06-02 07:31:17 +1000543 free(authctxt->authlist);
544 authctxt->authlist = NULL;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100545 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
djm@openbsd.org64c95982019-01-19 21:40:21 +0000546 authctxt->method->cleanup(ssh);
Darren Tuckera627d422013-06-02 07:31:17 +1000547 free(authctxt->methoddata);
548 authctxt->methoddata = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100549 authctxt->success = 1; /* break out */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000550 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100551}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000552
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000553#if 0
554static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000555input_userauth_success_unexpected(int type, u_int32_t seq, struct ssh *ssh)
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100556{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000557 Authctxt *authctxt = ssh->authctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100558
559 if (authctxt == NULL)
560 fatal("%s: no authentication context", __func__);
561
562 fatal("Unexpected authentication success during %s.",
563 authctxt->method->name);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000564 return 0;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100565}
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000566#endif
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100567
Damien Millerf7475d72008-11-03 19:26:18 +1100568/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000569static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000570input_userauth_failure(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100571{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000572 Authctxt *authctxt = ssh->authctxt;
Damien Miller62cee002000-09-23 17:15:56 +1100573 char *authlist = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000574 u_char partial;
Damien Miller62cee002000-09-23 17:15:56 +1100575
576 if (authctxt == NULL)
577 fatal("input_userauth_failure: no authentication context");
578
dtucker@openbsd.org696fb422019-07-07 01:05:00 +0000579 if (sshpkt_get_cstring(ssh, &authlist, NULL) != 0 ||
580 sshpkt_get_u8(ssh, &partial) != 0 ||
581 sshpkt_get_end(ssh) != 0)
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000582 goto out;
Damien Miller62cee002000-09-23 17:15:56 +1100583
Damien Miller62e9c4f2013-04-23 15:15:49 +1000584 if (partial != 0) {
dtucker@openbsd.org83cbca62016-07-22 05:46:11 +0000585 verbose("Authenticated with partial success.");
Damien Miller62e9c4f2013-04-23 15:15:49 +1000586 /* reset state */
djm@openbsd.orgb9844a42016-12-04 23:54:02 +0000587 pubkey_reset(authctxt);
Damien Miller62e9c4f2013-04-23 15:15:49 +1000588 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000589 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100590
djm@openbsd.org64c95982019-01-19 21:40:21 +0000591 userauth(ssh, authlist);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000592 authlist = NULL;
593 out:
594 free(authlist);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000595 return 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000596}
Damien Millerf7475d72008-11-03 19:26:18 +1100597
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000598/*
599 * Format an identity for logging including filename, key type, fingerprint
600 * and location (agent, etc.). Caller must free.
601 */
602static char *
603format_identity(Identity *id)
604{
605 char *fp = NULL, *ret = NULL;
djm@openbsd.org884416b2019-10-31 21:18:28 +0000606 const char *note = "";
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000607
608 if (id->key != NULL) {
609 fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
610 SSH_FP_DEFAULT);
611 }
djm@openbsd.org884416b2019-10-31 21:18:28 +0000612 if (id->key) {
613 if ((id->key->flags & SSHKEY_FLAG_EXT) != 0)
614 note = " token";
markus@openbsd.org2c557442019-11-12 19:33:08 +0000615 else if (sshkey_is_sk(id->key))
djm@openbsd.org884416b2019-10-31 21:18:28 +0000616 note = " security-key";
617 }
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000618 xasprintf(&ret, "%s %s%s%s%s%s%s",
619 id->filename,
620 id->key ? sshkey_type(id->key) : "", id->key ? " " : "",
621 fp ? fp : "",
djm@openbsd.org884416b2019-10-31 21:18:28 +0000622 id->userprovided ? " explicit" : "", note,
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000623 id->agent_fd != -1 ? " agent" : "");
624 free(fp);
625 return ret;
626}
627
Damien Millerf7475d72008-11-03 19:26:18 +1100628/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000629static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000630input_userauth_pk_ok(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000631{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000632 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000633 struct sshkey *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000634 Identity *id = NULL;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000635 int pktype, found = 0, sent = 0;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000636 size_t blen;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000637 char *pkalg = NULL, *fp = NULL, *ident = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000638 u_char *pkblob = NULL;
639 int r;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000640
641 if (authctxt == NULL)
642 fatal("input_userauth_pk_ok: no authentication context");
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000643
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000644 if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
645 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
646 (r = sshpkt_get_end(ssh)) != 0)
647 goto done;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000648
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000649 if ((pktype = sshkey_type_from_name(pkalg)) == KEY_UNSPEC) {
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000650 debug("%s: server sent unknown pkalg %s", __func__, pkalg);
Damien Miller280ecfb2003-05-14 13:46:00 +1000651 goto done;
652 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000653 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
654 debug("no key from blob. pkalg %s: %s", pkalg, ssh_err(r));
Damien Miller280ecfb2003-05-14 13:46:00 +1000655 goto done;
656 }
657 if (key->type != pktype) {
658 error("input_userauth_pk_ok: type mismatch "
659 "for decoded key (received %d, expected %d)",
660 key->type, pktype);
661 goto done;
662 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000663
Darren Tuckerd05b6012003-10-15 15:55:59 +1000664 /*
665 * search keys in the reverse order, because last candidate has been
666 * moved to the end of the queue. this also avoids confusion by
667 * duplicate keys
668 */
Damien Miller0b51a522004-04-20 20:07:19 +1000669 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000670 if (sshkey_equal(key, id->key)) {
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000671 found = 1;
Damien Miller280ecfb2003-05-14 13:46:00 +1000672 break;
673 }
674 }
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000675 if (!found || id == NULL) {
676 fp = sshkey_fingerprint(key, options.fingerprint_hash,
677 SSH_FP_DEFAULT);
678 error("%s: server replied with unknown key: %s %s", __func__,
679 sshkey_type(key), fp == NULL ? "<ERROR>" : fp);
680 goto done;
681 }
682 ident = format_identity(id);
683 debug("Server accepts key: %s", ident);
djm@openbsd.org64c95982019-01-19 21:40:21 +0000684 sent = sign_and_send_pubkey(ssh, id);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000685 r = 0;
686 done:
687 sshkey_free(key);
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000688 free(ident);
689 free(fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000690 free(pkalg);
691 free(pkblob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000692
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000693 /* try another method if we did not send a packet */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000694 if (r == 0 && sent == 0)
djm@openbsd.org64c95982019-01-19 21:40:21 +0000695 userauth(ssh, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000696 return r;
Damien Miller62cee002000-09-23 17:15:56 +1100697}
698
Darren Tucker0efd1552003-08-26 11:49:55 +1000699#ifdef GSSAPI
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000700static int
djm@openbsd.org64c95982019-01-19 21:40:21 +0000701userauth_gssapi(struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000702{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000703 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000704 Gssctxt *gssctxt = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000705 OM_uint32 min;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000706 int r, ok = 0;
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000707 gss_OID mech = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000708
709 /* Try one GSSAPI method at a time, rather than sending them all at
710 * once. */
711
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000712 if (authctxt->gss_supported_mechs == NULL)
713 gss_indicate_mechs(&min, &authctxt->gss_supported_mechs);
Darren Tucker0efd1552003-08-26 11:49:55 +1000714
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000715 /* Check to see whether the mechanism is usable before we offer it */
716 while (authctxt->mech_tried < authctxt->gss_supported_mechs->count &&
717 !ok) {
718 mech = &authctxt->gss_supported_mechs->
719 elements[authctxt->mech_tried];
Darren Tucker0efd1552003-08-26 11:49:55 +1000720 /* My DER encoding requires length<128 */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000721 if (mech->length < 128 && ssh_gssapi_check_mechanism(&gssctxt,
722 mech, authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000723 ok = 1; /* Mechanism works */
724 } else {
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000725 authctxt->mech_tried++;
Darren Tucker0efd1552003-08-26 11:49:55 +1000726 }
727 }
728
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000729 if (!ok || mech == NULL)
Damien Millereccb9de2005-06-17 12:59:34 +1000730 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000731
732 authctxt->methoddata=(void *)gssctxt;
733
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000734 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
735 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
736 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
737 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
738 (r = sshpkt_put_u32(ssh, 1)) != 0 ||
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000739 (r = sshpkt_put_u32(ssh, (mech->length) + 2)) != 0 ||
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000740 (r = sshpkt_put_u8(ssh, SSH_GSS_OIDTYPE)) != 0 ||
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000741 (r = sshpkt_put_u8(ssh, mech->length)) != 0 ||
742 (r = sshpkt_put(ssh, mech->elements, mech->length)) != 0 ||
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000743 (r = sshpkt_send(ssh)) != 0)
744 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +1000745
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000746 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
747 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
748 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
749 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000750
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000751 authctxt->mech_tried++; /* Move along to next candidate */
Darren Tucker0efd1552003-08-26 11:49:55 +1000752
753 return 1;
754}
755
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000756static void
757userauth_gssapi_cleanup(struct ssh *ssh)
758{
759 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
760 Gssctxt *gssctxt = (Gssctxt *)authctxt->methoddata;
761
762 ssh_gssapi_delete_ctx(&gssctxt);
763 authctxt->methoddata = NULL;
764
765 free(authctxt->gss_supported_mechs);
766 authctxt->gss_supported_mechs = NULL;
767}
768
Damien Miller91c6aa42003-11-17 21:20:18 +1100769static OM_uint32
djm@openbsd.org39896b72017-05-31 05:08:46 +0000770process_gssapi_token(struct ssh *ssh, gss_buffer_t recv_tok)
Damien Miller91c6aa42003-11-17 21:20:18 +1100771{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000772 Authctxt *authctxt = ssh->authctxt;
Damien Miller91c6aa42003-11-17 21:20:18 +1100773 Gssctxt *gssctxt = authctxt->methoddata;
774 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000775 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
776 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100777 OM_uint32 status, ms, flags;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000778 int r;
Damien Miller787b2ec2003-11-21 23:56:47 +1100779
Damien Miller91c6aa42003-11-17 21:20:18 +1100780 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100781 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100782
783 if (send_tok.length > 0) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000784 u_char type = GSS_ERROR(status) ?
785 SSH2_MSG_USERAUTH_GSSAPI_ERRTOK :
786 SSH2_MSG_USERAUTH_GSSAPI_TOKEN;
Damien Miller787b2ec2003-11-21 23:56:47 +1100787
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000788 if ((r = sshpkt_start(ssh, type)) != 0 ||
789 (r = sshpkt_put_string(ssh, send_tok.value,
790 send_tok.length)) != 0 ||
791 (r = sshpkt_send(ssh)) != 0)
792 fatal("%s: %s", __func__, ssh_err(r));
793
Damien Miller91c6aa42003-11-17 21:20:18 +1100794 gss_release_buffer(&ms, &send_tok);
795 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100796
Damien Miller91c6aa42003-11-17 21:20:18 +1100797 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100798 /* send either complete or MIC, depending on mechanism */
799 if (!(flags & GSS_C_INTEG_FLAG)) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000800 if ((r = sshpkt_start(ssh,
801 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE)) != 0 ||
802 (r = sshpkt_send(ssh)) != 0)
803 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0425d402003-11-17 22:18:21 +1100804 } else {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000805 struct sshbuf *b;
806
807 if ((b = sshbuf_new()) == NULL)
808 fatal("%s: sshbuf_new failed", __func__);
809 ssh_gssapi_buildmic(b, authctxt->server_user,
Damien Miller0425d402003-11-17 22:18:21 +1100810 authctxt->service, "gssapi-with-mic");
811
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000812 if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
813 fatal("%s: sshbuf_mutable_ptr failed", __func__);
814 gssbuf.length = sshbuf_len(b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100815
Damien Miller0425d402003-11-17 22:18:21 +1100816 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100817
Damien Miller0425d402003-11-17 22:18:21 +1100818 if (!GSS_ERROR(status)) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000819 if ((r = sshpkt_start(ssh,
820 SSH2_MSG_USERAUTH_GSSAPI_MIC)) != 0 ||
821 (r = sshpkt_put_string(ssh, mic.value,
822 mic.length)) != 0 ||
823 (r = sshpkt_send(ssh)) != 0)
824 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0425d402003-11-17 22:18:21 +1100825 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100826
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000827 sshbuf_free(b);
Damien Miller0425d402003-11-17 22:18:21 +1100828 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100829 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100830 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100831
Damien Miller91c6aa42003-11-17 21:20:18 +1100832 return status;
833}
834
Damien Millerf7475d72008-11-03 19:26:18 +1100835/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000836static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000837input_gssapi_response(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000838{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000839 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000840 Gssctxt *gssctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000841 size_t oidlen;
842 u_char *oidv = NULL;
843 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000844
845 if (authctxt == NULL)
846 fatal("input_gssapi_response: no authentication context");
847 gssctxt = authctxt->methoddata;
848
849 /* Setup our OID */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000850 if ((r = sshpkt_get_string(ssh, &oidv, &oidlen)) != 0)
851 goto done;
Darren Tucker0efd1552003-08-26 11:49:55 +1000852
Darren Tucker655a5e02003-11-03 20:09:03 +1100853 if (oidlen <= 2 ||
854 oidv[0] != SSH_GSS_OIDTYPE ||
855 oidv[1] != oidlen - 2) {
856 debug("Badly encoded mechanism OID received");
djm@openbsd.org0a5f2ea2019-01-21 01:05:00 +0000857 userauth(ssh, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000858 goto ok;
Darren Tucker0efd1552003-08-26 11:49:55 +1000859 }
860
Darren Tucker655a5e02003-11-03 20:09:03 +1100861 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
862 fatal("Server returned different OID than expected");
863
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000864 if ((r = sshpkt_get_end(ssh)) != 0)
865 goto done;
Darren Tucker0efd1552003-08-26 11:49:55 +1000866
djm@openbsd.org39896b72017-05-31 05:08:46 +0000867 if (GSS_ERROR(process_gssapi_token(ssh, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000868 /* Start again with next method on list */
869 debug("Trying to start again");
djm@openbsd.org0a5f2ea2019-01-21 01:05:00 +0000870 userauth(ssh, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000871 goto ok;
Darren Tucker0efd1552003-08-26 11:49:55 +1000872 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000873 ok:
874 r = 0;
875 done:
876 free(oidv);
877 return r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000878}
879
Damien Millerf7475d72008-11-03 19:26:18 +1100880/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000881static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000882input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000883{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000884 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000885 gss_buffer_desc recv_tok;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000886 u_char *p = NULL;
887 size_t len;
Damien Miller91c6aa42003-11-17 21:20:18 +1100888 OM_uint32 status;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000889 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000890
891 if (authctxt == NULL)
892 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000893
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000894 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
895 (r = sshpkt_get_end(ssh)) != 0)
896 goto out;
Darren Tucker0efd1552003-08-26 11:49:55 +1000897
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000898 recv_tok.value = p;
899 recv_tok.length = len;
djm@openbsd.org39896b72017-05-31 05:08:46 +0000900 status = process_gssapi_token(ssh, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000901
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000902 /* Start again with the next method in the list */
Darren Tucker0efd1552003-08-26 11:49:55 +1000903 if (GSS_ERROR(status)) {
djm@openbsd.org0a5f2ea2019-01-21 01:05:00 +0000904 userauth(ssh, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000905 /* ok */
Darren Tucker0efd1552003-08-26 11:49:55 +1000906 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000907 r = 0;
908 out:
909 free(p);
910 return r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000911}
912
Damien Millerf7475d72008-11-03 19:26:18 +1100913/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000914static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000915input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000916{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000917 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000918 Gssctxt *gssctxt;
919 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
920 gss_buffer_desc recv_tok;
Damien Millerd677ad12013-04-23 15:18:51 +1000921 OM_uint32 ms;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000922 u_char *p = NULL;
923 size_t len;
924 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000925
926 if (authctxt == NULL)
927 fatal("input_gssapi_response: no authentication context");
928 gssctxt = authctxt->methoddata;
929
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000930 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
931 (r = sshpkt_get_end(ssh)) != 0) {
932 free(p);
933 return r;
934 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000935
936 /* Stick it into GSSAPI and see what it says */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000937 recv_tok.value = p;
938 recv_tok.length = len;
Damien Millerd677ad12013-04-23 15:18:51 +1000939 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000940 &recv_tok, &send_tok, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000941 free(p);
Darren Tucker0efd1552003-08-26 11:49:55 +1000942 gss_release_buffer(&ms, &send_tok);
943
944 /* Server will be returning a failed packet after this one */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000945 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000946}
947
Damien Millerf7475d72008-11-03 19:26:18 +1100948/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000949static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000950input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000951{
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000952 char *msg = NULL;
953 char *lang = NULL;
954 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000955
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000956 if ((r = sshpkt_get_u32(ssh, NULL)) != 0 || /* maj */
957 (r = sshpkt_get_u32(ssh, NULL)) != 0 || /* min */
958 (r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
959 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
960 goto out;
961 r = sshpkt_get_end(ssh);
Damien Miller15d72a02005-11-05 15:07:33 +1100962 debug("Server GSSAPI Error:\n%s", msg);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000963 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000964 free(msg);
965 free(lang);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000966 return r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000967}
968#endif /* GSSAPI */
969
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000970static int
djm@openbsd.org64c95982019-01-19 21:40:21 +0000971userauth_none(struct ssh *ssh)
Damien Miller874d77b2000-10-14 16:23:11 +1100972{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000973 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000974 int r;
975
Damien Miller874d77b2000-10-14 16:23:11 +1100976 /* initial userauth request */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000977 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
978 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
979 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
980 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
981 (r = sshpkt_send(ssh)) != 0)
982 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100983 return 1;
984}
985
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000986static int
djm@openbsd.org64c95982019-01-19 21:40:21 +0000987userauth_passwd(struct ssh *ssh)
Damien Millereba71ba2000-04-29 23:57:08 +1000988{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000989 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
djm@openbsd.org91b19192018-11-28 06:00:38 +0000990 char *password, *prompt = NULL;
Darren Tuckerab791692010-01-08 18:48:02 +1100991 const char *host = options.host_key_alias ? options.host_key_alias :
992 authctxt->host;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000993 int r;
Damien Millereba71ba2000-04-29 23:57:08 +1000994
djm@openbsd.org4a526942019-01-04 03:27:50 +0000995 if (authctxt->attempt_passwd++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000996 return 0;
997
djm@openbsd.org4a526942019-01-04 03:27:50 +0000998 if (authctxt->attempt_passwd != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000999 error("Permission denied, please try again.");
1000
djm@openbsd.org91b19192018-11-28 06:00:38 +00001001 xasprintf(&prompt, "%s@%s's password: ", authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +10001002 password = read_passphrase(prompt, 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001003 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1004 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1005 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1006 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1007 (r = sshpkt_put_u8(ssh, 0)) != 0 ||
1008 (r = sshpkt_put_cstring(ssh, password)) != 0 ||
1009 (r = sshpkt_add_padding(ssh, 64)) != 0 ||
1010 (r = sshpkt_send(ssh)) != 0)
1011 fatal("%s: %s", __func__, ssh_err(r));
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001012
djm@openbsd.org91b19192018-11-28 06:00:38 +00001013 free(prompt);
1014 if (password != NULL)
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001015 freezero(password, strlen(password));
1016
1017 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001018 &input_userauth_passwd_changereq);
1019
Damien Millereba71ba2000-04-29 23:57:08 +10001020 return 1;
1021}
Damien Millerf7475d72008-11-03 19:26:18 +11001022
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001023/*
1024 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
1025 */
Damien Millerf7475d72008-11-03 19:26:18 +11001026/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +00001027static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001028input_userauth_passwd_changereq(int type, u_int32_t seqnr, struct ssh *ssh)
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001029{
markus@openbsd.org94583be2017-05-30 14:19:15 +00001030 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001031 char *info = NULL, *lang = NULL, *password = NULL, *retype = NULL;
dtucker@openbsd.orga54eb272017-08-27 00:38:41 +00001032 char prompt[256];
djm@openbsd.org3e032a92017-01-30 00:32:03 +00001033 const char *host;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001034 int r;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001035
1036 debug2("input_userauth_passwd_changereq");
1037
1038 if (authctxt == NULL)
1039 fatal("input_userauth_passwd_changereq: "
1040 "no authentication context");
djm@openbsd.org3e032a92017-01-30 00:32:03 +00001041 host = options.host_key_alias ? options.host_key_alias : authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001042
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001043 if ((r = sshpkt_get_cstring(ssh, &info, NULL)) != 0 ||
1044 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1045 goto out;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001046 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001047 logit("%s", info);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001048 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1049 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1050 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1051 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1052 (r = sshpkt_put_u8(ssh, 1)) != 0) /* additional info */
1053 goto out;
1054
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001055 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001056 "Enter %.30s@%.128s's old password: ",
Darren Tuckerab791692010-01-08 18:48:02 +11001057 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001058 password = read_passphrase(prompt, 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001059 if ((r = sshpkt_put_cstring(ssh, password)) != 0)
1060 goto out;
1061
1062 freezero(password, strlen(password));
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001063 password = NULL;
1064 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001065 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001066 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +11001067 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001068 password = read_passphrase(prompt, RP_ALLOW_EOF);
1069 if (password == NULL) {
1070 /* bail out */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001071 r = 0;
1072 goto out;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001073 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001074 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001075 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +11001076 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001077 retype = read_passphrase(prompt, 0);
1078 if (strcmp(password, retype) != 0) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001079 freezero(password, strlen(password));
Damien Miller996acd22003-04-09 20:59:48 +10001080 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001081 password = NULL;
1082 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001083 freezero(retype, strlen(retype));
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001084 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001085 if ((r = sshpkt_put_cstring(ssh, password)) != 0 ||
1086 (r = sshpkt_add_padding(ssh, 64)) != 0 ||
1087 (r = sshpkt_send(ssh)) != 0)
1088 goto out;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001089
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001090 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001091 &input_userauth_passwd_changereq);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001092 r = 0;
1093 out:
1094 if (password)
1095 freezero(password, strlen(password));
1096 free(info);
1097 free(lang);
1098 return r;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001099}
Damien Millereba71ba2000-04-29 23:57:08 +10001100
djm@openbsd.org26074382018-03-03 03:01:50 +00001101/*
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001102 * Select an algorithm for publickey signatures.
1103 * Returns algorithm (caller must free) or NULL if no mutual algorithm found.
1104 *
1105 * Call with ssh==NULL to ignore server-sig-algs extension list and
1106 * only attempt with the key's base signature type.
djm@openbsd.org26074382018-03-03 03:01:50 +00001107 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001108static char *
1109key_sig_algorithm(struct ssh *ssh, const struct sshkey *key)
djm@openbsd.org26074382018-03-03 03:01:50 +00001110{
djm@openbsd.orgb4d4eda2018-07-03 13:20:25 +00001111 char *allowed, *oallowed, *cp, *tmp, *alg = NULL;
djm@openbsd.org26074382018-03-03 03:01:50 +00001112
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001113 /*
1114 * The signature algorithm will only differ from the key algorithm
1115 * for RSA keys/certs and when the server advertises support for
1116 * newer (SHA2) algorithms.
1117 */
1118 if (ssh == NULL || ssh->kex->server_sig_algs == NULL ||
djm@openbsd.org1a4a9cf2018-10-11 03:48:04 +00001119 (key->type != KEY_RSA && key->type != KEY_RSA_CERT) ||
1120 (key->type == KEY_RSA_CERT && (datafellows & SSH_BUG_SIGTYPE))) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001121 /* Filter base key signature alg against our configuration */
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001122 return match_list(sshkey_ssh_name(key),
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001123 options.pubkey_key_types, NULL);
djm@openbsd.org26074382018-03-03 03:01:50 +00001124 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001125
1126 /*
1127 * For RSA keys/certs, since these might have a different sig type:
1128 * find the first entry in PubkeyAcceptedKeyTypes of the right type
1129 * that also appears in the supported signature algorithms list from
1130 * the server.
1131 */
1132 oallowed = allowed = xstrdup(options.pubkey_key_types);
1133 while ((cp = strsep(&allowed, ",")) != NULL) {
1134 if (sshkey_type_from_name(cp) != key->type)
1135 continue;
djm@openbsd.orgb4d4eda2018-07-03 13:20:25 +00001136 tmp = match_list(sshkey_sigalg_by_name(cp), ssh->kex->server_sig_algs, NULL);
1137 if (tmp != NULL)
1138 alg = xstrdup(cp);
1139 free(tmp);
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001140 if (alg != NULL)
1141 break;
1142 }
1143 free(oallowed);
1144 return alg;
djm@openbsd.org26074382018-03-03 03:01:50 +00001145}
1146
Ben Lindstrombba81212001-06-25 05:01:22 +00001147static int
djm@openbsd.org141efe42015-01-14 20:05:27 +00001148identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001149 const u_char *data, size_t datalen, u_int compat, const char *alg)
Damien Miller280ecfb2003-05-14 13:46:00 +10001150{
djm@openbsd.org884416b2019-10-31 21:18:28 +00001151 struct sshkey *sign_key = NULL, *prv = NULL;
1152 int r = SSH_ERR_INTERNAL_ERROR;
djm@openbsd.orge44bb612019-11-12 22:36:44 +00001153 struct notifier_ctx *notifier = NULL;
1154 char *fp = NULL;
djm@openbsd.org884416b2019-10-31 21:18:28 +00001155
1156 *sigp = NULL;
1157 *lenp = 0;
Damien Miller280ecfb2003-05-14 13:46:00 +10001158
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001159 /* The agent supports this key. */
djm@openbsd.org26074382018-03-03 03:01:50 +00001160 if (id->key != NULL && id->agent_fd != -1) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001161 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
1162 data, datalen, alg, compat);
1163 }
1164
1165 /*
1166 * We have already loaded the private key or the private key is
1167 * stored in external hardware.
1168 */
1169 if (id->key != NULL &&
1170 (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))) {
djm@openbsd.org884416b2019-10-31 21:18:28 +00001171 sign_key = id->key;
1172 } else {
1173 /* Load the private key from the file. */
1174 if ((prv = load_identity_file(id)) == NULL)
1175 return SSH_ERR_KEY_NOT_FOUND;
1176 if (id->key != NULL && !sshkey_equal_public(prv, id->key)) {
1177 error("%s: private key %s contents do not match public",
1178 __func__, id->filename);
1179 r = SSH_ERR_KEY_NOT_FOUND;
1180 goto out;
1181 }
1182 sign_key = prv;
djm@openbsd.orge44bb612019-11-12 22:36:44 +00001183 if (sshkey_is_sk(sign_key) &&
1184 (sign_key->sk_flags & SSH_SK_USER_PRESENCE_REQD)) {
1185 /* XXX match batch mode should just skip these keys? */
1186 if ((fp = sshkey_fingerprint(sign_key,
1187 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1188 fatal("%s: sshkey_fingerprint", __func__);
1189 notifier = notify_start(options.batch_mode,
1190 "Confirm user presence for key %s %s",
1191 sshkey_type(sign_key), fp);
1192 free(fp);
1193 }
djm@openbsd.org26074382018-03-03 03:01:50 +00001194 }
djm@openbsd.org9a14c642019-10-31 21:23:19 +00001195 if ((r = sshkey_sign(sign_key, sigp, lenp, data, datalen,
1196 alg, options.sk_provider, compat)) != 0) {
djm@openbsd.org884416b2019-10-31 21:18:28 +00001197 debug("%s: sshkey_sign: %s", __func__, ssh_err(r));
1198 goto out;
djm@openbsd.orgc4972d02017-08-11 04:47:12 +00001199 }
djm@openbsd.org884416b2019-10-31 21:18:28 +00001200 /*
1201 * PKCS#11 tokens may not support all signature algorithms,
1202 * so check what we get back.
1203 */
1204 if ((r = sshkey_check_sigtype(*sigp, *lenp, alg)) != 0) {
1205 debug("%s: sshkey_check_sigtype: %s", __func__, ssh_err(r));
1206 goto out;
1207 }
1208 /* success */
1209 r = 0;
1210 out:
djm@openbsd.org49dc9fa2019-11-15 02:37:24 +00001211 notify_complete(notifier);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001212 sshkey_free(prv);
djm@openbsd.org26074382018-03-03 03:01:50 +00001213 return r;
Damien Miller280ecfb2003-05-14 13:46:00 +10001214}
1215
1216static int
djm@openbsd.org1e245522017-03-11 23:40:26 +00001217id_filename_matches(Identity *id, Identity *private_id)
1218{
1219 const char *suffixes[] = { ".pub", "-cert.pub", NULL };
1220 size_t len = strlen(id->filename), plen = strlen(private_id->filename);
1221 size_t i, slen;
1222
1223 if (strcmp(id->filename, private_id->filename) == 0)
1224 return 1;
1225 for (i = 0; suffixes[i]; i++) {
1226 slen = strlen(suffixes[i]);
1227 if (len > slen && plen == len - slen &&
1228 strcmp(id->filename + (len - slen), suffixes[i]) == 0 &&
1229 memcmp(id->filename, private_id->filename, plen) == 0)
1230 return 1;
1231 }
1232 return 0;
1233}
1234
1235static int
djm@openbsd.org64c95982019-01-19 21:40:21 +00001236sign_and_send_pubkey(struct ssh *ssh, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +10001237{
djm@openbsd.org64c95982019-01-19 21:40:21 +00001238 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001239 struct sshbuf *b = NULL;
1240 Identity *private_id, *sign_id = NULL;
1241 u_char *signature = NULL;
1242 size_t slen = 0, skip = 0;
1243 int r, fallback_sigtype, sent = 0;
1244 char *alg = NULL, *fp = NULL;
1245 const char *loc = "";
Damien Millereba71ba2000-04-29 23:57:08 +10001246
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001247 if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1248 SSH_FP_DEFAULT)) == NULL)
1249 return 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001250
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001251 debug3("%s: %s %s", __func__, sshkey_type(id->key), fp);
Damien Millereba71ba2000-04-29 23:57:08 +10001252
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001253 /*
1254 * If the key is an certificate, try to find a matching private key
1255 * and use it to complete the signature.
djm@openbsd.org9ee692f2016-04-28 14:30:21 +00001256 * If no such private key exists, fall back to trying the certificate
1257 * key itself in case it has a private half already loaded.
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001258 * This will try to set sign_id to the private key that will perform
1259 * the signature.
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001260 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001261 if (sshkey_is_cert(id->key)) {
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001262 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1263 if (sshkey_equal_public(id->key, private_id->key) &&
1264 id->key->type != private_id->key->type) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001265 sign_id = private_id;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001266 break;
1267 }
1268 }
djm@openbsd.org1e245522017-03-11 23:40:26 +00001269 /*
1270 * Exact key matches are preferred, but also allow
1271 * filename matches for non-PKCS#11/agent keys that
1272 * didn't load public keys. This supports the case
1273 * of keeping just a private key file and public
1274 * certificate on disk.
1275 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001276 if (sign_id == NULL &&
1277 !id->isprivate && id->agent_fd == -1 &&
djm@openbsd.org1e245522017-03-11 23:40:26 +00001278 (id->key->flags & SSHKEY_FLAG_EXT) == 0) {
1279 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1280 if (private_id->key == NULL &&
1281 id_filename_matches(id, private_id)) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001282 sign_id = private_id;
djm@openbsd.org1e245522017-03-11 23:40:26 +00001283 break;
1284 }
1285 }
1286 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001287 if (sign_id != NULL) {
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001288 debug2("%s: using private key \"%s\"%s for "
1289 "certificate", __func__, id->filename,
1290 id->agent_fd != -1 ? " from agent" : "");
1291 } else {
djm@openbsd.orgc38905b2016-03-14 16:20:54 +00001292 debug("%s: no separate private key for certificate "
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001293 "\"%s\"", __func__, id->filename);
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001294 }
1295 }
1296
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001297 /*
1298 * If the above didn't select another identity to do the signing
1299 * then default to the one we started with.
1300 */
1301 if (sign_id == NULL)
1302 sign_id = id;
1303
1304 /* assemble and sign data */
1305 for (fallback_sigtype = 0; fallback_sigtype <= 1; fallback_sigtype++) {
1306 free(alg);
1307 slen = 0;
1308 signature = NULL;
1309 if ((alg = key_sig_algorithm(fallback_sigtype ? NULL : ssh,
1310 id->key)) == NULL) {
1311 error("%s: no mutual signature supported", __func__);
1312 goto out;
1313 }
1314 debug3("%s: signing using %s", __func__, alg);
1315
1316 sshbuf_free(b);
1317 if ((b = sshbuf_new()) == NULL)
1318 fatal("%s: sshbuf_new failed", __func__);
1319 if (datafellows & SSH_OLD_SESSIONID) {
1320 if ((r = sshbuf_put(b, session_id2,
1321 session_id2_len)) != 0) {
1322 fatal("%s: sshbuf_put: %s",
1323 __func__, ssh_err(r));
1324 }
1325 } else {
1326 if ((r = sshbuf_put_string(b, session_id2,
1327 session_id2_len)) != 0) {
1328 fatal("%s: sshbuf_put_string: %s",
1329 __func__, ssh_err(r));
1330 }
1331 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001332 skip = sshbuf_len(b);
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001333 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1334 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1335 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1336 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1337 (r = sshbuf_put_u8(b, 1)) != 0 ||
1338 (r = sshbuf_put_cstring(b, alg)) != 0 ||
1339 (r = sshkey_puts(id->key, b)) != 0) {
1340 fatal("%s: assemble signed data: %s",
1341 __func__, ssh_err(r));
1342 }
1343
1344 /* generate signature */
1345 r = identity_sign(sign_id, &signature, &slen,
1346 sshbuf_ptr(b), sshbuf_len(b), datafellows, alg);
1347 if (r == 0)
1348 break;
1349 else if (r == SSH_ERR_KEY_NOT_FOUND)
1350 goto out; /* soft failure */
1351 else if (r == SSH_ERR_SIGN_ALG_UNSUPPORTED &&
1352 !fallback_sigtype) {
1353 if (sign_id->agent_fd != -1)
1354 loc = "agent ";
1355 else if ((sign_id->key->flags & SSHKEY_FLAG_EXT) != 0)
1356 loc = "token ";
1357 logit("%skey %s %s returned incorrect signature type",
1358 loc, sshkey_type(id->key), fp);
1359 continue;
1360 }
1361 error("%s: signing failed: %s", __func__, ssh_err(r));
1362 goto out;
Damien Millerad833b32000-08-23 10:46:23 +10001363 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001364 if (slen == 0 || signature == NULL) /* shouldn't happen */
1365 fatal("%s: no signature", __func__);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001366
Damien Millereba71ba2000-04-29 23:57:08 +10001367 /* append signature */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001368 if ((r = sshbuf_put_string(b, signature, slen)) != 0)
1369 fatal("%s: append signature: %s", __func__, ssh_err(r));
Damien Millereba71ba2000-04-29 23:57:08 +10001370
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001371#ifdef DEBUG_PK
1372 sshbuf_dump(b, stderr);
1373#endif
Damien Millereba71ba2000-04-29 23:57:08 +10001374 /* skip session id and packet type */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001375 if ((r = sshbuf_consume(b, skip + 1)) != 0)
1376 fatal("%s: consume: %s", __func__, ssh_err(r));
Damien Millereba71ba2000-04-29 23:57:08 +10001377
1378 /* put remaining data from buffer into packet */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001379 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1380 (r = sshpkt_putb(ssh, b)) != 0 ||
1381 (r = sshpkt_send(ssh)) != 0)
1382 fatal("%s: enqueue request: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001383
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001384 /* success */
1385 sent = 1;
1386
1387 out:
1388 free(fp);
1389 free(alg);
1390 sshbuf_free(b);
1391 freezero(signature, slen);
1392 return sent;
Damien Miller994cf142000-07-21 10:19:44 +10001393}
1394
Ben Lindstrombba81212001-06-25 05:01:22 +00001395static int
djm@openbsd.org64c95982019-01-19 21:40:21 +00001396send_pubkey_test(struct ssh *ssh, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001397{
djm@openbsd.org64c95982019-01-19 21:40:21 +00001398 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001399 u_char *blob = NULL;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001400 char *alg = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001401 size_t bloblen;
1402 u_int have_sig = 0;
1403 int sent = 0, r;
Damien Miller994cf142000-07-21 10:19:44 +10001404
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001405 if ((alg = key_sig_algorithm(ssh, id->key)) == NULL) {
1406 debug("%s: no mutual signature algorithm", __func__);
1407 goto out;
1408 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001409
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001410 if ((r = sshkey_to_blob(id->key, &blob, &bloblen)) != 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001411 /* we cannot handle this key */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001412 debug3("%s: cannot handle key", __func__);
1413 goto out;
Damien Miller994cf142000-07-21 10:19:44 +10001414 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001415 /* register callback for USERAUTH_PK_OK message */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001416 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001417
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001418 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1419 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1420 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1421 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1422 (r = sshpkt_put_u8(ssh, have_sig)) != 0 ||
1423 (r = sshpkt_put_cstring(ssh, alg)) != 0 ||
1424 (r = sshpkt_put_string(ssh, blob, bloblen)) != 0 ||
1425 (r = sshpkt_send(ssh)) != 0)
1426 fatal("%s: %s", __func__, ssh_err(r));
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001427 sent = 1;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001428
1429 out:
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001430 free(alg);
1431 free(blob);
1432 return sent;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001433}
1434
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001435static struct sshkey *
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001436load_identity_file(Identity *id)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001437{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001438 struct sshkey *private = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001439 char prompt[300], *passphrase, *comment;
dtucker@openbsd.org6b39a7b2019-08-05 11:50:33 +00001440 int r, quit = 0, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001441 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001442
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001443 if (stat(id->filename, &st) == -1) {
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001444 (id->userprovided ? logit : debug3)("no such identity: %s: %s",
1445 id->filename, strerror(errno));
Ben Lindstrom329782e2001-03-10 17:08:59 +00001446 return NULL;
1447 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001448 snprintf(prompt, sizeof prompt,
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001449 "Enter passphrase for key '%.100s': ", id->filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001450 for (i = 0; i <= options.number_of_password_prompts; i++) {
1451 if (i == 0)
1452 passphrase = "";
1453 else {
Damien Miller62cee002000-09-23 17:15:56 +11001454 passphrase = read_passphrase(prompt, 0);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001455 if (*passphrase == '\0') {
Damien Miller62cee002000-09-23 17:15:56 +11001456 debug2("no passphrase given, try next key");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001457 free(passphrase);
1458 break;
Damien Miller62cee002000-09-23 17:15:56 +11001459 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001460 }
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001461 switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
dtucker@openbsd.org6b39a7b2019-08-05 11:50:33 +00001462 passphrase, &private, &comment))) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001463 case 0:
1464 break;
1465 case SSH_ERR_KEY_WRONG_PASSPHRASE:
1466 if (options.batch_mode) {
1467 quit = 1;
1468 break;
1469 }
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001470 if (i != 0)
1471 debug2("bad passphrase given, try again...");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001472 break;
1473 case SSH_ERR_SYSTEM_ERROR:
1474 if (errno == ENOENT) {
1475 debug2("Load key \"%s\": %s",
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001476 id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001477 quit = 1;
1478 break;
1479 }
1480 /* FALLTHROUGH */
1481 default:
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001482 error("Load key \"%s\": %s", id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001483 quit = 1;
1484 break;
1485 }
markus@openbsd.org2c557442019-11-12 19:33:08 +00001486 if (private != NULL && sshkey_is_sk(private) &&
djm@openbsd.org884416b2019-10-31 21:18:28 +00001487 options.sk_provider == NULL) {
1488 debug("key \"%s\" is a security key, but no "
1489 "provider specified", id->filename);
1490 sshkey_free(private);
1491 private = NULL;
1492 quit = 1;
1493 }
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001494 if (!quit && private != NULL && id->agent_fd == -1 &&
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001495 !(id->key && id->isprivate))
1496 maybe_add_key_to_agent(id->filename, private, comment,
1497 passphrase);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001498 if (i > 0)
1499 freezero(passphrase, strlen(passphrase));
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001500 free(comment);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001501 if (private != NULL || quit)
1502 break;
Damien Miller994cf142000-07-21 10:19:44 +10001503 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001504 return private;
1505}
1506
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001507static int
1508key_type_allowed_by_config(struct sshkey *key)
1509{
1510 if (match_pattern_list(sshkey_ssh_name(key),
1511 options.pubkey_key_types, 0) == 1)
1512 return 1;
1513
1514 /* RSA keys/certs might be allowed by alternate signature types */
1515 switch (key->type) {
1516 case KEY_RSA:
1517 if (match_pattern_list("rsa-sha2-512",
1518 options.pubkey_key_types, 0) == 1)
1519 return 1;
1520 if (match_pattern_list("rsa-sha2-256",
1521 options.pubkey_key_types, 0) == 1)
1522 return 1;
1523 break;
1524 case KEY_RSA_CERT:
1525 if (match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
1526 options.pubkey_key_types, 0) == 1)
1527 return 1;
1528 if (match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
1529 options.pubkey_key_types, 0) == 1)
1530 return 1;
1531 break;
1532 }
1533 return 0;
1534}
1535
1536
Damien Miller280ecfb2003-05-14 13:46:00 +10001537/*
1538 * try keys in the following order:
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001539 * 1. certificates listed in the config file
1540 * 2. other input certificates
1541 * 3. agent keys that are found in the config file
1542 * 4. other agent keys
1543 * 5. keys that are only listed in the config file
Damien Miller280ecfb2003-05-14 13:46:00 +10001544 */
1545static void
1546pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001547{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001548 struct identity *id, *id2, *tmp;
1549 struct idlist agent, files, *preferred;
1550 struct sshkey *key;
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001551 int agent_fd = -1, i, r, found;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001552 size_t j;
1553 struct ssh_identitylist *idlist;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001554 char *ident;
Damien Millerad833b32000-08-23 10:46:23 +10001555
Damien Miller280ecfb2003-05-14 13:46:00 +10001556 TAILQ_INIT(&agent); /* keys from the agent */
1557 TAILQ_INIT(&files); /* keys from the config file */
1558 preferred = &authctxt->keys;
1559 TAILQ_INIT(preferred); /* preferred order of keys */
1560
Damien Millercb6b68b2012-12-03 09:49:52 +11001561 /* list of keys stored in the filesystem and PKCS#11 */
Damien Miller280ecfb2003-05-14 13:46:00 +10001562 for (i = 0; i < options.num_identity_files; i++) {
1563 key = options.identity_keys[i];
djm@openbsd.org884416b2019-10-31 21:18:28 +00001564 if (key && key->cert &&
1565 key->cert->type != SSH2_CERT_TYPE_USER) {
1566 debug("%s: ignoring certificate %s: not a user "
1567 "certificate", __func__,
1568 options.identity_files[i]);
Damien Miller0a80ca12010-02-27 07:55:05 +11001569 continue;
djm@openbsd.org884416b2019-10-31 21:18:28 +00001570 }
markus@openbsd.org2c557442019-11-12 19:33:08 +00001571 if (key && sshkey_is_sk(key) && options.sk_provider == NULL) {
djm@openbsd.org884416b2019-10-31 21:18:28 +00001572 debug("%s: ignoring security key %s as no "
1573 "SecurityKeyProvider has been specified",
1574 __func__, options.identity_files[i]);
1575 continue;
1576 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001577 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001578 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001579 id->agent_fd = -1;
Damien Miller280ecfb2003-05-14 13:46:00 +10001580 id->key = key;
1581 id->filename = xstrdup(options.identity_files[i]);
Darren Tucker19104782013-04-05 11:13:08 +11001582 id->userprovided = options.identity_file_userprovided[i];
Damien Miller280ecfb2003-05-14 13:46:00 +10001583 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001584 }
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001585 /* list of certificates specified by user */
1586 for (i = 0; i < options.num_certificate_files; i++) {
1587 key = options.certificates[i];
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001588 if (!sshkey_is_cert(key) || key->cert == NULL ||
djm@openbsd.org884416b2019-10-31 21:18:28 +00001589 key->cert->type != SSH2_CERT_TYPE_USER) {
1590 debug("%s: ignoring certificate %s: not a user "
1591 "certificate", __func__,
1592 options.identity_files[i]);
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001593 continue;
djm@openbsd.org884416b2019-10-31 21:18:28 +00001594 }
markus@openbsd.org2c557442019-11-12 19:33:08 +00001595 if (key && sshkey_is_sk(key) && options.sk_provider == NULL) {
djm@openbsd.org884416b2019-10-31 21:18:28 +00001596 debug("%s: ignoring security key certificate %s as no "
1597 "SecurityKeyProvider has been specified",
1598 __func__, options.identity_files[i]);
1599 continue;
1600 }
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001601 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001602 id->agent_fd = -1;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001603 id->key = key;
1604 id->filename = xstrdup(options.certificate_files[i]);
1605 id->userprovided = options.certificate_file_userprovided[i];
1606 TAILQ_INSERT_TAIL(preferred, id, next);
1607 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001608 /* list of keys supported by the agent */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001609 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1610 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1611 debug("%s: ssh_get_authentication_socket: %s",
1612 __func__, ssh_err(r));
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +00001613 } else if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001614 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1615 debug("%s: ssh_fetch_identitylist: %s",
1616 __func__, ssh_err(r));
markus@openbsd.orgfc77ccd2016-01-14 22:56:56 +00001617 close(agent_fd);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001618 } else {
1619 for (j = 0; j < idlist->nkeys; j++) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001620 found = 0;
1621 TAILQ_FOREACH(id, &files, next) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001622 /*
1623 * agent keys from the config file are
1624 * preferred
1625 */
1626 if (sshkey_equal(idlist->keys[j], id->key)) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001627 TAILQ_REMOVE(&files, id, next);
1628 TAILQ_INSERT_TAIL(preferred, id, next);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001629 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001630 found = 1;
1631 break;
1632 }
1633 }
Damien Millerbd394c32004-03-08 23:12:36 +11001634 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001635 id = xcalloc(1, sizeof(*id));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001636 /* XXX "steals" key/comment from idlist */
1637 id->key = idlist->keys[j];
1638 id->filename = idlist->comments[j];
1639 idlist->keys[j] = NULL;
1640 idlist->comments[j] = NULL;
1641 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001642 TAILQ_INSERT_TAIL(&agent, id, next);
1643 }
1644 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001645 ssh_free_identitylist(idlist);
Damien Miller280ecfb2003-05-14 13:46:00 +10001646 /* append remaining agent keys */
1647 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1648 TAILQ_REMOVE(&agent, id, next);
1649 TAILQ_INSERT_TAIL(preferred, id, next);
1650 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001651 authctxt->agent_fd = agent_fd;
Damien Miller62cee002000-09-23 17:15:56 +11001652 }
djm@openbsd.org82f24c32016-05-23 23:30:50 +00001653 /* Prefer PKCS11 keys that are explicitly listed */
1654 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1655 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
1656 continue;
1657 found = 0;
1658 TAILQ_FOREACH(id2, &files, next) {
1659 if (id2->key == NULL ||
1660 (id2->key->flags & SSHKEY_FLAG_EXT) == 0)
1661 continue;
1662 if (sshkey_equal(id->key, id2->key)) {
1663 TAILQ_REMOVE(&files, id, next);
1664 TAILQ_INSERT_TAIL(preferred, id, next);
1665 found = 1;
1666 break;
1667 }
1668 }
1669 /* If IdentitiesOnly set and key not found then don't use it */
1670 if (!found && options.identities_only) {
1671 TAILQ_REMOVE(&files, id, next);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001672 freezero(id, sizeof(*id));
djm@openbsd.org82f24c32016-05-23 23:30:50 +00001673 }
1674 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001675 /* append remaining keys from the config file */
1676 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1677 TAILQ_REMOVE(&files, id, next);
1678 TAILQ_INSERT_TAIL(preferred, id, next);
1679 }
djm@openbsd.orge679c092015-10-13 16:15:21 +00001680 /* finally, filter by PubkeyAcceptedKeyTypes */
1681 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
djm@openbsd.orgd78b75d2018-07-03 13:07:58 +00001682 if (id->key != NULL && !key_type_allowed_by_config(id->key)) {
djm@openbsd.orge679c092015-10-13 16:15:21 +00001683 debug("Skipping %s key %s - "
1684 "not in PubkeyAcceptedKeyTypes",
1685 sshkey_ssh_name(id->key), id->filename);
1686 TAILQ_REMOVE(preferred, id, next);
1687 sshkey_free(id->key);
1688 free(id->filename);
1689 memset(id, 0, sizeof(*id));
1690 continue;
1691 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001692 }
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001693 /* List the keys we plan on using */
1694 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1695 ident = format_identity(id);
1696 debug("Will attempt key: %s", ident);
1697 free(ident);
1698 }
1699 debug2("%s: done", __func__);
Damien Miller280ecfb2003-05-14 13:46:00 +10001700}
1701
1702static void
djm@openbsd.org38e83e42019-02-12 23:53:10 +00001703pubkey_cleanup(struct ssh *ssh)
Damien Miller280ecfb2003-05-14 13:46:00 +10001704{
djm@openbsd.org5c68ea82019-02-11 09:44:42 +00001705 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
Damien Miller280ecfb2003-05-14 13:46:00 +10001706 Identity *id;
1707
djm@openbsd.orgaaed6352018-11-16 02:46:20 +00001708 if (authctxt->agent_fd != -1) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001709 ssh_close_authentication_socket(authctxt->agent_fd);
djm@openbsd.orgaaed6352018-11-16 02:46:20 +00001710 authctxt->agent_fd = -1;
1711 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001712 for (id = TAILQ_FIRST(&authctxt->keys); id;
1713 id = TAILQ_FIRST(&authctxt->keys)) {
1714 TAILQ_REMOVE(&authctxt->keys, id, next);
mmcc@openbsd.org89540b62015-12-11 02:31:47 +00001715 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +10001716 free(id->filename);
1717 free(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001718 }
Damien Millereba71ba2000-04-29 23:57:08 +10001719}
1720
djm@openbsd.orgb9844a42016-12-04 23:54:02 +00001721static void
1722pubkey_reset(Authctxt *authctxt)
1723{
1724 Identity *id;
1725
1726 TAILQ_FOREACH(id, &authctxt->keys, next)
1727 id->tried = 0;
1728}
1729
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001730static int
1731try_identity(Identity *id)
1732{
1733 if (!id->key)
1734 return (0);
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001735 if (sshkey_type_plain(id->key->type) == KEY_RSA &&
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001736 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1737 debug("Skipped %s key %s for RSA/MD5 server",
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001738 sshkey_type(id->key), id->filename);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001739 return (0);
1740 }
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001741 return 1;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001742}
1743
djm@openbsd.org5c68ea82019-02-11 09:44:42 +00001744static int
djm@openbsd.org64c95982019-01-19 21:40:21 +00001745userauth_pubkey(struct ssh *ssh)
Damien Millereba71ba2000-04-29 23:57:08 +10001746{
djm@openbsd.org64c95982019-01-19 21:40:21 +00001747 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
Damien Miller280ecfb2003-05-14 13:46:00 +10001748 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001749 int sent = 0;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001750 char *ident;
Damien Millereba71ba2000-04-29 23:57:08 +10001751
Damien Miller280ecfb2003-05-14 13:46:00 +10001752 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1753 if (id->tried++)
1754 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001755 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001756 TAILQ_REMOVE(&authctxt->keys, id, next);
1757 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1758 /*
1759 * send a test message if we have the public key. for
1760 * encrypted keys we cannot do this and have to load the
1761 * private key instead
1762 */
Damien Miller324541e2013-12-31 12:25:40 +11001763 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001764 if (try_identity(id)) {
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001765 ident = format_identity(id);
1766 debug("Offering public key: %s", ident);
1767 free(ident);
djm@openbsd.org64c95982019-01-19 21:40:21 +00001768 sent = send_pubkey_test(ssh, id);
Damien Miller324541e2013-12-31 12:25:40 +11001769 }
1770 } else {
Damien Miller280ecfb2003-05-14 13:46:00 +10001771 debug("Trying private key: %s", id->filename);
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001772 id->key = load_identity_file(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001773 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001774 if (try_identity(id)) {
1775 id->isprivate = 1;
djm@openbsd.org64c95982019-01-19 21:40:21 +00001776 sent = sign_and_send_pubkey(ssh, id);
Damien Miller324541e2013-12-31 12:25:40 +11001777 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001778 sshkey_free(id->key);
Damien Miller280ecfb2003-05-14 13:46:00 +10001779 id->key = NULL;
djm@openbsd.orgb9844a42016-12-04 23:54:02 +00001780 id->isprivate = 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001781 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001782 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001783 if (sent)
1784 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001785 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001786 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001787}
Damien Millereba71ba2000-04-29 23:57:08 +10001788
Damien Miller874d77b2000-10-14 16:23:11 +11001789/*
1790 * Send userauth request message specifying keyboard-interactive method.
1791 */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +00001792static int
djm@openbsd.org64c95982019-01-19 21:40:21 +00001793userauth_kbdint(struct ssh *ssh)
Damien Miller874d77b2000-10-14 16:23:11 +11001794{
djm@openbsd.org64c95982019-01-19 21:40:21 +00001795 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001796 int r;
Damien Miller874d77b2000-10-14 16:23:11 +11001797
djm@openbsd.org4a526942019-01-04 03:27:50 +00001798 if (authctxt->attempt_kbdint++ >= options.number_of_password_prompts)
Damien Miller874d77b2000-10-14 16:23:11 +11001799 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001800 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
djm@openbsd.org4a526942019-01-04 03:27:50 +00001801 if (authctxt->attempt_kbdint > 1 && !authctxt->info_req_seen) {
Ben Lindstrom7d199962001-09-12 18:29:00 +00001802 debug3("userauth_kbdint: disable: no info_req_seen");
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001803 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
Ben Lindstrom7d199962001-09-12 18:29:00 +00001804 return 0;
1805 }
Damien Miller874d77b2000-10-14 16:23:11 +11001806
1807 debug2("userauth_kbdint");
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001808 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1809 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1810 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1811 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1812 (r = sshpkt_put_cstring(ssh, "")) != 0 || /* lang */
1813 (r = sshpkt_put_cstring(ssh, options.kbd_interactive_devices ?
1814 options.kbd_interactive_devices : "")) != 0 ||
1815 (r = sshpkt_send(ssh)) != 0)
1816 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +11001817
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001818 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
Damien Miller874d77b2000-10-14 16:23:11 +11001819 return 1;
1820}
1821
1822/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001823 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001824 */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +00001825static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001826input_userauth_info_req(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller874d77b2000-10-14 16:23:11 +11001827{
markus@openbsd.org94583be2017-05-30 14:19:15 +00001828 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001829 char *name = NULL, *inst = NULL, *lang = NULL, *prompt = NULL;
1830 char *response = NULL;
1831 u_char echo = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001832 u_int num_prompts, i;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001833 int r;
Damien Miller874d77b2000-10-14 16:23:11 +11001834
1835 debug2("input_userauth_info_req");
1836
1837 if (authctxt == NULL)
1838 fatal("input_userauth_info_req: no authentication context");
1839
Ben Lindstrom7d199962001-09-12 18:29:00 +00001840 authctxt->info_req_seen = 1;
1841
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001842 if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 ||
1843 (r = sshpkt_get_cstring(ssh, &inst, NULL)) != 0 ||
1844 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1845 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +11001846 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001847 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001848 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001849 logit("%s", inst);
Damien Miller874d77b2000-10-14 16:23:11 +11001850
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001851 if ((r = sshpkt_get_u32(ssh, &num_prompts)) != 0)
1852 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +11001853 /*
1854 * Begin to build info response packet based on prompts requested.
1855 * We commit to providing the correct number of responses, so if
1856 * further on we run into a problem that prevents this, we have to
1857 * be sure and clean this up and send a correct error response.
1858 */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001859 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE)) != 0 ||
1860 (r = sshpkt_put_u32(ssh, num_prompts)) != 0)
1861 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +11001862
Ben Lindstrom551ea372001-06-05 18:56:16 +00001863 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001864 for (i = 0; i < num_prompts; i++) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001865 if ((r = sshpkt_get_cstring(ssh, &prompt, NULL)) != 0 ||
1866 (r = sshpkt_get_u8(ssh, &echo)) != 0)
1867 goto out;
Ben Lindstrom949974b2001-06-25 05:20:31 +00001868 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001869 if ((r = sshpkt_put_cstring(ssh, response)) != 0)
1870 goto out;
1871 freezero(response, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +10001872 free(prompt);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001873 response = prompt = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001874 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001875 /* done with parsing incoming message. */
1876 if ((r = sshpkt_get_end(ssh)) != 0 ||
1877 (r = sshpkt_add_padding(ssh, 64)) != 0)
1878 goto out;
1879 r = sshpkt_send(ssh);
1880 out:
1881 if (response)
1882 freezero(response, strlen(response));
1883 free(prompt);
1884 free(name);
1885 free(inst);
1886 free(lang);
1887 return r;
Damien Miller874d77b2000-10-14 16:23:11 +11001888}
Damien Miller62cee002000-09-23 17:15:56 +11001889
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001890static int
djm@openbsd.orged1df722019-01-19 21:34:45 +00001891ssh_keysign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001892 const u_char *data, size_t datalen)
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001893{
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001894 struct sshbuf *b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001895 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001896 pid_t pid;
deraadt@openbsd.orga1d29cc2019-05-15 04:43:31 +00001897 int r, to[2], from[2], status;
djm@openbsd.org64c95982019-01-19 21:40:21 +00001898 int sock = ssh_packet_get_connection_in(ssh);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001899 u_char rversion = 0, version = 2;
1900 void (*osigchld)(int);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001901
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001902 *sigp = NULL;
1903 *lenp = 0;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001904
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001905 if (stat(_PATH_SSH_KEY_SIGN, &st) == -1) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001906 error("%s: not installed: %s", __func__, strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001907 return -1;
1908 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001909 if (fflush(stdout) != 0) {
1910 error("%s: fflush: %s", __func__, strerror(errno));
1911 return -1;
1912 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001913 if (pipe(to) == -1) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001914 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001915 return -1;
1916 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001917 if (pipe(from) == -1) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001918 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001919 return -1;
1920 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001921 if ((pid = fork()) == -1) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001922 error("%s: fork: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001923 return -1;
1924 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001925 osigchld = signal(SIGCHLD, SIG_DFL);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001926 if (pid == 0) {
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001927 close(from[0]);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001928 if (dup2(from[1], STDOUT_FILENO) == -1)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001929 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001930 close(to[1]);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001931 if (dup2(to[0], STDIN_FILENO) == -1)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001932 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001933 close(from[1]);
1934 close(to[0]);
deraadt@openbsd.orga1d29cc2019-05-15 04:43:31 +00001935
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001936 if (dup2(sock, STDERR_FILENO + 1) == -1)
deraadt@openbsd.orga1d29cc2019-05-15 04:43:31 +00001937 fatal("%s: dup2: %s", __func__, strerror(errno));
1938 sock = STDERR_FILENO + 1;
1939 fcntl(sock, F_SETFD, 0); /* keep the socket on exec */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001940 closefrom(sock + 1);
deraadt@openbsd.orga1d29cc2019-05-15 04:43:31 +00001941
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001942 debug3("%s: [child] pid=%ld, exec %s",
1943 __func__, (long)getpid(), _PATH_SSH_KEY_SIGN);
mmcc@openbsd.org94141b72015-12-11 00:20:04 +00001944 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001945 fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001946 strerror(errno));
1947 }
1948 close(from[1]);
1949 close(to[0]);
djm@openbsd.orgc586d2d2019-05-31 03:20:07 +00001950 sock = STDERR_FILENO + 1;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001951
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001952 if ((b = sshbuf_new()) == NULL)
1953 fatal("%s: sshbuf_new failed", __func__);
1954 /* send # of sock, data to be signed */
djm@openbsd.org326e2fa2017-01-30 00:32:28 +00001955 if ((r = sshbuf_put_u32(b, sock)) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001956 (r = sshbuf_put_string(b, data, datalen)) != 0)
1957 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1958 if (ssh_msg_send(to[1], version, b) == -1)
1959 fatal("%s: couldn't send request", __func__);
1960 sshbuf_reset(b);
1961 r = ssh_msg_recv(from[0], b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001962 close(from[0]);
1963 close(to[1]);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001964 if (r < 0) {
1965 error("%s: no reply", __func__);
1966 goto fail;
1967 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001968
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001969 errno = 0;
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001970 while (waitpid(pid, &status, 0) == -1) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001971 if (errno != EINTR) {
1972 error("%s: waitpid %ld: %s",
1973 __func__, (long)pid, strerror(errno));
1974 goto fail;
1975 }
1976 }
1977 if (!WIFEXITED(status)) {
1978 error("%s: exited abnormally", __func__);
1979 goto fail;
1980 }
1981 if (WEXITSTATUS(status) != 0) {
1982 error("%s: exited with status %d",
1983 __func__, WEXITSTATUS(status));
1984 goto fail;
1985 }
1986 if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
1987 error("%s: buffer error: %s", __func__, ssh_err(r));
1988 goto fail;
1989 }
1990 if (rversion != version) {
1991 error("%s: bad version", __func__);
1992 goto fail;
1993 }
1994 if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
1995 error("%s: buffer error: %s", __func__, ssh_err(r));
1996 fail:
1997 signal(SIGCHLD, osigchld);
1998 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001999 return -1;
2000 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002001 signal(SIGCHLD, osigchld);
2002 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00002003
Ben Lindstrom1bad2562002-06-06 19:57:33 +00002004 return 0;
2005}
2006
djm@openbsd.org5c68ea82019-02-11 09:44:42 +00002007static int
djm@openbsd.org64c95982019-01-19 21:40:21 +00002008userauth_hostbased(struct ssh *ssh)
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002009{
djm@openbsd.org64c95982019-01-19 21:40:21 +00002010 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002011 struct sshkey *private = NULL;
2012 struct sshbuf *b = NULL;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002013 u_char *sig = NULL, *keyblob = NULL;
2014 char *fp = NULL, *chost = NULL, *lname = NULL;
2015 size_t siglen = 0, keylen = 0;
2016 int i, r, success = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002017
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002018 if (authctxt->ktypes == NULL) {
2019 authctxt->oktypes = xstrdup(options.hostbased_key_types);
2020 authctxt->ktypes = authctxt->oktypes;
2021 }
djm@openbsd.org1195f4c2015-01-08 10:14:08 +00002022
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002023 /*
2024 * Work through each listed type pattern in HostbasedKeyTypes,
2025 * trying each hostkey that matches the type in turn.
2026 */
2027 for (;;) {
2028 if (authctxt->active_ktype == NULL)
2029 authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
2030 if (authctxt->active_ktype == NULL ||
2031 *authctxt->active_ktype == '\0')
2032 break;
2033 debug3("%s: trying key type %s", __func__,
2034 authctxt->active_ktype);
2035
2036 /* check for a useful key */
2037 private = NULL;
2038 for (i = 0; i < authctxt->sensitive->nkeys; i++) {
2039 if (authctxt->sensitive->keys[i] == NULL ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002040 authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
2041 continue;
2042 if (match_pattern_list(
2043 sshkey_ssh_name(authctxt->sensitive->keys[i]),
djm@openbsd.orge661a862015-05-04 06:10:48 +00002044 authctxt->active_ktype, 0) != 1)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002045 continue;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002046 /* we take and free the key */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002047 private = authctxt->sensitive->keys[i];
2048 authctxt->sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002049 break;
2050 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002051 /* Found one */
2052 if (private != NULL)
2053 break;
2054 /* No more keys of this type; advance */
2055 authctxt->active_ktype = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002056 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002057 if (private == NULL) {
2058 free(authctxt->oktypes);
2059 authctxt->oktypes = authctxt->ktypes = NULL;
2060 authctxt->active_ktype = NULL;
Ben Lindstrom1d568f92002-12-23 02:44:36 +00002061 debug("No more client hostkeys for hostbased authentication.");
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002062 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002063 }
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00002064
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002065 if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
2066 SSH_FP_DEFAULT)) == NULL) {
2067 error("%s: sshkey_fingerprint failed", __func__);
2068 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002069 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002070 debug("%s: trying hostkey %s %s",
2071 __func__, sshkey_ssh_name(private), fp);
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00002072
Damien Miller91c18472001-11-12 11:02:03 +11002073 /* figure out a name for the client host */
djm@openbsd.org64c95982019-01-19 21:40:21 +00002074 lname = get_local_name(ssh_packet_get_connection_in(ssh));
2075 if (lname == NULL) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002076 error("%s: cannot get local ipaddr/name", __func__);
2077 goto out;
Damien Miller91c18472001-11-12 11:02:03 +11002078 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002079
2080 /* XXX sshbuf_put_stringf? */
2081 xasprintf(&chost, "%s.", lname);
2082 debug2("%s: chost %s", __func__, chost);
Damien Miller91c18472001-11-12 11:02:03 +11002083
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002084 /* construct data */
2085 if ((b = sshbuf_new()) == NULL) {
2086 error("%s: sshbuf_new failed", __func__);
2087 goto out;
2088 }
2089 if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
2090 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
2091 goto out;
2092 }
2093 if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
2094 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2095 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +00002096 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002097 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002098 (r = sshbuf_put_cstring(b, sshkey_ssh_name(private))) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002099 (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
2100 (r = sshbuf_put_cstring(b, chost)) != 0 ||
2101 (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
2102 error("%s: buffer error: %s", __func__, ssh_err(r));
2103 goto out;
2104 }
2105
2106#ifdef DEBUG_PK
2107 sshbuf_dump(b, stderr);
2108#endif
djm@openbsd.orged1df722019-01-19 21:34:45 +00002109 if ((r = ssh_keysign(ssh, private, &sig, &siglen,
2110 sshbuf_ptr(b), sshbuf_len(b))) != 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002111 error("sign using hostkey %s %s failed",
2112 sshkey_ssh_name(private), fp);
2113 goto out;
2114 }
2115 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2116 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
2117 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
2118 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002119 (r = sshpkt_put_cstring(ssh, sshkey_ssh_name(private))) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002120 (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
2121 (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
2122 (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
2123 (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
2124 (r = sshpkt_send(ssh)) != 0) {
2125 error("%s: packet error: %s", __func__, ssh_err(r));
2126 goto out;
2127 }
2128 success = 1;
2129
2130 out:
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002131 if (sig != NULL)
2132 freezero(sig, siglen);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002133 free(keyblob);
2134 free(lname);
2135 free(fp);
2136 free(chost);
2137 sshkey_free(private);
2138 sshbuf_free(b);
2139
2140 return success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002141}
2142
Damien Miller62cee002000-09-23 17:15:56 +11002143/* find auth method */
2144
Damien Miller62cee002000-09-23 17:15:56 +11002145/*
2146 * given auth method name, if configurable options permit this method fill
2147 * in auth_ident field and return true, otherwise return false.
2148 */
Ben Lindstrombba81212001-06-25 05:01:22 +00002149static int
Damien Miller62cee002000-09-23 17:15:56 +11002150authmethod_is_enabled(Authmethod *method)
2151{
2152 if (method == NULL)
2153 return 0;
2154 /* return false if options indicate this method is disabled */
2155 if (method->enabled == NULL || *method->enabled == 0)
2156 return 0;
2157 /* return false if batch mode is enabled but method needs interactive mode */
2158 if (method->batch_flag != NULL && *method->batch_flag != 0)
2159 return 0;
2160 return 1;
2161}
2162
Ben Lindstrombba81212001-06-25 05:01:22 +00002163static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11002164authmethod_lookup(const char *name)
2165{
2166 Authmethod *method = NULL;
2167 if (name != NULL)
2168 for (method = authmethods; method->name != NULL; method++)
2169 if (strcmp(name, method->name) == 0)
2170 return method;
2171 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
2172 return NULL;
2173}
2174
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002175/* XXX internal state */
2176static Authmethod *current = NULL;
2177static char *supported = NULL;
2178static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00002179
Damien Miller62cee002000-09-23 17:15:56 +11002180/*
2181 * Given the authentication method list sent by the server, return the
2182 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00002183 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00002184 */
Ben Lindstrombba81212001-06-25 05:01:22 +00002185static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11002186authmethod_get(char *authlist)
2187{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002188 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00002189 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00002190
Damien Miller62cee002000-09-23 17:15:56 +11002191 /* Use a suitable default if we're passed a nil list. */
2192 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002193 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11002194
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002195 if (supported == NULL || strcmp(authlist, supported) != 0) {
2196 debug3("start over, passed a different list %s", authlist);
Darren Tuckera627d422013-06-02 07:31:17 +10002197 free(supported);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002198 supported = xstrdup(authlist);
2199 preferred = options.preferred_authentications;
2200 debug3("preferred %s", preferred);
2201 current = NULL;
2202 } else if (current != NULL && authmethod_is_enabled(current))
2203 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10002204
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002205 for (;;) {
2206 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00002207 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002208 current = NULL;
2209 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11002210 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002211 preferred += next;
2212 debug3("authmethod_lookup %s", name);
2213 debug3("remaining preferred: %s", preferred);
2214 if ((current = authmethod_lookup(name)) != NULL &&
2215 authmethod_is_enabled(current)) {
2216 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00002217 debug("Next authentication method: %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002218 free(name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002219 return current;
2220 }
Darren Tuckere52a2602013-06-06 08:22:05 +10002221 free(name);
Damien Millereba71ba2000-04-29 23:57:08 +10002222 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002223}
Damien Miller62cee002000-09-23 17:15:56 +11002224
Ben Lindstrom79073822001-07-04 03:42:30 +00002225static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002226authmethods_get(void)
2227{
2228 Authmethod *method = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002229 struct sshbuf *b;
Damien Miller0e3b8722002-01-22 23:26:38 +11002230 char *list;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002231 int r;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002232
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002233 if ((b = sshbuf_new()) == NULL)
2234 fatal("%s: sshbuf_new failed", __func__);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002235 for (method = authmethods; method->name != NULL; method++) {
2236 if (authmethod_is_enabled(method)) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002237 if ((r = sshbuf_putf(b, "%s%s",
2238 sshbuf_len(b) ? "," : "", method->name)) != 0)
2239 fatal("%s: buffer error: %s",
2240 __func__, ssh_err(r));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002241 }
Damien Miller62cee002000-09-23 17:15:56 +11002242 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002243 if ((list = sshbuf_dup_string(b)) == NULL)
djm@openbsd.org1a31d022016-05-02 08:49:03 +00002244 fatal("%s: sshbuf_dup_string failed", __func__);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002245 sshbuf_free(b);
Damien Miller0e3b8722002-01-22 23:26:38 +11002246 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10002247}