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