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