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