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