blob: f58873e6580392fff76f6a33b65a73faf503b091 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* v3_ncons.c */
Adam Langley57707c72016-01-14 11:25:12 -08002/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
Adam Langley95c29f32014-06-20 12:00:00 -07004 * 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 Langley57707c72016-01-14 11:25:12 -080014 * notice, this list of conditions and the following disclaimer.
Adam Langley95c29f32014-06-20 12:00:00 -070015 *
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 Langley95c29f32014-06-20 12:00:00 -070058#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 Benjamin17cf2cb2016-12-13 01:07:13 -050068#include "../internal.h"
David Benjamindddb60e2021-08-11 01:23:09 -040069#include "../x509/internal.h"
David Benjamin17cf2cb2016-12-13 01:07:13 -050070
71
Adam Langley95c29f32014-06-20 12:00:00 -070072static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
Adam Langley57707c72016-01-14 11:25:12 -080073 X509V3_CTX *ctx,
74 STACK_OF(CONF_VALUE) *nval);
75static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
76 BIO *bp, int ind);
Adam Langley95c29f32014-06-20 12:00:00 -070077static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
Adam Langley57707c72016-01-14 11:25:12 -080078 STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp,
79 int ind, const char *name);
Adam Langley95c29f32014-06-20 12:00:00 -070080static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip);
81
82static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc);
83static int nc_match_single(GENERAL_NAME *sub, GENERAL_NAME *gen);
84static int nc_dn(X509_NAME *sub, X509_NAME *nm);
85static int nc_dns(ASN1_IA5STRING *sub, ASN1_IA5STRING *dns);
86static int nc_email(ASN1_IA5STRING *sub, ASN1_IA5STRING *eml);
87static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base);
88
89const X509V3_EXT_METHOD v3_name_constraints = {
Adam Langley57707c72016-01-14 11:25:12 -080090 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 Langley95c29f32014-06-20 12:00:00 -070097};
98
99ASN1_SEQUENCE(GENERAL_SUBTREE) = {
Adam Langley57707c72016-01-14 11:25:12 -0800100 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 Langley95c29f32014-06-20 12:00:00 -0700103} ASN1_SEQUENCE_END(GENERAL_SUBTREE)
104
105ASN1_SEQUENCE(NAME_CONSTRAINTS) = {
Adam Langley57707c72016-01-14 11:25:12 -0800106 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 Langley95c29f32014-06-20 12:00:00 -0700110} ASN1_SEQUENCE_END(NAME_CONSTRAINTS)
Adam Langley57707c72016-01-14 11:25:12 -0800111
Adam Langley95c29f32014-06-20 12:00:00 -0700112
113IMPLEMENT_ASN1_ALLOC_FUNCTIONS(GENERAL_SUBTREE)
114IMPLEMENT_ASN1_ALLOC_FUNCTIONS(NAME_CONSTRAINTS)
115
116static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
Adam Langley57707c72016-01-14 11:25:12 -0800117 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 Langley95c29f32014-06-20 12:00:00 -0700149
Adam Langley57707c72016-01-14 11:25:12 -0800150 return ncons;
Adam Langley95c29f32014-06-20 12:00:00 -0700151
Adam Langley57707c72016-01-14 11:25:12 -0800152 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 Langley95c29f32014-06-20 12:00:00 -0700159
Adam Langley57707c72016-01-14 11:25:12 -0800160 return NULL;
161}
Adam Langley95c29f32014-06-20 12:00:00 -0700162
163static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a,
Adam Langley57707c72016-01-14 11:25:12 -0800164 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 Langley95c29f32014-06-20 12:00:00 -0700173
174static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
Adam Langley57707c72016-01-14 11:25:12 -0800175 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 Langley95c29f32014-06-20 12:00:00 -0700193
194static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip)
Adam Langley57707c72016-01-14 11:25:12 -0800195{
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 Benjamin4f1fae32021-12-15 11:41:10 -0500206 uint16_t v = ((uint16_t)p[0] << 8) | p[1];
207 BIO_printf(bp, "%X", v);
Adam Langley57707c72016-01-14 11:25:12 -0800208 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 Langley95c29f32014-06-20 12:00:00 -0700218
Martin Kreichgauerb86be362017-08-14 15:55:48 -0700219/*-
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 Langley95c29f32014-06-20 12:00:00 -0700231 */
232
233int NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc)
Adam Langley57707c72016-01-14 11:25:12 -0800234{
235 int r, i;
236 size_t j;
237 X509_NAME *nm;
Adam Langley95c29f32014-06-20 12:00:00 -0700238
Adam Langley57707c72016-01-14 11:25:12 -0800239 nm = X509_get_subject_name(x);
Adam Langley95c29f32014-06-20 12:00:00 -0700240
Martin Kreichgauerb86be362017-08-14 15:55:48 -0700241 /* 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 Langley57707c72016-01-14 11:25:12 -0800256 if (X509_NAME_entry_count(nm) > 0) {
257 GENERAL_NAME gntmp;
258 gntmp.type = GEN_DIRNAME;
259 gntmp.d.directoryName = nm;
Adam Langley95c29f32014-06-20 12:00:00 -0700260
Adam Langley57707c72016-01-14 11:25:12 -0800261 r = nc_match(&gntmp, nc);
Adam Langley95c29f32014-06-20 12:00:00 -0700262
Adam Langley57707c72016-01-14 11:25:12 -0800263 if (r != X509_V_OK)
264 return r;
Adam Langley95c29f32014-06-20 12:00:00 -0700265
Adam Langley57707c72016-01-14 11:25:12 -0800266 gntmp.type = GEN_EMAIL;
Adam Langley95c29f32014-06-20 12:00:00 -0700267
Adam Langley57707c72016-01-14 11:25:12 -0800268 /* Process any email address attributes in subject name */
Adam Langley95c29f32014-06-20 12:00:00 -0700269
Adam Langley57707c72016-01-14 11:25:12 -0800270 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 Langley95c29f32014-06-20 12:00:00 -0700279
Adam Langley57707c72016-01-14 11:25:12 -0800280 r = nc_match(&gntmp, nc);
Adam Langley95c29f32014-06-20 12:00:00 -0700281
Adam Langley57707c72016-01-14 11:25:12 -0800282 if (r != X509_V_OK)
283 return r;
284 }
Adam Langley95c29f32014-06-20 12:00:00 -0700285
Adam Langley57707c72016-01-14 11:25:12 -0800286 }
Adam Langley95c29f32014-06-20 12:00:00 -0700287
Adam Langley57707c72016-01-14 11:25:12 -0800288 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 Langley95c29f32014-06-20 12:00:00 -0700294
Adam Langley57707c72016-01-14 11:25:12 -0800295 return X509_V_OK;
Adam Langley95c29f32014-06-20 12:00:00 -0700296
Adam Langley57707c72016-01-14 11:25:12 -0800297}
Adam Langley95c29f32014-06-20 12:00:00 -0700298
299static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
Adam Langley57707c72016-01-14 11:25:12 -0800300{
301 GENERAL_SUBTREE *sub;
302 int r, match = 0;
303 size_t i;
Adam Langley95c29f32014-06-20 12:00:00 -0700304
Adam Langley57707c72016-01-14 11:25:12 -0800305 /*
306 * Permitted subtrees: if any subtrees exist of matching the type at
307 * least one subtree must match.
308 */
Adam Langley95c29f32014-06-20 12:00:00 -0700309
Adam Langley57707c72016-01-14 11:25:12 -0800310 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 Langley95c29f32014-06-20 12:00:00 -0700327
Adam Langley57707c72016-01-14 11:25:12 -0800328 if (match == 1)
329 return X509_V_ERR_PERMITTED_VIOLATION;
Adam Langley95c29f32014-06-20 12:00:00 -0700330
Adam Langley57707c72016-01-14 11:25:12 -0800331 /* Excluded subtrees: must not match any of these */
Adam Langley95c29f32014-06-20 12:00:00 -0700332
Adam Langley57707c72016-01-14 11:25:12 -0800333 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 Langley95c29f32014-06-20 12:00:00 -0700339
Adam Langley57707c72016-01-14 11:25:12 -0800340 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 Langley95c29f32014-06-20 12:00:00 -0700345
Adam Langley57707c72016-01-14 11:25:12 -0800346 }
Adam Langley95c29f32014-06-20 12:00:00 -0700347
Adam Langley57707c72016-01-14 11:25:12 -0800348 return X509_V_OK;
Adam Langley95c29f32014-06-20 12:00:00 -0700349
Adam Langley57707c72016-01-14 11:25:12 -0800350}
Adam Langley95c29f32014-06-20 12:00:00 -0700351
352static int nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base)
Adam Langley57707c72016-01-14 11:25:12 -0800353{
354 switch (base->type) {
355 case GEN_DIRNAME:
356 return nc_dn(gen->d.directoryName, base->d.directoryName);
Adam Langley95c29f32014-06-20 12:00:00 -0700357
Adam Langley57707c72016-01-14 11:25:12 -0800358 case GEN_DNS:
359 return nc_dns(gen->d.dNSName, base->d.dNSName);
Adam Langley95c29f32014-06-20 12:00:00 -0700360
Adam Langley57707c72016-01-14 11:25:12 -0800361 case GEN_EMAIL:
362 return nc_email(gen->d.rfc822Name, base->d.rfc822Name);
Adam Langley95c29f32014-06-20 12:00:00 -0700363
Adam Langley57707c72016-01-14 11:25:12 -0800364 case GEN_URI:
365 return nc_uri(gen->d.uniformResourceIdentifier,
366 base->d.uniformResourceIdentifier);
Adam Langley95c29f32014-06-20 12:00:00 -0700367
Adam Langley57707c72016-01-14 11:25:12 -0800368 default:
369 return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
370 }
Adam Langley95c29f32014-06-20 12:00:00 -0700371
Adam Langley57707c72016-01-14 11:25:12 -0800372}
Adam Langley95c29f32014-06-20 12:00:00 -0700373
Adam Langley57707c72016-01-14 11:25:12 -0800374/*
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 Langley95c29f32014-06-20 12:00:00 -0700378 */
379
380static int nc_dn(X509_NAME *nm, X509_NAME *base)
Adam Langley57707c72016-01-14 11:25:12 -0800381{
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 Benjamin17cf2cb2016-12-13 01:07:13 -0500389 if (OPENSSL_memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen))
Adam Langley57707c72016-01-14 11:25:12 -0800390 return X509_V_ERR_PERMITTED_VIOLATION;
391 return X509_V_OK;
392}
Adam Langley95c29f32014-06-20 12:00:00 -0700393
David Benjaminb27438e2021-08-24 14:24:38 -0400394static int starts_with(const CBS *cbs, uint8_t c)
395{
396 return CBS_len(cbs) > 0 && CBS_data(cbs)[0] == c;
397}
398
399static 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
415static 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(&copy, CBS_len(a) - CBS_len(b));
422 return equal_case(&copy, b);
423}
424
Adam Langley95c29f32014-06-20 12:00:00 -0700425static int nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base)
Adam Langley57707c72016-01-14 11:25:12 -0800426{
David Benjaminb27438e2021-08-24 14:24:38 -0400427 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 Langley57707c72016-01-14 11:25:12 -0800431 /* Empty matches everything */
David Benjaminb27438e2021-08-24 14:24:38 -0400432 if (CBS_len(&base_cbs) == 0) {
Adam Langley57707c72016-01-14 11:25:12 -0800433 return X509_V_OK;
David Benjaminb27438e2021-08-24 14:24:38 -0400434 }
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 Langley57707c72016-01-14 11:25:12 -0800445 /*
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 Benjaminb27438e2021-08-24 14:24:38 -0400449 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 Langley57707c72016-01-14 11:25:12 -0800454 return X509_V_ERR_PERMITTED_VIOLATION;
David Benjaminb27438e2021-08-24 14:24:38 -0400455 }
Adam Langley57707c72016-01-14 11:25:12 -0800456 }
Adam Langley95c29f32014-06-20 12:00:00 -0700457
David Benjaminb27438e2021-08-24 14:24:38 -0400458 if (!equal_case(&dns_cbs, &base_cbs)) {
Adam Langley57707c72016-01-14 11:25:12 -0800459 return X509_V_ERR_PERMITTED_VIOLATION;
David Benjaminb27438e2021-08-24 14:24:38 -0400460 }
Adam Langley95c29f32014-06-20 12:00:00 -0700461
Adam Langley57707c72016-01-14 11:25:12 -0800462 return X509_V_OK;
Adam Langley95c29f32014-06-20 12:00:00 -0700463
Adam Langley57707c72016-01-14 11:25:12 -0800464}
Adam Langley95c29f32014-06-20 12:00:00 -0700465
466static int nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base)
Adam Langley57707c72016-01-14 11:25:12 -0800467{
David Benjaminb27438e2021-08-24 14:24:38 -0400468 CBS eml_cbs, base_cbs;
469 CBS_init(&eml_cbs, eml->data, eml->length);
470 CBS_init(&base_cbs, base->data, base->length);
Adam Langley95c29f32014-06-20 12:00:00 -0700471
David Benjaminb27438e2021-08-24 14:24:38 -0400472 /* 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 Langley57707c72016-01-14 11:25:12 -0800477 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
David Benjaminb27438e2021-08-24 14:24:38 -0400478 }
479 int base_has_at = CBS_get_until_first(&base_cbs, &base_local, '@');
480
Adam Langley57707c72016-01-14 11:25:12 -0800481 /* Special case: inital '.' is RHS match */
David Benjaminb27438e2021-08-24 14:24:38 -0400482 if (!base_has_at && starts_with(&base_cbs, '.')) {
483 if (has_suffix_case(&eml_cbs, &base_cbs)) {
484 return X509_V_OK;
Adam Langley57707c72016-01-14 11:25:12 -0800485 }
486 return X509_V_ERR_PERMITTED_VIOLATION;
487 }
Adam Langley95c29f32014-06-20 12:00:00 -0700488
Adam Langley57707c72016-01-14 11:25:12 -0800489 /* If we have anything before '@' match local part */
David Benjaminb27438e2021-08-24 14:24:38 -0400490 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 Langley57707c72016-01-14 11:25:12 -0800494 /* Case sensitive match of local part */
David Benjaminb27438e2021-08-24 14:24:38 -0400495 if (!CBS_mem_equal(&base_local, CBS_data(&eml_local),
496 CBS_len(&eml_local))) {
Adam Langley57707c72016-01-14 11:25:12 -0800497 return X509_V_ERR_PERMITTED_VIOLATION;
David Benjaminb27438e2021-08-24 14:24:38 -0400498 }
Adam Langley57707c72016-01-14 11:25:12 -0800499 }
500 /* Position base after '@' */
David Benjaminb27438e2021-08-24 14:24:38 -0400501 assert(starts_with(&base_cbs, '@'));
502 CBS_skip(&base_cbs, 1);
Adam Langley57707c72016-01-14 11:25:12 -0800503 }
David Benjaminb27438e2021-08-24 14:24:38 -0400504
Adam Langley57707c72016-01-14 11:25:12 -0800505 /* Just have hostname left to match: case insensitive */
David Benjaminb27438e2021-08-24 14:24:38 -0400506 assert(starts_with(&eml_cbs, '@'));
507 CBS_skip(&eml_cbs, 1);
508 if (!equal_case(&base_cbs, &eml_cbs)) {
Adam Langley57707c72016-01-14 11:25:12 -0800509 return X509_V_ERR_PERMITTED_VIOLATION;
David Benjaminb27438e2021-08-24 14:24:38 -0400510 }
Adam Langley95c29f32014-06-20 12:00:00 -0700511
Adam Langley57707c72016-01-14 11:25:12 -0800512 return X509_V_OK;
Adam Langley57707c72016-01-14 11:25:12 -0800513}
Adam Langley95c29f32014-06-20 12:00:00 -0700514
515static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base)
Adam Langley57707c72016-01-14 11:25:12 -0800516{
David Benjaminb27438e2021-08-24 14:24:38 -0400517 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 Langley57707c72016-01-14 11:25:12 -0800521 /* Check for foo:// and skip past it */
David Benjaminb27438e2021-08-24 14:24:38 -0400522 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 Langley57707c72016-01-14 11:25:12 -0800528 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
David Benjaminb27438e2021-08-24 14:24:38 -0400529 }
Adam Langley95c29f32014-06-20 12:00:00 -0700530
David Benjaminb27438e2021-08-24 14:24:38 -0400531 /* 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 Langley95c29f32014-06-20 12:00:00 -0700540
David Benjaminb27438e2021-08-24 14:24:38 -0400541 if (CBS_len(&host) == 0) {
Adam Langley57707c72016-01-14 11:25:12 -0800542 return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
David Benjaminb27438e2021-08-24 14:24:38 -0400543 }
Adam Langley95c29f32014-06-20 12:00:00 -0700544
Adam Langley57707c72016-01-14 11:25:12 -0800545 /* Special case: inital '.' is RHS match */
David Benjaminb27438e2021-08-24 14:24:38 -0400546 if (starts_with(&base_cbs, '.')) {
547 if (has_suffix_case(&host, &base_cbs)) {
548 return X509_V_OK;
Adam Langley57707c72016-01-14 11:25:12 -0800549 }
550 return X509_V_ERR_PERMITTED_VIOLATION;
551 }
Adam Langley95c29f32014-06-20 12:00:00 -0700552
David Benjaminb27438e2021-08-24 14:24:38 -0400553 if (!equal_case(&base_cbs, &host)) {
Adam Langley57707c72016-01-14 11:25:12 -0800554 return X509_V_ERR_PERMITTED_VIOLATION;
David Benjaminb27438e2021-08-24 14:24:38 -0400555 }
Adam Langley95c29f32014-06-20 12:00:00 -0700556
Adam Langley57707c72016-01-14 11:25:12 -0800557 return X509_V_OK;
Adam Langley95c29f32014-06-20 12:00:00 -0700558
Adam Langley57707c72016-01-14 11:25:12 -0800559}