Coverage Report

Created: 2026-03-03 06:43

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