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