GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/x509v3/v3_conf.c Lines: 79 206 38.3 %
Date: 2017-11-07 Branches: 37 117 31.6 %

Line Branch Exec Source
1
/* $OpenBSD: v3_conf.c,v 1.21 2017/01/29 17:49:23 beck Exp $ */
2
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3
 * project 1999.
4
 */
5
/* ====================================================================
6
 * Copyright (c) 1999-2002 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
/* extension creation utilities */
59
60
#include <ctype.h>
61
#include <stdio.h>
62
#include <string.h>
63
64
#include <openssl/conf.h>
65
#include <openssl/err.h>
66
#include <openssl/x509.h>
67
#include <openssl/x509v3.h>
68
69
static int v3_check_critical(char **value);
70
static int v3_check_generic(char **value);
71
static X509_EXTENSION *do_ext_nconf(CONF *conf, X509V3_CTX *ctx, int ext_nid,
72
    int crit, char *value);
73
static X509_EXTENSION *v3_generic_extension(const char *ext, char *value,
74
    int crit, int type, X509V3_CTX *ctx);
75
static char *conf_lhash_get_string(void *db, char *section, char *value);
76
static STACK_OF(CONF_VALUE) *conf_lhash_get_section(void *db, char *section);
77
static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method, int ext_nid,
78
    int crit, void *ext_struc);
79
static unsigned char *generic_asn1(char *value, X509V3_CTX *ctx, long *ext_len);
80
81
/* CONF *conf:  Config file    */
82
/* char *name:  Name    */
83
/* char *value:  Value    */
84
X509_EXTENSION *
85
X509V3_EXT_nconf(CONF *conf, X509V3_CTX *ctx, char *name, char *value)
86
{
87
	int crit;
88
	int ext_type;
89
	X509_EXTENSION *ret;
90
91
20
	crit = v3_check_critical(&value);
92
20
	if ((ext_type = v3_check_generic(&value)))
93
		return v3_generic_extension(name, value, crit, ext_type, ctx);
94
20
	ret = do_ext_nconf(conf, ctx, OBJ_sn2nid(name), crit, value);
95
20
	if (!ret) {
96
		X509V3error(X509V3_R_ERROR_IN_EXTENSION);
97
		ERR_asprintf_error_data("name=%s, value=%s", name, value);
98
	}
99
20
	return ret;
100
20
}
101
102
/* CONF *conf:  Config file    */
103
/* char *value:  Value    */
104
X509_EXTENSION *
105
X509V3_EXT_nconf_nid(CONF *conf, X509V3_CTX *ctx, int ext_nid, char *value)
106
{
107
	int crit;
108
	int ext_type;
109
110
	crit = v3_check_critical(&value);
111
	if ((ext_type = v3_check_generic(&value)))
112
		return v3_generic_extension(OBJ_nid2sn(ext_nid),
113
		    value, crit, ext_type, ctx);
114
	return do_ext_nconf(conf, ctx, ext_nid, crit, value);
115
}
116
117
/* CONF *conf:  Config file    */
118
/* char *value:  Value    */
119
static X509_EXTENSION *
120
do_ext_nconf(CONF *conf, X509V3_CTX *ctx, int ext_nid, int crit, char *value)
121
{
122
	const X509V3_EXT_METHOD *method;
123
	X509_EXTENSION *ext;
124
	void *ext_struc;
125
126
40
	if (ext_nid == NID_undef) {
127
		X509V3error(X509V3_R_UNKNOWN_EXTENSION_NAME);
128
		return NULL;
129
	}
130
20
	if (!(method = X509V3_EXT_get_nid(ext_nid))) {
131
		X509V3error(X509V3_R_UNKNOWN_EXTENSION);
132
		return NULL;
133
	}
134
	/* Now get internal extension representation based on type */
135
20
	if (method->v2i) {
136
		STACK_OF(CONF_VALUE) *nval;
137
138
20
		if (*value == '@')
139
			nval = NCONF_get_section(conf, value + 1);
140
		else
141
20
			nval = X509V3_parse_list(value);
142
20
		if (sk_CONF_VALUE_num(nval) <= 0) {
143
			X509V3error(X509V3_R_INVALID_EXTENSION_STRING);
144
			ERR_asprintf_error_data("name=%s,section=%s",
145
			    OBJ_nid2sn(ext_nid), value);
146
			if (*value != '@')
147
				sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
148
			return NULL;
149
		}
150
20
		ext_struc = method->v2i(method, ctx, nval);
151
20
		if (*value != '@')
152
20
			sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
153

20
	} else if (method->s2i) {
154
		ext_struc = method->s2i(method, ctx, value);
155
	} else if (method->r2i) {
156
		if (!ctx->db || !ctx->db_meth) {
157
			X509V3error(X509V3_R_NO_CONFIG_DATABASE);
158
			return NULL;
159
		}
160
		ext_struc = method->r2i(method, ctx, value);
161
	} else {
162
		X509V3error(X509V3_R_EXTENSION_SETTING_NOT_SUPPORTED);
163
		ERR_asprintf_error_data("name=%s", OBJ_nid2sn(ext_nid));
164
		return NULL;
165
	}
166
20
	if (ext_struc == NULL)
167
		return NULL;
168
169
20
	ext = do_ext_i2d(method, ext_nid, crit, ext_struc);
170
20
	if (method->it)
171
20
		ASN1_item_free(ext_struc, method->it);
172
	else
173
		method->ext_free(ext_struc);
174
20
	return ext;
175
20
}
176
177
static X509_EXTENSION *
178
do_ext_i2d(const X509V3_EXT_METHOD *method, int ext_nid, int crit,
179
    void *ext_struc)
