Annotation of src/usr.bin/ssh/sshconnect2.c, Revision 1.325
1.325 ! djm 1: /* $OpenBSD: sshconnect2.c,v 1.324 2020/06/27 13:39:09 bket 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.323 djm 1152: tmp = match_list(sshkey_sigalg_by_name(cp),
1153: ssh->kex->server_sig_algs, NULL);
1.274 djm 1154: if (tmp != NULL)
1155: alg = xstrdup(cp);
1156: free(tmp);
1.272 djm 1157: if (alg != NULL)
1158: break;
1.269 djm 1159: }
1.272 djm 1160: free(oallowed);
1161: return alg;
1.269 djm 1162: }
1163:
1.117 markus 1164: static int
1.214 djm 1165: identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
1.272 djm 1166: const u_char *data, size_t datalen, u_int compat, const char *alg)
1.117 markus 1167: {
1.309 djm 1168: struct sshkey *sign_key = NULL, *prv = NULL;
1169: int r = SSH_ERR_INTERNAL_ERROR;
1.312 djm 1170: struct notifier_ctx *notifier = NULL;
1.325 ! djm 1171: char *fp = NULL, *pin = NULL, *prompt = NULL;
1.309 djm 1172:
1173: *sigp = NULL;
1174: *lenp = 0;
1.99 markus 1175:
1.272 djm 1176: /* The agent supports this key. */
1.269 djm 1177: if (id->key != NULL && id->agent_fd != -1) {
1.272 djm 1178: return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
1179: data, datalen, alg, compat);
1.269 djm 1180: }
1.214 djm 1181:
1.117 markus 1182: /*
1.272 djm 1183: * We have already loaded the private key or the private key is
1184: * stored in external hardware.
1.117 markus 1185: */
1.255 djm 1186: if (id->key != NULL &&
1.272 djm 1187: (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))) {
1.309 djm 1188: sign_key = id->key;
1189: } else {
1190: /* Load the private key from the file. */
1191: if ((prv = load_identity_file(id)) == NULL)
1192: return SSH_ERR_KEY_NOT_FOUND;
1193: if (id->key != NULL && !sshkey_equal_public(prv, id->key)) {
1194: error("%s: private key %s contents do not match public",
1195: __func__, id->filename);
1196: r = SSH_ERR_KEY_NOT_FOUND;
1197: goto out;
1198: }
1199: sign_key = prv;
1.325 ! djm 1200: if (sshkey_is_sk(sign_key)) {
! 1201: if ((sign_key->sk_flags &
! 1202: SSH_SK_USER_VERIFICATION_REQD)) {
! 1203: xasprintf(&prompt, "Enter PIN for %s key %s: ",
! 1204: sshkey_type(sign_key), id->filename);
! 1205: pin = read_passphrase(prompt, 0);
! 1206: }
! 1207: if ((sign_key->sk_flags & SSH_SK_USER_PRESENCE_REQD)) {
! 1208: /* XXX should batch mode just skip these? */
! 1209: if ((fp = sshkey_fingerprint(sign_key,
! 1210: options.fingerprint_hash,
! 1211: SSH_FP_DEFAULT)) == NULL)
! 1212: fatal("%s: fingerprint", __func__);
! 1213: notifier = notify_start(options.batch_mode,
! 1214: "Confirm user presence for key %s %s",
! 1215: sshkey_type(sign_key), fp);
! 1216: free(fp);
! 1217: }
1.312 djm 1218: }
1.272 djm 1219: }
1.310 djm 1220: if ((r = sshkey_sign(sign_key, sigp, lenp, data, datalen,
1.325 ! djm 1221: alg, options.sk_provider, pin, compat)) != 0) {
1.309 djm 1222: debug("%s: sshkey_sign: %s", __func__, ssh_err(r));
1223: goto out;
1224: }
1225: /*
1226: * PKCS#11 tokens may not support all signature algorithms,
1227: * so check what we get back.
1228: */
1229: if ((r = sshkey_check_sigtype(*sigp, *lenp, alg)) != 0) {
1230: debug("%s: sshkey_check_sigtype: %s", __func__, ssh_err(r));
1231: goto out;
1.265 djm 1232: }
1.309 djm 1233: /* success */
1234: r = 0;
1235: out:
1.325 ! djm 1236: free(prompt);
! 1237: if (pin != NULL)
! 1238: freezero(pin, strlen(pin));
1.314 djm 1239: notify_complete(notifier);
1.214 djm 1240: sshkey_free(prv);
1.269 djm 1241: return r;
1.51 markus 1242: }
1243:
1.76 itojun 1244: static int
1.255 djm 1245: id_filename_matches(Identity *id, Identity *private_id)
1246: {
1247: const char *suffixes[] = { ".pub", "-cert.pub", NULL };
1248: size_t len = strlen(id->filename), plen = strlen(private_id->filename);
1249: size_t i, slen;
1250:
1251: if (strcmp(id->filename, private_id->filename) == 0)
1252: return 1;
1253: for (i = 0; suffixes[i]; i++) {
1254: slen = strlen(suffixes[i]);
1255: if (len > slen && plen == len - slen &&
1256: strcmp(id->filename + (len - slen), suffixes[i]) == 0 &&
1257: memcmp(id->filename, private_id->filename, plen) == 0)
1258: return 1;
1259: }
1260: return 0;
1261: }
1262:
1263: static int
1.295 djm 1264: sign_and_send_pubkey(struct ssh *ssh, Identity *id)
1.1 markus 1265: {
1.295 djm 1266: Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.272 djm 1267: struct sshbuf *b = NULL;
1268: Identity *private_id, *sign_id = NULL;
1269: u_char *signature = NULL;
1270: size_t slen = 0, skip = 0;
1271: int r, fallback_sigtype, sent = 0;
1272: char *alg = NULL, *fp = NULL;
1273: const char *loc = "";
1.1 markus 1274:
1.222 djm 1275: if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
1276: SSH_FP_DEFAULT)) == NULL)
1277: return 0;
1.51 markus 1278:
1.272 djm 1279: debug3("%s: %s %s", __func__, sshkey_type(id->key), fp);
1.1 markus 1280:
1.227 djm 1281: /*
1282: * If the key is an certificate, try to find a matching private key
1283: * and use it to complete the signature.
1.241 djm 1284: * If no such private key exists, fall back to trying the certificate
1285: * key itself in case it has a private half already loaded.
1.272 djm 1286: * This will try to set sign_id to the private key that will perform
1287: * the signature.
1.227 djm 1288: */
1.272 djm 1289: if (sshkey_is_cert(id->key)) {
1.227 djm 1290: TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1291: if (sshkey_equal_public(id->key, private_id->key) &&
1292: id->key->type != private_id->key->type) {
1.272 djm 1293: sign_id = private_id;
1.227 djm 1294: break;
1295: }
1296: }
1.255 djm 1297: /*
1298: * Exact key matches are preferred, but also allow
1299: * filename matches for non-PKCS#11/agent keys that
1300: * didn't load public keys. This supports the case
1301: * of keeping just a private key file and public
1302: * certificate on disk.
1303: */
1.272 djm 1304: if (sign_id == NULL &&
1305: !id->isprivate && id->agent_fd == -1 &&
1.255 djm 1306: (id->key->flags & SSHKEY_FLAG_EXT) == 0) {
1307: TAILQ_FOREACH(private_id, &authctxt->keys, next) {
1308: if (private_id->key == NULL &&
1309: id_filename_matches(id, private_id)) {
1.272 djm 1310: sign_id = private_id;
1.255 djm 1311: break;
1312: }
1313: }
1314: }
1.272 djm 1315: if (sign_id != NULL) {
1.227 djm 1316: debug2("%s: using private key \"%s\"%s for "
1317: "certificate", __func__, id->filename,
1318: id->agent_fd != -1 ? " from agent" : "");
1319: } else {
1.240 djm 1320: debug("%s: no separate private key for certificate "
1.227 djm 1321: "\"%s\"", __func__, id->filename);
1322: }
1323: }
1324:
1.272 djm 1325: /*
1326: * If the above didn't select another identity to do the signing
1327: * then default to the one we started with.
1328: */
1329: if (sign_id == NULL)
1330: sign_id = id;
1331:
1332: /* assemble and sign data */
1333: for (fallback_sigtype = 0; fallback_sigtype <= 1; fallback_sigtype++) {
1334: free(alg);
1335: slen = 0;
1336: signature = NULL;
1337: if ((alg = key_sig_algorithm(fallback_sigtype ? NULL : ssh,
1338: id->key)) == NULL) {
1339: error("%s: no mutual signature supported", __func__);
1340: goto out;
1341: }
1.315 djm 1342: debug3("%s: signing using %s %s", __func__, alg, fp);
1.272 djm 1343:
1344: sshbuf_free(b);
1345: if ((b = sshbuf_new()) == NULL)
1346: fatal("%s: sshbuf_new failed", __func__);
1347: if (datafellows & SSH_OLD_SESSIONID) {
1348: if ((r = sshbuf_put(b, session_id2,
1349: session_id2_len)) != 0) {
1350: fatal("%s: sshbuf_put: %s",
1351: __func__, ssh_err(r));
1352: }
1353: } else {
1354: if ((r = sshbuf_put_string(b, session_id2,
1355: session_id2_len)) != 0) {
1356: fatal("%s: sshbuf_put_string: %s",
1357: __func__, ssh_err(r));
1358: }
1359: }
1.278 markus 1360: skip = sshbuf_len(b);
1.272 djm 1361: if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1362: (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1363: (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1364: (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1365: (r = sshbuf_put_u8(b, 1)) != 0 ||
1366: (r = sshbuf_put_cstring(b, alg)) != 0 ||
1367: (r = sshkey_puts(id->key, b)) != 0) {
1368: fatal("%s: assemble signed data: %s",
1369: __func__, ssh_err(r));
1370: }
1371:
1372: /* generate signature */
1373: r = identity_sign(sign_id, &signature, &slen,
1374: sshbuf_ptr(b), sshbuf_len(b), datafellows, alg);
1375: if (r == 0)
1376: break;
1377: else if (r == SSH_ERR_KEY_NOT_FOUND)
1378: goto out; /* soft failure */
1379: else if (r == SSH_ERR_SIGN_ALG_UNSUPPORTED &&
1380: !fallback_sigtype) {
1381: if (sign_id->agent_fd != -1)
1382: loc = "agent ";
1383: else if ((sign_id->key->flags & SSHKEY_FLAG_EXT) != 0)
1384: loc = "token ";
1385: logit("%skey %s %s returned incorrect signature type",
1386: loc, sshkey_type(id->key), fp);
1387: continue;
1388: }
1.315 djm 1389: error("%s: signing failed for %s \"%s\"%s: %s", __func__,
1390: sshkey_type(sign_id->key), sign_id->filename,
1391: id->agent_fd != -1 ? " from agent" : "", ssh_err(r));
1.272 djm 1392: goto out;
1.17 markus 1393: }
1.272 djm 1394: if (slen == 0 || signature == NULL) /* shouldn't happen */
1395: fatal("%s: no signature", __func__);
1.51 markus 1396:
1.1 markus 1397: /* append signature */
1.272 djm 1398: if ((r = sshbuf_put_string(b, signature, slen)) != 0)
1399: fatal("%s: append signature: %s", __func__, ssh_err(r));
1.1 markus 1400:
1.272 djm 1401: #ifdef DEBUG_PK
1402: sshbuf_dump(b, stderr);
1403: #endif
1.1 markus 1404: /* skip session id and packet type */
1.272 djm 1405: if ((r = sshbuf_consume(b, skip + 1)) != 0)
1406: fatal("%s: consume: %s", __func__, ssh_err(r));
1.1 markus 1407:
1408: /* put remaining data from buffer into packet */
1.272 djm 1409: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1410: (r = sshpkt_putb(ssh, b)) != 0 ||
1411: (r = sshpkt_send(ssh)) != 0)
1412: fatal("%s: enqueue request: %s", __func__, ssh_err(r));
1413:
1414: /* success */
1415: sent = 1;
1.17 markus 1416:
1.272 djm 1417: out:
1418: free(fp);
1419: free(alg);
1420: sshbuf_free(b);
1421: freezero(signature, slen);
1422: return sent;
1.16 markus 1423: }
1424:
1.76 itojun 1425: static int
1.295 djm 1426: send_pubkey_test(struct ssh *ssh, Identity *id)
1.20 markus 1427: {
1.295 djm 1428: Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.272 djm 1429: u_char *blob = NULL;
1430: char *alg = NULL;
1.278 markus 1431: size_t bloblen;
1432: u_int have_sig = 0;
1433: int sent = 0, r;
1.20 markus 1434:
1.272 djm 1435: if ((alg = key_sig_algorithm(ssh, id->key)) == NULL) {
1436: debug("%s: no mutual signature algorithm", __func__);
1437: goto out;
1438: }
1.16 markus 1439:
1.278 markus 1440: if ((r = sshkey_to_blob(id->key, &blob, &bloblen)) != 0) {
1.51 markus 1441: /* we cannot handle this key */
1.272 djm 1442: debug3("%s: cannot handle key", __func__);
1443: goto out;
1.16 markus 1444: }
1.51 markus 1445: /* register callback for USERAUTH_PK_OK message */
1.278 markus 1446: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1.51 markus 1447:
1.278 markus 1448: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1449: (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1450: (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1451: (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1452: (r = sshpkt_put_u8(ssh, have_sig)) != 0 ||
1453: (r = sshpkt_put_cstring(ssh, alg)) != 0 ||
1454: (r = sshpkt_put_string(ssh, blob, bloblen)) != 0 ||
1455: (r = sshpkt_send(ssh)) != 0)
1456: fatal("%s: %s", __func__, ssh_err(r));
1.272 djm 1457: sent = 1;
1.278 markus 1458:
1459: out:
1.272 djm 1460: free(alg);
1.197 djm 1461: free(blob);
1.272 djm 1462: return sent;
1.51 markus 1463: }
1464:
1.259 markus 1465: static struct sshkey *
1.229 jcs 1466: load_identity_file(Identity *id)
1.51 markus 1467: {
1.259 markus 1468: struct sshkey *private = NULL;
1.229 jcs 1469: char prompt[300], *passphrase, *comment;
1.308 dtucker 1470: int r, quit = 0, i;
1.52 markus 1471: struct stat st;
1.16 markus 1472:
1.306 deraadt 1473: if (stat(id->filename, &st) == -1) {
1.229 jcs 1474: (id->userprovided ? logit : debug3)("no such identity: %s: %s",
1475: id->filename, strerror(errno));
1.52 markus 1476: return NULL;
1477: }
1.214 djm 1478: snprintf(prompt, sizeof prompt,
1.229 jcs 1479: "Enter passphrase for key '%.100s': ", id->filename);
1.214 djm 1480: for (i = 0; i <= options.number_of_password_prompts; i++) {
1481: if (i == 0)
1482: passphrase = "";
1483: else {
1.20 markus 1484: passphrase = read_passphrase(prompt, 0);
1.214 djm 1485: if (*passphrase == '\0') {
1.20 markus 1486: debug2("no passphrase given, try next key");
1.214 djm 1487: free(passphrase);
1488: break;
1489: }
1490: }
1.229 jcs 1491: switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
1.308 dtucker 1492: passphrase, &private, &comment))) {
1.214 djm 1493: case 0:
1494: break;
1495: case SSH_ERR_KEY_WRONG_PASSPHRASE:
1496: if (options.batch_mode) {
1497: quit = 1;
1498: break;
1499: }
1.215 djm 1500: if (i != 0)
1501: debug2("bad passphrase given, try again...");
1.214 djm 1502: break;
1503: case SSH_ERR_SYSTEM_ERROR:
1504: if (errno == ENOENT) {
1505: debug2("Load key \"%s\": %s",
1.229 jcs 1506: id->filename, ssh_err(r));
1.51 markus 1507: quit = 1;
1.214 djm 1508: break;
1.20 markus 1509: }
1.214 djm 1510: /* FALLTHROUGH */
1511: default:
1.229 jcs 1512: error("Load key \"%s\": %s", id->filename, ssh_err(r));
1.214 djm 1513: quit = 1;
1514: break;
1515: }
1.311 markus 1516: if (private != NULL && sshkey_is_sk(private) &&
1.309 djm 1517: options.sk_provider == NULL) {
1.319 naddy 1518: debug("key \"%s\" is an authenticator-hosted key, "
1519: "but no provider specified", id->filename);
1.309 djm 1520: sshkey_free(private);
1521: private = NULL;
1522: quit = 1;
1523: }
1.230 djm 1524: if (!quit && private != NULL && id->agent_fd == -1 &&
1.229 jcs 1525: !(id->key && id->isprivate))
1526: maybe_add_key_to_agent(id->filename, private, comment,
1527: passphrase);
1.278 markus 1528: if (i > 0)
1529: freezero(passphrase, strlen(passphrase));
1.232 mmcc 1530: free(comment);
1.214 djm 1531: if (private != NULL || quit)
1532: break;
1.16 markus 1533: }
1.51 markus 1534: return private;
1535: }
1536:
1.272 djm 1537: static int
1538: key_type_allowed_by_config(struct sshkey *key)
1539: {
1540: if (match_pattern_list(sshkey_ssh_name(key),
1541: options.pubkey_key_types, 0) == 1)
1542: return 1;
1543:
1544: /* RSA keys/certs might be allowed by alternate signature types */
1545: switch (key->type) {
1546: case KEY_RSA:
1547: if (match_pattern_list("rsa-sha2-512",
1548: options.pubkey_key_types, 0) == 1)
1549: return 1;
1550: if (match_pattern_list("rsa-sha2-256",
1551: options.pubkey_key_types, 0) == 1)
1552: return 1;
1553: break;
1554: case KEY_RSA_CERT:
1555: if (match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
1556: options.pubkey_key_types, 0) == 1)
1557: return 1;
1558: if (match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
1559: options.pubkey_key_types, 0) == 1)
1560: return 1;
1561: break;
1562: }
1563: return 0;
1564: }
1565:
1566:
1.117 markus 1567: /*
1568: * try keys in the following order:
1.227 djm 1569: * 1. certificates listed in the config file
1570: * 2. other input certificates
1571: * 3. agent keys that are found in the config file
1572: * 4. other agent keys
1573: * 5. keys that are only listed in the config file
1.117 markus 1574: */
1575: static void
1576: pubkey_prepare(Authctxt *authctxt)
1.51 markus 1577: {
1.214 djm 1578: struct identity *id, *id2, *tmp;
1579: struct idlist agent, files, *preferred;
1580: struct sshkey *key;
1.230 djm 1581: int agent_fd = -1, i, r, found;
1.214 djm 1582: size_t j;
1583: struct ssh_identitylist *idlist;
1.287 djm 1584: char *ident;
1.51 markus 1585:
1.117 markus 1586: TAILQ_INIT(&agent); /* keys from the agent */
1587: TAILQ_INIT(&files); /* keys from the config file */
1588: preferred = &authctxt->keys;
1589: TAILQ_INIT(preferred); /* preferred order of keys */
1590:
1.190 djm 1591: /* list of keys stored in the filesystem and PKCS#11 */
1.117 markus 1592: for (i = 0; i < options.num_identity_files; i++) {
1593: key = options.identity_keys[i];
1.309 djm 1594: if (key && key->cert &&
1595: key->cert->type != SSH2_CERT_TYPE_USER) {
1596: debug("%s: ignoring certificate %s: not a user "
1597: "certificate", __func__,
1598: options.identity_files[i]);
1599: continue;
1600: }
1.311 markus 1601: if (key && sshkey_is_sk(key) && options.sk_provider == NULL) {
1.319 naddy 1602: debug("%s: ignoring authenticator-hosted key %s as no "
1.309 djm 1603: "SecurityKeyProvider has been specified",
1604: __func__, options.identity_files[i]);
1.117 markus 1605: continue;
1.309 djm 1606: }
1.117 markus 1607: options.identity_keys[i] = NULL;
1.150 djm 1608: id = xcalloc(1, sizeof(*id));
1.230 djm 1609: id->agent_fd = -1;
1.117 markus 1610: id->key = key;
1611: id->filename = xstrdup(options.identity_files[i]);
1.192 dtucker 1612: id->userprovided = options.identity_file_userprovided[i];
1.117 markus 1613: TAILQ_INSERT_TAIL(&files, id, next);
1.190 djm 1614: }
1.227 djm 1615: /* list of certificates specified by user */
1616: for (i = 0; i < options.num_certificate_files; i++) {
1617: key = options.certificates[i];
1.278 markus 1618: if (!sshkey_is_cert(key) || key->cert == NULL ||
1.309 djm 1619: key->cert->type != SSH2_CERT_TYPE_USER) {
1620: debug("%s: ignoring certificate %s: not a user "
1621: "certificate", __func__,
1622: options.identity_files[i]);
1.227 djm 1623: continue;
1.309 djm 1624: }
1.311 markus 1625: if (key && sshkey_is_sk(key) && options.sk_provider == NULL) {
1.319 naddy 1626: debug("%s: ignoring authenticator-hosted key "
1627: "certificate %s as no "
1.309 djm 1628: "SecurityKeyProvider has been specified",
1629: __func__, options.identity_files[i]);
1630: continue;
1631: }
1.227 djm 1632: id = xcalloc(1, sizeof(*id));
1.230 djm 1633: id->agent_fd = -1;
1.227 djm 1634: id->key = key;
1635: id->filename = xstrdup(options.certificate_files[i]);
1636: id->userprovided = options.certificate_file_userprovided[i];
1637: TAILQ_INSERT_TAIL(preferred, id, next);
1.117 markus 1638: }
1639: /* list of keys supported by the agent */
1.214 djm 1640: if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
1641: if (r != SSH_ERR_AGENT_NOT_PRESENT)
1642: debug("%s: ssh_get_authentication_socket: %s",
1643: __func__, ssh_err(r));
1.258 naddy 1644: } else if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
1.214 djm 1645: if (r != SSH_ERR_AGENT_NO_IDENTITIES)
1646: debug("%s: ssh_fetch_identitylist: %s",
1647: __func__, ssh_err(r));
1.237 markus 1648: close(agent_fd);
1.214 djm 1649: } else {
1650: for (j = 0; j < idlist->nkeys; j++) {
1.117 markus 1651: found = 0;
1652: TAILQ_FOREACH(id, &files, next) {
1.214 djm 1653: /*
1654: * agent keys from the config file are
1655: * preferred
1656: */
1657: if (sshkey_equal(idlist->keys[j], id->key)) {
1.117 markus 1658: TAILQ_REMOVE(&files, id, next);
1659: TAILQ_INSERT_TAIL(preferred, id, next);
1.214 djm 1660: id->agent_fd = agent_fd;
1.117 markus 1661: found = 1;
1662: break;
1663: }
1664: }
1.135 markus 1665: if (!found && !options.identities_only) {
1.150 djm 1666: id = xcalloc(1, sizeof(*id));
1.214 djm 1667: /* XXX "steals" key/comment from idlist */
1668: id->key = idlist->keys[j];
1669: id->filename = idlist->comments[j];
1670: idlist->keys[j] = NULL;
1671: idlist->comments[j] = NULL;
1672: id->agent_fd = agent_fd;
1.117 markus 1673: TAILQ_INSERT_TAIL(&agent, id, next);
1674: }
1675: }
1.214 djm 1676: ssh_free_identitylist(idlist);
1.117 markus 1677: /* append remaining agent keys */
1.324 bket 1678: TAILQ_CONCAT(preferred, &agent, next);
1.214 djm 1679: authctxt->agent_fd = agent_fd;
1.244 djm 1680: }
1681: /* Prefer PKCS11 keys that are explicitly listed */
1682: TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1683: if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
1684: continue;
1685: found = 0;
1686: TAILQ_FOREACH(id2, &files, next) {
1687: if (id2->key == NULL ||
1.321 djm 1688: (id2->key->flags & SSHKEY_FLAG_EXT) != 0)
1.244 djm 1689: continue;
1690: if (sshkey_equal(id->key, id2->key)) {
1691: TAILQ_REMOVE(&files, id, next);
1692: TAILQ_INSERT_TAIL(preferred, id, next);
1693: found = 1;
1694: break;
1695: }
1696: }
1697: /* If IdentitiesOnly set and key not found then don't use it */
1698: if (!found && options.identities_only) {
1699: TAILQ_REMOVE(&files, id, next);
1.278 markus 1700: freezero(id, sizeof(*id));
1.244 djm 1701: }
1.117 markus 1702: }
1703: /* append remaining keys from the config file */
1.324 bket 1704: TAILQ_CONCAT(preferred, &files, next);
1.228 djm 1705: /* finally, filter by PubkeyAcceptedKeyTypes */
1706: TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1.273 djm 1707: if (id->key != NULL && !key_type_allowed_by_config(id->key)) {
1.228 djm 1708: debug("Skipping %s key %s - "
1709: "not in PubkeyAcceptedKeyTypes",
1710: sshkey_ssh_name(id->key), id->filename);
1711: TAILQ_REMOVE(preferred, id, next);
1712: sshkey_free(id->key);
1713: free(id->filename);
1714: memset(id, 0, sizeof(*id));
1715: continue;
1716: }
1.117 markus 1717: }
1.287 djm 1718: /* List the keys we plan on using */
1719: TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1720: ident = format_identity(id);
1721: debug("Will attempt key: %s", ident);
1722: free(ident);
1723: }
1724: debug2("%s: done", __func__);
1.17 markus 1725: }
1726:
1.117 markus 1727: static void
1.303 djm 1728: pubkey_cleanup(struct ssh *ssh)
1.51 markus 1729: {
1.302 djm 1730: Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.117 markus 1731: Identity *id;
1.51 markus 1732:
1.289 djm 1733: if (authctxt->agent_fd != -1) {
1.214 djm 1734: ssh_close_authentication_socket(authctxt->agent_fd);
1.289 djm 1735: authctxt->agent_fd = -1;
1736: }
1.117 markus 1737: for (id = TAILQ_FIRST(&authctxt->keys); id;
1738: id = TAILQ_FIRST(&authctxt->keys)) {
1739: TAILQ_REMOVE(&authctxt->keys, id, next);
1.235 mmcc 1740: sshkey_free(id->key);
1.197 djm 1741: free(id->filename);
1742: free(id);
1.117 markus 1743: }
1.1 markus 1744: }
1745:
1.251 djm 1746: static void
1747: pubkey_reset(Authctxt *authctxt)
1748: {
1749: Identity *id;
1750:
1751: TAILQ_FOREACH(id, &authctxt->keys, next)
1752: id->tried = 0;
1753: }
1754:
1.225 markus 1755: static int
1756: try_identity(Identity *id)
1757: {
1758: if (!id->key)
1759: return (0);
1.279 markus 1760: if (sshkey_type_plain(id->key->type) == KEY_RSA &&
1.225 markus 1761: (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1762: debug("Skipped %s key %s for RSA/MD5 server",
1.279 markus 1763: sshkey_type(id->key), id->filename);
1.225 markus 1764: return (0);
1765: }
1.257 djm 1766: return 1;
1.225 markus 1767: }
1768:
1.302 djm 1769: static int
1.295 djm 1770: userauth_pubkey(struct ssh *ssh)
1.20 markus 1771: {
1.295 djm 1772: Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.117 markus 1773: Identity *id;
1.20 markus 1774: int sent = 0;
1.287 djm 1775: char *ident;
1.20 markus 1776:
1.117 markus 1777: while ((id = TAILQ_FIRST(&authctxt->keys))) {
1778: if (id->tried++)
1779: return (0);
1.127 markus 1780: /* move key to the end of the queue */
1.117 markus 1781: TAILQ_REMOVE(&authctxt->keys, id, next);
1782: TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1783: /*
1784: * send a test message if we have the public key. for
1785: * encrypted keys we cannot do this and have to load the
1786: * private key instead
1787: */
1.200 djm 1788: if (id->key != NULL) {
1.225 markus 1789: if (try_identity(id)) {
1.287 djm 1790: ident = format_identity(id);
1791: debug("Offering public key: %s", ident);
1792: free(ident);
1.295 djm 1793: sent = send_pubkey_test(ssh, id);
1.200 djm 1794: }
1795: } else {
1.117 markus 1796: debug("Trying private key: %s", id->filename);
1.229 jcs 1797: id->key = load_identity_file(id);
1.117 markus 1798: if (id->key != NULL) {
1.225 markus 1799: if (try_identity(id)) {
1800: id->isprivate = 1;
1.295 djm 1801: sent = sign_and_send_pubkey(ssh, id);
1.200 djm 1802: }
1.278 markus 1803: sshkey_free(id->key);
1.117 markus 1804: id->key = NULL;
1.251 djm 1805: id->isprivate = 0;
1.51 markus 1806: }
1807: }
1.117 markus 1808: if (sent)
1809: return (sent);
1.28 markus 1810: }
1.117 markus 1811: return (0);
1.20 markus 1812: }
1813:
1.23 markus 1814: /*
1815: * Send userauth request message specifying keyboard-interactive method.
1816: */
1.302 djm 1817: static int
1.295 djm 1818: userauth_kbdint(struct ssh *ssh)
1.23 markus 1819: {
1.295 djm 1820: Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.278 markus 1821: int r;
1.23 markus 1822:
1.292 djm 1823: if (authctxt->attempt_kbdint++ >= options.number_of_password_prompts)
1.23 markus 1824: return 0;
1.82 markus 1825: /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1.292 djm 1826: if (authctxt->attempt_kbdint > 1 && !authctxt->info_req_seen) {
1.82 markus 1827: debug3("userauth_kbdint: disable: no info_req_seen");
1.278 markus 1828: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1.82 markus 1829: return 0;
1830: }
1.23 markus 1831:
1832: debug2("userauth_kbdint");
1.278 markus 1833: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
1834: (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
1835: (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
1836: (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1837: (r = sshpkt_put_cstring(ssh, "")) != 0 || /* lang */
1838: (r = sshpkt_put_cstring(ssh, options.kbd_interactive_devices ?
1839: options.kbd_interactive_devices : "")) != 0 ||
1840: (r = sshpkt_send(ssh)) != 0)
1841: fatal("%s: %s", __func__, ssh_err(r));
1.23 markus 1842:
1.278 markus 1843: ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1.23 markus 1844: return 1;
1845: }
1846:
1847: /*
1.46 markus 1848: * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1.23 markus 1849: */
1.302 djm 1850: static int
1.261 markus 1851: input_userauth_info_req(int type, u_int32_t seq, struct ssh *ssh)
1.23 markus 1852: {
1.260 markus 1853: Authctxt *authctxt = ssh->authctxt;
1.278 markus 1854: char *name = NULL, *inst = NULL, *lang = NULL, *prompt = NULL;
1855: char *response = NULL;
1856: u_char echo = 0;
1.32 markus 1857: u_int num_prompts, i;
1.278 markus 1858: int r;
1.23 markus 1859:
1860: debug2("input_userauth_info_req");
1861:
1862: if (authctxt == NULL)
1863: fatal("input_userauth_info_req: no authentication context");
1.82 markus 1864:
1865: authctxt->info_req_seen = 1;
1.23 markus 1866:
1.278 markus 1867: if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 ||
1868: (r = sshpkt_get_cstring(ssh, &inst, NULL)) != 0 ||
1869: (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
1870: goto out;
1.23 markus 1871: if (strlen(name) > 0)
1.116 itojun 1872: logit("%s", name);
1.23 markus 1873: if (strlen(inst) > 0)
1.116 itojun 1874: logit("%s", inst);
1.23 markus 1875:
1.278 markus 1876: if ((r = sshpkt_get_u32(ssh, &num_prompts)) != 0)
1877: goto out;
1.23 markus 1878: /*
1879: * Begin to build info response packet based on prompts requested.
1880: * We commit to providing the correct number of responses, so if
1881: * further on we run into a problem that prevents this, we have to
1882: * be sure and clean this up and send a correct error response.
1883: */
1.278 markus 1884: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE)) != 0 ||
1885: (r = sshpkt_put_u32(ssh, num_prompts)) != 0)
1886: goto out;
1.23 markus 1887:
1.73 markus 1888: debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1.23 markus 1889: for (i = 0; i < num_prompts; i++) {
1.278 markus 1890: if ((r = sshpkt_get_cstring(ssh, &prompt, NULL)) != 0 ||
1891: (r = sshpkt_get_u8(ssh, &echo)) != 0)
1892: goto out;
1.77 markus 1893: response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1.278 markus 1894: if ((r = sshpkt_put_cstring(ssh, response)) != 0)
1895: goto out;
1896: freezero(response, strlen(response));
1.197 djm 1897: free(prompt);
1.278 markus 1898: response = prompt = NULL;
1.23 markus 1899: }
1.278 markus 1900: /* done with parsing incoming message. */
1901: if ((r = sshpkt_get_end(ssh)) != 0 ||
1902: (r = sshpkt_add_padding(ssh, 64)) != 0)
1903: goto out;
1904: r = sshpkt_send(ssh);
1905: out:
1906: if (response)
1907: freezero(response, strlen(response));
1908: free(prompt);
1909: free(name);
1910: free(inst);
1911: free(lang);
1912: return r;
1.68 markus 1913: }
1914:
1.100 markus 1915: static int
1.294 djm 1916: ssh_keysign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
1.223 djm 1917: const u_char *data, size_t datalen)
1.100 markus 1918: {
1.223 djm 1919: struct sshbuf *b;
1.101 markus 1920: struct stat st;
1.100 markus 1921: pid_t pid;
1.304 deraadt 1922: int r, to[2], from[2], status;
1.295 djm 1923: int sock = ssh_packet_get_connection_in(ssh);
1.223 djm 1924: u_char rversion = 0, version = 2;
1925: void (*osigchld)(int);
1.100 markus 1926:
1.223 djm 1927: *sigp = NULL;
1928: *lenp = 0;
1.100 markus 1929:
1.306 deraadt 1930: if (stat(_PATH_SSH_KEY_SIGN, &st) == -1) {
1.223 djm 1931: error("%s: not installed: %s", __func__, strerror(errno));
1932: return -1;
1933: }
1934: if (fflush(stdout) != 0) {
1935: error("%s: fflush: %s", __func__, strerror(errno));
1.101 markus 1936: return -1;
1937: }
1.306 deraadt 1938: if (pipe(to) == -1) {
1.223 djm 1939: error("%s: pipe: %s", __func__, strerror(errno));
1.100 markus 1940: return -1;
1941: }
1.306 deraadt 1942: if (pipe(from) == -1) {
1.223 djm 1943: error("%s: pipe: %s", __func__, strerror(errno));
1.100 markus 1944: return -1;
1945: }
1.306 deraadt 1946: if ((pid = fork()) == -1) {
1.223 djm 1947: error("%s: fork: %s", __func__, strerror(errno));
1.100 markus 1948: return -1;
1949: }
1.317 dtucker 1950: osigchld = ssh_signal(SIGCHLD, SIG_DFL);
1.100 markus 1951: if (pid == 0) {
1952: close(from[0]);
1.306 deraadt 1953: if (dup2(from[1], STDOUT_FILENO) == -1)
1.223 djm 1954: fatal("%s: dup2: %s", __func__, strerror(errno));
1.100 markus 1955: close(to[1]);
1.306 deraadt 1956: if (dup2(to[0], STDIN_FILENO) == -1)
1.223 djm 1957: fatal("%s: dup2: %s", __func__, strerror(errno));
1.103 markus 1958: close(from[1]);
1959: close(to[0]);
1.304 deraadt 1960:
1.306 deraadt 1961: if (dup2(sock, STDERR_FILENO + 1) == -1)
1.304 deraadt 1962: fatal("%s: dup2: %s", __func__, strerror(errno));
1963: sock = STDERR_FILENO + 1;
1964: fcntl(sock, F_SETFD, 0); /* keep the socket on exec */
1.223 djm 1965: closefrom(sock + 1);
1.304 deraadt 1966:
1.223 djm 1967: debug3("%s: [child] pid=%ld, exec %s",
1968: __func__, (long)getpid(), _PATH_SSH_KEY_SIGN);
1.233 mmcc 1969: execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
1.223 djm 1970: fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN,
1.100 markus 1971: strerror(errno));
1972: }
1973: close(from[1]);
1974: close(to[0]);
1.305 djm 1975: sock = STDERR_FILENO + 1;
1.100 markus 1976:
1.223 djm 1977: if ((b = sshbuf_new()) == NULL)
1978: fatal("%s: sshbuf_new failed", __func__);
1979: /* send # of sock, data to be signed */
1.253 djm 1980: if ((r = sshbuf_put_u32(b, sock)) != 0 ||
1.223 djm 1981: (r = sshbuf_put_string(b, data, datalen)) != 0)
1982: fatal("%s: buffer error: %s", __func__, ssh_err(r));
1983: if (ssh_msg_send(to[1], version, b) == -1)
1984: fatal("%s: couldn't send request", __func__);
1985: sshbuf_reset(b);
1986: r = ssh_msg_recv(from[0], b);
1.101 markus 1987: close(from[0]);
1988: close(to[1]);
1.223 djm 1989: if (r < 0) {
1990: error("%s: no reply", __func__);
1991: goto fail;
1992: }
1.101 markus 1993:
1.223 djm 1994: errno = 0;
1.306 deraadt 1995: while (waitpid(pid, &status, 0) == -1) {
1.223 djm 1996: if (errno != EINTR) {
1997: error("%s: waitpid %ld: %s",
1998: __func__, (long)pid, strerror(errno));
1999: goto fail;
2000: }
2001: }
2002: if (!WIFEXITED(status)) {
2003: error("%s: exited abnormally", __func__);
2004: goto fail;
2005: }
2006: if (WEXITSTATUS(status) != 0) {
2007: error("%s: exited with status %d",
2008: __func__, WEXITSTATUS(status));
2009: goto fail;
2010: }
2011: if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
2012: error("%s: buffer error: %s", __func__, ssh_err(r));
2013: goto fail;
2014: }
2015: if (rversion != version) {
2016: error("%s: bad version", __func__);
2017: goto fail;
2018: }
2019: if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
2020: error("%s: buffer error: %s", __func__, ssh_err(r));
2021: fail:
1.317 dtucker 2022: ssh_signal(SIGCHLD, osigchld);
1.223 djm 2023: sshbuf_free(b);
1.100 markus 2024: return -1;
2025: }
1.317 dtucker 2026: ssh_signal(SIGCHLD, osigchld);
1.223 djm 2027: sshbuf_free(b);
1.100 markus 2028:
2029: return 0;
2030: }
2031:
1.302 djm 2032: static int
1.295 djm 2033: userauth_hostbased(struct ssh *ssh)
1.68 markus 2034: {
1.295 djm 2035: Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.223 djm 2036: struct sshkey *private = NULL;
2037: struct sshbuf *b = NULL;
2038: u_char *sig = NULL, *keyblob = NULL;
2039: char *fp = NULL, *chost = NULL, *lname = NULL;
2040: size_t siglen = 0, keylen = 0;
2041: int i, r, success = 0;
2042:
2043: if (authctxt->ktypes == NULL) {
2044: authctxt->oktypes = xstrdup(options.hostbased_key_types);
2045: authctxt->ktypes = authctxt->oktypes;
2046: }
1.213 djm 2047:
1.223 djm 2048: /*
2049: * Work through each listed type pattern in HostbasedKeyTypes,
2050: * trying each hostkey that matches the type in turn.
2051: */
2052: for (;;) {
2053: if (authctxt->active_ktype == NULL)
2054: authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
2055: if (authctxt->active_ktype == NULL ||
2056: *authctxt->active_ktype == '\0')
2057: break;
2058: debug3("%s: trying key type %s", __func__,
2059: authctxt->active_ktype);
1.68 markus 2060:
1.223 djm 2061: /* check for a useful key */
2062: private = NULL;
2063: for (i = 0; i < authctxt->sensitive->nkeys; i++) {
2064: if (authctxt->sensitive->keys[i] == NULL ||
2065: authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
2066: continue;
2067: if (match_pattern_list(
2068: sshkey_ssh_name(authctxt->sensitive->keys[i]),
1.224 djm 2069: authctxt->active_ktype, 0) != 1)
1.223 djm 2070: continue;
1.68 markus 2071: /* we take and free the key */
1.223 djm 2072: private = authctxt->sensitive->keys[i];
2073: authctxt->sensitive->keys[i] = NULL;
1.68 markus 2074: break;
2075: }
1.223 djm 2076: /* Found one */
2077: if (private != NULL)
2078: break;
2079: /* No more keys of this type; advance */
2080: authctxt->active_ktype = NULL;
1.68 markus 2081: }
1.223 djm 2082: if (private == NULL) {
2083: free(authctxt->oktypes);
2084: authctxt->oktypes = authctxt->ktypes = NULL;
2085: authctxt->active_ktype = NULL;
1.109 markus 2086: debug("No more client hostkeys for hostbased authentication.");
1.223 djm 2087: goto out;
1.68 markus 2088: }
1.211 djm 2089:
1.223 djm 2090: if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
2091: SSH_FP_DEFAULT)) == NULL) {
2092: error("%s: sshkey_fingerprint failed", __func__);
2093: goto out;
1.68 markus 2094: }
1.223 djm 2095: debug("%s: trying hostkey %s %s",
2096: __func__, sshkey_ssh_name(private), fp);
1.211 djm 2097:
1.84 markus 2098: /* figure out a name for the client host */
1.295 djm 2099: lname = get_local_name(ssh_packet_get_connection_in(ssh));
2100: if (lname == NULL) {
1.223 djm 2101: error("%s: cannot get local ipaddr/name", __func__);
2102: goto out;
1.84 markus 2103: }
1.223 djm 2104:
2105: /* XXX sshbuf_put_stringf? */
2106: xasprintf(&chost, "%s.", lname);
2107: debug2("%s: chost %s", __func__, chost);
1.84 markus 2108:
1.68 markus 2109: /* construct data */
1.223 djm 2110: if ((b = sshbuf_new()) == NULL) {
2111: error("%s: sshbuf_new failed", __func__);
2112: goto out;
2113: }
2114: if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
2115: error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
2116: goto out;
2117: }
2118: if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
2119: (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2120: (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1.267 djm 2121: (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1.223 djm 2122: (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1.279 markus 2123: (r = sshbuf_put_cstring(b, sshkey_ssh_name(private))) != 0 ||
1.223 djm 2124: (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
2125: (r = sshbuf_put_cstring(b, chost)) != 0 ||
2126: (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
2127: error("%s: buffer error: %s", __func__, ssh_err(r));
2128: goto out;
2129: }
2130:
1.68 markus 2131: #ifdef DEBUG_PK
1.223 djm 2132: sshbuf_dump(b, stderr);
1.68 markus 2133: #endif
1.294 djm 2134: if ((r = ssh_keysign(ssh, private, &sig, &siglen,
2135: sshbuf_ptr(b), sshbuf_len(b))) != 0) {
1.223 djm 2136: error("sign using hostkey %s %s failed",
2137: sshkey_ssh_name(private), fp);
2138: goto out;
2139: }
2140: if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
2141: (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
2142: (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
2143: (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1.279 markus 2144: (r = sshpkt_put_cstring(ssh, sshkey_ssh_name(private))) != 0 ||
1.223 djm 2145: (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
2146: (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
2147: (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
2148: (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
2149: (r = sshpkt_send(ssh)) != 0) {
2150: error("%s: packet error: %s", __func__, ssh_err(r));
2151: goto out;
2152: }
2153: success = 1;
2154:
2155: out:
1.278 markus 2156: if (sig != NULL)
2157: freezero(sig, siglen);
1.223 djm 2158: free(keyblob);
2159: free(lname);
2160: free(fp);
1.197 djm 2161: free(chost);
1.223 djm 2162: sshkey_free(private);
2163: sshbuf_free(b);
1.68 markus 2164:
1.223 djm 2165: return success;
1.23 markus 2166: }
1.170 djm 2167:
1.20 markus 2168: /* find auth method */
2169:
2170: /*
2171: * given auth method name, if configurable options permit this method fill
2172: * in auth_ident field and return true, otherwise return false.
2173: */
1.76 itojun 2174: static int
1.20 markus 2175: authmethod_is_enabled(Authmethod *method)
2176: {
2177: if (method == NULL)
2178: return 0;
2179: /* return false if options indicate this method is disabled */
2180: if (method->enabled == NULL || *method->enabled == 0)
2181: return 0;
2182: /* return false if batch mode is enabled but method needs interactive mode */
2183: if (method->batch_flag != NULL && *method->batch_flag != 0)
2184: return 0;
2185: return 1;
2186: }
2187:
1.76 itojun 2188: static Authmethod *
1.20 markus 2189: authmethod_lookup(const char *name)
1.1 markus 2190: {
1.20 markus 2191: Authmethod *method = NULL;
2192: if (name != NULL)
2193: for (method = authmethods; method->name != NULL; method++)
2194: if (strcmp(name, method->name) == 0)
2195: return method;
2196: debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
2197: return NULL;
2198: }
1.1 markus 2199:
1.53 markus 2200: /* XXX internal state */
2201: static Authmethod *current = NULL;
2202: static char *supported = NULL;
2203: static char *preferred = NULL;
1.105 deraadt 2204:
1.20 markus 2205: /*
2206: * Given the authentication method list sent by the server, return the
2207: * next method we should try. If the server initially sends a nil list,
1.67 markus 2208: * use a built-in default list.
1.41 stevesk 2209: */
1.76 itojun 2210: static Authmethod *
1.20 markus 2211: authmethod_get(char *authlist)
2212: {
1.53 markus 2213: char *name = NULL;
1.97 markus 2214: u_int next;
1.41 stevesk 2215:
1.20 markus 2216: /* Use a suitable default if we're passed a nil list. */
2217: if (authlist == NULL || strlen(authlist) == 0)
1.53 markus 2218: authlist = options.preferred_authentications;
1.20 markus 2219:
1.53 markus 2220: if (supported == NULL || strcmp(authlist, supported) != 0) {
2221: debug3("start over, passed a different list %s", authlist);
1.197 djm 2222: free(supported);
1.53 markus 2223: supported = xstrdup(authlist);
2224: preferred = options.preferred_authentications;
2225: debug3("preferred %s", preferred);
2226: current = NULL;
2227: } else if (current != NULL && authmethod_is_enabled(current))
2228: return current;
1.20 markus 2229:
1.53 markus 2230: for (;;) {
2231: if ((name = match_list(preferred, supported, &next)) == NULL) {
1.109 markus 2232: debug("No more authentication methods to try.");
1.53 markus 2233: current = NULL;
2234: return NULL;
2235: }
2236: preferred += next;
1.23 markus 2237: debug3("authmethod_lookup %s", name);
1.53 markus 2238: debug3("remaining preferred: %s", preferred);
2239: if ((current = authmethod_lookup(name)) != NULL &&
2240: authmethod_is_enabled(current)) {
1.23 markus 2241: debug3("authmethod_is_enabled %s", name);
1.109 markus 2242: debug("Next authentication method: %s", name);
1.197 djm 2243: free(name);
1.53 markus 2244: return current;
1.23 markus 2245: }
1.198 dtucker 2246: free(name);
1.1 markus 2247: }
1.53 markus 2248: }
1.1 markus 2249:
1.79 stevesk 2250: static char *
1.53 markus 2251: authmethods_get(void)
2252: {
2253: Authmethod *method = NULL;
1.278 markus 2254: struct sshbuf *b;
1.93 markus 2255: char *list;
1.278 markus 2256: int r;
1.27 provos 2257:
1.278 markus 2258: if ((b = sshbuf_new()) == NULL)
2259: fatal("%s: sshbuf_new failed", __func__);
1.53 markus 2260: for (method = authmethods; method->name != NULL; method++) {
2261: if (authmethod_is_enabled(method)) {
1.278 markus 2262: if ((r = sshbuf_putf(b, "%s%s",
2263: sshbuf_len(b) ? "," : "", method->name)) != 0)
2264: fatal("%s: buffer error: %s",
2265: __func__, ssh_err(r));
1.53 markus 2266: }
2267: }
1.278 markus 2268: if ((list = sshbuf_dup_string(b)) == NULL)
1.242 djm 2269: fatal("%s: sshbuf_dup_string failed", __func__);
1.278 markus 2270: sshbuf_free(b);
1.93 markus 2271: return list;
1.1 markus 2272: }