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