2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
10 /* extension creation utilities */
14 #include "internal/cryptlib.h"
15 #include <openssl/conf.h>
16 #include <openssl/x509.h>
17 #include "internal/x509_int.h"
18 #include <openssl/x509v3.h>
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,
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,
33 /* CONF *conf: Config file */
34 /* char *name: Name */
35 /* char *value: Value */
36 X509_EXTENSION *X509V3_EXT_nconf(CONF *conf, X509V3_CTX *ctx, const char *name,
42 crit = v3_check_critical(&value);
43 if ((ext_type = v3_check_generic(&value)))
44 return v3_generic_extension(name, value, crit, ext_type, ctx);
45 ret = do_ext_nconf(conf, ctx, OBJ_sn2nid(name), crit, value);
47 X509V3err(X509V3_F_X509V3_EXT_NCONF, X509V3_R_ERROR_IN_EXTENSION);
48 ERR_add_error_data(4, "name=", name, ", value=", value);
53 /* CONF *conf: Config file */
54 /* char *value: Value */
55 X509_EXTENSION *X509V3_EXT_nconf_nid(CONF *conf, X509V3_CTX *ctx, int ext_nid,
60 crit = v3_check_critical(&value);
61 if ((ext_type = v3_check_generic(&value)))
62 return v3_generic_extension(OBJ_nid2sn(ext_nid),
63 value, crit, ext_type, ctx);
64 return do_ext_nconf(conf, ctx, ext_nid, crit, value);
67 /* CONF *conf: Config file */
68 /* char *value: Value */
69 static X509_EXTENSION *do_ext_nconf(CONF *conf, X509V3_CTX *ctx, int ext_nid,
70 int crit, const char *value)
72 const X509V3_EXT_METHOD *method;
74 STACK_OF(CONF_VALUE) *nval;
77 if (ext_nid == NID_undef) {
78 X509V3err(X509V3_F_DO_EXT_NCONF, X509V3_R_UNKNOWN_EXTENSION_NAME);
81 if ((method = X509V3_EXT_get_nid(ext_nid)) == NULL) {
82 X509V3err(X509V3_F_DO_EXT_NCONF, X509V3_R_UNKNOWN_EXTENSION);
85 /* Now get internal extension representation based on type */
88 nval = NCONF_get_section(conf, value + 1);
90 nval = X509V3_parse_list(value);
91 if (nval == NULL || sk_CONF_VALUE_num(nval) <= 0) {
92 X509V3err(X509V3_F_DO_EXT_NCONF,
93 X509V3_R_INVALID_EXTENSION_STRING);
94 ERR_add_error_data(4, "name=", OBJ_nid2sn(ext_nid), ",section=",
97 sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
100 ext_struc = method->v2i(method, ctx, nval);
102 sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
105 } else if (method->s2i) {
106 if ((ext_struc = method->s2i(method, ctx, value)) == NULL)
108 } else if (method->r2i) {
109 if (!ctx->db || !ctx->db_meth) {
110 X509V3err(X509V3_F_DO_EXT_NCONF, X509V3_R_NO_CONFIG_DATABASE);
113 if ((ext_struc = method->r2i(method, ctx, value)) == NULL)
116 X509V3err(X509V3_F_DO_EXT_NCONF,
117 X509V3_R_EXTENSION_SETTING_NOT_SUPPORTED);
118 ERR_add_error_data(2, "name=", OBJ_nid2sn(ext_nid));
122 ext = do_ext_i2d(method, ext_nid, crit, ext_struc);
124 ASN1_item_free(ext_struc, ASN1_ITEM_ptr(method->it));
126 method->ext_free(ext_struc);
131 static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method,
132 int ext_nid, int crit, void *ext_struc)
134 unsigned char *ext_der = NULL;
136 ASN1_OCTET_STRING *ext_oct = NULL;
138 /* Convert internal representation to DER */
142 ASN1_item_i2d(ext_struc, &ext_der, ASN1_ITEM_ptr(method->it));
148 ext_len = method->i2d(ext_struc, NULL);
149 if ((ext_der = OPENSSL_malloc(ext_len)) == NULL)
152 method->i2d(ext_struc, &p);
154 if ((ext_oct = ASN1_OCTET_STRING_new()) == NULL)
156 ext_oct->data = ext_der;
158 ext_oct->length = ext_len;
160 ext = X509_EXTENSION_create_by_NID(NULL, ext_nid, crit, ext_oct);
163 ASN1_OCTET_STRING_free(ext_oct);
168 X509V3err(X509V3_F_DO_EXT_I2D, ERR_R_MALLOC_FAILURE);
169 OPENSSL_free(ext_der);
170 ASN1_OCTET_STRING_free(ext_oct);
175 /* Given an internal structure, nid and critical flag create an extension */
177 X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit, void *ext_struc)
179 const X509V3_EXT_METHOD *method;
181 if ((method = X509V3_EXT_get_nid(ext_nid)) == NULL) {
182 X509V3err(X509V3_F_X509V3_EXT_I2D, X509V3_R_UNKNOWN_EXTENSION);
185 return do_ext_i2d(method, ext_nid, crit, ext_struc);
188 /* Check the extension string for critical flag */
189 static int v3_check_critical(const char **value)
191 const char *p = *value;
192 if ((strlen(p) < 9) || strncmp(p, "critical,", 9))
195 while (isspace((unsigned char)*p))
201 /* Check extension string for generic extension and return the type */
202 static int v3_check_generic(const char **value)
205 const char *p = *value;
206 if ((strlen(p) >= 4) && strncmp(p, "DER:", 4) == 0) {
209 } else if ((strlen(p) >= 5) && strncmp(p, "ASN1:", 5) == 0) {
215 while (isspace((unsigned char)*p))
221 /* Create a generic extension: for now just handle DER type */
222 static X509_EXTENSION *v3_generic_extension(const char *ext, const char *value,
223 int crit, int gen_type,
226 unsigned char *ext_der = NULL;
228 ASN1_OBJECT *obj = NULL;
229 ASN1_OCTET_STRING *oct = NULL;
230 X509_EXTENSION *extension = NULL;
232 if ((obj = OBJ_txt2obj(ext, 0)) == NULL) {
233 X509V3err(X509V3_F_V3_GENERIC_EXTENSION,
234 X509V3_R_EXTENSION_NAME_ERROR);
235 ERR_add_error_data(2, "name=", ext);
240 ext_der = OPENSSL_hexstr2buf(value, &ext_len);
241 else if (gen_type == 2)
242 ext_der = generic_asn1(value, ctx, &ext_len);
244 if (ext_der == NULL) {
245 X509V3err(X509V3_F_V3_GENERIC_EXTENSION,
246 X509V3_R_EXTENSION_VALUE_ERROR);
247 ERR_add_error_data(2, "value=", value);
251 if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
252 X509V3err(X509V3_F_V3_GENERIC_EXTENSION, ERR_R_MALLOC_FAILURE);
257 oct->length = ext_len;
260 extension = X509_EXTENSION_create_by_OBJ(NULL, obj, crit, oct);
263 ASN1_OBJECT_free(obj);
264 ASN1_OCTET_STRING_free(oct);
265 OPENSSL_free(ext_der);
270 static unsigned char *generic_asn1(const char *value, X509V3_CTX *ctx,
274 unsigned char *ext_der = NULL;
275 typ = ASN1_generate_v3(value, ctx);
278 *ext_len = i2d_ASN1_TYPE(typ, &ext_der);
283 static void delete_ext(STACK_OF(X509_EXTENSION) *sk, X509_EXTENSION *dext)
287 obj = X509_EXTENSION_get_object(dext);
288 while ((idx = X509v3_get_ext_by_OBJ(sk, obj, -1)) >= 0) {
289 X509_EXTENSION *tmpext = X509v3_get_ext(sk, idx);
290 X509v3_delete_ext(sk, idx);
291 X509_EXTENSION_free(tmpext);
296 * This is the main function: add a bunch of extensions based on a config
297 * file section to an extension STACK.
300 int X509V3_EXT_add_nconf_sk(CONF *conf, X509V3_CTX *ctx, const char *section,
301 STACK_OF(X509_EXTENSION) **sk)
304 STACK_OF(CONF_VALUE) *nval;
308 if ((nval = NCONF_get_section(conf, section)) == NULL)
310 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
311 val = sk_CONF_VALUE_value(nval, i);
312 if ((ext = X509V3_EXT_nconf(conf, ctx, val->name, val->value)) == NULL)
314 if (ctx->flags == X509V3_CTX_REPLACE)
315 delete_ext(*sk, ext);
317 X509v3_add_ext(sk, ext, -1);
318 X509_EXTENSION_free(ext);
324 * Convenience functions to add extensions to a certificate, CRL and request
327 int X509V3_EXT_add_nconf(CONF *conf, X509V3_CTX *ctx, const char *section,
330 STACK_OF(X509_EXTENSION) **sk = NULL;
332 sk = &cert->cert_info.extensions;
333 return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
336 /* Same as above but for a CRL */
338 int X509V3_EXT_CRL_add_nconf(CONF *conf, X509V3_CTX *ctx, const char *section,
341 STACK_OF(X509_EXTENSION) **sk = NULL;
343 sk = &crl->crl.extensions;
344 return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
347 /* Add extensions to certificate request */
349 int X509V3_EXT_REQ_add_nconf(CONF *conf, X509V3_CTX *ctx, const char *section,
352 STACK_OF(X509_EXTENSION) *extlist = NULL, **sk = NULL;
356 i = X509V3_EXT_add_nconf_sk(conf, ctx, section, sk);
359 i = X509_REQ_add_extensions(req, extlist);
360 sk_X509_EXTENSION_pop_free(extlist, X509_EXTENSION_free);
364 /* Config database functions */
366 char *X509V3_get_string(X509V3_CTX *ctx, const char *name, const char *section)
368 if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_string) {
369 X509V3err(X509V3_F_X509V3_GET_STRING, X509V3_R_OPERATION_NOT_DEFINED);
372 if (ctx->db_meth->get_string)
373 return ctx->db_meth->get_string(ctx->db, name, section);
377 STACK_OF(CONF_VALUE) *X509V3_get_section(X509V3_CTX *ctx, const char *section)
379 if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_section) {
380 X509V3err(X509V3_F_X509V3_GET_SECTION,
381 X509V3_R_OPERATION_NOT_DEFINED);
384 if (ctx->db_meth->get_section)
385 return ctx->db_meth->get_section(ctx->db, section);
389 void X509V3_string_free(X509V3_CTX *ctx, char *str)
393 if (ctx->db_meth->free_string)
394 ctx->db_meth->free_string(ctx->db, str);
397 void X509V3_section_free(X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *section)
401 if (ctx->db_meth->free_section)
402 ctx->db_meth->free_section(ctx->db, section);
405 static char *nconf_get_string(void *db, const char *section, const char *value)
407 return NCONF_get_string(db, section, value);
410 static STACK_OF(CONF_VALUE) *nconf_get_section(void *db, const char *section)
412 return NCONF_get_section(db, section);
415 static X509V3_CONF_METHOD nconf_method = {
422 void X509V3_set_nconf(X509V3_CTX *ctx, CONF *conf)
424 ctx->db_meth = &nconf_method;
428 void X509V3_set_ctx(X509V3_CTX *ctx, X509 *issuer, X509 *subj, X509_REQ *req,
429 X509_CRL *crl, int flags)
431 ctx->issuer_cert = issuer;
432 ctx->subject_cert = subj;
434 ctx->subject_req = req;
438 /* Old conf compatibility functions */
440 X509_EXTENSION *X509V3_EXT_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
441 const char *name, const char *value)
444 CONF_set_nconf(&ctmp, conf);
445 return X509V3_EXT_nconf(&ctmp, ctx, name, value);
448 /* LHASH *conf: Config file */
449 /* char *value: Value */
450 X509_EXTENSION *X509V3_EXT_conf_nid(LHASH_OF(CONF_VALUE) *conf,
451 X509V3_CTX *ctx, int ext_nid, const char *value)
454 CONF_set_nconf(&ctmp, conf);
455 return X509V3_EXT_nconf_nid(&ctmp, ctx, ext_nid, value);
458 static char *conf_lhash_get_string(void *db, const char *section, const char *value)
460 return CONF_get_string(db, section, value);
463 static STACK_OF(CONF_VALUE) *conf_lhash_get_section(void *db, const char *section)
465 return CONF_get_section(db, section);
468 static X509V3_CONF_METHOD conf_lhash_method = {
469 conf_lhash_get_string,
470 conf_lhash_get_section,
475 void X509V3_set_conf_lhash(X509V3_CTX *ctx, LHASH_OF(CONF_VALUE) *lhash)
477 ctx->db_meth = &conf_lhash_method;
481 int X509V3_EXT_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
482 const char *section, X509 *cert)
485 CONF_set_nconf(&ctmp, conf);
486 return X509V3_EXT_add_nconf(&ctmp, ctx, section, cert);
489 /* Same as above but for a CRL */
491 int X509V3_EXT_CRL_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
492 const char *section, X509_CRL *crl)
495 CONF_set_nconf(&ctmp, conf);
496 return X509V3_EXT_CRL_add_nconf(&ctmp, ctx, section, crl);
499 /* Add extensions to certificate request */
501 int X509V3_EXT_REQ_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx,
502 const char *section, X509_REQ *req)
505 CONF_set_nconf(&ctmp, conf);
506 return X509V3_EXT_REQ_add_nconf(&ctmp, ctx, section, req);