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