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