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