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