180
{
181
156
	unsigned char *ext_der;
182
	int ext_len;
183
	ASN1_OCTET_STRING *ext_oct = NULL;
184
	X509_EXTENSION *ext;
185
186
	/* Convert internal representation to DER */
187
78
	if (method->it) {
188
74
		ext_der = NULL;
189
148
		ext_len = ASN1_item_i2d(ext_struc, &ext_der,
190
74
		    method->it);
191
74
		if (ext_len < 0)
192
			goto merr;
193
	} else {
194
4
		unsigned char *p;
195
4
		ext_len = method->i2d(ext_struc, NULL);
196
4
		if (!(ext_der = malloc(ext_len)))
197
			goto merr;
198
4
		p = ext_der;
199
4
		method->i2d(ext_struc, &p);
200
12
	}
201
78
	if (!(ext_oct = ASN1_OCTET_STRING_new()))
202
		goto merr;
203
78
	ext_oct->data = ext_der;
204
78
	ext_oct->length = ext_len;
205
206
78
	ext = X509_EXTENSION_create_by_NID(NULL, ext_nid, crit, ext_oct);
207
78
	if (!ext)
208
		goto merr;
209
78
	ASN1_OCTET_STRING_free(ext_oct);
210
211
78
	return ext;
212
213
merr:
214
	ASN1_OCTET_STRING_free(ext_oct);
215
	X509V3error(ERR_R_MALLOC_FAILURE);
216
	return NULL;
217
218
78
}
219
220
/* Given an internal structure, nid and critical flag create an extension */
221
222
X509_EXTENSION *
223
X509V3_EXT_i2d(int ext_nid, int crit, void *ext_struc)
224
{
225
	const X509V3_EXT_METHOD *method;
226
227
116
	if (!(method = X509V3_EXT_get_nid(ext_nid))) {
228
		X509V3error(X509V3_R_UNKNOWN_EXTENSION);
229
		return NULL;
230
	}
231
58
	return do_ext_i2d(method, ext_nid, crit, ext_struc);
232
58
}
233
234
/* Check the extension string for critical flag */
235
static int
236
v3_check_critical(char **value)
237
{
238
40
	char *p = *value;
239
240

36
	if ((strlen(p) < 9) || strncmp(p, "critical,", 9))
241
12
		return 0;
242
8
	p += 9;
243
16
	while (isspace((unsigned char)*p)) p++;
244
8
		*value = p;
245
8
	return 1;
246
20
}
247
248
/* Check extension string for generic extension and return the type */
249
static int
250
v3_check_generic(char **value)
251
{
252
	int gen_type = 0;
253
40
	char *p = *value;
254
255

40
	if ((strlen(p) >= 4) && !strncmp(p, "DER:", 4)) {
256
		p += 4;
257
		gen_type = 1;
258

40
	} else if ((strlen(p) >= 5) && !strncmp(p, "ASN1:", 5)) {
259
		p += 5;
260
		gen_type = 2;
261
	} else
262
20
		return 0;
263
264
	while (isspace((unsigned char)*p))
265
		p++;
266
	*value = p;
267
	return gen_type;
268
20
}
269
270
/* Create a generic extension: for now just handle DER type */
271
static X509_EXTENSION *
272
v3_generic_extension(const char *ext, char *value, int crit, int gen_type,
273
    X509V3_CTX *ctx)
