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