summaryrefslogtreecommitdiff
path: root/lib/libcrypto/bytestring/bs_ber.c
blob: eb1eed6185b718e29fe9f797de0759935c582c50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*	$OpenBSD: bs_ber.c,v 1.3 2024/05/25 15:12:47 tb Exp $	*/
/*
 * Copyright (c) 2014, Google Inc.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdint.h>
#include <string.h>

#include "bytestring.h"

/*
 * kMaxDepth is a just a sanity limit. The code should be such that the length
 * of the input being processes always decreases. None the less, a very large
 * input could otherwise cause the stack to overflow.
 */
static const unsigned int kMaxDepth = 2048;

/* Non-strict version that allows a relaxed DER with indefinite form. */
static int
cbs_nonstrict_get_any_asn1_element(CBS *cbs, CBS *out, unsigned int *out_tag,
    size_t *out_header_len)
{
	return cbs_get_any_asn1_element_internal(cbs, out,
	    out_tag, out_header_len, 0);
}

/*
 * cbs_find_indefinite walks an ASN.1 structure in |orig_in| and sets
 * |*indefinite_found| depending on whether an indefinite length element was
 * found. The value of |orig_in| is not modified.
 *
 * Returns one on success (i.e. |*indefinite_found| was set) and zero on error.
 */
static int
cbs_find_indefinite(const CBS *orig_in, char *indefinite_found,
    unsigned int depth)
{
	CBS in;

	if (depth > kMaxDepth)
		return 0;

	CBS_init(&in, CBS_data(orig_in), CBS_len(orig_in));

	while (CBS_len(&in) > 0) {
		CBS contents;
		unsigned int tag;
		size_t header_len;

		if (!cbs_nonstrict_get_any_asn1_element(&in, &contents, &tag,
		    &header_len))
			return 0;

		/* Indefinite form not allowed by DER. */
		if (CBS_len(&contents) == header_len && header_len > 0 &&
		    CBS_data(&contents)[header_len - 1] == 0x80) {
			*indefinite_found = 1;
			return 1;
		}
		if (tag & CBS_ASN1_CONSTRUCTED) {
			if (!CBS_skip(&contents, header_len) ||
			    !cbs_find_indefinite(&contents, indefinite_found,
			    depth + 1))
				return 0;
		}
	}

	*indefinite_found = 0;
	return 1;
}

/*
 * is_primitive_type returns true if |tag| likely a primitive type. Normally
 * one can just test the "constructed" bit in the tag but, in BER, even
 * primitive tags can have the constructed bit if they have indefinite
 * length.
 */
static char
is_primitive_type(unsigned int tag)
{
	return (tag & 0xc0) == 0 &&
	    (tag & 0x1f) != (CBS_ASN1_SEQUENCE & 0x1f) &&
	    (tag & 0x1f) != (CBS_ASN1_SET & 0x1f);
}

/*
 * is_eoc returns true if |header_len| and |contents|, as returned by
 * |cbs_nonstrict_get_any_asn1_element|, indicate an "end of contents" (EOC)
 * value.
 */
static char
is_eoc(size_t header_len, CBS *contents)
{
	const unsigned char eoc[] = {0x0, 0x0};

	return header_len == 2 && CBS_mem_equal(contents, eoc, 2);
}

/*
 * cbs_convert_indefinite reads data with DER encoding (but relaxed to allow
 * indefinite form) from |in| and writes definite form DER data to |out|. If
 * |squash_header| is set then the top-level of elements from |in| will not
 * have their headers written. This is used when concatenating the fragments of
 * an indefinite length, primitive value. If |looking_for_eoc| is set then any
 * EOC elements found will cause the function to return after consuming it.
 * It returns one on success and zero on error.
 */
