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