summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/x509v3/v3_conf.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/x509v3/v3_conf.c')
-rw-r--r--src/lib/libcrypto/x509v3/v3_conf.c572
1 files changed, 0 insertions, 572 deletions
diff --git a/src/lib/libcrypto/x509v3/v3_conf.c b/src/lib/libcrypto/x509v3/v3_conf.c
deleted file mode 100644
index d48a4ac65c..0000000000
--- a/src/lib/libcrypto/x509v3/v3_conf.c
+++ /dev/null
@@ -1,572 +0,0 @@
1/* $OpenBSD: v3_conf.c,v 1.17 2015/02/17 05:14:38 miod 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
69static int v3_check_critical(char **value);
70static int v3_check_generic(char **value);
71static X509_EXTENSION *do_ext_nconf(CONF *conf, X509V3_CTX *ctx, int ext_nid,
72 int crit, char *value);
73static X509_EXTENSION *v3_generic_extension(const char *ext, char *value,
74 int crit, int type, X509V3_CTX *ctx);
75static char *conf_lhash_get_string(void *db, char *section, char *value);
76static STACK_OF(CONF_VALUE) *conf_lhash_get_section(void *db, char *section);
77static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method, int ext_nid,
78 int crit, void *ext_struc);
79static 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 */
84X509_EXTENSION *
85X509V3_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 */
105X509_EXTENSION *
106X509V3_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 */
120static X509_EXTENSION *
121do_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
182static X509_EXTENSION *
183do_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 = M_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 M_ASN1_OCTET_STRING_free(ext_oct);
215
216 return ext;
217
218merr:
219 M_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
227X509_EXTENSION *
228X509V3_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 */
240static int
241v3_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 */
254static int
255v3_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 */
276static X509_EXTENSION *
277v3_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;
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
298 if (ext_der == NULL) {
299 X509V3err(X509V3_F_V3_GENERIC_EXTENSION,
300 X509V3_R_EXTENSION_VALUE_ERROR);
301 ERR_asprintf_error_data("value=%s", value);
302 goto err;
303 }
304
305 if (!(oct = M_ASN1_OCTET_STRING_new())) {
306 X509V3err(X509V3_F_V3_GENERIC_EXTENSION, ERR_R_MALLOC_FAILURE);
307 goto err;
308 }
309
310 oct->data = ext_der;
311 oct->length = ext_len;
312 ext_der = NULL;
313
314 extension = X509_EXTENSION_create_by_OBJ(NULL, obj, crit, oct);
315
316err:
317 ASN1_OBJECT_free(obj);
318 M_ASN1_OCTET_STRING_free(oct);
319 free(ext_der);
320 return extension;
321}
322
323static unsigned char *
324generic_asn1(char *value, X509V3_CTX *ctx, long *ext_len)
325{
326 ASN1_TYPE *typ;
327 unsigned char *ext_der = NULL;
328
329 typ = ASN1_generate_v3(value, ctx);
330 if (typ == NULL)
331 return NULL;
332 *ext_len = i2d_ASN1_TYPE(typ, &ext_der);
333 ASN1_TYPE_free(typ);
334 return ext_der;
335}
336
337/* This is the main function: add a bunch of extensions based on a config file
338 * section to an extension STACK.
339 */
340
341int
342X509V3_EXT_add_nconf_sk(CONF *conf, X509V3_CTX *ctx, char *section,
343 STACK_OF(X509_EXTENSION) **sk)
344{
345 X509_EXTENSION *ext;
346 STACK_OF(CONF_VALUE) *nval;
347 CONF_VALUE *val;
348 int i;
349
350 if (!(nval = NCONF_get_section(conf, section)))
351 return 0;
352 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
353 val = sk_CONF_VALUE_value(nval, i);
354 if (!(ext = X509V3_EXT_nconf(conf, ctx, val->name, val->value)))
355 return 0;
356 if (sk)
357 X509v3_add_ext(sk, ext, -1);
358 X509_EXTENSION_free(ext);
359 }
360 return 1;
361}
362
363/* Convenience functions to add extensions to a certificate, CRL and request */
364
365int
366X509V3_EXT_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section, X509 *cert)
367{
368 STACK_OF(X509_EXTENSION) **sk = NULL;
369
370 if (cert)
371 sk = &cert->cert_info->extensions;
372 return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
373}
374
375/* Same as above but for a CRL */
376
377int
378X509V3_EXT_CRL_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
379 X509_CRL *crl)
380{
381 STACK_OF(X509_EXTENSION) **sk = NULL;
382
383 if (crl)
384 sk = &crl->crl->extensions;
385 return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
386}
387
388/* Add extensions to certificate request */
389
390int
391X509V3_EXT_REQ_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
392 X509_REQ *req)
393{
394 STACK_OF(X509_EXTENSION) *extlist = NULL, **sk = NULL;
395 int i;
396
397 if (req)
398 sk = &extlist;
399 i = X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
400 if (!i || !sk)
401 return i;
402 i = X509_REQ_add_extensions(req, extlist);
403 sk_X509_EXTENSION_pop_free(extlist, X509_EXTENSION_free);
404 return i;
405}
406
407/* Config database functions */
408
409char *
410X509V3_get_string(X509V3_CTX *ctx, char *name, char *section)
411{
412 if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_string) {
413 X509V3err(X509V3_F_X509V3_GET_STRING,
414 X509V3_R_OPERATION_NOT_DEFINED);
415 return NULL;
416 }
417 if (ctx->db_meth->get_string)
418 return ctx->db_meth->get_string(ctx->db, name, section);
419 return NULL;
420}
421
422STACK_OF(CONF_VALUE) *
423X509V3_get_section(X509V3_CTX *ctx, char *section)
424{
425 if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_section) {
426 X509V3err(X509V3_F_X509V3_GET_SECTION,
427 X509V3_R_OPERATION_NOT_DEFINED);
428 return NULL;
429 }
430 if (ctx->db_meth->get_section)
431 return ctx->db_meth->get_section(ctx->db, section);
432 return NULL;
433}
434
435void
436X509V3_string_free(X509V3_CTX *ctx, char *str)
437{
438 if (!str)
439 return;
440 if (ctx->db_meth->free_string)
441 ctx->db_meth->free_string(ctx->db, str);
442}
443
444void
445X509V3_section_free(X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *section)
446{
447 if (!section)
448 return;
449 if (ctx->db_meth->free_section)
450 ctx->db_meth->free_section(ctx->db, section);
451}
452
453static char *
454nconf_get_string(void *db, char *section, char *value)
455{
456 return NCONF_get_string(db, section, value);
457}
458
459static
460STACK_OF(CONF_VALUE) *nconf_get_section(void *db, char *section)
461{
462 return NCONF_get_section(db, section);
463}
464
465static X509V3_CONF_METHOD nconf_method = {
466 nconf_get_string,
467 nconf_get_section,
468 NULL,
469 NULL
470};
471
472void
473X509V3_set_nconf(X509V3_CTX *ctx, CONF *conf)
474{
475 ctx->db_meth = &nconf_method;
476 ctx->db = conf;
477}
478
479void
480X509V3_set_ctx(X509V3_CTX *ctx, X509 *issuer, X509 *subj, X509_REQ *req,
481 X509_CRL *crl, int flags)
482{
483 ctx->issuer_cert = issuer;
484 ctx->subject_cert = subj;
485 ctx->crl = crl;
486 ctx->subject_req = req;
487 ctx->flags = flags;
488}
489
490/* Old conf compatibility functions */
491
492X509_EXTENSION *
493X509V3_EXT_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx, char *name,
494 char *value)
495{
496 CONF ctmp;
497
498 CONF_set_nconf(&ctmp, conf);
499 return X509V3_EXT_nconf(&ctmp, ctx, name, value);
500}
501
502/* LHASH *conf: Config file */
503/* char *value: Value */
504X509_EXTENSION *
505X509V3_EXT_conf_nid(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx, int ext_nid,
506 char *value)
507{
508 CONF ctmp;
509
510 CONF_set_nconf(&ctmp, conf);
511 return X509V3_EXT_nconf_nid(&ctmp, ctx, ext_nid, value);
512}
513
514static char *
515conf_lhash_get_string(void *db, char *section, char *value)
516{
517 return CONF_get_string(db, section, value);
518}
519
520static STACK_OF(CONF_VALUE) *
521conf_lhash_get_section(void *db, char *section)
522{
523 return CONF_get_section(db, section);
524}
525
526static X509V3_CONF_METHOD conf_lhash_method = {
527 conf_lhash_get_string,
528 conf_lhash_get_section,
529 NULL,
530 NULL
531};
532
533void
534X509V3_set_conf_lhash(X509V3_CTX *ctx, LHASH_OF(CONF_VALUE) *lhash)
535{
536 ctx->db_meth = &conf_lhash_method;
537 ctx->db = lhash;
538}
539
540int
541X509V3_EXT_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx, char *section,
542 X509 *cert)
543{
544 CONF ctmp;
545
546 CONF_set_nconf(&ctmp, conf);
547 return X509V3_EXT_add_nconf(&ctmp, ctx, section, cert);
548}
549
550/* Same as above but for a CRL */
551
552int
553X509V3_EXT_CRL_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
554 char *section, X509_CRL *crl)
555{
556 CONF ctmp;
557
558 CONF_set_nconf(&ctmp, conf);
559 return X509V3_EXT_CRL_add_nconf(&ctmp, ctx, section, crl);
560}
561
562/* Add extensions to certificate request */
563
564int
565X509V3_EXT_REQ_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
566 char *section, X509_REQ *req)
567{
568 CONF ctmp;
569
570 CONF_set_nconf(&ctmp, conf);
571 return X509V3_EXT_REQ_add_nconf(&ctmp, ctx, section, req);
572}