| 1 |
// Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project 1999. |
| 2 |
// Copyright (c) 1999-2002 The OpenSSL Project. All rights reserved. |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
// extension creation utilities |
| 6 |
|
| 7 |
#include <ctype.h> |
| 8 |
#include <limits.h> |
| 9 |
#include <stdio.h> |
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#include <openssl/conf.h> |
| 13 |
#include <openssl/err.h> |
| 14 |
#include <openssl/mem.h> |
| 15 |
#include <openssl/obj.h> |
| 16 |
#include <openssl/x509.h> |
| 17 |
|
| 18 |
#include "../internal.h" |
| 19 |
#include "internal.h" |
| 20 |
|
| 21 |
static int v3_check_critical(const char **value); |
| 22 |
static int v3_check_generic(const char **value); |
| 23 |
static X509_EXTENSION *do_ext_nconf(const CONF *conf, const X509V3_CTX *ctx, |
| 24 |
int ext_nid, int crit, const char *value); |
| 25 |
static X509_EXTENSION *v3_generic_extension(const char *ext, const char *value, |
| 26 |
int crit, int type, |
| 27 |
const X509V3_CTX *ctx); |
| 28 |
static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method, int ext_nid, |
| 29 |
int crit, void *ext_struc); |
| 30 |
static unsigned char *generic_asn1(const char *value, const X509V3_CTX *ctx, |
| 31 |
size_t *ext_len); |
| 32 |
|
| 33 |
static void delete_ext(STACK_OF(X509_EXTENSION) *sk, X509_EXTENSION *dext); |
| 34 |
|
| 35 |
X509_EXTENSION *X509V3_EXT_nconf(const CONF *conf, const X509V3_CTX *ctx, |
| 36 |
const char *name, const char *value) { |
| 37 |
// If omitted, fill in an empty |X509V3_CTX|. |
| 38 |
X509V3_CTX ctx_tmp; |
| 39 |
if (ctx == NULL) { |
| 40 |
X509V3_set_ctx(&ctx_tmp, NULL, NULL, NULL, NULL, 0); |
| 41 |
X509V3_set_nconf(&ctx_tmp, conf); |
| 42 |
ctx = &ctx_tmp; |
| 43 |
} |
| 44 |
|
| 45 |
int crit = v3_check_critical(&value); |
| 46 |
int ext_type = v3_check_generic(&value); |
| 47 |
if (ext_type != 0) { |
| 48 |
return v3_generic_extension(name, value, crit, ext_type, ctx); |
| 49 |
} |
| 50 |
X509_EXTENSION *ret = do_ext_nconf(conf, ctx, OBJ_sn2nid(name), crit, value); |
| 51 |
if (!ret) { |
| 52 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_ERROR_IN_EXTENSION); |
| 53 |
ERR_add_error_data(4, "name=", name, ", value=", value); |
| 54 |
} |
| 55 |
return ret; |
| 56 |
} |
| 57 |
|
| 58 |
X509_EXTENSION *X509V3_EXT_nconf_nid(const CONF *conf, const X509V3_CTX *ctx, |
| 59 |
int ext_nid, const char *value) { |
| 60 |
// If omitted, fill in an empty |X509V3_CTX|. |
| 61 |
X509V3_CTX ctx_tmp; |
| 62 |
if (ctx == NULL) { |
| 63 |
X509V3_set_ctx(&ctx_tmp, NULL, NULL, NULL, NULL, 0); |
| 64 |
X509V3_set_nconf(&ctx_tmp, conf); |
| 65 |
ctx = &ctx_tmp; |
| 66 |
} |
| 67 |
|
| 68 |
int crit = v3_check_critical(&value); |
| 69 |
int ext_type = v3_check_generic(&value); |
| 70 |
if (ext_type != 0) { |
| 71 |
return v3_generic_extension(OBJ_nid2sn(ext_nid), value, crit, ext_type, |
| 72 |
ctx); |
| 73 |
} |
| 74 |
return do_ext_nconf(conf, ctx, ext_nid, crit, value); |
| 75 |
} |
| 76 |
|
| 77 |
// CONF *conf: Config file |
| 78 |
// char *value: Value |
| 79 |
static X509_EXTENSION *do_ext_nconf(const CONF *conf, const X509V3_CTX *ctx, |
| 80 |
int ext_nid, int crit, const char *value) { |
| 81 |
const X509V3_EXT_METHOD *method; |
| 82 |
X509_EXTENSION *ext; |
| 83 |
const STACK_OF(CONF_VALUE) *nval; |
| 84 |
STACK_OF(CONF_VALUE) *nval_owned = NULL; |
| 85 |
void *ext_struc; |
| 86 |
if (ext_nid == NID_undef) { |
| 87 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNKNOWN_EXTENSION_NAME); |
| 88 |
return NULL; |
| 89 |
} |
| 90 |
if (!(method = X509V3_EXT_get_nid(ext_nid))) { |
| 91 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNKNOWN_EXTENSION); |
| 92 |
return NULL; |
| 93 |
} |
| 94 |
// Now get internal extension representation based on type |
| 95 |
if (method->v2i) { |
| 96 |
if (*value == '@') { |
| 97 |
// TODO(davidben): This is the only place where |X509V3_EXT_nconf|'s |
| 98 |
// |conf| parameter is used. All other codepaths use the copy inside |
| 99 |
// |ctx|. Should this be switched and then the parameter ignored? |
| 100 |
if (conf == NULL) { |
| 101 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_CONFIG_DATABASE); |
| 102 |
return NULL; |
| 103 |
} |
| 104 |
nval = NCONF_get_section(conf, value + 1); |
| 105 |
} else { |
| 106 |
nval_owned = X509V3_parse_list(value); |
| 107 |
nval = nval_owned; |
| 108 |
} |
| 109 |
if (nval == NULL || sk_CONF_VALUE_num(nval) <= 0) { |
| 110 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_EXTENSION_STRING); |
| 111 |
ERR_add_error_data(4, "name=", OBJ_nid2sn(ext_nid), ",section=", value); |
| 112 |
sk_CONF_VALUE_pop_free(nval_owned, X509V3_conf_free); |
| 113 |
return NULL; |
| 114 |
} |
| 115 |
ext_struc = method->v2i(method, ctx, nval); |
| 116 |
sk_CONF_VALUE_pop_free(nval_owned, X509V3_conf_free); |
| 117 |
if (!ext_struc) { |
| 118 |
return NULL; |
| 119 |
} |
| 120 |
} else if (method->s2i) { |
| 121 |
if (!(ext_struc = method->s2i(method, ctx, value))) { |
| 122 |
return NULL; |
| 123 |
} |
| 124 |
} else if (method->r2i) { |
| 125 |
// TODO(davidben): Should this check be removed? This matches OpenSSL, but |
| 126 |
// r2i-based extensions do not necessarily require a config database. The |
| 127 |
// two built-in extensions only use it some of the time, and already handle |
| 128 |
// |X509V3_get_section| returning NULL. |
| 129 |
if (!ctx->db) { |
| 130 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_CONFIG_DATABASE); |
| 131 |
return NULL; |
| 132 |
} |
| 133 |
if (!(ext_struc = method->r2i(method, ctx, value))) { |
| 134 |
return NULL; |
| 135 |
} |
| 136 |
} else { |
| 137 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXTENSION_SETTING_NOT_SUPPORTED); |
| 138 |
ERR_add_error_data(2, "name=", OBJ_nid2sn(ext_nid)); |
| 139 |
return NULL; |
| 140 |
} |
| 141 |
|
| 142 |
ext = do_ext_i2d(method, ext_nid, crit, ext_struc); |
| 143 |
ASN1_item_free(ext_struc, ASN1_ITEM_ptr(method->it)); |
| 144 |
return ext; |
| 145 |
} |
| 146 |
|
| 147 |
static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method, int ext_nid, |
| 148 |
int crit, void *ext_struc) { |
| 149 |
// Convert the extension's internal representation to DER. |
| 150 |
unsigned char *ext_der; |
| 151 |
int ext_len; |
| 152 |
if (method->it) { |
| 153 |
ext_der = NULL; |
| 154 |
ext_len = ASN1_item_i2d(ext_struc, &ext_der, ASN1_ITEM_ptr(method->it)); |
| 155 |
if (ext_len < 0) { |
| 156 |
return NULL; |
| 157 |
} |
| 158 |
} else if (method->ext_nid == NID_id_pkix_OCSP_Nonce && method->i2d != NULL) { |
| 159 |
// |NID_id_pkix_OCSP_Nonce| is the only extension using the "old-style" |
| 160 |
// ASN.1 callbacks for backwards compatibility reasons. |
| 161 |
// Note: See |v3_ext_method| under "include/openssl/x509.h". |
| 162 |
ext_len = method->i2d(ext_struc, NULL); |
| 163 |
if (!(ext_der = OPENSSL_malloc(ext_len))) { |
| 164 |
return NULL; |
| 165 |
} |
| 166 |
unsigned char *p = ext_der; |
| 167 |
method->i2d(ext_struc, &p); |
| 168 |
} else { |
| 169 |
OPENSSL_PUT_ERROR(X509, X509V3_R_OPERATION_NOT_DEFINED); |
| 170 |
return NULL; |
| 171 |
} |
| 172 |
|
| 173 |
ASN1_OCTET_STRING *ext_oct = ASN1_OCTET_STRING_new(); |
| 174 |
if (ext_oct == NULL) { |
| 175 |
OPENSSL_free(ext_der); |
| 176 |
return NULL; |
| 177 |
} |
| 178 |
ASN1_STRING_set0(ext_oct, ext_der, ext_len); |
| 179 |
|
| 180 |
X509_EXTENSION *ext = |
| 181 |
X509_EXTENSION_create_by_NID(NULL, ext_nid, crit, ext_oct); |
| 182 |
ASN1_OCTET_STRING_free(ext_oct); |
| 183 |
return ext; |
| 184 |
} |
| 185 |
|
| 186 |
// Given an internal structure, nid and critical flag create an extension |
| 187 |
|
| 188 |
X509_EXTENSION *X509V3_EXT_i2d(int ext_nid, int crit, void *ext_struc) { |
| 189 |
const X509V3_EXT_METHOD *method; |
| 190 |
if (!(method = X509V3_EXT_get_nid(ext_nid))) { |
| 191 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNKNOWN_EXTENSION); |
| 192 |
return NULL; |
| 193 |
} |
| 194 |
return do_ext_i2d(method, ext_nid, crit, ext_struc); |
| 195 |
} |
| 196 |
|
| 197 |
// Check the extension string for critical flag |
| 198 |
static int v3_check_critical(const char **value) { |
| 199 |
const char *p = *value; |
| 200 |
if ((strlen(p) < 9) || strncmp(p, "critical,", 9)) { |
| 201 |
return 0; |
| 202 |
} |
| 203 |
p += 9; |
| 204 |
while (OPENSSL_isspace((unsigned char)*p)) { |
| 205 |
p++; |
| 206 |
} |
| 207 |
*value = p; |
| 208 |
return 1; |
| 209 |
} |
| 210 |
|
| 211 |
// Check extension string for generic extension and return the type |
| 212 |
static int v3_check_generic(const char **value) { |
| 213 |
int gen_type = 0; |
| 214 |
const char *p = *value; |
| 215 |
if ((strlen(p) >= 4) && !strncmp(p, "DER:", 4)) { |
| 216 |
p += 4; |
| 217 |
gen_type = 1; |
| 218 |
} else if ((strlen(p) >= 5) && !strncmp(p, "ASN1:", 5)) { |
| 219 |
p += 5; |
| 220 |
gen_type = 2; |
| 221 |
} else { |
| 222 |
return 0; |
| 223 |
} |
| 224 |
|
| 225 |
while (OPENSSL_isspace((unsigned char)*p)) { |
| 226 |
p++; |
| 227 |
} |
| 228 |
*value = p; |
| 229 |
return gen_type; |
| 230 |
} |
| 231 |
|
| 232 |
// Create a generic extension: for now just handle DER type |
| 233 |
static X509_EXTENSION *v3_generic_extension(const char *ext, const char *value, |
| 234 |
int crit, int gen_type, |
| 235 |
const X509V3_CTX *ctx) { |
| 236 |
unsigned char *ext_der = NULL; |
| 237 |
size_t ext_len = 0; |
| 238 |
ASN1_OBJECT *obj = NULL; |
| 239 |
ASN1_OCTET_STRING *oct = NULL; |
| 240 |
X509_EXTENSION *extension = NULL; |
| 241 |
if (!(obj = OBJ_txt2obj(ext, 0))) { |
| 242 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXTENSION_NAME_ERROR); |
| 243 |
ERR_add_error_data(2, "name=", ext); |
| 244 |
goto err; |
| 245 |
} |
| 246 |
|
| 247 |
if (gen_type == 1) { |
| 248 |
ext_der = x509v3_hex_to_bytes(value, &ext_len); |
| 249 |
} else if (gen_type == 2) { |
| 250 |
ext_der = generic_asn1(value, ctx, &ext_len); |
| 251 |
} |
| 252 |
|
| 253 |
if (ext_der == NULL) { |
| 254 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXTENSION_VALUE_ERROR); |
| 255 |
ERR_add_error_data(2, "value=", value); |
| 256 |
goto err; |
| 257 |
} |
| 258 |
|
| 259 |
if (ext_len > INT_MAX) { |
| 260 |
OPENSSL_PUT_ERROR(X509V3, ERR_R_OVERFLOW); |
| 261 |
goto err; |
| 262 |
} |
| 263 |
|
| 264 |
oct = ASN1_OCTET_STRING_new(); |
| 265 |
if (oct == NULL) { |
| 266 |
goto err; |
| 267 |
} |
| 268 |
|
| 269 |
ASN1_STRING_set0(oct, ext_der, (int)ext_len); |
| 270 |
ext_der = NULL; |
| 271 |
|
| 272 |
extension = X509_EXTENSION_create_by_OBJ(NULL, obj, crit, oct); |
| 273 |
|
| 274 |
err: |
| 275 |
ASN1_OBJECT_free(obj); |
| 276 |
ASN1_OCTET_STRING_free(oct); |
| 277 |
OPENSSL_free(ext_der); |
| 278 |
return extension; |
| 279 |
} |
| 280 |
|
| 281 |
static unsigned char *generic_asn1(const char *value, const X509V3_CTX *ctx, |
| 282 |
size_t *ext_len) { |
| 283 |
ASN1_TYPE *typ = ASN1_generate_v3(value, ctx); |
| 284 |
if (typ == NULL) { |
| 285 |
return NULL; |
| 286 |
} |
| 287 |
unsigned char *ext_der = NULL; |
| 288 |
int len = i2d_ASN1_TYPE(typ, &ext_der); |
| 289 |
ASN1_TYPE_free(typ); |
| 290 |
if (len < 0) { |
| 291 |
return NULL; |
| 292 |
} |
| 293 |
*ext_len = len; |
| 294 |
return ext_der; |
| 295 |
} |
| 296 |
|
| 297 |
static void delete_ext(STACK_OF(X509_EXTENSION) *sk, X509_EXTENSION *dext) { |
| 298 |
int idx = 0; |
| 299 |
ASN1_OBJECT *obj = NULL; |
| 300 |
|
| 301 |
obj = X509_EXTENSION_get_object(dext); |
| 302 |
while ((idx = X509v3_get_ext_by_OBJ(sk, obj, -1)) >= 0) { |
| 303 |
X509_EXTENSION_free(X509v3_delete_ext(sk, idx)); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
// This is the main function: add a bunch of extensions based on a config |
| 308 |
// file section to an extension STACK. |
| 309 |
|
| 310 |
int X509V3_EXT_add_nconf_sk(const CONF *conf, const X509V3_CTX *ctx, |
| 311 |
const char *section, |
| 312 |
STACK_OF(X509_EXTENSION) **sk) { |
| 313 |
const STACK_OF(CONF_VALUE) *nval = NCONF_get_section(conf, section); |
| 314 |
if (nval == NULL) { |
| 315 |
return 0; |
| 316 |
} |
| 317 |
for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) { |
| 318 |
const CONF_VALUE *val = sk_CONF_VALUE_value(nval, i); |
| 319 |
X509_EXTENSION *ext = X509V3_EXT_nconf(conf, ctx, val->name, val->value); |
| 320 |
|
| 321 |
int ok = 0; |
| 322 |
if (ext) { |
| 323 |
if (!sk) { |
| 324 |
ok = 1; |
| 325 |
} else { |
| 326 |
if (ctx && ctx->flags == X509V3_CTX_REPLACE) { |
| 327 |
delete_ext(*sk, ext); |
| 328 |
} |
| 329 |
ok = X509v3_add_ext(sk, ext, -1) != NULL; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
X509_EXTENSION_free(ext); |
| 334 |
if (!ok) { |
| 335 |
return 0; |
| 336 |
} |
| 337 |
} |
| 338 |
return 1; |
| 339 |
} |
| 340 |
|
| 341 |
// Convenience functions to add extensions to a certificate, CRL and request |
| 342 |
|
| 343 |
int X509V3_EXT_add_nconf(const CONF *conf, const X509V3_CTX *ctx, |
| 344 |
const char *section, X509 *cert) { |
| 345 |
STACK_OF(X509_EXTENSION) **sk = NULL; |
| 346 |
if (cert) { |
| 347 |
sk = &cert->cert_info->extensions; |
| 348 |
} |
| 349 |
return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk); |
| 350 |
} |
| 351 |
|
| 352 |
// Same as above but for a CRL |
| 353 |
|
| 354 |
int X509V3_EXT_CRL_add_nconf(const CONF *conf, const X509V3_CTX *ctx, |
| 355 |
const char *section, X509_CRL *crl) { |
| 356 |
STACK_OF(X509_EXTENSION) **sk = NULL; |
| 357 |
if (crl) { |
| 358 |
sk = &crl->crl->extensions; |
| 359 |
} |
| 360 |
return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk); |
| 361 |
} |
| 362 |
|
| 363 |
// Add extensions to certificate request |
| 364 |
|
| 365 |
int X509V3_EXT_REQ_add_nconf(const CONF *conf, const X509V3_CTX *ctx, |
| 366 |
const char *section, X509_REQ *req) { |
| 367 |
STACK_OF(X509_EXTENSION) *extlist = NULL, **sk = NULL; |
| 368 |
int i; |
| 369 |
if (req) { |
| 370 |
sk = &extlist; |
| 371 |
} |
| 372 |
i = X509V3_EXT_add_nconf_sk(conf, ctx, section, sk); |
| 373 |
if (!i || !sk) { |
| 374 |
return i; |
| 375 |
} |
| 376 |
i = X509_REQ_add_extensions(req, extlist); |
| 377 |
sk_X509_EXTENSION_pop_free(extlist, X509_EXTENSION_free); |
| 378 |
return i; |
| 379 |
} |
| 380 |
|
| 381 |
// Config database functions |
| 382 |
|
| 383 |
const STACK_OF(CONF_VALUE) *X509V3_get_section(const X509V3_CTX *ctx, |
| 384 |
const char *section) { |
| 385 |
if (ctx->db == NULL) { |
| 386 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_OPERATION_NOT_DEFINED); |
| 387 |
return NULL; |
| 388 |
} |
| 389 |
return NCONF_get_section(ctx->db, section); |
| 390 |
} |
| 391 |
|
| 392 |
void X509V3_set_nconf(X509V3_CTX *ctx, const CONF *conf) { ctx->db = conf; } |
| 393 |
|
| 394 |
void X509V3_set_ctx(X509V3_CTX *ctx, const X509 *issuer, const X509 *subj, |
| 395 |
const X509_REQ *req, const X509_CRL *crl, int flags) { |
| 396 |
OPENSSL_memset(ctx, 0, sizeof(*ctx)); |
| 397 |
ctx->issuer_cert = issuer; |
| 398 |
ctx->subject_cert = subj; |
| 399 |
ctx->crl = crl; |
| 400 |
ctx->subject_req = req; |
| 401 |
ctx->flags = flags; |
| 402 |
} |