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