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