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