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