| 1 |
// Copyright (c) 2014, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <openssl/bytestring.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include "internal.h" |
| 10 |
|
| 11 |
|
| 12 |
// kMaxDepth limits the recursion depth to avoid overflowing the stack. |
| 13 |
static const uint32_t kMaxDepth = 128; |
| 14 |
|
| 15 |
// is_string_type returns one if |tag| is a string type and zero otherwise. It |
| 16 |
// ignores the constructed bit. |
| 17 |
static int is_string_type(CBS_ASN1_TAG tag) { |
| 18 |
// While BER supports constructed BIT STRINGS, OpenSSL misparses them. To |
| 19 |
// avoid acting on an ambiguous input, we do not support constructed BIT |
| 20 |
// STRINGS. See https://github.com/openssl/openssl/issues/12810. |
| 21 |
switch (tag & ~CBS_ASN1_CONSTRUCTED) { |
| 22 |
case CBS_ASN1_OCTETSTRING: |
| 23 |
case CBS_ASN1_UTF8STRING: |
| 24 |
case CBS_ASN1_NUMERICSTRING: |
| 25 |
case CBS_ASN1_PRINTABLESTRING: |
| 26 |
case CBS_ASN1_T61STRING: |
| 27 |
case CBS_ASN1_VIDEOTEXSTRING: |
| 28 |
case CBS_ASN1_IA5STRING: |
| 29 |
case CBS_ASN1_GRAPHICSTRING: |
| 30 |
case CBS_ASN1_VISIBLESTRING: |
| 31 |
case CBS_ASN1_GENERALSTRING: |
| 32 |
case CBS_ASN1_UNIVERSALSTRING: |
| 33 |
case CBS_ASN1_BMPSTRING: |
| 34 |
return 1; |
| 35 |
default: |
| 36 |
return 0; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
// cbs_find_ber walks an ASN.1 structure in |orig_in| and sets |*ber_found| |
| 41 |
// depending on whether an indefinite length element or constructed string was |
| 42 |
// found. The value of |orig_in| is not changed. It returns one on success (i.e. |
| 43 |
// |*ber_found| was set) and zero on error. |
| 44 |
static int cbs_find_ber(const CBS *orig_in, int *ber_found, uint32_t depth) { |
| 45 |
if (depth > kMaxDepth) { |
| 46 |
return 0; |
| 47 |
} |
| 48 |
|
| 49 |
CBS in = *orig_in; |
| 50 |
*ber_found = 0; |
| 51 |
|
| 52 |
while (CBS_len(&in) > 0) { |
| 53 |
CBS contents; |
| 54 |
CBS_ASN1_TAG tag; |
| 55 |
size_t header_len; |
| 56 |
int indefinite; |
| 57 |
if (!CBS_get_any_ber_asn1_element(&in, &contents, &tag, &header_len, |
| 58 |
ber_found, &indefinite)) { |
| 59 |
return 0; |
| 60 |
} |
| 61 |
if (*ber_found) { |
| 62 |
return 1; |
| 63 |
} |
| 64 |
if (tag & CBS_ASN1_CONSTRUCTED) { |
| 65 |
if (is_string_type(tag)) { |
| 66 |
// Constructed strings are only legal in BER and require conversion. |
| 67 |
*ber_found = 1; |
| 68 |
return 1; |
| 69 |
} |
| 70 |
if (!CBS_skip(&contents, header_len) || |
| 71 |
!cbs_find_ber(&contents, ber_found, depth + 1)) { |
| 72 |
return 0; |
| 73 |
} |
| 74 |
if (*ber_found) { |
| 75 |
// We already found BER. No need to continue parsing. |
| 76 |
return 1; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return 1; |
| 82 |
} |
| 83 |
|
| 84 |
// cbs_get_eoc returns one if |cbs| begins with an "end of contents" (EOC) value |
| 85 |
// and zero otherwise. If an EOC was found, it advances |cbs| past it. |
| 86 |
static int cbs_get_eoc(CBS *cbs) { |
| 87 |
if (CBS_len(cbs) >= 2 && |
| 88 |
CBS_data(cbs)[0] == 0 && CBS_data(cbs)[1] == 0) { |
| 89 |
return CBS_skip(cbs, 2); |
| 90 |
} |
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
// cbs_convert_ber reads BER data from |in| and writes DER data to |out|. If |
| 95 |
// |string_tag| is non-zero, then all elements must match |string_tag| up to the |
| 96 |
// constructed bit and primitive element bodies are written to |out| without |
| 97 |
// element headers. This is used when concatenating the fragments of a |
| 98 |
// constructed string. If |looking_for_eoc| is set then any EOC elements found |
| 99 |
// will cause the function to return after consuming it. It returns one on |
| 100 |
// success and zero on error. |
| 101 |
static int cbs_convert_ber(CBS *in, CBB *out, CBS_ASN1_TAG string_tag, |
| 102 |
int looking_for_eoc, uint32_t depth) { |
| 103 |
assert(!(string_tag & CBS_ASN1_CONSTRUCTED)); |
| 104 |
|
| 105 |
if (depth > kMaxDepth) { |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
while (CBS_len(in) > 0) { |
| 110 |
if (looking_for_eoc && cbs_get_eoc(in)) { |
| 111 |
return 1; |
| 112 |
} |
| 113 |
|
| 114 |
CBS contents; |
| 115 |
CBS_ASN1_TAG tag, child_string_tag = string_tag; |
| 116 |
size_t header_len; |
| 117 |
int indefinite; |
| 118 |
CBB *out_contents, out_contents_storage; |
| 119 |
if (!CBS_get_any_ber_asn1_element(in, &contents, &tag, &header_len, |
| 120 |
/*out_ber_found=*/NULL, &indefinite)) { |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
if (string_tag != 0) { |
| 125 |
// This is part of a constructed string. All elements must match |
| 126 |
// |string_tag| up to the constructed bit and get appended to |out| |
| 127 |
// without a child element. |
| 128 |
if ((tag & ~CBS_ASN1_CONSTRUCTED) != string_tag) { |
| 129 |
return 0; |
| 130 |
} |
| 131 |
out_contents = out; |
| 132 |
} else { |
| 133 |
CBS_ASN1_TAG out_tag = tag; |
| 134 |
if ((tag & CBS_ASN1_CONSTRUCTED) && is_string_type(tag)) { |
| 135 |
// If a constructed string, clear the constructed bit and inform |
| 136 |
// children to concatenate bodies. |
| 137 |
out_tag &= ~CBS_ASN1_CONSTRUCTED; |
| 138 |
child_string_tag = out_tag; |
| 139 |
} |
| 140 |
if (!CBB_add_asn1(out, &out_contents_storage, out_tag)) { |
| 141 |
return 0; |
| 142 |
} |
| 143 |
out_contents = &out_contents_storage; |
| 144 |
} |
| 145 |
|
| 146 |
if (indefinite) { |
| 147 |
if (!cbs_convert_ber(in, out_contents, child_string_tag, |
| 148 |
/*looking_for_eoc=*/1, depth + 1) || |
| 149 |
!CBB_flush(out)) { |
| 150 |
return 0; |
| 151 |
} |
| 152 |
continue; |
| 153 |
} |
| 154 |
|
| 155 |
if (!CBS_skip(&contents, header_len)) { |
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
if (tag & CBS_ASN1_CONSTRUCTED) { |
| 160 |
// Recurse into children. |
| 161 |
if (!cbs_convert_ber(&contents, out_contents, child_string_tag, |
| 162 |
/*looking_for_eoc=*/0, depth + 1)) { |
| 163 |
return 0; |
| 164 |
} |
| 165 |
} else { |
| 166 |
// Copy primitive contents as-is. |
| 167 |
if (!CBB_add_bytes(out_contents, CBS_data(&contents), |
| 168 |
CBS_len(&contents))) { |
| 169 |
return 0; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
if (!CBB_flush(out)) { |
| 174 |
return 0; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return looking_for_eoc == 0; |
| 179 |
} |
| 180 |
|
| 181 |
int CBS_asn1_ber_to_der(CBS *in, CBS *out, uint8_t **out_storage) { |
| 182 |
CBB cbb; |
| 183 |
|
| 184 |
// First, do a quick walk to find any indefinite-length elements. Most of the |
| 185 |
// time we hope that there aren't any and thus we can quickly return. |
| 186 |
int conversion_needed; |
| 187 |
if (!cbs_find_ber(in, &conversion_needed, 0)) { |
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
if (!conversion_needed) { |
| 192 |
if (!CBS_get_any_asn1_element(in, out, NULL, NULL)) { |
| 193 |
return 0; |
| 194 |
} |
| 195 |
*out_storage = NULL; |
| 196 |
return 1; |
| 197 |
} |
| 198 |
|
| 199 |
size_t len; |
| 200 |
if (!CBB_init(&cbb, CBS_len(in)) || |
| 201 |
!cbs_convert_ber(in, &cbb, 0, 0, 0) || |
| 202 |
!CBB_finish(&cbb, out_storage, &len)) { |
| 203 |
CBB_cleanup(&cbb); |
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
CBS_init(out, *out_storage, len); |
| 208 |
return 1; |
| 209 |
} |
| 210 |
|
| 211 |
int CBS_get_asn1_implicit_string(CBS *in, CBS *out, uint8_t **out_storage, |
| 212 |
CBS_ASN1_TAG outer_tag, |
| 213 |
CBS_ASN1_TAG inner_tag) { |
| 214 |
assert(!(outer_tag & CBS_ASN1_CONSTRUCTED)); |
| 215 |
assert(!(inner_tag & CBS_ASN1_CONSTRUCTED)); |
| 216 |
assert(is_string_type(inner_tag)); |
| 217 |
|
| 218 |
if (CBS_peek_asn1_tag(in, outer_tag)) { |
| 219 |
// Normal implicitly-tagged string. |
| 220 |
*out_storage = NULL; |
| 221 |
return CBS_get_asn1(in, out, outer_tag); |
| 222 |
} |
| 223 |
|
| 224 |
// Otherwise, try to parse an implicitly-tagged constructed string. |
| 225 |
// |CBS_asn1_ber_to_der| is assumed to have run, so only allow one level deep |
| 226 |
// of nesting. |
| 227 |
CBB result; |
| 228 |
CBS child; |
| 229 |
if (!CBB_init(&result, CBS_len(in)) || |
| 230 |
!CBS_get_asn1(in, &child, outer_tag | CBS_ASN1_CONSTRUCTED)) { |
| 231 |
goto err; |
| 232 |
} |
| 233 |
|
| 234 |
while (CBS_len(&child) > 0) { |
| 235 |
CBS chunk; |
| 236 |
if (!CBS_get_asn1(&child, &chunk, inner_tag) || |
| 237 |
!CBB_add_bytes(&result, CBS_data(&chunk), CBS_len(&chunk))) { |
| 238 |
goto err; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
uint8_t *data; |
| 243 |
size_t len; |
| 244 |
if (!CBB_finish(&result, &data, &len)) { |
| 245 |
goto err; |
| 246 |
} |
| 247 |
|
| 248 |
CBS_init(out, data, len); |
| 249 |
*out_storage = data; |
| 250 |
return 1; |
| 251 |
|
| 252 |
err: |
| 253 |
CBB_cleanup(&result); |
| 254 |
return 0; |
| 255 |
} |