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

Line Branch Exec Source
1
/* $OpenBSD: v3_conf.c,v 1.19 2015/12/14 03:39:14 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
	crit = v3_check_critical(&value);
92
	if ((ext_type = v3_check_generic(&value)))
93
		return v3_generic_extension(name, value, crit, ext_type, ctx);
94
	ret = do_ext_nconf(conf, ctx, OBJ_sn2nid(name), crit, value);
95
	if (!ret) {
96
		X509V3err(X509V3_F_X509V3_EXT_NCONF,
97
		    X509V3_R_ERROR_IN_EXTENSION);
98
		ERR_asprintf_error_data("name=%s, value=%s", name, value);
99
	}
100
	return ret;
101
}
102
103
/* CONF *conf:  Config file    */
104
/* char *value:  Value    */
105
X509_EXTENSION *
106
X509V3_EXT_nconf_nid(CONF *conf, X509V3_CTX *ctx, int ext_nid, char *value)
107
{
108
	int crit;
109
	int ext_type;
110
111
	crit = v3_check_critical(&value);
112
	if ((ext_type = v3_check_generic(&value)))
113
		return v3_generic_extension(OBJ_nid2sn(ext_nid),
114
		    value, crit, ext_type, ctx);
115
	return do_ext_nconf(conf, ctx, ext_nid, crit, value);
116
}
117
118
/* CONF *conf:  Config file    */
119
/* char *value:  Value    */
120
static X509_EXTENSION *
121
do_ext_nconf(CONF *conf, X509V3_CTX *ctx, int ext_nid, int crit, char *value)
122
{
123
	const X509V3_EXT_METHOD *method;
124
	X509_EXTENSION *ext;
125
	void *ext_struc;
126
127
	if (ext_nid == NID_undef) {
128
		X509V3err(X509V3_F_DO_EXT_NCONF,
129
		    X509V3_R_UNKNOWN_EXTENSION_NAME);
130
		return NULL;
131
	}
132
	if (!(method = X509V3_EXT_get_nid(ext_nid))) {
133
		X509V3err(X509V3_F_DO_EXT_NCONF, X509V3_R_UNKNOWN_EXTENSION);
134
		return NULL;
135
	}
136
	/* Now get internal extension representation based on type */
137
	if (method->v2i) {
138
		STACK_OF(CONF_VALUE) *nval;
139
140
		if (*value == '@')
141
			nval = NCONF_get_section(conf, value + 1);
142
		else
143
			nval = X509V3_parse_list(value);
144
		if (sk_CONF_VALUE_num(nval) <= 0) {
145
			X509V3err(X509V3_F_DO_EXT_NCONF,
146
			    X509V3_R_INVALID_EXTENSION_STRING);
147
			ERR_asprintf_error_data("name=%s,section=%s",
148
			    OBJ_nid2sn(ext_nid), value);
149
			if (*value != '@')
150
				sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
151
			return NULL;
152
		}
153
		ext_struc = method->v2i(method, ctx, nval);
154
		if (*value != '@')
155
			sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
156
	} else if (method->s2i) {
157
		ext_struc = method->s2i(method, ctx, value);
158
	} else if (method->r2i) {
159
		if (!ctx->db || !ctx->db_meth) {
160
			X509V3err(X509V3_F_DO_EXT_NCONF,
161
			    X509V3_R_NO_CONFIG_DATABASE);
162
			return NULL;
163
		}
164
		ext_struc = method->r2i(method, ctx, value);
165
	} else {
166
		X509V3err(X509V3_F_DO_EXT_NCONF,
167
		    X509V3_R_EXTENSION_SETTING_NOT_SUPPORTED);
168
		ERR_asprintf_error_data("name=%s", OBJ_nid2sn(ext_nid));
169
		return NULL;
170
	}
171
	if (ext_struc == NULL)
172
		return NULL;
173
174
	ext = do_ext_i2d(method, ext_nid, crit, ext_struc);
175
	if (method->it)
176
		ASN1_item_free(ext_struc, ASN1_ITEM_ptr(method->it));
177
	else
178
		method->ext_free(ext_struc);
179
	return ext;
180
}
181
182
static X509_EXTENSION *
183
do_ext_i2d(const X509V3_EXT_METHOD *method, int ext_nid, int crit,
184
    void *ext_struc)
