blob: be9397e481bd658f4ec55e616d56076864f2d750 [file] [log] [blame]
dtucker@openbsd.orga54eb272017-08-27 00:38:41 +00001/* $OpenBSD: sshconnect2.c,v 1.266 2017/08/27 00:38:41 dtucker Exp $ */
Damien Millereba71ba2000-04-29 23:57:08 +10002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Miller01ed2272008-11-05 16:20:46 +11004 * Copyright (c) 2008 Damien Miller. All rights reserved.
Damien Millereba71ba2000-04-29 23:57:08 +10005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100014 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
Damien Miller2eb63402006-03-15 11:09:42 +110028
Damien Miller9cf6d072006-03-15 11:29:24 +110029#include <sys/types.h>
Damien Millerd7834352006-08-05 12:39:39 +100030#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110031#include <sys/wait.h>
Damien Millerf17883e2006-03-15 11:45:54 +110032#include <sys/stat.h>
Damien Millereba71ba2000-04-29 23:57:08 +100033
Darren Tucker39972492006-07-12 22:22:46 +100034#include <errno.h>
Darren Tucker6e7fe1c2010-01-08 17:07:22 +110035#include <fcntl.h>
Darren Tuckerf520ea12007-05-20 15:11:33 +100036#include <netdb.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <pwd.h>
38#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100039#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100040#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100041#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100042#include <unistd.h>
Damien Miller63b4bcd2013-03-20 12:55:14 +110043#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
Damien Miller7ba0ca72008-07-17 18:57:06 +100044#include <vis.h>
Damien Miller2e28d862008-07-17 19:15:43 +100045#endif
Darren Tucker39972492006-07-12 22:22:46 +100046
Damien Miller9c617692003-05-14 14:31:11 +100047#include "openbsd-compat/sys-queue.h"
48
Damien Millerd7834352006-08-05 12:39:39 +100049#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100050#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "ssh2.h"
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 };
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000161 char *s;
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) {
181 if (kex_assemble_names(KEX_DEFAULT_PK_ALG,
182 &options.hostkeyalgorithms) != 0)
183 fatal("%s: kex_assemble_namelist", __func__);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100184 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100185 compat_pkalg_proposal(options.hostkeyalgorithms);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000186 } else {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000187 /* Enforce default */
188 options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG);
Damien Millerd925dcd2010-12-01 12:21:51 +1100189 /* Prefer algorithms that we already have keys for */
190 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100191 compat_pkalg_proposal(
192 order_hostkeyalgs(host, hostaddr, port));
Damien Millerd925dcd2010-12-01 12:21:51 +1100193 }
Damien Miller874d77b2000-10-14 16:23:11 +1100194
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000195 if (options.rekey_limit || options.rekey_interval)
dtucker@openbsd.orgc998bf02017-02-03 02:56:00 +0000196 packet_set_rekey_limits(options.rekey_limit,
197 options.rekey_interval);
Damien Millera5539d22003-04-09 20:50:06 +1000198
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000199 /* start key exchange */
markus@openbsd.org57e783c2015-01-20 20:16:21 +0000200 if ((r = kex_setup(active_state, myproposal)) != 0)
201 fatal("kex_setup: %s", ssh_err(r));
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000202 kex = active_state->kex;
Damien Miller1f0311c2014-05-15 14:24:09 +1000203#ifdef WITH_OPENSSL
Damien Miller8e7fb332003-02-24 12:03:03 +1100204 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000205 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +0000206 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_client;
207 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_client;
208 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100209 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100210 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100211# ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000212 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100213# endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000214#endif
Damien Miller1e124262013-11-04 08:26:52 +1100215 kex->kex[KEX_C25519_SHA256] = kexc25519_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000216 kex->client_version_string=client_version_string;
217 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000218 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100219
markus@openbsd.org92e9fe62017-05-31 07:00:13 +0000220 ssh_dispatch_run_fatal(active_state, DISPATCH_BLOCK, &kex->done);
Damien Miller874d77b2000-10-14 16:23:11 +1100221
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000222 /* remove ext-info from the KEX proposals for rekeying */
223 myproposal[PROPOSAL_KEX_ALGS] =
224 compat_kex_proposal(options.kex_algorithms);
225 if ((r = kex_prop2buf(kex->my, myproposal)) != 0)
226 fatal("kex_prop2buf: %s", ssh_err(r));
Darren Tucker36331b52010-01-08 16:50:41 +1100227
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000228 session_id2 = kex->session_id;
229 session_id2_len = kex->session_id_len;
230
Damien Miller874d77b2000-10-14 16:23:11 +1100231#ifdef DEBUG_KEXDH
232 /* send 1st encrypted/maced/compressed message */
233 packet_start(SSH2_MSG_IGNORE);
234 packet_put_cstring("markus");
235 packet_send();
236 packet_write_wait();
237#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000238}
Damien Millerb1715dc2000-05-30 13:44:51 +1000239
Damien Millereba71ba2000-04-29 23:57:08 +1000240/*
241 * Authenticate user
242 */
Damien Miller62cee002000-09-23 17:15:56 +1100243
djm@openbsd.org141efe42015-01-14 20:05:27 +0000244typedef struct cauthctxt Authctxt;
245typedef struct cauthmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000246typedef struct identity Identity;
247typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100248
Damien Miller280ecfb2003-05-14 13:46:00 +1000249struct identity {
250 TAILQ_ENTRY(identity) next;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000251 int agent_fd; /* >=0 if agent supports key */
252 struct sshkey *key; /* public/private key */
Damien Miller280ecfb2003-05-14 13:46:00 +1000253 char *filename; /* comment for agent-only keys */
254 int tried;
255 int isprivate; /* key points to the private key */
Damien Miller5ceddc32013-02-15 12:18:32 +1100256 int userprovided;
Damien Miller280ecfb2003-05-14 13:46:00 +1000257};
258TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100259
djm@openbsd.org141efe42015-01-14 20:05:27 +0000260struct cauthctxt {
Damien Miller62cee002000-09-23 17:15:56 +1100261 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000262 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100263 const char *host;
264 const char *service;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000265 struct cauthmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000266 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000267 char *authlist;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000268 int attempt;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000269 /* pubkey */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000270 struct idlist keys;
271 int agent_fd;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000272 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000273 Sensitive *sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000274 char *oktypes, *ktypes;
275 const char *active_ktype;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000276 /* kbd-interactive */
277 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000278 /* generic */
279 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100280};
djm@openbsd.org141efe42015-01-14 20:05:27 +0000281
282struct cauthmethod {
Damien Miller62cee002000-09-23 17:15:56 +1100283 char *name; /* string to compare against server's list */
284 int (*userauth)(Authctxt *authctxt);
Damien Miller01ed2272008-11-05 16:20:46 +1100285 void (*cleanup)(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100286 int *enabled; /* flag in option struct that enables method */
287 int *batch_flag; /* flag in option struct that disables method */
288};
289
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000290int input_userauth_service_accept(int, u_int32_t, struct ssh *);
291int input_userauth_ext_info(int, u_int32_t, struct ssh *);
292int input_userauth_success(int, u_int32_t, struct ssh *);
293int input_userauth_success_unexpected(int, u_int32_t, struct ssh *);
294int input_userauth_failure(int, u_int32_t, struct ssh *);
295int input_userauth_banner(int, u_int32_t, struct ssh *);
296int input_userauth_error(int, u_int32_t, struct ssh *);
297int input_userauth_info_req(int, u_int32_t, struct ssh *);
298int input_userauth_pk_ok(int, u_int32_t, struct ssh *);
299int input_userauth_passwd_changereq(int, u_int32_t, struct ssh *);
Damien Miller874d77b2000-10-14 16:23:11 +1100300
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000301int userauth_none(Authctxt *);
302int userauth_pubkey(Authctxt *);
303int userauth_passwd(Authctxt *);
304int userauth_kbdint(Authctxt *);
305int userauth_hostbased(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100306
Darren Tucker0efd1552003-08-26 11:49:55 +1000307#ifdef GSSAPI
308int userauth_gssapi(Authctxt *authctxt);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000309int input_gssapi_response(int type, u_int32_t, struct ssh *);
310int input_gssapi_token(int type, u_int32_t, struct ssh *);
311int input_gssapi_hash(int type, u_int32_t, struct ssh *);
312int input_gssapi_error(int, u_int32_t, struct ssh *);
313int input_gssapi_errtok(int, u_int32_t, struct ssh *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000314#endif
315
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000316void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000317
Damien Miller280ecfb2003-05-14 13:46:00 +1000318static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000319static void pubkey_prepare(Authctxt *);
320static void pubkey_cleanup(Authctxt *);
djm@openbsd.orgb9844a42016-12-04 23:54:02 +0000321static void pubkey_reset(Authctxt *);
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000322static struct sshkey *load_identity_file(Identity *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000323
Ben Lindstrombba81212001-06-25 05:01:22 +0000324static Authmethod *authmethod_get(char *authlist);
325static Authmethod *authmethod_lookup(const char *name);
326static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100327
328Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000329#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100330 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000331 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100332 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000333 &options.gss_authentication,
334 NULL},
335#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000336 {"hostbased",
337 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100338 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000339 &options.hostbased_authentication,
340 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000341 {"publickey",
342 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100343 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000344 &options.pubkey_authentication,
345 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100346 {"keyboard-interactive",
347 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100348 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100349 &options.kbd_interactive_authentication,
350 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000351 {"password",
352 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100353 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000354 &options.password_authentication,
355 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100356 {"none",
357 userauth_none,
358 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100359 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100360 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100361 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100362};
363
364void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000365ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000366 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100367{
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000368 struct ssh *ssh = active_state;
Damien Miller62cee002000-09-23 17:15:56 +1100369 Authctxt authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000370 int r;
Damien Miller62cee002000-09-23 17:15:56 +1100371
Ben Lindstrom551ea372001-06-05 18:56:16 +0000372 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000373 options.kbd_interactive_authentication = 1;
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000374 if (options.preferred_authentications == NULL)
375 options.preferred_authentications = authmethods_get();
376
Damien Miller62cee002000-09-23 17:15:56 +1100377 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000378 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000379 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100380 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000381 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100382 authctxt.host = host;
383 authctxt.service = "ssh-connection"; /* service name */
384 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100385 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000386 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000387 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000388 authctxt.sensitive = sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000389 authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000390 authctxt.info_req_seen = 0;
djm@openbsd.orgf14564c2015-01-15 11:04:36 +0000391 authctxt.agent_fd = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100392 if (authctxt.method == NULL)
393 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100394
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000395 if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
396 (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
397 (r = sshpkt_send(ssh)) != 0)
398 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller62cee002000-09-23 17:15:56 +1100399
markus@openbsd.org94583be2017-05-30 14:19:15 +0000400 ssh->authctxt = &authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000401 ssh_dispatch_init(ssh, &input_userauth_error);
402 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
403 ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
markus@openbsd.org92e9fe62017-05-31 07:00:13 +0000404 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt.success); /* loop until success */
markus@openbsd.org94583be2017-05-30 14:19:15 +0000405 ssh->authctxt = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100406
Damien Miller280ecfb2003-05-14 13:46:00 +1000407 pubkey_cleanup(&authctxt);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000408 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
Damien Millerf842fcb2003-05-15 12:01:28 +1000409
dtucker@openbsd.orgf31c6542016-09-22 02:29:57 +0000410 if (!authctxt.success)
411 fatal("Authentication failed.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000412 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100413}
Damien Millerf842fcb2003-05-15 12:01:28 +1000414
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000415/* ARGSUSED */
416int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000417input_userauth_service_accept(int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000418{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000419 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000420 int r;
421
422 if (ssh_packet_remaining(ssh) > 0) {
423 char *reply;
424
425 if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
426 goto out;
427 debug2("service_accept: %s", reply);
428 free(reply);
429 } else {
430 debug2("buggy server: service_accept w/o service");
431 }
432 if ((r = sshpkt_get_end(ssh)) != 0)
433 goto out;
434 debug("SSH2_MSG_SERVICE_ACCEPT received");
435
436 /* initial userauth request */
437 userauth_none(authctxt);
438
439 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error);
440 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
441 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
442 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
443 r = 0;
444 out:
445 return r;
446}
447
448/* ARGSUSED */
449int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000450input_userauth_ext_info(int type, u_int32_t seqnr, struct ssh *ssh)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000451{
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000452 return kex_input_ext_info(type, seqnr, ssh);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000453}
454
Damien Miller62cee002000-09-23 17:15:56 +1100455void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000456userauth(Authctxt *authctxt, char *authlist)
457{
Damien Miller01ed2272008-11-05 16:20:46 +1100458 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
459 authctxt->method->cleanup(authctxt);
460
Darren Tuckera627d422013-06-02 07:31:17 +1000461 free(authctxt->methoddata);
462 authctxt->methoddata = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000463 if (authlist == NULL) {
464 authlist = authctxt->authlist;
465 } else {
Darren Tuckera627d422013-06-02 07:31:17 +1000466 free(authctxt->authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000467 authctxt->authlist = authlist;
468 }
469 for (;;) {
470 Authmethod *method = authmethod_get(authlist);
471 if (method == NULL)
dtucker@openbsd.org4626e392017-06-14 00:31:38 +0000472 fatal("%s@%s: Permission denied (%s).",
473 authctxt->server_user, authctxt->host, authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000474 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000475
476 /* reset the per method handler */
477 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
478 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
479
480 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000481 if (method->userauth(authctxt) != 0) {
482 debug2("we sent a %s packet, wait for reply", method->name);
483 break;
484 } else {
485 debug2("we did not send a packet, disable method");
486 method->enabled = NULL;
487 }
488 }
489}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000490
Damien Millerf7475d72008-11-03 19:26:18 +1100491/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000492int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000493input_userauth_error(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100494{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000495 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000496 "type %d", type);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000497 return 0;
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000498}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000499
Damien Millerf7475d72008-11-03 19:26:18 +1100500/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000501int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000502input_userauth_banner(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000503{
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000504 char *msg, *lang;
Damien Miller7ba0ca72008-07-17 18:57:06 +1000505 u_int len;
Darren Tucker79644822003-10-08 17:37:58 +1000506
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000507 debug3("%s", __func__);
508 msg = packet_get_string(&len);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000509 lang = packet_get_string(NULL);
djm@openbsd.org65c6c6b2016-07-17 04:20:16 +0000510 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO)
511 fmprintf(stderr, "%s", msg);
512 free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000513 free(lang);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000514 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100515}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000516
Damien Millerf7475d72008-11-03 19:26:18 +1100517/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000518int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000519input_userauth_success(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100520{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000521 Authctxt *authctxt = ssh->authctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100522
Damien Miller62cee002000-09-23 17:15:56 +1100523 if (authctxt == NULL)
524 fatal("input_userauth_success: no authentication context");
Darren Tuckera627d422013-06-02 07:31:17 +1000525 free(authctxt->authlist);
526 authctxt->authlist = NULL;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100527 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
528 authctxt->method->cleanup(authctxt);
Darren Tuckera627d422013-06-02 07:31:17 +1000529 free(authctxt->methoddata);
530 authctxt->methoddata = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100531 authctxt->success = 1; /* break out */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000532 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100533}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000534
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000535int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000536input_userauth_success_unexpected(int type, u_int32_t seq, struct ssh *ssh)
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100537{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000538 Authctxt *authctxt = ssh->authctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100539
540 if (authctxt == NULL)
541 fatal("%s: no authentication context", __func__);
542
543 fatal("Unexpected authentication success during %s.",
544 authctxt->method->name);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000545 return 0;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100546}
547
Damien Millerf7475d72008-11-03 19:26:18 +1100548/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000549int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000550input_userauth_failure(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller62cee002000-09-23 17:15:56 +1100551{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000552 Authctxt *authctxt = ssh->authctxt;
Damien Miller62cee002000-09-23 17:15:56 +1100553 char *authlist = NULL;
554 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100555
556 if (authctxt == NULL)
557 fatal("input_userauth_failure: no authentication context");
558
Damien Miller874d77b2000-10-14 16:23:11 +1100559 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100560 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100561 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100562
Damien Miller62e9c4f2013-04-23 15:15:49 +1000563 if (partial != 0) {
dtucker@openbsd.org83cbca62016-07-22 05:46:11 +0000564 verbose("Authenticated with partial success.");
Damien Miller62e9c4f2013-04-23 15:15:49 +1000565 /* reset state */
djm@openbsd.orgb9844a42016-12-04 23:54:02 +0000566 pubkey_reset(authctxt);
Damien Miller62e9c4f2013-04-23 15:15:49 +1000567 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000568 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100569
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000570 userauth(authctxt, authlist);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000571 return 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000572}
Damien Millerf7475d72008-11-03 19:26:18 +1100573
574/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000575int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000576input_userauth_pk_ok(int type, u_int32_t seq, struct ssh *ssh)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000577{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000578 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000579 struct sshkey *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000580 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000581 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000582 int pktype, sent = 0;
583 u_int alen, blen;
584 char *pkalg, *fp;
585 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000586
587 if (authctxt == NULL)
588 fatal("input_userauth_pk_ok: no authentication context");
589 if (datafellows & SSH_BUG_PKOK) {
590 /* this is similar to SSH_BUG_PKAUTH */
591 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
592 pkblob = packet_get_string(&blen);
593 buffer_init(&b);
594 buffer_append(&b, pkblob, blen);
595 pkalg = buffer_get_string(&b, &alen);
596 buffer_free(&b);
597 } else {
598 pkalg = packet_get_string(&alen);
599 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000600 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100601 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000602
Damien Miller280ecfb2003-05-14 13:46:00 +1000603 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000604
Damien Miller280ecfb2003-05-14 13:46:00 +1000605 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
606 debug("unknown pkalg %s", pkalg);
607 goto done;
608 }
609 if ((key = key_from_blob(pkblob, blen)) == NULL) {
610 debug("no key from blob. pkalg %s", pkalg);
611 goto done;
612 }
613 if (key->type != pktype) {
614 error("input_userauth_pk_ok: type mismatch "
615 "for decoded key (received %d, expected %d)",
616 key->type, pktype);
617 goto done;
618 }
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000619 if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
620 SSH_FP_DEFAULT)) == NULL)
621 goto done;
Damien Miller280ecfb2003-05-14 13:46:00 +1000622 debug2("input_userauth_pk_ok: fp %s", fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000623 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000624
Darren Tuckerd05b6012003-10-15 15:55:59 +1000625 /*
626 * search keys in the reverse order, because last candidate has been
627 * moved to the end of the queue. this also avoids confusion by
628 * duplicate keys
629 */
Damien Miller0b51a522004-04-20 20:07:19 +1000630 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000631 if (key_equal(key, id->key)) {
632 sent = sign_and_send_pubkey(authctxt, id);
633 break;
634 }
635 }
636done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000637 if (key != NULL)
638 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000639 free(pkalg);
640 free(pkblob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000641
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000642 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000643 if (sent == 0)
644 userauth(authctxt, NULL);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000645 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100646}
647
Darren Tucker0efd1552003-08-26 11:49:55 +1000648#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100649int
Darren Tucker0efd1552003-08-26 11:49:55 +1000650userauth_gssapi(Authctxt *authctxt)
651{
652 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100653 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000654 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000655 OM_uint32 min;
656 int ok = 0;
657
658 /* Try one GSSAPI method at a time, rather than sending them all at
659 * once. */
660
Darren Tucker56afe142003-11-03 20:06:14 +1100661 if (gss_supported == NULL)
662 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000663
664 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100665 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000666 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100667 if (gss_supported->elements[mech].length < 128 &&
Damien Millera1cb9f32006-08-19 00:33:34 +1000668 ssh_gssapi_check_mechanism(&gssctxt,
669 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000670 ok = 1; /* Mechanism works */
671 } else {
672 mech++;
673 }
674 }
675
Damien Millera1cb9f32006-08-19 00:33:34 +1000676 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000677 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000678
679 authctxt->methoddata=(void *)gssctxt;
680
681 packet_start(SSH2_MSG_USERAUTH_REQUEST);
682 packet_put_cstring(authctxt->server_user);
683 packet_put_cstring(authctxt->service);
684 packet_put_cstring(authctxt->method->name);
685
686 packet_put_int(1);
687
Darren Tucker655a5e02003-11-03 20:09:03 +1100688 packet_put_int((gss_supported->elements[mech].length) + 2);
689 packet_put_char(SSH_GSS_OIDTYPE);
690 packet_put_char(gss_supported->elements[mech].length);
691 packet_put_raw(gss_supported->elements[mech].elements,
692 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000693
694 packet_send();
695
696 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
697 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
698 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
699 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
700
701 mech++; /* Move along to next candidate */
702
703 return 1;
704}
705
Damien Miller91c6aa42003-11-17 21:20:18 +1100706static OM_uint32
djm@openbsd.org39896b72017-05-31 05:08:46 +0000707process_gssapi_token(struct ssh *ssh, gss_buffer_t recv_tok)
Damien Miller91c6aa42003-11-17 21:20:18 +1100708{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000709 Authctxt *authctxt = ssh->authctxt;
Damien Miller91c6aa42003-11-17 21:20:18 +1100710 Gssctxt *gssctxt = authctxt->methoddata;
711 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000712 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
713 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100714 OM_uint32 status, ms, flags;
715 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100716
Damien Miller91c6aa42003-11-17 21:20:18 +1100717 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100718 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100719
720 if (send_tok.length > 0) {
721 if (GSS_ERROR(status))
722 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
723 else
724 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100725
Damien Miller91c6aa42003-11-17 21:20:18 +1100726 packet_put_string(send_tok.value, send_tok.length);
727 packet_send();
728 gss_release_buffer(&ms, &send_tok);
729 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100730
Damien Miller91c6aa42003-11-17 21:20:18 +1100731 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100732 /* send either complete or MIC, depending on mechanism */
733 if (!(flags & GSS_C_INTEG_FLAG)) {
734 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
735 packet_send();
736 } else {
737 ssh_gssapi_buildmic(&b, authctxt->server_user,
738 authctxt->service, "gssapi-with-mic");
739
740 gssbuf.value = buffer_ptr(&b);
741 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100742
Damien Miller0425d402003-11-17 22:18:21 +1100743 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100744
Damien Miller0425d402003-11-17 22:18:21 +1100745 if (!GSS_ERROR(status)) {
746 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
747 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100748
Damien Miller0425d402003-11-17 22:18:21 +1100749 packet_send();
750 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100751
Damien Miller0425d402003-11-17 22:18:21 +1100752 buffer_free(&b);
753 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100754 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100755 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100756
Damien Miller91c6aa42003-11-17 21:20:18 +1100757 return status;
758}
759
Damien Millerf7475d72008-11-03 19:26:18 +1100760/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000761int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000762input_gssapi_response(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000763{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000764 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000765 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000766 int oidlen;
767 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000768
769 if (authctxt == NULL)
770 fatal("input_gssapi_response: no authentication context");
771 gssctxt = authctxt->methoddata;
772
773 /* Setup our OID */
774 oidv = packet_get_string(&oidlen);
775
Darren Tucker655a5e02003-11-03 20:09:03 +1100776 if (oidlen <= 2 ||
777 oidv[0] != SSH_GSS_OIDTYPE ||
778 oidv[1] != oidlen - 2) {
Darren Tuckera627d422013-06-02 07:31:17 +1000779 free(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100780 debug("Badly encoded mechanism OID received");
781 userauth(authctxt, NULL);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000782 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000783 }
784
Darren Tucker655a5e02003-11-03 20:09:03 +1100785 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
786 fatal("Server returned different OID than expected");
787
Darren Tucker0efd1552003-08-26 11:49:55 +1000788 packet_check_eom();
789
Darren Tuckera627d422013-06-02 07:31:17 +1000790 free(oidv);
Darren Tucker0efd1552003-08-26 11:49:55 +1000791
djm@openbsd.org39896b72017-05-31 05:08:46 +0000792 if (GSS_ERROR(process_gssapi_token(ssh, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000793 /* Start again with next method on list */
794 debug("Trying to start again");
795 userauth(authctxt, NULL);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000796 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000797 }
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000798 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000799}
800
Damien Millerf7475d72008-11-03 19:26:18 +1100801/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000802int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000803input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000804{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000805 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000806 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100807 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000808 u_int slen;
809
810 if (authctxt == NULL)
811 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000812
813 recv_tok.value = packet_get_string(&slen);
814 recv_tok.length = slen; /* safe typecast */
815
816 packet_check_eom();
817
djm@openbsd.org39896b72017-05-31 05:08:46 +0000818 status = process_gssapi_token(ssh, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000819
Darren Tuckera627d422013-06-02 07:31:17 +1000820 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000821
822 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000823 /* Start again with the next method in the list */
824 userauth(authctxt, NULL);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000825 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000826 }
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000827 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000828}
829
Damien Millerf7475d72008-11-03 19:26:18 +1100830/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000831int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000832input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000833{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000834 Authctxt *authctxt = ssh->authctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000835 Gssctxt *gssctxt;
836 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
837 gss_buffer_desc recv_tok;
Damien Millerd677ad12013-04-23 15:18:51 +1000838 OM_uint32 ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000839 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000840
841 if (authctxt == NULL)
842 fatal("input_gssapi_response: no authentication context");
843 gssctxt = authctxt->methoddata;
844
Darren Tucker600ad8d2003-08-26 12:10:48 +1000845 recv_tok.value = packet_get_string(&len);
846 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000847
848 packet_check_eom();
849
850 /* Stick it into GSSAPI and see what it says */
Damien Millerd677ad12013-04-23 15:18:51 +1000851 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000852 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000853
Darren Tuckera627d422013-06-02 07:31:17 +1000854 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000855 gss_release_buffer(&ms, &send_tok);
856
857 /* Server will be returning a failed packet after this one */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000858 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000859}
860
Damien Millerf7475d72008-11-03 19:26:18 +1100861/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000862int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000863input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh)
Darren Tucker0efd1552003-08-26 11:49:55 +1000864{
Darren Tucker0efd1552003-08-26 11:49:55 +1000865 char *msg;
866 char *lang;
867
Damien Millerd677ad12013-04-23 15:18:51 +1000868 /* maj */(void)packet_get_int();
869 /* min */(void)packet_get_int();
Darren Tucker0efd1552003-08-26 11:49:55 +1000870 msg=packet_get_string(NULL);
871 lang=packet_get_string(NULL);
872
873 packet_check_eom();
874
Damien Miller15d72a02005-11-05 15:07:33 +1100875 debug("Server GSSAPI Error:\n%s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000876 free(msg);
877 free(lang);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000878 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000879}
880#endif /* GSSAPI */
881
Damien Millereba71ba2000-04-29 23:57:08 +1000882int
Damien Miller874d77b2000-10-14 16:23:11 +1100883userauth_none(Authctxt *authctxt)
884{
885 /* initial userauth request */
886 packet_start(SSH2_MSG_USERAUTH_REQUEST);
887 packet_put_cstring(authctxt->server_user);
888 packet_put_cstring(authctxt->service);
889 packet_put_cstring(authctxt->method->name);
890 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100891 return 1;
892}
893
894int
Damien Miller62cee002000-09-23 17:15:56 +1100895userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000896{
Damien Millere247cc42000-05-07 12:03:14 +1000897 static int attempt = 0;
dtucker@openbsd.orga54eb272017-08-27 00:38:41 +0000898 char prompt[256];
Damien Millereba71ba2000-04-29 23:57:08 +1000899 char *password;
Darren Tuckerab791692010-01-08 18:48:02 +1100900 const char *host = options.host_key_alias ? options.host_key_alias :
901 authctxt->host;
Damien Millereba71ba2000-04-29 23:57:08 +1000902
Damien Millerd3a18572000-06-07 19:55:44 +1000903 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000904 return 0;
905
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000906 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000907 error("Permission denied, please try again.");
908
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000909 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100910 authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +1000911 password = read_passphrase(prompt, 0);
912 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100913 packet_put_cstring(authctxt->server_user);
914 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100915 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000916 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000917 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100918 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000919 free(password);
Damien Miller9f643902001-11-12 11:02:52 +1100920 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000921 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000922
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000923 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000924 &input_userauth_passwd_changereq);
925
Damien Millereba71ba2000-04-29 23:57:08 +1000926 return 1;
927}
Damien Millerf7475d72008-11-03 19:26:18 +1100928
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000929/*
930 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
931 */
Damien Millerf7475d72008-11-03 19:26:18 +1100932/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000933int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +0000934input_userauth_passwd_changereq(int type, u_int32_t seqnr, struct ssh *ssh)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000935{
markus@openbsd.org94583be2017-05-30 14:19:15 +0000936 Authctxt *authctxt = ssh->authctxt;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000937 char *info, *lang, *password = NULL, *retype = NULL;
dtucker@openbsd.orga54eb272017-08-27 00:38:41 +0000938 char prompt[256];
djm@openbsd.org3e032a92017-01-30 00:32:03 +0000939 const char *host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000940
941 debug2("input_userauth_passwd_changereq");
942
943 if (authctxt == NULL)
944 fatal("input_userauth_passwd_changereq: "
945 "no authentication context");
djm@openbsd.org3e032a92017-01-30 00:32:03 +0000946 host = options.host_key_alias ? options.host_key_alias : authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000947
948 info = packet_get_string(NULL);
949 lang = packet_get_string(NULL);
950 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000951 logit("%s", info);
Darren Tuckera627d422013-06-02 07:31:17 +1000952 free(info);
953 free(lang);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000954 packet_start(SSH2_MSG_USERAUTH_REQUEST);
955 packet_put_cstring(authctxt->server_user);
956 packet_put_cstring(authctxt->service);
957 packet_put_cstring(authctxt->method->name);
958 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000959 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000960 "Enter %.30s@%.128s's old 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, 0);
963 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100964 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000965 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000966 password = NULL;
967 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000968 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000969 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100970 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000971 password = read_passphrase(prompt, RP_ALLOW_EOF);
972 if (password == NULL) {
973 /* bail out */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000974 return 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000975 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000976 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000977 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100978 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000979 retype = read_passphrase(prompt, 0);
980 if (strcmp(password, retype) != 0) {
Damien Millera5103f42014-02-04 11:20:14 +1100981 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000982 free(password);
Damien Miller996acd22003-04-09 20:59:48 +1000983 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000984 password = NULL;
985 }
Damien Millera5103f42014-02-04 11:20:14 +1100986 explicit_bzero(retype, strlen(retype));
Darren Tuckera627d422013-06-02 07:31:17 +1000987 free(retype);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000988 }
989 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100990 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000991 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000992 packet_add_padding(64);
993 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000994
995 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000996 &input_userauth_passwd_changereq);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000997 return 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000998}
Damien Millereba71ba2000-04-29 23:57:08 +1000999
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001000static const char *
djm@openbsd.org1e245522017-03-11 23:40:26 +00001001key_sign_encode(const struct sshkey *key)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001002{
1003 struct ssh *ssh = active_state;
1004
djm@openbsd.org1e245522017-03-11 23:40:26 +00001005 if (key->type == KEY_RSA) {
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001006 switch (ssh->kex->rsa_sha2) {
1007 case 256:
1008 return "rsa-sha2-256";
1009 case 512:
1010 return "rsa-sha2-512";
1011 }
1012 }
djm@openbsd.org1e245522017-03-11 23:40:26 +00001013 return key_ssh_name(key);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001014}
1015
Ben Lindstrombba81212001-06-25 05:01:22 +00001016static int
djm@openbsd.org141efe42015-01-14 20:05:27 +00001017identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
1018 const u_char *data, size_t datalen, u_int compat)
Damien Miller280ecfb2003-05-14 13:46:00 +10001019{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001020 struct sshkey *prv;
Damien Miller280ecfb2003-05-14 13:46:00 +10001021 int ret;
1022
1023 /* the agent supports this key */
djm@openbsd.org1e245522017-03-11 23:40:26 +00001024 if (id->key != NULL && id->agent_fd != -1)
djm@openbsd.org141efe42015-01-14 20:05:27 +00001025 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
djm@openbsd.org1e245522017-03-11 23:40:26 +00001026 data, datalen, key_sign_encode(id->key), compat);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001027
Damien Miller280ecfb2003-05-14 13:46:00 +10001028 /*
1029 * we have already loaded the private key or
1030 * the private key is stored in external hardware
1031 */
djm@openbsd.org1e245522017-03-11 23:40:26 +00001032 if (id->key != NULL &&
1033 (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT)))
1034 return (sshkey_sign(id->key, sigp, lenp, data, datalen,
1035 key_sign_encode(id->key), compat));
1036
Damien Miller280ecfb2003-05-14 13:46:00 +10001037 /* load the private key from the file */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001038 if ((prv = load_identity_file(id)) == NULL)
djm@openbsd.org0d1451a2016-02-23 01:34:14 +00001039 return SSH_ERR_KEY_NOT_FOUND;
djm@openbsd.orgc4972d02017-08-11 04:47:12 +00001040 if (id->key != NULL && !sshkey_equal_public(prv, id->key)) {
1041 error("%s: private key %s contents do not match public",
1042 __func__, id->filename);
1043 return SSH_ERR_KEY_NOT_FOUND;
1044 }
djm@openbsd.org1e245522017-03-11 23:40:26 +00001045 ret = sshkey_sign(prv, sigp, lenp, data, datalen,
1046 key_sign_encode(prv), compat);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001047 sshkey_free(prv);
Damien Miller280ecfb2003-05-14 13:46:00 +10001048 return (ret);
1049}
1050
1051static int
djm@openbsd.org1e245522017-03-11 23:40:26 +00001052id_filename_matches(Identity *id, Identity *private_id)
1053{
1054 const char *suffixes[] = { ".pub", "-cert.pub", NULL };
1055 size_t len = strlen(id->filename), plen = strlen(private_id->filename);
1056 size_t i, slen;
1057
1058 if (strcmp(id->filename, private_id->filename) == 0)
1059 return 1;
1060 for (i = 0; suffixes[i]; i++) {
1061 slen = strlen(suffixes[i]);
1062 if (len > slen && plen == len - slen &&
1063 strcmp(id->filename + (len - slen), suffixes[i]) == 0 &&
1064 memcmp(id->filename, private_id->filename, plen) == 0)
1065 return 1;
1066 }
1067 return 0;
1068}
1069
1070static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001071sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +10001072{
1073 Buffer b;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001074 Identity *private_id;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001075 u_char *blob, *signature;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001076 size_t slen;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001077 u_int bloblen, skip = 0;
1078 int matched, ret = -1, have_sig = 1;
Damien Miller4e270b02010-04-16 15:56:21 +10001079 char *fp;
Damien Millereba71ba2000-04-29 23:57:08 +10001080
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001081 if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1082 SSH_FP_DEFAULT)) == NULL)
1083 return 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001084 debug3("%s: %s %s", __func__, key_type(id->key), fp);
Darren Tuckera627d422013-06-02 07:31:17 +10001085 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001086
Damien Miller280ecfb2003-05-14 13:46:00 +10001087 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001088 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +00001089 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001090 return 0;
1091 }
Damien Millereba71ba2000-04-29 23:57:08 +10001092 /* data to be signed */
1093 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +11001094 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +10001095 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001096 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +11001097 } else {
1098 buffer_put_string(&b, session_id2, session_id2_len);
1099 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +10001100 }
Damien Millereba71ba2000-04-29 23:57:08 +10001101 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001102 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +10001103 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +00001104 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +10001105 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +11001106 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +00001107 if (datafellows & SSH_BUG_PKAUTH) {
1108 buffer_put_char(&b, have_sig);
1109 } else {
1110 buffer_put_cstring(&b, authctxt->method->name);
1111 buffer_put_char(&b, have_sig);
djm@openbsd.org1e245522017-03-11 23:40:26 +00001112 buffer_put_cstring(&b, key_sign_encode(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +00001113 }
Damien Millereba71ba2000-04-29 23:57:08 +10001114 buffer_put_string(&b, blob, bloblen);
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.org4e44a792015-09-24 06:15:11 +00001121 */
1122 if (key_is_cert(id->key)) {
1123 matched = 0;
1124 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1125 if (sshkey_equal_public(id->key, private_id->key) &&
1126 id->key->type != private_id->key->type) {
1127 id = private_id;
1128 matched = 1;
1129 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 */
1139 if (!matched && !id->isprivate && id->agent_fd == -1 &&
1140 (id->key->flags & SSHKEY_FLAG_EXT) == 0) {
1141 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1142 if (private_id->key == NULL &&
1143 id_filename_matches(id, private_id)) {
1144 id = private_id;
1145 matched = 1;
1146 break;
1147 }
1148 }
1149 }
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001150 if (matched) {
1151 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
Damien Millereba71ba2000-04-29 23:57:08 +10001160 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +10001161 ret = identity_sign(id, &signature, &slen,
djm@openbsd.org141efe42015-01-14 20:05:27 +00001162 buffer_ptr(&b), buffer_len(&b), datafellows);
1163 if (ret != 0) {
djm@openbsd.org0d1451a2016-02-23 01:34:14 +00001164 if (ret != SSH_ERR_KEY_NOT_FOUND)
1165 error("%s: signing failed: %s", __func__, ssh_err(ret));
Darren Tuckera627d422013-06-02 07:31:17 +10001166 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +10001167 buffer_free(&b);
1168 return 0;
1169 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001170#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +10001171 buffer_dump(&b);
1172#endif
Ben Lindstromd121f612000-12-03 17:00:47 +00001173 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +10001174 buffer_clear(&b);
1175 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001176 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +10001177 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001178 buffer_put_cstring(&b, authctxt->server_user);
1179 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +11001180 buffer_put_cstring(&b, authctxt->method->name);
1181 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +00001182 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001183 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +10001184 buffer_put_string(&b, blob, bloblen);
1185 }
Darren Tuckera627d422013-06-02 07:31:17 +10001186 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001187
Damien Millereba71ba2000-04-29 23:57:08 +10001188 /* append signature */
1189 buffer_put_string(&b, signature, slen);
Darren Tuckera627d422013-06-02 07:31:17 +10001190 free(signature);
Damien Millereba71ba2000-04-29 23:57:08 +10001191
1192 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +10001193 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +11001194 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +10001195 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +10001196
1197 /* put remaining data from buffer into packet */
1198 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1199 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1200 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +10001201 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +10001202
1203 return 1;
Damien Miller994cf142000-07-21 10:19:44 +10001204}
1205
Ben Lindstrombba81212001-06-25 05:01:22 +00001206static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001207send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001208{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001209 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001210 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +10001211
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001212 debug3("send_pubkey_test");
1213
Damien Miller280ecfb2003-05-14 13:46:00 +10001214 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001215 /* we cannot handle this key */
1216 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +10001217 return 0;
1218 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001219 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001220 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001221
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001222 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1223 packet_put_cstring(authctxt->server_user);
1224 packet_put_cstring(authctxt->service);
1225 packet_put_cstring(authctxt->method->name);
1226 packet_put_char(have_sig);
1227 if (!(datafellows & SSH_BUG_PKAUTH))
djm@openbsd.org1e245522017-03-11 23:40:26 +00001228 packet_put_cstring(key_sign_encode(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001229 packet_put_string(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001230 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001231 packet_send();
1232 return 1;
1233}
1234
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001235static struct sshkey *
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001236load_identity_file(Identity *id)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001237{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001238 struct sshkey *private = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001239 char prompt[300], *passphrase, *comment;
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001240 int r, perm_ok = 0, quit = 0, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001241 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001242
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001243 if (stat(id->filename, &st) < 0) {
1244 (id->userprovided ? logit : debug3)("no such identity: %s: %s",
1245 id->filename, strerror(errno));
Ben Lindstrom329782e2001-03-10 17:08:59 +00001246 return NULL;
1247 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001248 snprintf(prompt, sizeof prompt,
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001249 "Enter passphrase for key '%.100s': ", id->filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001250 for (i = 0; i <= options.number_of_password_prompts; i++) {
1251 if (i == 0)
1252 passphrase = "";
1253 else {
Damien Miller62cee002000-09-23 17:15:56 +11001254 passphrase = read_passphrase(prompt, 0);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001255 if (*passphrase == '\0') {
Damien Miller62cee002000-09-23 17:15:56 +11001256 debug2("no passphrase given, try next key");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001257 free(passphrase);
1258 break;
Damien Miller62cee002000-09-23 17:15:56 +11001259 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001260 }
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001261 switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
1262 passphrase, &private, &comment, &perm_ok))) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001263 case 0:
1264 break;
1265 case SSH_ERR_KEY_WRONG_PASSPHRASE:
1266 if (options.batch_mode) {
1267 quit = 1;
1268 break;
1269 }
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001270 if (i != 0)
1271 debug2("bad passphrase given, try again...");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001272 break;
1273 case SSH_ERR_SYSTEM_ERROR:
1274 if (errno == ENOENT) {
1275 debug2("Load key \"%s\": %s",
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001276 id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001277 quit = 1;
1278 break;
1279 }
1280 /* FALLTHROUGH */
1281 default:
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001282 error("Load key \"%s\": %s", id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001283 quit = 1;
1284 break;
1285 }
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001286 if (!quit && private != NULL && id->agent_fd == -1 &&
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001287 !(id->key && id->isprivate))
1288 maybe_add_key_to_agent(id->filename, private, comment,
1289 passphrase);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001290 if (i > 0) {
Damien Millera5103f42014-02-04 11:20:14 +11001291 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001292 free(passphrase);
Damien Miller62cee002000-09-23 17:15:56 +11001293 }
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001294 free(comment);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001295 if (private != NULL || quit)
1296 break;
Damien Miller994cf142000-07-21 10:19:44 +10001297 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001298 return private;
1299}
1300
Damien Miller280ecfb2003-05-14 13:46:00 +10001301/*
1302 * try keys in the following order:
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001303 * 1. certificates listed in the config file
1304 * 2. other input certificates
1305 * 3. agent keys that are found in the config file
1306 * 4. other agent keys
1307 * 5. keys that are only listed in the config file
Damien Miller280ecfb2003-05-14 13:46:00 +10001308 */
1309static void
1310pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001311{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001312 struct identity *id, *id2, *tmp;
1313 struct idlist agent, files, *preferred;
1314 struct sshkey *key;
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001315 int agent_fd = -1, i, r, found;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001316 size_t j;
1317 struct ssh_identitylist *idlist;
Damien Millerad833b32000-08-23 10:46:23 +10001318
Damien Miller280ecfb2003-05-14 13:46:00 +10001319 TAILQ_INIT(&agent); /* keys from the agent */
1320 TAILQ_INIT(&files); /* keys from the config file */
1321 preferred = &authctxt->keys;
1322 TAILQ_INIT(preferred); /* preferred order of keys */
1323
Damien Millercb6b68b2012-12-03 09:49:52 +11001324 /* list of keys stored in the filesystem and PKCS#11 */
Damien Miller280ecfb2003-05-14 13:46:00 +10001325 for (i = 0; i < options.num_identity_files; i++) {
1326 key = options.identity_keys[i];
Damien Miller0a80ca12010-02-27 07:55:05 +11001327 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1328 continue;
Damien Miller280ecfb2003-05-14 13:46:00 +10001329 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001330 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001331 id->agent_fd = -1;
Damien Miller280ecfb2003-05-14 13:46:00 +10001332 id->key = key;
1333 id->filename = xstrdup(options.identity_files[i]);
Darren Tucker19104782013-04-05 11:13:08 +11001334 id->userprovided = options.identity_file_userprovided[i];
Damien Miller280ecfb2003-05-14 13:46:00 +10001335 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001336 }
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001337 /* list of certificates specified by user */
1338 for (i = 0; i < options.num_certificate_files; i++) {
1339 key = options.certificates[i];
1340 if (!key_is_cert(key) || key->cert == NULL ||
1341 key->cert->type != SSH2_CERT_TYPE_USER)
1342 continue;
1343 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001344 id->agent_fd = -1;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001345 id->key = key;
1346 id->filename = xstrdup(options.certificate_files[i]);
1347 id->userprovided = options.certificate_file_userprovided[i];
1348 TAILQ_INSERT_TAIL(preferred, id, next);
1349 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001350 /* list of keys supported by the agent */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001351 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1352 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1353 debug("%s: ssh_get_authentication_socket: %s",
1354 __func__, ssh_err(r));
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +00001355 } else if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001356 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1357 debug("%s: ssh_fetch_identitylist: %s",
1358 __func__, ssh_err(r));
markus@openbsd.orgfc77ccd2016-01-14 22:56:56 +00001359 close(agent_fd);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001360 } else {
1361 for (j = 0; j < idlist->nkeys; j++) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001362 found = 0;
1363 TAILQ_FOREACH(id, &files, next) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001364 /*
1365 * agent keys from the config file are
1366 * preferred
1367 */
1368 if (sshkey_equal(idlist->keys[j], id->key)) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001369 TAILQ_REMOVE(&files, id, next);
1370 TAILQ_INSERT_TAIL(preferred, id, next);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001371 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001372 found = 1;
1373 break;
1374 }
1375 }
Damien Millerbd394c32004-03-08 23:12:36 +11001376 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001377 id = xcalloc(1, sizeof(*id));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001378 /* XXX "steals" key/comment from idlist */
1379 id->key = idlist->keys[j];
1380 id->filename = idlist->comments[j];
1381 idlist->keys[j] = NULL;
1382 idlist->comments[j] = NULL;
1383 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001384 TAILQ_INSERT_TAIL(&agent, id, next);
1385 }
1386 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001387 ssh_free_identitylist(idlist);
Damien Miller280ecfb2003-05-14 13:46:00 +10001388 /* append remaining agent keys */
1389 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1390 TAILQ_REMOVE(&agent, id, next);
1391 TAILQ_INSERT_TAIL(preferred, id, next);
1392 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001393 authctxt->agent_fd = agent_fd;
Damien Miller62cee002000-09-23 17:15:56 +11001394 }
djm@openbsd.org82f24c32016-05-23 23:30:50 +00001395 /* Prefer PKCS11 keys that are explicitly listed */
1396 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1397 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
1398 continue;
1399 found = 0;
1400 TAILQ_FOREACH(id2, &files, next) {
1401 if (id2->key == NULL ||
1402 (id2->key->flags & SSHKEY_FLAG_EXT) == 0)
1403 continue;
1404 if (sshkey_equal(id->key, id2->key)) {
1405 TAILQ_REMOVE(&files, id, next);
1406 TAILQ_INSERT_TAIL(preferred, id, next);
1407 found = 1;
1408 break;
1409 }
1410 }
1411 /* If IdentitiesOnly set and key not found then don't use it */
1412 if (!found && options.identities_only) {
1413 TAILQ_REMOVE(&files, id, next);
1414 explicit_bzero(id, sizeof(*id));
1415 free(id);
1416 }
1417 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001418 /* append remaining keys from the config file */
1419 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1420 TAILQ_REMOVE(&files, id, next);
1421 TAILQ_INSERT_TAIL(preferred, id, next);
1422 }
djm@openbsd.orge679c092015-10-13 16:15:21 +00001423 /* finally, filter by PubkeyAcceptedKeyTypes */
1424 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1425 if (id->key != NULL &&
1426 match_pattern_list(sshkey_ssh_name(id->key),
1427 options.pubkey_key_types, 0) != 1) {
1428 debug("Skipping %s key %s - "
1429 "not in PubkeyAcceptedKeyTypes",
1430 sshkey_ssh_name(id->key), id->filename);
1431 TAILQ_REMOVE(preferred, id, next);
1432 sshkey_free(id->key);
1433 free(id->filename);
1434 memset(id, 0, sizeof(*id));
1435 continue;
1436 }
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001437 debug2("key: %s (%p)%s%s", id->filename, id->key,
1438 id->userprovided ? ", explicit" : "",
1439 id->agent_fd != -1 ? ", agent" : "");
Damien Miller280ecfb2003-05-14 13:46:00 +10001440 }
1441}
1442
1443static void
1444pubkey_cleanup(Authctxt *authctxt)
1445{
1446 Identity *id;
1447
djm@openbsd.org141efe42015-01-14 20:05:27 +00001448 if (authctxt->agent_fd != -1)
1449 ssh_close_authentication_socket(authctxt->agent_fd);
Damien Miller280ecfb2003-05-14 13:46:00 +10001450 for (id = TAILQ_FIRST(&authctxt->keys); id;
1451 id = TAILQ_FIRST(&authctxt->keys)) {
1452 TAILQ_REMOVE(&authctxt->keys, id, next);
mmcc@openbsd.org89540b62015-12-11 02:31:47 +00001453 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +10001454 free(id->filename);
1455 free(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001456 }
Damien Millereba71ba2000-04-29 23:57:08 +10001457}
1458
djm@openbsd.orgb9844a42016-12-04 23:54:02 +00001459static void
1460pubkey_reset(Authctxt *authctxt)
1461{
1462 Identity *id;
1463
1464 TAILQ_FOREACH(id, &authctxt->keys, next)
1465 id->tried = 0;
1466}
1467
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001468static int
1469try_identity(Identity *id)
1470{
1471 if (!id->key)
1472 return (0);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001473 if (key_type_plain(id->key->type) == KEY_RSA &&
1474 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1475 debug("Skipped %s key %s for RSA/MD5 server",
1476 key_type(id->key), id->filename);
1477 return (0);
1478 }
djm@openbsd.org873d3e72017-04-30 23:18:44 +00001479 return 1;
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001480}
1481
Damien Miller62cee002000-09-23 17:15:56 +11001482int
1483userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001484{
Damien Miller280ecfb2003-05-14 13:46:00 +10001485 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001486 int sent = 0;
djm@openbsd.org6830be92017-04-28 03:24:53 +00001487 char *fp;
Damien Millereba71ba2000-04-29 23:57:08 +10001488
Damien Miller280ecfb2003-05-14 13:46:00 +10001489 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1490 if (id->tried++)
1491 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001492 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001493 TAILQ_REMOVE(&authctxt->keys, id, next);
1494 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1495 /*
1496 * send a test message if we have the public key. for
1497 * encrypted keys we cannot do this and have to load the
1498 * private key instead
1499 */
Damien Miller324541e2013-12-31 12:25:40 +11001500 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001501 if (try_identity(id)) {
djm@openbsd.org6830be92017-04-28 03:24:53 +00001502 if ((fp = sshkey_fingerprint(id->key,
1503 options.fingerprint_hash,
1504 SSH_FP_DEFAULT)) == NULL) {
1505 error("%s: sshkey_fingerprint failed",
1506 __func__);
1507 return 0;
1508 }
1509 debug("Offering public key: %s %s %s",
1510 sshkey_type(id->key), fp, id->filename);
1511 free(fp);
Damien Miller324541e2013-12-31 12:25:40 +11001512 sent = send_pubkey_test(authctxt, id);
1513 }
1514 } else {
Damien Miller280ecfb2003-05-14 13:46:00 +10001515 debug("Trying private key: %s", id->filename);
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001516 id->key = load_identity_file(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001517 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001518 if (try_identity(id)) {
1519 id->isprivate = 1;
Damien Miller324541e2013-12-31 12:25:40 +11001520 sent = sign_and_send_pubkey(
1521 authctxt, id);
1522 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001523 key_free(id->key);
1524 id->key = NULL;
djm@openbsd.orgb9844a42016-12-04 23:54:02 +00001525 id->isprivate = 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001526 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001527 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001528 if (sent)
1529 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001530 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001531 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001532}
Damien Millereba71ba2000-04-29 23:57:08 +10001533
Damien Miller874d77b2000-10-14 16:23:11 +11001534/*
1535 * Send userauth request message specifying keyboard-interactive method.
1536 */
1537int
1538userauth_kbdint(Authctxt *authctxt)
1539{
1540 static int attempt = 0;
1541
1542 if (attempt++ >= options.number_of_password_prompts)
1543 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001544 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1545 if (attempt > 1 && !authctxt->info_req_seen) {
1546 debug3("userauth_kbdint: disable: no info_req_seen");
1547 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1548 return 0;
1549 }
Damien Miller874d77b2000-10-14 16:23:11 +11001550
1551 debug2("userauth_kbdint");
1552 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1553 packet_put_cstring(authctxt->server_user);
1554 packet_put_cstring(authctxt->service);
1555 packet_put_cstring(authctxt->method->name);
1556 packet_put_cstring(""); /* lang */
1557 packet_put_cstring(options.kbd_interactive_devices ?
1558 options.kbd_interactive_devices : "");
1559 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001560
1561 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1562 return 1;
1563}
1564
1565/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001566 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001567 */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001568int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00001569input_userauth_info_req(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller874d77b2000-10-14 16:23:11 +11001570{
markus@openbsd.org94583be2017-05-30 14:19:15 +00001571 Authctxt *authctxt = ssh->authctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001572 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001573 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001574 int echo = 0;
1575
1576 debug2("input_userauth_info_req");
1577
1578 if (authctxt == NULL)
1579 fatal("input_userauth_info_req: no authentication context");
1580
Ben Lindstrom7d199962001-09-12 18:29:00 +00001581 authctxt->info_req_seen = 1;
1582
Damien Miller874d77b2000-10-14 16:23:11 +11001583 name = packet_get_string(NULL);
1584 inst = packet_get_string(NULL);
1585 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001586 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001587 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001588 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001589 logit("%s", inst);
Darren Tuckera627d422013-06-02 07:31:17 +10001590 free(name);
1591 free(inst);
1592 free(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001593
1594 num_prompts = packet_get_int();
1595 /*
1596 * Begin to build info response packet based on prompts requested.
1597 * We commit to providing the correct number of responses, so if
1598 * further on we run into a problem that prevents this, we have to
1599 * be sure and clean this up and send a correct error response.
1600 */
1601 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1602 packet_put_int(num_prompts);
1603
Ben Lindstrom551ea372001-06-05 18:56:16 +00001604 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001605 for (i = 0; i < num_prompts; i++) {
1606 prompt = packet_get_string(NULL);
1607 echo = packet_get_char();
1608
Ben Lindstrom949974b2001-06-25 05:20:31 +00001609 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001610
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001611 packet_put_cstring(response);
Damien Millera5103f42014-02-04 11:20:14 +11001612 explicit_bzero(response, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +10001613 free(response);
1614 free(prompt);
Damien Miller874d77b2000-10-14 16:23:11 +11001615 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001616 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001617
Damien Miller9f643902001-11-12 11:02:52 +11001618 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001619 packet_send();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001620 return 0;
Damien Miller874d77b2000-10-14 16:23:11 +11001621}
Damien Miller62cee002000-09-23 17:15:56 +11001622
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001623static int
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001624ssh_keysign(struct sshkey *key, u_char **sigp, size_t *lenp,
1625 const u_char *data, size_t datalen)
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001626{
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001627 struct sshbuf *b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001628 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001629 pid_t pid;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001630 int i, r, to[2], from[2], status, sock = packet_get_connection_in();
1631 u_char rversion = 0, version = 2;
1632 void (*osigchld)(int);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001633
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001634 *sigp = NULL;
1635 *lenp = 0;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001636
Ben Lindstrom5206b952002-06-06 19:59:29 +00001637 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001638 error("%s: not installed: %s", __func__, strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001639 return -1;
1640 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001641 if (fflush(stdout) != 0) {
1642 error("%s: fflush: %s", __func__, strerror(errno));
1643 return -1;
1644 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001645 if (pipe(to) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001646 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001647 return -1;
1648 }
1649 if (pipe(from) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001650 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001651 return -1;
1652 }
1653 if ((pid = fork()) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001654 error("%s: fork: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001655 return -1;
1656 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001657 osigchld = signal(SIGCHLD, SIG_DFL);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001658 if (pid == 0) {
Darren Tucker6e7fe1c2010-01-08 17:07:22 +11001659 /* keep the socket on exec */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001660 fcntl(sock, F_SETFD, 0);
Damien Miller2e5fe882006-06-13 13:10:00 +10001661 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001662 close(from[0]);
1663 if (dup2(from[1], STDOUT_FILENO) < 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001664 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001665 close(to[1]);
1666 if (dup2(to[0], STDIN_FILENO) < 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001667 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001668 close(from[1]);
1669 close(to[0]);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001670 /* Close everything but stdio and the socket */
1671 for (i = STDERR_FILENO + 1; i < sock; i++)
1672 close(i);
1673 closefrom(sock + 1);
1674 debug3("%s: [child] pid=%ld, exec %s",
1675 __func__, (long)getpid(), _PATH_SSH_KEY_SIGN);
mmcc@openbsd.org94141b72015-12-11 00:20:04 +00001676 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001677 fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001678 strerror(errno));
1679 }
1680 close(from[1]);
1681 close(to[0]);
1682
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001683 if ((b = sshbuf_new()) == NULL)
1684 fatal("%s: sshbuf_new failed", __func__);
1685 /* send # of sock, data to be signed */
djm@openbsd.org326e2fa2017-01-30 00:32:28 +00001686 if ((r = sshbuf_put_u32(b, sock)) != 0 ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001687 (r = sshbuf_put_string(b, data, datalen)) != 0)
1688 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1689 if (ssh_msg_send(to[1], version, b) == -1)
1690 fatal("%s: couldn't send request", __func__);
1691 sshbuf_reset(b);
1692 r = ssh_msg_recv(from[0], b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001693 close(from[0]);
1694 close(to[1]);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001695 if (r < 0) {
1696 error("%s: no reply", __func__);
1697 goto fail;
1698 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001699
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001700 errno = 0;
1701 while (waitpid(pid, &status, 0) < 0) {
1702 if (errno != EINTR) {
1703 error("%s: waitpid %ld: %s",
1704 __func__, (long)pid, strerror(errno));
1705 goto fail;
1706 }
1707 }
1708 if (!WIFEXITED(status)) {
1709 error("%s: exited abnormally", __func__);
1710 goto fail;
1711 }
1712 if (WEXITSTATUS(status) != 0) {
1713 error("%s: exited with status %d",
1714 __func__, WEXITSTATUS(status));
1715 goto fail;
1716 }
1717 if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
1718 error("%s: buffer error: %s", __func__, ssh_err(r));
1719 goto fail;
1720 }
1721 if (rversion != version) {
1722 error("%s: bad version", __func__);
1723 goto fail;
1724 }
1725 if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
1726 error("%s: buffer error: %s", __func__, ssh_err(r));
1727 fail:
1728 signal(SIGCHLD, osigchld);
1729 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001730 return -1;
1731 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001732 signal(SIGCHLD, osigchld);
1733 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001734
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001735 return 0;
1736}
1737
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001738int
1739userauth_hostbased(Authctxt *authctxt)
1740{
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001741 struct ssh *ssh = active_state;
1742 struct sshkey *private = NULL;
1743 struct sshbuf *b = NULL;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001744 const char *service;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001745 u_char *sig = NULL, *keyblob = NULL;
1746 char *fp = NULL, *chost = NULL, *lname = NULL;
1747 size_t siglen = 0, keylen = 0;
1748 int i, r, success = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001749
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001750 if (authctxt->ktypes == NULL) {
1751 authctxt->oktypes = xstrdup(options.hostbased_key_types);
1752 authctxt->ktypes = authctxt->oktypes;
1753 }
djm@openbsd.org1195f4c2015-01-08 10:14:08 +00001754
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001755 /*
1756 * Work through each listed type pattern in HostbasedKeyTypes,
1757 * trying each hostkey that matches the type in turn.
1758 */
1759 for (;;) {
1760 if (authctxt->active_ktype == NULL)
1761 authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
1762 if (authctxt->active_ktype == NULL ||
1763 *authctxt->active_ktype == '\0')
1764 break;
1765 debug3("%s: trying key type %s", __func__,
1766 authctxt->active_ktype);
1767
1768 /* check for a useful key */
1769 private = NULL;
1770 for (i = 0; i < authctxt->sensitive->nkeys; i++) {
1771 if (authctxt->sensitive->keys[i] == NULL ||
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001772 authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
1773 continue;
1774 if (match_pattern_list(
1775 sshkey_ssh_name(authctxt->sensitive->keys[i]),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001776 authctxt->active_ktype, 0) != 1)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001777 continue;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001778 /* we take and free the key */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001779 private = authctxt->sensitive->keys[i];
1780 authctxt->sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001781 break;
1782 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001783 /* Found one */
1784 if (private != NULL)
1785 break;
1786 /* No more keys of this type; advance */
1787 authctxt->active_ktype = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001788 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001789 if (private == NULL) {
1790 free(authctxt->oktypes);
1791 authctxt->oktypes = authctxt->ktypes = NULL;
1792 authctxt->active_ktype = NULL;
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001793 debug("No more client hostkeys for hostbased authentication.");
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001794 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001795 }
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001796
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001797 if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
1798 SSH_FP_DEFAULT)) == NULL) {
1799 error("%s: sshkey_fingerprint failed", __func__);
1800 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001801 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001802 debug("%s: trying hostkey %s %s",
1803 __func__, sshkey_ssh_name(private), fp);
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001804
Damien Miller91c18472001-11-12 11:02:03 +11001805 /* figure out a name for the client host */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001806 if ((lname = get_local_name(packet_get_connection_in())) == NULL) {
1807 error("%s: cannot get local ipaddr/name", __func__);
1808 goto out;
Damien Miller91c18472001-11-12 11:02:03 +11001809 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001810
1811 /* XXX sshbuf_put_stringf? */
1812 xasprintf(&chost, "%s.", lname);
1813 debug2("%s: chost %s", __func__, chost);
Damien Miller91c18472001-11-12 11:02:03 +11001814
Ben Lindstrom671388f2001-04-19 20:40:45 +00001815 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1816 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001817
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001818 /* construct data */
1819 if ((b = sshbuf_new()) == NULL) {
1820 error("%s: sshbuf_new failed", __func__);
1821 goto out;
1822 }
1823 if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
1824 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
1825 goto out;
1826 }
1827 if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
1828 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1829 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1830 (r = sshbuf_put_cstring(b, service)) != 0 ||
1831 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1832 (r = sshbuf_put_cstring(b, key_ssh_name(private))) != 0 ||
1833 (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
1834 (r = sshbuf_put_cstring(b, chost)) != 0 ||
1835 (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
1836 error("%s: buffer error: %s", __func__, ssh_err(r));
1837 goto out;
1838 }
1839
1840#ifdef DEBUG_PK
1841 sshbuf_dump(b, stderr);
1842#endif
1843 if (authctxt->sensitive->external_keysign)
1844 r = ssh_keysign(private, &sig, &siglen,
1845 sshbuf_ptr(b), sshbuf_len(b));
1846 else if ((r = sshkey_sign(private, &sig, &siglen,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001847 sshbuf_ptr(b), sshbuf_len(b), NULL, datafellows)) != 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001848 debug("%s: sshkey_sign: %s", __func__, ssh_err(r));
1849 if (r != 0) {
1850 error("sign using hostkey %s %s failed",
1851 sshkey_ssh_name(private), fp);
1852 goto out;
1853 }
1854 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1855 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1856 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1857 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1858 (r = sshpkt_put_cstring(ssh, key_ssh_name(private))) != 0 ||
1859 (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
1860 (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
1861 (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
1862 (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
1863 (r = sshpkt_send(ssh)) != 0) {
1864 error("%s: packet error: %s", __func__, ssh_err(r));
1865 goto out;
1866 }
1867 success = 1;
1868
1869 out:
1870 if (sig != NULL) {
1871 explicit_bzero(sig, siglen);
1872 free(sig);
1873 }
1874 free(keyblob);
1875 free(lname);
1876 free(fp);
1877 free(chost);
1878 sshkey_free(private);
1879 sshbuf_free(b);
1880
1881 return success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001882}
1883
Damien Miller62cee002000-09-23 17:15:56 +11001884/* find auth method */
1885
Damien Miller62cee002000-09-23 17:15:56 +11001886/*
1887 * given auth method name, if configurable options permit this method fill
1888 * in auth_ident field and return true, otherwise return false.
1889 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001890static int
Damien Miller62cee002000-09-23 17:15:56 +11001891authmethod_is_enabled(Authmethod *method)
1892{
1893 if (method == NULL)
1894 return 0;
1895 /* return false if options indicate this method is disabled */
1896 if (method->enabled == NULL || *method->enabled == 0)
1897 return 0;
1898 /* return false if batch mode is enabled but method needs interactive mode */
1899 if (method->batch_flag != NULL && *method->batch_flag != 0)
1900 return 0;
1901 return 1;
1902}
1903
Ben Lindstrombba81212001-06-25 05:01:22 +00001904static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001905authmethod_lookup(const char *name)
1906{
1907 Authmethod *method = NULL;
1908 if (name != NULL)
1909 for (method = authmethods; method->name != NULL; method++)
1910 if (strcmp(name, method->name) == 0)
1911 return method;
1912 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1913 return NULL;
1914}
1915
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001916/* XXX internal state */
1917static Authmethod *current = NULL;
1918static char *supported = NULL;
1919static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001920
Damien Miller62cee002000-09-23 17:15:56 +11001921/*
1922 * Given the authentication method list sent by the server, return the
1923 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001924 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001925 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001926static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001927authmethod_get(char *authlist)
1928{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001929 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001930 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001931
Damien Miller62cee002000-09-23 17:15:56 +11001932 /* Use a suitable default if we're passed a nil list. */
1933 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001934 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001935
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001936 if (supported == NULL || strcmp(authlist, supported) != 0) {
1937 debug3("start over, passed a different list %s", authlist);
Darren Tuckera627d422013-06-02 07:31:17 +10001938 free(supported);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001939 supported = xstrdup(authlist);
1940 preferred = options.preferred_authentications;
1941 debug3("preferred %s", preferred);
1942 current = NULL;
1943 } else if (current != NULL && authmethod_is_enabled(current))
1944 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001945
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001946 for (;;) {
1947 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001948 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001949 current = NULL;
1950 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001951 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001952 preferred += next;
1953 debug3("authmethod_lookup %s", name);
1954 debug3("remaining preferred: %s", preferred);
1955 if ((current = authmethod_lookup(name)) != NULL &&
1956 authmethod_is_enabled(current)) {
1957 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001958 debug("Next authentication method: %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10001959 free(name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001960 return current;
1961 }
Darren Tuckere52a2602013-06-06 08:22:05 +10001962 free(name);
Damien Millereba71ba2000-04-29 23:57:08 +10001963 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001964}
Damien Miller62cee002000-09-23 17:15:56 +11001965
Ben Lindstrom79073822001-07-04 03:42:30 +00001966static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001967authmethods_get(void)
1968{
1969 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001970 Buffer b;
1971 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001972
Damien Miller0e3b8722002-01-22 23:26:38 +11001973 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001974 for (method = authmethods; method->name != NULL; method++) {
1975 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001976 if (buffer_len(&b) > 0)
1977 buffer_append(&b, ",", 1);
1978 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001979 }
Damien Miller62cee002000-09-23 17:15:56 +11001980 }
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001981 if ((list = sshbuf_dup_string(&b)) == NULL)
1982 fatal("%s: sshbuf_dup_string failed", __func__);
Damien Miller0e3b8722002-01-22 23:26:38 +11001983 buffer_free(&b);
1984 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001985}
Damien Miller01ed2272008-11-05 16:20:46 +11001986