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