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