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