GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/crypto/../../libssl/src/crypto/x509v3/v3_ncons.c Lines: 0 193 0.0 %
Date: 2016-12-06 Branches: 0 151 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: v3_ncons.c,v 1.9 2015/07/29 16:13:48 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
67
static void *v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
68
    X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
69
static int i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method,
70
    void *a, BIO *bp, int ind);
71
static int do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
72
    STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp, int ind, char *name);
73
static int print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip);
74
75
static int nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc);
76
static int nc_match_single(GENERAL_NAME *sub, GENERAL_NAME *gen);
77
static int nc_dn(X509_NAME *sub, X509_NAME *nm);
78
static int nc_dns(ASN1_IA5STRING *sub, ASN1_IA5STRING *dns);
79
static int nc_email(ASN1_IA5STRING *sub, ASN1_IA5STRING *eml);
80
static int nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base);
81
82
const X509V3_EXT_METHOD v3_name_constraints = {
83
	.ext_nid = NID_name_constraints,
84
	.ext_flags = 0,
85
	.it = ASN1_ITEM_ref(NAME_CONSTRAINTS),
86
	.ext_new = NULL,
87
	.ext_free = NULL,
88
	.d2i = NULL,
89
	.i2d = NULL,
90
	.i2s = NULL,
91
	.s2i = NULL,
92
	.i2v = NULL,
93
	.v2i = v2i_NAME_CONSTRAINTS,
94
	.i2r = i2r_NAME_CONSTRAINTS,
95
	.r2i = NULL,
96
	.usr_data = NULL,
97
};
98
99
static const ASN1_TEMPLATE GENERAL_SUBTREE_seq_tt[] = {
100
	{
101
		.flags = 0,
102
		.tag = 0,
103
		.offset = offsetof(GENERAL_SUBTREE, base),
104
		.field_name = "base",
105
		.item = &GENERAL_NAME_it,
106
	},
107
	{
108
		.flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
109
		.tag = 0,
110
		.offset = offsetof(GENERAL_SUBTREE, minimum),
111
		.field_name = "minimum",
112
		.item = &ASN1_INTEGER_it,
113
	},
114
	{
115
		.flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
116
		.tag = 1,
117
		.offset = offsetof(GENERAL_SUBTREE, maximum),
118
		.field_name = "maximum",
119
		.item = &ASN1_INTEGER_it,
120
	},
121
};
122
123
const ASN1_ITEM GENERAL_SUBTREE_it = {
124
	.itype = ASN1_ITYPE_SEQUENCE,
125
	.utype = V_ASN1_SEQUENCE,
126
	.templates = GENERAL_SUBTREE_seq_tt,
127
	.tcount = sizeof(GENERAL_SUBTREE_seq_tt) / sizeof(ASN1_TEMPLATE),
128
	.funcs = NULL,
129
	.size = sizeof(GENERAL_SUBTREE),
130
	.sname = "GENERAL_SUBTREE",
131
};
132
133
static const ASN1_TEMPLATE NAME_CONSTRAINTS_seq_tt[] = {
134
	{
135
		.flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_OPTIONAL,
136
		.tag = 0,
137
		.offset = offsetof(NAME_CONSTRAINTS, permittedSubtrees),
138
		.field_name = "permittedSubtrees",
139
		.item = &GENERAL_SUBTREE_it,
140
	},
141
	{
142
		.flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_OPTIONAL,
143
		.tag = 1,
144
		.offset = offsetof(NAME_CONSTRAINTS, excludedSubtrees),
145
		.field_name = "excludedSubtrees",
146
		.item = &GENERAL_SUBTREE_it,
147
	},
148
};
149
150
const ASN1_ITEM NAME_CONSTRAINTS_it = {
151
	.itype = ASN1_ITYPE_SEQUENCE,
152
	.utype = V_ASN1_SEQUENCE,
153
	.templates = NAME_CONSTRAINTS_seq_tt,
154
	.tcount = sizeof(NAME_CONSTRAINTS_seq_tt) / sizeof(ASN1_TEMPLATE),
155
	.funcs = NULL,
156
	.size = sizeof(NAME_CONSTRAINTS),
157
	.sname = "NAME_CONSTRAINTS",
158
};
159
160
161
GENERAL_SUBTREE *
162
GENERAL_SUBTREE_new(void)
163
{
164
	return (GENERAL_SUBTREE*)ASN1_item_new(&GENERAL_SUBTREE_it);
165
}
166
167
void
168
GENERAL_SUBTREE_free(GENERAL_SUBTREE *a)
169
{
170
	ASN1_item_free((ASN1_VALUE *)a, &GENERAL_SUBTREE_it);
171
}
172
173
NAME_CONSTRAINTS *
174
NAME_CONSTRAINTS_new(void)
175
{
176
	return (NAME_CONSTRAINTS*)ASN1_item_new(&NAME_CONSTRAINTS_it);
177
}
178
179
void
180
NAME_CONSTRAINTS_free(NAME_CONSTRAINTS *a)
181
{
182
	ASN1_item_free((ASN1_VALUE *)a, &NAME_CONSTRAINTS_it);
183
}
184
185
static void *
186
v2i_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
187
    STACK_OF(CONF_VALUE) *nval)
