/src/boringssl/crypto/bytestring/ber.cc
Line | Count | Source |
1 | | // Copyright 2014 The BoringSSL Authors |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <openssl/bytestring.h> |
16 | | |
17 | | #include <assert.h> |
18 | | #include <string.h> |
19 | | |
20 | | #include "internal.h" |
21 | | |
22 | | |
23 | | using namespace bssl; |
24 | | |
25 | | // kMaxDepth limits the recursion depth to avoid overflowing the stack. |
26 | | static const uint32_t kMaxDepth = 128; |
27 | | |
28 | | // is_string_type returns one if `tag` is a string type and zero otherwise. It |
29 | | // ignores the constructed bit. |
30 | 64.0k | static int is_string_type(CBS_ASN1_TAG tag) { |
31 | | // While BER supports constructed BIT STRINGS, OpenSSL misparses them. To |
32 | | // avoid acting on an ambiguous input, we do not support constructed BIT |
33 | | // STRINGS. See https://github.com/openssl/openssl/issues/12810. |
34 | 64.0k | switch (tag & ~CBS_ASN1_CONSTRUCTED) { |
35 | 1.54k | case CBS_ASN1_OCTETSTRING: |
36 | 1.71k | case CBS_ASN1_UTF8STRING: |
37 | 5.83k | case CBS_ASN1_NUMERICSTRING: |
38 | 6.98k | case CBS_ASN1_PRINTABLESTRING: |
39 | 9.04k | case CBS_ASN1_T61STRING: |
40 | 11.2k | case CBS_ASN1_VIDEOTEXSTRING: |
41 | 11.7k | case CBS_ASN1_IA5STRING: |
42 | 13.7k | case CBS_ASN1_GRAPHICSTRING: |
43 | 14.0k | case CBS_ASN1_VISIBLESTRING: |
44 | 14.3k | case CBS_ASN1_GENERALSTRING: |
45 | 14.4k | case CBS_ASN1_UNIVERSALSTRING: |
46 | 14.7k | case CBS_ASN1_BMPSTRING: |
47 | 14.7k | return 1; |
48 | 49.2k | default: |
49 | 49.2k | return 0; |
50 | 64.0k | } |
51 | 64.0k | } |
52 | | |
53 | | // cbs_find_ber walks an ASN.1 structure in `orig_in` and sets `*ber_found` |
54 | | // depending on whether an indefinite length element or constructed string was |
55 | | // found. The value of `orig_in` is not changed. It returns one on success (i.e. |
56 | | // `*ber_found` was set) and zero on error. |
57 | 10.6k | static int cbs_find_ber(const CBS *orig_in, int *ber_found, uint32_t depth) { |
58 | 10.6k | if (depth > kMaxDepth) { |
59 | 0 | return 0; |
60 | 0 | } |
61 | | |
62 | 10.6k | CBS in = *orig_in; |
63 | 10.6k | *ber_found = 0; |
64 | | |
65 | 24.5k | while (CBS_len(&in) > 0) { |
66 | 18.7k | CBS contents; |
67 | 18.7k | CBS_ASN1_TAG tag; |
68 | 18.7k | size_t header_len; |
69 | 18.7k | int indefinite; |
70 | 18.7k | if (!CBS_get_any_ber_asn1_element(&in, &contents, &tag, &header_len, |
71 | 18.7k | ber_found, &indefinite)) { |
72 | 1.16k | return 0; |
73 | 1.16k | } |
74 | 17.5k | if (*ber_found) { |
75 | 1.49k | return 1; |
76 | 1.49k | } |
77 | 16.1k | if (tag & CBS_ASN1_CONSTRUCTED) { |
78 | 6.86k | if (is_string_type(tag)) { |
79 | | // Constructed strings are only legal in BER and require conversion. |
80 | 505 | *ber_found = 1; |
81 | 505 | return 1; |
82 | 505 | } |
83 | 6.36k | if (!CBS_skip(&contents, header_len) || |
84 | 6.36k | !cbs_find_ber(&contents, ber_found, depth + 1)) { |
85 | 394 | return 0; |
86 | 394 | } |
87 | 5.96k | if (*ber_found) { |
88 | | // We already found BER. No need to continue parsing. |
89 | 1.32k | return 1; |
90 | 1.32k | } |
91 | 5.96k | } |
92 | 16.1k | } |
93 | | |
94 | 5.74k | return 1; |
95 | 10.6k | } |
96 | | |
97 | | // cbs_get_eoc returns one if `cbs` begins with an "end of contents" (EOC) value |
98 | | // and zero otherwise. If an EOC was found, it advances `cbs` past it. |
99 | 1.54M | static int cbs_get_eoc(CBS *cbs) { |
100 | 1.54M | if (CBS_len(cbs) >= 2 && |
101 | 1.54M | CBS_data(cbs)[0] == 0 && CBS_data(cbs)[1] == 0) { |
102 | 15.8k | return CBS_skip(cbs, 2); |
103 | 15.8k | } |
104 | 1.53M | return 0; |
105 | 1.54M | } |
106 | | |
107 | | // cbs_convert_ber reads BER data from `in` and writes DER data to `out`. If |
108 | | // `string_tag` is non-zero, then all elements must match `string_tag` up to the |
109 | | // constructed bit and primitive element bodies are written to `out` without |
110 | | // element headers. This is used when concatenating the fragments of a |
111 | | // constructed string. If `looking_for_eoc` is set then any EOC elements found |
112 | | // will cause the function to return after consuming it. It returns one on |
113 | | // success and zero on error. |
114 | | static int cbs_convert_ber(CBS *in, CBB *out, CBS_ASN1_TAG string_tag, |
115 | 60.0k | int looking_for_eoc, uint32_t depth) { |
116 | 60.0k | assert(!(string_tag & CBS_ASN1_CONSTRUCTED)); |
117 | | |
118 | 60.0k | if (depth > kMaxDepth) { |
119 | 4 | return 0; |
120 | 4 | } |
121 | | |
122 | 1.63M | while (CBS_len(in) > 0) { |
123 | 1.60M | if (looking_for_eoc && cbs_get_eoc(in)) { |
124 | 15.8k | return 1; |
125 | 15.8k | } |
126 | | |
127 | 1.58M | CBS contents; |
128 | 1.58M | CBS_ASN1_TAG tag, child_string_tag = string_tag; |
129 | 1.58M | size_t header_len; |
130 | 1.58M | int indefinite; |
131 | 1.58M | CBB *out_contents, out_contents_storage; |
132 | 1.58M | if (!CBS_get_any_ber_asn1_element(in, &contents, &tag, &header_len, |
133 | 1.58M | /*out_ber_found=*/nullptr, &indefinite)) { |
134 | 424 | return 0; |
135 | 424 | } |
136 | | |
137 | 1.58M | if (string_tag != 0) { |
138 | | // This is part of a constructed string. All elements must match |
139 | | // `string_tag` up to the constructed bit and get appended to `out` |
140 | | // without a child element. |
141 | 24.0k | if ((tag & ~CBS_ASN1_CONSTRUCTED) != string_tag) { |
142 | 121 | return 0; |
143 | 121 | } |
144 | 23.9k | out_contents = out; |
145 | 1.56M | } else { |
146 | 1.56M | CBS_ASN1_TAG out_tag = tag; |
147 | 1.56M | if ((tag & CBS_ASN1_CONSTRUCTED) && is_string_type(tag)) { |
148 | | // If a constructed string, clear the constructed bit and inform |
149 | | // children to concatenate bodies. |
150 | 14.2k | out_tag &= ~CBS_ASN1_CONSTRUCTED; |
151 | 14.2k | child_string_tag = out_tag; |
152 | 14.2k | } |
153 | 1.56M | if (!CBB_add_asn1(out, &out_contents_storage, out_tag)) { |
154 | 0 | return 0; |
155 | 0 | } |
156 | 1.56M | out_contents = &out_contents_storage; |
157 | 1.56M | } |
158 | | |
159 | 1.58M | if (indefinite) { |
160 | 21.8k | if (!cbs_convert_ber(in, out_contents, child_string_tag, |
161 | 21.8k | /*looking_for_eoc=*/1, depth + 1) || |
162 | 15.8k | !CBB_flush(out)) { |
163 | 5.94k | return 0; |
164 | 5.94k | } |
165 | 15.8k | continue; |
166 | 21.8k | } |
167 | | |
168 | 1.56M | if (!CBS_skip(&contents, header_len)) { |
169 | 0 | return 0; |
170 | 0 | } |
171 | | |
172 | 1.56M | if (tag & CBS_ASN1_CONSTRUCTED) { |
173 | | // Recurse into children. |
174 | 36.2k | if (!cbs_convert_ber(&contents, out_contents, child_string_tag, |
175 | 36.2k | /*looking_for_eoc=*/0, depth + 1)) { |
176 | 1.54k | return 0; |
177 | 1.54k | } |
178 | 1.52M | } else { |
179 | | // Copy primitive contents as-is. |
180 | 1.52M | if (!CBB_add_bytes(out_contents, CBS_data(&contents), |
181 | 1.52M | CBS_len(&contents))) { |
182 | 0 | return 0; |
183 | 0 | } |
184 | 1.52M | } |
185 | | |
186 | 1.56M | if (!CBB_flush(out)) { |
187 | 0 | return 0; |
188 | 0 | } |
189 | 1.56M | } |
190 | | |
191 | 36.1k | return looking_for_eoc == 0; |
192 | 60.0k | } |
193 | | |
194 | 4.26k | int bssl::CBS_asn1_ber_to_der(CBS *in, CBS *out, uint8_t **out_storage) { |
195 | 4.26k | 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 | 4.26k | int conversion_needed; |
200 | 4.26k | if (!cbs_find_ber(in, &conversion_needed, 0)) { |
201 | 1.16k | return 0; |
202 | 1.16k | } |
203 | | |
204 | 3.09k | if (!conversion_needed) { |
205 | 1.09k | if (!CBS_get_any_asn1_element(in, out, nullptr, nullptr)) { |
206 | 0 | return 0; |
207 | 0 | } |
208 | 1.09k | *out_storage = nullptr; |
209 | 1.09k | return 1; |
210 | 1.09k | } |
211 | | |
212 | 1.99k | size_t len; |
213 | 1.99k | if (!CBB_init(&cbb, CBS_len(in)) || |
214 | 1.99k | !cbs_convert_ber(in, &cbb, 0, 0, 0) || |
215 | 1.22k | !CBB_finish(&cbb, out_storage, &len)) { |
216 | 769 | CBB_cleanup(&cbb); |
217 | 769 | return 0; |
218 | 769 | } |
219 | | |
220 | 1.22k | CBS_init(out, *out_storage, len); |
221 | 1.22k | return 1; |
222 | 1.99k | } |
223 | | |
224 | | int bssl::CBS_get_asn1_implicit_string(CBS *in, CBS *out, uint8_t **out_storage, |
225 | | CBS_ASN1_TAG outer_tag, |
226 | 8 | CBS_ASN1_TAG inner_tag) { |
227 | 8 | assert(!(outer_tag & CBS_ASN1_CONSTRUCTED)); |
228 | 8 | assert(!(inner_tag & CBS_ASN1_CONSTRUCTED)); |
229 | 8 | assert(is_string_type(inner_tag)); |
230 | | |
231 | 8 | if (CBS_peek_asn1_tag(in, outer_tag)) { |
232 | | // Normal implicitly-tagged string. |
233 | 7 | *out_storage = nullptr; |
234 | 7 | return CBS_get_asn1(in, out, outer_tag); |
235 | 7 | } |
236 | | |
237 | | // Otherwise, try to parse an implicitly-tagged constructed string. |
238 | | // `CBS_asn1_ber_to_der` is assumed to have run, so only allow one level deep |
239 | | // of nesting. |
240 | 1 | CBB result; |
241 | 1 | CBS child; |
242 | 1 | if (!CBB_init(&result, CBS_len(in)) || |
243 | 1 | !CBS_get_asn1(in, &child, outer_tag | CBS_ASN1_CONSTRUCTED)) { |
244 | 0 | goto err; |
245 | 0 | } |
246 | | |
247 | 3 | while (CBS_len(&child) > 0) { |
248 | 2 | CBS chunk; |
249 | 2 | if (!CBS_get_asn1(&child, &chunk, inner_tag) || |
250 | 2 | !CBB_add_bytes(&result, CBS_data(&chunk), CBS_len(&chunk))) { |
251 | 0 | goto err; |
252 | 0 | } |
253 | 2 | } |
254 | | |
255 | 1 | uint8_t *data; |
256 | 1 | size_t len; |
257 | 1 | if (!CBB_finish(&result, &data, &len)) { |
258 | 0 | goto err; |
259 | 0 | } |
260 | | |
261 | 1 | CBS_init(out, data, len); |
262 | 1 | *out_storage = data; |
263 | 1 | return 1; |
264 | | |
265 | 0 | err: |
266 | 0 | CBB_cleanup(&result); |
267 | 0 | return 0; |
268 | 1 | } |