blob: bd98a12a7c10be10da3f07b25156b2983a1b2cb3 [file] [log] [blame]
djm@openbsd.orged1df722019-01-19 21:34:45 +00001/* $OpenBSD: sshconnect2.c,v 1.294 2019/01/19 21:34:45 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
djm@openbsd.org0fa174e2019-01-19 21:31:32 +000080#include "opacket.h" /* XXX */
81extern struct ssh *active_state; /* XXX */
82
Damien Millereba71ba2000-04-29 23:57:08 +100083/* import */
84extern char *client_version_string;
85extern char *server_version_string;
86extern Options options;
87
88/*
89 * SSH2 key exchange
90 */
91
Ben Lindstrom46c16222000-12-22 01:43:59 +000092u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +100093u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100094
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000095char *xxx_host;
96struct sockaddr *xxx_hostaddr;
97
Ben Lindstrombba81212001-06-25 05:01:22 +000098static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +000099verify_host_key_callback(struct sshkey *hostkey, struct ssh *ssh)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000100{
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000101 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +1000102 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000103 return 0;
104}
105
Damien Millerd925dcd2010-12-01 12:21:51 +1100106static char *
107order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
108{
109 char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
110 size_t maxlen;
111 struct hostkeys *hostkeys;
112 int ktype;
Damien Miller295ee632011-05-29 21:42:31 +1000113 u_int i;
Damien Millerd925dcd2010-12-01 12:21:51 +1100114
115 /* Find all hostkeys for this hostname */
116 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
117 hostkeys = init_hostkeys();
Damien Miller295ee632011-05-29 21:42:31 +1000118 for (i = 0; i < options.num_user_hostfiles; i++)
119 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
120 for (i = 0; i < options.num_system_hostfiles; i++)
121 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
Damien Millerd925dcd2010-12-01 12:21:51 +1100122
123 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
124 maxlen = strlen(avail) + 1;
125 first = xmalloc(maxlen);
126 last = xmalloc(maxlen);
127 *first = *last = '\0';
128
129#define ALG_APPEND(to, from) \
130 do { \
131 if (*to != '\0') \
132 strlcat(to, ",", maxlen); \
133 strlcat(to, from, maxlen); \
134 } while (0)
135
136 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000137 if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
Damien Millerd925dcd2010-12-01 12:21:51 +1100138 fatal("%s: unknown alg %s", __func__, alg);
139 if (lookup_key_in_hostkeys_by_type(hostkeys,
djm@openbsd.org141efe42015-01-14 20:05:27 +0000140 sshkey_type_plain(ktype), NULL))
Damien Millerd925dcd2010-12-01 12:21:51 +1100141 ALG_APPEND(first, alg);
142 else
143 ALG_APPEND(last, alg);
144 }
145#undef ALG_APPEND
djm@openbsd.org35d60222015-01-18 13:33:34 +0000146 xasprintf(&ret, "%s%s%s", first,
147 (*first == '\0' || *last == '\0') ? "" : ",", last);
Damien Millerd925dcd2010-12-01 12:21:51 +1100148 if (*first != '\0')
149 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
150
Darren Tuckera627d422013-06-02 07:31:17 +1000151 free(first);
152 free(last);
153 free(hostname);
154 free(oavail);
Damien Millerd925dcd2010-12-01 12:21:51 +1100155 free_hostkeys(hostkeys);
156
157 return ret;
158}
159
Damien Millereba71ba2000-04-29 23:57:08 +1000160void
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000161ssh_kex2(struct ssh *ssh, char *host, struct sockaddr *hostaddr, u_short port)
Damien Miller874d77b2000-10-14 16:23:11 +1100162{
Damien Miller9235a032014-04-20 13:17:20 +1000163 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
djm@openbsd.org312d2f22018-07-04 13:49:31 +0000164 char *s, *all_key;
markus@openbsd.org57e783c2015-01-20 20:16:21 +0000165 int r;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000166
167 xxx_host = host;
168 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100169
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000170 if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
171 fatal("%s: kex_names_cat", __func__);
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000172 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(s);
Damien Millera0ff4662001-03-30 10:49:35 +1000173 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000174 compat_cipher_proposal(options.ciphers);
Damien Millera0ff4662001-03-30 10:49:35 +1000175 myproposal[PROPOSAL_ENC_ALGS_STOC] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000176 compat_cipher_proposal(options.ciphers);
dtucker@openbsd.org8c02e362016-05-24 04:43:45 +0000177 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
178 myproposal[PROPOSAL_COMP_ALGS_STOC] = options.compression ?
sf@openbsd.org168b46f2018-07-09 13:37:10 +0000179 "zlib@openssh.com,zlib,none" : "none,zlib@openssh.com,zlib";
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000180 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
181 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
182 if (options.hostkeyalgorithms != NULL) {
djm@openbsd.org312d2f22018-07-04 13:49:31 +0000183 all_key = sshkey_alg_list(0, 0, 1, ',');
184 if (kex_assemble_names(&options.hostkeyalgorithms,
185 KEX_DEFAULT_PK_ALG, all_key) != 0)
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000186 fatal("%s: kex_assemble_namelist", __func__);
djm@openbsd.org312d2f22018-07-04 13:49:31 +0000187 free(all_key);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100188 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000189 compat_pkalg_proposal(options.hostkeyalgorithms);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000190 } else {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000191 /* Enforce default */
192 options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG);
Damien Millerd925dcd2010-12-01 12:21:51 +1100193 /* Prefer algorithms that we already have keys for */
194 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100195 compat_pkalg_proposal(
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000196 order_hostkeyalgs(host, hostaddr, port));
Damien Millerd925dcd2010-12-01 12:21:51 +1100197 }
Damien Miller874d77b2000-10-14 16:23:11 +1100198
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000199 if (options.rekey_limit || options.rekey_interval)
djm@openbsd.orged1df722019-01-19 21:34:45 +0000200 ssh_packet_set_rekey_limits(ssh, options.rekey_limit,
dtucker@openbsd.orgc998bf02017-02-03 02:56:00 +0000201 options.rekey_interval);
Damien Millera5539d22003-04-09 20:50:06 +1000202
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000203 /* start key exchange */
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000204 if ((r = kex_setup(ssh, myproposal)) != 0)
markus@openbsd.org57e783c2015-01-20 20:16:21 +0000205 fatal("kex_setup: %s", ssh_err(r));
Damien Miller1f0311c2014-05-15 14:24:09 +1000206#ifdef WITH_OPENSSL
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000207 ssh->kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
208 ssh->kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
209 ssh->kex->kex[KEX_DH_GRP14_SHA256] = kexdh_client;
210 ssh->kex->kex[KEX_DH_GRP16_SHA512] = kexdh_client;
211 ssh->kex->kex[KEX_DH_GRP18_SHA512] = kexdh_client;
212 ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
213 ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100214# ifdef OPENSSL_HAS_ECC
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000215 ssh->kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100216# endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000217#endif
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000218 ssh->kex->kex[KEX_C25519_SHA256] = kexc25519_client;
219 ssh->kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100220
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000221 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &ssh->kex->done);
Damien Miller874d77b2000-10-14 16:23:11 +1100222
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000223 /* remove ext-info from the KEX proposals for rekeying */
224 myproposal[PROPOSAL_KEX_ALGS] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000225 compat_kex_proposal(options.kex_algorithms);
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000226 if ((r = kex_prop2buf(ssh->kex->my, myproposal)) != 0)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000227 fatal("kex_prop2buf: %s", ssh_err(r));
Darren Tucker36331b52010-01-08 16:50:41 +1100228
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000229 session_id2 = ssh->kex->session_id;
230 session_id2_len = ssh->kex->session_id_len;
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000231
Damien Miller874d77b2000-10-14 16:23:11 +1100232#ifdef DEBUG_KEXDH
233 /* send 1st encrypted/maced/compressed message */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000234 if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 ||
235 (r = sshpkt_put_cstring(ssh, "markus")) != 0 ||
markus@openbsd.org394a8422018-07-11 18:55:11 +0000236 (r = sshpkt_send(ssh)) != 0 ||
237 (r = ssh_packet_write_wait(ssh)) != 0)
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000238 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100239#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000240}
Damien Millerb1715dc2000-05-30 13:44:51 +1000241
Damien Millereba71ba2000-04-29 23:57:08 +1000242/*
243 * Authenticate user
244 */
Damien Miller62cee002000-09-23 17:15:56 +1100245
djm@openbsd.org141efe42015-01-14 20:05:27 +0000246typedef struct cauthctxt Authctxt;
247typedef struct cauthmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000248typedef struct identity Identity;
249typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100250
Damien Miller280ecfb2003-05-14 13:46:00 +1000251struct identity {
252 TAILQ_ENTRY(identity) next;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000253 int agent_fd; /* >=0 if agent supports key */
254 struct sshkey *key; /* public/private key */
Damien Miller280ecfb2003-05-14 13:46:00 +1000255 char *filename; /* comment for agent-only keys */
256 int tried;
257 int isprivate; /* key points to the private key */
Damien Miller5ceddc32013-02-15 12:18:32 +1100258 int userprovided;
Damien Miller280ecfb2003-05-14 13:46:00 +1000259};
260TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100261
djm@openbsd.org141efe42015-01-14 20:05:27 +0000262struct cauthctxt {
Damien Miller62cee002000-09-23 17:15:56 +1100263 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000264 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100265 const char *host;
266 const char *service;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000267 struct cauthmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000268 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000269 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000270 /* pubkey */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000271 struct idlist keys;
272 int agent_fd;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000273 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000274 Sensitive *sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000275 char *oktypes, *ktypes;
276 const char *active_ktype;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000277 /* kbd-interactive */
278 int info_req_seen;
djm@openbsd.org4a526942019-01-04 03:27:50 +0000279 int attempt_kbdint;
280 /* password */
281 int attempt_passwd;
Darren Tucker0efd1552003-08-26 11:49:55 +1000282 /* generic */
283 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100284};
djm@openbsd.org141efe42015-01-14 20:05:27 +0000285
286struct cauthmethod {
Damien Miller62cee002000-09-23 17:15:56 +1100287 char *name; /* string to compare against server's list */
288 int (*userauth)(Authctxt *authctxt);
Damien Miller01ed2272008-11-05 16:20:46 +1100289 void (*cleanup)(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100290 int *enabled; /* flag in option struct that enables method */
291 int *batch_flag; /* flag in option struct that disables method */
292};
293
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000294int input_userauth_service_accept(int, u_int32_t, struct ssh *);
295int input_userauth_ext_info(int, u_int32_t, struct ssh *);
296int input_userauth_success(int, u_int32_t, struct ssh *);
297int input_userauth_success_unexpected(int, u_int32_t, struct ssh *);
298int input_userauth_failure(int, u_int32_t, struct ssh *);
299int input_userauth_banner(int, u_int32_t, struct ssh *);
300int input_userauth_error(int, u_int32_t, struct ssh *);
301int input_userauth_info_req(int, u_int32_t, struct ssh *);
302int input_userauth_pk_ok(int, u_int32_t, struct ssh *);
303int input_userauth_passwd_changereq(int, u_int32_t, struct ssh *);
Damien Miller874d77b2000-10-14 16:23:11 +1100304
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000305int userauth_none(Authctxt *);
306int userauth_pubkey(Authctxt *);
307int userauth_passwd(Authctxt *);
308int userauth_kbdint(Authctxt *);
309int userauth_hostbased(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100310
Darren Tucker0efd1552003-08-26 11:49:55 +1000311#ifdef GSSAPI
312int userauth_gssapi(Authctxt *authctxt);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000313int input_gssapi_response(int type, u_int32_t, struct ssh *);
314int input_gssapi_token(int type, u_int32_t, struct ssh *);
315int input_gssapi_hash(int type, u_int32_t, struct ssh *);
316int input_gssapi_error(int, u_int32_t, struct ssh *);
317int input_gssapi_errtok(int, u_int32_t, struct ssh *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000318#endif
319
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000320void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000321
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000322static int sign_and_send_pubkey(struct ssh *ssh, Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000323static void pubkey_prepare(Authctxt *);
324static void pubkey_cleanup(Authctxt *);
djm@openbsd.orgb9844a42016-12-04 23:54:02 +0000325static void pubkey_reset(Authctxt *);
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000326static struct sshkey *load_identity_file(Identity *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000327
Ben Lindstrombba81212001-06-25 05:01:22 +0000328static Authmethod *authmethod_get(char *authlist);
329static Authmethod *authmethod_lookup(const char *name);
330static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100331
332Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000333#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100334 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000335 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100336 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000337 &options.gss_authentication,
338 NULL},
339#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000340 {"hostbased",
341 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100342 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000343 &options.hostbased_authentication,
344 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000345 {"publickey",
346 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100347 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000348 &options.pubkey_authentication,
349 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100350 {"keyboard-interactive",
351 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100352 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100353 &options.kbd_interactive_authentication,
354 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000355 {"password",
356 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100357 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000358 &options.password_authentication,
359 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100360 {"none",
361 userauth_none,
362 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100363 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100364 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100365 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100366};
367
368void
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000369ssh_userauth2(struct ssh *ssh, const char *local_user,
370 const char *server_user, char *host, Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100371{
372 Authctxt authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000373 int r;
Damien Miller62cee002000-09-23 17:15:56 +1100374
Ben Lindstrom551ea372001-06-05 18:56:16 +0000375 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000376 options.kbd_interactive_authentication = 1;
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000377 if (options.preferred_authentications == NULL)
378 options.preferred_authentications = authmethods_get();
379
Damien Miller62cee002000-09-23 17:15:56 +1100380 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000381 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller62cee002000-09-23 17:15:56 +1100382 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000383 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100384 authctxt.host = host;
385 authctxt.service = "ssh-connection"; /* service name */
386 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100387 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000388 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000389 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000390 authctxt.sensitive = sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000391 authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000392 authctxt.info_req_seen = 0;
djm@openbsd.org4a526942019-01-04 03:27:50 +0000393 authctxt.attempt_kbdint = 0;
394 authctxt.attempt_passwd = 0;
djm@openbsd.orgf14564c2015-01-15 11:04:36 +0000395 authctxt.agent_fd = -1;
djm@openbsd.orgaaed6352018-11-16 02:46:20 +0000396 pubkey_prepare(&authctxt);
djm@openbsd.org0a843d92018-12-27 03:25:24 +0000397 if (authctxt.method == NULL) {
398 fatal("%s: internal error: cannot send userauth none request",
399 __func__);
400 }
Damien Miller62cee002000-09-23 17:15:56 +1100401
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000402 if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
403 (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
404 (r = sshpkt_send(ssh)) != 0)
405 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller62cee002000-09-23 17:15:56 +1100406
markus@openbsd.org94583be2017-05-30 14:19:15 +0000407 ssh->authctxt = &authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000408 ssh_dispatch_init(ssh, &input_userauth_error);
409 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
410 ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
markus@openbsd.org92e9fe62017-05-31 07:00:13 +0000411 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt.success); /* loop until success */
markus@openbsd.org94583be2017-05-30 14:19:15 +0000412 ssh->authctxt = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100413
Damien Miller280ecfb2003-05-14 13:46:00 +1000414 pubkey_cleanup(&authctxt);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000415 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
Damien Millerf842fcb2003-05-15 12:01:28 +1000416
dtucker@openbsd.orgf31c6542016-09-22 02:29:57 +0000417 if (!authctxt.success)
418 fatal("Authentication failed.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000419 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100420}
Damien Millerf842fcb2003-05-15 12:01:28 +1000421
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000422/* ARGSUSED */
423int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000424input_userauth_service_accept(int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000425{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000426 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000427 int r;
428
429 if (ssh_packet_remaining(ssh) > 0) {
430 char *reply;
431
432 if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
433 goto out;
434 debug2("service_accept: %s", reply);
435 free(reply);
436 } else {
437 debug2("buggy server: service_accept w/o service");
438 }
439 if ((r = sshpkt_get_end(ssh)) != 0)
440 goto out;
441 debug("SSH2_MSG_SERVICE_ACCEPT received");
442
443 /* initial userauth request */
444 userauth_none(authctxt);
445
446 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error);
447 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
448 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
449 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
450 r = 0;
451 out:
452 return r;
453}
454
455/* ARGSUSED */
456int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000457input_userauth_ext_info(int type, u_int32_t seqnr, struct ssh *ssh)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000458{
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000459 return kex_input_ext_info(type, seqnr, ssh);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000460}
461
Damien Miller62cee002000-09-23 17:15:56 +1100462void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000463userauth(Authctxt *authctxt, char *authlist)
464{
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000465 struct ssh *ssh = active_state; /* XXX */
466
Damien Miller01ed2272008-11-05 16:20:46 +1100467 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
468 authctxt->method->cleanup(authctxt);
469
Darren Tuckera627d422013-06-02 07:31:17 +1000470 free(authctxt->methoddata);
471 authctxt->methoddata = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000472 if (authlist == NULL) {
473 authlist = authctxt->authlist;
474 } else {
Darren Tuckera627d422013-06-02 07:31:17 +1000475 free(authctxt->authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000476 authctxt->authlist = authlist;
477 }
478 for (;;) {
479 Authmethod *method = authmethod_get(authlist);
480 if (method == NULL)
dtucker@openbsd.org4626e392017-06-14 00:31:38 +0000481 fatal("%s@%s: Permission denied (%s).",
482 authctxt->server_user, authctxt->host, authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000483 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000484
485 /* reset the per method handler */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000486 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_PER_METHOD_MIN,
Damien Millerf842fcb2003-05-15 12:01:28 +1000487 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
488
489 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000490 if (method->userauth(authctxt) != 0) {
491 debug2("we sent a %s packet, wait for reply", method->name);
492 break;
493 } else {
494 debug2("we did not send a packet, disable method");
495 method->enabled = NULL;
496 }
497 }
498}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000499
Damien Millerf7475d72008-11-03 19:26:18 +1100500/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000501int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000502input_userauth_error(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100503{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000504 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000505 "type %d", type);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000506 return 0;
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000507}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000508
Damien Millerf7475d72008-11-03 19:26:18 +1100509/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000510int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000511input_userauth_banner(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000512{
djm@openbsd.orged1df722019-01-19 21:34:45 +0000513 char *msg = NULL, *lang = NULL;
514 size_t len;
515 int r;
Darren Tucker79644822003-10-08 17:37:58 +1000516
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000517 debug3("%s", __func__);
djm@openbsd.orged1df722019-01-19 21:34:45 +0000518 if ((r = sshpkt_get_cstring(ssh, &msg, &len)) != 0 ||
519 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
520 goto out;
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000521 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO)
522 fmprintf(stderr, "%s", msg);
djm@openbsd.orged1df722019-01-19 21:34:45 +0000523 r = 0;
524 out:
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000525 free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000526 free(lang);
djm@openbsd.orged1df722019-01-19 21:34:45 +0000527 return r;
Damien Miller62cee002000-09-23 17:15:56 +1100528}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000529
Damien Millerf7475d72008-11-03 19:26:18 +1100530/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000531int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000532input_userauth_success(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100533{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000534 Authctxt *authctxt = ssh->authctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100535
Damien Miller62cee002000-09-23 17:15:56 +1100536 if (authctxt == NULL)
537 fatal("input_userauth_success: no authentication context");
Darren Tuckera627d422013-06-02 07:31:17 +1000538 free(authctxt->authlist);
539 authctxt->authlist = NULL;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100540 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
541 authctxt->method->cleanup(authctxt);
Darren Tuckera627d422013-06-02 07:31:17 +1000542 free(authctxt->methoddata);
543 authctxt->methoddata = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100544 authctxt->success = 1; /* break out */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000545 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100546}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000547
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000548int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000549input_userauth_success_unexpected(int type, u_int32_t seq, struct ssh *ssh)
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100550{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000551 Authctxt *authctxt = ssh->authctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100552
553 if (authctxt == NULL)
554 fatal("%s: no authentication context", __func__);
555
556 fatal("Unexpected authentication success during %s.",
557 authctxt->method->name);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000558 return 0;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100559}
560
Damien Millerf7475d72008-11-03 19:26:18 +1100561/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000562int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000563input_userauth_failure(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100564{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000565 Authctxt *authctxt = ssh->authctxt;
Damien Miller62cee002000-09-23 17:15:56 +1100566 char *authlist = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000567 u_char partial;
568 int r;
Damien Miller62cee002000-09-23 17:15:56 +1100569
570 if (authctxt == NULL)
571 fatal("input_userauth_failure: no authentication context");
572
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000573 if ((r = sshpkt_get_cstring(ssh, &authlist, NULL)) != 0 ||
574 (r = sshpkt_get_u8(ssh, &partial)) != 0 ||
575 (r = sshpkt_get_end(ssh)) != 0)
576 goto out;
Damien Miller62cee002000-09-23 17:15:56 +1100577
Damien Miller62e9c4f2013-04-23 15:15:49 +1000578 if (partial != 0) {
dtucker@openbsd.org83cbca62016-07-22 05:46:11 +0000579 verbose("Authenticated with partial success.");
Damien Miller62e9c4f2013-04-23 15:15:49 +1000580 /* reset state */
djm@openbsd.orgb9844a42016-12-04 23:54:02 +0000581 pubkey_reset(authctxt);
Damien Miller62e9c4f2013-04-23 15:15:49 +1000582 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000583 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100584
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000585 userauth(authctxt, authlist);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000586 authlist = NULL;
587 out:
588 free(authlist);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000589 return 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000590}
Damien Millerf7475d72008-11-03 19:26:18 +1100591
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000592/*
593 * Format an identity for logging including filename, key type, fingerprint
594 * and location (agent, etc.). Caller must free.
595 */
596static char *
597format_identity(Identity *id)
598{
599 char *fp = NULL, *ret = NULL;
600
601 if (id->key != NULL) {
602 fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
603 SSH_FP_DEFAULT);
604 }
605 xasprintf(&ret, "%s %s%s%s%s%s%s",
606 id->filename,
607 id->key ? sshkey_type(id->key) : "", id->key ? " " : "",
608 fp ? fp : "",
609 id->userprovided ? " explicit" : "",
610 (id->key && (id->key->flags & SSHKEY_FLAG_EXT)) ? " token" : "",
611 id->agent_fd != -1 ? " agent" : "");
612 free(fp);
613 return ret;
614}
615
Damien Millerf7475d72008-11-03 19:26:18 +1100616/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000617int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000618input_userauth_pk_ok(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000619{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000620 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000621 struct sshkey *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000622 Identity *id = NULL;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000623 int pktype, found = 0, sent = 0;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000624 size_t blen;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000625 char *pkalg = NULL, *fp = NULL, *ident = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000626 u_char *pkblob = NULL;
627 int r;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000628
629 if (authctxt == NULL)
630 fatal("input_userauth_pk_ok: no authentication context");
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000631
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000632 if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
633 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
634 (r = sshpkt_get_end(ssh)) != 0)
635 goto done;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000636
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000637 if ((pktype = sshkey_type_from_name(pkalg)) == KEY_UNSPEC) {
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000638 debug("%s: server sent unknown pkalg %s", __func__, pkalg);
Damien Miller280ecfb2003-05-14 13:46:00 +1000639 goto done;
640 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000641 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
642 debug("no key from blob. pkalg %s: %s", pkalg, ssh_err(r));
Damien Miller280ecfb2003-05-14 13:46:00 +1000643 goto done;
644 }
645 if (key->type != pktype) {
646 error("input_userauth_pk_ok: type mismatch "
647 "for decoded key (received %d, expected %d)",
648 key->type, pktype);
649 goto done;
650 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000651
Darren Tuckerd05b6012003-10-15 15:55:59 +1000652 /*
653 * search keys in the reverse order, because last candidate has been
654 * moved to the end of the queue. this also avoids confusion by
655 * duplicate keys
656 */
Damien Miller0b51a522004-04-20 20:07:19 +1000657 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000658 if (sshkey_equal(key, id->key)) {
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000659 found = 1;
Damien Miller280ecfb2003-05-14 13:46:00 +1000660 break;
661 }
662 }
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000663 if (!found || id == NULL) {
664 fp = sshkey_fingerprint(key, options.fingerprint_hash,
665 SSH_FP_DEFAULT);
666 error("%s: server replied with unknown key: %s %s", __func__,
667 sshkey_type(key), fp == NULL ? "<ERROR>" : fp);
668 goto done;
669 }
670 ident = format_identity(id);
671 debug("Server accepts key: %s", ident);
672 sent = sign_and_send_pubkey(ssh, authctxt, id);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000673 r = 0;
674 done:
675 sshkey_free(key);
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +0000676 free(ident);
677 free(fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000678 free(pkalg);
679 free(pkblob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000680
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000681 /* try another method if we did not send a packet */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000682 if (r == 0 && sent == 0)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000683 userauth(authctxt, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000684 return r;
Damien Miller62cee002000-09-23 17:15:56 +1100685}
686
Darren Tucker0efd1552003-08-26 11:49:55 +1000687#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100688int
Darren Tucker0efd1552003-08-26 11:49:55 +1000689userauth_gssapi(Authctxt *authctxt)
690{
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000691 struct ssh *ssh = active_state; /* XXX */
Darren Tucker0efd1552003-08-26 11:49:55 +1000692 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100693 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000694 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000695 OM_uint32 min;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000696 int r, ok = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000697
698 /* Try one GSSAPI method at a time, rather than sending them all at
699 * once. */
700
Darren Tucker56afe142003-11-03 20:06:14 +1100701 if (gss_supported == NULL)
702 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000703
704 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100705 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000706 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100707 if (gss_supported->elements[mech].length < 128 &&
djm@openbsd.org84623e02018-06-26 02:02:36 +0000708 ssh_gssapi_check_mechanism(&gssctxt,
Damien Millera1cb9f32006-08-19 00:33:34 +1000709 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000710 ok = 1; /* Mechanism works */
711 } else {
712 mech++;
713 }
714 }
715
Damien Millera1cb9f32006-08-19 00:33:34 +1000716 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000717 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000718
719 authctxt->methoddata=(void *)gssctxt;
720
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000721 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
722 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
723 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
724 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
725 (r = sshpkt_put_u32(ssh, 1)) != 0 ||
726 (r = sshpkt_put_u32(ssh,
727 (gss_supported->elements[mech].length) + 2)) != 0 ||
728 (r = sshpkt_put_u8(ssh, SSH_GSS_OIDTYPE)) != 0 ||
729 (r = sshpkt_put_u8(ssh,
730 gss_supported->elements[mech].length)) != 0 ||
731 (r = sshpkt_put(ssh,
732 gss_supported->elements[mech].elements,
733 gss_supported->elements[mech].length)) != 0 ||
734 (r = sshpkt_send(ssh)) != 0)
735 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +1000736
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000737 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
738 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
739 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
740 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000741
742 mech++; /* Move along to next candidate */
743
744 return 1;
745}
746
Damien Miller91c6aa42003-11-17 21:20:18 +1100747static OM_uint32
djm@openbsd.org39896b72017-05-31 05:08:46 +0000748process_gssapi_token(struct ssh *ssh, gss_buffer_t recv_tok)
Damien Miller91c6aa42003-11-17 21:20:18 +1100749{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000750 Authctxt *authctxt = ssh->authctxt;
Damien Miller91c6aa42003-11-17 21:20:18 +1100751 Gssctxt *gssctxt = authctxt->methoddata;
752 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000753 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
754 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100755 OM_uint32 status, ms, flags;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000756 int r;
Damien Miller787b2ec2003-11-21 23:56:47 +1100757
Damien Miller91c6aa42003-11-17 21:20:18 +1100758 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100759 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100760
761 if (send_tok.length > 0) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000762 u_char type = GSS_ERROR(status) ?
763 SSH2_MSG_USERAUTH_GSSAPI_ERRTOK :
764 SSH2_MSG_USERAUTH_GSSAPI_TOKEN;
Damien Miller787b2ec2003-11-21 23:56:47 +1100765
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000766 if ((r = sshpkt_start(ssh, type)) != 0 ||
767 (r = sshpkt_put_string(ssh, send_tok.value,
768 send_tok.length)) != 0 ||
769 (r = sshpkt_send(ssh)) != 0)
770 fatal("%s: %s", __func__, ssh_err(r));
771
Damien Miller91c6aa42003-11-17 21:20:18 +1100772 gss_release_buffer(&ms, &send_tok);
773 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100774
Damien Miller91c6aa42003-11-17 21:20:18 +1100775 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100776 /* send either complete or MIC, depending on mechanism */
777 if (!(flags & GSS_C_INTEG_FLAG)) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000778 if ((r = sshpkt_start(ssh,
779 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE)) != 0 ||
780 (r = sshpkt_send(ssh)) != 0)
781 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0425d402003-11-17 22:18:21 +1100782 } else {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000783 struct sshbuf *b;
784
785 if ((b = sshbuf_new()) == NULL)
786 fatal("%s: sshbuf_new failed", __func__);
787 ssh_gssapi_buildmic(b, authctxt->server_user,
Damien Miller0425d402003-11-17 22:18:21 +1100788 authctxt->service, "gssapi-with-mic");
789
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000790 if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
791 fatal("%s: sshbuf_mutable_ptr failed", __func__);
792 gssbuf.length = sshbuf_len(b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100793
Damien Miller0425d402003-11-17 22:18:21 +1100794 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100795
Damien Miller0425d402003-11-17 22:18:21 +1100796 if (!GSS_ERROR(status)) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000797 if ((r = sshpkt_start(ssh,
798 SSH2_MSG_USERAUTH_GSSAPI_MIC)) != 0 ||
799 (r = sshpkt_put_string(ssh, mic.value,
800 mic.length)) != 0 ||
801 (r = sshpkt_send(ssh)) != 0)
802 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0425d402003-11-17 22:18:21 +1100803 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100804
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000805 sshbuf_free(b);
Damien Miller0425d402003-11-17 22:18:21 +1100806 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100807 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100808 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100809
Damien Miller91c6aa42003-11-17 21:20:18 +1100810 return status;
811}
812
Damien Millerf7475d72008-11-03 19:26:18 +1100813/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000814int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000815input_gssapi_response(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000816{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000817 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000818 Gssctxt *gssctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000819 size_t oidlen;
820 u_char *oidv = NULL;
821 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000822
823 if (authctxt == NULL)
824 fatal("input_gssapi_response: no authentication context");
825 gssctxt = authctxt->methoddata;
826
827 /* Setup our OID */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000828 if ((r = sshpkt_get_string(ssh, &oidv, &oidlen)) != 0)
829 goto done;
Darren Tucker0efd1552003-08-26 11:49:55 +1000830
Darren Tucker655a5e02003-11-03 20:09:03 +1100831 if (oidlen <= 2 ||
832 oidv[0] != SSH_GSS_OIDTYPE ||
833 oidv[1] != oidlen - 2) {
834 debug("Badly encoded mechanism OID received");
835 userauth(authctxt, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000836 goto ok;
Darren Tucker0efd1552003-08-26 11:49:55 +1000837 }
838
Darren Tucker655a5e02003-11-03 20:09:03 +1100839 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
840 fatal("Server returned different OID than expected");
841
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000842 if ((r = sshpkt_get_end(ssh)) != 0)
843 goto done;
Darren Tucker0efd1552003-08-26 11:49:55 +1000844
djm@openbsd.org39896b72017-05-31 05:08:46 +0000845 if (GSS_ERROR(process_gssapi_token(ssh, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000846 /* Start again with next method on list */
847 debug("Trying to start again");
848 userauth(authctxt, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000849 goto ok;
Darren Tucker0efd1552003-08-26 11:49:55 +1000850 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000851 ok:
852 r = 0;
853 done:
854 free(oidv);
855 return r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000856}
857
Damien Millerf7475d72008-11-03 19:26:18 +1100858/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000859int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000860input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000861{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000862 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000863 gss_buffer_desc recv_tok;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000864 u_char *p = NULL;
865 size_t len;
Damien Miller91c6aa42003-11-17 21:20:18 +1100866 OM_uint32 status;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000867 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000868
869 if (authctxt == NULL)
870 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000871
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000872 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
873 (r = sshpkt_get_end(ssh)) != 0)
874 goto out;
Darren Tucker0efd1552003-08-26 11:49:55 +1000875
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000876 recv_tok.value = p;
877 recv_tok.length = len;
djm@openbsd.org39896b72017-05-31 05:08:46 +0000878 status = process_gssapi_token(ssh, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000879
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000880 /* Start again with the next method in the list */
Darren Tucker0efd1552003-08-26 11:49:55 +1000881 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000882 userauth(authctxt, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000883 /* ok */
Darren Tucker0efd1552003-08-26 11:49:55 +1000884 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000885 r = 0;
886 out:
887 free(p);
888 return r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000889}
890
Damien Millerf7475d72008-11-03 19:26:18 +1100891/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000892int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000893input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000894{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000895 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000896 Gssctxt *gssctxt;
897 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
898 gss_buffer_desc recv_tok;
Damien Millerd677ad12013-04-23 15:18:51 +1000899 OM_uint32 ms;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000900 u_char *p = NULL;
901 size_t len;
902 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000903
904 if (authctxt == NULL)
905 fatal("input_gssapi_response: no authentication context");
906 gssctxt = authctxt->methoddata;
907
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000908 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
909 (r = sshpkt_get_end(ssh)) != 0) {
910 free(p);
911 return r;
912 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000913
914 /* Stick it into GSSAPI and see what it says */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000915 recv_tok.value = p;
916 recv_tok.length = len;
Damien Millerd677ad12013-04-23 15:18:51 +1000917 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000918 &recv_tok, &send_tok, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000919 free(p);
Darren Tucker0efd1552003-08-26 11:49:55 +1000920 gss_release_buffer(&ms, &send_tok);
921
922 /* Server will be returning a failed packet after this one */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000923 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000924}
925
Damien Millerf7475d72008-11-03 19:26:18 +1100926/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000927int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000928input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000929{
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000930 char *msg = NULL;
931 char *lang = NULL;
932 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000933
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000934 if ((r = sshpkt_get_u32(ssh, NULL)) != 0 || /* maj */
935 (r = sshpkt_get_u32(ssh, NULL)) != 0 || /* min */
936 (r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
937 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
938 goto out;
939 r = sshpkt_get_end(ssh);
Damien Miller15d72a02005-11-05 15:07:33 +1100940 debug("Server GSSAPI Error:\n%s", msg);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000941 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000942 free(msg);
943 free(lang);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000944 return r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000945}
946#endif /* GSSAPI */
947
Damien Millereba71ba2000-04-29 23:57:08 +1000948int
Damien Miller874d77b2000-10-14 16:23:11 +1100949userauth_none(Authctxt *authctxt)
950{
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000951 struct ssh *ssh = active_state; /* XXX */
952 int r;
953
Damien Miller874d77b2000-10-14 16:23:11 +1100954 /* initial userauth request */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000955 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
956 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
957 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
958 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
959 (r = sshpkt_send(ssh)) != 0)
960 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100961 return 1;
962}
963
964int
Damien Miller62cee002000-09-23 17:15:56 +1100965userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000966{
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000967 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org91b19192018-11-28 06:00:38 +0000968 char *password, *prompt = NULL;
Darren Tuckerab791692010-01-08 18:48:02 +1100969 const char *host = options.host_key_alias ? options.host_key_alias :
970 authctxt->host;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000971 int r;
Damien Millereba71ba2000-04-29 23:57:08 +1000972
djm@openbsd.org4a526942019-01-04 03:27:50 +0000973 if (authctxt->attempt_passwd++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000974 return 0;
975
djm@openbsd.org4a526942019-01-04 03:27:50 +0000976 if (authctxt->attempt_passwd != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000977 error("Permission denied, please try again.");
978
djm@openbsd.org91b19192018-11-28 06:00:38 +0000979 xasprintf(&prompt, "%s@%s's password: ", authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +1000980 password = read_passphrase(prompt, 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000981 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
982 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
983 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
984 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
985 (r = sshpkt_put_u8(ssh, 0)) != 0 ||
986 (r = sshpkt_put_cstring(ssh, password)) != 0 ||
987 (r = sshpkt_add_padding(ssh, 64)) != 0 ||
988 (r = sshpkt_send(ssh)) != 0)
989 fatal("%s: %s", __func__, ssh_err(r));
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000990
djm@openbsd.org91b19192018-11-28 06:00:38 +0000991 free(prompt);
992 if (password != NULL)
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000993 freezero(password, strlen(password));
994
995 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000996 &input_userauth_passwd_changereq);
997
Damien Millereba71ba2000-04-29 23:57:08 +1000998 return 1;
999}
Damien Millerf7475d72008-11-03 19:26:18 +11001000
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001001/*
1002 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
1003 */
Damien Millerf7475d72008-11-03 19:26:18 +11001004/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001005int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001006input_userauth_passwd_changereq(int type, u_int32_t seqnr, struct ssh *ssh)
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001007{
markus@openbsd.org94583be2017-05-30 14:19:15 +00001008 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001009 char *info = NULL, *lang = NULL, *password = NULL, *retype = NULL;
dtucker@openbsd.orga54eb272017-08-27 00:38:41 +00001010 char prompt[256];
djm@openbsd.org3e032a92017-01-30 00:32:03 +00001011 const char *host;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001012 int r;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001013
1014 debug2("input_userauth_passwd_changereq");
1015
1016 if (authctxt == NULL)
1017 fatal("input_userauth_passwd_changereq: "
1018 "no authentication context");
djm@openbsd.org3e032a92017-01-30 00:32:03 +00001019 host = options.host_key_alias ? options.host_key_alias : authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001020
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001021 if ((r = sshpkt_get_cstring(ssh, &info, NULL)) != 0 ||
1022 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1023 goto out;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001024 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001025 logit("%s", info);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001026 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1027 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1028 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1029 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1030 (r = sshpkt_put_u8(ssh, 1)) != 0) /* additional info */
1031 goto out;
1032
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001033 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001034 "Enter %.30s@%.128s's old password: ",
Darren Tuckerab791692010-01-08 18:48:02 +11001035 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001036 password = read_passphrase(prompt, 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001037 if ((r = sshpkt_put_cstring(ssh, password)) != 0)
1038 goto out;
1039
1040 freezero(password, strlen(password));
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001041 password = NULL;
1042 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001043 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001044 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +11001045 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001046 password = read_passphrase(prompt, RP_ALLOW_EOF);
1047 if (password == NULL) {
1048 /* bail out */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001049 r = 0;
1050 goto out;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001051 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001052 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001053 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +11001054 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001055 retype = read_passphrase(prompt, 0);
1056 if (strcmp(password, retype) != 0) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001057 freezero(password, strlen(password));
Damien Miller996acd22003-04-09 20:59:48 +10001058 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001059 password = NULL;
1060 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001061 freezero(retype, strlen(retype));
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001062 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001063 if ((r = sshpkt_put_cstring(ssh, password)) != 0 ||
1064 (r = sshpkt_add_padding(ssh, 64)) != 0 ||
1065 (r = sshpkt_send(ssh)) != 0)
1066 goto out;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001067
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001068 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001069 &input_userauth_passwd_changereq);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001070 r = 0;
1071 out:
1072 if (password)
1073 freezero(password, strlen(password));
1074 free(info);
1075 free(lang);
1076 return r;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001077}
Damien Millereba71ba2000-04-29 23:57:08 +10001078
djm@openbsd.org26074382018-03-03 03:01:50 +00001079/*
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001080 * Select an algorithm for publickey signatures.
1081 * Returns algorithm (caller must free) or NULL if no mutual algorithm found.
1082 *
1083 * Call with ssh==NULL to ignore server-sig-algs extension list and
1084 * only attempt with the key's base signature type.
djm@openbsd.org26074382018-03-03 03:01:50 +00001085 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001086static char *
1087key_sig_algorithm(struct ssh *ssh, const struct sshkey *key)
djm@openbsd.org26074382018-03-03 03:01:50 +00001088{
djm@openbsd.orgb4d4eda2018-07-03 13:20:25 +00001089 char *allowed, *oallowed, *cp, *tmp, *alg = NULL;
djm@openbsd.org26074382018-03-03 03:01:50 +00001090
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001091 /*
1092 * The signature algorithm will only differ from the key algorithm
1093 * for RSA keys/certs and when the server advertises support for
1094 * newer (SHA2) algorithms.
1095 */
1096 if (ssh == NULL || ssh->kex->server_sig_algs == NULL ||
djm@openbsd.org1a4a9cf2018-10-11 03:48:04 +00001097 (key->type != KEY_RSA && key->type != KEY_RSA_CERT) ||
1098 (key->type == KEY_RSA_CERT && (datafellows & SSH_BUG_SIGTYPE))) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001099 /* Filter base key signature alg against our configuration */
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001100 return match_list(sshkey_ssh_name(key),
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001101 options.pubkey_key_types, NULL);
djm@openbsd.org26074382018-03-03 03:01:50 +00001102 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001103
1104 /*
1105 * For RSA keys/certs, since these might have a different sig type:
1106 * find the first entry in PubkeyAcceptedKeyTypes of the right type
1107 * that also appears in the supported signature algorithms list from
1108 * the server.
1109 */
1110 oallowed = allowed = xstrdup(options.pubkey_key_types);
1111 while ((cp = strsep(&allowed, ",")) != NULL) {
1112 if (sshkey_type_from_name(cp) != key->type)
1113 continue;
djm@openbsd.orgb4d4eda2018-07-03 13:20:25 +00001114 tmp = match_list(sshkey_sigalg_by_name(cp), ssh->kex->server_sig_algs, NULL);
1115 if (tmp != NULL)
1116 alg = xstrdup(cp);
1117 free(tmp);
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001118 if (alg != NULL)
1119 break;
1120 }
1121 free(oallowed);
1122 return alg;
djm@openbsd.org26074382018-03-03 03:01:50 +00001123}
1124
Ben Lindstrombba81212001-06-25 05:01:22 +00001125static int
djm@openbsd.org141efe42015-01-14 20:05:27 +00001126identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001127 const u_char *data, size_t datalen, u_int compat, const char *alg)
Damien Miller280ecfb2003-05-14 13:46:00 +10001128{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001129 struct sshkey *prv;
djm@openbsd.org26074382018-03-03 03:01:50 +00001130 int r;
Damien Miller280ecfb2003-05-14 13:46:00 +10001131
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001132 /* The agent supports this key. */
djm@openbsd.org26074382018-03-03 03:01:50 +00001133 if (id->key != NULL && id->agent_fd != -1) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001134 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
1135 data, datalen, alg, compat);
1136 }
1137
1138 /*
1139 * We have already loaded the private key or the private key is
1140 * stored in external hardware.
1141 */
1142 if (id->key != NULL &&
1143 (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))) {
1144 if ((r = sshkey_sign(id->key, sigp, lenp, data, datalen,
1145 alg, compat)) != 0)
1146 return r;
1147 /*
1148 * PKCS#11 tokens may not support all signature algorithms,
1149 * so check what we get back.
1150 */
1151 if ((r = sshkey_check_sigtype(*sigp, *lenp, alg)) != 0)
djm@openbsd.org26074382018-03-03 03:01:50 +00001152 return r;
1153 return 0;
1154 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001155
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001156 /* Load the private key from the file. */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001157 if ((prv = load_identity_file(id)) == NULL)
djm@openbsd.org0d1451a2016-02-23 01:34:14 +00001158 return SSH_ERR_KEY_NOT_FOUND;
djm@openbsd.orgc4972d02017-08-11 04:47:12 +00001159 if (id->key != NULL && !sshkey_equal_public(prv, id->key)) {
1160 error("%s: private key %s contents do not match public",
1161 __func__, id->filename);
1162 return SSH_ERR_KEY_NOT_FOUND;
1163 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001164 r = sshkey_sign(prv, sigp, lenp, data, datalen, alg, compat);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001165 sshkey_free(prv);
djm@openbsd.org26074382018-03-03 03:01:50 +00001166 return r;
Damien Miller280ecfb2003-05-14 13:46:00 +10001167}
1168
1169static int
djm@openbsd.org1e245522017-03-11 23:40:26 +00001170id_filename_matches(Identity *id, Identity *private_id)
1171{
1172 const char *suffixes[] = { ".pub", "-cert.pub", NULL };
1173 size_t len = strlen(id->filename), plen = strlen(private_id->filename);
1174 size_t i, slen;
1175
1176 if (strcmp(id->filename, private_id->filename) == 0)
1177 return 1;
1178 for (i = 0; suffixes[i]; i++) {
1179 slen = strlen(suffixes[i]);
1180 if (len > slen && plen == len - slen &&
1181 strcmp(id->filename + (len - slen), suffixes[i]) == 0 &&
1182 memcmp(id->filename, private_id->filename, plen) == 0)
1183 return 1;
1184 }
1185 return 0;
1186}
1187
1188static int
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001189sign_and_send_pubkey(struct ssh *ssh, Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +10001190{
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001191 struct sshbuf *b = NULL;
1192 Identity *private_id, *sign_id = NULL;
1193 u_char *signature = NULL;
1194 size_t slen = 0, skip = 0;
1195 int r, fallback_sigtype, sent = 0;
1196 char *alg = NULL, *fp = NULL;
1197 const char *loc = "";
Damien Millereba71ba2000-04-29 23:57:08 +10001198
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001199 if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1200 SSH_FP_DEFAULT)) == NULL)
1201 return 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001202
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001203 debug3("%s: %s %s", __func__, sshkey_type(id->key), fp);
Damien Millereba71ba2000-04-29 23:57:08 +10001204
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001205 /*
1206 * If the key is an certificate, try to find a matching private key
1207 * and use it to complete the signature.
djm@openbsd.org9ee692f2016-04-28 14:30:21 +00001208 * If no such private key exists, fall back to trying the certificate
1209 * key itself in case it has a private half already loaded.
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001210 * This will try to set sign_id to the private key that will perform
1211 * the signature.
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001212 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001213 if (sshkey_is_cert(id->key)) {
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001214 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1215 if (sshkey_equal_public(id->key, private_id->key) &&
1216 id->key->type != private_id->key->type) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001217 sign_id = private_id;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001218 break;
1219 }
1220 }
djm@openbsd.org1e245522017-03-11 23:40:26 +00001221 /*
1222 * Exact key matches are preferred, but also allow
1223 * filename matches for non-PKCS#11/agent keys that
1224 * didn't load public keys. This supports the case
1225 * of keeping just a private key file and public
1226 * certificate on disk.
1227 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001228 if (sign_id == NULL &&
1229 !id->isprivate && id->agent_fd == -1 &&
djm@openbsd.org1e245522017-03-11 23:40:26 +00001230 (id->key->flags & SSHKEY_FLAG_EXT) == 0) {
1231 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1232 if (private_id->key == NULL &&
1233 id_filename_matches(id, private_id)) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001234 sign_id = private_id;
djm@openbsd.org1e245522017-03-11 23:40:26 +00001235 break;
1236 }
1237 }
1238 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001239 if (sign_id != NULL) {
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001240 debug2("%s: using private key \"%s\"%s for "
1241 "certificate", __func__, id->filename,
1242 id->agent_fd != -1 ? " from agent" : "");
1243 } else {
djm@openbsd.orgc38905b2016-03-14 16:20:54 +00001244 debug("%s: no separate private key for certificate "
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001245 "\"%s\"", __func__, id->filename);
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001246 }
1247 }
1248
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001249 /*
1250 * If the above didn't select another identity to do the signing
1251 * then default to the one we started with.
1252 */
1253 if (sign_id == NULL)
1254 sign_id = id;
1255
1256 /* assemble and sign data */
1257 for (fallback_sigtype = 0; fallback_sigtype <= 1; fallback_sigtype++) {
1258 free(alg);
1259 slen = 0;
1260 signature = NULL;
1261 if ((alg = key_sig_algorithm(fallback_sigtype ? NULL : ssh,
1262 id->key)) == NULL) {
1263 error("%s: no mutual signature supported", __func__);
1264 goto out;
1265 }
1266 debug3("%s: signing using %s", __func__, alg);
1267
1268 sshbuf_free(b);
1269 if ((b = sshbuf_new()) == NULL)
1270 fatal("%s: sshbuf_new failed", __func__);
1271 if (datafellows & SSH_OLD_SESSIONID) {
1272 if ((r = sshbuf_put(b, session_id2,
1273 session_id2_len)) != 0) {
1274 fatal("%s: sshbuf_put: %s",
1275 __func__, ssh_err(r));
1276 }
1277 } else {
1278 if ((r = sshbuf_put_string(b, session_id2,
1279 session_id2_len)) != 0) {
1280 fatal("%s: sshbuf_put_string: %s",
1281 __func__, ssh_err(r));
1282 }
1283 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001284 skip = sshbuf_len(b);
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001285 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1286 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1287 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1288 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1289 (r = sshbuf_put_u8(b, 1)) != 0 ||
1290 (r = sshbuf_put_cstring(b, alg)) != 0 ||
1291 (r = sshkey_puts(id->key, b)) != 0) {
1292 fatal("%s: assemble signed data: %s",
1293 __func__, ssh_err(r));
1294 }
1295
1296 /* generate signature */
1297 r = identity_sign(sign_id, &signature, &slen,
1298 sshbuf_ptr(b), sshbuf_len(b), datafellows, alg);
1299 if (r == 0)
1300 break;
1301 else if (r == SSH_ERR_KEY_NOT_FOUND)
1302 goto out; /* soft failure */
1303 else if (r == SSH_ERR_SIGN_ALG_UNSUPPORTED &&
1304 !fallback_sigtype) {
1305 if (sign_id->agent_fd != -1)
1306 loc = "agent ";
1307 else if ((sign_id->key->flags & SSHKEY_FLAG_EXT) != 0)
1308 loc = "token ";
1309 logit("%skey %s %s returned incorrect signature type",
1310 loc, sshkey_type(id->key), fp);
1311 continue;
1312 }
1313 error("%s: signing failed: %s", __func__, ssh_err(r));
1314 goto out;
Damien Millerad833b32000-08-23 10:46:23 +10001315 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001316 if (slen == 0 || signature == NULL) /* shouldn't happen */
1317 fatal("%s: no signature", __func__);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001318
Damien Millereba71ba2000-04-29 23:57:08 +10001319 /* append signature */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001320 if ((r = sshbuf_put_string(b, signature, slen)) != 0)
1321 fatal("%s: append signature: %s", __func__, ssh_err(r));
Damien Millereba71ba2000-04-29 23:57:08 +10001322
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001323#ifdef DEBUG_PK
1324 sshbuf_dump(b, stderr);
1325#endif
Damien Millereba71ba2000-04-29 23:57:08 +10001326 /* skip session id and packet type */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001327 if ((r = sshbuf_consume(b, skip + 1)) != 0)
1328 fatal("%s: consume: %s", __func__, ssh_err(r));
Damien Millereba71ba2000-04-29 23:57:08 +10001329
1330 /* put remaining data from buffer into packet */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001331 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1332 (r = sshpkt_putb(ssh, b)) != 0 ||
1333 (r = sshpkt_send(ssh)) != 0)
1334 fatal("%s: enqueue request: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001335
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001336 /* success */
1337 sent = 1;
1338
1339 out:
1340 free(fp);
1341 free(alg);
1342 sshbuf_free(b);
1343 freezero(signature, slen);
1344 return sent;
Damien Miller994cf142000-07-21 10:19:44 +10001345}
1346
Ben Lindstrombba81212001-06-25 05:01:22 +00001347static int
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001348send_pubkey_test(struct ssh *ssh, Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001349{
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001350 u_char *blob = NULL;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001351 char *alg = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001352 size_t bloblen;
1353 u_int have_sig = 0;
1354 int sent = 0, r;
Damien Miller994cf142000-07-21 10:19:44 +10001355
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001356 if ((alg = key_sig_algorithm(ssh, id->key)) == NULL) {
1357 debug("%s: no mutual signature algorithm", __func__);
1358 goto out;
1359 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001360
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001361 if ((r = sshkey_to_blob(id->key, &blob, &bloblen)) != 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001362 /* we cannot handle this key */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001363 debug3("%s: cannot handle key", __func__);
1364 goto out;
Damien Miller994cf142000-07-21 10:19:44 +10001365 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001366 /* register callback for USERAUTH_PK_OK message */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001367 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001368
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001369 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1370 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1371 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1372 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1373 (r = sshpkt_put_u8(ssh, have_sig)) != 0 ||
1374 (r = sshpkt_put_cstring(ssh, alg)) != 0 ||
1375 (r = sshpkt_put_string(ssh, blob, bloblen)) != 0 ||
1376 (r = sshpkt_send(ssh)) != 0)
1377 fatal("%s: %s", __func__, ssh_err(r));
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001378 sent = 1;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001379
1380 out:
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001381 free(alg);
1382 free(blob);
1383 return sent;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001384}
1385
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001386static struct sshkey *
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001387load_identity_file(Identity *id)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001388{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001389 struct sshkey *private = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001390 char prompt[300], *passphrase, *comment;
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001391 int r, perm_ok = 0, quit = 0, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001392 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001393
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001394 if (stat(id->filename, &st) < 0) {
1395 (id->userprovided ? logit : debug3)("no such identity: %s: %s",
1396 id->filename, strerror(errno));
Ben Lindstrom329782e2001-03-10 17:08:59 +00001397 return NULL;
1398 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001399 snprintf(prompt, sizeof prompt,
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001400 "Enter passphrase for key '%.100s': ", id->filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001401 for (i = 0; i <= options.number_of_password_prompts; i++) {
1402 if (i == 0)
1403 passphrase = "";
1404 else {
Damien Miller62cee002000-09-23 17:15:56 +11001405 passphrase = read_passphrase(prompt, 0);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001406 if (*passphrase == '\0') {
Damien Miller62cee002000-09-23 17:15:56 +11001407 debug2("no passphrase given, try next key");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001408 free(passphrase);
1409 break;
Damien Miller62cee002000-09-23 17:15:56 +11001410 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001411 }
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001412 switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
1413 passphrase, &private, &comment, &perm_ok))) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001414 case 0:
1415 break;
1416 case SSH_ERR_KEY_WRONG_PASSPHRASE:
1417 if (options.batch_mode) {
1418 quit = 1;
1419 break;
1420 }
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001421 if (i != 0)
1422 debug2("bad passphrase given, try again...");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001423 break;
1424 case SSH_ERR_SYSTEM_ERROR:
1425 if (errno == ENOENT) {
1426 debug2("Load key \"%s\": %s",
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001427 id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001428 quit = 1;
1429 break;
1430 }
1431 /* FALLTHROUGH */
1432 default:
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001433 error("Load key \"%s\": %s", id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001434 quit = 1;
1435 break;
1436 }
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001437 if (!quit && private != NULL && id->agent_fd == -1 &&
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001438 !(id->key && id->isprivate))
1439 maybe_add_key_to_agent(id->filename, private, comment,
1440 passphrase);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001441 if (i > 0)
1442 freezero(passphrase, strlen(passphrase));
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001443 free(comment);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001444 if (private != NULL || quit)
1445 break;
Damien Miller994cf142000-07-21 10:19:44 +10001446 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001447 return private;
1448}
1449
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001450static int
1451key_type_allowed_by_config(struct sshkey *key)
1452{
1453 if (match_pattern_list(sshkey_ssh_name(key),
1454 options.pubkey_key_types, 0) == 1)
1455 return 1;
1456
1457 /* RSA keys/certs might be allowed by alternate signature types */
1458 switch (key->type) {
1459 case KEY_RSA:
1460 if (match_pattern_list("rsa-sha2-512",
1461 options.pubkey_key_types, 0) == 1)
1462 return 1;
1463 if (match_pattern_list("rsa-sha2-256",
1464 options.pubkey_key_types, 0) == 1)
1465 return 1;
1466 break;
1467 case KEY_RSA_CERT:
1468 if (match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
1469 options.pubkey_key_types, 0) == 1)
1470 return 1;
1471 if (match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
1472 options.pubkey_key_types, 0) == 1)
1473 return 1;
1474 break;
1475 }
1476 return 0;
1477}
1478
1479
Damien Miller280ecfb2003-05-14 13:46:00 +10001480/*
1481 * try keys in the following order:
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001482 * 1. certificates listed in the config file
1483 * 2. other input certificates
1484 * 3. agent keys that are found in the config file
1485 * 4. other agent keys
1486 * 5. keys that are only listed in the config file
Damien Miller280ecfb2003-05-14 13:46:00 +10001487 */
1488static void
1489pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001490{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001491 struct identity *id, *id2, *tmp;
1492 struct idlist agent, files, *preferred;
1493 struct sshkey *key;
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001494 int agent_fd = -1, i, r, found;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001495 size_t j;
1496 struct ssh_identitylist *idlist;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001497 char *ident;
Damien Millerad833b32000-08-23 10:46:23 +10001498
Damien Miller280ecfb2003-05-14 13:46:00 +10001499 TAILQ_INIT(&agent); /* keys from the agent */
1500 TAILQ_INIT(&files); /* keys from the config file */
1501 preferred = &authctxt->keys;
1502 TAILQ_INIT(preferred); /* preferred order of keys */
1503
Damien Millercb6b68b2012-12-03 09:49:52 +11001504 /* list of keys stored in the filesystem and PKCS#11 */
Damien Miller280ecfb2003-05-14 13:46:00 +10001505 for (i = 0; i < options.num_identity_files; i++) {
1506 key = options.identity_keys[i];
Damien Miller0a80ca12010-02-27 07:55:05 +11001507 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1508 continue;
Damien Miller280ecfb2003-05-14 13:46:00 +10001509 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001510 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001511 id->agent_fd = -1;
Damien Miller280ecfb2003-05-14 13:46:00 +10001512 id->key = key;
1513 id->filename = xstrdup(options.identity_files[i]);
Darren Tucker19104782013-04-05 11:13:08 +11001514 id->userprovided = options.identity_file_userprovided[i];
Damien Miller280ecfb2003-05-14 13:46:00 +10001515 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001516 }
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001517 /* list of certificates specified by user */
1518 for (i = 0; i < options.num_certificate_files; i++) {
1519 key = options.certificates[i];
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001520 if (!sshkey_is_cert(key) || key->cert == NULL ||
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001521 key->cert->type != SSH2_CERT_TYPE_USER)
1522 continue;
1523 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001524 id->agent_fd = -1;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001525 id->key = key;
1526 id->filename = xstrdup(options.certificate_files[i]);
1527 id->userprovided = options.certificate_file_userprovided[i];
1528 TAILQ_INSERT_TAIL(preferred, id, next);
1529 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001530 /* list of keys supported by the agent */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001531 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1532 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1533 debug("%s: ssh_get_authentication_socket: %s",
1534 __func__, ssh_err(r));
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +00001535 } else if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001536 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1537 debug("%s: ssh_fetch_identitylist: %s",
1538 __func__, ssh_err(r));
markus@openbsd.orgfc77ccd2016-01-14 22:56:56 +00001539 close(agent_fd);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001540 } else {
1541 for (j = 0; j < idlist->nkeys; j++) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001542 found = 0;
1543 TAILQ_FOREACH(id, &files, next) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001544 /*
1545 * agent keys from the config file are
1546 * preferred
1547 */
1548 if (sshkey_equal(idlist->keys[j], id->key)) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001549 TAILQ_REMOVE(&files, id, next);
1550 TAILQ_INSERT_TAIL(preferred, id, next);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001551 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001552 found = 1;
1553 break;
1554 }
1555 }
Damien Millerbd394c32004-03-08 23:12:36 +11001556 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001557 id = xcalloc(1, sizeof(*id));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001558 /* XXX "steals" key/comment from idlist */
1559 id->key = idlist->keys[j];
1560 id->filename = idlist->comments[j];
1561 idlist->keys[j] = NULL;
1562 idlist->comments[j] = NULL;
1563 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001564 TAILQ_INSERT_TAIL(&agent, id, next);
1565 }
1566 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001567 ssh_free_identitylist(idlist);
Damien Miller280ecfb2003-05-14 13:46:00 +10001568 /* append remaining agent keys */
1569 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1570 TAILQ_REMOVE(&agent, id, next);
1571 TAILQ_INSERT_TAIL(preferred, id, next);
1572 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001573 authctxt->agent_fd = agent_fd;
Damien Miller62cee002000-09-23 17:15:56 +11001574 }
djm@openbsd.org82f24c32016-05-23 23:30:50 +00001575 /* Prefer PKCS11 keys that are explicitly listed */
1576 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1577 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
1578 continue;
1579 found = 0;
1580 TAILQ_FOREACH(id2, &files, next) {
1581 if (id2->key == NULL ||
1582 (id2->key->flags & SSHKEY_FLAG_EXT) == 0)
1583 continue;
1584 if (sshkey_equal(id->key, id2->key)) {
1585 TAILQ_REMOVE(&files, id, next);
1586 TAILQ_INSERT_TAIL(preferred, id, next);
1587 found = 1;
1588 break;
1589 }
1590 }
1591 /* If IdentitiesOnly set and key not found then don't use it */
1592 if (!found && options.identities_only) {
1593 TAILQ_REMOVE(&files, id, next);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001594 freezero(id, sizeof(*id));
djm@openbsd.org82f24c32016-05-23 23:30:50 +00001595 }
1596 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001597 /* append remaining keys from the config file */
1598 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1599 TAILQ_REMOVE(&files, id, next);
1600 TAILQ_INSERT_TAIL(preferred, id, next);
1601 }
djm@openbsd.orge679c092015-10-13 16:15:21 +00001602 /* finally, filter by PubkeyAcceptedKeyTypes */
1603 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
djm@openbsd.orgd78b75d2018-07-03 13:07:58 +00001604 if (id->key != NULL && !key_type_allowed_by_config(id->key)) {
djm@openbsd.orge679c092015-10-13 16:15:21 +00001605 debug("Skipping %s key %s - "
1606 "not in PubkeyAcceptedKeyTypes",
1607 sshkey_ssh_name(id->key), id->filename);
1608 TAILQ_REMOVE(preferred, id, next);
1609 sshkey_free(id->key);
1610 free(id->filename);
1611 memset(id, 0, sizeof(*id));
1612 continue;
1613 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001614 }
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001615 /* List the keys we plan on using */
1616 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1617 ident = format_identity(id);
1618 debug("Will attempt key: %s", ident);
1619 free(ident);
1620 }
1621 debug2("%s: done", __func__);
Damien Miller280ecfb2003-05-14 13:46:00 +10001622}
1623
1624static void
1625pubkey_cleanup(Authctxt *authctxt)
1626{
1627 Identity *id;
1628
djm@openbsd.orgaaed6352018-11-16 02:46:20 +00001629 if (authctxt->agent_fd != -1) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001630 ssh_close_authentication_socket(authctxt->agent_fd);
djm@openbsd.orgaaed6352018-11-16 02:46:20 +00001631 authctxt->agent_fd = -1;
1632 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001633 for (id = TAILQ_FIRST(&authctxt->keys); id;
1634 id = TAILQ_FIRST(&authctxt->keys)) {
1635 TAILQ_REMOVE(&authctxt->keys, id, next);
mmcc@openbsd.org89540b62015-12-11 02:31:47 +00001636 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +10001637 free(id->filename);
1638 free(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001639 }
Damien Millereba71ba2000-04-29 23:57:08 +10001640}
1641
djm@openbsd.orgb9844a42016-12-04 23:54:02 +00001642static void
1643pubkey_reset(Authctxt *authctxt)
1644{
1645 Identity *id;
1646
1647 TAILQ_FOREACH(id, &authctxt->keys, next)
1648 id->tried = 0;
1649}
1650
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001651static int
1652try_identity(Identity *id)
1653{
1654 if (!id->key)
1655 return (0);
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001656 if (sshkey_type_plain(id->key->type) == KEY_RSA &&
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001657 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1658 debug("Skipped %s key %s for RSA/MD5 server",
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001659 sshkey_type(id->key), id->filename);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001660 return (0);
1661 }
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001662 return 1;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001663}
1664
Damien Miller62cee002000-09-23 17:15:56 +11001665int
1666userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001667{
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001668 struct ssh *ssh = active_state; /* XXX */
Damien Miller280ecfb2003-05-14 13:46:00 +10001669 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001670 int sent = 0;
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001671 char *ident;
Damien Millereba71ba2000-04-29 23:57:08 +10001672
Damien Miller280ecfb2003-05-14 13:46:00 +10001673 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1674 if (id->tried++)
1675 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001676 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001677 TAILQ_REMOVE(&authctxt->keys, id, next);
1678 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1679 /*
1680 * send a test message if we have the public key. for
1681 * encrypted keys we cannot do this and have to load the
1682 * private key instead
1683 */
Damien Miller324541e2013-12-31 12:25:40 +11001684 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001685 if (try_identity(id)) {
djm@openbsd.orgbeb9e522018-09-14 05:26:27 +00001686 ident = format_identity(id);
1687 debug("Offering public key: %s", ident);
1688 free(ident);
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001689 sent = send_pubkey_test(ssh, authctxt, id);
Damien Miller324541e2013-12-31 12:25:40 +11001690 }
1691 } else {
Damien Miller280ecfb2003-05-14 13:46:00 +10001692 debug("Trying private key: %s", id->filename);
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001693 id->key = load_identity_file(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001694 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001695 if (try_identity(id)) {
1696 id->isprivate = 1;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001697 sent = sign_and_send_pubkey(ssh,
Damien Miller324541e2013-12-31 12:25:40 +11001698 authctxt, id);
1699 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001700 sshkey_free(id->key);
Damien Miller280ecfb2003-05-14 13:46:00 +10001701 id->key = NULL;
djm@openbsd.orgb9844a42016-12-04 23:54:02 +00001702 id->isprivate = 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001703 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001704 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001705 if (sent)
1706 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001707 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001708 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001709}
Damien Millereba71ba2000-04-29 23:57:08 +10001710
Damien Miller874d77b2000-10-14 16:23:11 +11001711/*
1712 * Send userauth request message specifying keyboard-interactive method.
1713 */
1714int
1715userauth_kbdint(Authctxt *authctxt)
1716{
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001717 struct ssh *ssh = active_state; /* XXX */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001718 int r;
Damien Miller874d77b2000-10-14 16:23:11 +11001719
djm@openbsd.org4a526942019-01-04 03:27:50 +00001720 if (authctxt->attempt_kbdint++ >= options.number_of_password_prompts)
Damien Miller874d77b2000-10-14 16:23:11 +11001721 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001722 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
djm@openbsd.org4a526942019-01-04 03:27:50 +00001723 if (authctxt->attempt_kbdint > 1 && !authctxt->info_req_seen) {
Ben Lindstrom7d199962001-09-12 18:29:00 +00001724 debug3("userauth_kbdint: disable: no info_req_seen");
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001725 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
Ben Lindstrom7d199962001-09-12 18:29:00 +00001726 return 0;
1727 }
Damien Miller874d77b2000-10-14 16:23:11 +11001728
1729 debug2("userauth_kbdint");
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001730 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1731 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1732 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1733 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1734 (r = sshpkt_put_cstring(ssh, "")) != 0 || /* lang */
1735 (r = sshpkt_put_cstring(ssh, options.kbd_interactive_devices ?
1736 options.kbd_interactive_devices : "")) != 0 ||
1737 (r = sshpkt_send(ssh)) != 0)
1738 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +11001739
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001740 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
Damien Miller874d77b2000-10-14 16:23:11 +11001741 return 1;
1742}
1743
1744/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001745 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001746 */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001747int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001748input_userauth_info_req(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller874d77b2000-10-14 16:23:11 +11001749{
markus@openbsd.org94583be2017-05-30 14:19:15 +00001750 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001751 char *name = NULL, *inst = NULL, *lang = NULL, *prompt = NULL;
1752 char *response = NULL;
1753 u_char echo = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001754 u_int num_prompts, i;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001755 int r;
Damien Miller874d77b2000-10-14 16:23:11 +11001756
1757 debug2("input_userauth_info_req");
1758
1759 if (authctxt == NULL)
1760 fatal("input_userauth_info_req: no authentication context");
1761
Ben Lindstrom7d199962001-09-12 18:29:00 +00001762 authctxt->info_req_seen = 1;
1763
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001764 if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 ||
1765 (r = sshpkt_get_cstring(ssh, &inst, NULL)) != 0 ||
1766 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1767 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +11001768 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001769 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001770 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001771 logit("%s", inst);
Damien Miller874d77b2000-10-14 16:23:11 +11001772
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001773 if ((r = sshpkt_get_u32(ssh, &num_prompts)) != 0)
1774 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +11001775 /*
1776 * Begin to build info response packet based on prompts requested.
1777 * We commit to providing the correct number of responses, so if
1778 * further on we run into a problem that prevents this, we have to
1779 * be sure and clean this up and send a correct error response.
1780 */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001781 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE)) != 0 ||
1782 (r = sshpkt_put_u32(ssh, num_prompts)) != 0)
1783 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +11001784
Ben Lindstrom551ea372001-06-05 18:56:16 +00001785 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001786 for (i = 0; i < num_prompts; i++) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001787 if ((r = sshpkt_get_cstring(ssh, &prompt, NULL)) != 0 ||
1788 (r = sshpkt_get_u8(ssh, &echo)) != 0)
1789 goto out;
Ben Lindstrom949974b2001-06-25 05:20:31 +00001790 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001791 if ((r = sshpkt_put_cstring(ssh, response)) != 0)
1792 goto out;
1793 freezero(response, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +10001794 free(prompt);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001795 response = prompt = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001796 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001797 /* done with parsing incoming message. */
1798 if ((r = sshpkt_get_end(ssh)) != 0 ||
1799 (r = sshpkt_add_padding(ssh, 64)) != 0)
1800 goto out;
1801 r = sshpkt_send(ssh);
1802 out:
1803 if (response)
1804 freezero(response, strlen(response));
1805 free(prompt);
1806 free(name);
1807 free(inst);
1808 free(lang);
1809 return r;
Damien Miller874d77b2000-10-14 16:23:11 +11001810}
Damien Miller62cee002000-09-23 17:15:56 +11001811
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001812static int
djm@openbsd.orged1df722019-01-19 21:34:45 +00001813ssh_keysign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001814 const u_char *data, size_t datalen)
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001815{
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001816 struct sshbuf *b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001817 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001818 pid_t pid;
djm@openbsd.orged1df722019-01-19 21:34:45 +00001819 int i, r, to[2], from[2], status, sock = ssh_packet_get_connection_in(ssh);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001820 u_char rversion = 0, version = 2;
1821 void (*osigchld)(int);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001822
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001823 *sigp = NULL;
1824 *lenp = 0;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001825
Ben Lindstrom5206b952002-06-06 19:59:29 +00001826 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001827 error("%s: not installed: %s", __func__, strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001828 return -1;
1829 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001830 if (fflush(stdout) != 0) {
1831 error("%s: fflush: %s", __func__, strerror(errno));
1832 return -1;
1833 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001834 if (pipe(to) < 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 (pipe(from) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001839 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001840 return -1;
1841 }
1842 if ((pid = fork()) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001843 error("%s: fork: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001844 return -1;
1845 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001846 osigchld = signal(SIGCHLD, SIG_DFL);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001847 if (pid == 0) {
Darren Tucker6e7fe1c2010-01-08 17:07:22 +11001848 /* keep the socket on exec */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001849 fcntl(sock, F_SETFD, 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001850 close(from[0]);
1851 if (dup2(from[1], STDOUT_FILENO) < 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001852 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001853 close(to[1]);
1854 if (dup2(to[0], STDIN_FILENO) < 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001855 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001856 close(from[1]);
1857 close(to[0]);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001858 /* Close everything but stdio and the socket */
1859 for (i = STDERR_FILENO + 1; i < sock; i++)
1860 close(i);
1861 closefrom(sock + 1);
1862 debug3("%s: [child] pid=%ld, exec %s",
1863 __func__, (long)getpid(), _PATH_SSH_KEY_SIGN);
mmcc@openbsd.org94141b72015-12-11 00:20:04 +00001864 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001865 fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001866 strerror(errno));
1867 }
1868 close(from[1]);
1869 close(to[0]);
1870
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001871 if ((b = sshbuf_new()) == NULL)
1872 fatal("%s: sshbuf_new failed", __func__);
1873 /* send # of sock, data to be signed */
djm@openbsd.org326e2fa2017-01-30 00:32:28 +00001874 if ((r = sshbuf_put_u32(b, sock)) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001875 (r = sshbuf_put_string(b, data, datalen)) != 0)
1876 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1877 if (ssh_msg_send(to[1], version, b) == -1)
1878 fatal("%s: couldn't send request", __func__);
1879 sshbuf_reset(b);
1880 r = ssh_msg_recv(from[0], b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001881 close(from[0]);
1882 close(to[1]);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001883 if (r < 0) {
1884 error("%s: no reply", __func__);
1885 goto fail;
1886 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001887
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001888 errno = 0;
1889 while (waitpid(pid, &status, 0) < 0) {
1890 if (errno != EINTR) {
1891 error("%s: waitpid %ld: %s",
1892 __func__, (long)pid, strerror(errno));
1893 goto fail;
1894 }
1895 }
1896 if (!WIFEXITED(status)) {
1897 error("%s: exited abnormally", __func__);
1898 goto fail;
1899 }
1900 if (WEXITSTATUS(status) != 0) {
1901 error("%s: exited with status %d",
1902 __func__, WEXITSTATUS(status));
1903 goto fail;
1904 }
1905 if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
1906 error("%s: buffer error: %s", __func__, ssh_err(r));
1907 goto fail;
1908 }
1909 if (rversion != version) {
1910 error("%s: bad version", __func__);
1911 goto fail;
1912 }
1913 if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
1914 error("%s: buffer error: %s", __func__, ssh_err(r));
1915 fail:
1916 signal(SIGCHLD, osigchld);
1917 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001918 return -1;
1919 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001920 signal(SIGCHLD, osigchld);
1921 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001922
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001923 return 0;
1924}
1925
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001926int
1927userauth_hostbased(Authctxt *authctxt)
1928{
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001929 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001930 struct sshkey *private = NULL;
1931 struct sshbuf *b = NULL;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001932 u_char *sig = NULL, *keyblob = NULL;
1933 char *fp = NULL, *chost = NULL, *lname = NULL;
1934 size_t siglen = 0, keylen = 0;
1935 int i, r, success = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001936
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001937 if (authctxt->ktypes == NULL) {
1938 authctxt->oktypes = xstrdup(options.hostbased_key_types);
1939 authctxt->ktypes = authctxt->oktypes;
1940 }
djm@openbsd.org1195f4c2015-01-08 10:14:08 +00001941
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001942 /*
1943 * Work through each listed type pattern in HostbasedKeyTypes,
1944 * trying each hostkey that matches the type in turn.
1945 */
1946 for (;;) {
1947 if (authctxt->active_ktype == NULL)
1948 authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
1949 if (authctxt->active_ktype == NULL ||
1950 *authctxt->active_ktype == '\0')
1951 break;
1952 debug3("%s: trying key type %s", __func__,
1953 authctxt->active_ktype);
1954
1955 /* check for a useful key */
1956 private = NULL;
1957 for (i = 0; i < authctxt->sensitive->nkeys; i++) {
1958 if (authctxt->sensitive->keys[i] == NULL ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001959 authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
1960 continue;
1961 if (match_pattern_list(
1962 sshkey_ssh_name(authctxt->sensitive->keys[i]),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001963 authctxt->active_ktype, 0) != 1)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001964 continue;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001965 /* we take and free the key */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001966 private = authctxt->sensitive->keys[i];
1967 authctxt->sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001968 break;
1969 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001970 /* Found one */
1971 if (private != NULL)
1972 break;
1973 /* No more keys of this type; advance */
1974 authctxt->active_ktype = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001975 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001976 if (private == NULL) {
1977 free(authctxt->oktypes);
1978 authctxt->oktypes = authctxt->ktypes = NULL;
1979 authctxt->active_ktype = NULL;
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001980 debug("No more client hostkeys for hostbased authentication.");
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001981 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001982 }
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001983
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001984 if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
1985 SSH_FP_DEFAULT)) == NULL) {
1986 error("%s: sshkey_fingerprint failed", __func__);
1987 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001988 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001989 debug("%s: trying hostkey %s %s",
1990 __func__, sshkey_ssh_name(private), fp);
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001991
Damien Miller91c18472001-11-12 11:02:03 +11001992 /* figure out a name for the client host */
djm@openbsd.orged1df722019-01-19 21:34:45 +00001993 if ((lname = get_local_name(ssh_packet_get_connection_in(ssh))) == NULL) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001994 error("%s: cannot get local ipaddr/name", __func__);
1995 goto out;
Damien Miller91c18472001-11-12 11:02:03 +11001996 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001997
1998 /* XXX sshbuf_put_stringf? */
1999 xasprintf(&chost, "%s.", lname);
2000 debug2("%s: chost %s", __func__, chost);
Damien Miller91c18472001-11-12 11:02:03 +11002001
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002002 /* construct data */
2003 if ((b = sshbuf_new()) == NULL) {
2004 error("%s: sshbuf_new failed", __func__);
2005 goto out;
2006 }
2007 if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
2008 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
2009 goto out;
2010 }
2011 if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
2012 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2013 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +00002014 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002015 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002016 (r = sshbuf_put_cstring(b, sshkey_ssh_name(private))) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002017 (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
2018 (r = sshbuf_put_cstring(b, chost)) != 0 ||
2019 (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
2020 error("%s: buffer error: %s", __func__, ssh_err(r));
2021 goto out;
2022 }
2023
2024#ifdef DEBUG_PK
2025 sshbuf_dump(b, stderr);
2026#endif
djm@openbsd.orged1df722019-01-19 21:34:45 +00002027 if ((r = ssh_keysign(ssh, private, &sig, &siglen,
2028 sshbuf_ptr(b), sshbuf_len(b))) != 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002029 error("sign using hostkey %s %s failed",
2030 sshkey_ssh_name(private), fp);
2031 goto out;
2032 }
2033 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2034 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
2035 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
2036 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002037 (r = sshpkt_put_cstring(ssh, sshkey_ssh_name(private))) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002038 (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
2039 (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
2040 (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
2041 (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
2042 (r = sshpkt_send(ssh)) != 0) {
2043 error("%s: packet error: %s", __func__, ssh_err(r));
2044 goto out;
2045 }
2046 success = 1;
2047
2048 out:
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002049 if (sig != NULL)
2050 freezero(sig, siglen);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002051 free(keyblob);
2052 free(lname);
2053 free(fp);
2054 free(chost);
2055 sshkey_free(private);
2056 sshbuf_free(b);
2057
2058 return success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002059}
2060
Damien Miller62cee002000-09-23 17:15:56 +11002061/* find auth method */
2062
Damien Miller62cee002000-09-23 17:15:56 +11002063/*
2064 * given auth method name, if configurable options permit this method fill
2065 * in auth_ident field and return true, otherwise return false.
2066 */
Ben Lindstrombba81212001-06-25 05:01:22 +00002067static int
Damien Miller62cee002000-09-23 17:15:56 +11002068authmethod_is_enabled(Authmethod *method)
2069{
2070 if (method == NULL)
2071 return 0;
2072 /* return false if options indicate this method is disabled */
2073 if (method->enabled == NULL || *method->enabled == 0)
2074 return 0;
2075 /* return false if batch mode is enabled but method needs interactive mode */
2076 if (method->batch_flag != NULL && *method->batch_flag != 0)
2077 return 0;
2078 return 1;
2079}
2080
Ben Lindstrombba81212001-06-25 05:01:22 +00002081static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11002082authmethod_lookup(const char *name)
2083{
2084 Authmethod *method = NULL;
2085 if (name != NULL)
2086 for (method = authmethods; method->name != NULL; method++)
2087 if (strcmp(name, method->name) == 0)
2088 return method;
2089 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
2090 return NULL;
2091}
2092
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002093/* XXX internal state */
2094static Authmethod *current = NULL;
2095static char *supported = NULL;
2096static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00002097
Damien Miller62cee002000-09-23 17:15:56 +11002098/*
2099 * Given the authentication method list sent by the server, return the
2100 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00002101 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00002102 */
Ben Lindstrombba81212001-06-25 05:01:22 +00002103static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11002104authmethod_get(char *authlist)
2105{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002106 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00002107 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00002108
Damien Miller62cee002000-09-23 17:15:56 +11002109 /* Use a suitable default if we're passed a nil list. */
2110 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002111 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11002112
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002113 if (supported == NULL || strcmp(authlist, supported) != 0) {
2114 debug3("start over, passed a different list %s", authlist);
Darren Tuckera627d422013-06-02 07:31:17 +10002115 free(supported);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002116 supported = xstrdup(authlist);
2117 preferred = options.preferred_authentications;
2118 debug3("preferred %s", preferred);
2119 current = NULL;
2120 } else if (current != NULL && authmethod_is_enabled(current))
2121 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10002122
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002123 for (;;) {
2124 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00002125 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002126 current = NULL;
2127 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11002128 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002129 preferred += next;
2130 debug3("authmethod_lookup %s", name);
2131 debug3("remaining preferred: %s", preferred);
2132 if ((current = authmethod_lookup(name)) != NULL &&
2133 authmethod_is_enabled(current)) {
2134 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00002135 debug("Next authentication method: %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002136 free(name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002137 return current;
2138 }
Darren Tuckere52a2602013-06-06 08:22:05 +10002139 free(name);
Damien Millereba71ba2000-04-29 23:57:08 +10002140 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002141}
Damien Miller62cee002000-09-23 17:15:56 +11002142
Ben Lindstrom79073822001-07-04 03:42:30 +00002143static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002144authmethods_get(void)
2145{
2146 Authmethod *method = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002147 struct sshbuf *b;
Damien Miller0e3b8722002-01-22 23:26:38 +11002148 char *list;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002149 int r;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002150
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002151 if ((b = sshbuf_new()) == NULL)
2152 fatal("%s: sshbuf_new failed", __func__);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002153 for (method = authmethods; method->name != NULL; method++) {
2154 if (authmethod_is_enabled(method)) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002155 if ((r = sshbuf_putf(b, "%s%s",
2156 sshbuf_len(b) ? "," : "", method->name)) != 0)
2157 fatal("%s: buffer error: %s",
2158 __func__, ssh_err(r));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002159 }
Damien Miller62cee002000-09-23 17:15:56 +11002160 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002161 if ((list = sshbuf_dup_string(b)) == NULL)
djm@openbsd.org1a31d022016-05-02 08:49:03 +00002162 fatal("%s: sshbuf_dup_string failed", __func__);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002163 sshbuf_free(b);
Damien Miller0e3b8722002-01-22 23:26:38 +11002164 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10002165}