1 /* $OpenBSD: x509_constraints.c,v 1.17 2021/09/23 15:49:48 jsing Exp $ */ 2 /* 3 * Copyright (c) 2020 Bob Beck <beck@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <ctype.h> 19 #include <errno.h> 20 #include <stdio.h> 21 #include <string.h> 22 #include <time.h> 23 #include <unistd.h> 24 25 #include <sys/socket.h> 26 #include <arpa/inet.h> 27 28 #include <openssl/safestack.h> 29 #include <openssl/x509.h> 30 #include <openssl/x509v3.h> 31 32 #include "x509_internal.h" 33 34 /* RFC 2821 section 4.5.3.1 */ 35 #define LOCAL_PART_MAX_LEN 64 36 #define DOMAIN_PART_MAX_LEN 255 37 38 struct x509_constraints_name * 39 x509_constraints_name_new(void) 40 { 41 return (calloc(1, sizeof(struct x509_constraints_name))); 42 } 43 44 void 45 x509_constraints_name_clear(struct x509_constraints_name *name) 46 { 47 free(name->name); 48 free(name->local); 49 free(name->der); 50 memset(name, 0, sizeof(*name)); 51 } 52 53 void 54 x509_constraints_name_free(struct x509_constraints_name *name) 55 { 56 if (name == NULL) 57 return; 58 x509_constraints_name_clear(name); 59 free(name); 60 } 61 62 struct x509_constraints_name * 63 x509_constraints_name_dup(struct x509_constraints_name *name) 64 { 65 struct x509_constraints_name *new; 66 67 if ((new = x509_constraints_name_new()) == NULL) 68 goto err; 69 new->type = name->type; 70 new->af = name->af; 71 new->der_len = name->der_len; 72 if (name->der_len > 0) { 73 if ((new->der = malloc(name->der_len)) == NULL) 74 goto err; 75 memcpy(new->der, name->der, name->der_len); 76 } 77 if (name->name != NULL && (new->name = strdup(name->name)) == NULL) 78 goto err; 79 if (name->local != NULL && (new->local = strdup(name->local)) == NULL) 80 goto err; 81 memcpy(new->address, name->address, sizeof(name->address)); 82 return new; 83 err: 84 x509_constraints_name_free(new); 85 return NULL; 86 } 87 88 struct x509_constraints_names * 89 x509_constraints_names_new(size_t names_max) 90 { 91 struct x509_constraints_names *new; 92 93 if ((new = calloc(1, sizeof(struct x509_constraints_names))) == NULL) 94 return NULL; 95 96 new->names_max = names_max; 97 98 return new; 99 } 100 101 void 102 x509_constraints_names_clear(struct x509_constraints_names *names) 103 { 104 size_t i; 105 106 for (i = 0; i < names->names_count; i++) 107 x509_constraints_name_free(names->names[i]); 108 free(names->names); 109 memset(names, 0, sizeof(*names)); 110 } 111 112 void 113 x509_constraints_names_free(struct x509_constraints_names *names) 114 { 115 if (names == NULL) 116 return; 117 118 x509_constraints_names_clear(names); 119 free(names); 120 } 121 122 int 123 x509_constraints_names_add(struct x509_constraints_names *names, 124 struct x509_constraints_name *name) 125 { 126 if (names->names_count >= names->names_max) 127 return 0; 128 if (names->names_count == names->names_len) { 129 struct x509_constraints_name **tmp; 130 if ((tmp = recallocarray(names->names, names->names_len, 131 names->names_len + 32, sizeof(*tmp))) == NULL) 132 return 0; 133 names->names_len += 32; 134 names->names = tmp; 135 } 136 names->names[names->names_count] = name; 137 names->names_count++; 138 return 1; 139 } 140 141 struct x509_constraints_names * 142 x509_constraints_names_dup(struct x509_constraints_names *names) 143 { 144 struct x509_constraints_names *new = NULL; 145 struct x509_constraints_name *name = NULL; 146 size_t i; 147 148 if (names == NULL) 149 return NULL; 150 151 if ((new = x509_constraints_names_new(names->names_max)) == NULL) 152 goto err; 153 154 for (i = 0; i < names->names_count; i++) { 155 if ((name = x509_constraints_name_dup(names->names[i])) == NULL) 156 goto err; 157 if (!x509_constraints_names_add(new, name)) 158 goto err; 159 } 160 161 return new; 162 err: 163 x509_constraints_names_free(new); 164 x509_constraints_name_free(name); 165 return NULL; 166 } 167 168 169 /* 170 * Validate that the name contains only a hostname consisting of RFC 171 * 5890 compliant A-labels (see RFC 6066 section 3). This is more 172 * permissive to allow for a leading '.' for a subdomain based 173 * constraint, as well as allowing for '_' which is commonly accepted 174 * by nonconformant DNS implementaitons. 175 * 176 * if "wildcards" is set it allows '*' to occur in the string at the end of a 177 * component. 178 */ 179 static int 180 x509_constraints_valid_domain_internal(uint8_t *name, size_t len, int wildcards) 181 { 182 uint8_t prev, c = 0; 183 int component = 0; 184 int first; 185 size_t i; 186 187 if (len > DOMAIN_PART_MAX_LEN) 188 return 0; 189 190 for (i = 0; i < len; i++) { 191 prev = c; 192 c = name[i]; 193 194 first = (i == 0); 195 196 /* Everything has to be ASCII, with no NUL byte */ 197 if (!isascii(c) || c == '\0') 198 return 0; 199 /* It must be alphanumeric, a '-', '.', '_' or '*' */ 200 if (!isalnum(c) && c != '-' && c != '.' && c != '_' && c != '*') 201 return 0; 202 203 /* if it is a '*', fail if not wildcards */ 204 if (!wildcards && c == '*') 205 return 0; 206 207 /* '-' must not start a component or be at the end. */ 208 if (c == '-' && (component == 0 || i == len - 1)) 209 return 0; 210 211 /* 212 * '.' must not be at the end. It may be first overall 213 * but must not otherwise start a component. 214 */ 215 if (c == '.' && ((component == 0 && !first) || i == len - 1)) 216 return 0; 217 218 if (c == '.') { 219 /* Components can not end with a dash. */ 220 if (prev == '-') 221 return 0; 222 /* Start new component */ 223 component = 0; 224 continue; 225 } 226 /* 227 * Wildcards can only occur at the end of a component. 228 * c*.com is valid, c*c.com is not. 229 */ 230 if (prev == '*') 231 return 0; 232 233 /* Components must be 63 chars or less. */ 234 if (++component > 63) 235 return 0; 236 } 237 return 1; 238 } 239 240 int 241 x509_constraints_valid_domain(uint8_t *name, size_t len) 242 { 243 if (len == 0) 244 return 0; 245 /* 246 * A domain may not be less than two characters, so you can't 247 * have a require subdomain name with less than that. 248 */ 249 if (len < 3 && name[0] == '.') 250 return 0; 251 return x509_constraints_valid_domain_internal(name, len, 0); 252 } 253 254 int 255 x509_constraints_valid_host(uint8_t *name, size_t len) 256 { 257 struct sockaddr_in sin4; 258 struct sockaddr_in6 sin6; 259 260 if (len == 0) 261 return 0; 262 if (name[0] == '.') /* leading . not allowed in a host name*/ 263 return 0; 264 if (inet_pton(AF_INET, name, &sin4) == 1) 265 return 0; 266 if (inet_pton(AF_INET6, name, &sin6) == 1) 267 return 0; 268 return x509_constraints_valid_domain_internal(name, len, 0); 269 } 270 271 int 272 x509_constraints_valid_sandns(uint8_t *name, size_t len) 273 { 274 if (len == 0) 275 return 0; 276 277 if (name[0] == '.') /* leading . not allowed in a SAN DNS name */ 278 return 0; 279 /* 280 * A domain may not be less than two characters, so you 281 * can't wildcard a single domain of less than that 282 */ 283 if (len < 4 && name[0] == '*') 284 return 0; 285 /* 286 * A wildcard may only be followed by a '.' 287 */ 288 if (len >= 4 && name[0] == '*' && name[1] != '.') 289 return 0; 290 291 return x509_constraints_valid_domain_internal(name, len, 1); 292 } 293 294 static inline int 295 local_part_ok(char c) 296 { 297 return (('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || 298 ('A' <= c && c <= 'Z') || c == '!' || c == '#' || c == '$' || 299 c == '%' || c == '&' || c == '\'' || c == '*' || c == '+' || 300 c == '-' || c == '/' || c == '=' || c == '?' || c == '^' || 301 c == '_' || c == '`' || c == '{' || c == '|' || c == '}' || 302 c == '~' || c == '.'); 303 } 304 305 /* 306 * Parse "candidate" as an RFC 2821 mailbox. 307 * Returns 0 if candidate is not a valid mailbox or if an error occurs. 308 * Returns 1 if candidate is a mailbox and adds newly allocated 309 * local and domain parts of the mailbox to "name->local" and name->name" 310 */ 311 int 312 x509_constraints_parse_mailbox(uint8_t *candidate, size_t len, 313 struct x509_constraints_name *name) 314 { 315 char working[DOMAIN_PART_MAX_LEN + 1] = { 0 }; 316 char *candidate_local = NULL; 317 char *candidate_domain = NULL; 318 size_t i, wi = 0; 319 int accept = 0; 320 int quoted = 0; 321 322 if (candidate == NULL) 323 return 0; 324 325 /* It can't be bigger than the local part, domain part and the '@' */ 326 if (len > LOCAL_PART_MAX_LEN + DOMAIN_PART_MAX_LEN + 1) 327 return 0; 328 329 for (i = 0; i < len; i++) { 330 char c = candidate[i]; 331 /* non ascii, cr, lf, or nul is never allowed */ 332 if (!isascii(c) || c == '\r' || c == '\n' || c == '\0') 333 goto bad; 334 if (i == 0) { 335 /* local part is quoted part */ 336 if (c == '"') 337 quoted = 1; 338 /* can not start with a . */ 339 if (c == '.') 340 goto bad; 341 } 342 if (accept) { 343 if (wi >= DOMAIN_PART_MAX_LEN) 344 goto bad; 345 working[wi++] = c; 346 accept = 0; 347 continue; 348 } 349 if (candidate_local != NULL) { 350 /* We are looking for the domain part */ 351 if (wi >= DOMAIN_PART_MAX_LEN) 352 goto bad; 353 working[wi++] = c; 354 if (i == len - 1) { 355 if (wi == 0) 356 goto bad; 357 if (candidate_domain != NULL) 358 goto bad; 359 candidate_domain = strdup(working); 360 if (candidate_domain == NULL) 361 goto bad; 362 } 363 continue; 364 } 365 /* We are looking for the local part */ 366 if (wi >= LOCAL_PART_MAX_LEN) 367 break; 368 369 if (quoted) { 370 if (c == '\\') { 371 accept = 1; 372 continue; 373 } 374 if (c == '"' && i != 0) { 375 /* end the quoted part. @ must be next */ 376 if (i + 1 == len || candidate[i + 1] != '@') 377 goto bad; 378 quoted = 0; 379 } 380 /* 381 * XXX Go strangely permits sp but forbids ht 382 * mimic that for now 383 */ 384 if (c == 9) 385 goto bad; 386 if (wi >= LOCAL_PART_MAX_LEN) 387 goto bad; 388 working[wi++] = c; 389 continue; /* all's good inside our quoted string */ 390 } 391 if (c == '@') { 392 if (wi == 0) 393 goto bad;; 394 if (candidate_local != NULL) 395 goto bad; 396 candidate_local = strdup(working); 397 if (candidate_local == NULL) 398 goto bad; 399 memset(working, 0, sizeof(working)); 400 wi = 0; 401 continue; 402 } 403 if (c == '\\') { 404 /* 405 * RFC 3936 hints these can happen outside of 406 * quotend string. don't include the \ but 407 * next character must be ok. 408 */ 409 if (i + 1 == len) 410 goto bad; 411 if (!local_part_ok(candidate[i + 1])) 412 goto bad; 413 accept = 1; 414 } 415 if (!local_part_ok(c)) 416 goto bad; 417 if (wi >= LOCAL_PART_MAX_LEN) 418 goto bad; 419 working[wi++] = c; 420 } 421 if (candidate_local == NULL || candidate_domain == NULL) 422 goto bad; 423 if (!x509_constraints_valid_host(candidate_domain, 424 strlen(candidate_domain))) 425 goto bad; 426 427 name->local = candidate_local; 428 name->name = candidate_domain; 429 name->type = GEN_EMAIL; 430 return 1; 431 bad: 432 free(candidate_local); 433 free(candidate_domain); 434 return 0; 435 } 436 437 int 438 x509_constraints_valid_domain_constraint(uint8_t *constraint, size_t len) 439 { 440 if (len == 0) 441 return 1; /* empty constraints match */ 442 443 /* 444 * A domain may not be less than two characters, so you 445 * can't match a single domain of less than that 446 */ 447 if (len < 3 && constraint[0] == '.') 448 return 0; 449 return x509_constraints_valid_domain_internal(constraint, len, 0); 450 } 451 452 /* 453 * Extract the host part of a URI, returns the host part as a c string 454 * the caller must free, or or NULL if it could not be found or is 455 * invalid. 456 * 457 * RFC 3986: 458 * the authority part of a uri starts with // and is terminated with 459 * the next '/', '?', '#' or end of the URI. 460 * 461 * The authority itself contains [userinfo '@'] host [: port] 462 * 463 * so the host starts at the start or after the '@', and ends 464 * with end of URI, '/', '?', "#', or ':'. 465 */ 466 int 467 x509_constraints_uri_host(uint8_t *uri, size_t len, char **hostpart) 468 { 469 size_t i, hostlen = 0; 470 uint8_t *authority = NULL; 471 char *host = NULL; 472 473 /* 474 * Find first '//'. there must be at least a '//' and 475 * something else. 476 */ 477 if (len < 3) 478 return 0; 479 for (i = 0; i < len - 1; i++) { 480 if (!isascii(uri[i])) 481 return 0; 482 if (uri[i] == '/' && uri[i + 1] == '/') { 483 authority = uri + i + 2; 484 break; 485 } 486 } 487 if (authority == NULL) 488 return 0; 489 for (i = authority - uri; i < len; i++) { 490 if (!isascii(uri[i])) 491 return 0; 492 /* it has a userinfo part */ 493 if (uri[i] == '@') { 494 hostlen = 0; 495 /* it can only have one */ 496 if (host != NULL) 497 break; 498 /* start after the userinfo part */ 499 host = uri + i + 1; 500 continue; 501 } 502 /* did we find the end? */ 503 if (uri[i] == ':' || uri[i] == '/' || uri[i] == '?' || 504 uri[i] == '#') 505 break; 506 hostlen++; 507 } 508 if (hostlen == 0) 509 return 0; 510 if (host == NULL) 511 host = authority; 512 if (!x509_constraints_valid_host(host, hostlen)) 513 return 0; 514 *hostpart = strndup(host, hostlen); 515 return 1; 516 } 517 518 int 519 x509_constraints_sandns(char *sandns, size_t dlen, char *constraint, size_t len) 520 { 521 char *suffix; 522 523 if (len == 0) 524 return 1; /* an empty constraint matches everything */ 525 526 /* match the end of the domain */ 527 if (dlen < len) 528 return 0; 529 suffix = sandns + (dlen - len); 530 return (strncasecmp(suffix, constraint, len) == 0); 531 } 532 533 /* 534 * Validate a pre-validated domain of length dlen against a pre-validated 535 * constraint of length len. 536 * 537 * returns 1 if the domain and constraint match. 538 * returns 0 otherwise. 539 * 540 * an empty constraint matches everyting. 541 * constraint will be matched against the domain as a suffix if it 542 * starts with a '.'. 543 * domain will be matched against the constraint as a suffix if it 544 * starts with a '.'. 545 */ 546 int 547 x509_constraints_domain(char *domain, size_t dlen, char *constraint, size_t len) 548 { 549 if (len == 0) 550 return 1; /* an empty constraint matches everything */ 551 552 if (constraint[0] == '.') { 553 /* match the end of the domain */ 554 char *suffix; 555 if (dlen < len) 556 return 0; 557 suffix = domain + (dlen - len); 558 return (strncasecmp(suffix, constraint, len) == 0); 559 } 560 if (domain[0] == '.') { 561 /* match the end of the constraint */ 562 char *suffix; 563 if (len < dlen) 564 return 0; 565 suffix = constraint + (len - dlen); 566 return (strncasecmp(suffix, domain, dlen) == 0); 567 } 568 /* otherwise we must exactly match the constraint */ 569 if (dlen != len) 570 return 0; 571 return (strncasecmp(domain, constraint, len) == 0); 572 } 573 574 int 575 x509_constraints_uri(uint8_t *uri, size_t ulen, uint8_t *constraint, size_t len, 576 int *error) 577 { 578 int ret = 0; 579 char *hostpart = NULL; 580 581 if (!x509_constraints_uri_host(uri, ulen, &hostpart)) { 582 *error = X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; 583 goto err; 584 } 585 if (hostpart == NULL) { 586 *error = X509_V_ERR_OUT_OF_MEM; 587 goto err; 588 } 589 if (!x509_constraints_valid_domain_constraint(constraint, len)) { 590 *error = X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX; 591 goto err; 592 } 593 ret = x509_constraints_domain(hostpart, strlen(hostpart), constraint, 594 len); 595 err: 596 free(hostpart); 597 return ret; 598 } 599 600 /* 601 * Verify a validated address of size alen with a validated contraint 602 * of size constraint_len. returns 1 if matching, 0 if not. 603 * Addresses are assumed to be pre-validated for a length of 4 and 8 604 * respectively for ipv4 addreses and constraints, and a length of 605 * 16 and 32 respectively for ipv6 address constraints by the caller. 606 */ 607 int 608 x509_constraints_ipaddr(uint8_t *address, size_t alen, uint8_t *constraint, 609 size_t len) 610 { 611 uint8_t *mask; 612 size_t i; 613 614 if (alen * 2 != len) 615 return 0; 616 617 mask = constraint + alen; 618 for (i = 0; i < alen; i++) { 619 if ((address[i] & mask[i]) != (constraint[i] & mask[i])) 620 return 0; 621 } 622 return 1; 623 } 624 625 /* 626 * Verify a canonicalized der encoded constraint dirname 627 * a canonicalized der encoded constraint. 628 */ 629 int 630 x509_constraints_dirname(uint8_t *dirname, size_t dlen, 631 uint8_t *constraint, size_t len) 632 { 633 if (len != dlen) 634 return 0; 635 return (memcmp(constraint, dirname, len) == 0); 636 } 637 638 /* 639 * De-obfuscate a GENERAL_NAME into useful bytes for a name or constraint. 640 */ 641 int 642 x509_constraints_general_to_bytes(GENERAL_NAME *name, uint8_t **bytes, 643 size_t *len) 644 { 645 *bytes = NULL; 646 *len = 0; 647 648 if (name->type == GEN_DNS) { 649 ASN1_IA5STRING *aname = name->d.dNSName; 650 *bytes = aname->data; 651 *len = strlen(aname->data); 652 return name->type; 653 } 654 if (name->type == GEN_EMAIL) { 655 ASN1_IA5STRING *aname = name->d.rfc822Name; 656 *bytes = aname->data; 657 *len = strlen(aname->data); 658 return name->type; 659 } 660 if (name->type == GEN_URI) { 661 ASN1_IA5STRING *aname = name->d.uniformResourceIdentifier; 662 *bytes = aname->data; 663 *len = strlen(aname->data); 664 return name->type; 665 } 666 if (name->type == GEN_DIRNAME) { 667 X509_NAME *dname = name->d.directoryName; 668 if (!dname->modified || i2d_X509_NAME(dname, NULL) >= 0) { 669 *bytes = dname->canon_enc; 670 *len = dname->canon_enclen; 671 return name->type; 672 } 673 } 674 if (name->type == GEN_IPADD) { 675 *bytes = name->d.ip->data; 676 *len = name->d.ip->length; 677 return name->type; 678 } 679 return 0; 680 } 681 682 683 /* 684 * Extract the relevant names for constraint checking from "cert", 685 * validate them, and add them to the list of cert names for "chain". 686 * returns 1 on success sets error and returns 0 on failure. 687 */ 688 int 689 x509_constraints_extract_names(struct x509_constraints_names *names, 690 X509 *cert, int is_leaf, int *error) 691 { 692 struct x509_constraints_name *vname = NULL; 693 X509_NAME *subject_name; 694 GENERAL_NAME *name; 695 ssize_t i = 0; 696 int name_type, include_cn = is_leaf, include_email = is_leaf; 697 698 /* first grab the altnames */ 699 while ((name = sk_GENERAL_NAME_value(cert->altname, i++)) != NULL) { 700 uint8_t *bytes = NULL; 701 size_t len = 0; 702 703 if ((vname = x509_constraints_name_new()) == NULL) { 704 *error = X509_V_ERR_OUT_OF_MEM; 705 goto err; 706 } 707 708 name_type = x509_constraints_general_to_bytes(name, &bytes, 709 &len); 710 switch(name_type) { 711 case GEN_DNS: 712 if (!x509_constraints_valid_sandns(bytes, len)) { 713 *error = X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; 714 goto err; 715 } 716 if ((vname->name = strdup(bytes)) == NULL) { 717 *error = X509_V_ERR_OUT_OF_MEM; 718 goto err; 719 } 720 vname->type = GEN_DNS; 721 include_cn = 0; /* don't use cn from subject */ 722 break; 723 case GEN_EMAIL: 724 if (!x509_constraints_parse_mailbox(bytes, len, 725 vname)) { 726 *error = X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; 727 goto err; 728 } 729 vname->type = GEN_EMAIL; 730 include_email = 0; /* don't use email from subject */ 731 break; 732 case GEN_URI: 733 if (!x509_constraints_uri_host(bytes, len, &vname->name)) { 734 *error = X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; 735 goto err; 736 } 737 if (vname->name == NULL) { 738 *error = X509_V_ERR_OUT_OF_MEM; 739 goto err; 740 } 741 vname->type = GEN_URI; 742 break; 743 case GEN_DIRNAME: 744 if (bytes == NULL || ((vname->der = malloc(len)) == 745 NULL)) { 746 *error = X509_V_ERR_OUT_OF_MEM; 747 goto err; 748 } 749 if (len == 0) { 750 *error = X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; 751 goto err; 752 } 753 memcpy(vname->der, bytes, len); 754 vname->der_len = len; 755 vname->type = GEN_DIRNAME; 756 break; 757 case GEN_IPADD: 758 if (len == 4) 759 vname->af = AF_INET; 760 if (len == 16) 761 vname->af = AF_INET6; 762 if (vname->af != AF_INET && vname->af != 763 AF_INET6) { 764 *error = X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; 765 goto err; 766 } 767 memcpy(vname->address, bytes, len); 768 vname->type = GEN_IPADD; 769 break; 770 default: 771 /* Ignore this name */ 772 x509_constraints_name_free(vname); 773 vname = NULL; 774 continue; 775 } 776 if (!x509_constraints_names_add(names, vname)) { 777 *error = X509_V_ERR_OUT_OF_MEM; 778 goto err; 779 } 780 vname = NULL; 781 } 782 783 x509_constraints_name_free(vname); 784 vname = NULL; 785 786 subject_name = X509_get_subject_name(cert); 787 if (X509_NAME_entry_count(subject_name) > 0) { 788 X509_NAME_ENTRY *email; 789 X509_NAME_ENTRY *cn; 790 /* 791 * This cert has a non-empty subject, so we must add 792 * the subject as a dirname to be compared against 793 * any dirname constraints 794 */ 795 if ((subject_name->modified && 796 i2d_X509_NAME(subject_name, NULL) < 0) || 797 (vname = x509_constraints_name_new()) == NULL || 798 (vname->der = malloc(subject_name->canon_enclen)) == NULL) { 799 *error = X509_V_ERR_OUT_OF_MEM; 800 goto err; 801 } 802 803 memcpy(vname->der, subject_name->canon_enc, 804 subject_name->canon_enclen); 805 vname->der_len = subject_name->canon_enclen; 806 vname->type = GEN_DIRNAME; 807 if (!x509_constraints_names_add(names, vname)) { 808 *error = X509_V_ERR_OUT_OF_MEM; 809 goto err; 810 } 811 vname = NULL; 812 /* 813 * Get any email addresses from the subject line, and 814 * add them as mbox names to be compared against any 815 * email constraints 816 */ 817 while (include_email && 818 (i = X509_NAME_get_index_by_NID(subject_name, 819 NID_pkcs9_emailAddress, i)) >= 0) { 820 ASN1_STRING *aname; 821 if ((email = X509_NAME_get_entry(subject_name, i)) == NULL || 822 (aname = X509_NAME_ENTRY_get_data(email)) == NULL) { 823 *error = X509_V_ERR_OUT_OF_MEM; 824 goto err; 825 } 826 if ((vname = x509_constraints_name_new()) == NULL) { 827 *error = X509_V_ERR_OUT_OF_MEM; 828 goto err; 829 } 830 if (!x509_constraints_parse_mailbox(aname->data, 831 aname->length, vname)) { 832 *error = X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; 833 goto err; 834 } 835 vname->type = GEN_EMAIL; 836 if (!x509_constraints_names_add(names, vname)) { 837 *error = X509_V_ERR_OUT_OF_MEM; 838 goto err; 839 } 840 vname = NULL; 841 } 842 /* 843 * Include the CN as a hostname to be checked againt 844 * name constraints if it looks like a hostname. 845 */ 846 while (include_cn && 847 (i = X509_NAME_get_index_by_NID(subject_name, 848 NID_commonName, i)) >= 0) { 849 ASN1_STRING *aname; 850 if ((cn = X509_NAME_get_entry(subject_name, i)) == NULL || 851 (aname = X509_NAME_ENTRY_get_data(cn)) == NULL) { 852 *error = X509_V_ERR_OUT_OF_MEM; 853 goto err; 854 } 855 if (!x509_constraints_valid_host(aname->data, 856 aname->length)) 857 continue; /* ignore it if not a hostname */ 858 if ((vname = x509_constraints_name_new()) == NULL) { 859 *error = X509_V_ERR_OUT_OF_MEM; 860 goto err; 861 } 862 if ((vname->name = strndup(aname->data, 863 aname->length)) == NULL) { 864 *error = X509_V_ERR_OUT_OF_MEM; 865 goto err; 866 } 867 vname->type = GEN_DNS; 868 if (!x509_constraints_names_add(names, vname)) { 869 *error = X509_V_ERR_OUT_OF_MEM; 870 goto err; 871 } 872 vname = NULL; 873 } 874 } 875 return 1; 876 err: 877 x509_constraints_name_free(vname); 878 return 0; 879 } 880 881 /* 882 * Validate a constraint in a general name, putting the relevant data 883 * into "name" if valid. returns 0, and sets error if the constraint is 884 * not valid. returns 1 if the constraint validated. name->type will be 885 * set to a valid type if there is constraint data in name, or unmodified 886 * if the GENERAL_NAME had a valid type but was ignored. 887 */ 888 int 889 x509_constraints_validate(GENERAL_NAME *constraint, 890 struct x509_constraints_name *name, int *error) 891 { 892 uint8_t *bytes = NULL; 893 size_t len = 0; 894 int name_type; 895 896 name_type = x509_constraints_general_to_bytes(constraint, &bytes, &len); 897 switch (name_type) { 898 case GEN_DIRNAME: 899 if (bytes == NULL || (name->der = malloc(len)) == NULL) { 900 *error = X509_V_ERR_OUT_OF_MEM; 901 return 0; 902 } 903 if (len == 0) 904 goto err; /* XXX The RFCs are delightfully vague */ 905 memcpy(name->der, bytes, len); 906 name->der_len = len; 907 name->type = GEN_DIRNAME; 908 break; 909 case GEN_DNS: 910 if (!x509_constraints_valid_domain_constraint(bytes, len)) 911 goto err; 912 if ((name->name = strdup(bytes)) == NULL) { 913 *error = X509_V_ERR_OUT_OF_MEM; 914 return 0; 915 } 916 name->type = GEN_DNS; 917 break; 918 case GEN_EMAIL: 919 if (memchr(bytes, '@', len) != NULL) { 920 if (!x509_constraints_parse_mailbox(bytes, len, name)) 921 goto err; 922 } else { 923 if (!x509_constraints_valid_domain_constraint(bytes, 924 len)) 925 goto err; 926 if ((name->name = strdup(bytes)) == NULL) { 927 *error = X509_V_ERR_OUT_OF_MEM; 928 return 0; 929 } 930 } 931 name->type = GEN_EMAIL; 932 break; 933 case GEN_IPADD: 934 /* Constraints are ip then mask */ 935 if (len == 8) 936 name->af = AF_INET; 937 else if (len == 32) 938 name->af = AF_INET6; 939 else 940 goto err; 941 memcpy(&name->address[0], bytes, len); 942 name->type = GEN_IPADD; 943 break; 944 case GEN_URI: 945 if (!x509_constraints_valid_domain_constraint(bytes, len)) 946 goto err; 947 name->name = strdup(bytes); 948 name->type = GEN_URI; 949 break; 950 default: 951 break; 952 } 953 return 1; 954 err: 955 *error = X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX; 956 return 0; 957 } 958 959 int 960 x509_constraints_extract_constraints(X509 *cert, 961 struct x509_constraints_names *permitted, 962 struct x509_constraints_names *excluded, 963 int *error) 964 { 965 struct x509_constraints_name *vname; 966 NAME_CONSTRAINTS *nc = cert->nc; 967 GENERAL_SUBTREE *subtree; 968 int i; 969 970 if (nc == NULL) 971 return 1; 972 973 for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) { 974 975 subtree = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i); 976 if (subtree->minimum || subtree->maximum) { 977 *error = X509_V_ERR_SUBTREE_MINMAX; 978 return 0; 979 } 980 if ((vname = x509_constraints_name_new()) == NULL) { 981 *error = X509_V_ERR_OUT_OF_MEM; 982 return 0; 983 } 984 if (x509_constraints_validate(subtree->base, vname, error) == 985 0) { 986 x509_constraints_name_free(vname); 987 return 0; 988 } 989 if (vname->type == 0) { 990 x509_constraints_name_free(vname); 991 continue; 992 } 993 if (!x509_constraints_names_add(permitted, vname)) { 994 x509_constraints_name_free(vname); 995 *error = X509_V_ERR_OUT_OF_MEM; 996 return 0; 997 } 998 } 999 1000 for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) { 1001 subtree = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i); 1002 if (subtree->minimum || subtree->maximum) { 1003 *error = X509_V_ERR_SUBTREE_MINMAX; 1004 return 0; 1005 } 1006 if ((vname = x509_constraints_name_new()) == NULL) { 1007 *error = X509_V_ERR_OUT_OF_MEM; 1008 return 0; 1009 } 1010 if (x509_constraints_validate(subtree->base, vname, error) == 1011 0) { 1012 x509_constraints_name_free(vname); 1013 return 0; 1014 } 1015 if (vname->type == 0) { 1016 x509_constraints_name_free(vname); 1017 continue; 1018 } 1019 if (!x509_constraints_names_add(excluded, vname)) { 1020 x509_constraints_name_free(vname); 1021 *error = X509_V_ERR_OUT_OF_MEM; 1022 return 0; 1023 } 1024 } 1025 1026 return 1; 1027 } 1028 1029 /* 1030 * Match a validated name in "name" against a validated constraint in 1031 * "constraint" return 1 if then name matches, 0 otherwise. 1032 */ 1033 int 1034 x509_constraints_match(struct x509_constraints_name *name, 1035 struct x509_constraints_name *constraint) 1036 { 1037 if (name->type != constraint->type) 1038 return 0; 1039 if (name->type == GEN_DNS) 1040 return x509_constraints_sandns(name->name, strlen(name->name), 1041 constraint->name, strlen(constraint->name)); 1042 if (name->type == GEN_URI) 1043 return x509_constraints_domain(name->name, strlen(name->name), 1044 constraint->name, strlen(constraint->name)); 1045 if (name->type == GEN_IPADD) { 1046 size_t nlen = name->af == AF_INET ? 4 : 16; 1047 size_t clen = name->af == AF_INET ? 8 : 32; 1048 if (name->af != AF_INET && name->af != AF_INET6) 1049 return 0; 1050 if (constraint->af != AF_INET && constraint->af != AF_INET6) 1051 return 0; 1052 if (name->af != constraint->af) 1053 return 0; 1054 return x509_constraints_ipaddr(name->address, nlen, 1055 constraint->address, clen); 1056 } 1057 if (name->type == GEN_EMAIL) { 1058 if (constraint->local) { 1059 /* mailbox local and domain parts must exactly match */ 1060 return (strcmp(name->local, constraint->local) == 0 && 1061 strcmp(name->name, constraint->name) == 0); 1062 } 1063 /* otherwise match the constraint to the domain part */ 1064 return x509_constraints_domain(name->name, strlen(name->name), 1065 constraint->name, strlen(constraint->name)); 1066 } 1067 if (name->type == GEN_DIRNAME) 1068 return x509_constraints_dirname(name->der, name->der_len, 1069 constraint->der, constraint->der_len); 1070 return 0; 1071 } 1072 1073 /* 1074 * Make sure every name in names does not match any excluded 1075 * constraints, and does match at least one permitted constraint if 1076 * any are present. Returns 1 if ok, 0, and sets error if not. 1077 */ 1078 int 1079 x509_constraints_check(struct x509_constraints_names *names, 1080 struct x509_constraints_names *permitted, 1081 struct x509_constraints_names *excluded, int *error) 1082 { 1083 size_t i, j; 1084 1085 for (i = 0; i < names->names_count; i++) { 1086 int permitted_seen = 0; 1087 int permitted_matched = 0; 1088 1089 for (j = 0; j < excluded->names_count; j++) { 1090 if (x509_constraints_match(names->names[i], 1091 excluded->names[j])) { 1092 *error = X509_V_ERR_EXCLUDED_VIOLATION; 1093 return 0; 1094 } 1095 } 1096 for (j = 0; j < permitted->names_count; j++) { 1097 if (permitted->names[j]->type == names->names[i]->type) 1098 permitted_seen++; 1099 if (x509_constraints_match(names->names[i], 1100 permitted->names[j])) { 1101 permitted_matched++; 1102 break; 1103 } 1104 } 1105 if (permitted_seen && !permitted_matched) { 1106 *error = X509_V_ERR_PERMITTED_VIOLATION; 1107 return 0; 1108 } 1109 } 1110 return 1; 1111 } 1112 1113 /* 1114 * Walk a validated chain of X509 certs, starting at the leaf, and 1115 * validate the name constraints in the chain. Intended for use with 1116 * the legacy X509 validtion code in x509_vfy.c 1117 * 1118 * returns 1 if the constraints are ok, 0 otherwise, setting error and 1119 * depth 1120 */ 1121 int 1122 x509_constraints_chain(STACK_OF(X509) *chain, int *error, int *depth) 1123 { 1124 int chain_length, verify_err = X509_V_ERR_UNSPECIFIED, i = 0; 1125 struct x509_constraints_names *names = NULL; 1126 struct x509_constraints_names *excluded = NULL; 1127 struct x509_constraints_names *permitted = NULL; 1128 size_t constraints_count = 0; 1129 X509 *cert; 1130 1131 if (chain == NULL || (chain_length = sk_X509_num(chain)) == 0) 1132 goto err; 1133 if (chain_length == 1) 1134 return 1; 1135 if ((names = x509_constraints_names_new( 1136 X509_VERIFY_MAX_CHAIN_NAMES)) == NULL) { 1137 verify_err = X509_V_ERR_OUT_OF_MEM; 1138 goto err; 1139 } 1140 1141 if ((cert = sk_X509_value(chain, 0)) == NULL) 1142 goto err; 1143 if (!x509_constraints_extract_names(names, cert, 1, &verify_err)) 1144 goto err; 1145 for (i = 1; i < chain_length; i++) { 1146 if ((cert = sk_X509_value(chain, i)) == NULL) 1147 goto err; 1148 if (cert->nc != NULL) { 1149 if ((permitted = x509_constraints_names_new( 1150 X509_VERIFY_MAX_CHAIN_CONSTRAINTS)) == NULL) { 1151 verify_err = X509_V_ERR_OUT_OF_MEM; 1152 goto err; 1153 } 1154 if ((excluded = x509_constraints_names_new( 1155 X509_VERIFY_MAX_CHAIN_CONSTRAINTS)) == NULL) { 1156 verify_err = X509_V_ERR_OUT_OF_MEM; 1157 goto err; 1158 } 1159 if (!x509_constraints_extract_constraints(cert, 1160 permitted, excluded, &verify_err)) 1161 goto err; 1162 constraints_count += permitted->names_count; 1163 constraints_count += excluded->names_count; 1164 if (constraints_count > 1165 X509_VERIFY_MAX_CHAIN_CONSTRAINTS) { 1166 verify_err = X509_V_ERR_OUT_OF_MEM; 1167 goto err; 1168 } 1169 if (!x509_constraints_check(names, permitted, excluded, 1170 &verify_err)) 1171 goto err; 1172 x509_constraints_names_free(excluded); 1173 excluded = NULL; 1174 x509_constraints_names_free(permitted); 1175 permitted = NULL; 1176 } 1177 if (!x509_constraints_extract_names(names, cert, 0, 1178 &verify_err)) 1179 goto err; 1180 } 1181 1182 x509_constraints_names_free(names); 1183 return 1; 1184 1185 err: 1186 *error = verify_err; 1187 *depth = i; 1188 x509_constraints_names_free(excluded); 1189 x509_constraints_names_free(permitted); 1190 x509_constraints_names_free(names); 1191 return 0; 1192 } 1193