188
{
189
	int i;
190
	CONF_VALUE tval, *val;
191
	STACK_OF(GENERAL_SUBTREE) **ptree = NULL;
192
	NAME_CONSTRAINTS *ncons = NULL;
193
	GENERAL_SUBTREE *sub = NULL;
194
195
	ncons = NAME_CONSTRAINTS_new();
196
	if (!ncons)
197
		goto memerr;
198
	for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
199
		val = sk_CONF_VALUE_value(nval, i);
200
		if (!strncmp(val->name, "permitted", 9) && val->name[9]) {
201
			ptree = &ncons->permittedSubtrees;
202
			tval.name = val->name + 10;
203
		} else if (!strncmp(val->name, "excluded", 8) && val->name[8]) {
204
			ptree = &ncons->excludedSubtrees;
205
			tval.name = val->name + 9;
206
		} else {
207
			X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS,
208
			    X509V3_R_INVALID_SYNTAX);
209
			goto err;
210
		}
211
		tval.value = val->value;
212
		sub = GENERAL_SUBTREE_new();
213
		if (!v2i_GENERAL_NAME_ex(sub->base, method, ctx, &tval, 1))
214
			goto err;
215
		if (!*ptree)
216
			*ptree = sk_GENERAL_SUBTREE_new_null();
217
		if (!*ptree || !sk_GENERAL_SUBTREE_push(*ptree, sub))
218
			goto memerr;
219
		sub = NULL;
220
	}
221
222
	return ncons;
223
224
memerr:
225
	X509V3err(X509V3_F_V2I_NAME_CONSTRAINTS, ERR_R_MALLOC_FAILURE);
226
err:
227
	if (ncons)
228
		NAME_CONSTRAINTS_free(ncons);
229
	if (sub)
230
		GENERAL_SUBTREE_free(sub);
231
232
	return NULL;
233
}
234
235
static int
236
i2r_NAME_CONSTRAINTS(const X509V3_EXT_METHOD *method, void *a, BIO *bp, int ind)
237
{
238
	NAME_CONSTRAINTS *ncons = a;
239
240
	do_i2r_name_constraints(method, ncons->permittedSubtrees,
241
	    bp, ind, "Permitted");
242
	do_i2r_name_constraints(method, ncons->excludedSubtrees,
243
	    bp, ind, "Excluded");
244
	return 1;
245
}
246
247
static int
248
do_i2r_name_constraints(const X509V3_EXT_METHOD *method,
249
    STACK_OF(GENERAL_SUBTREE) *trees, BIO *bp, int ind, char *name)
