Coverage Report

Created: 2026-10-28 06:28

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-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
    const 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
41.8k
{
325
41.8k
    X509_EXTENSION *ext;
326
41.8k
    STACK_OF(CONF_VALUE) *nval;
327
41.8k
    const CONF_VALUE *val;
328
41.8k
    int i, akid = -1, skid = -1;
329
41.8k
    X509V3_CTX tmpctx;
330
331
41.8k
    if (ctx == NULL) {
332
0
        X509V3_set_ctx(&tmpctx, NULL, NULL, NULL, NULL, 0);
333
0
        X509V3_set_nconf(&tmpctx, conf);
334
0
        ctx = &tmpctx;
335
0
    }
336
337
41.8k
    if ((nval = NCONF_get_section(conf, section)) == NULL)
338
0
        return 0;
339
110k
    for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
340
69.1k
        val = sk_CONF_VALUE_value(nval, i);
341
69.1k
        if (strcmp(val->name, "authorityKeyIdentifier") == 0)
342
5.33k
            akid = i;
343
63.8k
        else if (strcmp(val->name, "subjectKeyIdentifier") == 0)
344
882
            skid = i;
345
69.1k
    }
346
61.2k
    for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
347
52.7k
        val = sk_CONF_VALUE_value(nval, i);
348
52.7k
        if (skid > akid && akid >= 0) {
349
            /* make sure SKID is handled before AKID */
350
511
            if (i == akid)
351
175
                val = sk_CONF_VALUE_value(nval, skid);
352
336
            else if (i == skid)
353
130
                val = sk_CONF_VALUE_value(nval, akid);
354
511
        }
355
52.7k
        if ((ext = X509V3_EXT_nconf_int(conf, ctx, val->section,
356
52.7k
                 val->name, val->value))
357
52.7k
            == NULL)
358
33.2k
            return 0;
359
19.4k
        if (sk != NULL) {
360
19.4k
            if (ctx->flags == X509V3_CTX_REPLACE)
361
0
                delete_ext(*sk, ext);
362
19.4k
            if (X509v3_add_ext(sk, ext, -1) == NULL) {
363
23
                X509_EXTENSION_free(ext);
364
23
                return 0;
365
23
            }
366
19.4k
        }
367
19.4k
        X509_EXTENSION_free(ext);
368
19.4k
    }
369
8.51k
    if (sk != NULL && sk_X509_EXTENSION_num(*sk) == 0) {
370
22
        sk_X509_EXTENSION_free(*sk);
371
22
        *sk = NULL;
372
22
    }
373
8.51k
    return 1;
374
41.8k
}
375
376
/*
377
 * Add extensions to a certificate. Just check in case cert == NULL.
378
 * Note that on error new elements may remain added to cert if cert != NULL.
379
 */
380
int X509V3_EXT_add_nconf(CONF *conf, X509V3_CTX *ctx, const char *section,
381
    X509 *cert)
382
41.8k
{
383
41.8k
    STACK_OF(X509_EXTENSION) **sk = NULL;
384
385
41.8k
    if (cert != NULL)
386
41.8k
        sk = &cert->cert_info.extensions;
387
41.8k
    return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
388
41.8k
}
389
390
/*
391
 * Add extensions to a CRL. Just check in case crl == NULL.
392
 * Note that on error new elements may remain added to crl if crl != NULL.
393
 */
394
int X509V3_EXT_CRL_add_nconf(CONF *conf, X509V3_CTX *ctx, const char *section,
395
    X509_CRL *crl)
396
0
{
397
0
    STACK_OF(X509_EXTENSION) **sk = NULL;
398
0
    if (crl != NULL)
399
0
        sk = &crl->crl.extensions;
400
0
    return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
401
0
}
402
403
static int
404
update_req_extensions(X509_REQ *req, int *pnid, STACK_OF(X509_EXTENSION) *exts)
405
0
{
406
0
    unsigned char *ext = NULL;
407
0
    int ret = 0, loc = -1, extlen = 0;
408
409
0
    if (pnid == NULL || *pnid == NID_undef)
410
0
        if ((pnid = X509_REQ_get_extension_nids()) == NULL)
411
0
            return 0;
412
0
    loc = X509at_get_attr_by_NID(req->req_info.attributes, *pnid, -1);
413
414
0
    if (exts != NULL) {
415
0
        extlen = ASN1_item_i2d((const ASN1_VALUE *)exts,
416
0
            &ext, ASN1_ITEM_rptr(X509_EXTENSIONS));
417
0
        if (extlen <= 0)
418
0
            return ret;
419
0
    }
420
421
0
    if (loc != -1) {
422
0
        X509_ATTRIBUTE *att = X509_REQ_delete_attr(req, loc);
423
424
0
        if (att == NULL)
425
0
            goto end;
426
0
        X509_ATTRIBUTE_free(att);
427
0
    }
428
0
    if (sk_X509_EXTENSION_num(exts) > 0)
429
0
        ret = X509_REQ_add1_attr_by_NID(req, *pnid, V_ASN1_SEQUENCE, ext, extlen);
430
0
    else
431
0
        ret = 1;
432
0
end:
433
0
    OPENSSL_free(ext);
434
0
    return ret;
435
0
}
436
437
/*
438
 * Add extensions to certificate request. Just check in case req is NULL.
439
 * Note that on error new elements may remain added to req if req != NULL.
440
 */
