Annotation of src/usr.bin/ssh/sshconnect2.c, Revision 1.357
1.357 ! dtucker 1: /* $OpenBSD: sshconnect2.c,v 1.356 2022/02/01 23:32:51 djm Exp $ */
1.1 markus 2: /*
3: * Copyright (c) 2000 Markus Friedl. All rights reserved.
1.170 djm 4: * Copyright (c) 2008 Damien Miller. All rights reserved.
1.1 markus 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:
1.145 stevesk 27: #include <sys/types.h>
1.160 deraadt 28: #include <sys/socket.h>
1.145 stevesk 29: #include <sys/wait.h>
1.144 stevesk 30: #include <sys/queue.h>
1.146 stevesk 31: #include <sys/stat.h>
1.156 stevesk 32:
33: #include <errno.h>
1.174 dtucker 34: #include <fcntl.h>
1.336 djm 35: #include <limits.h>
1.164 jolan 36: #include <netdb.h>
1.159 stevesk 37: #include <stdio.h>
1.158 stevesk 38: #include <string.h>
1.313 deraadt 39: #include <stdarg.h>
1.160 deraadt 40: #include <signal.h>
41: #include <pwd.h>
1.157 stevesk 42: #include <unistd.h>
1.166 djm 43: #include <vis.h>
1.1 markus 44:
1.160 deraadt 45: #include "xmalloc.h"
1.1 markus 46: #include "ssh.h"
1.37 markus 47: #include "ssh2.h"
1.278 markus 48: #include "sshbuf.h"
1.1 markus 49: #include "packet.h"
50: #include "compat.h"
1.37 markus 51: #include "cipher.h"
1.278 markus 52: #include "sshkey.h"
1.1 markus 53: #include "kex.h"
54: #include "myproposal.h"
55: #include "sshconnect.h"
56: #include "authfile.h"
1.57 provos 57: #include "dh.h"
1.17 markus 58: #include "authfd.h"
1.37 markus 59: #include "log.h"
1.210 millert 60: #include "misc.h"
1.37 markus 61: #include "readconf.h"
1.53 markus 62: #include "match.h"
1.61 markus 63: #include "dispatch.h"
1.68 markus 64: #include "canohost.h"
1.100 markus 65: #include "msg.h"
66: #include "pathnames.h"
1.154 markus 67: #include "uidswap.h"
1.186 djm 68: #include "hostfile.h"
1.214 djm 69: #include "ssherr.h"
1.246 djm 70: #include "utf8.h"
1.309 djm 71: #include "ssh-sk.h"
1.312 djm 72: #include "sk-api.h"
1.22 provos 73:
1.121 markus 74: #ifdef GSSAPI
75: #include "ssh-gss.h"
76: #endif
1.293 djm 77:
1.1 markus 78: /* import */
79: extern char *client_version_string;
80: extern char *server_version_string;
81: extern Options options;
82:
83: /*
84: * SSH2 key exchange
85: */
86:
1.338 djm 87: static char *xxx_host;
88: static struct sockaddr *xxx_hostaddr;
89: static const struct ssh_conn_info *xxx_conn_info;
1.61 markus 90:
1.76 itojun 91: static int
1.259 markus 92: verify_host_key_callback(struct sshkey *hostkey, struct ssh *ssh)
1.61 markus 93: {
1.338 djm 94: if (verify_host_key(xxx_host, xxx_hostaddr, hostkey,
95: xxx_conn_info) == -1)
1.83 markus 96: fatal("Host key verification failed.");
1.61 markus 97: return 0;
98: }
99:
1.326 djm 100: /* Returns the first item from a comma-separated algorithm list */
101: static char *
102: first_alg(const char *algs)
103: {
104: char *ret, *cp;
105:
106: ret = xstrdup(algs);
107: if ((cp = strchr(ret, ',')) != NULL)
108: *cp = '\0';
109: return ret;
110: }
111:
1.357 ! dtucker 112: /* Returns an allocated string that the caller must free. */
1.186 djm 113: static char *
1.338 djm 114: order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port,
115: const struct ssh_conn_info *cinfo)
1.186 djm 116: {
1.326 djm 117: char *oavail = NULL, *avail = NULL, *first = NULL, *last = NULL;
118: char *alg = NULL, *hostname = NULL, *ret = NULL, *best = NULL;
1.186 djm 119: size_t maxlen;
1.326 djm 120: struct hostkeys *hostkeys = NULL;
1.186 djm 121: int ktype;
1.188 djm 122: u_int i;
1.186 djm 123:
124: /* Find all hostkeys for this hostname */
125: get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
126: hostkeys = init_hostkeys();
1.188 djm 127: for (i = 0; i < options.num_user_hostfiles; i++)
1.337 djm 128: load_hostkeys(hostkeys, hostname, options.user_hostfiles[i], 0);
129: for (i = 0; i < options.num_system_hostfiles; i++) {
130: load_hostkeys(hostkeys, hostname,
131: options.system_hostfiles[i], 0);
132: }
1.339 djm 133: if (options.known_hosts_command != NULL) {
134: load_hostkeys_command(hostkeys, options.known_hosts_command,
135: "ORDER", cinfo, NULL, host);
136: }
1.326 djm 137: /*
138: * If a plain public key exists that matches the type of the best
139: * preference HostkeyAlgorithms, then use the whole list as is.
140: * Note that we ignore whether the best preference algorithm is a
141: * certificate type, as sshconnect.c will downgrade certs to
142: * plain keys if necessary.
143: */
144: best = first_alg(options.hostkeyalgorithms);
145: if (lookup_key_in_hostkeys_by_type(hostkeys,
1.328 djm 146: sshkey_type_plain(sshkey_type_from_name(best)),
147: sshkey_ecdsa_nid_from_name(best), NULL)) {
1.331 djm 148: debug3_f("have matching best-preference key type %s, "
149: "using HostkeyAlgorithms verbatim", best);
1.326 djm 150: ret = xstrdup(options.hostkeyalgorithms);
151: goto out;
152: }
153:
154: /*
155: * Otherwise, prefer the host key algorithms that match known keys
156: * while keeping the ordering of HostkeyAlgorithms as much as possible.
157: */
1.320 djm 158: oavail = avail = xstrdup(options.hostkeyalgorithms);
1.186 djm 159: maxlen = strlen(avail) + 1;
160: first = xmalloc(maxlen);
161: last = xmalloc(maxlen);
162: *first = *last = '\0';
163:
164: #define ALG_APPEND(to, from) \
165: do { \
166: if (*to != '\0') \
167: strlcat(to, ",", maxlen); \
168: strlcat(to, from, maxlen); \
169: } while (0)
170:
171: while ((alg = strsep(&avail, ",")) && *alg != '\0') {
1.214 djm 172: if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
1.331 djm 173: fatal_f("unknown alg %s", alg);
1.322 djm 174: /*
175: * If we have a @cert-authority marker in known_hosts then
176: * prefer all certificate algorithms.
177: */
178: if (sshkey_type_is_cert(ktype) &&
179: lookup_marker_in_hostkeys(hostkeys, MRK_CA)) {
180: ALG_APPEND(first, alg);
181: continue;
182: }
183: /* If the key appears in known_hosts then prefer it */
1.186 djm 184: if (lookup_key_in_hostkeys_by_type(hostkeys,
1.328 djm 185: sshkey_type_plain(ktype),
186: sshkey_ecdsa_nid_from_name(alg), NULL)) {
1.186 djm 187: ALG_APPEND(first, alg);
1.322 djm 188: continue;
189: }
190: /* Otherwise, put it last */
191: ALG_APPEND(last, alg);
1.186 djm 192: }
193: #undef ALG_APPEND
1.216 djm 194: xasprintf(&ret, "%s%s%s", first,
195: (*first == '\0' || *last == '\0') ? "" : ",", last);
1.186 djm 196: if (*first != '\0')
1.331 djm 197: debug3_f("prefer hostkeyalgs: %s", first);
1.339 djm 198: else
199: debug3_f("no algorithms matched; accept original");
1.326 djm 200: out:
201: free(best);
1.197 djm 202: free(first);
203: free(last);
204: free(hostname);
205: free(oavail);
1.186 djm 206: free_hostkeys(hostkeys);
207:
208: return ret;
209: }
210:
1.1 markus 211: void
1.338 djm 212: ssh_kex2(struct ssh *ssh, char *host, struct sockaddr *hostaddr, u_short port,
213: const struct ssh_conn_info *cinfo)
1.22 provos 214: {
1.205 markus 215: char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
1.357 ! dtucker 216: char *s, *all_key, *hostkeyalgs = NULL;
1.320 djm 217: int r, use_known_hosts_order = 0;
1.61 markus 218:
219: xxx_host = host;
220: xxx_hostaddr = hostaddr;
1.338 djm 221: xxx_conn_info = cinfo;
1.22 provos 222:
1.320 djm 223: /*
224: * If the user has not specified HostkeyAlgorithms, or has only
225: * appended or removed algorithms from that list then prefer algorithms
226: * that are in the list that are supported by known_hosts keys.
227: */
228: if (options.hostkeyalgorithms == NULL ||
229: options.hostkeyalgorithms[0] == '-' ||
230: options.hostkeyalgorithms[0] == '+')
231: use_known_hosts_order = 1;
232:
233: /* Expand or fill in HostkeyAlgorithms */
234: all_key = sshkey_alg_list(0, 0, 1, ',');
1.333 djm 235: if ((r = kex_assemble_names(&options.hostkeyalgorithms,
236: kex_default_pk_alg(), all_key)) != 0)
237: fatal_fr(r, "kex_assemble_namelist");
1.320 djm 238: free(all_key);
239:
1.231 markus 240: if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
1.331 djm 241: fatal_f("kex_names_cat");
1.345 djm 242: myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(ssh, s);
1.60 stevesk 243: myproposal[PROPOSAL_ENC_ALGS_CTOS] =
1.345 djm 244: compat_cipher_proposal(ssh, options.ciphers);
1.60 stevesk 245: myproposal[PROPOSAL_ENC_ALGS_STOC] =
1.345 djm 246: compat_cipher_proposal(ssh, options.ciphers);
1.245 dtucker 247: myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1.318 dtucker 248: myproposal[PROPOSAL_COMP_ALGS_STOC] =
249: (char *)compression_alg_list(options.compression);
1.226 djm 250: myproposal[PROPOSAL_MAC_ALGS_CTOS] =
251: myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
1.320 djm 252: if (use_known_hosts_order) {
253: /* Query known_hosts and prefer algorithms that appear there */
1.357 ! dtucker 254: hostkeyalgs = order_hostkeyalgs(host, hostaddr, port, cinfo);
1.88 deraadt 255: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
1.357 ! dtucker 256: compat_pkalg_proposal(ssh, hostkeyalgs);
! 257: free(hostkeyalgs);
1.226 djm 258: } else {
1.320 djm 259: /* Use specified HostkeyAlgorithms exactly */
1.186 djm 260: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
1.345 djm 261: compat_pkalg_proposal(ssh, options.hostkeyalgorithms);
1.186 djm 262: }
1.115 markus 263:
1.196 dtucker 264: if (options.rekey_limit || options.rekey_interval)
1.294 djm 265: ssh_packet_set_rekey_limits(ssh, options.rekey_limit,
1.254 dtucker 266: options.rekey_interval);
1.22 provos 267:
1.65 markus 268: /* start key exchange */
1.291 djm 269: if ((r = kex_setup(ssh, myproposal)) != 0)
1.331 djm 270: fatal_r(r, "kex_setup");
1.207 markus 271: #ifdef WITH_OPENSSL
1.301 djm 272: ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client;
273: ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client;
274: ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client;
275: ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client;
276: ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client;
1.291 djm 277: ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
278: ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
1.301 djm 279: ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client;
1.207 markus 280: #endif
1.301 djm 281: ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client;
1.340 djm 282: ssh->kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_client;
1.291 djm 283: ssh->kex->verify_host_key=&verify_host_key_callback;
1.22 provos 284:
1.291 djm 285: ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &ssh->kex->done);
1.173 andreas 286:
1.231 markus 287: /* remove ext-info from the KEX proposals for rekeying */
288: myproposal[PROPOSAL_KEX_ALGS] =
1.345 djm 289: compat_kex_proposal(ssh, options.kex_algorithms);
1.291 djm 290: if ((r = kex_prop2buf(ssh->kex->my, myproposal)) != 0)
1.331 djm 291: fatal_r(r, "kex_prop2buf");
1.62 markus 292:
1.22 provos 293: #ifdef DEBUG_KEXDH
294: /* send 1st encrypted/maced/compressed message */
1.278 markus 295: if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 ||
296: (r = sshpkt_put_cstring(ssh, "markus")) != 0 ||
1.280 markus 297: (r = sshpkt_send(ssh)) != 0 ||
298: (r = ssh_packet_write_wait(ssh)) != 0)
1.331 djm 299: fatal_fr(r, "send packet");
1.22 provos 300: #endif
1.1 markus 301: }
1.11 markus 302:
1.1 markus 303: /*
304: * Authenticate user
305: */
1.20 markus 306:
1.214 djm 307: typedef struct cauthctxt Authctxt;
308: typedef struct cauthmethod Authmethod;
1.117 markus 309: typedef struct identity Identity;
310: typedef struct idlist Idlist;
1.20 markus 311:
1.117 markus 312: struct identity {
313: TAILQ_ENTRY(identity) next;
1.214 djm 314: int agent_fd; /* >=0 if agent supports key */
315: struct sshkey *key; /* public/private key */
1.117 markus 316: char *filename; /* comment for agent-only keys */
317: int tried;
318: int isprivate; /* key points to the private key */
1.191 dtucker 319: int userprovided;
1.117 markus 320: };
321: TAILQ_HEAD(idlist, identity);
1.20 markus 322:
1.214 djm 323: struct cauthctxt {
1.20 markus 324: const char *server_user;
1.68 markus 325: const char *local_user;
1.20 markus 326: const char *host;
327: const char *service;
1.214 djm 328: struct cauthmethod *method;
1.183 djm 329: sig_atomic_t success;
1.51 markus 330: char *authlist;
1.302 djm 331: #ifdef GSSAPI
332: /* gssapi */
333: gss_OID_set gss_supported_mechs;
334: u_int mech_tried;
335: #endif
1.68 markus 336: /* pubkey */
1.214 djm 337: struct idlist keys;
338: int agent_fd;
1.68 markus 339: /* hostbased */
1.100 markus 340: Sensitive *sensitive;
1.223 djm 341: char *oktypes, *ktypes;
342: const char *active_ktype;
1.82 markus 343: /* kbd-interactive */
344: int info_req_seen;
1.292 djm 345: int attempt_kbdint;
346: /* password */
347: int attempt_passwd;
1.121 markus 348: /* generic */
349: void *methoddata;
1.20 markus 350: };
1.214 djm 351:
352: struct cauthmethod {
1.20 markus 353: char *name; /* string to compare against server's list */
1.295 djm 354: int (*userauth)(struct ssh *ssh);
355: void (*cleanup)(struct ssh *ssh);
1.20 markus 356: int *enabled; /* flag in option struct that enables method */
357: int *batch_flag; /* flag in option struct that disables method */
358: };
359:
1.302 djm 360: static int input_userauth_service_accept(int, u_int32_t, struct ssh *);
361: static int input_userauth_ext_info(int, u_int32_t, struct ssh *);
362: static int input_userauth_success(int, u_int32_t, struct ssh *);
363: static int input_userauth_failure(int, u_int32_t, struct ssh *);
364: static int input_userauth_banner(int, u_int32_t, struct ssh *);
365: static int input_userauth_error(int, u_int32_t, struct ssh *);
366: static int input_userauth_info_req(int, u_int32_t, struct ssh *);
367: static int input_userauth_pk_ok(int, u_int32_t, struct ssh *);
368: static int input_userauth_passwd_changereq(int, u_int32_t, struct ssh *);
369:
370: static int userauth_none(struct ssh *);
371: static int userauth_pubkey(struct ssh *);
372: static int userauth_passwd(struct ssh *);
373: static int userauth_kbdint(struct ssh *);
374: static int userauth_hostbased(struct ssh *);
1.20 markus 375:
1.121 markus 376: #ifdef GSSAPI
1.302 djm 377: static int userauth_gssapi(struct ssh *);
378: static void userauth_gssapi_cleanup(struct ssh *);
379: static int input_gssapi_response(int type, u_int32_t, struct ssh *);
380: static int input_gssapi_token(int type, u_int32_t, struct ssh *);
381: static int input_gssapi_error(int, u_int32_t, struct ssh *);
382: static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
1.121 markus 383: #endif
384:
1.295 djm 385: void userauth(struct ssh *, char *);
1.51 markus 386:
1.303 djm 387: static void pubkey_cleanup(struct ssh *);
1.295 djm 388: static int sign_and_send_pubkey(struct ssh *ssh, Identity *);
1.352 djm 389: static void pubkey_prepare(struct ssh *, Authctxt *);
1.251 djm 390: static void pubkey_reset(Authctxt *);
1.259 markus 391: static struct sshkey *load_identity_file(Identity *);
1.76 itojun 392:
393: static Authmethod *authmethod_get(char *authlist);
394: static Authmethod *authmethod_lookup(const char *name);
395: static char *authmethods_get(void);
1.20 markus 396:
397: Authmethod authmethods[] = {
1.121 markus 398: #ifdef GSSAPI
1.132 markus 399: {"gssapi-with-mic",
1.121 markus 400: userauth_gssapi,
1.302 djm 401: userauth_gssapi_cleanup,
1.121 markus 402: &options.gss_authentication,
403: NULL},
404: #endif
1.81 markus 405: {"hostbased",
406: userauth_hostbased,
1.170 djm 407: NULL,
1.81 markus 408: &options.hostbased_authentication,
409: NULL},
1.20 markus 410: {"publickey",
411: userauth_pubkey,
1.303 djm 412: NULL,
1.28 markus 413: &options.pubkey_authentication,
1.20 markus 414: NULL},
1.81 markus 415: {"keyboard-interactive",
416: userauth_kbdint,
1.170 djm 417: NULL,
1.81 markus 418: &options.kbd_interactive_authentication,
419: &options.batch_mode},
1.20 markus 420: {"password",
421: userauth_passwd,
1.170 djm 422: NULL,
1.20 markus 423: &options.password_authentication,
1.23 markus 424: &options.batch_mode},
425: {"none",
426: userauth_none,
427: NULL,
1.170 djm 428: NULL,
1.23 markus 429: NULL},
1.170 djm 430: {NULL, NULL, NULL, NULL, NULL}
1.20 markus 431: };
432:
433: void
1.291 djm 434: ssh_userauth2(struct ssh *ssh, const char *local_user,
435: const char *server_user, char *host, Sensitive *sensitive)
1.20 markus 436: {
437: Authctxt authctxt;
1.231 markus 438: int r;
1.39 markus 439:
1.53 markus 440: if (options.preferred_authentications == NULL)
441: options.preferred_authentications = authmethods_get();
442:
1.20 markus 443: /* setup authentication context */
1.82 markus 444: memset(&authctxt, 0, sizeof(authctxt));
1.20 markus 445: authctxt.server_user = server_user;
1.68 markus 446: authctxt.local_user = local_user;
1.20 markus 447: authctxt.host = host;
448: authctxt.service = "ssh-connection"; /* service name */
449: authctxt.success = 0;
1.23 markus 450: authctxt.method = authmethod_lookup("none");
1.51 markus 451: authctxt.authlist = NULL;
1.121 markus 452: authctxt.methoddata = NULL;
1.100 markus 453: authctxt.sensitive = sensitive;
1.223 djm 454: authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
1.82 markus 455: authctxt.info_req_seen = 0;
1.292 djm 456: authctxt.attempt_kbdint = 0;
457: authctxt.attempt_passwd = 0;
1.302 djm 458: #if GSSAPI
459: authctxt.gss_supported_mechs = NULL;
460: authctxt.mech_tried = 0;
461: #endif
1.215 djm 462: authctxt.agent_fd = -1;
1.352 djm 463: pubkey_prepare(ssh, &authctxt);
1.291 djm 464: if (authctxt.method == NULL) {
1.331 djm 465: fatal_f("internal error: cannot send userauth none request");
1.291 djm 466: }
1.20 markus 467:
1.231 markus 468: if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
469: (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
470: (r = sshpkt_send(ssh)) != 0)
1.331 djm 471: fatal_fr(r, "send packet");
1.231 markus 472:
1.260 markus 473: ssh->authctxt = &authctxt;
1.231 markus 474: ssh_dispatch_init(ssh, &input_userauth_error);
475: ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
476: ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
1.263 markus 477: ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt.success); /* loop until success */
1.303 djm 478: pubkey_cleanup(ssh);
1.260 markus 479: ssh->authctxt = NULL;
1.20 markus 480:
1.231 markus 481: ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
1.119 markus 482:
1.248 dtucker 483: if (!authctxt.success)
484: fatal("Authentication failed.");
1.351 djm 485: if (ssh_packet_connection_is_on_socket(ssh)) {
486: verbose("Authenticated to %s ([%s]:%d) using \"%s\".", host,
487: ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
488: authctxt.method->name);
489: } else {
490: verbose("Authenticated to %s (via proxy) using \"%s\".", host,
491: authctxt.method->name);
492: }
1.20 markus 493: }
1.119 markus 494:
1.231 markus 495: /* ARGSUSED */
1.302 djm 496: static int
1.261 markus 497: input_userauth_service_accept(int type, u_int32_t seq, struct ssh *ssh)
1.231 markus 498: {
499: int r;
500:
501: if (ssh_packet_remaining(ssh) > 0) {
502: char *reply;
503:
504: if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
505: goto out;
506: debug2("service_accept: %s", reply);
507: free(reply);
508: } else {
509: debug2("buggy server: service_accept w/o service");
510: }
511: if ((r = sshpkt_get_end(ssh)) != 0)
512: goto out;
513: debug("SSH2_MSG_SERVICE_ACCEPT received");
514:
515: /* initial userauth request */
1.295 djm 516: userauth_none(ssh);
1.231 markus 517:
518: ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error);
519: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
520: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
521: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
522: r = 0;
523: out:
524: return r;
525: }
526:
527: /* ARGSUSED */
1.302 djm 528: static int
1.261 markus 529: input_userauth_ext_info(int type, u_int32_t seqnr, struct ssh *ssh)
1.231 markus 530: {
1.261 markus 531: return kex_input_ext_info(type, seqnr, ssh);
1.231 markus 532: }
533:
1.20 markus 534: void
1.295 djm 535: userauth(struct ssh *ssh, char *authlist)
1.51 markus 536: {
1.295 djm 537: Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.278 markus 538:
1.170 djm 539: if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
1.295 djm 540: authctxt->method->cleanup(ssh);
1.170 djm 541:
1.197 djm 542: free(authctxt->methoddata);
543: authctxt->methoddata = NULL;
1.51 markus 544: if (authlist == NULL) {
545: authlist = authctxt->authlist;
546: } else {
1.197 djm 547: free(authctxt->authlist);
1.51 markus 548: authctxt->authlist = authlist;
549: }
550: for (;;) {
551: Authmethod *method = authmethod_get(authlist);
552: if (method == NULL)
1.264 dtucker 553: fatal("%s@%s: Permission denied (%s).",
554: authctxt->server_user, authctxt->host, authlist);
1.51 markus 555: authctxt->method = method;
1.119 markus 556:
557: /* reset the per method handler */
1.278 markus 558: ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_PER_METHOD_MIN,
1.119 markus 559: SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
560:
561: /* and try new method */
1.295 djm 562: if (method->userauth(ssh) != 0) {
1.51 markus 563: debug2("we sent a %s packet, wait for reply", method->name);
564: break;
565: } else {
566: debug2("we did not send a packet, disable method");
567: method->enabled = NULL;
568: }
569: }
570: }
1.105 deraadt 571:
1.169 djm 572: /* ARGSUSED */
1.302 djm 573: static int
1.261 markus 574: input_userauth_error(int type, u_int32_t seq, struct ssh *ssh)
1.20 markus 575: {
1.331 djm 576: fatal_f("bad message during authentication: type %d", type);
1.218 markus 577: return 0;
1.35 markus 578: }
1.105 deraadt 579:
1.169 djm 580: /* ARGSUSED */
1.302 djm 581: static int
1.261 markus 582: input_userauth_banner(int type, u_int32_t seq, struct ssh *ssh)
1.35 markus 583: {
1.295 djm 584: char *msg = NULL;
1.294 djm 585: size_t len;
586: int r;
1.126 deraadt 587:
1.331 djm 588: debug3_f("entering");
1.294 djm 589: if ((r = sshpkt_get_cstring(ssh, &msg, &len)) != 0 ||
1.295 djm 590: (r = sshpkt_get_cstring(ssh, NULL, NULL)) != 0)
1.294 djm 591: goto out;
1.246 djm 592: if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO)
593: fmprintf(stderr, "%s", msg);
1.294 djm 594: r = 0;
595: out:
1.246 djm 596: free(msg);
1.294 djm 597: return r;
1.20 markus 598: }
1.105 deraadt 599:
1.169 djm 600: /* ARGSUSED */
1.302 djm 601: static int
1.261 markus 602: input_userauth_success(int type, u_int32_t seq, struct ssh *ssh)
1.20 markus 603: {
1.260 markus 604: Authctxt *authctxt = ssh->authctxt;
1.172 djm 605:
1.20 markus 606: if (authctxt == NULL)
1.331 djm 607: fatal_f("no authentication context");
1.197 djm 608: free(authctxt->authlist);
609: authctxt->authlist = NULL;
1.172 djm 610: if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
1.295 djm 611: authctxt->method->cleanup(ssh);
1.197 djm 612: free(authctxt->methoddata);
613: authctxt->methoddata = NULL;
1.20 markus 614: authctxt->success = 1; /* break out */
1.218 markus 615: return 0;
1.20 markus 616: }
1.105 deraadt 617:
1.302 djm 618: #if 0
619: static int
1.261 markus 620: input_userauth_success_unexpected(int type, u_int32_t seq, struct ssh *ssh)
1.172 djm 621: {
1.260 markus 622: Authctxt *authctxt = ssh->authctxt;
1.172 djm 623:
624: if (authctxt == NULL)
1.331 djm 625: fatal_f("no authentication context");
1.172 djm 626:
627: fatal("Unexpected authentication success during %s.",
628: authctxt->method->name);
1.218 markus 629: return 0;
1.172 djm 630: }
1.302 djm 631: #endif
1.172 djm 632:
1.169 djm 633: /* ARGSUSED */
1.302 djm 634: static int
1.261 markus 635: input_userauth_failure(int type, u_int32_t seq, struct ssh *ssh)
1.20 markus 636: {
1.260 markus 637: Authctxt *authctxt = ssh->authctxt;
1.20 markus 638: char *authlist = NULL;
1.278 markus 639: u_char partial;
1.20 markus 640:
641: if (authctxt == NULL)
642: fatal("input_userauth_failure: no authentication context");
643:
1.307 dtucker 644: if (sshpkt_get_cstring(ssh, &authlist, NULL) != 0 ||
645: sshpkt_get_u8(ssh, &partial) != 0 ||
646: sshpkt_get_end(ssh) != 0)
1.278 markus 647: goto out;
1.20 markus 648:
1.193 markus 649: if (partial != 0) {
1.351 djm 650: verbose("Authenticated using \"%s\" with partial success.",
651: authctxt->method->name);
1.193 markus 652: /* reset state */
1.251 djm 653: pubkey_reset(authctxt);
1.193 markus 654: }
1.109 markus 655: debug("Authentications that can continue: %s", authlist);
1.20 markus 656:
1.295 djm 657: userauth(ssh, authlist);
1.278 markus 658: authlist = NULL;
659: out:
660: free(authlist);
1.218 markus 661: return 0;
1.51 markus 662: }
1.169 djm 663:
1.287 djm 664: /*
665: * Format an identity for logging including filename, key type, fingerprint
666: * and location (agent, etc.). Caller must free.
667: */
668: static char *
669: format_identity(Identity *id)
670: {
671: char *fp = NULL, *ret = NULL;
1.309 djm 672: const char *note = "";
1.287 djm 673:
674: if (id->key != NULL) {
1.347 djm 675: fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1.287 djm 676: SSH_FP_DEFAULT);
677: }
1.309 djm 678: if (id->key) {
679: if ((id->key->flags & SSHKEY_FLAG_EXT) != 0)
680: note = " token";
1.311 markus 681: else if (sshkey_is_sk(id->key))
1.319 naddy 682: note = " authenticator";
1.309 djm 683: }
1.287 djm 684: xasprintf(&ret, "%s %s%s%s%s%s%s",
685: id->filename,
686: id->key ? sshkey_type(id->key) : "", id->key ? " " : "",
687: fp ? fp : "",
1.309 djm 688: id->userprovided ? " explicit" : "", note,
1.287 djm 689: id->agent_fd != -1 ? " agent" : "");
690: free(fp);
691: return ret;
692: }
693:
1.169 djm 694: /* ARGSUSED */
1.302 djm 695: static int
1.261 markus 696: input_userauth_pk_ok(int type, u_int32_t seq, struct ssh *ssh)
1.51 markus 697: {
1.260 markus 698: Authctxt *authctxt = ssh->authctxt;
1.259 markus 699: struct sshkey *key = NULL;
1.117 markus 700: Identity *id = NULL;
1.287 djm 701: int pktype, found = 0, sent = 0;
1.278 markus 702: size_t blen;
1.287 djm 703: char *pkalg = NULL, *fp = NULL, *ident = NULL;
1.278 markus 704: u_char *pkblob = NULL;
705: int r;
1.51 markus 706:
707: if (authctxt == NULL)
708: fatal("input_userauth_pk_ok: no authentication context");
1.267 djm 709:
1.278 markus 710: if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
711: (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
712: (r = sshpkt_get_end(ssh)) != 0)
713: goto done;
1.51 markus 714:
1.278 markus 715: if ((pktype = sshkey_type_from_name(pkalg)) == KEY_UNSPEC) {
1.331 djm 716: debug_f("server sent unknown pkalg %s", pkalg);
1.117 markus 717: goto done;
718: }
1.278 markus 719: if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
1.331 djm 720: debug_r(r, "no key from blob. pkalg %s", pkalg);
1.117 markus 721: goto done;
722: }
723: if (key->type != pktype) {
724: error("input_userauth_pk_ok: type mismatch "
725: "for decoded key (received %d, expected %d)",
726: key->type, pktype);
727: goto done;
728: }
729:
1.127 markus 730: /*
731: * search keys in the reverse order, because last candidate has been
732: * moved to the end of the queue. this also avoids confusion by
733: * duplicate keys
734: */
1.136 henning 735: TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
1.278 markus 736: if (sshkey_equal(key, id->key)) {
1.287 djm 737: found = 1;
1.51 markus 738: break;
739: }
1.117 markus 740: }
1.287 djm 741: if (!found || id == NULL) {
742: fp = sshkey_fingerprint(key, options.fingerprint_hash,
743: SSH_FP_DEFAULT);
1.331 djm 744: error_f("server replied with unknown key: %s %s",
1.287 djm 745: sshkey_type(key), fp == NULL ? "<ERROR>" : fp);
746: goto done;
747: }
748: ident = format_identity(id);
749: debug("Server accepts key: %s", ident);
1.295 djm 750: sent = sign_and_send_pubkey(ssh, id);
1.278 markus 751: r = 0;
752: done:
753: sshkey_free(key);
1.287 djm 754: free(ident);
755: free(fp);
1.197 djm 756: free(pkalg);
757: free(pkblob);
1.51 markus 758:
1.106 deraadt 759: /* try another method if we did not send a packet */
1.278 markus 760: if (r == 0 && sent == 0)
1.295 djm 761: userauth(ssh, NULL);
1.278 markus 762: return r;
1.20 markus 763: }
1.121 markus 764:
765: #ifdef GSSAPI
1.302 djm 766: static int
1.295 djm 767: userauth_gssapi(struct ssh *ssh)
1.121 markus 768: {
1.295 djm 769: Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.121 markus 770: Gssctxt *gssctxt = NULL;
771: OM_uint32 min;
1.278 markus 772: int r, ok = 0;
1.302 djm 773: gss_OID mech = NULL;
1.121 markus 774:
775: /* Try one GSSAPI method at a time, rather than sending them all at
776: * once. */
777:
1.302 djm 778: if (authctxt->gss_supported_mechs == NULL)
779: gss_indicate_mechs(&min, &authctxt->gss_supported_mechs);
1.121 markus 780:
1.302 djm 781: /* Check to see whether the mechanism is usable before we offer it */
782: while (authctxt->mech_tried < authctxt->gss_supported_mechs->count &&
783: !ok) {
784: mech = &authctxt->gss_supported_mechs->
785: elements[authctxt->mech_tried];
1.121 markus 786: /* My DER encoding requires length<128 */
1.302 djm 787: if (mech->length < 128 && ssh_gssapi_check_mechanism(&gssctxt,
788: mech, authctxt->host)) {
1.121 markus 789: ok = 1; /* Mechanism works */
790: } else {
1.302 djm 791: authctxt->mech_tried++;
1.121 markus 792: }
793: }
794:
1.302 djm 795: if (!ok || mech == NULL)
1.139 djm 796: return 0;
1.121 markus 797:
798: authctxt->methoddata=(void *)gssctxt;
799:
1.278 markus 800: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
801: (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
802: (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
803: (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
804: (r = sshpkt_put_u32(ssh, 1)) != 0 ||
1.302 djm 805: (r = sshpkt_put_u32(ssh, (mech->length) + 2)) != 0 ||
1.278 markus 806: (r = sshpkt_put_u8(ssh, SSH_GSS_OIDTYPE)) != 0 ||
1.302 djm 807: (r = sshpkt_put_u8(ssh, mech->length)) != 0 ||
808: (r = sshpkt_put(ssh, mech->elements, mech->length)) != 0 ||
1.278 markus 809: (r = sshpkt_send(ssh)) != 0)
1.331 djm 810: fatal_fr(r, "send packet");
1.278 markus 811:
812: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
813: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
814: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
815: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
1.121 markus 816:
1.302 djm 817: authctxt->mech_tried++; /* Move along to next candidate */
1.121 markus 818:
819: return 1;
820: }
821:
1.302 djm 822: static void
823: userauth_gssapi_cleanup(struct ssh *ssh)
824: {
825: Authctxt *authctxt = (Authctxt *)ssh->authctxt;
826: Gssctxt *gssctxt = (Gssctxt *)authctxt->methoddata;
827:
828: ssh_gssapi_delete_ctx(&gssctxt);
829: authctxt->methoddata = NULL;
830:
831: free(authctxt->gss_supported_mechs);
832: authctxt->gss_supported_mechs = NULL;
833: }
834:
1.130 markus 835: static OM_uint32
1.262 djm 836: process_gssapi_token(struct ssh *ssh, gss_buffer_t recv_tok)
1.130 markus 837: {
1.260 markus 838: Authctxt *authctxt = ssh->authctxt;
1.130 markus 839: Gssctxt *gssctxt = authctxt->methoddata;
840: gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
1.142 djm 841: gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
842: gss_buffer_desc gssbuf;
1.132 markus 843: OM_uint32 status, ms, flags;
1.278 markus 844: int r;
1.133 djm 845:
1.130 markus 846: status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
1.132 markus 847: recv_tok, &send_tok, &flags);
1.130 markus 848:
849: if (send_tok.length > 0) {
1.278 markus 850: u_char type = GSS_ERROR(status) ?
851: SSH2_MSG_USERAUTH_GSSAPI_ERRTOK :
852: SSH2_MSG_USERAUTH_GSSAPI_TOKEN;
853:
854: if ((r = sshpkt_start(ssh, type)) != 0 ||
855: (r = sshpkt_put_string(ssh, send_tok.value,
856: send_tok.length)) != 0 ||
857: (r = sshpkt_send(ssh)) != 0)
1.331 djm 858: fatal_fr(r, "send %u packet", type);
1.133 djm 859:
1.130 markus 860: gss_release_buffer(&ms, &send_tok);
861: }
1.133 djm 862:
1.130 markus 863: if (status == GSS_S_COMPLETE) {
1.132 markus 864: /* send either complete or MIC, depending on mechanism */
865: if (!(flags & GSS_C_INTEG_FLAG)) {
1.278 markus 866: if ((r = sshpkt_start(ssh,
867: SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE)) != 0 ||
868: (r = sshpkt_send(ssh)) != 0)
1.331 djm 869: fatal_fr(r, "send completion");
1.132 markus 870: } else {
1.278 markus 871: struct sshbuf *b;
872:
873: if ((b = sshbuf_new()) == NULL)
1.331 djm 874: fatal_f("sshbuf_new failed");
1.278 markus 875: ssh_gssapi_buildmic(b, authctxt->server_user,
1.346 djm 876: authctxt->service, "gssapi-with-mic",
877: ssh->kex->session_id);
1.132 markus 878:
1.278 markus 879: if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
1.331 djm 880: fatal_f("sshbuf_mutable_ptr failed");
1.278 markus 881: gssbuf.length = sshbuf_len(b);
1.133 djm 882:
1.132 markus 883: status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
1.133 djm 884:
1.132 markus 885: if (!GSS_ERROR(status)) {
1.278 markus 886: if ((r = sshpkt_start(ssh,
887: SSH2_MSG_USERAUTH_GSSAPI_MIC)) != 0 ||
888: (r = sshpkt_put_string(ssh, mic.value,
889: mic.length)) != 0 ||
890: (r = sshpkt_send(ssh)) != 0)
1.331 djm 891: fatal_fr(r, "send MIC");
1.132 markus 892: }
1.133 djm 893:
1.278 markus 894: sshbuf_free(b);
1.132 markus 895: gss_release_buffer(&ms, &mic);
1.133 djm 896: }
1.130 markus 897: }
1.133 djm 898:
1.130 markus 899: return status;
900: }
901:
1.169 djm 902: /* ARGSUSED */
1.302 djm 903: static int
1.261 markus 904: input_gssapi_response(int type, u_int32_t plen, struct ssh *ssh)
1.121 markus 905: {
1.260 markus 906: Authctxt *authctxt = ssh->authctxt;
1.121 markus 907: Gssctxt *gssctxt;
1.278 markus 908: size_t oidlen;
909: u_char *oidv = NULL;
910: int r;
1.121 markus 911:
912: if (authctxt == NULL)
913: fatal("input_gssapi_response: no authentication context");
914: gssctxt = authctxt->methoddata;
915:
916: /* Setup our OID */
1.278 markus 917: if ((r = sshpkt_get_string(ssh, &oidv, &oidlen)) != 0)
918: goto done;
1.121 markus 919:
1.129 markus 920: if (oidlen <= 2 ||
921: oidv[0] != SSH_GSS_OIDTYPE ||
922: oidv[1] != oidlen - 2) {
923: debug("Badly encoded mechanism OID received");
1.296 djm 924: userauth(ssh, NULL);
1.278 markus 925: goto ok;
1.121 markus 926: }
1.129 markus 927:
928: if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
929: fatal("Server returned different OID than expected");
1.121 markus 930:
1.278 markus 931: if ((r = sshpkt_get_end(ssh)) != 0)
932: goto done;
1.121 markus 933:
1.262 djm 934: if (GSS_ERROR(process_gssapi_token(ssh, GSS_C_NO_BUFFER))) {
1.121 markus 935: /* Start again with next method on list */
936: debug("Trying to start again");
1.296 djm 937: userauth(ssh, NULL);
1.278 markus 938: goto ok;
1.121 markus 939: }
1.278 markus 940: ok:
941: r = 0;
942: done:
943: free(oidv);
944: return r;
1.121 markus 945: }
946:
1.169 djm 947: /* ARGSUSED */
1.302 djm 948: static int
1.261 markus 949: input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
1.121 markus 950: {
1.260 markus 951: Authctxt *authctxt = ssh->authctxt;
1.121 markus 952: gss_buffer_desc recv_tok;
1.278 markus 953: u_char *p = NULL;
954: size_t len;
1.130 markus 955: OM_uint32 status;
1.278 markus 956: int r;
1.121 markus 957:
958: if (authctxt == NULL)
959: fatal("input_gssapi_response: no authentication context");
960:
1.278 markus 961: if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
962: (r = sshpkt_get_end(ssh)) != 0)
963: goto out;
1.121 markus 964:
1.278 markus 965: recv_tok.value = p;
966: recv_tok.length = len;
1.262 djm 967: status = process_gssapi_token(ssh, &recv_tok);
1.121 markus 968:
1.278 markus 969: /* Start again with the next method in the list */
1.121 markus 970: if (GSS_ERROR(status)) {
1.296 djm 971: userauth(ssh, NULL);
1.278 markus 972: /* ok */
1.121 markus 973: }
1.278 markus 974: r = 0;
975: out:
976: free(p);
977: return r;
1.121 markus 978: }
979:
1.169 djm 980: /* ARGSUSED */
1.302 djm 981: static int
1.261 markus 982: input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
1.121 markus 983: {
1.260 markus 984: Authctxt *authctxt = ssh->authctxt;
1.121 markus 985: Gssctxt *gssctxt;
986: gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
987: gss_buffer_desc recv_tok;
1.194 djm 988: OM_uint32 ms;
1.278 markus 989: u_char *p = NULL;
990: size_t len;
991: int r;
1.121 markus 992:
993: if (authctxt == NULL)
994: fatal("input_gssapi_response: no authentication context");
995: gssctxt = authctxt->methoddata;
996:
1.278 markus 997: if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
998: (r = sshpkt_get_end(ssh)) != 0) {
999: free(p);
1000: return r;
1001: }
1.121 markus 1002:
1003: /* Stick it into GSSAPI and see what it says */
1.278 markus 1004: recv_tok.value = p;
1005: recv_tok.length = len;
1.194 djm 1006: (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
1.140 djm 1007: &recv_tok, &send_tok, NULL);
1.278 markus 1008: free(p);
1.121 markus 1009: gss_release_buffer(&ms, &send_tok);
1010:
1011: /* Server will be returning a failed packet after this one */
1.220 djm 1012: return 0;
1.121 markus 1013: }
1014:
1.169 djm 1015: /* ARGSUSED */
1.302 djm 1016: static int
1.261 markus 1017: input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh)
1.121 markus 1018: {
1.278 markus 1019: char *msg = NULL;
1020: char *lang = NULL;
1021: int r;
1.121 markus 1022:
1.278 markus 1023: if ((r = sshpkt_get_u32(ssh, NULL)) != 0 || /* maj */
1024: (r = sshpkt_get_u32(ssh, NULL)) != 0 || /* min */
1025: (r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
1026: (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1027: goto out;
1028: r = sshpkt_get_end(ssh);
1.143 stevesk 1029: debug("Server GSSAPI Error:\n%s", msg);
1.278 markus 1030: out:
1.197 djm 1031: free(msg);
1032: free(lang);
1.278 markus 1033: return r;
1.121 markus 1034: }
1035: #endif /* GSSAPI */
1.20 markus 1036:
1.302 djm 1037: static int
1.295 djm 1038: userauth_none(struct ssh *ssh)
1.23 markus 1039: {
1.295 djm 1040: Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.278 markus 1041: int r;
1042:
1.23 markus 1043: /* initial userauth request */
1.278 markus 1044: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1045: (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1046: (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1047: (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1048: (r = sshpkt_send(ssh)) != 0)
1.331 djm 1049: fatal_fr(r, "send packet");
1.23 markus 1050: return 1;
1051: }
1052:
1.302 djm 1053: static int
1.295 djm 1054: userauth_passwd(struct ssh *ssh)
1.1 markus 1055: {
1.295 djm 1056: Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.290 djm 1057: char *password, *prompt = NULL;
1.175 dtucker 1058: const char *host = options.host_key_alias ? options.host_key_alias :
1059: authctxt->host;
1.278 markus 1060: int r;
1.6 markus 1061:
1.292 djm 1062: if (authctxt->attempt_passwd++ >= options.number_of_password_prompts)
1.6 markus 1063: return 0;
1.13 todd 1064:
1.292 djm 1065: if (authctxt->attempt_passwd != 1)
1.13 todd 1066: error("Permission denied, please try again.");
1.1 markus 1067:
1.290 djm 1068: xasprintf(&prompt, "%s@%s's password: ", authctxt->server_user, host);
1.1 markus 1069: password = read_passphrase(prompt, 0);
1.278 markus 1070: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1071: (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1072: (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1073: (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1074: (r = sshpkt_put_u8(ssh, 0)) != 0 ||
1075: (r = sshpkt_put_cstring(ssh, password)) != 0 ||
1076: (r = sshpkt_add_padding(ssh, 64)) != 0 ||
1077: (r = sshpkt_send(ssh)) != 0)
1.331 djm 1078: fatal_fr(r, "send packet");
1.99 markus 1079:
1.290 djm 1080: free(prompt);
1081: if (password != NULL)
1.278 markus 1082: freezero(password, strlen(password));
1083:
1084: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99 markus 1085: &input_userauth_passwd_changereq);
1086:
1.1 markus 1087: return 1;
1088: }
1.169 djm 1089:
1.99 markus 1090: /*
1091: * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
1092: */
1.169 djm 1093: /* ARGSUSED */
1.302 djm 1094: static int
1.261 markus 1095: input_userauth_passwd_changereq(int type, u_int32_t seqnr, struct ssh *ssh)
1.99 markus 1096: {
1.260 markus 1097: Authctxt *authctxt = ssh->authctxt;
1.278 markus 1098: char *info = NULL, *lang = NULL, *password = NULL, *retype = NULL;
1.266 dtucker 1099: char prompt[256];
1.252 djm 1100: const char *host;
1.278 markus 1101: int r;
1.99 markus 1102:
1103: debug2("input_userauth_passwd_changereq");
1104:
1105: if (authctxt == NULL)
1106: fatal("input_userauth_passwd_changereq: "
1107: "no authentication context");
1.252 djm 1108: host = options.host_key_alias ? options.host_key_alias : authctxt->host;
1.99 markus 1109:
1.278 markus 1110: if ((r = sshpkt_get_cstring(ssh, &info, NULL)) != 0 ||
1111: (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1112: goto out;
1.99 markus 1113: if (strlen(info) > 0)
1.116 itojun 1114: logit("%s", info);
1.278 markus 1115: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1116: (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1117: (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1118: (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1119: (r = sshpkt_put_u8(ssh, 1)) != 0) /* additional info */
1120: goto out;
1121:
1.104 deraadt 1122: snprintf(prompt, sizeof(prompt),
1.99 markus 1123: "Enter %.30s@%.128s's old password: ",
1.175 dtucker 1124: authctxt->server_user, host);
1.99 markus 1125: password = read_passphrase(prompt, 0);
1.278 markus 1126: if ((r = sshpkt_put_cstring(ssh, password)) != 0)
1127: goto out;
1128:
1129: freezero(password, strlen(password));
1.99 markus 1130: password = NULL;
1131: while (password == NULL) {
1.104 deraadt 1132: snprintf(prompt, sizeof(prompt),
1.99 markus 1133: "Enter %.30s@%.128s's new password: ",
1.175 dtucker 1134: authctxt->server_user, host);
1.99 markus 1135: password = read_passphrase(prompt, RP_ALLOW_EOF);
1136: if (password == NULL) {
1137: /* bail out */
1.278 markus 1138: r = 0;
1139: goto out;
1.99 markus 1140: }
1.104 deraadt 1141: snprintf(prompt, sizeof(prompt),
1.99 markus 1142: "Retype %.30s@%.128s's new password: ",
1.175 dtucker 1143: authctxt->server_user, host);
1.99 markus 1144: retype = read_passphrase(prompt, 0);
1145: if (strcmp(password, retype) != 0) {
1.278 markus 1146: freezero(password, strlen(password));
1.116 itojun 1147: logit("Mismatch; try again, EOF to quit.");
1.99 markus 1148: password = NULL;
1149: }
1.278 markus 1150: freezero(retype, strlen(retype));
1.99 markus 1151: }
1.278 markus 1152: if ((r = sshpkt_put_cstring(ssh, password)) != 0 ||
1153: (r = sshpkt_add_padding(ssh, 64)) != 0 ||
1154: (r = sshpkt_send(ssh)) != 0)
1155: goto out;
1.104 deraadt 1156:
1.278 markus 1157: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99 markus 1158: &input_userauth_passwd_changereq);
1.278 markus 1159: r = 0;
1160: out:
1161: if (password)
1162: freezero(password, strlen(password));
1163: free(info);
1164: free(lang);
1165: return r;
1.117 markus 1166: }
1167:
1.272 djm 1168: /*
1169: * Select an algorithm for publickey signatures.
1170: * Returns algorithm (caller must free) or NULL if no mutual algorithm found.
1171: *
1172: * Call with ssh==NULL to ignore server-sig-algs extension list and
1173: * only attempt with the key's base signature type.
1174: */
1175: static char *
1176: key_sig_algorithm(struct ssh *ssh, const struct sshkey *key)
1.231 markus 1177: {
1.274 djm 1178: char *allowed, *oallowed, *cp, *tmp, *alg = NULL;
1.348 djm 1179: const char *server_sig_algs;
1.231 markus 1180:
1.272 djm 1181: /*
1182: * The signature algorithm will only differ from the key algorithm
1183: * for RSA keys/certs and when the server advertises support for
1184: * newer (SHA2) algorithms.
1185: */
1186: if (ssh == NULL || ssh->kex->server_sig_algs == NULL ||
1.288 djm 1187: (key->type != KEY_RSA && key->type != KEY_RSA_CERT) ||
1.345 djm 1188: (key->type == KEY_RSA_CERT && (ssh->compat & SSH_BUG_SIGTYPE))) {
1.272 djm 1189: /* Filter base key signature alg against our configuration */
1.279 markus 1190: return match_list(sshkey_ssh_name(key),
1.342 dtucker 1191: options.pubkey_accepted_algos, NULL);
1.231 markus 1192: }
1193:
1.272 djm 1194: /*
1.348 djm 1195: * Workaround OpenSSH 7.4 bug: this version supports RSA/SHA-2 but
1196: * fails to advertise it via SSH2_MSG_EXT_INFO.
1197: */
1198: server_sig_algs = ssh->kex->server_sig_algs;
1199: if (key->type == KEY_RSA && (ssh->compat & SSH_BUG_SIGTYPE74))
1200: server_sig_algs = "rsa-sha2-256,rsa-sha2-512";
1201:
1202: /*
1.272 djm 1203: * For RSA keys/certs, since these might have a different sig type:
1.342 dtucker 1204: * find the first entry in PubkeyAcceptedAlgorithms of the right type
1.272 djm 1205: * that also appears in the supported signature algorithms list from
1206: * the server.
1207: */
1.342 dtucker 1208: oallowed = allowed = xstrdup(options.pubkey_accepted_algos);
1.272 djm 1209: while ((cp = strsep(&allowed, ",")) != NULL) {
1210: if (sshkey_type_from_name(cp) != key->type)
1211: continue;
1.323 djm 1212: tmp = match_list(sshkey_sigalg_by_name(cp),
1.348 djm 1213: server_sig_algs, NULL);
1.274 djm 1214: if (tmp != NULL)
1215: alg = xstrdup(cp);
1216: free(tmp);
1.272 djm 1217: if (alg != NULL)
1218: break;
1.269 djm 1219: }
1.272 djm 1220: free(oallowed);
1221: return alg;
1.269 djm 1222: }
1223:
1.117 markus 1224: static int
1.214 djm 1225: identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
1.272 djm 1226: const u_char *data, size_t datalen, u_int compat, const char *alg)
1.117 markus 1227: {
1.309 djm 1228: struct sshkey *sign_key = NULL, *prv = NULL;
1.341 djm 1229: int retried = 0, r = SSH_ERR_INTERNAL_ERROR;
1.312 djm 1230: struct notifier_ctx *notifier = NULL;
1.325 djm 1231: char *fp = NULL, *pin = NULL, *prompt = NULL;
1.309 djm 1232:
1233: *sigp = NULL;
1234: *lenp = 0;
1.99 markus 1235:
1.272 djm 1236: /* The agent supports this key. */
1.269 djm 1237: if (id->key != NULL && id->agent_fd != -1) {
1.272 djm 1238: return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
1239: data, datalen, alg, compat);
1.269 djm 1240: }
1.214 djm 1241:
1.117 markus 1242: /*
1.272 djm 1243: * We have already loaded the private key or the private key is
1244: * stored in external hardware.
1.117 markus 1245: */
1.255 djm 1246: if (id->key != NULL &&
1.272 djm 1247: (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))) {
1.309 djm 1248: sign_key = id->key;
1249: } else {
1250: /* Load the private key from the file. */
1251: if ((prv = load_identity_file(id)) == NULL)
1252: return SSH_ERR_KEY_NOT_FOUND;
1253: if (id->key != NULL && !sshkey_equal_public(prv, id->key)) {
1.331 djm 1254: error_f("private key %s contents do not match public",
1.347 djm 1255: id->filename);
1.309 djm 1256: r = SSH_ERR_KEY_NOT_FOUND;
1257: goto out;
1258: }
1259: sign_key = prv;
1.325 djm 1260: if (sshkey_is_sk(sign_key)) {
1261: if ((sign_key->sk_flags &
1262: SSH_SK_USER_VERIFICATION_REQD)) {
1.341 djm 1263: retry_pin:
1.325 djm 1264: xasprintf(&prompt, "Enter PIN for %s key %s: ",
1265: sshkey_type(sign_key), id->filename);
1266: pin = read_passphrase(prompt, 0);
1267: }
1268: if ((sign_key->sk_flags & SSH_SK_USER_PRESENCE_REQD)) {
1269: /* XXX should batch mode just skip these? */
1270: if ((fp = sshkey_fingerprint(sign_key,
1271: options.fingerprint_hash,
1272: SSH_FP_DEFAULT)) == NULL)
1.331 djm 1273: fatal_f("fingerprint failed");
1.325 djm 1274: notifier = notify_start(options.batch_mode,
1275: "Confirm user presence for key %s %s",
1276: sshkey_type(sign_key), fp);
1277: free(fp);
1278: }
1.312 djm 1279: }
1.272 djm 1280: }
1.310 djm 1281: if ((r = sshkey_sign(sign_key, sigp, lenp, data, datalen,
1.325 djm 1282: alg, options.sk_provider, pin, compat)) != 0) {
1.331 djm 1283: debug_fr(r, "sshkey_sign");
1.341 djm 1284: if (pin == NULL && !retried && sshkey_is_sk(sign_key) &&
1285: r == SSH_ERR_KEY_WRONG_PASSPHRASE) {
1286: notify_complete(notifier, NULL);
1287: notifier = NULL;
1288: retried = 1;
1289: goto retry_pin;
1290: }
1.309 djm 1291: goto out;
1292: }
1.341 djm 1293:
1.309 djm 1294: /*
1295: * PKCS#11 tokens may not support all signature algorithms,
1296: * so check what we get back.
1297: */
1298: if ((r = sshkey_check_sigtype(*sigp, *lenp, alg)) != 0) {
1.331 djm 1299: debug_fr(r, "sshkey_check_sigtype");
1.309 djm 1300: goto out;
1.265 djm 1301: }
1.309 djm 1302: /* success */
1303: r = 0;
1304: out:
1.325 djm 1305: free(prompt);
1306: if (pin != NULL)
1307: freezero(pin, strlen(pin));
1.341 djm 1308: notify_complete(notifier, r == 0 ? "User presence confirmed" : NULL);
1.214 djm 1309: sshkey_free(prv);
1.269 djm 1310: return r;
1.51 markus 1311: }
1312:
1.76 itojun 1313: static int
1.255 djm 1314: id_filename_matches(Identity *id, Identity *private_id)
1315: {
1.356 djm 1316: static const char * const suffixes[] = { ".pub", "-cert.pub", NULL };
1.255 djm 1317: size_t len = strlen(id->filename), plen = strlen(private_id->filename);
1318: size_t i, slen;
1319:
1320: if (strcmp(id->filename, private_id->filename) == 0)
1321: return 1;
1322: for (i = 0; suffixes[i]; i++) {
1323: slen = strlen(suffixes[i]);
1324: if (len > slen && plen == len - slen &&
1325: strcmp(id->filename + (len - slen), suffixes[i]) == 0 &&
1326: memcmp(id->filename, private_id->filename, plen) == 0)
1327: return 1;
1328: }
1329: return 0;
1330: }
1331:
1332: static int
1.295 djm 1333: sign_and_send_pubkey(struct ssh *ssh, Identity *id)
1.1 markus 1334: {
1.295 djm 1335: Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.272 djm 1336: struct sshbuf *b = NULL;
1337: Identity *private_id, *sign_id = NULL;
1338: u_char *signature = NULL;
1339: size_t slen = 0, skip = 0;
1340: int r, fallback_sigtype, sent = 0;
1341: char *alg = NULL, *fp = NULL;
1.353 djm 1342: const char *loc = "", *method = "publickey";
1.354 djm 1343: int hostbound = 0;
1.353 djm 1344:
1345: /* prefer host-bound pubkey signatures if supported by server */
1.354 djm 1346: if ((ssh->kex->flags & KEX_HAS_PUBKEY_HOSTBOUND) != 0 &&
1347: (options.pubkey_authentication & SSH_PUBKEY_AUTH_HBOUND) != 0) {
1348: hostbound = 1;
1.353 djm 1349: method = "publickey-hostbound-v00@openssh.com";
1.354 djm 1350: }
1.1 markus 1351:
1.222 djm 1352: if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1353: SSH_FP_DEFAULT)) == NULL)
1354: return 0;
1.51 markus 1355:
1.354 djm 1356: debug3_f("using %s with %s %s", method, sshkey_type(id->key), fp);
1.1 markus 1357:
1.227 djm 1358: /*
1359: * If the key is an certificate, try to find a matching private key
1360: * and use it to complete the signature.
1.241 djm 1361: * If no such private key exists, fall back to trying the certificate
1362: * key itself in case it has a private half already loaded.
1.272 djm 1363: * This will try to set sign_id to the private key that will perform
1364: * the signature.
1.227 djm 1365: */
1.272 djm 1366: if (sshkey_is_cert(id->key)) {
1.227 djm 1367: TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1368: if (sshkey_equal_public(id->key, private_id->key) &&
1369: id->key->type != private_id->key->type) {
1.272 djm 1370: sign_id = private_id;
1.227 djm 1371: break;
1372: }
1373: }
1.255 djm 1374: /*
1375: * Exact key matches are preferred, but also allow
1376: * filename matches for non-PKCS#11/agent keys that
1377: * didn't load public keys. This supports the case
1378: * of keeping just a private key file and public
1379: * certificate on disk.
1380: */
1.272 djm 1381: if (sign_id == NULL &&
1382: !id->isprivate && id->agent_fd == -1 &&
1.255 djm 1383: (id->key->flags & SSHKEY_FLAG_EXT) == 0) {
1384: TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1385: if (private_id->key == NULL &&
1386: id_filename_matches(id, private_id)) {
1.272 djm 1387: sign_id = private_id;
1.255 djm 1388: break;
1389: }
1390: }
1391: }
1.272 djm 1392: if (sign_id != NULL) {
1.331 djm 1393: debug2_f("using private key \"%s\"%s for "
1.349 djm 1394: "certificate", sign_id->filename,
1395: sign_id->agent_fd != -1 ? " from agent" : "");
1.227 djm 1396: } else {
1.331 djm 1397: debug_f("no separate private key for certificate "
1398: "\"%s\"", id->filename);
1.227 djm 1399: }
1400: }
1401:
1.272 djm 1402: /*
1403: * If the above didn't select another identity to do the signing
1404: * then default to the one we started with.
1405: */
1406: if (sign_id == NULL)
1407: sign_id = id;
1408:
1409: /* assemble and sign data */
1410: for (fallback_sigtype = 0; fallback_sigtype <= 1; fallback_sigtype++) {
1411: free(alg);
1412: slen = 0;
1413: signature = NULL;
1414: if ((alg = key_sig_algorithm(fallback_sigtype ? NULL : ssh,
1415: id->key)) == NULL) {
1.331 djm 1416: error_f("no mutual signature supported");
1.272 djm 1417: goto out;
1418: }
1.331 djm 1419: debug3_f("signing using %s %s", alg, fp);
1.272 djm 1420:
1421: sshbuf_free(b);
1422: if ((b = sshbuf_new()) == NULL)
1.331 djm 1423: fatal_f("sshbuf_new failed");
1.345 djm 1424: if (ssh->compat & SSH_OLD_SESSIONID) {
1.346 djm 1425: if ((r = sshbuf_putb(b, ssh->kex->session_id)) != 0)
1426: fatal_fr(r, "sshbuf_putb");
1.272 djm 1427: } else {
1.346 djm 1428: if ((r = sshbuf_put_stringb(b,
1429: ssh->kex->session_id)) != 0)
1430: fatal_fr(r, "sshbuf_put_stringb");
1.272 djm 1431: }
1.278 markus 1432: skip = sshbuf_len(b);
1.272 djm 1433: if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1434: (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1435: (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1.353 djm 1436: (r = sshbuf_put_cstring(b, method)) != 0 ||
1.272 djm 1437: (r = sshbuf_put_u8(b, 1)) != 0 ||
1438: (r = sshbuf_put_cstring(b, alg)) != 0 ||
1439: (r = sshkey_puts(id->key, b)) != 0) {
1.331 djm 1440: fatal_fr(r, "assemble signed data");
1.272 djm 1441: }
1.354 djm 1442: if (hostbound) {
1.353 djm 1443: if (ssh->kex->initial_hostkey == NULL) {
1444: fatal_f("internal error: initial hostkey "
1445: "not recorded");
1446: }
1447: if ((r = sshkey_puts(ssh->kex->initial_hostkey, b)) != 0)
1448: fatal_fr(r, "assemble %s hostkey", method);
1449: }
1.272 djm 1450: /* generate signature */
1451: r = identity_sign(sign_id, &signature, &slen,
1.345 djm 1452: sshbuf_ptr(b), sshbuf_len(b), ssh->compat, alg);
1.272 djm 1453: if (r == 0)
1454: break;
1455: else if (r == SSH_ERR_KEY_NOT_FOUND)
1456: goto out; /* soft failure */
1457: else if (r == SSH_ERR_SIGN_ALG_UNSUPPORTED &&
1458: !fallback_sigtype) {
1459: if (sign_id->agent_fd != -1)
1460: loc = "agent ";
1461: else if ((sign_id->key->flags & SSHKEY_FLAG_EXT) != 0)
1462: loc = "token ";
1463: logit("%skey %s %s returned incorrect signature type",
1464: loc, sshkey_type(id->key), fp);
1465: continue;
1466: }
1.331 djm 1467: error_fr(r, "signing failed for %s \"%s\"%s",
1.315 djm 1468: sshkey_type(sign_id->key), sign_id->filename,
1.331 djm 1469: id->agent_fd != -1 ? " from agent" : "");
1.272 djm 1470: goto out;
1.17 markus 1471: }
1.272 djm 1472: if (slen == 0 || signature == NULL) /* shouldn't happen */
1.331 djm 1473: fatal_f("no signature");
1.51 markus 1474:
1.1 markus 1475: /* append signature */
1.272 djm 1476: if ((r = sshbuf_put_string(b, signature, slen)) != 0)
1.331 djm 1477: fatal_fr(r, "append signature");
1.1 markus 1478:
1.272 djm 1479: #ifdef DEBUG_PK
1480: sshbuf_dump(b, stderr);
1481: #endif
1.1 markus 1482: /* skip session id and packet type */
1.272 djm 1483: if ((r = sshbuf_consume(b, skip + 1)) != 0)
1.331 djm 1484: fatal_fr(r, "consume");
1.1 markus 1485:
1486: /* put remaining data from buffer into packet */
1.272 djm 1487: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1488: (r = sshpkt_putb(ssh, b)) != 0 ||
1489: (r = sshpkt_send(ssh)) != 0)
1.331 djm 1490: fatal_fr(r, "enqueue request");
1.272 djm 1491:
1492: /* success */
1493: sent = 1;
1.17 markus 1494:
1.272 djm 1495: out:
1496: free(fp);
1497: free(alg);
1498: sshbuf_free(b);
1499: freezero(signature, slen);
1500: return sent;
1.16 markus 1501: }
1502:
1.76 itojun 1503: static int
1.295 djm 1504: send_pubkey_test(struct ssh *ssh, Identity *id)
1.20 markus 1505: {
1.295 djm 1506: Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.272 djm 1507: u_char *blob = NULL;
1508: char *alg = NULL;
1.278 markus 1509: size_t bloblen;
1510: u_int have_sig = 0;
1511: int sent = 0, r;
1.20 markus 1512:
1.272 djm 1513: if ((alg = key_sig_algorithm(ssh, id->key)) == NULL) {
1.331 djm 1514: debug_f("no mutual signature algorithm");
1.272 djm 1515: goto out;
1516: }
1.16 markus 1517:
1.278 markus 1518: if ((r = sshkey_to_blob(id->key, &blob, &bloblen)) != 0) {
1.51 markus 1519: /* we cannot handle this key */
1.331 djm 1520: debug3_f("cannot handle key");
1.272 djm 1521: goto out;
1.16 markus 1522: }
1.51 markus 1523: /* register callback for USERAUTH_PK_OK message */
1.278 markus 1524: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1.51 markus 1525:
1.278 markus 1526: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1527: (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1528: (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1529: (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1530: (r = sshpkt_put_u8(ssh, have_sig)) != 0 ||
1531: (r = sshpkt_put_cstring(ssh, alg)) != 0 ||
1532: (r = sshpkt_put_string(ssh, blob, bloblen)) != 0 ||
1533: (r = sshpkt_send(ssh)) != 0)
1.331 djm 1534: fatal_fr(r, "send packet");
1.272 djm 1535: sent = 1;
1.278 markus 1536:
1537: out:
1.272 djm 1538: free(alg);
1.197 djm 1539: free(blob);
1.272 djm 1540: return sent;
1.51 markus 1541: }
1542:
1.259 markus 1543: static struct sshkey *
1.229 jcs 1544: load_identity_file(Identity *id)
1.51 markus 1545: {
1.259 markus 1546: struct sshkey *private = NULL;
1.229 jcs 1547: char prompt[300], *passphrase, *comment;
1.308 dtucker 1548: int r, quit = 0, i;
1.52 markus 1549: struct stat st;
1.16 markus 1550:
1.306 deraadt 1551: if (stat(id->filename, &st) == -1) {
1.330 djm 1552: do_log2(id->userprovided ?
1553: SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_DEBUG3,
1554: "no such identity: %s: %s", id->filename, strerror(errno));
1.52 markus 1555: return NULL;
1556: }
1.214 djm 1557: snprintf(prompt, sizeof prompt,
1.229 jcs 1558: "Enter passphrase for key '%.100s': ", id->filename);
1.214 djm 1559: for (i = 0; i <= options.number_of_password_prompts; i++) {
1560: if (i == 0)
1561: passphrase = "";
1562: else {
1.20 markus 1563: passphrase = read_passphrase(prompt, 0);
1.214 djm 1564: if (*passphrase == '\0') {
1.20 markus 1565: debug2("no passphrase given, try next key");
1.214 djm 1566: free(passphrase);
1567: break;
1568: }
1569: }
1.229 jcs 1570: switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
1.308 dtucker 1571: passphrase, &private, &comment))) {
1.214 djm 1572: case 0:
1573: break;
1574: case SSH_ERR_KEY_WRONG_PASSPHRASE:
1575: if (options.batch_mode) {
1576: quit = 1;
1577: break;
1578: }
1.215 djm 1579: if (i != 0)
1580: debug2("bad passphrase given, try again...");
1.214 djm 1581: break;
1582: case SSH_ERR_SYSTEM_ERROR:
1583: if (errno == ENOENT) {
1.331 djm 1584: debug2_r(r, "Load key \"%s\"", id->filename);
1.51 markus 1585: quit = 1;
1.214 djm 1586: break;
1.20 markus 1587: }
1.214 djm 1588: /* FALLTHROUGH */
1589: default:
1.331 djm 1590: error_r(r, "Load key \"%s\"", id->filename);
1.214 djm 1591: quit = 1;
1592: break;
1593: }
1.311 markus 1594: if (private != NULL && sshkey_is_sk(private) &&
1.309 djm 1595: options.sk_provider == NULL) {
1.319 naddy 1596: debug("key \"%s\" is an authenticator-hosted key, "
1597: "but no provider specified", id->filename);
1.309 djm 1598: sshkey_free(private);
1599: private = NULL;
1600: quit = 1;
1601: }
1.230 djm 1602: if (!quit && private != NULL && id->agent_fd == -1 &&
1.229 jcs 1603: !(id->key && id->isprivate))
1604: maybe_add_key_to_agent(id->filename, private, comment,
1605: passphrase);
1.278 markus 1606: if (i > 0)
1607: freezero(passphrase, strlen(passphrase));
1.232 mmcc 1608: free(comment);
1.214 djm 1609: if (private != NULL || quit)
1610: break;
1.16 markus 1611: }
1.51 markus 1612: return private;
1613: }
1614:
1.272 djm 1615: static int
1616: key_type_allowed_by_config(struct sshkey *key)
1617: {
1618: if (match_pattern_list(sshkey_ssh_name(key),
1.342 dtucker 1619: options.pubkey_accepted_algos, 0) == 1)
1.272 djm 1620: return 1;
1621:
1622: /* RSA keys/certs might be allowed by alternate signature types */
1623: switch (key->type) {
1624: case KEY_RSA:
1625: if (match_pattern_list("rsa-sha2-512",
1.342 dtucker 1626: options.pubkey_accepted_algos, 0) == 1)
1.272 djm 1627: return 1;
1628: if (match_pattern_list("rsa-sha2-256",
1.342 dtucker 1629: options.pubkey_accepted_algos, 0) == 1)
1.272 djm 1630: return 1;
1631: break;
1632: case KEY_RSA_CERT:
1633: if (match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
1.342 dtucker 1634: options.pubkey_accepted_algos, 0) == 1)
1.272 djm 1635: return 1;
1636: if (match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
1.342 dtucker 1637: options.pubkey_accepted_algos, 0) == 1)
1.272 djm 1638: return 1;
1639: break;
1640: }
1641: return 0;
1642: }
1643:
1.352 djm 1644: /* obtain a list of keys from the agent */
1645: static int
1646: get_agent_identities(struct ssh *ssh, int *agent_fdp,
1647: struct ssh_identitylist **idlistp)
1648: {
1649: int r, agent_fd;
1650: struct ssh_identitylist *idlist;
1651:
1652: if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1653: if (r != SSH_ERR_AGENT_NOT_PRESENT)
1654: debug_fr(r, "ssh_get_authentication_socket");
1655: return r;
1656: }
1657: if ((r = ssh_agent_bind_hostkey(agent_fd, ssh->kex->initial_hostkey,
1658: ssh->kex->session_id, ssh->kex->initial_sig, 0)) == 0)
1659: debug_f("bound agent to hostkey");
1660: else
1661: debug2_fr(r, "ssh_agent_bind_hostkey");
1662:
1663: if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
1664: debug_fr(r, "ssh_fetch_identitylist");
1665: close(agent_fd);
1666: return r;
1667: }
1668: /* success */
1669: *agent_fdp = agent_fd;
1670: *idlistp = idlist;
1671: debug_f("agent returned %zu keys", idlist->nkeys);
1672: return 0;
1673: }
1.272 djm 1674:
1.117 markus 1675: /*
1676: * try keys in the following order:
1.332 djm 1677: * 1. certificates listed in the config file
1678: * 2. other input certificates
1.227 djm 1679: * 3. agent keys that are found in the config file
1680: * 4. other agent keys
1681: * 5. keys that are only listed in the config file
1.117 markus 1682: */
1683: static void
1.352 djm 1684: pubkey_prepare(struct ssh *ssh, Authctxt *authctxt)
1.51 markus 1685: {
1.214 djm 1686: struct identity *id, *id2, *tmp;
1687: struct idlist agent, files, *preferred;
1688: struct sshkey *key;
1.230 djm 1689: int agent_fd = -1, i, r, found;
1.214 djm 1690: size_t j;
1691: struct ssh_identitylist *idlist;
1.287 djm 1692: char *ident;
1.51 markus 1693:
1.117 markus 1694: TAILQ_INIT(&agent); /* keys from the agent */
1695: TAILQ_INIT(&files); /* keys from the config file */
1696: preferred = &authctxt->keys;
1697: TAILQ_INIT(preferred); /* preferred order of keys */
1698:
1.190 djm 1699: /* list of keys stored in the filesystem and PKCS#11 */
1.117 markus 1700: for (i = 0; i < options.num_identity_files; i++) {
1701: key = options.identity_keys[i];
1.309 djm 1702: if (key && key->cert &&
1703: key->cert->type != SSH2_CERT_TYPE_USER) {
1.331 djm 1704: debug_f("ignoring certificate %s: not a user "
1705: "certificate", options.identity_files[i]);
1.309 djm 1706: continue;
1707: }
1.311 markus 1708: if (key && sshkey_is_sk(key) && options.sk_provider == NULL) {
1.331 djm 1709: debug_f("ignoring authenticator-hosted key %s as no "
1.309 djm 1710: "SecurityKeyProvider has been specified",
1.331 djm 1711: options.identity_files[i]);
1.117 markus 1712: continue;
1.309 djm 1713: }
1.117 markus 1714: options.identity_keys[i] = NULL;
1.150 djm 1715: id = xcalloc(1, sizeof(*id));
1.230 djm 1716: id->agent_fd = -1;
1.117 markus 1717: id->key = key;
1718: id->filename = xstrdup(options.identity_files[i]);
1.192 dtucker 1719: id->userprovided = options.identity_file_userprovided[i];
1.117 markus 1720: TAILQ_INSERT_TAIL(&files, id, next);
1.190 djm 1721: }
1.227 djm 1722: /* list of certificates specified by user */
1723: for (i = 0; i < options.num_certificate_files; i++) {
1724: key = options.certificates[i];
1.278 markus 1725: if (!sshkey_is_cert(key) || key->cert == NULL ||
1.309 djm 1726: key->cert->type != SSH2_CERT_TYPE_USER) {
1.331 djm 1727: debug_f("ignoring certificate %s: not a user "
1728: "certificate", options.identity_files[i]);
1.227 djm 1729: continue;
1.309 djm 1730: }
1.311 markus 1731: if (key && sshkey_is_sk(key) && options.sk_provider == NULL) {
1.331 djm 1732: debug_f("ignoring authenticator-hosted key "
1.319 naddy 1733: "certificate %s as no "
1.309 djm 1734: "SecurityKeyProvider has been specified",
1.331 djm 1735: options.identity_files[i]);
1.309 djm 1736: continue;
1737: }
1.227 djm 1738: id = xcalloc(1, sizeof(*id));
1.230 djm 1739: id->agent_fd = -1;
1.227 djm 1740: id->key = key;
1741: id->filename = xstrdup(options.certificate_files[i]);
1742: id->userprovided = options.certificate_file_userprovided[i];
1743: TAILQ_INSERT_TAIL(preferred, id, next);
1.117 markus 1744: }
1745: /* list of keys supported by the agent */
1.352 djm 1746: if ((r = get_agent_identities(ssh, &agent_fd, &idlist)) == 0) {
1.214 djm 1747: for (j = 0; j < idlist->nkeys; j++) {
1.117 markus 1748: found = 0;
1749: TAILQ_FOREACH(id, &files, next) {
1.214 djm 1750: /*
1751: * agent keys from the config file are
1752: * preferred
1753: */
1754: if (sshkey_equal(idlist->keys[j], id->key)) {
1.117 markus 1755: TAILQ_REMOVE(&files, id, next);
1756: TAILQ_INSERT_TAIL(preferred, id, next);
1.214 djm 1757: id->agent_fd = agent_fd;
1.117 markus 1758: found = 1;
1759: break;
1760: }
1761: }
1.135 markus 1762: if (!found && !options.identities_only) {
1.150 djm 1763: id = xcalloc(1, sizeof(*id));
1.214 djm 1764: /* XXX "steals" key/comment from idlist */
1765: id->key = idlist->keys[j];
1766: id->filename = idlist->comments[j];
1767: idlist->keys[j] = NULL;
1768: idlist->comments[j] = NULL;
1769: id->agent_fd = agent_fd;
1.117 markus 1770: TAILQ_INSERT_TAIL(&agent, id, next);
1771: }
1772: }
1.214 djm 1773: ssh_free_identitylist(idlist);
1.117 markus 1774: /* append remaining agent keys */
1.324 bket 1775: TAILQ_CONCAT(preferred, &agent, next);
1.214 djm 1776: authctxt->agent_fd = agent_fd;
1.244 djm 1777: }
1778: /* Prefer PKCS11 keys that are explicitly listed */
1779: TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1780: if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
1781: continue;
1782: found = 0;
1783: TAILQ_FOREACH(id2, &files, next) {
1784: if (id2->key == NULL ||
1.321 djm 1785: (id2->key->flags & SSHKEY_FLAG_EXT) != 0)
1.244 djm 1786: continue;
1787: if (sshkey_equal(id->key, id2->key)) {
1788: TAILQ_REMOVE(&files, id, next);
1789: TAILQ_INSERT_TAIL(preferred, id, next);
1790: found = 1;
1791: break;
1792: }
1793: }
1794: /* If IdentitiesOnly set and key not found then don't use it */
1795: if (!found && options.identities_only) {
1796: TAILQ_REMOVE(&files, id, next);
1.278 markus 1797: freezero(id, sizeof(*id));
1.244 djm 1798: }
1.117 markus 1799: }
1800: /* append remaining keys from the config file */
1.324 bket 1801: TAILQ_CONCAT(preferred, &files, next);
1.342 dtucker 1802: /* finally, filter by PubkeyAcceptedAlgorithms */
1.228 djm 1803: TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1.273 djm 1804: if (id->key != NULL && !key_type_allowed_by_config(id->key)) {
1.228 djm 1805: debug("Skipping %s key %s - "
1.342 dtucker 1806: "corresponding algo not in PubkeyAcceptedAlgorithms",
1.228 djm 1807: sshkey_ssh_name(id->key), id->filename);
1808: TAILQ_REMOVE(preferred, id, next);
1809: sshkey_free(id->key);
1810: free(id->filename);
1811: memset(id, 0, sizeof(*id));
1812: continue;
1813: }
1.117 markus 1814: }
1.287 djm 1815: /* List the keys we plan on using */
1816: TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1817: ident = format_identity(id);
1818: debug("Will attempt key: %s", ident);
1819: free(ident);
1820: }
1.331 djm 1821: debug2_f("done");
1.17 markus 1822: }
1823:
1.117 markus 1824: static void
1.303 djm 1825: pubkey_cleanup(struct ssh *ssh)
1.51 markus 1826: {
1.302 djm 1827: Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.117 markus 1828: Identity *id;
1.51 markus 1829:
1.289 djm 1830: if (authctxt->agent_fd != -1) {
1.214 djm 1831: ssh_close_authentication_socket(authctxt->agent_fd);
1.289 djm 1832: authctxt->agent_fd = -1;
1833: }
1.117 markus 1834: for (id = TAILQ_FIRST(&authctxt->keys); id;
1835: id = TAILQ_FIRST(&authctxt->keys)) {
1836: TAILQ_REMOVE(&authctxt->keys, id, next);
1.235 mmcc 1837: sshkey_free(id->key);
1.197 djm 1838: free(id->filename);
1839: free(id);
1.117 markus 1840: }
1.1 markus 1841: }
1842:
1.251 djm 1843: static void
1844: pubkey_reset(Authctxt *authctxt)
1845: {
1846: Identity *id;
1847:
1848: TAILQ_FOREACH(id, &authctxt->keys, next)
1849: id->tried = 0;
1850: }
1851:
1.225 markus 1852: static int
1.345 djm 1853: try_identity(struct ssh *ssh, Identity *id)
1.225 markus 1854: {
1855: if (!id->key)
1856: return (0);
1.279 markus 1857: if (sshkey_type_plain(id->key->type) == KEY_RSA &&
1.345 djm 1858: (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
1.225 markus 1859: debug("Skipped %s key %s for RSA/MD5 server",
1.279 markus 1860: sshkey_type(id->key), id->filename);
1.225 markus 1861: return (0);
1862: }
1.257 djm 1863: return 1;
1.225 markus 1864: }
1865:
1.302 djm 1866: static int
1.295 djm 1867: userauth_pubkey(struct ssh *ssh)
1.20 markus 1868: {
1.295 djm 1869: Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.117 markus 1870: Identity *id;
1.20 markus 1871: int sent = 0;
1.287 djm 1872: char *ident;
1.20 markus 1873:
1.117 markus 1874: while ((id = TAILQ_FIRST(&authctxt->keys))) {
1875: if (id->tried++)
1876: return (0);
1.127 markus 1877: /* move key to the end of the queue */
1.117 markus 1878: TAILQ_REMOVE(&authctxt->keys, id, next);
1879: TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1880: /*
1881: * send a test message if we have the public key. for
1882: * encrypted keys we cannot do this and have to load the
1883: * private key instead
1884: */
1.200 djm 1885: if (id->key != NULL) {
1.345 djm 1886: if (try_identity(ssh, id)) {
1.287 djm 1887: ident = format_identity(id);
1888: debug("Offering public key: %s", ident);
1889: free(ident);
1.295 djm 1890: sent = send_pubkey_test(ssh, id);
1.200 djm 1891: }
1892: } else {
1.117 markus 1893: debug("Trying private key: %s", id->filename);
1.229 jcs 1894: id->key = load_identity_file(id);
1.117 markus 1895: if (id->key != NULL) {
1.345 djm 1896: if (try_identity(ssh, id)) {
1.225 markus 1897: id->isprivate = 1;
1.295 djm 1898: sent = sign_and_send_pubkey(ssh, id);
1.200 djm 1899: }
1.278 markus 1900: sshkey_free(id->key);
1.117 markus 1901: id->key = NULL;
1.251 djm 1902: id->isprivate = 0;
1.51 markus 1903: }
1904: }
1.117 markus 1905: if (sent)
1906: return (sent);
1.28 markus 1907: }
1.117 markus 1908: return (0);
1.20 markus 1909: }
1910:
1.23 markus 1911: /*
1912: * Send userauth request message specifying keyboard-interactive method.
1913: */
1.302 djm 1914: static int
1.295 djm 1915: userauth_kbdint(struct ssh *ssh)
1.23 markus 1916: {
1.295 djm 1917: Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.278 markus 1918: int r;
1.23 markus 1919:
1.292 djm 1920: if (authctxt->attempt_kbdint++ >= options.number_of_password_prompts)
1.23 markus 1921: return 0;
1.82 markus 1922: /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1.292 djm 1923: if (authctxt->attempt_kbdint > 1 && !authctxt->info_req_seen) {
1.82 markus 1924: debug3("userauth_kbdint: disable: no info_req_seen");
1.278 markus 1925: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1.82 markus 1926: return 0;
1927: }
1.23 markus 1928:
1929: debug2("userauth_kbdint");
1.278 markus 1930: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1931: (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1932: (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1933: (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1934: (r = sshpkt_put_cstring(ssh, "")) != 0 || /* lang */
1935: (r = sshpkt_put_cstring(ssh, options.kbd_interactive_devices ?
1936: options.kbd_interactive_devices : "")) != 0 ||
1937: (r = sshpkt_send(ssh)) != 0)
1.331 djm 1938: fatal_fr(r, "send packet");
1.23 markus 1939:
1.278 markus 1940: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1.23 markus 1941: return 1;
1942: }
1943:
1944: /*
1.46 markus 1945: * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1.23 markus 1946: */
1.302 djm 1947: static int
1.261 markus 1948: input_userauth_info_req(int type, u_int32_t seq, struct ssh *ssh)
1.23 markus 1949: {
1.260 markus 1950: Authctxt *authctxt = ssh->authctxt;
1.278 markus 1951: char *name = NULL, *inst = NULL, *lang = NULL, *prompt = NULL;
1.335 djm 1952: char *display_prompt = NULL, *response = NULL;
1.278 markus 1953: u_char echo = 0;
1.32 markus 1954: u_int num_prompts, i;
1.278 markus 1955: int r;
1.23 markus 1956:
1.335 djm 1957: debug2_f("entering");
1.23 markus 1958:
1959: if (authctxt == NULL)
1.335 djm 1960: fatal_f("no authentication context");
1.82 markus 1961:
1962: authctxt->info_req_seen = 1;
1.23 markus 1963:
1.278 markus 1964: if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 ||
1965: (r = sshpkt_get_cstring(ssh, &inst, NULL)) != 0 ||
1966: (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1967: goto out;
1.23 markus 1968: if (strlen(name) > 0)
1.116 itojun 1969: logit("%s", name);
1.23 markus 1970: if (strlen(inst) > 0)
1.116 itojun 1971: logit("%s", inst);
1.23 markus 1972:
1.278 markus 1973: if ((r = sshpkt_get_u32(ssh, &num_prompts)) != 0)
1974: goto out;
1.23 markus 1975: /*
1976: * Begin to build info response packet based on prompts requested.
1977: * We commit to providing the correct number of responses, so if
1978: * further on we run into a problem that prevents this, we have to
1979: * be sure and clean this up and send a correct error response.
1980: */
1.278 markus 1981: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE)) != 0 ||
1982: (r = sshpkt_put_u32(ssh, num_prompts)) != 0)
1983: goto out;
1.23 markus 1984:
1.335 djm 1985: debug2_f("num_prompts %d", num_prompts);
1.23 markus 1986: for (i = 0; i < num_prompts; i++) {
1.278 markus 1987: if ((r = sshpkt_get_cstring(ssh, &prompt, NULL)) != 0 ||
1988: (r = sshpkt_get_u8(ssh, &echo)) != 0)
1989: goto out;
1.336 djm 1990: if (asmprintf(&display_prompt, INT_MAX, NULL, "(%s@%s) %s",
1.335 djm 1991: authctxt->server_user, options.host_key_alias ?
1.336 djm 1992: options.host_key_alias : authctxt->host, prompt) == -1)
1993: fatal_f("asmprintf failed");
1.335 djm 1994: response = read_passphrase(display_prompt, echo ? RP_ECHO : 0);
1.278 markus 1995: if ((r = sshpkt_put_cstring(ssh, response)) != 0)
1996: goto out;
1997: freezero(response, strlen(response));
1.197 djm 1998: free(prompt);
1.335 djm 1999: free(display_prompt);
2000: display_prompt = response = prompt = NULL;
1.23 markus 2001: }
1.278 markus 2002: /* done with parsing incoming message. */
2003: if ((r = sshpkt_get_end(ssh)) != 0 ||
2004: (r = sshpkt_add_padding(ssh, 64)) != 0)
2005: goto out;
2006: r = sshpkt_send(ssh);
2007: out:
2008: if (response)
2009: freezero(response, strlen(response));
2010: free(prompt);
1.335 djm 2011: free(display_prompt);
1.278 markus 2012: free(name);
2013: free(inst);
2014: free(lang);
2015: return r;
1.68 markus 2016: }
2017:
1.100 markus 2018: static int
1.294 djm 2019: ssh_keysign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
1.223 djm 2020: const u_char *data, size_t datalen)
1.100 markus 2021: {
1.223 djm 2022: struct sshbuf *b;
1.101 markus 2023: struct stat st;
1.100 markus 2024: pid_t pid;
1.304 deraadt 2025: int r, to[2], from[2], status;
1.295 djm 2026: int sock = ssh_packet_get_connection_in(ssh);
1.223 djm 2027: u_char rversion = 0, version = 2;
2028: void (*osigchld)(int);
1.100 markus 2029:
1.223 djm 2030: *sigp = NULL;
2031: *lenp = 0;
1.100 markus 2032:
1.306 deraadt 2033: if (stat(_PATH_SSH_KEY_SIGN, &st) == -1) {
1.331 djm 2034: error_f("not installed: %s", strerror(errno));
1.223 djm 2035: return -1;
2036: }
2037: if (fflush(stdout) != 0) {
1.331 djm 2038: error_f("fflush: %s", strerror(errno));
1.101 markus 2039: return -1;
2040: }
1.306 deraadt 2041: if (pipe(to) == -1) {
1.331 djm 2042: error_f("pipe: %s", strerror(errno));
1.100 markus 2043: return -1;
2044: }
1.306 deraadt 2045: if (pipe(from) == -1) {
1.331 djm 2046: error_f("pipe: %s", strerror(errno));
1.100 markus 2047: return -1;
2048: }
1.306 deraadt 2049: if ((pid = fork()) == -1) {
1.331 djm 2050: error_f("fork: %s", strerror(errno));
1.100 markus 2051: return -1;
2052: }
1.317 dtucker 2053: osigchld = ssh_signal(SIGCHLD, SIG_DFL);
1.100 markus 2054: if (pid == 0) {
2055: close(from[0]);
1.306 deraadt 2056: if (dup2(from[1], STDOUT_FILENO) == -1)
1.331 djm 2057: fatal_f("dup2: %s", strerror(errno));
1.100 markus 2058: close(to[1]);
1.306 deraadt 2059: if (dup2(to[0], STDIN_FILENO) == -1)
1.331 djm 2060: fatal_f("dup2: %s", strerror(errno));
1.103 markus 2061: close(from[1]);
2062: close(to[0]);
1.304 deraadt 2063:
1.306 deraadt 2064: if (dup2(sock, STDERR_FILENO + 1) == -1)
1.331 djm 2065: fatal_f("dup2: %s", strerror(errno));
1.304 deraadt 2066: sock = STDERR_FILENO + 1;
2067: fcntl(sock, F_SETFD, 0); /* keep the socket on exec */
1.223 djm 2068: closefrom(sock + 1);
1.304 deraadt 2069:
1.331 djm 2070: debug3_f("[child] pid=%ld, exec %s",
2071: (long)getpid(), _PATH_SSH_KEY_SIGN);
1.233 mmcc 2072: execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
1.331 djm 2073: fatal_f("exec(%s): %s", _PATH_SSH_KEY_SIGN,
1.100 markus 2074: strerror(errno));
2075: }
2076: close(from[1]);
2077: close(to[0]);
1.305 djm 2078: sock = STDERR_FILENO + 1;
1.100 markus 2079:
1.223 djm 2080: if ((b = sshbuf_new()) == NULL)
1.331 djm 2081: fatal_f("sshbuf_new failed");
1.223 djm 2082: /* send # of sock, data to be signed */
1.253 djm 2083: if ((r = sshbuf_put_u32(b, sock)) != 0 ||
1.223 djm 2084: (r = sshbuf_put_string(b, data, datalen)) != 0)
1.331 djm 2085: fatal_fr(r, "buffer error");
1.223 djm 2086: if (ssh_msg_send(to[1], version, b) == -1)
1.331 djm 2087: fatal_f("couldn't send request");
1.223 djm 2088: sshbuf_reset(b);
2089: r = ssh_msg_recv(from[0], b);
1.101 markus 2090: close(from[0]);
2091: close(to[1]);
1.223 djm 2092: if (r < 0) {
1.331 djm 2093: error_f("no reply");
1.223 djm 2094: goto fail;
2095: }
1.101 markus 2096:
1.223 djm 2097: errno = 0;
1.306 deraadt 2098: while (waitpid(pid, &status, 0) == -1) {
1.223 djm 2099: if (errno != EINTR) {
1.331 djm 2100: error_f("waitpid %ld: %s", (long)pid, strerror(errno));
1.223 djm 2101: goto fail;
2102: }
2103: }
2104: if (!WIFEXITED(status)) {
1.331 djm 2105: error_f("exited abnormally");
1.223 djm 2106: goto fail;
2107: }
2108: if (WEXITSTATUS(status) != 0) {
1.331 djm 2109: error_f("exited with status %d", WEXITSTATUS(status));
1.223 djm 2110: goto fail;
2111: }
2112: if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
1.331 djm 2113: error_fr(r, "buffer error");
1.223 djm 2114: goto fail;
2115: }
2116: if (rversion != version) {
1.331 djm 2117: error_f("bad version");
1.223 djm 2118: goto fail;
2119: }
2120: if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
1.331 djm 2121: error_fr(r, "buffer error");
1.223 djm 2122: fail:
1.317 dtucker 2123: ssh_signal(SIGCHLD, osigchld);
1.223 djm 2124: sshbuf_free(b);
1.100 markus 2125: return -1;
2126: }
1.317 dtucker 2127: ssh_signal(SIGCHLD, osigchld);
1.223 djm 2128: sshbuf_free(b);
1.100 markus 2129:
2130: return 0;
2131: }
2132:
1.302 djm 2133: static int
1.295 djm 2134: userauth_hostbased(struct ssh *ssh)
1.68 markus 2135: {
1.295 djm 2136: Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.223 djm 2137: struct sshkey *private = NULL;
2138: struct sshbuf *b = NULL;
2139: u_char *sig = NULL, *keyblob = NULL;
2140: char *fp = NULL, *chost = NULL, *lname = NULL;
2141: size_t siglen = 0, keylen = 0;
2142: int i, r, success = 0;
2143:
2144: if (authctxt->ktypes == NULL) {
1.344 dtucker 2145: authctxt->oktypes = xstrdup(options.hostbased_accepted_algos);
1.223 djm 2146: authctxt->ktypes = authctxt->oktypes;
2147: }
1.213 djm 2148:
1.223 djm 2149: /*
1.344 dtucker 2150: * Work through each listed type pattern in HostbasedAcceptedAlgorithms,
1.223 djm 2151: * trying each hostkey that matches the type in turn.
2152: */
2153: for (;;) {
2154: if (authctxt->active_ktype == NULL)
2155: authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
2156: if (authctxt->active_ktype == NULL ||
2157: *authctxt->active_ktype == '\0')
2158: break;
1.331 djm 2159: debug3_f("trying key type %s", authctxt->active_ktype);
1.68 markus 2160:
1.223 djm 2161: /* check for a useful key */
2162: private = NULL;
2163: for (i = 0; i < authctxt->sensitive->nkeys; i++) {
2164: if (authctxt->sensitive->keys[i] == NULL ||
2165: authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
2166: continue;
1.355 djm 2167: if (!sshkey_match_keyname_to_sigalgs(
1.223 djm 2168: sshkey_ssh_name(authctxt->sensitive->keys[i]),
1.355 djm 2169: authctxt->active_ktype))
1.223 djm 2170: continue;
1.68 markus 2171: /* we take and free the key */
1.223 djm 2172: private = authctxt->sensitive->keys[i];
2173: authctxt->sensitive->keys[i] = NULL;
1.68 markus 2174: break;
2175: }
1.223 djm 2176: /* Found one */
2177: if (private != NULL)
2178: break;
2179: /* No more keys of this type; advance */
2180: authctxt->active_ktype = NULL;
1.68 markus 2181: }
1.223 djm 2182: if (private == NULL) {
2183: free(authctxt->oktypes);
2184: authctxt->oktypes = authctxt->ktypes = NULL;
2185: authctxt->active_ktype = NULL;
1.109 markus 2186: debug("No more client hostkeys for hostbased authentication.");
1.223 djm 2187: goto out;
1.68 markus 2188: }
1.211 djm 2189:
1.223 djm 2190: if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
2191: SSH_FP_DEFAULT)) == NULL) {
1.331 djm 2192: error_f("sshkey_fingerprint failed");
1.223 djm 2193: goto out;
1.68 markus 2194: }
1.355 djm 2195: debug_f("trying hostkey %s %s using sigalg %s",
2196: sshkey_ssh_name(private), fp, authctxt->active_ktype);
1.211 djm 2197:
1.84 markus 2198: /* figure out a name for the client host */
1.295 djm 2199: lname = get_local_name(ssh_packet_get_connection_in(ssh));
2200: if (lname == NULL) {
1.331 djm 2201: error_f("cannot get local ipaddr/name");
1.223 djm 2202: goto out;
1.84 markus 2203: }
1.223 djm 2204:
2205: /* XXX sshbuf_put_stringf? */
2206: xasprintf(&chost, "%s.", lname);
1.331 djm 2207: debug2_f("chost %s", chost);
1.84 markus 2208:
1.68 markus 2209: /* construct data */
1.223 djm 2210: if ((b = sshbuf_new()) == NULL) {
1.331 djm 2211: error_f("sshbuf_new failed");
1.223 djm 2212: goto out;
2213: }
2214: if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
1.331 djm 2215: error_fr(r, "sshkey_to_blob");
1.223 djm 2216: goto out;
2217: }
1.346 djm 2218: if ((r = sshbuf_put_stringb(b, ssh->kex->session_id)) != 0 ||
1.223 djm 2219: (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2220: (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1.267 djm 2221: (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1.223 djm 2222: (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1.343 djm 2223: (r = sshbuf_put_cstring(b, authctxt->active_ktype)) != 0 ||
1.223 djm 2224: (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
2225: (r = sshbuf_put_cstring(b, chost)) != 0 ||
2226: (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
1.331 djm 2227: error_fr(r, "buffer error");
1.223 djm 2228: goto out;
2229: }
2230:
1.68 markus 2231: #ifdef DEBUG_PK
1.223 djm 2232: sshbuf_dump(b, stderr);
1.68 markus 2233: #endif
1.294 djm 2234: if ((r = ssh_keysign(ssh, private, &sig, &siglen,
2235: sshbuf_ptr(b), sshbuf_len(b))) != 0) {
1.223 djm 2236: error("sign using hostkey %s %s failed",
2237: sshkey_ssh_name(private), fp);
2238: goto out;
2239: }
2240: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2241: (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
2242: (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
2243: (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1.343 djm 2244: (r = sshpkt_put_cstring(ssh, authctxt->active_ktype)) != 0 ||
1.223 djm 2245: (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
2246: (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
2247: (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
2248: (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
2249: (r = sshpkt_send(ssh)) != 0) {
1.331 djm 2250: error_fr(r, "packet error");
1.223 djm 2251: goto out;
2252: }
2253: success = 1;
2254:
2255: out:
1.278 markus 2256: if (sig != NULL)
2257: freezero(sig, siglen);
1.223 djm 2258: free(keyblob);
2259: free(lname);
2260: free(fp);
1.197 djm 2261: free(chost);
1.223 djm 2262: sshkey_free(private);
2263: sshbuf_free(b);
1.68 markus 2264:
1.223 djm 2265: return success;
1.23 markus 2266: }
1.170 djm 2267:
1.20 markus 2268: /* find auth method */
2269:
2270: /*
2271: * given auth method name, if configurable options permit this method fill
2272: * in auth_ident field and return true, otherwise return false.
2273: */
1.76 itojun 2274: static int
1.20 markus 2275: authmethod_is_enabled(Authmethod *method)
2276: {
2277: if (method == NULL)
2278: return 0;
2279: /* return false if options indicate this method is disabled */
2280: if (method->enabled == NULL || *method->enabled == 0)
2281: return 0;
2282: /* return false if batch mode is enabled but method needs interactive mode */
2283: if (method->batch_flag != NULL && *method->batch_flag != 0)
2284: return 0;
2285: return 1;
2286: }
2287:
1.76 itojun 2288: static Authmethod *
1.20 markus 2289: authmethod_lookup(const char *name)
1.1 markus 2290: {
1.20 markus 2291: Authmethod *method = NULL;
2292: if (name != NULL)
2293: for (method = authmethods; method->name != NULL; method++)
2294: if (strcmp(name, method->name) == 0)
2295: return method;
2296: debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
2297: return NULL;
2298: }
1.1 markus 2299:
1.53 markus 2300: /* XXX internal state */
2301: static Authmethod *current = NULL;
2302: static char *supported = NULL;
2303: static char *preferred = NULL;
1.105 deraadt 2304:
1.20 markus 2305: /*
2306: * Given the authentication method list sent by the server, return the
2307: * next method we should try. If the server initially sends a nil list,
1.67 markus 2308: * use a built-in default list.
1.41 stevesk 2309: */
1.76 itojun 2310: static Authmethod *
1.20 markus 2311: authmethod_get(char *authlist)
2312: {
1.53 markus 2313: char *name = NULL;
1.97 markus 2314: u_int next;
1.41 stevesk 2315:
1.20 markus 2316: /* Use a suitable default if we're passed a nil list. */
2317: if (authlist == NULL || strlen(authlist) == 0)
1.53 markus 2318: authlist = options.preferred_authentications;
1.20 markus 2319:
1.53 markus 2320: if (supported == NULL || strcmp(authlist, supported) != 0) {
2321: debug3("start over, passed a different list %s", authlist);
1.197 djm 2322: free(supported);
1.53 markus 2323: supported = xstrdup(authlist);
2324: preferred = options.preferred_authentications;
2325: debug3("preferred %s", preferred);
2326: current = NULL;
2327: } else if (current != NULL && authmethod_is_enabled(current))
2328: return current;
1.20 markus 2329:
1.53 markus 2330: for (;;) {
2331: if ((name = match_list(preferred, supported, &next)) == NULL) {
1.109 markus 2332: debug("No more authentication methods to try.");
1.53 markus 2333: current = NULL;
2334: return NULL;
2335: }
2336: preferred += next;
1.23 markus 2337: debug3("authmethod_lookup %s", name);
1.53 markus 2338: debug3("remaining preferred: %s", preferred);
2339: if ((current = authmethod_lookup(name)) != NULL &&
2340: authmethod_is_enabled(current)) {
1.23 markus 2341: debug3("authmethod_is_enabled %s", name);
1.109 markus 2342: debug("Next authentication method: %s", name);
1.197 djm 2343: free(name);
1.53 markus 2344: return current;
1.23 markus 2345: }
1.198 dtucker 2346: free(name);
1.1 markus 2347: }
1.53 markus 2348: }
1.1 markus 2349:
1.79 stevesk 2350: static char *
1.53 markus 2351: authmethods_get(void)
2352: {
2353: Authmethod *method = NULL;
1.278 markus 2354: struct sshbuf *b;
1.93 markus 2355: char *list;
1.278 markus 2356: int r;
1.27 provos 2357:
1.278 markus 2358: if ((b = sshbuf_new()) == NULL)
1.331 djm 2359: fatal_f("sshbuf_new failed");
1.53 markus 2360: for (method = authmethods; method->name != NULL; method++) {
2361: if (authmethod_is_enabled(method)) {
1.278 markus 2362: if ((r = sshbuf_putf(b, "%s%s",
2363: sshbuf_len(b) ? "," : "", method->name)) != 0)
1.331 djm 2364: fatal_fr(r, "buffer error");
1.53 markus 2365: }
2366: }
1.278 markus 2367: if ((list = sshbuf_dup_string(b)) == NULL)
1.331 djm 2368: fatal_f("sshbuf_dup_string failed");
1.278 markus 2369: sshbuf_free(b);
1.93 markus 2370: return list;
1.1 markus 2371: }