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