185
{
186
	unsigned char *ext_der;
187
	int ext_len;
188
	ASN1_OCTET_STRING *ext_oct = NULL;
189
	X509_EXTENSION *ext;
190
191
	/* Convert internal representation to DER */
192
	if (method->it) {
193
		ext_der = NULL;
194
		ext_len = ASN1_item_i2d(ext_struc, &ext_der,
195
		    ASN1_ITEM_ptr(method->it));
196
		if (ext_len < 0)
197
			goto merr;
198
	} else {
199
		unsigned char *p;
200
		ext_len = method->i2d(ext_struc, NULL);
201
		if (!(ext_der = malloc(ext_len)))
202
			goto merr;
203
		p = ext_der;
204
		method->i2d(ext_struc, &p);
205
	}
206
	if (!(ext_oct = ASN1_OCTET_STRING_new()))
207
		goto merr;
208
	ext_oct->data = ext_der;
209
	ext_oct->length = ext_len;
210
211
	ext = X509_EXTENSION_create_by_NID(NULL, ext_nid, crit, ext_oct);
212
	if (!ext)
213
		goto merr;
214
	ASN1_OCTET_STRING_free(ext_oct);
215
216
	return ext;
217
218
merr:
219
	ASN1_OCTET_STRING_free(ext_oct);
220
	X509V3err(X509V3_F_DO_EXT_I2D, ERR_R_MALLOC_FAILURE);
221
	return NULL;
222
223
}
224
225
/* Given an internal structure, nid and critical flag create an extension */
226
227
X509_EXTENSION *
228
X509V3_EXT_i2d(int ext_nid, int crit, void *ext_struc)
229
{
230
	const X509V3_EXT_METHOD *method;
231
232
	if (!(method = X509V3_EXT_get_nid(ext_nid))) {
233
		X509V3err(X509V3_F_X509V3_EXT_I2D, X509V3_R_UNKNOWN_EXTENSION);
234
		return NULL;
235
	}
236
	return do_ext_i2d(method, ext_nid, crit, ext_struc);
237
}
238
239
/* Check the extension string for critical flag */
240
static int
241
v3_check_critical(char **value)
242
{
243
	char *p = *value;
244
245
	if ((strlen(p) < 9) || strncmp(p, "critical,", 9))
246
		return 0;
247
	p += 9;
248
	while (isspace((unsigned char)*p)) p++;
249
		*value = p;
250
	return 1;
251
}
252
253
/* Check extension string for generic extension and return the type */
254
static int
255
v3_check_generic(char **value)
256
{
257
	int gen_type = 0;
258
	char *p = *value;
259
260
	if ((strlen(p) >= 4) && !strncmp(p, "DER:", 4)) {
261
		p += 4;
262
		gen_type = 1;
263
	} else if ((strlen(p) >= 5) && !strncmp(p, "ASN1:", 5)) {
264
		p += 5;
265
		gen_type = 2;
266
	} else
267
		return 0;
268
269
	while (isspace((unsigned char)*p))
270
		p++;
271
	*value = p;
272
	return gen_type;
273
}
274
275
/* Create a generic extension: for now just handle DER type */
276
static X509_EXTENSION *
277
v3_generic_extension(const char *ext, char *value, int crit, int gen_type,
278
    X509V3_CTX *ctx)
