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