250
{
251
	GENERAL_SUBTREE *tree;
252
	int i;
253
254
	if (sk_GENERAL_SUBTREE_num(trees) > 0)
255
		BIO_printf(bp, "%*s%s:\n", ind, "", name);
256
	for (i = 0; i < sk_GENERAL_SUBTREE_num(trees); i++) {
257
		tree = sk_GENERAL_SUBTREE_value(trees, i);
258
		BIO_printf(bp, "%*s", ind + 2, "");
259
		if (tree->base->type == GEN_IPADD)
260
			print_nc_ipadd(bp, tree->base->d.ip);
261
		else
262
			GENERAL_NAME_print(bp, tree->base);
263
		BIO_puts(bp, "\n");
264
	}
265
	return 1;
266
}
267
268
static int
269
print_nc_ipadd(BIO *bp, ASN1_OCTET_STRING *ip)
270
{
271
	int i, len;
272
	unsigned char *p;
273
274
	p = ip->data;
275
	len = ip->length;
276
	BIO_puts(bp, "IP:");
277
	if (len == 8) {
278
		BIO_printf(bp, "%d.%d.%d.%d/%d.%d.%d.%d",
279
		    p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
280
	} else if (len == 32) {
281
		for (i = 0; i < 16; i++) {
282
			BIO_printf(bp, "%X", p[0] << 8 | p[1]);
283
			p += 2;
284
			if (i == 7)
285
				BIO_puts(bp, "/");
286
			else if (i != 15)
287
				BIO_puts(bp, ":");
288
		}
289
	} else
290
		BIO_printf(bp, "IP Address:<invalid>");
291
	return 1;
292
}
293
294
/* Check a certificate conforms to a specified set of constraints.
295
 * Return values:
296
 *  X509_V_OK: All constraints obeyed.
297
 *  X509_V_ERR_PERMITTED_VIOLATION: Permitted subtree violation.
298
 *  X509_V_ERR_EXCLUDED_VIOLATION: Excluded subtree violation.
299
 *  X509_V_ERR_SUBTREE_MINMAX: Min or max values present and matching type.
300
 *  X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE:  Unsupported constraint type.
301
 *  X509_V_ERR_UNSUPPORTED_CONSTRAINT_SYNTAX: bad unsupported constraint syntax.
302
 *  X509_V_ERR_UNSUPPORTED_NAME_SYNTAX: bad or unsupported syntax of name
303
 */
304
305
int
306
NAME_CONSTRAINTS_check(X509 *x, NAME_CONSTRAINTS *nc)
307
{
308
	int r, i;
309
	X509_NAME *nm;
310
311
	nm = X509_get_subject_name(x);
312
313
	if (X509_NAME_entry_count(nm) > 0) {
314
		GENERAL_NAME gntmp;
315
		gntmp.type = GEN_DIRNAME;
316
		gntmp.d.directoryName = nm;
317
318
		r = nc_match(&gntmp, nc);
319
320
		if (r != X509_V_OK)
321
			return r;
322
323
		gntmp.type = GEN_EMAIL;
324
325
		/* Process any email address attributes in subject name */
326
327
		for (i = -1;;) {
328
			X509_NAME_ENTRY *ne;
329
			i = X509_NAME_get_index_by_NID(nm,
330
			    NID_pkcs9_emailAddress, i);
331
			if (i == -1)
332
				break;
333
			ne = X509_NAME_get_entry(nm, i);
334
			gntmp.d.rfc822Name = X509_NAME_ENTRY_get_data(ne);
335
			if (gntmp.d.rfc822Name->type != V_ASN1_IA5STRING)
336
				return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
337
338
			r = nc_match(&gntmp, nc);
339
340
			if (r != X509_V_OK)
341
				return r;
342
		}
343
344
	}
345
346
	for (i = 0; i < sk_GENERAL_NAME_num(x->altname); i++) {
347
		GENERAL_NAME *gen = sk_GENERAL_NAME_value(x->altname, i);
348
		r = nc_match(gen, nc);
349
		if (r != X509_V_OK)
350
			return r;
351
	}
352
353
	return X509_V_OK;
354
}
355
356
static int
357
nc_match(GENERAL_NAME *gen, NAME_CONSTRAINTS *nc)
358
{
359
	GENERAL_SUBTREE *sub;
360
	int i, r, match = 0;
361
362
	/* Permitted subtrees: if any subtrees exist of matching the type
363
	 * at least one subtree must match.
364
	 */
365
366
	for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->permittedSubtrees); i++) {
367
		sub = sk_GENERAL_SUBTREE_value(nc->permittedSubtrees, i);
368
		if (gen->type != sub->base->type)
369
			continue;
370
		if (sub->minimum || sub->maximum)
371
			return X509_V_ERR_SUBTREE_MINMAX;
372
		/* If we already have a match don't bother trying any more */
373
		if (match == 2)
374
			continue;
375
		if (match == 0)
376
			match = 1;
377
		r = nc_match_single(gen, sub->base);
378
		if (r == X509_V_OK)
379
			match = 2;
380
		else if (r != X509_V_ERR_PERMITTED_VIOLATION)
381
			return r;
382
	}
