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