279
{
280
	unsigned char *ext_der = NULL;
281
	long ext_len = 0;
282
	ASN1_OBJECT *obj = NULL;
283
	ASN1_OCTET_STRING *oct = NULL;
284
	X509_EXTENSION *extension = NULL;
285
286
	if (!(obj = OBJ_txt2obj(ext, 0))) {
287
		X509V3err(X509V3_F_V3_GENERIC_EXTENSION,
288
		    X509V3_R_EXTENSION_NAME_ERROR);
289
		ERR_asprintf_error_data("name=%s", ext);
290
		goto err;
291
	}
292
293
	if (gen_type == 1)
294
		ext_der = string_to_hex(value, &ext_len);
295
	else if (gen_type == 2)
296
		ext_der = generic_asn1(value, ctx, &ext_len);
297
	else {
298
		ERR_asprintf_error_data("Unexpected generic extension type %d", gen_type);
299
		goto err;
300
	}
301
302
	if (ext_der == NULL) {
303
		X509V3err(X509V3_F_V3_GENERIC_EXTENSION,
304
		    X509V3_R_EXTENSION_VALUE_ERROR);
305
		ERR_asprintf_error_data("value=%s", value);
306
		goto err;
307
	}
308
309
	if (!(oct = ASN1_OCTET_STRING_new())) {
310
		X509V3err(X509V3_F_V3_GENERIC_EXTENSION, ERR_R_MALLOC_FAILURE);
311
		goto err;
312
	}
313
314
	oct->data = ext_der;
315
	oct->length = ext_len;
316
	ext_der = NULL;
317
318
	extension = X509_EXTENSION_create_by_OBJ(NULL, obj, crit, oct);
319
320
err:
321
	ASN1_OBJECT_free(obj);
322
	ASN1_OCTET_STRING_free(oct);
323
	free(ext_der);
324
	return extension;
325
}
326
327
static unsigned char *
328
generic_asn1(char *value, X509V3_CTX *ctx, long *ext_len)
329
{
330
	ASN1_TYPE *typ;
331
	unsigned char *ext_der = NULL;
332
333
	typ = ASN1_generate_v3(value, ctx);
334
	if (typ == NULL)
335
		return NULL;
336
	*ext_len = i2d_ASN1_TYPE(typ, &ext_der);
337
	ASN1_TYPE_free(typ);
338
	return ext_der;
339
}
340
341
/* This is the main function: add a bunch of extensions based on a config file
342
 * section to an extension STACK.
343
 */
344
345
int
346
X509V3_EXT_add_nconf_sk(CONF *conf, X509V3_CTX *ctx, char *section,
347
    STACK_OF(X509_EXTENSION) **sk)
348
{
349
	X509_EXTENSION *ext;
350
	STACK_OF(CONF_VALUE) *nval;
351
	CONF_VALUE *val;
352
	int i;
353
354
	if (!(nval = NCONF_get_section(conf, section)))
355
		return 0;
356
	for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
357
		val = sk_CONF_VALUE_value(nval, i);
358
		if (!(ext = X509V3_EXT_nconf(conf, ctx, val->name, val->value)))
359
			return 0;
360
		if (sk)
361
			X509v3_add_ext(sk, ext, -1);
362
		X509_EXTENSION_free(ext);
363
	}
364
	return 1;
365
}
366
367
/* Convenience functions to add extensions to a certificate, CRL and request */
368
369
int
370
X509V3_EXT_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section, X509 *cert)
371
{
372
	STACK_OF(X509_EXTENSION) **sk = NULL;
373
374
	if (cert)
375
		sk = &cert->cert_info->extensions;
376
	return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
377
}
378
379
/* Same as above but for a CRL */
380
381
int
382
X509V3_EXT_CRL_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
383
    X509_CRL *crl)
384
{
385
	STACK_OF(X509_EXTENSION) **sk = NULL;
386
387
	if (crl)
388
		sk = &crl->crl->extensions;
389
	return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
390
}
391
392
/* Add extensions to certificate request */
393
394
int
395
X509V3_EXT_REQ_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
396
    X509_REQ *req)
397
{
398
	STACK_OF(X509_EXTENSION) *extlist = NULL, **sk = NULL;
399
	int i;
400
401
	if (req)
402
		sk = &extlist;
403
	i = X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
404
	if (!i || !sk)
405
		return i;
406
	i = X509_REQ_add_extensions(req, extlist);
407
	sk_X509_EXTENSION_pop_free(extlist, X509_EXTENSION_free);
408
	return i;
409
}
410
411
/* Config database functions */
412
413
char *
414
X509V3_get_string(X509V3_CTX *ctx, char *name, char *section)
415
{
416
	if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_string) {
417
		X509V3err(X509V3_F_X509V3_GET_STRING,
418
		    X509V3_R_OPERATION_NOT_DEFINED);
419
		return NULL;
420
	}
421
	if (ctx->db_meth->get_string)
422
		return ctx->db_meth->get_string(ctx->db, name, section);
423
	return NULL;
424
}
425
426
STACK_OF(CONF_VALUE) *
427
X509V3_get_section(X509V3_CTX *ctx, char *section)
428
{
429
	if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_section) {
430
		X509V3err(X509V3_F_X509V3_GET_SECTION,
431
		    X509V3_R_OPERATION_NOT_DEFINED);
432
		return NULL;
433
	}