383
384
	if (match == 1)
385
		return X509_V_ERR_PERMITTED_VIOLATION;
386
387
	/* Excluded subtrees: must not match any of these */
388
389
	for (i = 0; i < sk_GENERAL_SUBTREE_num(nc->excludedSubtrees); i++) {
390
		sub = sk_GENERAL_SUBTREE_value(nc->excludedSubtrees, i);
391
		if (gen->type != sub->base->type)
392
			continue;
393
		if (sub->minimum || sub->maximum)
394
			return X509_V_ERR_SUBTREE_MINMAX;
395
396
		r = nc_match_single(gen, sub->base);
397
		if (r == X509_V_OK)
398
			return X509_V_ERR_EXCLUDED_VIOLATION;
399
		else if (r != X509_V_ERR_PERMITTED_VIOLATION)
400
			return r;
401
402
	}
403
404
	return X509_V_OK;
405
}
406
407
static int
408
nc_match_single(GENERAL_NAME *gen, GENERAL_NAME *base)
409
{
410
	switch (base->type) {
411
	case GEN_DIRNAME:
412
		return nc_dn(gen->d.directoryName, base->d.directoryName);
413
414
	case GEN_DNS:
415
		return nc_dns(gen->d.dNSName, base->d.dNSName);
416
417
	case GEN_EMAIL:
418
		return nc_email(gen->d.rfc822Name, base->d.rfc822Name);
419
420
	case GEN_URI:
421
		return nc_uri(gen->d.uniformResourceIdentifier,
422
		    base->d.uniformResourceIdentifier);
423
424
	default:
425
		return X509_V_ERR_UNSUPPORTED_CONSTRAINT_TYPE;
426
	}
427
}
428
429
/* directoryName name constraint matching.
430
 * The canonical encoding of X509_NAME makes this comparison easy. It is
431
 * matched if the subtree is a subset of the name.
432
 */
