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