jcs's openbsd hax
openbsd
at jcs 2354 lines 66 kB view raw
1/* $OpenBSD: sshconnect2.c,v 1.382 2026/02/16 00:45:41 dtucker 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 <pwd.h> 37#include <signal.h> 38#include <stdio.h> 39#include <string.h> 40#include <stdarg.h> 41#include <unistd.h> 42 43#include "xmalloc.h" 44#include "ssh.h" 45#include "ssh2.h" 46#include "sshbuf.h" 47#include "packet.h" 48#include "compat.h" 49#include "cipher.h" 50#include "sshkey.h" 51#include "kex.h" 52#include "sshconnect.h" 53#include "authfile.h" 54#include "authfd.h" 55#include "log.h" 56#include "misc.h" 57#include "readconf.h" 58#include "match.h" 59#include "dispatch.h" 60#include "canohost.h" 61#include "msg.h" 62#include "pathnames.h" 63#include "hostfile.h" 64#include "ssherr.h" 65#include "utf8.h" 66#include "sk-api.h" 67 68#ifdef GSSAPI 69#include "ssh-gss.h" 70#endif 71 72/* import */ 73extern Options options; 74 75/* 76 * SSH2 key exchange 77 */ 78 79static char *xxx_host; 80static struct sockaddr *xxx_hostaddr; 81static const struct ssh_conn_info *xxx_conn_info; 82 83static int 84verify_host_key_callback(struct sshkey *hostkey, struct ssh *ssh) 85{ 86 int r; 87 88 if ((r = sshkey_check_rsa_length(hostkey, 89 options.required_rsa_size)) != 0) 90 fatal_r(r, "Bad server host key"); 91 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey, 92 xxx_conn_info) != 0) 93 fatal("Host key verification failed."); 94 return 0; 95} 96 97/* Returns the first item from a comma-separated algorithm list */ 98static char * 99first_alg(const char *algs) 100{ 101 char *ret, *cp; 102 103 ret = xstrdup(algs); 104 if ((cp = strchr(ret, ',')) != NULL) 105 *cp = '\0'; 106 return ret; 107} 108 109static char * 110order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port, 111 const struct ssh_conn_info *cinfo) 112{ 113 char *oavail = NULL, *avail = NULL, *first = NULL, *last = NULL; 114 char *alg = NULL, *hostname = NULL, *ret = NULL, *best = NULL; 115 size_t maxlen; 116 struct hostkeys *hostkeys = NULL; 117 int ktype; 118 u_int i; 119 120 /* Find all hostkeys for this hostname */ 121 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL); 122 hostkeys = init_hostkeys(); 123 for (i = 0; i < options.num_user_hostfiles; i++) 124 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i], 0); 125 for (i = 0; i < options.num_system_hostfiles; i++) { 126 load_hostkeys(hostkeys, hostname, 127 options.system_hostfiles[i], 0); 128 } 129 if (options.known_hosts_command != NULL) { 130 load_hostkeys_command(hostkeys, options.known_hosts_command, 131 "ORDER", cinfo, NULL, hostname); 132 } 133 /* 134 * If a plain public key exists that matches the type of the best 135 * preference HostkeyAlgorithms, then use the whole list as is. 136 * Note that we ignore whether the best preference algorithm is a 137 * certificate type, as sshconnect.c will downgrade certs to 138 * plain keys if necessary. 139 */ 140 best = first_alg(options.hostkeyalgorithms); 141 if (lookup_key_in_hostkeys_by_type(hostkeys, 142 sshkey_type_plain(sshkey_type_from_name(best)), 143 sshkey_ecdsa_nid_from_name(best), NULL)) { 144 debug3_f("have matching best-preference key type %s, " 145 "using HostkeyAlgorithms verbatim", best); 146 ret = xstrdup(options.hostkeyalgorithms); 147 goto out; 148 } 149 150 /* 151 * Otherwise, prefer the host key algorithms that match known keys 152 * while keeping the ordering of HostkeyAlgorithms as much as possible. 153 */ 154 oavail = avail = xstrdup(options.hostkeyalgorithms); 155 maxlen = strlen(avail) + 1; 156 first = xmalloc(maxlen); 157 last = xmalloc(maxlen); 158 *first = *last = '\0'; 159 160#define ALG_APPEND(to, from) \ 161 do { \ 162 if (*to != '\0') \ 163 strlcat(to, ",", maxlen); \ 164 strlcat(to, from, maxlen); \ 165 } while (0) 166 167 while ((alg = strsep(&avail, ",")) && *alg != '\0') { 168 if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC) 169 fatal_f("unknown alg %s", alg); 170 /* 171 * If we have a @cert-authority marker in known_hosts then 172 * prefer all certificate algorithms. 173 */ 174 if (sshkey_type_is_cert(ktype) && 175 lookup_marker_in_hostkeys(hostkeys, MRK_CA)) { 176 ALG_APPEND(first, alg); 177 continue; 178 } 179 /* If the key appears in known_hosts then prefer it */ 180 if (lookup_key_in_hostkeys_by_type(hostkeys, 181 sshkey_type_plain(ktype), 182 sshkey_ecdsa_nid_from_name(alg), NULL)) { 183 ALG_APPEND(first, alg); 184 continue; 185 } 186 /* Otherwise, put it last */ 187 ALG_APPEND(last, alg); 188 } 189#undef ALG_APPEND 190 xasprintf(&ret, "%s%s%s", first, 191 (*first == '\0' || *last == '\0') ? "" : ",", last); 192 if (*first != '\0') 193 debug3_f("prefer hostkeyalgs: %s", first); 194 else 195 debug3_f("no algorithms matched; accept original"); 196 out: 197 free(best); 198 free(first); 199 free(last); 200 free(hostname); 201 free(oavail); 202 free_hostkeys(hostkeys); 203 204 return ret; 205} 206 207void 208ssh_kex2(struct ssh *ssh, char *host, struct sockaddr *hostaddr, u_short port, 209 const struct ssh_conn_info *cinfo) 210{ 211 char *myproposal[PROPOSAL_MAX]; 212 char *all_key, *hkalgs = NULL; 213 int r, use_known_hosts_order = 0; 214 215 xxx_host = host; 216 xxx_hostaddr = hostaddr; 217 xxx_conn_info = cinfo; 218 219 if (options.rekey_limit || options.rekey_interval) 220 ssh_packet_set_rekey_limits(ssh, options.rekey_limit, 221 options.rekey_interval); 222 223 /* 224 * If the user has not specified HostkeyAlgorithms, or has only 225 * appended or removed algorithms from that list then prefer algorithms 226 * that are in the list that are supported by known_hosts keys. 227 */ 228 if (options.hostkeyalgorithms == NULL || 229 options.hostkeyalgorithms[0] == '-' || 230 options.hostkeyalgorithms[0] == '+') 231 use_known_hosts_order = 1; 232 233 /* Expand or fill in HostkeyAlgorithms */ 234 all_key = sshkey_alg_list(0, 0, 1, ','); 235 if ((r = kex_assemble_names(&options.hostkeyalgorithms, 236 kex_default_pk_alg(), all_key)) != 0) 237 fatal_fr(r, "kex_assemble_namelist"); 238 free(all_key); 239 240 if (use_known_hosts_order) 241 hkalgs = order_hostkeyalgs(host, hostaddr, port, cinfo); 242 243 kex_proposal_populate_entries(ssh, myproposal, 244 options.kex_algorithms, options.ciphers, options.macs, 245 compression_alg_list(options.compression), 246 hkalgs ? hkalgs : options.hostkeyalgorithms); 247 248 free(hkalgs); 249 250 /* start key exchange */ 251 if ((r = kex_setup(ssh, myproposal)) != 0) 252 fatal_r(r, "kex_setup"); 253#ifdef WITH_OPENSSL 254 ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client; 255 ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client; 256 ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client; 257 ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client; 258 ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client; 259 ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client; 260 ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client; 261 ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client; 262#endif 263 ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client; 264 ssh->kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_client; 265 ssh->kex->kex[KEX_KEM_MLKEM768X25519_SHA256] = kex_gen_client; 266 ssh->kex->verify_host_key=&verify_host_key_callback; 267 268 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &ssh->kex->done); 269 kex_proposal_free_entries(myproposal); 270 271#ifdef DEBUG_KEXDH 272 /* send 1st encrypted/maced/compressed message */ 273 if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 || 274 (r = sshpkt_put_cstring(ssh, "markus")) != 0 || 275 (r = sshpkt_send(ssh)) != 0 || 276 (r = ssh_packet_write_wait(ssh)) != 0) 277 fatal_fr(r, "send packet"); 278#endif 279} 280 281/* 282 * Authenticate user 283 */ 284 285typedef struct cauthctxt Authctxt; 286typedef struct cauthmethod Authmethod; 287typedef struct identity Identity; 288typedef struct idlist Idlist; 289 290struct identity { 291 TAILQ_ENTRY(identity) next; 292 int agent_fd; /* >=0 if agent supports key */ 293 struct sshkey *key; /* public/private key */ 294 char *filename; /* comment for agent-only keys */ 295 int tried; 296 int isprivate; /* key points to the private key */ 297 int userprovided; 298}; 299TAILQ_HEAD(idlist, identity); 300 301struct cauthctxt { 302 const char *server_user; 303 const char *local_user; 304 const char *host; 305 const char *service; 306 struct cauthmethod *method; 307 sig_atomic_t success; 308 char *authlist; 309#ifdef GSSAPI 310 /* gssapi */ 311 gss_OID_set gss_supported_mechs; 312 u_int mech_tried; 313#endif 314 /* pubkey */ 315 struct idlist keys; 316 int agent_fd; 317 /* hostbased */ 318 Sensitive *sensitive; 319 char *oktypes, *ktypes; 320 const char *active_ktype; 321 /* kbd-interactive */ 322 int info_req_seen; 323 int attempt_kbdint; 324 /* password */ 325 int attempt_passwd; 326 /* generic */ 327 void *methoddata; 328}; 329 330struct cauthmethod { 331 char *name; /* string to compare against server's list */ 332 int (*userauth)(struct ssh *ssh); 333 void (*cleanup)(struct ssh *ssh); 334 int *enabled; /* flag in option struct that enables method */ 335 int *batch_flag; /* flag in option struct that disables method */ 336}; 337 338static int input_userauth_service_accept(int, u_int32_t, struct ssh *); 339static int input_userauth_success(int, u_int32_t, struct ssh *); 340static int input_userauth_failure(int, u_int32_t, struct ssh *); 341static int input_userauth_banner(int, u_int32_t, struct ssh *); 342static int input_userauth_error(int, u_int32_t, struct ssh *); 343static int input_userauth_info_req(int, u_int32_t, struct ssh *); 344static int input_userauth_pk_ok(int, u_int32_t, struct ssh *); 345static int input_userauth_passwd_changereq(int, u_int32_t, struct ssh *); 346 347static int userauth_none(struct ssh *); 348static int userauth_pubkey(struct ssh *); 349static int userauth_passwd(struct ssh *); 350static int userauth_kbdint(struct ssh *); 351static int userauth_hostbased(struct ssh *); 352 353#ifdef GSSAPI 354static int userauth_gssapi(struct ssh *); 355static void userauth_gssapi_cleanup(struct ssh *); 356static int input_gssapi_response(int type, u_int32_t, struct ssh *); 357static int input_gssapi_token(int type, u_int32_t, struct ssh *); 358static int input_gssapi_error(int, u_int32_t, struct ssh *); 359static int input_gssapi_errtok(int, u_int32_t, struct ssh *); 360#endif 361 362void userauth(struct ssh *, char *); 363 364static void pubkey_cleanup(struct ssh *); 365static int sign_and_send_pubkey(struct ssh *ssh, Identity *); 366static void pubkey_prepare(struct ssh *, Authctxt *); 367static void pubkey_reset(Authctxt *); 368static struct sshkey *load_identity_file(Identity *); 369 370static Authmethod *authmethod_get(char *authlist); 371static Authmethod *authmethod_lookup(const char *name); 372static char *authmethods_get(void); 373 374Authmethod authmethods[] = { 375#ifdef GSSAPI 376 {"gssapi-with-mic", 377 userauth_gssapi, 378 userauth_gssapi_cleanup, 379 &options.gss_authentication, 380 NULL}, 381#endif 382 {"hostbased", 383 userauth_hostbased, 384 NULL, 385 &options.hostbased_authentication, 386 NULL}, 387 {"publickey", 388 userauth_pubkey, 389 NULL, 390 &options.pubkey_authentication, 391 NULL}, 392 {"keyboard-interactive", 393 userauth_kbdint, 394 NULL, 395 &options.kbd_interactive_authentication, 396 &options.batch_mode}, 397 {"password", 398 userauth_passwd, 399 NULL, 400 &options.password_authentication, 401 &options.batch_mode}, 402 {"none", 403 userauth_none, 404 NULL, 405 NULL, 406 NULL}, 407 {NULL, NULL, NULL, NULL, NULL} 408}; 409 410void 411ssh_userauth2(struct ssh *ssh, const char *local_user, 412 const char *server_user, char *host, Sensitive *sensitive) 413{ 414 Authctxt authctxt; 415 int r; 416 417 if (options.preferred_authentications == NULL) 418 options.preferred_authentications = authmethods_get(); 419 420 /* setup authentication context */ 421 memset(&authctxt, 0, sizeof(authctxt)); 422 authctxt.server_user = server_user; 423 authctxt.local_user = local_user; 424 authctxt.host = host; 425 authctxt.service = "ssh-connection"; /* service name */ 426 authctxt.success = 0; 427 authctxt.method = authmethod_lookup("none"); 428 authctxt.authlist = NULL; 429 authctxt.methoddata = NULL; 430 authctxt.sensitive = sensitive; 431 authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL; 432 authctxt.info_req_seen = 0; 433 authctxt.attempt_kbdint = 0; 434 authctxt.attempt_passwd = 0; 435#if GSSAPI 436 authctxt.gss_supported_mechs = NULL; 437 authctxt.mech_tried = 0; 438#endif 439 authctxt.agent_fd = -1; 440 if (authctxt.method == NULL) 441 fatal_f("internal error: cannot send userauth none request"); 442 443 if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 || 444 (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 || 445 (r = sshpkt_send(ssh)) != 0) 446 fatal_fr(r, "send packet"); 447 448 ssh->authctxt = &authctxt; 449 ssh_dispatch_init(ssh, &input_userauth_error); 450 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, kex_input_ext_info); 451 ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept); 452 ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt.success); /* loop until success */ 453 pubkey_cleanup(ssh); 454#ifdef GSSAPI 455 if (authctxt.gss_supported_mechs != NULL) { 456 u_int ms; 457 458 gss_release_oid_set(&ms, &authctxt.gss_supported_mechs); 459 authctxt.gss_supported_mechs = NULL; 460 } 461#endif 462 ssh->authctxt = NULL; 463 464 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL); 465 466 if (!authctxt.success) 467 fatal("Authentication failed."); 468 if (ssh_packet_connection_is_on_socket(ssh)) { 469 verbose("Authenticated to %s ([%s]:%d) using \"%s\".", host, 470 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), 471 authctxt.method->name); 472 } else { 473 verbose("Authenticated to %s (via proxy) using \"%s\".", host, 474 authctxt.method->name); 475 } 476} 477 478static int 479input_userauth_service_accept(int type, u_int32_t seq, struct ssh *ssh) 480{ 481 int r; 482 483 if (ssh_packet_remaining(ssh) > 0) { 484 char *reply; 485 486 if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0) 487 goto out; 488 debug2("service_accept: %s", reply); 489 free(reply); 490 } else { 491 debug2("buggy server: service_accept w/o service"); 492 } 493 if ((r = sshpkt_get_end(ssh)) != 0) 494 goto out; 495 debug("SSH2_MSG_SERVICE_ACCEPT received"); 496 497 /* initial userauth request */ 498 userauth_none(ssh); 499 500 /* accept EXT_INFO at any time during userauth */ 501 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, ssh->kex->ext_info_s ? 502 &kex_input_ext_info : &input_userauth_error); 503 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success); 504 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure); 505 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner); 506 r = 0; 507 out: 508 return r; 509} 510 511void 512userauth(struct ssh *ssh, char *authlist) 513{ 514 Authctxt *authctxt = (Authctxt *)ssh->authctxt; 515 516 if (authctxt->method != NULL && authctxt->method->cleanup != NULL) 517 authctxt->method->cleanup(ssh); 518 519 free(authctxt->methoddata); 520 authctxt->methoddata = NULL; 521 if (authlist == NULL) { 522 authlist = authctxt->authlist; 523 } else { 524 free(authctxt->authlist); 525 authctxt->authlist = authlist; 526 } 527 for (;;) { 528 Authmethod *method = authmethod_get(authlist); 529 if (method == NULL) 530 fatal("%s@%s: Permission denied (%s).", 531 authctxt->server_user, authctxt->host, authlist); 532 authctxt->method = method; 533 534 /* reset the per method handler */ 535 ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_PER_METHOD_MIN, 536 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL); 537 538 /* and try new method */ 539 if (method->userauth(ssh) != 0) { 540 debug2("we sent a %s packet, wait for reply", method->name); 541 break; 542 } else { 543 debug2("we did not send a packet, disable method"); 544 method->enabled = NULL; 545 } 546 } 547} 548 549static int 550input_userauth_error(int type, u_int32_t seq, struct ssh *ssh) 551{ 552 fatal_f("bad message during authentication: type %d", type); 553 return 0; 554} 555 556static int 557input_userauth_banner(int type, u_int32_t seq, struct ssh *ssh) 558{ 559 char *msg = NULL; 560 size_t len; 561 int r; 562 563 debug3_f("entering"); 564 if ((r = sshpkt_get_cstring(ssh, &msg, &len)) != 0 || 565 (r = sshpkt_get_cstring(ssh, NULL, NULL)) != 0) 566 goto out; 567 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) 568 fmprintf(stderr, "%s", msg); 569 r = 0; 570 out: 571 free(msg); 572 return r; 573} 574 575static int 576input_userauth_success(int type, u_int32_t seq, struct ssh *ssh) 577{ 578 Authctxt *authctxt = ssh->authctxt; 579 580 if (authctxt == NULL) 581 fatal_f("no authentication context"); 582 free(authctxt->authlist); 583 authctxt->authlist = NULL; 584 if (authctxt->method != NULL && authctxt->method->cleanup != NULL) 585 authctxt->method->cleanup(ssh); 586 free(authctxt->methoddata); 587 authctxt->methoddata = NULL; 588 authctxt->success = 1; /* break out */ 589 ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, dispatch_protocol_error); 590 return 0; 591} 592 593#if 0 594static int 595input_userauth_success_unexpected(int type, u_int32_t seq, struct ssh *ssh) 596{ 597 Authctxt *authctxt = ssh->authctxt; 598 599 if (authctxt == NULL) 600 fatal_f("no authentication context"); 601 602 fatal("Unexpected authentication success during %s.", 603 authctxt->method->name); 604 return 0; 605} 606#endif 607 608static int 609input_userauth_failure(int type, u_int32_t seq, struct ssh *ssh) 610{ 611 Authctxt *authctxt = ssh->authctxt; 612 char *authlist = NULL; 613 u_char partial; 614 615 if (authctxt == NULL) 616 fatal("input_userauth_failure: no authentication context"); 617 618 if (sshpkt_get_cstring(ssh, &authlist, NULL) != 0 || 619 sshpkt_get_u8(ssh, &partial) != 0 || 620 sshpkt_get_end(ssh) != 0) 621 goto out; 622 623 if (partial != 0) { 624 verbose("Authenticated using \"%s\" with partial success.", 625 authctxt->method->name); 626 /* reset state */ 627 pubkey_reset(authctxt); 628 } 629 debug("Authentications that can continue: %s", authlist); 630 631 userauth(ssh, authlist); 632 authlist = NULL; 633 out: 634 free(authlist); 635 return 0; 636} 637 638/* 639 * Format an identity for logging including filename, key type, fingerprint 640 * and location (agent, etc.). Caller must free. 641 */ 642static char * 643format_identity(Identity *id) 644{ 645 char *fp = NULL, *ret = NULL; 646 const char *note = ""; 647 648 if (id->key != NULL) { 649 fp = sshkey_fingerprint(id->key, options.fingerprint_hash, 650 SSH_FP_DEFAULT); 651 } 652 if (id->key) { 653 if ((id->key->flags & SSHKEY_FLAG_EXT) != 0) 654 note = " token"; 655 else if (sshkey_is_sk(id->key)) 656 note = " authenticator"; 657 } 658 xasprintf(&ret, "%s %s%s%s%s%s%s", 659 id->filename, 660 id->key ? sshkey_type(id->key) : "", id->key ? " " : "", 661 fp ? fp : "", 662 id->userprovided ? " explicit" : "", note, 663 id->agent_fd != -1 ? " agent" : ""); 664 free(fp); 665 return ret; 666} 667 668static int 669input_userauth_pk_ok(int type, u_int32_t seq, struct ssh *ssh) 670{ 671 Authctxt *authctxt = ssh->authctxt; 672 struct sshkey *key = NULL; 673 Identity *id = NULL; 674 int pktype, found = 0, sent = 0; 675 size_t blen; 676 char *pkalg = NULL, *fp = NULL, *ident = NULL; 677 u_char *pkblob = NULL; 678 int r; 679 680 if (authctxt == NULL) 681 fatal("input_userauth_pk_ok: no authentication context"); 682 683 if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 || 684 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 || 685 (r = sshpkt_get_end(ssh)) != 0) 686 goto done; 687 688 if ((pktype = sshkey_type_from_name(pkalg)) == KEY_UNSPEC) { 689 debug_f("server sent unknown pkalg %s", pkalg); 690 r = SSH_ERR_INVALID_FORMAT; 691 goto done; 692 } 693 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) { 694 debug_r(r, "no key from blob. pkalg %s", pkalg); 695 goto done; 696 } 697 if (key->type != pktype) { 698 error("input_userauth_pk_ok: type mismatch " 699 "for decoded key (received %d, expected %d)", 700 key->type, pktype); 701 r = SSH_ERR_INVALID_FORMAT; 702 goto done; 703 } 704 705 /* 706 * search keys in the reverse order, because last candidate has been 707 * moved to the end of the queue. this also avoids confusion by 708 * duplicate keys 709 */ 710 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) { 711 if (sshkey_equal(key, id->key)) { 712 found = 1; 713 break; 714 } 715 } 716 if (!found || id == NULL) { 717 fp = sshkey_fingerprint(key, options.fingerprint_hash, 718 SSH_FP_DEFAULT); 719 error_f("server replied with unknown key: %s %s", 720 sshkey_type(key), fp == NULL ? "<ERROR>" : fp); 721 r = SSH_ERR_INVALID_FORMAT; 722 goto done; 723 } 724 ident = format_identity(id); 725 debug("Server accepts key: %s", ident); 726 sent = sign_and_send_pubkey(ssh, id); 727 r = 0; 728 done: 729 sshkey_free(key); 730 free(ident); 731 free(fp); 732 free(pkalg); 733 free(pkblob); 734 735 /* try another method if we did not send a packet */ 736 if (r == 0 && sent == 0) 737 userauth(ssh, NULL); 738 return r; 739} 740 741#ifdef GSSAPI 742static int 743userauth_gssapi(struct ssh *ssh) 744{ 745 Authctxt *authctxt = (Authctxt *)ssh->authctxt; 746 Gssctxt *gssctxt = NULL; 747 OM_uint32 min; 748 int r, ok = 0; 749 gss_OID mech = NULL; 750 751 /* Try one GSSAPI method at a time, rather than sending them all at 752 * once. */ 753 754 if (authctxt->gss_supported_mechs == NULL) 755 gss_indicate_mechs(&min, &authctxt->gss_supported_mechs); 756 757 /* Check to see whether the mechanism is usable before we offer it */ 758 while (authctxt->mech_tried < authctxt->gss_supported_mechs->count && 759 !ok) { 760 mech = &authctxt->gss_supported_mechs-> 761 elements[authctxt->mech_tried]; 762 /* My DER encoding requires length<128 */ 763 if (mech->length < 128 && ssh_gssapi_check_mechanism(&gssctxt, 764 mech, authctxt->host)) { 765 ok = 1; /* Mechanism works */ 766 } else { 767 authctxt->mech_tried++; 768 } 769 } 770 771 if (!ok || mech == NULL) 772 return 0; 773 774 authctxt->methoddata=(void *)gssctxt; 775 776 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 777 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || 778 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || 779 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || 780 (r = sshpkt_put_u32(ssh, 1)) != 0 || 781 (r = sshpkt_put_u32(ssh, (mech->length) + 2)) != 0 || 782 (r = sshpkt_put_u8(ssh, SSH_GSS_OIDTYPE)) != 0 || 783 (r = sshpkt_put_u8(ssh, mech->length)) != 0 || 784 (r = sshpkt_put(ssh, mech->elements, mech->length)) != 0 || 785 (r = sshpkt_send(ssh)) != 0) 786 fatal_fr(r, "send packet"); 787 788 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response); 789 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token); 790 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error); 791 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok); 792 793 authctxt->mech_tried++; /* Move along to next candidate */ 794 795 return 1; 796} 797 798static void 799userauth_gssapi_cleanup(struct ssh *ssh) 800{ 801 Authctxt *authctxt = (Authctxt *)ssh->authctxt; 802 Gssctxt *gssctxt = (Gssctxt *)authctxt->methoddata; 803 804 ssh_gssapi_delete_ctx(&gssctxt); 805 authctxt->methoddata = NULL; 806} 807 808static OM_uint32 809process_gssapi_token(struct ssh *ssh, gss_buffer_t recv_tok) 810{ 811 Authctxt *authctxt = ssh->authctxt; 812 Gssctxt *gssctxt = authctxt->methoddata; 813 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; 814 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER; 815 gss_buffer_desc gssbuf; 816 OM_uint32 status, ms, flags; 817 int r; 818 819 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, 820 recv_tok, &send_tok, &flags); 821 822 if (send_tok.length > 0) { 823 u_char type = GSS_ERROR(status) ? 824 SSH2_MSG_USERAUTH_GSSAPI_ERRTOK : 825 SSH2_MSG_USERAUTH_GSSAPI_TOKEN; 826 827 if ((r = sshpkt_start(ssh, type)) != 0 || 828 (r = sshpkt_put_string(ssh, send_tok.value, 829 send_tok.length)) != 0 || 830 (r = sshpkt_send(ssh)) != 0) 831 fatal_fr(r, "send %u packet", type); 832 833 gss_release_buffer(&ms, &send_tok); 834 } 835 836 if (status == GSS_S_COMPLETE) { 837 /* send either complete or MIC, depending on mechanism */ 838 if (!(flags & GSS_C_INTEG_FLAG)) { 839 if ((r = sshpkt_start(ssh, 840 SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE)) != 0 || 841 (r = sshpkt_send(ssh)) != 0) 842 fatal_fr(r, "send completion"); 843 } else { 844 struct sshbuf *b; 845 846 if ((b = sshbuf_new()) == NULL) 847 fatal_f("sshbuf_new failed"); 848 ssh_gssapi_buildmic(b, authctxt->server_user, 849 authctxt->service, "gssapi-with-mic", 850 ssh->kex->session_id); 851 852 if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL) 853 fatal_f("sshbuf_mutable_ptr failed"); 854 gssbuf.length = sshbuf_len(b); 855 856 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic); 857 858 if (!GSS_ERROR(status)) { 859 if ((r = sshpkt_start(ssh, 860 SSH2_MSG_USERAUTH_GSSAPI_MIC)) != 0 || 861 (r = sshpkt_put_string(ssh, mic.value, 862 mic.length)) != 0 || 863 (r = sshpkt_send(ssh)) != 0) 864 fatal_fr(r, "send MIC"); 865 } 866 867 sshbuf_free(b); 868 gss_release_buffer(&ms, &mic); 869 } 870 } 871 872 return status; 873} 874 875static int 876input_gssapi_response(int type, u_int32_t plen, struct ssh *ssh) 877{ 878 Authctxt *authctxt = ssh->authctxt; 879 Gssctxt *gssctxt; 880 size_t oidlen; 881 u_char *oidv = NULL; 882 int r; 883 884 if (authctxt == NULL) 885 fatal("input_gssapi_response: no authentication context"); 886 gssctxt = authctxt->methoddata; 887 888 /* Setup our OID */ 889 if ((r = sshpkt_get_string(ssh, &oidv, &oidlen)) != 0) 890 goto done; 891 892 if (oidlen <= 2 || 893 oidv[0] != SSH_GSS_OIDTYPE || 894 oidv[1] != oidlen - 2) { 895 debug("Badly encoded mechanism OID received"); 896 userauth(ssh, NULL); 897 goto ok; 898 } 899 900 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2)) 901 fatal("Server returned different OID than expected"); 902 903 if ((r = sshpkt_get_end(ssh)) != 0) 904 goto done; 905 906 if (GSS_ERROR(process_gssapi_token(ssh, GSS_C_NO_BUFFER))) { 907 /* Start again with next method on list */ 908 debug("Trying to start again"); 909 userauth(ssh, NULL); 910 goto ok; 911 } 912 ok: 913 r = 0; 914 done: 915 free(oidv); 916 return r; 917} 918 919static int 920input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh) 921{ 922 Authctxt *authctxt = ssh->authctxt; 923 gss_buffer_desc recv_tok; 924 u_char *p = NULL; 925 size_t len; 926 OM_uint32 status; 927 int r; 928 929 if (authctxt == NULL) 930 fatal("input_gssapi_response: no authentication context"); 931 932 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 || 933 (r = sshpkt_get_end(ssh)) != 0) 934 goto out; 935 936 recv_tok.value = p; 937 recv_tok.length = len; 938 status = process_gssapi_token(ssh, &recv_tok); 939 940 /* Start again with the next method in the list */ 941 if (GSS_ERROR(status)) { 942 userauth(ssh, NULL); 943 /* ok */ 944 } 945 r = 0; 946 out: 947 free(p); 948 return r; 949} 950 951static int 952input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh) 953{ 954 Authctxt *authctxt = ssh->authctxt; 955 Gssctxt *gssctxt; 956 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER; 957 gss_buffer_desc recv_tok; 958 OM_uint32 ms; 959 u_char *p = NULL; 960 size_t len; 961 int r; 962 963 if (authctxt == NULL) 964 fatal("input_gssapi_response: no authentication context"); 965 gssctxt = authctxt->methoddata; 966 967 if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 || 968 (r = sshpkt_get_end(ssh)) != 0) { 969 free(p); 970 return r; 971 } 972 973 /* Stick it into GSSAPI and see what it says */ 974 recv_tok.value = p; 975 recv_tok.length = len; 976 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds, 977 &recv_tok, &send_tok, NULL); 978 free(p); 979 gss_release_buffer(&ms, &send_tok); 980 981 /* Server will be returning a failed packet after this one */ 982 return 0; 983} 984 985static int 986input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh) 987{ 988 char *msg = NULL; 989 char *lang = NULL; 990 int r; 991 992 if ((r = sshpkt_get_u32(ssh, NULL)) != 0 || /* maj */ 993 (r = sshpkt_get_u32(ssh, NULL)) != 0 || /* min */ 994 (r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 || 995 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0) 996 goto out; 997 r = sshpkt_get_end(ssh); 998 debug("Server GSSAPI Error:\n%s", msg); 999 out: 1000 free(msg); 1001 free(lang); 1002 return r; 1003} 1004#endif /* GSSAPI */ 1005 1006static int 1007userauth_none(struct ssh *ssh) 1008{ 1009 Authctxt *authctxt = (Authctxt *)ssh->authctxt; 1010 int r; 1011 1012 /* initial userauth request */ 1013 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 1014 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || 1015 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || 1016 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || 1017 (r = sshpkt_send(ssh)) != 0) 1018 fatal_fr(r, "send packet"); 1019 return 1; 1020} 1021 1022static int 1023userauth_passwd(struct ssh *ssh) 1024{ 1025 Authctxt *authctxt = (Authctxt *)ssh->authctxt; 1026 char *password, *prompt = NULL; 1027 const char *host = options.host_key_alias ? options.host_key_alias : 1028 authctxt->host; 1029 int r; 1030 1031 if (authctxt->attempt_passwd++ >= options.number_of_password_prompts) 1032 return 0; 1033 1034 if (authctxt->attempt_passwd != 1) 1035 error("Permission denied, please try again."); 1036 1037 xasprintf(&prompt, "%s@%s's password: ", authctxt->server_user, host); 1038 password = read_passphrase(prompt, 0); 1039 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 1040 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || 1041 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || 1042 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || 1043 (r = sshpkt_put_u8(ssh, 0)) != 0 || 1044 (r = sshpkt_put_cstring(ssh, password)) != 0 || 1045 (r = sshpkt_add_padding(ssh, 64)) != 0 || 1046 (r = sshpkt_send(ssh)) != 0) 1047 fatal_fr(r, "send packet"); 1048 1049 free(prompt); 1050 if (password != NULL) 1051 freezero(password, strlen(password)); 1052 1053 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, 1054 &input_userauth_passwd_changereq); 1055 1056 return 1; 1057} 1058 1059/* 1060 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST 1061 */ 1062static int 1063input_userauth_passwd_changereq(int type, u_int32_t seqnr, struct ssh *ssh) 1064{ 1065 Authctxt *authctxt = ssh->authctxt; 1066 char *info = NULL, *lang = NULL, *password = NULL, *retype = NULL; 1067 char prompt[256]; 1068 const char *host; 1069 int r; 1070 1071 debug2("input_userauth_passwd_changereq"); 1072 1073 if (authctxt == NULL) 1074 fatal("input_userauth_passwd_changereq: " 1075 "no authentication context"); 1076 host = options.host_key_alias ? options.host_key_alias : authctxt->host; 1077 1078 if ((r = sshpkt_get_cstring(ssh, &info, NULL)) != 0 || 1079 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0) 1080 goto out; 1081 if (strlen(info) > 0) 1082 logit("%s", info); 1083 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 1084 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || 1085 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || 1086 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || 1087 (r = sshpkt_put_u8(ssh, 1)) != 0) /* additional info */ 1088 goto out; 1089 1090 snprintf(prompt, sizeof(prompt), 1091 "Enter %.30s@%.128s's old password: ", 1092 authctxt->server_user, host); 1093 password = read_passphrase(prompt, 0); 1094 if ((r = sshpkt_put_cstring(ssh, password)) != 0) 1095 goto out; 1096 1097 freezero(password, strlen(password)); 1098 password = NULL; 1099 while (password == NULL) { 1100 snprintf(prompt, sizeof(prompt), 1101 "Enter %.30s@%.128s's new password: ", 1102 authctxt->server_user, host); 1103 password = read_passphrase(prompt, RP_ALLOW_EOF); 1104 if (password == NULL) { 1105 /* bail out */ 1106 r = 0; 1107 goto out; 1108 } 1109 snprintf(prompt, sizeof(prompt), 1110 "Retype %.30s@%.128s's new password: ", 1111 authctxt->server_user, host); 1112 retype = read_passphrase(prompt, 0); 1113 if (strcmp(password, retype) != 0) { 1114 freezero(password, strlen(password)); 1115 logit("Mismatch; try again, EOF to quit."); 1116 password = NULL; 1117 } 1118 freezero(retype, strlen(retype)); 1119 } 1120 if ((r = sshpkt_put_cstring(ssh, password)) != 0 || 1121 (r = sshpkt_add_padding(ssh, 64)) != 0 || 1122 (r = sshpkt_send(ssh)) != 0) 1123 goto out; 1124 1125 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, 1126 &input_userauth_passwd_changereq); 1127 r = 0; 1128 out: 1129 if (password) 1130 freezero(password, strlen(password)); 1131 free(info); 1132 free(lang); 1133 return r; 1134} 1135 1136/* 1137 * Select an algorithm for publickey signatures. 1138 * Returns algorithm (caller must free) or NULL if no mutual algorithm found. 1139 * 1140 * Call with ssh==NULL to ignore server-sig-algs extension list and 1141 * only attempt with the key's base signature type. 1142 */ 1143static char * 1144key_sig_algorithm(struct ssh *ssh, const struct sshkey *key) 1145{ 1146 char *allowed, *oallowed, *cp, *tmp, *alg = NULL; 1147 const char *server_sig_algs; 1148 1149 /* 1150 * The signature algorithm will only differ from the key algorithm 1151 * for RSA keys/certs and when the server advertises support for 1152 * newer (SHA2) algorithms. 1153 */ 1154 if (ssh == NULL || ssh->kex->server_sig_algs == NULL || 1155 (key->type != KEY_RSA && key->type != KEY_RSA_CERT) || 1156 (key->type == KEY_RSA_CERT && (ssh->compat & SSH_BUG_SIGTYPE))) { 1157 /* Filter base key signature alg against our configuration */ 1158 return match_list(sshkey_ssh_name(key), 1159 options.pubkey_accepted_algos, NULL); 1160 } 1161 1162 /* 1163 * Workaround OpenSSH 7.4 bug: this version supports RSA/SHA-2 but 1164 * fails to advertise it via SSH2_MSG_EXT_INFO. 1165 */ 1166 server_sig_algs = ssh->kex->server_sig_algs; 1167 if (key->type == KEY_RSA && (ssh->compat & SSH_BUG_SIGTYPE74)) 1168 server_sig_algs = "rsa-sha2-256,rsa-sha2-512"; 1169 1170 /* 1171 * For RSA keys/certs, since these might have a different sig type: 1172 * find the first entry in PubkeyAcceptedAlgorithms of the right type 1173 * that also appears in the supported signature algorithms list from 1174 * the server. 1175 */ 1176 oallowed = allowed = xstrdup(options.pubkey_accepted_algos); 1177 while ((cp = strsep(&allowed, ",")) != NULL) { 1178 if (sshkey_type_from_name(cp) != key->type) 1179 continue; 1180 tmp = match_list(sshkey_sigalg_by_name(cp), 1181 server_sig_algs, NULL); 1182 if (tmp != NULL) 1183 alg = xstrdup(cp); 1184 free(tmp); 1185 if (alg != NULL) 1186 break; 1187 } 1188 free(oallowed); 1189 return alg; 1190} 1191 1192static int 1193identity_sign(struct identity *id, u_char **sigp, size_t *lenp, 1194 const u_char *data, size_t datalen, u_int compat, const char *alg) 1195{ 1196 struct sshkey *sign_key = NULL, *prv = NULL; 1197 int is_agent = 0, retried = 0, r = SSH_ERR_INTERNAL_ERROR; 1198 struct notifier_ctx *notifier = NULL; 1199 char *fp = NULL, *pin = NULL, *prompt = NULL; 1200 1201 *sigp = NULL; 1202 *lenp = 0; 1203 1204 /* The agent supports this key. */ 1205 if (id->key != NULL && id->agent_fd != -1) { 1206 return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp, 1207 data, datalen, alg, compat); 1208 } 1209 1210 /* 1211 * We have already loaded the private key or the private key is 1212 * stored in external hardware. 1213 */ 1214 if (id->key != NULL && 1215 (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))) { 1216 sign_key = id->key; 1217 is_agent = 1; 1218 } else { 1219 /* Load the private key from the file. */ 1220 if ((prv = load_identity_file(id)) == NULL) 1221 return SSH_ERR_KEY_NOT_FOUND; 1222 if (id->key != NULL && !sshkey_equal_public(prv, id->key)) { 1223 error_f("private key %s contents do not match public", 1224 id->filename); 1225 r = SSH_ERR_KEY_NOT_FOUND; 1226 goto out; 1227 } 1228 sign_key = prv; 1229 } 1230 retry_pin: 1231 /* Prompt for touch for non-agent FIDO keys that request UP */ 1232 if (!is_agent && sshkey_is_sk(sign_key) && 1233 (sign_key->sk_flags & SSH_SK_USER_PRESENCE_REQD)) { 1234 /* XXX should batch mode just skip these? */ 1235 if ((fp = sshkey_fingerprint(sign_key, 1236 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) 1237 fatal_f("fingerprint failed"); 1238 notifier = notify_start(options.batch_mode, 1239 "Confirm user presence for key %s %s", 1240 sshkey_type(sign_key), fp); 1241 free(fp); 1242 } 1243 if ((r = sshkey_sign(sign_key, sigp, lenp, data, datalen, 1244 alg, options.sk_provider, pin, compat)) != 0) { 1245 debug_fr(r, "sshkey_sign"); 1246 if (!retried && pin == NULL && !is_agent && 1247 sshkey_is_sk(sign_key) && 1248 r == SSH_ERR_KEY_WRONG_PASSPHRASE) { 1249 notify_complete(notifier, NULL); 1250 notifier = NULL; 1251 xasprintf(&prompt, "Enter PIN for %s key %s: ", 1252 sshkey_type(sign_key), id->filename); 1253 pin = read_passphrase(prompt, 0); 1254 retried = 1; 1255 goto retry_pin; 1256 } 1257 goto out; 1258 } 1259 1260 /* 1261 * PKCS#11 tokens may not support all signature algorithms, 1262 * so check what we get back. 1263 */ 1264 if ((id->key->flags & SSHKEY_FLAG_EXT) != 0 && 1265 (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 (id->agent_fd == -1 && 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 freezero(id, 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 id->isprivate = 1; 1881 sent = sign_and_send_pubkey(ssh, id); 1882 sshkey_free(id->key); 1883 id->key = NULL; 1884 id->isprivate = 0; 1885 } 1886 } 1887 if (sent) 1888 return (sent); 1889 } 1890 return (0); 1891} 1892 1893/* 1894 * Send userauth request message specifying keyboard-interactive method. 1895 */ 1896static int 1897userauth_kbdint(struct ssh *ssh) 1898{ 1899 Authctxt *authctxt = (Authctxt *)ssh->authctxt; 1900 int r; 1901 1902 if (authctxt->attempt_kbdint++ >= options.number_of_password_prompts) 1903 return 0; 1904 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */ 1905 if (authctxt->attempt_kbdint > 1 && !authctxt->info_req_seen) { 1906 debug3("userauth_kbdint: disable: no info_req_seen"); 1907 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, NULL); 1908 return 0; 1909 } 1910 1911 debug2("userauth_kbdint"); 1912 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 1913 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || 1914 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || 1915 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || 1916 (r = sshpkt_put_cstring(ssh, "")) != 0 || /* lang */ 1917 (r = sshpkt_put_cstring(ssh, options.kbd_interactive_devices ? 1918 options.kbd_interactive_devices : "")) != 0 || 1919 (r = sshpkt_send(ssh)) != 0) 1920 fatal_fr(r, "send packet"); 1921 1922 ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req); 1923 return 1; 1924} 1925 1926/* 1927 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE 1928 */ 1929static int 1930input_userauth_info_req(int type, u_int32_t seq, struct ssh *ssh) 1931{ 1932 Authctxt *authctxt = ssh->authctxt; 1933 char *name = NULL, *inst = NULL, *lang = NULL, *prompt = NULL; 1934 char *display_prompt = NULL, *response = NULL; 1935 u_char echo = 0; 1936 u_int num_prompts, i; 1937 int r; 1938 1939 debug2_f("entering"); 1940 1941 if (authctxt == NULL) 1942 fatal_f("no authentication context"); 1943 1944 authctxt->info_req_seen = 1; 1945 1946 if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 || 1947 (r = sshpkt_get_cstring(ssh, &inst, NULL)) != 0 || 1948 (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0) 1949 goto out; 1950 if (strlen(name) > 0) 1951 logit("%s", name); 1952 if (strlen(inst) > 0) 1953 logit("%s", inst); 1954 1955 if ((r = sshpkt_get_u32(ssh, &num_prompts)) != 0) 1956 goto out; 1957 /* 1958 * Begin to build info response packet based on prompts requested. 1959 * We commit to providing the correct number of responses, so if 1960 * further on we run into a problem that prevents this, we have to 1961 * be sure and clean this up and send a correct error response. 1962 */ 1963 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE)) != 0 || 1964 (r = sshpkt_put_u32(ssh, num_prompts)) != 0) 1965 goto out; 1966 1967 debug2_f("num_prompts %d", num_prompts); 1968 for (i = 0; i < num_prompts; i++) { 1969 if ((r = sshpkt_get_cstring(ssh, &prompt, NULL)) != 0 || 1970 (r = sshpkt_get_u8(ssh, &echo)) != 0) 1971 goto out; 1972 if (asmprintf(&display_prompt, INT_MAX, NULL, "(%s@%s) %s", 1973 authctxt->server_user, options.host_key_alias ? 1974 options.host_key_alias : authctxt->host, prompt) == -1) 1975 fatal_f("asmprintf failed"); 1976 response = read_passphrase(display_prompt, echo ? RP_ECHO : 0); 1977 if ((r = sshpkt_put_cstring(ssh, response)) != 0) 1978 goto out; 1979 freezero(response, strlen(response)); 1980 free(prompt); 1981 free(display_prompt); 1982 display_prompt = response = prompt = NULL; 1983 } 1984 /* done with parsing incoming message. */ 1985 if ((r = sshpkt_get_end(ssh)) != 0 || 1986 (r = sshpkt_add_padding(ssh, 64)) != 0) 1987 goto out; 1988 r = sshpkt_send(ssh); 1989 out: 1990 if (response) 1991 freezero(response, strlen(response)); 1992 free(prompt); 1993 free(display_prompt); 1994 free(name); 1995 free(inst); 1996 free(lang); 1997 return r; 1998} 1999 2000static int 2001ssh_keysign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp, 2002 const u_char *data, size_t datalen) 2003{ 2004 struct sshbuf *b; 2005 struct stat st; 2006 pid_t pid; 2007 int r, to[2], from[2], status; 2008 int sock = ssh_packet_get_connection_in(ssh); 2009 u_char rversion = 0, version = 2; 2010 void (*osigchld)(int); 2011 2012 *sigp = NULL; 2013 *lenp = 0; 2014 2015 if (stat(_PATH_SSH_KEY_SIGN, &st) == -1) { 2016 error_f("not installed: %s", strerror(errno)); 2017 return -1; 2018 } 2019 if (fflush(stdout) != 0) { 2020 error_f("fflush: %s", strerror(errno)); 2021 return -1; 2022 } 2023 if (pipe(to) == -1) { 2024 error_f("pipe: %s", strerror(errno)); 2025 return -1; 2026 } 2027 if (pipe(from) == -1) { 2028 error_f("pipe: %s", strerror(errno)); 2029 return -1; 2030 } 2031 if ((pid = fork()) == -1) { 2032 error_f("fork: %s", strerror(errno)); 2033 return -1; 2034 } 2035 osigchld = ssh_signal(SIGCHLD, SIG_DFL); 2036 if (pid == 0) { 2037 close(from[0]); 2038 if (dup2(from[1], STDOUT_FILENO) == -1) 2039 fatal_f("dup2: %s", strerror(errno)); 2040 close(to[1]); 2041 if (dup2(to[0], STDIN_FILENO) == -1) 2042 fatal_f("dup2: %s", strerror(errno)); 2043 close(from[1]); 2044 close(to[0]); 2045 2046 if (dup2(sock, STDERR_FILENO + 1) == -1) 2047 fatal_f("dup2: %s", strerror(errno)); 2048 sock = STDERR_FILENO + 1; 2049 if (fcntl(sock, F_SETFD, 0) == -1) /* keep the socket on exec */ 2050 debug3_f("fcntl F_SETFD: %s", strerror(errno)); 2051 closefrom(sock + 1); 2052 2053 debug3_f("[child] pid=%ld, exec %s", 2054 (long)getpid(), _PATH_SSH_KEY_SIGN); 2055 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL); 2056 fatal_f("exec(%s): %s", _PATH_SSH_KEY_SIGN, 2057 strerror(errno)); 2058 } 2059 close(from[1]); 2060 close(to[0]); 2061 sock = STDERR_FILENO + 1; 2062 2063 if ((b = sshbuf_new()) == NULL) 2064 fatal_f("sshbuf_new failed"); 2065 /* send # of sock, data to be signed */ 2066 if ((r = sshbuf_put_u32(b, sock)) != 0 || 2067 (r = sshbuf_put_string(b, data, datalen)) != 0) 2068 fatal_fr(r, "buffer error"); 2069 if (ssh_msg_send(to[1], version, b) == -1) 2070 fatal_f("couldn't send request"); 2071 sshbuf_reset(b); 2072 r = ssh_msg_recv(from[0], b); 2073 close(from[0]); 2074 close(to[1]); 2075 if (r < 0) { 2076 error_f("no reply"); 2077 goto fail; 2078 } 2079 2080 errno = 0; 2081 while (waitpid(pid, &status, 0) == -1) { 2082 if (errno != EINTR) { 2083 error_f("waitpid %ld: %s", (long)pid, strerror(errno)); 2084 goto fail; 2085 } 2086 } 2087 if (!WIFEXITED(status)) { 2088 error_f("exited abnormally"); 2089 goto fail; 2090 } 2091 if (WEXITSTATUS(status) != 0) { 2092 error_f("exited with status %d", WEXITSTATUS(status)); 2093 goto fail; 2094 } 2095 if ((r = sshbuf_get_u8(b, &rversion)) != 0) { 2096 error_fr(r, "buffer error"); 2097 goto fail; 2098 } 2099 if (rversion != version) { 2100 error_f("bad version"); 2101 goto fail; 2102 } 2103 if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) { 2104 error_fr(r, "buffer error"); 2105 fail: 2106 ssh_signal(SIGCHLD, osigchld); 2107 sshbuf_free(b); 2108 return -1; 2109 } 2110 ssh_signal(SIGCHLD, osigchld); 2111 sshbuf_free(b); 2112 2113 return 0; 2114} 2115 2116static int 2117userauth_hostbased(struct ssh *ssh) 2118{ 2119 Authctxt *authctxt = (Authctxt *)ssh->authctxt; 2120 struct sshkey *private = NULL; 2121 struct sshbuf *b = NULL; 2122 u_char *sig = NULL, *keyblob = NULL; 2123 char *fp = NULL, *chost = NULL, *lname = NULL; 2124 size_t siglen = 0, keylen = 0; 2125 int i, r, success = 0; 2126 2127 if (authctxt->ktypes == NULL) { 2128 authctxt->oktypes = xstrdup(options.hostbased_accepted_algos); 2129 authctxt->ktypes = authctxt->oktypes; 2130 } 2131 2132 /* 2133 * Work through each listed type pattern in HostbasedAcceptedAlgorithms, 2134 * trying each hostkey that matches the type in turn. 2135 */ 2136 for (;;) { 2137 if (authctxt->active_ktype == NULL) 2138 authctxt->active_ktype = strsep(&authctxt->ktypes, ","); 2139 if (authctxt->active_ktype == NULL || 2140 *authctxt->active_ktype == '\0') 2141 break; 2142 debug3_f("trying key type %s", authctxt->active_ktype); 2143 2144 /* check for a useful key */ 2145 private = NULL; 2146 for (i = 0; i < authctxt->sensitive->nkeys; i++) { 2147 if (authctxt->sensitive->keys[i] == NULL || 2148 authctxt->sensitive->keys[i]->type == KEY_UNSPEC) 2149 continue; 2150 if (!sshkey_match_keyname_to_sigalgs( 2151 sshkey_ssh_name(authctxt->sensitive->keys[i]), 2152 authctxt->active_ktype)) 2153 continue; 2154 /* we take and free the key */ 2155 private = authctxt->sensitive->keys[i]; 2156 authctxt->sensitive->keys[i] = NULL; 2157 break; 2158 } 2159 /* Found one */ 2160 if (private != NULL) 2161 break; 2162 /* No more keys of this type; advance */ 2163 authctxt->active_ktype = NULL; 2164 } 2165 if (private == NULL) { 2166 free(authctxt->oktypes); 2167 authctxt->oktypes = authctxt->ktypes = NULL; 2168 authctxt->active_ktype = NULL; 2169 debug("No more client hostkeys for hostbased authentication."); 2170 goto out; 2171 } 2172 2173 if ((fp = sshkey_fingerprint(private, options.fingerprint_hash, 2174 SSH_FP_DEFAULT)) == NULL) { 2175 error_f("sshkey_fingerprint failed"); 2176 goto out; 2177 } 2178 debug_f("trying hostkey %s %s using sigalg %s", 2179 sshkey_ssh_name(private), fp, authctxt->active_ktype); 2180 2181 /* figure out a name for the client host */ 2182 lname = get_local_name(ssh_packet_get_connection_in(ssh)); 2183 if (lname == NULL) { 2184 error_f("cannot get local ipaddr/name"); 2185 goto out; 2186 } 2187 2188 /* XXX sshbuf_put_stringf? */ 2189 xasprintf(&chost, "%s.", lname); 2190 debug2_f("chost %s", chost); 2191 2192 /* construct data */ 2193 if ((b = sshbuf_new()) == NULL) { 2194 error_f("sshbuf_new failed"); 2195 goto out; 2196 } 2197 if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) { 2198 error_fr(r, "sshkey_to_blob"); 2199 goto out; 2200 } 2201 if ((r = sshbuf_put_stringb(b, ssh->kex->session_id)) != 0 || 2202 (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 2203 (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 || 2204 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 || 2205 (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 || 2206 (r = sshbuf_put_cstring(b, authctxt->active_ktype)) != 0 || 2207 (r = sshbuf_put_string(b, keyblob, keylen)) != 0 || 2208 (r = sshbuf_put_cstring(b, chost)) != 0 || 2209 (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) { 2210 error_fr(r, "buffer error"); 2211 goto out; 2212 } 2213 2214#ifdef DEBUG_PK 2215 sshbuf_dump(b, stderr); 2216#endif 2217 if ((r = ssh_keysign(ssh, private, &sig, &siglen, 2218 sshbuf_ptr(b), sshbuf_len(b))) != 0) { 2219 error("sign using hostkey %s %s failed", 2220 sshkey_ssh_name(private), fp); 2221 goto out; 2222 } 2223 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 2224 (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 || 2225 (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 || 2226 (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 || 2227 (r = sshpkt_put_cstring(ssh, authctxt->active_ktype)) != 0 || 2228 (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 || 2229 (r = sshpkt_put_cstring(ssh, chost)) != 0 || 2230 (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 || 2231 (r = sshpkt_put_string(ssh, sig, siglen)) != 0 || 2232 (r = sshpkt_send(ssh)) != 0) { 2233 error_fr(r, "packet error"); 2234 goto out; 2235 } 2236 success = 1; 2237 2238 out: 2239 if (sig != NULL) 2240 freezero(sig, siglen); 2241 free(keyblob); 2242 free(lname); 2243 free(fp); 2244 free(chost); 2245 sshkey_free(private); 2246 sshbuf_free(b); 2247 2248 return success; 2249} 2250 2251/* find auth method */ 2252 2253/* 2254 * given auth method name, if configurable options permit this method fill 2255 * in auth_ident field and return true, otherwise return false. 2256 */ 2257static int 2258authmethod_is_enabled(Authmethod *method) 2259{ 2260 if (method == NULL) 2261 return 0; 2262 /* return false if options indicate this method is disabled */ 2263 if (method->enabled == NULL || *method->enabled == 0) 2264 return 0; 2265 /* return false if batch mode is enabled but method needs interactive mode */ 2266 if (method->batch_flag != NULL && *method->batch_flag != 0) 2267 return 0; 2268 return 1; 2269} 2270 2271static Authmethod * 2272authmethod_lookup(const char *name) 2273{ 2274 Authmethod *method = NULL; 2275 if (name != NULL) 2276 for (method = authmethods; method->name != NULL; method++) 2277 if (strcmp(name, method->name) == 0) 2278 return method; 2279 debug2("Unrecognized authentication method name: %s", name ? name : "NULL"); 2280 return NULL; 2281} 2282 2283/* XXX internal state */ 2284static Authmethod *current = NULL; 2285static char *supported = NULL; 2286static char *preferred = NULL; 2287 2288/* 2289 * Given the authentication method list sent by the server, return the 2290 * next method we should try. If the server initially sends a nil list, 2291 * use a built-in default list. 2292 */ 2293static Authmethod * 2294authmethod_get(char *authlist) 2295{ 2296 char *name = NULL; 2297 u_int next; 2298 2299 /* Use a suitable default if we're passed a nil list. */ 2300 if (authlist == NULL || strlen(authlist) == 0) 2301 authlist = options.preferred_authentications; 2302 2303 if (supported == NULL || strcmp(authlist, supported) != 0) { 2304 debug3("start over, passed a different list %s", authlist); 2305 free(supported); 2306 supported = xstrdup(authlist); 2307 preferred = options.preferred_authentications; 2308 debug3("preferred %s", preferred); 2309 current = NULL; 2310 } else if (current != NULL && authmethod_is_enabled(current)) 2311 return current; 2312 2313 for (;;) { 2314 if ((name = match_list(preferred, supported, &next)) == NULL) { 2315 debug("No more authentication methods to try."); 2316 current = NULL; 2317 return NULL; 2318 } 2319 preferred += next; 2320 debug3("authmethod_lookup %s", name); 2321 debug3("remaining preferred: %s", preferred); 2322 if ((current = authmethod_lookup(name)) != NULL && 2323 authmethod_is_enabled(current)) { 2324 debug3("authmethod_is_enabled %s", name); 2325 debug("Next authentication method: %s", name); 2326 free(name); 2327 return current; 2328 } 2329 free(name); 2330 } 2331} 2332 2333static char * 2334authmethods_get(void) 2335{ 2336 Authmethod *method = NULL; 2337 struct sshbuf *b; 2338 char *list; 2339 int r; 2340 2341 if ((b = sshbuf_new()) == NULL) 2342 fatal_f("sshbuf_new failed"); 2343 for (method = authmethods; method->name != NULL; method++) { 2344 if (authmethod_is_enabled(method)) { 2345 if ((r = sshbuf_putf(b, "%s%s", 2346 sshbuf_len(b) ? "," : "", method->name)) != 0) 2347 fatal_fr(r, "buffer error"); 2348 } 2349 } 2350 if ((list = sshbuf_dup_string(b)) == NULL) 2351 fatal_f("sshbuf_dup_string failed"); 2352 sshbuf_free(b); 2353 return list; 2354}