1 /* $OpenBSD: auth2-pubkey.c,v 1.99 2020/02/06 22:30:54 naddy Exp $ */ 2 /* 3 * Copyright (c) 2000 Markus Friedl. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 27 #include <sys/types.h> 28 #include <sys/stat.h> 29 30 #include <stdlib.h> 31 #include <errno.h> 32 #include <fcntl.h> 33 #include <paths.h> 34 #include <pwd.h> 35 #include <signal.h> 36 #include <stdio.h> 37 #include <stdarg.h> 38 #include <string.h> 39 #include <time.h> 40 #include <unistd.h> 41 #include <limits.h> 42 43 #include "xmalloc.h" 44 #include "ssh.h" 45 #include "ssh2.h" 46 #include "packet.h" 47 #include "sshbuf.h" 48 #include "log.h" 49 #include "misc.h" 50 #include "servconf.h" 51 #include "compat.h" 52 #include "sshkey.h" 53 #include "hostfile.h" 54 #include "auth.h" 55 #include "pathnames.h" 56 #include "uidswap.h" 57 #include "auth-options.h" 58 #include "canohost.h" 59 #ifdef GSSAPI 60 #include "ssh-gss.h" 61 #endif 62 #include "monitor_wrap.h" 63 #include "authfile.h" 64 #include "match.h" 65 #include "ssherr.h" 66 #include "channels.h" /* XXX for session.h */ 67 #include "session.h" /* XXX for child_set_env(); refactor? */ 68 #include "sk-api.h" 69 70 /* import */ 71 extern ServerOptions options; 72 extern u_char *session_id2; 73 extern u_int session_id2_len; 74 75 static char * 76 format_key(const struct sshkey *key) 77 { 78 char *ret, *fp = sshkey_fingerprint(key, 79 options.fingerprint_hash, SSH_FP_DEFAULT); 80 81 xasprintf(&ret, "%s %s", sshkey_type(key), fp); 82 free(fp); 83 return ret; 84 } 85 86 static int 87 userauth_pubkey(struct ssh *ssh) 88 { 89 Authctxt *authctxt = ssh->authctxt; 90 struct passwd *pw = authctxt->pw; 91 struct sshbuf *b = NULL; 92 struct sshkey *key = NULL; 93 char *pkalg = NULL, *userstyle = NULL, *key_s = NULL, *ca_s = NULL; 94 u_char *pkblob = NULL, *sig = NULL, have_sig; 95 size_t blen, slen; 96 int r, pktype; 97 int req_presence = 0, authenticated = 0; 98 struct sshauthopt *authopts = NULL; 99 struct sshkey_sig_details *sig_details = NULL; 100 101 if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0 || 102 (r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 || 103 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0) 104 fatal("%s: parse request failed: %s", __func__, ssh_err(r)); 105 106 if (log_level_get() >= SYSLOG_LEVEL_DEBUG2) { 107 char *keystring; 108 struct sshbuf *pkbuf; 109 110 if ((pkbuf = sshbuf_from(pkblob, blen)) == NULL) 111 fatal("%s: sshbuf_from failed", __func__); 112 if ((keystring = sshbuf_dtob64_string(pkbuf, 0)) == NULL) 113 fatal("%s: sshbuf_dtob64 failed", __func__); 114 debug2("%s: %s user %s %s public key %s %s", __func__, 115 authctxt->valid ? "valid" : "invalid", authctxt->user, 116 have_sig ? "attempting" : "querying", pkalg, keystring); 117 sshbuf_free(pkbuf); 118 free(keystring); 119 } 120 121 pktype = sshkey_type_from_name(pkalg); 122 if (pktype == KEY_UNSPEC) { 123 /* this is perfectly legal */ 124 verbose("%s: unsupported public key algorithm: %s", 125 __func__, pkalg); 126 goto done; 127 } 128 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) { 129 error("%s: could not parse key: %s", __func__, ssh_err(r)); 130 goto done; 131 } 132 if (key == NULL) { 133 error("%s: cannot decode key: %s", __func__, pkalg); 134 goto done; 135 } 136 if (key->type != pktype) { 137 error("%s: type mismatch for decoded key " 138 "(received %d, expected %d)", __func__, key->type, pktype); 139 goto done; 140 } 141 if (sshkey_type_plain(key->type) == KEY_RSA && 142 (ssh->compat & SSH_BUG_RSASIGMD5) != 0) { 143 logit("Refusing RSA key because client uses unsafe " 144 "signature scheme"); 145 goto done; 146 } 147 if (auth2_key_already_used(authctxt, key)) { 148 logit("refusing previously-used %s key", sshkey_type(key)); 149 goto done; 150 } 151 if (match_pattern_list(pkalg, options.pubkey_key_types, 0) != 1) { 152 logit("%s: key type %s not in PubkeyAcceptedKeyTypes", 153 __func__, sshkey_ssh_name(key)); 154 goto done; 155 } 156 if ((r = sshkey_check_cert_sigtype(key, 157 options.ca_sign_algorithms)) != 0) { 158 logit("%s: certificate signature algorithm %s: %s", __func__, 159 (key->cert == NULL || key->cert->signature_type == NULL) ? 160 "(null)" : key->cert->signature_type, ssh_err(r)); 161 goto done; 162 } 163 key_s = format_key(key); 164 if (sshkey_is_cert(key)) 165 ca_s = format_key(key->cert->signature_key); 166 167 if (have_sig) { 168 debug3("%s: have %s signature for %s%s%s", 169 __func__, pkalg, key_s, 170 ca_s == NULL ? "" : " CA ", 171 ca_s == NULL ? "" : ca_s); 172 if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 || 173 (r = sshpkt_get_end(ssh)) != 0) 174 fatal("%s: %s", __func__, ssh_err(r)); 175 if ((b = sshbuf_new()) == NULL) 176 fatal("%s: sshbuf_new failed", __func__); 177 if (ssh->compat & SSH_OLD_SESSIONID) { 178 if ((r = sshbuf_put(b, session_id2, 179 session_id2_len)) != 0) 180 fatal("%s: sshbuf_put session id: %s", 181 __func__, ssh_err(r)); 182 } else { 183 if ((r = sshbuf_put_string(b, session_id2, 184 session_id2_len)) != 0) 185 fatal("%s: sshbuf_put_string session id: %s", 186 __func__, ssh_err(r)); 187 } 188 if (!authctxt->valid || authctxt->user == NULL) { 189 debug2("%s: disabled because of invalid user", 190 __func__); 191 goto done; 192 } 193 /* reconstruct packet */ 194 xasprintf(&userstyle, "%s%s%s", authctxt->user, 195 authctxt->style ? ":" : "", 196 authctxt->style ? authctxt->style : ""); 197 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 || 198 (r = sshbuf_put_cstring(b, userstyle)) != 0 || 199 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 || 200 (r = sshbuf_put_cstring(b, "publickey")) != 0 || 201 (r = sshbuf_put_u8(b, have_sig)) != 0 || 202 (r = sshbuf_put_cstring(b, pkalg)) != 0 || 203 (r = sshbuf_put_string(b, pkblob, blen)) != 0) 204 fatal("%s: build packet failed: %s", 205 __func__, ssh_err(r)); 206 #ifdef DEBUG_PK 207 sshbuf_dump(b, stderr); 208 #endif 209 /* test for correct signature */ 210 authenticated = 0; 211 if (PRIVSEP(user_key_allowed(ssh, pw, key, 1, &authopts)) && 212 PRIVSEP(sshkey_verify(key, sig, slen, 213 sshbuf_ptr(b), sshbuf_len(b), 214 (ssh->compat & SSH_BUG_SIGTYPE) == 0 ? pkalg : NULL, 215 ssh->compat, &sig_details)) == 0) { 216 authenticated = 1; 217 } 218 if (authenticated == 1 && sig_details != NULL) { 219 auth2_record_info(authctxt, "signature count = %u", 220 sig_details->sk_counter); 221 debug("%s: sk_counter = %u, sk_flags = 0x%02x", 222 __func__, sig_details->sk_counter, 223 sig_details->sk_flags); 224 req_presence = (options.pubkey_auth_options & 225 PUBKEYAUTH_TOUCH_REQUIRED) || 226 !authopts->no_require_user_presence; 227 if (req_presence && (sig_details->sk_flags & 228 SSH_SK_USER_PRESENCE_REQD) == 0) { 229 error("public key %s signature for %s%s from " 230 "%.128s port %d rejected: user presence " 231 "(authenticator touch) requirement " 232 "not met ", key_s, 233 authctxt->valid ? "" : "invalid user ", 234 authctxt->user, ssh_remote_ipaddr(ssh), 235 ssh_remote_port(ssh)); 236 authenticated = 0; 237 goto done; 238 } 239 } 240 auth2_record_key(authctxt, authenticated, key); 241 } else { 242 debug("%s: test pkalg %s pkblob %s%s%s", 243 __func__, pkalg, key_s, 244 ca_s == NULL ? "" : " CA ", 245 ca_s == NULL ? "" : ca_s); 246 247 if ((r = sshpkt_get_end(ssh)) != 0) 248 fatal("%s: %s", __func__, ssh_err(r)); 249 250 if (!authctxt->valid || authctxt->user == NULL) { 251 debug2("%s: disabled because of invalid user", 252 __func__); 253 goto done; 254 } 255 /* XXX fake reply and always send PK_OK ? */ 256 /* 257 * XXX this allows testing whether a user is allowed 258 * to login: if you happen to have a valid pubkey this 259 * message is sent. the message is NEVER sent at all 260 * if a user is not allowed to login. is this an 261 * issue? -markus 262 */ 263 if (PRIVSEP(user_key_allowed(ssh, pw, key, 0, NULL))) { 264 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK)) 265 != 0 || 266 (r = sshpkt_put_cstring(ssh, pkalg)) != 0 || 267 (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 || 268 (r = sshpkt_send(ssh)) != 0 || 269 (r = ssh_packet_write_wait(ssh)) != 0) 270 fatal("%s: %s", __func__, ssh_err(r)); 271 authctxt->postponed = 1; 272 } 273 } 274 done: 275 if (authenticated == 1 && auth_activate_options(ssh, authopts) != 0) { 276 debug("%s: key options inconsistent with existing", __func__); 277 authenticated = 0; 278 } 279 debug2("%s: authenticated %d pkalg %s", __func__, authenticated, pkalg); 280 281 sshbuf_free(b); 282 sshauthopt_free(authopts); 283 sshkey_free(key); 284 free(userstyle); 285 free(pkalg); 286 free(pkblob); 287 free(key_s); 288 free(ca_s); 289 free(sig); 290 sshkey_sig_details_free(sig_details); 291 return authenticated; 292 } 293 294 static int 295 match_principals_option(const char *principal_list, struct sshkey_cert *cert) 296 { 297 char *result; 298 u_int i; 299 300 /* XXX percent_expand() sequences for authorized_principals? */ 301 302 for (i = 0; i < cert->nprincipals; i++) { 303 if ((result = match_list(cert->principals[i], 304 principal_list, NULL)) != NULL) { 305 debug3("matched principal from key options \"%.100s\"", 306 result); 307 free(result); 308 return 1; 309 } 310 } 311 return 0; 312 } 313 314 /* 315 * Process a single authorized_principals format line. Returns 0 and sets 316 * authoptsp is principal is authorised, -1 otherwise. "loc" is used as a 317 * log preamble for file/line information. 318 */ 319 static int 320 check_principals_line(struct ssh *ssh, char *cp, const struct sshkey_cert *cert, 321 const char *loc, struct sshauthopt **authoptsp) 322 { 323 u_int i, found = 0; 324 char *ep, *line_opts; 325 const char *reason = NULL; 326 struct sshauthopt *opts = NULL; 327 328 if (authoptsp != NULL) 329 *authoptsp = NULL; 330 331 /* Trim trailing whitespace. */ 332 ep = cp + strlen(cp) - 1; 333 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t')) 334 *ep-- = '\0'; 335 336 /* 337 * If the line has internal whitespace then assume it has 338 * key options. 339 */ 340 line_opts = NULL; 341 if ((ep = strrchr(cp, ' ')) != NULL || 342 (ep = strrchr(cp, '\t')) != NULL) { 343 for (; *ep == ' ' || *ep == '\t'; ep++) 344 ; 345 line_opts = cp; 346 cp = ep; 347 } 348 if ((opts = sshauthopt_parse(line_opts, &reason)) == NULL) { 349 debug("%s: bad principals options: %s", loc, reason); 350 auth_debug_add("%s: bad principals options: %s", loc, reason); 351 return -1; 352 } 353 /* Check principals in cert against those on line */ 354 for (i = 0; i < cert->nprincipals; i++) { 355 if (strcmp(cp, cert->principals[i]) != 0) 356 continue; 357 debug3("%s: matched principal \"%.100s\"", 358 loc, cert->principals[i]); 359 found = 1; 360 } 361 if (found && authoptsp != NULL) { 362 *authoptsp = opts; 363 opts = NULL; 364 } 365 sshauthopt_free(opts); 366 return found ? 0 : -1; 367 } 368 369 static int 370 process_principals(struct ssh *ssh, FILE *f, const char *file, 371 const struct sshkey_cert *cert, struct sshauthopt **authoptsp) 372 { 373 char loc[256], *line = NULL, *cp, *ep; 374 size_t linesize = 0; 375 u_long linenum = 0; 376 u_int found_principal = 0; 377 378 if (authoptsp != NULL) 379 *authoptsp = NULL; 380 381 while (getline(&line, &linesize, f) != -1) { 382 linenum++; 383 /* Always consume entire input */ 384 if (found_principal) 385 continue; 386 387 /* Skip leading whitespace. */ 388 for (cp = line; *cp == ' ' || *cp == '\t'; cp++) 389 ; 390 /* Skip blank and comment lines. */ 391 if ((ep = strchr(cp, '#')) != NULL) 392 *ep = '\0'; 393 if (!*cp || *cp == '\n') 394 continue; 395 396 snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum); 397 if (check_principals_line(ssh, cp, cert, loc, authoptsp) == 0) 398 found_principal = 1; 399 } 400 free(line); 401 return found_principal; 402 } 403 404 /* XXX remove pw args here and elsewhere once ssh->authctxt is guaranteed */ 405 406 static int 407 match_principals_file(struct ssh *ssh, struct passwd *pw, char *file, 408 struct sshkey_cert *cert, struct sshauthopt **authoptsp) 409 { 410 FILE *f; 411 int success; 412 413 if (authoptsp != NULL) 414 *authoptsp = NULL; 415 416 temporarily_use_uid(pw); 417 debug("trying authorized principals file %s", file); 418 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) { 419 restore_uid(); 420 return 0; 421 } 422 success = process_principals(ssh, f, file, cert, authoptsp); 423 fclose(f); 424 restore_uid(); 425 return success; 426 } 427 428 /* 429 * Checks whether principal is allowed in output of command. 430 * returns 1 if the principal is allowed or 0 otherwise. 431 */ 432 static int 433 match_principals_command(struct ssh *ssh, struct passwd *user_pw, 434 const struct sshkey *key, struct sshauthopt **authoptsp) 435 { 436 struct passwd *runas_pw = NULL; 437 const struct sshkey_cert *cert = key->cert; 438 FILE *f = NULL; 439 int r, ok, found_principal = 0; 440 int i, ac = 0, uid_swapped = 0; 441 pid_t pid; 442 char *tmp, *username = NULL, *command = NULL, **av = NULL; 443 char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL; 444 char serial_s[32], uidstr[32]; 445 void (*osigchld)(int); 446 447 if (authoptsp != NULL) 448 *authoptsp = NULL; 449 if (options.authorized_principals_command == NULL) 450 return 0; 451 if (options.authorized_principals_command_user == NULL) { 452 error("No user for AuthorizedPrincipalsCommand specified, " 453 "skipping"); 454 return 0; 455 } 456 457 /* 458 * NB. all returns later this function should go via "out" to 459 * ensure the original SIGCHLD handler is restored properly. 460 */ 461 osigchld = ssh_signal(SIGCHLD, SIG_DFL); 462 463 /* Prepare and verify the user for the command */ 464 username = percent_expand(options.authorized_principals_command_user, 465 "u", user_pw->pw_name, (char *)NULL); 466 runas_pw = getpwnam(username); 467 if (runas_pw == NULL) { 468 error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s", 469 username, strerror(errno)); 470 goto out; 471 } 472 473 /* Turn the command into an argument vector */ 474 if (argv_split(options.authorized_principals_command, &ac, &av) != 0) { 475 error("AuthorizedPrincipalsCommand \"%s\" contains " 476 "invalid quotes", options.authorized_principals_command); 477 goto out; 478 } 479 if (ac == 0) { 480 error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments", 481 options.authorized_principals_command); 482 goto out; 483 } 484 if ((ca_fp = sshkey_fingerprint(cert->signature_key, 485 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) { 486 error("%s: sshkey_fingerprint failed", __func__); 487 goto out; 488 } 489 if ((key_fp = sshkey_fingerprint(key, 490 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) { 491 error("%s: sshkey_fingerprint failed", __func__); 492 goto out; 493 } 494 if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) { 495 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); 496 goto out; 497 } 498 if ((r = sshkey_to_base64(key, &keytext)) != 0) { 499 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); 500 goto out; 501 } 502 snprintf(serial_s, sizeof(serial_s), "%llu", 503 (unsigned long long)cert->serial); 504 snprintf(uidstr, sizeof(uidstr), "%llu", 505 (unsigned long long)user_pw->pw_uid); 506 for (i = 1; i < ac; i++) { 507 tmp = percent_expand(av[i], 508 "U", uidstr, 509 "u", user_pw->pw_name, 510 "h", user_pw->pw_dir, 511 "t", sshkey_ssh_name(key), 512 "T", sshkey_ssh_name(cert->signature_key), 513 "f", key_fp, 514 "F", ca_fp, 515 "k", keytext, 516 "K", catext, 517 "i", cert->key_id, 518 "s", serial_s, 519 (char *)NULL); 520 if (tmp == NULL) 521 fatal("%s: percent_expand failed", __func__); 522 free(av[i]); 523 av[i] = tmp; 524 } 525 /* Prepare a printable command for logs, etc. */ 526 command = argv_assemble(ac, av); 527 528 if ((pid = subprocess("AuthorizedPrincipalsCommand", runas_pw, command, 529 ac, av, &f, 530 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0) 531 goto out; 532 533 uid_swapped = 1; 534 temporarily_use_uid(runas_pw); 535 536 ok = process_principals(ssh, f, "(command)", cert, authoptsp); 537 538 fclose(f); 539 f = NULL; 540 541 if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0) 542 goto out; 543 544 /* Read completed successfully */ 545 found_principal = ok; 546 out: 547 if (f != NULL) 548 fclose(f); 549 ssh_signal(SIGCHLD, osigchld); 550 for (i = 0; i < ac; i++) 551 free(av[i]); 552 free(av); 553 if (uid_swapped) 554 restore_uid(); 555 free(command); 556 free(username); 557 free(ca_fp); 558 free(key_fp); 559 free(catext); 560 free(keytext); 561 return found_principal; 562 } 563 564 /* 565 * Check a single line of an authorized_keys-format file. Returns 0 if key 566 * matches, -1 otherwise. Will return key/cert options via *authoptsp 567 * on success. "loc" is used as file/line location in log messages. 568 */ 569 static int 570 check_authkey_line(struct ssh *ssh, struct passwd *pw, struct sshkey *key, 571 char *cp, const char *loc, struct sshauthopt **authoptsp) 572 { 573 int want_keytype = sshkey_is_cert(key) ? KEY_UNSPEC : key->type; 574 struct sshkey *found = NULL; 575 struct sshauthopt *keyopts = NULL, *certopts = NULL, *finalopts = NULL; 576 char *key_options = NULL, *fp = NULL; 577 const char *reason = NULL; 578 int ret = -1; 579 580 if (authoptsp != NULL) 581 *authoptsp = NULL; 582 583 if ((found = sshkey_new(want_keytype)) == NULL) { 584 debug3("%s: keytype %d failed", __func__, want_keytype); 585 goto out; 586 } 587 588 /* XXX djm: peek at key type in line and skip if unwanted */ 589 590 if (sshkey_read(found, &cp) != 0) { 591 /* no key? check for options */ 592 debug2("%s: check options: '%s'", loc, cp); 593 key_options = cp; 594 if (sshkey_advance_past_options(&cp) != 0) { 595 reason = "invalid key option string"; 596 goto fail_reason; 597 } 598 skip_space(&cp); 599 if (sshkey_read(found, &cp) != 0) { 600 /* still no key? advance to next line*/ 601 debug2("%s: advance: '%s'", loc, cp); 602 goto out; 603 } 604 } 605 /* Parse key options now; we need to know if this is a CA key */ 606 if ((keyopts = sshauthopt_parse(key_options, &reason)) == NULL) { 607 debug("%s: bad key options: %s", loc, reason); 608 auth_debug_add("%s: bad key options: %s", loc, reason); 609 goto out; 610 } 611 /* Ignore keys that don't match or incorrectly marked as CAs */ 612 if (sshkey_is_cert(key)) { 613 /* Certificate; check signature key against CA */ 614 if (!sshkey_equal(found, key->cert->signature_key) || 615 !keyopts->cert_authority) 616 goto out; 617 } else { 618 /* Plain key: check it against key found in file */ 619 if (!sshkey_equal(found, key) || keyopts->cert_authority) 620 goto out; 621 } 622 623 /* We have a candidate key, perform authorisation checks */ 624 if ((fp = sshkey_fingerprint(found, 625 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) 626 fatal("%s: fingerprint failed", __func__); 627 628 debug("%s: matching %s found: %s %s", loc, 629 sshkey_is_cert(key) ? "CA" : "key", sshkey_type(found), fp); 630 631 if (auth_authorise_keyopts(ssh, pw, keyopts, 632 sshkey_is_cert(key), loc) != 0) { 633 reason = "Refused by key options"; 634 goto fail_reason; 635 } 636 /* That's all we need for plain keys. */ 637 if (!sshkey_is_cert(key)) { 638 verbose("Accepted key %s %s found at %s", 639 sshkey_type(found), fp, loc); 640 finalopts = keyopts; 641 keyopts = NULL; 642 goto success; 643 } 644 645 /* 646 * Additional authorisation for certificates. 647 */ 648 649 /* Parse and check options present in certificate */ 650 if ((certopts = sshauthopt_from_cert(key)) == NULL) { 651 reason = "Invalid certificate options"; 652 goto fail_reason; 653 } 654 if (auth_authorise_keyopts(ssh, pw, certopts, 0, loc) != 0) { 655 reason = "Refused by certificate options"; 656 goto fail_reason; 657 } 658 if ((finalopts = sshauthopt_merge(keyopts, certopts, &reason)) == NULL) 659 goto fail_reason; 660 661 /* 662 * If the user has specified a list of principals as 663 * a key option, then prefer that list to matching 664 * their username in the certificate principals list. 665 */ 666 if (keyopts->cert_principals != NULL && 667 !match_principals_option(keyopts->cert_principals, key->cert)) { 668 reason = "Certificate does not contain an authorized principal"; 669 goto fail_reason; 670 } 671 if (sshkey_cert_check_authority(key, 0, 0, 672 keyopts->cert_principals == NULL ? pw->pw_name : NULL, &reason) != 0) 673 goto fail_reason; 674 675 verbose("Accepted certificate ID \"%s\" (serial %llu) " 676 "signed by CA %s %s found at %s", 677 key->cert->key_id, 678 (unsigned long long)key->cert->serial, 679 sshkey_type(found), fp, loc); 680 681 success: 682 if (finalopts == NULL) 683 fatal("%s: internal error: missing options", __func__); 684 if (authoptsp != NULL) { 685 *authoptsp = finalopts; 686 finalopts = NULL; 687 } 688 /* success */ 689 ret = 0; 690 goto out; 691 692 fail_reason: 693 error("%s", reason); 694 auth_debug_add("%s", reason); 695 out: 696 free(fp); 697 sshauthopt_free(keyopts); 698 sshauthopt_free(certopts); 699 sshauthopt_free(finalopts); 700 sshkey_free(found); 701 return ret; 702 } 703 704 /* 705 * Checks whether key is allowed in authorized_keys-format file, 706 * returns 1 if the key is allowed or 0 otherwise. 707 */ 708 static int 709 check_authkeys_file(struct ssh *ssh, struct passwd *pw, FILE *f, 710 char *file, struct sshkey *key, struct sshauthopt **authoptsp) 711 { 712 char *cp, *line = NULL, loc[256]; 713 size_t linesize = 0; 714 int found_key = 0; 715 u_long linenum = 0; 716 717 if (authoptsp != NULL) 718 *authoptsp = NULL; 719 720 while (getline(&line, &linesize, f) != -1) { 721 linenum++; 722 /* Always consume entire file */ 723 if (found_key) 724 continue; 725 726 /* Skip leading whitespace, empty and comment lines. */ 727 cp = line; 728 skip_space(&cp); 729 if (!*cp || *cp == '\n' || *cp == '#') 730 continue; 731 snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum); 732 if (check_authkey_line(ssh, pw, key, cp, loc, authoptsp) == 0) 733 found_key = 1; 734 } 735 free(line); 736 return found_key; 737 } 738 739 /* Authenticate a certificate key against TrustedUserCAKeys */ 740 static int 741 user_cert_trusted_ca(struct ssh *ssh, struct passwd *pw, struct sshkey *key, 742 struct sshauthopt **authoptsp) 743 { 744 char *ca_fp, *principals_file = NULL; 745 const char *reason; 746 struct sshauthopt *principals_opts = NULL, *cert_opts = NULL; 747 struct sshauthopt *final_opts = NULL; 748 int r, ret = 0, found_principal = 0, use_authorized_principals; 749 750 if (authoptsp != NULL) 751 *authoptsp = NULL; 752 753 if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL) 754 return 0; 755 756 if ((ca_fp = sshkey_fingerprint(key->cert->signature_key, 757 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) 758 return 0; 759 760 if ((r = sshkey_in_file(key->cert->signature_key, 761 options.trusted_user_ca_keys, 1, 0)) != 0) { 762 debug2("%s: CA %s %s is not listed in %s: %s", __func__, 763 sshkey_type(key->cert->signature_key), ca_fp, 764 options.trusted_user_ca_keys, ssh_err(r)); 765 goto out; 766 } 767 /* 768 * If AuthorizedPrincipals is in use, then compare the certificate 769 * principals against the names in that file rather than matching 770 * against the username. 771 */ 772 if ((principals_file = authorized_principals_file(pw)) != NULL) { 773 if (match_principals_file(ssh, pw, principals_file, 774 key->cert, &principals_opts)) 775 found_principal = 1; 776 } 777 /* Try querying command if specified */ 778 if (!found_principal && match_principals_command(ssh, pw, key, 779 &principals_opts)) 780 found_principal = 1; 781 /* If principals file or command is specified, then require a match */ 782 use_authorized_principals = principals_file != NULL || 783 options.authorized_principals_command != NULL; 784 if (!found_principal && use_authorized_principals) { 785 reason = "Certificate does not contain an authorized principal"; 786 goto fail_reason; 787 } 788 if (use_authorized_principals && principals_opts == NULL) 789 fatal("%s: internal error: missing principals_opts", __func__); 790 if (sshkey_cert_check_authority(key, 0, 1, 791 use_authorized_principals ? NULL : pw->pw_name, &reason) != 0) 792 goto fail_reason; 793 794 /* Check authority from options in key and from principals file/cmd */ 795 if ((cert_opts = sshauthopt_from_cert(key)) == NULL) { 796 reason = "Invalid certificate options"; 797 goto fail_reason; 798 } 799 if (auth_authorise_keyopts(ssh, pw, cert_opts, 0, "cert") != 0) { 800 reason = "Refused by certificate options"; 801 goto fail_reason; 802 } 803 if (principals_opts == NULL) { 804 final_opts = cert_opts; 805 cert_opts = NULL; 806 } else { 807 if (auth_authorise_keyopts(ssh, pw, principals_opts, 0, 808 "principals") != 0) { 809 reason = "Refused by certificate principals options"; 810 goto fail_reason; 811 } 812 if ((final_opts = sshauthopt_merge(principals_opts, 813 cert_opts, &reason)) == NULL) { 814 fail_reason: 815 error("%s", reason); 816 auth_debug_add("%s", reason); 817 goto out; 818 } 819 } 820 821 /* Success */ 822 verbose("Accepted certificate ID \"%s\" (serial %llu) signed by " 823 "%s CA %s via %s", key->cert->key_id, 824 (unsigned long long)key->cert->serial, 825 sshkey_type(key->cert->signature_key), ca_fp, 826 options.trusted_user_ca_keys); 827 if (authoptsp != NULL) { 828 *authoptsp = final_opts; 829 final_opts = NULL; 830 } 831 ret = 1; 832 out: 833 sshauthopt_free(principals_opts); 834 sshauthopt_free(cert_opts); 835 sshauthopt_free(final_opts); 836 free(principals_file); 837 free(ca_fp); 838 return ret; 839 } 840 841 /* 842 * Checks whether key is allowed in file. 843 * returns 1 if the key is allowed or 0 otherwise. 844 */ 845 static int 846 user_key_allowed2(struct ssh *ssh, struct passwd *pw, struct sshkey *key, 847 char *file, struct sshauthopt **authoptsp) 848 { 849 FILE *f; 850 int found_key = 0; 851 852 if (authoptsp != NULL) 853 *authoptsp = NULL; 854 855 /* Temporarily use the user's uid. */ 856 temporarily_use_uid(pw); 857 858 debug("trying public key file %s", file); 859 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) { 860 found_key = check_authkeys_file(ssh, pw, f, file, 861 key, authoptsp); 862 fclose(f); 863 } 864 865 restore_uid(); 866 return found_key; 867 } 868 869 /* 870 * Checks whether key is allowed in output of command. 871 * returns 1 if the key is allowed or 0 otherwise. 872 */ 873 static int 874 user_key_command_allowed2(struct ssh *ssh, struct passwd *user_pw, 875 struct sshkey *key, struct sshauthopt **authoptsp) 876 { 877 struct passwd *runas_pw = NULL; 878 FILE *f = NULL; 879 int r, ok, found_key = 0; 880 int i, uid_swapped = 0, ac = 0; 881 pid_t pid; 882 char *username = NULL, *key_fp = NULL, *keytext = NULL; 883 char uidstr[32], *tmp, *command = NULL, **av = NULL; 884 void (*osigchld)(int); 885 886 if (authoptsp != NULL) 887 *authoptsp = NULL; 888 if (options.authorized_keys_command == NULL) 889 return 0; 890 if (options.authorized_keys_command_user == NULL) { 891 error("No user for AuthorizedKeysCommand specified, skipping"); 892 return 0; 893 } 894 895 /* 896 * NB. all returns later this function should go via "out" to 897 * ensure the original SIGCHLD handler is restored properly. 898 */ 899 osigchld = ssh_signal(SIGCHLD, SIG_DFL); 900 901 /* Prepare and verify the user for the command */ 902 username = percent_expand(options.authorized_keys_command_user, 903 "u", user_pw->pw_name, (char *)NULL); 904 runas_pw = getpwnam(username); 905 if (runas_pw == NULL) { 906 error("AuthorizedKeysCommandUser \"%s\" not found: %s", 907 username, strerror(errno)); 908 goto out; 909 } 910 911 /* Prepare AuthorizedKeysCommand */ 912 if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash, 913 SSH_FP_DEFAULT)) == NULL) { 914 error("%s: sshkey_fingerprint failed", __func__); 915 goto out; 916 } 917 if ((r = sshkey_to_base64(key, &keytext)) != 0) { 918 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); 919 goto out; 920 } 921 922 /* Turn the command into an argument vector */ 923 if (argv_split(options.authorized_keys_command, &ac, &av) != 0) { 924 error("AuthorizedKeysCommand \"%s\" contains invalid quotes", 925 command); 926 goto out; 927 } 928 if (ac == 0) { 929 error("AuthorizedKeysCommand \"%s\" yielded no arguments", 930 command); 931 goto out; 932 } 933 snprintf(uidstr, sizeof(uidstr), "%llu", 934 (unsigned long long)user_pw->pw_uid); 935 for (i = 1; i < ac; i++) { 936 tmp = percent_expand(av[i], 937 "U", uidstr, 938 "u", user_pw->pw_name, 939 "h", user_pw->pw_dir, 940 "t", sshkey_ssh_name(key), 941 "f", key_fp, 942 "k", keytext, 943 (char *)NULL); 944 if (tmp == NULL) 945 fatal("%s: percent_expand failed", __func__); 946 free(av[i]); 947 av[i] = tmp; 948 } 949 /* Prepare a printable command for logs, etc. */ 950 command = argv_assemble(ac, av); 951 952 /* 953 * If AuthorizedKeysCommand was run without arguments 954 * then fall back to the old behaviour of passing the 955 * target username as a single argument. 956 */ 957 if (ac == 1) { 958 av = xreallocarray(av, ac + 2, sizeof(*av)); 959 av[1] = xstrdup(user_pw->pw_name); 960 av[2] = NULL; 961 /* Fix up command too, since it is used in log messages */ 962 free(command); 963 xasprintf(&command, "%s %s", av[0], av[1]); 964 } 965 966 if ((pid = subprocess("AuthorizedKeysCommand", runas_pw, command, 967 ac, av, &f, 968 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0) 969 goto out; 970 971 uid_swapped = 1; 972 temporarily_use_uid(runas_pw); 973 974 ok = check_authkeys_file(ssh, user_pw, f, 975 options.authorized_keys_command, key, authoptsp); 976 977 fclose(f); 978 f = NULL; 979 980 if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0) 981 goto out; 982 983 /* Read completed successfully */ 984 found_key = ok; 985 out: 986 if (f != NULL) 987 fclose(f); 988 ssh_signal(SIGCHLD, osigchld); 989 for (i = 0; i < ac; i++) 990 free(av[i]); 991 free(av); 992 if (uid_swapped) 993 restore_uid(); 994 free(command); 995 free(username); 996 free(key_fp); 997 free(keytext); 998 return found_key; 999 } 1000 1001 /* 1002 * Check whether key authenticates and authorises the user. 1003 */ 1004 int 1005 user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key, 1006 int auth_attempt, struct sshauthopt **authoptsp) 1007 { 1008 u_int success = 0, i; 1009 char *file; 1010 struct sshauthopt *opts = NULL; 1011 1012 if (authoptsp != NULL) 1013 *authoptsp = NULL; 1014 1015 if (auth_key_is_revoked(key)) 1016 return 0; 1017 if (sshkey_is_cert(key) && 1018 auth_key_is_revoked(key->cert->signature_key)) 1019 return 0; 1020 1021 for (i = 0; !success && i < options.num_authkeys_files; i++) { 1022 if (strcasecmp(options.authorized_keys_files[i], "none") == 0) 1023 continue; 1024 file = expand_authorized_keys( 1025 options.authorized_keys_files[i], pw); 1026 success = user_key_allowed2(ssh, pw, key, file, &opts); 1027 free(file); 1028 if (!success) { 1029 sshauthopt_free(opts); 1030 opts = NULL; 1031 } 1032 } 1033 if (success) 1034 goto out; 1035 1036 if ((success = user_cert_trusted_ca(ssh, pw, key, &opts)) != 0) 1037 goto out; 1038 sshauthopt_free(opts); 1039 opts = NULL; 1040 1041 if ((success = user_key_command_allowed2(ssh, pw, key, &opts)) != 0) 1042 goto out; 1043 sshauthopt_free(opts); 1044 opts = NULL; 1045 1046 out: 1047 if (success && authoptsp != NULL) { 1048 *authoptsp = opts; 1049 opts = NULL; 1050 } 1051 sshauthopt_free(opts); 1052 return success; 1053 } 1054 1055 Authmethod method_pubkey = { 1056 "publickey", 1057 userauth_pubkey, 1058 &options.pubkey_authentication 1059 }; 1060