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