blob: f521f4a55649fd7a8b72125f923b4f8cb2cd45bb [file] [log] [blame]
djm@openbsd.org64c95982019-01-19 21:40:21 +00001/* $OpenBSD: sshconnect2.c,v 1.295 2019/01/19 21:40:21 djm Exp $ */
Damien Millereba71ba2000-04-29 23:57:08 +10002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Miller01ed2272008-11-05 16:20:46 +11004 * Copyright (c) 2008 Damien Miller. All rights reserved.
Damien Millereba71ba2000-04-29 23:57:08 +10005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100014 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
Damien Miller2eb63402006-03-15 11:09:42 +110028
Damien Miller9cf6d072006-03-15 11:29:24 +110029#include <sys/types.h>
Damien Millerd7834352006-08-05 12:39:39 +100030#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110031#include <sys/wait.h>
Damien Millerf17883e2006-03-15 11:45:54 +110032#include <sys/stat.h>
Damien Millereba71ba2000-04-29 23:57:08 +100033
Darren Tucker39972492006-07-12 22:22:46 +100034#include <errno.h>
Darren Tucker6e7fe1c2010-01-08 17:07:22 +110035#include <fcntl.h>
Darren Tuckerf520ea12007-05-20 15:11:33 +100036#include <netdb.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <pwd.h>
38#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100039#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100040#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100041#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100042#include <unistd.h>
Damien Miller63b4bcd2013-03-20 12:55:14 +110043#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
Damien Miller7ba0ca72008-07-17 18:57:06 +100044#include <vis.h>
Damien Miller2e28d862008-07-17 19:15:43 +100045#endif
Darren Tucker39972492006-07-12 22:22:46 +100046
Damien Miller9c617692003-05-14 14:31:11 +100047#include "openbsd-compat/sys-queue.h"
48
Damien Millerd7834352006-08-05 12:39:39 +100049#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100050#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "ssh2.h"
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.org0a843d92018-12-27 03:25:24 +0000204 ssh->kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
205 ssh->kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
206 ssh->kex->kex[KEX_DH_GRP14_SHA256] = kexdh_client;
207 ssh->kex->kex[KEX_DH_GRP16_SHA512] = kexdh_client;
208 ssh->kex->kex[KEX_DH_GRP18_SHA512] = kexdh_client;
209 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.org0a843d92018-12-27 03:25:24 +0000212 ssh->kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100213# endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000214#endif
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000215 ssh->kex->kex[KEX_C25519_SHA256] = kexc25519_client;
216 ssh->kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100217
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000218 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &ssh->kex->done);
Damien Miller874d77b2000-10-14 16:23:11 +1100219
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000220 /* remove ext-info from the KEX proposals for rekeying */
221 myproposal[PROPOSAL_KEX_ALGS] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000222 compat_kex_proposal(options.kex_algorithms);
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000223 if ((r = kex_prop2buf(ssh->kex->my, myproposal)) != 0)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000224 fatal("kex_prop2buf: %s", ssh_err(r));
Darren Tucker36331b52010-01-08 16:50:41 +1100225
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000226 session_id2 = ssh->kex->session_id;
227 session_id2_len = ssh->kex->session_id_len;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000228
Damien Miller874d77b2000-10-14 16:23:11 +1100229#ifdef DEBUG_KEXDH
230 /* send 1st encrypted/maced/compressed message */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000231 if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 ||
232 (r = sshpkt_put_cstring(ssh, "markus")) != 0 ||
markus@openbsd.org394a8422018-07-11 18:55:11 +0000233 (r = sshpkt_send(ssh)) != 0 ||
234 (r = ssh_packet_write_wait(ssh)) != 0)
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000235 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100236#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000237}
Damien Millerb1715dc2000-05-30 13:44:51 +1000238
Damien Millereba71ba2000-04-29 23:57:08 +1000239/*
240 * Authenticate user
241 */
Damien Miller62cee002000-09-23 17:15:56 +1100242
djm@openbsd.org141efe42015-01-14 20:05:27 +0000243typedef struct cauthctxt Authctxt;
244typedef struct cauthmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000245typedef struct identity Identity;
246typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100247
Damien Miller280ecfb2003-05-14 13:46:00 +1000248struct identity {
249 TAILQ_ENTRY(identity) next;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000250 int agent_fd; /* >=0 if agent supports key */
251 struct sshkey *key; /* public/private key */
Damien Miller280ecfb2003-05-14 13:46:00 +1000252 char *filename; /* comment for agent-only keys */
253 int tried;
254 int isprivate; /* key points to the private key */
Damien Miller5ceddc32013-02-15 12:18:32 +1100255 int userprovided;
Damien Miller280ecfb2003-05-14 13:46:00 +1000256};
257TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100258
djm@openbsd.org141efe42015-01-14 20:05:27 +0000259struct cauthctxt {
Damien Miller62cee002000-09-23 17:15:56 +1100260 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000261 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100262 const char *host;
263 const char *service;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000264 struct cauthmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000265 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000266 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000267 /* pubkey */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000268 struct idlist keys;
269 int agent_fd;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000270 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000271 Sensitive *sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000272 char *oktypes, *ktypes;
273 const char *active_ktype;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000274 /* kbd-interactive */
275 int info_req_seen;
djm@openbsd.org4a526942019-01-04 03:27:50 +0000276 int attempt_kbdint;
277 /* password */
278 int attempt_passwd;
Darren Tucker0efd1552003-08-26 11:49:55 +1000279 /* generic */
280 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100281};
djm@openbsd.org141efe42015-01-14 20:05:27 +0000282
283struct cauthmethod {
Damien Miller62cee002000-09-23 17:15:56 +1100284 char *name; /* string to compare against server's list */
djm@openbsd.org64c95982019-01-19 21:40:21 +0000285 int (*userauth)(struct ssh *ssh);
286 void (*cleanup)(struct ssh *ssh);
Damien Miller62cee002000-09-23 17:15:56 +1100287 int *enabled; /* flag in option struct that enables method */
288 int *batch_flag; /* flag in option struct that disables method */
289};
290
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000291int input_userauth_service_accept(int, u_int32_t, struct ssh *);
292int input_userauth_ext_info(int, u_int32_t, struct ssh *);
293int input_userauth_success(int, u_int32_t, struct ssh *);
294int input_userauth_success_unexpected(int, u_int32_t, struct ssh *);
295int input_userauth_failure(int, u_int32_t, struct ssh *);
296int input_userauth_banner(int, u_int32_t, struct ssh *);
297int input_userauth_error(int, u_int32_t, struct ssh *);
298int input_userauth_info_req(int, u_int32_t, struct ssh *);
299int input_userauth_pk_ok(int, u_int32_t, struct ssh *);
300int input_userauth_passwd_changereq(int, u_int32_t, struct ssh *);
Damien Miller874d77b2000-10-14 16:23:11 +1100301
djm@openbsd.org64c95982019-01-19 21:40:21 +0000302int userauth_none(struct ssh *);
303int userauth_pubkey(struct ssh *);
304int userauth_passwd(struct ssh *);
305int userauth_kbdint(struct ssh *);
306int userauth_hostbased(struct ssh *);
Damien Miller62cee002000-09-23 17:15:56 +1100307
Darren Tucker0efd1552003-08-26 11:49:55 +1000308#ifdef GSSAPI
djm@openbsd.org64c95982019-01-19 21:40:21 +0000309int userauth_gssapi(struct ssh *);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000310int input_gssapi_response(int type, u_int32_t, struct ssh *);
311int input_gssapi_token(int type, u_int32_t, struct ssh *);
312int input_gssapi_hash(int type, u_int32_t, struct ssh *);
313int input_gssapi_error(int, u_int32_t, struct ssh *);
314int input_gssapi_errtok(int, u_int32_t, struct ssh *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000315#endif
316
djm@openbsd.org64c95982019-01-19 21:40:21 +0000317void userauth(struct ssh *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000318
djm@openbsd.org64c95982019-01-19 21:40:21 +0000319static int sign_and_send_pubkey(struct ssh *ssh, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000320static void pubkey_prepare(Authctxt *);
321static void pubkey_cleanup(Authctxt *);
djm@openbsd.orgb9844a42016-12-04 23:54:02 +0000322static void pubkey_reset(Authctxt *);
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000323static struct sshkey *load_identity_file(Identity *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000324
Ben Lindstrombba81212001-06-25 05:01:22 +0000325static Authmethod *authmethod_get(char *authlist);
326static Authmethod *authmethod_lookup(const char *name);
327static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100328
329Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000330#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100331 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000332 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100333 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000334 &options.gss_authentication,
335 NULL},
336#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000337 {"hostbased",
338 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100339 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000340 &options.hostbased_authentication,
341 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000342 {"publickey",
343 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100344 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000345 &options.pubkey_authentication,
346 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100347 {"keyboard-interactive",
348 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100349 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100350 &options.kbd_interactive_authentication,
351 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000352 {"password",
353 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100354 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000355 &options.password_authentication,
356 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100357 {"none",
358 userauth_none,
359 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100360 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100361 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100362 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100363};
364
365void
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000366ssh_userauth2(struct ssh *ssh, const char *local_user,
367 const char *server_user, char *host, Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100368{
369 Authctxt authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000370 int r;
Damien Miller62cee002000-09-23 17:15:56 +1100371
Ben Lindstrom551ea372001-06-05 18:56:16 +0000372 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000373 options.kbd_interactive_authentication = 1;
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000374 if (options.preferred_authentications == NULL)
375 options.preferred_authentications = authmethods_get();
376
Damien Miller62cee002000-09-23 17:15:56 +1100377 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000378 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller62cee002000-09-23 17:15:56 +1100379 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000380 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100381 authctxt.host = host;
382 authctxt.service = "ssh-connection"; /* service name */
383 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100384 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000385 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000386 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000387 authctxt.sensitive = sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000388 authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000389 authctxt.info_req_seen = 0;
djm@openbsd.org4a526942019-01-04 03:27:50 +0000390 authctxt.attempt_kbdint = 0;
391 authctxt.attempt_passwd = 0;
djm@openbsd.orgf14564c2015-01-15 11:04:36 +0000392 authctxt.agent_fd = -1;
djm@openbsd.orgaaed6352018-11-16 02:46:20 +0000393 pubkey_prepare(&authctxt);
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000394 if (authctxt.method == NULL) {
395 fatal("%s: internal error: cannot send userauth none request",
396 __func__);
397 }
Damien Miller62cee002000-09-23 17:15:56 +1100398
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000399 if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
400 (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
401 (r = sshpkt_send(ssh)) != 0)
402 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller62cee002000-09-23 17:15:56 +1100403
markus@openbsd.org94583be2017-05-30 14:19:15 +0000404 ssh->authctxt = &authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000405 ssh_dispatch_init(ssh, &input_userauth_error);
406 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
407 ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
markus@openbsd.org92e9fe62017-05-31 07:00:13 +0000408 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt.success); /* loop until success */
markus@openbsd.org94583be2017-05-30 14:19:15 +0000409 ssh->authctxt = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100410
Damien Miller280ecfb2003-05-14 13:46:00 +1000411 pubkey_cleanup(&authctxt);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000412 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
Damien Millerf842fcb2003-05-15 12:01:28 +1000413
dtucker@openbsd.orgf31c6542016-09-22 02:29:57 +0000414 if (!authctxt.success)
415 fatal("Authentication failed.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000416 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100417}
Damien Millerf842fcb2003-05-15 12:01:28 +1000418
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000419/* ARGSUSED */
420int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000421input_userauth_service_accept(int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000422{
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000423 int r;
424
425 if (ssh_packet_remaining(ssh) > 0) {
426 char *reply;
427
428 if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
429 goto out;
430 debug2("service_accept: %s", reply);
431 free(reply);
432 } else {
433 debug2("buggy server: service_accept w/o service");
434 }
435 if ((r = sshpkt_get_end(ssh)) != 0)
436 goto out;
437 debug("SSH2_MSG_SERVICE_ACCEPT received");
438
439 /* initial userauth request */
djm@openbsd.org64c95982019-01-19 21:40:21 +0000440 userauth_none(ssh);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000441
442 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error);
443 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
444 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
445 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
446 r = 0;
447 out:
448 return r;
449}
450
451/* ARGSUSED */
452int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000453input_userauth_ext_info(int type, u_int32_t seqnr, struct ssh *ssh)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000454{
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000455 return kex_input_ext_info(type, seqnr, ssh);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000456}
457
Damien Miller62cee002000-09-23 17:15:56 +1100458void
djm@openbsd.org64c95982019-01-19 21:40:21 +0000459userauth(struct ssh *ssh, char *authlist)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000460{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000461 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000462
Damien Miller01ed2272008-11-05 16:20:46 +1100463 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
djm@openbsd.org64c95982019-01-19 21:40:21 +0000464 authctxt->method->cleanup(ssh);
Damien Miller01ed2272008-11-05 16:20:46 +1100465
Darren Tuckera627d422013-06-02 07:31:17 +1000466 free(authctxt->methoddata);
467 authctxt->methoddata = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000468 if (authlist == NULL) {
469 authlist = authctxt->authlist;
470 } else {
Darren Tuckera627d422013-06-02 07:31:17 +1000471 free(authctxt->authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000472 authctxt->authlist = authlist;
473 }
474 for (;;) {
475 Authmethod *method = authmethod_get(authlist);
476 if (method == NULL)
dtucker@openbsd.org4626e392017-06-14 00:31:38 +0000477 fatal("%s@%s: Permission denied (%s).",
478 authctxt->server_user, authctxt->host, authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000479 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000480
481 /* reset the per method handler */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000482 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_PER_METHOD_MIN,
Damien Millerf842fcb2003-05-15 12:01:28 +1000483 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
484
485 /* and try new method */
djm@openbsd.org64c95982019-01-19 21:40:21 +0000486 if (method->userauth(ssh) != 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000487 debug2("we sent a %s packet, wait for reply", method->name);
488 break;
489 } else {
490 debug2("we did not send a packet, disable method");
491 method->enabled = NULL;
492 }
493 }
494}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000495
Damien Millerf7475d72008-11-03 19:26:18 +1100496/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000497int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000498input_userauth_error(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100499{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000500 fatal("%s: bad message during authentication: type %d", __func__, type);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000501 return 0;
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000502}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000503
Damien Millerf7475d72008-11-03 19:26:18 +1100504/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000505int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000506input_userauth_banner(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000507{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000508 char *msg = NULL;
djm@openbsd.orged1df722019-01-19 21:34:45 +0000509 size_t len;
510 int r;
Darren Tucker79644822003-10-08 17:37:58 +1000511
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000512 debug3("%s", __func__);
djm@openbsd.orged1df722019-01-19 21:34:45 +0000513 if ((r = sshpkt_get_cstring(ssh, &msg, &len)) != 0 ||
djm@openbsd.org64c95982019-01-19 21:40:21 +0000514 (r = sshpkt_get_cstring(ssh, NULL, NULL)) != 0)
djm@openbsd.orged1df722019-01-19 21:34:45 +0000515 goto out;
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000516 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO)
517 fmprintf(stderr, "%s", msg);
djm@openbsd.orged1df722019-01-19 21:34:45 +0000518 r = 0;
519 out:
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000520 free(msg);
djm@openbsd.orged1df722019-01-19 21:34:45 +0000521 return r;
Damien Miller62cee002000-09-23 17:15:56 +1100522}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000523
Damien Millerf7475d72008-11-03 19:26:18 +1100524/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000525int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000526input_userauth_success(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100527{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000528 Authctxt *authctxt = ssh->authctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100529
Damien Miller62cee002000-09-23 17:15:56 +1100530 if (authctxt == NULL)
djm@openbsd.org64c95982019-01-19 21:40:21 +0000531 fatal("%s: no authentication context", __func__);
Darren Tuckera627d422013-06-02 07:31:17 +1000532 free(authctxt->authlist);
533 authctxt->authlist = NULL;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100534 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
djm@openbsd.org64c95982019-01-19 21:40:21 +0000535 authctxt->method->cleanup(ssh);
Darren Tuckera627d422013-06-02 07:31:17 +1000536 free(authctxt->methoddata);
537 authctxt->methoddata = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100538 authctxt->success = 1; /* break out */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000539 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100540}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000541
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000542int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000543input_userauth_success_unexpected(int type, u_int32_t seq, struct ssh *ssh)
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100544{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000545 Authctxt *authctxt = ssh->authctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100546
547 if (authctxt == NULL)
548 fatal("%s: no authentication context", __func__);
549
550 fatal("Unexpected authentication success during %s.",
551 authctxt->method->name);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000552 return 0;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100553}
554
Damien Millerf7475d72008-11-03 19:26:18 +1100555/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000556int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000557input_userauth_failure(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100558{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000559 Authctxt *authctxt = ssh->authctxt;
Damien Miller62cee002000-09-23 17:15:56 +1100560 char *authlist = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000561 u_char partial;
562 int r;
Damien Miller62cee002000-09-23 17:15:56 +1100563
564 if (authctxt == NULL)
565 fatal("input_userauth_failure: no authentication context");
566
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000567 if ((r = sshpkt_get_cstring(ssh, &authlist, NULL)) != 0 ||
568 (r = sshpkt_get_u8(ssh, &partial)) != 0 ||
569 (r = sshpkt_get_end(ssh)) != 0)
570 goto out;
Damien Miller62cee002000-09-23 17:15:56 +1100571
Damien Miller62e9c4f2013-04-23 15:15:49 +1000572 if (partial != 0) {
dtucker@openbsd.org83cbca62016-07-22 05:46:11 +0000573 verbose("Authenticated with partial success.");
Damien Miller62e9c4f2013-04-23 15:15:49 +1000574 /* reset state */
djm@openbsd.orgb9844a42016-12-04 23:54:02 +0000575 pubkey_reset(authctxt);
Damien Miller62e9c4f2013-04-23 15:15:49 +1000576 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000577 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100578
djm@openbsd.org64c95982019-01-19 21:40:21 +0000579 userauth(ssh, authlist);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000580 authlist = NULL;
581 out:
582 free(authlist);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000583 return 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000584}
Damien Millerf7475d72008-11-03 19:26:18 +1100585
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000586/*
587 * Format an identity for logging including filename, key type, fingerprint
588 * and location (agent, etc.). Caller must free.
589 */
590static char *
591format_identity(Identity *id)
592{
593 char *fp = NULL, *ret = NULL;
594
595 if (id->key != NULL) {
596 fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
597 SSH_FP_DEFAULT);
598 }
599 xasprintf(&ret, "%s %s%s%s%s%s%s",
600 id->filename,
601 id->key ? sshkey_type(id->key) : "", id->key ? " " : "",
602 fp ? fp : "",
603 id->userprovided ? " explicit" : "",
604 (id->key && (id->key->flags & SSHKEY_FLAG_EXT)) ? " token" : "",
605 id->agent_fd != -1 ? " agent" : "");
606 free(fp);
607 return ret;
608}
609
Damien Millerf7475d72008-11-03 19:26:18 +1100610/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000611int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000612input_userauth_pk_ok(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000613{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000614 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000615 struct sshkey *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000616 Identity *id = NULL;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000617 int pktype, found = 0, sent = 0;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000618 size_t blen;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000619 char *pkalg = NULL, *fp = NULL, *ident = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000620 u_char *pkblob = NULL;
621 int r;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000622
623 if (authctxt == NULL)
624 fatal("input_userauth_pk_ok: no authentication context");
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000625
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000626 if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
627 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
628 (r = sshpkt_get_end(ssh)) != 0)
629 goto done;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000630
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000631 if ((pktype = sshkey_type_from_name(pkalg)) == KEY_UNSPEC) {
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000632 debug("%s: server sent unknown pkalg %s", __func__, pkalg);
Damien Miller280ecfb2003-05-14 13:46:00 +1000633 goto done;
634 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000635 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
636 debug("no key from blob. pkalg %s: %s", pkalg, ssh_err(r));
Damien Miller280ecfb2003-05-14 13:46:00 +1000637 goto done;
638 }
639 if (key->type != pktype) {
640 error("input_userauth_pk_ok: type mismatch "
641 "for decoded key (received %d, expected %d)",
642 key->type, pktype);
643 goto done;
644 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000645
Darren Tuckerd05b6012003-10-15 15:55:59 +1000646 /*
647 * search keys in the reverse order, because last candidate has been
648 * moved to the end of the queue. this also avoids confusion by
649 * duplicate keys
650 */
Damien Miller0b51a522004-04-20 20:07:19 +1000651 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000652 if (sshkey_equal(key, id->key)) {
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000653 found = 1;
Damien Miller280ecfb2003-05-14 13:46:00 +1000654 break;
655 }
656 }
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000657 if (!found || id == NULL) {
658 fp = sshkey_fingerprint(key, options.fingerprint_hash,
659 SSH_FP_DEFAULT);
660 error("%s: server replied with unknown key: %s %s", __func__,
661 sshkey_type(key), fp == NULL ? "<ERROR>" : fp);
662 goto done;
663 }
664 ident = format_identity(id);
665 debug("Server accepts key: %s", ident);
djm@openbsd.org64c95982019-01-19 21:40:21 +0000666 sent = sign_and_send_pubkey(ssh, id);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000667 r = 0;
668 done:
669 sshkey_free(key);
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000670 free(ident);
671 free(fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000672 free(pkalg);
673 free(pkblob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000674
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000675 /* try another method if we did not send a packet */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000676 if (r == 0 && sent == 0)
djm@openbsd.org64c95982019-01-19 21:40:21 +0000677 userauth(ssh, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000678 return r;
Damien Miller62cee002000-09-23 17:15:56 +1100679}
680
Darren Tucker0efd1552003-08-26 11:49:55 +1000681#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100682int
djm@openbsd.org64c95982019-01-19 21:40:21 +0000683userauth_gssapi(struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000684{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000685 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000686 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100687 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000688 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000689 OM_uint32 min;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000690 int r, ok = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000691
692 /* Try one GSSAPI method at a time, rather than sending them all at
693 * once. */
694
Darren Tucker56afe142003-11-03 20:06:14 +1100695 if (gss_supported == NULL)
696 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000697
698 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100699 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000700 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100701 if (gss_supported->elements[mech].length < 128 &&
djm@openbsd.org84623e02018-06-26 02:02:36 +0000702 ssh_gssapi_check_mechanism(&gssctxt,
Damien Millera1cb9f32006-08-19 00:33:34 +1000703 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000704 ok = 1; /* Mechanism works */
705 } else {
706 mech++;
707 }
708 }
709
Damien Millera1cb9f32006-08-19 00:33:34 +1000710 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000711 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000712
713 authctxt->methoddata=(void *)gssctxt;
714
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000715 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
716 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
717 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
718 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
719 (r = sshpkt_put_u32(ssh, 1)) != 0 ||
720 (r = sshpkt_put_u32(ssh,
721 (gss_supported->elements[mech].length) + 2)) != 0 ||
722 (r = sshpkt_put_u8(ssh, SSH_GSS_OIDTYPE)) != 0 ||
723 (r = sshpkt_put_u8(ssh,
724 gss_supported->elements[mech].length)) != 0 ||
725 (r = sshpkt_put(ssh,
726 gss_supported->elements[mech].elements,
727 gss_supported->elements[mech].length)) != 0 ||
728 (r = sshpkt_send(ssh)) != 0)
729 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +1000730
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000731 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
732 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
733 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
734 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000735
736 mech++; /* Move along to next candidate */
737
738 return 1;
739}
740
Damien Miller91c6aa42003-11-17 21:20:18 +1100741static OM_uint32
djm@openbsd.org39896b72017-05-31 05:08:46 +0000742process_gssapi_token(struct ssh *ssh, gss_buffer_t recv_tok)
Damien Miller91c6aa42003-11-17 21:20:18 +1100743{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000744 Authctxt *authctxt = ssh->authctxt;
Damien Miller91c6aa42003-11-17 21:20:18 +1100745 Gssctxt *gssctxt = authctxt->methoddata;
746 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000747 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
748 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100749 OM_uint32 status, ms, flags;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000750 int r;
Damien Miller787b2ec2003-11-21 23:56:47 +1100751
Damien Miller91c6aa42003-11-17 21:20:18 +1100752 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100753 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100754
755 if (send_tok.length > 0) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000756 u_char type = GSS_ERROR(status) ?
757 SSH2_MSG_USERAUTH_GSSAPI_ERRTOK :
758 SSH2_MSG_USERAUTH_GSSAPI_TOKEN;
Damien Miller787b2ec2003-11-21 23:56:47 +1100759
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000760 if ((r = sshpkt_start(ssh, type)) != 0 ||
761 (r = sshpkt_put_string(ssh, send_tok.value,
762 send_tok.length)) != 0 ||
763 (r = sshpkt_send(ssh)) != 0)
764 fatal("%s: %s", __func__, ssh_err(r));
765
Damien Miller91c6aa42003-11-17 21:20:18 +1100766 gss_release_buffer(&ms, &send_tok);
767 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100768
Damien Miller91c6aa42003-11-17 21:20:18 +1100769 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100770 /* send either complete or MIC, depending on mechanism */
771 if (!(flags & GSS_C_INTEG_FLAG)) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000772 if ((r = sshpkt_start(ssh,
773 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE)) != 0 ||
774 (r = sshpkt_send(ssh)) != 0)
775 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0425d402003-11-17 22:18:21 +1100776 } else {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000777 struct sshbuf *b;
778
779 if ((b = sshbuf_new()) == NULL)
780 fatal("%s: sshbuf_new failed", __func__);
781 ssh_gssapi_buildmic(b, authctxt->server_user,
Damien Miller0425d402003-11-17 22:18:21 +1100782 authctxt->service, "gssapi-with-mic");
783
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000784 if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
785 fatal("%s: sshbuf_mutable_ptr failed", __func__);
786 gssbuf.length = sshbuf_len(b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100787
Damien Miller0425d402003-11-17 22:18:21 +1100788 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100789
Damien Miller0425d402003-11-17 22:18:21 +1100790 if (!GSS_ERROR(status)) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000791 if ((r = sshpkt_start(ssh,
792 SSH2_MSG_USERAUTH_GSSAPI_MIC)) != 0 ||
793 (r = sshpkt_put_string(ssh, mic.value,
794 mic.length)) != 0 ||
795 (r = sshpkt_send(ssh)) != 0)
796 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0425d402003-11-17 22:18:21 +1100797 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100798
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000799 sshbuf_free(b);
Damien Miller0425d402003-11-17 22:18:21 +1100800 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100801 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100802 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100803
Damien Miller91c6aa42003-11-17 21:20:18 +1100804 return status;
805}
806
Damien Millerf7475d72008-11-03 19:26:18 +1100807/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000808int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000809input_gssapi_response(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000810{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000811 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000812 Gssctxt *gssctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000813 size_t oidlen;
814 u_char *oidv = NULL;
815 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000816
817 if (authctxt == NULL)
818 fatal("input_gssapi_response: no authentication context");
819 gssctxt = authctxt->methoddata;
820
821 /* Setup our OID */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000822 if ((r = sshpkt_get_string(ssh, &oidv, &oidlen)) != 0)
823 goto done;
Darren Tucker0efd1552003-08-26 11:49:55 +1000824
Darren Tucker655a5e02003-11-03 20:09:03 +1100825 if (oidlen <= 2 ||
826 oidv[0] != SSH_GSS_OIDTYPE ||
827 oidv[1] != oidlen - 2) {
828 debug("Badly encoded mechanism OID received");
829 userauth(authctxt, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000830 goto ok;
Darren Tucker0efd1552003-08-26 11:49:55 +1000831 }
832
Darren Tucker655a5e02003-11-03 20:09:03 +1100833 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
834 fatal("Server returned different OID than expected");
835
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000836 if ((r = sshpkt_get_end(ssh)) != 0)
837 goto done;
Darren Tucker0efd1552003-08-26 11:49:55 +1000838
djm@openbsd.org39896b72017-05-31 05:08:46 +0000839 if (GSS_ERROR(process_gssapi_token(ssh, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000840 /* Start again with next method on list */
841 debug("Trying to start again");
842 userauth(authctxt, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000843 goto ok;
Darren Tucker0efd1552003-08-26 11:49:55 +1000844 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000845 ok:
846 r = 0;
847 done:
848 free(oidv);
849 return r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000850}
851
Damien Millerf7475d72008-11-03 19:26:18 +1100852/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000853int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000854input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000855{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000856 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000857 gss_buffer_desc recv_tok;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000858 u_char *p = NULL;
859 size_t len;
Damien Miller91c6aa42003-11-17 21:20:18 +1100860 OM_uint32 status;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000861 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000862
863 if (authctxt == NULL)
864 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000865
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000866 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
867 (r = sshpkt_get_end(ssh)) != 0)
868 goto out;
Darren Tucker0efd1552003-08-26 11:49:55 +1000869
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000870 recv_tok.value = p;
871 recv_tok.length = len;
djm@openbsd.org39896b72017-05-31 05:08:46 +0000872 status = process_gssapi_token(ssh, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000873
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000874 /* Start again with the next method in the list */
Darren Tucker0efd1552003-08-26 11:49:55 +1000875 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000876 userauth(authctxt, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000877 /* ok */
Darren Tucker0efd1552003-08-26 11:49:55 +1000878 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000879 r = 0;
880 out:
881 free(p);
882 return r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000883}
884
Damien Millerf7475d72008-11-03 19:26:18 +1100885/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000886int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000887input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000888{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000889 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000890 Gssctxt *gssctxt;
891 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
892 gss_buffer_desc recv_tok;
Damien Millerd677ad12013-04-23 15:18:51 +1000893 OM_uint32 ms;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000894 u_char *p = NULL;
895 size_t len;
896 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000897
898 if (authctxt == NULL)
899 fatal("input_gssapi_response: no authentication context");
900 gssctxt = authctxt->methoddata;
901
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000902 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
903 (r = sshpkt_get_end(ssh)) != 0) {
904 free(p);
905 return r;
906 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000907
908 /* Stick it into GSSAPI and see what it says */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000909 recv_tok.value = p;
910 recv_tok.length = len;
Damien Millerd677ad12013-04-23 15:18:51 +1000911 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000912 &recv_tok, &send_tok, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000913 free(p);
Darren Tucker0efd1552003-08-26 11:49:55 +1000914 gss_release_buffer(&ms, &send_tok);
915
916 /* Server will be returning a failed packet after this one */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000917 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000918}
919
Damien Millerf7475d72008-11-03 19:26:18 +1100920/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000921int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000922input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000923{
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000924 char *msg = NULL;
925 char *lang = NULL;
926 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000927
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000928 if ((r = sshpkt_get_u32(ssh, NULL)) != 0 || /* maj */
929 (r = sshpkt_get_u32(ssh, NULL)) != 0 || /* min */
930 (r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
931 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
932 goto out;
933 r = sshpkt_get_end(ssh);
Damien Miller15d72a02005-11-05 15:07:33 +1100934 debug("Server GSSAPI Error:\n%s", msg);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000935 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000936 free(msg);
937 free(lang);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000938 return r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000939}
940#endif /* GSSAPI */
941
Damien Millereba71ba2000-04-29 23:57:08 +1000942int
djm@openbsd.org64c95982019-01-19 21:40:21 +0000943userauth_none(struct ssh *ssh)
Damien Miller874d77b2000-10-14 16:23:11 +1100944{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000945 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000946 int r;
947
Damien Miller874d77b2000-10-14 16:23:11 +1100948 /* initial userauth request */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000949 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
950 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
951 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
952 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
953 (r = sshpkt_send(ssh)) != 0)
954 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100955 return 1;
956}
957
958int
djm@openbsd.org64c95982019-01-19 21:40:21 +0000959userauth_passwd(struct ssh *ssh)
Damien Millereba71ba2000-04-29 23:57:08 +1000960{
djm@openbsd.org64c95982019-01-19 21:40:21 +0000961 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
djm@openbsd.org91b19192018-11-28 06:00:38 +0000962 char *password, *prompt = NULL;
Darren Tuckerab791692010-01-08 18:48:02 +1100963 const char *host = options.host_key_alias ? options.host_key_alias :
964 authctxt->host;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000965 int r;
Damien Millereba71ba2000-04-29 23:57:08 +1000966
djm@openbsd.org4a526942019-01-04 03:27:50 +0000967 if (authctxt->attempt_passwd++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000968 return 0;
969
djm@openbsd.org4a526942019-01-04 03:27:50 +0000970 if (authctxt->attempt_passwd != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000971 error("Permission denied, please try again.");
972
djm@openbsd.org91b19192018-11-28 06:00:38 +0000973 xasprintf(&prompt, "%s@%s's password: ", authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +1000974 password = read_passphrase(prompt, 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000975 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
976 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
977 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
978 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
979 (r = sshpkt_put_u8(ssh, 0)) != 0 ||
980 (r = sshpkt_put_cstring(ssh, password)) != 0 ||
981 (r = sshpkt_add_padding(ssh, 64)) != 0 ||
982 (r = sshpkt_send(ssh)) != 0)
983 fatal("%s: %s", __func__, ssh_err(r));
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000984
djm@openbsd.org91b19192018-11-28 06:00:38 +0000985 free(prompt);
986 if (password != NULL)
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000987 freezero(password, strlen(password));
988
989 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000990 &input_userauth_passwd_changereq);
991
Damien Millereba71ba2000-04-29 23:57:08 +1000992 return 1;
993}
Damien Millerf7475d72008-11-03 19:26:18 +1100994
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000995/*
996 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
997 */
Damien Millerf7475d72008-11-03 19:26:18 +1100998/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000999int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001000input_userauth_passwd_changereq(int type, u_int32_t seqnr, struct ssh *ssh)
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001001{
markus@openbsd.org94583be2017-05-30 14:19:15 +00001002 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001003 char *info = NULL, *lang = NULL, *password = NULL, *retype = NULL;
dtucker@openbsd.orga54eb272017-08-27 00:38:41 +00001004 char prompt[256];
djm@openbsd.org3e032a92017-01-30 00:32:03 +00001005 const char *host;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001006 int r;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001007
1008 debug2("input_userauth_passwd_changereq");
1009
1010 if (authctxt == NULL)
1011 fatal("input_userauth_passwd_changereq: "
1012 "no authentication context");
djm@openbsd.org3e032a92017-01-30 00:32:03 +00001013 host = options.host_key_alias ? options.host_key_alias : authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001014
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001015 if ((r = sshpkt_get_cstring(ssh, &info, NULL)) != 0 ||
1016 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1017 goto out;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001018 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001019 logit("%s", info);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001020 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1021 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1022 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1023 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1024 (r = sshpkt_put_u8(ssh, 1)) != 0) /* additional info */
1025 goto out;
1026
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001027 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001028 "Enter %.30s@%.128s's old password: ",
Darren Tuckerab791692010-01-08 18:48:02 +11001029 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001030 password = read_passphrase(prompt, 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001031 if ((r = sshpkt_put_cstring(ssh, password)) != 0)
1032 goto out;
1033
1034 freezero(password, strlen(password));
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001035 password = NULL;
1036 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001037 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001038 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +11001039 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001040 password = read_passphrase(prompt, RP_ALLOW_EOF);
1041 if (password == NULL) {
1042 /* bail out */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001043 r = 0;
1044 goto out;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001045 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001046 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001047 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +11001048 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001049 retype = read_passphrase(prompt, 0);
1050 if (strcmp(password, retype) != 0) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001051 freezero(password, strlen(password));
Damien Miller996acd22003-04-09 20:59:48 +10001052 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001053 password = NULL;
1054 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001055 freezero(retype, strlen(retype));
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001056 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001057 if ((r = sshpkt_put_cstring(ssh, password)) != 0 ||
1058 (r = sshpkt_add_padding(ssh, 64)) != 0 ||
1059 (r = sshpkt_send(ssh)) != 0)
1060 goto out;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001061
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001062 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001063 &input_userauth_passwd_changereq);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001064 r = 0;
1065 out:
1066 if (password)
1067 freezero(password, strlen(password));
1068 free(info);
1069 free(lang);
1070 return r;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001071}
Damien Millereba71ba2000-04-29 23:57:08 +10001072
djm@openbsd.org26074382018-03-03 03:01:50 +00001073/*
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001074 * Select an algorithm for publickey signatures.
1075 * Returns algorithm (caller must free) or NULL if no mutual algorithm found.
1076 *
1077 * Call with ssh==NULL to ignore server-sig-algs extension list and
1078 * only attempt with the key's base signature type.
djm@openbsd.org26074382018-03-03 03:01:50 +00001079 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001080static char *
1081key_sig_algorithm(struct ssh *ssh, const struct sshkey *key)
djm@openbsd.org26074382018-03-03 03:01:50 +00001082{
djm@openbsd.orgb4d4eda2018-07-03 13:20:25 +00001083 char *allowed, *oallowed, *cp, *tmp, *alg = NULL;
djm@openbsd.org26074382018-03-03 03:01:50 +00001084
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001085 /*
1086 * The signature algorithm will only differ from the key algorithm
1087 * for RSA keys/certs and when the server advertises support for
1088 * newer (SHA2) algorithms.
1089 */
1090 if (ssh == NULL || ssh->kex->server_sig_algs == NULL ||
djm@openbsd.org1a4a9cf2018-10-11 03:48:04 +00001091 (key->type != KEY_RSA && key->type != KEY_RSA_CERT) ||
1092 (key->type == KEY_RSA_CERT && (datafellows & SSH_BUG_SIGTYPE))) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001093 /* Filter base key signature alg against our configuration */
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001094 return match_list(sshkey_ssh_name(key),
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001095 options.pubkey_key_types, NULL);
djm@openbsd.org26074382018-03-03 03:01:50 +00001096 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001097
1098 /*
1099 * For RSA keys/certs, since these might have a different sig type:
1100 * find the first entry in PubkeyAcceptedKeyTypes of the right type
1101 * that also appears in the supported signature algorithms list from
1102 * the server.
1103 */
1104 oallowed = allowed = xstrdup(options.pubkey_key_types);
1105 while ((cp = strsep(&allowed, ",")) != NULL) {
1106 if (sshkey_type_from_name(cp) != key->type)
1107 continue;
djm@openbsd.orgb4d4eda2018-07-03 13:20:25 +00001108 tmp = match_list(sshkey_sigalg_by_name(cp), ssh->kex->server_sig_algs, NULL);
1109 if (tmp != NULL)
1110 alg = xstrdup(cp);
1111 free(tmp);
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001112 if (alg != NULL)
1113 break;
1114 }
1115 free(oallowed);
1116 return alg;
djm@openbsd.org26074382018-03-03 03:01:50 +00001117}
1118
Ben Lindstrombba81212001-06-25 05:01:22 +00001119static int
djm@openbsd.org141efe42015-01-14 20:05:27 +00001120identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001121 const u_char *data, size_t datalen, u_int compat, const char *alg)
Damien Miller280ecfb2003-05-14 13:46:00 +10001122{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001123 struct sshkey *prv;
djm@openbsd.org26074382018-03-03 03:01:50 +00001124 int r;
Damien Miller280ecfb2003-05-14 13:46:00 +10001125
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001126 /* The agent supports this key. */
djm@openbsd.org26074382018-03-03 03:01:50 +00001127 if (id->key != NULL && id->agent_fd != -1) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001128 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
1129 data, datalen, alg, compat);
1130 }
1131
1132 /*
1133 * We have already loaded the private key or the private key is
1134 * stored in external hardware.
1135 */
1136 if (id->key != NULL &&
1137 (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))) {
1138 if ((r = sshkey_sign(id->key, sigp, lenp, data, datalen,
1139 alg, compat)) != 0)
1140 return r;
1141 /*
1142 * PKCS#11 tokens may not support all signature algorithms,
1143 * so check what we get back.
1144 */
1145 if ((r = sshkey_check_sigtype(*sigp, *lenp, alg)) != 0)
djm@openbsd.org26074382018-03-03 03:01:50 +00001146 return r;
1147 return 0;
1148 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001149
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001150 /* Load the private key from the file. */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001151 if ((prv = load_identity_file(id)) == NULL)
djm@openbsd.org0d1451a2016-02-23 01:34:14 +00001152 return SSH_ERR_KEY_NOT_FOUND;
djm@openbsd.orgc4972d02017-08-11 04:47:12 +00001153 if (id->key != NULL && !sshkey_equal_public(prv, id->key)) {
1154 error("%s: private key %s contents do not match public",
1155 __func__, id->filename);
1156 return SSH_ERR_KEY_NOT_FOUND;
1157 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001158 r = sshkey_sign(prv, sigp, lenp, data, datalen, alg, compat);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001159 sshkey_free(prv);
djm@openbsd.org26074382018-03-03 03:01:50 +00001160 return r;
Damien Miller280ecfb2003-05-14 13:46:00 +10001161}
1162
1163static int
djm@openbsd.org1e245522017-03-11 23:40:26 +00001164id_filename_matches(Identity *id, Identity *private_id)
1165{
1166 const char *suffixes[] = { ".pub", "-cert.pub", NULL };
1167 size_t len = strlen(id->filename), plen = strlen(private_id->filename);
1168 size_t i, slen;
1169
1170 if (strcmp(id->filename, private_id->filename) == 0)
1171 return 1;
1172 for (i = 0; suffixes[i]; i++) {
1173 slen = strlen(suffixes[i]);
1174 if (len > slen && plen == len - slen &&
1175 strcmp(id->filename + (len - slen), suffixes[i]) == 0 &&
1176 memcmp(id->filename, private_id->filename, plen) == 0)
1177 return 1;
1178 }
1179 return 0;
1180}
1181
1182static int
djm@openbsd.org64c95982019-01-19 21:40:21 +00001183sign_and_send_pubkey(struct ssh *ssh, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +10001184{
djm@openbsd.org64c95982019-01-19 21:40:21 +00001185 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001186 struct sshbuf *b = NULL;
1187 Identity *private_id, *sign_id = NULL;
1188 u_char *signature = NULL;
1189 size_t slen = 0, skip = 0;
1190 int r, fallback_sigtype, sent = 0;
1191 char *alg = NULL, *fp = NULL;
1192 const char *loc = "";
Damien Millereba71ba2000-04-29 23:57:08 +10001193
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001194 if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1195 SSH_FP_DEFAULT)) == NULL)
1196 return 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001197
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001198 debug3("%s: %s %s", __func__, sshkey_type(id->key), fp);
Damien Millereba71ba2000-04-29 23:57:08 +10001199
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001200 /*
1201 * If the key is an certificate, try to find a matching private key
1202 * and use it to complete the signature.
djm@openbsd.org9ee692f2016-04-28 14:30:21 +00001203 * If no such private key exists, fall back to trying the certificate
1204 * key itself in case it has a private half already loaded.
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001205 * This will try to set sign_id to the private key that will perform
1206 * the signature.
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001207 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001208 if (sshkey_is_cert(id->key)) {
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001209 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1210 if (sshkey_equal_public(id->key, private_id->key) &&
1211 id->key->type != private_id->key->type) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001212 sign_id = private_id;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001213 break;
1214 }
1215 }
djm@openbsd.org1e245522017-03-11 23:40:26 +00001216 /*
1217 * Exact key matches are preferred, but also allow
1218 * filename matches for non-PKCS#11/agent keys that
1219 * didn't load public keys. This supports the case
1220 * of keeping just a private key file and public
1221 * certificate on disk.
1222 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001223 if (sign_id == NULL &&
1224 !id->isprivate && id->agent_fd == -1 &&
djm@openbsd.org1e245522017-03-11 23:40:26 +00001225 (id->key->flags & SSHKEY_FLAG_EXT) == 0) {
1226 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1227 if (private_id->key == NULL &&
1228 id_filename_matches(id, private_id)) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001229 sign_id = private_id;
djm@openbsd.org1e245522017-03-11 23:40:26 +00001230 break;
1231 }
1232 }
1233 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001234 if (sign_id != NULL) {
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001235 debug2("%s: using private key \"%s\"%s for "
1236 "certificate", __func__, id->filename,
1237 id->agent_fd != -1 ? " from agent" : "");
1238 } else {
djm@openbsd.orgc38905b2016-03-14 16:20:54 +00001239 debug("%s: no separate private key for certificate "
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001240 "\"%s\"", __func__, id->filename);
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001241 }
1242 }
1243
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001244 /*
1245 * If the above didn't select another identity to do the signing
1246 * then default to the one we started with.
1247 */
1248 if (sign_id == NULL)
1249 sign_id = id;
1250
1251 /* assemble and sign data */
1252 for (fallback_sigtype = 0; fallback_sigtype <= 1; fallback_sigtype++) {
1253 free(alg);
1254 slen = 0;
1255 signature = NULL;
1256 if ((alg = key_sig_algorithm(fallback_sigtype ? NULL : ssh,
1257 id->key)) == NULL) {
1258 error("%s: no mutual signature supported", __func__);
1259 goto out;
1260 }
1261 debug3("%s: signing using %s", __func__, alg);
1262
1263 sshbuf_free(b);
1264 if ((b = sshbuf_new()) == NULL)
1265 fatal("%s: sshbuf_new failed", __func__);
1266 if (datafellows & SSH_OLD_SESSIONID) {
1267 if ((r = sshbuf_put(b, session_id2,
1268 session_id2_len)) != 0) {
1269 fatal("%s: sshbuf_put: %s",
1270 __func__, ssh_err(r));
1271 }
1272 } else {
1273 if ((r = sshbuf_put_string(b, session_id2,
1274 session_id2_len)) != 0) {
1275 fatal("%s: sshbuf_put_string: %s",
1276 __func__, ssh_err(r));
1277 }
1278 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001279 skip = sshbuf_len(b);
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001280 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1281 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1282 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1283 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1284 (r = sshbuf_put_u8(b, 1)) != 0 ||
1285 (r = sshbuf_put_cstring(b, alg)) != 0 ||
1286 (r = sshkey_puts(id->key, b)) != 0) {
1287 fatal("%s: assemble signed data: %s",
1288 __func__, ssh_err(r));
1289 }
1290
1291 /* generate signature */
1292 r = identity_sign(sign_id, &signature, &slen,
1293 sshbuf_ptr(b), sshbuf_len(b), datafellows, alg);
1294 if (r == 0)
1295 break;
1296 else if (r == SSH_ERR_KEY_NOT_FOUND)
1297 goto out; /* soft failure */
1298 else if (r == SSH_ERR_SIGN_ALG_UNSUPPORTED &&
1299 !fallback_sigtype) {
1300 if (sign_id->agent_fd != -1)
1301 loc = "agent ";
1302 else if ((sign_id->key->flags & SSHKEY_FLAG_EXT) != 0)
1303 loc = "token ";
1304 logit("%skey %s %s returned incorrect signature type",
1305 loc, sshkey_type(id->key), fp);
1306 continue;
1307 }
1308 error("%s: signing failed: %s", __func__, ssh_err(r));
1309 goto out;
Damien Millerad833b32000-08-23 10:46:23 +10001310 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001311 if (slen == 0 || signature == NULL) /* shouldn't happen */
1312 fatal("%s: no signature", __func__);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001313
Damien Millereba71ba2000-04-29 23:57:08 +10001314 /* append signature */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001315 if ((r = sshbuf_put_string(b, signature, slen)) != 0)
1316 fatal("%s: append signature: %s", __func__, ssh_err(r));
Damien Millereba71ba2000-04-29 23:57:08 +10001317
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001318#ifdef DEBUG_PK
1319 sshbuf_dump(b, stderr);
1320#endif
Damien Millereba71ba2000-04-29 23:57:08 +10001321 /* skip session id and packet type */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001322 if ((r = sshbuf_consume(b, skip + 1)) != 0)
1323 fatal("%s: consume: %s", __func__, ssh_err(r));
Damien Millereba71ba2000-04-29 23:57:08 +10001324
1325 /* put remaining data from buffer into packet */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001326 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1327 (r = sshpkt_putb(ssh, b)) != 0 ||
1328 (r = sshpkt_send(ssh)) != 0)
1329 fatal("%s: enqueue request: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001330
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001331 /* success */
1332 sent = 1;
1333
1334 out:
1335 free(fp);
1336 free(alg);
1337 sshbuf_free(b);
1338 freezero(signature, slen);
1339 return sent;
Damien Miller994cf142000-07-21 10:19:44 +10001340}
1341
Ben Lindstrombba81212001-06-25 05:01:22 +00001342static int
djm@openbsd.org64c95982019-01-19 21:40:21 +00001343send_pubkey_test(struct ssh *ssh, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001344{
djm@openbsd.org64c95982019-01-19 21:40:21 +00001345 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001346 u_char *blob = NULL;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001347 char *alg = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001348 size_t bloblen;
1349 u_int have_sig = 0;
1350 int sent = 0, r;
Damien Miller994cf142000-07-21 10:19:44 +10001351
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001352 if ((alg = key_sig_algorithm(ssh, id->key)) == NULL) {
1353 debug("%s: no mutual signature algorithm", __func__);
1354 goto out;
1355 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001356
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001357 if ((r = sshkey_to_blob(id->key, &blob, &bloblen)) != 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001358 /* we cannot handle this key */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001359 debug3("%s: cannot handle key", __func__);
1360 goto out;
Damien Miller994cf142000-07-21 10:19:44 +10001361 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001362 /* register callback for USERAUTH_PK_OK message */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001363 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001364
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001365 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1366 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1367 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1368 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1369 (r = sshpkt_put_u8(ssh, have_sig)) != 0 ||
1370 (r = sshpkt_put_cstring(ssh, alg)) != 0 ||
1371 (r = sshpkt_put_string(ssh, blob, bloblen)) != 0 ||
1372 (r = sshpkt_send(ssh)) != 0)
1373 fatal("%s: %s", __func__, ssh_err(r));
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001374 sent = 1;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001375
1376 out:
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001377 free(alg);
1378 free(blob);
1379 return sent;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001380}
1381
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001382static struct sshkey *
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001383load_identity_file(Identity *id)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001384{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001385 struct sshkey *private = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001386 char prompt[300], *passphrase, *comment;
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001387 int r, perm_ok = 0, quit = 0, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001388 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001389
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001390 if (stat(id->filename, &st) < 0) {
1391 (id->userprovided ? logit : debug3)("no such identity: %s: %s",
1392 id->filename, strerror(errno));
Ben Lindstrom329782e2001-03-10 17:08:59 +00001393 return NULL;
1394 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001395 snprintf(prompt, sizeof prompt,
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001396 "Enter passphrase for key '%.100s': ", id->filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001397 for (i = 0; i <= options.number_of_password_prompts; i++) {
1398 if (i == 0)
1399 passphrase = "";
1400 else {
Damien Miller62cee002000-09-23 17:15:56 +11001401 passphrase = read_passphrase(prompt, 0);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001402 if (*passphrase == '\0') {
Damien Miller62cee002000-09-23 17:15:56 +11001403 debug2("no passphrase given, try next key");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001404 free(passphrase);
1405 break;
Damien Miller62cee002000-09-23 17:15:56 +11001406 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001407 }
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001408 switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
1409 passphrase, &private, &comment, &perm_ok))) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001410 case 0:
1411 break;
1412 case SSH_ERR_KEY_WRONG_PASSPHRASE:
1413 if (options.batch_mode) {
1414 quit = 1;
1415 break;
1416 }
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001417 if (i != 0)
1418 debug2("bad passphrase given, try again...");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001419 break;
1420 case SSH_ERR_SYSTEM_ERROR:
1421 if (errno == ENOENT) {
1422 debug2("Load key \"%s\": %s",
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001423 id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001424 quit = 1;
1425 break;
1426 }
1427 /* FALLTHROUGH */
1428 default:
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001429 error("Load key \"%s\": %s", id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001430 quit = 1;
1431 break;
1432 }
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001433 if (!quit && private != NULL && id->agent_fd == -1 &&
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001434 !(id->key && id->isprivate))
1435 maybe_add_key_to_agent(id->filename, private, comment,
1436 passphrase);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001437 if (i > 0)
1438 freezero(passphrase, strlen(passphrase));
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001439 free(comment);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001440 if (private != NULL || quit)
1441 break;
Damien Miller994cf142000-07-21 10:19:44 +10001442 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001443 return private;
1444}
1445
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001446static int
1447key_type_allowed_by_config(struct sshkey *key)
1448{
1449 if (match_pattern_list(sshkey_ssh_name(key),
1450 options.pubkey_key_types, 0) == 1)
1451 return 1;
1452
1453 /* RSA keys/certs might be allowed by alternate signature types */
1454 switch (key->type) {
1455 case KEY_RSA:
1456 if (match_pattern_list("rsa-sha2-512",
1457 options.pubkey_key_types, 0) == 1)
1458 return 1;
1459 if (match_pattern_list("rsa-sha2-256",
1460 options.pubkey_key_types, 0) == 1)
1461 return 1;
1462 break;
1463 case KEY_RSA_CERT:
1464 if (match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
1465 options.pubkey_key_types, 0) == 1)
1466 return 1;
1467 if (match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
1468 options.pubkey_key_types, 0) == 1)
1469 return 1;
1470 break;
1471 }
1472 return 0;
1473}
1474
1475
Damien Miller280ecfb2003-05-14 13:46:00 +10001476/*
1477 * try keys in the following order:
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001478 * 1. certificates listed in the config file
1479 * 2. other input certificates
1480 * 3. agent keys that are found in the config file
1481 * 4. other agent keys
1482 * 5. keys that are only listed in the config file
Damien Miller280ecfb2003-05-14 13:46:00 +10001483 */
1484static void
1485pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001486{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001487 struct identity *id, *id2, *tmp;
1488 struct idlist agent, files, *preferred;
1489 struct sshkey *key;
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001490 int agent_fd = -1, i, r, found;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001491 size_t j;
1492 struct ssh_identitylist *idlist;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001493 char *ident;
Damien Millerad833b32000-08-23 10:46:23 +10001494
Damien Miller280ecfb2003-05-14 13:46:00 +10001495 TAILQ_INIT(&agent); /* keys from the agent */
1496 TAILQ_INIT(&files); /* keys from the config file */
1497 preferred = &authctxt->keys;
1498 TAILQ_INIT(preferred); /* preferred order of keys */
1499
Damien Millercb6b68b2012-12-03 09:49:52 +11001500 /* list of keys stored in the filesystem and PKCS#11 */
Damien Miller280ecfb2003-05-14 13:46:00 +10001501 for (i = 0; i < options.num_identity_files; i++) {
1502 key = options.identity_keys[i];
Damien Miller0a80ca12010-02-27 07:55:05 +11001503 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1504 continue;
Damien Miller280ecfb2003-05-14 13:46:00 +10001505 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001506 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001507 id->agent_fd = -1;
Damien Miller280ecfb2003-05-14 13:46:00 +10001508 id->key = key;
1509 id->filename = xstrdup(options.identity_files[i]);
Darren Tucker19104782013-04-05 11:13:08 +11001510 id->userprovided = options.identity_file_userprovided[i];
Damien Miller280ecfb2003-05-14 13:46:00 +10001511 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001512 }
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001513 /* list of certificates specified by user */
1514 for (i = 0; i < options.num_certificate_files; i++) {
1515 key = options.certificates[i];
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001516 if (!sshkey_is_cert(key) || key->cert == NULL ||
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001517 key->cert->type != SSH2_CERT_TYPE_USER)
1518 continue;
1519 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001520 id->agent_fd = -1;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001521 id->key = key;
1522 id->filename = xstrdup(options.certificate_files[i]);
1523 id->userprovided = options.certificate_file_userprovided[i];
1524 TAILQ_INSERT_TAIL(preferred, id, next);
1525 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001526 /* list of keys supported by the agent */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001527 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1528 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1529 debug("%s: ssh_get_authentication_socket: %s",
1530 __func__, ssh_err(r));
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +00001531 } else if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001532 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1533 debug("%s: ssh_fetch_identitylist: %s",
1534 __func__, ssh_err(r));
markus@openbsd.orgfc77ccd2016-01-14 22:56:56 +00001535 close(agent_fd);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001536 } else {
1537 for (j = 0; j < idlist->nkeys; j++) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001538 found = 0;
1539 TAILQ_FOREACH(id, &files, next) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001540 /*
1541 * agent keys from the config file are
1542 * preferred
1543 */
1544 if (sshkey_equal(idlist->keys[j], id->key)) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001545 TAILQ_REMOVE(&files, id, next);
1546 TAILQ_INSERT_TAIL(preferred, id, next);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001547 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001548 found = 1;
1549 break;
1550 }
1551 }
Damien Millerbd394c32004-03-08 23:12:36 +11001552 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001553 id = xcalloc(1, sizeof(*id));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001554 /* XXX "steals" key/comment from idlist */
1555 id->key = idlist->keys[j];
1556 id->filename = idlist->comments[j];
1557 idlist->keys[j] = NULL;
1558 idlist->comments[j] = NULL;
1559 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001560 TAILQ_INSERT_TAIL(&agent, id, next);
1561 }
1562 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001563 ssh_free_identitylist(idlist);
Damien Miller280ecfb2003-05-14 13:46:00 +10001564 /* append remaining agent keys */
1565 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1566 TAILQ_REMOVE(&agent, id, next);
1567 TAILQ_INSERT_TAIL(preferred, id, next);
1568 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001569 authctxt->agent_fd = agent_fd;
Damien Miller62cee002000-09-23 17:15:56 +11001570 }
djm@openbsd.org82f24c32016-05-23 23:30:50 +00001571 /* Prefer PKCS11 keys that are explicitly listed */
1572 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1573 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
1574 continue;
1575 found = 0;
1576 TAILQ_FOREACH(id2, &files, next) {
1577 if (id2->key == NULL ||
1578 (id2->key->flags & SSHKEY_FLAG_EXT) == 0)
1579 continue;
1580 if (sshkey_equal(id->key, id2->key)) {
1581 TAILQ_REMOVE(&files, id, next);
1582 TAILQ_INSERT_TAIL(preferred, id, next);
1583 found = 1;
1584 break;
1585 }
1586 }
1587 /* If IdentitiesOnly set and key not found then don't use it */
1588 if (!found && options.identities_only) {
1589 TAILQ_REMOVE(&files, id, next);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001590 freezero(id, sizeof(*id));
djm@openbsd.org82f24c32016-05-23 23:30:50 +00001591 }
1592 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001593 /* append remaining keys from the config file */
1594 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1595 TAILQ_REMOVE(&files, id, next);
1596 TAILQ_INSERT_TAIL(preferred, id, next);
1597 }
djm@openbsd.orge679c092015-10-13 16:15:21 +00001598 /* finally, filter by PubkeyAcceptedKeyTypes */
1599 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
djm@openbsd.orgd78b75d2018-07-03 13:07:58 +00001600 if (id->key != NULL && !key_type_allowed_by_config(id->key)) {
djm@openbsd.orge679c092015-10-13 16:15:21 +00001601 debug("Skipping %s key %s - "
1602 "not in PubkeyAcceptedKeyTypes",
1603 sshkey_ssh_name(id->key), id->filename);
1604 TAILQ_REMOVE(preferred, id, next);
1605 sshkey_free(id->key);
1606 free(id->filename);
1607 memset(id, 0, sizeof(*id));
1608 continue;
1609 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001610 }
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001611 /* List the keys we plan on using */
1612 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1613 ident = format_identity(id);
1614 debug("Will attempt key: %s", ident);
1615 free(ident);
1616 }
1617 debug2("%s: done", __func__);
Damien Miller280ecfb2003-05-14 13:46:00 +10001618}
1619
1620static void
1621pubkey_cleanup(Authctxt *authctxt)
1622{
1623 Identity *id;
1624
djm@openbsd.orgaaed6352018-11-16 02:46:20 +00001625 if (authctxt->agent_fd != -1) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001626 ssh_close_authentication_socket(authctxt->agent_fd);
djm@openbsd.orgaaed6352018-11-16 02:46:20 +00001627 authctxt->agent_fd = -1;
1628 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001629 for (id = TAILQ_FIRST(&authctxt->keys); id;
1630 id = TAILQ_FIRST(&authctxt->keys)) {
1631 TAILQ_REMOVE(&authctxt->keys, id, next);
mmcc@openbsd.org89540b62015-12-11 02:31:47 +00001632 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +10001633 free(id->filename);
1634 free(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001635 }
Damien Millereba71ba2000-04-29 23:57:08 +10001636}
1637
djm@openbsd.orgb9844a42016-12-04 23:54:02 +00001638static void
1639pubkey_reset(Authctxt *authctxt)
1640{
1641 Identity *id;
1642
1643 TAILQ_FOREACH(id, &authctxt->keys, next)
1644 id->tried = 0;
1645}
1646
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001647static int
1648try_identity(Identity *id)
1649{
1650 if (!id->key)
1651 return (0);
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001652 if (sshkey_type_plain(id->key->type) == KEY_RSA &&
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001653 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1654 debug("Skipped %s key %s for RSA/MD5 server",
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001655 sshkey_type(id->key), id->filename);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001656 return (0);
1657 }
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001658 return 1;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001659}
1660
Damien Miller62cee002000-09-23 17:15:56 +11001661int
djm@openbsd.org64c95982019-01-19 21:40:21 +00001662userauth_pubkey(struct ssh *ssh)
Damien Millereba71ba2000-04-29 23:57:08 +10001663{
djm@openbsd.org64c95982019-01-19 21:40:21 +00001664 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
Damien Miller280ecfb2003-05-14 13:46:00 +10001665 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001666 int sent = 0;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001667 char *ident;
Damien Millereba71ba2000-04-29 23:57:08 +10001668
Damien Miller280ecfb2003-05-14 13:46:00 +10001669 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1670 if (id->tried++)
1671 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001672 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001673 TAILQ_REMOVE(&authctxt->keys, id, next);
1674 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1675 /*
1676 * send a test message if we have the public key. for
1677 * encrypted keys we cannot do this and have to load the
1678 * private key instead
1679 */
Damien Miller324541e2013-12-31 12:25:40 +11001680 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001681 if (try_identity(id)) {
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001682 ident = format_identity(id);
1683 debug("Offering public key: %s", ident);
1684 free(ident);
djm@openbsd.org64c95982019-01-19 21:40:21 +00001685 sent = send_pubkey_test(ssh, id);
Damien Miller324541e2013-12-31 12:25:40 +11001686 }
1687 } else {
Damien Miller280ecfb2003-05-14 13:46:00 +10001688 debug("Trying private key: %s", id->filename);
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001689 id->key = load_identity_file(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001690 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001691 if (try_identity(id)) {
1692 id->isprivate = 1;
djm@openbsd.org64c95982019-01-19 21:40:21 +00001693 sent = sign_and_send_pubkey(ssh, id);
Damien Miller324541e2013-12-31 12:25:40 +11001694 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001695 sshkey_free(id->key);
Damien Miller280ecfb2003-05-14 13:46:00 +10001696 id->key = NULL;
djm@openbsd.orgb9844a42016-12-04 23:54:02 +00001697 id->isprivate = 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001698 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001699 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001700 if (sent)
1701 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001702 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001703 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001704}
Damien Millereba71ba2000-04-29 23:57:08 +10001705
Damien Miller874d77b2000-10-14 16:23:11 +11001706/*
1707 * Send userauth request message specifying keyboard-interactive method.
1708 */
1709int
djm@openbsd.org64c95982019-01-19 21:40:21 +00001710userauth_kbdint(struct ssh *ssh)
Damien Miller874d77b2000-10-14 16:23:11 +11001711{
djm@openbsd.org64c95982019-01-19 21:40:21 +00001712 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001713 int r;
Damien Miller874d77b2000-10-14 16:23:11 +11001714
djm@openbsd.org4a526942019-01-04 03:27:50 +00001715 if (authctxt->attempt_kbdint++ >= options.number_of_password_prompts)
Damien Miller874d77b2000-10-14 16:23:11 +11001716 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001717 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
djm@openbsd.org4a526942019-01-04 03:27:50 +00001718 if (authctxt->attempt_kbdint > 1 && !authctxt->info_req_seen) {
Ben Lindstrom7d199962001-09-12 18:29:00 +00001719 debug3("userauth_kbdint: disable: no info_req_seen");
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001720 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
Ben Lindstrom7d199962001-09-12 18:29:00 +00001721 return 0;
1722 }
Damien Miller874d77b2000-10-14 16:23:11 +11001723
1724 debug2("userauth_kbdint");
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001725 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1726 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1727 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1728 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1729 (r = sshpkt_put_cstring(ssh, "")) != 0 || /* lang */
1730 (r = sshpkt_put_cstring(ssh, options.kbd_interactive_devices ?
1731 options.kbd_interactive_devices : "")) != 0 ||
1732 (r = sshpkt_send(ssh)) != 0)
1733 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +11001734
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001735 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
Damien Miller874d77b2000-10-14 16:23:11 +11001736 return 1;
1737}
1738
1739/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001740 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001741 */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001742int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001743input_userauth_info_req(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller874d77b2000-10-14 16:23:11 +11001744{
markus@openbsd.org94583be2017-05-30 14:19:15 +00001745 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001746 char *name = NULL, *inst = NULL, *lang = NULL, *prompt = NULL;
1747 char *response = NULL;
1748 u_char echo = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001749 u_int num_prompts, i;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001750 int r;
Damien Miller874d77b2000-10-14 16:23:11 +11001751
1752 debug2("input_userauth_info_req");
1753
1754 if (authctxt == NULL)
1755 fatal("input_userauth_info_req: no authentication context");
1756
Ben Lindstrom7d199962001-09-12 18:29:00 +00001757 authctxt->info_req_seen = 1;
1758
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001759 if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 ||
1760 (r = sshpkt_get_cstring(ssh, &inst, NULL)) != 0 ||
1761 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1762 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +11001763 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001764 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001765 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001766 logit("%s", inst);
Damien Miller874d77b2000-10-14 16:23:11 +11001767
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001768 if ((r = sshpkt_get_u32(ssh, &num_prompts)) != 0)
1769 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +11001770 /*
1771 * Begin to build info response packet based on prompts requested.
1772 * We commit to providing the correct number of responses, so if
1773 * further on we run into a problem that prevents this, we have to
1774 * be sure and clean this up and send a correct error response.
1775 */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001776 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE)) != 0 ||
1777 (r = sshpkt_put_u32(ssh, num_prompts)) != 0)
1778 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +11001779
Ben Lindstrom551ea372001-06-05 18:56:16 +00001780 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001781 for (i = 0; i < num_prompts; i++) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001782 if ((r = sshpkt_get_cstring(ssh, &prompt, NULL)) != 0 ||
1783 (r = sshpkt_get_u8(ssh, &echo)) != 0)
1784 goto out;
Ben Lindstrom949974b2001-06-25 05:20:31 +00001785 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001786 if ((r = sshpkt_put_cstring(ssh, response)) != 0)
1787 goto out;
1788 freezero(response, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +10001789 free(prompt);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001790 response = prompt = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001791 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001792 /* done with parsing incoming message. */
1793 if ((r = sshpkt_get_end(ssh)) != 0 ||
1794 (r = sshpkt_add_padding(ssh, 64)) != 0)
1795 goto out;
1796 r = sshpkt_send(ssh);
1797 out:
1798 if (response)
1799 freezero(response, strlen(response));
1800 free(prompt);
1801 free(name);
1802 free(inst);
1803 free(lang);
1804 return r;
Damien Miller874d77b2000-10-14 16:23:11 +11001805}
Damien Miller62cee002000-09-23 17:15:56 +11001806
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001807static int
djm@openbsd.orged1df722019-01-19 21:34:45 +00001808ssh_keysign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001809 const u_char *data, size_t datalen)
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001810{
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001811 struct sshbuf *b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001812 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001813 pid_t pid;
djm@openbsd.org64c95982019-01-19 21:40:21 +00001814 int i, r, to[2], from[2], status;
1815 int sock = ssh_packet_get_connection_in(ssh);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001816 u_char rversion = 0, version = 2;
1817 void (*osigchld)(int);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001818
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001819 *sigp = NULL;
1820 *lenp = 0;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001821
Ben Lindstrom5206b952002-06-06 19:59:29 +00001822 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001823 error("%s: not installed: %s", __func__, strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001824 return -1;
1825 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001826 if (fflush(stdout) != 0) {
1827 error("%s: fflush: %s", __func__, strerror(errno));
1828 return -1;
1829 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001830 if (pipe(to) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001831 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001832 return -1;
1833 }
1834 if (pipe(from) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001835 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001836 return -1;
1837 }
1838 if ((pid = fork()) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001839 error("%s: fork: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001840 return -1;
1841 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001842 osigchld = signal(SIGCHLD, SIG_DFL);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001843 if (pid == 0) {
Darren Tucker6e7fe1c2010-01-08 17:07:22 +11001844 /* keep the socket on exec */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001845 fcntl(sock, F_SETFD, 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001846 close(from[0]);
1847 if (dup2(from[1], STDOUT_FILENO) < 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001848 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001849 close(to[1]);
1850 if (dup2(to[0], STDIN_FILENO) < 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001851 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001852 close(from[1]);
1853 close(to[0]);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001854 /* Close everything but stdio and the socket */
1855 for (i = STDERR_FILENO + 1; i < sock; i++)
1856 close(i);
1857 closefrom(sock + 1);
1858 debug3("%s: [child] pid=%ld, exec %s",
1859 __func__, (long)getpid(), _PATH_SSH_KEY_SIGN);
mmcc@openbsd.org94141b72015-12-11 00:20:04 +00001860 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001861 fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001862 strerror(errno));
1863 }
1864 close(from[1]);
1865 close(to[0]);
1866
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001867 if ((b = sshbuf_new()) == NULL)
1868 fatal("%s: sshbuf_new failed", __func__);
1869 /* send # of sock, data to be signed */
djm@openbsd.org326e2fa2017-01-30 00:32:28 +00001870 if ((r = sshbuf_put_u32(b, sock)) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001871 (r = sshbuf_put_string(b, data, datalen)) != 0)
1872 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1873 if (ssh_msg_send(to[1], version, b) == -1)
1874 fatal("%s: couldn't send request", __func__);
1875 sshbuf_reset(b);
1876 r = ssh_msg_recv(from[0], b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001877 close(from[0]);
1878 close(to[1]);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001879 if (r < 0) {
1880 error("%s: no reply", __func__);
1881 goto fail;
1882 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001883
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001884 errno = 0;
1885 while (waitpid(pid, &status, 0) < 0) {
1886 if (errno != EINTR) {
1887 error("%s: waitpid %ld: %s",
1888 __func__, (long)pid, strerror(errno));
1889 goto fail;
1890 }
1891 }
1892 if (!WIFEXITED(status)) {
1893 error("%s: exited abnormally", __func__);
1894 goto fail;
1895 }
1896 if (WEXITSTATUS(status) != 0) {
1897 error("%s: exited with status %d",
1898 __func__, WEXITSTATUS(status));
1899 goto fail;
1900 }
1901 if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
1902 error("%s: buffer error: %s", __func__, ssh_err(r));
1903 goto fail;
1904 }
1905 if (rversion != version) {
1906 error("%s: bad version", __func__);
1907 goto fail;
1908 }
1909 if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
1910 error("%s: buffer error: %s", __func__, ssh_err(r));
1911 fail:
1912 signal(SIGCHLD, osigchld);
1913 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001914 return -1;
1915 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001916 signal(SIGCHLD, osigchld);
1917 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001918
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001919 return 0;
1920}
1921
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001922int
djm@openbsd.org64c95982019-01-19 21:40:21 +00001923userauth_hostbased(struct ssh *ssh)
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001924{
djm@openbsd.org64c95982019-01-19 21:40:21 +00001925 Authctxt *authctxt = (Authctxt *)ssh->authctxt;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001926 struct sshkey *private = NULL;
1927 struct sshbuf *b = NULL;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001928 u_char *sig = NULL, *keyblob = NULL;
1929 char *fp = NULL, *chost = NULL, *lname = NULL;
1930 size_t siglen = 0, keylen = 0;
1931 int i, r, success = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001932
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001933 if (authctxt->ktypes == NULL) {
1934 authctxt->oktypes = xstrdup(options.hostbased_key_types);
1935 authctxt->ktypes = authctxt->oktypes;
1936 }
djm@openbsd.org1195f4c2015-01-08 10:14:08 +00001937
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001938 /*
1939 * Work through each listed type pattern in HostbasedKeyTypes,
1940 * trying each hostkey that matches the type in turn.
1941 */
1942 for (;;) {
1943 if (authctxt->active_ktype == NULL)
1944 authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
1945 if (authctxt->active_ktype == NULL ||
1946 *authctxt->active_ktype == '\0')
1947 break;
1948 debug3("%s: trying key type %s", __func__,
1949 authctxt->active_ktype);
1950
1951 /* check for a useful key */
1952 private = NULL;
1953 for (i = 0; i < authctxt->sensitive->nkeys; i++) {
1954 if (authctxt->sensitive->keys[i] == NULL ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001955 authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
1956 continue;
1957 if (match_pattern_list(
1958 sshkey_ssh_name(authctxt->sensitive->keys[i]),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001959 authctxt->active_ktype, 0) != 1)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001960 continue;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001961 /* we take and free the key */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001962 private = authctxt->sensitive->keys[i];
1963 authctxt->sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001964 break;
1965 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001966 /* Found one */
1967 if (private != NULL)
1968 break;
1969 /* No more keys of this type; advance */
1970 authctxt->active_ktype = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001971 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001972 if (private == NULL) {
1973 free(authctxt->oktypes);
1974 authctxt->oktypes = authctxt->ktypes = NULL;
1975 authctxt->active_ktype = NULL;
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001976 debug("No more client hostkeys for hostbased authentication.");
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001977 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001978 }
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001979
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001980 if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
1981 SSH_FP_DEFAULT)) == NULL) {
1982 error("%s: sshkey_fingerprint failed", __func__);
1983 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001984 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001985 debug("%s: trying hostkey %s %s",
1986 __func__, sshkey_ssh_name(private), fp);
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001987
Damien Miller91c18472001-11-12 11:02:03 +11001988 /* figure out a name for the client host */
djm@openbsd.org64c95982019-01-19 21:40:21 +00001989 lname = get_local_name(ssh_packet_get_connection_in(ssh));
1990 if (lname == NULL) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001991 error("%s: cannot get local ipaddr/name", __func__);
1992 goto out;
Damien Miller91c18472001-11-12 11:02:03 +11001993 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001994
1995 /* XXX sshbuf_put_stringf? */
1996 xasprintf(&chost, "%s.", lname);
1997 debug2("%s: chost %s", __func__, chost);
Damien Miller91c18472001-11-12 11:02:03 +11001998
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001999 /* construct data */
2000 if ((b = sshbuf_new()) == NULL) {
2001 error("%s: sshbuf_new failed", __func__);
2002 goto out;
2003 }
2004 if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
2005 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
2006 goto out;
2007 }
2008 if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
2009 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2010 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +00002011 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002012 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002013 (r = sshbuf_put_cstring(b, sshkey_ssh_name(private))) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002014 (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
2015 (r = sshbuf_put_cstring(b, chost)) != 0 ||
2016 (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
2017 error("%s: buffer error: %s", __func__, ssh_err(r));
2018 goto out;
2019 }
2020
2021#ifdef DEBUG_PK
2022 sshbuf_dump(b, stderr);
2023#endif
djm@openbsd.orged1df722019-01-19 21:34:45 +00002024 if ((r = ssh_keysign(ssh, private, &sig, &siglen,
2025 sshbuf_ptr(b), sshbuf_len(b))) != 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002026 error("sign using hostkey %s %s failed",
2027 sshkey_ssh_name(private), fp);
2028 goto out;
2029 }
2030 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2031 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
2032 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
2033 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002034 (r = sshpkt_put_cstring(ssh, sshkey_ssh_name(private))) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002035 (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
2036 (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
2037 (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
2038 (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
2039 (r = sshpkt_send(ssh)) != 0) {
2040 error("%s: packet error: %s", __func__, ssh_err(r));
2041 goto out;
2042 }
2043 success = 1;
2044
2045 out:
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002046 if (sig != NULL)
2047 freezero(sig, siglen);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002048 free(keyblob);
2049 free(lname);
2050 free(fp);
2051 free(chost);
2052 sshkey_free(private);
2053 sshbuf_free(b);
2054
2055 return success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002056}
2057
Damien Miller62cee002000-09-23 17:15:56 +11002058/* find auth method */
2059
Damien Miller62cee002000-09-23 17:15:56 +11002060/*
2061 * given auth method name, if configurable options permit this method fill
2062 * in auth_ident field and return true, otherwise return false.
2063 */
Ben Lindstrombba81212001-06-25 05:01:22 +00002064static int
Damien Miller62cee002000-09-23 17:15:56 +11002065authmethod_is_enabled(Authmethod *method)
2066{
2067 if (method == NULL)
2068 return 0;
2069 /* return false if options indicate this method is disabled */
2070 if (method->enabled == NULL || *method->enabled == 0)
2071 return 0;
2072 /* return false if batch mode is enabled but method needs interactive mode */
2073 if (method->batch_flag != NULL && *method->batch_flag != 0)
2074 return 0;
2075 return 1;
2076}
2077
Ben Lindstrombba81212001-06-25 05:01:22 +00002078static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11002079authmethod_lookup(const char *name)
2080{
2081 Authmethod *method = NULL;
2082 if (name != NULL)
2083 for (method = authmethods; method->name != NULL; method++)
2084 if (strcmp(name, method->name) == 0)
2085 return method;
2086 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
2087 return NULL;
2088}
2089
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002090/* XXX internal state */
2091static Authmethod *current = NULL;
2092static char *supported = NULL;
2093static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00002094
Damien Miller62cee002000-09-23 17:15:56 +11002095/*
2096 * Given the authentication method list sent by the server, return the
2097 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00002098 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00002099 */
Ben Lindstrombba81212001-06-25 05:01:22 +00002100static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11002101authmethod_get(char *authlist)
2102{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002103 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00002104 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00002105
Damien Miller62cee002000-09-23 17:15:56 +11002106 /* Use a suitable default if we're passed a nil list. */
2107 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002108 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11002109
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002110 if (supported == NULL || strcmp(authlist, supported) != 0) {
2111 debug3("start over, passed a different list %s", authlist);
Darren Tuckera627d422013-06-02 07:31:17 +10002112 free(supported);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002113 supported = xstrdup(authlist);
2114 preferred = options.preferred_authentications;
2115 debug3("preferred %s", preferred);
2116 current = NULL;
2117 } else if (current != NULL && authmethod_is_enabled(current))
2118 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10002119
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002120 for (;;) {
2121 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00002122 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002123 current = NULL;
2124 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11002125 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002126 preferred += next;
2127 debug3("authmethod_lookup %s", name);
2128 debug3("remaining preferred: %s", preferred);
2129 if ((current = authmethod_lookup(name)) != NULL &&
2130 authmethod_is_enabled(current)) {
2131 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00002132 debug("Next authentication method: %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002133 free(name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002134 return current;
2135 }
Darren Tuckere52a2602013-06-06 08:22:05 +10002136 free(name);
Damien Millereba71ba2000-04-29 23:57:08 +10002137 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002138}
Damien Miller62cee002000-09-23 17:15:56 +11002139
Ben Lindstrom79073822001-07-04 03:42:30 +00002140static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002141authmethods_get(void)
2142{
2143 Authmethod *method = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002144 struct sshbuf *b;
Damien Miller0e3b8722002-01-22 23:26:38 +11002145 char *list;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002146 int r;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002147
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002148 if ((b = sshbuf_new()) == NULL)
2149 fatal("%s: sshbuf_new failed", __func__);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002150 for (method = authmethods; method->name != NULL; method++) {
2151 if (authmethod_is_enabled(method)) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002152 if ((r = sshbuf_putf(b, "%s%s",
2153 sshbuf_len(b) ? "," : "", method->name)) != 0)
2154 fatal("%s: buffer error: %s",
2155 __func__, ssh_err(r));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002156 }
Damien Miller62cee002000-09-23 17:15:56 +11002157 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002158 if ((list = sshbuf_dup_string(b)) == NULL)
djm@openbsd.org1a31d022016-05-02 08:49:03 +00002159 fatal("%s: sshbuf_dup_string failed", __func__);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002160 sshbuf_free(b);
Damien Miller0e3b8722002-01-22 23:26:38 +11002161 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10002162}