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