434
	if (ctx->db_meth->get_section)
435
		return ctx->db_meth->get_section(ctx->db, section);
436
	return NULL;
437
}
438
439
void
440
X509V3_string_free(X509V3_CTX *ctx, char *str)
441
{
442
	if (!str)
443
		return;
444
	if (ctx->db_meth->free_string)
445
		ctx->db_meth->free_string(ctx->db, str);
446
}
447
448
void
449
X509V3_section_free(X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *section)
450
{
451
	if (!section)
452
		return;
453
	if (ctx->db_meth->free_section)
454
		ctx->db_meth->free_section(ctx->db, section);
455
}
456
457
static char *
458
nconf_get_string(void *db, char *section, char *value)
459
{
460
	return NCONF_get_string(db, section, value);
461
}
462
463
static
464
STACK_OF(CONF_VALUE) *nconf_get_section(void *db, char *section)
465
{
466
	return NCONF_get_section(db, section);
467
}
468
469
static X509V3_CONF_METHOD nconf_method = {
470
	nconf_get_string,
471
	nconf_get_section,
472
	NULL,
473
	NULL
474
};
475
476
void
477
X509V3_set_nconf(X509V3_CTX *ctx, CONF *conf)
478
{
479
	ctx->db_meth = &nconf_method;
480
	ctx->db = conf;
481
}
482
483
void
484
X509V3_set_ctx(X509V3_CTX *ctx, X509 *issuer, X509 *subj, X509_REQ *req,
485
    X509_CRL *crl, int flags)
486
{
487
	ctx->issuer_cert = issuer;
488
	ctx->subject_cert = subj;
489
	ctx->crl = crl;
490
	ctx->subject_req = req;
491
	ctx->flags = flags;
492
}
493
494
/* Old conf compatibility functions */
495
496
X509_EXTENSION *
497
X509V3_EXT_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx, char *name,
498
    char *value)
499
{
500
	CONF ctmp;
501
502
	CONF_set_nconf(&ctmp, conf);
503
	return X509V3_EXT_nconf(&ctmp, ctx, name, value);
504
}
505
506
/* LHASH *conf:  Config file    */
507
/* char *value:  Value    */
508
X509_EXTENSION *
509
X509V3_EXT_conf_nid(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx, int ext_nid,
510
    char *value)
511
{
512
	CONF ctmp;
513
514
	CONF_set_nconf(&ctmp, conf);
515
	return X509V3_EXT_nconf_nid(&ctmp, ctx, ext_nid, value);
516
}
517
518
static char *
519
conf_lhash_get_string(void *db, char *section, char *value)
520
{
521
	return CONF_get_string(db, section, value);
522
}
523
524
static STACK_OF(CONF_VALUE) *
525
conf_lhash_get_section(void *db, char *section)
526
{
527
	return CONF_get_section(db, section);
528
}
529
530
static X509V3_CONF_METHOD conf_lhash_method = {
531
	conf_lhash_get_string,
532
	conf_lhash_get_section,
533
	NULL,
534
	NULL
535
};
536
537
void
538
X509V3_set_conf_lhash(X509V3_CTX *ctx, LHASH_OF(CONF_VALUE) *lhash)
539
{
540
	ctx->db_meth = &conf_lhash_method;
541
	ctx->db = lhash;
542
}
543
544
int
545
X509V3_EXT_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx, char *section,
546
    X509 *cert)
547
{
548
	CONF ctmp;
549
550
	CONF_set_nconf(&ctmp, conf);
551
	return X509V3_EXT_add_nconf(&ctmp, ctx, section, cert);
552
}
553
554
/* Same as above but for a CRL */
555
556
int
557
X509V3_EXT_CRL_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
558
    char *section, X509_CRL *crl)
559
{
560
	CONF ctmp;
561
562
	CONF_set_nconf(&ctmp, conf);
563
	return X509V3_EXT_CRL_add_nconf(&ctmp, ctx, section, crl);
564
}
565
566
/* Add extensions to certificate request */
567
568
int
569
X509V3_EXT_REQ_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
570
    char *section, X509_REQ *req)
571
{
572
	CONF ctmp;
573
574
	CONF_set_nconf(&ctmp, conf);
575
	return X509V3_EXT_REQ_add_nconf(&ctmp, ctx, section, req);
576
}