441
int X509V3_EXT_REQ_add_nconf(CONF *conf, X509V3_CTX *ctx, const char *section,
442
    X509_REQ *req)
443
0
{
444
0
    STACK_OF(X509_EXTENSION) *exts = NULL;
445
0
    int ret, *pnid = NULL;
446
447
    /*
448
     * Load current extensions if any, so we can replace any duplicates, possibly
449
     * with nothing in the case of empty AKID/SKID.
450
     */
451
0
    if (req != NULL) {
452
0
        for (pnid = X509_REQ_get_extension_nids(); *pnid != NID_undef; pnid++) {
453
0
            exts = ossl_x509_req_get1_extensions_by_nid(req, *pnid);
454
0
            if (sk_X509_EXTENSION_num(exts) > 0)
455
0
                break;
456
0
            sk_X509_EXTENSION_free(exts);
457
0
            exts = NULL;
458
0
        }
459
0
    }
460
461
0
    ret = X509V3_EXT_add_nconf_sk(conf, ctx, section, &exts);
462
    /* Replace original extension list (stack) with updated stack */
463
0
    if (ret && req != NULL)
464
0
        ret = update_req_extensions(req, pnid, exts);
465
0
    sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
466
0
    return ret;
467
0
}
468
469
/* Config database functions */
470
471
char *X509V3_get_string(X509V3_CTX *ctx, const char *name, const char *section)
472
0
{
473
0
    if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_string) {
474
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_OPERATION_NOT_DEFINED);
475
0
        return NULL;
476
0
    }
477
0
    return ctx->db_meth->get_string(ctx->db, name, section);
478
0
}
479
480
STACK_OF(CONF_VALUE) *X509V3_get_section(X509V3_CTX *ctx, const char *section)
481
52.2k
{
482
52.2k
    if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_section) {
483
0
        ERR_raise(ERR_LIB_X509V3, X509V3_R_OPERATION_NOT_DEFINED);
484
0
        return NULL;
485
0
    }
486
52.2k
    return ctx->db_meth->get_section(ctx->db, section);
487
52.2k
}
488
489
void X509V3_string_free(X509V3_CTX *ctx, char *str)
490
0
{
491
0
    if (!str)
492
0
        return;
493
0
    if (ctx->db_meth->free_string)
494
0
        ctx->db_meth->free_string(ctx->db, str);
495
0
}
496
497
void X509V3_section_free(X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *section)
498
53.8k
{
499
53.8k
    if (!section)
500
1.74k
        return;
501
52.0k
    if (ctx->db_meth->free_section)
502
0
        ctx->db_meth->free_section(ctx->db, section);
503
52.0k
}
504
505
static char *nconf_get_string(void *db, const char *section, const char *value)
506
0
{
507
0
    return NCONF_get_string(db, section, value);
508
0
}
509
510
static STACK_OF(CONF_VALUE) *nconf_get_section(void *db, const char *section)
511
52.2k
{
512
52.2k
    return NCONF_get_section(db, section);
513
52.2k
}
514
515
static X509V3_CONF_METHOD nconf_method = {
516
    nconf_get_string,
517
    nconf_get_section,
518
    NULL,
519
    NULL
520
};
521
522
void X509V3_set_nconf(X509V3_CTX *ctx, CONF *conf)
523
41.8k
{
524
41.8k
    if (ctx == NULL)
525
0
        return;
526
41.8k
    ctx->db_meth = &nconf_method;
527
41.8k
    ctx->db = conf;
528
41.8k
}
529
530
void X509V3_set_ctx(X509V3_CTX *ctx, X509 *issuer, X509 *subject, X509_REQ *req,
531
    X509_CRL *crl, int flags)
532
41.8k
{
533
41.8k
    if (ctx == NULL)
534
0
        return;
535
41.8k
    ctx->flags = flags;
536
41.8k
    ctx->issuer_cert = issuer;
537
41.8k
    ctx->subject_cert = subject;
538
41.8k
    ctx->subject_req = req;
539
41.8k
    ctx->crl = crl;
540
41.8k
    ctx->db_meth = NULL;
541
41.8k
    ctx->db = NULL;
542
41.8k
    ctx->issuer_pkey = NULL;
543
41.8k
}
544
545
/* For API backward compatibility, this is separate from X509V3_set_ctx() */
546
int X509V3_set_issuer_pkey(X509V3_CTX *ctx, EVP_PKEY *pkey)
547
0
{
548
0
    if (ctx == NULL) {
549
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_NULL_PARAMETER);
550
0
        return 0;
551
0
    }
