/src/openssl35/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 | 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 | 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 | 17.1k | { |
379 | 17.1k | STACK_OF(X509_EXTENSION) **sk = NULL; |
380 | 17.1k | if (cert != NULL) |
381 | 17.1k | sk = &cert->cert_info.extensions; |
382 | 17.1k | return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk); |
383 | 17.1k | } |
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 | 3.41k | { |
448 | 3.41k | if (!section) |
449 | 297 | return; |
450 | 3.12k | if (ctx->db_meth->free_section) |
451 | 0 | ctx->db_meth->free_section(ctx->db, section); |
452 | 3.12k | } |
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 | 3.23k | { |
461 | 3.23k | return NCONF_get_section(db, section); |
462 | 3.23k | } |
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 | 17.1k | { |
473 | 17.1k | if (ctx == NULL) { |
474 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_NULL_PARAMETER); |
475 | 0 | return; |
476 | 0 | } |
477 | 17.1k | ctx->db_meth = &nconf_method; |
478 | 17.1k | ctx->db = conf; |
479 | 17.1k | } |
480 | | |
481 | | void X509V3_set_ctx(X509V3_CTX *ctx, X509 *issuer, X509 *subj, X509_REQ *req, |
482 | | X509_CRL *crl, int flags) |
483 | 17.1k | { |
484 | 17.1k | if (ctx == NULL) { |
485 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_PASSED_NULL_PARAMETER); |
486 | 0 | return; |
487 | 0 | } |
488 | 17.1k | ctx->flags = flags; |
489 | 17.1k | ctx->issuer_cert = issuer; |
490 | 17.1k | ctx->subject_cert = subj; |
491 | 17.1k | ctx->subject_req = req; |
492 | 17.1k | ctx->crl = crl; |
493 | 17.1k | ctx->db_meth = NULL; |
494 | 17.1k | ctx->db = NULL; |
495 | 17.1k | ctx->issuer_pkey = NULL; |
496 | 17.1k | } |
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 | } |