| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/x509.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <ctype.h> |
| 8 |
#include <limits.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#include <openssl/asn1.h> |
| 12 |
#include <openssl/bytestring.h> |
| 13 |
#include <openssl/err.h> |
| 14 |
#include <openssl/obj.h> |
| 15 |
|
| 16 |
#include "../conf/internal.h" |
| 17 |
#include "../internal.h" |
| 18 |
#include "internal.h" |
| 19 |
|
| 20 |
|
| 21 |
// Although this file is in crypto/x509 for layering purposes, it emits |
| 22 |
// errors from the ASN.1 module for OpenSSL compatibility. |
| 23 |
|
| 24 |
// ASN1_GEN_MAX_DEPTH is the maximum number of nested TLVs allowed. |
| 25 |
#define ASN1_GEN_MAX_DEPTH 50 |
| 26 |
|
| 27 |
// ASN1_GEN_MAX_OUTPUT is the maximum output, in bytes, allowed. This limit is |
| 28 |
// necessary because the SEQUENCE and SET section reference mechanism allows the |
| 29 |
// output length to grow super-linearly with the input length. |
| 30 |
#define ASN1_GEN_MAX_OUTPUT (64 * 1024) |
| 31 |
|
| 32 |
// ASN1_GEN_FORMAT_* are the values for the format modifiers. |
| 33 |
#define ASN1_GEN_FORMAT_ASCII 1 |
| 34 |
#define ASN1_GEN_FORMAT_UTF8 2 |
| 35 |
#define ASN1_GEN_FORMAT_HEX 3 |
| 36 |
#define ASN1_GEN_FORMAT_BITLIST 4 |
| 37 |
|
| 38 |
// generate_v3 converts |str| into an ASN.1 structure and writes the result to |
| 39 |
// |cbb|. It returns one on success and zero on error. |depth| bounds recursion, |
| 40 |
// and |format| specifies the current format modifier. |
| 41 |
// |
| 42 |
// If |tag| is non-zero, the structure is implicitly tagged with |tag|. |tag| |
| 43 |
// must not have the constructed bit set. |
| 44 |
static int generate_v3(CBB *cbb, const char *str, const X509V3_CTX *cnf, |
| 45 |
CBS_ASN1_TAG tag, int format, int depth); |
| 46 |
|
| 47 |
static int bitstr_cb(const char *elem, size_t len, void *bitstr); |
| 48 |
|
| 49 |
ASN1_TYPE *ASN1_generate_v3(const char *str, const X509V3_CTX *cnf) { |
| 50 |
CBB cbb; |
| 51 |
if (!CBB_init(&cbb, 0) || // |
| 52 |
!generate_v3(&cbb, str, cnf, /*tag=*/0, ASN1_GEN_FORMAT_ASCII, |
| 53 |
/*depth=*/0)) { |
| 54 |
CBB_cleanup(&cbb); |
| 55 |
return NULL; |
| 56 |
} |
| 57 |
|
| 58 |
// While not strictly necessary to avoid a DoS (we rely on any super-linear |
| 59 |
// checks being performed internally), cap the overall output to |
| 60 |
// |ASN1_GEN_MAX_OUTPUT| so the externally-visible behavior is consistent. |
| 61 |
if (CBB_len(&cbb) > ASN1_GEN_MAX_OUTPUT) { |
| 62 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG); |
| 63 |
CBB_cleanup(&cbb); |
| 64 |
return NULL; |
| 65 |
} |
| 66 |
|
| 67 |
const uint8_t *der = CBB_data(&cbb); |
| 68 |
ASN1_TYPE *ret = d2i_ASN1_TYPE(NULL, &der, CBB_len(&cbb)); |
| 69 |
CBB_cleanup(&cbb); |
| 70 |
return ret; |
| 71 |
} |
| 72 |
|
| 73 |
static int cbs_str_equal(const CBS *cbs, const char *str) { |
| 74 |
return CBS_len(cbs) == strlen(str) && |
| 75 |
OPENSSL_memcmp(CBS_data(cbs), str, strlen(str)) == 0; |
| 76 |
} |
| 77 |
|
| 78 |
// parse_tag decodes a tag specifier in |cbs|. It returns the tag on success or |
| 79 |
// zero on error. |
| 80 |
static CBS_ASN1_TAG parse_tag(const CBS *cbs) { |
| 81 |
CBS copy = *cbs; |
| 82 |
uint64_t num; |
| 83 |
if (!CBS_get_u64_decimal(©, &num) || |
| 84 |
num > CBS_ASN1_TAG_NUMBER_MASK) { |
| 85 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_NUMBER); |
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
CBS_ASN1_TAG tag_class = CBS_ASN1_CONTEXT_SPECIFIC; |
| 90 |
// The tag may be suffixed by a class. |
| 91 |
uint8_t c; |
| 92 |
if (CBS_get_u8(©, &c)) { |
| 93 |
switch (c) { |
| 94 |
case 'U': |
| 95 |
tag_class = CBS_ASN1_UNIVERSAL; |
| 96 |
break; |
| 97 |
case 'A': |
| 98 |
tag_class = CBS_ASN1_APPLICATION; |
| 99 |
break; |
| 100 |
case 'P': |
| 101 |
tag_class = CBS_ASN1_PRIVATE; |
| 102 |
break; |
| 103 |
case 'C': |
| 104 |
tag_class = CBS_ASN1_CONTEXT_SPECIFIC; |
| 105 |
break; |
| 106 |
default: { |
| 107 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_MODIFIER); |
| 108 |
return 0; |
| 109 |
} |
| 110 |
} |
| 111 |
if (CBS_len(©) != 0) { |
| 112 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_MODIFIER); |
| 113 |
return 0; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// Tag [UNIVERSAL 0] is reserved for indefinite-length end-of-contents. We |
| 118 |
// also use zero in this file to indicator no explicit tagging. |
| 119 |
if (tag_class == CBS_ASN1_UNIVERSAL && num == 0) { |
| 120 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_NUMBER); |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
return tag_class | (CBS_ASN1_TAG)num; |
| 125 |
} |
| 126 |
|
| 127 |
static int generate_wrapped(CBB *cbb, const char *str, const X509V3_CTX *cnf, |
| 128 |
CBS_ASN1_TAG tag, int padding, int format, |
| 129 |
int depth) { |
| 130 |
CBB child; |
| 131 |
return CBB_add_asn1(cbb, &child, tag) && |
| 132 |
(!padding || CBB_add_u8(&child, 0)) && |
| 133 |
generate_v3(&child, str, cnf, /*tag=*/0, format, depth + 1) && |
| 134 |
CBB_flush(cbb); |
| 135 |
} |
| 136 |
|
| 137 |
static int generate_v3(CBB *cbb, const char *str, const X509V3_CTX *cnf, |
| 138 |
CBS_ASN1_TAG tag, int format, int depth) { |
| 139 |
assert((tag & CBS_ASN1_CONSTRUCTED) == 0); |
| 140 |
if (depth > ASN1_GEN_MAX_DEPTH) { |
| 141 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_NESTED_TAGGING); |
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
// Process modifiers. This function uses a mix of NUL-terminated strings and |
| 146 |
// |CBS|. Several functions only work with NUL-terminated strings, so we need |
| 147 |
// to keep track of when a slice spans the whole buffer. |
| 148 |
for (;;) { |
| 149 |
// Skip whitespace. |
| 150 |
while (*str != '\0' && OPENSSL_isspace((unsigned char)*str)) { |
| 151 |
str++; |
| 152 |
} |
| 153 |
|
| 154 |
// Modifiers end at commas. |
| 155 |
const char *comma = strchr(str, ','); |
| 156 |
if (comma == NULL) { |
| 157 |
break; |
| 158 |
} |
| 159 |
|
| 160 |
// Remove trailing whitespace. |
| 161 |
CBS modifier; |
| 162 |
CBS_init(&modifier, (const uint8_t *)str, comma - str); |
| 163 |
for (;;) { |
| 164 |
uint8_t v; |
| 165 |
CBS copy = modifier; |
| 166 |
if (!CBS_get_last_u8(©, &v) || !OPENSSL_isspace(v)) { |
| 167 |
break; |
| 168 |
} |
| 169 |
modifier = copy; |
| 170 |
} |
| 171 |
|
| 172 |
// Advance the string past the modifier, but save the original value. We |
| 173 |
// will need to rewind if this is not a recognized modifier. |
| 174 |
const char *str_old = str; |
| 175 |
str = comma + 1; |
| 176 |
|
| 177 |
// Each modifier is either NAME:VALUE or NAME. |
| 178 |
CBS name; |
| 179 |
int has_value = CBS_get_until_first(&modifier, &name, ':'); |
| 180 |
if (has_value) { |
| 181 |
CBS_skip(&modifier, 1); // Skip the colon. |
| 182 |
} else { |
| 183 |
name = modifier; |
| 184 |
CBS_init(&modifier, NULL, 0); |
| 185 |
} |
| 186 |
|
| 187 |
if (cbs_str_equal(&name, "FORMAT") || cbs_str_equal(&name, "FORM")) { |
| 188 |
if (cbs_str_equal(&modifier, "ASCII")) { |
| 189 |
format = ASN1_GEN_FORMAT_ASCII; |
| 190 |
} else if (cbs_str_equal(&modifier, "UTF8")) { |
| 191 |
format = ASN1_GEN_FORMAT_UTF8; |
| 192 |
} else if (cbs_str_equal(&modifier, "HEX")) { |
| 193 |
format = ASN1_GEN_FORMAT_HEX; |
| 194 |
} else if (cbs_str_equal(&modifier, "BITLIST")) { |
| 195 |
format = ASN1_GEN_FORMAT_BITLIST; |
| 196 |
} else { |
| 197 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_FORMAT); |
| 198 |
return 0; |
| 199 |
} |
| 200 |
} else if (cbs_str_equal(&name, "IMP") || |
| 201 |
cbs_str_equal(&name, "IMPLICIT")) { |
| 202 |
if (tag != 0) { |
| 203 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_NESTED_TAGGING); |
| 204 |
return 0; |
| 205 |
} |
| 206 |
tag = parse_tag(&modifier); |
| 207 |
if (tag == 0) { |
| 208 |
return 0; |
| 209 |
} |
| 210 |
} else if (cbs_str_equal(&name, "EXP") || |
| 211 |
cbs_str_equal(&name, "EXPLICIT")) { |
| 212 |
// It would actually be supportable, but OpenSSL does not allow wrapping |
| 213 |
// an explicit tag in an implicit tag. |
| 214 |
if (tag != 0) { |
| 215 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_NESTED_TAGGING); |
| 216 |
return 0; |
| 217 |
} |
| 218 |
tag = parse_tag(&modifier); |
| 219 |
return tag != 0 && |
| 220 |
generate_wrapped(cbb, str, cnf, tag | CBS_ASN1_CONSTRUCTED, |
| 221 |
/*padding=*/0, format, depth); |
| 222 |
} else if (cbs_str_equal(&name, "OCTWRAP")) { |
| 223 |
tag = tag == 0 ? CBS_ASN1_OCTETSTRING : tag; |
| 224 |
return generate_wrapped(cbb, str, cnf, tag, /*padding=*/0, format, depth); |
| 225 |
} else if (cbs_str_equal(&name, "BITWRAP")) { |
| 226 |
tag = tag == 0 ? CBS_ASN1_BITSTRING : tag; |
| 227 |
return generate_wrapped(cbb, str, cnf, tag, /*padding=*/1, format, depth); |
| 228 |
} else if (cbs_str_equal(&name, "SEQWRAP")) { |
| 229 |
tag = tag == 0 ? CBS_ASN1_SEQUENCE : (tag | CBS_ASN1_CONSTRUCTED); |
| 230 |
tag |= CBS_ASN1_CONSTRUCTED; |
| 231 |
return generate_wrapped(cbb, str, cnf, tag, /*padding=*/0, format, depth); |
| 232 |
} else if (cbs_str_equal(&name, "SETWRAP")) { |
| 233 |
tag = tag == 0 ? CBS_ASN1_SET : (tag | CBS_ASN1_CONSTRUCTED); |
| 234 |
return generate_wrapped(cbb, str, cnf, tag, /*padding=*/0, format, depth); |
| 235 |
} else { |
| 236 |
// If this was not a recognized modifier, rewind |str| to before splitting |
| 237 |
// on the comma. The type itself consumes all remaining input. |
| 238 |
str = str_old; |
| 239 |
break; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
// The final element is, like modifiers, NAME:VALUE or NAME, but VALUE spans |
| 244 |
// the length of the string, including any commas. |
| 245 |
const char *colon = strchr(str, ':'); |
| 246 |
CBS name; |
| 247 |
const char *value; |
| 248 |
int has_value = colon != NULL; |
| 249 |
if (has_value) { |
| 250 |
CBS_init(&name, (const uint8_t *)str, colon - str); |
| 251 |
value = colon + 1; |
| 252 |
} else { |
| 253 |
CBS_init(&name, (const uint8_t *)str, strlen(str)); |
| 254 |
value = ""; // Most types treat missing and empty value equivalently. |
| 255 |
} |
| 256 |
|
| 257 |
static const struct { |
| 258 |
const char *name; |
| 259 |
CBS_ASN1_TAG type; |
| 260 |
} kTypes[] = { |
| 261 |
{"BOOL", CBS_ASN1_BOOLEAN}, |
| 262 |
{"BOOLEAN", CBS_ASN1_BOOLEAN}, |
| 263 |
{"NULL", CBS_ASN1_NULL}, |
| 264 |
{"INT", CBS_ASN1_INTEGER}, |
| 265 |
{"INTEGER", CBS_ASN1_INTEGER}, |
| 266 |
{"ENUM", CBS_ASN1_ENUMERATED}, |
| 267 |
{"ENUMERATED", CBS_ASN1_ENUMERATED}, |
| 268 |
{"OID", CBS_ASN1_OBJECT}, |
| 269 |
{"OBJECT", CBS_ASN1_OBJECT}, |
| 270 |
{"UTCTIME", CBS_ASN1_UTCTIME}, |
| 271 |
{"UTC", CBS_ASN1_UTCTIME}, |
| 272 |
{"GENERALIZEDTIME", CBS_ASN1_GENERALIZEDTIME}, |
| 273 |
{"GENTIME", CBS_ASN1_GENERALIZEDTIME}, |
| 274 |
{"OCT", CBS_ASN1_OCTETSTRING}, |
| 275 |
{"OCTETSTRING", CBS_ASN1_OCTETSTRING}, |
| 276 |
{"BITSTR", CBS_ASN1_BITSTRING}, |
| 277 |
{"BITSTRING", CBS_ASN1_BITSTRING}, |
| 278 |
{"UNIVERSALSTRING", CBS_ASN1_UNIVERSALSTRING}, |
| 279 |
{"UNIV", CBS_ASN1_UNIVERSALSTRING}, |
| 280 |
{"IA5", CBS_ASN1_IA5STRING}, |
| 281 |
{"IA5STRING", CBS_ASN1_IA5STRING}, |
| 282 |
{"UTF8", CBS_ASN1_UTF8STRING}, |
| 283 |
{"UTF8String", CBS_ASN1_UTF8STRING}, |
| 284 |
{"BMP", CBS_ASN1_BMPSTRING}, |
| 285 |
{"BMPSTRING", CBS_ASN1_BMPSTRING}, |
| 286 |
{"PRINTABLESTRING", CBS_ASN1_PRINTABLESTRING}, |
| 287 |
{"PRINTABLE", CBS_ASN1_PRINTABLESTRING}, |
| 288 |
{"T61", CBS_ASN1_T61STRING}, |
| 289 |
{"T61STRING", CBS_ASN1_T61STRING}, |
| 290 |
{"TELETEXSTRING", CBS_ASN1_T61STRING}, |
| 291 |
{"SEQUENCE", CBS_ASN1_SEQUENCE}, |
| 292 |
{"SEQ", CBS_ASN1_SEQUENCE}, |
| 293 |
{"SET", CBS_ASN1_SET}, |
| 294 |
}; |
| 295 |
CBS_ASN1_TAG type = 0; |
| 296 |
for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kTypes); i++) { |
| 297 |
if (cbs_str_equal(&name, kTypes[i].name)) { |
| 298 |
type = kTypes[i].type; |
| 299 |
break; |
| 300 |
} |
| 301 |
} |
| 302 |
if (type == 0) { |
| 303 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_TAG); |
| 304 |
return 0; |
| 305 |
} |
| 306 |
|
| 307 |
// If there is an implicit tag, use the constructed bit from the base type. |
| 308 |
tag = tag == 0 ? type : (tag | (type & CBS_ASN1_CONSTRUCTED)); |
| 309 |
CBB child; |
| 310 |
if (!CBB_add_asn1(cbb, &child, tag)) { |
| 311 |
return 0; |
| 312 |
} |
| 313 |
|
| 314 |
switch (type) { |
| 315 |
case CBS_ASN1_NULL: |
| 316 |
if (*value != '\0') { |
| 317 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_NULL_VALUE); |
| 318 |
return 0; |
| 319 |
} |
| 320 |
return CBB_flush(cbb); |
| 321 |
|
| 322 |
case CBS_ASN1_BOOLEAN: { |
| 323 |
if (format != ASN1_GEN_FORMAT_ASCII) { |
| 324 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ASCII_FORMAT); |
| 325 |
return 0; |
| 326 |
} |
| 327 |
ASN1_BOOLEAN boolean; |
| 328 |
if (!X509V3_bool_from_string(value, &boolean)) { |
| 329 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_BOOLEAN); |
| 330 |
return 0; |
| 331 |
} |
| 332 |
return CBB_add_u8(&child, boolean ? 0xff : 0x00) && CBB_flush(cbb); |
| 333 |
} |
| 334 |
|
| 335 |
case CBS_ASN1_INTEGER: |
| 336 |
case CBS_ASN1_ENUMERATED: { |
| 337 |
if (format != ASN1_GEN_FORMAT_ASCII) { |
| 338 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INTEGER_NOT_ASCII_FORMAT); |
| 339 |
return 0; |
| 340 |
} |
| 341 |
ASN1_INTEGER *obj = s2i_ASN1_INTEGER(NULL, value); |
| 342 |
if (obj == NULL) { |
| 343 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_INTEGER); |
| 344 |
return 0; |
| 345 |
} |
| 346 |
int len = i2c_ASN1_INTEGER(obj, NULL); |
| 347 |
uint8_t *out; |
| 348 |
int ok = len > 0 && // |
| 349 |
CBB_add_space(&child, &out, len) && |
| 350 |
i2c_ASN1_INTEGER(obj, &out) == len && |
| 351 |
CBB_flush(cbb); |
| 352 |
ASN1_INTEGER_free(obj); |
| 353 |
return ok; |
| 354 |
} |
| 355 |
|
| 356 |
case CBS_ASN1_OBJECT: { |
| 357 |
if (format != ASN1_GEN_FORMAT_ASCII) { |
| 358 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_OBJECT_NOT_ASCII_FORMAT); |
| 359 |
return 0; |
| 360 |
} |
| 361 |
ASN1_OBJECT *obj = OBJ_txt2obj(value, /*dont_search_names=*/0); |
| 362 |
if (obj == NULL || obj->length == 0) { |
| 363 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OBJECT); |
| 364 |
return 0; |
| 365 |
} |
| 366 |
int ok = CBB_add_bytes(&child, obj->data, obj->length) && CBB_flush(cbb); |
| 367 |
ASN1_OBJECT_free(obj); |
| 368 |
return ok; |
| 369 |
} |
| 370 |
|
| 371 |
case CBS_ASN1_UTCTIME: |
| 372 |
case CBS_ASN1_GENERALIZEDTIME: { |
| 373 |
if (format != ASN1_GEN_FORMAT_ASCII) { |
| 374 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TIME_NOT_ASCII_FORMAT); |
| 375 |
return 0; |
| 376 |
} |
| 377 |
CBS value_cbs; |
| 378 |
CBS_init(&value_cbs, (const uint8_t*)value, strlen(value)); |
| 379 |
int ok = type == CBS_ASN1_UTCTIME |
| 380 |
? CBS_parse_utc_time(&value_cbs, NULL, |
| 381 |
/*allow_timezone_offset=*/0) |
| 382 |
: CBS_parse_generalized_time(&value_cbs, NULL, |
| 383 |
/*allow_timezone_offset=*/0); |
| 384 |
if (!ok) { |
| 385 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_TIME_VALUE); |
| 386 |
return 0; |
| 387 |
} |
| 388 |
return CBB_add_bytes(&child, (const uint8_t *)value, strlen(value)) && |
| 389 |
CBB_flush(cbb); |
| 390 |
} |
| 391 |
|
| 392 |
case CBS_ASN1_UNIVERSALSTRING: |
| 393 |
case CBS_ASN1_IA5STRING: |
| 394 |
case CBS_ASN1_UTF8STRING: |
| 395 |
case CBS_ASN1_BMPSTRING: |
| 396 |
case CBS_ASN1_PRINTABLESTRING: |
| 397 |
case CBS_ASN1_T61STRING: { |
| 398 |
int encoding; |
| 399 |
if (format == ASN1_GEN_FORMAT_ASCII) { |
| 400 |
encoding = MBSTRING_ASC; |
| 401 |
} else if (format == ASN1_GEN_FORMAT_UTF8) { |
| 402 |
encoding = MBSTRING_UTF8; |
| 403 |
} else { |
| 404 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_FORMAT); |
| 405 |
return 0; |
| 406 |
} |
| 407 |
|
| 408 |
// |maxsize| is measured in code points, rather than bytes, but pass it in |
| 409 |
// as a loose cap so fuzzers can exit from excessively long inputs |
| 410 |
// earlier. This limit is not load-bearing because |ASN1_mbstring_ncopy|'s |
| 411 |
// output is already linear in the input. |
| 412 |
ASN1_STRING *obj = NULL; |
| 413 |
if (ASN1_mbstring_ncopy(&obj, (const uint8_t *)value, -1, encoding, |
| 414 |
ASN1_tag2bit(type), /*minsize=*/0, |
| 415 |
/*maxsize=*/ASN1_GEN_MAX_OUTPUT) <= 0) { |
| 416 |
return 0; |
| 417 |
} |
| 418 |
int ok = CBB_add_bytes(&child, obj->data, obj->length) && CBB_flush(cbb); |
| 419 |
ASN1_STRING_free(obj); |
| 420 |
return ok; |
| 421 |
} |
| 422 |
|
| 423 |
case CBS_ASN1_BITSTRING: |
| 424 |
if (format == ASN1_GEN_FORMAT_BITLIST) { |
| 425 |
ASN1_BIT_STRING *obj = ASN1_BIT_STRING_new(); |
| 426 |
if (obj == NULL) { |
| 427 |
return 0; |
| 428 |
} |
| 429 |
if (!CONF_parse_list(value, ',', 1, bitstr_cb, obj)) { |
| 430 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_LIST_ERROR); |
| 431 |
ASN1_BIT_STRING_free(obj); |
| 432 |
return 0; |
| 433 |
} |
| 434 |
int len = i2c_ASN1_BIT_STRING(obj, NULL); |
| 435 |
uint8_t *out; |
| 436 |
int ok = len > 0 && // |
| 437 |
CBB_add_space(&child, &out, len) && |
| 438 |
i2c_ASN1_BIT_STRING(obj, &out) == len && // |
| 439 |
CBB_flush(cbb); |
| 440 |
ASN1_BIT_STRING_free(obj); |
| 441 |
return ok; |
| 442 |
} |
| 443 |
|
| 444 |
// The other formats are the same as OCTET STRING, but with the leading |
| 445 |
// zero bytes. |
| 446 |
if (!CBB_add_u8(&child, 0)) { |
| 447 |
return 0; |
| 448 |
} |
| 449 |
OPENSSL_FALLTHROUGH; |
| 450 |
|
| 451 |
case CBS_ASN1_OCTETSTRING: |
| 452 |
if (format == ASN1_GEN_FORMAT_ASCII) { |
| 453 |
return CBB_add_bytes(&child, (const uint8_t *)value, strlen(value)) && |
| 454 |
CBB_flush(cbb); |
| 455 |
} |
| 456 |
if (format == ASN1_GEN_FORMAT_HEX) { |
| 457 |
size_t len; |
| 458 |
uint8_t *data = x509v3_hex_to_bytes(value, &len); |
| 459 |
if (data == NULL) { |
| 460 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_HEX); |
| 461 |
return 0; |
| 462 |
} |
| 463 |
int ok = CBB_add_bytes(&child, data, len) && CBB_flush(cbb); |
| 464 |
OPENSSL_free(data); |
| 465 |
return ok; |
| 466 |
} |
| 467 |
|
| 468 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_BITSTRING_FORMAT); |
| 469 |
return 0; |
| 470 |
|
| 471 |
case CBS_ASN1_SEQUENCE: |
| 472 |
case CBS_ASN1_SET: |
| 473 |
if (has_value) { |
| 474 |
if (cnf == NULL) { |
| 475 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG); |
| 476 |
return 0; |
| 477 |
} |
| 478 |
const STACK_OF(CONF_VALUE) *section = X509V3_get_section(cnf, value); |
| 479 |
if (section == NULL) { |
| 480 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG); |
| 481 |
return 0; |
| 482 |
} |
| 483 |
for (size_t i = 0; i < sk_CONF_VALUE_num(section); i++) { |
| 484 |
const CONF_VALUE *conf = sk_CONF_VALUE_value(section, i); |
| 485 |
if (!generate_v3(&child, conf->value, cnf, /*tag=*/0, |
| 486 |
ASN1_GEN_FORMAT_ASCII, depth + 1)) { |
| 487 |
return 0; |
| 488 |
} |
| 489 |
// This recursive call, by referencing |section|, is the one place |
| 490 |
// where |generate_v3|'s output can be super-linear in the input. |
| 491 |
// Check bounds here. |
| 492 |
if (CBB_len(&child) > ASN1_GEN_MAX_OUTPUT) { |
| 493 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG); |
| 494 |
return 0; |
| 495 |
} |
| 496 |
} |
| 497 |
} |
| 498 |
if (type == CBS_ASN1_SET) { |
| 499 |
// The SET type here is a SET OF and must be sorted. |
| 500 |
return CBB_flush_asn1_set_of(&child) && CBB_flush(cbb); |
| 501 |
} |
| 502 |
return CBB_flush(cbb); |
| 503 |
|
| 504 |
default: |
| 505 |
OPENSSL_PUT_ERROR(ASN1, ERR_R_INTERNAL_ERROR); |
| 506 |
return 0; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
static int bitstr_cb(const char *elem, size_t len, void *bitstr) { |
| 511 |
CBS cbs; |
| 512 |
CBS_init(&cbs, (const uint8_t *)elem, len); |
| 513 |
uint64_t bitnum; |
| 514 |
if (!CBS_get_u64_decimal(&cbs, &bitnum) || CBS_len(&cbs) != 0 || |
| 515 |
// Cap the highest allowed bit so this mechanism cannot be used to create |
| 516 |
// extremely large allocations with short inputs. The highest named bit in |
| 517 |
// RFC 5280 is 8, so 256 should give comfortable margin but still only |
| 518 |
// allow a 32-byte allocation. |
| 519 |
// |
| 520 |
// We do not consider this function to be safe with untrusted inputs (even |
| 521 |
// without bugs, it is prone to string injection vulnerabilities), so DoS |
| 522 |
// is not truly a concern, but the limit is necessary to keep fuzzing |
| 523 |
// effective. |
| 524 |
bitnum > 256) { |
| 525 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_NUMBER); |
| 526 |
return 0; |
| 527 |
} |
| 528 |
if (!ASN1_BIT_STRING_set_bit(bitstr, (int)bitnum, 1)) { |
| 529 |
return 0; |
| 530 |
} |
| 531 |
return 1; |
| 532 |
} |