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