| 1 |
/* Copyright (c) 2014, Google Inc. |
| 2 |
* |
| 3 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 4 |
* purpose with or without fee is hereby granted, provided that the above |
| 5 |
* copyright notice and this permission notice appear in all copies. |
| 6 |
* |
| 7 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 |
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 |
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 |
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 |
|
| 15 |
#include <openssl/bytestring.h> |
| 16 |
|
| 17 |
#include <assert.h> |
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
#include "internal.h" |
| 21 |
|
| 22 |
|
| 23 |
/* kMaxDepth is a just a sanity limit. The code should be such that the length |
| 24 |
* of the input being processes always decreases. None the less, a very large |
| 25 |
* input could otherwise cause the stack to overflow. */ |
| 26 |
static const unsigned kMaxDepth = 2048; |
| 27 |
|
| 28 |
/* is_string_type returns one if |tag| is a string type and zero otherwise. It |
| 29 |
* ignores the constructed bit. */ |
| 30 |
static int is_string_type(unsigned tag) { |
| 31 |
if ((tag & 0xc0) != 0) { |
| 32 |
return 0; |
| 33 |
} |
| 34 |
switch (tag & 0x1f) { |
| 35 |
case CBS_ASN1_BITSTRING: |
| 36 |
case CBS_ASN1_OCTETSTRING: |
| 37 |
case CBS_ASN1_UTF8STRING: |
| 38 |
case CBS_ASN1_NUMERICSTRING: |
| 39 |
case CBS_ASN1_PRINTABLESTRING: |
| 40 |
case CBS_ASN1_T16STRING: |
| 41 |
case CBS_ASN1_VIDEOTEXSTRING: |
| 42 |
case CBS_ASN1_IA5STRING: |
| 43 |
case CBS_ASN1_GRAPHICSTRING: |
| 44 |
case CBS_ASN1_VISIBLESTRING: |
| 45 |
case CBS_ASN1_GENERALSTRING: |
| 46 |
case CBS_ASN1_UNIVERSALSTRING: |
| 47 |
case CBS_ASN1_BMPSTRING: |
| 48 |
return 1; |
| 49 |
default: |
| 50 |
return 0; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/* cbs_find_ber walks an ASN.1 structure in |orig_in| and sets |*ber_found| |
| 55 |
* depending on whether an indefinite length element or constructed string was |
| 56 |
* found. The value of |orig_in| is not changed. It returns one on success (i.e. |
| 57 |
* |*ber_found| was set) and zero on error. */ |
| 58 |
static int cbs_find_ber(const CBS *orig_in, char *ber_found, unsigned depth) { |
| 59 |
CBS in; |
| 60 |
|
| 61 |
if (depth > kMaxDepth) { |
| 62 |
return 0; |
| 63 |
} |
| 64 |
|
| 65 |
CBS_init(&in, CBS_data(orig_in), CBS_len(orig_in)); |
| 66 |
*ber_found = 0; |
| 67 |
|
| 68 |
while (CBS_len(&in) > 0) { |
| 69 |
CBS contents; |
| 70 |
unsigned tag; |
| 71 |
size_t header_len; |
| 72 |
|
| 73 |
if (!CBS_get_any_ber_asn1_element(&in, &contents, &tag, &header_len)) { |
| 74 |
return 0; |
| 75 |
} |
| 76 |
if (CBS_len(&contents) == header_len && |
| 77 |
header_len > 0 && |
| 78 |
CBS_data(&contents)[header_len-1] == 0x80) { |
| 79 |
/* Found an indefinite-length element. */ |
| 80 |
*ber_found = 1; |
| 81 |
return 1; |
| 82 |
} |
| 83 |
if (tag & CBS_ASN1_CONSTRUCTED) { |
| 84 |
if (is_string_type(tag)) { |
| 85 |
/* Constructed strings are only legal in BER and require conversion. */ |
| 86 |
*ber_found = 1; |
| 87 |
return 1; |
| 88 |
} |
| 89 |
if (!CBS_skip(&contents, header_len) || |
| 90 |
!cbs_find_ber(&contents, ber_found, depth + 1)) { |
| 91 |
return 0; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return 1; |
| 97 |
} |
| 98 |
|
| 99 |
/* is_eoc returns true if |header_len| and |contents|, as returned by |
| 100 |
* |CBS_get_any_ber_asn1_element|, indicate an "end of contents" (EOC) value. */ |
| 101 |
static char is_eoc(size_t header_len, CBS *contents) { |
| 102 |
return header_len == 2 && CBS_len(contents) == 2 && |
| 103 |
memcmp(CBS_data(contents), "\x00\x00", 2) == 0; |
| 104 |
} |
| 105 |
|
| 106 |
/* cbs_convert_ber reads BER data from |in| and writes DER data to |out|. If |
| 107 |
* |string_tag| is non-zero, then all elements must match |string_tag| up to the |
| 108 |
* constructed bit and primitive element bodies are written to |out| without |
| 109 |
* element headers. This is used when concatenating the fragments of a |
| 110 |
* constructed string. If |looking_for_eoc| is set then any EOC elements found |
| 111 |
* will cause the function to return after consuming it. It returns one on |
| 112 |
* success and zero on error. */ |
| 113 |
static int cbs_convert_ber(CBS *in, CBB *out, unsigned string_tag, |
| 114 |
char looking_for_eoc, unsigned depth) { |
| 115 |
assert(!(string_tag & CBS_ASN1_CONSTRUCTED)); |
| 116 |
|
| 117 |
if (depth > kMaxDepth) { |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
while (CBS_len(in) > 0) { |
| 122 |
CBS contents; |
| 123 |
unsigned tag, child_string_tag = string_tag; |
| 124 |
size_t header_len; |
| 125 |
CBB *out_contents, out_contents_storage; |
| 126 |
|
| 127 |
if (!CBS_get_any_ber_asn1_element(in, &contents, &tag, &header_len)) { |
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
if (is_eoc(header_len, &contents)) { |
| 132 |
return looking_for_eoc; |
| 133 |
} |
| 134 |
|
| 135 |
if (string_tag != 0) { |
| 136 |
/* This is part of a constructed string. All elements must match |
| 137 |
* |string_tag| up to the constructed bit and get appended to |out| |
| 138 |
* without a child element. */ |
| 139 |
if ((tag & ~CBS_ASN1_CONSTRUCTED) != string_tag) { |
| 140 |
return 0; |
| 141 |
} |
| 142 |
out_contents = out; |
| 143 |
} else { |
| 144 |
unsigned out_tag = tag; |
| 145 |
if ((tag & CBS_ASN1_CONSTRUCTED) && is_string_type(tag)) { |
| 146 |
/* If a constructed string, clear the constructed bit and inform |
| 147 |
* children to concatenate bodies. */ |
| 148 |
out_tag &= ~CBS_ASN1_CONSTRUCTED; |
| 149 |
child_string_tag = out_tag; |
| 150 |
} |
| 151 |
if (!CBB_add_asn1(out, &out_contents_storage, out_tag)) { |
| 152 |
return 0; |
| 153 |
} |
| 154 |
out_contents = &out_contents_storage; |
| 155 |
} |
| 156 |
|
| 157 |
if (CBS_len(&contents) == header_len && header_len > 0 && |
| 158 |
CBS_data(&contents)[header_len - 1] == 0x80) { |
| 159 |
/* This is an indefinite length element. */ |
| 160 |
if (!cbs_convert_ber(in, out_contents, child_string_tag, |
| 161 |
1 /* looking for eoc */, depth + 1) || |
| 162 |
!CBB_flush(out)) { |
| 163 |
return 0; |
| 164 |
} |
| 165 |
continue; |
| 166 |
} |
| 167 |
|
| 168 |
if (!CBS_skip(&contents, header_len)) { |
| 169 |
return 0; |
| 170 |
} |
| 171 |
|
| 172 |
if (tag & CBS_ASN1_CONSTRUCTED) { |
| 173 |
/* Recurse into children. */ |
| 174 |
if (!cbs_convert_ber(&contents, out_contents, child_string_tag, |
| 175 |
0 /* not looking for eoc */, depth + 1)) { |
| 176 |
return 0; |
| 177 |
} |
| 178 |
} else { |
| 179 |
/* Copy primitive contents as-is. */ |
| 180 |
if (!CBB_add_bytes(out_contents, CBS_data(&contents), |
| 181 |
CBS_len(&contents))) { |
| 182 |
return 0; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if (!CBB_flush(out)) { |
| 187 |
return 0; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
return looking_for_eoc == 0; |
| 192 |
} |
| 193 |
|
| 194 |
int CBS_asn1_ber_to_der(CBS *in, uint8_t **out, size_t *out_len) { |
| 195 |
CBB cbb; |
| 196 |
|
| 197 |
/* First, do a quick walk to find any indefinite-length elements. Most of the |
| 198 |
* time we hope that there aren't any and thus we can quickly return. */ |
| 199 |
char conversion_needed; |
| 200 |
if (!cbs_find_ber(in, &conversion_needed, 0)) { |
| 201 |
return 0; |
| 202 |
} |
| 203 |
|
| 204 |
if (!conversion_needed) { |
| 205 |
*out = NULL; |
| 206 |
*out_len = 0; |
| 207 |
return 1; |
| 208 |
} |
| 209 |
|
| 210 |
if (!CBB_init(&cbb, CBS_len(in)) || |
| 211 |
!cbs_convert_ber(in, &cbb, 0, 0, 0) || |
| 212 |
!CBB_finish(&cbb, out, out_len)) { |
| 213 |
CBB_cleanup(&cbb); |
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
return 1; |
| 218 |
} |
| 219 |
|
| 220 |
int CBS_get_asn1_implicit_string(CBS *in, CBS *out, uint8_t **out_storage, |
| 221 |
unsigned outer_tag, unsigned inner_tag) { |
| 222 |
assert(!(outer_tag & CBS_ASN1_CONSTRUCTED)); |
| 223 |
assert(!(inner_tag & CBS_ASN1_CONSTRUCTED)); |
| 224 |
assert(is_string_type(inner_tag)); |
| 225 |
|
| 226 |
if (CBS_peek_asn1_tag(in, outer_tag)) { |
| 227 |
/* Normal implicitly-tagged string. */ |
| 228 |
*out_storage = NULL; |
| 229 |
return CBS_get_asn1(in, out, outer_tag); |
| 230 |
} |
| 231 |
|
| 232 |
/* Otherwise, try to parse an implicitly-tagged constructed string. |
| 233 |
* |CBS_asn1_ber_to_der| is assumed to have run, so only allow one level deep |
| 234 |
* of nesting. */ |
| 235 |
CBB result; |
| 236 |
CBS child; |
| 237 |
if (!CBB_init(&result, CBS_len(in)) || |
| 238 |
!CBS_get_asn1(in, &child, outer_tag | CBS_ASN1_CONSTRUCTED)) { |
| 239 |
goto err; |
| 240 |
} |
| 241 |
|
| 242 |
while (CBS_len(&child) > 0) { |
| 243 |
CBS chunk; |
| 244 |
if (!CBS_get_asn1(&child, &chunk, inner_tag) || |
| 245 |
!CBB_add_bytes(&result, CBS_data(&chunk), CBS_len(&chunk))) { |
| 246 |
goto err; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
uint8_t *data; |
| 251 |
size_t len; |
| 252 |
if (!CBB_finish(&result, &data, &len)) { |
| 253 |
goto err; |
| 254 |
} |
| 255 |
|
| 256 |
CBS_init(out, data, len); |
| 257 |
*out_storage = data; |
| 258 |
return 1; |
| 259 |
|
| 260 |
err: |
| 261 |
CBB_cleanup(&result); |
| 262 |
return 0; |
| 263 |
} |