blob: f3ccd53a9b468ccc97d4bb91edeb27759a50a4c7 [file] [log] [blame]
djm@openbsd.org312d2f22018-07-04 13:49:31 +00001/* $OpenBSD: sshconnect2.c,v 1.275 2018/07/04 13:49:31 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"
Damien Millereba71ba2000-04-29 23:57:08 +100052#include "buffer.h"
53#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"
Damien Millerd7834352006-08-05 12:39:39 +100056#include "key.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__);
170 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(s);
Damien Millera0ff4662001-03-30 10:49:35 +1000171 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000172 compat_cipher_proposal(options.ciphers);
Damien Millera0ff4662001-03-30 10:49:35 +1000173 myproposal[PROPOSAL_ENC_ALGS_STOC] =
djm@openbsd.orgf9eca242015-07-30 00:01:34 +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 ?
djm@openbsd.org4577ade2016-09-28 20:32:42 +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] =
Damien Miller324541e2013-12-31 12:25:40 +1100187 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(
194 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] =
226 compat_kex_proposal(options.kex_algorithms);
227 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 */
235 packet_start(SSH2_MSG_IGNORE);
236 packet_put_cstring("markus");
237 packet_send();
238 packet_write_wait();
239#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000240}
Damien Millerb1715dc2000-05-30 13:44:51 +1000241
Damien Millereba71ba2000-04-29 23:57:08 +1000242/*
243 * Authenticate user
244 */
Damien Miller62cee002000-09-23 17:15:56 +1100245
djm@openbsd.org141efe42015-01-14 20:05:27 +0000246typedef struct cauthctxt Authctxt;
247typedef struct cauthmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000248typedef struct identity Identity;
249typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100250
Damien Miller280ecfb2003-05-14 13:46:00 +1000251struct identity {
252 TAILQ_ENTRY(identity) next;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000253 int agent_fd; /* >=0 if agent supports key */
254 struct sshkey *key; /* public/private key */
Damien Miller280ecfb2003-05-14 13:46:00 +1000255 char *filename; /* comment for agent-only keys */
256 int tried;
257 int isprivate; /* key points to the private key */
Damien Miller5ceddc32013-02-15 12:18:32 +1100258 int userprovided;
Damien Miller280ecfb2003-05-14 13:46:00 +1000259};
260TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100261
djm@openbsd.org141efe42015-01-14 20:05:27 +0000262struct cauthctxt {
Damien Miller62cee002000-09-23 17:15:56 +1100263 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000264 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100265 const char *host;
266 const char *service;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000267 struct cauthmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000268 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000269 char *authlist;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000270 int attempt;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000271 /* pubkey */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000272 struct idlist keys;
273 int agent_fd;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000274 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000275 Sensitive *sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000276 char *oktypes, *ktypes;
277 const char *active_ktype;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000278 /* kbd-interactive */
279 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000280 /* generic */
281 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100282};
djm@openbsd.org141efe42015-01-14 20:05:27 +0000283
284struct cauthmethod {
Damien Miller62cee002000-09-23 17:15:56 +1100285 char *name; /* string to compare against server's list */
286 int (*userauth)(Authctxt *authctxt);
Damien Miller01ed2272008-11-05 16:20:46 +1100287 void (*cleanup)(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100288 int *enabled; /* flag in option struct that enables method */
289 int *batch_flag; /* flag in option struct that disables method */
290};
291
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000292int input_userauth_service_accept(int, u_int32_t, struct ssh *);
293int input_userauth_ext_info(int, u_int32_t, struct ssh *);
294int input_userauth_success(int, u_int32_t, struct ssh *);
295int input_userauth_success_unexpected(int, u_int32_t, struct ssh *);
296int input_userauth_failure(int, u_int32_t, struct ssh *);
297int input_userauth_banner(int, u_int32_t, struct ssh *);
298int input_userauth_error(int, u_int32_t, struct ssh *);
299int input_userauth_info_req(int, u_int32_t, struct ssh *);
300int input_userauth_pk_ok(int, u_int32_t, struct ssh *);
301int input_userauth_passwd_changereq(int, u_int32_t, struct ssh *);
Damien Miller874d77b2000-10-14 16:23:11 +1100302
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000303int userauth_none(Authctxt *);
304int userauth_pubkey(Authctxt *);
305int userauth_passwd(Authctxt *);
306int userauth_kbdint(Authctxt *);
307int userauth_hostbased(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100308
Darren Tucker0efd1552003-08-26 11:49:55 +1000309#ifdef GSSAPI
310int userauth_gssapi(Authctxt *authctxt);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000311int input_gssapi_response(int type, u_int32_t, struct ssh *);
312int input_gssapi_token(int type, u_int32_t, struct ssh *);
313int input_gssapi_hash(int type, u_int32_t, struct ssh *);
314int input_gssapi_error(int, u_int32_t, struct ssh *);
315int input_gssapi_errtok(int, u_int32_t, struct ssh *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000316#endif
317
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000318void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000319
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000320static int sign_and_send_pubkey(struct ssh *ssh, Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000321static void pubkey_prepare(Authctxt *);
322static void pubkey_cleanup(Authctxt *);
djm@openbsd.orgb9844a42016-12-04 23:54:02 +0000323static void pubkey_reset(Authctxt *);
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000324static struct sshkey *load_identity_file(Identity *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000325
Ben Lindstrombba81212001-06-25 05:01:22 +0000326static Authmethod *authmethod_get(char *authlist);
327static Authmethod *authmethod_lookup(const char *name);
328static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100329
330Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000331#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100332 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000333 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100334 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000335 &options.gss_authentication,
336 NULL},
337#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000338 {"hostbased",
339 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100340 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000341 &options.hostbased_authentication,
342 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000343 {"publickey",
344 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100345 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000346 &options.pubkey_authentication,
347 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100348 {"keyboard-interactive",
349 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100350 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100351 &options.kbd_interactive_authentication,
352 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000353 {"password",
354 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100355 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000356 &options.password_authentication,
357 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100358 {"none",
359 userauth_none,
360 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100361 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100362 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100363 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100364};
365
366void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000367ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000368 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100369{
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000370 struct ssh *ssh = active_state;
Damien Miller62cee002000-09-23 17:15:56 +1100371 Authctxt authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000372 int r;
Damien Miller62cee002000-09-23 17:15:56 +1100373
Ben Lindstrom551ea372001-06-05 18:56:16 +0000374 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000375 options.kbd_interactive_authentication = 1;
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000376 if (options.preferred_authentications == NULL)
377 options.preferred_authentications = authmethods_get();
378
Damien Miller62cee002000-09-23 17:15:56 +1100379 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000380 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000381 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100382 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000383 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100384 authctxt.host = host;
385 authctxt.service = "ssh-connection"; /* service name */
386 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100387 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000388 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000389 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000390 authctxt.sensitive = sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000391 authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000392 authctxt.info_req_seen = 0;
djm@openbsd.orgf14564c2015-01-15 11:04:36 +0000393 authctxt.agent_fd = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100394 if (authctxt.method == NULL)
395 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100396
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000397 if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
398 (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
399 (r = sshpkt_send(ssh)) != 0)
400 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller62cee002000-09-23 17:15:56 +1100401
markus@openbsd.org94583be2017-05-30 14:19:15 +0000402 ssh->authctxt = &authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000403 ssh_dispatch_init(ssh, &input_userauth_error);
404 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
405 ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
markus@openbsd.org92e9fe62017-05-31 07:00:13 +0000406 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt.success); /* loop until success */
markus@openbsd.org94583be2017-05-30 14:19:15 +0000407 ssh->authctxt = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100408
Damien Miller280ecfb2003-05-14 13:46:00 +1000409 pubkey_cleanup(&authctxt);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000410 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
Damien Millerf842fcb2003-05-15 12:01:28 +1000411
dtucker@openbsd.orgf31c6542016-09-22 02:29:57 +0000412 if (!authctxt.success)
413 fatal("Authentication failed.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000414 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100415}
Damien Millerf842fcb2003-05-15 12:01:28 +1000416
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000417/* ARGSUSED */
418int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000419input_userauth_service_accept(int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000420{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000421 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000422 int r;
423
424 if (ssh_packet_remaining(ssh) > 0) {
425 char *reply;
426
427 if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
428 goto out;
429 debug2("service_accept: %s", reply);
430 free(reply);
431 } else {
432 debug2("buggy server: service_accept w/o service");
433 }
434 if ((r = sshpkt_get_end(ssh)) != 0)
435 goto out;
436 debug("SSH2_MSG_SERVICE_ACCEPT received");
437
438 /* initial userauth request */
439 userauth_none(authctxt);
440
441 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error);
442 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
443 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
444 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
445 r = 0;
446 out:
447 return r;
448}
449
450/* ARGSUSED */
451int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000452input_userauth_ext_info(int type, u_int32_t seqnr, struct ssh *ssh)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000453{
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000454 return kex_input_ext_info(type, seqnr, ssh);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000455}
456
Damien Miller62cee002000-09-23 17:15:56 +1100457void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000458userauth(Authctxt *authctxt, char *authlist)
459{
Damien Miller01ed2272008-11-05 16:20:46 +1100460 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
461 authctxt->method->cleanup(authctxt);
462
Darren Tuckera627d422013-06-02 07:31:17 +1000463 free(authctxt->methoddata);
464 authctxt->methoddata = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000465 if (authlist == NULL) {
466 authlist = authctxt->authlist;
467 } else {
Darren Tuckera627d422013-06-02 07:31:17 +1000468 free(authctxt->authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000469 authctxt->authlist = authlist;
470 }
471 for (;;) {
472 Authmethod *method = authmethod_get(authlist);
473 if (method == NULL)
dtucker@openbsd.org4626e392017-06-14 00:31:38 +0000474 fatal("%s@%s: Permission denied (%s).",
475 authctxt->server_user, authctxt->host, authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000476 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000477
478 /* reset the per method handler */
479 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
480 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
481
482 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000483 if (method->userauth(authctxt) != 0) {
484 debug2("we sent a %s packet, wait for reply", method->name);
485 break;
486 } else {
487 debug2("we did not send a packet, disable method");
488 method->enabled = NULL;
489 }
490 }
491}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000492
Damien Millerf7475d72008-11-03 19:26:18 +1100493/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000494int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000495input_userauth_error(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100496{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000497 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000498 "type %d", type);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000499 return 0;
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000500}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000501
Damien Millerf7475d72008-11-03 19:26:18 +1100502/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000503int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000504input_userauth_banner(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000505{
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000506 char *msg, *lang;
Damien Miller7ba0ca72008-07-17 18:57:06 +1000507 u_int len;
Darren Tucker79644822003-10-08 17:37:58 +1000508
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000509 debug3("%s", __func__);
510 msg = packet_get_string(&len);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000511 lang = packet_get_string(NULL);
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000512 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO)
513 fmprintf(stderr, "%s", msg);
514 free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000515 free(lang);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000516 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100517}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000518
Damien Millerf7475d72008-11-03 19:26:18 +1100519/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000520int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000521input_userauth_success(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100522{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000523 Authctxt *authctxt = ssh->authctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100524
Damien Miller62cee002000-09-23 17:15:56 +1100525 if (authctxt == NULL)
526 fatal("input_userauth_success: no authentication context");
Darren Tuckera627d422013-06-02 07:31:17 +1000527 free(authctxt->authlist);
528 authctxt->authlist = NULL;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100529 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
530 authctxt->method->cleanup(authctxt);
Darren Tuckera627d422013-06-02 07:31:17 +1000531 free(authctxt->methoddata);
532 authctxt->methoddata = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100533 authctxt->success = 1; /* break out */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000534 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100535}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000536
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000537int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000538input_userauth_success_unexpected(int type, u_int32_t seq, struct ssh *ssh)
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100539{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000540 Authctxt *authctxt = ssh->authctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100541
542 if (authctxt == NULL)
543 fatal("%s: no authentication context", __func__);
544
545 fatal("Unexpected authentication success during %s.",
546 authctxt->method->name);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000547 return 0;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100548}
549
Damien Millerf7475d72008-11-03 19:26:18 +1100550/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000551int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000552input_userauth_failure(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100553{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000554 Authctxt *authctxt = ssh->authctxt;
Damien Miller62cee002000-09-23 17:15:56 +1100555 char *authlist = NULL;
556 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100557
558 if (authctxt == NULL)
559 fatal("input_userauth_failure: no authentication context");
560
Damien Miller874d77b2000-10-14 16:23:11 +1100561 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100562 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100563 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100564
Damien Miller62e9c4f2013-04-23 15:15:49 +1000565 if (partial != 0) {
dtucker@openbsd.org83cbca62016-07-22 05:46:11 +0000566 verbose("Authenticated with partial success.");
Damien Miller62e9c4f2013-04-23 15:15:49 +1000567 /* reset state */
djm@openbsd.orgb9844a42016-12-04 23:54:02 +0000568 pubkey_reset(authctxt);
Damien Miller62e9c4f2013-04-23 15:15:49 +1000569 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000570 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100571
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000572 userauth(authctxt, authlist);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000573 return 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000574}
Damien Millerf7475d72008-11-03 19:26:18 +1100575
576/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000577int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000578input_userauth_pk_ok(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000579{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000580 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000581 struct sshkey *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000582 Identity *id = NULL;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000583 int pktype, sent = 0;
584 u_int alen, blen;
585 char *pkalg, *fp;
586 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000587
588 if (authctxt == NULL)
589 fatal("input_userauth_pk_ok: no authentication context");
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000590
591 pkalg = packet_get_string(&alen);
592 pkblob = packet_get_string(&blen);
Damien Miller48b03fc2002-01-22 23:11:40 +1100593 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000594
Damien Miller280ecfb2003-05-14 13:46:00 +1000595 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000596
Damien Miller280ecfb2003-05-14 13:46:00 +1000597 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
598 debug("unknown pkalg %s", pkalg);
599 goto done;
600 }
601 if ((key = key_from_blob(pkblob, blen)) == NULL) {
602 debug("no key from blob. pkalg %s", pkalg);
603 goto done;
604 }
605 if (key->type != pktype) {
606 error("input_userauth_pk_ok: type mismatch "
607 "for decoded key (received %d, expected %d)",
608 key->type, pktype);
609 goto done;
610 }
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000611 if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
612 SSH_FP_DEFAULT)) == NULL)
613 goto done;
Damien Miller280ecfb2003-05-14 13:46:00 +1000614 debug2("input_userauth_pk_ok: fp %s", fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000615 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000616
Darren Tuckerd05b6012003-10-15 15:55:59 +1000617 /*
618 * search keys in the reverse order, because last candidate has been
619 * moved to the end of the queue. this also avoids confusion by
620 * duplicate keys
621 */
Damien Miller0b51a522004-04-20 20:07:19 +1000622 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000623 if (key_equal(key, id->key)) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000624 sent = sign_and_send_pubkey(ssh, authctxt, id);
Damien Miller280ecfb2003-05-14 13:46:00 +1000625 break;
626 }
627 }
628done:
dtucker@openbsd.orgf1f047f2018-02-07 22:52:45 +0000629 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000630 free(pkalg);
631 free(pkblob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000632
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000633 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000634 if (sent == 0)
635 userauth(authctxt, NULL);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000636 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100637}
638
Darren Tucker0efd1552003-08-26 11:49:55 +1000639#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100640int
Darren Tucker0efd1552003-08-26 11:49:55 +1000641userauth_gssapi(Authctxt *authctxt)
642{
643 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100644 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000645 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000646 OM_uint32 min;
647 int ok = 0;
648
649 /* Try one GSSAPI method at a time, rather than sending them all at
650 * once. */
651
Darren Tucker56afe142003-11-03 20:06:14 +1100652 if (gss_supported == NULL)
653 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000654
655 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100656 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000657 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100658 if (gss_supported->elements[mech].length < 128 &&
djm@openbsd.org84623e02018-06-26 02:02:36 +0000659 ssh_gssapi_check_mechanism(&gssctxt,
Damien Millera1cb9f32006-08-19 00:33:34 +1000660 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000661 ok = 1; /* Mechanism works */
662 } else {
663 mech++;
664 }
665 }
666
Damien Millera1cb9f32006-08-19 00:33:34 +1000667 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000668 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000669
670 authctxt->methoddata=(void *)gssctxt;
671
672 packet_start(SSH2_MSG_USERAUTH_REQUEST);
673 packet_put_cstring(authctxt->server_user);
674 packet_put_cstring(authctxt->service);
675 packet_put_cstring(authctxt->method->name);
676
677 packet_put_int(1);
678
Darren Tucker655a5e02003-11-03 20:09:03 +1100679 packet_put_int((gss_supported->elements[mech].length) + 2);
680 packet_put_char(SSH_GSS_OIDTYPE);
681 packet_put_char(gss_supported->elements[mech].length);
682 packet_put_raw(gss_supported->elements[mech].elements,
683 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000684
685 packet_send();
686
687 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
688 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
689 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
690 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
691
692 mech++; /* Move along to next candidate */
693
694 return 1;
695}
696
Damien Miller91c6aa42003-11-17 21:20:18 +1100697static OM_uint32
djm@openbsd.org39896b72017-05-31 05:08:46 +0000698process_gssapi_token(struct ssh *ssh, gss_buffer_t recv_tok)
Damien Miller91c6aa42003-11-17 21:20:18 +1100699{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000700 Authctxt *authctxt = ssh->authctxt;
Damien Miller91c6aa42003-11-17 21:20:18 +1100701 Gssctxt *gssctxt = authctxt->methoddata;
702 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000703 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
704 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100705 OM_uint32 status, ms, flags;
706 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100707
Damien Miller91c6aa42003-11-17 21:20:18 +1100708 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100709 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100710
711 if (send_tok.length > 0) {
712 if (GSS_ERROR(status))
713 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
714 else
715 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100716
Damien Miller91c6aa42003-11-17 21:20:18 +1100717 packet_put_string(send_tok.value, send_tok.length);
718 packet_send();
719 gss_release_buffer(&ms, &send_tok);
720 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100721
Damien Miller91c6aa42003-11-17 21:20:18 +1100722 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100723 /* send either complete or MIC, depending on mechanism */
724 if (!(flags & GSS_C_INTEG_FLAG)) {
725 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
726 packet_send();
727 } else {
728 ssh_gssapi_buildmic(&b, authctxt->server_user,
729 authctxt->service, "gssapi-with-mic");
730
731 gssbuf.value = buffer_ptr(&b);
732 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100733
Damien Miller0425d402003-11-17 22:18:21 +1100734 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100735
Damien Miller0425d402003-11-17 22:18:21 +1100736 if (!GSS_ERROR(status)) {
737 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
738 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100739
Damien Miller0425d402003-11-17 22:18:21 +1100740 packet_send();
741 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100742
Damien Miller0425d402003-11-17 22:18:21 +1100743 buffer_free(&b);
744 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100745 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100746 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100747
Damien Miller91c6aa42003-11-17 21:20:18 +1100748 return status;
749}
750
Damien Millerf7475d72008-11-03 19:26:18 +1100751/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000752int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000753input_gssapi_response(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000754{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000755 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000756 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000757 int oidlen;
758 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000759
760 if (authctxt == NULL)
761 fatal("input_gssapi_response: no authentication context");
762 gssctxt = authctxt->methoddata;
763
764 /* Setup our OID */
765 oidv = packet_get_string(&oidlen);
766
Darren Tucker655a5e02003-11-03 20:09:03 +1100767 if (oidlen <= 2 ||
768 oidv[0] != SSH_GSS_OIDTYPE ||
769 oidv[1] != oidlen - 2) {
Darren Tuckera627d422013-06-02 07:31:17 +1000770 free(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100771 debug("Badly encoded mechanism OID received");
772 userauth(authctxt, NULL);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000773 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000774 }
775
Darren Tucker655a5e02003-11-03 20:09:03 +1100776 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
777 fatal("Server returned different OID than expected");
778
Darren Tucker0efd1552003-08-26 11:49:55 +1000779 packet_check_eom();
780
Darren Tuckera627d422013-06-02 07:31:17 +1000781 free(oidv);
Darren Tucker0efd1552003-08-26 11:49:55 +1000782
djm@openbsd.org39896b72017-05-31 05:08:46 +0000783 if (GSS_ERROR(process_gssapi_token(ssh, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000784 /* Start again with next method on list */
785 debug("Trying to start again");
786 userauth(authctxt, NULL);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000787 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000788 }
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000789 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000790}
791
Damien Millerf7475d72008-11-03 19:26:18 +1100792/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000793int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000794input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000795{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000796 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000797 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100798 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000799 u_int slen;
800
801 if (authctxt == NULL)
802 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000803
804 recv_tok.value = packet_get_string(&slen);
805 recv_tok.length = slen; /* safe typecast */
806
807 packet_check_eom();
808
djm@openbsd.org39896b72017-05-31 05:08:46 +0000809 status = process_gssapi_token(ssh, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000810
Darren Tuckera627d422013-06-02 07:31:17 +1000811 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000812
813 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000814 /* Start again with the next method in the list */
815 userauth(authctxt, NULL);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000816 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000817 }
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000818 return 0;
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_errtok(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 Gssctxt *gssctxt;
827 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
828 gss_buffer_desc recv_tok;
Damien Millerd677ad12013-04-23 15:18:51 +1000829 OM_uint32 ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000830 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000831
832 if (authctxt == NULL)
833 fatal("input_gssapi_response: no authentication context");
834 gssctxt = authctxt->methoddata;
835
Darren Tucker600ad8d2003-08-26 12:10:48 +1000836 recv_tok.value = packet_get_string(&len);
837 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000838
839 packet_check_eom();
840
841 /* Stick it into GSSAPI and see what it says */
Damien Millerd677ad12013-04-23 15:18:51 +1000842 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000843 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000844
Darren Tuckera627d422013-06-02 07:31:17 +1000845 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000846 gss_release_buffer(&ms, &send_tok);
847
848 /* Server will be returning a failed packet after this one */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000849 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000850}
851
Damien Millerf7475d72008-11-03 19:26:18 +1100852/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000853int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000854input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000855{
Darren Tucker0efd1552003-08-26 11:49:55 +1000856 char *msg;
857 char *lang;
858
Damien Millerd677ad12013-04-23 15:18:51 +1000859 /* maj */(void)packet_get_int();
860 /* min */(void)packet_get_int();
Darren Tucker0efd1552003-08-26 11:49:55 +1000861 msg=packet_get_string(NULL);
862 lang=packet_get_string(NULL);
863
864 packet_check_eom();
865
Damien Miller15d72a02005-11-05 15:07:33 +1100866 debug("Server GSSAPI Error:\n%s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000867 free(msg);
868 free(lang);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000869 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000870}
871#endif /* GSSAPI */
872
Damien Millereba71ba2000-04-29 23:57:08 +1000873int
Damien Miller874d77b2000-10-14 16:23:11 +1100874userauth_none(Authctxt *authctxt)
875{
876 /* initial userauth request */
877 packet_start(SSH2_MSG_USERAUTH_REQUEST);
878 packet_put_cstring(authctxt->server_user);
879 packet_put_cstring(authctxt->service);
880 packet_put_cstring(authctxt->method->name);
881 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100882 return 1;
883}
884
885int
Damien Miller62cee002000-09-23 17:15:56 +1100886userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000887{
Damien Millere247cc42000-05-07 12:03:14 +1000888 static int attempt = 0;
dtucker@openbsd.orga54eb272017-08-27 00:38:41 +0000889 char prompt[256];
Damien Millereba71ba2000-04-29 23:57:08 +1000890 char *password;
Darren Tuckerab791692010-01-08 18:48:02 +1100891 const char *host = options.host_key_alias ? options.host_key_alias :
892 authctxt->host;
Damien Millereba71ba2000-04-29 23:57:08 +1000893
Damien Millerd3a18572000-06-07 19:55:44 +1000894 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000895 return 0;
896
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000897 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000898 error("Permission denied, please try again.");
899
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000900 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100901 authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +1000902 password = read_passphrase(prompt, 0);
903 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100904 packet_put_cstring(authctxt->server_user);
905 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100906 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000907 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000908 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100909 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000910 free(password);
Damien Miller9f643902001-11-12 11:02:52 +1100911 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000912 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000913
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000914 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000915 &input_userauth_passwd_changereq);
916
Damien Millereba71ba2000-04-29 23:57:08 +1000917 return 1;
918}
Damien Millerf7475d72008-11-03 19:26:18 +1100919
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000920/*
921 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
922 */
Damien Millerf7475d72008-11-03 19:26:18 +1100923/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000924int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000925input_userauth_passwd_changereq(int type, u_int32_t seqnr, struct ssh *ssh)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000926{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000927 Authctxt *authctxt = ssh->authctxt;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000928 char *info, *lang, *password = NULL, *retype = NULL;
dtucker@openbsd.orga54eb272017-08-27 00:38:41 +0000929 char prompt[256];
djm@openbsd.org3e032a92017-01-30 00:32:03 +0000930 const char *host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000931
932 debug2("input_userauth_passwd_changereq");
933
934 if (authctxt == NULL)
935 fatal("input_userauth_passwd_changereq: "
936 "no authentication context");
djm@openbsd.org3e032a92017-01-30 00:32:03 +0000937 host = options.host_key_alias ? options.host_key_alias : authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000938
939 info = packet_get_string(NULL);
940 lang = packet_get_string(NULL);
941 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000942 logit("%s", info);
Darren Tuckera627d422013-06-02 07:31:17 +1000943 free(info);
944 free(lang);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000945 packet_start(SSH2_MSG_USERAUTH_REQUEST);
946 packet_put_cstring(authctxt->server_user);
947 packet_put_cstring(authctxt->service);
948 packet_put_cstring(authctxt->method->name);
949 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000950 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000951 "Enter %.30s@%.128s's old password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100952 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000953 password = read_passphrase(prompt, 0);
954 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100955 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000956 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000957 password = NULL;
958 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000959 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000960 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100961 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000962 password = read_passphrase(prompt, RP_ALLOW_EOF);
963 if (password == NULL) {
964 /* bail out */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000965 return 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000966 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000967 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000968 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100969 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000970 retype = read_passphrase(prompt, 0);
971 if (strcmp(password, retype) != 0) {
Damien Millera5103f42014-02-04 11:20:14 +1100972 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000973 free(password);
Damien Miller996acd22003-04-09 20:59:48 +1000974 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000975 password = NULL;
976 }
Damien Millera5103f42014-02-04 11:20:14 +1100977 explicit_bzero(retype, strlen(retype));
Darren Tuckera627d422013-06-02 07:31:17 +1000978 free(retype);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000979 }
980 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100981 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000982 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000983 packet_add_padding(64);
984 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000985
986 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000987 &input_userauth_passwd_changereq);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000988 return 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000989}
Damien Millereba71ba2000-04-29 23:57:08 +1000990
djm@openbsd.org26074382018-03-03 03:01:50 +0000991/*
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000992 * Select an algorithm for publickey signatures.
993 * Returns algorithm (caller must free) or NULL if no mutual algorithm found.
994 *
995 * Call with ssh==NULL to ignore server-sig-algs extension list and
996 * only attempt with the key's base signature type.
djm@openbsd.org26074382018-03-03 03:01:50 +0000997 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000998static char *
999key_sig_algorithm(struct ssh *ssh, const struct sshkey *key)
djm@openbsd.org26074382018-03-03 03:01:50 +00001000{
djm@openbsd.orgb4d4eda2018-07-03 13:20:25 +00001001 char *allowed, *oallowed, *cp, *tmp, *alg = NULL;
djm@openbsd.org26074382018-03-03 03:01:50 +00001002
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001003 /*
1004 * The signature algorithm will only differ from the key algorithm
1005 * for RSA keys/certs and when the server advertises support for
1006 * newer (SHA2) algorithms.
1007 */
1008 if (ssh == NULL || ssh->kex->server_sig_algs == NULL ||
1009 (key->type != KEY_RSA && key->type != KEY_RSA_CERT)) {
1010 /* Filter base key signature alg against our configuration */
1011 return match_list(key_ssh_name(key),
1012 options.pubkey_key_types, NULL);
djm@openbsd.org26074382018-03-03 03:01:50 +00001013 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001014
1015 /*
1016 * For RSA keys/certs, since these might have a different sig type:
1017 * find the first entry in PubkeyAcceptedKeyTypes of the right type
1018 * that also appears in the supported signature algorithms list from
1019 * the server.
1020 */
1021 oallowed = allowed = xstrdup(options.pubkey_key_types);
1022 while ((cp = strsep(&allowed, ",")) != NULL) {
1023 if (sshkey_type_from_name(cp) != key->type)
1024 continue;
djm@openbsd.orgb4d4eda2018-07-03 13:20:25 +00001025 tmp = match_list(sshkey_sigalg_by_name(cp), ssh->kex->server_sig_algs, NULL);
1026 if (tmp != NULL)
1027 alg = xstrdup(cp);
1028 free(tmp);
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001029 if (alg != NULL)
1030 break;
1031 }
1032 free(oallowed);
1033 return alg;
djm@openbsd.org26074382018-03-03 03:01:50 +00001034}
1035
Ben Lindstrombba81212001-06-25 05:01:22 +00001036static int
djm@openbsd.org141efe42015-01-14 20:05:27 +00001037identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001038 const u_char *data, size_t datalen, u_int compat, const char *alg)
Damien Miller280ecfb2003-05-14 13:46:00 +10001039{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001040 struct sshkey *prv;
djm@openbsd.org26074382018-03-03 03:01:50 +00001041 int r;
Damien Miller280ecfb2003-05-14 13:46:00 +10001042
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001043 /* The agent supports this key. */
djm@openbsd.org26074382018-03-03 03:01:50 +00001044 if (id->key != NULL && id->agent_fd != -1) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001045 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
1046 data, datalen, alg, compat);
1047 }
1048
1049 /*
1050 * We have already loaded the private key or the private key is
1051 * stored in external hardware.
1052 */
1053 if (id->key != NULL &&
1054 (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))) {
1055 if ((r = sshkey_sign(id->key, sigp, lenp, data, datalen,
1056 alg, compat)) != 0)
1057 return r;
1058 /*
1059 * PKCS#11 tokens may not support all signature algorithms,
1060 * so check what we get back.
1061 */
1062 if ((r = sshkey_check_sigtype(*sigp, *lenp, alg)) != 0)
djm@openbsd.org26074382018-03-03 03:01:50 +00001063 return r;
1064 return 0;
1065 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001066
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001067 /* Load the private key from the file. */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001068 if ((prv = load_identity_file(id)) == NULL)
djm@openbsd.org0d1451a2016-02-23 01:34:14 +00001069 return SSH_ERR_KEY_NOT_FOUND;
djm@openbsd.orgc4972d02017-08-11 04:47:12 +00001070 if (id->key != NULL && !sshkey_equal_public(prv, id->key)) {
1071 error("%s: private key %s contents do not match public",
1072 __func__, id->filename);
1073 return SSH_ERR_KEY_NOT_FOUND;
1074 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001075 r = sshkey_sign(prv, sigp, lenp, data, datalen, alg, compat);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001076 sshkey_free(prv);
djm@openbsd.org26074382018-03-03 03:01:50 +00001077 return r;
Damien Miller280ecfb2003-05-14 13:46:00 +10001078}
1079
1080static int
djm@openbsd.org1e245522017-03-11 23:40:26 +00001081id_filename_matches(Identity *id, Identity *private_id)
1082{
1083 const char *suffixes[] = { ".pub", "-cert.pub", NULL };
1084 size_t len = strlen(id->filename), plen = strlen(private_id->filename);
1085 size_t i, slen;
1086
1087 if (strcmp(id->filename, private_id->filename) == 0)
1088 return 1;
1089 for (i = 0; suffixes[i]; i++) {
1090 slen = strlen(suffixes[i]);
1091 if (len > slen && plen == len - slen &&
1092 strcmp(id->filename + (len - slen), suffixes[i]) == 0 &&
1093 memcmp(id->filename, private_id->filename, plen) == 0)
1094 return 1;
1095 }
1096 return 0;
1097}
1098
1099static int
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001100sign_and_send_pubkey(struct ssh *ssh, Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +10001101{
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001102 struct sshbuf *b = NULL;
1103 Identity *private_id, *sign_id = NULL;
1104 u_char *signature = NULL;
1105 size_t slen = 0, skip = 0;
1106 int r, fallback_sigtype, sent = 0;
1107 char *alg = NULL, *fp = NULL;
1108 const char *loc = "";
Damien Millereba71ba2000-04-29 23:57:08 +10001109
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001110 if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1111 SSH_FP_DEFAULT)) == NULL)
1112 return 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001113
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001114 debug3("%s: %s %s", __func__, sshkey_type(id->key), fp);
Damien Millereba71ba2000-04-29 23:57:08 +10001115
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001116 /*
1117 * If the key is an certificate, try to find a matching private key
1118 * and use it to complete the signature.
djm@openbsd.org9ee692f2016-04-28 14:30:21 +00001119 * If no such private key exists, fall back to trying the certificate
1120 * key itself in case it has a private half already loaded.
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001121 * This will try to set sign_id to the private key that will perform
1122 * the signature.
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001123 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001124 if (sshkey_is_cert(id->key)) {
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001125 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1126 if (sshkey_equal_public(id->key, private_id->key) &&
1127 id->key->type != private_id->key->type) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001128 sign_id = private_id;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001129 break;
1130 }
1131 }
djm@openbsd.org1e245522017-03-11 23:40:26 +00001132 /*
1133 * Exact key matches are preferred, but also allow
1134 * filename matches for non-PKCS#11/agent keys that
1135 * didn't load public keys. This supports the case
1136 * of keeping just a private key file and public
1137 * certificate on disk.
1138 */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001139 if (sign_id == NULL &&
1140 !id->isprivate && id->agent_fd == -1 &&
djm@openbsd.org1e245522017-03-11 23:40:26 +00001141 (id->key->flags & SSHKEY_FLAG_EXT) == 0) {
1142 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1143 if (private_id->key == NULL &&
1144 id_filename_matches(id, private_id)) {
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001145 sign_id = private_id;
djm@openbsd.org1e245522017-03-11 23:40:26 +00001146 break;
1147 }
1148 }
1149 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001150 if (sign_id != NULL) {
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001151 debug2("%s: using private key \"%s\"%s for "
1152 "certificate", __func__, id->filename,
1153 id->agent_fd != -1 ? " from agent" : "");
1154 } else {
djm@openbsd.orgc38905b2016-03-14 16:20:54 +00001155 debug("%s: no separate private key for certificate "
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001156 "\"%s\"", __func__, id->filename);
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001157 }
1158 }
1159
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001160 /*
1161 * If the above didn't select another identity to do the signing
1162 * then default to the one we started with.
1163 */
1164 if (sign_id == NULL)
1165 sign_id = id;
1166
1167 /* assemble and sign data */
1168 for (fallback_sigtype = 0; fallback_sigtype <= 1; fallback_sigtype++) {
1169 free(alg);
1170 slen = 0;
1171 signature = NULL;
1172 if ((alg = key_sig_algorithm(fallback_sigtype ? NULL : ssh,
1173 id->key)) == NULL) {
1174 error("%s: no mutual signature supported", __func__);
1175 goto out;
1176 }
1177 debug3("%s: signing using %s", __func__, alg);
1178
1179 sshbuf_free(b);
1180 if ((b = sshbuf_new()) == NULL)
1181 fatal("%s: sshbuf_new failed", __func__);
1182 if (datafellows & SSH_OLD_SESSIONID) {
1183 if ((r = sshbuf_put(b, session_id2,
1184 session_id2_len)) != 0) {
1185 fatal("%s: sshbuf_put: %s",
1186 __func__, ssh_err(r));
1187 }
1188 } else {
1189 if ((r = sshbuf_put_string(b, session_id2,
1190 session_id2_len)) != 0) {
1191 fatal("%s: sshbuf_put_string: %s",
1192 __func__, ssh_err(r));
1193 }
1194 }
1195 skip = buffer_len(b);
1196 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1197 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1198 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1199 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1200 (r = sshbuf_put_u8(b, 1)) != 0 ||
1201 (r = sshbuf_put_cstring(b, alg)) != 0 ||
1202 (r = sshkey_puts(id->key, b)) != 0) {
1203 fatal("%s: assemble signed data: %s",
1204 __func__, ssh_err(r));
1205 }
1206
1207 /* generate signature */
1208 r = identity_sign(sign_id, &signature, &slen,
1209 sshbuf_ptr(b), sshbuf_len(b), datafellows, alg);
1210 if (r == 0)
1211 break;
1212 else if (r == SSH_ERR_KEY_NOT_FOUND)
1213 goto out; /* soft failure */
1214 else if (r == SSH_ERR_SIGN_ALG_UNSUPPORTED &&
1215 !fallback_sigtype) {
1216 if (sign_id->agent_fd != -1)
1217 loc = "agent ";
1218 else if ((sign_id->key->flags & SSHKEY_FLAG_EXT) != 0)
1219 loc = "token ";
1220 logit("%skey %s %s returned incorrect signature type",
1221 loc, sshkey_type(id->key), fp);
1222 continue;
1223 }
1224 error("%s: signing failed: %s", __func__, ssh_err(r));
1225 goto out;
Damien Millerad833b32000-08-23 10:46:23 +10001226 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001227 if (slen == 0 || signature == NULL) /* shouldn't happen */
1228 fatal("%s: no signature", __func__);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001229
Damien Millereba71ba2000-04-29 23:57:08 +10001230 /* append signature */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001231 if ((r = sshbuf_put_string(b, signature, slen)) != 0)
1232 fatal("%s: append signature: %s", __func__, ssh_err(r));
Damien Millereba71ba2000-04-29 23:57:08 +10001233
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001234#ifdef DEBUG_PK
1235 sshbuf_dump(b, stderr);
1236#endif
Damien Millereba71ba2000-04-29 23:57:08 +10001237 /* skip session id and packet type */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001238 if ((r = sshbuf_consume(b, skip + 1)) != 0)
1239 fatal("%s: consume: %s", __func__, ssh_err(r));
Damien Millereba71ba2000-04-29 23:57:08 +10001240
1241 /* put remaining data from buffer into packet */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001242 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1243 (r = sshpkt_putb(ssh, b)) != 0 ||
1244 (r = sshpkt_send(ssh)) != 0)
1245 fatal("%s: enqueue request: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +10001246
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001247 /* success */
1248 sent = 1;
1249
1250 out:
1251 free(fp);
1252 free(alg);
1253 sshbuf_free(b);
1254 freezero(signature, slen);
1255 return sent;
Damien Miller994cf142000-07-21 10:19:44 +10001256}
1257
Ben Lindstrombba81212001-06-25 05:01:22 +00001258static int
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001259send_pubkey_test(struct ssh *ssh, Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001260{
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001261 u_char *blob = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001262 u_int bloblen, have_sig = 0;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001263 char *alg = NULL;
1264 int sent = 0;
Damien Miller994cf142000-07-21 10:19:44 +10001265
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001266 if ((alg = key_sig_algorithm(ssh, id->key)) == NULL) {
1267 debug("%s: no mutual signature algorithm", __func__);
1268 goto out;
1269 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001270
Damien Miller280ecfb2003-05-14 13:46:00 +10001271 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001272 /* we cannot handle this key */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001273 debug3("%s: cannot handle key", __func__);
1274 goto out;
Damien Miller994cf142000-07-21 10:19:44 +10001275 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001276 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001277 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001278
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001279 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1280 packet_put_cstring(authctxt->server_user);
1281 packet_put_cstring(authctxt->service);
1282 packet_put_cstring(authctxt->method->name);
1283 packet_put_char(have_sig);
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001284 packet_put_cstring(alg);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001285 packet_put_string(blob, bloblen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001286 packet_send();
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001287 /* success */
1288 sent = 1;
1289out:
1290 free(alg);
1291 free(blob);
1292 return sent;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001293}
1294
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001295static struct sshkey *
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001296load_identity_file(Identity *id)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001297{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001298 struct sshkey *private = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001299 char prompt[300], *passphrase, *comment;
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001300 int r, perm_ok = 0, quit = 0, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001301 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001302
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001303 if (stat(id->filename, &st) < 0) {
1304 (id->userprovided ? logit : debug3)("no such identity: %s: %s",
1305 id->filename, strerror(errno));
Ben Lindstrom329782e2001-03-10 17:08:59 +00001306 return NULL;
1307 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001308 snprintf(prompt, sizeof prompt,
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001309 "Enter passphrase for key '%.100s': ", id->filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001310 for (i = 0; i <= options.number_of_password_prompts; i++) {
1311 if (i == 0)
1312 passphrase = "";
1313 else {
Damien Miller62cee002000-09-23 17:15:56 +11001314 passphrase = read_passphrase(prompt, 0);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001315 if (*passphrase == '\0') {
Damien Miller62cee002000-09-23 17:15:56 +11001316 debug2("no passphrase given, try next key");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001317 free(passphrase);
1318 break;
Damien Miller62cee002000-09-23 17:15:56 +11001319 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001320 }
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001321 switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
1322 passphrase, &private, &comment, &perm_ok))) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001323 case 0:
1324 break;
1325 case SSH_ERR_KEY_WRONG_PASSPHRASE:
1326 if (options.batch_mode) {
1327 quit = 1;
1328 break;
1329 }
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001330 if (i != 0)
1331 debug2("bad passphrase given, try again...");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001332 break;
1333 case SSH_ERR_SYSTEM_ERROR:
1334 if (errno == ENOENT) {
1335 debug2("Load key \"%s\": %s",
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001336 id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001337 quit = 1;
1338 break;
1339 }
1340 /* FALLTHROUGH */
1341 default:
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001342 error("Load key \"%s\": %s", id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001343 quit = 1;
1344 break;
1345 }
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001346 if (!quit && private != NULL && id->agent_fd == -1 &&
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001347 !(id->key && id->isprivate))
1348 maybe_add_key_to_agent(id->filename, private, comment,
1349 passphrase);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001350 if (i > 0) {
Damien Millera5103f42014-02-04 11:20:14 +11001351 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001352 free(passphrase);
Damien Miller62cee002000-09-23 17:15:56 +11001353 }
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001354 free(comment);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001355 if (private != NULL || quit)
1356 break;
Damien Miller994cf142000-07-21 10:19:44 +10001357 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001358 return private;
1359}
1360
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001361static int
1362key_type_allowed_by_config(struct sshkey *key)
1363{
1364 if (match_pattern_list(sshkey_ssh_name(key),
1365 options.pubkey_key_types, 0) == 1)
1366 return 1;
1367
1368 /* RSA keys/certs might be allowed by alternate signature types */
1369 switch (key->type) {
1370 case KEY_RSA:
1371 if (match_pattern_list("rsa-sha2-512",
1372 options.pubkey_key_types, 0) == 1)
1373 return 1;
1374 if (match_pattern_list("rsa-sha2-256",
1375 options.pubkey_key_types, 0) == 1)
1376 return 1;
1377 break;
1378 case KEY_RSA_CERT:
1379 if (match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
1380 options.pubkey_key_types, 0) == 1)
1381 return 1;
1382 if (match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
1383 options.pubkey_key_types, 0) == 1)
1384 return 1;
1385 break;
1386 }
1387 return 0;
1388}
1389
1390
Damien Miller280ecfb2003-05-14 13:46:00 +10001391/*
1392 * try keys in the following order:
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001393 * 1. certificates listed in the config file
1394 * 2. other input certificates
1395 * 3. agent keys that are found in the config file
1396 * 4. other agent keys
1397 * 5. keys that are only listed in the config file
Damien Miller280ecfb2003-05-14 13:46:00 +10001398 */
1399static void
1400pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001401{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001402 struct identity *id, *id2, *tmp;
1403 struct idlist agent, files, *preferred;
1404 struct sshkey *key;
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001405 int agent_fd = -1, i, r, found;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001406 size_t j;
1407 struct ssh_identitylist *idlist;
Damien Millerad833b32000-08-23 10:46:23 +10001408
Damien Miller280ecfb2003-05-14 13:46:00 +10001409 TAILQ_INIT(&agent); /* keys from the agent */
1410 TAILQ_INIT(&files); /* keys from the config file */
1411 preferred = &authctxt->keys;
1412 TAILQ_INIT(preferred); /* preferred order of keys */
1413
Damien Millercb6b68b2012-12-03 09:49:52 +11001414 /* list of keys stored in the filesystem and PKCS#11 */
Damien Miller280ecfb2003-05-14 13:46:00 +10001415 for (i = 0; i < options.num_identity_files; i++) {
1416 key = options.identity_keys[i];
Damien Miller0a80ca12010-02-27 07:55:05 +11001417 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1418 continue;
Damien Miller280ecfb2003-05-14 13:46:00 +10001419 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001420 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001421 id->agent_fd = -1;
Damien Miller280ecfb2003-05-14 13:46:00 +10001422 id->key = key;
1423 id->filename = xstrdup(options.identity_files[i]);
Darren Tucker19104782013-04-05 11:13:08 +11001424 id->userprovided = options.identity_file_userprovided[i];
Damien Miller280ecfb2003-05-14 13:46:00 +10001425 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001426 }
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001427 /* list of certificates specified by user */
1428 for (i = 0; i < options.num_certificate_files; i++) {
1429 key = options.certificates[i];
1430 if (!key_is_cert(key) || key->cert == NULL ||
1431 key->cert->type != SSH2_CERT_TYPE_USER)
1432 continue;
1433 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001434 id->agent_fd = -1;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001435 id->key = key;
1436 id->filename = xstrdup(options.certificate_files[i]);
1437 id->userprovided = options.certificate_file_userprovided[i];
1438 TAILQ_INSERT_TAIL(preferred, id, next);
1439 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001440 /* list of keys supported by the agent */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001441 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1442 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1443 debug("%s: ssh_get_authentication_socket: %s",
1444 __func__, ssh_err(r));
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +00001445 } else if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001446 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1447 debug("%s: ssh_fetch_identitylist: %s",
1448 __func__, ssh_err(r));
markus@openbsd.orgfc77ccd2016-01-14 22:56:56 +00001449 close(agent_fd);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001450 } else {
1451 for (j = 0; j < idlist->nkeys; j++) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001452 found = 0;
1453 TAILQ_FOREACH(id, &files, next) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001454 /*
1455 * agent keys from the config file are
1456 * preferred
1457 */
1458 if (sshkey_equal(idlist->keys[j], id->key)) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001459 TAILQ_REMOVE(&files, id, next);
1460 TAILQ_INSERT_TAIL(preferred, id, next);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001461 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001462 found = 1;
1463 break;
1464 }
1465 }
Damien Millerbd394c32004-03-08 23:12:36 +11001466 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001467 id = xcalloc(1, sizeof(*id));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001468 /* XXX "steals" key/comment from idlist */
1469 id->key = idlist->keys[j];
1470 id->filename = idlist->comments[j];
1471 idlist->keys[j] = NULL;
1472 idlist->comments[j] = NULL;
1473 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001474 TAILQ_INSERT_TAIL(&agent, id, next);
1475 }
1476 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001477 ssh_free_identitylist(idlist);
Damien Miller280ecfb2003-05-14 13:46:00 +10001478 /* append remaining agent keys */
1479 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1480 TAILQ_REMOVE(&agent, id, next);
1481 TAILQ_INSERT_TAIL(preferred, id, next);
1482 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001483 authctxt->agent_fd = agent_fd;
Damien Miller62cee002000-09-23 17:15:56 +11001484 }
djm@openbsd.org82f24c32016-05-23 23:30:50 +00001485 /* Prefer PKCS11 keys that are explicitly listed */
1486 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1487 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
1488 continue;
1489 found = 0;
1490 TAILQ_FOREACH(id2, &files, next) {
1491 if (id2->key == NULL ||
1492 (id2->key->flags & SSHKEY_FLAG_EXT) == 0)
1493 continue;
1494 if (sshkey_equal(id->key, id2->key)) {
1495 TAILQ_REMOVE(&files, id, next);
1496 TAILQ_INSERT_TAIL(preferred, id, next);
1497 found = 1;
1498 break;
1499 }
1500 }
1501 /* If IdentitiesOnly set and key not found then don't use it */
1502 if (!found && options.identities_only) {
1503 TAILQ_REMOVE(&files, id, next);
1504 explicit_bzero(id, sizeof(*id));
1505 free(id);
1506 }
1507 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001508 /* append remaining keys from the config file */
1509 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1510 TAILQ_REMOVE(&files, id, next);
1511 TAILQ_INSERT_TAIL(preferred, id, next);
1512 }
djm@openbsd.orge679c092015-10-13 16:15:21 +00001513 /* finally, filter by PubkeyAcceptedKeyTypes */
1514 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
djm@openbsd.orgd78b75d2018-07-03 13:07:58 +00001515 if (id->key != NULL && !key_type_allowed_by_config(id->key)) {
djm@openbsd.orge679c092015-10-13 16:15:21 +00001516 debug("Skipping %s key %s - "
1517 "not in PubkeyAcceptedKeyTypes",
1518 sshkey_ssh_name(id->key), id->filename);
1519 TAILQ_REMOVE(preferred, id, next);
1520 sshkey_free(id->key);
1521 free(id->filename);
1522 memset(id, 0, sizeof(*id));
1523 continue;
1524 }
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001525 debug2("key: %s (%p)%s%s", id->filename, id->key,
1526 id->userprovided ? ", explicit" : "",
1527 id->agent_fd != -1 ? ", agent" : "");
Damien Miller280ecfb2003-05-14 13:46:00 +10001528 }
1529}
1530
1531static void
1532pubkey_cleanup(Authctxt *authctxt)
1533{
1534 Identity *id;
1535
djm@openbsd.org141efe42015-01-14 20:05:27 +00001536 if (authctxt->agent_fd != -1)
1537 ssh_close_authentication_socket(authctxt->agent_fd);
Damien Miller280ecfb2003-05-14 13:46:00 +10001538 for (id = TAILQ_FIRST(&authctxt->keys); id;
1539 id = TAILQ_FIRST(&authctxt->keys)) {
1540 TAILQ_REMOVE(&authctxt->keys, id, next);
mmcc@openbsd.org89540b62015-12-11 02:31:47 +00001541 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +10001542 free(id->filename);
1543 free(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001544 }
Damien Millereba71ba2000-04-29 23:57:08 +10001545}
1546
djm@openbsd.orgb9844a42016-12-04 23:54:02 +00001547static void
1548pubkey_reset(Authctxt *authctxt)
1549{
1550 Identity *id;
1551
1552 TAILQ_FOREACH(id, &authctxt->keys, next)
1553 id->tried = 0;
1554}
1555
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001556static int
1557try_identity(Identity *id)
1558{
1559 if (!id->key)
1560 return (0);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001561 if (key_type_plain(id->key->type) == KEY_RSA &&
1562 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1563 debug("Skipped %s key %s for RSA/MD5 server",
1564 key_type(id->key), id->filename);
1565 return (0);
1566 }
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001567 return 1;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001568}
1569
Damien Miller62cee002000-09-23 17:15:56 +11001570int
1571userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001572{
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001573 struct ssh *ssh = active_state; /* XXX */
Damien Miller280ecfb2003-05-14 13:46:00 +10001574 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001575 int sent = 0;
djm@openbsd.org6830be92017-04-28 03:24:53 +00001576 char *fp;
Damien Millereba71ba2000-04-29 23:57:08 +10001577
Damien Miller280ecfb2003-05-14 13:46:00 +10001578 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1579 if (id->tried++)
1580 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001581 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001582 TAILQ_REMOVE(&authctxt->keys, id, next);
1583 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1584 /*
1585 * send a test message if we have the public key. for
1586 * encrypted keys we cannot do this and have to load the
1587 * private key instead
1588 */
Damien Miller324541e2013-12-31 12:25:40 +11001589 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001590 if (try_identity(id)) {
djm@openbsd.org6830be92017-04-28 03:24:53 +00001591 if ((fp = sshkey_fingerprint(id->key,
1592 options.fingerprint_hash,
1593 SSH_FP_DEFAULT)) == NULL) {
1594 error("%s: sshkey_fingerprint failed",
1595 __func__);
1596 return 0;
1597 }
1598 debug("Offering public key: %s %s %s",
1599 sshkey_type(id->key), fp, id->filename);
1600 free(fp);
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001601 sent = send_pubkey_test(ssh, authctxt, id);
Damien Miller324541e2013-12-31 12:25:40 +11001602 }
1603 } else {
Damien Miller280ecfb2003-05-14 13:46:00 +10001604 debug("Trying private key: %s", id->filename);
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001605 id->key = load_identity_file(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001606 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001607 if (try_identity(id)) {
1608 id->isprivate = 1;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001609 sent = sign_and_send_pubkey(ssh,
Damien Miller324541e2013-12-31 12:25:40 +11001610 authctxt, id);
1611 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001612 key_free(id->key);
1613 id->key = NULL;
djm@openbsd.orgb9844a42016-12-04 23:54:02 +00001614 id->isprivate = 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001615 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001616 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001617 if (sent)
1618 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001619 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001620 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001621}
Damien Millereba71ba2000-04-29 23:57:08 +10001622
Damien Miller874d77b2000-10-14 16:23:11 +11001623/*
1624 * Send userauth request message specifying keyboard-interactive method.
1625 */
1626int
1627userauth_kbdint(Authctxt *authctxt)
1628{
1629 static int attempt = 0;
1630
1631 if (attempt++ >= options.number_of_password_prompts)
1632 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001633 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1634 if (attempt > 1 && !authctxt->info_req_seen) {
1635 debug3("userauth_kbdint: disable: no info_req_seen");
1636 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1637 return 0;
1638 }
Damien Miller874d77b2000-10-14 16:23:11 +11001639
1640 debug2("userauth_kbdint");
1641 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1642 packet_put_cstring(authctxt->server_user);
1643 packet_put_cstring(authctxt->service);
1644 packet_put_cstring(authctxt->method->name);
1645 packet_put_cstring(""); /* lang */
1646 packet_put_cstring(options.kbd_interactive_devices ?
1647 options.kbd_interactive_devices : "");
1648 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001649
1650 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1651 return 1;
1652}
1653
1654/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001655 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001656 */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001657int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001658input_userauth_info_req(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller874d77b2000-10-14 16:23:11 +11001659{
markus@openbsd.org94583be2017-05-30 14:19:15 +00001660 Authctxt *authctxt = ssh->authctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001661 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001662 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001663 int echo = 0;
1664
1665 debug2("input_userauth_info_req");
1666
1667 if (authctxt == NULL)
1668 fatal("input_userauth_info_req: no authentication context");
1669
Ben Lindstrom7d199962001-09-12 18:29:00 +00001670 authctxt->info_req_seen = 1;
1671
Damien Miller874d77b2000-10-14 16:23:11 +11001672 name = packet_get_string(NULL);
1673 inst = packet_get_string(NULL);
1674 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001675 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001676 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001677 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001678 logit("%s", inst);
Darren Tuckera627d422013-06-02 07:31:17 +10001679 free(name);
1680 free(inst);
1681 free(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001682
1683 num_prompts = packet_get_int();
1684 /*
1685 * Begin to build info response packet based on prompts requested.
1686 * We commit to providing the correct number of responses, so if
1687 * further on we run into a problem that prevents this, we have to
1688 * be sure and clean this up and send a correct error response.
1689 */
1690 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1691 packet_put_int(num_prompts);
1692
Ben Lindstrom551ea372001-06-05 18:56:16 +00001693 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001694 for (i = 0; i < num_prompts; i++) {
1695 prompt = packet_get_string(NULL);
1696 echo = packet_get_char();
1697
Ben Lindstrom949974b2001-06-25 05:20:31 +00001698 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001699
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001700 packet_put_cstring(response);
Damien Millera5103f42014-02-04 11:20:14 +11001701 explicit_bzero(response, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +10001702 free(response);
1703 free(prompt);
Damien Miller874d77b2000-10-14 16:23:11 +11001704 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001705 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001706
Damien Miller9f643902001-11-12 11:02:52 +11001707 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001708 packet_send();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001709 return 0;
Damien Miller874d77b2000-10-14 16:23:11 +11001710}
Damien Miller62cee002000-09-23 17:15:56 +11001711
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001712static int
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001713ssh_keysign(struct sshkey *key, u_char **sigp, size_t *lenp,
1714 const u_char *data, size_t datalen)
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001715{
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001716 struct sshbuf *b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001717 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001718 pid_t pid;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001719 int i, r, to[2], from[2], status, sock = packet_get_connection_in();
1720 u_char rversion = 0, version = 2;
1721 void (*osigchld)(int);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001722
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001723 *sigp = NULL;
1724 *lenp = 0;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001725
Ben Lindstrom5206b952002-06-06 19:59:29 +00001726 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001727 error("%s: not installed: %s", __func__, strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001728 return -1;
1729 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001730 if (fflush(stdout) != 0) {
1731 error("%s: fflush: %s", __func__, strerror(errno));
1732 return -1;
1733 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001734 if (pipe(to) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001735 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001736 return -1;
1737 }
1738 if (pipe(from) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001739 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001740 return -1;
1741 }
1742 if ((pid = fork()) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001743 error("%s: fork: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001744 return -1;
1745 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001746 osigchld = signal(SIGCHLD, SIG_DFL);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001747 if (pid == 0) {
Darren Tucker6e7fe1c2010-01-08 17:07:22 +11001748 /* keep the socket on exec */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001749 fcntl(sock, F_SETFD, 0);
Damien Miller2e5fe882006-06-13 13:10:00 +10001750 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001751 close(from[0]);
1752 if (dup2(from[1], STDOUT_FILENO) < 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001753 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001754 close(to[1]);
1755 if (dup2(to[0], STDIN_FILENO) < 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001756 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001757 close(from[1]);
1758 close(to[0]);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001759 /* Close everything but stdio and the socket */
1760 for (i = STDERR_FILENO + 1; i < sock; i++)
1761 close(i);
1762 closefrom(sock + 1);
1763 debug3("%s: [child] pid=%ld, exec %s",
1764 __func__, (long)getpid(), _PATH_SSH_KEY_SIGN);
mmcc@openbsd.org94141b72015-12-11 00:20:04 +00001765 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001766 fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001767 strerror(errno));
1768 }
1769 close(from[1]);
1770 close(to[0]);
1771
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001772 if ((b = sshbuf_new()) == NULL)
1773 fatal("%s: sshbuf_new failed", __func__);
1774 /* send # of sock, data to be signed */
djm@openbsd.org326e2fa2017-01-30 00:32:28 +00001775 if ((r = sshbuf_put_u32(b, sock)) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001776 (r = sshbuf_put_string(b, data, datalen)) != 0)
1777 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1778 if (ssh_msg_send(to[1], version, b) == -1)
1779 fatal("%s: couldn't send request", __func__);
1780 sshbuf_reset(b);
1781 r = ssh_msg_recv(from[0], b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001782 close(from[0]);
1783 close(to[1]);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001784 if (r < 0) {
1785 error("%s: no reply", __func__);
1786 goto fail;
1787 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001788
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001789 errno = 0;
1790 while (waitpid(pid, &status, 0) < 0) {
1791 if (errno != EINTR) {
1792 error("%s: waitpid %ld: %s",
1793 __func__, (long)pid, strerror(errno));
1794 goto fail;
1795 }
1796 }
1797 if (!WIFEXITED(status)) {
1798 error("%s: exited abnormally", __func__);
1799 goto fail;
1800 }
1801 if (WEXITSTATUS(status) != 0) {
1802 error("%s: exited with status %d",
1803 __func__, WEXITSTATUS(status));
1804 goto fail;
1805 }
1806 if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
1807 error("%s: buffer error: %s", __func__, ssh_err(r));
1808 goto fail;
1809 }
1810 if (rversion != version) {
1811 error("%s: bad version", __func__);
1812 goto fail;
1813 }
1814 if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
1815 error("%s: buffer error: %s", __func__, ssh_err(r));
1816 fail:
1817 signal(SIGCHLD, osigchld);
1818 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001819 return -1;
1820 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001821 signal(SIGCHLD, osigchld);
1822 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001823
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001824 return 0;
1825}
1826
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001827int
1828userauth_hostbased(Authctxt *authctxt)
1829{
djm@openbsd.org4ba0d542018-07-03 11:39:54 +00001830 struct ssh *ssh = active_state; /* XXX */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001831 struct sshkey *private = NULL;
1832 struct sshbuf *b = NULL;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001833 u_char *sig = NULL, *keyblob = NULL;
1834 char *fp = NULL, *chost = NULL, *lname = NULL;
1835 size_t siglen = 0, keylen = 0;
1836 int i, r, success = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001837
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001838 if (authctxt->ktypes == NULL) {
1839 authctxt->oktypes = xstrdup(options.hostbased_key_types);
1840 authctxt->ktypes = authctxt->oktypes;
1841 }
djm@openbsd.org1195f4c2015-01-08 10:14:08 +00001842
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001843 /*
1844 * Work through each listed type pattern in HostbasedKeyTypes,
1845 * trying each hostkey that matches the type in turn.
1846 */
1847 for (;;) {
1848 if (authctxt->active_ktype == NULL)
1849 authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
1850 if (authctxt->active_ktype == NULL ||
1851 *authctxt->active_ktype == '\0')
1852 break;
1853 debug3("%s: trying key type %s", __func__,
1854 authctxt->active_ktype);
1855
1856 /* check for a useful key */
1857 private = NULL;
1858 for (i = 0; i < authctxt->sensitive->nkeys; i++) {
1859 if (authctxt->sensitive->keys[i] == NULL ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001860 authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
1861 continue;
1862 if (match_pattern_list(
1863 sshkey_ssh_name(authctxt->sensitive->keys[i]),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001864 authctxt->active_ktype, 0) != 1)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001865 continue;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001866 /* we take and free the key */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001867 private = authctxt->sensitive->keys[i];
1868 authctxt->sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001869 break;
1870 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001871 /* Found one */
1872 if (private != NULL)
1873 break;
1874 /* No more keys of this type; advance */
1875 authctxt->active_ktype = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001876 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001877 if (private == NULL) {
1878 free(authctxt->oktypes);
1879 authctxt->oktypes = authctxt->ktypes = NULL;
1880 authctxt->active_ktype = NULL;
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001881 debug("No more client hostkeys for hostbased authentication.");
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001882 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001883 }
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001884
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001885 if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
1886 SSH_FP_DEFAULT)) == NULL) {
1887 error("%s: sshkey_fingerprint failed", __func__);
1888 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001889 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001890 debug("%s: trying hostkey %s %s",
1891 __func__, sshkey_ssh_name(private), fp);
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001892
Damien Miller91c18472001-11-12 11:02:03 +11001893 /* figure out a name for the client host */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001894 if ((lname = get_local_name(packet_get_connection_in())) == NULL) {
1895 error("%s: cannot get local ipaddr/name", __func__);
1896 goto out;
Damien Miller91c18472001-11-12 11:02:03 +11001897 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001898
1899 /* XXX sshbuf_put_stringf? */
1900 xasprintf(&chost, "%s.", lname);
1901 debug2("%s: chost %s", __func__, chost);
Damien Miller91c18472001-11-12 11:02:03 +11001902
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001903 /* construct data */
1904 if ((b = sshbuf_new()) == NULL) {
1905 error("%s: sshbuf_new failed", __func__);
1906 goto out;
1907 }
1908 if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
1909 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
1910 goto out;
1911 }
1912 if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
1913 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1914 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001915 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001916 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1917 (r = sshbuf_put_cstring(b, key_ssh_name(private))) != 0 ||
1918 (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
1919 (r = sshbuf_put_cstring(b, chost)) != 0 ||
1920 (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
1921 error("%s: buffer error: %s", __func__, ssh_err(r));
1922 goto out;
1923 }
1924
1925#ifdef DEBUG_PK
1926 sshbuf_dump(b, stderr);
1927#endif
1928 if (authctxt->sensitive->external_keysign)
1929 r = ssh_keysign(private, &sig, &siglen,
1930 sshbuf_ptr(b), sshbuf_len(b));
1931 else if ((r = sshkey_sign(private, &sig, &siglen,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001932 sshbuf_ptr(b), sshbuf_len(b), NULL, datafellows)) != 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001933 debug("%s: sshkey_sign: %s", __func__, ssh_err(r));
1934 if (r != 0) {
1935 error("sign using hostkey %s %s failed",
1936 sshkey_ssh_name(private), fp);
1937 goto out;
1938 }
1939 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1940 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1941 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1942 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1943 (r = sshpkt_put_cstring(ssh, key_ssh_name(private))) != 0 ||
1944 (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
1945 (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
1946 (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
1947 (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
1948 (r = sshpkt_send(ssh)) != 0) {
1949 error("%s: packet error: %s", __func__, ssh_err(r));
1950 goto out;
1951 }
1952 success = 1;
1953
1954 out:
1955 if (sig != NULL) {
1956 explicit_bzero(sig, siglen);
1957 free(sig);
1958 }
1959 free(keyblob);
1960 free(lname);
1961 free(fp);
1962 free(chost);
1963 sshkey_free(private);
1964 sshbuf_free(b);
1965
1966 return success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001967}
1968
Damien Miller62cee002000-09-23 17:15:56 +11001969/* find auth method */
1970
Damien Miller62cee002000-09-23 17:15:56 +11001971/*
1972 * given auth method name, if configurable options permit this method fill
1973 * in auth_ident field and return true, otherwise return false.
1974 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001975static int
Damien Miller62cee002000-09-23 17:15:56 +11001976authmethod_is_enabled(Authmethod *method)
1977{
1978 if (method == NULL)
1979 return 0;
1980 /* return false if options indicate this method is disabled */
1981 if (method->enabled == NULL || *method->enabled == 0)
1982 return 0;
1983 /* return false if batch mode is enabled but method needs interactive mode */
1984 if (method->batch_flag != NULL && *method->batch_flag != 0)
1985 return 0;
1986 return 1;
1987}
1988
Ben Lindstrombba81212001-06-25 05:01:22 +00001989static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001990authmethod_lookup(const char *name)
1991{
1992 Authmethod *method = NULL;
1993 if (name != NULL)
1994 for (method = authmethods; method->name != NULL; method++)
1995 if (strcmp(name, method->name) == 0)
1996 return method;
1997 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1998 return NULL;
1999}
2000
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002001/* XXX internal state */
2002static Authmethod *current = NULL;
2003static char *supported = NULL;
2004static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00002005
Damien Miller62cee002000-09-23 17:15:56 +11002006/*
2007 * Given the authentication method list sent by the server, return the
2008 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00002009 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00002010 */
Ben Lindstrombba81212001-06-25 05:01:22 +00002011static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11002012authmethod_get(char *authlist)
2013{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002014 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00002015 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00002016
Damien Miller62cee002000-09-23 17:15:56 +11002017 /* Use a suitable default if we're passed a nil list. */
2018 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002019 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11002020
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002021 if (supported == NULL || strcmp(authlist, supported) != 0) {
2022 debug3("start over, passed a different list %s", authlist);
Darren Tuckera627d422013-06-02 07:31:17 +10002023 free(supported);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002024 supported = xstrdup(authlist);
2025 preferred = options.preferred_authentications;
2026 debug3("preferred %s", preferred);
2027 current = NULL;
2028 } else if (current != NULL && authmethod_is_enabled(current))
2029 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10002030
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002031 for (;;) {
2032 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00002033 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002034 current = NULL;
2035 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11002036 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002037 preferred += next;
2038 debug3("authmethod_lookup %s", name);
2039 debug3("remaining preferred: %s", preferred);
2040 if ((current = authmethod_lookup(name)) != NULL &&
2041 authmethod_is_enabled(current)) {
2042 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00002043 debug("Next authentication method: %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10002044 free(name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002045 return current;
2046 }
Darren Tuckere52a2602013-06-06 08:22:05 +10002047 free(name);
Damien Millereba71ba2000-04-29 23:57:08 +10002048 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002049}
Damien Miller62cee002000-09-23 17:15:56 +11002050
Ben Lindstrom79073822001-07-04 03:42:30 +00002051static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002052authmethods_get(void)
2053{
2054 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11002055 Buffer b;
2056 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002057
Damien Miller0e3b8722002-01-22 23:26:38 +11002058 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002059 for (method = authmethods; method->name != NULL; method++) {
2060 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11002061 if (buffer_len(&b) > 0)
2062 buffer_append(&b, ",", 1);
2063 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00002064 }
Damien Miller62cee002000-09-23 17:15:56 +11002065 }
djm@openbsd.org1a31d022016-05-02 08:49:03 +00002066 if ((list = sshbuf_dup_string(&b)) == NULL)
2067 fatal("%s: sshbuf_dup_string failed", __func__);
Damien Miller0e3b8722002-01-22 23:26:38 +11002068 buffer_free(&b);
2069 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10002070}
Damien Miller01ed2272008-11-05 16:20:46 +11002071