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