| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <stdio.h> |
| 16 | #include <string.h> |
| 17 |
| 18 | #include <openssl/asn1t.h> |
| 19 | #include <openssl/conf.h> |
| 20 | #include <openssl/err.h> |
| 21 | #include <openssl/mem.h> |
| 22 | #include <openssl/obj.h> |
| 23 | #include <openssl/x509.h> |
| 24 |
| 25 | #include "../internal.h" |
| 26 | #include "ext_dat.h" |
| 27 | #include "internal.h" |
| 28 |
| 29 |
| 30 | static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, |
| 31 | const X509V3_CTX *ctx, |
| 32 | const STACK_OF(CONF_VALUE) *nval); |
| 33 | static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a, |
| 34 | BIO *bp, int ind); |
| 35 | static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method, |
| 36 | STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp, |
| 37 | int ind, const char *name); |
| 38 | static int print_nc_ipadd(BIO *bp, const ASN1_OCTET_STRING *ip); |
| 39 |
| 40 | static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc); |
| 41 | static int nc_match_single(GENERAL_NAME *sub, GENERAL_NAME *gen); |
| 42 | static int nc_dn(X509_NAME *sub, X509_NAME *nm); |
| 43 | static int nc_dns(const ASN1_IA5STRING *sub, const ASN1_IA5STRING *dns); |
| 44 | static int nc_email(const ASN1_IA5STRING *sub, const ASN1_IA5STRING *eml); |
| 45 | static int nc_uri(const ASN1_IA5STRING *uri, const ASN1_IA5STRING *base); |
| 46 |
| 47 | const X509V3_EXT_METHOD v3_name_constraints = { |
| 48 | NID_name_constraints, |
| 49 | 0, |
| 50 | ASN1_ITEM_ref(NAME_CONSTRAINTS), |
| 51 | 0, |
| 52 | 0, |
| 53 | 0, |
| 54 | 0, |
| 55 | 0, |
| 56 | 0, |
| 57 | 0, |
| 58 | v2i_NAME_CONSTRAINTS, |
| 59 | i2r_NAME_CONSTRAINTS, |
| 60 | 0, |
| 61 | NULL, |
| 62 | }; |
| 63 |
| 64 | ASN1_SEQUENCE(GENERAL_SUBTREE) = { |
| 65 | ASN1_SIMPLE(GENERAL_SUBTREE, base, GENERAL_NAME), |
| 66 | ASN1_IMP_OPT(GENERAL_SUBTREE, minimum, ASN1_INTEGER, 0), |
| 67 | ASN1_IMP_OPT(GENERAL_SUBTREE, maximum, ASN1_INTEGER, 1), |
| 68 | } ASN1_SEQUENCE_END(GENERAL_SUBTREE) |
| 69 |
| 70 | ASN1_SEQUENCE(NAME_CONSTRAINTS) = { |
| 71 | ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, permittedSubtrees, |
| 72 | GENERAL_SUBTREE, 0), |
| 73 | ASN1_IMP_SEQUENCE_OF_OPT(NAME_CONSTRAINTS, excludedSubtrees, |
| 74 | GENERAL_SUBTREE, 1), |
| 75 | } ASN1_SEQUENCE_END(NAME_CONSTRAINTS) |
| 76 |
| 77 |
| 78 | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE) |
| 79 | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS) |
| 80 |
| 81 | static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, |
| 82 | const X509V3_CTX *ctx, |
| 83 | const STACK_OF(CONF_VALUE) *nval) { |
| 84 | STACK_OF(GENERAL_SUBTREE) **ptree = NULL; |
| 85 | NAME_CONSTRAINTS *ncons = NULL; |
| 86 | GENERAL_SUBTREE *sub = NULL; |
| 87 | ncons = NAME_CONSTRAINTS_new(); |
| 88 | if (!ncons) { |
| 89 | goto err; |
| 90 | } |
| 91 | for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) { |
| 92 | const CONF_VALUE *val = sk_CONF_VALUE_value(nval, i); |
| 93 | CONF_VALUE tval; |
| 94 | if (!strncmp(val->name, "permitted", 9) && val->name[9]) { |
| 95 | ptree = &ncons->permittedSubtrees; |
| 96 | tval.name = val->name + 10; |
| 97 | } else if (!strncmp(val->name, "excluded", 8) && val->name[8]) { |
| 98 | ptree = &ncons->excludedSubtrees; |
| 99 | tval.name = val->name + 9; |
| 100 | } else { |
| 101 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_SYNTAX); |
| 102 | goto err; |
| 103 | } |
| 104 | tval.value = val->value; |
| 105 | sub = GENERAL_SUBTREE_new(); |
| 106 | if (!v2i_GENERAL_NAME_ex(sub->base, method, ctx, &tval, 1)) { |
| 107 | goto err; |
| 108 | } |
| 109 | if (!*ptree) { |
| 110 | *ptree = sk_GENERAL_SUBTREE_new_null(); |
| 111 | } |
| 112 | if (!*ptree || !sk_GENERAL_SUBTREE_push(*ptree, sub)) { |
| 113 | goto err; |
| 114 | } |
| 115 | sub = NULL; |
| 116 | } |
| 117 |
| 118 | return ncons; |
| 119 |
| 120 | err: |
| 121 | NAME_CONSTRAINTS_free(ncons); |
| 122 | GENERAL_SUBTREE_free(sub); |
| 123 | return NULL; |
| 124 | } |
| 125 |
| 126 | static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a, |
| 127 | BIO *bp, int ind) { |
| 128 | NAME_CONSTRAINTS *ncons = reinterpret_cast<NAME_CONSTRAINTS *>(a); |
| 129 | do_i2r_name_constraints(method, ncons->permittedSubtrees, bp, ind, |
| 130 | "Permitted"); |
| 131 | do_i2r_name_constraints(method, ncons->excludedSubtrees, bp, ind, "Excluded"); |
| 132 | return 1; |
| 133 | } |
| 134 |
| 135 | static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method, |
| 136 | STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp, |
| 137 | int ind, const char *name) { |
| 138 | GENERAL_SUBTREE *tree; |
| 139 | size_t i; |
| 140 | if (sk_GENERAL_SUBTREE_num(trees) > 0) { |
| 141 | BIO_printf(bp, "%*s%s:\n", ind, "", name); |
| 142 | } |
| 143 | for (i = 0; i < sk_GENERAL_SUBTREE_num(trees); i++) { |
| 144 | tree = sk_GENERAL_SUBTREE_value(trees, i); |
| 145 | BIO_printf(bp, "%*s", ind + 2, ""); |
| 146 | if (tree->base->type == GEN_IPADD) { |
| 147 | print_nc_ipadd(bp, tree->base->d.ip); |
| 148 | } else { |
| 149 | GENERAL_NAME_print(bp, tree->base); |
| 150 | } |
| 151 | BIO_puts(bp, "\n"); |
| 152 | } |
| 153 | return 1; |
| 154 | } |
| 155 |
| 156 | static int print_nc_ipadd(BIO *bp, const ASN1_OCTET_STRING *ip) { |
| 157 | int i, len; |
| 158 | unsigned char *p; |
| 159 | p = ip->data; |
| 160 | len = ip->length; |
| 161 | BIO_puts(bp, "IP:"); |
| 162 | if (len == 8) { |
| 163 | BIO_printf(bp, "%d.%d.%d.%d/%d.%d.%d.%d", p[0], p[1], p[2], p[3], p[4], |
| 164 | p[5], p[6], p[7]); |
| 165 | } else if (len == 32) { |
| 166 | for (i = 0; i < 16; i++) { |
| 167 | uint16_t v = ((uint16_t)p[0] << 8) | p[1]; |
| 168 | BIO_printf(bp, "%X", v); |
| 169 | p += 2; |
| 170 | if (i == 7) { |
| 171 | BIO_puts(bp, "/"); |
| 172 | } else if (i != 15) { |
| 173 | BIO_puts(bp, ":"); |
| 174 | } |
| 175 | } |
| 176 | } else { |
| 177 | BIO_printf(bp, "IP Address:<invalid>"); |
| 178 | } |
| 179 | return 1; |
| 180 | } |
| 181 |
| 182 | //- |
| 183 | // Check a certificate conforms to a specified set of constraints. |
| 184 | // Return values: |
| 185 | // X509_V_OK: All constraints obeyed. |
| 186 | // X509_V_ERR_PERMITTED_VIOLATION: Permitted subtree violation. |
| 187 | // X509_V_ERR_EXCLUDED_VIOLATION: Excluded subtree violation. |
| 188 | // X509_V_ERR_SUBTREE_MINMAX: Min or max values present and matching type. |
| 189 | // X509_V_ERR_UNSPECIFIED: Unspecified error. |
| 190 | // X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE: Unsupported constraint type. |
| 191 | // X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX: Bad or unsupported constraint |
| 192 | // syntax. |
| 193 | // X509_V_ERR_UNSUPPORTED_NAME_SYNTAX: Bad or unsupported syntax of name. |
| 194 |
| 195 | int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc) { |
| 196 | int r, i; |
| 197 | size_t j; |
| 198 | X509_NAME *nm; |
| 199 |
| 200 | nm = X509_get_subject_name(x); |
| 201 |
| 202 | // Guard against certificates with an excessive number of names or |
| 203 | // constraints causing a computationally expensive name constraints |
| 204 | // check. |
| 205 | size_t name_count = |
| 206 | X509_NAME_entry_count(nm) + sk_GENERAL_NAME_num(x->altname); |
| 207 | size_t constraint_count = sk_GENERAL_SUBTREE_num(nc->permittedSubtrees) + |
| 208 | sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); |
| 209 | size_t check_count = constraint_count * name_count; |
| 210 | if (name_count < (size_t)X509_NAME_entry_count(nm) || |
| 211 | constraint_count < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees) || |
| 212 | (constraint_count && check_count / constraint_count != name_count) || |
| 213 | check_count > 1 << 20) { |
| 214 | return X509_V_ERR_UNSPECIFIED; |
| 215 | } |
| 216 |
| 217 | if (X509_NAME_entry_count(nm) > 0) { |
| 218 | GENERAL_NAME gntmp; |
| 219 | gntmp.type = GEN_DIRNAME; |
| 220 | gntmp.d.directoryName = nm; |
| 221 |
| 222 | r = nc_match(&gntmp, nc); |
| 223 |
| 224 | if (r != X509_V_OK) { |
| 225 | return r; |
| 226 | } |
| 227 |
| 228 | gntmp.type = GEN_EMAIL; |
| 229 |
| 230 | // Process any email address attributes in subject name |
| 231 |
| 232 | for (i = -1;;) { |
| 233 | i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i); |
| 234 | if (i == -1) { |
| 235 | break; |
| 236 | } |
| 237 | const X509_NAME_ENTRY *ne = X509_NAME_get_entry(nm, i); |
| 238 | gntmp.d.rfc822Name = X509_NAME_ENTRY_get_data(ne); |
| 239 | if (gntmp.d.rfc822Name->type != V_ASN1_IA5STRING) { |
| 240 | return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; |
| 241 | } |
| 242 |
| 243 | r = nc_match(&gntmp, nc); |
| 244 |
| 245 | if (r != X509_V_OK) { |
| 246 | return r; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 |
| 251 | for (j = 0; j < sk_GENERAL_NAME_num(x->altname); j++) { |
| 252 | GENERAL_NAME *gen = sk_GENERAL_NAME_value(x->altname, j); |
| 253 | r = nc_match(gen, nc); |
| 254 | if (r != X509_V_OK) { |
| 255 | return r; |
| 256 | } |
| 257 | } |
| 258 |
| 259 | return X509_V_OK; |
| 260 | } |
| 261 |
| 262 | static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc) { |
| 263 | GENERAL_SUBTREE *sub; |
| 264 | int r, match = 0; |
| 265 | size_t i; |
| 266 |
| 267 | // Permitted subtrees: if any subtrees exist of matching the type at |
| 268 | // least one subtree must match. |
| 269 |
| 270 | for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) { |
| 271 | sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i); |
| 272 | if (gen->type != sub->base->type) { |
| 273 | continue; |
| 274 | } |
| 275 | if (sub->minimum || sub->maximum) { |
| 276 | return X509_V_ERR_SUBTREE_MINMAX; |
| 277 | } |
| 278 | // If we already have a match don't bother trying any more |
| 279 | if (match == 2) { |
| 280 | continue; |
| 281 | } |
| 282 | if (match == 0) { |
| 283 | match = 1; |
| 284 | } |
| 285 | r = nc_match_single(gen, sub->base); |
| 286 | if (r == X509_V_OK) { |
| 287 | match = 2; |
| 288 | } else if (r != X509_V_ERR_PERMITTED_VIOLATION) { |
| 289 | return r; |
| 290 | } |
| 291 | } |
| 292 |
| 293 | if (match == 1) { |
| 294 | return X509_V_ERR_PERMITTED_VIOLATION; |
| 295 | } |
| 296 |
| 297 | // Excluded subtrees: must not match any of these |
| 298 |
| 299 | for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) { |
| 300 | sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i); |
| 301 | if (gen->type != sub->base->type) { |
| 302 | continue; |
| 303 | } |
| 304 | if (sub->minimum || sub->maximum) { |
| 305 | return X509_V_ERR_SUBTREE_MINMAX; |
| 306 | } |
| 307 |
| 308 | r = nc_match_single(gen, sub->base); |
| 309 | if (r == X509_V_OK) { |
| 310 | return X509_V_ERR_EXCLUDED_VIOLATION; |
| 311 | } else if (r != X509_V_ERR_PERMITTED_VIOLATION) { |
| 312 | return r; |
| 313 | } |
| 314 | } |
| 315 |
| 316 | return X509_V_OK; |
| 317 | } |
| 318 |
| 319 | static int nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base) { |
| 320 | switch (base->type) { |
| 321 | case GEN_DIRNAME: |
| 322 | return nc_dn(gen->d.directoryName, base->d.directoryName); |
| 323 |
| 324 | case GEN_DNS: |
| 325 | return nc_dns(gen->d.dNSName, base->d.dNSName); |
| 326 |
| 327 | case GEN_EMAIL: |
| 328 | return nc_email(gen->d.rfc822Name, base->d.rfc822Name); |
| 329 |
| 330 | case GEN_URI: |
| 331 | return nc_uri(gen->d.uniformResourceIdentifier, |
| 332 | base->d.uniformResourceIdentifier); |
| 333 |
| 334 | default: |
| 335 | return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE; |
| 336 | } |
| 337 | } |
| 338 |
| 339 | // directoryName name constraint matching. The canonical encoding of |
| 340 | // X509_NAME makes this comparison easy. It is matched if the subtree is a |
| 341 | // subset of the name. |
| 342 |
| 343 | static int nc_dn(X509_NAME *nm, X509_NAME *base) { |
| 344 | // Ensure canonical encodings are up to date. |
| 345 | if (nm->modified && i2d_X509_NAME(nm, NULL) < 0) { |
| 346 | return X509_V_ERR_OUT_OF_MEM; |
| 347 | } |
| 348 | if (base->modified && i2d_X509_NAME(base, NULL) < 0) { |
| 349 | return X509_V_ERR_OUT_OF_MEM; |
| 350 | } |
| 351 | if (base->canon_enclen > nm->canon_enclen) { |
| 352 | return X509_V_ERR_PERMITTED_VIOLATION; |
| 353 | } |
| 354 | if (OPENSSL_memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen)) { |
| 355 | return X509_V_ERR_PERMITTED_VIOLATION; |
| 356 | } |
| 357 | return X509_V_OK; |
| 358 | } |
| 359 |
| 360 | static int starts_with(const CBS *cbs, uint8_t c) { |
| 361 | return CBS_len(cbs) > 0 && CBS_data(cbs)[0] == c; |
| 362 | } |
| 363 |
| 364 | static int equal_case(const CBS *a, const CBS *b) { |
| 365 | if (CBS_len(a) != CBS_len(b)) { |
| 366 | return 0; |
| 367 | } |
| 368 | // Note we cannot use |OPENSSL_strncasecmp| because that would stop |
| 369 | // iterating at NUL. |
| 370 | const uint8_t *a_data = CBS_data(a), *b_data = CBS_data(b); |
| 371 | for (size_t i = 0; i < CBS_len(a); i++) { |
| 372 | if (OPENSSL_tolower(a_data[i]) != OPENSSL_tolower(b_data[i])) { |
| 373 | return 0; |
| 374 | } |
| 375 | } |
| 376 | return 1; |
| 377 | } |
| 378 |
| 379 | static int has_suffix_case(const CBS *a, const CBS *b) { |
| 380 | if (CBS_len(a) < CBS_len(b)) { |
| 381 | return 0; |
| 382 | } |
| 383 | CBS copy = *a; |
| 384 | CBS_skip(©, CBS_len(a) - CBS_len(b)); |
| 385 | return equal_case(©, b); |
| 386 | } |
| 387 |
| 388 | static int nc_dns(const ASN1_IA5STRING *dns, const ASN1_IA5STRING *base) { |
| 389 | CBS dns_cbs, base_cbs; |
| 390 | CBS_init(&dns_cbs, dns->data, dns->length); |
| 391 | CBS_init(&base_cbs, base->data, base->length); |
| 392 |
| 393 | // Empty matches everything |
| 394 | if (CBS_len(&base_cbs) == 0) { |
| 395 | return X509_V_OK; |
| 396 | } |
| 397 |
| 398 | // If |base_cbs| begins with a '.', do a simple suffix comparison. This is |
| 399 | // not part of RFC5280, but is part of OpenSSL's original behavior. |
| 400 | if (starts_with(&base_cbs, '.')) { |
| 401 | if (has_suffix_case(&dns_cbs, &base_cbs)) { |
| 402 | return X509_V_OK; |
| 403 | } |
| 404 | return X509_V_ERR_PERMITTED_VIOLATION; |
| 405 | } |
| 406 |
| 407 | // Otherwise can add zero or more components on the left so compare RHS |
| 408 | // and if dns is longer and expect '.' as preceding character. |
| 409 | if (CBS_len(&dns_cbs) > CBS_len(&base_cbs)) { |
| 410 | uint8_t dot; |
| 411 | if (!CBS_skip(&dns_cbs, CBS_len(&dns_cbs) - CBS_len(&base_cbs) - 1) || |
| 412 | !CBS_get_u8(&dns_cbs, &dot) || dot != '.') { |
| 413 | return X509_V_ERR_PERMITTED_VIOLATION; |
| 414 | } |
| 415 | } |
| 416 |
| 417 | if (!equal_case(&dns_cbs, &base_cbs)) { |
| 418 | return X509_V_ERR_PERMITTED_VIOLATION; |
| 419 | } |
| 420 |
| 421 | return X509_V_OK; |
| 422 | } |
| 423 |
| 424 | static int nc_email(const ASN1_IA5STRING *eml, const ASN1_IA5STRING *base) { |
| 425 | CBS eml_cbs, base_cbs; |
| 426 | CBS_init(&eml_cbs, eml->data, eml->length); |
| 427 | CBS_init(&base_cbs, base->data, base->length); |
| 428 |
| 429 | // TODO(davidben): In OpenSSL 1.1.1, this switched from the first '@' to the |
| 430 | // last one. Match them here, or perhaps do an actual parse. Looks like |
| 431 | // multiple '@'s may be allowed in quoted strings. |
| 432 | CBS eml_local, base_local; |
| 433 | if (!CBS_get_until_first(&eml_cbs, &eml_local, '@')) { |
| 434 | return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; |
| 435 | } |
| 436 | int base_has_at = CBS_get_until_first(&base_cbs, &base_local, '@'); |
| 437 |
| 438 | // Special case: initial '.' is RHS match |
| 439 | if (!base_has_at && starts_with(&base_cbs, '.')) { |
| 440 | if (has_suffix_case(&eml_cbs, &base_cbs)) { |
| 441 | return X509_V_OK; |
| 442 | } |
| 443 | return X509_V_ERR_PERMITTED_VIOLATION; |
| 444 | } |
| 445 |
| 446 | // If we have anything before '@' match local part |
| 447 | if (base_has_at) { |
| 448 | // TODO(davidben): This interprets a constraint of "@example.com" as |
| 449 | // "example.com", which is not part of RFC5280. |
| 450 | if (CBS_len(&base_local) > 0) { |
| 451 | // Case sensitive match of local part |
| 452 | if (!CBS_mem_equal(&base_local, CBS_data(&eml_local), |
| 453 | CBS_len(&eml_local))) { |
| 454 | return X509_V_ERR_PERMITTED_VIOLATION; |
| 455 | } |
| 456 | } |
| 457 | // Position base after '@' |
| 458 | assert(starts_with(&base_cbs, '@')); |
| 459 | CBS_skip(&base_cbs, 1); |
| 460 | } |
| 461 |
| 462 | // Just have hostname left to match: case insensitive |
| 463 | assert(starts_with(&eml_cbs, '@')); |
| 464 | CBS_skip(&eml_cbs, 1); |
| 465 | if (!equal_case(&base_cbs, &eml_cbs)) { |
| 466 | return X509_V_ERR_PERMITTED_VIOLATION; |
| 467 | } |
| 468 |
| 469 | return X509_V_OK; |
| 470 | } |
| 471 |
| 472 | static int nc_uri(const ASN1_IA5STRING *uri, const ASN1_IA5STRING *base) { |
| 473 | CBS uri_cbs, base_cbs; |
| 474 | CBS_init(&uri_cbs, uri->data, uri->length); |
| 475 | CBS_init(&base_cbs, base->data, base->length); |
| 476 |
| 477 | // Check for foo:// and skip past it |
| 478 | CBS scheme; |
| 479 | uint8_t byte; |
| 480 | if (!CBS_get_until_first(&uri_cbs, &scheme, ':') || |
| 481 | !CBS_skip(&uri_cbs, 1) || // Skip the colon |
| 482 | !CBS_get_u8(&uri_cbs, &byte) || byte != '/' || |
| 483 | !CBS_get_u8(&uri_cbs, &byte) || byte != '/') { |
| 484 | return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; |
| 485 | } |
| 486 |
| 487 | // Look for a port indicator as end of hostname first. Otherwise look for |
| 488 | // trailing slash, or the end of the string. |
| 489 | // TODO(davidben): This is not a correct URI parser and mishandles IPv6 |
| 490 | // literals. |
| 491 | CBS host; |
| 492 | if (!CBS_get_until_first(&uri_cbs, &host, ':') && |
| 493 | !CBS_get_until_first(&uri_cbs, &host, '/')) { |
| 494 | host = uri_cbs; |
| 495 | } |
| 496 |
| 497 | if (CBS_len(&host) == 0) { |
| 498 | return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX; |
| 499 | } |
| 500 |
| 501 | // Special case: initial '.' is RHS match |
| 502 | if (starts_with(&base_cbs, '.')) { |
| 503 | if (has_suffix_case(&host, &base_cbs)) { |
| 504 | return X509_V_OK; |
| 505 | } |
| 506 | return X509_V_ERR_PERMITTED_VIOLATION; |
| 507 | } |
| 508 |
| 509 | if (!equal_case(&base_cbs, &host)) { |
| 510 | return X509_V_ERR_PERMITTED_VIOLATION; |
| 511 | } |
| 512 |
| 513 | return X509_V_OK; |
| 514 | } |
| 515 |