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