433
434
static int
435
nc_dn(X509_NAME *nm, X509_NAME *base)
436
{
437
	/* Ensure canonical encodings are up to date.  */
438
	if (nm->modified && i2d_X509_NAME(nm, NULL) < 0)
439
		return X509_V_ERR_OUT_OF_MEM;
440
	if (base->modified && i2d_X509_NAME(base, NULL) < 0)
441
		return X509_V_ERR_OUT_OF_MEM;
442
	if (base->canon_enclen > nm->canon_enclen)
443
		return X509_V_ERR_PERMITTED_VIOLATION;
444
	if (memcmp(base->canon_enc, nm->canon_enc, base->canon_enclen))
445
		return X509_V_ERR_PERMITTED_VIOLATION;
446
	return X509_V_OK;
447
}
448
449
static int
450
nc_dns(ASN1_IA5STRING *dns, ASN1_IA5STRING *base)
451
{
452
	char *baseptr = (char *)base->data;
453
	char *dnsptr = (char *)dns->data;
454
455
	/* Empty matches everything */
456
	if (!*baseptr)
457
		return X509_V_OK;
458
	/* Otherwise can add zero or more components on the left so
459
	 * compare RHS and if dns is longer and expect '.' as preceding
460
	 * character.
461
	 */
462
	if (dns->length > base->length) {
463
		dnsptr += dns->length - base->length;
464
		if (dnsptr[-1] != '.')
465
			return X509_V_ERR_PERMITTED_VIOLATION;
466
	}
467
468
	if (strcasecmp(baseptr, dnsptr))
469
		return X509_V_ERR_PERMITTED_VIOLATION;
470
471
	return X509_V_OK;
472
}
473
474
static int
475
nc_email(ASN1_IA5STRING *eml, ASN1_IA5STRING *base)
476
{
477
	const char *baseptr = (char *)base->data;
478
	const char *emlptr = (char *)eml->data;
479
	const char *baseat = strchr(baseptr, '@');
480
	const char *emlat = strchr(emlptr, '@');
481
482
	if (!emlat)
483
		return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
484
	/* Special case: inital '.' is RHS match */
485
	if (!baseat && (*baseptr == '.')) {
486
		if (eml->length > base->length) {
487
			emlptr += eml->length - base->length;
488
			if (!strcasecmp(baseptr, emlptr))
489
				return X509_V_OK;
490
		}
491
		return X509_V_ERR_PERMITTED_VIOLATION;
492
	}
493
494
	/* If we have anything before '@' match local part */
495
496
	if (baseat) {
497
		if (baseat != baseptr) {
498
			if ((baseat - baseptr) != (emlat - emlptr))
499
				return X509_V_ERR_PERMITTED_VIOLATION;
500
			/* Case sensitive match of local part */
501
			if (strncmp(baseptr, emlptr, emlat - emlptr))
502
				return X509_V_ERR_PERMITTED_VIOLATION;
503
		}
504
		/* Position base after '@' */
505
		baseptr = baseat + 1;
506
	}
507
	emlptr = emlat + 1;
508
	/* Just have hostname left to match: case insensitive */
509
	if (strcasecmp(baseptr, emlptr))
510
		return X509_V_ERR_PERMITTED_VIOLATION;
511
512
	return X509_V_OK;
513
}
514
515
static int
516
nc_uri(ASN1_IA5STRING *uri, ASN1_IA5STRING *base)
517
{
518
	const char *baseptr = (char *)base->data;
519
	const char *hostptr = (char *)uri->data;
520
	const char *p = strchr(hostptr, ':');
521
	int hostlen;
522
523
	/* Check for foo:// and skip past it */
524
	if (!p || (p[1] != '/') || (p[2] != '/'))
525
		return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
526
	hostptr = p + 3;
527
528
	/* Determine length of hostname part of URI */
529
530
	/* Look for a port indicator as end of hostname first */
531
532
	p = strchr(hostptr, ':');
533
	/* Otherwise look for trailing slash */
534
	if (!p)
535
		p = strchr(hostptr, '/');
536
537
	if (!p)
538
		hostlen = strlen(hostptr);
539
	else
540
		hostlen = p - hostptr;
541
542
	if (hostlen == 0)
543
		return X509_V_ERR_UNSUPPORTED_NAME_SYNTAX;
544
545
	/* Special case: inital '.' is RHS match */
546
	if (*baseptr == '.') {
547
		if (hostlen > base->length) {
548
			p = hostptr + hostlen - base->length;
549
			if (!strncasecmp(p, baseptr, base->length))
550
				return X509_V_OK;
551
		}
552
		return X509_V_ERR_PERMITTED_VIOLATION;
553
	}
554
555
	if ((base->length != (int)hostlen) ||
556
	    strncasecmp(hostptr, baseptr, hostlen))
557
		return X509_V_ERR_PERMITTED_VIOLATION;
558
559
	return X509_V_OK;
560
}