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