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