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