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