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