Copyright consolidation 07/10
[openssl.git] / crypto / x509v3 / v3_conf.c
1 /*
2  * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /* extension creation utilities */
11
12 #include <stdio.h>
13 #include <ctype.h>
14 #include "internal/cryptlib.h"
15 #include <openssl/conf.h>
16 #include <openssl/x509.h>
17 #include "internal/x509_int.h"
18 #include <openssl/x509v3.h>
19
20 static int v3_check_critical(char **value);
21 static int v3_check_generic(char **value);
22 static X509_EXTENSION *do_ext_nconf(CONF *conf, X509V3_CTX *ctx, int ext_nid,
23                                     int crit, char *value);
24 static X509_EXTENSION *v3_generic_extension(const char *ext, char *value,
25                                             int crit, int type,
26                                             X509V3_CTX *ctx);
27 static char *conf_lhash_get_string(void *db, char *section, char *value);
28 static STACK_OF(CONF_VALUE) *conf_lhash_get_section(void *db, char *section);
29 static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method,
30                                   int ext_nid, int crit, void *ext_struc);
31 static unsigned char *generic_asn1(char *value, X509V3_CTX *ctx,
32                                    long *ext_len);
33 /* CONF *conf:  Config file    */
34 /* char *name:  Name    */
35 /* char *value:  Value    */
36 X509_EXTENSION *X509V3_EXT_nconf(CONF *conf, X509V3_CTX *ctx, char *name,
37                                  char *value)
38 {
39     int crit;
40     int ext_type;
41     X509_EXTENSION *ret;
42     crit = v3_check_critical(&value);
43     if ((ext_type = v3_check_generic(&value)))
44         return v3_generic_extension(name, value, crit, ext_type, ctx);
45     ret = do_ext_nconf(conf, ctx, OBJ_sn2nid(name), crit, value);
46     if (!ret) {
47         X509V3err(X509V3_F_X509V3_EXT_NCONF, X509V3_R_ERROR_IN_EXTENSION);
48         ERR_add_error_data(4, "name=", name, ", value=", value);
49     }
50     return ret;
51 }
52
53 /* CONF *conf:  Config file    */
54 /* char *value:  Value    */
55 X509_EXTENSION *X509V3_EXT_nconf_nid(CONF *conf, X509V3_CTX *ctx, int ext_nid,
56                                      char *value)
57 {
58     int crit;
59     int ext_type;
60     crit = v3_check_critical(&value);
61     if ((ext_type = v3_check_generic(&value)))
62         return v3_generic_extension(OBJ_nid2sn(ext_nid),
63                                     value, crit, ext_type, ctx);
64     return do_ext_nconf(conf, ctx, ext_nid, crit, value);
65 }
66
67 /* CONF *conf:  Config file    */
68 /* char *value:  Value    */
69 static X509_EXTENSION *do_ext_nconf(CONF *conf, X509V3_CTX *ctx, int ext_nid,
70                                     int crit, char *value)
71 {
72     const X509V3_EXT_METHOD *method;
73     X509_EXTENSION *ext;
74     STACK_OF(CONF_VALUE) *nval;
75     void *ext_struc;
76
77     if (ext_nid == NID_undef) {
78         X509V3err(X509V3_F_DO_EXT_NCONF, X509V3_R_UNKNOWN_EXTENSION_NAME);
79         return NULL;
80     }
81     if ((method = X509V3_EXT_get_nid(ext_nid)) == NULL) {
82         X509V3err(X509V3_F_DO_EXT_NCONF, X509V3_R_UNKNOWN_EXTENSION);
83         return NULL;
84     }
85     /* Now get internal extension representation based on type */
86     if (method->v2i) {
87         if (*value == '@')
88             nval = NCONF_get_section(conf, value + 1);
89         else
90             nval = X509V3_parse_list(value);
91         if (sk_CONF_VALUE_num(nval) <= 0) {
92             X509V3err(X509V3_F_DO_EXT_NCONF,
93                       X509V3_R_INVALID_EXTENSION_STRING);
94             ERR_add_error_data(4, "name=", OBJ_nid2sn(ext_nid), ",section=",
95                                value);
96             return NULL;
97         }
98         ext_struc = method->v2i(method, ctx, nval);
99         if (*value != '@')
100             sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
101         if (!ext_struc)
102             return NULL;
103     } else if (method->s2i) {
104         if ((ext_struc = method->s2i(method, ctx, value)) == NULL)
105             return NULL;
106     } else if (method->r2i) {
107         if (!ctx->db || !ctx->db_meth) {
108             X509V3err(X509V3_F_DO_EXT_NCONF, X509V3_R_NO_CONFIG_DATABASE);
109             return NULL;
110         }
111         if ((ext_struc = method->r2i(method, ctx, value)) == NULL)
112             return NULL;
113     } else {
114         X509V3err(X509V3_F_DO_EXT_NCONF,
115                   X509V3_R_EXTENSION_SETTING_NOT_SUPPORTED);
116         ERR_add_error_data(2, "name=", OBJ_nid2sn(ext_nid));
117         return NULL;
118     }
119
120     ext = do_ext_i2d(method, ext_nid, crit, ext_struc);
121     if (method->it)
122         ASN1_item_free(ext_struc, ASN1_ITEM_ptr(method->it));
123     else
124         method->ext_free(ext_struc);
125     return ext;
126
127 }
128
129 static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method,
130                                   int ext_nid, int crit, void *ext_struc)
131 {
132     unsigned char *ext_der = NULL;
133     int ext_len;
134     ASN1_OCTET_STRING *ext_oct = NULL;
135     X509_EXTENSION *ext;
136     /* Convert internal representation to DER */
137     if (method->it) {
138         ext_der = NULL;
139         ext_len =
140             ASN1_item_i2d(ext_struc, &ext_der, ASN1_ITEM_ptr(method->it));
141         if (ext_len < 0)
142             goto merr;
143     } else {
144         unsigned char *p;
145
146         ext_len = method->i2d(ext_struc, NULL);
147         if ((ext_der = OPENSSL_malloc(ext_len)) == NULL)
148             goto merr;
149         p = ext_der;
150         method->i2d(ext_struc, &p);
151     }
152     if ((ext_oct = ASN1_OCTET_STRING_new()) == NULL)
153         goto merr;
154     ext_oct->data = ext_der;
155     ext_der = NULL;
156     ext_oct->length = ext_len;
157
158     ext = X509_EXTENSION_create_by_NID(NULL, ext_nid, crit, ext_oct);
159     if (!ext)
160         goto merr;
161     ASN1_OCTET_STRING_free(ext_oct);
162
163     return ext;
164
165  merr:
166     X509V3err(X509V3_F_DO_EXT_I2D, ERR_R_MALLOC_FAILURE);
167     OPENSSL_free(ext_der);
168     ASN1_OCTET_STRING_free(ext_oct);
169     return NULL;
170
171 }
172
173 /* Given an internal structure, nid and critical flag create an extension */
174
175 X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit, void *ext_struc)
176 {
177     const X509V3_EXT_METHOD *method;
178
179     if ((method = X509V3_EXT_get_nid(ext_nid)) == NULL) {
180         X509V3err(X509V3_F_X509V3_EXT_I2D, X509V3_R_UNKNOWN_EXTENSION);
181         return NULL;
182     }
183     return do_ext_i2d(method, ext_nid, crit, ext_struc);
184 }
185
186 /* Check the extension string for critical flag */
187 static int v3_check_critical(char **value)
188 {
189     char *p = *value;
190     if ((strlen(p) < 9) || strncmp(p, "critical,", 9))
191         return 0;
192     p += 9;
193     while (isspace((unsigned char)*p))
194         p++;
195     *value = p;
196     return 1;
197 }
198
199 /* Check extension string for generic extension and return the type */
200 static int v3_check_generic(char **value)
201 {
202     int gen_type = 0;
203     char *p = *value;
204     if ((strlen(p) >= 4) && strncmp(p, "DER:", 4) == 0) {
205         p += 4;
206         gen_type = 1;
207     } else if ((strlen(p) >= 5) && strncmp(p, "ASN1:", 5) == 0) {
208         p += 5;
209         gen_type = 2;
210     } else
211         return 0;
212
213     while (isspace((unsigned char)*p))
214         p++;
215     *value = p;
216     return gen_type;
217 }
218
219 /* Create a generic extension: for now just handle DER type */
220 static X509_EXTENSION *v3_generic_extension(const char *ext, char *value,
221                                             int crit, int gen_type,
222                                             X509V3_CTX *ctx)
223 {
224     unsigned char *ext_der = NULL;
225     long ext_len = 0;
226     ASN1_OBJECT *obj = NULL;
227     ASN1_OCTET_STRING *oct = NULL;
228     X509_EXTENSION *extension = NULL;
229
230     if ((obj = OBJ_txt2obj(ext, 0)) == NULL) {
231         X509V3err(X509V3_F_V3_GENERIC_EXTENSION,
232                   X509V3_R_EXTENSION_NAME_ERROR);
233         ERR_add_error_data(2, "name=", ext);
234         goto err;
235     }
236
237     if (gen_type == 1)
238         ext_der = OPENSSL_hexstr2buf(value, &ext_len);
239     else if (gen_type == 2)
240         ext_der = generic_asn1(value, ctx, &ext_len);
241
242     if (ext_der == NULL) {
243         X509V3err(X509V3_F_V3_GENERIC_EXTENSION,
244                   X509V3_R_EXTENSION_VALUE_ERROR);
245         ERR_add_error_data(2, "value=", value);
246         goto err;
247     }
248
249     if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
250         X509V3err(X509V3_F_V3_GENERIC_EXTENSION, ERR_R_MALLOC_FAILURE);
251         goto err;
252     }
253
254     oct->data = ext_der;
255     oct->length = ext_len;
256     ext_der = NULL;
257
258     extension = X509_EXTENSION_create_by_OBJ(NULL, obj, crit, oct);
259
260  err:
261     ASN1_OBJECT_free(obj);
262     ASN1_OCTET_STRING_free(oct);
263     OPENSSL_free(ext_der);
264     return extension;
265
266 }
267
268 static unsigned char *generic_asn1(char *value, X509V3_CTX *ctx,
269                                    long *ext_len)
270 {
271     ASN1_TYPE *typ;
272     unsigned char *ext_der = NULL;
273     typ = ASN1_generate_v3(value, ctx);
274     if (typ == NULL)
275         return NULL;
276     *ext_len = i2d_ASN1_TYPE(typ, &ext_der);
277     ASN1_TYPE_free(typ);
278     return ext_der;
279 }
280
281 static void delete_ext(STACK_OF(X509_EXTENSION) *sk, X509_EXTENSION *dext)
282 {
283     int idx;
284     ASN1_OBJECT *obj;
285     obj = X509_EXTENSION_get_object(dext);
286     while ((idx = X509v3_get_ext_by_OBJ(sk, obj, -1)) >= 0) {
287         X509_EXTENSION *tmpext = X509v3_get_ext(sk, idx);
288         X509v3_delete_ext(sk, idx);
289         X509_EXTENSION_free(tmpext);
290     }
291 }
292
293 /*
294  * This is the main function: add a bunch of extensions based on a config
295  * file section to an extension STACK.
296  */
297
298 int X509V3_EXT_add_nconf_sk(CONF *conf, X509V3_CTX *ctx, char *section,
299                             STACK_OF(X509_EXTENSION) **sk)
300 {
301     X509_EXTENSION *ext;
302     STACK_OF(CONF_VALUE) *nval;
303     CONF_VALUE *val;
304     int i;
305
306     if ((nval = NCONF_get_section(conf, section)) == NULL)
307         return 0;
308     for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
309         val = sk_CONF_VALUE_value(nval, i);
310         if ((ext = X509V3_EXT_nconf(conf, ctx, val->name, val->value)) == NULL)
311             return 0;
312         if (ctx->flags == X509V3_CTX_REPLACE)
313             delete_ext(*sk, ext);
314         if (sk)
315             X509v3_add_ext(sk, ext, -1);
316         X509_EXTENSION_free(ext);
317     }
318     return 1;
319 }
320
321 /*
322  * Convenience functions to add extensions to a certificate, CRL and request
323  */
324
325 int X509V3_EXT_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
326                          X509 *cert)
327 {
328     STACK_OF(X509_EXTENSION) **sk = NULL;
329     if (cert)
330         sk = &cert->cert_info.extensions;
331     return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
332 }
333
334 /* Same as above but for a CRL */
335
336 int X509V3_EXT_CRL_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
337                              X509_CRL *crl)
338 {
339     STACK_OF(X509_EXTENSION) **sk = NULL;
340     if (crl)
341         sk = &crl->crl.extensions;
342     return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
343 }
344
345 /* Add extensions to certificate request */
346
347 int X509V3_EXT_REQ_add_nconf(CONF *conf, X509V3_CTX *ctx, char *section,
348                              X509_REQ *req)
349 {
350     STACK_OF(X509_EXTENSION) *extlist = NULL, **sk = NULL;
351     int i;
352     if (req)
353         sk = &extlist;
354     i = X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
355     if (!i || !sk)
356         return i;
357     i = X509_REQ_add_extensions(req, extlist);
358     sk_X509_EXTENSION_pop_free(extlist, X509_EXTENSION_free);
359     return i;
360 }
361
362 /* Config database functions */
363
364 char *X509V3_get_string(X509V3_CTX *ctx, char *name, char *section)
365 {
366     if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_string) {
367         X509V3err(X509V3_F_X509V3_GET_STRING, X509V3_R_OPERATION_NOT_DEFINED);
368         return NULL;
369     }
370     if (ctx->db_meth->get_string)
371         return ctx->db_meth->get_string(ctx->db, name, section);
372     return NULL;
373 }
374
375 STACK_OF(CONF_VALUE) *X509V3_get_section(X509V3_CTX *ctx, char *section)
376 {
377     if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_section) {
378         X509V3err(X509V3_F_X509V3_GET_SECTION,
379                   X509V3_R_OPERATION_NOT_DEFINED);
380         return NULL;
381     }
382     if (ctx->db_meth->get_section)
383         return ctx->db_meth->get_section(ctx->db, section);
384     return NULL;
385 }
386
387 void X509V3_string_free(X509V3_CTX *ctx, char *str)
388 {
389     if (!str)
390         return;
391     if (ctx->db_meth->free_string)
392         ctx->db_meth->free_string(ctx->db, str);
393 }
394
395 void X509V3_section_free(X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *section)
396 {
397     if (!section)
398         return;
399     if (ctx->db_meth->free_section)
400         ctx->db_meth->free_section(ctx->db, section);
401 }
402
403 static char *nconf_get_string(void *db, char *section, char *value)
404 {
405     return NCONF_get_string(db, section, value);
406 }
407
408 static STACK_OF(CONF_VALUE) *nconf_get_section(void *db, char *section)
409 {
410     return NCONF_get_section(db, section);
411 }
412
413 static X509V3_CONF_METHOD nconf_method = {
414     nconf_get_string,
415     nconf_get_section,
416     NULL,
417     NULL
418 };
419
420 void X509V3_set_nconf(X509V3_CTX *ctx, CONF *conf)
421 {
422     ctx->db_meth = &nconf_method;
423     ctx->db = conf;
424 }
425
426 void X509V3_set_ctx(X509V3_CTX *ctx, X509 *issuer, X509 *subj, X509_REQ *req,
427                     X509_CRL *crl, int flags)
428 {
429     ctx->issuer_cert = issuer;
430     ctx->subject_cert = subj;
431     ctx->crl = crl;
432     ctx->subject_req = req;
433     ctx->flags = flags;
434 }
435
436 /* Old conf compatibility functions */
437
438 X509_EXTENSION *X509V3_EXT_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
439                                 char *name, char *value)
440 {
441     CONF ctmp;
442     CONF_set_nconf(&ctmp, conf);
443     return X509V3_EXT_nconf(&ctmp, ctx, name, value);
444 }
445
446 /* LHASH *conf:  Config file    */
447 /* char *value:  Value    */
448 X509_EXTENSION *X509V3_EXT_conf_nid(LHASH_OF(CONF_VALUE) *conf,
449                                     X509V3_CTX *ctx, int ext_nid, char *value)
450 {
451     CONF ctmp;
452     CONF_set_nconf(&ctmp, conf);
453     return X509V3_EXT_nconf_nid(&ctmp, ctx, ext_nid, value);
454 }
455
456 static char *conf_lhash_get_string(void *db, char *section, char *value)
457 {
458     return CONF_get_string(db, section, value);
459 }
460
461 static STACK_OF(CONF_VALUE) *conf_lhash_get_section(void *db, char *section)
462 {
463     return CONF_get_section(db, section);
464 }
465
466 static X509V3_CONF_METHOD conf_lhash_method = {
467     conf_lhash_get_string,
468     conf_lhash_get_section,
469     NULL,
470     NULL
471 };
472
473 void X509V3_set_conf_lhash(X509V3_CTX *ctx, LHASH_OF(CONF_VALUE) *lhash)
474 {
475     ctx->db_meth = &conf_lhash_method;
476     ctx->db = lhash;
477 }
478
479 int X509V3_EXT_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
480                         char *section, X509 *cert)
481 {
482     CONF ctmp;
483     CONF_set_nconf(&ctmp, conf);
484     return X509V3_EXT_add_nconf(&ctmp, ctx, section, cert);
485 }
486
487 /* Same as above but for a CRL */
488
489 int X509V3_EXT_CRL_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
490                             char *section, X509_CRL *crl)
491 {
492     CONF ctmp;
493     CONF_set_nconf(&ctmp, conf);
494     return X509V3_EXT_CRL_add_nconf(&ctmp, ctx, section, crl);
495 }
496
497 /* Add extensions to certificate request */
498
499 int X509V3_EXT_REQ_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
500                             char *section, X509_REQ *req)
501 {
502     CONF ctmp;
503     CONF_set_nconf(&ctmp, conf);
504     return X509V3_EXT_REQ_add_nconf(&ctmp, ctx, section, req);
505 }