blob: 10e4f0a086ec4fca82c38eb948be357cda6fd13b [file] [log] [blame]
djm@openbsd.orgc3903c32018-08-13 02:41:05 +00001/* $OpenBSD: sshconnect2.c,v 1.284 2018/08/13 02:41:05 djm Exp $ */
Damien Millereba71ba2000-04-29 23:57:08 +10002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Miller01ed2272008-11-05 16:20:46 +11004 * Copyright (c) 2008 Damien Miller. All rights reserved.
Damien Millereba71ba2000-04-29 23:57:08 +10005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100014 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
Damien Miller2eb63402006-03-15 11:09:42 +110028
Damien Miller9cf6d072006-03-15 11:29:24 +110029#include <sys/types.h>
Damien Millerd7834352006-08-05 12:39:39 +100030#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110031#include <sys/wait.h>
Damien Millerf17883e2006-03-15 11:45:54 +110032#include <sys/stat.h>
Damien Millereba71ba2000-04-29 23:57:08 +100033
Darren Tucker39972492006-07-12 22:22:46 +100034#include <errno.h>
Darren Tucker6e7fe1c2010-01-08 17:07:22 +110035#include <fcntl.h>
Darren Tuckerf520ea12007-05-20 15:11:33 +100036#include <netdb.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <pwd.h>
38#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100039#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100040#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100041#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100042#include <unistd.h>
Damien Miller63b4bcd2013-03-20 12:55:14 +110043#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
Damien Miller7ba0ca72008-07-17 18:57:06 +100044#include <vis.h>
Damien Miller2e28d862008-07-17 19:15:43 +100045#endif
Darren Tucker39972492006-07-12 22:22:46 +100046
Damien Miller9c617692003-05-14 14:31:11 +100047#include "openbsd-compat/sys-queue.h"
48
Damien Millerd7834352006-08-05 12:39:39 +100049#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100050#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "ssh2.h"
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +000052#include "sshbuf.h"
Damien Millereba71ba2000-04-29 23:57:08 +100053#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100054#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055#include "cipher.h"
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +000056#include "sshkey.h"
Damien Millereba71ba2000-04-29 23:57:08 +100057#include "kex.h"
58#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100059#include "sshconnect.h"
60#include "authfile.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000061#include "dh.h"
Damien Millerad833b32000-08-23 10:46:23 +100062#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "log.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100064#include "misc.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100065#include "readconf.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000066#include "match.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000067#include "dispatch.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000068#include "canohost.h"
Ben Lindstrom1bad2562002-06-06 19:57:33 +000069#include "msg.h"
70#include "pathnames.h"
Damien Miller6b4069a2006-06-13 13:05:15 +100071#include "uidswap.h"
Damien Millerd925dcd2010-12-01 12:21:51 +110072#include "hostfile.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +000073#include "ssherr.h"
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +000074#include "utf8.h"
Damien Miller874d77b2000-10-14 16:23:11 +110075
Darren Tucker0efd1552003-08-26 11:49:55 +100076#ifdef GSSAPI
77#include "ssh-gss.h"
78#endif
79
Damien Millereba71ba2000-04-29 23:57:08 +100080/* import */
81extern char *client_version_string;
82extern char *server_version_string;
83extern Options options;
84
85/*
86 * SSH2 key exchange
87 */
88
Ben Lindstrom46c16222000-12-22 01:43:59 +000089u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +100090u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100091
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000092char *xxx_host;
93struct sockaddr *xxx_hostaddr;
94
Ben Lindstrombba81212001-06-25 05:01:22 +000095static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +000096verify_host_key_callback(struct sshkey *hostkey, struct ssh *ssh)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000097{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000098 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +100099 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000100 return 0;
101}
102
Damien Millerd925dcd2010-12-01 12:21:51 +1100103static char *
104order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
105{
106 char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
107 size_t maxlen;
108 struct hostkeys *hostkeys;
109 int ktype;
Damien Miller295ee632011-05-29 21:42:31 +1000110 u_int i;
Damien Millerd925dcd2010-12-01 12:21:51 +1100111
112 /* Find all hostkeys for this hostname */
113 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
114 hostkeys = init_hostkeys();
Damien Miller295ee632011-05-29 21:42:31 +1000115 for (i = 0; i < options.num_user_hostfiles; i++)
116 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
117 for (i = 0; i < options.num_system_hostfiles; i++)
118 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
Damien Millerd925dcd2010-12-01 12:21:51 +1100119
120 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
121 maxlen = strlen(avail) + 1;
122 first = xmalloc(maxlen);
123 last = xmalloc(maxlen);
124 *first = *last = '\0';
125
126#define ALG_APPEND(to, from) \
127 do { \
128 if (*to != '\0') \
129 strlcat(to, ",", maxlen); \
130 strlcat(to, from, maxlen); \
131 } while (0)
132
133 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000134 if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
Damien Millerd925dcd2010-12-01 12:21:51 +1100135 fatal("%s: unknown alg %s", __func__, alg);
136 if (lookup_key_in_hostkeys_by_type(hostkeys,
djm@openbsd.org141efe42015-01-14 20:05:27 +0000137 sshkey_type_plain(ktype), NULL))
Damien Millerd925dcd2010-12-01 12:21:51 +1100138 ALG_APPEND(first, alg);
139 else
140 ALG_APPEND(last, alg);
141 }
142#undef ALG_APPEND
djm@openbsd.org35d60222015-01-18 13:33:34 +0000143 xasprintf(&ret, "%s%s%s", first,
144 (*first == '\0' || *last == '\0') ? "" : ",", last);
Damien Millerd925dcd2010-12-01 12:21:51 +1100145 if (*first != '\0')
146 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
147
Darren Tuckera627d422013-06-02 07:31:17 +1000148 free(first);
149 free(last);
150 free(hostname);
151 free(oavail);
Damien Millerd925dcd2010-12-01 12:21:51 +1100152 free_hostkeys(hostkeys);
153
154 return ret;
155}
156
Damien Millereba71ba2000-04-29 23:57:08 +1000157void
Damien Millerd925dcd2010-12-01 12:21:51 +1100158ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
Damien Miller874d77b2000-10-14 16:23:11 +1100159{
Damien Miller9235a032014-04-20 13:17:20 +1000160 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
djm@openbsd.org312d2f22018-07-04 13:49:31 +0000161 char *s, *all_key;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000162 struct kex *kex;
markus@openbsd.org57e783c2015-01-20 20:16:21 +0000163 int r;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000164
165 xxx_host = host;
166 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100167
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000168 if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
169 fatal("%s: kex_names_cat", __func__);
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000170 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(s);
Damien Millera0ff4662001-03-30 10:49:35 +1000171 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000172 compat_cipher_proposal(options.ciphers);
Damien Millera0ff4662001-03-30 10:49:35 +1000173 myproposal[PROPOSAL_ENC_ALGS_STOC] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000174 compat_cipher_proposal(options.ciphers);
dtucker@openbsd.org8c02e362016-05-24 04:43:45 +0000175 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
176 myproposal[PROPOSAL_COMP_ALGS_STOC] = options.compression ?
sf@openbsd.org168b46f2018-07-09 13:37:10 +0000177 "zlib@openssh.com,zlib,none" : "none,zlib@openssh.com,zlib";
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000178 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
179 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
180 if (options.hostkeyalgorithms != NULL) {
djm@openbsd.org312d2f22018-07-04 13:49:31 +0000181 all_key = sshkey_alg_list(0, 0, 1, ',');
182 if (kex_assemble_names(&options.hostkeyalgorithms,
183 KEX_DEFAULT_PK_ALG, all_key) != 0)
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000184 fatal("%s: kex_assemble_namelist", __func__);
djm@openbsd.org312d2f22018-07-04 13:49:31 +0000185 free(all_key);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100186 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000187 compat_pkalg_proposal(options.hostkeyalgorithms);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000188 } else {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000189 /* Enforce default */
190 options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG);
Damien Millerd925dcd2010-12-01 12:21:51 +1100191 /* Prefer algorithms that we already have keys for */
192 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100193 compat_pkalg_proposal(
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000194 order_hostkeyalgs(host, hostaddr, port));
Damien Millerd925dcd2010-12-01 12:21:51 +1100195 }
Damien Miller874d77b2000-10-14 16:23:11 +1100196
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000197 if (options.rekey_limit || options.rekey_interval)
dtucker@openbsd.orgc998bf02017-02-03 02:56:00 +0000198 packet_set_rekey_limits(options.rekey_limit,
199 options.rekey_interval);
Damien Millera5539d22003-04-09 20:50:06 +1000200
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000201 /* start key exchange */
markus@openbsd.org57e783c2015-01-20 20:16:21 +0000202 if ((r = kex_setup(active_state, myproposal)) != 0)
203 fatal("kex_setup: %s", ssh_err(r));
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000204 kex = active_state->kex;
Damien Miller1f0311c2014-05-15 14:24:09 +1000205#ifdef WITH_OPENSSL
Damien Miller8e7fb332003-02-24 12:03:03 +1100206 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000207 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +0000208 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_client;
209 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_client;
210 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100211 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100212 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100213# ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000214 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100215# endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000216#endif
Damien Miller1e124262013-11-04 08:26:52 +1100217 kex->kex[KEX_C25519_SHA256] = kexc25519_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000218 kex->client_version_string=client_version_string;
219 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000220 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100221
markus@openbsd.org92e9fe62017-05-31 07:00:13 +0000222 ssh_dispatch_run_fatal(active_state, DISPATCH_BLOCK, &kex->done);
Damien Miller874d77b2000-10-14 16:23:11 +1100223
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000224 /* remove ext-info from the KEX proposals for rekeying */
225 myproposal[PROPOSAL_KEX_ALGS] =
djm@openbsd.orgc3903c32018-08-13 02:41:05 +0000226 compat_kex_proposal(options.kex_algorithms);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000227 if ((r = kex_prop2buf(kex->my, myproposal)) != 0)
228 fatal("kex_prop2buf: %s", ssh_err(r));
Darren Tucker36331b52010-01-08 16:50:41 +1100229
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000230 session_id2 = kex->session_id;
231 session_id2_len = kex->session_id_len;
232
Damien Miller874d77b2000-10-14 16:23:11 +1100233#ifdef DEBUG_KEXDH
234 /* send 1st encrypted/maced/compressed message */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000235 if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 ||
236 (r = sshpkt_put_cstring(ssh, "markus")) != 0 ||
markus@openbsd.org394a8422018-07-11 18:55:11 +0000237 (r = sshpkt_send(ssh)) != 0 ||
238 (r = ssh_packet_write_wait(ssh)) != 0)
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000239 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100240#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000241}
Damien Millerb1715dc2000-05-30 13:44:51 +1000242
Damien Millereba71ba2000-04-29 23:57:08 +1000243/*
244 * Authenticate user
245 */
Damien Miller62cee002000-09-23 17:15:56 +1100246
djm@openbsd.org141efe42015-01-14 20:05:27 +0000247typedef struct cauthctxt Authctxt;
248typedef struct cauthmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000249typedef struct identity Identity;
250typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100251
Damien Miller280ecfb2003-05-14 13:46:00 +1000252struct identity {
253 TAILQ_ENTRY(identity) next;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000254 int agent_fd; /* >=0 if agent supports key */
255 struct sshkey *key; /* public/private key */
Damien Miller280ecfb2003-05-14 13:46:00 +1000256 char *filename; /* comment for agent-only keys */
257 int tried;
258 int isprivate; /* key points to the private key */
Damien Miller5ceddc32013-02-15 12:18:32 +1100259 int userprovided;
Damien Miller280ecfb2003-05-14 13:46:00 +1000260};
261TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100262
djm@openbsd.org141efe42015-01-14 20:05:27 +0000263struct cauthctxt {
Damien Miller62cee002000-09-23 17:15:56 +1100264 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000265 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100266 const char *host;
267 const char *service;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000268 struct cauthmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000269 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000270 char *authlist;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000271 int attempt;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000272 /* pubkey */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000273 struct idlist keys;
274 int agent_fd;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000275 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000276 Sensitive *sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000277 char *oktypes, *ktypes;
278 const char *active_ktype;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000279 /* kbd-interactive */
280 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000281 /* generic */
282 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100283};
djm@openbsd.org141efe42015-01-14 20:05:27 +0000284
285struct cauthmethod {
Damien Miller62cee002000-09-23 17:15:56 +1100286 char *name; /* string to compare against server's list */
287 int (*userauth)(Authctxt *authctxt);
Damien Miller01ed2272008-11-05 16:20:46 +1100288 void (*cleanup)(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100289 int *enabled; /* flag in option struct that enables method */
290 int *batch_flag; /* flag in option struct that disables method */
291};
292
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000293int input_userauth_service_accept(int, u_int32_t, struct ssh *);
294int input_userauth_ext_info(int, u_int32_t, struct ssh *);
295int input_userauth_success(int, u_int32_t, struct ssh *);
296int input_userauth_success_unexpected(int, u_int32_t, struct ssh *);
297int input_userauth_failure(int, u_int32_t, struct ssh *);
298int input_userauth_banner(int, u_int32_t, struct ssh *);
299int input_userauth_error(int, u_int32_t, struct ssh *);
300int input_userauth_info_req(int, u_int32_t, struct ssh *);
301int input_userauth_pk_ok(int, u_int32_t, struct ssh *);
302int input_userauth_passwd_changereq(int, u_int32_t, struct ssh *);
Damien Miller874d77b2000-10-14 16:23:11 +1100303
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000304int userauth_none(Authctxt *);
305int userauth_pubkey(Authctxt *);
306int userauth_passwd(Authctxt *);
307int userauth_kbdint(Authctxt *);
308int userauth_hostbased(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100309
Darren Tucker0efd1552003-08-26 11:49:55 +1000310#ifdef GSSAPI
311int userauth_gssapi(Authctxt *authctxt);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000312int input_gssapi_response(int type, u_int32_t, struct ssh *);
313int input_gssapi_token(int type, u_int32_t, struct ssh *);
314int input_gssapi_hash(int type, u_int32_t, struct ssh *);
315int input_gssapi_error(int, u_int32_t, struct ssh *);
316int input_gssapi_errtok(int, u_int32_t, struct ssh *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000317#endif
318
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000319void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000320
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000321static int sign_and_send_pubkey(struct ssh *ssh, Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000322static void pubkey_prepare(Authctxt *);
323static void pubkey_cleanup(Authctxt *);
djm@openbsd.orgb9844a42016-12-04 23:54:02 +0000324static void pubkey_reset(Authctxt *);
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000325static struct sshkey *load_identity_file(Identity *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000326
Ben Lindstrombba81212001-06-25 05:01:22 +0000327static Authmethod *authmethod_get(char *authlist);
328static Authmethod *authmethod_lookup(const char *name);
329static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100330
331Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000332#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100333 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000334 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100335 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000336 &options.gss_authentication,
337 NULL},
338#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000339 {"hostbased",
340 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100341 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000342 &options.hostbased_authentication,
343 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000344 {"publickey",
345 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100346 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000347 &options.pubkey_authentication,
348 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100349 {"keyboard-interactive",
350 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100351 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100352 &options.kbd_interactive_authentication,
353 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000354 {"password",
355 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100356 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000357 &options.password_authentication,
358 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100359 {"none",
360 userauth_none,
361 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100362 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100363 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100364 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100365};
366
367void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000368ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000369 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100370{
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000371 struct ssh *ssh = active_state;
Damien Miller62cee002000-09-23 17:15:56 +1100372 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 Miller280ecfb2003-05-14 13:46:00 +1000382 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100383 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000384 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100385 authctxt.host = host;
386 authctxt.service = "ssh-connection"; /* service name */
387 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100388 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000389 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000390 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000391 authctxt.sensitive = sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000392 authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000393 authctxt.info_req_seen = 0;
djm@openbsd.orgf14564c2015-01-15 11:04:36 +0000394 authctxt.agent_fd = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100395 if (authctxt.method == NULL)
396 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100397
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000398 if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
399 (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
400 (r = sshpkt_send(ssh)) != 0)
401 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller62cee002000-09-23 17:15:56 +1100402
markus@openbsd.org94583be2017-05-30 14:19:15 +0000403 ssh->authctxt = &authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000404 ssh_dispatch_init(ssh, &input_userauth_error);
405 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
406 ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
markus@openbsd.org92e9fe62017-05-31 07:00:13 +0000407 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt.success); /* loop until success */
markus@openbsd.org94583be2017-05-30 14:19:15 +0000408 ssh->authctxt = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100409
Damien Miller280ecfb2003-05-14 13:46:00 +1000410 pubkey_cleanup(&authctxt);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000411 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
Damien Millerf842fcb2003-05-15 12:01:28 +1000412
dtucker@openbsd.orgf31c6542016-09-22 02:29:57 +0000413 if (!authctxt.success)
414 fatal("Authentication failed.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000415 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100416}
Damien Millerf842fcb2003-05-15 12:01:28 +1000417
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000418/* ARGSUSED */
419int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000420input_userauth_service_accept(int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000421{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000422 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000423 int r;
424
425 if (ssh_packet_remaining(ssh) > 0) {
426 char *reply;
427
428 if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
429 goto out;
430 debug2("service_accept: %s", reply);
431 free(reply);
432 } else {
433 debug2("buggy server: service_accept w/o service");
434 }
435 if ((r = sshpkt_get_end(ssh)) != 0)
436 goto out;
437 debug("SSH2_MSG_SERVICE_ACCEPT received");
438
439 /* initial userauth request */
440 userauth_none(authctxt);
441
442 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error);
443 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
444 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
445 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
446 r = 0;
447 out:
448 return r;
449}
450
451/* ARGSUSED */
452int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000453input_userauth_ext_info(int type, u_int32_t seqnr, struct ssh *ssh)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000454{
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000455 return kex_input_ext_info(type, seqnr, ssh);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000456}
457
Damien Miller62cee002000-09-23 17:15:56 +1100458void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000459userauth(Authctxt *authctxt, char *authlist)
460{
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000461 struct ssh *ssh = active_state; /* XXX */
462
Damien Miller01ed2272008-11-05 16:20:46 +1100463 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
464 authctxt->method->cleanup(authctxt);
465
Darren Tuckera627d422013-06-02 07:31:17 +1000466 free(authctxt->methoddata);
467 authctxt->methoddata = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000468 if (authlist == NULL) {
469 authlist = authctxt->authlist;
470 } else {
Darren Tuckera627d422013-06-02 07:31:17 +1000471 free(authctxt->authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000472 authctxt->authlist = authlist;
473 }
474 for (;;) {
475 Authmethod *method = authmethod_get(authlist);
476 if (method == NULL)
dtucker@openbsd.org4626e392017-06-14 00:31:38 +0000477 fatal("%s@%s: Permission denied (%s).",
478 authctxt->server_user, authctxt->host, authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000479 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000480
481 /* reset the per method handler */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000482 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_PER_METHOD_MIN,
Damien Millerf842fcb2003-05-15 12:01:28 +1000483 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
484
485 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000486 if (method->userauth(authctxt) != 0) {
487 debug2("we sent a %s packet, wait for reply", method->name);
488 break;
489 } else {
490 debug2("we did not send a packet, disable method");
491 method->enabled = NULL;
492 }
493 }
494}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000495
Damien Millerf7475d72008-11-03 19:26:18 +1100496/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000497int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000498input_userauth_error(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100499{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000500 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000501 "type %d", type);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000502 return 0;
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000503}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000504
Damien Millerf7475d72008-11-03 19:26:18 +1100505/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000506int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000507input_userauth_banner(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000508{
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000509 char *msg, *lang;
Damien Miller7ba0ca72008-07-17 18:57:06 +1000510 u_int len;
Darren Tucker79644822003-10-08 17:37:58 +1000511
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000512 debug3("%s", __func__);
513 msg = packet_get_string(&len);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000514 lang = packet_get_string(NULL);
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000515 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO)
516 fmprintf(stderr, "%s", msg);
517 free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000518 free(lang);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000519 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100520}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000521
Damien Millerf7475d72008-11-03 19:26:18 +1100522/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000523int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000524input_userauth_success(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100525{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000526 Authctxt *authctxt = ssh->authctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100527
Damien Miller62cee002000-09-23 17:15:56 +1100528 if (authctxt == NULL)
529 fatal("input_userauth_success: no authentication context");
Darren Tuckera627d422013-06-02 07:31:17 +1000530 free(authctxt->authlist);
531 authctxt->authlist = NULL;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100532 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
533 authctxt->method->cleanup(authctxt);
Darren Tuckera627d422013-06-02 07:31:17 +1000534 free(authctxt->methoddata);
535 authctxt->methoddata = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100536 authctxt->success = 1; /* break out */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000537 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100538}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000539
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000540int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000541input_userauth_success_unexpected(int type, u_int32_t seq, struct ssh *ssh)
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100542{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000543 Authctxt *authctxt = ssh->authctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100544
545 if (authctxt == NULL)
546 fatal("%s: no authentication context", __func__);
547
548 fatal("Unexpected authentication success during %s.",
549 authctxt->method->name);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000550 return 0;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100551}
552
Damien Millerf7475d72008-11-03 19:26:18 +1100553/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000554int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000555input_userauth_failure(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100556{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000557 Authctxt *authctxt = ssh->authctxt;
Damien Miller62cee002000-09-23 17:15:56 +1100558 char *authlist = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000559 u_char partial;
560 int r;
Damien Miller62cee002000-09-23 17:15:56 +1100561
562 if (authctxt == NULL)
563 fatal("input_userauth_failure: no authentication context");
564
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000565 if ((r = sshpkt_get_cstring(ssh, &authlist, NULL)) != 0 ||
566 (r = sshpkt_get_u8(ssh, &partial)) != 0 ||
567 (r = sshpkt_get_end(ssh)) != 0)
568 goto out;
Damien Miller62cee002000-09-23 17:15:56 +1100569
Damien Miller62e9c4f2013-04-23 15:15:49 +1000570 if (partial != 0) {
dtucker@openbsd.org83cbca62016-07-22 05:46:11 +0000571 verbose("Authenticated with partial success.");
Damien Miller62e9c4f2013-04-23 15:15:49 +1000572 /* reset state */
djm@openbsd.orgb9844a42016-12-04 23:54:02 +0000573 pubkey_reset(authctxt);
Damien Miller62e9c4f2013-04-23 15:15:49 +1000574 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000575 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100576
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000577 userauth(authctxt, authlist);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000578 authlist = NULL;
579 out:
580 free(authlist);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000581 return 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000582}
Damien Millerf7475d72008-11-03 19:26:18 +1100583
584/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000585int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000586input_userauth_pk_ok(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000587{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000588 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000589 struct sshkey *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000590 Identity *id = NULL;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000591 int pktype, sent = 0;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000592 size_t blen;
593 char *pkalg = NULL, *fp;
594 u_char *pkblob = NULL;
595 int r;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000596
597 if (authctxt == NULL)
598 fatal("input_userauth_pk_ok: no authentication context");
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000599
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000600 if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
601 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
602 (r = sshpkt_get_end(ssh)) != 0)
603 goto done;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000604
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000605 debug("Server accepts key: pkalg %s blen %zu", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000606
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000607 if ((pktype = sshkey_type_from_name(pkalg)) == KEY_UNSPEC) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000608 debug("unknown pkalg %s", pkalg);
609 goto done;
610 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000611 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
612 debug("no key from blob. pkalg %s: %s", pkalg, ssh_err(r));
Damien Miller280ecfb2003-05-14 13:46:00 +1000613 goto done;
614 }
615 if (key->type != pktype) {
616 error("input_userauth_pk_ok: type mismatch "
617 "for decoded key (received %d, expected %d)",
618 key->type, pktype);
619 goto done;
620 }
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000621 if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
622 SSH_FP_DEFAULT)) == NULL)
623 goto done;
Damien Miller280ecfb2003-05-14 13:46:00 +1000624 debug2("input_userauth_pk_ok: fp %s", fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000625 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000626
Darren Tuckerd05b6012003-10-15 15:55:59 +1000627 /*
628 * search keys in the reverse order, because last candidate has been
629 * moved to the end of the queue. this also avoids confusion by
630 * duplicate keys
631 */
Damien Miller0b51a522004-04-20 20:07:19 +1000632 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000633 if (sshkey_equal(key, id->key)) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000634 sent = sign_and_send_pubkey(ssh, authctxt, id);
Damien Miller280ecfb2003-05-14 13:46:00 +1000635 break;
636 }
637 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000638 r = 0;
639 done:
640 sshkey_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000641 free(pkalg);
642 free(pkblob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000643
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000644 /* try another method if we did not send a packet */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000645 if (r == 0 && sent == 0)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000646 userauth(authctxt, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000647 return r;
Damien Miller62cee002000-09-23 17:15:56 +1100648}
649
Darren Tucker0efd1552003-08-26 11:49:55 +1000650#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100651int
Darren Tucker0efd1552003-08-26 11:49:55 +1000652userauth_gssapi(Authctxt *authctxt)
653{
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000654 struct ssh *ssh = active_state; /* XXX */
Darren Tucker0efd1552003-08-26 11:49:55 +1000655 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100656 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000657 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000658 OM_uint32 min;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000659 int r, ok = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000660
661 /* Try one GSSAPI method at a time, rather than sending them all at
662 * once. */
663
Darren Tucker56afe142003-11-03 20:06:14 +1100664 if (gss_supported == NULL)
665 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000666
667 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100668 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000669 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100670 if (gss_supported->elements[mech].length < 128 &&
djm@openbsd.org84623e02018-06-26 02:02:36 +0000671 ssh_gssapi_check_mechanism(&gssctxt,
Damien Millera1cb9f32006-08-19 00:33:34 +1000672 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000673 ok = 1; /* Mechanism works */
674 } else {
675 mech++;
676 }
677 }
678
Damien Millera1cb9f32006-08-19 00:33:34 +1000679 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000680 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000681
682 authctxt->methoddata=(void *)gssctxt;
683
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000684 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
685 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
686 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
687 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
688 (r = sshpkt_put_u32(ssh, 1)) != 0 ||
689 (r = sshpkt_put_u32(ssh,
690 (gss_supported->elements[mech].length) + 2)) != 0 ||
691 (r = sshpkt_put_u8(ssh, SSH_GSS_OIDTYPE)) != 0 ||
692 (r = sshpkt_put_u8(ssh,
693 gss_supported->elements[mech].length)) != 0 ||
694 (r = sshpkt_put(ssh,
695 gss_supported->elements[mech].elements,
696 gss_supported->elements[mech].length)) != 0 ||
697 (r = sshpkt_send(ssh)) != 0)
698 fatal("%s: %s", __func__, ssh_err(r));
Darren Tucker0efd1552003-08-26 11:49:55 +1000699
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000700 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
701 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
702 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
703 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000704
705 mech++; /* Move along to next candidate */
706
707 return 1;
708}
709
Damien Miller91c6aa42003-11-17 21:20:18 +1100710static OM_uint32
djm@openbsd.org39896b72017-05-31 05:08:46 +0000711process_gssapi_token(struct ssh *ssh, gss_buffer_t recv_tok)
Damien Miller91c6aa42003-11-17 21:20:18 +1100712{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000713 Authctxt *authctxt = ssh->authctxt;
Damien Miller91c6aa42003-11-17 21:20:18 +1100714 Gssctxt *gssctxt = authctxt->methoddata;
715 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000716 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
717 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100718 OM_uint32 status, ms, flags;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000719 int r;
Damien Miller787b2ec2003-11-21 23:56:47 +1100720
Damien Miller91c6aa42003-11-17 21:20:18 +1100721 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100722 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100723
724 if (send_tok.length > 0) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000725 u_char type = GSS_ERROR(status) ?
726 SSH2_MSG_USERAUTH_GSSAPI_ERRTOK :
727 SSH2_MSG_USERAUTH_GSSAPI_TOKEN;
Damien Miller787b2ec2003-11-21 23:56:47 +1100728
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000729 if ((r = sshpkt_start(ssh, type)) != 0 ||
730 (r = sshpkt_put_string(ssh, send_tok.value,
731 send_tok.length)) != 0 ||
732 (r = sshpkt_send(ssh)) != 0)
733 fatal("%s: %s", __func__, ssh_err(r));
734
Damien Miller91c6aa42003-11-17 21:20:18 +1100735 gss_release_buffer(&ms, &send_tok);
736 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100737
Damien Miller91c6aa42003-11-17 21:20:18 +1100738 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100739 /* send either complete or MIC, depending on mechanism */
740 if (!(flags & GSS_C_INTEG_FLAG)) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000741 if ((r = sshpkt_start(ssh,
742 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE)) != 0 ||
743 (r = sshpkt_send(ssh)) != 0)
744 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0425d402003-11-17 22:18:21 +1100745 } else {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000746 struct sshbuf *b;
747
748 if ((b = sshbuf_new()) == NULL)
749 fatal("%s: sshbuf_new failed", __func__);
750 ssh_gssapi_buildmic(b, authctxt->server_user,
Damien Miller0425d402003-11-17 22:18:21 +1100751 authctxt->service, "gssapi-with-mic");
752
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000753 if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
754 fatal("%s: sshbuf_mutable_ptr failed", __func__);
755 gssbuf.length = sshbuf_len(b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100756
Damien Miller0425d402003-11-17 22:18:21 +1100757 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100758
Damien Miller0425d402003-11-17 22:18:21 +1100759 if (!GSS_ERROR(status)) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000760 if ((r = sshpkt_start(ssh,
761 SSH2_MSG_USERAUTH_GSSAPI_MIC)) != 0 ||
762 (r = sshpkt_put_string(ssh, mic.value,
763 mic.length)) != 0 ||
764 (r = sshpkt_send(ssh)) != 0)
765 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller0425d402003-11-17 22:18:21 +1100766 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100767
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000768 sshbuf_free(b);
Damien Miller0425d402003-11-17 22:18:21 +1100769 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100770 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100771 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100772
Damien Miller91c6aa42003-11-17 21:20:18 +1100773 return status;
774}
775
Damien Millerf7475d72008-11-03 19:26:18 +1100776/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000777int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000778input_gssapi_response(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000779{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000780 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000781 Gssctxt *gssctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000782 size_t oidlen;
783 u_char *oidv = NULL;
784 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000785
786 if (authctxt == NULL)
787 fatal("input_gssapi_response: no authentication context");
788 gssctxt = authctxt->methoddata;
789
790 /* Setup our OID */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000791 if ((r = sshpkt_get_string(ssh, &oidv, &oidlen)) != 0)
792 goto done;
Darren Tucker0efd1552003-08-26 11:49:55 +1000793
Darren Tucker655a5e02003-11-03 20:09:03 +1100794 if (oidlen <= 2 ||
795 oidv[0] != SSH_GSS_OIDTYPE ||
796 oidv[1] != oidlen - 2) {
797 debug("Badly encoded mechanism OID received");
798 userauth(authctxt, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000799 goto ok;
Darren Tucker0efd1552003-08-26 11:49:55 +1000800 }
801
Darren Tucker655a5e02003-11-03 20:09:03 +1100802 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
803 fatal("Server returned different OID than expected");
804
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000805 if ((r = sshpkt_get_end(ssh)) != 0)
806 goto done;
Darren Tucker0efd1552003-08-26 11:49:55 +1000807
djm@openbsd.org39896b72017-05-31 05:08:46 +0000808 if (GSS_ERROR(process_gssapi_token(ssh, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000809 /* Start again with next method on list */
810 debug("Trying to start again");
811 userauth(authctxt, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000812 goto ok;
Darren Tucker0efd1552003-08-26 11:49:55 +1000813 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000814 ok:
815 r = 0;
816 done:
817 free(oidv);
818 return r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000819}
820
Damien Millerf7475d72008-11-03 19:26:18 +1100821/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000822int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000823input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000824{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000825 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000826 gss_buffer_desc recv_tok;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000827 u_char *p = NULL;
828 size_t len;
Damien Miller91c6aa42003-11-17 21:20:18 +1100829 OM_uint32 status;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000830 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000831
832 if (authctxt == NULL)
833 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000834
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000835 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
836 (r = sshpkt_get_end(ssh)) != 0)
837 goto out;
Darren Tucker0efd1552003-08-26 11:49:55 +1000838
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000839 recv_tok.value = p;
840 recv_tok.length = len;
djm@openbsd.org39896b72017-05-31 05:08:46 +0000841 status = process_gssapi_token(ssh, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000842
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000843 /* Start again with the next method in the list */
Darren Tucker0efd1552003-08-26 11:49:55 +1000844 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000845 userauth(authctxt, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000846 /* ok */
Darren Tucker0efd1552003-08-26 11:49:55 +1000847 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000848 r = 0;
849 out:
850 free(p);
851 return r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000852}
853
Damien Millerf7475d72008-11-03 19:26:18 +1100854/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000855int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000856input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000857{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000858 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000859 Gssctxt *gssctxt;
860 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
861 gss_buffer_desc recv_tok;
Damien Millerd677ad12013-04-23 15:18:51 +1000862 OM_uint32 ms;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000863 u_char *p = NULL;
864 size_t len;
865 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000866
867 if (authctxt == NULL)
868 fatal("input_gssapi_response: no authentication context");
869 gssctxt = authctxt->methoddata;
870
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000871 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
872 (r = sshpkt_get_end(ssh)) != 0) {
873 free(p);
874 return r;
875 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000876
877 /* Stick it into GSSAPI and see what it says */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000878 recv_tok.value = p;
879 recv_tok.length = len;
Damien Millerd677ad12013-04-23 15:18:51 +1000880 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000881 &recv_tok, &send_tok, NULL);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000882 free(p);
Darren Tucker0efd1552003-08-26 11:49:55 +1000883 gss_release_buffer(&ms, &send_tok);
884
885 /* Server will be returning a failed packet after this one */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000886 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000887}
888
Damien Millerf7475d72008-11-03 19:26:18 +1100889/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000890int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000891input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000892{
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000893 char *msg = NULL;
894 char *lang = NULL;
895 int r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000896
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000897 if ((r = sshpkt_get_u32(ssh, NULL)) != 0 || /* maj */
898 (r = sshpkt_get_u32(ssh, NULL)) != 0 || /* min */
899 (r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
900 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
901 goto out;
902 r = sshpkt_get_end(ssh);
Damien Miller15d72a02005-11-05 15:07:33 +1100903 debug("Server GSSAPI Error:\n%s", msg);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000904 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000905 free(msg);
906 free(lang);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000907 return r;
Darren Tucker0efd1552003-08-26 11:49:55 +1000908}
909#endif /* GSSAPI */
910
Damien Millereba71ba2000-04-29 23:57:08 +1000911int
Damien Miller874d77b2000-10-14 16:23:11 +1100912userauth_none(Authctxt *authctxt)
913{
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000914 struct ssh *ssh = active_state; /* XXX */
915 int r;
916
Damien Miller874d77b2000-10-14 16:23:11 +1100917 /* initial userauth request */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000918 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
919 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
920 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
921 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
922 (r = sshpkt_send(ssh)) != 0)
923 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +1100924 return 1;
925}
926
927int
Damien Miller62cee002000-09-23 17:15:56 +1100928userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000929{
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000930 struct ssh *ssh = active_state; /* XXX */
Damien Millere247cc42000-05-07 12:03:14 +1000931 static int attempt = 0;
dtucker@openbsd.orga54eb272017-08-27 00:38:41 +0000932 char prompt[256];
Damien Millereba71ba2000-04-29 23:57:08 +1000933 char *password;
Darren Tuckerab791692010-01-08 18:48:02 +1100934 const char *host = options.host_key_alias ? options.host_key_alias :
935 authctxt->host;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000936 int r;
Damien Millereba71ba2000-04-29 23:57:08 +1000937
Damien Millerd3a18572000-06-07 19:55:44 +1000938 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000939 return 0;
940
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000941 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000942 error("Permission denied, please try again.");
943
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000944 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100945 authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +1000946 password = read_passphrase(prompt, 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000947 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
948 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
949 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
950 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
951 (r = sshpkt_put_u8(ssh, 0)) != 0 ||
952 (r = sshpkt_put_cstring(ssh, password)) != 0 ||
953 (r = sshpkt_add_padding(ssh, 64)) != 0 ||
954 (r = sshpkt_send(ssh)) != 0)
955 fatal("%s: %s", __func__, ssh_err(r));
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000956
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000957 if (password)
958 freezero(password, strlen(password));
959
960 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000961 &input_userauth_passwd_changereq);
962
Damien Millereba71ba2000-04-29 23:57:08 +1000963 return 1;
964}
Damien Millerf7475d72008-11-03 19:26:18 +1100965
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000966/*
967 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
968 */
Damien Millerf7475d72008-11-03 19:26:18 +1100969/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000970int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000971input_userauth_passwd_changereq(int type, u_int32_t seqnr, struct ssh *ssh)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000972{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000973 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000974 char *info = NULL, *lang = NULL, *password = NULL, *retype = NULL;
dtucker@openbsd.orga54eb272017-08-27 00:38:41 +0000975 char prompt[256];
djm@openbsd.org3e032a92017-01-30 00:32:03 +0000976 const char *host;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000977 int r;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000978
979 debug2("input_userauth_passwd_changereq");
980
981 if (authctxt == NULL)
982 fatal("input_userauth_passwd_changereq: "
983 "no authentication context");
djm@openbsd.org3e032a92017-01-30 00:32:03 +0000984 host = options.host_key_alias ? options.host_key_alias : authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000985
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000986 if ((r = sshpkt_get_cstring(ssh, &info, NULL)) != 0 ||
987 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
988 goto out;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000989 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000990 logit("%s", info);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +0000991 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
992 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
993 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
994 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
995 (r = sshpkt_put_u8(ssh, 1)) != 0) /* additional info */
996 goto out;
997
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000998 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000999 "Enter %.30s@%.128s's old password: ",
Darren Tuckerab791692010-01-08 18:48:02 +11001000 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001001 password = read_passphrase(prompt, 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001002 if ((r = sshpkt_put_cstring(ssh, password)) != 0)
1003 goto out;
1004
1005 freezero(password, strlen(password));
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001006 password = NULL;
1007 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001008 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001009 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +11001010 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001011 password = read_passphrase(prompt, RP_ALLOW_EOF);
1012 if (password == NULL) {
1013 /* bail out */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001014 r = 0;
1015 goto out;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001016 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001017 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001018 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +11001019 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001020 retype = read_passphrase(prompt, 0);
1021 if (strcmp(password, retype) != 0) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001022 freezero(password, strlen(password));
Damien Miller996acd22003-04-09 20:59:48 +10001023 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001024 password = NULL;
1025 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001026 freezero(retype, strlen(retype));
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001027 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001028 if ((r = sshpkt_put_cstring(ssh, password)) != 0 ||
1029 (r = sshpkt_add_padding(ssh, 64)) != 0 ||
1030 (r = sshpkt_send(ssh)) != 0)
1031 goto out;
Ben Lindstromcb72e4f2002-06-21 00:41:51 +00001032
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001033 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001034 &input_userauth_passwd_changereq);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001035 r = 0;
1036 out:
1037 if (password)
1038 freezero(password, strlen(password));
1039 free(info);
1040 free(lang);
1041 return r;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001042}
Damien Millereba71ba2000-04-29 23:57:08 +10001043
djm@openbsd.org26074382018-03-03 03:01:50 +00001044/*
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001045 * Select an algorithm for publickey signatures.
1046 * Returns algorithm (caller must free) or NULL if no mutual algorithm found.
1047 *
1048 * Call with ssh==NULL to ignore server-sig-algs extension list and
1049 * only attempt with the key's base signature type.
djm@openbsd.org26074382018-03-03 03:01:50 +00001050 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001051static char *
1052key_sig_algorithm(struct ssh *ssh, const struct sshkey *key)
djm@openbsd.org26074382018-03-03 03:01:50 +00001053{
djm@openbsd.orgb4d4eda2018-07-03 13:20:25 +00001054 char *allowed, *oallowed, *cp, *tmp, *alg = NULL;
djm@openbsd.org26074382018-03-03 03:01:50 +00001055
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001056 /*
1057 * The signature algorithm will only differ from the key algorithm
1058 * for RSA keys/certs and when the server advertises support for
1059 * newer (SHA2) algorithms.
1060 */
1061 if (ssh == NULL || ssh->kex->server_sig_algs == NULL ||
1062 (key->type != KEY_RSA && key->type != KEY_RSA_CERT)) {
1063 /* Filter base key signature alg against our configuration */
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001064 return match_list(sshkey_ssh_name(key),
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001065 options.pubkey_key_types, NULL);
djm@openbsd.org26074382018-03-03 03:01:50 +00001066 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001067
1068 /*
1069 * For RSA keys/certs, since these might have a different sig type:
1070 * find the first entry in PubkeyAcceptedKeyTypes of the right type
1071 * that also appears in the supported signature algorithms list from
1072 * the server.
1073 */
1074 oallowed = allowed = xstrdup(options.pubkey_key_types);
1075 while ((cp = strsep(&allowed, ",")) != NULL) {
1076 if (sshkey_type_from_name(cp) != key->type)
1077 continue;
djm@openbsd.orgb4d4eda2018-07-03 13:20:25 +00001078 tmp = match_list(sshkey_sigalg_by_name(cp), ssh->kex->server_sig_algs, NULL);
1079 if (tmp != NULL)
1080 alg = xstrdup(cp);
1081 free(tmp);
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001082 if (alg != NULL)
1083 break;
1084 }
1085 free(oallowed);
1086 return alg;
djm@openbsd.org26074382018-03-03 03:01:50 +00001087}
1088
Ben Lindstrombba81212001-06-25 05:01:22 +00001089static int
djm@openbsd.org141efe42015-01-14 20:05:27 +00001090identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001091 const u_char *data, size_t datalen, u_int compat, const char *alg)
Damien Miller280ecfb2003-05-14 13:46:00 +10001092{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001093 struct sshkey *prv;
djm@openbsd.org26074382018-03-03 03:01:50 +00001094 int r;
Damien Miller280ecfb2003-05-14 13:46:00 +10001095
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001096 /* The agent supports this key. */
djm@openbsd.org26074382018-03-03 03:01:50 +00001097 if (id->key != NULL && id->agent_fd != -1) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001098 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
1099 data, datalen, alg, compat);
1100 }
1101
1102 /*
1103 * We have already loaded the private key or the private key is
1104 * stored in external hardware.
1105 */
1106 if (id->key != NULL &&
1107 (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))) {
1108 if ((r = sshkey_sign(id->key, sigp, lenp, data, datalen,
1109 alg, compat)) != 0)
1110 return r;
1111 /*
1112 * PKCS#11 tokens may not support all signature algorithms,
1113 * so check what we get back.
1114 */
1115 if ((r = sshkey_check_sigtype(*sigp, *lenp, alg)) != 0)
djm@openbsd.org26074382018-03-03 03:01:50 +00001116 return r;
1117 return 0;
1118 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001119
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001120 /* Load the private key from the file. */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001121 if ((prv = load_identity_file(id)) == NULL)
djm@openbsd.org0d1451a2016-02-23 01:34:14 +00001122 return SSH_ERR_KEY_NOT_FOUND;
djm@openbsd.orgc4972d02017-08-11 04:47:12 +00001123 if (id->key != NULL && !sshkey_equal_public(prv, id->key)) {
1124 error("%s: private key %s contents do not match public",
1125 __func__, id->filename);
1126 return SSH_ERR_KEY_NOT_FOUND;
1127 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001128 r = sshkey_sign(prv, sigp, lenp, data, datalen, alg, compat);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001129 sshkey_free(prv);
djm@openbsd.org26074382018-03-03 03:01:50 +00001130 return r;
Damien Miller280ecfb2003-05-14 13:46:00 +10001131}
1132
1133static int
djm@openbsd.org1e245522017-03-11 23:40:26 +00001134id_filename_matches(Identity *id, Identity *private_id)
1135{
1136 const char *suffixes[] = { ".pub", "-cert.pub", NULL };
1137 size_t len = strlen(id->filename), plen = strlen(private_id->filename);
1138 size_t i, slen;
1139
1140 if (strcmp(id->filename, private_id->filename) == 0)
1141 return 1;
1142 for (i = 0; suffixes[i]; i++) {
1143 slen = strlen(suffixes[i]);
1144 if (len > slen && plen == len - slen &&
1145 strcmp(id->filename + (len - slen), suffixes[i]) == 0 &&
1146 memcmp(id->filename, private_id->filename, plen) == 0)
1147 return 1;
1148 }
1149 return 0;
1150}
1151
1152static int
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001153sign_and_send_pubkey(struct ssh *ssh, Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +10001154{
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001155 struct sshbuf *b = NULL;
1156 Identity *private_id, *sign_id = NULL;
1157 u_char *signature = NULL;
1158 size_t slen = 0, skip = 0;
1159 int r, fallback_sigtype, sent = 0;
1160 char *alg = NULL, *fp = NULL;
1161 const char *loc = "";
Damien Millereba71ba2000-04-29 23:57:08 +10001162
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001163 if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1164 SSH_FP_DEFAULT)) == NULL)
1165 return 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001166
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001167 debug3("%s: %s %s", __func__, sshkey_type(id->key), fp);
Damien Millereba71ba2000-04-29 23:57:08 +10001168
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001169 /*
1170 * If the key is an certificate, try to find a matching private key
1171 * and use it to complete the signature.
djm@openbsd.org9ee692f2016-04-28 14:30:21 +00001172 * If no such private key exists, fall back to trying the certificate
1173 * key itself in case it has a private half already loaded.
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001174 * This will try to set sign_id to the private key that will perform
1175 * the signature.
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001176 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001177 if (sshkey_is_cert(id->key)) {
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001178 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1179 if (sshkey_equal_public(id->key, private_id->key) &&
1180 id->key->type != private_id->key->type) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001181 sign_id = private_id;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001182 break;
1183 }
1184 }
djm@openbsd.org1e245522017-03-11 23:40:26 +00001185 /*
1186 * Exact key matches are preferred, but also allow
1187 * filename matches for non-PKCS#11/agent keys that
1188 * didn't load public keys. This supports the case
1189 * of keeping just a private key file and public
1190 * certificate on disk.
1191 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001192 if (sign_id == NULL &&
1193 !id->isprivate && id->agent_fd == -1 &&
djm@openbsd.org1e245522017-03-11 23:40:26 +00001194 (id->key->flags & SSHKEY_FLAG_EXT) == 0) {
1195 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1196 if (private_id->key == NULL &&
1197 id_filename_matches(id, private_id)) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001198 sign_id = private_id;
djm@openbsd.org1e245522017-03-11 23:40:26 +00001199 break;
1200 }
1201 }
1202 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001203 if (sign_id != NULL) {
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001204 debug2("%s: using private key \"%s\"%s for "
1205 "certificate", __func__, id->filename,
1206 id->agent_fd != -1 ? " from agent" : "");
1207 } else {
djm@openbsd.orgc38905b2016-03-14 16:20:54 +00001208 debug("%s: no separate private key for certificate "
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001209 "\"%s\"", __func__, id->filename);
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001210 }
1211 }
1212
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001213 /*
1214 * If the above didn't select another identity to do the signing
1215 * then default to the one we started with.
1216 */
1217 if (sign_id == NULL)
1218 sign_id = id;
1219
1220 /* assemble and sign data */
1221 for (fallback_sigtype = 0; fallback_sigtype <= 1; fallback_sigtype++) {
1222 free(alg);
1223 slen = 0;
1224 signature = NULL;
1225 if ((alg = key_sig_algorithm(fallback_sigtype ? NULL : ssh,
1226 id->key)) == NULL) {
1227 error("%s: no mutual signature supported", __func__);
1228 goto out;
1229 }
1230 debug3("%s: signing using %s", __func__, alg);
1231
1232 sshbuf_free(b);
1233 if ((b = sshbuf_new()) == NULL)
1234 fatal("%s: sshbuf_new failed", __func__);
1235 if (datafellows & SSH_OLD_SESSIONID) {
1236 if ((r = sshbuf_put(b, session_id2,
1237 session_id2_len)) != 0) {
1238 fatal("%s: sshbuf_put: %s",
1239 __func__, ssh_err(r));
1240 }
1241 } else {
1242 if ((r = sshbuf_put_string(b, session_id2,
1243 session_id2_len)) != 0) {
1244 fatal("%s: sshbuf_put_string: %s",
1245 __func__, ssh_err(r));
1246 }
1247 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001248 skip = sshbuf_len(b);
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001249 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1250 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1251 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1252 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1253 (r = sshbuf_put_u8(b, 1)) != 0 ||
1254 (r = sshbuf_put_cstring(b, alg)) != 0 ||
1255 (r = sshkey_puts(id->key, b)) != 0) {
1256 fatal("%s: assemble signed data: %s",
1257 __func__, ssh_err(r));
1258 }
1259
1260 /* generate signature */
1261 r = identity_sign(sign_id, &signature, &slen,
1262 sshbuf_ptr(b), sshbuf_len(b), datafellows, alg);
1263 if (r == 0)
1264 break;
1265 else if (r == SSH_ERR_KEY_NOT_FOUND)
1266 goto out; /* soft failure */
1267 else if (r == SSH_ERR_SIGN_ALG_UNSUPPORTED &&
1268 !fallback_sigtype) {
1269 if (sign_id->agent_fd != -1)
1270 loc = "agent ";
1271 else if ((sign_id->key->flags & SSHKEY_FLAG_EXT) != 0)
1272 loc = "token ";
1273 logit("%skey %s %s returned incorrect signature type",
1274 loc, sshkey_type(id->key), fp);
1275 continue;
1276 }
1277 error("%s: signing failed: %s", __func__, ssh_err(r));
1278 goto out;
Damien Millerad833b32000-08-23 10:46:23 +10001279 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001280 if (slen == 0 || signature == NULL) /* shouldn't happen */
1281 fatal("%s: no signature", __func__);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001282
Damien Millereba71ba2000-04-29 23:57:08 +10001283 /* append signature */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001284 if ((r = sshbuf_put_string(b, signature, slen)) != 0)
1285 fatal("%s: append signature: %s", __func__, ssh_err(r));
Damien Millereba71ba2000-04-29 23:57:08 +10001286
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001287#ifdef DEBUG_PK
1288 sshbuf_dump(b, stderr);
1289#endif
Damien Millereba71ba2000-04-29 23:57:08 +10001290 /* skip session id and packet type */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001291 if ((r = sshbuf_consume(b, skip + 1)) != 0)
1292 fatal("%s: consume: %s", __func__, ssh_err(r));
Damien Millereba71ba2000-04-29 23:57:08 +10001293
1294 /* put remaining data from buffer into packet */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001295 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1296 (r = sshpkt_putb(ssh, b)) != 0 ||
1297 (r = sshpkt_send(ssh)) != 0)
1298 fatal("%s: enqueue request: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001299
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001300 /* success */
1301 sent = 1;
1302
1303 out:
1304 free(fp);
1305 free(alg);
1306 sshbuf_free(b);
1307 freezero(signature, slen);
1308 return sent;
Damien Miller994cf142000-07-21 10:19:44 +10001309}
1310
Ben Lindstrombba81212001-06-25 05:01:22 +00001311static int
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001312send_pubkey_test(struct ssh *ssh, Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001313{
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001314 u_char *blob = NULL;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001315 char *alg = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001316 size_t bloblen;
1317 u_int have_sig = 0;
1318 int sent = 0, r;
Damien Miller994cf142000-07-21 10:19:44 +10001319
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001320 if ((alg = key_sig_algorithm(ssh, id->key)) == NULL) {
1321 debug("%s: no mutual signature algorithm", __func__);
1322 goto out;
1323 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001324
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001325 if ((r = sshkey_to_blob(id->key, &blob, &bloblen)) != 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001326 /* we cannot handle this key */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001327 debug3("%s: cannot handle key", __func__);
1328 goto out;
Damien Miller994cf142000-07-21 10:19:44 +10001329 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001330 /* register callback for USERAUTH_PK_OK message */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001331 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001332
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001333 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1334 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1335 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1336 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1337 (r = sshpkt_put_u8(ssh, have_sig)) != 0 ||
1338 (r = sshpkt_put_cstring(ssh, alg)) != 0 ||
1339 (r = sshpkt_put_string(ssh, blob, bloblen)) != 0 ||
1340 (r = sshpkt_send(ssh)) != 0)
1341 fatal("%s: %s", __func__, ssh_err(r));
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001342 sent = 1;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001343
1344 out:
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001345 free(alg);
1346 free(blob);
1347 return sent;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001348}
1349
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001350static struct sshkey *
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001351load_identity_file(Identity *id)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001352{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001353 struct sshkey *private = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001354 char prompt[300], *passphrase, *comment;
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001355 int r, perm_ok = 0, quit = 0, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001356 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001357
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001358 if (stat(id->filename, &st) < 0) {
1359 (id->userprovided ? logit : debug3)("no such identity: %s: %s",
1360 id->filename, strerror(errno));
Ben Lindstrom329782e2001-03-10 17:08:59 +00001361 return NULL;
1362 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001363 snprintf(prompt, sizeof prompt,
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001364 "Enter passphrase for key '%.100s': ", id->filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001365 for (i = 0; i <= options.number_of_password_prompts; i++) {
1366 if (i == 0)
1367 passphrase = "";
1368 else {
Damien Miller62cee002000-09-23 17:15:56 +11001369 passphrase = read_passphrase(prompt, 0);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001370 if (*passphrase == '\0') {
Damien Miller62cee002000-09-23 17:15:56 +11001371 debug2("no passphrase given, try next key");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001372 free(passphrase);
1373 break;
Damien Miller62cee002000-09-23 17:15:56 +11001374 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001375 }
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001376 switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
1377 passphrase, &private, &comment, &perm_ok))) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001378 case 0:
1379 break;
1380 case SSH_ERR_KEY_WRONG_PASSPHRASE:
1381 if (options.batch_mode) {
1382 quit = 1;
1383 break;
1384 }
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001385 if (i != 0)
1386 debug2("bad passphrase given, try again...");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001387 break;
1388 case SSH_ERR_SYSTEM_ERROR:
1389 if (errno == ENOENT) {
1390 debug2("Load key \"%s\": %s",
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001391 id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001392 quit = 1;
1393 break;
1394 }
1395 /* FALLTHROUGH */
1396 default:
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001397 error("Load key \"%s\": %s", id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001398 quit = 1;
1399 break;
1400 }
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001401 if (!quit && private != NULL && id->agent_fd == -1 &&
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001402 !(id->key && id->isprivate))
1403 maybe_add_key_to_agent(id->filename, private, comment,
1404 passphrase);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001405 if (i > 0)
1406 freezero(passphrase, strlen(passphrase));
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001407 free(comment);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001408 if (private != NULL || quit)
1409 break;
Damien Miller994cf142000-07-21 10:19:44 +10001410 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001411 return private;
1412}
1413
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001414static int
1415key_type_allowed_by_config(struct sshkey *key)
1416{
1417 if (match_pattern_list(sshkey_ssh_name(key),
1418 options.pubkey_key_types, 0) == 1)
1419 return 1;
1420
1421 /* RSA keys/certs might be allowed by alternate signature types */
1422 switch (key->type) {
1423 case KEY_RSA:
1424 if (match_pattern_list("rsa-sha2-512",
1425 options.pubkey_key_types, 0) == 1)
1426 return 1;
1427 if (match_pattern_list("rsa-sha2-256",
1428 options.pubkey_key_types, 0) == 1)
1429 return 1;
1430 break;
1431 case KEY_RSA_CERT:
1432 if (match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
1433 options.pubkey_key_types, 0) == 1)
1434 return 1;
1435 if (match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
1436 options.pubkey_key_types, 0) == 1)
1437 return 1;
1438 break;
1439 }
1440 return 0;
1441}
1442
1443
Damien Miller280ecfb2003-05-14 13:46:00 +10001444/*
1445 * try keys in the following order:
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001446 * 1. certificates listed in the config file
1447 * 2. other input certificates
1448 * 3. agent keys that are found in the config file
1449 * 4. other agent keys
1450 * 5. keys that are only listed in the config file
Damien Miller280ecfb2003-05-14 13:46:00 +10001451 */
1452static void
1453pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001454{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001455 struct identity *id, *id2, *tmp;
1456 struct idlist agent, files, *preferred;
1457 struct sshkey *key;
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001458 int agent_fd = -1, i, r, found;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001459 size_t j;
1460 struct ssh_identitylist *idlist;
Damien Millerad833b32000-08-23 10:46:23 +10001461
Damien Miller280ecfb2003-05-14 13:46:00 +10001462 TAILQ_INIT(&agent); /* keys from the agent */
1463 TAILQ_INIT(&files); /* keys from the config file */
1464 preferred = &authctxt->keys;
1465 TAILQ_INIT(preferred); /* preferred order of keys */
1466
Damien Millercb6b68b2012-12-03 09:49:52 +11001467 /* list of keys stored in the filesystem and PKCS#11 */
Damien Miller280ecfb2003-05-14 13:46:00 +10001468 for (i = 0; i < options.num_identity_files; i++) {
1469 key = options.identity_keys[i];
Damien Miller0a80ca12010-02-27 07:55:05 +11001470 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1471 continue;
Damien Miller280ecfb2003-05-14 13:46:00 +10001472 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001473 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001474 id->agent_fd = -1;
Damien Miller280ecfb2003-05-14 13:46:00 +10001475 id->key = key;
1476 id->filename = xstrdup(options.identity_files[i]);
Darren Tucker19104782013-04-05 11:13:08 +11001477 id->userprovided = options.identity_file_userprovided[i];
Damien Miller280ecfb2003-05-14 13:46:00 +10001478 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001479 }
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001480 /* list of certificates specified by user */
1481 for (i = 0; i < options.num_certificate_files; i++) {
1482 key = options.certificates[i];
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001483 if (!sshkey_is_cert(key) || key->cert == NULL ||
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001484 key->cert->type != SSH2_CERT_TYPE_USER)
1485 continue;
1486 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001487 id->agent_fd = -1;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001488 id->key = key;
1489 id->filename = xstrdup(options.certificate_files[i]);
1490 id->userprovided = options.certificate_file_userprovided[i];
1491 TAILQ_INSERT_TAIL(preferred, id, next);
1492 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001493 /* list of keys supported by the agent */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001494 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1495 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1496 debug("%s: ssh_get_authentication_socket: %s",
1497 __func__, ssh_err(r));
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +00001498 } else if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001499 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1500 debug("%s: ssh_fetch_identitylist: %s",
1501 __func__, ssh_err(r));
markus@openbsd.orgfc77ccd2016-01-14 22:56:56 +00001502 close(agent_fd);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001503 } else {
1504 for (j = 0; j < idlist->nkeys; j++) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001505 found = 0;
1506 TAILQ_FOREACH(id, &files, next) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001507 /*
1508 * agent keys from the config file are
1509 * preferred
1510 */
1511 if (sshkey_equal(idlist->keys[j], id->key)) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001512 TAILQ_REMOVE(&files, id, next);
1513 TAILQ_INSERT_TAIL(preferred, id, next);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001514 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001515 found = 1;
1516 break;
1517 }
1518 }
Damien Millerbd394c32004-03-08 23:12:36 +11001519 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001520 id = xcalloc(1, sizeof(*id));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001521 /* XXX "steals" key/comment from idlist */
1522 id->key = idlist->keys[j];
1523 id->filename = idlist->comments[j];
1524 idlist->keys[j] = NULL;
1525 idlist->comments[j] = NULL;
1526 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001527 TAILQ_INSERT_TAIL(&agent, id, next);
1528 }
1529 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001530 ssh_free_identitylist(idlist);
Damien Miller280ecfb2003-05-14 13:46:00 +10001531 /* append remaining agent keys */
1532 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1533 TAILQ_REMOVE(&agent, id, next);
1534 TAILQ_INSERT_TAIL(preferred, id, next);
1535 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001536 authctxt->agent_fd = agent_fd;
Damien Miller62cee002000-09-23 17:15:56 +11001537 }
djm@openbsd.org82f24c32016-05-23 23:30:50 +00001538 /* Prefer PKCS11 keys that are explicitly listed */
1539 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1540 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
1541 continue;
1542 found = 0;
1543 TAILQ_FOREACH(id2, &files, next) {
1544 if (id2->key == NULL ||
1545 (id2->key->flags & SSHKEY_FLAG_EXT) == 0)
1546 continue;
1547 if (sshkey_equal(id->key, id2->key)) {
1548 TAILQ_REMOVE(&files, id, next);
1549 TAILQ_INSERT_TAIL(preferred, id, next);
1550 found = 1;
1551 break;
1552 }
1553 }
1554 /* If IdentitiesOnly set and key not found then don't use it */
1555 if (!found && options.identities_only) {
1556 TAILQ_REMOVE(&files, id, next);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001557 freezero(id, sizeof(*id));
djm@openbsd.org82f24c32016-05-23 23:30:50 +00001558 }
1559 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001560 /* append remaining keys from the config file */
1561 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1562 TAILQ_REMOVE(&files, id, next);
1563 TAILQ_INSERT_TAIL(preferred, id, next);
1564 }
djm@openbsd.orge679c092015-10-13 16:15:21 +00001565 /* finally, filter by PubkeyAcceptedKeyTypes */
1566 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
djm@openbsd.orgd78b75d2018-07-03 13:07:58 +00001567 if (id->key != NULL && !key_type_allowed_by_config(id->key)) {
djm@openbsd.orge679c092015-10-13 16:15:21 +00001568 debug("Skipping %s key %s - "
1569 "not in PubkeyAcceptedKeyTypes",
1570 sshkey_ssh_name(id->key), id->filename);
1571 TAILQ_REMOVE(preferred, id, next);
1572 sshkey_free(id->key);
1573 free(id->filename);
1574 memset(id, 0, sizeof(*id));
1575 continue;
1576 }
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001577 debug2("key: %s (%p)%s%s", id->filename, id->key,
1578 id->userprovided ? ", explicit" : "",
1579 id->agent_fd != -1 ? ", agent" : "");
Damien Miller280ecfb2003-05-14 13:46:00 +10001580 }
1581}
1582
1583static void
1584pubkey_cleanup(Authctxt *authctxt)
1585{
1586 Identity *id;
1587
djm@openbsd.org141efe42015-01-14 20:05:27 +00001588 if (authctxt->agent_fd != -1)
1589 ssh_close_authentication_socket(authctxt->agent_fd);
Damien Miller280ecfb2003-05-14 13:46:00 +10001590 for (id = TAILQ_FIRST(&authctxt->keys); id;
1591 id = TAILQ_FIRST(&authctxt->keys)) {
1592 TAILQ_REMOVE(&authctxt->keys, id, next);
mmcc@openbsd.org89540b62015-12-11 02:31:47 +00001593 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +10001594 free(id->filename);
1595 free(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001596 }
Damien Millereba71ba2000-04-29 23:57:08 +10001597}
1598
djm@openbsd.orgb9844a42016-12-04 23:54:02 +00001599static void
1600pubkey_reset(Authctxt *authctxt)
1601{
1602 Identity *id;
1603
1604 TAILQ_FOREACH(id, &authctxt->keys, next)
1605 id->tried = 0;
1606}
1607
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001608static int
1609try_identity(Identity *id)
1610{
1611 if (!id->key)
1612 return (0);
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001613 if (sshkey_type_plain(id->key->type) == KEY_RSA &&
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001614 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1615 debug("Skipped %s key %s for RSA/MD5 server",
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001616 sshkey_type(id->key), id->filename);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001617 return (0);
1618 }
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001619 return 1;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001620}
1621
Damien Miller62cee002000-09-23 17:15:56 +11001622int
1623userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001624{
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001625 struct ssh *ssh = active_state; /* XXX */
Damien Miller280ecfb2003-05-14 13:46:00 +10001626 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001627 int sent = 0;
djm@openbsd.org6830be92017-04-28 03:24:53 +00001628 char *fp;
Damien Millereba71ba2000-04-29 23:57:08 +10001629
Damien Miller280ecfb2003-05-14 13:46:00 +10001630 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1631 if (id->tried++)
1632 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001633 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001634 TAILQ_REMOVE(&authctxt->keys, id, next);
1635 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1636 /*
1637 * send a test message if we have the public key. for
1638 * encrypted keys we cannot do this and have to load the
1639 * private key instead
1640 */
Damien Miller324541e2013-12-31 12:25:40 +11001641 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001642 if (try_identity(id)) {
djm@openbsd.org6830be92017-04-28 03:24:53 +00001643 if ((fp = sshkey_fingerprint(id->key,
1644 options.fingerprint_hash,
1645 SSH_FP_DEFAULT)) == NULL) {
1646 error("%s: sshkey_fingerprint failed",
1647 __func__);
1648 return 0;
1649 }
1650 debug("Offering public key: %s %s %s",
1651 sshkey_type(id->key), fp, id->filename);
1652 free(fp);
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001653 sent = send_pubkey_test(ssh, authctxt, id);
Damien Miller324541e2013-12-31 12:25:40 +11001654 }
1655 } else {
Damien Miller280ecfb2003-05-14 13:46:00 +10001656 debug("Trying private key: %s", id->filename);
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001657 id->key = load_identity_file(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001658 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001659 if (try_identity(id)) {
1660 id->isprivate = 1;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001661 sent = sign_and_send_pubkey(ssh,
Damien Miller324541e2013-12-31 12:25:40 +11001662 authctxt, id);
1663 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001664 sshkey_free(id->key);
Damien Miller280ecfb2003-05-14 13:46:00 +10001665 id->key = NULL;
djm@openbsd.orgb9844a42016-12-04 23:54:02 +00001666 id->isprivate = 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001667 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001668 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001669 if (sent)
1670 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001671 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001672 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001673}
Damien Millereba71ba2000-04-29 23:57:08 +10001674
Damien Miller874d77b2000-10-14 16:23:11 +11001675/*
1676 * Send userauth request message specifying keyboard-interactive method.
1677 */
1678int
1679userauth_kbdint(Authctxt *authctxt)
1680{
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001681 struct ssh *ssh = active_state; /* XXX */
Damien Miller874d77b2000-10-14 16:23:11 +11001682 static int attempt = 0;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001683 int r;
Damien Miller874d77b2000-10-14 16:23:11 +11001684
1685 if (attempt++ >= options.number_of_password_prompts)
1686 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001687 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1688 if (attempt > 1 && !authctxt->info_req_seen) {
1689 debug3("userauth_kbdint: disable: no info_req_seen");
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001690 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
Ben Lindstrom7d199962001-09-12 18:29:00 +00001691 return 0;
1692 }
Damien Miller874d77b2000-10-14 16:23:11 +11001693
1694 debug2("userauth_kbdint");
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001695 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1696 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1697 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1698 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1699 (r = sshpkt_put_cstring(ssh, "")) != 0 || /* lang */
1700 (r = sshpkt_put_cstring(ssh, options.kbd_interactive_devices ?
1701 options.kbd_interactive_devices : "")) != 0 ||
1702 (r = sshpkt_send(ssh)) != 0)
1703 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller874d77b2000-10-14 16:23:11 +11001704
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001705 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
Damien Miller874d77b2000-10-14 16:23:11 +11001706 return 1;
1707}
1708
1709/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001710 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001711 */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001712int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001713input_userauth_info_req(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller874d77b2000-10-14 16:23:11 +11001714{
markus@openbsd.org94583be2017-05-30 14:19:15 +00001715 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001716 char *name = NULL, *inst = NULL, *lang = NULL, *prompt = NULL;
1717 char *response = NULL;
1718 u_char echo = 0;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001719 u_int num_prompts, i;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001720 int r;
Damien Miller874d77b2000-10-14 16:23:11 +11001721
1722 debug2("input_userauth_info_req");
1723
1724 if (authctxt == NULL)
1725 fatal("input_userauth_info_req: no authentication context");
1726
Ben Lindstrom7d199962001-09-12 18:29:00 +00001727 authctxt->info_req_seen = 1;
1728
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001729 if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 ||
1730 (r = sshpkt_get_cstring(ssh, &inst, NULL)) != 0 ||
1731 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1732 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +11001733 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001734 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001735 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001736 logit("%s", inst);
Damien Miller874d77b2000-10-14 16:23:11 +11001737
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001738 if ((r = sshpkt_get_u32(ssh, &num_prompts)) != 0)
1739 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +11001740 /*
1741 * Begin to build info response packet based on prompts requested.
1742 * We commit to providing the correct number of responses, so if
1743 * further on we run into a problem that prevents this, we have to
1744 * be sure and clean this up and send a correct error response.
1745 */
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001746 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE)) != 0 ||
1747 (r = sshpkt_put_u32(ssh, num_prompts)) != 0)
1748 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +11001749
Ben Lindstrom551ea372001-06-05 18:56:16 +00001750 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001751 for (i = 0; i < num_prompts; i++) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001752 if ((r = sshpkt_get_cstring(ssh, &prompt, NULL)) != 0 ||
1753 (r = sshpkt_get_u8(ssh, &echo)) != 0)
1754 goto out;
Ben Lindstrom949974b2001-06-25 05:20:31 +00001755 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001756 if ((r = sshpkt_put_cstring(ssh, response)) != 0)
1757 goto out;
1758 freezero(response, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +10001759 free(prompt);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001760 response = prompt = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001761 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00001762 /* done with parsing incoming message. */
1763 if ((r = sshpkt_get_end(ssh)) != 0 ||
1764 (r = sshpkt_add_padding(ssh, 64)) != 0)
1765 goto out;
1766 r = sshpkt_send(ssh);
1767 out:
1768 if (response)
1769 freezero(response, strlen(response));
1770 free(prompt);
1771 free(name);
1772 free(inst);
1773 free(lang);
1774 return r;
Damien Miller874d77b2000-10-14 16:23:11 +11001775}
Damien Miller62cee002000-09-23 17:15:56 +11001776
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001777static int
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001778ssh_keysign(struct sshkey *key, u_char **sigp, size_t *lenp,
1779 const u_char *data, size_t datalen)
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001780{
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001781 struct sshbuf *b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001782 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001783 pid_t pid;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001784 int i, r, to[2], from[2], status, sock = packet_get_connection_in();
1785 u_char rversion = 0, version = 2;
1786 void (*osigchld)(int);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001787
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001788 *sigp = NULL;
1789 *lenp = 0;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001790
Ben Lindstrom5206b952002-06-06 19:59:29 +00001791 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001792 error("%s: not installed: %s", __func__, strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001793 return -1;
1794 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001795 if (fflush(stdout) != 0) {
1796 error("%s: fflush: %s", __func__, strerror(errno));
1797 return -1;
1798 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001799 if (pipe(to) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001800 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001801 return -1;
1802 }
1803 if (pipe(from) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001804 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001805 return -1;
1806 }
1807 if ((pid = fork()) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001808 error("%s: fork: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001809 return -1;
1810 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001811 osigchld = signal(SIGCHLD, SIG_DFL);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001812 if (pid == 0) {
Darren Tucker6e7fe1c2010-01-08 17:07:22 +11001813 /* keep the socket on exec */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001814 fcntl(sock, F_SETFD, 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001815 close(from[0]);
1816 if (dup2(from[1], STDOUT_FILENO) < 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001817 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001818 close(to[1]);
1819 if (dup2(to[0], STDIN_FILENO) < 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001820 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001821 close(from[1]);
1822 close(to[0]);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001823 /* Close everything but stdio and the socket */
1824 for (i = STDERR_FILENO + 1; i < sock; i++)
1825 close(i);
1826 closefrom(sock + 1);
1827 debug3("%s: [child] pid=%ld, exec %s",
1828 __func__, (long)getpid(), _PATH_SSH_KEY_SIGN);
mmcc@openbsd.org94141b72015-12-11 00:20:04 +00001829 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001830 fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001831 strerror(errno));
1832 }
1833 close(from[1]);
1834 close(to[0]);
1835
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001836 if ((b = sshbuf_new()) == NULL)
1837 fatal("%s: sshbuf_new failed", __func__);
1838 /* send # of sock, data to be signed */
djm@openbsd.org326e2fa2017-01-30 00:32:28 +00001839 if ((r = sshbuf_put_u32(b, sock)) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001840 (r = sshbuf_put_string(b, data, datalen)) != 0)
1841 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1842 if (ssh_msg_send(to[1], version, b) == -1)
1843 fatal("%s: couldn't send request", __func__);
1844 sshbuf_reset(b);
1845 r = ssh_msg_recv(from[0], b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001846 close(from[0]);
1847 close(to[1]);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001848 if (r < 0) {
1849 error("%s: no reply", __func__);
1850 goto fail;
1851 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001852
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001853 errno = 0;
1854 while (waitpid(pid, &status, 0) < 0) {
1855 if (errno != EINTR) {
1856 error("%s: waitpid %ld: %s",
1857 __func__, (long)pid, strerror(errno));
1858 goto fail;
1859 }
1860 }
1861 if (!WIFEXITED(status)) {
1862 error("%s: exited abnormally", __func__);
1863 goto fail;
1864 }
1865 if (WEXITSTATUS(status) != 0) {
1866 error("%s: exited with status %d",
1867 __func__, WEXITSTATUS(status));
1868 goto fail;
1869 }
1870 if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
1871 error("%s: buffer error: %s", __func__, ssh_err(r));
1872 goto fail;
1873 }
1874 if (rversion != version) {
1875 error("%s: bad version", __func__);
1876 goto fail;
1877 }
1878 if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
1879 error("%s: buffer error: %s", __func__, ssh_err(r));
1880 fail:
1881 signal(SIGCHLD, osigchld);
1882 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001883 return -1;
1884 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001885 signal(SIGCHLD, osigchld);
1886 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001887
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001888 return 0;
1889}
1890
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001891int
1892userauth_hostbased(Authctxt *authctxt)
1893{
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001894 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001895 struct sshkey *private = NULL;
1896 struct sshbuf *b = NULL;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001897 u_char *sig = NULL, *keyblob = NULL;
1898 char *fp = NULL, *chost = NULL, *lname = NULL;
1899 size_t siglen = 0, keylen = 0;
1900 int i, r, success = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001901
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001902 if (authctxt->ktypes == NULL) {
1903 authctxt->oktypes = xstrdup(options.hostbased_key_types);
1904 authctxt->ktypes = authctxt->oktypes;
1905 }
djm@openbsd.org1195f4c2015-01-08 10:14:08 +00001906
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001907 /*
1908 * Work through each listed type pattern in HostbasedKeyTypes,
1909 * trying each hostkey that matches the type in turn.
1910 */
1911 for (;;) {
1912 if (authctxt->active_ktype == NULL)
1913 authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
1914 if (authctxt->active_ktype == NULL ||
1915 *authctxt->active_ktype == '\0')
1916 break;
1917 debug3("%s: trying key type %s", __func__,
1918 authctxt->active_ktype);
1919
1920 /* check for a useful key */
1921 private = NULL;
1922 for (i = 0; i < authctxt->sensitive->nkeys; i++) {
1923 if (authctxt->sensitive->keys[i] == NULL ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001924 authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
1925 continue;
1926 if (match_pattern_list(
1927 sshkey_ssh_name(authctxt->sensitive->keys[i]),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001928 authctxt->active_ktype, 0) != 1)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001929 continue;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001930 /* we take and free the key */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001931 private = authctxt->sensitive->keys[i];
1932 authctxt->sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001933 break;
1934 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001935 /* Found one */
1936 if (private != NULL)
1937 break;
1938 /* No more keys of this type; advance */
1939 authctxt->active_ktype = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001940 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001941 if (private == NULL) {
1942 free(authctxt->oktypes);
1943 authctxt->oktypes = authctxt->ktypes = NULL;
1944 authctxt->active_ktype = NULL;
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001945 debug("No more client hostkeys for hostbased authentication.");
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001946 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001947 }
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001948
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001949 if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
1950 SSH_FP_DEFAULT)) == NULL) {
1951 error("%s: sshkey_fingerprint failed", __func__);
1952 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001953 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001954 debug("%s: trying hostkey %s %s",
1955 __func__, sshkey_ssh_name(private), fp);
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001956
Damien Miller91c18472001-11-12 11:02:03 +11001957 /* figure out a name for the client host */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001958 if ((lname = get_local_name(packet_get_connection_in())) == NULL) {
1959 error("%s: cannot get local ipaddr/name", __func__);
1960 goto out;
Damien Miller91c18472001-11-12 11:02:03 +11001961 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001962
1963 /* XXX sshbuf_put_stringf? */
1964 xasprintf(&chost, "%s.", lname);
1965 debug2("%s: chost %s", __func__, chost);
Damien Miller91c18472001-11-12 11:02:03 +11001966
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001967 /* construct data */
1968 if ((b = sshbuf_new()) == NULL) {
1969 error("%s: sshbuf_new failed", __func__);
1970 goto out;
1971 }
1972 if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
1973 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
1974 goto out;
1975 }
1976 if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
1977 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1978 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001979 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001980 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00001981 (r = sshbuf_put_cstring(b, sshkey_ssh_name(private))) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001982 (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
1983 (r = sshbuf_put_cstring(b, chost)) != 0 ||
1984 (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
1985 error("%s: buffer error: %s", __func__, ssh_err(r));
1986 goto out;
1987 }
1988
1989#ifdef DEBUG_PK
1990 sshbuf_dump(b, stderr);
1991#endif
dtucker@openbsd.org26efc2f2018-07-16 11:05:41 +00001992 r = ssh_keysign(private, &sig, &siglen,
1993 sshbuf_ptr(b), sshbuf_len(b));
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001994 if (r != 0) {
1995 error("sign using hostkey %s %s failed",
1996 sshkey_ssh_name(private), fp);
1997 goto out;
1998 }
1999 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2000 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
2001 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
2002 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
markus@openbsd.org5467fbc2018-07-11 18:53:29 +00002003 (r = sshpkt_put_cstring(ssh, sshkey_ssh_name(private))) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002004 (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
2005 (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
2006 (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
2007 (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
2008 (r = sshpkt_send(ssh)) != 0) {
2009 error("%s: packet error: %s", __func__, ssh_err(r));
2010 goto out;
2011 }
2012 success = 1;
2013
2014 out:
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002015 if (sig != NULL)
2016 freezero(sig, siglen);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00002017 free(keyblob);
2018 free(lname);
2019 free(fp);
2020 free(chost);
2021 sshkey_free(private);
2022 sshbuf_free(b);
2023
2024 return success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00002025}
2026
Damien Miller62cee002000-09-23 17:15:56 +11002027/* find auth method */
2028
Damien Miller62cee002000-09-23 17:15:56 +11002029/*
2030 * given auth method name, if configurable options permit this method fill
2031 * in auth_ident field and return true, otherwise return false.
2032 */
Ben Lindstrombba81212001-06-25 05:01:22 +00002033static int
Damien Miller62cee002000-09-23 17:15:56 +11002034authmethod_is_enabled(Authmethod *method)
2035{
2036 if (method == NULL)
2037 return 0;
2038 /* return false if options indicate this method is disabled */
2039 if (method->enabled == NULL || *method->enabled == 0)
2040 return 0;
2041 /* return false if batch mode is enabled but method needs interactive mode */
2042 if (method->batch_flag != NULL && *method->batch_flag != 0)
2043 return 0;
2044 return 1;
2045}
2046
Ben Lindstrombba81212001-06-25 05:01:22 +00002047static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11002048authmethod_lookup(const char *name)
2049{
2050 Authmethod *method = NULL;
2051 if (name != NULL)
2052 for (method = authmethods; method->name != NULL; method++)
2053 if (strcmp(name, method->name) == 0)
2054 return method;
2055 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
2056 return NULL;
2057}
2058
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002059/* XXX internal state */
2060static Authmethod *current = NULL;
2061static char *supported = NULL;
2062static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00002063
Damien Miller62cee002000-09-23 17:15:56 +11002064/*
2065 * Given the authentication method list sent by the server, return the
2066 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00002067 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00002068 */
Ben Lindstrombba81212001-06-25 05:01:22 +00002069static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11002070authmethod_get(char *authlist)
2071{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002072 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00002073 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00002074
Damien Miller62cee002000-09-23 17:15:56 +11002075 /* Use a suitable default if we're passed a nil list. */
2076 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002077 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11002078
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002079 if (supported == NULL || strcmp(authlist, supported) != 0) {
2080 debug3("start over, passed a different list %s", authlist);
Darren Tuckera627d422013-06-02 07:31:17 +10002081 free(supported);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002082 supported = xstrdup(authlist);
2083 preferred = options.preferred_authentications;
2084 debug3("preferred %s", preferred);
2085 current = NULL;
2086 } else if (current != NULL && authmethod_is_enabled(current))
2087 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10002088
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002089 for (;;) {
2090 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00002091 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002092 current = NULL;
2093 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11002094 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002095 preferred += next;
2096 debug3("authmethod_lookup %s", name);
2097 debug3("remaining preferred: %s", preferred);
2098 if ((current = authmethod_lookup(name)) != NULL &&
2099 authmethod_is_enabled(current)) {
2100 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00002101 debug("Next authentication method: %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002102 free(name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002103 return current;
2104 }
Darren Tuckere52a2602013-06-06 08:22:05 +10002105 free(name);
Damien Millereba71ba2000-04-29 23:57:08 +10002106 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002107}
Damien Miller62cee002000-09-23 17:15:56 +11002108
Ben Lindstrom79073822001-07-04 03:42:30 +00002109static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002110authmethods_get(void)
2111{
2112 Authmethod *method = NULL;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002113 struct sshbuf *b;
Damien Miller0e3b8722002-01-22 23:26:38 +11002114 char *list;
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002115 int r;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002116
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002117 if ((b = sshbuf_new()) == NULL)
2118 fatal("%s: sshbuf_new failed", __func__);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002119 for (method = authmethods; method->name != NULL; method++) {
2120 if (authmethod_is_enabled(method)) {
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002121 if ((r = sshbuf_putf(b, "%s%s",
2122 sshbuf_len(b) ? "," : "", method->name)) != 0)
2123 fatal("%s: buffer error: %s",
2124 __func__, ssh_err(r));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002125 }
Damien Miller62cee002000-09-23 17:15:56 +11002126 }
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002127 if ((list = sshbuf_dup_string(b)) == NULL)
djm@openbsd.org1a31d022016-05-02 08:49:03 +00002128 fatal("%s: sshbuf_dup_string failed", __func__);
markus@openbsd.orgcecee2d2018-07-09 21:03:30 +00002129 sshbuf_free(b);
Damien Miller0e3b8722002-01-22 23:26:38 +11002130 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10002131}