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 <string.h>
18 
19 #include "internal.h"
20 
21 
22 /* kMaxDepth is a just a sanity limit. The code should be such that the length
23  * of the input being processes always decreases. None the less, a very large
24  * input could otherwise cause the stack to overflow. */
25 static const unsigned kMaxDepth = 2048;
26 
27 /* cbs_find_ber walks an ASN.1 structure in |orig_in| and sets |*ber_found|
28  * depending on whether an indefinite length element was found. The value of
29  * |in| is not changed. It returns one on success (i.e. |*ber_found| was set)
30  * and zero on error. */
cbs_find_ber(CBS * orig_in,char * ber_found,unsigned depth)31 static int cbs_find_ber(CBS *orig_in, char *ber_found, unsigned depth) {
32   CBS in;
33 
34   if (depth > kMaxDepth) {
35     return 0;
36   }
37 
38   CBS_init(&in, CBS_data(orig_in), CBS_len(orig_in));
39   *ber_found = 0;
40 
41   while (CBS_len(&in) > 0) {
42     CBS contents;
43     unsigned tag;
44     size_t header_len;
45 
46     if (!CBS_get_any_ber_asn1_element(&in, &contents, &tag, &header_len)) {
47       return 0;
48     }
49     if (CBS_len(&contents) == header_len &&
50         header_len > 0 &&
51         CBS_data(&contents)[header_len-1] == 0x80) {
52       *ber_found = 1;
53       return 1;
54     }
55     if (tag & CBS_ASN1_CONSTRUCTED) {
56       if (!CBS_skip(&contents, header_len) ||
57           !cbs_find_ber(&contents, ber_found, depth + 1)) {
58         return 0;
59       }
60     }
61   }
62 
63   return 1;
64 }
65 
66 /* is_primitive_type returns true if |tag| likely a primitive type. Normally
67  * one can just test the "constructed" bit in the tag but, in BER, even
68  * primitive tags can have the constructed bit if they have indefinite
69  * length. */
is_primitive_type(unsigned tag)70 static char is_primitive_type(unsigned tag) {
71   return (tag & 0xc0) == 0 &&
72          (tag & 0x1f) != (CBS_ASN1_SEQUENCE & 0x1f) &&
73          (tag & 0x1f) != (CBS_ASN1_SET & 0x1f);
74 }
75 
76 /* is_eoc returns true if |header_len| and |contents|, as returned by
77  * |CBS_get_any_ber_asn1_element|, indicate an "end of contents" (EOC) value. */
is_eoc(size_t header_len,CBS * contents)78 static char is_eoc(size_t header_len, CBS *contents) {
79   return header_len == 2 && CBS_len(contents) == 2 &&
80          memcmp(CBS_data(contents), "\x00\x00", 2) == 0;
81 }
82 
83 /* cbs_convert_ber reads BER data from |in| and writes DER data to |out|. If
84  * |squash_header| is set then the top-level of elements from |in| will not
85  * have their headers written. This is used when concatenating the fragments of
86  * an indefinite length, primitive value. If |looking_for_eoc| is set then any
87  * EOC elements found will cause the function to return after consuming it.
88  * It returns one on success and zero on error. */
cbs_convert_ber(CBS * in,CBB * out,char squash_header,char looking_for_eoc,unsigned depth)89 static int cbs_convert_ber(CBS *in, CBB *out, char squash_header,
90                            char looking_for_eoc, unsigned depth) {
91   if (depth > kMaxDepth) {
92     return 0;
93   }
94 
95   while (CBS_len(in) > 0) {
96     CBS contents;
97     unsigned tag;
98     size_t header_len;
99     CBB *out_contents, out_contents_storage;
100 
101     if (!CBS_get_any_ber_asn1_element(in, &contents, &tag, &header_len)) {
102       return 0;
103     }
104     out_contents = out;
105 
106     if (CBS_len(&contents) == header_len) {
107       if (is_eoc(header_len, &contents)) {
108         return looking_for_eoc;
109       }
110 
111       if (header_len > 0 && CBS_data(&contents)[header_len - 1] == 0x80) {
112         /* This is an indefinite length element. If it's a SEQUENCE or SET then
113          * we just need to write the out the contents as normal, but with a
114          * concrete length prefix.
115          *
116          * If it's a something else then the contents will be a series of BER
117          * elements of the same type which need to be concatenated. */
118         const char context_specific = (tag & 0xc0) == 0x80;
119         char squash_child_headers = is_primitive_type(tag);
120 
121         /* This is a hack, but it sufficies to handle NSS's output. If we find
122          * an indefinite length, context-specific tag with a definite, primitive
123          * tag inside it, then we assume that the context-specific tag is
124          * implicit and the tags within are fragments of a primitive type that
125          * need to be concatenated. */
126         if (context_specific && (tag & CBS_ASN1_CONSTRUCTED)) {
127           CBS in_copy, inner_contents;
128           unsigned inner_tag;
129           size_t inner_header_len;
130 
131           CBS_init(&in_copy, CBS_data(in), CBS_len(in));
132           if (!CBS_get_any_ber_asn1_element(&in_copy, &inner_contents,
133                                             &inner_tag, &inner_header_len)) {
134             return 0;
135           }
136           if (CBS_len(&inner_contents) > inner_header_len &&
137               is_primitive_type(inner_tag)) {
138             squash_child_headers = 1;
139           }
140         }
141 
142         if (!squash_header) {
143           unsigned out_tag = tag;
144           if (squash_child_headers) {
145             out_tag &= ~CBS_ASN1_CONSTRUCTED;
146           }
147           if (!CBB_add_asn1(out, &out_contents_storage, out_tag)) {
148             return 0;
149           }
150           out_contents = &out_contents_storage;
151         }
152 
153         if (!cbs_convert_ber(in, out_contents,
154                              squash_child_headers,
155                              1 /* looking for eoc */, depth + 1)) {
156           return 0;
157         }
158         if (out_contents != out && !CBB_flush(out)) {
159           return 0;
160         }
161         continue;
162       }
163     }
164 
165     if (!squash_header) {
166       if (!CBB_add_asn1(out, &out_contents_storage, tag)) {
167         return 0;
168       }
169       out_contents = &out_contents_storage;
170     }
171 
172     if (!CBS_skip(&contents, header_len)) {
173       return 0;
174     }
175 
176     if (tag & CBS_ASN1_CONSTRUCTED) {
177       if (!cbs_convert_ber(&contents, out_contents, 0 /* don't squash header */,
178                            0 /* not looking for eoc */, depth + 1)) {
179         return 0;
180       }
181     } else {
182       if (!CBB_add_bytes(out_contents, CBS_data(&contents),
183                          CBS_len(&contents))) {
184         return 0;
185       }
186     }
187 
188     if (out_contents != out && !CBB_flush(out)) {
189       return 0;
190     }
191   }
192 
193   return looking_for_eoc == 0;
194 }
195 
CBS_asn1_ber_to_der(CBS * in,uint8_t ** out,size_t * out_len)196 int CBS_asn1_ber_to_der(CBS *in, uint8_t **out, size_t *out_len) {
197   CBB cbb;
198 
199   /* First, do a quick walk to find any indefinite-length elements. Most of the
200    * time we hope that there aren't any and thus we can quickly return. */
201   char conversion_needed;
202   if (!cbs_find_ber(in, &conversion_needed, 0)) {
203     return 0;
204   }
205 
206   if (!conversion_needed) {
207     *out = NULL;
208     *out_len = 0;
209     return 1;
210   }
211 
212   if (!CBB_init(&cbb, CBS_len(in))) {
213     return 0;
214   }
215   if (!cbs_convert_ber(in, &cbb, 0, 0, 0)) {
216     CBB_cleanup(&cbb);
217     return 0;
218   }
219 
220   return CBB_finish(&cbb, out, out_len);
221 }
222