Annotation of src/usr.bin/ssh/sshconnect2.c, Revision 1.200
1.200 ! djm 1: /* $OpenBSD: sshconnect2.c,v 1.199 2013/11/02 21:59:15 markus 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"
58: #include "readconf.h"
1.137 djm 59: #include "misc.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.171 djm 67: #include "schnorr.h"
1.170 djm 68: #include "jpake.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.63 markus 89: Kex *xxx_kex = NULL;
90:
1.76 itojun 91: static int
1.75 markus 92: verify_host_key_callback(Key *hostkey)
1.61 markus 93: {
1.75 markus 94: if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
1.83 markus 95: fatal("Host key verification failed.");
1.61 markus 96: return 0;
97: }
98:
1.186 djm 99: static char *
100: order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
101: {
102: char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
103: size_t maxlen;
104: struct hostkeys *hostkeys;
105: int ktype;
1.188 djm 106: u_int i;
1.186 djm 107:
108: /* Find all hostkeys for this hostname */
109: get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
110: hostkeys = init_hostkeys();
1.188 djm 111: for (i = 0; i < options.num_user_hostfiles; i++)
112: load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
113: for (i = 0; i < options.num_system_hostfiles; i++)
114: load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
1.186 djm 115:
116: oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
117: maxlen = strlen(avail) + 1;
118: first = xmalloc(maxlen);
119: last = xmalloc(maxlen);
120: *first = *last = '\0';
121:
122: #define ALG_APPEND(to, from) \
123: do { \
124: if (*to != '\0') \
125: strlcat(to, ",", maxlen); \
126: strlcat(to, from, maxlen); \
127: } while (0)
128:
129: while ((alg = strsep(&avail, ",")) && *alg != '\0') {
130: if ((ktype = key_type_from_name(alg)) == KEY_UNSPEC)
131: fatal("%s: unknown alg %s", __func__, alg);
132: if (lookup_key_in_hostkeys_by_type(hostkeys,
133: key_type_plain(ktype), NULL))
134: ALG_APPEND(first, alg);
135: else
136: ALG_APPEND(last, alg);
137: }
138: #undef ALG_APPEND
139: xasprintf(&ret, "%s%s%s", first, *first == '\0' ? "" : ",", last);
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.186 djm 153: ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
1.22 provos 154: {
155: Kex *kex;
1.61 markus 156:
157: xxx_host = host;
158: xxx_hostaddr = hostaddr;
1.22 provos 159:
1.29 markus 160: if (options.ciphers == (char *)-1) {
1.116 itojun 161: logit("No valid ciphers for protocol version 2 given, using defaults.");
1.29 markus 162: options.ciphers = NULL;
1.24 markus 163: }
1.22 provos 164: if (options.ciphers != NULL) {
165: myproposal[PROPOSAL_ENC_ALGS_CTOS] =
166: myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
167: }
1.60 stevesk 168: myproposal[PROPOSAL_ENC_ALGS_CTOS] =
169: compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
170: myproposal[PROPOSAL_ENC_ALGS_STOC] =
171: compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
1.22 provos 172: if (options.compression) {
1.47 markus 173: myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1.141 markus 174: myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
1.22 provos 175: } else {
1.47 markus 176: myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1.141 markus 177: myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
1.47 markus 178: }
179: if (options.macs != NULL) {
180: myproposal[PROPOSAL_MAC_ALGS_CTOS] =
181: myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
1.22 provos 182: }
1.70 markus 183: if (options.hostkeyalgorithms != NULL)
1.88 deraadt 184: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
1.200 ! djm 185: compat_pkalg_proposal(options.hostkeyalgorithms);
1.186 djm 186: else {
187: /* Prefer algorithms that we already have keys for */
188: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
1.200 ! djm 189: compat_pkalg_proposal(
! 190: order_hostkeyalgs(host, hostaddr, port));
1.186 djm 191: }
1.185 djm 192: if (options.kex_algorithms != NULL)
193: myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
1.115 markus 194:
1.196 dtucker 195: if (options.rekey_limit || options.rekey_interval)
196: packet_set_rekey_limits((u_int32_t)options.rekey_limit,
197: (time_t)options.rekey_interval);
1.22 provos 198:
1.65 markus 199: /* start key exchange */
1.64 markus 200: kex = kex_setup(myproposal);
1.111 markus 201: kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
1.138 djm 202: kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
1.111 markus 203: kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
1.147 djm 204: kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
1.184 djm 205: kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
1.199 markus 206: kex->kex[KEX_C25519_SHA256] = kexc25519_client;
1.61 markus 207: kex->client_version_string=client_version_string;
208: kex->server_version_string=server_version_string;
1.75 markus 209: kex->verify_host_key=&verify_host_key_callback;
1.63 markus 210:
211: xxx_kex = kex;
1.22 provos 212:
1.66 markus 213: dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
1.173 andreas 214:
215: if (options.use_roaming && !kex->roaming) {
216: debug("Roaming not allowed by server");
217: options.use_roaming = 0;
218: }
1.62 markus 219:
220: session_id2 = kex->session_id;
221: session_id2_len = kex->session_id_len;
1.22 provos 222:
223: #ifdef DEBUG_KEXDH
224: /* send 1st encrypted/maced/compressed message */
225: packet_start(SSH2_MSG_IGNORE);
226: packet_put_cstring("markus");
227: packet_send();
228: packet_write_wait();
229: #endif
1.1 markus 230: }
1.11 markus 231:
1.1 markus 232: /*
233: * Authenticate user
234: */
1.20 markus 235:
236: typedef struct Authctxt Authctxt;
237: typedef struct Authmethod Authmethod;
1.117 markus 238: typedef struct identity Identity;
239: typedef struct idlist Idlist;
1.20 markus 240:
1.117 markus 241: struct identity {
242: TAILQ_ENTRY(identity) next;
243: AuthenticationConnection *ac; /* set if agent supports key */
244: Key *key; /* public/private key */
245: char *filename; /* comment for agent-only keys */
246: int tried;
247: int isprivate; /* key points to the private key */
1.191 dtucker 248: int userprovided;
1.117 markus 249: };
250: TAILQ_HEAD(idlist, identity);
1.20 markus 251:
252: struct Authctxt {
253: const char *server_user;
1.68 markus 254: const char *local_user;
1.20 markus 255: const char *host;
256: const char *service;
1.23 markus 257: Authmethod *method;
1.183 djm 258: sig_atomic_t success;
1.51 markus 259: char *authlist;
1.68 markus 260: /* pubkey */
1.117 markus 261: Idlist keys;
1.68 markus 262: AuthenticationConnection *agent;
263: /* hostbased */
1.100 markus 264: Sensitive *sensitive;
1.82 markus 265: /* kbd-interactive */
266: int info_req_seen;
1.121 markus 267: /* generic */
268: void *methoddata;
1.20 markus 269: };
270: struct Authmethod {
271: char *name; /* string to compare against server's list */
272: int (*userauth)(Authctxt *authctxt);
1.170 djm 273: void (*cleanup)(Authctxt *authctxt);
1.20 markus 274: int *enabled; /* flag in option struct that enables method */
275: int *batch_flag; /* flag in option struct that disables method */
276: };
277:
1.92 markus 278: void input_userauth_success(int, u_int32_t, void *);
1.172 djm 279: void input_userauth_success_unexpected(int, u_int32_t, void *);
1.92 markus 280: void input_userauth_failure(int, u_int32_t, void *);
281: void input_userauth_banner(int, u_int32_t, void *);
282: void input_userauth_error(int, u_int32_t, void *);
283: void input_userauth_info_req(int, u_int32_t, void *);
284: void input_userauth_pk_ok(int, u_int32_t, void *);
1.99 markus 285: void input_userauth_passwd_changereq(int, u_int32_t, void *);
1.170 djm 286: void input_userauth_jpake_server_step1(int, u_int32_t, void *);
287: void input_userauth_jpake_server_step2(int, u_int32_t, void *);
288: void input_userauth_jpake_server_confirm(int, u_int32_t, void *);
1.86 itojun 289:
290: int userauth_none(Authctxt *);
291: int userauth_pubkey(Authctxt *);
292: int userauth_passwd(Authctxt *);
293: int userauth_kbdint(Authctxt *);
294: int userauth_hostbased(Authctxt *);
1.170 djm 295: int userauth_jpake(Authctxt *);
296:
297: void userauth_jpake_cleanup(Authctxt *);
1.20 markus 298:
1.121 markus 299: #ifdef GSSAPI
300: int userauth_gssapi(Authctxt *authctxt);
301: void input_gssapi_response(int type, u_int32_t, void *);
302: void input_gssapi_token(int type, u_int32_t, void *);
303: void input_gssapi_hash(int type, u_int32_t, void *);
304: void input_gssapi_error(int, u_int32_t, void *);
305: void input_gssapi_errtok(int, u_int32_t, void *);
306: #endif
307:
1.86 itojun 308: void userauth(Authctxt *, char *);
1.51 markus 309:
1.117 markus 310: static int sign_and_send_pubkey(Authctxt *, Identity *);
311: static void pubkey_prepare(Authctxt *);
312: static void pubkey_cleanup(Authctxt *);
1.191 dtucker 313: static Key *load_identity_file(char *, int);
1.76 itojun 314:
315: static Authmethod *authmethod_get(char *authlist);
316: static Authmethod *authmethod_lookup(const char *name);
317: static char *authmethods_get(void);
1.20 markus 318:
319: Authmethod authmethods[] = {
1.121 markus 320: #ifdef GSSAPI
1.132 markus 321: {"gssapi-with-mic",
1.121 markus 322: userauth_gssapi,
1.170 djm 323: NULL,
1.121 markus 324: &options.gss_authentication,
325: NULL},
326: #endif
1.81 markus 327: {"hostbased",
328: userauth_hostbased,
1.170 djm 329: NULL,
1.81 markus 330: &options.hostbased_authentication,
331: NULL},
1.20 markus 332: {"publickey",
333: userauth_pubkey,
1.170 djm 334: NULL,
1.28 markus 335: &options.pubkey_authentication,
1.20 markus 336: NULL},
1.170 djm 337: #ifdef JPAKE
338: {"jpake-01@openssh.com",
339: userauth_jpake,
340: userauth_jpake_cleanup,
341: &options.zero_knowledge_password_authentication,
342: &options.batch_mode},
343: #endif
1.81 markus 344: {"keyboard-interactive",
345: userauth_kbdint,
1.170 djm 346: NULL,
1.81 markus 347: &options.kbd_interactive_authentication,
348: &options.batch_mode},
1.20 markus 349: {"password",
350: userauth_passwd,
1.170 djm 351: NULL,
1.20 markus 352: &options.password_authentication,
1.23 markus 353: &options.batch_mode},
354: {"none",
355: userauth_none,
356: NULL,
1.170 djm 357: NULL,
1.23 markus 358: NULL},
1.170 djm 359: {NULL, NULL, NULL, NULL, NULL}
1.20 markus 360: };
361:
362: void
1.68 markus 363: ssh_userauth2(const char *local_user, const char *server_user, char *host,
1.100 markus 364: Sensitive *sensitive)
1.20 markus 365: {
366: Authctxt authctxt;
367: int type;
1.39 markus 368:
1.73 markus 369: if (options.challenge_response_authentication)
1.39 markus 370: options.kbd_interactive_authentication = 1;
1.20 markus 371:
372: packet_start(SSH2_MSG_SERVICE_REQUEST);
373: packet_put_cstring("ssh-userauth");
374: packet_send();
1.108 markus 375: debug("SSH2_MSG_SERVICE_REQUEST sent");
1.20 markus 376: packet_write_wait();
1.91 markus 377: type = packet_read();
1.108 markus 378: if (type != SSH2_MSG_SERVICE_ACCEPT)
379: fatal("Server denied authentication request: %d", type);
1.20 markus 380: if (packet_remaining() > 0) {
1.91 markus 381: char *reply = packet_get_string(NULL);
1.108 markus 382: debug2("service_accept: %s", reply);
1.197 djm 383: free(reply);
1.20 markus 384: } else {
1.109 markus 385: debug2("buggy server: service_accept w/o service");
1.20 markus 386: }
1.90 markus 387: packet_check_eom();
1.108 markus 388: debug("SSH2_MSG_SERVICE_ACCEPT received");
1.20 markus 389:
1.53 markus 390: if (options.preferred_authentications == NULL)
391: options.preferred_authentications = authmethods_get();
392:
1.20 markus 393: /* setup authentication context */
1.82 markus 394: memset(&authctxt, 0, sizeof(authctxt));
1.117 markus 395: pubkey_prepare(&authctxt);
1.20 markus 396: authctxt.server_user = server_user;
1.68 markus 397: authctxt.local_user = local_user;
1.20 markus 398: authctxt.host = host;
399: authctxt.service = "ssh-connection"; /* service name */
400: authctxt.success = 0;
1.23 markus 401: authctxt.method = authmethod_lookup("none");
1.51 markus 402: authctxt.authlist = NULL;
1.121 markus 403: authctxt.methoddata = NULL;
1.100 markus 404: authctxt.sensitive = sensitive;
1.82 markus 405: authctxt.info_req_seen = 0;
1.23 markus 406: if (authctxt.method == NULL)
407: fatal("ssh_userauth2: internal error: cannot send userauth none request");
1.20 markus 408:
409: /* initial userauth request */
1.23 markus 410: userauth_none(&authctxt);
1.20 markus 411:
1.65 markus 412: dispatch_init(&input_userauth_error);
1.20 markus 413: dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
414: dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
1.35 markus 415: dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
1.20 markus 416: dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
417:
1.117 markus 418: pubkey_cleanup(&authctxt);
1.119 markus 419: dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
420:
1.109 markus 421: debug("Authentication succeeded (%s).", authctxt.method->name);
1.20 markus 422: }
1.119 markus 423:
1.20 markus 424: void
1.51 markus 425: userauth(Authctxt *authctxt, char *authlist)
426: {
1.170 djm 427: if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
428: authctxt->method->cleanup(authctxt);
429:
1.197 djm 430: free(authctxt->methoddata);
431: authctxt->methoddata = NULL;
1.51 markus 432: if (authlist == NULL) {
433: authlist = authctxt->authlist;
434: } else {
1.197 djm 435: free(authctxt->authlist);
1.51 markus 436: authctxt->authlist = authlist;
437: }
438: for (;;) {
439: Authmethod *method = authmethod_get(authlist);
440: if (method == NULL)
441: fatal("Permission denied (%s).", authlist);
442: authctxt->method = method;
1.119 markus 443:
444: /* reset the per method handler */
445: dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
446: SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
447:
448: /* and try new method */
1.51 markus 449: if (method->userauth(authctxt) != 0) {
450: debug2("we sent a %s packet, wait for reply", method->name);
451: break;
452: } else {
453: debug2("we did not send a packet, disable method");
454: method->enabled = NULL;
455: }
456: }
457: }
1.105 deraadt 458:
1.169 djm 459: /* ARGSUSED */
1.51 markus 460: void
1.92 markus 461: input_userauth_error(int type, u_int32_t seq, void *ctxt)
1.20 markus 462: {
1.35 markus 463: fatal("input_userauth_error: bad message during authentication: "
1.140 djm 464: "type %d", type);
1.35 markus 465: }
1.105 deraadt 466:
1.169 djm 467: /* ARGSUSED */
1.35 markus 468: void
1.92 markus 469: input_userauth_banner(int type, u_int32_t seq, void *ctxt)
1.35 markus 470: {
1.166 djm 471: char *msg, *raw, *lang;
472: u_int len;
1.126 deraadt 473:
1.35 markus 474: debug3("input_userauth_banner");
1.166 djm 475: raw = packet_get_string(&len);
1.35 markus 476: lang = packet_get_string(NULL);
1.167 markus 477: if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
1.166 djm 478: if (len > 65536)
479: len = 65536;
1.168 deraadt 480: msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
1.177 dtucker 481: strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
1.125 dtucker 482: fprintf(stderr, "%s", msg);
1.197 djm 483: free(msg);
1.166 djm 484: }
1.197 djm 485: free(raw);
486: free(lang);
1.20 markus 487: }
1.105 deraadt 488:
1.169 djm 489: /* ARGSUSED */
1.20 markus 490: void
1.92 markus 491: input_userauth_success(int type, u_int32_t seq, void *ctxt)
1.20 markus 492: {
493: Authctxt *authctxt = ctxt;
1.172 djm 494:
1.20 markus 495: if (authctxt == NULL)
496: fatal("input_userauth_success: no authentication context");
1.197 djm 497: free(authctxt->authlist);
498: authctxt->authlist = NULL;
1.172 djm 499: if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
500: authctxt->method->cleanup(authctxt);
1.197 djm 501: free(authctxt->methoddata);
502: authctxt->methoddata = NULL;
1.20 markus 503: authctxt->success = 1; /* break out */
504: }
1.105 deraadt 505:
1.172 djm 506: void
507: input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
508: {
509: Authctxt *authctxt = ctxt;
510:
511: if (authctxt == NULL)
512: fatal("%s: no authentication context", __func__);
513:
514: fatal("Unexpected authentication success during %s.",
515: authctxt->method->name);
516: }
517:
1.169 djm 518: /* ARGSUSED */
1.20 markus 519: void
1.92 markus 520: input_userauth_failure(int type, u_int32_t seq, void *ctxt)
1.20 markus 521: {
522: Authctxt *authctxt = ctxt;
523: char *authlist = NULL;
524: int partial;
525:
526: if (authctxt == NULL)
527: fatal("input_userauth_failure: no authentication context");
528:
1.23 markus 529: authlist = packet_get_string(NULL);
1.20 markus 530: partial = packet_get_char();
1.90 markus 531: packet_check_eom();
1.20 markus 532:
1.193 markus 533: if (partial != 0) {
1.116 itojun 534: logit("Authenticated with partial success.");
1.193 markus 535: /* reset state */
536: pubkey_cleanup(authctxt);
537: pubkey_prepare(authctxt);
538: }
1.109 markus 539: debug("Authentications that can continue: %s", authlist);
1.20 markus 540:
1.51 markus 541: userauth(authctxt, authlist);
542: }
1.169 djm 543:
544: /* ARGSUSED */
1.51 markus 545: void
1.92 markus 546: input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
1.51 markus 547: {
548: Authctxt *authctxt = ctxt;
549: Key *key = NULL;
1.117 markus 550: Identity *id = NULL;
1.51 markus 551: Buffer b;
1.96 markus 552: int pktype, sent = 0;
553: u_int alen, blen;
554: char *pkalg, *fp;
555: u_char *pkblob;
1.51 markus 556:
557: if (authctxt == NULL)
558: fatal("input_userauth_pk_ok: no authentication context");
559: if (datafellows & SSH_BUG_PKOK) {
560: /* this is similar to SSH_BUG_PKAUTH */
561: debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
562: pkblob = packet_get_string(&blen);
563: buffer_init(&b);
564: buffer_append(&b, pkblob, blen);
565: pkalg = buffer_get_string(&b, &alen);
566: buffer_free(&b);
567: } else {
568: pkalg = packet_get_string(&alen);
569: pkblob = packet_get_string(&blen);
570: }
1.90 markus 571: packet_check_eom();
1.51 markus 572:
1.117 markus 573: debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
1.51 markus 574:
1.117 markus 575: if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
576: debug("unknown pkalg %s", pkalg);
577: goto done;
578: }
579: if ((key = key_from_blob(pkblob, blen)) == NULL) {
580: debug("no key from blob. pkalg %s", pkalg);
581: goto done;
582: }
583: if (key->type != pktype) {
584: error("input_userauth_pk_ok: type mismatch "
585: "for decoded key (received %d, expected %d)",
586: key->type, pktype);
587: goto done;
588: }
589: fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
590: debug2("input_userauth_pk_ok: fp %s", fp);
1.197 djm 591: free(fp);
1.117 markus 592:
1.127 markus 593: /*
594: * search keys in the reverse order, because last candidate has been
595: * moved to the end of the queue. this also avoids confusion by
596: * duplicate keys
597: */
1.136 henning 598: TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
1.117 markus 599: if (key_equal(key, id->key)) {
600: sent = sign_and_send_pubkey(authctxt, id);
1.51 markus 601: break;
602: }
1.117 markus 603: }
604: done:
1.51 markus 605: if (key != NULL)
606: key_free(key);
1.197 djm 607: free(pkalg);
608: free(pkblob);
1.51 markus 609:
1.106 deraadt 610: /* try another method if we did not send a packet */
1.51 markus 611: if (sent == 0)
612: userauth(authctxt, NULL);
1.20 markus 613: }
1.121 markus 614:
615: #ifdef GSSAPI
1.133 djm 616: int
1.121 markus 617: userauth_gssapi(Authctxt *authctxt)
618: {
619: Gssctxt *gssctxt = NULL;
1.128 avsm 620: static gss_OID_set gss_supported = NULL;
1.139 djm 621: static u_int mech = 0;
1.121 markus 622: OM_uint32 min;
623: int ok = 0;
624:
625: /* Try one GSSAPI method at a time, rather than sending them all at
626: * once. */
627:
1.128 avsm 628: if (gss_supported == NULL)
629: gss_indicate_mechs(&min, &gss_supported);
1.121 markus 630:
631: /* Check to see if the mechanism is usable before we offer it */
1.128 avsm 632: while (mech < gss_supported->count && !ok) {
1.121 markus 633: /* My DER encoding requires length<128 */
1.128 avsm 634: if (gss_supported->elements[mech].length < 128 &&
1.161 djm 635: ssh_gssapi_check_mechanism(&gssctxt,
636: &gss_supported->elements[mech], authctxt->host)) {
1.121 markus 637: ok = 1; /* Mechanism works */
638: } else {
639: mech++;
640: }
641: }
642:
1.161 djm 643: if (!ok)
1.139 djm 644: return 0;
1.121 markus 645:
646: authctxt->methoddata=(void *)gssctxt;
647:
648: packet_start(SSH2_MSG_USERAUTH_REQUEST);
649: packet_put_cstring(authctxt->server_user);
650: packet_put_cstring(authctxt->service);
651: packet_put_cstring(authctxt->method->name);
652:
653: packet_put_int(1);
654:
1.129 markus 655: packet_put_int((gss_supported->elements[mech].length) + 2);
656: packet_put_char(SSH_GSS_OIDTYPE);
657: packet_put_char(gss_supported->elements[mech].length);
658: packet_put_raw(gss_supported->elements[mech].elements,
659: gss_supported->elements[mech].length);
1.121 markus 660:
661: packet_send();
662:
663: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
664: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
665: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
666: dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
667:
668: mech++; /* Move along to next candidate */
669:
670: return 1;
671: }
672:
1.130 markus 673: static OM_uint32
674: process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
675: {
676: Authctxt *authctxt = ctxt;
677: Gssctxt *gssctxt = authctxt->methoddata;
678: gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
1.142 djm 679: gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
680: gss_buffer_desc gssbuf;
1.132 markus 681: OM_uint32 status, ms, flags;
682: Buffer b;
1.133 djm 683:
1.130 markus 684: status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
1.132 markus 685: recv_tok, &send_tok, &flags);
1.130 markus 686:
687: if (send_tok.length > 0) {
688: if (GSS_ERROR(status))
689: packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
690: else
691: packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
1.133 djm 692:
1.130 markus 693: packet_put_string(send_tok.value, send_tok.length);
694: packet_send();
695: gss_release_buffer(&ms, &send_tok);
696: }
1.133 djm 697:
1.130 markus 698: if (status == GSS_S_COMPLETE) {
1.132 markus 699: /* send either complete or MIC, depending on mechanism */
700: if (!(flags & GSS_C_INTEG_FLAG)) {
701: packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
702: packet_send();
703: } else {
704: ssh_gssapi_buildmic(&b, authctxt->server_user,
705: authctxt->service, "gssapi-with-mic");
706:
707: gssbuf.value = buffer_ptr(&b);
708: gssbuf.length = buffer_len(&b);
1.133 djm 709:
1.132 markus 710: status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
1.133 djm 711:
1.132 markus 712: if (!GSS_ERROR(status)) {
713: packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
714: packet_put_string(mic.value, mic.length);
1.133 djm 715:
1.132 markus 716: packet_send();
717: }
1.133 djm 718:
1.132 markus 719: buffer_free(&b);
720: gss_release_buffer(&ms, &mic);
1.133 djm 721: }
1.130 markus 722: }
1.133 djm 723:
1.130 markus 724: return status;
725: }
726:
1.169 djm 727: /* ARGSUSED */
1.121 markus 728: void
729: input_gssapi_response(int type, u_int32_t plen, void *ctxt)
730: {
731: Authctxt *authctxt = ctxt;
732: Gssctxt *gssctxt;
733: int oidlen;
734: char *oidv;
735:
736: if (authctxt == NULL)
737: fatal("input_gssapi_response: no authentication context");
738: gssctxt = authctxt->methoddata;
739:
740: /* Setup our OID */
741: oidv = packet_get_string(&oidlen);
742:
1.129 markus 743: if (oidlen <= 2 ||
744: oidv[0] != SSH_GSS_OIDTYPE ||
745: oidv[1] != oidlen - 2) {
1.197 djm 746: free(oidv);
1.129 markus 747: debug("Badly encoded mechanism OID received");
748: userauth(authctxt, NULL);
749: return;
1.121 markus 750: }
1.129 markus 751:
752: if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
753: fatal("Server returned different OID than expected");
1.121 markus 754:
755: packet_check_eom();
756:
1.197 djm 757: free(oidv);
1.121 markus 758:
1.130 markus 759: if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
1.121 markus 760: /* Start again with next method on list */
761: debug("Trying to start again");
762: userauth(authctxt, NULL);
763: return;
764: }
765: }
766:
1.169 djm 767: /* ARGSUSED */
1.121 markus 768: void
769: input_gssapi_token(int type, u_int32_t plen, void *ctxt)
770: {
771: Authctxt *authctxt = ctxt;
772: gss_buffer_desc recv_tok;
1.130 markus 773: OM_uint32 status;
1.121 markus 774: u_int slen;
775:
776: if (authctxt == NULL)
777: fatal("input_gssapi_response: no authentication context");
778:
779: recv_tok.value = packet_get_string(&slen);
780: recv_tok.length = slen; /* safe typecast */
781:
782: packet_check_eom();
783:
1.130 markus 784: status = process_gssapi_token(ctxt, &recv_tok);
1.121 markus 785:
1.197 djm 786: free(recv_tok.value);
1.121 markus 787:
788: if (GSS_ERROR(status)) {
789: /* Start again with the next method in the list */
790: userauth(authctxt, NULL);
791: return;
792: }
793: }
794:
1.169 djm 795: /* ARGSUSED */
1.121 markus 796: void
797: input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
798: {
799: Authctxt *authctxt = ctxt;
800: Gssctxt *gssctxt;
801: gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
802: gss_buffer_desc recv_tok;
1.194 djm 803: OM_uint32 ms;
1.123 deraadt 804: u_int len;
1.121 markus 805:
806: if (authctxt == NULL)
807: fatal("input_gssapi_response: no authentication context");
808: gssctxt = authctxt->methoddata;
809:
1.123 deraadt 810: recv_tok.value = packet_get_string(&len);
811: recv_tok.length = len;
1.121 markus 812:
813: packet_check_eom();
814:
815: /* Stick it into GSSAPI and see what it says */
1.194 djm 816: (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
1.140 djm 817: &recv_tok, &send_tok, NULL);
1.121 markus 818:
1.197 djm 819: free(recv_tok.value);
1.121 markus 820: gss_release_buffer(&ms, &send_tok);
821:
822: /* Server will be returning a failed packet after this one */
823: }
824:
1.169 djm 825: /* ARGSUSED */
1.121 markus 826: void
827: input_gssapi_error(int type, u_int32_t plen, void *ctxt)
828: {
829: char *msg;
830: char *lang;
831:
1.194 djm 832: /* maj */(void)packet_get_int();
833: /* min */(void)packet_get_int();
1.121 markus 834: msg=packet_get_string(NULL);
835: lang=packet_get_string(NULL);
836:
837: packet_check_eom();
838:
1.143 stevesk 839: debug("Server GSSAPI Error:\n%s", msg);
1.197 djm 840: free(msg);
841: free(lang);
1.121 markus 842: }
843: #endif /* GSSAPI */
1.20 markus 844:
1.1 markus 845: int
1.23 markus 846: userauth_none(Authctxt *authctxt)
847: {
848: /* initial userauth request */
849: packet_start(SSH2_MSG_USERAUTH_REQUEST);
850: packet_put_cstring(authctxt->server_user);
851: packet_put_cstring(authctxt->service);
852: packet_put_cstring(authctxt->method->name);
853: packet_send();
854: return 1;
855: }
856:
857: int
1.20 markus 858: userauth_passwd(Authctxt *authctxt)
1.1 markus 859: {
1.6 markus 860: static int attempt = 0;
1.99 markus 861: char prompt[150];
1.1 markus 862: char *password;
1.175 dtucker 863: const char *host = options.host_key_alias ? options.host_key_alias :
864: authctxt->host;
1.6 markus 865:
1.13 todd 866: if (attempt++ >= options.number_of_password_prompts)
1.6 markus 867: return 0;
1.13 todd 868:
1.87 deraadt 869: if (attempt != 1)
1.13 todd 870: error("Permission denied, please try again.");
1.1 markus 871:
1.43 itojun 872: snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
1.175 dtucker 873: authctxt->server_user, host);
1.1 markus 874: password = read_passphrase(prompt, 0);
875: packet_start(SSH2_MSG_USERAUTH_REQUEST);
1.20 markus 876: packet_put_cstring(authctxt->server_user);
877: packet_put_cstring(authctxt->service);
1.23 markus 878: packet_put_cstring(authctxt->method->name);
1.1 markus 879: packet_put_char(0);
1.49 markus 880: packet_put_cstring(password);
1.1 markus 881: memset(password, 0, strlen(password));
1.197 djm 882: free(password);
1.85 markus 883: packet_add_padding(64);
1.1 markus 884: packet_send();
1.99 markus 885:
1.104 deraadt 886: dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99 markus 887: &input_userauth_passwd_changereq);
888:
1.1 markus 889: return 1;
890: }
1.169 djm 891:
1.99 markus 892: /*
893: * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
894: */
1.169 djm 895: /* ARGSUSED */
1.99 markus 896: void
1.153 djm 897: input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
1.99 markus 898: {
899: Authctxt *authctxt = ctxt;
900: char *info, *lang, *password = NULL, *retype = NULL;
901: char prompt[150];
1.175 dtucker 902: const char *host = options.host_key_alias ? options.host_key_alias :
903: authctxt->host;
1.99 markus 904:
905: debug2("input_userauth_passwd_changereq");
906:
907: if (authctxt == NULL)
908: fatal("input_userauth_passwd_changereq: "
909: "no authentication context");
910:
911: info = packet_get_string(NULL);
912: lang = packet_get_string(NULL);
913: if (strlen(info) > 0)
1.116 itojun 914: logit("%s", info);
1.197 djm 915: free(info);
916: free(lang);
1.99 markus 917: packet_start(SSH2_MSG_USERAUTH_REQUEST);
918: packet_put_cstring(authctxt->server_user);
919: packet_put_cstring(authctxt->service);
920: packet_put_cstring(authctxt->method->name);
921: packet_put_char(1); /* additional info */
1.104 deraadt 922: snprintf(prompt, sizeof(prompt),
1.99 markus 923: "Enter %.30s@%.128s's old password: ",
1.175 dtucker 924: authctxt->server_user, host);
1.99 markus 925: password = read_passphrase(prompt, 0);
926: packet_put_cstring(password);
927: memset(password, 0, strlen(password));
1.197 djm 928: free(password);
1.99 markus 929: password = NULL;
930: while (password == NULL) {
1.104 deraadt 931: snprintf(prompt, sizeof(prompt),
1.99 markus 932: "Enter %.30s@%.128s's new password: ",
1.175 dtucker 933: authctxt->server_user, host);
1.99 markus 934: password = read_passphrase(prompt, RP_ALLOW_EOF);
935: if (password == NULL) {
936: /* bail out */
937: return;
938: }
1.104 deraadt 939: snprintf(prompt, sizeof(prompt),
1.99 markus 940: "Retype %.30s@%.128s's new password: ",
1.175 dtucker 941: authctxt->server_user, host);
1.99 markus 942: retype = read_passphrase(prompt, 0);
943: if (strcmp(password, retype) != 0) {
944: memset(password, 0, strlen(password));
1.197 djm 945: free(password);
1.116 itojun 946: logit("Mismatch; try again, EOF to quit.");
1.99 markus 947: password = NULL;
948: }
949: memset(retype, 0, strlen(retype));
1.197 djm 950: free(retype);
1.99 markus 951: }
952: packet_put_cstring(password);
953: memset(password, 0, strlen(password));
1.197 djm 954: free(password);
1.99 markus 955: packet_add_padding(64);
956: packet_send();
1.104 deraadt 957:
958: dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99 markus 959: &input_userauth_passwd_changereq);
1.117 markus 960: }
961:
1.170 djm 962: #ifdef JPAKE
963: static char *
964: pw_encrypt(const char *password, const char *crypt_scheme, const char *salt)
965: {
966: /* OpenBSD crypt(3) handles all of these */
967: if (strcmp(crypt_scheme, "crypt") == 0 ||
968: strcmp(crypt_scheme, "bcrypt") == 0 ||
969: strcmp(crypt_scheme, "md5crypt") == 0 ||
970: strcmp(crypt_scheme, "crypt-extended") == 0)
971: return xstrdup(crypt(password, salt));
972: error("%s: unsupported password encryption scheme \"%.100s\"",
973: __func__, crypt_scheme);
974: return NULL;
975: }
976:
977: static BIGNUM *
978: jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme,
979: const char *salt)
980: {
981: char prompt[256], *password, *crypted;
982: u_char *secret;
983: u_int secret_len;
984: BIGNUM *ret;
985:
986: snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ",
987: authctxt->server_user, authctxt->host);
988: password = read_passphrase(prompt, 0);
989:
990: if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) {
991: logit("Disabling %s authentication", authctxt->method->name);
992: authctxt->method->enabled = NULL;
993: /* Continue with an empty password to fail gracefully */
994: crypted = xstrdup("");
995: }
996:
997: #ifdef JPAKE_DEBUG
998: debug3("%s: salt = %s", __func__, salt);
999: debug3("%s: scheme = %s", __func__, crypt_scheme);
1000: debug3("%s: crypted = %s", __func__, crypted);
1001: #endif
1002:
1003: if (hash_buffer(crypted, strlen(crypted), EVP_sha256(),
1004: &secret, &secret_len) != 0)
1005: fatal("%s: hash_buffer", __func__);
1006:
1007: bzero(password, strlen(password));
1008: bzero(crypted, strlen(crypted));
1.197 djm 1009: free(password);
1010: free(crypted);
1.170 djm 1011:
1012: if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL)
1013: fatal("%s: BN_bin2bn (secret)", __func__);
1014: bzero(secret, secret_len);
1.197 djm 1015: free(secret);
1.170 djm 1016:
1017: return ret;
1018: }
1019:
1020: /* ARGSUSED */
1021: void
1022: input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt)
1023: {
1024: Authctxt *authctxt = ctxt;
1025: struct jpake_ctx *pctx = authctxt->methoddata;
1026: u_char *x3_proof, *x4_proof, *x2_s_proof;
1027: u_int x3_proof_len, x4_proof_len, x2_s_proof_len;
1028: char *crypt_scheme, *salt;
1029:
1030: /* Disable this message */
1031: dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL);
1032:
1033: if ((pctx->g_x3 = BN_new()) == NULL ||
1034: (pctx->g_x4 = BN_new()) == NULL)
1035: fatal("%s: BN_new", __func__);
1036:
1037: /* Fetch step 1 values */
1038: crypt_scheme = packet_get_string(NULL);
1039: salt = packet_get_string(NULL);
1040: pctx->server_id = packet_get_string(&pctx->server_id_len);
1041: packet_get_bignum2(pctx->g_x3);
1042: packet_get_bignum2(pctx->g_x4);
1043: x3_proof = packet_get_string(&x3_proof_len);
1044: x4_proof = packet_get_string(&x4_proof_len);
1045: packet_check_eom();
1046:
1047: JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__));
1048:
1049: /* Obtain password and derive secret */
1050: pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt);
1051: bzero(crypt_scheme, strlen(crypt_scheme));
1052: bzero(salt, strlen(salt));
1.197 djm 1053: free(crypt_scheme);
1054: free(salt);
1.170 djm 1055: JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__));
1056:
1057: /* Calculate step 2 values */
1058: jpake_step2(pctx->grp, pctx->s, pctx->g_x1,
1059: pctx->g_x3, pctx->g_x4, pctx->x2,
1060: pctx->server_id, pctx->server_id_len,
1061: pctx->client_id, pctx->client_id_len,
1062: x3_proof, x3_proof_len,
1063: x4_proof, x4_proof_len,
1064: &pctx->a,
1065: &x2_s_proof, &x2_s_proof_len);
1066:
1067: bzero(x3_proof, x3_proof_len);
1068: bzero(x4_proof, x4_proof_len);
1.197 djm 1069: free(x3_proof);
1070: free(x4_proof);
1.170 djm 1071:
1072: JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__));
1073:
1074: /* Send values for step 2 */
1075: packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2);
1076: packet_put_bignum2(pctx->a);
1077: packet_put_string(x2_s_proof, x2_s_proof_len);
1078: packet_send();
1079:
1080: bzero(x2_s_proof, x2_s_proof_len);
1.197 djm 1081: free(x2_s_proof);
1.170 djm 1082:
1083: /* Expect step 2 packet from peer */
1084: dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2,
1085: input_userauth_jpake_server_step2);
1086: }
1087:
1088: /* ARGSUSED */
1089: void
1090: input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt)
1091: {
1092: Authctxt *authctxt = ctxt;
1093: struct jpake_ctx *pctx = authctxt->methoddata;
1094: u_char *x4_s_proof;
1095: u_int x4_s_proof_len;
1096:
1097: /* Disable this message */
1098: dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL);
1099:
1100: if ((pctx->b = BN_new()) == NULL)
1101: fatal("%s: BN_new", __func__);
1102:
1103: /* Fetch step 2 values */
1104: packet_get_bignum2(pctx->b);
1105: x4_s_proof = packet_get_string(&x4_s_proof_len);
1106: packet_check_eom();
1107:
1108: JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__));
1109:
1110: /* Derive shared key and calculate confirmation hash */
1111: jpake_key_confirm(pctx->grp, pctx->s, pctx->b,
1112: pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4,
1113: pctx->client_id, pctx->client_id_len,
1114: pctx->server_id, pctx->server_id_len,
1115: session_id2, session_id2_len,
1116: x4_s_proof, x4_s_proof_len,
1117: &pctx->k,
1118: &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len);
1119:
1120: bzero(x4_s_proof, x4_s_proof_len);
1.197 djm 1121: free(x4_s_proof);
1.170 djm 1122:
1123: JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__));
1124:
1125: /* Send key confirmation proof */
1126: packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM);
1127: packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
1128: packet_send();
1129:
1130: /* Expect confirmation from peer */
1131: dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM,
1132: input_userauth_jpake_server_confirm);
1133: }
1134:
1135: /* ARGSUSED */
1136: void
1137: input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt)
1138: {
1139: Authctxt *authctxt = ctxt;
1140: struct jpake_ctx *pctx = authctxt->methoddata;
1141:
1142: /* Disable this message */
1143: dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL);
1144:
1145: pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len);
1146: packet_check_eom();
1147:
1148: JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__));
1149:
1150: /* Verify expected confirmation hash */
1151: if (jpake_check_confirm(pctx->k,
1152: pctx->server_id, pctx->server_id_len,
1153: session_id2, session_id2_len,
1154: pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1)
1155: debug("%s: %s success", __func__, authctxt->method->name);
1156: else {
1157: debug("%s: confirmation mismatch", __func__);
1158: /* XXX stash this so if auth succeeds then we can warn/kill */
1159: }
1160:
1161: userauth_jpake_cleanup(authctxt);
1162: }
1163: #endif /* JPAKE */
1164:
1.117 markus 1165: static int
1166: identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1167: u_char *data, u_int datalen)
1168: {
1169: Key *prv;
1170: int ret;
1.99 markus 1171:
1.117 markus 1172: /* the agent supports this key */
1173: if (id->ac)
1174: return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1175: data, datalen));
1176: /*
1177: * we have already loaded the private key or
1178: * the private key is stored in external hardware
1179: */
1180: if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1181: return (key_sign(id->key, sigp, lenp, data, datalen));
1182: /* load the private key from the file */
1.191 dtucker 1183: if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL)
1.117 markus 1184: return (-1);
1185: ret = key_sign(prv, sigp, lenp, data, datalen);
1186: key_free(prv);
1187: return (ret);
1.51 markus 1188: }
1189:
1.76 itojun 1190: static int
1.117 markus 1191: sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1.1 markus 1192: {
1193: Buffer b;
1.32 markus 1194: u_char *blob, *signature;
1.96 markus 1195: u_int bloblen, slen;
1.120 markus 1196: u_int skip = 0;
1.17 markus 1197: int ret = -1;
1.23 markus 1198: int have_sig = 1;
1.182 djm 1199: char *fp;
1.1 markus 1200:
1.182 djm 1201: fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
1202: debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
1.197 djm 1203: free(fp);
1.51 markus 1204:
1.117 markus 1205: if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1.28 markus 1206: /* we cannot handle this key */
1.30 markus 1207: debug3("sign_and_send_pubkey: cannot handle key");
1.28 markus 1208: return 0;
1209: }
1.1 markus 1210: /* data to be signed */
1211: buffer_init(&b);
1.26 markus 1212: if (datafellows & SSH_OLD_SESSIONID) {
1213: buffer_append(&b, session_id2, session_id2_len);
1.41 stevesk 1214: skip = session_id2_len;
1.26 markus 1215: } else {
1.14 markus 1216: buffer_put_string(&b, session_id2, session_id2_len);
1217: skip = buffer_len(&b);
1218: }
1.1 markus 1219: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.20 markus 1220: buffer_put_cstring(&b, authctxt->server_user);
1.10 markus 1221: buffer_put_cstring(&b,
1.30 markus 1222: datafellows & SSH_BUG_PKSERVICE ?
1.10 markus 1223: "ssh-userauth" :
1.20 markus 1224: authctxt->service);
1.30 markus 1225: if (datafellows & SSH_BUG_PKAUTH) {
1226: buffer_put_char(&b, have_sig);
1227: } else {
1228: buffer_put_cstring(&b, authctxt->method->name);
1229: buffer_put_char(&b, have_sig);
1.117 markus 1230: buffer_put_cstring(&b, key_ssh_name(id->key));
1.30 markus 1231: }
1.1 markus 1232: buffer_put_string(&b, blob, bloblen);
1233:
1234: /* generate signature */
1.117 markus 1235: ret = identity_sign(id, &signature, &slen,
1.51 markus 1236: buffer_ptr(&b), buffer_len(&b));
1.17 markus 1237: if (ret == -1) {
1.197 djm 1238: free(blob);
1.17 markus 1239: buffer_free(&b);
1240: return 0;
1241: }
1.28 markus 1242: #ifdef DEBUG_PK
1.1 markus 1243: buffer_dump(&b);
1244: #endif
1.30 markus 1245: if (datafellows & SSH_BUG_PKSERVICE) {
1.10 markus 1246: buffer_clear(&b);
1247: buffer_append(&b, session_id2, session_id2_len);
1.51 markus 1248: skip = session_id2_len;
1.10 markus 1249: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.20 markus 1250: buffer_put_cstring(&b, authctxt->server_user);
1251: buffer_put_cstring(&b, authctxt->service);
1.23 markus 1252: buffer_put_cstring(&b, authctxt->method->name);
1253: buffer_put_char(&b, have_sig);
1.30 markus 1254: if (!(datafellows & SSH_BUG_PKAUTH))
1.117 markus 1255: buffer_put_cstring(&b, key_ssh_name(id->key));
1.10 markus 1256: buffer_put_string(&b, blob, bloblen);
1257: }
1.197 djm 1258: free(blob);
1.51 markus 1259:
1.1 markus 1260: /* append signature */
1261: buffer_put_string(&b, signature, slen);
1.197 djm 1262: free(signature);
1.1 markus 1263:
1264: /* skip session id and packet type */
1.14 markus 1265: if (buffer_len(&b) < skip + 1)
1.20 markus 1266: fatal("userauth_pubkey: internal error");
1.14 markus 1267: buffer_consume(&b, skip + 1);
1.1 markus 1268:
1269: /* put remaining data from buffer into packet */
1270: packet_start(SSH2_MSG_USERAUTH_REQUEST);
1271: packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1272: buffer_free(&b);
1273: packet_send();
1.17 markus 1274:
1275: return 1;
1.16 markus 1276: }
1277:
1.76 itojun 1278: static int
1.117 markus 1279: send_pubkey_test(Authctxt *authctxt, Identity *id)
1.20 markus 1280: {
1.51 markus 1281: u_char *blob;
1.97 markus 1282: u_int bloblen, have_sig = 0;
1.20 markus 1283:
1.51 markus 1284: debug3("send_pubkey_test");
1.16 markus 1285:
1.117 markus 1286: if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1.51 markus 1287: /* we cannot handle this key */
1288: debug3("send_pubkey_test: cannot handle key");
1.16 markus 1289: return 0;
1290: }
1.51 markus 1291: /* register callback for USERAUTH_PK_OK message */
1292: dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1293:
1294: packet_start(SSH2_MSG_USERAUTH_REQUEST);
1295: packet_put_cstring(authctxt->server_user);
1296: packet_put_cstring(authctxt->service);
1297: packet_put_cstring(authctxt->method->name);
1298: packet_put_char(have_sig);
1299: if (!(datafellows & SSH_BUG_PKAUTH))
1.117 markus 1300: packet_put_cstring(key_ssh_name(id->key));
1.51 markus 1301: packet_put_string(blob, bloblen);
1.197 djm 1302: free(blob);
1.51 markus 1303: packet_send();
1304: return 1;
1305: }
1306:
1.76 itojun 1307: static Key *
1.191 dtucker 1308: load_identity_file(char *filename, int userprovided)
1.51 markus 1309: {
1310: Key *private;
1311: char prompt[300], *passphrase;
1.178 dtucker 1312: int perm_ok = 0, quit, i;
1.52 markus 1313: struct stat st;
1.16 markus 1314:
1.52 markus 1315: if (stat(filename, &st) < 0) {
1.191 dtucker 1316: (userprovided ? logit : debug3)("no such identity: %s: %s",
1317: filename, strerror(errno));
1.52 markus 1318: return NULL;
1319: }
1.152 dtucker 1320: private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1.198 dtucker 1321: if (!perm_ok) {
1322: if (private != NULL)
1323: key_free(private);
1.152 dtucker 1324: return NULL;
1.198 dtucker 1325: }
1.56 markus 1326: if (private == NULL) {
1327: if (options.batch_mode)
1.51 markus 1328: return NULL;
1.16 markus 1329: snprintf(prompt, sizeof prompt,
1.88 deraadt 1330: "Enter passphrase for key '%.100s': ", filename);
1.20 markus 1331: for (i = 0; i < options.number_of_password_prompts; i++) {
1332: passphrase = read_passphrase(prompt, 0);
1333: if (strcmp(passphrase, "") != 0) {
1.152 dtucker 1334: private = key_load_private_type(KEY_UNSPEC,
1335: filename, passphrase, NULL, NULL);
1.51 markus 1336: quit = 0;
1.20 markus 1337: } else {
1338: debug2("no passphrase given, try next key");
1.51 markus 1339: quit = 1;
1.20 markus 1340: }
1341: memset(passphrase, 0, strlen(passphrase));
1.197 djm 1342: free(passphrase);
1.56 markus 1343: if (private != NULL || quit)
1.20 markus 1344: break;
1345: debug2("bad passphrase given, try again...");
1.16 markus 1346: }
1347: }
1.51 markus 1348: return private;
1349: }
1350:
1.117 markus 1351: /*
1352: * try keys in the following order:
1353: * 1. agent keys that are found in the config file
1354: * 2. other agent keys
1355: * 3. keys that are only listed in the config file
1356: */
1357: static void
1358: pubkey_prepare(Authctxt *authctxt)
1.51 markus 1359: {
1.190 djm 1360: Identity *id, *id2, *tmp;
1.117 markus 1361: Idlist agent, files, *preferred;
1362: Key *key;
1363: AuthenticationConnection *ac;
1364: char *comment;
1365: int i, found;
1.51 markus 1366:
1.117 markus 1367: TAILQ_INIT(&agent); /* keys from the agent */
1368: TAILQ_INIT(&files); /* keys from the config file */
1369: preferred = &authctxt->keys;
1370: TAILQ_INIT(preferred); /* preferred order of keys */
1371:
1.190 djm 1372: /* list of keys stored in the filesystem and PKCS#11 */
1.117 markus 1373: for (i = 0; i < options.num_identity_files; i++) {
1374: key = options.identity_keys[i];
1375: if (key && key->type == KEY_RSA1)
1.180 djm 1376: continue;
1377: if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1.117 markus 1378: continue;
1379: options.identity_keys[i] = NULL;
1.150 djm 1380: id = xcalloc(1, sizeof(*id));
1.117 markus 1381: id->key = key;
1382: id->filename = xstrdup(options.identity_files[i]);
1.192 dtucker 1383: id->userprovided = options.identity_file_userprovided[i];
1.117 markus 1384: TAILQ_INSERT_TAIL(&files, id, next);
1.190 djm 1385: }
1386: /* Prefer PKCS11 keys that are explicitly listed */
1387: TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1388: if (id->key == NULL || (id->key->flags & KEY_FLAG_EXT) == 0)
1389: continue;
1390: found = 0;
1391: TAILQ_FOREACH(id2, &files, next) {
1392: if (id2->key == NULL ||
1393: (id2->key->flags & KEY_FLAG_EXT) != 0)
1394: continue;
1395: if (key_equal(id->key, id2->key)) {
1396: TAILQ_REMOVE(&files, id, next);
1397: TAILQ_INSERT_TAIL(preferred, id, next);
1398: found = 1;
1399: break;
1400: }
1401: }
1402: /* If IdentitiesOnly set and key not found then don't use it */
1403: if (!found && options.identities_only) {
1404: TAILQ_REMOVE(&files, id, next);
1.195 djm 1405: bzero(id, sizeof(*id));
1.190 djm 1406: free(id);
1407: }
1.117 markus 1408: }
1409: /* list of keys supported by the agent */
1410: if ((ac = ssh_get_authentication_connection())) {
1411: for (key = ssh_get_first_identity(ac, &comment, 2);
1412: key != NULL;
1413: key = ssh_get_next_identity(ac, &comment, 2)) {
1414: found = 0;
1415: TAILQ_FOREACH(id, &files, next) {
1.133 djm 1416: /* agent keys from the config file are preferred */
1.117 markus 1417: if (key_equal(key, id->key)) {
1418: key_free(key);
1.197 djm 1419: free(comment);
1.117 markus 1420: TAILQ_REMOVE(&files, id, next);
1421: TAILQ_INSERT_TAIL(preferred, id, next);
1422: id->ac = ac;
1423: found = 1;
1424: break;
1425: }
1426: }
1.135 markus 1427: if (!found && !options.identities_only) {
1.150 djm 1428: id = xcalloc(1, sizeof(*id));
1.117 markus 1429: id->key = key;
1430: id->filename = comment;
1431: id->ac = ac;
1432: TAILQ_INSERT_TAIL(&agent, id, next);
1433: }
1434: }
1435: /* append remaining agent keys */
1436: for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1437: TAILQ_REMOVE(&agent, id, next);
1438: TAILQ_INSERT_TAIL(preferred, id, next);
1439: }
1440: authctxt->agent = ac;
1441: }
1442: /* append remaining keys from the config file */
1443: for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1444: TAILQ_REMOVE(&files, id, next);
1445: TAILQ_INSERT_TAIL(preferred, id, next);
1446: }
1447: TAILQ_FOREACH(id, preferred, next) {
1.191 dtucker 1448: debug2("key: %s (%p),%s", id->filename, id->key,
1449: id->userprovided ? " explicit" : "");
1.117 markus 1450: }
1.17 markus 1451: }
1452:
1.117 markus 1453: static void
1454: pubkey_cleanup(Authctxt *authctxt)
1.51 markus 1455: {
1.117 markus 1456: Identity *id;
1.51 markus 1457:
1.117 markus 1458: if (authctxt->agent != NULL)
1459: ssh_close_authentication_connection(authctxt->agent);
1460: for (id = TAILQ_FIRST(&authctxt->keys); id;
1461: id = TAILQ_FIRST(&authctxt->keys)) {
1462: TAILQ_REMOVE(&authctxt->keys, id, next);
1463: if (id->key)
1464: key_free(id->key);
1.197 djm 1465: free(id->filename);
1466: free(id);
1.117 markus 1467: }
1.1 markus 1468: }
1469:
1.20 markus 1470: int
1471: userauth_pubkey(Authctxt *authctxt)
1472: {
1.117 markus 1473: Identity *id;
1.20 markus 1474: int sent = 0;
1475:
1.117 markus 1476: while ((id = TAILQ_FIRST(&authctxt->keys))) {
1477: if (id->tried++)
1478: return (0);
1.127 markus 1479: /* move key to the end of the queue */
1.117 markus 1480: TAILQ_REMOVE(&authctxt->keys, id, next);
1481: TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1482: /*
1483: * send a test message if we have the public key. for
1484: * encrypted keys we cannot do this and have to load the
1485: * private key instead
1486: */
1.200 ! djm 1487: if (id->key != NULL) {
! 1488: if (key_type_plain(id->key->type) == KEY_RSA &&
! 1489: (datafellows & SSH_BUG_RSASIGMD5) != 0) {
! 1490: debug("Skipped %s key %s for RSA/MD5 server",
! 1491: key_type(id->key), id->filename);
! 1492: } else if (id->key->type != KEY_RSA1) {
! 1493: debug("Offering %s public key: %s",
! 1494: key_type(id->key), id->filename);
! 1495: sent = send_pubkey_test(authctxt, id);
! 1496: }
! 1497: } else {
1.117 markus 1498: debug("Trying private key: %s", id->filename);
1.191 dtucker 1499: id->key = load_identity_file(id->filename,
1500: id->userprovided);
1.117 markus 1501: if (id->key != NULL) {
1502: id->isprivate = 1;
1.200 ! djm 1503: if (key_type_plain(id->key->type) == KEY_RSA &&
! 1504: (datafellows & SSH_BUG_RSASIGMD5) != 0) {
! 1505: debug("Skipped %s key %s for RSA/MD5 "
! 1506: "server", key_type(id->key),
! 1507: id->filename);
! 1508: } else {
! 1509: sent = sign_and_send_pubkey(
! 1510: authctxt, id);
! 1511: }
1.117 markus 1512: key_free(id->key);
1513: id->key = NULL;
1.51 markus 1514: }
1515: }
1.117 markus 1516: if (sent)
1517: return (sent);
1.28 markus 1518: }
1.117 markus 1519: return (0);
1.20 markus 1520: }
1521:
1.23 markus 1522: /*
1523: * Send userauth request message specifying keyboard-interactive method.
1524: */
1525: int
1526: userauth_kbdint(Authctxt *authctxt)
1527: {
1528: static int attempt = 0;
1529:
1530: if (attempt++ >= options.number_of_password_prompts)
1531: return 0;
1.82 markus 1532: /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1533: if (attempt > 1 && !authctxt->info_req_seen) {
1534: debug3("userauth_kbdint: disable: no info_req_seen");
1535: dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1536: return 0;
1537: }
1.23 markus 1538:
1539: debug2("userauth_kbdint");
1540: packet_start(SSH2_MSG_USERAUTH_REQUEST);
1541: packet_put_cstring(authctxt->server_user);
1542: packet_put_cstring(authctxt->service);
1543: packet_put_cstring(authctxt->method->name);
1544: packet_put_cstring(""); /* lang */
1545: packet_put_cstring(options.kbd_interactive_devices ?
1546: options.kbd_interactive_devices : "");
1547: packet_send();
1548:
1549: dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1550: return 1;
1551: }
1552:
1553: /*
1.46 markus 1554: * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1.23 markus 1555: */
1556: void
1.92 markus 1557: input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1.23 markus 1558: {
1559: Authctxt *authctxt = ctxt;
1.46 markus 1560: char *name, *inst, *lang, *prompt, *response;
1.32 markus 1561: u_int num_prompts, i;
1.23 markus 1562: int echo = 0;
1563:
1564: debug2("input_userauth_info_req");
1565:
1566: if (authctxt == NULL)
1567: fatal("input_userauth_info_req: no authentication context");
1.82 markus 1568:
1569: authctxt->info_req_seen = 1;
1.23 markus 1570:
1571: name = packet_get_string(NULL);
1572: inst = packet_get_string(NULL);
1573: lang = packet_get_string(NULL);
1574: if (strlen(name) > 0)
1.116 itojun 1575: logit("%s", name);
1.23 markus 1576: if (strlen(inst) > 0)
1.116 itojun 1577: logit("%s", inst);
1.197 djm 1578: free(name);
1579: free(inst);
1580: free(lang);
1.23 markus 1581:
1582: num_prompts = packet_get_int();
1583: /*
1584: * Begin to build info response packet based on prompts requested.
1585: * We commit to providing the correct number of responses, so if
1586: * further on we run into a problem that prevents this, we have to
1587: * be sure and clean this up and send a correct error response.
1588: */
1589: packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1590: packet_put_int(num_prompts);
1591:
1.73 markus 1592: debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1.23 markus 1593: for (i = 0; i < num_prompts; i++) {
1594: prompt = packet_get_string(NULL);
1595: echo = packet_get_char();
1596:
1.77 markus 1597: response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1.23 markus 1598:
1.49 markus 1599: packet_put_cstring(response);
1.23 markus 1600: memset(response, 0, strlen(response));
1.197 djm 1601: free(response);
1602: free(prompt);
1.23 markus 1603: }
1.90 markus 1604: packet_check_eom(); /* done with parsing incoming message. */
1.23 markus 1605:
1.85 markus 1606: packet_add_padding(64);
1.23 markus 1607: packet_send();
1.68 markus 1608: }
1609:
1.100 markus 1610: static int
1.105 deraadt 1611: ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1.100 markus 1612: u_char *data, u_int datalen)
1613: {
1614: Buffer b;
1.101 markus 1615: struct stat st;
1.100 markus 1616: pid_t pid;
1.103 markus 1617: int to[2], from[2], status, version = 2;
1.100 markus 1618:
1.109 markus 1619: debug2("ssh_keysign called");
1.100 markus 1620:
1.101 markus 1621: if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1.179 dtucker 1622: error("ssh_keysign: not installed: %s", strerror(errno));
1.101 markus 1623: return -1;
1624: }
1.100 markus 1625: if (fflush(stdout) != 0)
1626: error("ssh_keysign: fflush: %s", strerror(errno));
1627: if (pipe(to) < 0) {
1628: error("ssh_keysign: pipe: %s", strerror(errno));
1629: return -1;
1630: }
1631: if (pipe(from) < 0) {
1632: error("ssh_keysign: pipe: %s", strerror(errno));
1633: return -1;
1634: }
1635: if ((pid = fork()) < 0) {
1636: error("ssh_keysign: fork: %s", strerror(errno));
1637: return -1;
1638: }
1639: if (pid == 0) {
1.174 dtucker 1640: /* keep the socket on exec */
1641: fcntl(packet_get_connection_in(), F_SETFD, 0);
1.155 markus 1642: permanently_drop_suid(getuid());
1.100 markus 1643: close(from[0]);
1644: if (dup2(from[1], STDOUT_FILENO) < 0)
1645: fatal("ssh_keysign: dup2: %s", strerror(errno));
1646: close(to[1]);
1647: if (dup2(to[0], STDIN_FILENO) < 0)
1648: fatal("ssh_keysign: dup2: %s", strerror(errno));
1.103 markus 1649: close(from[1]);
1650: close(to[0]);
1.102 markus 1651: execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1.100 markus 1652: fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1653: strerror(errno));
1654: }
1655: close(from[1]);
1656: close(to[0]);
1657:
1658: buffer_init(&b);
1.103 markus 1659: buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1.100 markus 1660: buffer_put_string(&b, data, datalen);
1.131 djm 1661: if (ssh_msg_send(to[1], version, &b) == -1)
1662: fatal("ssh_keysign: couldn't send request");
1.100 markus 1663:
1.110 djm 1664: if (ssh_msg_recv(from[0], &b) < 0) {
1.101 markus 1665: error("ssh_keysign: no reply");
1.134 markus 1666: buffer_free(&b);
1.100 markus 1667: return -1;
1668: }
1.101 markus 1669: close(from[0]);
1670: close(to[1]);
1671:
1.103 markus 1672: while (waitpid(pid, &status, 0) < 0)
1673: if (errno != EINTR)
1674: break;
1.101 markus 1675:
1.100 markus 1676: if (buffer_get_char(&b) != version) {
1.101 markus 1677: error("ssh_keysign: bad version");
1.134 markus 1678: buffer_free(&b);
1.100 markus 1679: return -1;
1680: }
1681: *sigp = buffer_get_string(&b, lenp);
1.134 markus 1682: buffer_free(&b);
1.100 markus 1683:
1684: return 0;
1685: }
1686:
1.68 markus 1687: int
1688: userauth_hostbased(Authctxt *authctxt)
1689: {
1690: Key *private = NULL;
1.100 markus 1691: Sensitive *sensitive = authctxt->sensitive;
1.68 markus 1692: Buffer b;
1693: u_char *signature, *blob;
1.179 dtucker 1694: char *chost, *pkalg, *p;
1.72 markus 1695: const char *service;
1.68 markus 1696: u_int blen, slen;
1.176 dtucker 1697: int ok, i, found = 0;
1.68 markus 1698:
1699: /* check for a useful key */
1.100 markus 1700: for (i = 0; i < sensitive->nkeys; i++) {
1701: private = sensitive->keys[i];
1.68 markus 1702: if (private && private->type != KEY_RSA1) {
1703: found = 1;
1704: /* we take and free the key */
1.100 markus 1705: sensitive->keys[i] = NULL;
1.68 markus 1706: break;
1707: }
1708: }
1709: if (!found) {
1.109 markus 1710: debug("No more client hostkeys for hostbased authentication.");
1.68 markus 1711: return 0;
1712: }
1713: if (key_to_blob(private, &blob, &blen) == 0) {
1714: key_free(private);
1715: return 0;
1716: }
1.84 markus 1717: /* figure out a name for the client host */
1.179 dtucker 1718: p = get_local_name(packet_get_connection_in());
1.84 markus 1719: if (p == NULL) {
1720: error("userauth_hostbased: cannot get local ipaddr/name");
1721: key_free(private);
1.197 djm 1722: free(blob);
1.84 markus 1723: return 0;
1724: }
1.150 djm 1725: xasprintf(&chost, "%s.", p);
1.84 markus 1726: debug2("userauth_hostbased: chost %s", chost);
1.197 djm 1727: free(p);
1.84 markus 1728:
1.72 markus 1729: service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1730: authctxt->service;
1.68 markus 1731: pkalg = xstrdup(key_ssh_name(private));
1732: buffer_init(&b);
1733: /* construct data */
1.72 markus 1734: buffer_put_string(&b, session_id2, session_id2_len);
1.68 markus 1735: buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1736: buffer_put_cstring(&b, authctxt->server_user);
1.72 markus 1737: buffer_put_cstring(&b, service);
1.68 markus 1738: buffer_put_cstring(&b, authctxt->method->name);
1739: buffer_put_cstring(&b, pkalg);
1740: buffer_put_string(&b, blob, blen);
1741: buffer_put_cstring(&b, chost);
1742: buffer_put_cstring(&b, authctxt->local_user);
1743: #ifdef DEBUG_PK
1744: buffer_dump(&b);
1745: #endif
1.100 markus 1746: if (sensitive->external_keysign)
1747: ok = ssh_keysign(private, &signature, &slen,
1748: buffer_ptr(&b), buffer_len(&b));
1749: else
1750: ok = key_sign(private, &signature, &slen,
1751: buffer_ptr(&b), buffer_len(&b));
1.68 markus 1752: key_free(private);
1753: buffer_free(&b);
1754: if (ok != 0) {
1755: error("key_sign failed");
1.197 djm 1756: free(chost);
1757: free(pkalg);
1758: free(blob);
1.68 markus 1759: return 0;
1760: }
1761: packet_start(SSH2_MSG_USERAUTH_REQUEST);
1762: packet_put_cstring(authctxt->server_user);
1763: packet_put_cstring(authctxt->service);
1764: packet_put_cstring(authctxt->method->name);
1765: packet_put_cstring(pkalg);
1766: packet_put_string(blob, blen);
1767: packet_put_cstring(chost);
1768: packet_put_cstring(authctxt->local_user);
1769: packet_put_string(signature, slen);
1770: memset(signature, 's', slen);
1.197 djm 1771: free(signature);
1772: free(chost);
1773: free(pkalg);
1774: free(blob);
1.68 markus 1775:
1776: packet_send();
1777: return 1;
1.23 markus 1778: }
1.20 markus 1779:
1.170 djm 1780: #ifdef JPAKE
1781: int
1782: userauth_jpake(Authctxt *authctxt)
1783: {
1784: struct jpake_ctx *pctx;
1785: u_char *x1_proof, *x2_proof;
1786: u_int x1_proof_len, x2_proof_len;
1787: static int attempt = 0; /* XXX share with userauth_password's? */
1788:
1789: if (attempt++ >= options.number_of_password_prompts)
1790: return 0;
1791: if (attempt != 1)
1792: error("Permission denied, please try again.");
1793:
1794: if (authctxt->methoddata != NULL)
1795: fatal("%s: authctxt->methoddata already set (%p)",
1796: __func__, authctxt->methoddata);
1797:
1798: authctxt->methoddata = pctx = jpake_new();
1799:
1800: /*
1801: * Send request immediately, to get the protocol going while
1802: * we do the initial computations.
1803: */
1804: packet_start(SSH2_MSG_USERAUTH_REQUEST);
1805: packet_put_cstring(authctxt->server_user);
1806: packet_put_cstring(authctxt->service);
1807: packet_put_cstring(authctxt->method->name);
1808: packet_send();
1809: packet_write_wait();
1810:
1811: jpake_step1(pctx->grp,
1812: &pctx->client_id, &pctx->client_id_len,
1813: &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2,
1814: &x1_proof, &x1_proof_len,
1815: &x2_proof, &x2_proof_len);
1816:
1817: JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__));
1818:
1819: packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1);
1820: packet_put_string(pctx->client_id, pctx->client_id_len);
1821: packet_put_bignum2(pctx->g_x1);
1822: packet_put_bignum2(pctx->g_x2);
1823: packet_put_string(x1_proof, x1_proof_len);
1824: packet_put_string(x2_proof, x2_proof_len);
1825: packet_send();
1826:
1827: bzero(x1_proof, x1_proof_len);
1828: bzero(x2_proof, x2_proof_len);
1.197 djm 1829: free(x1_proof);
1830: free(x2_proof);
1.170 djm 1831:
1832: /* Expect step 1 packet from peer */
1833: dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1,
1834: input_userauth_jpake_server_step1);
1.172 djm 1835: dispatch_set(SSH2_MSG_USERAUTH_SUCCESS,
1836: &input_userauth_success_unexpected);
1.170 djm 1837:
1838: return 1;
1839: }
1840:
1841: void
1842: userauth_jpake_cleanup(Authctxt *authctxt)
1843: {
1844: debug3("%s: clean up", __func__);
1845: if (authctxt->methoddata != NULL) {
1846: jpake_free(authctxt->methoddata);
1847: authctxt->methoddata = NULL;
1848: }
1.172 djm 1849: dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
1.170 djm 1850: }
1851: #endif /* JPAKE */
1852:
1.20 markus 1853: /* find auth method */
1854:
1855: /*
1856: * given auth method name, if configurable options permit this method fill
1857: * in auth_ident field and return true, otherwise return false.
1858: */
1.76 itojun 1859: static int
1.20 markus 1860: authmethod_is_enabled(Authmethod *method)
1861: {
1862: if (method == NULL)
1863: return 0;
1864: /* return false if options indicate this method is disabled */
1865: if (method->enabled == NULL || *method->enabled == 0)
1866: return 0;
1867: /* return false if batch mode is enabled but method needs interactive mode */
1868: if (method->batch_flag != NULL && *method->batch_flag != 0)
1869: return 0;
1870: return 1;
1871: }
1872:
1.76 itojun 1873: static Authmethod *
1.20 markus 1874: authmethod_lookup(const char *name)
1.1 markus 1875: {
1.20 markus 1876: Authmethod *method = NULL;
1877: if (name != NULL)
1878: for (method = authmethods; method->name != NULL; method++)
1879: if (strcmp(name, method->name) == 0)
1880: return method;
1881: debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1882: return NULL;
1883: }
1.1 markus 1884:
1.53 markus 1885: /* XXX internal state */
1886: static Authmethod *current = NULL;
1887: static char *supported = NULL;
1888: static char *preferred = NULL;
1.105 deraadt 1889:
1.20 markus 1890: /*
1891: * Given the authentication method list sent by the server, return the
1892: * next method we should try. If the server initially sends a nil list,
1.67 markus 1893: * use a built-in default list.
1.41 stevesk 1894: */
1.76 itojun 1895: static Authmethod *
1.20 markus 1896: authmethod_get(char *authlist)
1897: {
1.53 markus 1898: char *name = NULL;
1.97 markus 1899: u_int next;
1.41 stevesk 1900:
1.20 markus 1901: /* Use a suitable default if we're passed a nil list. */
1902: if (authlist == NULL || strlen(authlist) == 0)
1.53 markus 1903: authlist = options.preferred_authentications;
1.20 markus 1904:
1.53 markus 1905: if (supported == NULL || strcmp(authlist, supported) != 0) {
1906: debug3("start over, passed a different list %s", authlist);
1.197 djm 1907: free(supported);
1.53 markus 1908: supported = xstrdup(authlist);
1909: preferred = options.preferred_authentications;
1910: debug3("preferred %s", preferred);
1911: current = NULL;
1912: } else if (current != NULL && authmethod_is_enabled(current))
1913: return current;
1.20 markus 1914:
1.53 markus 1915: for (;;) {
1916: if ((name = match_list(preferred, supported, &next)) == NULL) {
1.109 markus 1917: debug("No more authentication methods to try.");
1.53 markus 1918: current = NULL;
1919: return NULL;
1920: }
1921: preferred += next;
1.23 markus 1922: debug3("authmethod_lookup %s", name);
1.53 markus 1923: debug3("remaining preferred: %s", preferred);
1924: if ((current = authmethod_lookup(name)) != NULL &&
1925: authmethod_is_enabled(current)) {
1.23 markus 1926: debug3("authmethod_is_enabled %s", name);
1.109 markus 1927: debug("Next authentication method: %s", name);
1.197 djm 1928: free(name);
1.53 markus 1929: return current;
1.23 markus 1930: }
1.198 dtucker 1931: free(name);
1.1 markus 1932: }
1.53 markus 1933: }
1.1 markus 1934:
1.79 stevesk 1935: static char *
1.53 markus 1936: authmethods_get(void)
1937: {
1938: Authmethod *method = NULL;
1.93 markus 1939: Buffer b;
1940: char *list;
1.27 provos 1941:
1.93 markus 1942: buffer_init(&b);
1.53 markus 1943: for (method = authmethods; method->name != NULL; method++) {
1944: if (authmethod_is_enabled(method)) {
1.93 markus 1945: if (buffer_len(&b) > 0)
1946: buffer_append(&b, ",", 1);
1947: buffer_append(&b, method->name, strlen(method->name));
1.53 markus 1948: }
1949: }
1.93 markus 1950: buffer_append(&b, "\0", 1);
1951: list = xstrdup(buffer_ptr(&b));
1952: buffer_free(&b);
1953: return list;
1.1 markus 1954: }
1.170 djm 1955: