blob: 945471f151e33f930b7b0c691d17db1fe939431e [file] [log] [blame]
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +00001/* $OpenBSD: sshconnect2.c,v 1.243 2016/05/02 10:26:04 djm Exp $ */
Damien Millereba71ba2000-04-29 23:57:08 +10002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Miller01ed2272008-11-05 16:20:46 +11004 * Copyright (c) 2008 Damien Miller. All rights reserved.
Damien Millereba71ba2000-04-29 23:57:08 +10005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100014 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
Damien Miller2eb63402006-03-15 11:09:42 +110028
Damien Miller9cf6d072006-03-15 11:29:24 +110029#include <sys/types.h>
Damien Millerd7834352006-08-05 12:39:39 +100030#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110031#include <sys/wait.h>
Damien Millerf17883e2006-03-15 11:45:54 +110032#include <sys/stat.h>
Damien Millereba71ba2000-04-29 23:57:08 +100033
Darren Tucker39972492006-07-12 22:22:46 +100034#include <errno.h>
Darren Tucker6e7fe1c2010-01-08 17:07:22 +110035#include <fcntl.h>
Darren Tuckerf520ea12007-05-20 15:11:33 +100036#include <netdb.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <pwd.h>
38#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100039#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100040#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100041#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100042#include <unistd.h>
Damien Miller63b4bcd2013-03-20 12:55:14 +110043#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
Damien Miller7ba0ca72008-07-17 18:57:06 +100044#include <vis.h>
Damien Miller2e28d862008-07-17 19:15:43 +100045#endif
Darren Tucker39972492006-07-12 22:22:46 +100046
Damien Miller9c617692003-05-14 14:31:11 +100047#include "openbsd-compat/sys-queue.h"
48
Damien Millerd7834352006-08-05 12:39:39 +100049#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100050#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100052#include "buffer.h"
53#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100054#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100056#include "key.h"
Damien Millereba71ba2000-04-29 23:57:08 +100057#include "kex.h"
58#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100059#include "sshconnect.h"
60#include "authfile.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000061#include "dh.h"
Damien Millerad833b32000-08-23 10:46:23 +100062#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "log.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100064#include "misc.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100065#include "readconf.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000066#include "match.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000067#include "dispatch.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000068#include "canohost.h"
Ben Lindstrom1bad2562002-06-06 19:57:33 +000069#include "msg.h"
70#include "pathnames.h"
Damien Miller6b4069a2006-06-13 13:05:15 +100071#include "uidswap.h"
Damien Millerd925dcd2010-12-01 12:21:51 +110072#include "hostfile.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +000073#include "ssherr.h"
Damien Miller874d77b2000-10-14 16:23:11 +110074
Darren Tucker0efd1552003-08-26 11:49:55 +100075#ifdef GSSAPI
76#include "ssh-gss.h"
77#endif
78
Damien Millereba71ba2000-04-29 23:57:08 +100079/* import */
80extern char *client_version_string;
81extern char *server_version_string;
82extern Options options;
83
84/*
85 * SSH2 key exchange
86 */
87
Ben Lindstrom46c16222000-12-22 01:43:59 +000088u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +100089u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100090
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000091char *xxx_host;
92struct sockaddr *xxx_hostaddr;
93
Ben Lindstrombba81212001-06-25 05:01:22 +000094static int
markus@openbsd.org57d10cb2015-01-19 20:16:15 +000095verify_host_key_callback(Key *hostkey, struct ssh *ssh)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000096{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000097 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +100098 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000099 return 0;
100}
101
Damien Millerd925dcd2010-12-01 12:21:51 +1100102static char *
103order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
104{
105 char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
106 size_t maxlen;
107 struct hostkeys *hostkeys;
108 int ktype;
Damien Miller295ee632011-05-29 21:42:31 +1000109 u_int i;
Damien Millerd925dcd2010-12-01 12:21:51 +1100110
111 /* Find all hostkeys for this hostname */
112 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
113 hostkeys = init_hostkeys();
Damien Miller295ee632011-05-29 21:42:31 +1000114 for (i = 0; i < options.num_user_hostfiles; i++)
115 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
116 for (i = 0; i < options.num_system_hostfiles; i++)
117 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
Damien Millerd925dcd2010-12-01 12:21:51 +1100118
119 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
120 maxlen = strlen(avail) + 1;
121 first = xmalloc(maxlen);
122 last = xmalloc(maxlen);
123 *first = *last = '\0';
124
125#define ALG_APPEND(to, from) \
126 do { \
127 if (*to != '\0') \
128 strlcat(to, ",", maxlen); \
129 strlcat(to, from, maxlen); \
130 } while (0)
131
132 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000133 if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
Damien Millerd925dcd2010-12-01 12:21:51 +1100134 fatal("%s: unknown alg %s", __func__, alg);
135 if (lookup_key_in_hostkeys_by_type(hostkeys,
djm@openbsd.org141efe42015-01-14 20:05:27 +0000136 sshkey_type_plain(ktype), NULL))
Damien Millerd925dcd2010-12-01 12:21:51 +1100137 ALG_APPEND(first, alg);
138 else
139 ALG_APPEND(last, alg);
140 }
141#undef ALG_APPEND
djm@openbsd.org35d60222015-01-18 13:33:34 +0000142 xasprintf(&ret, "%s%s%s", first,
143 (*first == '\0' || *last == '\0') ? "" : ",", last);
Damien Millerd925dcd2010-12-01 12:21:51 +1100144 if (*first != '\0')
145 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
146
Darren Tuckera627d422013-06-02 07:31:17 +1000147 free(first);
148 free(last);
149 free(hostname);
150 free(oavail);
Damien Millerd925dcd2010-12-01 12:21:51 +1100151 free_hostkeys(hostkeys);
152
153 return ret;
154}
155
Damien Millereba71ba2000-04-29 23:57:08 +1000156void
Damien Millerd925dcd2010-12-01 12:21:51 +1100157ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
Damien Miller874d77b2000-10-14 16:23:11 +1100158{
Damien Miller9235a032014-04-20 13:17:20 +1000159 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000160 char *s;
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000161 struct kex *kex;
markus@openbsd.org57e783c2015-01-20 20:16:21 +0000162 int r;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000163
164 xxx_host = host;
165 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100166
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000167 if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
168 fatal("%s: kex_names_cat", __func__);
169 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(s);
Damien Millera0ff4662001-03-30 10:49:35 +1000170 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000171 compat_cipher_proposal(options.ciphers);
Damien Millera0ff4662001-03-30 10:49:35 +1000172 myproposal[PROPOSAL_ENC_ALGS_STOC] =
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000173 compat_cipher_proposal(options.ciphers);
Damien Miller874d77b2000-10-14 16:23:11 +1100174 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000175 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000176 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100177 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000178 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000179 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100180 }
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000181 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
182 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
183 if (options.hostkeyalgorithms != NULL) {
184 if (kex_assemble_names(KEX_DEFAULT_PK_ALG,
185 &options.hostkeyalgorithms) != 0)
186 fatal("%s: kex_assemble_namelist", __func__);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100187 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100188 compat_pkalg_proposal(options.hostkeyalgorithms);
djm@openbsd.orgf9eca242015-07-30 00:01:34 +0000189 } else {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +0000190 /* Enforce default */
191 options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG);
Damien Millerd925dcd2010-12-01 12:21:51 +1100192 /* Prefer algorithms that we already have keys for */
193 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100194 compat_pkalg_proposal(
195 order_hostkeyalgs(host, hostaddr, port));
Damien Millerd925dcd2010-12-01 12:21:51 +1100196 }
Damien Miller874d77b2000-10-14 16:23:11 +1100197
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000198 if (options.rekey_limit || options.rekey_interval)
199 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
200 (time_t)options.rekey_interval);
Damien Millera5539d22003-04-09 20:50:06 +1000201
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000202 /* start key exchange */
markus@openbsd.org57e783c2015-01-20 20:16:21 +0000203 if ((r = kex_setup(active_state, myproposal)) != 0)
204 fatal("kex_setup: %s", ssh_err(r));
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000205 kex = active_state->kex;
Damien Miller1f0311c2014-05-15 14:24:09 +1000206#ifdef WITH_OPENSSL
Damien Miller8e7fb332003-02-24 12:03:03 +1100207 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000208 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
djm@openbsd.org0e8eeec2016-05-02 10:26:04 +0000209 kex->kex[KEX_DH_GRP14_SHA256] = kexdh_client;
210 kex->kex[KEX_DH_GRP16_SHA512] = kexdh_client;
211 kex->kex[KEX_DH_GRP18_SHA512] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100212 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100213 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100214# ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000215 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100216# endif
Damien Miller1f0311c2014-05-15 14:24:09 +1000217#endif
Damien Miller1e124262013-11-04 08:26:52 +1100218 kex->kex[KEX_C25519_SHA256] = kexc25519_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000219 kex->client_version_string=client_version_string;
220 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000221 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100222
markus@openbsd.org57d10cb2015-01-19 20:16:15 +0000223 dispatch_run(DISPATCH_BLOCK, &kex->done, active_state);
Damien Miller874d77b2000-10-14 16:23:11 +1100224
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000225 /* remove ext-info from the KEX proposals for rekeying */
226 myproposal[PROPOSAL_KEX_ALGS] =
227 compat_kex_proposal(options.kex_algorithms);
228 if ((r = kex_prop2buf(kex->my, myproposal)) != 0)
229 fatal("kex_prop2buf: %s", ssh_err(r));
Darren Tucker36331b52010-01-08 16:50:41 +1100230
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000231 session_id2 = kex->session_id;
232 session_id2_len = kex->session_id_len;
233
Damien Miller874d77b2000-10-14 16:23:11 +1100234#ifdef DEBUG_KEXDH
235 /* send 1st encrypted/maced/compressed message */
236 packet_start(SSH2_MSG_IGNORE);
237 packet_put_cstring("markus");
238 packet_send();
239 packet_write_wait();
240#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000241}
Damien Millerb1715dc2000-05-30 13:44:51 +1000242
Damien Millereba71ba2000-04-29 23:57:08 +1000243/*
244 * Authenticate user
245 */
Damien Miller62cee002000-09-23 17:15:56 +1100246
djm@openbsd.org141efe42015-01-14 20:05:27 +0000247typedef struct cauthctxt Authctxt;
248typedef struct cauthmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000249typedef struct identity Identity;
250typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100251
Damien Miller280ecfb2003-05-14 13:46:00 +1000252struct identity {
253 TAILQ_ENTRY(identity) next;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000254 int agent_fd; /* >=0 if agent supports key */
255 struct sshkey *key; /* public/private key */
Damien Miller280ecfb2003-05-14 13:46:00 +1000256 char *filename; /* comment for agent-only keys */
257 int tried;
258 int isprivate; /* key points to the private key */
Damien Miller5ceddc32013-02-15 12:18:32 +1100259 int userprovided;
Damien Miller280ecfb2003-05-14 13:46:00 +1000260};
261TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100262
djm@openbsd.org141efe42015-01-14 20:05:27 +0000263struct cauthctxt {
Damien Miller62cee002000-09-23 17:15:56 +1100264 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000265 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100266 const char *host;
267 const char *service;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000268 struct cauthmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000269 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000270 char *authlist;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000271 int attempt;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000272 /* pubkey */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000273 struct idlist keys;
274 int agent_fd;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000275 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000276 Sensitive *sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000277 char *oktypes, *ktypes;
278 const char *active_ktype;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000279 /* kbd-interactive */
280 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000281 /* generic */
282 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100283};
djm@openbsd.org141efe42015-01-14 20:05:27 +0000284
285struct cauthmethod {
Damien Miller62cee002000-09-23 17:15:56 +1100286 char *name; /* string to compare against server's list */
287 int (*userauth)(Authctxt *authctxt);
Damien Miller01ed2272008-11-05 16:20:46 +1100288 void (*cleanup)(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100289 int *enabled; /* flag in option struct that enables method */
290 int *batch_flag; /* flag in option struct that disables method */
291};
292
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000293int input_userauth_service_accept(int, u_int32_t, void *);
294int input_userauth_ext_info(int, u_int32_t, void *);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000295int input_userauth_success(int, u_int32_t, void *);
296int input_userauth_success_unexpected(int, u_int32_t, void *);
297int input_userauth_failure(int, u_int32_t, void *);
298int input_userauth_banner(int, u_int32_t, void *);
299int input_userauth_error(int, u_int32_t, void *);
300int input_userauth_info_req(int, u_int32_t, void *);
301int input_userauth_pk_ok(int, u_int32_t, void *);
302int input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100303
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000304int userauth_none(Authctxt *);
305int userauth_pubkey(Authctxt *);
306int userauth_passwd(Authctxt *);
307int userauth_kbdint(Authctxt *);
308int userauth_hostbased(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100309
Darren Tucker0efd1552003-08-26 11:49:55 +1000310#ifdef GSSAPI
311int userauth_gssapi(Authctxt *authctxt);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000312int input_gssapi_response(int type, u_int32_t, void *);
313int input_gssapi_token(int type, u_int32_t, void *);
314int input_gssapi_hash(int type, u_int32_t, void *);
315int input_gssapi_error(int, u_int32_t, void *);
316int input_gssapi_errtok(int, u_int32_t, void *);
Darren Tucker0efd1552003-08-26 11:49:55 +1000317#endif
318
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000319void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000320
Damien Miller280ecfb2003-05-14 13:46:00 +1000321static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000322static void pubkey_prepare(Authctxt *);
323static void pubkey_cleanup(Authctxt *);
jcs@openbsd.orgf361df42015-11-15 22:26:49 +0000324static Key *load_identity_file(Identity *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000325
Ben Lindstrombba81212001-06-25 05:01:22 +0000326static Authmethod *authmethod_get(char *authlist);
327static Authmethod *authmethod_lookup(const char *name);
328static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100329
330Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000331#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100332 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000333 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100334 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000335 &options.gss_authentication,
336 NULL},
337#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000338 {"hostbased",
339 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100340 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000341 &options.hostbased_authentication,
342 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000343 {"publickey",
344 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100345 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000346 &options.pubkey_authentication,
347 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100348 {"keyboard-interactive",
349 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100350 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100351 &options.kbd_interactive_authentication,
352 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000353 {"password",
354 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100355 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000356 &options.password_authentication,
357 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100358 {"none",
359 userauth_none,
360 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100361 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100362 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100363 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100364};
365
366void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000367ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000368 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100369{
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000370 struct ssh *ssh = active_state;
Damien Miller62cee002000-09-23 17:15:56 +1100371 Authctxt authctxt;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000372 int r;
Damien Miller62cee002000-09-23 17:15:56 +1100373
Ben Lindstrom551ea372001-06-05 18:56:16 +0000374 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000375 options.kbd_interactive_authentication = 1;
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000376 if (options.preferred_authentications == NULL)
377 options.preferred_authentications = authmethods_get();
378
Damien Miller62cee002000-09-23 17:15:56 +1100379 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000380 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000381 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100382 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000383 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100384 authctxt.host = host;
385 authctxt.service = "ssh-connection"; /* service name */
386 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100387 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000388 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000389 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000390 authctxt.sensitive = sensitive;
djm@openbsd.org46347ed2015-01-30 11:43:14 +0000391 authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000392 authctxt.info_req_seen = 0;
djm@openbsd.orgf14564c2015-01-15 11:04:36 +0000393 authctxt.agent_fd = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100394 if (authctxt.method == NULL)
395 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100396
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000397 if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
398 (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
399 (r = sshpkt_send(ssh)) != 0)
400 fatal("%s: %s", __func__, ssh_err(r));
Damien Miller62cee002000-09-23 17:15:56 +1100401
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000402 ssh_dispatch_init(ssh, &input_userauth_error);
403 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
404 ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
405 ssh_dispatch_run(ssh, DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
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
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000410 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100411}
Damien Millerf842fcb2003-05-15 12:01:28 +1000412
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000413/* ARGSUSED */
414int
415input_userauth_service_accept(int type, u_int32_t seqnr, void *ctxt)
416{
417 Authctxt *authctxt = ctxt;
418 struct ssh *ssh = active_state;
419 int r;
420
421 if (ssh_packet_remaining(ssh) > 0) {
422 char *reply;
423
424 if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
425 goto out;
426 debug2("service_accept: %s", reply);
427 free(reply);
428 } else {
429 debug2("buggy server: service_accept w/o service");
430 }
431 if ((r = sshpkt_get_end(ssh)) != 0)
432 goto out;
433 debug("SSH2_MSG_SERVICE_ACCEPT received");
434
435 /* initial userauth request */
436 userauth_none(authctxt);
437
438 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error);
439 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
440 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
441 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
442 r = 0;
443 out:
444 return r;
445}
446
447/* ARGSUSED */
448int
449input_userauth_ext_info(int type, u_int32_t seqnr, void *ctxt)
450{
451 return kex_input_ext_info(type, seqnr, active_state);
452}
453
Damien Miller62cee002000-09-23 17:15:56 +1100454void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000455userauth(Authctxt *authctxt, char *authlist)
456{
Damien Miller01ed2272008-11-05 16:20:46 +1100457 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
458 authctxt->method->cleanup(authctxt);
459
Darren Tuckera627d422013-06-02 07:31:17 +1000460 free(authctxt->methoddata);
461 authctxt->methoddata = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000462 if (authlist == NULL) {
463 authlist = authctxt->authlist;
464 } else {
Darren Tuckera627d422013-06-02 07:31:17 +1000465 free(authctxt->authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000466 authctxt->authlist = authlist;
467 }
468 for (;;) {
469 Authmethod *method = authmethod_get(authlist);
470 if (method == NULL)
471 fatal("Permission denied (%s).", authlist);
472 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000473
474 /* reset the per method handler */
475 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
476 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
477
478 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000479 if (method->userauth(authctxt) != 0) {
480 debug2("we sent a %s packet, wait for reply", method->name);
481 break;
482 } else {
483 debug2("we did not send a packet, disable method");
484 method->enabled = NULL;
485 }
486 }
487}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000488
Damien Millerf7475d72008-11-03 19:26:18 +1100489/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000490int
Damien Miller630d6f42002-01-22 23:17:30 +1100491input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100492{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000493 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000494 "type %d", type);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000495 return 0;
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000496}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000497
Damien Millerf7475d72008-11-03 19:26:18 +1100498/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000499int
Damien Miller630d6f42002-01-22 23:17:30 +1100500input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000501{
Damien Miller7ba0ca72008-07-17 18:57:06 +1000502 char *msg, *raw, *lang;
503 u_int len;
Darren Tucker79644822003-10-08 17:37:58 +1000504
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000505 debug3("input_userauth_banner");
Damien Miller7ba0ca72008-07-17 18:57:06 +1000506 raw = packet_get_string(&len);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000507 lang = packet_get_string(NULL);
Damien Millerc674d582008-11-03 19:16:57 +1100508 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
Damien Miller7ba0ca72008-07-17 18:57:06 +1000509 if (len > 65536)
510 len = 65536;
Damien Millerc4d1b362008-11-03 19:22:09 +1100511 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
Darren Tucker0c348f52010-01-08 18:58:05 +1100512 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
Darren Tucker046dff22003-10-08 17:32:02 +1000513 fprintf(stderr, "%s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000514 free(msg);
Damien Miller7ba0ca72008-07-17 18:57:06 +1000515 }
Darren Tuckera627d422013-06-02 07:31:17 +1000516 free(raw);
517 free(lang);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000518 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100519}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000520
Damien Millerf7475d72008-11-03 19:26:18 +1100521/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000522int
Damien Miller630d6f42002-01-22 23:17:30 +1100523input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100524{
525 Authctxt *authctxt = ctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100526
Damien Miller62cee002000-09-23 17:15:56 +1100527 if (authctxt == NULL)
528 fatal("input_userauth_success: no authentication context");
Darren Tuckera627d422013-06-02 07:31:17 +1000529 free(authctxt->authlist);
530 authctxt->authlist = NULL;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100531 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
532 authctxt->method->cleanup(authctxt);
Darren Tuckera627d422013-06-02 07:31:17 +1000533 free(authctxt->methoddata);
534 authctxt->methoddata = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100535 authctxt->success = 1; /* break out */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000536 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100537}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000538
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000539int
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100540input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
541{
542 Authctxt *authctxt = ctxt;
543
544 if (authctxt == NULL)
545 fatal("%s: no authentication context", __func__);
546
547 fatal("Unexpected authentication success during %s.",
548 authctxt->method->name);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000549 return 0;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100550}
551
Damien Millerf7475d72008-11-03 19:26:18 +1100552/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000553int
Damien Miller630d6f42002-01-22 23:17:30 +1100554input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100555{
Damien Miller62cee002000-09-23 17:15:56 +1100556 Authctxt *authctxt = ctxt;
557 char *authlist = NULL;
558 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100559
560 if (authctxt == NULL)
561 fatal("input_userauth_failure: no authentication context");
562
Damien Miller874d77b2000-10-14 16:23:11 +1100563 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100564 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100565 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100566
Damien Miller62e9c4f2013-04-23 15:15:49 +1000567 if (partial != 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000568 logit("Authenticated with partial success.");
Damien Miller62e9c4f2013-04-23 15:15:49 +1000569 /* reset state */
570 pubkey_cleanup(authctxt);
571 pubkey_prepare(authctxt);
572 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000573 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100574
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000575 userauth(authctxt, authlist);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000576 return 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000577}
Damien Millerf7475d72008-11-03 19:26:18 +1100578
579/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000580int
Damien Miller630d6f42002-01-22 23:17:30 +1100581input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000582{
583 Authctxt *authctxt = ctxt;
584 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000585 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000586 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000587 int pktype, sent = 0;
588 u_int alen, blen;
589 char *pkalg, *fp;
590 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000591
592 if (authctxt == NULL)
593 fatal("input_userauth_pk_ok: no authentication context");
594 if (datafellows & SSH_BUG_PKOK) {
595 /* this is similar to SSH_BUG_PKAUTH */
596 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
597 pkblob = packet_get_string(&blen);
598 buffer_init(&b);
599 buffer_append(&b, pkblob, blen);
600 pkalg = buffer_get_string(&b, &alen);
601 buffer_free(&b);
602 } else {
603 pkalg = packet_get_string(&alen);
604 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000605 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100606 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000607
Damien Miller280ecfb2003-05-14 13:46:00 +1000608 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000609
Damien Miller280ecfb2003-05-14 13:46:00 +1000610 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
611 debug("unknown pkalg %s", pkalg);
612 goto done;
613 }
614 if ((key = key_from_blob(pkblob, blen)) == NULL) {
615 debug("no key from blob. pkalg %s", pkalg);
616 goto done;
617 }
618 if (key->type != pktype) {
619 error("input_userauth_pk_ok: type mismatch "
620 "for decoded key (received %d, expected %d)",
621 key->type, pktype);
622 goto done;
623 }
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000624 if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
625 SSH_FP_DEFAULT)) == NULL)
626 goto done;
Damien Miller280ecfb2003-05-14 13:46:00 +1000627 debug2("input_userauth_pk_ok: fp %s", fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000628 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000629
Darren Tuckerd05b6012003-10-15 15:55:59 +1000630 /*
631 * search keys in the reverse order, because last candidate has been
632 * moved to the end of the queue. this also avoids confusion by
633 * duplicate keys
634 */
Damien Miller0b51a522004-04-20 20:07:19 +1000635 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000636 if (key_equal(key, id->key)) {
637 sent = sign_and_send_pubkey(authctxt, id);
638 break;
639 }
640 }
641done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000642 if (key != NULL)
643 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000644 free(pkalg);
645 free(pkblob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000646
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000647 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000648 if (sent == 0)
649 userauth(authctxt, NULL);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000650 return 0;
Damien Miller62cee002000-09-23 17:15:56 +1100651}
652
Darren Tucker0efd1552003-08-26 11:49:55 +1000653#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100654int
Darren Tucker0efd1552003-08-26 11:49:55 +1000655userauth_gssapi(Authctxt *authctxt)
656{
657 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100658 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000659 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000660 OM_uint32 min;
661 int ok = 0;
662
663 /* Try one GSSAPI method at a time, rather than sending them all at
664 * once. */
665
Darren Tucker56afe142003-11-03 20:06:14 +1100666 if (gss_supported == NULL)
667 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000668
669 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100670 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000671 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100672 if (gss_supported->elements[mech].length < 128 &&
Damien Millera1cb9f32006-08-19 00:33:34 +1000673 ssh_gssapi_check_mechanism(&gssctxt,
674 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000675 ok = 1; /* Mechanism works */
676 } else {
677 mech++;
678 }
679 }
680
Damien Millera1cb9f32006-08-19 00:33:34 +1000681 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000682 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000683
684 authctxt->methoddata=(void *)gssctxt;
685
686 packet_start(SSH2_MSG_USERAUTH_REQUEST);
687 packet_put_cstring(authctxt->server_user);
688 packet_put_cstring(authctxt->service);
689 packet_put_cstring(authctxt->method->name);
690
691 packet_put_int(1);
692
Darren Tucker655a5e02003-11-03 20:09:03 +1100693 packet_put_int((gss_supported->elements[mech].length) + 2);
694 packet_put_char(SSH_GSS_OIDTYPE);
695 packet_put_char(gss_supported->elements[mech].length);
696 packet_put_raw(gss_supported->elements[mech].elements,
697 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000698
699 packet_send();
700
701 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
702 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
703 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
704 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
705
706 mech++; /* Move along to next candidate */
707
708 return 1;
709}
710
Damien Miller91c6aa42003-11-17 21:20:18 +1100711static OM_uint32
712process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
713{
714 Authctxt *authctxt = ctxt;
715 Gssctxt *gssctxt = authctxt->methoddata;
716 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000717 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
718 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100719 OM_uint32 status, ms, flags;
720 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100721
Damien Miller91c6aa42003-11-17 21:20:18 +1100722 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100723 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100724
725 if (send_tok.length > 0) {
726 if (GSS_ERROR(status))
727 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
728 else
729 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100730
Damien Miller91c6aa42003-11-17 21:20:18 +1100731 packet_put_string(send_tok.value, send_tok.length);
732 packet_send();
733 gss_release_buffer(&ms, &send_tok);
734 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100735
Damien Miller91c6aa42003-11-17 21:20:18 +1100736 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100737 /* send either complete or MIC, depending on mechanism */
738 if (!(flags & GSS_C_INTEG_FLAG)) {
739 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
740 packet_send();
741 } else {
742 ssh_gssapi_buildmic(&b, authctxt->server_user,
743 authctxt->service, "gssapi-with-mic");
744
745 gssbuf.value = buffer_ptr(&b);
746 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100747
Damien Miller0425d402003-11-17 22:18:21 +1100748 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100749
Damien Miller0425d402003-11-17 22:18:21 +1100750 if (!GSS_ERROR(status)) {
751 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
752 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100753
Damien Miller0425d402003-11-17 22:18:21 +1100754 packet_send();
755 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100756
Damien Miller0425d402003-11-17 22:18:21 +1100757 buffer_free(&b);
758 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100759 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100760 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100761
Damien Miller91c6aa42003-11-17 21:20:18 +1100762 return status;
763}
764
Damien Millerf7475d72008-11-03 19:26:18 +1100765/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000766int
Darren Tucker0efd1552003-08-26 11:49:55 +1000767input_gssapi_response(int type, u_int32_t plen, void *ctxt)
768{
769 Authctxt *authctxt = ctxt;
770 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000771 int oidlen;
772 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000773
774 if (authctxt == NULL)
775 fatal("input_gssapi_response: no authentication context");
776 gssctxt = authctxt->methoddata;
777
778 /* Setup our OID */
779 oidv = packet_get_string(&oidlen);
780
Darren Tucker655a5e02003-11-03 20:09:03 +1100781 if (oidlen <= 2 ||
782 oidv[0] != SSH_GSS_OIDTYPE ||
783 oidv[1] != oidlen - 2) {
Darren Tuckera627d422013-06-02 07:31:17 +1000784 free(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100785 debug("Badly encoded mechanism OID received");
786 userauth(authctxt, NULL);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000787 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000788 }
789
Darren Tucker655a5e02003-11-03 20:09:03 +1100790 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
791 fatal("Server returned different OID than expected");
792
Darren Tucker0efd1552003-08-26 11:49:55 +1000793 packet_check_eom();
794
Darren Tuckera627d422013-06-02 07:31:17 +1000795 free(oidv);
Darren Tucker0efd1552003-08-26 11:49:55 +1000796
Damien Miller91c6aa42003-11-17 21:20:18 +1100797 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000798 /* Start again with next method on list */
799 debug("Trying to start again");
800 userauth(authctxt, NULL);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000801 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000802 }
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000803 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000804}
805
Damien Millerf7475d72008-11-03 19:26:18 +1100806/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000807int
Darren Tucker0efd1552003-08-26 11:49:55 +1000808input_gssapi_token(int type, u_int32_t plen, void *ctxt)
809{
810 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000811 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100812 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000813 u_int slen;
814
815 if (authctxt == NULL)
816 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000817
818 recv_tok.value = packet_get_string(&slen);
819 recv_tok.length = slen; /* safe typecast */
820
821 packet_check_eom();
822
Damien Miller91c6aa42003-11-17 21:20:18 +1100823 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000824
Darren Tuckera627d422013-06-02 07:31:17 +1000825 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000826
827 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000828 /* Start again with the next method in the list */
829 userauth(authctxt, NULL);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000830 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000831 }
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000832 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000833}
834
Damien Millerf7475d72008-11-03 19:26:18 +1100835/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000836int
Darren Tucker0efd1552003-08-26 11:49:55 +1000837input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
838{
839 Authctxt *authctxt = ctxt;
840 Gssctxt *gssctxt;
841 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
842 gss_buffer_desc recv_tok;
Damien Millerd677ad12013-04-23 15:18:51 +1000843 OM_uint32 ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000844 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000845
846 if (authctxt == NULL)
847 fatal("input_gssapi_response: no authentication context");
848 gssctxt = authctxt->methoddata;
849
Darren Tucker600ad8d2003-08-26 12:10:48 +1000850 recv_tok.value = packet_get_string(&len);
851 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000852
853 packet_check_eom();
854
855 /* Stick it into GSSAPI and see what it says */
Damien Millerd677ad12013-04-23 15:18:51 +1000856 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000857 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000858
Darren Tuckera627d422013-06-02 07:31:17 +1000859 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000860 gss_release_buffer(&ms, &send_tok);
861
862 /* Server will be returning a failed packet after this one */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000863 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000864}
865
Damien Millerf7475d72008-11-03 19:26:18 +1100866/* ARGSUSED */
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000867int
Darren Tucker0efd1552003-08-26 11:49:55 +1000868input_gssapi_error(int type, u_int32_t plen, void *ctxt)
869{
Darren Tucker0efd1552003-08-26 11:49:55 +1000870 char *msg;
871 char *lang;
872
Damien Millerd677ad12013-04-23 15:18:51 +1000873 /* maj */(void)packet_get_int();
874 /* min */(void)packet_get_int();
Darren Tucker0efd1552003-08-26 11:49:55 +1000875 msg=packet_get_string(NULL);
876 lang=packet_get_string(NULL);
877
878 packet_check_eom();
879
Damien Miller15d72a02005-11-05 15:07:33 +1100880 debug("Server GSSAPI Error:\n%s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000881 free(msg);
882 free(lang);
djm@openbsd.org12b5f502015-01-20 07:56:44 +0000883 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000884}
885#endif /* GSSAPI */
886
Damien Millereba71ba2000-04-29 23:57:08 +1000887int
Damien Miller874d77b2000-10-14 16:23:11 +1100888userauth_none(Authctxt *authctxt)
889{
890 /* initial userauth request */
891 packet_start(SSH2_MSG_USERAUTH_REQUEST);
892 packet_put_cstring(authctxt->server_user);
893 packet_put_cstring(authctxt->service);
894 packet_put_cstring(authctxt->method->name);
895 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100896 return 1;
897}
898
899int
Damien Miller62cee002000-09-23 17:15:56 +1100900userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000901{
Damien Millere247cc42000-05-07 12:03:14 +1000902 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000903 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000904 char *password;
Darren Tuckerab791692010-01-08 18:48:02 +1100905 const char *host = options.host_key_alias ? options.host_key_alias :
906 authctxt->host;
Damien Millereba71ba2000-04-29 23:57:08 +1000907
Damien Millerd3a18572000-06-07 19:55:44 +1000908 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000909 return 0;
910
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000911 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000912 error("Permission denied, please try again.");
913
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000914 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100915 authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +1000916 password = read_passphrase(prompt, 0);
917 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100918 packet_put_cstring(authctxt->server_user);
919 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100920 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000921 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000922 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100923 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000924 free(password);
Damien Miller9f643902001-11-12 11:02:52 +1100925 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000926 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000927
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000928 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000929 &input_userauth_passwd_changereq);
930
Damien Millereba71ba2000-04-29 23:57:08 +1000931 return 1;
932}
Damien Millerf7475d72008-11-03 19:26:18 +1100933
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000934/*
935 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
936 */
Damien Millerf7475d72008-11-03 19:26:18 +1100937/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000938int
Tim Ricec8549622002-03-31 12:49:38 -0800939input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000940{
941 Authctxt *authctxt = ctxt;
942 char *info, *lang, *password = NULL, *retype = NULL;
943 char prompt[150];
Darren Tuckerab791692010-01-08 18:48:02 +1100944 const char *host = options.host_key_alias ? options.host_key_alias :
945 authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000946
947 debug2("input_userauth_passwd_changereq");
948
949 if (authctxt == NULL)
950 fatal("input_userauth_passwd_changereq: "
951 "no authentication context");
952
953 info = packet_get_string(NULL);
954 lang = packet_get_string(NULL);
955 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000956 logit("%s", info);
Darren Tuckera627d422013-06-02 07:31:17 +1000957 free(info);
958 free(lang);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000959 packet_start(SSH2_MSG_USERAUTH_REQUEST);
960 packet_put_cstring(authctxt->server_user);
961 packet_put_cstring(authctxt->service);
962 packet_put_cstring(authctxt->method->name);
963 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000964 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000965 "Enter %.30s@%.128s's old password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100966 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000967 password = read_passphrase(prompt, 0);
968 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100969 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000970 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000971 password = NULL;
972 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000973 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000974 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100975 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000976 password = read_passphrase(prompt, RP_ALLOW_EOF);
977 if (password == NULL) {
978 /* bail out */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +0000979 return 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000980 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000981 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000982 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100983 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000984 retype = read_passphrase(prompt, 0);
985 if (strcmp(password, retype) != 0) {
Damien Millera5103f42014-02-04 11:20:14 +1100986 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000987 free(password);
Damien Miller996acd22003-04-09 20:59:48 +1000988 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000989 password = NULL;
990 }
Damien Millera5103f42014-02-04 11:20:14 +1100991 explicit_bzero(retype, strlen(retype));
Darren Tuckera627d422013-06-02 07:31:17 +1000992 free(retype);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000993 }
994 packet_put_cstring(password);
Damien Millera5103f42014-02-04 11:20:14 +1100995 explicit_bzero(password, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000996 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000997 packet_add_padding(64);
998 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000999
1000 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001001 &input_userauth_passwd_changereq);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001002 return 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +00001003}
Damien Millereba71ba2000-04-29 23:57:08 +10001004
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001005static const char *
1006identity_sign_encode(struct identity *id)
1007{
1008 struct ssh *ssh = active_state;
1009
1010 if (id->key->type == KEY_RSA) {
1011 switch (ssh->kex->rsa_sha2) {
1012 case 256:
1013 return "rsa-sha2-256";
1014 case 512:
1015 return "rsa-sha2-512";
1016 }
1017 }
1018 return key_ssh_name(id->key);
1019}
1020
Ben Lindstrombba81212001-06-25 05:01:22 +00001021static int
djm@openbsd.org141efe42015-01-14 20:05:27 +00001022identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
1023 const u_char *data, size_t datalen, u_int compat)
Damien Miller280ecfb2003-05-14 13:46:00 +10001024{
1025 Key *prv;
1026 int ret;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001027 const char *alg;
1028
1029 alg = identity_sign_encode(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001030
1031 /* the agent supports this key */
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001032 if (id->agent_fd != -1)
djm@openbsd.org141efe42015-01-14 20:05:27 +00001033 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001034 data, datalen, alg, compat);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001035
Damien Miller280ecfb2003-05-14 13:46:00 +10001036 /*
1037 * we have already loaded the private key or
1038 * the private key is stored in external hardware
1039 */
Damien Miller86687062014-07-02 15:28:02 +10001040 if (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001041 return (sshkey_sign(id->key, sigp, lenp, data, datalen, alg,
djm@openbsd.org141efe42015-01-14 20:05:27 +00001042 compat));
Damien Miller280ecfb2003-05-14 13:46:00 +10001043 /* load the private key from the file */
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001044 if ((prv = load_identity_file(id)) == NULL)
djm@openbsd.org0d1451a2016-02-23 01:34:14 +00001045 return SSH_ERR_KEY_NOT_FOUND;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001046 ret = sshkey_sign(prv, sigp, lenp, data, datalen, alg, 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
1052sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +10001053{
1054 Buffer b;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001055 Identity *private_id;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001056 u_char *blob, *signature;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001057 size_t slen;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001058 u_int bloblen, skip = 0;
1059 int matched, ret = -1, have_sig = 1;
Damien Miller4e270b02010-04-16 15:56:21 +10001060 char *fp;
Damien Millereba71ba2000-04-29 23:57:08 +10001061
djm@openbsd.org9ce86c92015-01-28 22:36:00 +00001062 if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1063 SSH_FP_DEFAULT)) == NULL)
1064 return 0;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001065 debug3("%s: %s %s", __func__, key_type(id->key), fp);
Darren Tuckera627d422013-06-02 07:31:17 +10001066 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001067
Damien Miller280ecfb2003-05-14 13:46:00 +10001068 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001069 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +00001070 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001071 return 0;
1072 }
Damien Millereba71ba2000-04-29 23:57:08 +10001073 /* data to be signed */
1074 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +11001075 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +10001076 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001077 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +11001078 } else {
1079 buffer_put_string(&b, session_id2, session_id2_len);
1080 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +10001081 }
Damien Millereba71ba2000-04-29 23:57:08 +10001082 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001083 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +10001084 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +00001085 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +10001086 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +11001087 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +00001088 if (datafellows & SSH_BUG_PKAUTH) {
1089 buffer_put_char(&b, have_sig);
1090 } else {
1091 buffer_put_cstring(&b, authctxt->method->name);
1092 buffer_put_char(&b, have_sig);
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001093 buffer_put_cstring(&b, identity_sign_encode(id));
Ben Lindstromd121f612000-12-03 17:00:47 +00001094 }
Damien Millereba71ba2000-04-29 23:57:08 +10001095 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +10001096
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001097 /*
1098 * If the key is an certificate, try to find a matching private key
1099 * and use it to complete the signature.
djm@openbsd.org9ee692f2016-04-28 14:30:21 +00001100 * If no such private key exists, fall back to trying the certificate
1101 * key itself in case it has a private half already loaded.
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001102 */
1103 if (key_is_cert(id->key)) {
1104 matched = 0;
1105 TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1106 if (sshkey_equal_public(id->key, private_id->key) &&
1107 id->key->type != private_id->key->type) {
1108 id = private_id;
1109 matched = 1;
1110 break;
1111 }
1112 }
1113 if (matched) {
1114 debug2("%s: using private key \"%s\"%s for "
1115 "certificate", __func__, id->filename,
1116 id->agent_fd != -1 ? " from agent" : "");
1117 } else {
djm@openbsd.orgc38905b2016-03-14 16:20:54 +00001118 debug("%s: no separate private key for certificate "
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001119 "\"%s\"", __func__, id->filename);
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001120 }
1121 }
1122
Damien Millereba71ba2000-04-29 23:57:08 +10001123 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +10001124 ret = identity_sign(id, &signature, &slen,
djm@openbsd.org141efe42015-01-14 20:05:27 +00001125 buffer_ptr(&b), buffer_len(&b), datafellows);
1126 if (ret != 0) {
djm@openbsd.org0d1451a2016-02-23 01:34:14 +00001127 if (ret != SSH_ERR_KEY_NOT_FOUND)
1128 error("%s: signing failed: %s", __func__, ssh_err(ret));
Darren Tuckera627d422013-06-02 07:31:17 +10001129 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +10001130 buffer_free(&b);
1131 return 0;
1132 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001133#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +10001134 buffer_dump(&b);
1135#endif
Ben Lindstromd121f612000-12-03 17:00:47 +00001136 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +10001137 buffer_clear(&b);
1138 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001139 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +10001140 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001141 buffer_put_cstring(&b, authctxt->server_user);
1142 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +11001143 buffer_put_cstring(&b, authctxt->method->name);
1144 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +00001145 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001146 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +10001147 buffer_put_string(&b, blob, bloblen);
1148 }
Darren Tuckera627d422013-06-02 07:31:17 +10001149 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001150
Damien Millereba71ba2000-04-29 23:57:08 +10001151 /* append signature */
1152 buffer_put_string(&b, signature, slen);
Darren Tuckera627d422013-06-02 07:31:17 +10001153 free(signature);
Damien Millereba71ba2000-04-29 23:57:08 +10001154
1155 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +10001156 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +11001157 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +10001158 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +10001159
1160 /* put remaining data from buffer into packet */
1161 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1162 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1163 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +10001164 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +10001165
1166 return 1;
Damien Miller994cf142000-07-21 10:19:44 +10001167}
1168
Ben Lindstrombba81212001-06-25 05:01:22 +00001169static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001170send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001171{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001172 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001173 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +10001174
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001175 debug3("send_pubkey_test");
1176
Damien Miller280ecfb2003-05-14 13:46:00 +10001177 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001178 /* we cannot handle this key */
1179 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +10001180 return 0;
1181 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001182 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001183 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001184
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001185 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1186 packet_put_cstring(authctxt->server_user);
1187 packet_put_cstring(authctxt->service);
1188 packet_put_cstring(authctxt->method->name);
1189 packet_put_char(have_sig);
1190 if (!(datafellows & SSH_BUG_PKAUTH))
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001191 packet_put_cstring(identity_sign_encode(id));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001192 packet_put_string(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001193 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001194 packet_send();
1195 return 1;
1196}
1197
Ben Lindstrombba81212001-06-25 05:01:22 +00001198static Key *
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001199load_identity_file(Identity *id)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001200{
jsg@openbsd.org56d7dac2016-02-05 04:31:21 +00001201 Key *private = NULL;
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001202 char prompt[300], *passphrase, *comment;
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001203 int r, perm_ok = 0, quit = 0, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001204 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001205
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001206 if (stat(id->filename, &st) < 0) {
1207 (id->userprovided ? logit : debug3)("no such identity: %s: %s",
1208 id->filename, strerror(errno));
Ben Lindstrom329782e2001-03-10 17:08:59 +00001209 return NULL;
1210 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001211 snprintf(prompt, sizeof prompt,
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001212 "Enter passphrase for key '%.100s': ", id->filename);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001213 for (i = 0; i <= options.number_of_password_prompts; i++) {
1214 if (i == 0)
1215 passphrase = "";
1216 else {
Damien Miller62cee002000-09-23 17:15:56 +11001217 passphrase = read_passphrase(prompt, 0);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001218 if (*passphrase == '\0') {
Damien Miller62cee002000-09-23 17:15:56 +11001219 debug2("no passphrase given, try next key");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001220 free(passphrase);
1221 break;
Damien Miller62cee002000-09-23 17:15:56 +11001222 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001223 }
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001224 switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
1225 passphrase, &private, &comment, &perm_ok))) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001226 case 0:
1227 break;
1228 case SSH_ERR_KEY_WRONG_PASSPHRASE:
1229 if (options.batch_mode) {
1230 quit = 1;
1231 break;
1232 }
djm@openbsd.orgf14564c2015-01-15 11:04:36 +00001233 if (i != 0)
1234 debug2("bad passphrase given, try again...");
djm@openbsd.org141efe42015-01-14 20:05:27 +00001235 break;
1236 case SSH_ERR_SYSTEM_ERROR:
1237 if (errno == ENOENT) {
1238 debug2("Load key \"%s\": %s",
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001239 id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001240 quit = 1;
1241 break;
1242 }
1243 /* FALLTHROUGH */
1244 default:
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001245 error("Load key \"%s\": %s", id->filename, ssh_err(r));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001246 quit = 1;
1247 break;
1248 }
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001249 if (!quit && private != NULL && id->agent_fd == -1 &&
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001250 !(id->key && id->isprivate))
1251 maybe_add_key_to_agent(id->filename, private, comment,
1252 passphrase);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001253 if (i > 0) {
Damien Millera5103f42014-02-04 11:20:14 +11001254 explicit_bzero(passphrase, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001255 free(passphrase);
Damien Miller62cee002000-09-23 17:15:56 +11001256 }
mmcc@openbsd.orgd59ce082015-12-10 17:08:40 +00001257 free(comment);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001258 if (private != NULL || quit)
1259 break;
Damien Miller994cf142000-07-21 10:19:44 +10001260 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001261 return private;
1262}
1263
Damien Miller280ecfb2003-05-14 13:46:00 +10001264/*
1265 * try keys in the following order:
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001266 * 1. certificates listed in the config file
1267 * 2. other input certificates
1268 * 3. agent keys that are found in the config file
1269 * 4. other agent keys
1270 * 5. keys that are only listed in the config file
Damien Miller280ecfb2003-05-14 13:46:00 +10001271 */
1272static void
1273pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001274{
djm@openbsd.org141efe42015-01-14 20:05:27 +00001275 struct identity *id, *id2, *tmp;
1276 struct idlist agent, files, *preferred;
1277 struct sshkey *key;
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001278 int agent_fd = -1, i, r, found;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001279 size_t j;
1280 struct ssh_identitylist *idlist;
Damien Millerad833b32000-08-23 10:46:23 +10001281
Damien Miller280ecfb2003-05-14 13:46:00 +10001282 TAILQ_INIT(&agent); /* keys from the agent */
1283 TAILQ_INIT(&files); /* keys from the config file */
1284 preferred = &authctxt->keys;
1285 TAILQ_INIT(preferred); /* preferred order of keys */
1286
Damien Millercb6b68b2012-12-03 09:49:52 +11001287 /* list of keys stored in the filesystem and PKCS#11 */
Damien Miller280ecfb2003-05-14 13:46:00 +10001288 for (i = 0; i < options.num_identity_files; i++) {
1289 key = options.identity_keys[i];
1290 if (key && key->type == KEY_RSA1)
1291 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +11001292 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1293 continue;
Damien Miller280ecfb2003-05-14 13:46:00 +10001294 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001295 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001296 id->agent_fd = -1;
Damien Miller280ecfb2003-05-14 13:46:00 +10001297 id->key = key;
1298 id->filename = xstrdup(options.identity_files[i]);
Darren Tucker19104782013-04-05 11:13:08 +11001299 id->userprovided = options.identity_file_userprovided[i];
Damien Miller280ecfb2003-05-14 13:46:00 +10001300 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001301 }
Damien Millercb6b68b2012-12-03 09:49:52 +11001302 /* Prefer PKCS11 keys that are explicitly listed */
1303 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
Damien Miller86687062014-07-02 15:28:02 +10001304 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
Damien Millercb6b68b2012-12-03 09:49:52 +11001305 continue;
1306 found = 0;
1307 TAILQ_FOREACH(id2, &files, next) {
1308 if (id2->key == NULL ||
Damien Miller86687062014-07-02 15:28:02 +10001309 (id2->key->flags & SSHKEY_FLAG_EXT) == 0)
Damien Millercb6b68b2012-12-03 09:49:52 +11001310 continue;
djm@openbsd.org141efe42015-01-14 20:05:27 +00001311 if (sshkey_equal(id->key, id2->key)) {
Damien Millercb6b68b2012-12-03 09:49:52 +11001312 TAILQ_REMOVE(&files, id, next);
1313 TAILQ_INSERT_TAIL(preferred, id, next);
1314 found = 1;
1315 break;
1316 }
1317 }
1318 /* If IdentitiesOnly set and key not found then don't use it */
1319 if (!found && options.identities_only) {
1320 TAILQ_REMOVE(&files, id, next);
Damien Miller1d2c4562014-02-04 11:18:20 +11001321 explicit_bzero(id, sizeof(*id));
Damien Millercb6b68b2012-12-03 09:49:52 +11001322 free(id);
1323 }
1324 }
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001325 /* list of certificates specified by user */
1326 for (i = 0; i < options.num_certificate_files; i++) {
1327 key = options.certificates[i];
1328 if (!key_is_cert(key) || key->cert == NULL ||
1329 key->cert->type != SSH2_CERT_TYPE_USER)
1330 continue;
1331 id = xcalloc(1, sizeof(*id));
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001332 id->agent_fd = -1;
djm@openbsd.org4e44a792015-09-24 06:15:11 +00001333 id->key = key;
1334 id->filename = xstrdup(options.certificate_files[i]);
1335 id->userprovided = options.certificate_file_userprovided[i];
1336 TAILQ_INSERT_TAIL(preferred, id, next);
1337 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001338 /* list of keys supported by the agent */
djm@openbsd.org141efe42015-01-14 20:05:27 +00001339 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1340 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1341 debug("%s: ssh_get_authentication_socket: %s",
1342 __func__, ssh_err(r));
1343 } else if ((r = ssh_fetch_identitylist(agent_fd, 2, &idlist)) != 0) {
1344 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1345 debug("%s: ssh_fetch_identitylist: %s",
1346 __func__, ssh_err(r));
markus@openbsd.orgfc77ccd2016-01-14 22:56:56 +00001347 close(agent_fd);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001348 } else {
1349 for (j = 0; j < idlist->nkeys; j++) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001350 found = 0;
1351 TAILQ_FOREACH(id, &files, next) {
djm@openbsd.org141efe42015-01-14 20:05:27 +00001352 /*
1353 * agent keys from the config file are
1354 * preferred
1355 */
1356 if (sshkey_equal(idlist->keys[j], id->key)) {
Damien Miller280ecfb2003-05-14 13:46:00 +10001357 TAILQ_REMOVE(&files, id, next);
1358 TAILQ_INSERT_TAIL(preferred, id, next);
djm@openbsd.org141efe42015-01-14 20:05:27 +00001359 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001360 found = 1;
1361 break;
1362 }
1363 }
Damien Millerbd394c32004-03-08 23:12:36 +11001364 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001365 id = xcalloc(1, sizeof(*id));
djm@openbsd.org141efe42015-01-14 20:05:27 +00001366 /* XXX "steals" key/comment from idlist */
1367 id->key = idlist->keys[j];
1368 id->filename = idlist->comments[j];
1369 idlist->keys[j] = NULL;
1370 idlist->comments[j] = NULL;
1371 id->agent_fd = agent_fd;
Damien Miller280ecfb2003-05-14 13:46:00 +10001372 TAILQ_INSERT_TAIL(&agent, id, next);
1373 }
1374 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001375 ssh_free_identitylist(idlist);
Damien Miller280ecfb2003-05-14 13:46:00 +10001376 /* append remaining agent keys */
1377 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1378 TAILQ_REMOVE(&agent, id, next);
1379 TAILQ_INSERT_TAIL(preferred, id, next);
1380 }
djm@openbsd.org141efe42015-01-14 20:05:27 +00001381 authctxt->agent_fd = agent_fd;
Damien Miller62cee002000-09-23 17:15:56 +11001382 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001383 /* append remaining keys from the config file */
1384 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1385 TAILQ_REMOVE(&files, id, next);
1386 TAILQ_INSERT_TAIL(preferred, id, next);
1387 }
djm@openbsd.orge679c092015-10-13 16:15:21 +00001388 /* finally, filter by PubkeyAcceptedKeyTypes */
1389 TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1390 if (id->key != NULL &&
1391 match_pattern_list(sshkey_ssh_name(id->key),
1392 options.pubkey_key_types, 0) != 1) {
1393 debug("Skipping %s key %s - "
1394 "not in PubkeyAcceptedKeyTypes",
1395 sshkey_ssh_name(id->key), id->filename);
1396 TAILQ_REMOVE(preferred, id, next);
1397 sshkey_free(id->key);
1398 free(id->filename);
1399 memset(id, 0, sizeof(*id));
1400 continue;
1401 }
djm@openbsd.org6064a8b2015-12-04 00:24:55 +00001402 debug2("key: %s (%p)%s%s", id->filename, id->key,
1403 id->userprovided ? ", explicit" : "",
1404 id->agent_fd != -1 ? ", agent" : "");
Damien Miller280ecfb2003-05-14 13:46:00 +10001405 }
1406}
1407
1408static void
1409pubkey_cleanup(Authctxt *authctxt)
1410{
1411 Identity *id;
1412
djm@openbsd.org141efe42015-01-14 20:05:27 +00001413 if (authctxt->agent_fd != -1)
1414 ssh_close_authentication_socket(authctxt->agent_fd);
Damien Miller280ecfb2003-05-14 13:46:00 +10001415 for (id = TAILQ_FIRST(&authctxt->keys); id;
1416 id = TAILQ_FIRST(&authctxt->keys)) {
1417 TAILQ_REMOVE(&authctxt->keys, id, next);
mmcc@openbsd.org89540b62015-12-11 02:31:47 +00001418 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +10001419 free(id->filename);
1420 free(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001421 }
Damien Millereba71ba2000-04-29 23:57:08 +10001422}
1423
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001424static int
1425try_identity(Identity *id)
1426{
1427 if (!id->key)
1428 return (0);
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001429 if (key_type_plain(id->key->type) == KEY_RSA &&
1430 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1431 debug("Skipped %s key %s for RSA/MD5 server",
1432 key_type(id->key), id->filename);
1433 return (0);
1434 }
1435 return (id->key->type != KEY_RSA1);
1436}
1437
Damien Miller62cee002000-09-23 17:15:56 +11001438int
1439userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001440{
Damien Miller280ecfb2003-05-14 13:46:00 +10001441 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001442 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001443
Damien Miller280ecfb2003-05-14 13:46:00 +10001444 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1445 if (id->tried++)
1446 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001447 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001448 TAILQ_REMOVE(&authctxt->keys, id, next);
1449 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1450 /*
1451 * send a test message if we have the public key. for
1452 * encrypted keys we cannot do this and have to load the
1453 * private key instead
1454 */
Damien Miller324541e2013-12-31 12:25:40 +11001455 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001456 if (try_identity(id)) {
Damien Miller324541e2013-12-31 12:25:40 +11001457 debug("Offering %s public key: %s",
1458 key_type(id->key), id->filename);
1459 sent = send_pubkey_test(authctxt, id);
1460 }
1461 } else {
Damien Miller280ecfb2003-05-14 13:46:00 +10001462 debug("Trying private key: %s", id->filename);
jcs@openbsd.orgf361df42015-11-15 22:26:49 +00001463 id->key = load_identity_file(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001464 if (id->key != NULL) {
markus@openbsd.org3a1638d2015-07-10 06:21:53 +00001465 if (try_identity(id)) {
1466 id->isprivate = 1;
Damien Miller324541e2013-12-31 12:25:40 +11001467 sent = sign_and_send_pubkey(
1468 authctxt, id);
1469 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001470 key_free(id->key);
1471 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001472 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001473 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001474 if (sent)
1475 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001476 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001477 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001478}
Damien Millereba71ba2000-04-29 23:57:08 +10001479
Damien Miller874d77b2000-10-14 16:23:11 +11001480/*
1481 * Send userauth request message specifying keyboard-interactive method.
1482 */
1483int
1484userauth_kbdint(Authctxt *authctxt)
1485{
1486 static int attempt = 0;
1487
1488 if (attempt++ >= options.number_of_password_prompts)
1489 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001490 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1491 if (attempt > 1 && !authctxt->info_req_seen) {
1492 debug3("userauth_kbdint: disable: no info_req_seen");
1493 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1494 return 0;
1495 }
Damien Miller874d77b2000-10-14 16:23:11 +11001496
1497 debug2("userauth_kbdint");
1498 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1499 packet_put_cstring(authctxt->server_user);
1500 packet_put_cstring(authctxt->service);
1501 packet_put_cstring(authctxt->method->name);
1502 packet_put_cstring(""); /* lang */
1503 packet_put_cstring(options.kbd_interactive_devices ?
1504 options.kbd_interactive_devices : "");
1505 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001506
1507 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1508 return 1;
1509}
1510
1511/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001512 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001513 */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001514int
Damien Miller630d6f42002-01-22 23:17:30 +11001515input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001516{
1517 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001518 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001519 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001520 int echo = 0;
1521
1522 debug2("input_userauth_info_req");
1523
1524 if (authctxt == NULL)
1525 fatal("input_userauth_info_req: no authentication context");
1526
Ben Lindstrom7d199962001-09-12 18:29:00 +00001527 authctxt->info_req_seen = 1;
1528
Damien Miller874d77b2000-10-14 16:23:11 +11001529 name = packet_get_string(NULL);
1530 inst = packet_get_string(NULL);
1531 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001532 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001533 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001534 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001535 logit("%s", inst);
Darren Tuckera627d422013-06-02 07:31:17 +10001536 free(name);
1537 free(inst);
1538 free(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001539
1540 num_prompts = packet_get_int();
1541 /*
1542 * Begin to build info response packet based on prompts requested.
1543 * We commit to providing the correct number of responses, so if
1544 * further on we run into a problem that prevents this, we have to
1545 * be sure and clean this up and send a correct error response.
1546 */
1547 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1548 packet_put_int(num_prompts);
1549
Ben Lindstrom551ea372001-06-05 18:56:16 +00001550 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001551 for (i = 0; i < num_prompts; i++) {
1552 prompt = packet_get_string(NULL);
1553 echo = packet_get_char();
1554
Ben Lindstrom949974b2001-06-25 05:20:31 +00001555 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001556
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001557 packet_put_cstring(response);
Damien Millera5103f42014-02-04 11:20:14 +11001558 explicit_bzero(response, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +10001559 free(response);
1560 free(prompt);
Damien Miller874d77b2000-10-14 16:23:11 +11001561 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001562 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001563
Damien Miller9f643902001-11-12 11:02:52 +11001564 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001565 packet_send();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00001566 return 0;
Damien Miller874d77b2000-10-14 16:23:11 +11001567}
Damien Miller62cee002000-09-23 17:15:56 +11001568
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001569static int
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001570ssh_keysign(struct sshkey *key, u_char **sigp, size_t *lenp,
1571 const u_char *data, size_t datalen)
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001572{
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001573 struct sshbuf *b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001574 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001575 pid_t pid;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001576 int i, r, to[2], from[2], status, sock = packet_get_connection_in();
1577 u_char rversion = 0, version = 2;
1578 void (*osigchld)(int);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001579
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001580 *sigp = NULL;
1581 *lenp = 0;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001582
Ben Lindstrom5206b952002-06-06 19:59:29 +00001583 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001584 error("%s: not installed: %s", __func__, strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001585 return -1;
1586 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001587 if (fflush(stdout) != 0) {
1588 error("%s: fflush: %s", __func__, strerror(errno));
1589 return -1;
1590 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001591 if (pipe(to) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001592 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001593 return -1;
1594 }
1595 if (pipe(from) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001596 error("%s: pipe: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001597 return -1;
1598 }
1599 if ((pid = fork()) < 0) {
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001600 error("%s: fork: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001601 return -1;
1602 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001603 osigchld = signal(SIGCHLD, SIG_DFL);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001604 if (pid == 0) {
Darren Tucker6e7fe1c2010-01-08 17:07:22 +11001605 /* keep the socket on exec */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001606 fcntl(sock, F_SETFD, 0);
Damien Miller2e5fe882006-06-13 13:10:00 +10001607 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001608 close(from[0]);
1609 if (dup2(from[1], STDOUT_FILENO) < 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001610 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001611 close(to[1]);
1612 if (dup2(to[0], STDIN_FILENO) < 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001613 fatal("%s: dup2: %s", __func__, strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001614 close(from[1]);
1615 close(to[0]);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001616 /* Close everything but stdio and the socket */
1617 for (i = STDERR_FILENO + 1; i < sock; i++)
1618 close(i);
1619 closefrom(sock + 1);
1620 debug3("%s: [child] pid=%ld, exec %s",
1621 __func__, (long)getpid(), _PATH_SSH_KEY_SIGN);
mmcc@openbsd.org94141b72015-12-11 00:20:04 +00001622 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001623 fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001624 strerror(errno));
1625 }
1626 close(from[1]);
1627 close(to[0]);
1628
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001629 if ((b = sshbuf_new()) == NULL)
1630 fatal("%s: sshbuf_new failed", __func__);
1631 /* send # of sock, data to be signed */
1632 if ((r = sshbuf_put_u32(b, sock) != 0) ||
1633 (r = sshbuf_put_string(b, data, datalen)) != 0)
1634 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1635 if (ssh_msg_send(to[1], version, b) == -1)
1636 fatal("%s: couldn't send request", __func__);
1637 sshbuf_reset(b);
1638 r = ssh_msg_recv(from[0], b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001639 close(from[0]);
1640 close(to[1]);
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001641 if (r < 0) {
1642 error("%s: no reply", __func__);
1643 goto fail;
1644 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001645
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001646 errno = 0;
1647 while (waitpid(pid, &status, 0) < 0) {
1648 if (errno != EINTR) {
1649 error("%s: waitpid %ld: %s",
1650 __func__, (long)pid, strerror(errno));
1651 goto fail;
1652 }
1653 }
1654 if (!WIFEXITED(status)) {
1655 error("%s: exited abnormally", __func__);
1656 goto fail;
1657 }
1658 if (WEXITSTATUS(status) != 0) {
1659 error("%s: exited with status %d",
1660 __func__, WEXITSTATUS(status));
1661 goto fail;
1662 }
1663 if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
1664 error("%s: buffer error: %s", __func__, ssh_err(r));
1665 goto fail;
1666 }
1667 if (rversion != version) {
1668 error("%s: bad version", __func__);
1669 goto fail;
1670 }
1671 if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
1672 error("%s: buffer error: %s", __func__, ssh_err(r));
1673 fail:
1674 signal(SIGCHLD, osigchld);
1675 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001676 return -1;
1677 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001678 signal(SIGCHLD, osigchld);
1679 sshbuf_free(b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001680
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001681 return 0;
1682}
1683
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001684int
1685userauth_hostbased(Authctxt *authctxt)
1686{
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001687 struct ssh *ssh = active_state;
1688 struct sshkey *private = NULL;
1689 struct sshbuf *b = NULL;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001690 const char *service;
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001691 u_char *sig = NULL, *keyblob = NULL;
1692 char *fp = NULL, *chost = NULL, *lname = NULL;
1693 size_t siglen = 0, keylen = 0;
1694 int i, r, success = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001695
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001696 if (authctxt->ktypes == NULL) {
1697 authctxt->oktypes = xstrdup(options.hostbased_key_types);
1698 authctxt->ktypes = authctxt->oktypes;
1699 }
djm@openbsd.org1195f4c2015-01-08 10:14:08 +00001700
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001701 /*
1702 * Work through each listed type pattern in HostbasedKeyTypes,
1703 * trying each hostkey that matches the type in turn.
1704 */
1705 for (;;) {
1706 if (authctxt->active_ktype == NULL)
1707 authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
1708 if (authctxt->active_ktype == NULL ||
1709 *authctxt->active_ktype == '\0')
1710 break;
1711 debug3("%s: trying key type %s", __func__,
1712 authctxt->active_ktype);
1713
1714 /* check for a useful key */
1715 private = NULL;
1716 for (i = 0; i < authctxt->sensitive->nkeys; i++) {
1717 if (authctxt->sensitive->keys[i] == NULL ||
1718 authctxt->sensitive->keys[i]->type == KEY_RSA1 ||
1719 authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
1720 continue;
1721 if (match_pattern_list(
1722 sshkey_ssh_name(authctxt->sensitive->keys[i]),
djm@openbsd.orge661a862015-05-04 06:10:48 +00001723 authctxt->active_ktype, 0) != 1)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001724 continue;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001725 /* we take and free the key */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001726 private = authctxt->sensitive->keys[i];
1727 authctxt->sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001728 break;
1729 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001730 /* Found one */
1731 if (private != NULL)
1732 break;
1733 /* No more keys of this type; advance */
1734 authctxt->active_ktype = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001735 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001736 if (private == NULL) {
1737 free(authctxt->oktypes);
1738 authctxt->oktypes = authctxt->ktypes = NULL;
1739 authctxt->active_ktype = NULL;
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001740 debug("No more client hostkeys for hostbased authentication.");
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001741 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001742 }
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001743
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001744 if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
1745 SSH_FP_DEFAULT)) == NULL) {
1746 error("%s: sshkey_fingerprint failed", __func__);
1747 goto out;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001748 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001749 debug("%s: trying hostkey %s %s",
1750 __func__, sshkey_ssh_name(private), fp);
djm@openbsd.org17bf3d82014-12-11 05:13:28 +00001751
Damien Miller91c18472001-11-12 11:02:03 +11001752 /* figure out a name for the client host */
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001753 if ((lname = get_local_name(packet_get_connection_in())) == NULL) {
1754 error("%s: cannot get local ipaddr/name", __func__);
1755 goto out;
Damien Miller91c18472001-11-12 11:02:03 +11001756 }
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001757
1758 /* XXX sshbuf_put_stringf? */
1759 xasprintf(&chost, "%s.", lname);
1760 debug2("%s: chost %s", __func__, chost);
Damien Miller91c18472001-11-12 11:02:03 +11001761
Ben Lindstrom671388f2001-04-19 20:40:45 +00001762 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1763 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001764
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001765 /* construct data */
1766 if ((b = sshbuf_new()) == NULL) {
1767 error("%s: sshbuf_new failed", __func__);
1768 goto out;
1769 }
1770 if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
1771 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
1772 goto out;
1773 }
1774 if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
1775 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1776 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1777 (r = sshbuf_put_cstring(b, service)) != 0 ||
1778 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1779 (r = sshbuf_put_cstring(b, key_ssh_name(private))) != 0 ||
1780 (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
1781 (r = sshbuf_put_cstring(b, chost)) != 0 ||
1782 (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
1783 error("%s: buffer error: %s", __func__, ssh_err(r));
1784 goto out;
1785 }
1786
1787#ifdef DEBUG_PK
1788 sshbuf_dump(b, stderr);
1789#endif
1790 if (authctxt->sensitive->external_keysign)
1791 r = ssh_keysign(private, &sig, &siglen,
1792 sshbuf_ptr(b), sshbuf_len(b));
1793 else if ((r = sshkey_sign(private, &sig, &siglen,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +00001794 sshbuf_ptr(b), sshbuf_len(b), NULL, datafellows)) != 0)
djm@openbsd.org46347ed2015-01-30 11:43:14 +00001795 debug("%s: sshkey_sign: %s", __func__, ssh_err(r));
1796 if (r != 0) {
1797 error("sign using hostkey %s %s failed",
1798 sshkey_ssh_name(private), fp);
1799 goto out;
1800 }
1801 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1802 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1803 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1804 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1805 (r = sshpkt_put_cstring(ssh, key_ssh_name(private))) != 0 ||
1806 (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
1807 (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
1808 (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
1809 (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
1810 (r = sshpkt_send(ssh)) != 0) {
1811 error("%s: packet error: %s", __func__, ssh_err(r));
1812 goto out;
1813 }
1814 success = 1;
1815
1816 out:
1817 if (sig != NULL) {
1818 explicit_bzero(sig, siglen);
1819 free(sig);
1820 }
1821 free(keyblob);
1822 free(lname);
1823 free(fp);
1824 free(chost);
1825 sshkey_free(private);
1826 sshbuf_free(b);
1827
1828 return success;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001829}
1830
Damien Miller62cee002000-09-23 17:15:56 +11001831/* find auth method */
1832
Damien Miller62cee002000-09-23 17:15:56 +11001833/*
1834 * given auth method name, if configurable options permit this method fill
1835 * in auth_ident field and return true, otherwise return false.
1836 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001837static int
Damien Miller62cee002000-09-23 17:15:56 +11001838authmethod_is_enabled(Authmethod *method)
1839{
1840 if (method == NULL)
1841 return 0;
1842 /* return false if options indicate this method is disabled */
1843 if (method->enabled == NULL || *method->enabled == 0)
1844 return 0;
1845 /* return false if batch mode is enabled but method needs interactive mode */
1846 if (method->batch_flag != NULL && *method->batch_flag != 0)
1847 return 0;
1848 return 1;
1849}
1850
Ben Lindstrombba81212001-06-25 05:01:22 +00001851static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001852authmethod_lookup(const char *name)
1853{
1854 Authmethod *method = NULL;
1855 if (name != NULL)
1856 for (method = authmethods; method->name != NULL; method++)
1857 if (strcmp(name, method->name) == 0)
1858 return method;
1859 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1860 return NULL;
1861}
1862
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001863/* XXX internal state */
1864static Authmethod *current = NULL;
1865static char *supported = NULL;
1866static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001867
Damien Miller62cee002000-09-23 17:15:56 +11001868/*
1869 * Given the authentication method list sent by the server, return the
1870 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001871 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001872 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001873static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001874authmethod_get(char *authlist)
1875{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001876 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001877 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001878
Damien Miller62cee002000-09-23 17:15:56 +11001879 /* Use a suitable default if we're passed a nil list. */
1880 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001881 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001882
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001883 if (supported == NULL || strcmp(authlist, supported) != 0) {
1884 debug3("start over, passed a different list %s", authlist);
Darren Tuckera627d422013-06-02 07:31:17 +10001885 free(supported);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001886 supported = xstrdup(authlist);
1887 preferred = options.preferred_authentications;
1888 debug3("preferred %s", preferred);
1889 current = NULL;
1890 } else if (current != NULL && authmethod_is_enabled(current))
1891 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001892
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001893 for (;;) {
1894 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001895 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001896 current = NULL;
1897 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001898 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001899 preferred += next;
1900 debug3("authmethod_lookup %s", name);
1901 debug3("remaining preferred: %s", preferred);
1902 if ((current = authmethod_lookup(name)) != NULL &&
1903 authmethod_is_enabled(current)) {
1904 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001905 debug("Next authentication method: %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10001906 free(name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001907 return current;
1908 }
Darren Tuckere52a2602013-06-06 08:22:05 +10001909 free(name);
Damien Millereba71ba2000-04-29 23:57:08 +10001910 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001911}
Damien Miller62cee002000-09-23 17:15:56 +11001912
Ben Lindstrom79073822001-07-04 03:42:30 +00001913static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001914authmethods_get(void)
1915{
1916 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001917 Buffer b;
1918 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001919
Damien Miller0e3b8722002-01-22 23:26:38 +11001920 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001921 for (method = authmethods; method->name != NULL; method++) {
1922 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001923 if (buffer_len(&b) > 0)
1924 buffer_append(&b, ",", 1);
1925 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001926 }
Damien Miller62cee002000-09-23 17:15:56 +11001927 }
djm@openbsd.org1a31d022016-05-02 08:49:03 +00001928 if ((list = sshbuf_dup_string(&b)) == NULL)
1929 fatal("%s: sshbuf_dup_string failed", __func__);
Damien Miller0e3b8722002-01-22 23:26:38 +11001930 buffer_free(&b);
1931 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001932}
Damien Miller01ed2272008-11-05 16:20:46 +11001933