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