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