blob: ba56f6433326b48d887ca63d4d852ca91784f449 [file] [log] [blame]
Adam Langleyd0592972015-03-30 14:49:51 -07001/* $OpenBSD: sshconnect2.c,v 1.223 2015/01/30 11:43:14 djm Exp $ */
Greg Hartmanbd77cf72015-02-25 13:21:06 -08002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Copyright (c) 2008 Damien Miller. All rights reserved.
5 *
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.
14 *
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"
28
29#include <sys/types.h>
30#include <sys/socket.h>
31#include <sys/wait.h>
32#include <sys/stat.h>
33
34#include <errno.h>
35#include <fcntl.h>
36#include <netdb.h>
37#include <pwd.h>
38#include <signal.h>
39#include <stdarg.h>
40#include <stdio.h>
41#include <string.h>
42#include <unistd.h>
Adam Langleyd0592972015-03-30 14:49:51 -070043#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
Greg Hartmanbd77cf72015-02-25 13:21:06 -080044#include <vis.h>
45#endif
46
47#include "openbsd-compat/sys-queue.h"
48
49#include "xmalloc.h"
50#include "ssh.h"
51#include "ssh2.h"
52#include "buffer.h"
53#include "packet.h"
54#include "compat.h"
55#include "cipher.h"
56#include "key.h"
57#include "kex.h"
58#include "myproposal.h"
59#include "sshconnect.h"
60#include "authfile.h"
61#include "dh.h"
62#include "authfd.h"
63#include "log.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -080064#include "misc.h"
Adam Langleyd0592972015-03-30 14:49:51 -070065#include "readconf.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -080066#include "match.h"
67#include "dispatch.h"
68#include "canohost.h"
69#include "msg.h"
70#include "pathnames.h"
71#include "uidswap.h"
72#include "hostfile.h"
Adam Langleyd0592972015-03-30 14:49:51 -070073#include "ssherr.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -080074
75#ifdef GSSAPI
76#include "ssh-gss.h"
77#endif
78
79/* import */
80extern char *client_version_string;
81extern char *server_version_string;
82extern Options options;
83
84/*
85 * SSH2 key exchange
86 */
87
88u_char *session_id2 = NULL;
89u_int session_id2_len = 0;
90
91char *xxx_host;
92struct sockaddr *xxx_hostaddr;
93
Greg Hartmanbd77cf72015-02-25 13:21:06 -080094static int
Adam Langleyd0592972015-03-30 14:49:51 -070095verify_host_key_callback(Key *hostkey, struct ssh *ssh)
Greg Hartmanbd77cf72015-02-25 13:21:06 -080096{
97 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
98 fatal("Host key verification failed.");
99 return 0;
100}
101
102static 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;
109 u_int i;
110
111 /* Find all hostkeys for this hostname */
112 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
113 hostkeys = init_hostkeys();
114 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]);
118
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') {
Adam Langleyd0592972015-03-30 14:49:51 -0700133 if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800134 fatal("%s: unknown alg %s", __func__, alg);
135 if (lookup_key_in_hostkeys_by_type(hostkeys,
Adam Langleyd0592972015-03-30 14:49:51 -0700136 sshkey_type_plain(ktype), NULL))
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800137 ALG_APPEND(first, alg);
138 else
139 ALG_APPEND(last, alg);
140 }
141#undef ALG_APPEND
Adam Langleyd0592972015-03-30 14:49:51 -0700142 xasprintf(&ret, "%s%s%s", first,
143 (*first == '\0' || *last == '\0') ? "" : ",", last);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800144 if (*first != '\0')
145 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
146
Adam Langleyd0592972015-03-30 14:49:51 -0700147 free(first);
148 free(last);
149 free(hostname);
150 free(oavail);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800151 free_hostkeys(hostkeys);
152
153 return ret;
154}
155
156void
157ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
158{
Adam Langleyd0592972015-03-30 14:49:51 -0700159 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
160 struct kex *kex;
161 int r;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800162
163 xxx_host = host;
164 xxx_hostaddr = hostaddr;
165
166 if (options.ciphers == (char *)-1) {
167 logit("No valid ciphers for protocol version 2 given, using defaults.");
168 options.ciphers = NULL;
169 }
170 if (options.ciphers != NULL) {
171 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
172 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
173 }
174 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
175 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
176 myproposal[PROPOSAL_ENC_ALGS_STOC] =
177 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
178 if (options.compression) {
179 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
180 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
181 } else {
182 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
183 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
184 }
185 if (options.macs != NULL) {
186 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
187 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
188 }
189 if (options.hostkeyalgorithms != NULL)
190 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Adam Langleyd0592972015-03-30 14:49:51 -0700191 compat_pkalg_proposal(options.hostkeyalgorithms);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800192 else {
193 /* Prefer algorithms that we already have keys for */
194 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Adam Langleyd0592972015-03-30 14:49:51 -0700195 compat_pkalg_proposal(
196 order_hostkeyalgs(host, hostaddr, port));
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800197 }
198 if (options.kex_algorithms != NULL)
199 myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
Adam Langleyd0592972015-03-30 14:49:51 -0700200 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(
201 myproposal[PROPOSAL_KEX_ALGS]);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800202
Adam Langleyd0592972015-03-30 14:49:51 -0700203 if (options.rekey_limit || options.rekey_interval)
204 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
205 (time_t)options.rekey_interval);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800206
207 /* start key exchange */
Adam Langleyd0592972015-03-30 14:49:51 -0700208 if ((r = kex_setup(active_state, myproposal)) != 0)
209 fatal("kex_setup: %s", ssh_err(r));
210 kex = active_state->kex;
211#ifdef WITH_OPENSSL
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800212 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
213 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
214 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
215 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Adam Langleyd0592972015-03-30 14:49:51 -0700216# ifdef OPENSSL_HAS_ECC
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800217 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
Adam Langleyd0592972015-03-30 14:49:51 -0700218# endif
219#endif
220 kex->kex[KEX_C25519_SHA256] = kexc25519_client;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800221 kex->client_version_string=client_version_string;
222 kex->server_version_string=server_version_string;
223 kex->verify_host_key=&verify_host_key_callback;
224
Adam Langleyd0592972015-03-30 14:49:51 -0700225 dispatch_run(DISPATCH_BLOCK, &kex->done, active_state);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800226
227 if (options.use_roaming && !kex->roaming) {
228 debug("Roaming not allowed by server");
229 options.use_roaming = 0;
230 }
231
232 session_id2 = kex->session_id;
233 session_id2_len = kex->session_id_len;
234
235#ifdef DEBUG_KEXDH
236 /* send 1st encrypted/maced/compressed message */
237 packet_start(SSH2_MSG_IGNORE);
238 packet_put_cstring("markus");
239 packet_send();
240 packet_write_wait();
241#endif
242}
243
244/*
245 * Authenticate user
246 */
247
Adam Langleyd0592972015-03-30 14:49:51 -0700248typedef struct cauthctxt Authctxt;
249typedef struct cauthmethod Authmethod;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800250typedef struct identity Identity;
251typedef struct idlist Idlist;
252
253struct identity {
254 TAILQ_ENTRY(identity) next;
Adam Langleyd0592972015-03-30 14:49:51 -0700255 int agent_fd; /* >=0 if agent supports key */
256 struct sshkey *key; /* public/private key */
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800257 char *filename; /* comment for agent-only keys */
258 int tried;
259 int isprivate; /* key points to the private key */
Adam Langleyd0592972015-03-30 14:49:51 -0700260 int userprovided;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800261};
262TAILQ_HEAD(idlist, identity);
263
Adam Langleyd0592972015-03-30 14:49:51 -0700264struct cauthctxt {
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800265 const char *server_user;
266 const char *local_user;
267 const char *host;
268 const char *service;
Adam Langleyd0592972015-03-30 14:49:51 -0700269 struct cauthmethod *method;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800270 sig_atomic_t success;
271 char *authlist;
Adam Langleyd0592972015-03-30 14:49:51 -0700272 int attempt;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800273 /* pubkey */
Adam Langleyd0592972015-03-30 14:49:51 -0700274 struct idlist keys;
275 int agent_fd;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800276 /* hostbased */
277 Sensitive *sensitive;
Adam Langleyd0592972015-03-30 14:49:51 -0700278 char *oktypes, *ktypes;
279 const char *active_ktype;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800280 /* kbd-interactive */
281 int info_req_seen;
282 /* generic */
283 void *methoddata;
284};
Adam Langleyd0592972015-03-30 14:49:51 -0700285
286struct cauthmethod {
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800287 char *name; /* string to compare against server's list */
288 int (*userauth)(Authctxt *authctxt);
289 void (*cleanup)(Authctxt *authctxt);
290 int *enabled; /* flag in option struct that enables method */
291 int *batch_flag; /* flag in option struct that disables method */
292};
293
Adam Langleyd0592972015-03-30 14:49:51 -0700294int input_userauth_success(int, u_int32_t, void *);
295int input_userauth_success_unexpected(int, u_int32_t, void *);
296int input_userauth_failure(int, u_int32_t, void *);
297int input_userauth_banner(int, u_int32_t, void *);
298int input_userauth_error(int, u_int32_t, void *);
299int input_userauth_info_req(int, u_int32_t, void *);
300int input_userauth_pk_ok(int, u_int32_t, void *);
301int input_userauth_passwd_changereq(int, u_int32_t, void *);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800302
303int userauth_none(Authctxt *);
304int userauth_pubkey(Authctxt *);
305int userauth_passwd(Authctxt *);
306int userauth_kbdint(Authctxt *);
307int userauth_hostbased(Authctxt *);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800308
309#ifdef GSSAPI
310int userauth_gssapi(Authctxt *authctxt);
Adam Langleyd0592972015-03-30 14:49:51 -0700311int input_gssapi_response(int type, u_int32_t, void *);
312int input_gssapi_token(int type, u_int32_t, void *);
313int input_gssapi_hash(int type, u_int32_t, void *);
314int input_gssapi_error(int, u_int32_t, void *);
315int input_gssapi_errtok(int, u_int32_t, void *);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800316#endif
317
318void userauth(Authctxt *, char *);
319
320static int sign_and_send_pubkey(Authctxt *, Identity *);
321static void pubkey_prepare(Authctxt *);
322static void pubkey_cleanup(Authctxt *);
Adam Langleyd0592972015-03-30 14:49:51 -0700323static Key *load_identity_file(char *, int);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800324
325static Authmethod *authmethod_get(char *authlist);
326static Authmethod *authmethod_lookup(const char *name);
327static char *authmethods_get(void);
328
329Authmethod authmethods[] = {
330#ifdef GSSAPI
331 {"gssapi-with-mic",
332 userauth_gssapi,
333 NULL,
334 &options.gss_authentication,
335 NULL},
336#endif
337 {"hostbased",
338 userauth_hostbased,
339 NULL,
340 &options.hostbased_authentication,
341 NULL},
342 {"publickey",
343 userauth_pubkey,
344 NULL,
345 &options.pubkey_authentication,
346 NULL},
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800347 {"keyboard-interactive",
348 userauth_kbdint,
349 NULL,
350 &options.kbd_interactive_authentication,
351 &options.batch_mode},
352 {"password",
353 userauth_passwd,
354 NULL,
355 &options.password_authentication,
356 &options.batch_mode},
357 {"none",
358 userauth_none,
359 NULL,
360 NULL,
361 NULL},
362 {NULL, NULL, NULL, NULL, NULL}
363};
364
365void
366ssh_userauth2(const char *local_user, const char *server_user, char *host,
367 Sensitive *sensitive)
368{
369 Authctxt authctxt;
370 int type;
371
372 if (options.challenge_response_authentication)
373 options.kbd_interactive_authentication = 1;
374
375 packet_start(SSH2_MSG_SERVICE_REQUEST);
376 packet_put_cstring("ssh-userauth");
377 packet_send();
378 debug("SSH2_MSG_SERVICE_REQUEST sent");
379 packet_write_wait();
380 type = packet_read();
381 if (type != SSH2_MSG_SERVICE_ACCEPT)
382 fatal("Server denied authentication request: %d", type);
383 if (packet_remaining() > 0) {
384 char *reply = packet_get_string(NULL);
385 debug2("service_accept: %s", reply);
Adam Langleyd0592972015-03-30 14:49:51 -0700386 free(reply);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800387 } else {
388 debug2("buggy server: service_accept w/o service");
389 }
390 packet_check_eom();
391 debug("SSH2_MSG_SERVICE_ACCEPT received");
392
393 if (options.preferred_authentications == NULL)
394 options.preferred_authentications = authmethods_get();
395
396 /* setup authentication context */
397 memset(&authctxt, 0, sizeof(authctxt));
398 pubkey_prepare(&authctxt);
399 authctxt.server_user = server_user;
400 authctxt.local_user = local_user;
401 authctxt.host = host;
402 authctxt.service = "ssh-connection"; /* service name */
403 authctxt.success = 0;
404 authctxt.method = authmethod_lookup("none");
405 authctxt.authlist = NULL;
406 authctxt.methoddata = NULL;
407 authctxt.sensitive = sensitive;
Adam Langleyd0592972015-03-30 14:49:51 -0700408 authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800409 authctxt.info_req_seen = 0;
Adam Langleyd0592972015-03-30 14:49:51 -0700410 authctxt.agent_fd = -1;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800411 if (authctxt.method == NULL)
412 fatal("ssh_userauth2: internal error: cannot send userauth none request");
413
414 /* initial userauth request */
415 userauth_none(&authctxt);
416
417 dispatch_init(&input_userauth_error);
418 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
419 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
420 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
421 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
422
423 pubkey_cleanup(&authctxt);
424 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
425
426 debug("Authentication succeeded (%s).", authctxt.method->name);
427}
428
429void
430userauth(Authctxt *authctxt, char *authlist)
431{
432 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
433 authctxt->method->cleanup(authctxt);
434
Adam Langleyd0592972015-03-30 14:49:51 -0700435 free(authctxt->methoddata);
436 authctxt->methoddata = NULL;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800437 if (authlist == NULL) {
438 authlist = authctxt->authlist;
439 } else {
Adam Langleyd0592972015-03-30 14:49:51 -0700440 free(authctxt->authlist);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800441 authctxt->authlist = authlist;
442 }
443 for (;;) {
444 Authmethod *method = authmethod_get(authlist);
445 if (method == NULL)
446 fatal("Permission denied (%s).", authlist);
447 authctxt->method = method;
448
449 /* reset the per method handler */
450 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
451 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
452
453 /* and try new method */
454 if (method->userauth(authctxt) != 0) {
455 debug2("we sent a %s packet, wait for reply", method->name);
456 break;
457 } else {
458 debug2("we did not send a packet, disable method");
459 method->enabled = NULL;
460 }
461 }
462}
463
464/* ARGSUSED */
Adam Langleyd0592972015-03-30 14:49:51 -0700465int
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800466input_userauth_error(int type, u_int32_t seq, void *ctxt)
467{
468 fatal("input_userauth_error: bad message during authentication: "
469 "type %d", type);
Adam Langleyd0592972015-03-30 14:49:51 -0700470 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800471}
472
473/* ARGSUSED */
Adam Langleyd0592972015-03-30 14:49:51 -0700474int
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800475input_userauth_banner(int type, u_int32_t seq, void *ctxt)
476{
477 char *msg, *raw, *lang;
478 u_int len;
479
480 debug3("input_userauth_banner");
481 raw = packet_get_string(&len);
482 lang = packet_get_string(NULL);
483 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
484 if (len > 65536)
485 len = 65536;
486 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
487 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
488 fprintf(stderr, "%s", msg);
Adam Langleyd0592972015-03-30 14:49:51 -0700489 free(msg);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800490 }
Adam Langleyd0592972015-03-30 14:49:51 -0700491 free(raw);
492 free(lang);
493 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800494}
495
496/* ARGSUSED */
Adam Langleyd0592972015-03-30 14:49:51 -0700497int
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800498input_userauth_success(int type, u_int32_t seq, void *ctxt)
499{
500 Authctxt *authctxt = ctxt;
501
502 if (authctxt == NULL)
503 fatal("input_userauth_success: no authentication context");
Adam Langleyd0592972015-03-30 14:49:51 -0700504 free(authctxt->authlist);
505 authctxt->authlist = NULL;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800506 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
507 authctxt->method->cleanup(authctxt);
Adam Langleyd0592972015-03-30 14:49:51 -0700508 free(authctxt->methoddata);
509 authctxt->methoddata = NULL;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800510 authctxt->success = 1; /* break out */
Adam Langleyd0592972015-03-30 14:49:51 -0700511 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800512}
513
Adam Langleyd0592972015-03-30 14:49:51 -0700514int
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800515input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
516{
517 Authctxt *authctxt = ctxt;
518
519 if (authctxt == NULL)
520 fatal("%s: no authentication context", __func__);
521
522 fatal("Unexpected authentication success during %s.",
523 authctxt->method->name);
Adam Langleyd0592972015-03-30 14:49:51 -0700524 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800525}
526
527/* ARGSUSED */
Adam Langleyd0592972015-03-30 14:49:51 -0700528int
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800529input_userauth_failure(int type, u_int32_t seq, void *ctxt)
530{
531 Authctxt *authctxt = ctxt;
532 char *authlist = NULL;
533 int partial;
534
535 if (authctxt == NULL)
536 fatal("input_userauth_failure: no authentication context");
537
538 authlist = packet_get_string(NULL);
539 partial = packet_get_char();
540 packet_check_eom();
541
Adam Langleyd0592972015-03-30 14:49:51 -0700542 if (partial != 0) {
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800543 logit("Authenticated with partial success.");
Adam Langleyd0592972015-03-30 14:49:51 -0700544 /* reset state */
545 pubkey_cleanup(authctxt);
546 pubkey_prepare(authctxt);
547 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800548 debug("Authentications that can continue: %s", authlist);
549
550 userauth(authctxt, authlist);
Adam Langleyd0592972015-03-30 14:49:51 -0700551 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800552}
553
554/* ARGSUSED */
Adam Langleyd0592972015-03-30 14:49:51 -0700555int
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800556input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
557{
558 Authctxt *authctxt = ctxt;
559 Key *key = NULL;
560 Identity *id = NULL;
561 Buffer b;
562 int pktype, sent = 0;
563 u_int alen, blen;
564 char *pkalg, *fp;
565 u_char *pkblob;
566
567 if (authctxt == NULL)
568 fatal("input_userauth_pk_ok: no authentication context");
569 if (datafellows & SSH_BUG_PKOK) {
570 /* this is similar to SSH_BUG_PKAUTH */
571 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
572 pkblob = packet_get_string(&blen);
573 buffer_init(&b);
574 buffer_append(&b, pkblob, blen);
575 pkalg = buffer_get_string(&b, &alen);
576 buffer_free(&b);
577 } else {
578 pkalg = packet_get_string(&alen);
579 pkblob = packet_get_string(&blen);
580 }
581 packet_check_eom();
582
583 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
584
585 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
586 debug("unknown pkalg %s", pkalg);
587 goto done;
588 }
589 if ((key = key_from_blob(pkblob, blen)) == NULL) {
590 debug("no key from blob. pkalg %s", pkalg);
591 goto done;
592 }
593 if (key->type != pktype) {
594 error("input_userauth_pk_ok: type mismatch "
595 "for decoded key (received %d, expected %d)",
596 key->type, pktype);
597 goto done;
598 }
Adam Langleyd0592972015-03-30 14:49:51 -0700599 if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
600 SSH_FP_DEFAULT)) == NULL)
601 goto done;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800602 debug2("input_userauth_pk_ok: fp %s", fp);
Adam Langleyd0592972015-03-30 14:49:51 -0700603 free(fp);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800604
605 /*
606 * search keys in the reverse order, because last candidate has been
607 * moved to the end of the queue. this also avoids confusion by
608 * duplicate keys
609 */
610 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
611 if (key_equal(key, id->key)) {
612 sent = sign_and_send_pubkey(authctxt, id);
613 break;
614 }
615 }
616done:
617 if (key != NULL)
618 key_free(key);
Adam Langleyd0592972015-03-30 14:49:51 -0700619 free(pkalg);
620 free(pkblob);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800621
622 /* try another method if we did not send a packet */
623 if (sent == 0)
624 userauth(authctxt, NULL);
Adam Langleyd0592972015-03-30 14:49:51 -0700625 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800626}
627
628#ifdef GSSAPI
629int
630userauth_gssapi(Authctxt *authctxt)
631{
632 Gssctxt *gssctxt = NULL;
633 static gss_OID_set gss_supported = NULL;
634 static u_int mech = 0;
635 OM_uint32 min;
636 int ok = 0;
637
638 /* Try one GSSAPI method at a time, rather than sending them all at
639 * once. */
640
641 if (gss_supported == NULL)
642 gss_indicate_mechs(&min, &gss_supported);
643
644 /* Check to see if the mechanism is usable before we offer it */
645 while (mech < gss_supported->count && !ok) {
646 /* My DER encoding requires length<128 */
647 if (gss_supported->elements[mech].length < 128 &&
648 ssh_gssapi_check_mechanism(&gssctxt,
649 &gss_supported->elements[mech], authctxt->host)) {
650 ok = 1; /* Mechanism works */
651 } else {
652 mech++;
653 }
654 }
655
656 if (!ok)
657 return 0;
658
659 authctxt->methoddata=(void *)gssctxt;
660
661 packet_start(SSH2_MSG_USERAUTH_REQUEST);
662 packet_put_cstring(authctxt->server_user);
663 packet_put_cstring(authctxt->service);
664 packet_put_cstring(authctxt->method->name);
665
666 packet_put_int(1);
667
668 packet_put_int((gss_supported->elements[mech].length) + 2);
669 packet_put_char(SSH_GSS_OIDTYPE);
670 packet_put_char(gss_supported->elements[mech].length);
671 packet_put_raw(gss_supported->elements[mech].elements,
672 gss_supported->elements[mech].length);
673
674 packet_send();
675
676 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
677 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
678 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
679 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
680
681 mech++; /* Move along to next candidate */
682
683 return 1;
684}
685
686static OM_uint32
687process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
688{
689 Authctxt *authctxt = ctxt;
690 Gssctxt *gssctxt = authctxt->methoddata;
691 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
692 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
693 gss_buffer_desc gssbuf;
694 OM_uint32 status, ms, flags;
695 Buffer b;
696
697 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
698 recv_tok, &send_tok, &flags);
699
700 if (send_tok.length > 0) {
701 if (GSS_ERROR(status))
702 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
703 else
704 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
705
706 packet_put_string(send_tok.value, send_tok.length);
707 packet_send();
708 gss_release_buffer(&ms, &send_tok);
709 }
710
711 if (status == GSS_S_COMPLETE) {
712 /* send either complete or MIC, depending on mechanism */
713 if (!(flags & GSS_C_INTEG_FLAG)) {
714 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
715 packet_send();
716 } else {
717 ssh_gssapi_buildmic(&b, authctxt->server_user,
718 authctxt->service, "gssapi-with-mic");
719
720 gssbuf.value = buffer_ptr(&b);
721 gssbuf.length = buffer_len(&b);
722
723 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
724
725 if (!GSS_ERROR(status)) {
726 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
727 packet_put_string(mic.value, mic.length);
728
729 packet_send();
730 }
731
732 buffer_free(&b);
733 gss_release_buffer(&ms, &mic);
734 }
735 }
736
737 return status;
738}
739
740/* ARGSUSED */
Adam Langleyd0592972015-03-30 14:49:51 -0700741int
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800742input_gssapi_response(int type, u_int32_t plen, void *ctxt)
743{
744 Authctxt *authctxt = ctxt;
745 Gssctxt *gssctxt;
746 int oidlen;
747 char *oidv;
748
749 if (authctxt == NULL)
750 fatal("input_gssapi_response: no authentication context");
751 gssctxt = authctxt->methoddata;
752
753 /* Setup our OID */
754 oidv = packet_get_string(&oidlen);
755
756 if (oidlen <= 2 ||
757 oidv[0] != SSH_GSS_OIDTYPE ||
758 oidv[1] != oidlen - 2) {
Adam Langleyd0592972015-03-30 14:49:51 -0700759 free(oidv);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800760 debug("Badly encoded mechanism OID received");
761 userauth(authctxt, NULL);
Adam Langleyd0592972015-03-30 14:49:51 -0700762 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800763 }
764
765 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
766 fatal("Server returned different OID than expected");
767
768 packet_check_eom();
769
Adam Langleyd0592972015-03-30 14:49:51 -0700770 free(oidv);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800771
772 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
773 /* Start again with next method on list */
774 debug("Trying to start again");
775 userauth(authctxt, NULL);
Adam Langleyd0592972015-03-30 14:49:51 -0700776 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800777 }
Adam Langleyd0592972015-03-30 14:49:51 -0700778 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800779}
780
781/* ARGSUSED */
Adam Langleyd0592972015-03-30 14:49:51 -0700782int
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800783input_gssapi_token(int type, u_int32_t plen, void *ctxt)
784{
785 Authctxt *authctxt = ctxt;
786 gss_buffer_desc recv_tok;
787 OM_uint32 status;
788 u_int slen;
789
790 if (authctxt == NULL)
791 fatal("input_gssapi_response: no authentication context");
792
793 recv_tok.value = packet_get_string(&slen);
794 recv_tok.length = slen; /* safe typecast */
795
796 packet_check_eom();
797
798 status = process_gssapi_token(ctxt, &recv_tok);
799
Adam Langleyd0592972015-03-30 14:49:51 -0700800 free(recv_tok.value);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800801
802 if (GSS_ERROR(status)) {
803 /* Start again with the next method in the list */
804 userauth(authctxt, NULL);
Adam Langleyd0592972015-03-30 14:49:51 -0700805 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800806 }
Adam Langleyd0592972015-03-30 14:49:51 -0700807 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800808}
809
810/* ARGSUSED */
Adam Langleyd0592972015-03-30 14:49:51 -0700811int
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800812input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
813{
814 Authctxt *authctxt = ctxt;
815 Gssctxt *gssctxt;
816 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
817 gss_buffer_desc recv_tok;
Adam Langleyd0592972015-03-30 14:49:51 -0700818 OM_uint32 ms;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800819 u_int len;
820
821 if (authctxt == NULL)
822 fatal("input_gssapi_response: no authentication context");
823 gssctxt = authctxt->methoddata;
824
825 recv_tok.value = packet_get_string(&len);
826 recv_tok.length = len;
827
828 packet_check_eom();
829
830 /* Stick it into GSSAPI and see what it says */
Adam Langleyd0592972015-03-30 14:49:51 -0700831 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800832 &recv_tok, &send_tok, NULL);
833
Adam Langleyd0592972015-03-30 14:49:51 -0700834 free(recv_tok.value);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800835 gss_release_buffer(&ms, &send_tok);
836
837 /* Server will be returning a failed packet after this one */
Adam Langleyd0592972015-03-30 14:49:51 -0700838 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800839}
840
841/* ARGSUSED */
Adam Langleyd0592972015-03-30 14:49:51 -0700842int
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800843input_gssapi_error(int type, u_int32_t plen, void *ctxt)
844{
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800845 char *msg;
846 char *lang;
847
Adam Langleyd0592972015-03-30 14:49:51 -0700848 /* maj */(void)packet_get_int();
849 /* min */(void)packet_get_int();
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800850 msg=packet_get_string(NULL);
851 lang=packet_get_string(NULL);
852
853 packet_check_eom();
854
855 debug("Server GSSAPI Error:\n%s", msg);
Adam Langleyd0592972015-03-30 14:49:51 -0700856 free(msg);
857 free(lang);
858 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800859}
860#endif /* GSSAPI */
861
862int
863userauth_none(Authctxt *authctxt)
864{
865 /* initial userauth request */
866 packet_start(SSH2_MSG_USERAUTH_REQUEST);
867 packet_put_cstring(authctxt->server_user);
868 packet_put_cstring(authctxt->service);
869 packet_put_cstring(authctxt->method->name);
870 packet_send();
871 return 1;
872}
873
874int
875userauth_passwd(Authctxt *authctxt)
876{
877 static int attempt = 0;
878 char prompt[150];
879 char *password;
880 const char *host = options.host_key_alias ? options.host_key_alias :
881 authctxt->host;
882
883 if (attempt++ >= options.number_of_password_prompts)
884 return 0;
885
886 if (attempt != 1)
887 error("Permission denied, please try again.");
888
889 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
890 authctxt->server_user, host);
891 password = read_passphrase(prompt, 0);
892 packet_start(SSH2_MSG_USERAUTH_REQUEST);
893 packet_put_cstring(authctxt->server_user);
894 packet_put_cstring(authctxt->service);
895 packet_put_cstring(authctxt->method->name);
896 packet_put_char(0);
897 packet_put_cstring(password);
Adam Langleyd0592972015-03-30 14:49:51 -0700898 explicit_bzero(password, strlen(password));
899 free(password);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800900 packet_add_padding(64);
901 packet_send();
902
903 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
904 &input_userauth_passwd_changereq);
905
906 return 1;
907}
908
909/*
910 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
911 */
912/* ARGSUSED */
Adam Langleyd0592972015-03-30 14:49:51 -0700913int
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800914input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
915{
916 Authctxt *authctxt = ctxt;
917 char *info, *lang, *password = NULL, *retype = NULL;
918 char prompt[150];
919 const char *host = options.host_key_alias ? options.host_key_alias :
920 authctxt->host;
921
922 debug2("input_userauth_passwd_changereq");
923
924 if (authctxt == NULL)
925 fatal("input_userauth_passwd_changereq: "
926 "no authentication context");
927
928 info = packet_get_string(NULL);
929 lang = packet_get_string(NULL);
930 if (strlen(info) > 0)
931 logit("%s", info);
Adam Langleyd0592972015-03-30 14:49:51 -0700932 free(info);
933 free(lang);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800934 packet_start(SSH2_MSG_USERAUTH_REQUEST);
935 packet_put_cstring(authctxt->server_user);
936 packet_put_cstring(authctxt->service);
937 packet_put_cstring(authctxt->method->name);
938 packet_put_char(1); /* additional info */
939 snprintf(prompt, sizeof(prompt),
940 "Enter %.30s@%.128s's old password: ",
941 authctxt->server_user, host);
942 password = read_passphrase(prompt, 0);
943 packet_put_cstring(password);
Adam Langleyd0592972015-03-30 14:49:51 -0700944 explicit_bzero(password, strlen(password));
945 free(password);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800946 password = NULL;
947 while (password == NULL) {
948 snprintf(prompt, sizeof(prompt),
949 "Enter %.30s@%.128s's new password: ",
950 authctxt->server_user, host);
951 password = read_passphrase(prompt, RP_ALLOW_EOF);
952 if (password == NULL) {
953 /* bail out */
Adam Langleyd0592972015-03-30 14:49:51 -0700954 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800955 }
956 snprintf(prompt, sizeof(prompt),
957 "Retype %.30s@%.128s's new password: ",
958 authctxt->server_user, host);
959 retype = read_passphrase(prompt, 0);
960 if (strcmp(password, retype) != 0) {
Adam Langleyd0592972015-03-30 14:49:51 -0700961 explicit_bzero(password, strlen(password));
962 free(password);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800963 logit("Mismatch; try again, EOF to quit.");
964 password = NULL;
965 }
Adam Langleyd0592972015-03-30 14:49:51 -0700966 explicit_bzero(retype, strlen(retype));
967 free(retype);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800968 }
969 packet_put_cstring(password);
Adam Langleyd0592972015-03-30 14:49:51 -0700970 explicit_bzero(password, strlen(password));
971 free(password);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800972 packet_add_padding(64);
973 packet_send();
974
975 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
976 &input_userauth_passwd_changereq);
Adam Langleyd0592972015-03-30 14:49:51 -0700977 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800978}
979
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800980static int
Adam Langleyd0592972015-03-30 14:49:51 -0700981identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
982 const u_char *data, size_t datalen, u_int compat)
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800983{
984 Key *prv;
985 int ret;
986
987 /* the agent supports this key */
Adam Langleyd0592972015-03-30 14:49:51 -0700988 if (id->agent_fd)
989 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
990 data, datalen, compat);
991
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800992 /*
993 * we have already loaded the private key or
994 * the private key is stored in external hardware
995 */
Adam Langleyd0592972015-03-30 14:49:51 -0700996 if (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))
997 return (sshkey_sign(id->key, sigp, lenp, data, datalen,
998 compat));
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800999 /* load the private key from the file */
Adam Langleyd0592972015-03-30 14:49:51 -07001000 if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL)
1001 return (-1); /* XXX return decent error code */
1002 ret = sshkey_sign(prv, sigp, lenp, data, datalen, compat);
1003 sshkey_free(prv);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001004 return (ret);
1005}
1006
1007static int
1008sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1009{
1010 Buffer b;
1011 u_char *blob, *signature;
Adam Langleyd0592972015-03-30 14:49:51 -07001012 u_int bloblen;
1013 size_t slen;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001014 u_int skip = 0;
1015 int ret = -1;
1016 int have_sig = 1;
1017 char *fp;
1018
Adam Langleyd0592972015-03-30 14:49:51 -07001019 if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1020 SSH_FP_DEFAULT)) == NULL)
1021 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001022 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
Adam Langleyd0592972015-03-30 14:49:51 -07001023 free(fp);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001024
1025 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1026 /* we cannot handle this key */
1027 debug3("sign_and_send_pubkey: cannot handle key");
1028 return 0;
1029 }
1030 /* data to be signed */
1031 buffer_init(&b);
1032 if (datafellows & SSH_OLD_SESSIONID) {
1033 buffer_append(&b, session_id2, session_id2_len);
1034 skip = session_id2_len;
1035 } else {
1036 buffer_put_string(&b, session_id2, session_id2_len);
1037 skip = buffer_len(&b);
1038 }
1039 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1040 buffer_put_cstring(&b, authctxt->server_user);
1041 buffer_put_cstring(&b,
1042 datafellows & SSH_BUG_PKSERVICE ?
1043 "ssh-userauth" :
1044 authctxt->service);
1045 if (datafellows & SSH_BUG_PKAUTH) {
1046 buffer_put_char(&b, have_sig);
1047 } else {
1048 buffer_put_cstring(&b, authctxt->method->name);
1049 buffer_put_char(&b, have_sig);
1050 buffer_put_cstring(&b, key_ssh_name(id->key));
1051 }
1052 buffer_put_string(&b, blob, bloblen);
1053
1054 /* generate signature */
1055 ret = identity_sign(id, &signature, &slen,
Adam Langleyd0592972015-03-30 14:49:51 -07001056 buffer_ptr(&b), buffer_len(&b), datafellows);
1057 if (ret != 0) {
1058 free(blob);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001059 buffer_free(&b);
1060 return 0;
1061 }
1062#ifdef DEBUG_PK
1063 buffer_dump(&b);
1064#endif
1065 if (datafellows & SSH_BUG_PKSERVICE) {
1066 buffer_clear(&b);
1067 buffer_append(&b, session_id2, session_id2_len);
1068 skip = session_id2_len;
1069 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1070 buffer_put_cstring(&b, authctxt->server_user);
1071 buffer_put_cstring(&b, authctxt->service);
1072 buffer_put_cstring(&b, authctxt->method->name);
1073 buffer_put_char(&b, have_sig);
1074 if (!(datafellows & SSH_BUG_PKAUTH))
1075 buffer_put_cstring(&b, key_ssh_name(id->key));
1076 buffer_put_string(&b, blob, bloblen);
1077 }
Adam Langleyd0592972015-03-30 14:49:51 -07001078 free(blob);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001079
1080 /* append signature */
1081 buffer_put_string(&b, signature, slen);
Adam Langleyd0592972015-03-30 14:49:51 -07001082 free(signature);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001083
1084 /* skip session id and packet type */
1085 if (buffer_len(&b) < skip + 1)
1086 fatal("userauth_pubkey: internal error");
1087 buffer_consume(&b, skip + 1);
1088
1089 /* put remaining data from buffer into packet */
1090 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1091 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1092 buffer_free(&b);
1093 packet_send();
1094
1095 return 1;
1096}
1097
1098static int
1099send_pubkey_test(Authctxt *authctxt, Identity *id)
1100{
1101 u_char *blob;
1102 u_int bloblen, have_sig = 0;
1103
1104 debug3("send_pubkey_test");
1105
1106 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1107 /* we cannot handle this key */
1108 debug3("send_pubkey_test: cannot handle key");
1109 return 0;
1110 }
1111 /* register callback for USERAUTH_PK_OK message */
1112 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1113
1114 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1115 packet_put_cstring(authctxt->server_user);
1116 packet_put_cstring(authctxt->service);
1117 packet_put_cstring(authctxt->method->name);
1118 packet_put_char(have_sig);
1119 if (!(datafellows & SSH_BUG_PKAUTH))
1120 packet_put_cstring(key_ssh_name(id->key));
1121 packet_put_string(blob, bloblen);
Adam Langleyd0592972015-03-30 14:49:51 -07001122 free(blob);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001123 packet_send();
1124 return 1;
1125}
1126
1127static Key *
Adam Langleyd0592972015-03-30 14:49:51 -07001128load_identity_file(char *filename, int userprovided)
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001129{
1130 Key *private;
1131 char prompt[300], *passphrase;
Adam Langleyd0592972015-03-30 14:49:51 -07001132 int r, perm_ok = 0, quit = 0, i;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001133 struct stat st;
1134
1135 if (stat(filename, &st) < 0) {
Adam Langleyd0592972015-03-30 14:49:51 -07001136 (userprovided ? logit : debug3)("no such identity: %s: %s",
1137 filename, strerror(errno));
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001138 return NULL;
1139 }
Adam Langleyd0592972015-03-30 14:49:51 -07001140 snprintf(prompt, sizeof prompt,
1141 "Enter passphrase for key '%.100s': ", filename);
1142 for (i = 0; i <= options.number_of_password_prompts; i++) {
1143 if (i == 0)
1144 passphrase = "";
1145 else {
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001146 passphrase = read_passphrase(prompt, 0);
Adam Langleyd0592972015-03-30 14:49:51 -07001147 if (*passphrase == '\0') {
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001148 debug2("no passphrase given, try next key");
Adam Langleyd0592972015-03-30 14:49:51 -07001149 free(passphrase);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001150 break;
Adam Langleyd0592972015-03-30 14:49:51 -07001151 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001152 }
Adam Langleyd0592972015-03-30 14:49:51 -07001153 switch ((r = sshkey_load_private_type(KEY_UNSPEC, filename,
1154 passphrase, &private, NULL, &perm_ok))) {
1155 case 0:
1156 break;
1157 case SSH_ERR_KEY_WRONG_PASSPHRASE:
1158 if (options.batch_mode) {
1159 quit = 1;
1160 break;
1161 }
1162 if (i != 0)
1163 debug2("bad passphrase given, try again...");
1164 break;
1165 case SSH_ERR_SYSTEM_ERROR:
1166 if (errno == ENOENT) {
1167 debug2("Load key \"%s\": %s",
1168 filename, ssh_err(r));
1169 quit = 1;
1170 break;
1171 }
1172 /* FALLTHROUGH */
1173 default:
1174 error("Load key \"%s\": %s", filename, ssh_err(r));
1175 quit = 1;
1176 break;
1177 }
1178 if (i > 0) {
1179 explicit_bzero(passphrase, strlen(passphrase));
1180 free(passphrase);
1181 }
1182 if (private != NULL || quit)
1183 break;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001184 }
1185 return private;
1186}
1187
1188/*
1189 * try keys in the following order:
1190 * 1. agent keys that are found in the config file
1191 * 2. other agent keys
1192 * 3. keys that are only listed in the config file
1193 */
1194static void
1195pubkey_prepare(Authctxt *authctxt)
1196{
Adam Langleyd0592972015-03-30 14:49:51 -07001197 struct identity *id, *id2, *tmp;
1198 struct idlist agent, files, *preferred;
1199 struct sshkey *key;
1200 int agent_fd, i, r, found;
1201 size_t j;
1202 struct ssh_identitylist *idlist;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001203
1204 TAILQ_INIT(&agent); /* keys from the agent */
1205 TAILQ_INIT(&files); /* keys from the config file */
1206 preferred = &authctxt->keys;
1207 TAILQ_INIT(preferred); /* preferred order of keys */
1208
Adam Langleyd0592972015-03-30 14:49:51 -07001209 /* list of keys stored in the filesystem and PKCS#11 */
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001210 for (i = 0; i < options.num_identity_files; i++) {
1211 key = options.identity_keys[i];
1212 if (key && key->type == KEY_RSA1)
1213 continue;
1214 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1215 continue;
1216 options.identity_keys[i] = NULL;
1217 id = xcalloc(1, sizeof(*id));
1218 id->key = key;
1219 id->filename = xstrdup(options.identity_files[i]);
Adam Langleyd0592972015-03-30 14:49:51 -07001220 id->userprovided = options.identity_file_userprovided[i];
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001221 TAILQ_INSERT_TAIL(&files, id, next);
1222 }
Adam Langleyd0592972015-03-30 14:49:51 -07001223 /* Prefer PKCS11 keys that are explicitly listed */
1224 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1225 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
1226 continue;
1227 found = 0;
1228 TAILQ_FOREACH(id2, &files, next) {
1229 if (id2->key == NULL ||
1230 (id2->key->flags & SSHKEY_FLAG_EXT) == 0)
1231 continue;
1232 if (sshkey_equal(id->key, id2->key)) {
1233 TAILQ_REMOVE(&files, id, next);
1234 TAILQ_INSERT_TAIL(preferred, id, next);
1235 found = 1;
1236 break;
1237 }
1238 }
1239 /* If IdentitiesOnly set and key not found then don't use it */
1240 if (!found && options.identities_only) {
1241 TAILQ_REMOVE(&files, id, next);
1242 explicit_bzero(id, sizeof(*id));
1243 free(id);
1244 }
1245 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001246 /* list of keys supported by the agent */
Adam Langleyd0592972015-03-30 14:49:51 -07001247 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1248 if (r != SSH_ERR_AGENT_NOT_PRESENT)
1249 debug("%s: ssh_get_authentication_socket: %s",
1250 __func__, ssh_err(r));
1251 } else if ((r = ssh_fetch_identitylist(agent_fd, 2, &idlist)) != 0) {
1252 if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1253 debug("%s: ssh_fetch_identitylist: %s",
1254 __func__, ssh_err(r));
1255 } else {
1256 for (j = 0; j < idlist->nkeys; j++) {
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001257 found = 0;
1258 TAILQ_FOREACH(id, &files, next) {
Adam Langleyd0592972015-03-30 14:49:51 -07001259 /*
1260 * agent keys from the config file are
1261 * preferred
1262 */
1263 if (sshkey_equal(idlist->keys[j], id->key)) {
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001264 TAILQ_REMOVE(&files, id, next);
1265 TAILQ_INSERT_TAIL(preferred, id, next);
Adam Langleyd0592972015-03-30 14:49:51 -07001266 id->agent_fd = agent_fd;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001267 found = 1;
1268 break;
1269 }
1270 }
1271 if (!found && !options.identities_only) {
1272 id = xcalloc(1, sizeof(*id));
Adam Langleyd0592972015-03-30 14:49:51 -07001273 /* XXX "steals" key/comment from idlist */
1274 id->key = idlist->keys[j];
1275 id->filename = idlist->comments[j];
1276 idlist->keys[j] = NULL;
1277 idlist->comments[j] = NULL;
1278 id->agent_fd = agent_fd;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001279 TAILQ_INSERT_TAIL(&agent, id, next);
1280 }
1281 }
Adam Langleyd0592972015-03-30 14:49:51 -07001282 ssh_free_identitylist(idlist);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001283 /* append remaining agent keys */
1284 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1285 TAILQ_REMOVE(&agent, id, next);
1286 TAILQ_INSERT_TAIL(preferred, id, next);
1287 }
Adam Langleyd0592972015-03-30 14:49:51 -07001288 authctxt->agent_fd = agent_fd;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001289 }
1290 /* append remaining keys from the config file */
1291 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1292 TAILQ_REMOVE(&files, id, next);
1293 TAILQ_INSERT_TAIL(preferred, id, next);
1294 }
1295 TAILQ_FOREACH(id, preferred, next) {
Adam Langleyd0592972015-03-30 14:49:51 -07001296 debug2("key: %s (%p),%s", id->filename, id->key,
1297 id->userprovided ? " explicit" : "");
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001298 }
1299}
1300
1301static void
1302pubkey_cleanup(Authctxt *authctxt)
1303{
1304 Identity *id;
1305
Adam Langleyd0592972015-03-30 14:49:51 -07001306 if (authctxt->agent_fd != -1)
1307 ssh_close_authentication_socket(authctxt->agent_fd);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001308 for (id = TAILQ_FIRST(&authctxt->keys); id;
1309 id = TAILQ_FIRST(&authctxt->keys)) {
1310 TAILQ_REMOVE(&authctxt->keys, id, next);
1311 if (id->key)
Adam Langleyd0592972015-03-30 14:49:51 -07001312 sshkey_free(id->key);
1313 free(id->filename);
1314 free(id);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001315 }
1316}
1317
1318int
1319userauth_pubkey(Authctxt *authctxt)
1320{
1321 Identity *id;
1322 int sent = 0;
1323
1324 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1325 if (id->tried++)
1326 return (0);
1327 /* move key to the end of the queue */
1328 TAILQ_REMOVE(&authctxt->keys, id, next);
1329 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1330 /*
1331 * send a test message if we have the public key. for
1332 * encrypted keys we cannot do this and have to load the
1333 * private key instead
1334 */
Adam Langleyd0592972015-03-30 14:49:51 -07001335 if (id->key != NULL) {
1336 if (key_type_plain(id->key->type) == KEY_RSA &&
1337 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1338 debug("Skipped %s key %s for RSA/MD5 server",
1339 key_type(id->key), id->filename);
1340 } else if (id->key->type != KEY_RSA1) {
1341 debug("Offering %s public key: %s",
1342 key_type(id->key), id->filename);
1343 sent = send_pubkey_test(authctxt, id);
1344 }
1345 } else {
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001346 debug("Trying private key: %s", id->filename);
Adam Langleyd0592972015-03-30 14:49:51 -07001347 id->key = load_identity_file(id->filename,
1348 id->userprovided);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001349 if (id->key != NULL) {
1350 id->isprivate = 1;
Adam Langleyd0592972015-03-30 14:49:51 -07001351 if (key_type_plain(id->key->type) == KEY_RSA &&
1352 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1353 debug("Skipped %s key %s for RSA/MD5 "
1354 "server", key_type(id->key),
1355 id->filename);
1356 } else {
1357 sent = sign_and_send_pubkey(
1358 authctxt, id);
1359 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001360 key_free(id->key);
1361 id->key = NULL;
1362 }
1363 }
1364 if (sent)
1365 return (sent);
1366 }
1367 return (0);
1368}
1369
1370/*
1371 * Send userauth request message specifying keyboard-interactive method.
1372 */
1373int
1374userauth_kbdint(Authctxt *authctxt)
1375{
1376 static int attempt = 0;
1377
1378 if (attempt++ >= options.number_of_password_prompts)
1379 return 0;
1380 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1381 if (attempt > 1 && !authctxt->info_req_seen) {
1382 debug3("userauth_kbdint: disable: no info_req_seen");
1383 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1384 return 0;
1385 }
1386
1387 debug2("userauth_kbdint");
1388 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1389 packet_put_cstring(authctxt->server_user);
1390 packet_put_cstring(authctxt->service);
1391 packet_put_cstring(authctxt->method->name);
1392 packet_put_cstring(""); /* lang */
1393 packet_put_cstring(options.kbd_interactive_devices ?
1394 options.kbd_interactive_devices : "");
1395 packet_send();
1396
1397 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1398 return 1;
1399}
1400
1401/*
1402 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1403 */
Adam Langleyd0592972015-03-30 14:49:51 -07001404int
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001405input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1406{
1407 Authctxt *authctxt = ctxt;
1408 char *name, *inst, *lang, *prompt, *response;
1409 u_int num_prompts, i;
1410 int echo = 0;
1411
1412 debug2("input_userauth_info_req");
1413
1414 if (authctxt == NULL)
1415 fatal("input_userauth_info_req: no authentication context");
1416
1417 authctxt->info_req_seen = 1;
1418
1419 name = packet_get_string(NULL);
1420 inst = packet_get_string(NULL);
1421 lang = packet_get_string(NULL);
1422 if (strlen(name) > 0)
1423 logit("%s", name);
1424 if (strlen(inst) > 0)
1425 logit("%s", inst);
Adam Langleyd0592972015-03-30 14:49:51 -07001426 free(name);
1427 free(inst);
1428 free(lang);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001429
1430 num_prompts = packet_get_int();
1431 /*
1432 * Begin to build info response packet based on prompts requested.
1433 * We commit to providing the correct number of responses, so if
1434 * further on we run into a problem that prevents this, we have to
1435 * be sure and clean this up and send a correct error response.
1436 */
1437 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1438 packet_put_int(num_prompts);
1439
1440 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1441 for (i = 0; i < num_prompts; i++) {
1442 prompt = packet_get_string(NULL);
1443 echo = packet_get_char();
1444
1445 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1446
1447 packet_put_cstring(response);
Adam Langleyd0592972015-03-30 14:49:51 -07001448 explicit_bzero(response, strlen(response));
1449 free(response);
1450 free(prompt);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001451 }
1452 packet_check_eom(); /* done with parsing incoming message. */
1453
1454 packet_add_padding(64);
1455 packet_send();
Adam Langleyd0592972015-03-30 14:49:51 -07001456 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001457}
1458
1459static int
Adam Langleyd0592972015-03-30 14:49:51 -07001460ssh_keysign(struct sshkey *key, u_char **sigp, size_t *lenp,
1461 const u_char *data, size_t datalen)
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001462{
Adam Langleyd0592972015-03-30 14:49:51 -07001463 struct sshbuf *b;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001464 struct stat st;
1465 pid_t pid;
Adam Langleyd0592972015-03-30 14:49:51 -07001466 int i, r, to[2], from[2], status, sock = packet_get_connection_in();
1467 u_char rversion = 0, version = 2;
1468 void (*osigchld)(int);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001469
Adam Langleyd0592972015-03-30 14:49:51 -07001470 *sigp = NULL;
1471 *lenp = 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001472
1473 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
Adam Langleyd0592972015-03-30 14:49:51 -07001474 error("%s: not installed: %s", __func__, strerror(errno));
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001475 return -1;
1476 }
Adam Langleyd0592972015-03-30 14:49:51 -07001477 if (fflush(stdout) != 0) {
1478 error("%s: fflush: %s", __func__, strerror(errno));
1479 return -1;
1480 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001481 if (pipe(to) < 0) {
Adam Langleyd0592972015-03-30 14:49:51 -07001482 error("%s: pipe: %s", __func__, strerror(errno));
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001483 return -1;
1484 }
1485 if (pipe(from) < 0) {
Adam Langleyd0592972015-03-30 14:49:51 -07001486 error("%s: pipe: %s", __func__, strerror(errno));
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001487 return -1;
1488 }
1489 if ((pid = fork()) < 0) {
Adam Langleyd0592972015-03-30 14:49:51 -07001490 error("%s: fork: %s", __func__, strerror(errno));
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001491 return -1;
1492 }
Adam Langleyd0592972015-03-30 14:49:51 -07001493 osigchld = signal(SIGCHLD, SIG_DFL);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001494 if (pid == 0) {
1495 /* keep the socket on exec */
Adam Langleyd0592972015-03-30 14:49:51 -07001496 fcntl(sock, F_SETFD, 0);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001497 permanently_drop_suid(getuid());
1498 close(from[0]);
1499 if (dup2(from[1], STDOUT_FILENO) < 0)
Adam Langleyd0592972015-03-30 14:49:51 -07001500 fatal("%s: dup2: %s", __func__, strerror(errno));
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001501 close(to[1]);
1502 if (dup2(to[0], STDIN_FILENO) < 0)
Adam Langleyd0592972015-03-30 14:49:51 -07001503 fatal("%s: dup2: %s", __func__, strerror(errno));
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001504 close(from[1]);
1505 close(to[0]);
Adam Langleyd0592972015-03-30 14:49:51 -07001506 /* Close everything but stdio and the socket */
1507 for (i = STDERR_FILENO + 1; i < sock; i++)
1508 close(i);
1509 closefrom(sock + 1);
1510 debug3("%s: [child] pid=%ld, exec %s",
1511 __func__, (long)getpid(), _PATH_SSH_KEY_SIGN);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001512 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Adam Langleyd0592972015-03-30 14:49:51 -07001513 fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN,
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001514 strerror(errno));
1515 }
1516 close(from[1]);
1517 close(to[0]);
1518
Adam Langleyd0592972015-03-30 14:49:51 -07001519 if ((b = sshbuf_new()) == NULL)
1520 fatal("%s: sshbuf_new failed", __func__);
1521 /* send # of sock, data to be signed */
1522 if ((r = sshbuf_put_u32(b, sock) != 0) ||
1523 (r = sshbuf_put_string(b, data, datalen)) != 0)
1524 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1525 if (ssh_msg_send(to[1], version, b) == -1)
1526 fatal("%s: couldn't send request", __func__);
1527 sshbuf_reset(b);
1528 r = ssh_msg_recv(from[0], b);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001529 close(from[0]);
1530 close(to[1]);
Adam Langleyd0592972015-03-30 14:49:51 -07001531 if (r < 0) {
1532 error("%s: no reply", __func__);
1533 goto fail;
1534 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001535
Adam Langleyd0592972015-03-30 14:49:51 -07001536 errno = 0;
1537 while (waitpid(pid, &status, 0) < 0) {
1538 if (errno != EINTR) {
1539 error("%s: waitpid %ld: %s",
1540 __func__, (long)pid, strerror(errno));
1541 goto fail;
1542 }
1543 }
1544 if (!WIFEXITED(status)) {
1545 error("%s: exited abnormally", __func__);
1546 goto fail;
1547 }
1548 if (WEXITSTATUS(status) != 0) {
1549 error("%s: exited with status %d",
1550 __func__, WEXITSTATUS(status));
1551 goto fail;
1552 }
1553 if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
1554 error("%s: buffer error: %s", __func__, ssh_err(r));
1555 goto fail;
1556 }
1557 if (rversion != version) {
1558 error("%s: bad version", __func__);
1559 goto fail;
1560 }
1561 if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
1562 error("%s: buffer error: %s", __func__, ssh_err(r));
1563 fail:
1564 signal(SIGCHLD, osigchld);
1565 sshbuf_free(b);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001566 return -1;
1567 }
Adam Langleyd0592972015-03-30 14:49:51 -07001568 signal(SIGCHLD, osigchld);
1569 sshbuf_free(b);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001570
1571 return 0;
1572}
1573
1574int
1575userauth_hostbased(Authctxt *authctxt)
1576{
Adam Langleyd0592972015-03-30 14:49:51 -07001577 struct ssh *ssh = active_state;
1578 struct sshkey *private = NULL;
1579 struct sshbuf *b = NULL;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001580 const char *service;
Adam Langleyd0592972015-03-30 14:49:51 -07001581 u_char *sig = NULL, *keyblob = NULL;
1582 char *fp = NULL, *chost = NULL, *lname = NULL;
1583 size_t siglen = 0, keylen = 0;
1584 int i, r, success = 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001585
Adam Langleyd0592972015-03-30 14:49:51 -07001586 if (authctxt->ktypes == NULL) {
1587 authctxt->oktypes = xstrdup(options.hostbased_key_types);
1588 authctxt->ktypes = authctxt->oktypes;
1589 }
1590
1591 /*
1592 * Work through each listed type pattern in HostbasedKeyTypes,
1593 * trying each hostkey that matches the type in turn.
1594 */
1595 for (;;) {
1596 if (authctxt->active_ktype == NULL)
1597 authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
1598 if (authctxt->active_ktype == NULL ||
1599 *authctxt->active_ktype == '\0')
1600 break;
1601 debug3("%s: trying key type %s", __func__,
1602 authctxt->active_ktype);
1603
1604 /* check for a useful key */
1605 private = NULL;
1606 for (i = 0; i < authctxt->sensitive->nkeys; i++) {
1607 if (authctxt->sensitive->keys[i] == NULL ||
1608 authctxt->sensitive->keys[i]->type == KEY_RSA1 ||
1609 authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
1610 continue;
1611 if (match_pattern_list(
1612 sshkey_ssh_name(authctxt->sensitive->keys[i]),
1613 authctxt->active_ktype,
1614 strlen(authctxt->active_ktype), 0) != 1)
1615 continue;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001616 /* we take and free the key */
Adam Langleyd0592972015-03-30 14:49:51 -07001617 private = authctxt->sensitive->keys[i];
1618 authctxt->sensitive->keys[i] = NULL;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001619 break;
1620 }
Adam Langleyd0592972015-03-30 14:49:51 -07001621 /* Found one */
1622 if (private != NULL)
1623 break;
1624 /* No more keys of this type; advance */
1625 authctxt->active_ktype = NULL;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001626 }
Adam Langleyd0592972015-03-30 14:49:51 -07001627 if (private == NULL) {
1628 free(authctxt->oktypes);
1629 authctxt->oktypes = authctxt->ktypes = NULL;
1630 authctxt->active_ktype = NULL;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001631 debug("No more client hostkeys for hostbased authentication.");
Adam Langleyd0592972015-03-30 14:49:51 -07001632 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001633 }
Adam Langleyd0592972015-03-30 14:49:51 -07001634
1635 if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
1636 SSH_FP_DEFAULT)) == NULL) {
1637 error("%s: sshkey_fingerprint failed", __func__);
1638 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001639 }
Adam Langleyd0592972015-03-30 14:49:51 -07001640 debug("%s: trying hostkey %s %s",
1641 __func__, sshkey_ssh_name(private), fp);
1642
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001643 /* figure out a name for the client host */
Adam Langleyd0592972015-03-30 14:49:51 -07001644 if ((lname = get_local_name(packet_get_connection_in())) == NULL) {
1645 error("%s: cannot get local ipaddr/name", __func__);
1646 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001647 }
Adam Langleyd0592972015-03-30 14:49:51 -07001648
1649 /* XXX sshbuf_put_stringf? */
1650 xasprintf(&chost, "%s.", lname);
1651 debug2("%s: chost %s", __func__, chost);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001652
1653 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1654 authctxt->service;
Adam Langleyd0592972015-03-30 14:49:51 -07001655
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001656 /* construct data */
Adam Langleyd0592972015-03-30 14:49:51 -07001657 if ((b = sshbuf_new()) == NULL) {
1658 error("%s: sshbuf_new failed", __func__);
1659 goto out;
1660 }
1661 if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
1662 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
1663 goto out;
1664 }
1665 if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
1666 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1667 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1668 (r = sshbuf_put_cstring(b, service)) != 0 ||
1669 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1670 (r = sshbuf_put_cstring(b, key_ssh_name(private))) != 0 ||
1671 (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
1672 (r = sshbuf_put_cstring(b, chost)) != 0 ||
1673 (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
1674 error("%s: buffer error: %s", __func__, ssh_err(r));
1675 goto out;
1676 }
1677
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001678#ifdef DEBUG_PK
Adam Langleyd0592972015-03-30 14:49:51 -07001679 sshbuf_dump(b, stderr);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001680#endif
Adam Langleyd0592972015-03-30 14:49:51 -07001681 if (authctxt->sensitive->external_keysign)
1682 r = ssh_keysign(private, &sig, &siglen,
1683 sshbuf_ptr(b), sshbuf_len(b));
1684 else if ((r = sshkey_sign(private, &sig, &siglen,
1685 sshbuf_ptr(b), sshbuf_len(b), datafellows)) != 0)
1686 debug("%s: sshkey_sign: %s", __func__, ssh_err(r));
1687 if (r != 0) {
1688 error("sign using hostkey %s %s failed",
1689 sshkey_ssh_name(private), fp);
1690 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001691 }
Adam Langleyd0592972015-03-30 14:49:51 -07001692 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1693 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1694 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1695 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1696 (r = sshpkt_put_cstring(ssh, key_ssh_name(private))) != 0 ||
1697 (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
1698 (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
1699 (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
1700 (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
1701 (r = sshpkt_send(ssh)) != 0) {
1702 error("%s: packet error: %s", __func__, ssh_err(r));
1703 goto out;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001704 }
Adam Langleyd0592972015-03-30 14:49:51 -07001705 success = 1;
1706
1707 out:
1708 if (sig != NULL) {
1709 explicit_bzero(sig, siglen);
1710 free(sig);
1711 }
1712 free(keyblob);
1713 free(lname);
1714 free(fp);
1715 free(chost);
1716 sshkey_free(private);
1717 sshbuf_free(b);
1718
1719 return success;
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001720}
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001721
1722/* find auth method */
1723
1724/*
1725 * given auth method name, if configurable options permit this method fill
1726 * in auth_ident field and return true, otherwise return false.
1727 */
1728static int
1729authmethod_is_enabled(Authmethod *method)
1730{
1731 if (method == NULL)
1732 return 0;
1733 /* return false if options indicate this method is disabled */
1734 if (method->enabled == NULL || *method->enabled == 0)
1735 return 0;
1736 /* return false if batch mode is enabled but method needs interactive mode */
1737 if (method->batch_flag != NULL && *method->batch_flag != 0)
1738 return 0;
1739 return 1;
1740}
1741
1742static Authmethod *
1743authmethod_lookup(const char *name)
1744{
1745 Authmethod *method = NULL;
1746 if (name != NULL)
1747 for (method = authmethods; method->name != NULL; method++)
1748 if (strcmp(name, method->name) == 0)
1749 return method;
1750 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1751 return NULL;
1752}
1753
1754/* XXX internal state */
1755static Authmethod *current = NULL;
1756static char *supported = NULL;
1757static char *preferred = NULL;
1758
1759/*
1760 * Given the authentication method list sent by the server, return the
1761 * next method we should try. If the server initially sends a nil list,
1762 * use a built-in default list.
1763 */
1764static Authmethod *
1765authmethod_get(char *authlist)
1766{
1767 char *name = NULL;
1768 u_int next;
1769
1770 /* Use a suitable default if we're passed a nil list. */
1771 if (authlist == NULL || strlen(authlist) == 0)
1772 authlist = options.preferred_authentications;
1773
1774 if (supported == NULL || strcmp(authlist, supported) != 0) {
1775 debug3("start over, passed a different list %s", authlist);
Adam Langleyd0592972015-03-30 14:49:51 -07001776 free(supported);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001777 supported = xstrdup(authlist);
1778 preferred = options.preferred_authentications;
1779 debug3("preferred %s", preferred);
1780 current = NULL;
1781 } else if (current != NULL && authmethod_is_enabled(current))
1782 return current;
1783
1784 for (;;) {
1785 if ((name = match_list(preferred, supported, &next)) == NULL) {
1786 debug("No more authentication methods to try.");
1787 current = NULL;
1788 return NULL;
1789 }
1790 preferred += next;
1791 debug3("authmethod_lookup %s", name);
1792 debug3("remaining preferred: %s", preferred);
1793 if ((current = authmethod_lookup(name)) != NULL &&
1794 authmethod_is_enabled(current)) {
1795 debug3("authmethod_is_enabled %s", name);
1796 debug("Next authentication method: %s", name);
Adam Langleyd0592972015-03-30 14:49:51 -07001797 free(name);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001798 return current;
1799 }
Adam Langleyd0592972015-03-30 14:49:51 -07001800 free(name);
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001801 }
Greg Hartmanbd77cf72015-02-25 13:21:06 -08001802}
1803
1804static char *
1805authmethods_get(void)
1806{
1807 Authmethod *method = NULL;
1808 Buffer b;
1809 char *list;
1810
1811 buffer_init(&b);
1812 for (method = authmethods; method->name != NULL; method++) {
1813 if (authmethod_is_enabled(method)) {
1814 if (buffer_len(&b) > 0)
1815 buffer_append(&b, ",", 1);
1816 buffer_append(&b, method->name, strlen(method->name));
1817 }
1818 }
1819 buffer_append(&b, "\0", 1);
1820 list = xstrdup(buffer_ptr(&b));
1821 buffer_free(&b);
1822 return list;
1823}
1824