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