552
0
    if (ctx->subject_cert == NULL && pkey != NULL) {
553
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_INVALID_ARGUMENT);
554
0
        return 0;
555
0
    }
556
0
    ctx->issuer_pkey = pkey;
557
0
    return 1;
558
0
}
559
560
/* Old conf compatibility functions */
561
562
X509_EXTENSION *X509V3_EXT_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
563
    const char *name, const char *value)
564
0
{
565
0
    CONF *ctmp;
566
0
    X509_EXTENSION *ret;
567
568
0
    if ((ctmp = NCONF_new(NULL)) == NULL)
569
0
        return NULL;
570
0
    CONF_set_nconf(ctmp, conf);
571
0
    ret = X509V3_EXT_nconf(ctmp, ctx, name, value);
572
0
    CONF_set_nconf(ctmp, NULL);
573
0
    NCONF_free(ctmp);
574
0
    return ret;
575
0
}
576
577
X509_EXTENSION *X509V3_EXT_conf_nid(LHASH_OF(CONF_VALUE) *conf,
578
    X509V3_CTX *ctx, int ext_nid, const char *value)
579
0
{
580
0
    CONF *ctmp;
581
0
    X509_EXTENSION *ret;
582
583
0
    if ((ctmp = NCONF_new(NULL)) == NULL)
584
0
        return NULL;
585
0
    CONF_set_nconf(ctmp, conf);
586
0
    ret = X509V3_EXT_nconf_nid(ctmp, ctx, ext_nid, value);
587
0
    CONF_set_nconf(ctmp, NULL);
588
0
    NCONF_free(ctmp);
589
0
    return ret;
590
0
}
591
592
static char *conf_lhash_get_string(void *db, const char *section, const char *value)
593
0
{
594
0
    return CONF_get_string(db, section, value);
595
0
}
596
597
static STACK_OF(CONF_VALUE) *conf_lhash_get_section(void *db, const char *section)
598
0
{
599
0
    return CONF_get_section(db, section);
600
0
}
601
602
static X509V3_CONF_METHOD conf_lhash_method = {
603
    conf_lhash_get_string,
604
    conf_lhash_get_section,
605
    NULL,
606
    NULL
607
};
608
609
void X509V3_set_conf_lhash(X509V3_CTX *ctx, LHASH_OF(CONF_VALUE) *lhash)
610
0
{
611
0
    if (ctx == NULL) {
612
0
        ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_NULL_PARAMETER);
613
0
        return;
614
0
    }
615
0
    ctx->db_meth = &conf_lhash_method;
616
0
    ctx->db = lhash;
617
0
}
618
619
int X509V3_EXT_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
620
    const char *section, X509 *cert)
621
0
{
622
0
    CONF *ctmp;
623
0
    int ret;
624
625
0
    if ((ctmp = NCONF_new(NULL)) == NULL)
626
0
        return 0;
627
0
    CONF_set_nconf(ctmp, conf);
628
0
    ret = X509V3_EXT_add_nconf(ctmp, ctx, section, cert);
629
0
    CONF_set_nconf(ctmp, NULL);
630
0
    NCONF_free(ctmp);
631
0
    return ret;
632
0
}
633
634
/* Same as above but for a CRL */
635
636
int X509V3_EXT_CRL_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
637
    const char *section, X509_CRL *crl)
638
0
{
639
0
    CONF *ctmp;
640
0
    int ret;
641
642
0
    if ((ctmp = NCONF_new(NULL)) == NULL)
643
0
        return 0;
644
0
    CONF_set_nconf(ctmp, conf);
645
0
    ret = X509V3_EXT_CRL_add_nconf(ctmp, ctx, section, crl);
646
0
    CONF_set_nconf(ctmp, NULL);
647
0
    NCONF_free(ctmp);
648
0
    return ret;
649
0
}
650
651
/* Add extensions to certificate request */
652
653
int X509V3_EXT_REQ_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
654
    const char *section, X509_REQ *req)
655
0
{
656
0
    CONF *ctmp;
657
0
    int ret;
658
659
0
    if ((ctmp = NCONF_new(NULL)) == NULL)
660
0
        return 0;
661
0
    CONF_set_nconf(ctmp, conf);
662
0
    ret = X509V3_EXT_REQ_add_nconf(ctmp, ctx, section, req);
663
    CONF_set_nconf(ctmp, NULL);
664
0
    NCONF_free(ctmp);
665
0
    return ret;
666
0
}