| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/asn1.h> |
| 5 |
|
| 6 |
#include <limits.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include <openssl/bytestring.h> |
| 10 |
#include <openssl/err.h> |
| 11 |
#include <openssl/mem.h> |
| 12 |
|
| 13 |
#include "../bytestring/internal.h" |
| 14 |
#include "../internal.h" |
| 15 |
#include "internal.h" |
| 16 |
|
| 17 |
|
| 18 |
// Cross-module errors from crypto/x509/i2d_pr.c. |
| 19 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_PUBLIC_KEY_TYPE) |
| 20 |
|
| 21 |
// Cross-module errors from crypto/x509/algorithm.c. |
| 22 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, CONTEXT_NOT_INITIALISED) |
| 23 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, DIGEST_AND_KEY_TYPE_NOT_SUPPORTED) |
| 24 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_MESSAGE_DIGEST_ALGORITHM) |
| 25 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_SIGNATURE_ALGORITHM) |
| 26 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, WRONG_PUBLIC_KEY_TYPE) |
| 27 |
// Cross-module errors from crypto/x509/asn1_gen.c. TODO(davidben): Remove |
| 28 |
// these once asn1_gen.c is gone. |
| 29 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, DEPTH_EXCEEDED) |
| 30 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_BITSTRING_FORMAT) |
| 31 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_BOOLEAN) |
| 32 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_FORMAT) |
| 33 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_HEX) |
| 34 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_IMPLICIT_TAG) |
| 35 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_INTEGER) |
| 36 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_NESTED_TAGGING) |
| 37 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_NULL_VALUE) |
| 38 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_OBJECT) |
| 39 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, ILLEGAL_TIME_VALUE) |
| 40 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, INTEGER_NOT_ASCII_FORMAT) |
| 41 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_MODIFIER) |
| 42 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_NUMBER) |
| 43 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, LIST_ERROR) |
| 44 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, MISSING_VALUE) |
| 45 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, NOT_ASCII_FORMAT) |
| 46 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, OBJECT_NOT_ASCII_FORMAT) |
| 47 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, SEQUENCE_OR_SET_NEEDS_CONFIG) |
| 48 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, TIME_NOT_ASCII_FORMAT) |
| 49 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_FORMAT) |
| 50 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, UNKNOWN_TAG) |
| 51 |
OPENSSL_DECLARE_ERROR_REASON(ASN1, UNSUPPORTED_TYPE) |
| 52 |
|
| 53 |
// Limit |ASN1_STRING|s to 64 MiB of data. Most of this module, as well as |
| 54 |
// downstream code, does not correctly handle overflow. We cap string fields |
| 55 |
// more tightly than strictly necessary to fit in |int|. This is not expected to |
| 56 |
// impact real world uses of this field. |
| 57 |
// |
| 58 |
// In particular, this limit is small enough that the bit count of a BIT STRING |
| 59 |
// comfortably fits in an |int|, with room for arithmetic. |
| 60 |
#define ASN1_STRING_MAX (64 * 1024 * 1024) |
| 61 |
|
| 62 |
static void asn1_put_length(unsigned char **pp, int length); |
| 63 |
|
| 64 |
// |ASN1_get_object| parses an ASN.1 header, including tag, class, and length |
| 65 |
// information. The tag number is written to |*out_tag|. The class is written to |
| 66 |
// |*out_class|. If the tag is not indefinite, the content length is written to |
| 67 |
// |*out_len|. If the tag indicates the indefinite form, |*out_len| is set to 0. |
| 68 |
// |inp| is advanced past the header in the input buffer. |
| 69 |
// |
| 70 |
// Indefinite-length encoding and universal tags are always allowed to align |
| 71 |
// with OpenSSL behavior. |
| 72 |
// |
| 73 |
// The return value may have the following bits set: |
| 74 |
// * 0x80: error occurred while parsing. |
| 75 |
// * 0x20: the encoding is constructed, not primitive. |
| 76 |
// * 0x01: indefinite-length constructed encoding. |
| 77 |
int ASN1_get_object(const unsigned char **inp, long *out_len, int *out_tag, |
| 78 |
int *out_class, long in_len) { |
| 79 |
if (in_len < 0) { |
| 80 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG); |
| 81 |
return 0x80; |
| 82 |
} |
| 83 |
|
| 84 |
// TODO(https://crbug.com/boringssl/354): This should use |CBS_get_asn1| to |
| 85 |
// reject non-minimal lengths, which are only allowed in BER. However, |
| 86 |
// Android sometimes needs allow a non-minimal length in certificate |
| 87 |
// signature fields (see b/18228011). Make this only apply to that field, |
| 88 |
// while requiring DER elsewhere. Better yet, it should be limited to an |
| 89 |
// preprocessing step in that part of Android. |
| 90 |
CBS_ASN1_TAG tag; |
| 91 |
size_t header_len; |
| 92 |
int indefinite; |
| 93 |
CBS cbs, body; |
| 94 |
CBS_init(&cbs, *inp, (size_t)in_len); |
| 95 |
|
| 96 |
int ber_found_temp; |
| 97 |
if (!cbs_get_any_asn1_element(&cbs, &body, &tag, &header_len, &ber_found_temp, |
| 98 |
&indefinite, /*ber_ok=*/1, |
| 99 |
/*universal_tag_ok=*/1) || |
| 100 |
!CBS_skip(&body, header_len) || |
| 101 |
// Bound the length to comfortably fit in an int. Lengths in this |
| 102 |
// module often switch between int and long without overflow checks. |
| 103 |
CBS_len(&body) > INT_MAX / 2) { |
| 104 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG); |
| 105 |
return 0x80; |
| 106 |
} |
| 107 |
|
| 108 |
// Convert between tag representations. |
| 109 |
int tag_class = (tag & CBS_ASN1_CLASS_MASK) >> CBS_ASN1_TAG_SHIFT; |
| 110 |
int constructed = (indefinite ? 1 : 0) | |
| 111 |
((tag & CBS_ASN1_CONSTRUCTED) >> CBS_ASN1_TAG_SHIFT); |
| 112 |
int tag_number = tag & CBS_ASN1_TAG_NUMBER_MASK; |
| 113 |
|
| 114 |
// To avoid ambiguity with V_ASN1_NEG, impose a limit on universal tags. |
| 115 |
if (tag_class == V_ASN1_UNIVERSAL && tag_number > V_ASN1_MAX_UNIVERSAL) { |
| 116 |
// Ruby's OpenSSL::test_basic_asn1data has an assertion that violates this |
| 117 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG); |
| 118 |
return 0x80; |
| 119 |
} |
| 120 |
|
| 121 |
*inp = CBS_data(&body); |
| 122 |
// If |indefinite|, |header_len| and |CBS_len(&body)| were equal prior to the |
| 123 |
// |CBS_skip(&body, header_len)| call above and |CBS_len(&body)| is now zero. |
| 124 |
*out_len = CBS_len(&body); |
| 125 |
*out_tag = tag_number; |
| 126 |
*out_class = tag_class; |
| 127 |
return constructed; |
| 128 |
} |
| 129 |
|
| 130 |
// class 0 is constructed constructed == 2 for indefinite length constructed |
| 131 |
void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag, |
| 132 |
int xclass) { |
| 133 |
unsigned char *p = *pp; |
| 134 |
int i, ttag; |
| 135 |
|
| 136 |
i = (constructed) ? V_ASN1_CONSTRUCTED : 0; |
| 137 |
i |= (xclass & V_ASN1_PRIVATE); |
| 138 |
if (tag < 31) { |
| 139 |
*(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG); |
| 140 |
} else { |
| 141 |
*(p++) = i | V_ASN1_PRIMITIVE_TAG; |
| 142 |
for (i = 0, ttag = tag; ttag > 0; i++) { |
| 143 |
ttag >>= 7; |
| 144 |
} |
| 145 |
ttag = i; |
| 146 |
while (i-- > 0) { |
| 147 |
p[i] = tag & 0x7f; |
| 148 |
if (i != (ttag - 1)) { |
| 149 |
p[i] |= 0x80; |
| 150 |
} |
| 151 |
tag >>= 7; |
| 152 |
} |
| 153 |
p += ttag; |
| 154 |
} |
| 155 |
if (constructed == 2) { |
| 156 |
*(p++) = 0x80; |
| 157 |
} else { |
| 158 |
asn1_put_length(&p, length); |
| 159 |
} |
| 160 |
*pp = p; |
| 161 |
} |
| 162 |
|
| 163 |
int ASN1_put_eoc(unsigned char **pp) { |
| 164 |
// This function is no longer used in the library, but some external code |
| 165 |
// uses it. |
| 166 |
unsigned char *p = *pp; |
| 167 |
*p++ = 0; |
| 168 |
*p++ = 0; |
| 169 |
*pp = p; |
| 170 |
return 2; |
| 171 |
} |
| 172 |
|
| 173 |
static void asn1_put_length(unsigned char **pp, int length) { |
| 174 |
unsigned char *p = *pp; |
| 175 |
int i, l; |
| 176 |
if (length <= 127) { |
| 177 |
*(p++) = (unsigned char)length; |
| 178 |
} else { |
| 179 |
l = length; |
| 180 |
for (i = 0; l > 0; i++) { |
| 181 |
l >>= 8; |
| 182 |
} |
| 183 |
*(p++) = i | 0x80; |
| 184 |
l = i; |
| 185 |
while (i-- > 0) { |
| 186 |
p[i] = length & 0xff; |
| 187 |
length >>= 8; |
| 188 |
} |
| 189 |
p += l; |
| 190 |
} |
| 191 |
*pp = p; |
| 192 |
} |
| 193 |
|
| 194 |
int ASN1_object_size(int constructed, int length, int tag) { |
| 195 |
int ret = 1; |
| 196 |
if (length < 0) { |
| 197 |
return -1; |
| 198 |
} |
| 199 |
if (tag >= 31) { |
| 200 |
while (tag > 0) { |
| 201 |
tag >>= 7; |
| 202 |
ret++; |
| 203 |
} |
| 204 |
} |
| 205 |
if (constructed == 2) { |
| 206 |
ret += 3; |
| 207 |
} else { |
| 208 |
ret++; |
| 209 |
if (length > 127) { |
| 210 |
int tmplen = length; |
| 211 |
while (tmplen > 0) { |
| 212 |
tmplen >>= 8; |
| 213 |
ret++; |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
if (ret >= INT_MAX - length) { |
| 218 |
return -1; |
| 219 |
} |
| 220 |
return ret + length; |
| 221 |
} |
| 222 |
|
| 223 |
int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str) { |
| 224 |
if (str == NULL) { |
| 225 |
return 0; |
| 226 |
} |
| 227 |
if (!ASN1_STRING_set(dst, str->data, str->length)) { |
| 228 |
return 0; |
| 229 |
} |
| 230 |
dst->type = str->type; |
| 231 |
dst->flags = str->flags; |
| 232 |
return 1; |
| 233 |
} |
| 234 |
|
| 235 |
ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str) { |
| 236 |
ASN1_STRING *ret; |
| 237 |
if (!str) { |
| 238 |
return NULL; |
| 239 |
} |
| 240 |
ret = ASN1_STRING_new(); |
| 241 |
if (!ret) { |
| 242 |
return NULL; |
| 243 |
} |
| 244 |
if (!ASN1_STRING_copy(ret, str)) { |
| 245 |
ASN1_STRING_free(ret); |
| 246 |
return NULL; |
| 247 |
} |
| 248 |
return ret; |
| 249 |
} |
| 250 |
|
| 251 |
int ASN1_STRING_set(ASN1_STRING *str, const void *_data, ossl_ssize_t len_s) { |
| 252 |
const char *data = _data; |
| 253 |
size_t len; |
| 254 |
if (len_s < 0) { |
| 255 |
if (data == NULL) { |
| 256 |
return 0; |
| 257 |
} |
| 258 |
len = strlen(data); |
| 259 |
} else { |
| 260 |
len = (size_t)len_s; |
| 261 |
} |
| 262 |
|
| 263 |
OPENSSL_STATIC_ASSERT(ASN1_STRING_MAX < INT_MAX, len_will_not_overflow_int); |
| 264 |
if (len > ASN1_STRING_MAX) { |
| 265 |
OPENSSL_PUT_ERROR(ASN1, ERR_R_OVERFLOW); |
| 266 |
return 0; |
| 267 |
} |
| 268 |
|
| 269 |
if (str->length <= (int)len || str->data == NULL) { |
| 270 |
unsigned char *c = str->data; |
| 271 |
if (c == NULL) { |
| 272 |
str->data = OPENSSL_malloc(len + 1); |
| 273 |
} else { |
| 274 |
str->data = OPENSSL_realloc(c, len + 1); |
| 275 |
} |
| 276 |
|
| 277 |
if (str->data == NULL) { |
| 278 |
str->data = c; |
| 279 |
return 0; |
| 280 |
} |
| 281 |
} |
| 282 |
str->length = (int)len; |
| 283 |
if (data != NULL) { |
| 284 |
OPENSSL_memcpy(str->data, data, len); |
| 285 |
// Historically, OpenSSL would NUL-terminate most (but not all) |
| 286 |
// |ASN1_STRING|s, in case anyone accidentally passed |str->data| into a |
| 287 |
// function expecting a C string. We retain this behavior for compatibility, |
| 288 |
// but code must not rely on this. See CVE-2021-3712. |
| 289 |
str->data[len] = '\0'; |
| 290 |
} |
| 291 |
return 1; |
| 292 |
} |
| 293 |
|
| 294 |
void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len) { |
| 295 |
OPENSSL_free(str->data); |
| 296 |
str->data = data; |
| 297 |
str->length = len; |
| 298 |
} |
| 299 |
|
| 300 |
ASN1_STRING *ASN1_STRING_new(void) { |
| 301 |
return (ASN1_STRING_type_new(V_ASN1_OCTET_STRING)); |
| 302 |
} |
| 303 |
|
| 304 |
ASN1_STRING *ASN1_STRING_type_new(int type) { |
| 305 |
ASN1_STRING *ret; |
| 306 |
|
| 307 |
ret = (ASN1_STRING *)OPENSSL_zalloc(sizeof(ASN1_STRING)); |
| 308 |
if (ret == NULL) { |
| 309 |
return NULL; |
| 310 |
} |
| 311 |
ret->type = type; |
| 312 |
return ret; |
| 313 |
} |
| 314 |
|
| 315 |
void ASN1_STRING_free(ASN1_STRING *str) { |
| 316 |
if (str == NULL) { |
| 317 |
return; |
| 318 |
} |
| 319 |
OPENSSL_free(str->data); |
| 320 |
OPENSSL_free(str); |
| 321 |
} |
| 322 |
|
| 323 |
void ASN1_STRING_clear_free(ASN1_STRING *str) { |
| 324 |
ASN1_STRING_free(str); |
| 325 |
} |
| 326 |
|
| 327 |
int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b) { |
| 328 |
// Capture padding bits and implicit truncation in BIT STRINGs. |
| 329 |
int a_length = a->length, b_length = b->length; |
| 330 |
uint8_t a_padding = 0, b_padding = 0; |
| 331 |
if (a->type == V_ASN1_BIT_STRING) { |
| 332 |
a_length = asn1_bit_string_length(a, &a_padding); |
| 333 |
} |
| 334 |
if (b->type == V_ASN1_BIT_STRING) { |
| 335 |
b_length = asn1_bit_string_length(b, &b_padding); |
| 336 |
} |
| 337 |
|
| 338 |
if (a_length < b_length) { |
| 339 |
return -1; |
| 340 |
} |
| 341 |
if (a_length > b_length) { |
| 342 |
return 1; |
| 343 |
} |
| 344 |
// In a BIT STRING, the number of bits is 8 * length - padding. Invert this |
| 345 |
// comparison so we compare by lengths. |
| 346 |
if (a_padding > b_padding) { |
| 347 |
return -1; |
| 348 |
} |
| 349 |
if (a_padding < b_padding) { |
| 350 |
return 1; |
| 351 |
} |
| 352 |
|
| 353 |
int ret = OPENSSL_memcmp(a->data, b->data, a_length); |
| 354 |
if (ret != 0) { |
| 355 |
return ret; |
| 356 |
} |
| 357 |
|
| 358 |
// Comparing the type first is more natural, but this matches OpenSSL. |
| 359 |
if (a->type < b->type) { |
| 360 |
return -1; |
| 361 |
} |
| 362 |
if (a->type > b->type) { |
| 363 |
return 1; |
| 364 |
} |
| 365 |
return 0; |
| 366 |
} |
| 367 |
|
| 368 |
int ASN1_STRING_length(const ASN1_STRING *str) { return str->length; } |
| 369 |
|
| 370 |
int ASN1_STRING_type(const ASN1_STRING *str) { return str->type; } |
| 371 |
|
| 372 |
unsigned char *ASN1_STRING_data(ASN1_STRING *str) { return str->data; } |
| 373 |
|
| 374 |
const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *str) { |
| 375 |
return str->data; |
| 376 |
} |