274
{
275
	unsigned char *ext_der = NULL;
276
	long ext_len = 0;
277
	ASN1_OBJECT *obj = NULL;
278
	ASN1_OCTET_STRING *oct = NULL;
279
	X509_EXTENSION *extension = NULL;
280
281
	if (!(obj = OBJ_txt2obj(ext, 0))) {
282
		X509V3error(X509V3_R_EXTENSION_NAME_ERROR);
283
		ERR_asprintf_error_data("name=%s", ext);
284
		goto err;
285
	}
286
287
	if (gen_type == 1)
288
		ext_der = string_to_hex(value, &ext_len);
289
	else if (gen_type == 2)
290
		ext_der = generic_asn1(value, ctx, &ext_len);
291
	else {
292
		ERR_asprintf_error_data("Unexpected generic extension type %d", gen_type);
293
		goto err;
294
	}
295
296
	if (ext_der == NULL) {
297
		X509V3error(X509V3_R_EXTENSION_VALUE_ERROR);
298
		ERR_asprintf_error_data("value=%s", value);
299
		goto err;
300
	}
301
302
	if (!(oct = ASN1_OCTET_STRING_new())) {
303
		X509V3error(ERR_R_MALLOC_FAILURE);
304
		goto err;
305
	}
306
307
	oct->data = ext_der;
308
	oct->length = ext_len;
309
	ext_der = NULL;
310
311
	extension = X509_EXTENSION_create_by_OBJ(NULL, obj, crit, oct);
312
313
err:
314
	ASN1_OBJECT_free(obj);
315
	ASN1_OCTET_STRING_free(oct);
316
	free(ext_der);
317
	return extension;
318
}
319
320
static unsigned char *
321
generic_asn1(char *value, X509V3_CTX *ctx, long *ext_len)
322
{
323
	ASN1_TYPE *typ;
324
	unsigned char *ext_der = NULL;
325
326
	typ = ASN1_generate_v3(value, ctx);
327
	if (typ == NULL)
328
		return NULL;
329
	*ext_len = i2d_ASN1_TYPE(typ, &ext_der);
330
	ASN1_TYPE_free(typ);
331
	return ext_der;
332
}
333
334
/* This is the main function: add a bunch of extensions based on a config file
335
 * section to an extension STACK.
336
 */
337
338
int
339
X509V3_EXT_add_nconf_sk(CONF *conf, X509V3_CTX *ctx, char *section,
340
    STACK_OF(X509_EXTENSION) **sk)
341
{
342
	X509_EXTENSION *ext;
343
	STACK_OF(CONF_VALUE) *nval;
344
	CONF_VALUE *val;
345
	int i;
346
347
16
	if (!(nval = NCONF_get_section(conf, section)))
348
		return 0;
349
56
	for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
350
20
		val = sk_CONF_VALUE_value(nval, i);
351
20
		if (!(ext = X509V3_EXT_nconf(conf, ctx, val->name, val->value)))
352
			return 0;
353
20
		if (sk)
354
10
			X509v3_add_ext(sk, ext, -1);
355
20
		X509_EXTENSION_free(ext);
356
	}
357
8
	return 1;
358
8
}
359
360
/* Convenience functions to add extensions to a certificate, CRL and request */
361
362
int
363
X509V3_EXT_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section, X509 *cert)
364
{
365
	STACK_OF(X509_EXTENSION) **sk = NULL;
366
367
16
	if (cert)
368
4
		sk = &cert->cert_info->extensions;
369
8
	return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
370
}
371
372
/* Same as above but for a CRL */
373
374
int
375
X509V3_EXT_CRL_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
376
    X509_CRL *crl)
377
{
378
	STACK_OF(X509_EXTENSION) **sk = NULL;
379
380
	if (crl)
381
		sk = &crl->crl->extensions;
382
	return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
383
}
384
385
/* Add extensions to certificate request */
386
387
int
388
X509V3_EXT_REQ_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
389
    X509_REQ *req)
390
{
391
	STACK_OF(X509_EXTENSION) *extlist = NULL, **sk = NULL;
392
	int i;
393
394
	if (req)
395
		sk = &extlist;
396
	i = X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
397
	if (!i || !sk)
398
		return i;
399
	i = X509_REQ_add_extensions(req, extlist);
400
	sk_X509_EXTENSION_pop_free(extlist, X509_EXTENSION_free);
401
	return i;
402
}
403
404
/* Config database functions */
405
406
char *
407
X509V3_get_string(X509V3_CTX *ctx, char *name, char *section)
408
{
409
	if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_string) {
410
		X509V3error(X509V3_R_OPERATION_NOT_DEFINED);
411
		return NULL;
412
	}
413
	if (ctx->db_meth->get_string)
414
		return ctx->db_meth->get_string(ctx->db, name, section);
415
	return NULL;
