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