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