blob: 87fa70a40cf2204248416faeb07214dca54016a7 [file] [log] [blame]
dtucker@openbsd.org6b39a7b2019-08-05 11:50:33 +00001/* $OpenBSD: sshconnect2.c,v 1.308 2019/08/05 11:50:33 dtucker Exp $ */
Damien Millereba71ba2000-04-29 23:57:08 +10002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Miller01ed2272008-11-05 16:20:46 +11004 * Copyright (c) 2008 Damien Miller. All rights reserved.
Damien Millereba71ba2000-04-29 23:57:08 +10005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100014 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
Damien Miller2eb63402006-03-15 11:09:42 +110028
Damien Miller9cf6d072006-03-15 11:29:24 +110029#include <sys/types.h>
Damien Millerd7834352006-08-05 12:39:39 +100030#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110031#include <sys/wait.h>
Damien Millerf17883e2006-03-15 11:45:54 +110032#include <sys/stat.h>
Damien Millereba71ba2000-04-29 23:57:08 +100033
Darren Tucker39972492006-07-12 22:22:46 +100034#include <errno.h>
Darren Tucker6e7fe1c2010-01-08 17:07:22 +110035#include <fcntl.h>
Darren Tuckerf520ea12007-05-20 15:11:33 +100036#include <netdb.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <pwd.h>
38#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100039#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100040#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100041#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100042#include <unistd.h>
Damien Miller63b4bcd2013-03-20 12:55:14 +110043#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
Damien Miller7ba0ca72008-07-17 18:57:06 +100044#include <vis.h>
Damien Miller2e28d862008-07-17 19:15:43 +100045#endif
Darren Tucker39972492006-07-12 22:22:46 +100046
Damien Miller9c617692003-05-14 14:31:11 +100047#include "openbsd-compat/sys-queue.h"
48
Damien Millerd7834352006-08-05 12:39:39 +100049#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100050#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "ssh2.h"
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"
Damien Miller874d77b2000-10-14 16:23:11 +110075
Darren Tucker0efd1552003-08-26 11:49:55 +100076#ifdef GSSAPI
77#include "ssh-gss.h"
78#endif
79
Damien Millereba71ba2000-04-29 23:57:08 +100080/* import */
81extern char *client_version_string;
82extern char *server_version_string;
83extern Options options;
84
85/*
86 * SSH2 key exchange
87 */
88
Ben Lindstrom46c16222000-12-22 01:43:59 +000089u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +100090u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100091
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000092char *xxx_host;
93struct sockaddr *xxx_hostaddr;
94
Ben Lindstrombba81212001-06-25 05:01:22 +000095static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +000096verify_host_key_callback(struct sshkey *hostkey, struct ssh *ssh)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000097{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000098 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +100099 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000100 return 0;
101}
102
Damien Millerd925dcd2010-12-01 12:21:51 +1100103static char *
104order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
105{
106 char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
107 size_t maxlen;
108 struct hostkeys *hostkeys;
109 int ktype;
Damien Miller295ee632011-05-29 21:42:31 +1000110 u_int i;
Damien Millerd925dcd2010-12-01 12:21:51 +1100111
112 /* Find all hostkeys for this hostname */
113 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
114 hostkeys = init_hostkeys();
Damien Miller295ee632011-05-29 21:42:31 +1000115 for (i = 0; i < options.num_user_hostfiles; i++)
116 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
117 for (i = 0; i < options.num_system_hostfiles; i++)
118 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
Damien Millerd925dcd2010-12-01 12:21:51 +1100119
120 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
121 maxlen = strlen(avail) + 1;
122 first = xmalloc(maxlen);
123 last = xmalloc(maxlen);
124 *first = *last = '\0';
125
126#define ALG_APPEND(to, from) \
127 do { \
128 if (*to != '\0') \
129 strlcat(to, ",", maxlen); \
130 strlcat(to, from, maxlen); \
131 } while (0)
132
133 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000134 if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
Damien Millerd925dcd2010-12-01 12:21:51 +1100135 fatal("%s: unknown alg %s", __func__, alg);
136 if (lookup_key_in_hostkeys_by_type(hostkeys,
djm@openbsd.org141efe42015-01-14 20:05:27 +0000137 sshkey_type_plain(ktype), NULL))
Damien Millerd925dcd2010-12-01 12:21:51 +1100138 ALG_APPEND(first, alg);
139 else
140 ALG_APPEND(last, alg);
141 }
142#undef ALG_APPEND
djm@openbsd.org35d60222015-01-18 13:33:34 +0000143 xasprintf(&ret, "%s%s%s", first,
144 (*first == '\0' || *last == '\0') ? "" : ",", last);
Damien Millerd925dcd2010-12-01 12:21:51 +1100145 if (*first != '\0')
146 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
147
Darren Tuckera627d422013-06-02 07:31:17 +1000148 free(first);
149 free(last);
150 free(hostname);
151 free(oavail);
Damien Millerd925dcd2010-12-01 12:21:51 +1100152 free_hostkeys(hostkeys);
153
154 return ret;
155}
156
Damien Millereba71ba2000-04-29 23:57:08 +1000157void
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000158ssh_kex2(struct ssh *ssh, char *host, struct sockaddr *hostaddr, u_short port)
Damien Miller874d77b2000-10-14 16:23:11 +1100159{
Damien Miller9235a032014-04-20 13:17:20 +1000160 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
djm@openbsd.org312d2f22018-07-04 13:49:31 +0000161 char *s, *all_key;
markus@openbsd.org57e783c2015-01-20 20:16:21 +0000162 int r;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000163
164 xxx_host = host;
165 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100166
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000167 if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
168 fatal("%s: kex_names_cat", __func__);
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000169 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(s);
Damien Millera0ff4662001-03-30 10:49:35 +1000170 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000171 compat_cipher_proposal(options.ciphers);
Damien Millera0ff4662001-03-30 10:49:35 +1000172 myproposal[PROPOSAL_ENC_ALGS_STOC] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000173 compat_cipher_proposal(options.ciphers);
dtucker@openbsd.org8c02e362016-05-24 04:43:45 +0000174 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
175 myproposal[PROPOSAL_COMP_ALGS_STOC] = options.compression ?
sf@openbsd.org168b46f2018-07-09 13:37:10 +0000176 "zlib@openssh.com,zlib,none" : "none,zlib@openssh.com,zlib";
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000177 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
178 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
179 if (options.hostkeyalgorithms != NULL) {
djm@openbsd.org312d2f22018-07-04 13:49:31 +0000180 all_key = sshkey_alg_list(0, 0, 1, ',');
181 if (kex_assemble_names(&options.hostkeyalgorithms,
182 KEX_DEFAULT_PK_ALG, all_key) != 0)
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000183 fatal("%s: kex_assemble_namelist", __func__);
djm@openbsd.org312d2f22018-07-04 13:49:31 +0000184 free(all_key);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100185 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000186 compat_pkalg_proposal(options.hostkeyalgorithms);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000187 } else {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000188 /* Enforce default */
189 options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG);
Damien Millerd925dcd2010-12-01 12:21:51 +1100190 /* Prefer algorithms that we already have keys for */
191 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100192 compat_pkalg_proposal(
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000193 order_hostkeyalgs(host, hostaddr, port));
Damien Millerd925dcd2010-12-01 12:21:51 +1100194 }
Damien Miller874d77b2000-10-14 16:23:11 +1100195
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000196 if (options.rekey_limit || options.rekey_interval)
djm@openbsd.orged1df722019-01-19 21:34:45 +0000197 ssh_packet_set_rekey_limits(ssh, options.rekey_limit,
dtucker@openbsd.orgc998bf02017-02-03 02:56:00 +0000198 options.rekey_interval);
Damien Millera5539d22003-04-09 20:50:06 +1000199
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000200 /* start key exchange */
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000201 if ((r = kex_setup(ssh, myproposal)) != 0)
markus@openbsd.org57e783c2015-01-20 20:16:21 +0000202 fatal("kex_setup: %s", ssh_err(r));
Damien Miller1f0311c2014-05-15 14:24:09 +1000203#ifdef WITH_OPENSSL
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000204 ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client;
205 ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client;
206 ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client;
207 ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client;
208 ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000209 ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
210 ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100211# ifdef OPENSSL_HAS_ECC
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000212 ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100213# endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000214#endif
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +0000215 ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client;
216 ssh->kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_client;
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000217 ssh->kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100218
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000219 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &ssh->kex->done);
Damien Miller874d77b2000-10-14 16:23:11 +1100220
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000221 /* remove ext-info from the KEX proposals for rekeying */
222 myproposal[PROPOSAL_KEX_ALGS] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000223 compat_kex_proposal(options.kex_algorithms);
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000224 if ((r = kex_prop2buf(ssh->kex->my, myproposal)) != 0)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000225 fatal("kex_prop2buf: %s", ssh_err(r));
Darren Tucker36331b52010-01-08 16:50:41 +1100226
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000227 session_id2 = ssh->kex->session_id;
228 session_id2_len = ssh->kex->session_id_len;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000229
Damien Miller874d77b2000-10-14 16:23:11 +1100230#ifdef DEBUG_KEXDH
231 /* send 1st encrypted/maced/compressed message */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000232 if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 ||
233 (r = sshpkt_put_cstring(ssh, "markus")) != 0 ||
markus@openbsd.org394a8422018-07-11 18:55:11 +0000234 (r = sshpkt_send(ssh)) != 0 ||
235 (r = ssh_packet_write_wait(ssh)) != 0)
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000236 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100237#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000238}
Damien Millerb1715dc2000-05-30 13:44:51 +1000239
Damien Millereba71ba2000-04-29 23:57:08 +1000240/*
241 * Authenticate user
242 */
Damien Miller62cee002000-09-23 17:15:56 +1100243
djm@openbsd.org141efe42015-01-14 20:05:27 +0000244typedef struct cauthctxt Authctxt;
245typedef struct cauthmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000246typedef struct identity Identity;
247typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100248
Damien Miller280ecfb2003-05-14 13:46:00 +1000249struct identity {
250 TAILQ_ENTRY(identity) next;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000251 int agent_fd; /* >=0 if agent supports key */
252 struct sshkey *key; /* public/private key */
Damien Miller280ecfb2003-05-14 13:46:00 +1000253 char *filename; /* comment for agent-only keys */
254 int tried;
255 int isprivate; /* key points to the private key */
Damien Miller5ceddc32013-02-15 12:18:32 +1100256 int userprovided;
Damien Miller280ecfb2003-05-14 13:46:00 +1000257};
258TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100259
djm@openbsd.org141efe42015-01-14 20:05:27 +0000260struct cauthctxt {
Damien Miller62cee002000-09-23 17:15:56 +1100261 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000262 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100263 const char *host;
264 const char *service;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000265 struct cauthmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000266 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000267 char *authlist;
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000268#ifdef GSSAPI
269 /* gssapi */
270 gss_OID_set gss_supported_mechs;
271 u_int mech_tried;
272#endif
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000273 /* pubkey */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000274 struct idlist keys;
275 int agent_fd;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000276 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000277 Sensitive *sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000278 char *oktypes, *ktypes;
279 const char *active_ktype;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000280 /* kbd-interactive */
281 int info_req_seen;
djm@openbsd.org4a526942019-01-04 03:27:50 +0000282 int attempt_kbdint;
283 /* password */
284 int attempt_passwd;
Darren Tucker0efd1552003-08-26 11:49:55 +1000285 /* generic */
286 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100287};
djm@openbsd.org141efe42015-01-14 20:05:27 +0000288
289struct cauthmethod {
Damien Miller62cee002000-09-23 17:15:56 +1100290 char *name; /* string to compare against server's list */
djm@openbsd.org64c95982019-01-19 21:40:21 +0000291 int (*userauth)(struct ssh *ssh);
292 void (*cleanup)(struct ssh *ssh);
Damien Miller62cee002000-09-23 17:15:56 +1100293 int *enabled; /* flag in option struct that enables method */
294 int *batch_flag; /* flag in option struct that disables method */
295};
296
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000297static int input_userauth_service_accept(int, u_int32_t, struct ssh *);
298static int input_userauth_ext_info(int, u_int32_t, struct ssh *);
299static int input_userauth_success(int, u_int32_t, struct ssh *);
300static int input_userauth_failure(int, u_int32_t, struct ssh *);
301static int input_userauth_banner(int, u_int32_t, struct ssh *);
302static int input_userauth_error(int, u_int32_t, struct ssh *);
303static int input_userauth_info_req(int, u_int32_t, struct ssh *);
304static int input_userauth_pk_ok(int, u_int32_t, struct ssh *);
305static int input_userauth_passwd_changereq(int, u_int32_t, struct ssh *);
Damien Miller874d77b2000-10-14 16:23:11 +1100306
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000307static int userauth_none(struct ssh *);
308static int userauth_pubkey(struct ssh *);
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000309static int userauth_passwd(struct ssh *);
310static int userauth_kbdint(struct ssh *);
311static int userauth_hostbased(struct ssh *);
Damien Miller62cee002000-09-23 17:15:56 +1100312
Darren Tucker0efd1552003-08-26 11:49:55 +1000313#ifdef GSSAPI
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000314static int userauth_gssapi(struct ssh *);
315static void userauth_gssapi_cleanup(struct ssh *);
316static int input_gssapi_response(int type, u_int32_t, struct ssh *);
317static int input_gssapi_token(int type, u_int32_t, struct ssh *);
318static int input_gssapi_error(int, u_int32_t, struct ssh *);
319static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000320#endif
321
djm@openbsd.org64c95982019-01-19 21:40:21 +0000322void userauth(struct ssh *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000323
djm@openbsd.org38e83e42019-02-12 23:53:10 +0000324static void pubkey_cleanup(struct ssh *);
djm@openbsd.org64c95982019-01-19 21:40:21 +0000325static int sign_and_send_pubkey(struct ssh *ssh, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000326static void pubkey_prepare(Authctxt *);
djm@openbsd.orgb9844a42016-12-04 23:54:02 +0000327static void pubkey_reset(Authctxt *);
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000328static struct sshkey *load_identity_file(Identity *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000329
Ben Lindstrombba81212001-06-25 05:01:22 +0000330static Authmethod *authmethod_get(char *authlist);
331static Authmethod *authmethod_lookup(const char *name);
332static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100333
334Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000335#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100336 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000337 userauth_gssapi,
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000338 userauth_gssapi_cleanup,
Darren Tucker0efd1552003-08-26 11:49:55 +1000339 &options.gss_authentication,
340 NULL},
341#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000342 {"hostbased",
343 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100344 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000345 &options.hostbased_authentication,
346 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000347 {"publickey",
348 userauth_pubkey,
djm@openbsd.org38e83e42019-02-12 23:53:10 +0000349 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000350 &options.pubkey_authentication,
351 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100352 {"keyboard-interactive",
353 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100354 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100355 &options.kbd_interactive_authentication,
356 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000357 {"password",
358 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100359 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000360 &options.password_authentication,
361 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100362 {"none",
363 userauth_none,
364 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100365 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100366 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100367 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100368};
369
370void
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000371ssh_userauth2(struct ssh *ssh, const char *local_user,
372 const char *server_user, char *host, Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100373{
374 Authctxt authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000375 int r;
Damien Miller62cee002000-09-23 17:15:56 +1100376
Ben Lindstrom551ea372001-06-05 18:56:16 +0000377 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000378 options.kbd_interactive_authentication = 1;
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000379 if (options.preferred_authentications == NULL)
380 options.preferred_authentications = authmethods_get();
381
Damien Miller62cee002000-09-23 17:15:56 +1100382 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000383 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller62cee002000-09-23 17:15:56 +1100384 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000385 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100386 authctxt.host = host;
387 authctxt.service = "ssh-connection"; /* service name */
388 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100389 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000390 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000391 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000392 authctxt.sensitive = sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000393 authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000394 authctxt.info_req_seen = 0;
djm@openbsd.org4a526942019-01-04 03:27:50 +0000395 authctxt.attempt_kbdint = 0;
396 authctxt.attempt_passwd = 0;
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000397#if GSSAPI
398 authctxt.gss_supported_mechs = NULL;
399 authctxt.mech_tried = 0;
400#endif
djm@openbsd.orgf14564c2015-01-15 11:04:36 +0000401 authctxt.agent_fd = -1;
djm@openbsd.orgaaed6352018-11-16 02:46:20 +0000402 pubkey_prepare(&authctxt);
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000403 if (authctxt.method == NULL) {
404 fatal("%s: internal error: cannot send userauth none request",
405 __func__);
406 }
Damien Miller62cee002000-09-23 17:15:56 +1100407
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000408 if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
409 (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
410 (r = sshpkt_send(ssh)) != 0)
411 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller62cee002000-09-23 17:15:56 +1100412
markus@openbsd.org94583be2017-05-30 14:19:15 +0000413 ssh->authctxt = &authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000414 ssh_dispatch_init(ssh, &input_userauth_error);
415 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
416 ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
markus@openbsd.org92e9fe62017-05-31 07:00:13 +0000417 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt.success); /* loop until success */
djm@openbsd.org38e83e42019-02-12 23:53:10 +0000418 pubkey_cleanup(ssh);
markus@openbsd.org94583be2017-05-30 14:19:15 +0000419 ssh->authctxt = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100420
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000421 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
Damien Millerf842fcb2003-05-15 12:01:28 +1000422
dtucker@openbsd.orgf31c6542016-09-22 02:29:57 +0000423 if (!authctxt.success)
424 fatal("Authentication failed.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000425 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100426}
Damien Millerf842fcb2003-05-15 12:01:28 +1000427
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000428/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000429static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000430input_userauth_service_accept(int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000431{
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000432 int r;
433
434 if (ssh_packet_remaining(ssh) > 0) {
435 char *reply;
436
437 if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
438 goto out;
439 debug2("service_accept: %s", reply);
440 free(reply);
441 } else {
442 debug2("buggy server: service_accept w/o service");
443 }
444 if ((r = sshpkt_get_end(ssh)) != 0)
445 goto out;
446 debug("SSH2_MSG_SERVICE_ACCEPT received");
447
448 /* initial userauth request */
djm@openbsd.org64c95982019-01-19 21:40:21 +0000449 userauth_none(ssh);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000450
451 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error);
452 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
453 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
454 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
455 r = 0;
456 out:
457 return r;
458}
459
460/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000461static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000462input_userauth_ext_info(int type, u_int32_t seqnr, struct ssh *ssh)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000463{
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000464 return kex_input_ext_info(type, seqnr, ssh);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000465}
466
Damien Miller62cee002000-09-23 17:15:56 +1100467void
djm@openbsd.org64c95982019-01-19 21:40:21 +0000468userauth(struct ssh *ssh, char *authlist)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000469{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000470 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000471
Damien Miller01ed2272008-11-05 16:20:46 +1100472 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
djm@openbsd.org64c95982019-01-19 21:40:21 +0000473 authctxt->method->cleanup(ssh);
Damien Miller01ed2272008-11-05 16:20:46 +1100474
Darren Tuckera627d422013-06-02 07:31:17 +1000475 free(authctxt->methoddata);
476 authctxt->methoddata = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000477 if (authlist == NULL) {
478 authlist = authctxt->authlist;
479 } else {
Darren Tuckera627d422013-06-02 07:31:17 +1000480 free(authctxt->authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000481 authctxt->authlist = authlist;
482 }
483 for (;;) {
484 Authmethod *method = authmethod_get(authlist);
485 if (method == NULL)
dtucker@openbsd.org4626e392017-06-14 00:31:38 +0000486 fatal("%s@%s: Permission denied (%s).",
487 authctxt->server_user, authctxt->host, authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000488 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000489
490 /* reset the per method handler */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000491 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_PER_METHOD_MIN,
Damien Millerf842fcb2003-05-15 12:01:28 +1000492 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
493
494 /* and try new method */
djm@openbsd.org64c95982019-01-19 21:40:21 +0000495 if (method->userauth(ssh) != 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000496 debug2("we sent a %s packet, wait for reply", method->name);
497 break;
498 } else {
499 debug2("we did not send a packet, disable method");
500 method->enabled = NULL;
501 }
502 }
503}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000504
Damien Millerf7475d72008-11-03 19:26:18 +1100505/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000506static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000507input_userauth_error(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100508{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000509 fatal("%s: bad message during authentication: type %d", __func__, type);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000510 return 0;
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000511}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000512
Damien Millerf7475d72008-11-03 19:26:18 +1100513/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000514static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000515input_userauth_banner(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000516{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000517 char *msg = NULL;
djm@openbsd.orged1df722019-01-19 21:34:45 +0000518 size_t len;
519 int r;
Darren Tucker79644822003-10-08 17:37:58 +1000520
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000521 debug3("%s", __func__);
djm@openbsd.orged1df722019-01-19 21:34:45 +0000522 if ((r = sshpkt_get_cstring(ssh, &msg, &len)) != 0 ||
djm@openbsd.org64c95982019-01-19 21:40:21 +0000523 (r = sshpkt_get_cstring(ssh, NULL, NULL)) != 0)
djm@openbsd.orged1df722019-01-19 21:34:45 +0000524 goto out;
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000525 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO)
526 fmprintf(stderr, "%s", msg);
djm@openbsd.orged1df722019-01-19 21:34:45 +0000527 r = 0;
528 out:
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000529 free(msg);
djm@openbsd.orged1df722019-01-19 21:34:45 +0000530 return r;
Damien Miller62cee002000-09-23 17:15:56 +1100531}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000532
Damien Millerf7475d72008-11-03 19:26:18 +1100533/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000534static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000535input_userauth_success(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100536{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000537 Authctxt *authctxt = ssh->authctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100538
Damien Miller62cee002000-09-23 17:15:56 +1100539 if (authctxt == NULL)
djm@openbsd.org64c95982019-01-19 21:40:21 +0000540 fatal("%s: no authentication context", __func__);
Darren Tuckera627d422013-06-02 07:31:17 +1000541 free(authctxt->authlist);
542 authctxt->authlist = NULL;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100543 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
djm@openbsd.org64c95982019-01-19 21:40:21 +0000544 authctxt->method->cleanup(ssh);
Darren Tuckera627d422013-06-02 07:31:17 +1000545 free(authctxt->methoddata);
546 authctxt->methoddata = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100547 authctxt->success = 1; /* break out */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000548 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100549}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000550
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000551#if 0
552static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000553input_userauth_success_unexpected(int type, u_int32_t seq, struct ssh *ssh)
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100554{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000555 Authctxt *authctxt = ssh->authctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100556
557 if (authctxt == NULL)
558 fatal("%s: no authentication context", __func__);
559
560 fatal("Unexpected authentication success during %s.",
561 authctxt->method->name);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000562 return 0;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100563}
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000564#endif
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100565
Damien Millerf7475d72008-11-03 19:26:18 +1100566/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000567static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000568input_userauth_failure(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100569{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000570 Authctxt *authctxt = ssh->authctxt;
Damien Miller62cee002000-09-23 17:15:56 +1100571 char *authlist = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000572 u_char partial;
Damien Miller62cee002000-09-23 17:15:56 +1100573
574 if (authctxt == NULL)
575 fatal("input_userauth_failure: no authentication context");
576
dtucker@openbsd.org696fb422019-07-07 01:05:00 +0000577 if (sshpkt_get_cstring(ssh, &authlist, NULL) != 0 ||
578 sshpkt_get_u8(ssh, &partial) != 0 ||
579 sshpkt_get_end(ssh) != 0)
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000580 goto out;
Damien Miller62cee002000-09-23 17:15:56 +1100581
Damien Miller62e9c4f2013-04-23 15:15:49 +1000582 if (partial != 0) {
dtucker@openbsd.org83cbca62016-07-22 05:46:11 +0000583 verbose("Authenticated with partial success.");
Damien Miller62e9c4f2013-04-23 15:15:49 +1000584 /* reset state */
djm@openbsd.orgb9844a42016-12-04 23:54:02 +0000585 pubkey_reset(authctxt);
Damien Miller62e9c4f2013-04-23 15:15:49 +1000586 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000587 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100588
djm@openbsd.org64c95982019-01-19 21:40:21 +0000589 userauth(ssh, authlist);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000590 authlist = NULL;
591 out:
592 free(authlist);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000593 return 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000594}
Damien Millerf7475d72008-11-03 19:26:18 +1100595
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000596/*
597 * Format an identity for logging including filename, key type, fingerprint
598 * and location (agent, etc.). Caller must free.
599 */
600static char *
601format_identity(Identity *id)
602{
603 char *fp = NULL, *ret = NULL;
604
605 if (id->key != NULL) {
606 fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
607 SSH_FP_DEFAULT);
608 }
609 xasprintf(&ret, "%s %s%s%s%s%s%s",
610 id->filename,
611 id->key ? sshkey_type(id->key) : "", id->key ? " " : "",
612 fp ? fp : "",
613 id->userprovided ? " explicit" : "",
614 (id->key && (id->key->flags & SSHKEY_FLAG_EXT)) ? " token" : "",
615 id->agent_fd != -1 ? " agent" : "");
616 free(fp);
617 return ret;
618}
619
Damien Millerf7475d72008-11-03 19:26:18 +1100620/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000621static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000622input_userauth_pk_ok(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000623{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000624 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000625 struct sshkey *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000626 Identity *id = NULL;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000627 int pktype, found = 0, sent = 0;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000628 size_t blen;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000629 char *pkalg = NULL, *fp = NULL, *ident = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000630 u_char *pkblob = NULL;
631 int r;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000632
633 if (authctxt == NULL)
634 fatal("input_userauth_pk_ok: no authentication context");
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000635
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000636 if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
637 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
638 (r = sshpkt_get_end(ssh)) != 0)
639 goto done;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000640
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000641 if ((pktype = sshkey_type_from_name(pkalg)) == KEY_UNSPEC) {
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000642 debug("%s: server sent unknown pkalg %s", __func__, pkalg);
Damien Miller280ecfb2003-05-14 13:46:00 +1000643 goto done;
644 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000645 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
646 debug("no key from blob. pkalg %s: %s", pkalg, ssh_err(r));
Damien Miller280ecfb2003-05-14 13:46:00 +1000647 goto done;
648 }
649 if (key->type != pktype) {
650 error("input_userauth_pk_ok: type mismatch "
651 "for decoded key (received %d, expected %d)",
652 key->type, pktype);
653 goto done;
654 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000655
Darren Tuckerd05b6012003-10-15 15:55:59 +1000656 /*
657 * search keys in the reverse order, because last candidate has been
658 * moved to the end of the queue. this also avoids confusion by
659 * duplicate keys
660 */
Damien Miller0b51a522004-04-20 20:07:19 +1000661 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000662 if (sshkey_equal(key, id->key)) {
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000663 found = 1;
Damien Miller280ecfb2003-05-14 13:46:00 +1000664 break;
665 }
666 }
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000667 if (!found || id == NULL) {
668 fp = sshkey_fingerprint(key, options.fingerprint_hash,
669 SSH_FP_DEFAULT);
670 error("%s: server replied with unknown key: %s %s", __func__,
671 sshkey_type(key), fp == NULL ? "<ERROR>" : fp);
672 goto done;
673 }
674 ident = format_identity(id);
675 debug("Server accepts key: %s", ident);
djm@openbsd.org64c95982019-01-19 21:40:21 +0000676 sent = sign_and_send_pubkey(ssh, id);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000677 r = 0;
678 done:
679 sshkey_free(key);
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000680 free(ident);
681 free(fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000682 free(pkalg);
683 free(pkblob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000684
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000685 /* try another method if we did not send a packet */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000686 if (r == 0 && sent == 0)
djm@openbsd.org64c95982019-01-19 21:40:21 +0000687 userauth(ssh, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000688 return r;
Damien Miller62cee002000-09-23 17:15:56 +1100689}
690
Darren Tucker0efd1552003-08-26 11:49:55 +1000691#ifdef GSSAPI
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000692static int
djm@openbsd.org64c95982019-01-19 21:40:21 +0000693userauth_gssapi(struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000694{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000695 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000696 Gssctxt *gssctxt = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000697 OM_uint32 min;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000698 int r, ok = 0;
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000699 gss_OID mech = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000700
701 /* Try one GSSAPI method at a time, rather than sending them all at
702 * once. */
703
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000704 if (authctxt->gss_supported_mechs == NULL)
705 gss_indicate_mechs(&min, &authctxt->gss_supported_mechs);
Darren Tucker0efd1552003-08-26 11:49:55 +1000706
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000707 /* Check to see whether the mechanism is usable before we offer it */
708 while (authctxt->mech_tried < authctxt->gss_supported_mechs->count &&
709 !ok) {
710 mech = &authctxt->gss_supported_mechs->
711 elements[authctxt->mech_tried];
Darren Tucker0efd1552003-08-26 11:49:55 +1000712 /* My DER encoding requires length<128 */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000713 if (mech->length < 128 && ssh_gssapi_check_mechanism(&gssctxt,
714 mech, authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000715 ok = 1; /* Mechanism works */
716 } else {
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000717 authctxt->mech_tried++;
Darren Tucker0efd1552003-08-26 11:49:55 +1000718 }
719 }
720
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000721 if (!ok || mech == NULL)
Damien Millereccb9de2005-06-17 12:59:34 +1000722 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000723
724 authctxt->methoddata=(void *)gssctxt;
725
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000726 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
727 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
728 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
729 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
730 (r = sshpkt_put_u32(ssh, 1)) != 0 ||
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000731 (r = sshpkt_put_u32(ssh, (mech->length) + 2)) != 0 ||
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000732 (r = sshpkt_put_u8(ssh, SSH_GSS_OIDTYPE)) != 0 ||
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000733 (r = sshpkt_put_u8(ssh, mech->length)) != 0 ||
734 (r = sshpkt_put(ssh, mech->elements, mech->length)) != 0 ||
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000735 (r = sshpkt_send(ssh)) != 0)
736 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +1000737
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000738 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
739 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
740 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
741 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000742
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000743 authctxt->mech_tried++; /* Move along to next candidate */
Darren Tucker0efd1552003-08-26 11:49:55 +1000744
745 return 1;
746}
747
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000748static void
749userauth_gssapi_cleanup(struct ssh *ssh)
750{
751 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
752 Gssctxt *gssctxt = (Gssctxt *)authctxt->methoddata;
753
754 ssh_gssapi_delete_ctx(&gssctxt);
755 authctxt->methoddata = NULL;
756
757 free(authctxt->gss_supported_mechs);
758 authctxt->gss_supported_mechs = NULL;
759}
760
Damien Miller91c6aa42003-11-17 21:20:18 +1100761static OM_uint32
djm@openbsd.org39896b72017-05-31 05:08:46 +0000762process_gssapi_token(struct ssh *ssh, gss_buffer_t recv_tok)
Damien Miller91c6aa42003-11-17 21:20:18 +1100763{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000764 Authctxt *authctxt = ssh->authctxt;
Damien Miller91c6aa42003-11-17 21:20:18 +1100765 Gssctxt *gssctxt = authctxt->methoddata;
766 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000767 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
768 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100769 OM_uint32 status, ms, flags;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000770 int r;
Damien Miller787b2ec2003-11-21 23:56:47 +1100771
Damien Miller91c6aa42003-11-17 21:20:18 +1100772 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100773 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100774
775 if (send_tok.length > 0) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000776 u_char type = GSS_ERROR(status) ?
777 SSH2_MSG_USERAUTH_GSSAPI_ERRTOK :
778 SSH2_MSG_USERAUTH_GSSAPI_TOKEN;
Damien Miller787b2ec2003-11-21 23:56:47 +1100779
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000780 if ((r = sshpkt_start(ssh, type)) != 0 ||
781 (r = sshpkt_put_string(ssh, send_tok.value,
782 send_tok.length)) != 0 ||
783 (r = sshpkt_send(ssh)) != 0)
784 fatal("%s: %s", __func__, ssh_err(r));
785
Damien Miller91c6aa42003-11-17 21:20:18 +1100786 gss_release_buffer(&ms, &send_tok);
787 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100788
Damien Miller91c6aa42003-11-17 21:20:18 +1100789 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100790 /* send either complete or MIC, depending on mechanism */
791 if (!(flags & GSS_C_INTEG_FLAG)) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000792 if ((r = sshpkt_start(ssh,
793 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE)) != 0 ||
794 (r = sshpkt_send(ssh)) != 0)
795 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0425d402003-11-17 22:18:21 +1100796 } else {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000797 struct sshbuf *b;
798
799 if ((b = sshbuf_new()) == NULL)
800 fatal("%s: sshbuf_new failed", __func__);
801 ssh_gssapi_buildmic(b, authctxt->server_user,
Damien Miller0425d402003-11-17 22:18:21 +1100802 authctxt->service, "gssapi-with-mic");
803
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000804 if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
805 fatal("%s: sshbuf_mutable_ptr failed", __func__);
806 gssbuf.length = sshbuf_len(b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100807
Damien Miller0425d402003-11-17 22:18:21 +1100808 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100809
Damien Miller0425d402003-11-17 22:18:21 +1100810 if (!GSS_ERROR(status)) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000811 if ((r = sshpkt_start(ssh,
812 SSH2_MSG_USERAUTH_GSSAPI_MIC)) != 0 ||
813 (r = sshpkt_put_string(ssh, mic.value,
814 mic.length)) != 0 ||
815 (r = sshpkt_send(ssh)) != 0)
816 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0425d402003-11-17 22:18:21 +1100817 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100818
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000819 sshbuf_free(b);
Damien Miller0425d402003-11-17 22:18:21 +1100820 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100821 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100822 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100823
Damien Miller91c6aa42003-11-17 21:20:18 +1100824 return status;
825}
826
Damien Millerf7475d72008-11-03 19:26:18 +1100827/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000828static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000829input_gssapi_response(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000830{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000831 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000832 Gssctxt *gssctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000833 size_t oidlen;
834 u_char *oidv = NULL;
835 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000836
837 if (authctxt == NULL)
838 fatal("input_gssapi_response: no authentication context");
839 gssctxt = authctxt->methoddata;
840
841 /* Setup our OID */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000842 if ((r = sshpkt_get_string(ssh, &oidv, &oidlen)) != 0)
843 goto done;
Darren Tucker0efd1552003-08-26 11:49:55 +1000844
Darren Tucker655a5e02003-11-03 20:09:03 +1100845 if (oidlen <= 2 ||
846 oidv[0] != SSH_GSS_OIDTYPE ||
847 oidv[1] != oidlen - 2) {
848 debug("Badly encoded mechanism OID received");
djm@openbsd.org0a5f2ea2019-01-21 01:05:00 +0000849 userauth(ssh, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000850 goto ok;
Darren Tucker0efd1552003-08-26 11:49:55 +1000851 }
852
Darren Tucker655a5e02003-11-03 20:09:03 +1100853 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
854 fatal("Server returned different OID than expected");
855
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000856 if ((r = sshpkt_get_end(ssh)) != 0)
857 goto done;
Darren Tucker0efd1552003-08-26 11:49:55 +1000858
djm@openbsd.org39896b72017-05-31 05:08:46 +0000859 if (GSS_ERROR(process_gssapi_token(ssh, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000860 /* Start again with next method on list */
861 debug("Trying to start again");
djm@openbsd.org0a5f2ea2019-01-21 01:05:00 +0000862 userauth(ssh, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000863 goto ok;
Darren Tucker0efd1552003-08-26 11:49:55 +1000864 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000865 ok:
866 r = 0;
867 done:
868 free(oidv);
869 return r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000870}
871
Damien Millerf7475d72008-11-03 19:26:18 +1100872/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000873static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000874input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000875{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000876 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000877 gss_buffer_desc recv_tok;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000878 u_char *p = NULL;
879 size_t len;
Damien Miller91c6aa42003-11-17 21:20:18 +1100880 OM_uint32 status;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000881 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000882
883 if (authctxt == NULL)
884 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000885
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000886 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
887 (r = sshpkt_get_end(ssh)) != 0)
888 goto out;
Darren Tucker0efd1552003-08-26 11:49:55 +1000889
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000890 recv_tok.value = p;
891 recv_tok.length = len;
djm@openbsd.org39896b72017-05-31 05:08:46 +0000892 status = process_gssapi_token(ssh, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000893
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000894 /* Start again with the next method in the list */
Darren Tucker0efd1552003-08-26 11:49:55 +1000895 if (GSS_ERROR(status)) {
djm@openbsd.org0a5f2ea2019-01-21 01:05:00 +0000896 userauth(ssh, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000897 /* ok */
Darren Tucker0efd1552003-08-26 11:49:55 +1000898 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000899 r = 0;
900 out:
901 free(p);
902 return r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000903}
904
Damien Millerf7475d72008-11-03 19:26:18 +1100905/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000906static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000907input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000908{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000909 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000910 Gssctxt *gssctxt;
911 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
912 gss_buffer_desc recv_tok;
Damien Millerd677ad12013-04-23 15:18:51 +1000913 OM_uint32 ms;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000914 u_char *p = NULL;
915 size_t len;
916 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000917
918 if (authctxt == NULL)
919 fatal("input_gssapi_response: no authentication context");
920 gssctxt = authctxt->methoddata;
921
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000922 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
923 (r = sshpkt_get_end(ssh)) != 0) {
924 free(p);
925 return r;
926 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000927
928 /* Stick it into GSSAPI and see what it says */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000929 recv_tok.value = p;
930 recv_tok.length = len;
Damien Millerd677ad12013-04-23 15:18:51 +1000931 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000932 &recv_tok, &send_tok, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000933 free(p);
Darren Tucker0efd1552003-08-26 11:49:55 +1000934 gss_release_buffer(&ms, &send_tok);
935
936 /* Server will be returning a failed packet after this one */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000937 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000938}
939
Damien Millerf7475d72008-11-03 19:26:18 +1100940/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000941static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000942input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000943{
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000944 char *msg = NULL;
945 char *lang = NULL;
946 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000947
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000948 if ((r = sshpkt_get_u32(ssh, NULL)) != 0 || /* maj */
949 (r = sshpkt_get_u32(ssh, NULL)) != 0 || /* min */
950 (r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
951 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
952 goto out;
953 r = sshpkt_get_end(ssh);
Damien Miller15d72a02005-11-05 15:07:33 +1100954 debug("Server GSSAPI Error:\n%s", msg);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000955 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000956 free(msg);
957 free(lang);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000958 return r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000959}
960#endif /* GSSAPI */
961
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000962static int
djm@openbsd.org64c95982019-01-19 21:40:21 +0000963userauth_none(struct ssh *ssh)
Damien Miller874d77b2000-10-14 16:23:11 +1100964{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000965 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000966 int r;
967
Damien Miller874d77b2000-10-14 16:23:11 +1100968 /* initial userauth request */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000969 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
970 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
971 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
972 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
973 (r = sshpkt_send(ssh)) != 0)
974 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100975 return 1;
976}
977
djm@openbsd.org5c68ea82019-02-11 09:44:42 +0000978static int
djm@openbsd.org64c95982019-01-19 21:40:21 +0000979userauth_passwd(struct ssh *ssh)
Damien Millereba71ba2000-04-29 23:57:08 +1000980{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000981 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
djm@openbsd.org91b19192018-11-28 06:00:38 +0000982 char *password, *prompt = NULL;
Darren Tuckerab791692010-01-08 18:48:02 +1100983 const char *host = options.host_key_alias ? options.host_key_alias :
984 authctxt->host;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000985 int r;
Damien Millereba71ba2000-04-29 23:57:08 +1000986
djm@openbsd.org4a526942019-01-04 03:27:50 +0000987 if (authctxt->attempt_passwd++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000988 return 0;
989
djm@openbsd.org4a526942019-01-04 03:27:50 +0000990 if (authctxt->attempt_passwd != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000991 error("Permission denied, please try again.");
992
djm@openbsd.org91b19192018-11-28 06:00:38 +0000993 xasprintf(&prompt, "%s@%s's password: ", authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +1000994 password = read_passphrase(prompt, 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000995 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
996 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
997 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
998 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
999 (r = sshpkt_put_u8(ssh, 0)) != 0 ||
1000 (r = sshpkt_put_cstring(ssh, password)) != 0 ||
1001 (r = sshpkt_add_padding(ssh, 64)) != 0 ||
1002 (r = sshpkt_send(ssh)) != 0)
1003 fatal("%s: %s", __func__, ssh_err(r));
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001004
djm@openbsd.org91b19192018-11-28 06:00:38 +00001005 free(prompt);
1006 if (password != NULL)
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001007 freezero(password, strlen(password));
1008
1009 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001010 &input_userauth_passwd_changereq);
1011
Damien Millereba71ba2000-04-29 23:57:08 +10001012 return 1;
1013}
Damien Millerf7475d72008-11-03 19:26:18 +11001014
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001015/*
1016 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
1017 */
Damien Millerf7475d72008-11-03 19:26:18 +11001018/* ARGSUSED */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +00001019static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001020input_userauth_passwd_changereq(int type, u_int32_t seqnr, struct ssh *ssh)
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001021{
markus@openbsd.org94583be2017-05-30 14:19:15 +00001022 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001023 char *info = NULL, *lang = NULL, *password = NULL, *retype = NULL;
dtucker@openbsd.orga54eb272017-08-27 00:38:41 +00001024 char prompt[256];
djm@openbsd.org3e032a92017-01-30 00:32:03 +00001025 const char *host;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001026 int r;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001027
1028 debug2("input_userauth_passwd_changereq");
1029
1030 if (authctxt == NULL)
1031 fatal("input_userauth_passwd_changereq: "
1032 "no authentication context");
djm@openbsd.org3e032a92017-01-30 00:32:03 +00001033 host = options.host_key_alias ? options.host_key_alias : authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001034
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001035 if ((r = sshpkt_get_cstring(ssh, &info, NULL)) != 0 ||
1036 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1037 goto out;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001038 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001039 logit("%s", info);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001040 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1041 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1042 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1043 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1044 (r = sshpkt_put_u8(ssh, 1)) != 0) /* additional info */
1045 goto out;
1046
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001047 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001048 "Enter %.30s@%.128s's old password: ",
Darren Tuckerab791692010-01-08 18:48:02 +11001049 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001050 password = read_passphrase(prompt, 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001051 if ((r = sshpkt_put_cstring(ssh, password)) != 0)
1052 goto out;
1053
1054 freezero(password, strlen(password));
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001055 password = NULL;
1056 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001057 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001058 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +11001059 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001060 password = read_passphrase(prompt, RP_ALLOW_EOF);
1061 if (password == NULL) {
1062 /* bail out */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001063 r = 0;
1064 goto out;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001065 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001066 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001067 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +11001068 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001069 retype = read_passphrase(prompt, 0);
1070 if (strcmp(password, retype) != 0) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001071 freezero(password, strlen(password));
Damien Miller996acd22003-04-09 20:59:48 +10001072 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001073 password = NULL;
1074 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001075 freezero(retype, strlen(retype));
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001076 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001077 if ((r = sshpkt_put_cstring(ssh, password)) != 0 ||
1078 (r = sshpkt_add_padding(ssh, 64)) != 0 ||
1079 (r = sshpkt_send(ssh)) != 0)
1080 goto out;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001081
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001082 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001083 &input_userauth_passwd_changereq);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001084 r = 0;
1085 out:
1086 if (password)
1087 freezero(password, strlen(password));
1088 free(info);
1089 free(lang);
1090 return r;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001091}
Damien Millereba71ba2000-04-29 23:57:08 +10001092
djm@openbsd.org26074382018-03-03 03:01:50 +00001093/*
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001094 * Select an algorithm for publickey signatures.
1095 * Returns algorithm (caller must free) or NULL if no mutual algorithm found.
1096 *
1097 * Call with ssh==NULL to ignore server-sig-algs extension list and
1098 * only attempt with the key's base signature type.
djm@openbsd.org26074382018-03-03 03:01:50 +00001099 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001100static char *
1101key_sig_algorithm(struct ssh *ssh, const struct sshkey *key)
djm@openbsd.org26074382018-03-03 03:01:50 +00001102{
djm@openbsd.orgb4d4eda2018-07-03 13:20:25 +00001103 char *allowed, *oallowed, *cp, *tmp, *alg = NULL;
djm@openbsd.org26074382018-03-03 03:01:50 +00001104
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001105 /*
1106 * The signature algorithm will only differ from the key algorithm
1107 * for RSA keys/certs and when the server advertises support for
1108 * newer (SHA2) algorithms.
1109 */
1110 if (ssh == NULL || ssh->kex->server_sig_algs == NULL ||
djm@openbsd.org1a4a9cf2018-10-11 03:48:04 +00001111 (key->type != KEY_RSA && key->type != KEY_RSA_CERT) ||
1112 (key->type == KEY_RSA_CERT && (datafellows & SSH_BUG_SIGTYPE))) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001113 /* Filter base key signature alg against our configuration */
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001114 return match_list(sshkey_ssh_name(key),
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001115 options.pubkey_key_types, NULL);
djm@openbsd.org26074382018-03-03 03:01:50 +00001116 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001117
1118 /*
1119 * For RSA keys/certs, since these might have a different sig type:
1120 * find the first entry in PubkeyAcceptedKeyTypes of the right type
1121 * that also appears in the supported signature algorithms list from
1122 * the server.
1123 */
1124 oallowed = allowed = xstrdup(options.pubkey_key_types);
1125 while ((cp = strsep(&allowed, ",")) != NULL) {
1126 if (sshkey_type_from_name(cp) != key->type)
1127 continue;
djm@openbsd.orgb4d4eda2018-07-03 13:20:25 +00001128 tmp = match_list(sshkey_sigalg_by_name(cp), ssh->kex->server_sig_algs, NULL);
1129 if (tmp != NULL)
1130 alg = xstrdup(cp);
1131 free(tmp);
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001132 if (alg != NULL)
1133 break;
1134 }
1135 free(oallowed);
1136 return alg;
djm@openbsd.org26074382018-03-03 03:01:50 +00001137}
1138
Ben Lindstrombba81212001-06-25 05:01:22 +00001139static int
djm@openbsd.org141efe42015-01-14 20:05:27 +00001140identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001141 const u_char *data, size_t datalen, u_int compat, const char *alg)
Damien Miller280ecfb2003-05-14 13:46:00 +10001142{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001143 struct sshkey *prv;
djm@openbsd.org26074382018-03-03 03:01:50 +00001144 int r;
Damien Miller280ecfb2003-05-14 13:46:00 +10001145
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001146 /* The agent supports this key. */
djm@openbsd.org26074382018-03-03 03:01:50 +00001147 if (id->key != NULL && id->agent_fd != -1) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001148 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
1149 data, datalen, alg, compat);
1150 }
1151
1152 /*
1153 * We have already loaded the private key or the private key is
1154 * stored in external hardware.
1155 */
1156 if (id->key != NULL &&
1157 (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))) {
1158 if ((r = sshkey_sign(id->key, sigp, lenp, data, datalen,
1159 alg, compat)) != 0)
1160 return r;
1161 /*
1162 * PKCS#11 tokens may not support all signature algorithms,
1163 * so check what we get back.
1164 */
1165 if ((r = sshkey_check_sigtype(*sigp, *lenp, alg)) != 0)
djm@openbsd.org26074382018-03-03 03:01:50 +00001166 return r;
1167 return 0;
1168 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001169
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001170 /* Load the private key from the file. */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001171 if ((prv = load_identity_file(id)) == NULL)
djm@openbsd.org0d1451a2016-02-23 01:34:14 +00001172 return SSH_ERR_KEY_NOT_FOUND;
djm@openbsd.orgc4972d02017-08-11 04:47:12 +00001173 if (id->key != NULL && !sshkey_equal_public(prv, id->key)) {
1174 error("%s: private key %s contents do not match public",
1175 __func__, id->filename);
1176 return SSH_ERR_KEY_NOT_FOUND;
1177 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001178 r = sshkey_sign(prv, sigp, lenp, data, datalen, alg, compat);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001179 sshkey_free(prv);
djm@openbsd.org26074382018-03-03 03:01:50 +00001180 return r;
Damien Miller280ecfb2003-05-14 13:46:00 +10001181}
1182
1183static int
djm@openbsd.org1e245522017-03-11 23:40:26 +00001184id_filename_matches(Identity *id, Identity *private_id)
1185{
1186 const char *suffixes[] = { ".pub", "-cert.pub", NULL };
1187 size_t len = strlen(id->filename), plen = strlen(private_id->filename);
1188 size_t i, slen;
1189
1190 if (strcmp(id->filename, private_id->filename) == 0)
1191 return 1;
1192 for (i = 0; suffixes[i]; i++) {
1193 slen = strlen(suffixes[i]);
1194 if (len > slen && plen == len - slen &&
1195 strcmp(id->filename + (len - slen), suffixes[i]) == 0 &&
1196 memcmp(id->filename, private_id->filename, plen) == 0)
1197 return 1;
1198 }
1199 return 0;
1200}
1201
1202static int
djm@openbsd.org64c95982019-01-19 21:40:21 +00001203sign_and_send_pubkey(struct ssh *ssh, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +10001204{
djm@openbsd.org64c95982019-01-19 21:40:21 +00001205 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001206 struct sshbuf *b = NULL;
1207 Identity *private_id, *sign_id = NULL;
1208 u_char *signature = NULL;
1209 size_t slen = 0, skip = 0;
1210 int r, fallback_sigtype, sent = 0;
1211 char *alg = NULL, *fp = NULL;
1212 const char *loc = "";
Damien Millereba71ba2000-04-29 23:57:08 +10001213
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001214 if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1215 SSH_FP_DEFAULT)) == NULL)
1216 return 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001217
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001218 debug3("%s: %s %s", __func__, sshkey_type(id->key), fp);
Damien Millereba71ba2000-04-29 23:57:08 +10001219
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001220 /*
1221 * If the key is an certificate, try to find a matching private key
1222 * and use it to complete the signature.
djm@openbsd.org9ee692f2016-04-28 14:30:21 +00001223 * If no such private key exists, fall back to trying the certificate
1224 * key itself in case it has a private half already loaded.
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001225 * This will try to set sign_id to the private key that will perform
1226 * the signature.
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001227 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001228 if (sshkey_is_cert(id->key)) {
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001229 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1230 if (sshkey_equal_public(id->key, private_id->key) &&
1231 id->key->type != private_id->key->type) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001232 sign_id = private_id;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001233 break;
1234 }
1235 }
djm@openbsd.org1e245522017-03-11 23:40:26 +00001236 /*
1237 * Exact key matches are preferred, but also allow
1238 * filename matches for non-PKCS#11/agent keys that
1239 * didn't load public keys. This supports the case
1240 * of keeping just a private key file and public
1241 * certificate on disk.
1242 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001243 if (sign_id == NULL &&
1244 !id->isprivate && id->agent_fd == -1 &&
djm@openbsd.org1e245522017-03-11 23:40:26 +00001245 (id->key->flags & SSHKEY_FLAG_EXT) == 0) {
1246 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1247 if (private_id->key == NULL &&
1248 id_filename_matches(id, private_id)) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001249 sign_id = private_id;
djm@openbsd.org1e245522017-03-11 23:40:26 +00001250 break;
1251 }
1252 }
1253 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001254 if (sign_id != NULL) {
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001255 debug2("%s: using private key \"%s\"%s for "
1256 "certificate", __func__, id->filename,
1257 id->agent_fd != -1 ? " from agent" : "");
1258 } else {
djm@openbsd.orgc38905b2016-03-14 16:20:54 +00001259 debug("%s: no separate private key for certificate "
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001260 "\"%s\"", __func__, id->filename);
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001261 }
1262 }
1263
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001264 /*
1265 * If the above didn't select another identity to do the signing
1266 * then default to the one we started with.
1267 */
1268 if (sign_id == NULL)
1269 sign_id = id;
1270
1271 /* assemble and sign data */
1272 for (fallback_sigtype = 0; fallback_sigtype <= 1; fallback_sigtype++) {
1273 free(alg);
1274 slen = 0;
1275 signature = NULL;
1276 if ((alg = key_sig_algorithm(fallback_sigtype ? NULL : ssh,
1277 id->key)) == NULL) {
1278 error("%s: no mutual signature supported", __func__);
1279 goto out;
1280 }
1281 debug3("%s: signing using %s", __func__, alg);
1282
1283 sshbuf_free(b);
1284 if ((b = sshbuf_new()) == NULL)
1285 fatal("%s: sshbuf_new failed", __func__);
1286 if (datafellows & SSH_OLD_SESSIONID) {
1287 if ((r = sshbuf_put(b, session_id2,
1288 session_id2_len)) != 0) {
1289 fatal("%s: sshbuf_put: %s",
1290 __func__, ssh_err(r));
1291 }
1292 } else {
1293 if ((r = sshbuf_put_string(b, session_id2,
1294 session_id2_len)) != 0) {
1295 fatal("%s: sshbuf_put_string: %s",
1296 __func__, ssh_err(r));
1297 }
1298 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001299 skip = sshbuf_len(b);
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001300 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1301 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1302 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1303 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1304 (r = sshbuf_put_u8(b, 1)) != 0 ||
1305 (r = sshbuf_put_cstring(b, alg)) != 0 ||
1306 (r = sshkey_puts(id->key, b)) != 0) {
1307 fatal("%s: assemble signed data: %s",
1308 __func__, ssh_err(r));
1309 }
1310
1311 /* generate signature */
1312 r = identity_sign(sign_id, &signature, &slen,
1313 sshbuf_ptr(b), sshbuf_len(b), datafellows, alg);
1314 if (r == 0)
1315 break;
1316 else if (r == SSH_ERR_KEY_NOT_FOUND)
1317 goto out; /* soft failure */
1318 else if (r == SSH_ERR_SIGN_ALG_UNSUPPORTED &&
1319 !fallback_sigtype) {
1320 if (sign_id->agent_fd != -1)
1321 loc = "agent ";
1322 else if ((sign_id->key->flags & SSHKEY_FLAG_EXT) != 0)
1323 loc = "token ";
1324 logit("%skey %s %s returned incorrect signature type",
1325 loc, sshkey_type(id->key), fp);
1326 continue;
1327 }
1328 error("%s: signing failed: %s", __func__, ssh_err(r));
1329 goto out;
Damien Millerad833b32000-08-23 10:46:23 +10001330 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001331 if (slen == 0 || signature == NULL) /* shouldn't happen */
1332 fatal("%s: no signature", __func__);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001333
Damien Millereba71ba2000-04-29 23:57:08 +10001334 /* append signature */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001335 if ((r = sshbuf_put_string(b, signature, slen)) != 0)
1336 fatal("%s: append signature: %s", __func__, ssh_err(r));
Damien Millereba71ba2000-04-29 23:57:08 +10001337
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001338#ifdef DEBUG_PK
1339 sshbuf_dump(b, stderr);
1340#endif
Damien Millereba71ba2000-04-29 23:57:08 +10001341 /* skip session id and packet type */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001342 if ((r = sshbuf_consume(b, skip + 1)) != 0)
1343 fatal("%s: consume: %s", __func__, ssh_err(r));
Damien Millereba71ba2000-04-29 23:57:08 +10001344
1345 /* put remaining data from buffer into packet */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001346 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1347 (r = sshpkt_putb(ssh, b)) != 0 ||
1348 (r = sshpkt_send(ssh)) != 0)
1349 fatal("%s: enqueue request: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001350
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001351 /* success */
1352 sent = 1;
1353
1354 out:
1355 free(fp);
1356 free(alg);
1357 sshbuf_free(b);
1358 freezero(signature, slen);
1359 return sent;
Damien Miller994cf142000-07-21 10:19:44 +10001360}
1361
Ben Lindstrombba81212001-06-25 05:01:22 +00001362static int
djm@openbsd.org64c95982019-01-19 21:40:21 +00001363send_pubkey_test(struct ssh *ssh, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001364{
djm@openbsd.org64c95982019-01-19 21:40:21 +00001365 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001366 u_char *blob = NULL;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001367 char *alg = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001368 size_t bloblen;
1369 u_int have_sig = 0;
1370 int sent = 0, r;
Damien Miller994cf142000-07-21 10:19:44 +10001371
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001372 if ((alg = key_sig_algorithm(ssh, id->key)) == NULL) {
1373 debug("%s: no mutual signature algorithm", __func__);
1374 goto out;
1375 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001376
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001377 if ((r = sshkey_to_blob(id->key, &blob, &bloblen)) != 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001378 /* we cannot handle this key */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001379 debug3("%s: cannot handle key", __func__);
1380 goto out;
Damien Miller994cf142000-07-21 10:19:44 +10001381 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001382 /* register callback for USERAUTH_PK_OK message */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001383 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001384
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001385 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1386 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1387 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1388 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1389 (r = sshpkt_put_u8(ssh, have_sig)) != 0 ||
1390 (r = sshpkt_put_cstring(ssh, alg)) != 0 ||
1391 (r = sshpkt_put_string(ssh, blob, bloblen)) != 0 ||
1392 (r = sshpkt_send(ssh)) != 0)
1393 fatal("%s: %s", __func__, ssh_err(r));
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001394 sent = 1;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001395
1396 out:
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001397 free(alg);
1398 free(blob);
1399 return sent;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001400}
1401
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001402static struct sshkey *
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001403load_identity_file(Identity *id)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001404{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001405 struct sshkey *private = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001406 char prompt[300], *passphrase, *comment;
dtucker@openbsd.org6b39a7b2019-08-05 11:50:33 +00001407 int r, quit = 0, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001408 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001409
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001410 if (stat(id->filename, &st) == -1) {
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001411 (id->userprovided ? logit : debug3)("no such identity: %s: %s",
1412 id->filename, strerror(errno));
Ben Lindstrom329782e2001-03-10 17:08:59 +00001413 return NULL;
1414 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001415 snprintf(prompt, sizeof prompt,
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001416 "Enter passphrase for key '%.100s': ", id->filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001417 for (i = 0; i <= options.number_of_password_prompts; i++) {
1418 if (i == 0)
1419 passphrase = "";
1420 else {
Damien Miller62cee002000-09-23 17:15:56 +11001421 passphrase = read_passphrase(prompt, 0);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001422 if (*passphrase == '\0') {
Damien Miller62cee002000-09-23 17:15:56 +11001423 debug2("no passphrase given, try next key");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001424 free(passphrase);
1425 break;
Damien Miller62cee002000-09-23 17:15:56 +11001426 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001427 }
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001428 switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
dtucker@openbsd.org6b39a7b2019-08-05 11:50:33 +00001429 passphrase, &private, &comment))) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001430 case 0:
1431 break;
1432 case SSH_ERR_KEY_WRONG_PASSPHRASE:
1433 if (options.batch_mode) {
1434 quit = 1;
1435 break;
1436 }
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001437 if (i != 0)
1438 debug2("bad passphrase given, try again...");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001439 break;
1440 case SSH_ERR_SYSTEM_ERROR:
1441 if (errno == ENOENT) {
1442 debug2("Load key \"%s\": %s",
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001443 id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001444 quit = 1;
1445 break;
1446 }
1447 /* FALLTHROUGH */
1448 default:
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001449 error("Load key \"%s\": %s", id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001450 quit = 1;
1451 break;
1452 }
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001453 if (!quit && private != NULL && id->agent_fd == -1 &&
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001454 !(id->key && id->isprivate))
1455 maybe_add_key_to_agent(id->filename, private, comment,
1456 passphrase);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001457 if (i > 0)
1458 freezero(passphrase, strlen(passphrase));
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001459 free(comment);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001460 if (private != NULL || quit)
1461 break;
Damien Miller994cf142000-07-21 10:19:44 +10001462 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001463 return private;
1464}
1465
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001466static int
1467key_type_allowed_by_config(struct sshkey *key)
1468{
1469 if (match_pattern_list(sshkey_ssh_name(key),
1470 options.pubkey_key_types, 0) == 1)
1471 return 1;
1472
1473 /* RSA keys/certs might be allowed by alternate signature types */
1474 switch (key->type) {
1475 case KEY_RSA:
1476 if (match_pattern_list("rsa-sha2-512",
1477 options.pubkey_key_types, 0) == 1)
1478 return 1;
1479 if (match_pattern_list("rsa-sha2-256",
1480 options.pubkey_key_types, 0) == 1)
1481 return 1;
1482 break;
1483 case KEY_RSA_CERT:
1484 if (match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
1485 options.pubkey_key_types, 0) == 1)
1486 return 1;
1487 if (match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
1488 options.pubkey_key_types, 0) == 1)
1489 return 1;
1490 break;
1491 }
1492 return 0;
1493}
1494
1495
Damien Miller280ecfb2003-05-14 13:46:00 +10001496/*
1497 * try keys in the following order:
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001498 * 1. certificates listed in the config file
1499 * 2. other input certificates
1500 * 3. agent keys that are found in the config file
1501 * 4. other agent keys
1502 * 5. keys that are only listed in the config file
Damien Miller280ecfb2003-05-14 13:46:00 +10001503 */
1504static void
1505pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001506{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001507 struct identity *id, *id2, *tmp;
1508 struct idlist agent, files, *preferred;
1509 struct sshkey *key;
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001510 int agent_fd = -1, i, r, found;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001511 size_t j;
1512 struct ssh_identitylist *idlist;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001513 char *ident;
Damien Millerad833b32000-08-23 10:46:23 +10001514
Damien Miller280ecfb2003-05-14 13:46:00 +10001515 TAILQ_INIT(&agent); /* keys from the agent */
1516 TAILQ_INIT(&files); /* keys from the config file */
1517 preferred = &authctxt->keys;
1518 TAILQ_INIT(preferred); /* preferred order of keys */
1519
Damien Millercb6b68b2012-12-03 09:49:52 +11001520 /* list of keys stored in the filesystem and PKCS#11 */
Damien Miller280ecfb2003-05-14 13:46:00 +10001521 for (i = 0; i < options.num_identity_files; i++) {
1522 key = options.identity_keys[i];
Damien Miller0a80ca12010-02-27 07:55:05 +11001523 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1524 continue;
Damien Miller280ecfb2003-05-14 13:46:00 +10001525 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001526 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001527 id->agent_fd = -1;
Damien Miller280ecfb2003-05-14 13:46:00 +10001528 id->key = key;
1529 id->filename = xstrdup(options.identity_files[i]);
Darren Tucker19104782013-04-05 11:13:08 +11001530 id->userprovided = options.identity_file_userprovided[i];
Damien Miller280ecfb2003-05-14 13:46:00 +10001531 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001532 }
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001533 /* list of certificates specified by user */
1534 for (i = 0; i < options.num_certificate_files; i++) {
1535 key = options.certificates[i];
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001536 if (!sshkey_is_cert(key) || key->cert == NULL ||
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001537 key->cert->type != SSH2_CERT_TYPE_USER)
1538 continue;
1539 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001540 id->agent_fd = -1;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001541 id->key = key;
1542 id->filename = xstrdup(options.certificate_files[i]);
1543 id->userprovided = options.certificate_file_userprovided[i];
1544 TAILQ_INSERT_TAIL(preferred, id, next);
1545 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001546 /* list of keys supported by the agent */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001547 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1548 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1549 debug("%s: ssh_get_authentication_socket: %s",
1550 __func__, ssh_err(r));
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +00001551 } else if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001552 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1553 debug("%s: ssh_fetch_identitylist: %s",
1554 __func__, ssh_err(r));
markus@openbsd.orgfc77ccd2016-01-14 22:56:56 +00001555 close(agent_fd);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001556 } else {
1557 for (j = 0; j < idlist->nkeys; j++) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001558 found = 0;
1559 TAILQ_FOREACH(id, &files, next) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001560 /*
1561 * agent keys from the config file are
1562 * preferred
1563 */
1564 if (sshkey_equal(idlist->keys[j], id->key)) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001565 TAILQ_REMOVE(&files, id, next);
1566 TAILQ_INSERT_TAIL(preferred, id, next);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001567 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001568 found = 1;
1569 break;
1570 }
1571 }
Damien Millerbd394c32004-03-08 23:12:36 +11001572 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001573 id = xcalloc(1, sizeof(*id));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001574 /* XXX "steals" key/comment from idlist */
1575 id->key = idlist->keys[j];
1576 id->filename = idlist->comments[j];
1577 idlist->keys[j] = NULL;
1578 idlist->comments[j] = NULL;
1579 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001580 TAILQ_INSERT_TAIL(&agent, id, next);
1581 }
1582 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001583 ssh_free_identitylist(idlist);
Damien Miller280ecfb2003-05-14 13:46:00 +10001584 /* append remaining agent keys */
1585 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1586 TAILQ_REMOVE(&agent, id, next);
1587 TAILQ_INSERT_TAIL(preferred, id, next);
1588 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001589 authctxt->agent_fd = agent_fd;
Damien Miller62cee002000-09-23 17:15:56 +11001590 }
djm@openbsd.org82f24c32016-05-23 23:30:50 +00001591 /* Prefer PKCS11 keys that are explicitly listed */
1592 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1593 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
1594 continue;
1595 found = 0;
1596 TAILQ_FOREACH(id2, &files, next) {
1597 if (id2->key == NULL ||
1598 (id2->key->flags & SSHKEY_FLAG_EXT) == 0)
1599 continue;
1600 if (sshkey_equal(id->key, id2->key)) {
1601 TAILQ_REMOVE(&files, id, next);
1602 TAILQ_INSERT_TAIL(preferred, id, next);
1603 found = 1;
1604 break;
1605 }
1606 }
1607 /* If IdentitiesOnly set and key not found then don't use it */
1608 if (!found && options.identities_only) {
1609 TAILQ_REMOVE(&files, id, next);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001610 freezero(id, sizeof(*id));
djm@openbsd.org82f24c32016-05-23 23:30:50 +00001611 }
1612 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001613 /* append remaining keys from the config file */
1614 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1615 TAILQ_REMOVE(&files, id, next);
1616 TAILQ_INSERT_TAIL(preferred, id, next);
1617 }
djm@openbsd.orge679c092015-10-13 16:15:21 +00001618 /* finally, filter by PubkeyAcceptedKeyTypes */
1619 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
djm@openbsd.orgd78b75d2018-07-03 13:07:58 +00001620 if (id->key != NULL && !key_type_allowed_by_config(id->key)) {
djm@openbsd.orge679c092015-10-13 16:15:21 +00001621 debug("Skipping %s key %s - "
1622 "not in PubkeyAcceptedKeyTypes",
1623 sshkey_ssh_name(id->key), id->filename);
1624 TAILQ_REMOVE(preferred, id, next);
1625 sshkey_free(id->key);
1626 free(id->filename);
1627 memset(id, 0, sizeof(*id));
1628 continue;
1629 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001630 }
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001631 /* List the keys we plan on using */
1632 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1633 ident = format_identity(id);
1634 debug("Will attempt key: %s", ident);
1635 free(ident);
1636 }
1637 debug2("%s: done", __func__);
Damien Miller280ecfb2003-05-14 13:46:00 +10001638}
1639
1640static void
djm@openbsd.org38e83e42019-02-12 23:53:10 +00001641pubkey_cleanup(struct ssh *ssh)
Damien Miller280ecfb2003-05-14 13:46:00 +10001642{
djm@openbsd.org5c68ea82019-02-11 09:44:42 +00001643 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
Damien Miller280ecfb2003-05-14 13:46:00 +10001644 Identity *id;
1645
djm@openbsd.orgaaed6352018-11-16 02:46:20 +00001646 if (authctxt->agent_fd != -1) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001647 ssh_close_authentication_socket(authctxt->agent_fd);
djm@openbsd.orgaaed6352018-11-16 02:46:20 +00001648 authctxt->agent_fd = -1;
1649 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001650 for (id = TAILQ_FIRST(&authctxt->keys); id;
1651 id = TAILQ_FIRST(&authctxt->keys)) {
1652 TAILQ_REMOVE(&authctxt->keys, id, next);
mmcc@openbsd.org89540b62015-12-11 02:31:47 +00001653 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +10001654 free(id->filename);
1655 free(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001656 }
Damien Millereba71ba2000-04-29 23:57:08 +10001657}
1658
djm@openbsd.orgb9844a42016-12-04 23:54:02 +00001659static void
1660pubkey_reset(Authctxt *authctxt)
1661{
1662 Identity *id;
1663
1664 TAILQ_FOREACH(id, &authctxt->keys, next)
1665 id->tried = 0;
1666}
1667
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001668static int
1669try_identity(Identity *id)
1670{
1671 if (!id->key)
1672 return (0);
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001673 if (sshkey_type_plain(id->key->type) == KEY_RSA &&
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001674 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1675 debug("Skipped %s key %s for RSA/MD5 server",
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001676 sshkey_type(id->key), id->filename);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001677 return (0);
1678 }
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001679 return 1;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001680}
1681
djm@openbsd.org5c68ea82019-02-11 09:44:42 +00001682static int
djm@openbsd.org64c95982019-01-19 21:40:21 +00001683userauth_pubkey(struct ssh *ssh)
Damien Millereba71ba2000-04-29 23:57:08 +10001684{
djm@openbsd.org64c95982019-01-19 21:40:21 +00001685 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
Damien Miller280ecfb2003-05-14 13:46:00 +10001686 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001687 int sent = 0;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001688 char *ident;
Damien Millereba71ba2000-04-29 23:57:08 +10001689
Damien Miller280ecfb2003-05-14 13:46:00 +10001690 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1691 if (id->tried++)
1692 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001693 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001694 TAILQ_REMOVE(&authctxt->keys, id, next);
1695 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1696 /*
1697 * send a test message if we have the public key. for
1698 * encrypted keys we cannot do this and have to load the
1699 * private key instead
1700 */
Damien Miller324541e2013-12-31 12:25:40 +11001701 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001702 if (try_identity(id)) {
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001703 ident = format_identity(id);
1704 debug("Offering public key: %s", ident);
1705 free(ident);
djm@openbsd.org64c95982019-01-19 21:40:21 +00001706 sent = send_pubkey_test(ssh, id);
Damien Miller324541e2013-12-31 12:25:40 +11001707 }
1708 } else {
Damien Miller280ecfb2003-05-14 13:46:00 +10001709 debug("Trying private key: %s", id->filename);
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001710 id->key = load_identity_file(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001711 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001712 if (try_identity(id)) {
1713 id->isprivate = 1;
djm@openbsd.org64c95982019-01-19 21:40:21 +00001714 sent = sign_and_send_pubkey(ssh, id);
Damien Miller324541e2013-12-31 12:25:40 +11001715 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001716 sshkey_free(id->key);
Damien Miller280ecfb2003-05-14 13:46:00 +10001717 id->key = NULL;
djm@openbsd.orgb9844a42016-12-04 23:54:02 +00001718 id->isprivate = 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001719 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001720 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001721 if (sent)
1722 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001723 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001724 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001725}
Damien Millereba71ba2000-04-29 23:57:08 +10001726
Damien Miller874d77b2000-10-14 16:23:11 +11001727/*
1728 * Send userauth request message specifying keyboard-interactive method.
1729 */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +00001730static int
djm@openbsd.org64c95982019-01-19 21:40:21 +00001731userauth_kbdint(struct ssh *ssh)
Damien Miller874d77b2000-10-14 16:23:11 +11001732{
djm@openbsd.org64c95982019-01-19 21:40:21 +00001733 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001734 int r;
Damien Miller874d77b2000-10-14 16:23:11 +11001735
djm@openbsd.org4a526942019-01-04 03:27:50 +00001736 if (authctxt->attempt_kbdint++ >= options.number_of_password_prompts)
Damien Miller874d77b2000-10-14 16:23:11 +11001737 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001738 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
djm@openbsd.org4a526942019-01-04 03:27:50 +00001739 if (authctxt->attempt_kbdint > 1 && !authctxt->info_req_seen) {
Ben Lindstrom7d199962001-09-12 18:29:00 +00001740 debug3("userauth_kbdint: disable: no info_req_seen");
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001741 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
Ben Lindstrom7d199962001-09-12 18:29:00 +00001742 return 0;
1743 }
Damien Miller874d77b2000-10-14 16:23:11 +11001744
1745 debug2("userauth_kbdint");
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001746 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1747 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1748 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1749 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1750 (r = sshpkt_put_cstring(ssh, "")) != 0 || /* lang */
1751 (r = sshpkt_put_cstring(ssh, options.kbd_interactive_devices ?
1752 options.kbd_interactive_devices : "")) != 0 ||
1753 (r = sshpkt_send(ssh)) != 0)
1754 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +11001755
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001756 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
Damien Miller874d77b2000-10-14 16:23:11 +11001757 return 1;
1758}
1759
1760/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001761 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001762 */
djm@openbsd.org5c68ea82019-02-11 09:44:42 +00001763static int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001764input_userauth_info_req(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller874d77b2000-10-14 16:23:11 +11001765{
markus@openbsd.org94583be2017-05-30 14:19:15 +00001766 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001767 char *name = NULL, *inst = NULL, *lang = NULL, *prompt = NULL;
1768 char *response = NULL;
1769 u_char echo = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001770 u_int num_prompts, i;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001771 int r;
Damien Miller874d77b2000-10-14 16:23:11 +11001772
1773 debug2("input_userauth_info_req");
1774
1775 if (authctxt == NULL)
1776 fatal("input_userauth_info_req: no authentication context");
1777
Ben Lindstrom7d199962001-09-12 18:29:00 +00001778 authctxt->info_req_seen = 1;
1779
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001780 if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 ||
1781 (r = sshpkt_get_cstring(ssh, &inst, NULL)) != 0 ||
1782 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1783 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +11001784 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001785 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001786 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001787 logit("%s", inst);
Damien Miller874d77b2000-10-14 16:23:11 +11001788
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001789 if ((r = sshpkt_get_u32(ssh, &num_prompts)) != 0)
1790 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +11001791 /*
1792 * Begin to build info response packet based on prompts requested.
1793 * We commit to providing the correct number of responses, so if
1794 * further on we run into a problem that prevents this, we have to
1795 * be sure and clean this up and send a correct error response.
1796 */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001797 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE)) != 0 ||
1798 (r = sshpkt_put_u32(ssh, num_prompts)) != 0)
1799 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +11001800
Ben Lindstrom551ea372001-06-05 18:56:16 +00001801 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001802 for (i = 0; i < num_prompts; i++) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001803 if ((r = sshpkt_get_cstring(ssh, &prompt, NULL)) != 0 ||
1804 (r = sshpkt_get_u8(ssh, &echo)) != 0)
1805 goto out;
Ben Lindstrom949974b2001-06-25 05:20:31 +00001806 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001807 if ((r = sshpkt_put_cstring(ssh, response)) != 0)
1808 goto out;
1809 freezero(response, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +10001810 free(prompt);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001811 response = prompt = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001812 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001813 /* done with parsing incoming message. */
1814 if ((r = sshpkt_get_end(ssh)) != 0 ||
1815 (r = sshpkt_add_padding(ssh, 64)) != 0)
1816 goto out;
1817 r = sshpkt_send(ssh);
1818 out:
1819 if (response)
1820 freezero(response, strlen(response));
1821 free(prompt);
1822 free(name);
1823 free(inst);
1824 free(lang);
1825 return r;
Damien Miller874d77b2000-10-14 16:23:11 +11001826}
Damien Miller62cee002000-09-23 17:15:56 +11001827
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001828static int
djm@openbsd.orged1df722019-01-19 21:34:45 +00001829ssh_keysign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001830 const u_char *data, size_t datalen)
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001831{
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001832 struct sshbuf *b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001833 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001834 pid_t pid;
deraadt@openbsd.orga1d29cc2019-05-15 04:43:31 +00001835 int r, to[2], from[2], status;
djm@openbsd.org64c95982019-01-19 21:40:21 +00001836 int sock = ssh_packet_get_connection_in(ssh);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001837 u_char rversion = 0, version = 2;
1838 void (*osigchld)(int);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001839
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001840 *sigp = NULL;
1841 *lenp = 0;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001842
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001843 if (stat(_PATH_SSH_KEY_SIGN, &st) == -1) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001844 error("%s: not installed: %s", __func__, strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001845 return -1;
1846 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001847 if (fflush(stdout) != 0) {
1848 error("%s: fflush: %s", __func__, strerror(errno));
1849 return -1;
1850 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001851 if (pipe(to) == -1) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001852 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001853 return -1;
1854 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001855 if (pipe(from) == -1) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001856 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001857 return -1;
1858 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001859 if ((pid = fork()) == -1) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001860 error("%s: fork: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001861 return -1;
1862 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001863 osigchld = signal(SIGCHLD, SIG_DFL);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001864 if (pid == 0) {
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001865 close(from[0]);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001866 if (dup2(from[1], STDOUT_FILENO) == -1)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001867 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001868 close(to[1]);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001869 if (dup2(to[0], STDIN_FILENO) == -1)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001870 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001871 close(from[1]);
1872 close(to[0]);
deraadt@openbsd.orga1d29cc2019-05-15 04:43:31 +00001873
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001874 if (dup2(sock, STDERR_FILENO + 1) == -1)
deraadt@openbsd.orga1d29cc2019-05-15 04:43:31 +00001875 fatal("%s: dup2: %s", __func__, strerror(errno));
1876 sock = STDERR_FILENO + 1;
1877 fcntl(sock, F_SETFD, 0); /* keep the socket on exec */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001878 closefrom(sock + 1);
deraadt@openbsd.orga1d29cc2019-05-15 04:43:31 +00001879
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001880 debug3("%s: [child] pid=%ld, exec %s",
1881 __func__, (long)getpid(), _PATH_SSH_KEY_SIGN);
mmcc@openbsd.org94141b72015-12-11 00:20:04 +00001882 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001883 fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001884 strerror(errno));
1885 }
1886 close(from[1]);
1887 close(to[0]);
djm@openbsd.orgc586d2d2019-05-31 03:20:07 +00001888 sock = STDERR_FILENO + 1;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001889
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001890 if ((b = sshbuf_new()) == NULL)
1891 fatal("%s: sshbuf_new failed", __func__);
1892 /* send # of sock, data to be signed */
djm@openbsd.org326e2fa2017-01-30 00:32:28 +00001893 if ((r = sshbuf_put_u32(b, sock)) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001894 (r = sshbuf_put_string(b, data, datalen)) != 0)
1895 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1896 if (ssh_msg_send(to[1], version, b) == -1)
1897 fatal("%s: couldn't send request", __func__);
1898 sshbuf_reset(b);
1899 r = ssh_msg_recv(from[0], b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001900 close(from[0]);
1901 close(to[1]);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001902 if (r < 0) {
1903 error("%s: no reply", __func__);
1904 goto fail;
1905 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001906
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001907 errno = 0;
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001908 while (waitpid(pid, &status, 0) == -1) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001909 if (errno != EINTR) {
1910 error("%s: waitpid %ld: %s",
1911 __func__, (long)pid, strerror(errno));
1912 goto fail;
1913 }
1914 }
1915 if (!WIFEXITED(status)) {
1916 error("%s: exited abnormally", __func__);
1917 goto fail;
1918 }
1919 if (WEXITSTATUS(status) != 0) {
1920 error("%s: exited with status %d",
1921 __func__, WEXITSTATUS(status));
1922 goto fail;
1923 }
1924 if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
1925 error("%s: buffer error: %s", __func__, ssh_err(r));
1926 goto fail;
1927 }
1928 if (rversion != version) {
1929 error("%s: bad version", __func__);
1930 goto fail;
1931 }
1932 if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
1933 error("%s: buffer error: %s", __func__, ssh_err(r));
1934 fail:
1935 signal(SIGCHLD, osigchld);
1936 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001937 return -1;
1938 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001939 signal(SIGCHLD, osigchld);
1940 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001941
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001942 return 0;
1943}
1944
djm@openbsd.org5c68ea82019-02-11 09:44:42 +00001945static int
djm@openbsd.org64c95982019-01-19 21:40:21 +00001946userauth_hostbased(struct ssh *ssh)
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001947{
djm@openbsd.org64c95982019-01-19 21:40:21 +00001948 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001949 struct sshkey *private = NULL;
1950 struct sshbuf *b = NULL;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001951 u_char *sig = NULL, *keyblob = NULL;
1952 char *fp = NULL, *chost = NULL, *lname = NULL;
1953 size_t siglen = 0, keylen = 0;
1954 int i, r, success = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001955
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001956 if (authctxt->ktypes == NULL) {
1957 authctxt->oktypes = xstrdup(options.hostbased_key_types);
1958 authctxt->ktypes = authctxt->oktypes;
1959 }
djm@openbsd.org1195f4c2015-01-08 10:14:08 +00001960
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001961 /*
1962 * Work through each listed type pattern in HostbasedKeyTypes,
1963 * trying each hostkey that matches the type in turn.
1964 */
1965 for (;;) {
1966 if (authctxt->active_ktype == NULL)
1967 authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
1968 if (authctxt->active_ktype == NULL ||
1969 *authctxt->active_ktype == '\0')
1970 break;
1971 debug3("%s: trying key type %s", __func__,
1972 authctxt->active_ktype);
1973
1974 /* check for a useful key */
1975 private = NULL;
1976 for (i = 0; i < authctxt->sensitive->nkeys; i++) {
1977 if (authctxt->sensitive->keys[i] == NULL ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001978 authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
1979 continue;
1980 if (match_pattern_list(
1981 sshkey_ssh_name(authctxt->sensitive->keys[i]),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001982 authctxt->active_ktype, 0) != 1)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001983 continue;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001984 /* we take and free the key */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001985 private = authctxt->sensitive->keys[i];
1986 authctxt->sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001987 break;
1988 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001989 /* Found one */
1990 if (private != NULL)
1991 break;
1992 /* No more keys of this type; advance */
1993 authctxt->active_ktype = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001994 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001995 if (private == NULL) {
1996 free(authctxt->oktypes);
1997 authctxt->oktypes = authctxt->ktypes = NULL;
1998 authctxt->active_ktype = NULL;
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001999 debug("No more client hostkeys for hostbased authentication.");
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002000 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002001 }
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00002002
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002003 if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
2004 SSH_FP_DEFAULT)) == NULL) {
2005 error("%s: sshkey_fingerprint failed", __func__);
2006 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002007 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002008 debug("%s: trying hostkey %s %s",
2009 __func__, sshkey_ssh_name(private), fp);
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00002010
Damien Miller91c18472001-11-12 11:02:03 +11002011 /* figure out a name for the client host */
djm@openbsd.org64c95982019-01-19 21:40:21 +00002012 lname = get_local_name(ssh_packet_get_connection_in(ssh));
2013 if (lname == NULL) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002014 error("%s: cannot get local ipaddr/name", __func__);
2015 goto out;
Damien Miller91c18472001-11-12 11:02:03 +11002016 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002017
2018 /* XXX sshbuf_put_stringf? */
2019 xasprintf(&chost, "%s.", lname);
2020 debug2("%s: chost %s", __func__, chost);
Damien Miller91c18472001-11-12 11:02:03 +11002021
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002022 /* construct data */
2023 if ((b = sshbuf_new()) == NULL) {
2024 error("%s: sshbuf_new failed", __func__);
2025 goto out;
2026 }
2027 if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
2028 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
2029 goto out;
2030 }
2031 if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
2032 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2033 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +00002034 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002035 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002036 (r = sshbuf_put_cstring(b, sshkey_ssh_name(private))) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002037 (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
2038 (r = sshbuf_put_cstring(b, chost)) != 0 ||
2039 (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
2040 error("%s: buffer error: %s", __func__, ssh_err(r));
2041 goto out;
2042 }
2043
2044#ifdef DEBUG_PK
2045 sshbuf_dump(b, stderr);
2046#endif
djm@openbsd.orged1df722019-01-19 21:34:45 +00002047 if ((r = ssh_keysign(ssh, private, &sig, &siglen,
2048 sshbuf_ptr(b), sshbuf_len(b))) != 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002049 error("sign using hostkey %s %s failed",
2050 sshkey_ssh_name(private), fp);
2051 goto out;
2052 }
2053 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2054 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
2055 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
2056 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002057 (r = sshpkt_put_cstring(ssh, sshkey_ssh_name(private))) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002058 (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
2059 (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
2060 (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
2061 (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
2062 (r = sshpkt_send(ssh)) != 0) {
2063 error("%s: packet error: %s", __func__, ssh_err(r));
2064 goto out;
2065 }
2066 success = 1;
2067
2068 out:
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002069 if (sig != NULL)
2070 freezero(sig, siglen);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002071 free(keyblob);
2072 free(lname);
2073 free(fp);
2074 free(chost);
2075 sshkey_free(private);
2076 sshbuf_free(b);
2077
2078 return success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002079}
2080
Damien Miller62cee002000-09-23 17:15:56 +11002081/* find auth method */
2082
Damien Miller62cee002000-09-23 17:15:56 +11002083/*
2084 * given auth method name, if configurable options permit this method fill
2085 * in auth_ident field and return true, otherwise return false.
2086 */
Ben Lindstrombba81212001-06-25 05:01:22 +00002087static int
Damien Miller62cee002000-09-23 17:15:56 +11002088authmethod_is_enabled(Authmethod *method)
2089{
2090 if (method == NULL)
2091 return 0;
2092 /* return false if options indicate this method is disabled */
2093 if (method->enabled == NULL || *method->enabled == 0)
2094 return 0;
2095 /* return false if batch mode is enabled but method needs interactive mode */
2096 if (method->batch_flag != NULL && *method->batch_flag != 0)
2097 return 0;
2098 return 1;
2099}
2100
Ben Lindstrombba81212001-06-25 05:01:22 +00002101static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11002102authmethod_lookup(const char *name)
2103{
2104 Authmethod *method = NULL;
2105 if (name != NULL)
2106 for (method = authmethods; method->name != NULL; method++)
2107 if (strcmp(name, method->name) == 0)
2108 return method;
2109 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
2110 return NULL;
2111}
2112
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002113/* XXX internal state */
2114static Authmethod *current = NULL;
2115static char *supported = NULL;
2116static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00002117
Damien Miller62cee002000-09-23 17:15:56 +11002118/*
2119 * Given the authentication method list sent by the server, return the
2120 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00002121 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00002122 */
Ben Lindstrombba81212001-06-25 05:01:22 +00002123static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11002124authmethod_get(char *authlist)
2125{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002126 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00002127 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00002128
Damien Miller62cee002000-09-23 17:15:56 +11002129 /* Use a suitable default if we're passed a nil list. */
2130 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002131 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11002132
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002133 if (supported == NULL || strcmp(authlist, supported) != 0) {
2134 debug3("start over, passed a different list %s", authlist);
Darren Tuckera627d422013-06-02 07:31:17 +10002135 free(supported);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002136 supported = xstrdup(authlist);
2137 preferred = options.preferred_authentications;
2138 debug3("preferred %s", preferred);
2139 current = NULL;
2140 } else if (current != NULL && authmethod_is_enabled(current))
2141 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10002142
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002143 for (;;) {
2144 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00002145 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002146 current = NULL;
2147 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11002148 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002149 preferred += next;
2150 debug3("authmethod_lookup %s", name);
2151 debug3("remaining preferred: %s", preferred);
2152 if ((current = authmethod_lookup(name)) != NULL &&
2153 authmethod_is_enabled(current)) {
2154 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00002155 debug("Next authentication method: %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002156 free(name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002157 return current;
2158 }
Darren Tuckere52a2602013-06-06 08:22:05 +10002159 free(name);
Damien Millereba71ba2000-04-29 23:57:08 +10002160 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002161}
Damien Miller62cee002000-09-23 17:15:56 +11002162
Ben Lindstrom79073822001-07-04 03:42:30 +00002163static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002164authmethods_get(void)
2165{
2166 Authmethod *method = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002167 struct sshbuf *b;
Damien Miller0e3b8722002-01-22 23:26:38 +11002168 char *list;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002169 int r;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002170
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002171 if ((b = sshbuf_new()) == NULL)
2172 fatal("%s: sshbuf_new failed", __func__);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002173 for (method = authmethods; method->name != NULL; method++) {
2174 if (authmethod_is_enabled(method)) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002175 if ((r = sshbuf_putf(b, "%s%s",
2176 sshbuf_len(b) ? "," : "", method->name)) != 0)
2177 fatal("%s: buffer error: %s",
2178 __func__, ssh_err(r));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002179 }
Damien Miller62cee002000-09-23 17:15:56 +11002180 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002181 if ((list = sshbuf_dup_string(b)) == NULL)
djm@openbsd.org1a31d022016-05-02 08:49:03 +00002182 fatal("%s: sshbuf_dup_string failed", __func__);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002183 sshbuf_free(b);
Damien Miller0e3b8722002-01-22 23:26:38 +11002184 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10002185}