Coverage Report

Created: 2026-10-28 06:28

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