416
}
417
418
STACK_OF(CONF_VALUE) *
419
X509V3_get_section(X509V3_CTX *ctx, char *section)
420
{
421
	if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_section) {
422
		X509V3error(X509V3_R_OPERATION_NOT_DEFINED);
423
		return NULL;
424
	}
425
	if (ctx->db_meth->get_section)
426
		return ctx->db_meth->get_section(ctx->db, section);
427
	return NULL;
428
}
429
430
void
431
X509V3_string_free(X509V3_CTX *ctx, char *str)
432
{
433
	if (!str)
434
		return;
435
	if (ctx->db_meth->free_string)
436
		ctx->db_meth->free_string(ctx->db, str);
437
}
438
439
void
440
X509V3_section_free(X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *section)
441
{
442
	if (!section)
443
		return;
444
	if (ctx->db_meth->free_section)
445
		ctx->db_meth->free_section(ctx->db, section);
446
}
447
448
static char *
449
nconf_get_string(void *db, char *section, char *value)
450
{
451
	return NCONF_get_string(db, section, value);
452
}
453
454
static
455
STACK_OF(CONF_VALUE) *nconf_get_section(void *db, char *section)
456
{
457
	return NCONF_get_section(db, section);
458
}
459
460
static X509V3_CONF_METHOD nconf_method = {
461
	nconf_get_string,
462
	nconf_get_section,
463
	NULL,
464
	NULL
465
};
466
467
void
468
X509V3_set_nconf(X509V3_CTX *ctx, CONF *conf)
469
{
470
92
	ctx->db_meth = &nconf_method;
471
46
	ctx->db = conf;
472
46
}
473
474
void
475
X509V3_set_ctx(X509V3_CTX *ctx, X509 *issuer, X509 *subj, X509_REQ *req,
476
    X509_CRL *crl, int flags)
477
{
478
92
	ctx->issuer_cert = issuer;
479
46
	ctx->subject_cert = subj;
480
46
	ctx->crl = crl;
481
46
	ctx->subject_req = req;
482
46
	ctx->flags = flags;
483
46
}
484
485
/* Old conf compatibility functions */
486
487
X509_EXTENSION *
488
X509V3_EXT_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx, char *name,
489
    char *value)
490
{
491
	CONF ctmp;
492
493
	CONF_set_nconf(&ctmp, conf);
494
	return X509V3_EXT_nconf(&ctmp, ctx, name, value);
495
}
496
497
/* LHASH *conf:  Config file    */
498
/* char *value:  Value    */
499
X509_EXTENSION *
500
X509V3_EXT_conf_nid(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx, int ext_nid,
501
    char *value)
502
{
503
	CONF ctmp;
504
505
	CONF_set_nconf(&ctmp, conf);
506
	return X509V3_EXT_nconf_nid(&ctmp, ctx, ext_nid, value);
507
}
508
509
static char *
510
conf_lhash_get_string(void *db, char *section, char *value)
511
{
512
	return CONF_get_string(db, section, value);
513
}
514
515
static STACK_OF(CONF_VALUE) *
516
conf_lhash_get_section(void *db, char *section)
517
{
518
	return CONF_get_section(db, section);
519
}
520
521
static X509V3_CONF_METHOD conf_lhash_method = {
522
	conf_lhash_get_string,
523
	conf_lhash_get_section,
524
	NULL,
525
	NULL
526
};
527
528
void
529
X509V3_set_conf_lhash(X509V3_CTX *ctx, LHASH_OF(CONF_VALUE) *lhash)
530
{
531
	ctx->db_meth = &conf_lhash_method;
532
	ctx->db = lhash;
533
}
534
535
int
536
X509V3_EXT_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx, char *section,
537
    X509 *cert)
538
{
539
	CONF ctmp;
540
541
	CONF_set_nconf(&ctmp, conf);
542
	return X509V3_EXT_add_nconf(&ctmp, ctx, section, cert);
543
}
544
545
/* Same as above but for a CRL */
546
547
int
548
X509V3_EXT_CRL_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
549
    char *section, X509_CRL *crl)
550
{
551
	CONF ctmp;
552
553
	CONF_set_nconf(&ctmp, conf);
554
	return X509V3_EXT_CRL_add_nconf(&ctmp, ctx, section, crl);
555
}
556
557
/* Add extensions to certificate request */
558
559
int
560
X509V3_EXT_REQ_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
561
    char *section, X509_REQ *req)
562
{
563
	CONF ctmp;
564
565
	CONF_set_nconf(&ctmp, conf);
566
	return X509V3_EXT_REQ_add_nconf(&ctmp, ctx, section, req);
567
}