static int
cbs_convert_indefinite(CBS *in, CBB *out, char squash_header,
    char looking_for_eoc, unsigned int depth)
{
	if (depth > kMaxDepth)
		return 0;

	while (CBS_len(in) > 0) {
		CBS contents;
		unsigned int tag;
		size_t header_len;
		CBB *out_contents, out_contents_storage;

		if (!cbs_nonstrict_get_any_asn1_element(in, &contents, &tag,
		    &header_len))
			return 0;

		out_contents = out;

		if (CBS_len(&contents) == header_len) {
			if (is_eoc(header_len, &contents))
				return looking_for_eoc;

			if (header_len > 0 &&
			    CBS_data(&contents)[header_len - 1] == 0x80) {
				/*
				 * This is an indefinite length element. If
				 * it's a SEQUENCE or SET then we just need to
				 * write the out the contents as normal, but
				 * with a concrete length prefix.
				 *
				 * If it's a something else then the contents
				 * will be a series of DER elements of the same
				 * type which need to be concatenated.
				 */
				const char context_specific = (tag & 0xc0)
				    == 0x80;
				char squash_child_headers =
				    is_primitive_type(tag);

				/*
				 * This is a hack, but it sufficies to handle
				 * NSS's output. If we find an indefinite
				 * length, context-specific tag with a definite,
				 * primtive tag inside it, then we assume that
				 * the context-specific tag is implicit and the
				 * tags within are fragments of a primitive type
				 * that need to be concatenated.
				 */
				if (context_specific &&
				    (tag & CBS_ASN1_CONSTRUCTED)) {
					CBS in_copy, inner_contents;
					unsigned int inner_tag;
					size_t inner_header_len;

					CBS_init(&in_copy, CBS_data(in),
					    CBS_len(in));
					if (!cbs_nonstrict_get_any_asn1_element(
					    &in_copy, &inner_contents,
					    &inner_tag, &inner_header_len))
						return 0;

					if (CBS_len(&inner_contents) >
					    inner_header_len &&
					    is_primitive_type(inner_tag))
						squash_child_headers = 1;
				}

				if (!squash_header) {
					unsigned int out_tag = tag;

					if (squash_child_headers)
						out_tag &=
						    ~CBS_ASN1_CONSTRUCTED;

					if (!CBB_add_asn1(out,
					    &out_contents_storage, out_tag))
						return 0;

					out_contents = &out_contents_storage;
				}

				if (!cbs_convert_indefinite(in, out_contents,
				    squash_child_headers,
				    1 /* looking for eoc */, depth + 1))
					return 0;

				if (out_contents != out && !CBB_flush(out))
					return 0;

				continue;
			}
		}

		if (!squash_header) {
			if (!CBB_add_asn1(out, &out_contents_storage, tag))
				return 0;

			out_contents = &out_contents_storage;
		}

		if (!CBS_skip(&contents, header_len))
			return 0;

		if (tag & CBS_ASN1_CONSTRUCTED) {
			if (!cbs_convert_indefinite(&contents, out_contents,
			    0 /* don't squash header */,
			    0 /* not looking for eoc */, depth + 1))
				return 0;
		} else {
			if (!CBB_add_bytes(out_contents, CBS_data(&contents),
			    CBS_len(&contents)))
				return 0;
		}

		if (out_contents != out && !CBB_flush(out))
			return 0;
	}

	return looking_for_eoc == 0;
}

int
CBS_asn1_indefinite_to_definite(CBS *in, uint8_t **out, size_t *out_len)
{
	CBB cbb;

	/*
	 * First, do a quick walk to find any indefinite-length elements. Most
	 * of the time we hope that there aren't any and thus we can quickly
	 * return.
	 */
	char conversion_needed;
	if (!cbs_find_indefinite(in, &conversion_needed, 0))
		return 0;

	if (!conversion_needed) {
		*out = NULL;
		*out_len = 0;
		return 1;
	}

	if (!CBB_init(&cbb, CBS_len(in)))
		return 0;
	if (!cbs_convert_indefinite(in, &cbb, 0, 0, 0)) {
		CBB_cleanup(&cbb);
		return 0;
	}

	return CBB_finish(&cbb, out, out_len);
}