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