summaryrefslogtreecommitdiff
path: root/src/lib/libssl/bs_ber.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/bs_ber.c')
-rw-r--r--src/lib/libssl/bs_ber.c220
1 files changed, 220 insertions, 0 deletions
diff --git a/src/lib/libssl/bs_ber.c b/src/lib/libssl/bs_ber.c
new file mode 100644
index 0000000000..b94b63e37e
--- /dev/null
+++ b/src/lib/libssl/bs_ber.c
@@ -0,0 +1,220 @@
1/* $OpenBSD: bs_ber.c,v 1.1 2015/02/06 09:36:16 doug Exp $ */
2/*
3 * Copyright (c) 2014, Google Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
14 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
15 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
16
17#include <string.h>
18
19#include <openssl/opensslconf.h>
20
21#include "bytestring.h"
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. */
26static const unsigned kMaxDepth = 2048;
27
28/* cbs_find_ber walks an ASN.1 structure in |orig_in| and sets |*ber_found|
29 * depending on whether an indefinite length element was found. The value of
30 * |in| is not changed. It returns one on success (i.e. |*ber_found| was set)
31 * and zero on error. */
32static int cbs_find_ber(CBS *orig_in, char *ber_found, unsigned depth) {
33 CBS in;
34
35 if (depth > kMaxDepth) {
36 return 0;
37 }
38
39 CBS_init(&in, CBS_data(orig_in), CBS_len(orig_in));
40 *ber_found = 0;
41
42 while (CBS_len(&in) > 0) {
43 CBS contents;
44 unsigned tag;
45 size_t header_len;
46
47 if (!CBS_get_any_asn1_element(&in, &contents, &tag, &header_len)) {
48 return 0;
49 }
50 if (CBS_len(&contents) == header_len &&
51 header_len > 0 &&
52 CBS_data(&contents)[header_len-1] == 0x80) {
53 *ber_found = 1;
54 return 1;
55 }
56 if (tag & CBS_ASN1_CONSTRUCTED) {
57 if (!CBS_skip(&contents, header_len) ||
58 !cbs_find_ber(&contents, ber_found, depth + 1)) {
59 return 0;
60 }
61 }
62 }
63
64 return 1;
65}
66
67/* is_primitive_type returns true if |tag| likely a primitive type. Normally
68 * one can just test the "constructed" bit in the tag but, in BER, even
69 * primitive tags can have the constructed bit if they have indefinite
70 * length. */
71static char is_primitive_type(unsigned tag) {
72 return (tag & 0xc0) == 0 &&
73 (tag & 0x1f) != (CBS_ASN1_SEQUENCE & 0x1f) &&
74 (tag & 0x1f) != (CBS_ASN1_SET & 0x1f);
75}
76
77/* is_eoc returns true if |header_len| and |contents|, as returned by
78 * |CBS_get_any_asn1_element|, indicate an "end of contents" (EOC) value. */
79static char is_eoc(size_t header_len, CBS *contents) {
80 return header_len == 2 && CBS_len(contents) == 2 &&
81 memcmp(CBS_data(contents), "\x00\x00", 2) == 0;
82}
83
84/* cbs_convert_ber reads BER data from |in| and writes DER data to |out|. If
85 * |squash_header| is set then the top-level of elements from |in| will not
86 * have their headers written. This is used when concatenating the fragments of
87 * an indefinite length, primitive value. If |looking_for_eoc| is set then any
88 * EOC elements found will cause the function to return after consuming it.
89 * It returns one on success and zero on error. */
90static int cbs_convert_ber(CBS *in, CBB *out, char squash_header,
91 char looking_for_eoc, unsigned depth) {
92 if (depth > kMaxDepth) {
93 return 0;
94 }
95
96 while (CBS_len(in) > 0) {
97 CBS contents;
98 unsigned tag;
99 size_t header_len;
100 CBB *out_contents, out_contents_storage;
101
102 if (!CBS_get_any_asn1_element(in, &contents, &tag, &header_len)) {
103 return 0;
104 }
105 out_contents = out;
106
107 if (CBS_len(&contents) == header_len) {
108 if (is_eoc(header_len, &contents)) {
109 return looking_for_eoc;
110 }
111
112 if (header_len > 0 && CBS_data(&contents)[header_len - 1] == 0x80) {
113 /* This is an indefinite length element. If it's a SEQUENCE or SET then
114 * we just need to write the out the contents as normal, but with a
115 * concrete length prefix.
116 *
117 * If it's a something else then the contents will be a series of BER
118 * elements of the same type which need to be concatenated. */
119 const char context_specific = (tag & 0xc0) == 0x80;
120 char squash_child_headers = is_primitive_type(tag);
121
122 /* This is a hack, but it sufficies to handle NSS's output. If we find
123 * an indefinite length, context-specific tag with a definite, primtive
124 * tag inside it, then we assume that the context-specific tag is
125 * implicit and the tags within are fragments of a primitive type that
126 * need to be concatenated. */
127 if (context_specific && (tag & CBS_ASN1_CONSTRUCTED)) {
128 CBS in_copy, inner_contents;
129 unsigned inner_tag;
130 size_t inner_header_len;
131
132 CBS_init(&in_copy, CBS_data(in), CBS_len(in));
133 if (!CBS_get_any_asn1_element(&in_copy, &inner_contents, &inner_tag,
134 &inner_header_len)) {
135 return 0;
136 }
137 if (CBS_len(&inner_contents) > inner_header_len &&
138 is_primitive_type(inner_tag)) {
139 squash_child_headers = 1;
140 }
141 }
142
143 if (!squash_header) {
144 unsigned out_tag = tag;
145 if (squash_child_headers) {
146 out_tag &= ~CBS_ASN1_CONSTRUCTED;
147 }
148 if (!CBB_add_asn1(out, &out_contents_storage, out_tag)) {
149 return 0;
150 }
151 out_contents = &out_contents_storage;
152 }
153
154 if (!cbs_convert_ber(in, out_contents,
155 squash_child_headers,
156 1 /* looking for eoc */, depth + 1)) {
157 return 0;
158 }
159 if (out_contents != out && !CBB_flush(out)) {
160 return 0;
161 }
162 continue;
163 }
164 }
165
166 if (!squash_header) {
167 if (!CBB_add_asn1(out, &out_contents_storage, tag)) {
168 return 0;
169 }
170 out_contents = &out_contents_storage;
171 }
172
173 if (!CBS_skip(&contents, header_len)) {
174 return 0;
175 }
176
177 if (tag & CBS_ASN1_CONSTRUCTED) {
178 if (!cbs_convert_ber(&contents, out_contents, 0 /* don't squash header */,
179 0 /* not looking for eoc */, depth + 1)) {
180 return 0;
181 }
182 } else {
183 if (!CBB_add_bytes(out_contents, CBS_data(&contents),
184 CBS_len(&contents))) {
185 return 0;
186 }
187 }
188
189 if (out_contents != out && !CBB_flush(out)) {
190 return 0;
191 }
192 }
193
194 return looking_for_eoc == 0;
195}
196
197int CBS_asn1_ber_to_der(CBS *in, uint8_t **out, size_t *out_len) {
198 CBB cbb;
199
200 /* First, do a quick walk to find any indefinite-length elements. Most of the
201 * time we hope that there aren't any and thus we can quickly return. */
202 char conversion_needed;
203 if (!cbs_find_ber(in, &conversion_needed, 0)) {
204 return 0;
205 }
206
207 if (!conversion_needed) {
208 *out = NULL;
209 *out_len = 0;
210 return 1;
211 }
212
213 CBB_init(&cbb, CBS_len(in));
214 if (!cbs_convert_ber(in, &cbb, 0, 0, 0)) {
215 CBB_cleanup(&cbb);
216 return 0;
217 }
218
219 return CBB_finish(&cbb, out, out_len);
220}