1 /* $OpenBSD: x509_ncons.c,v 1.11 2024/07/13 15:08:58 tb Exp $ */ 2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * project. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 2003 The OpenSSL Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. All advertising materials mentioning features or use of this 21 * software must display the following acknowledgment: 22 * "This product includes software developed by the OpenSSL Project 23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24 * 25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26 * endorse or promote products derived from this software without 27 * prior written permission. For written permission, please contact 28 * licensing@OpenSSL.org. 29 * 30 * 5. Products derived from this software may not be called "OpenSSL" 31 * nor may "OpenSSL" appear in their names without prior written 32 * permission of the OpenSSL Project. 33 * 34 * 6. Redistributions of any form whatsoever must retain the following 35 * acknowledgment: 36 * "This product includes software developed by the OpenSSL Project 37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50 * OF THE POSSIBILITY OF SUCH DAMAGE. 51 * ==================================================================== 52 * 53 * This product includes cryptographic software written by Eric Young 54 * (eay@cryptsoft.com). This product includes software written by Tim 55 * Hudson (tjh@cryptsoft.com). 56 * 57 */ 58 59 #include <stdio.h> 60 #include <string.h> 61 62 #include <openssl/asn1t.h> 63 #include <openssl/conf.h> 64 #include <openssl/err.h> 65 #include <openssl/x509v3.h> 66 67 #include "x509_local.h" 68 69 static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, 70 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval); 71 static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, 72 void *a, BIO *bp, int ind); 73 static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method, 74 STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp, int ind, char *name); 75 static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip); 76 77 static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc); 78 static int nc_match_single(GENERAL_NAME *sub, GENERAL_NAME *gen); 79 static int nc_dn(X509_NAME *sub, X509_NAME *nm); 80 static int nc_dns(ASN1_IA5STRING *sub, ASN1_IA5STRING *dns); 81 static int nc_email(ASN1_IA5STRING *sub, ASN1_IA5STRING *eml); 82 static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base); 83 84 static const X509V3_EXT_METHOD x509v3_ext_name_constraints = { 85 .ext_nid = NID_name_constraints, 86 .ext_flags = 0, 87 .it = &NAME_CONSTRAINTS_it, 88 .ext_new = NULL, 89 .ext_free = NULL, 90 .d2i = NULL, 91 .i2d = NULL, 92 .i2s = NULL, 93 .s2i = NULL, 94 .i2v = NULL, 95 .v2i = v2i_NAME_CONSTRAINTS, 96 .i2r = i2r_NAME_CONSTRAINTS, 97 .r2i = NULL, 98 .usr_data = NULL, 99 }; 100 101 const X509V3_EXT_METHOD * 102 x509v3_ext_method_name_constraints(void) 103 { 104 return &x509v3_ext_name_constraints; 105 } 106 107 static const ASN1_TEMPLATE GENERAL_SUBTREE_seq_tt[] = { 108 { 109 .flags = 0, 110 .tag = 0, 111 .offset = offsetof(GENERAL_SUBTREE, base), 112 .field_name = "base", 113 .item = &GENERAL_NAME_it, 114 }, 115 { 116 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL, 117 .tag = 0, 118 .offset = offsetof(GENERAL_SUBTREE, minimum), 119 .field_name = "minimum", 120 .item = &ASN1_INTEGER_it, 121 }, 122 { 123 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL, 124 .tag = 1, 125 .offset = offsetof(GENERAL_SUBTREE, maximum), 126 .field_name = "maximum", 127 .item = &ASN1_INTEGER_it, 128 }, 129 }; 130 131 const ASN1_ITEM GENERAL_SUBTREE_it = { 132 .itype = ASN1_ITYPE_SEQUENCE, 133 .utype = V_ASN1_SEQUENCE, 134 .templates = GENERAL_SUBTREE_seq_tt, 135 .tcount = sizeof(GENERAL_SUBTREE_seq_tt) / sizeof(ASN1_TEMPLATE), 136 .funcs = NULL, 137 .size = sizeof(GENERAL_SUBTREE), 138 .sname = "GENERAL_SUBTREE", 139 }; 140 LCRYPTO_ALIAS(GENERAL_SUBTREE_it); 141 142 static const ASN1_TEMPLATE NAME_CONSTRAINTS_seq_tt[] = { 143 { 144 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_OPTIONAL, 145 .tag = 0, 146 .offset = offsetof(NAME_CONSTRAINTS, permittedSubtrees), 147 .field_name = "permittedSubtrees", 148 .item = &GENERAL_SUBTREE_it, 149 }, 150 { 151 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_OPTIONAL, 152 .tag = 1, 153 .offset = offsetof(NAME_CONSTRAINTS, excludedSubtrees), 154 .field_name = "excludedSubtrees", 155 .item = &GENERAL_SUBTREE_it, 156 }, 157 }; 158 159 const ASN1_ITEM NAME_CONSTRAINTS_it = { 160 .itype = ASN1_ITYPE_SEQUENCE, 161 .utype = V_ASN1_SEQUENCE, 162 .templates = NAME_CONSTRAINTS_seq_tt, 163 .tcount = sizeof(NAME_CONSTRAINTS_seq_tt) / sizeof(ASN1_TEMPLATE), 164 .funcs = NULL, 165 .size = sizeof(NAME_CONSTRAINTS), 166 .sname = "NAME_CONSTRAINTS", 167 }; 168 LCRYPTO_ALIAS(NAME_CONSTRAINTS_it); 169 170 171 GENERAL_SUBTREE * 172 GENERAL_SUBTREE_new(void) 173 { 174 return (GENERAL_SUBTREE*)ASN1_item_new(&GENERAL_SUBTREE_it); 175 } 176 LCRYPTO_ALIAS(GENERAL_SUBTREE_new); 177 178 void 179 GENERAL_SUBTREE_free(GENERAL_SUBTREE *a) 180 { 181 ASN1_item_free((ASN1_VALUE *)a, &GENERAL_SUBTREE_it); 182 } 183 LCRYPTO_ALIAS(GENERAL_SUBTREE_free); 184 185 NAME_CONSTRAINTS * 186 NAME_CONSTRAINTS_new(void) 187 { 188 return (NAME_CONSTRAINTS*)ASN1_item_new(&NAME_CONSTRAINTS_it); 189 } 190 LCRYPTO_ALIAS(NAME_CONSTRAINTS_new); 191 192 void 193 NAME_CONSTRAINTS_free(NAME_CONSTRAINTS *a) 194 { 195 ASN1_item_free((ASN1_VALUE *)a, &NAME_CONSTRAINTS_it); 196 } 197 LCRYPTO_ALIAS(NAME_CONSTRAINTS_free); 198 199 static void * 200 v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx, 201 STACK_OF(CONF_VALUE) *nval) 202 { 203 int i; 204 CONF_VALUE tval, *val; 205 STACK_OF(GENERAL_SUBTREE) **ptree = NULL; 206 NAME_CONSTRAINTS *ncons = NULL; 207 GENERAL_SUBTREE *sub = NULL; 208 209 ncons = NAME_CONSTRAINTS_new(); 210 if (!ncons) 211 goto memerr; 212 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) { 213 val = sk_CONF_VALUE_value(nval, i); 214 if (!strncmp(val->name, "permitted", 9) && val->name[9]) { 215 ptree = &ncons->permittedSubtrees; 216 tval.name = val->name + 10; 217 } else if (!strncmp(val->name, "excluded", 8) && val->name[8]) { 218 ptree = &ncons->excludedSubtrees; 219 tval.name = val->name + 9; 220 } else { 221 X509V3error(X509V3_R_INVALID_SYNTAX); 222 goto err; 223 } 224 tval.value = val->value; 225 sub = GENERAL_SUBTREE_new(); 226 if (!v2i_GENERAL_NAME_ex(sub->base, method, ctx, &tval, 1)) 227 goto err; 228 if (!*ptree) 229 *ptree = sk_GENERAL_SUBTREE_new_null(); 230 if (!*ptree || !sk_GENERAL_SUBTREE_push(*ptree, sub)) 231 goto memerr; 232 sub = NULL; 233 } 234 235 return ncons; 236 237 memerr: 238 X509V3error(ERR_R_MALLOC_FAILURE); 239 err: 240 NAME_CONSTRAINTS_free(ncons); 241 GENERAL_SUBTREE_free(sub); 242 return NULL; 243 } 244 245 static int 246 i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a, BIO *bp, int ind) 247 { 248 NAME_CONSTRAINTS *ncons = a; 249 250 do_i2r_name_constraints(method, ncons->permittedSubtrees, 251 bp, ind, "Permitted"); 252 do_i2r_name_constraints(method, ncons->excludedSubtrees, 253 bp, ind, "Excluded"); 254 return 1; 255 } 256 257 static int 258 do_i2r_name_constraints(const X509V3_EXT_METHOD *method, 259 STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp, int ind, char *name) 260 { 261 GENERAL_SUBTREE *tree; 262 int i; 263 264 if (sk_GENERAL_SUBTREE_num(trees) > 0) 265 BIO_printf(bp, "%*s%s:\n", ind, "", name); 266 for (i = 0; i < sk_GENERAL_SUBTREE_num(trees); i++) { 267 tree = sk_GENERAL_SUBTREE_value(trees, i); 268 BIO_printf(bp, "%*s", ind + 2, ""); 269 if (tree->base->type == GEN_IPADD) 270 print_nc_ipadd(bp, tree->base->d.ip); 271 else 272 GENERAL_NAME_print(bp, tree->base); 273 BIO_puts(bp, "\n"); 274 } 275 return 1; 276 } 277 278 static int 279 print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip) 280 { 281 int i, len; 282 unsigned char *p; 283 284 p = ip->data; 285 len = ip->length; 286 BIO_puts(bp, "IP:"); 287 if (len == 8) { 288 BIO_printf(bp, "%d.%d.%d.%d/%d.%d.%d.%d", 289 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 290 } else if (len == 32) { 291 for (i = 0; i < 16; i++) { 292 BIO_printf(bp, "%X", p[0] << 8 | p[1]); 293 p += 2; 294 if (i == 7) 295 BIO_puts(bp, "/"); 296 else if (i != 15) 297 BIO_puts(bp, ":"); 298 } 299 } else 300 BIO_printf(bp, "IP Address:<invalid>"); 301 return 1; 302 } 303 304 /* Check a certificate conforms to a specified set of constraints. 305 * Return values: 306 * X509_V_OK: All constraints obeyed. 307 * X509_V_ERR_PERMITTED_VIOLATION: Permitted subtree violation. 308 * X509_V_ERR_EXCLUDED_VIOLATION: Excluded subtree violation. 309 * X509_V_ERR_SUBTREE_MINMAX: Min or max values present and matching type. 310 * X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE: Unsupported constraint type. 311 * X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX: bad unsupported constraint syntax. 312 * X509_V_ERR_UNSUPPORTED_NAME_SYNTAX: bad or unsupported syntax of name 313 */ 314 315 int 316 NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc) 317 { 318 int r, i; 319 X509_NAME *nm; 320 321 nm = X509_get_subject_name(x); 322 323 if (X509_NAME_entry_count(nm) > 0) { 324 GENERAL_NAME gntmp; 325 gntmp.type = GEN_DIRNAME; 326 gntmp.d.directoryName = nm; 327 328 r = nc_match(&gntmp, nc); 329 330 if (r != X509_V_OK) 331 return r; 332 333 gntmp.type = GEN_EMAIL; 334 335 /* Process any email address attributes in subject name */ 336 337 for (i = -1;;) { 338 X509_NAME_ENTRY *ne; 339 i = X509_NAME_get_index_by_NID(nm, 340 NID_pkcs9_emailAddress, i); 341 if (i == -1) 342 break; 343 ne = X509_NAME_get_entry(nm, i); 344 gntmp.d.rfc822Name = X509_NAME_ENTRY_get_data(ne); 345 if (gntmp.d.rfc822Name->type != V_ASN1_IA5STRING) 346 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; 347 348 r = nc_match(&gntmp, nc); 349 350 if (r != X509_V_OK) 351 return r; 352 } 353 354 } 355 356 for (i = 0; i < sk_GENERAL_NAME_num(x->altname); i++) { 357 GENERAL_NAME *gen = sk_GENERAL_NAME_value(x->altname, i); 358 r = nc_match(gen, nc); 359 if (r != X509_V_OK) 360 return r; 361 } 362 return X509_V_OK; 363 } 364 LCRYPTO_ALIAS(NAME_CONSTRAINTS_check); 365 static int 366 nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc) 367 { 368 GENERAL_SUBTREE *sub; 369 int i, r, match = 0; 370 371 /* Permitted subtrees: if any subtrees exist of matching the type 372 * at least one subtree must match. 373 */ 374 375 for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) { 376 sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i); 377 if (gen->type != sub->base->type) 378 continue; 379 if (sub->minimum || sub->maximum) 380 return X509_V_ERR_SUBTREE_MINMAX; 381 /* If we already have a match don't bother trying any more */ 382 if (match == 2) 383 continue; 384 if (match == 0) 385 match = 1; 386 r = nc_match_single(gen, sub->base); 387 if (r == X509_V_OK) 388 match = 2; 389 else if (r != X509_V_ERR_PERMITTED_VIOLATION) 390 return r; 391 } 392 393 if (match == 1) 394 return X509_V_ERR_PERMITTED_VIOLATION; 395 396 /* Excluded subtrees: must not match any of these */ 397 398 for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) { 399 sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i); 400 if (gen->type != sub->base->type) 401 continue; 402 if (sub->minimum || sub->maximum) 403 return X509_V_ERR_SUBTREE_MINMAX; 404 405 r = nc_match_single(gen, sub->base); 406 if (r == X509_V_OK) 407 return X509_V_ERR_EXCLUDED_VIOLATION; 408 else if (r != X509_V_ERR_PERMITTED_VIOLATION) 409 return r; 410 411 } 412 413 return X509_V_OK; 414 } 415 416 static int 417 nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base) 418 { 419 switch (base->type) { 420 case GEN_DIRNAME: 421 return nc_dn(gen->d.directoryName, base->d.directoryName); 422 423 case GEN_DNS: 424 return nc_dns(gen->d.dNSName, base->d.dNSName); 425 426 case GEN_EMAIL: 427 return nc_email(gen->d.rfc822Name, base->d.rfc822Name); 428 429 case GEN_URI: 430 return nc_uri(gen->d.uniformResourceIdentifier, 431 base->d.uniformResourceIdentifier); 432 433 default: 434 return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE; 435 } 436 } 437 438 /* directoryName name constraint matching. 439 * The canonical encoding of X509_NAME makes this comparison easy. It is 440 * matched if the subtree is a subset of the name. 441 */ 442 443 static int 444 nc_dn(X509_NAME *nm, X509_NAME *base) 445 { 446 /* Ensure canonical encodings are up to date. */ 447 if (nm->modified && i2d_X509_NAME(nm, NULL) < 0) 448 return X509_V_ERR_OUT_OF_MEM; 449 if (base->modified && i2d_X509_NAME(base, NULL) < 0) 450 return X509_V_ERR_OUT_OF_MEM; 451 if (base->canon_enclen > nm->canon_enclen) 452 return X509_V_ERR_PERMITTED_VIOLATION; 453 if (memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen)) 454 return X509_V_ERR_PERMITTED_VIOLATION; 455 return X509_V_OK; 456 } 457 458 static int 459 nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base) 460 { 461 char *baseptr = (char *)base->data; 462 char *dnsptr = (char *)dns->data; 463 464 /* Empty matches everything */ 465 if (!*baseptr) 466 return X509_V_OK; 467 /* Otherwise can add zero or more components on the left so 468 * compare RHS and if dns is longer and expect '.' as preceding 469 * character. 470 */ 471 if (dns->length > base->length) { 472 dnsptr += dns->length - base->length; 473 if (baseptr[0] != '.' && dnsptr[-1] != '.') 474 return X509_V_ERR_PERMITTED_VIOLATION; 475 } 476 477 if (strcasecmp(baseptr, dnsptr)) 478 return X509_V_ERR_PERMITTED_VIOLATION; 479 480 return X509_V_OK; 481 } 482 483 static int 484 nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base) 485 { 486 const char *baseptr = (char *)base->data; 487 const char *emlptr = (char *)eml->data; 488 const char *baseat = strchr(baseptr, '@'); 489 const char *emlat = strchr(emlptr, '@'); 490 491 if (!emlat) 492 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; 493 /* Special case: initial '.' is RHS match */ 494 if (!baseat && (*baseptr == '.')) { 495 if (eml->length > base->length) { 496 emlptr += eml->length - base->length; 497 if (!strcasecmp(baseptr, emlptr)) 498 return X509_V_OK; 499 } 500 return X509_V_ERR_PERMITTED_VIOLATION; 501 } 502 503 /* If we have anything before '@' match local part */ 504 505 if (baseat) { 506 if (baseat != baseptr) { 507 if ((baseat - baseptr) != (emlat - emlptr)) 508 return X509_V_ERR_PERMITTED_VIOLATION; 509 /* Case sensitive match of local part */ 510 if (strncmp(baseptr, emlptr, emlat - emlptr)) 511 return X509_V_ERR_PERMITTED_VIOLATION; 512 } 513 /* Position base after '@' */ 514 baseptr = baseat + 1; 515 } 516 emlptr = emlat + 1; 517 /* Just have hostname left to match: case insensitive */ 518 if (strcasecmp(baseptr, emlptr)) 519 return X509_V_ERR_PERMITTED_VIOLATION; 520 521 return X509_V_OK; 522 } 523 524 static int 525 nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base) 526 { 527 const char *baseptr = (char *)base->data; 528 const char *hostptr = (char *)uri->data; 529 const char *p = strchr(hostptr, ':'); 530 int hostlen; 531 532 /* Check for foo:// and skip past it */ 533 if (!p || (p[1] != '/') || (p[2] != '/')) 534 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; 535 hostptr = p + 3; 536 537 /* Determine length of hostname part of URI */ 538 539 /* Look for a port indicator as end of hostname first */ 540 541 p = strchr(hostptr, ':'); 542 /* Otherwise look for trailing slash */ 543 if (!p) 544 p = strchr(hostptr, '/'); 545 546 if (!p) 547 hostlen = strlen(hostptr); 548 else 549 hostlen = p - hostptr; 550 551 if (hostlen == 0) 552 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; 553 554 /* Special case: initial '.' is RHS match */ 555 if (*baseptr == '.') { 556 if (hostlen > base->length) { 557 p = hostptr + hostlen - base->length; 558 if (!strncasecmp(p, baseptr, base->length)) 559 return X509_V_OK; 560 } 561 return X509_V_ERR_PERMITTED_VIOLATION; 562 } 563 564 if ((base->length != (int)hostlen) || 565 strncasecmp(hostptr, baseptr, hostlen)) 566 return X509_V_ERR_PERMITTED_VIOLATION; 567 568 return X509_V_OK; 569 } 570