1 /* $OpenBSD: sshconnect2.c,v 1.328 2020/10/04 09:45:01 djm Exp $ */ 2 /* 3 * Copyright (c) 2000 Markus Friedl. All rights reserved. 4 * Copyright (c) 2008 Damien Miller. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include "includes.h" 28 29 #include <sys/types.h> 30 #include <sys/socket.h> 31 #include <sys/wait.h> 32 #include <sys/stat.h> 33 34 #include <errno.h> 35 #include <fcntl.h> 36 #include <netdb.h> 37 #include <pwd.h> 38 #include <signal.h> 39 #include <stdio.h> 40 #include <string.h> 41 #include <stdarg.h> 42 #include <unistd.h> 43 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS) 44 #include <vis.h> 45 #endif 46 47 #include "openbsd-compat/sys-queue.h" 48 49 #include "xmalloc.h" 50 #include "ssh.h" 51 #include "ssh2.h" 52 #include "sshbuf.h" 53 #include "packet.h" 54 #include "compat.h" 55 #include "cipher.h" 56 #include "sshkey.h" 57 #include "kex.h" 58 #include "myproposal.h" 59 #include "sshconnect.h" 60 #include "authfile.h" 61 #include "dh.h" 62 #include "authfd.h" 63 #include "log.h" 64 #include "misc.h" 65 #include "readconf.h" 66 #include "match.h" 67 #include "dispatch.h" 68 #include "canohost.h" 69 #include "msg.h" 70 #include "pathnames.h" 71 #include "uidswap.h" 72 #include "hostfile.h" 73 #include "ssherr.h" 74 #include "utf8.h" 75 #include "ssh-sk.h" 76 #include "sk-api.h" 77 78 #ifdef GSSAPI 79 #include "ssh-gss.h" 80 #endif 81 82 /* import */ 83 extern char *client_version_string; 84 extern char *server_version_string; 85 extern Options options; 86 87 /* 88 * SSH2 key exchange 89 */ 90 91 u_char *session_id2 = NULL; 92 u_int session_id2_len = 0; 93 94 char *xxx_host; 95 struct sockaddr *xxx_hostaddr; 96 97 static int 98 verify_host_key_callback(struct sshkey *hostkey, struct ssh *ssh) 99 { 100 int cert_downgraded = 0; 101 102 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey, 103 &cert_downgraded) == -1) 104 fatal("Host key verification failed."); 105 if (cert_downgraded) 106 ssh->kex->flags |= KEX_HOSTCERT_CONVERT; 107 return 0; 108 } 109 110 /* Returns the first item from a comma-separated algorithm list */ 111 static char * 112 first_alg(const char *algs) 113 { 114 char *ret, *cp; 115 116 ret = xstrdup(algs); 117 if ((cp = strchr(ret, ',')) != NULL) 118 *cp = '\0'; 119 return ret; 120 } 121 122 static char * 123 order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port) 124 { 125 char *oavail = NULL, *avail = NULL, *first = NULL, *last = NULL; 126 char *alg = NULL, *hostname = NULL, *ret = NULL, *best = NULL; 127 size_t maxlen; 128 struct hostkeys *hostkeys = NULL; 129 int ktype; 130 u_int i; 131 132 /* Find all hostkeys for this hostname */ 133 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL); 134 hostkeys = init_hostkeys(); 135 for (i = 0; i < options.num_user_hostfiles; i++) 136 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]); 137 for (i = 0; i < options.num_system_hostfiles; i++) 138 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]); 139 140 /* 141 * If a plain public key exists that matches the type of the best 142 * preference HostkeyAlgorithms, then use the whole list as is. 143 * Note that we ignore whether the best preference algorithm is a 144 * certificate type, as sshconnect.c will downgrade certs to 145 * plain keys if necessary. 146 */ 147 best = first_alg(options.hostkeyalgorithms); 148 if (lookup_key_in_hostkeys_by_type(hostkeys, 149 sshkey_type_plain(sshkey_type_from_name(best)), 150 sshkey_ecdsa_nid_from_name(best), NULL)) { 151 debug3("%s: have matching best-preference key type %s, " 152 "using HostkeyAlgorithms verbatim", __func__, best); 153 ret = xstrdup(options.hostkeyalgorithms); 154 goto out; 155 } 156 157 /* 158 * Otherwise, prefer the host key algorithms that match known keys 159 * while keeping the ordering of HostkeyAlgorithms as much as possible. 160 */ 161 oavail = avail = xstrdup(options.hostkeyalgorithms); 162 maxlen = strlen(avail) + 1; 163 first = xmalloc(maxlen); 164 last = xmalloc(maxlen); 165 *first = *last = '\0'; 166 167 #define ALG_APPEND(to, from) \ 168 do { \ 169 if (*to != '\0') \ 170 strlcat(to, ",", maxlen); \ 171 strlcat(to, from, maxlen); \ 172 } while (0) 173 174 while ((alg = strsep(&avail, ",")) && *alg != '\0') { 175 if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC) 176 fatal("%s: unknown alg %s", __func__, alg); 177 /* 178 * If we have a @cert-authority marker in known_hosts then 179 * prefer all certificate algorithms. 180 */ 181 if (sshkey_type_is_cert(ktype) && 182 lookup_marker_in_hostkeys(hostkeys, MRK_CA)) { 183 ALG_APPEND(first, alg); 184 continue; 185 } 186 /* If the key appears in known_hosts then prefer it */ 187 if (lookup_key_in_hostkeys_by_type(hostkeys, 188 sshkey_type_plain(ktype), 189 sshkey_ecdsa_nid_from_name(alg), NULL)) { 190 ALG_APPEND(first, alg); 191 continue; 192 } 193 /* Otherwise, put it last */ 194 ALG_APPEND(last, alg); 195 } 196 #undef ALG_APPEND 197 xasprintf(&ret, "%s%s%s", first, 198 (*first == '\0' || *last == '\0') ? "" : ",", last); 199 if (*first != '\0') 200 debug3("%s: prefer hostkeyalgs: %s", __func__, first); 201 202 out: 203 free(best); 204 free(first); 205 free(last); 206 free(hostname); 207 free(oavail); 208 free_hostkeys(hostkeys); 209 210 return ret; 211 } 212 213 void 214 ssh_kex2(struct ssh *ssh, char *host, struct sockaddr *hostaddr, u_short port) 215 { 216 char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT }; 217 char *s, *all_key; 218 int r, use_known_hosts_order = 0; 219 220 xxx_host = host; 221 xxx_hostaddr = hostaddr; 222 223 /* 224 * If the user has not specified HostkeyAlgorithms, or has only 225 * appended or removed algorithms from that list then prefer algorithms 226 * that are in the list that are supported by known_hosts keys. 227 */ 228 if (options.hostkeyalgorithms == NULL || 229 options.hostkeyalgorithms[0] == '-' || 230 options.hostkeyalgorithms[0] == '+') 231 use_known_hosts_order = 1; 232 233 /* Expand or fill in HostkeyAlgorithms */ 234 all_key = sshkey_alg_list(0, 0, 1, ','); 235 if (kex_assemble_names(&options.hostkeyalgorithms, 236 kex_default_pk_alg(), all_key) != 0) 237 fatal("%s: kex_assemble_namelist", __func__); 238 free(all_key); 239 240 if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL) 241 fatal("%s: kex_names_cat", __func__); 242 myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(s); 243 myproposal[PROPOSAL_ENC_ALGS_CTOS] = 244 compat_cipher_proposal(options.ciphers); 245 myproposal[PROPOSAL_ENC_ALGS_STOC] = 246 compat_cipher_proposal(options.ciphers); 247 myproposal[PROPOSAL_COMP_ALGS_CTOS] = 248 myproposal[PROPOSAL_COMP_ALGS_STOC] = 249 (char *)compression_alg_list(options.compression); 250 myproposal[PROPOSAL_MAC_ALGS_CTOS] = 251 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs; 252 if (use_known_hosts_order) { 253 /* Query known_hosts and prefer algorithms that appear there */ 254 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = 255 compat_pkalg_proposal( 256 order_hostkeyalgs(host, hostaddr, port)); 257 } else { 258 /* Use specified HostkeyAlgorithms exactly */ 259 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = 260 compat_pkalg_proposal(options.hostkeyalgorithms); 261 } 262 263 if (options.rekey_limit || options.rekey_interval) 264 ssh_packet_set_rekey_limits(ssh, options.rekey_limit, 265 options.rekey_interval); 266 267 /* start key exchange */ 268 if ((r = kex_setup(ssh, myproposal)) != 0) 269 fatal("kex_setup: %s", ssh_err(r)); 270 #ifdef WITH_OPENSSL 271 ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client; 272 ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client; 273 ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client; 274 ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client; 275 ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client; 276 ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; 277 ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client; 278 # ifdef OPENSSL_HAS_ECC 279 ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client; 280 # endif 281 #endif 282 ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client; 283 ssh->kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_client; 284 ssh->kex->verify_host_key=&verify_host_key_callback; 285 286 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &ssh->kex->done); 287 288 /* remove ext-info from the KEX proposals for rekeying */ 289 myproposal[PROPOSAL_KEX_ALGS] = 290 compat_kex_proposal(options.kex_algorithms); 291 if ((r = kex_prop2buf(ssh->kex->my, myproposal)) != 0) 292 fatal("kex_prop2buf: %s", ssh_err(r)); 293 294 session_id2 = ssh->kex->session_id; 295 session_id2_len = ssh->kex->session_id_len; 296 297 #ifdef DEBUG_KEXDH 298 /* send 1st encrypted/maced/compressed message */ 299 if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 || 300 (r = sshpkt_put_cstring(ssh, "markus")) != 0 || 301 (r = sshpkt_send(ssh)) != 0 || 302 (r = ssh_packet_write_wait(ssh)) != 0) 303 fatal("%s: %s", __func__, ssh_err(r)); 304 #endif 305 } 306 307 /* 308 * Authenticate user 309 */ 310 311 typedef struct cauthctxt Authctxt; 312 typedef struct cauthmethod Authmethod; 313 typedef struct identity Identity; 314 typedef struct idlist Idlist; 315 316 struct identity { 317 TAILQ_ENTRY(identity) next; 318 int agent_fd; /* >=0 if agent supports key */ 319 struct sshkey *key; /* public/private key */ 320 char *filename; /* comment for agent-only keys */ 321 int tried; 322 int isprivate; /* key points to the private key */ 323 int userprovided; 324 }; 325 TAILQ_HEAD(idlist, identity); 326 327 struct cauthctxt { 328 const char *server_user; 329 const char *local_user; 330 const char *host; 331 const char *service; 332 struct cauthmethod *method; 333 sig_atomic_t success; 334 char *authlist; 335 #ifdef GSSAPI 336 /* gssapi */ 337 gss_OID_set gss_supported_mechs; 338 u_int mech_tried; 339 #endif 340 /* pubkey */ 341 struct idlist keys; 342 int agent_fd; 343 /* hostbased */ 344 Sensitive *sensitive; 345 char *oktypes, *ktypes; 346 const char *active_ktype; 347 /* kbd-interactive */ 348 int info_req_seen; 349 int attempt_kbdint; 350 /* password */ 351 int attempt_passwd; 352 /* generic */ 353 void *methoddata; 354 }; 355 356 struct cauthmethod { 357 char *name; /* string to compare against server's list */ 358 int (*userauth)(struct ssh *ssh); 359 void (*cleanup)(struct ssh *ssh); 360 int *enabled; /* flag in option struct that enables method */ 361 int *batch_flag; /* flag in option struct that disables method */ 362 }; 363 364 static int input_userauth_service_accept(int, u_int32_t, struct ssh *); 365 static int input_userauth_ext_info(int, u_int32_t, struct ssh *); 366 static int input_userauth_success(int, u_int32_t, struct ssh *); 367 static int input_userauth_failure(int, u_int32_t, struct ssh *); 368 static int input_userauth_banner(int, u_int32_t, struct ssh *); 369 static int input_userauth_error(int, u_int32_t, struct ssh *); 370 static int input_userauth_info_req(int, u_int32_t, struct ssh *); 371 static int input_userauth_pk_ok(int, u_int32_t, struct ssh *); 372 static int input_userauth_passwd_changereq(int, u_int32_t, struct ssh *); 373 374 static int userauth_none(struct ssh *); 375 static int userauth_pubkey(struct ssh *); 376 static int userauth_passwd(struct ssh *); 377 static int userauth_kbdint(struct ssh *); 378 static int userauth_hostbased(struct ssh *); 379 380 #ifdef GSSAPI 381 static int userauth_gssapi(struct ssh *); 382 static void userauth_gssapi_cleanup(struct ssh *); 383 static int input_gssapi_response(int type, u_int32_t, struct ssh *); 384 static int input_gssapi_token(int type, u_int32_t, struct ssh *); 385 static int input_gssapi_error(int, u_int32_t, struct ssh *); 386 static int input_gssapi_errtok(int, u_int32_t, struct ssh *); 387 #endif 388 389 void userauth(struct ssh *, char *); 390 391 static void pubkey_cleanup(struct ssh *); 392 static int sign_and_send_pubkey(struct ssh *ssh, Identity *); 393 static void pubkey_prepare(Authctxt *); 394 static void pubkey_reset(Authctxt *); 395 static struct sshkey *load_identity_file(Identity *); 396 397 static Authmethod *authmethod_get(char *authlist); 398 static Authmethod *authmethod_lookup(const char *name); 399 static char *authmethods_get(void); 400 401 Authmethod authmethods[] = { 402 #ifdef GSSAPI 403 {"gssapi-with-mic", 404 userauth_gssapi, 405 userauth_gssapi_cleanup, 406 &options.gss_authentication, 407 NULL}, 408 #endif 409 {"hostbased", 410 userauth_hostbased, 411 NULL, 412 &options.hostbased_authentication, 413 NULL}, 414 {"publickey", 415 userauth_pubkey, 416 NULL, 417 &options.pubkey_authentication, 418 NULL}, 419 {"keyboard-interactive", 420 userauth_kbdint, 421 NULL, 422 &options.kbd_interactive_authentication, 423 &options.batch_mode}, 424 {"password", 425 userauth_passwd, 426 NULL, 427 &options.password_authentication, 428 &options.batch_mode}, 429 {"none", 430 userauth_none, 431 NULL, 432 NULL, 433 NULL}, 434 {NULL, NULL, NULL, NULL, NULL} 435 }; 436 437 void 438 ssh_userauth2(struct ssh *ssh, const char *local_user, 439 const char *server_user, char *host, Sensitive *sensitive) 440 { 441 Authctxt authctxt; 442 int r; 443 444 if (options.challenge_response_authentication) 445 options.kbd_interactive_authentication = 1; 446 if (options.preferred_authentications == NULL) 447 options.preferred_authentications = authmethods_get(); 448 449 /* setup authentication context */ 450 memset(&authctxt, 0, sizeof(authctxt)); 451 authctxt.server_user = server_user; 452 authctxt.local_user = local_user; 453 authctxt.host = host; 454 authctxt.service = "ssh-connection"; /* service name */ 455 authctxt.success = 0; 456 authctxt.method = authmethod_lookup("none"); 457 authctxt.authlist = NULL; 458 authctxt.methoddata = NULL; 459 authctxt.sensitive = sensitive; 460 authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL; 461 authctxt.info_req_seen = 0; 462 authctxt.attempt_kbdint = 0; 463 authctxt.attempt_passwd = 0; 464 #if GSSAPI 465 authctxt.gss_supported_mechs = NULL; 466 authctxt.mech_tried = 0; 467 #endif 468 authctxt.agent_fd = -1; 469 pubkey_prepare(&authctxt); 470 if (authctxt.method == NULL) { 471 fatal("%s: internal error: cannot send userauth none request", 472 __func__); 473 } 474 475 if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 || 476 (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 || 477 (r = sshpkt_send(ssh)) != 0) 478 fatal("%s: %s", __func__, ssh_err(r)); 479 480 ssh->authctxt = &authctxt; 481 ssh_dispatch_init(ssh, &input_userauth_error); 482 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info); 483 ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept); 484 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt.success); /* loop until success */ 485 pubkey_cleanup(ssh); 486 ssh->authctxt = NULL; 487 488 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL); 489 490 if (!authctxt.success) 491 fatal("Authentication failed."); 492 debug("Authentication succeeded (%s).", authctxt.method->name); 493 } 494 495 /* ARGSUSED */ 496 static int 497 input_userauth_service_accept(int type, u_int32_t seq, struct ssh *ssh) 498 { 499 int r; 500 501 if (ssh_packet_remaining(ssh) > 0) { 502 char *reply; 503 504 if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0) 505 goto out; 506 debug2("service_accept: %s", reply); 507 free(reply); 508 } else { 509 debug2("buggy server: service_accept w/o service"); 510 } 511 if ((r = sshpkt_get_end(ssh)) != 0) 512 goto out; 513 debug("SSH2_MSG_SERVICE_ACCEPT received"); 514 515 /* initial userauth request */ 516 userauth_none(ssh); 517 518 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error); 519 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); 520 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure); 521 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner); 522 r = 0; 523 out: 524 return r; 525 } 526 527 /* ARGSUSED */ 528 static int 529 input_userauth_ext_info(int type, u_int32_t seqnr, struct ssh *ssh) 530 { 531 return kex_input_ext_info(type, seqnr, ssh); 532 } 533 534 void 535 userauth(struct ssh *ssh, char *authlist) 536 { 537 Authctxt *authctxt = (Authctxt *)ssh->authctxt; 538 539 if (authctxt->method != NULL && authctxt->method->cleanup != NULL) 540 authctxt->method->cleanup(ssh); 541 542 free(authctxt->methoddata); 543 authctxt->methoddata = NULL; 544 if (authlist == NULL) { 545 authlist = authctxt->authlist; 546 } else { 547 free(authctxt->authlist); 548 authctxt->authlist = authlist; 549 } 550 for (;;) { 551 Authmethod *method = authmethod_get(authlist); 552 if (method == NULL) 553 fatal("%s@%s: Permission denied (%s).", 554 authctxt->server_user, authctxt->host, authlist); 555 authctxt->method = method; 556 557 /* reset the per method handler */ 558 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_PER_METHOD_MIN, 559 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL); 560 561 /* and try new method */ 562 if (method->userauth(ssh) != 0) { 563 debug2("we sent a %s packet, wait for reply", method->name); 564 break; 565 } else { 566 debug2("we did not send a packet, disable method"); 567 method->enabled = NULL; 568 } 569 } 570 } 571 572 /* ARGSUSED */ 573 static int 574 input_userauth_error(int type, u_int32_t seq, struct ssh *ssh) 575 { 576 fatal("%s: bad message during authentication: type %d", __func__, type); 577 return 0; 578 } 579 580 /* ARGSUSED */ 581 static int 582 input_userauth_banner(int type, u_int32_t seq, struct ssh *ssh) 583 { 584 char *msg = NULL; 585 size_t len; 586 int r; 587 588 debug3("%s", __func__); 589 if ((r = sshpkt_get_cstring(ssh, &msg, &len)) != 0 || 590 (r = sshpkt_get_cstring(ssh, NULL, NULL)) != 0) 591 goto out; 592 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) 593 fmprintf(stderr, "%s", msg); 594 r = 0; 595 out: 596 free(msg); 597 return r; 598 } 599 600 /* ARGSUSED */ 601 static int 602 input_userauth_success(int type, u_int32_t seq, struct ssh *ssh) 603 { 604 Authctxt *authctxt = ssh->authctxt; 605 606 if (authctxt == NULL) 607 fatal("%s: no authentication context", __func__); 608 free(authctxt->authlist); 609 authctxt->authlist = NULL; 610 if (authctxt->method != NULL && authctxt->method->cleanup != NULL) 611 authctxt->method->cleanup(ssh); 612 free(authctxt->methoddata); 613 authctxt->methoddata = NULL; 614 authctxt->success = 1; /* break out */ 615 return 0; 616 } 617 618 #if 0 619 static int 620 input_userauth_success_unexpected(int type, u_int32_t seq, struct ssh *ssh) 621 { 622 Authctxt *authctxt = ssh->authctxt; 623 624 if (authctxt == NULL) 625 fatal("%s: no authentication context", __func__); 626 627 fatal("Unexpected authentication success during %s.", 628 authctxt->method->name); 629 return 0; 630 } 631 #endif 632 633 /* ARGSUSED */ 634 static int 635 input_userauth_failure(int type, u_int32_t seq, struct ssh *ssh) 636 { 637 Authctxt *authctxt = ssh->authctxt; 638 char *authlist = NULL; 639 u_char partial; 640 641 if (authctxt == NULL) 642 fatal("input_userauth_failure: no authentication context"); 643 644 if (sshpkt_get_cstring(ssh, &authlist, NULL) != 0 || 645 sshpkt_get_u8(ssh, &partial) != 0 || 646 sshpkt_get_end(ssh) != 0) 647 goto out; 648 649 if (partial != 0) { 650 verbose("Authenticated with partial success."); 651 /* reset state */ 652 pubkey_reset(authctxt); 653 } 654 debug("Authentications that can continue: %s", authlist); 655 656 userauth(ssh, authlist); 657 authlist = NULL; 658 out: 659 free(authlist); 660 return 0; 661 } 662 663 /* 664 * Format an identity for logging including filename, key type, fingerprint 665 * and location (agent, etc.). Caller must free. 666 */ 667 static char * 668 format_identity(Identity *id) 669 { 670 char *fp = NULL, *ret = NULL; 671 const char *note = ""; 672 673 if (id->key != NULL) { 674 fp = sshkey_fingerprint(id->key, options.fingerprint_hash, 675 SSH_FP_DEFAULT); 676 } 677 if (id->key) { 678 if ((id->key->flags & SSHKEY_FLAG_EXT) != 0) 679 note = " token"; 680 else if (sshkey_is_sk(id->key)) 681 note = " authenticator"; 682 } 683 xasprintf(&ret, "%s %s%s%s%s%s%s", 684 id->filename, 685 id->key ? sshkey_type(id->key) : "", id->key ? " " : "", 686 fp ? fp : "", 687 id->userprovided ? " explicit" : "", note, 688 id->agent_fd != -1 ? " agent" : ""); 689 free(fp); 690 return ret; 691 } 692 693 /* ARGSUSED */ 694 static int 695 input_userauth_pk_ok(int type, u_int32_t seq, struct ssh *ssh) 696 { 697 Authctxt *authctxt = ssh->authctxt; 698 struct sshkey *key = NULL; 699 Identity *id = NULL; 700 int pktype, found = 0, sent = 0; 701 size_t blen; 702 char *pkalg = NULL, *fp = NULL, *ident = NULL; 703 u_char *pkblob = NULL; 704 int r; 705 706 if (authctxt == NULL) 707 fatal("input_userauth_pk_ok: no authentication context"); 708 709 if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 || 710 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 || 711 (r = sshpkt_get_end(ssh)) != 0) 712 goto done; 713 714 if ((pktype = sshkey_type_from_name(pkalg)) == KEY_UNSPEC) { 715 debug("%s: server sent unknown pkalg %s", __func__, pkalg); 716 goto done; 717 } 718 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) { 719 debug("no key from blob. pkalg %s: %s", pkalg, ssh_err(r)); 720 goto done; 721 } 722 if (key->type != pktype) { 723 error("input_userauth_pk_ok: type mismatch " 724 "for decoded key (received %d, expected %d)", 725 key->type, pktype); 726 goto done; 727 } 728 729 /* 730 * search keys in the reverse order, because last candidate has been 731 * moved to the end of the queue. this also avoids confusion by 732 * duplicate keys 733 */ 734 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) { 735 if (sshkey_equal(key, id->key)) { 736 found = 1; 737 break; 738 } 739 } 740 if (!found || id == NULL) { 741 fp = sshkey_fingerprint(key, options.fingerprint_hash, 742 SSH_FP_DEFAULT); 743 error("%s: server replied with unknown key: %s %s", __func__, 744 sshkey_type(key), fp == NULL ? "<ERROR>" : fp); 745 goto done; 746 } 747 ident = format_identity(id); 748 debug("Server accepts key: %s", ident); 749 sent = sign_and_send_pubkey(ssh, id); 750 r = 0; 751 done: 752 sshkey_free(key); 753 free(ident); 754 free(fp); 755 free(pkalg); 756 free(pkblob); 757 758 /* try another method if we did not send a packet */ 759 if (r == 0 && sent == 0) 760 userauth(ssh, NULL); 761 return r; 762 } 763 764 #ifdef GSSAPI 765 static int 766 userauth_gssapi(struct ssh *ssh) 767 { 768 Authctxt *authctxt = (Authctxt *)ssh->authctxt; 769 Gssctxt *gssctxt = NULL; 770 OM_uint32 min; 771 int r, ok = 0; 772 gss_OID mech = NULL; 773 774 /* Try one GSSAPI method at a time, rather than sending them all at 775 * once. */ 776 777 if (authctxt->gss_supported_mechs == NULL) 778 gss_indicate_mechs(&min, &authctxt->gss_supported_mechs); 779 780 /* Check to see whether the mechanism is usable before we offer it */ 781 while (authctxt->mech_tried < authctxt->gss_supported_mechs->count && 782 !ok) { 783 mech = &authctxt->gss_supported_mechs-> 784 elements[authctxt->mech_tried]; 785 /* My DER encoding requires length<128 */ 786 if (mech->length < 128 && ssh_gssapi_check_mechanism(&gssctxt, 787 mech, authctxt->host)) { 788 ok = 1; /* Mechanism works */ 789 } else { 790 authctxt->mech_tried++; 791 } 792 } 793 794 if (!ok || mech == NULL) 795 return 0; 796 797 authctxt->methoddata=(void *)gssctxt; 798 799 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 800 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || 801 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || 802 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || 803 (r = sshpkt_put_u32(ssh, 1)) != 0 || 804 (r = sshpkt_put_u32(ssh, (mech->length) + 2)) != 0 || 805 (r = sshpkt_put_u8(ssh, SSH_GSS_OIDTYPE)) != 0 || 806 (r = sshpkt_put_u8(ssh, mech->length)) != 0 || 807 (r = sshpkt_put(ssh, mech->elements, mech->length)) != 0 || 808 (r = sshpkt_send(ssh)) != 0) 809 fatal("%s: %s", __func__, ssh_err(r)); 810 811 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response); 812 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token); 813 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error); 814 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok); 815 816 authctxt->mech_tried++; /* Move along to next candidate */ 817 818 return 1; 819 } 820 821 static void 822 userauth_gssapi_cleanup(struct ssh *ssh) 823 { 824 Authctxt *authctxt = (Authctxt *)ssh->authctxt; 825 Gssctxt *gssctxt = (Gssctxt *)authctxt->methoddata; 826 827 ssh_gssapi_delete_ctx(&gssctxt); 828 authctxt->methoddata = NULL; 829 830 free(authctxt->gss_supported_mechs); 831 authctxt->gss_supported_mechs = NULL; 832 } 833 834 static OM_uint32 835 process_gssapi_token(struct ssh *ssh, gss_buffer_t recv_tok) 836 { 837 Authctxt *authctxt = ssh->authctxt; 838 Gssctxt *gssctxt = authctxt->methoddata; 839 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; 840 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER; 841 gss_buffer_desc gssbuf; 842 OM_uint32 status, ms, flags; 843 int r; 844 845 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, 846 recv_tok, &send_tok, &flags); 847 848 if (send_tok.length > 0) { 849 u_char type = GSS_ERROR(status) ? 850 SSH2_MSG_USERAUTH_GSSAPI_ERRTOK : 851 SSH2_MSG_USERAUTH_GSSAPI_TOKEN; 852 853 if ((r = sshpkt_start(ssh, type)) != 0 || 854 (r = sshpkt_put_string(ssh, send_tok.value, 855 send_tok.length)) != 0 || 856 (r = sshpkt_send(ssh)) != 0) 857 fatal("%s: %s", __func__, ssh_err(r)); 858 859 gss_release_buffer(&ms, &send_tok); 860 } 861 862 if (status == GSS_S_COMPLETE) { 863 /* send either complete or MIC, depending on mechanism */ 864 if (!(flags & GSS_C_INTEG_FLAG)) { 865 if ((r = sshpkt_start(ssh, 866 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE)) != 0 || 867 (r = sshpkt_send(ssh)) != 0) 868 fatal("%s: %s", __func__, ssh_err(r)); 869 } else { 870 struct sshbuf *b; 871 872 if ((b = sshbuf_new()) == NULL) 873 fatal("%s: sshbuf_new failed", __func__); 874 ssh_gssapi_buildmic(b, authctxt->server_user, 875 authctxt->service, "gssapi-with-mic"); 876 877 if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL) 878 fatal("%s: sshbuf_mutable_ptr failed", __func__); 879 gssbuf.length = sshbuf_len(b); 880 881 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic); 882 883 if (!GSS_ERROR(status)) { 884 if ((r = sshpkt_start(ssh, 885 SSH2_MSG_USERAUTH_GSSAPI_MIC)) != 0 || 886 (r = sshpkt_put_string(ssh, mic.value, 887 mic.length)) != 0 || 888 (r = sshpkt_send(ssh)) != 0) 889 fatal("%s: %s", __func__, ssh_err(r)); 890 } 891 892 sshbuf_free(b); 893 gss_release_buffer(&ms, &mic); 894 } 895 } 896 897 return status; 898 } 899 900 /* ARGSUSED */ 901 static int 902 input_gssapi_response(int type, u_int32_t plen, struct ssh *ssh) 903 { 904 Authctxt *authctxt = ssh->authctxt; 905 Gssctxt *gssctxt; 906 size_t oidlen; 907 u_char *oidv = NULL; 908 int r; 909 910 if (authctxt == NULL) 911 fatal("input_gssapi_response: no authentication context"); 912 gssctxt = authctxt->methoddata; 913 914 /* Setup our OID */ 915 if ((r = sshpkt_get_string(ssh, &oidv, &oidlen)) != 0) 916 goto done; 917 918 if (oidlen <= 2 || 919 oidv[0] != SSH_GSS_OIDTYPE || 920 oidv[1] != oidlen - 2) { 921 debug("Badly encoded mechanism OID received"); 922 userauth(ssh, NULL); 923 goto ok; 924 } 925 926 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2)) 927 fatal("Server returned different OID than expected"); 928 929 if ((r = sshpkt_get_end(ssh)) != 0) 930 goto done; 931 932 if (GSS_ERROR(process_gssapi_token(ssh, GSS_C_NO_BUFFER))) { 933 /* Start again with next method on list */ 934 debug("Trying to start again"); 935 userauth(ssh, NULL); 936 goto ok; 937 } 938 ok: 939 r = 0; 940 done: 941 free(oidv); 942 return r; 943 } 944 945 /* ARGSUSED */ 946 static int 947 input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh) 948 { 949 Authctxt *authctxt = ssh->authctxt; 950 gss_buffer_desc recv_tok; 951 u_char *p = NULL; 952 size_t len; 953 OM_uint32 status; 954 int r; 955 956 if (authctxt == NULL) 957 fatal("input_gssapi_response: no authentication context"); 958 959 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 || 960 (r = sshpkt_get_end(ssh)) != 0) 961 goto out; 962 963 recv_tok.value = p; 964 recv_tok.length = len; 965 status = process_gssapi_token(ssh, &recv_tok); 966 967 /* Start again with the next method in the list */ 968 if (GSS_ERROR(status)) { 969 userauth(ssh, NULL); 970 /* ok */ 971 } 972 r = 0; 973 out: 974 free(p); 975 return r; 976 } 977 978 /* ARGSUSED */ 979 static int 980 input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh) 981 { 982 Authctxt *authctxt = ssh->authctxt; 983 Gssctxt *gssctxt; 984 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; 985 gss_buffer_desc recv_tok; 986 OM_uint32 ms; 987 u_char *p = NULL; 988 size_t len; 989 int r; 990 991 if (authctxt == NULL) 992 fatal("input_gssapi_response: no authentication context"); 993 gssctxt = authctxt->methoddata; 994 995 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 || 996 (r = sshpkt_get_end(ssh)) != 0) { 997 free(p); 998 return r; 999 } 1000 1001 /* Stick it into GSSAPI and see what it says */ 1002 recv_tok.value = p; 1003 recv_tok.length = len; 1004 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, 1005 &recv_tok, &send_tok, NULL); 1006 free(p); 1007 gss_release_buffer(&ms, &send_tok); 1008 1009 /* Server will be returning a failed packet after this one */ 1010 return 0; 1011 } 1012 1013 /* ARGSUSED */ 1014 static int 1015 input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh) 1016 { 1017 char *msg = NULL; 1018 char *lang = NULL; 1019 int r; 1020 1021 if ((r = sshpkt_get_u32(ssh, NULL)) != 0 || /* maj */ 1022 (r = sshpkt_get_u32(ssh, NULL)) != 0 || /* min */ 1023 (r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 || 1024 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0) 1025 goto out; 1026 r = sshpkt_get_end(ssh); 1027 debug("Server GSSAPI Error:\n%s", msg); 1028 out: 1029 free(msg); 1030 free(lang); 1031 return r; 1032 } 1033 #endif /* GSSAPI */ 1034 1035 static int 1036 userauth_none(struct ssh *ssh) 1037 { 1038 Authctxt *authctxt = (Authctxt *)ssh->authctxt; 1039 int r; 1040 1041 /* initial userauth request */ 1042 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 1043 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || 1044 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || 1045 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || 1046 (r = sshpkt_send(ssh)) != 0) 1047 fatal("%s: %s", __func__, ssh_err(r)); 1048 return 1; 1049 } 1050 1051 static int 1052 userauth_passwd(struct ssh *ssh) 1053 { 1054 Authctxt *authctxt = (Authctxt *)ssh->authctxt; 1055 char *password, *prompt = NULL; 1056 const char *host = options.host_key_alias ? options.host_key_alias : 1057 authctxt->host; 1058 int r; 1059 1060 if (authctxt->attempt_passwd++ >= options.number_of_password_prompts) 1061 return 0; 1062 1063 if (authctxt->attempt_passwd != 1) 1064 error("Permission denied, please try again."); 1065 1066 xasprintf(&prompt, "%s@%s's password: ", authctxt->server_user, host); 1067 password = read_passphrase(prompt, 0); 1068 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 1069 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || 1070 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || 1071 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || 1072 (r = sshpkt_put_u8(ssh, 0)) != 0 || 1073 (r = sshpkt_put_cstring(ssh, password)) != 0 || 1074 (r = sshpkt_add_padding(ssh, 64)) != 0 || 1075 (r = sshpkt_send(ssh)) != 0) 1076 fatal("%s: %s", __func__, ssh_err(r)); 1077 1078 free(prompt); 1079 if (password != NULL) 1080 freezero(password, strlen(password)); 1081 1082 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, 1083 &input_userauth_passwd_changereq); 1084 1085 return 1; 1086 } 1087 1088 /* 1089 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST 1090 */ 1091 /* ARGSUSED */ 1092 static int 1093 input_userauth_passwd_changereq(int type, u_int32_t seqnr, struct ssh *ssh) 1094 { 1095 Authctxt *authctxt = ssh->authctxt; 1096 char *info = NULL, *lang = NULL, *password = NULL, *retype = NULL; 1097 char prompt[256]; 1098 const char *host; 1099 int r; 1100 1101 debug2("input_userauth_passwd_changereq"); 1102 1103 if (authctxt == NULL) 1104 fatal("input_userauth_passwd_changereq: " 1105 "no authentication context"); 1106 host = options.host_key_alias ? options.host_key_alias : authctxt->host; 1107 1108 if ((r = sshpkt_get_cstring(ssh, &info, NULL)) != 0 || 1109 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0) 1110 goto out; 1111 if (strlen(info) > 0) 1112 logit("%s", info); 1113 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 1114 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || 1115 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || 1116 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || 1117 (r = sshpkt_put_u8(ssh, 1)) != 0) /* additional info */ 1118 goto out; 1119 1120 snprintf(prompt, sizeof(prompt), 1121 "Enter %.30s@%.128s's old password: ", 1122 authctxt->server_user, host); 1123 password = read_passphrase(prompt, 0); 1124 if ((r = sshpkt_put_cstring(ssh, password)) != 0) 1125 goto out; 1126 1127 freezero(password, strlen(password)); 1128 password = NULL; 1129 while (password == NULL) { 1130 snprintf(prompt, sizeof(prompt), 1131 "Enter %.30s@%.128s's new password: ", 1132 authctxt->server_user, host); 1133 password = read_passphrase(prompt, RP_ALLOW_EOF); 1134 if (password == NULL) { 1135 /* bail out */ 1136 r = 0; 1137 goto out; 1138 } 1139 snprintf(prompt, sizeof(prompt), 1140 "Retype %.30s@%.128s's new password: ", 1141 authctxt->server_user, host); 1142 retype = read_passphrase(prompt, 0); 1143 if (strcmp(password, retype) != 0) { 1144 freezero(password, strlen(password)); 1145 logit("Mismatch; try again, EOF to quit."); 1146 password = NULL; 1147 } 1148 freezero(retype, strlen(retype)); 1149 } 1150 if ((r = sshpkt_put_cstring(ssh, password)) != 0 || 1151 (r = sshpkt_add_padding(ssh, 64)) != 0 || 1152 (r = sshpkt_send(ssh)) != 0) 1153 goto out; 1154 1155 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, 1156 &input_userauth_passwd_changereq); 1157 r = 0; 1158 out: 1159 if (password) 1160 freezero(password, strlen(password)); 1161 free(info); 1162 free(lang); 1163 return r; 1164 } 1165 1166 /* 1167 * Select an algorithm for publickey signatures. 1168 * Returns algorithm (caller must free) or NULL if no mutual algorithm found. 1169 * 1170 * Call with ssh==NULL to ignore server-sig-algs extension list and 1171 * only attempt with the key's base signature type. 1172 */ 1173 static char * 1174 key_sig_algorithm(struct ssh *ssh, const struct sshkey *key) 1175 { 1176 char *allowed, *oallowed, *cp, *tmp, *alg = NULL; 1177 1178 /* 1179 * The signature algorithm will only differ from the key algorithm 1180 * for RSA keys/certs and when the server advertises support for 1181 * newer (SHA2) algorithms. 1182 */ 1183 if (ssh == NULL || ssh->kex->server_sig_algs == NULL || 1184 (key->type != KEY_RSA && key->type != KEY_RSA_CERT) || 1185 (key->type == KEY_RSA_CERT && (datafellows & SSH_BUG_SIGTYPE))) { 1186 /* Filter base key signature alg against our configuration */ 1187 return match_list(sshkey_ssh_name(key), 1188 options.pubkey_key_types, NULL); 1189 } 1190 1191 /* 1192 * For RSA keys/certs, since these might have a different sig type: 1193 * find the first entry in PubkeyAcceptedKeyTypes of the right type 1194 * that also appears in the supported signature algorithms list from 1195 * the server. 1196 */ 1197 oallowed = allowed = xstrdup(options.pubkey_key_types); 1198 while ((cp = strsep(&allowed, ",")) != NULL) { 1199 if (sshkey_type_from_name(cp) != key->type) 1200 continue; 1201 tmp = match_list(sshkey_sigalg_by_name(cp), 1202 ssh->kex->server_sig_algs, NULL); 1203 if (tmp != NULL) 1204 alg = xstrdup(cp); 1205 free(tmp); 1206 if (alg != NULL) 1207 break; 1208 } 1209 free(oallowed); 1210 return alg; 1211 } 1212 1213 static int 1214 identity_sign(struct identity *id, u_char **sigp, size_t *lenp, 1215 const u_char *data, size_t datalen, u_int compat, const char *alg) 1216 { 1217 struct sshkey *sign_key = NULL, *prv = NULL; 1218 int r = SSH_ERR_INTERNAL_ERROR; 1219 struct notifier_ctx *notifier = NULL; 1220 char *fp = NULL, *pin = NULL, *prompt = NULL; 1221 1222 *sigp = NULL; 1223 *lenp = 0; 1224 1225 /* The agent supports this key. */ 1226 if (id->key != NULL && id->agent_fd != -1) { 1227 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp, 1228 data, datalen, alg, compat); 1229 } 1230 1231 /* 1232 * We have already loaded the private key or the private key is 1233 * stored in external hardware. 1234 */ 1235 if (id->key != NULL && 1236 (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))) { 1237 sign_key = id->key; 1238 } else { 1239 /* Load the private key from the file. */ 1240 if ((prv = load_identity_file(id)) == NULL) 1241 return SSH_ERR_KEY_NOT_FOUND; 1242 if (id->key != NULL && !sshkey_equal_public(prv, id->key)) { 1243 error("%s: private key %s contents do not match public", 1244 __func__, id->filename); 1245 r = SSH_ERR_KEY_NOT_FOUND; 1246 goto out; 1247 } 1248 sign_key = prv; 1249 if (sshkey_is_sk(sign_key)) { 1250 if ((sign_key->sk_flags & 1251 SSH_SK_USER_VERIFICATION_REQD)) { 1252 xasprintf(&prompt, "Enter PIN for %s key %s: ", 1253 sshkey_type(sign_key), id->filename); 1254 pin = read_passphrase(prompt, 0); 1255 } 1256 if ((sign_key->sk_flags & SSH_SK_USER_PRESENCE_REQD)) { 1257 /* XXX should batch mode just skip these? */ 1258 if ((fp = sshkey_fingerprint(sign_key, 1259 options.fingerprint_hash, 1260 SSH_FP_DEFAULT)) == NULL) 1261 fatal("%s: fingerprint", __func__); 1262 notifier = notify_start(options.batch_mode, 1263 "Confirm user presence for key %s %s", 1264 sshkey_type(sign_key), fp); 1265 free(fp); 1266 } 1267 } 1268 } 1269 if ((r = sshkey_sign(sign_key, sigp, lenp, data, datalen, 1270 alg, options.sk_provider, pin, compat)) != 0) { 1271 debug("%s: sshkey_sign: %s", __func__, ssh_err(r)); 1272 goto out; 1273 } 1274 /* 1275 * PKCS#11 tokens may not support all signature algorithms, 1276 * so check what we get back. 1277 */ 1278 if ((r = sshkey_check_sigtype(*sigp, *lenp, alg)) != 0) { 1279 debug("%s: sshkey_check_sigtype: %s", __func__, ssh_err(r)); 1280 goto out; 1281 } 1282 /* success */ 1283 r = 0; 1284 out: 1285 free(prompt); 1286 if (pin != NULL) 1287 freezero(pin, strlen(pin)); 1288 notify_complete(notifier); 1289 sshkey_free(prv); 1290 return r; 1291 } 1292 1293 static int 1294 id_filename_matches(Identity *id, Identity *private_id) 1295 { 1296 const char *suffixes[] = { ".pub", "-cert.pub", NULL }; 1297 size_t len = strlen(id->filename), plen = strlen(private_id->filename); 1298 size_t i, slen; 1299 1300 if (strcmp(id->filename, private_id->filename) == 0) 1301 return 1; 1302 for (i = 0; suffixes[i]; i++) { 1303 slen = strlen(suffixes[i]); 1304 if (len > slen && plen == len - slen && 1305 strcmp(id->filename + (len - slen), suffixes[i]) == 0 && 1306 memcmp(id->filename, private_id->filename, plen) == 0) 1307 return 1; 1308 } 1309 return 0; 1310 } 1311 1312 static int 1313 sign_and_send_pubkey(struct ssh *ssh, Identity *id) 1314 { 1315 Authctxt *authctxt = (Authctxt *)ssh->authctxt; 1316 struct sshbuf *b = NULL; 1317 Identity *private_id, *sign_id = NULL; 1318 u_char *signature = NULL; 1319 size_t slen = 0, skip = 0; 1320 int r, fallback_sigtype, sent = 0; 1321 char *alg = NULL, *fp = NULL; 1322 const char *loc = ""; 1323 1324 if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash, 1325 SSH_FP_DEFAULT)) == NULL) 1326 return 0; 1327 1328 debug3("%s: %s %s", __func__, sshkey_type(id->key), fp); 1329 1330 /* 1331 * If the key is an certificate, try to find a matching private key 1332 * and use it to complete the signature. 1333 * If no such private key exists, fall back to trying the certificate 1334 * key itself in case it has a private half already loaded. 1335 * This will try to set sign_id to the private key that will perform 1336 * the signature. 1337 */ 1338 if (sshkey_is_cert(id->key)) { 1339 TAILQ_FOREACH(private_id, &authctxt->keys, next) { 1340 if (sshkey_equal_public(id->key, private_id->key) && 1341 id->key->type != private_id->key->type) { 1342 sign_id = private_id; 1343 break; 1344 } 1345 } 1346 /* 1347 * Exact key matches are preferred, but also allow 1348 * filename matches for non-PKCS#11/agent keys that 1349 * didn't load public keys. This supports the case 1350 * of keeping just a private key file and public 1351 * certificate on disk. 1352 */ 1353 if (sign_id == NULL && 1354 !id->isprivate && id->agent_fd == -1 && 1355 (id->key->flags & SSHKEY_FLAG_EXT) == 0) { 1356 TAILQ_FOREACH(private_id, &authctxt->keys, next) { 1357 if (private_id->key == NULL && 1358 id_filename_matches(id, private_id)) { 1359 sign_id = private_id; 1360 break; 1361 } 1362 } 1363 } 1364 if (sign_id != NULL) { 1365 debug2("%s: using private key \"%s\"%s for " 1366 "certificate", __func__, id->filename, 1367 id->agent_fd != -1 ? " from agent" : ""); 1368 } else { 1369 debug("%s: no separate private key for certificate " 1370 "\"%s\"", __func__, id->filename); 1371 } 1372 } 1373 1374 /* 1375 * If the above didn't select another identity to do the signing 1376 * then default to the one we started with. 1377 */ 1378 if (sign_id == NULL) 1379 sign_id = id; 1380 1381 /* assemble and sign data */ 1382 for (fallback_sigtype = 0; fallback_sigtype <= 1; fallback_sigtype++) { 1383 free(alg); 1384 slen = 0; 1385 signature = NULL; 1386 if ((alg = key_sig_algorithm(fallback_sigtype ? NULL : ssh, 1387 id->key)) == NULL) { 1388 error("%s: no mutual signature supported", __func__); 1389 goto out; 1390 } 1391 debug3("%s: signing using %s %s", __func__, alg, fp); 1392 1393 sshbuf_free(b); 1394 if ((b = sshbuf_new()) == NULL) 1395 fatal("%s: sshbuf_new failed", __func__); 1396 if (datafellows & SSH_OLD_SESSIONID) { 1397 if ((r = sshbuf_put(b, session_id2, 1398 session_id2_len)) != 0) { 1399 fatal("%s: sshbuf_put: %s", 1400 __func__, ssh_err(r)); 1401 } 1402 } else { 1403 if ((r = sshbuf_put_string(b, session_id2, 1404 session_id2_len)) != 0) { 1405 fatal("%s: sshbuf_put_string: %s", 1406 __func__, ssh_err(r)); 1407 } 1408 } 1409 skip = sshbuf_len(b); 1410 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 1411 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 || 1412 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 || 1413 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 || 1414 (r = sshbuf_put_u8(b, 1)) != 0 || 1415 (r = sshbuf_put_cstring(b, alg)) != 0 || 1416 (r = sshkey_puts(id->key, b)) != 0) { 1417 fatal("%s: assemble signed data: %s", 1418 __func__, ssh_err(r)); 1419 } 1420 1421 /* generate signature */ 1422 r = identity_sign(sign_id, &signature, &slen, 1423 sshbuf_ptr(b), sshbuf_len(b), datafellows, alg); 1424 if (r == 0) 1425 break; 1426 else if (r == SSH_ERR_KEY_NOT_FOUND) 1427 goto out; /* soft failure */ 1428 else if (r == SSH_ERR_SIGN_ALG_UNSUPPORTED && 1429 !fallback_sigtype) { 1430 if (sign_id->agent_fd != -1) 1431 loc = "agent "; 1432 else if ((sign_id->key->flags & SSHKEY_FLAG_EXT) != 0) 1433 loc = "token "; 1434 logit("%skey %s %s returned incorrect signature type", 1435 loc, sshkey_type(id->key), fp); 1436 continue; 1437 } 1438 error("%s: signing failed for %s \"%s\"%s: %s", __func__, 1439 sshkey_type(sign_id->key), sign_id->filename, 1440 id->agent_fd != -1 ? " from agent" : "", ssh_err(r)); 1441 goto out; 1442 } 1443 if (slen == 0 || signature == NULL) /* shouldn't happen */ 1444 fatal("%s: no signature", __func__); 1445 1446 /* append signature */ 1447 if ((r = sshbuf_put_string(b, signature, slen)) != 0) 1448 fatal("%s: append signature: %s", __func__, ssh_err(r)); 1449 1450 #ifdef DEBUG_PK 1451 sshbuf_dump(b, stderr); 1452 #endif 1453 /* skip session id and packet type */ 1454 if ((r = sshbuf_consume(b, skip + 1)) != 0) 1455 fatal("%s: consume: %s", __func__, ssh_err(r)); 1456 1457 /* put remaining data from buffer into packet */ 1458 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 1459 (r = sshpkt_putb(ssh, b)) != 0 || 1460 (r = sshpkt_send(ssh)) != 0) 1461 fatal("%s: enqueue request: %s", __func__, ssh_err(r)); 1462 1463 /* success */ 1464 sent = 1; 1465 1466 out: 1467 free(fp); 1468 free(alg); 1469 sshbuf_free(b); 1470 freezero(signature, slen); 1471 return sent; 1472 } 1473 1474 static int 1475 send_pubkey_test(struct ssh *ssh, Identity *id) 1476 { 1477 Authctxt *authctxt = (Authctxt *)ssh->authctxt; 1478 u_char *blob = NULL; 1479 char *alg = NULL; 1480 size_t bloblen; 1481 u_int have_sig = 0; 1482 int sent = 0, r; 1483 1484 if ((alg = key_sig_algorithm(ssh, id->key)) == NULL) { 1485 debug("%s: no mutual signature algorithm", __func__); 1486 goto out; 1487 } 1488 1489 if ((r = sshkey_to_blob(id->key, &blob, &bloblen)) != 0) { 1490 /* we cannot handle this key */ 1491 debug3("%s: cannot handle key", __func__); 1492 goto out; 1493 } 1494 /* register callback for USERAUTH_PK_OK message */ 1495 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok); 1496 1497 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 1498 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || 1499 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || 1500 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || 1501 (r = sshpkt_put_u8(ssh, have_sig)) != 0 || 1502 (r = sshpkt_put_cstring(ssh, alg)) != 0 || 1503 (r = sshpkt_put_string(ssh, blob, bloblen)) != 0 || 1504 (r = sshpkt_send(ssh)) != 0) 1505 fatal("%s: %s", __func__, ssh_err(r)); 1506 sent = 1; 1507 1508 out: 1509 free(alg); 1510 free(blob); 1511 return sent; 1512 } 1513 1514 static struct sshkey * 1515 load_identity_file(Identity *id) 1516 { 1517 struct sshkey *private = NULL; 1518 char prompt[300], *passphrase, *comment; 1519 int r, quit = 0, i; 1520 struct stat st; 1521 1522 if (stat(id->filename, &st) == -1) { 1523 (id->userprovided ? logit : debug3)("no such identity: %s: %s", 1524 id->filename, strerror(errno)); 1525 return NULL; 1526 } 1527 snprintf(prompt, sizeof prompt, 1528 "Enter passphrase for key '%.100s': ", id->filename); 1529 for (i = 0; i <= options.number_of_password_prompts; i++) { 1530 if (i == 0) 1531 passphrase = ""; 1532 else { 1533 passphrase = read_passphrase(prompt, 0); 1534 if (*passphrase == '\0') { 1535 debug2("no passphrase given, try next key"); 1536 free(passphrase); 1537 break; 1538 } 1539 } 1540 switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename, 1541 passphrase, &private, &comment))) { 1542 case 0: 1543 break; 1544 case SSH_ERR_KEY_WRONG_PASSPHRASE: 1545 if (options.batch_mode) { 1546 quit = 1; 1547 break; 1548 } 1549 if (i != 0) 1550 debug2("bad passphrase given, try again..."); 1551 break; 1552 case SSH_ERR_SYSTEM_ERROR: 1553 if (errno == ENOENT) { 1554 debug2("Load key \"%s\": %s", 1555 id->filename, ssh_err(r)); 1556 quit = 1; 1557 break; 1558 } 1559 /* FALLTHROUGH */ 1560 default: 1561 error("Load key \"%s\": %s", id->filename, ssh_err(r)); 1562 quit = 1; 1563 break; 1564 } 1565 if (private != NULL && sshkey_is_sk(private) && 1566 options.sk_provider == NULL) { 1567 debug("key \"%s\" is an authenticator-hosted key, " 1568 "but no provider specified", id->filename); 1569 sshkey_free(private); 1570 private = NULL; 1571 quit = 1; 1572 } 1573 if (!quit && private != NULL && id->agent_fd == -1 && 1574 !(id->key && id->isprivate)) 1575 maybe_add_key_to_agent(id->filename, private, comment, 1576 passphrase); 1577 if (i > 0) 1578 freezero(passphrase, strlen(passphrase)); 1579 free(comment); 1580 if (private != NULL || quit) 1581 break; 1582 } 1583 return private; 1584 } 1585 1586 static int 1587 key_type_allowed_by_config(struct sshkey *key) 1588 { 1589 if (match_pattern_list(sshkey_ssh_name(key), 1590 options.pubkey_key_types, 0) == 1) 1591 return 1; 1592 1593 /* RSA keys/certs might be allowed by alternate signature types */ 1594 switch (key->type) { 1595 case KEY_RSA: 1596 if (match_pattern_list("rsa-sha2-512", 1597 options.pubkey_key_types, 0) == 1) 1598 return 1; 1599 if (match_pattern_list("rsa-sha2-256", 1600 options.pubkey_key_types, 0) == 1) 1601 return 1; 1602 break; 1603 case KEY_RSA_CERT: 1604 if (match_pattern_list("rsa-sha2-512-cert-v01@openssh.com", 1605 options.pubkey_key_types, 0) == 1) 1606 return 1; 1607 if (match_pattern_list("rsa-sha2-256-cert-v01@openssh.com", 1608 options.pubkey_key_types, 0) == 1) 1609 return 1; 1610 break; 1611 } 1612 return 0; 1613 } 1614 1615 1616 /* 1617 * try keys in the following order: 1618 * 1. certificates listed in the config file 1619 * 2. other input certificates 1620 * 3. agent keys that are found in the config file 1621 * 4. other agent keys 1622 * 5. keys that are only listed in the config file 1623 */ 1624 static void 1625 pubkey_prepare(Authctxt *authctxt) 1626 { 1627 struct identity *id, *id2, *tmp; 1628 struct idlist agent, files, *preferred; 1629 struct sshkey *key; 1630 int agent_fd = -1, i, r, found; 1631 size_t j; 1632 struct ssh_identitylist *idlist; 1633 char *ident; 1634 1635 TAILQ_INIT(&agent); /* keys from the agent */ 1636 TAILQ_INIT(&files); /* keys from the config file */ 1637 preferred = &authctxt->keys; 1638 TAILQ_INIT(preferred); /* preferred order of keys */ 1639 1640 /* list of keys stored in the filesystem and PKCS#11 */ 1641 for (i = 0; i < options.num_identity_files; i++) { 1642 key = options.identity_keys[i]; 1643 if (key && key->cert && 1644 key->cert->type != SSH2_CERT_TYPE_USER) { 1645 debug("%s: ignoring certificate %s: not a user " 1646 "certificate", __func__, 1647 options.identity_files[i]); 1648 continue; 1649 } 1650 if (key && sshkey_is_sk(key) && options.sk_provider == NULL) { 1651 debug("%s: ignoring authenticator-hosted key %s as no " 1652 "SecurityKeyProvider has been specified", 1653 __func__, options.identity_files[i]); 1654 continue; 1655 } 1656 options.identity_keys[i] = NULL; 1657 id = xcalloc(1, sizeof(*id)); 1658 id->agent_fd = -1; 1659 id->key = key; 1660 id->filename = xstrdup(options.identity_files[i]); 1661 id->userprovided = options.identity_file_userprovided[i]; 1662 TAILQ_INSERT_TAIL(&files, id, next); 1663 } 1664 /* list of certificates specified by user */ 1665 for (i = 0; i < options.num_certificate_files; i++) { 1666 key = options.certificates[i]; 1667 if (!sshkey_is_cert(key) || key->cert == NULL || 1668 key->cert->type != SSH2_CERT_TYPE_USER) { 1669 debug("%s: ignoring certificate %s: not a user " 1670 "certificate", __func__, 1671 options.identity_files[i]); 1672 continue; 1673 } 1674 if (key && sshkey_is_sk(key) && options.sk_provider == NULL) { 1675 debug("%s: ignoring authenticator-hosted key " 1676 "certificate %s as no " 1677 "SecurityKeyProvider has been specified", 1678 __func__, options.identity_files[i]); 1679 continue; 1680 } 1681 id = xcalloc(1, sizeof(*id)); 1682 id->agent_fd = -1; 1683 id->key = key; 1684 id->filename = xstrdup(options.certificate_files[i]); 1685 id->userprovided = options.certificate_file_userprovided[i]; 1686 TAILQ_INSERT_TAIL(preferred, id, next); 1687 } 1688 /* list of keys supported by the agent */ 1689 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) { 1690 if (r != SSH_ERR_AGENT_NOT_PRESENT) 1691 debug("%s: ssh_get_authentication_socket: %s", 1692 __func__, ssh_err(r)); 1693 } else if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) { 1694 if (r != SSH_ERR_AGENT_NO_IDENTITIES) 1695 debug("%s: ssh_fetch_identitylist: %s", 1696 __func__, ssh_err(r)); 1697 close(agent_fd); 1698 } else { 1699 for (j = 0; j < idlist->nkeys; j++) { 1700 found = 0; 1701 TAILQ_FOREACH(id, &files, next) { 1702 /* 1703 * agent keys from the config file are 1704 * preferred 1705 */ 1706 if (sshkey_equal(idlist->keys[j], id->key)) { 1707 TAILQ_REMOVE(&files, id, next); 1708 TAILQ_INSERT_TAIL(preferred, id, next); 1709 id->agent_fd = agent_fd; 1710 found = 1; 1711 break; 1712 } 1713 } 1714 if (!found && !options.identities_only) { 1715 id = xcalloc(1, sizeof(*id)); 1716 /* XXX "steals" key/comment from idlist */ 1717 id->key = idlist->keys[j]; 1718 id->filename = idlist->comments[j]; 1719 idlist->keys[j] = NULL; 1720 idlist->comments[j] = NULL; 1721 id->agent_fd = agent_fd; 1722 TAILQ_INSERT_TAIL(&agent, id, next); 1723 } 1724 } 1725 ssh_free_identitylist(idlist); 1726 /* append remaining agent keys */ 1727 TAILQ_CONCAT(preferred, &agent, next); 1728 authctxt->agent_fd = agent_fd; 1729 } 1730 /* Prefer PKCS11 keys that are explicitly listed */ 1731 TAILQ_FOREACH_SAFE(id, &files, next, tmp) { 1732 if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0) 1733 continue; 1734 found = 0; 1735 TAILQ_FOREACH(id2, &files, next) { 1736 if (id2->key == NULL || 1737 (id2->key->flags & SSHKEY_FLAG_EXT) != 0) 1738 continue; 1739 if (sshkey_equal(id->key, id2->key)) { 1740 TAILQ_REMOVE(&files, id, next); 1741 TAILQ_INSERT_TAIL(preferred, id, next); 1742 found = 1; 1743 break; 1744 } 1745 } 1746 /* If IdentitiesOnly set and key not found then don't use it */ 1747 if (!found && options.identities_only) { 1748 TAILQ_REMOVE(&files, id, next); 1749 freezero(id, sizeof(*id)); 1750 } 1751 } 1752 /* append remaining keys from the config file */ 1753 TAILQ_CONCAT(preferred, &files, next); 1754 /* finally, filter by PubkeyAcceptedKeyTypes */ 1755 TAILQ_FOREACH_SAFE(id, preferred, next, id2) { 1756 if (id->key != NULL && !key_type_allowed_by_config(id->key)) { 1757 debug("Skipping %s key %s - " 1758 "not in PubkeyAcceptedKeyTypes", 1759 sshkey_ssh_name(id->key), id->filename); 1760 TAILQ_REMOVE(preferred, id, next); 1761 sshkey_free(id->key); 1762 free(id->filename); 1763 memset(id, 0, sizeof(*id)); 1764 continue; 1765 } 1766 } 1767 /* List the keys we plan on using */ 1768 TAILQ_FOREACH_SAFE(id, preferred, next, id2) { 1769 ident = format_identity(id); 1770 debug("Will attempt key: %s", ident); 1771 free(ident); 1772 } 1773 debug2("%s: done", __func__); 1774 } 1775 1776 static void 1777 pubkey_cleanup(struct ssh *ssh) 1778 { 1779 Authctxt *authctxt = (Authctxt *)ssh->authctxt; 1780 Identity *id; 1781 1782 if (authctxt->agent_fd != -1) { 1783 ssh_close_authentication_socket(authctxt->agent_fd); 1784 authctxt->agent_fd = -1; 1785 } 1786 for (id = TAILQ_FIRST(&authctxt->keys); id; 1787 id = TAILQ_FIRST(&authctxt->keys)) { 1788 TAILQ_REMOVE(&authctxt->keys, id, next); 1789 sshkey_free(id->key); 1790 free(id->filename); 1791 free(id); 1792 } 1793 } 1794 1795 static void 1796 pubkey_reset(Authctxt *authctxt) 1797 { 1798 Identity *id; 1799 1800 TAILQ_FOREACH(id, &authctxt->keys, next) 1801 id->tried = 0; 1802 } 1803 1804 static int 1805 try_identity(Identity *id) 1806 { 1807 if (!id->key) 1808 return (0); 1809 if (sshkey_type_plain(id->key->type) == KEY_RSA && 1810 (datafellows & SSH_BUG_RSASIGMD5) != 0) { 1811 debug("Skipped %s key %s for RSA/MD5 server", 1812 sshkey_type(id->key), id->filename); 1813 return (0); 1814 } 1815 return 1; 1816 } 1817 1818 static int 1819 userauth_pubkey(struct ssh *ssh) 1820 { 1821 Authctxt *authctxt = (Authctxt *)ssh->authctxt; 1822 Identity *id; 1823 int sent = 0; 1824 char *ident; 1825 1826 while ((id = TAILQ_FIRST(&authctxt->keys))) { 1827 if (id->tried++) 1828 return (0); 1829 /* move key to the end of the queue */ 1830 TAILQ_REMOVE(&authctxt->keys, id, next); 1831 TAILQ_INSERT_TAIL(&authctxt->keys, id, next); 1832 /* 1833 * send a test message if we have the public key. for 1834 * encrypted keys we cannot do this and have to load the 1835 * private key instead 1836 */ 1837 if (id->key != NULL) { 1838 if (try_identity(id)) { 1839 ident = format_identity(id); 1840 debug("Offering public key: %s", ident); 1841 free(ident); 1842 sent = send_pubkey_test(ssh, id); 1843 } 1844 } else { 1845 debug("Trying private key: %s", id->filename); 1846 id->key = load_identity_file(id); 1847 if (id->key != NULL) { 1848 if (try_identity(id)) { 1849 id->isprivate = 1; 1850 sent = sign_and_send_pubkey(ssh, id); 1851 } 1852 sshkey_free(id->key); 1853 id->key = NULL; 1854 id->isprivate = 0; 1855 } 1856 } 1857 if (sent) 1858 return (sent); 1859 } 1860 return (0); 1861 } 1862 1863 /* 1864 * Send userauth request message specifying keyboard-interactive method. 1865 */ 1866 static int 1867 userauth_kbdint(struct ssh *ssh) 1868 { 1869 Authctxt *authctxt = (Authctxt *)ssh->authctxt; 1870 int r; 1871 1872 if (authctxt->attempt_kbdint++ >= options.number_of_password_prompts) 1873 return 0; 1874 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */ 1875 if (authctxt->attempt_kbdint > 1 && !authctxt->info_req_seen) { 1876 debug3("userauth_kbdint: disable: no info_req_seen"); 1877 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, NULL); 1878 return 0; 1879 } 1880 1881 debug2("userauth_kbdint"); 1882 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 1883 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || 1884 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || 1885 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || 1886 (r = sshpkt_put_cstring(ssh, "")) != 0 || /* lang */ 1887 (r = sshpkt_put_cstring(ssh, options.kbd_interactive_devices ? 1888 options.kbd_interactive_devices : "")) != 0 || 1889 (r = sshpkt_send(ssh)) != 0) 1890 fatal("%s: %s", __func__, ssh_err(r)); 1891 1892 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req); 1893 return 1; 1894 } 1895 1896 /* 1897 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE 1898 */ 1899 static int 1900 input_userauth_info_req(int type, u_int32_t seq, struct ssh *ssh) 1901 { 1902 Authctxt *authctxt = ssh->authctxt; 1903 char *name = NULL, *inst = NULL, *lang = NULL, *prompt = NULL; 1904 char *response = NULL; 1905 u_char echo = 0; 1906 u_int num_prompts, i; 1907 int r; 1908 1909 debug2("input_userauth_info_req"); 1910 1911 if (authctxt == NULL) 1912 fatal("input_userauth_info_req: no authentication context"); 1913 1914 authctxt->info_req_seen = 1; 1915 1916 if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 || 1917 (r = sshpkt_get_cstring(ssh, &inst, NULL)) != 0 || 1918 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0) 1919 goto out; 1920 if (strlen(name) > 0) 1921 logit("%s", name); 1922 if (strlen(inst) > 0) 1923 logit("%s", inst); 1924 1925 if ((r = sshpkt_get_u32(ssh, &num_prompts)) != 0) 1926 goto out; 1927 /* 1928 * Begin to build info response packet based on prompts requested. 1929 * We commit to providing the correct number of responses, so if 1930 * further on we run into a problem that prevents this, we have to 1931 * be sure and clean this up and send a correct error response. 1932 */ 1933 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE)) != 0 || 1934 (r = sshpkt_put_u32(ssh, num_prompts)) != 0) 1935 goto out; 1936 1937 debug2("input_userauth_info_req: num_prompts %d", num_prompts); 1938 for (i = 0; i < num_prompts; i++) { 1939 if ((r = sshpkt_get_cstring(ssh, &prompt, NULL)) != 0 || 1940 (r = sshpkt_get_u8(ssh, &echo)) != 0) 1941 goto out; 1942 response = read_passphrase(prompt, echo ? RP_ECHO : 0); 1943 if ((r = sshpkt_put_cstring(ssh, response)) != 0) 1944 goto out; 1945 freezero(response, strlen(response)); 1946 free(prompt); 1947 response = prompt = NULL; 1948 } 1949 /* done with parsing incoming message. */ 1950 if ((r = sshpkt_get_end(ssh)) != 0 || 1951 (r = sshpkt_add_padding(ssh, 64)) != 0) 1952 goto out; 1953 r = sshpkt_send(ssh); 1954 out: 1955 if (response) 1956 freezero(response, strlen(response)); 1957 free(prompt); 1958 free(name); 1959 free(inst); 1960 free(lang); 1961 return r; 1962 } 1963 1964 static int 1965 ssh_keysign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp, 1966 const u_char *data, size_t datalen) 1967 { 1968 struct sshbuf *b; 1969 struct stat st; 1970 pid_t pid; 1971 int r, to[2], from[2], status; 1972 int sock = ssh_packet_get_connection_in(ssh); 1973 u_char rversion = 0, version = 2; 1974 void (*osigchld)(int); 1975 1976 *sigp = NULL; 1977 *lenp = 0; 1978 1979 if (stat(_PATH_SSH_KEY_SIGN, &st) == -1) { 1980 error("%s: not installed: %s", __func__, strerror(errno)); 1981 return -1; 1982 } 1983 if (fflush(stdout) != 0) { 1984 error("%s: fflush: %s", __func__, strerror(errno)); 1985 return -1; 1986 } 1987 if (pipe(to) == -1) { 1988 error("%s: pipe: %s", __func__, strerror(errno)); 1989 return -1; 1990 } 1991 if (pipe(from) == -1) { 1992 error("%s: pipe: %s", __func__, strerror(errno)); 1993 return -1; 1994 } 1995 if ((pid = fork()) == -1) { 1996 error("%s: fork: %s", __func__, strerror(errno)); 1997 return -1; 1998 } 1999 osigchld = ssh_signal(SIGCHLD, SIG_DFL); 2000 if (pid == 0) { 2001 close(from[0]); 2002 if (dup2(from[1], STDOUT_FILENO) == -1) 2003 fatal("%s: dup2: %s", __func__, strerror(errno)); 2004 close(to[1]); 2005 if (dup2(to[0], STDIN_FILENO) == -1) 2006 fatal("%s: dup2: %s", __func__, strerror(errno)); 2007 close(from[1]); 2008 close(to[0]); 2009 2010 if (dup2(sock, STDERR_FILENO + 1) == -1) 2011 fatal("%s: dup2: %s", __func__, strerror(errno)); 2012 sock = STDERR_FILENO + 1; 2013 fcntl(sock, F_SETFD, 0); /* keep the socket on exec */ 2014 closefrom(sock + 1); 2015 2016 debug3("%s: [child] pid=%ld, exec %s", 2017 __func__, (long)getpid(), _PATH_SSH_KEY_SIGN); 2018 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL); 2019 fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN, 2020 strerror(errno)); 2021 } 2022 close(from[1]); 2023 close(to[0]); 2024 sock = STDERR_FILENO + 1; 2025 2026 if ((b = sshbuf_new()) == NULL) 2027 fatal("%s: sshbuf_new failed", __func__); 2028 /* send # of sock, data to be signed */ 2029 if ((r = sshbuf_put_u32(b, sock)) != 0 || 2030 (r = sshbuf_put_string(b, data, datalen)) != 0) 2031 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 2032 if (ssh_msg_send(to[1], version, b) == -1) 2033 fatal("%s: couldn't send request", __func__); 2034 sshbuf_reset(b); 2035 r = ssh_msg_recv(from[0], b); 2036 close(from[0]); 2037 close(to[1]); 2038 if (r < 0) { 2039 error("%s: no reply", __func__); 2040 goto fail; 2041 } 2042 2043 errno = 0; 2044 while (waitpid(pid, &status, 0) == -1) { 2045 if (errno != EINTR) { 2046 error("%s: waitpid %ld: %s", 2047 __func__, (long)pid, strerror(errno)); 2048 goto fail; 2049 } 2050 } 2051 if (!WIFEXITED(status)) { 2052 error("%s: exited abnormally", __func__); 2053 goto fail; 2054 } 2055 if (WEXITSTATUS(status) != 0) { 2056 error("%s: exited with status %d", 2057 __func__, WEXITSTATUS(status)); 2058 goto fail; 2059 } 2060 if ((r = sshbuf_get_u8(b, &rversion)) != 0) { 2061 error("%s: buffer error: %s", __func__, ssh_err(r)); 2062 goto fail; 2063 } 2064 if (rversion != version) { 2065 error("%s: bad version", __func__); 2066 goto fail; 2067 } 2068 if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) { 2069 error("%s: buffer error: %s", __func__, ssh_err(r)); 2070 fail: 2071 ssh_signal(SIGCHLD, osigchld); 2072 sshbuf_free(b); 2073 return -1; 2074 } 2075 ssh_signal(SIGCHLD, osigchld); 2076 sshbuf_free(b); 2077 2078 return 0; 2079 } 2080 2081 static int 2082 userauth_hostbased(struct ssh *ssh) 2083 { 2084 Authctxt *authctxt = (Authctxt *)ssh->authctxt; 2085 struct sshkey *private = NULL; 2086 struct sshbuf *b = NULL; 2087 u_char *sig = NULL, *keyblob = NULL; 2088 char *fp = NULL, *chost = NULL, *lname = NULL; 2089 size_t siglen = 0, keylen = 0; 2090 int i, r, success = 0; 2091 2092 if (authctxt->ktypes == NULL) { 2093 authctxt->oktypes = xstrdup(options.hostbased_key_types); 2094 authctxt->ktypes = authctxt->oktypes; 2095 } 2096 2097 /* 2098 * Work through each listed type pattern in HostbasedKeyTypes, 2099 * trying each hostkey that matches the type in turn. 2100 */ 2101 for (;;) { 2102 if (authctxt->active_ktype == NULL) 2103 authctxt->active_ktype = strsep(&authctxt->ktypes, ","); 2104 if (authctxt->active_ktype == NULL || 2105 *authctxt->active_ktype == '\0') 2106 break; 2107 debug3("%s: trying key type %s", __func__, 2108 authctxt->active_ktype); 2109 2110 /* check for a useful key */ 2111 private = NULL; 2112 for (i = 0; i < authctxt->sensitive->nkeys; i++) { 2113 if (authctxt->sensitive->keys[i] == NULL || 2114 authctxt->sensitive->keys[i]->type == KEY_UNSPEC) 2115 continue; 2116 if (match_pattern_list( 2117 sshkey_ssh_name(authctxt->sensitive->keys[i]), 2118 authctxt->active_ktype, 0) != 1) 2119 continue; 2120 /* we take and free the key */ 2121 private = authctxt->sensitive->keys[i]; 2122 authctxt->sensitive->keys[i] = NULL; 2123 break; 2124 } 2125 /* Found one */ 2126 if (private != NULL) 2127 break; 2128 /* No more keys of this type; advance */ 2129 authctxt->active_ktype = NULL; 2130 } 2131 if (private == NULL) { 2132 free(authctxt->oktypes); 2133 authctxt->oktypes = authctxt->ktypes = NULL; 2134 authctxt->active_ktype = NULL; 2135 debug("No more client hostkeys for hostbased authentication."); 2136 goto out; 2137 } 2138 2139 if ((fp = sshkey_fingerprint(private, options.fingerprint_hash, 2140 SSH_FP_DEFAULT)) == NULL) { 2141 error("%s: sshkey_fingerprint failed", __func__); 2142 goto out; 2143 } 2144 debug("%s: trying hostkey %s %s", 2145 __func__, sshkey_ssh_name(private), fp); 2146 2147 /* figure out a name for the client host */ 2148 lname = get_local_name(ssh_packet_get_connection_in(ssh)); 2149 if (lname == NULL) { 2150 error("%s: cannot get local ipaddr/name", __func__); 2151 goto out; 2152 } 2153 2154 /* XXX sshbuf_put_stringf? */ 2155 xasprintf(&chost, "%s.", lname); 2156 debug2("%s: chost %s", __func__, chost); 2157 2158 /* construct data */ 2159 if ((b = sshbuf_new()) == NULL) { 2160 error("%s: sshbuf_new failed", __func__); 2161 goto out; 2162 } 2163 if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) { 2164 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r)); 2165 goto out; 2166 } 2167 if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 || 2168 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 2169 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 || 2170 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 || 2171 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 || 2172 (r = sshbuf_put_cstring(b, sshkey_ssh_name(private))) != 0 || 2173 (r = sshbuf_put_string(b, keyblob, keylen)) != 0 || 2174 (r = sshbuf_put_cstring(b, chost)) != 0 || 2175 (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) { 2176 error("%s: buffer error: %s", __func__, ssh_err(r)); 2177 goto out; 2178 } 2179 2180 #ifdef DEBUG_PK 2181 sshbuf_dump(b, stderr); 2182 #endif 2183 if ((r = ssh_keysign(ssh, private, &sig, &siglen, 2184 sshbuf_ptr(b), sshbuf_len(b))) != 0) { 2185 error("sign using hostkey %s %s failed", 2186 sshkey_ssh_name(private), fp); 2187 goto out; 2188 } 2189 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 2190 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || 2191 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || 2192 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || 2193 (r = sshpkt_put_cstring(ssh, sshkey_ssh_name(private))) != 0 || 2194 (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 || 2195 (r = sshpkt_put_cstring(ssh, chost)) != 0 || 2196 (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 || 2197 (r = sshpkt_put_string(ssh, sig, siglen)) != 0 || 2198 (r = sshpkt_send(ssh)) != 0) { 2199 error("%s: packet error: %s", __func__, ssh_err(r)); 2200 goto out; 2201 } 2202 success = 1; 2203 2204 out: 2205 if (sig != NULL) 2206 freezero(sig, siglen); 2207 free(keyblob); 2208 free(lname); 2209 free(fp); 2210 free(chost); 2211 sshkey_free(private); 2212 sshbuf_free(b); 2213 2214 return success; 2215 } 2216 2217 /* find auth method */ 2218 2219 /* 2220 * given auth method name, if configurable options permit this method fill 2221 * in auth_ident field and return true, otherwise return false. 2222 */ 2223 static int 2224 authmethod_is_enabled(Authmethod *method) 2225 { 2226 if (method == NULL) 2227 return 0; 2228 /* return false if options indicate this method is disabled */ 2229 if (method->enabled == NULL || *method->enabled == 0) 2230 return 0; 2231 /* return false if batch mode is enabled but method needs interactive mode */ 2232 if (method->batch_flag != NULL && *method->batch_flag != 0) 2233 return 0; 2234 return 1; 2235 } 2236 2237 static Authmethod * 2238 authmethod_lookup(const char *name) 2239 { 2240 Authmethod *method = NULL; 2241 if (name != NULL) 2242 for (method = authmethods; method->name != NULL; method++) 2243 if (strcmp(name, method->name) == 0) 2244 return method; 2245 debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); 2246 return NULL; 2247 } 2248 2249 /* XXX internal state */ 2250 static Authmethod *current = NULL; 2251 static char *supported = NULL; 2252 static char *preferred = NULL; 2253 2254 /* 2255 * Given the authentication method list sent by the server, return the 2256 * next method we should try. If the server initially sends a nil list, 2257 * use a built-in default list. 2258 */ 2259 static Authmethod * 2260 authmethod_get(char *authlist) 2261 { 2262 char *name = NULL; 2263 u_int next; 2264 2265 /* Use a suitable default if we're passed a nil list. */ 2266 if (authlist == NULL || strlen(authlist) == 0) 2267 authlist = options.preferred_authentications; 2268 2269 if (supported == NULL || strcmp(authlist, supported) != 0) { 2270 debug3("start over, passed a different list %s", authlist); 2271 free(supported); 2272 supported = xstrdup(authlist); 2273 preferred = options.preferred_authentications; 2274 debug3("preferred %s", preferred); 2275 current = NULL; 2276 } else if (current != NULL && authmethod_is_enabled(current)) 2277 return current; 2278 2279 for (;;) { 2280 if ((name = match_list(preferred, supported, &next)) == NULL) { 2281 debug("No more authentication methods to try."); 2282 current = NULL; 2283 return NULL; 2284 } 2285 preferred += next; 2286 debug3("authmethod_lookup %s", name); 2287 debug3("remaining preferred: %s", preferred); 2288 if ((current = authmethod_lookup(name)) != NULL && 2289 authmethod_is_enabled(current)) { 2290 debug3("authmethod_is_enabled %s", name); 2291 debug("Next authentication method: %s", name); 2292 free(name); 2293 return current; 2294 } 2295 free(name); 2296 } 2297 } 2298 2299 static char * 2300 authmethods_get(void) 2301 { 2302 Authmethod *method = NULL; 2303 struct sshbuf *b; 2304 char *list; 2305 int r; 2306 2307 if ((b = sshbuf_new()) == NULL) 2308 fatal("%s: sshbuf_new failed", __func__); 2309 for (method = authmethods; method->name != NULL; method++) { 2310 if (authmethod_is_enabled(method)) { 2311 if ((r = sshbuf_putf(b, "%s%s", 2312 sshbuf_len(b) ? "," : "", method->name)) != 0) 2313 fatal("%s: buffer error: %s", 2314 __func__, ssh_err(r)); 2315 } 2316 } 2317 if ((list = sshbuf_dup_string(b)) == NULL) 2318 fatal("%s: sshbuf_dup_string failed", __func__); 2319 sshbuf_free(b); 2320 return list; 2321 } 2322