/src/openssl41/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 | 24.1k | { |
38 | 24.1k | int crit; |
39 | 24.1k | int ext_type; |
40 | 24.1k | X509_EXTENSION *ret; |
41 | | |
42 | 24.1k | crit = v3_check_critical(&value); |
43 | 24.1k | if ((ext_type = v3_check_generic(&value))) |
44 | 6.72k | return v3_generic_extension(name, value, crit, ext_type, ctx); |
45 | 17.3k | ret = do_ext_nconf(conf, ctx, OBJ_sn2nid(name), crit, value); |
46 | 17.3k | if (!ret) { |
47 | 11.0k | if (section != NULL) |
48 | 11.0k | ERR_raise_data(ERR_LIB_X509V3, X509V3_R_ERROR_IN_EXTENSION, |
49 | 11.0k | "section=%s, name=%s, value=%s", |
50 | 11.0k | 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 | 11.0k | } |
55 | 17.3k | return ret; |
56 | 24.1k | } |
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 | 17.3k | { |
95 | 17.3k | const X509V3_EXT_METHOD *method; |
96 | 17.3k | X509_EXTENSION *ext; |
97 | 17.3k | STACK_OF(CONF_VALUE) *nval; |
98 | 17.3k | void *ext_struc; |
99 | | |
100 | 17.3k | if (ext_nid == NID_undef) { |
101 | 3.47k | ERR_raise(ERR_LIB_X509V3, X509V3_R_UNKNOWN_EXTENSION_NAME); |
102 | 3.47k | return NULL; |
103 | 3.47k | } |
104 | 13.9k | if ((method = X509V3_EXT_get_nid(ext_nid)) == NULL) { |
105 | 29 | ERR_raise(ERR_LIB_X509V3, X509V3_R_UNKNOWN_EXTENSION); |
106 | 29 | return NULL; |
107 | 29 | } |
108 | | /* Now get internal extension representation based on type */ |
109 | 13.8k | if (method->v2i) { |
110 | 11.9k | if (*value == '@') |
111 | 212 | nval = NCONF_get_section(conf, value + 1); |
112 | 11.7k | else |
113 | 11.7k | nval = X509V3_parse_list(value); |
114 | 11.9k | if (nval == NULL || sk_CONF_VALUE_num(nval) <= 0) { |
115 | 105 | ERR_raise_data(ERR_LIB_X509V3, X509V3_R_INVALID_EXTENSION_STRING, |
116 | 105 | "name=%s,section=%s", OBJ_nid2sn(ext_nid), value); |
117 | 105 | if (*value != '@') |
118 | 90 | sk_CONF_VALUE_pop_free(nval, X509V3_conf_free); |
119 | 105 | return NULL; |
120 | 105 | } |
121 | 11.8k | ext_struc = method->v2i(method, ctx, nval); |
122 | 11.8k | if (*value != '@') |
123 | 11.6k | sk_CONF_VALUE_pop_free(nval, X509V3_conf_free); |
124 | 11.8k | if (!ext_struc) |
125 | 6.08k | return NULL; |
126 | 11.8k | } else if (method->s2i) { |
127 | 753 | if ((ext_struc = method->s2i(method, ctx, value)) == NULL) |
128 | 261 | return NULL; |
129 | 1.15k | } else if (method->r2i) { |
130 | 1.15k | 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 | 1.15k | if ((ext_struc = method->r2i(method, ctx, value)) == NULL) |
135 | 906 | return NULL; |
136 | 1.15k | } else { |
137 | 3 | ERR_raise_data(ERR_LIB_X509V3, X509V3_R_EXTENSION_SETTING_NOT_SUPPORTED, |
138 | 3 | "name=%s", OBJ_nid2sn(ext_nid)); |
139 | 3 | return NULL; |
140 | 3 | } |
141 | | |
142 | 6.52k | ext = do_ext_i2d(method, ext_nid, crit, ext_struc); |
143 | 6.52k | if (method->it) |
144 | 6.52k | ASN1_item_free(ext_struc, ASN1_ITEM_ptr(method->it)); |
145 | 0 | else |
146 | 0 | method->ext_free(ext_struc); |
147 | 6.52k | return ext; |
148 | 13.8k | } |
149 | | |
150 | | static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method, |
151 | | int ext_nid, int crit, void *ext_struc) |
152 | 6.52k | { |
153 | 6.52k | unsigned char *ext_der = NULL; |
154 | 6.52k | int ext_len; |
155 | 6.52k | ASN1_OCTET_STRING *ext_oct = NULL; |
156 | 6.52k | X509_EXTENSION *ext; |
157 | | |
158 | | /* Convert internal representation to DER */ |
159 | 6.52k | if (method->it) { |
160 | 6.52k | ext_der = NULL; |
161 | 6.52k | ext_len = ASN1_item_i2d(ext_struc, &ext_der, ASN1_ITEM_ptr(method->it)); |
162 | 6.52k | if (ext_len < 0) { |
163 | 179 | ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB); |
164 | 179 | goto err; |
165 | 179 | } |
166 | 6.52k | } 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 | 6.35k | 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 | 6.35k | ext_oct->data = ext_der; |
184 | 6.35k | ext_der = NULL; |
185 | 6.35k | ext_oct->length = ext_len; |
186 | | |
187 | 6.35k | ext = X509_EXTENSION_create_by_NID(NULL, ext_nid, crit, ext_oct); |
188 | 6.35k | if (!ext) { |
189 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_X509V3_LIB); |
190 | 0 | goto err; |
191 | 0 | } |
192 | 6.35k | ASN1_OCTET_STRING_free(ext_oct); |
193 | | |
194 | 6.35k | return ext; |
195 | | |
196 | 179 | err: |
197 | 179 | OPENSSL_free(ext_der); |
198 | 179 | ASN1_OCTET_STRING_free(ext_oct); |
199 | 179 | return NULL; |
200 | 6.35k | } |
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 | 24.1k | { |
218 | 24.1k | const char *p = *value; |
219 | | |
220 | 24.1k | if (!CHECK_AND_SKIP_PREFIX(p, "critical,")) |
221 | 22.6k | return 0; |
222 | 1.67k | while (ossl_isspace(*p)) |
223 | 209 | p++; |
224 | 1.46k | *value = p; |
225 | 1.46k | return 1; |
226 | 24.1k | } |
227 | | |
228 | | /* Check extension string for generic extension and return the type */ |
229 | | static int v3_check_generic(const char **value) |
230 | 24.1k | { |
231 | 24.1k | int gen_type = 0; |
232 | 24.1k | const char *p = *value; |
233 | | |
234 | 24.1k | if (CHECK_AND_SKIP_PREFIX(p, "DER:")) { |
235 | 571 | gen_type = 1; |
236 | 23.5k | } else if (CHECK_AND_SKIP_PREFIX(p, "ASN1:")) { |
237 | 6.15k | gen_type = 2; |
238 | 6.15k | } else |
239 | 17.3k | return 0; |
240 | | |
241 | 7.15k | while (ossl_isspace(*p)) |
242 | 432 | p++; |
243 | 6.72k | *value = p; |
244 | 6.72k | return gen_type; |
245 | 24.1k | } |
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 | 6.72k | { |
252 | 6.72k | unsigned char *ext_der = NULL; |
253 | 6.72k | long ext_len = 0; |
254 | 6.72k | ASN1_OBJECT *obj = NULL; |
255 | 6.72k | ASN1_OCTET_STRING *oct = NULL; |
256 | 6.72k | X509_EXTENSION *extension = NULL; |
257 | | |
258 | 6.72k | if ((obj = OBJ_txt2obj(ext, 0)) == NULL) { |
259 | 356 | ERR_raise_data(ERR_LIB_X509V3, X509V3_R_EXTENSION_NAME_ERROR, |
260 | 356 | "name=%s", ext); |
261 | 356 | goto err; |
262 | 356 | } |
263 | | |
264 | 6.36k | if (gen_type == 1) |
265 | 517 | ext_der = OPENSSL_hexstr2buf(value, &ext_len); |
266 | 5.85k | else if (gen_type == 2) |
267 | 5.85k | ext_der = generic_asn1(value, ctx, &ext_len); |
268 | | |
269 | 6.36k | if (ext_der == NULL) { |
270 | 2.68k | ERR_raise_data(ERR_LIB_X509V3, X509V3_R_EXTENSION_VALUE_ERROR, |
271 | 2.68k | "value=%s", value); |
272 | 2.68k | goto err; |
273 | 2.68k | } |
274 | | |
275 | 3.68k | 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 | 3.68k | oct->data = ext_der; |
281 | 3.68k | oct->length = ext_len; |
282 | 3.68k | ext_der = NULL; |
283 | | |
284 | 3.68k | extension = X509_EXTENSION_create_by_OBJ(NULL, obj, crit, oct); |
285 | | |
286 | 6.72k | err: |
287 | 6.72k | ASN1_OBJECT_free(obj); |
288 | 6.72k | ASN1_OCTET_STRING_free(oct); |
289 | 6.72k | OPENSSL_free(ext_der); |
290 | 6.72k | return extension; |
291 | 3.68k | } |
292 | | |
293 | | static unsigned char *generic_asn1(const char *value, X509V3_CTX *ctx, |
294 | | long *ext_len) |
295 | 5.85k | { |
296 | 5.85k | ASN1_TYPE *typ; |
297 | 5.85k | unsigned char *ext_der = NULL; |
298 | | |
299 | 5.85k | typ = ASN1_generate_v3(value, ctx); |
300 | 5.85k | if (typ == NULL) |
301 | 2.54k | return NULL; |
302 | 3.30k | *ext_len = i2d_ASN1_TYPE(typ, &ext_der); |
303 | 3.30k | ASN1_TYPE_free(typ); |
304 | 3.30k | return ext_der; |
305 | 5.85k | } |
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 | 17.1k | { |
325 | 17.1k | X509_EXTENSION *ext; |
326 | 17.1k | STACK_OF(CONF_VALUE) *nval; |
327 | 17.1k | const CONF_VALUE *val; |
328 | 17.1k | int i, akid = -1, skid = -1; |
329 | 17.1k | X509V3_CTX tmpctx; |
330 | | |
331 | 17.1k | 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 | 17.1k | if ((nval = NCONF_get_section(conf, section)) == NULL) |
338 | 0 | return 0; |
339 | 50.0k | for (i = 0; i < sk_CONF_VALUE_num(nval); i++) { |
340 | 32.8k | val = sk_CONF_VALUE_value(nval, i); |
341 | 32.8k | if (strcmp(val->name, "authorityKeyIdentifier") == 0) |
342 | 3.73k | akid = i; |
343 | 29.1k | else if (strcmp(val->name, "subjectKeyIdentifier") == 0) |
344 | 272 | skid = i; |
345 | 32.8k | } |
346 | 27.2k | for (i = 0; i < sk_CONF_VALUE_num(nval); i++) { |
347 | 24.1k | val = sk_CONF_VALUE_value(nval, i); |
348 | 24.1k | if (skid > akid && akid >= 0) { |
349 | | /* make sure SKID is handled before AKID */ |
350 | 130 | if (i == akid) |
351 | 37 | val = sk_CONF_VALUE_value(nval, skid); |
352 | 93 | else if (i == skid) |
353 | 12 | val = sk_CONF_VALUE_value(nval, akid); |
354 | 130 | } |
355 | 24.1k | if ((ext = X509V3_EXT_nconf_int(conf, ctx, val->section, |
356 | 24.1k | val->name, val->value)) |
357 | 24.1k | == NULL) |
358 | 14.0k | return 0; |
359 | 10.0k | if (sk != NULL) { |
360 | 10.0k | if (ctx->flags == X509V3_CTX_REPLACE) |
361 | 0 | delete_ext(*sk, ext); |
362 | 10.0k | if (X509v3_add_ext(sk, ext, -1) == NULL) { |
363 | 13 | X509_EXTENSION_free(ext); |
364 | 13 | return 0; |
365 | 13 | } |
366 | 10.0k | } |
367 | 10.0k | X509_EXTENSION_free(ext); |
368 | 10.0k | } |
369 | 3.09k | if (sk != NULL && sk_X509_EXTENSION_num(*sk) == 0) { |
370 | 10 | sk_X509_EXTENSION_free(*sk); |
371 | 10 | *sk = NULL; |
372 | 10 | } |
373 | 3.09k | return 1; |
374 | 17.1k | } |
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 | 17.1k | { |
383 | 17.1k | STACK_OF(X509_EXTENSION) **sk = NULL; |
384 | | |
385 | 17.1k | if (cert != NULL) |
386 | 17.1k | sk = &cert->cert_info.extensions; |
387 | 17.1k | return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk); |
388 | 17.1k | } |
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 | 3.23k | { |
482 | 3.23k | 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 | 3.23k | return ctx->db_meth->get_section(ctx->db, section); |
487 | 3.23k | } |
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 | 3.41k | { |
499 | 3.41k | if (!section) |
500 | 297 | return; |
501 | 3.12k | if (ctx->db_meth->free_section) |
502 | 0 | ctx->db_meth->free_section(ctx->db, section); |
503 | 3.12k | } |
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 | 3.23k | { |
512 | 3.23k | return NCONF_get_section(db, section); |
513 | 3.23k | } |
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 | 17.1k | { |
524 | 17.1k | if (ctx == NULL) |
525 | 0 | return; |
526 | 17.1k | ctx->db_meth = &nconf_method; |
527 | 17.1k | ctx->db = conf; |
528 | 17.1k | } |
529 | | |
530 | | void X509V3_set_ctx(X509V3_CTX *ctx, X509 *issuer, X509 *subject, X509_REQ *req, |
531 | | X509_CRL *crl, int flags) |
532 | 17.1k | { |
533 | 17.1k | if (ctx == NULL) |
534 | 0 | return; |
535 | 17.1k | ctx->flags = flags; |
536 | 17.1k | ctx->issuer_cert = issuer; |
537 | 17.1k | ctx->subject_cert = subject; |
538 | 17.1k | ctx->subject_req = req; |
539 | 17.1k | ctx->crl = crl; |
540 | 17.1k | ctx->db_meth = NULL; |
541 | 17.1k | ctx->db = NULL; |
542 | 17.1k | ctx->issuer_pkey = NULL; |
543 | 17.1k | } |
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 | } |