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