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