blob: ed54b499518519147634065fb1d5bb80b498d5b6 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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/buf.h>
16#include <openssl/mem.h>
17#include <openssl/bytestring.h>
18
19#include <assert.h>
20#include <string.h>
21
22#include "internal.h"
23
24
25void CBS_init(CBS *cbs, const uint8_t *data, size_t len) {
26 cbs->data = data;
27 cbs->len = len;
28}
29
30static int cbs_get(CBS *cbs, const uint8_t **p, size_t n) {
31 if (cbs->len < n) {
32 return 0;
33 }
34
35 *p = cbs->data;
36 cbs->data += n;
37 cbs->len -= n;
38 return 1;
39}
40
41int CBS_skip(CBS *cbs, size_t len) {
42 const uint8_t *dummy;
43 return cbs_get(cbs, &dummy, len);
44}
45
46const uint8_t *CBS_data(const CBS *cbs) {
47 return cbs->data;
48}
49
50size_t CBS_len(const CBS *cbs) {
51 return cbs->len;
52}
53
54int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len) {
Adam Langleye9ada862015-05-11 17:20:37 -070055 OPENSSL_free(*out_ptr);
56 *out_ptr = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -080057 *out_len = 0;
58
59 if (cbs->len == 0) {
60 return 1;
61 }
62 *out_ptr = BUF_memdup(cbs->data, cbs->len);
63 if (*out_ptr == NULL) {
64 return 0;
65 }
66 *out_len = cbs->len;
67 return 1;
68}
69
70int CBS_strdup(const CBS *cbs, char **out_ptr) {
71 if (*out_ptr != NULL) {
72 OPENSSL_free(*out_ptr);
73 }
74 *out_ptr = BUF_strndup((const char*)cbs->data, cbs->len);
75 return (*out_ptr != NULL);
76}
77
78int CBS_contains_zero_byte(const CBS *cbs) {
79 return memchr(cbs->data, 0, cbs->len) != NULL;
80}
81
82int CBS_mem_equal(const CBS *cbs, const uint8_t *data, size_t len) {
Adam Langleye9ada862015-05-11 17:20:37 -070083 if (len != cbs->len) {
Adam Langleyd9e397b2015-01-22 14:27:53 -080084 return 0;
Adam Langleye9ada862015-05-11 17:20:37 -070085 }
Adam Langleyd9e397b2015-01-22 14:27:53 -080086 return CRYPTO_memcmp(cbs->data, data, len) == 0;
87}
88
89static int cbs_get_u(CBS *cbs, uint32_t *out, size_t len) {
90 uint32_t result = 0;
91 size_t i;
92 const uint8_t *data;
93
94 if (!cbs_get(cbs, &data, len)) {
95 return 0;
96 }
97 for (i = 0; i < len; i++) {
98 result <<= 8;
99 result |= data[i];
100 }
101 *out = result;
102 return 1;
103}
104
105int CBS_get_u8(CBS *cbs, uint8_t *out) {
106 const uint8_t *v;
107 if (!cbs_get(cbs, &v, 1)) {
108 return 0;
109 }
110 *out = *v;
111 return 1;
112}
113
114int CBS_get_u16(CBS *cbs, uint16_t *out) {
115 uint32_t v;
116 if (!cbs_get_u(cbs, &v, 2)) {
117 return 0;
118 }
119 *out = v;
120 return 1;
121}
122
123int CBS_get_u24(CBS *cbs, uint32_t *out) {
124 return cbs_get_u(cbs, out, 3);
125}
126
127int CBS_get_u32(CBS *cbs, uint32_t *out) {
128 return cbs_get_u(cbs, out, 4);
129}
130
131int CBS_get_bytes(CBS *cbs, CBS *out, size_t len) {
132 const uint8_t *v;
133 if (!cbs_get(cbs, &v, len)) {
134 return 0;
135 }
136 CBS_init(out, v, len);
137 return 1;
138}
139
Kenny Rootb8494592015-09-25 02:29:14 +0000140int CBS_copy_bytes(CBS *cbs, uint8_t *out, size_t len) {
141 const uint8_t *v;
142 if (!cbs_get(cbs, &v, len)) {
143 return 0;
144 }
145 memcpy(out, v, len);
146 return 1;
147}
148
Adam Langleyd9e397b2015-01-22 14:27:53 -0800149static int cbs_get_length_prefixed(CBS *cbs, CBS *out, size_t len_len) {
150 uint32_t len;
151 if (!cbs_get_u(cbs, &len, len_len)) {
152 return 0;
153 }
154 return CBS_get_bytes(cbs, out, len);
155}
156
157int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out) {
158 return cbs_get_length_prefixed(cbs, out, 1);
159}
160
161int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out) {
162 return cbs_get_length_prefixed(cbs, out, 2);
163}
164
165int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) {
166 return cbs_get_length_prefixed(cbs, out, 3);
167}
168
Adam Langleyf4e42722015-06-04 17:45:09 -0700169static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
170 size_t *out_header_len, int ber_ok) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800171 uint8_t tag, length_byte;
172 CBS header = *cbs;
173 CBS throwaway;
174
175 if (out == NULL) {
176 out = &throwaway;
177 }
178
179 if (!CBS_get_u8(&header, &tag) ||
180 !CBS_get_u8(&header, &length_byte)) {
181 return 0;
182 }
183
David Benjamin4969cc92016-04-22 15:02:23 -0400184 /* ITU-T X.690 section 8.1.2.3 specifies the format for identifiers with a tag
185 * number no greater than 30.
186 *
187 * If the number portion is 31 (0x1f, the largest value that fits in the
188 * allotted bits), then the tag is more than one byte long and the
189 * continuation bytes contain the tag number. This parser only supports tag
190 * numbers less than 31 (and thus single-byte tags). */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800191 if ((tag & 0x1f) == 0x1f) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800192 return 0;
193 }
194
195 if (out_tag != NULL) {
196 *out_tag = tag;
197 }
198
199 size_t len;
David Benjamin4969cc92016-04-22 15:02:23 -0400200 /* The format for the length encoding is specified in ITU-T X.690 section
201 * 8.1.3. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800202 if ((length_byte & 0x80) == 0) {
203 /* Short form length. */
204 len = ((size_t) length_byte) + 2;
205 if (out_header_len != NULL) {
206 *out_header_len = 2;
207 }
208 } else {
David Benjamin4969cc92016-04-22 15:02:23 -0400209 /* The high bit indicate that this is the long form, while the next 7 bits
210 * encode the number of subsequent octets used to encode the length (ITU-T
211 * X.690 clause 8.1.3.5.b). */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800212 const size_t num_bytes = length_byte & 0x7f;
213 uint32_t len32;
214
Adam Langleyf4e42722015-06-04 17:45:09 -0700215 if (ber_ok && (tag & CBS_ASN1_CONSTRUCTED) != 0 && num_bytes == 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800216 /* indefinite length */
Adam Langleyf4e42722015-06-04 17:45:09 -0700217 if (out_header_len != NULL) {
218 *out_header_len = 2;
219 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800220 return CBS_get_bytes(cbs, out, 2);
221 }
222
David Benjamin4969cc92016-04-22 15:02:23 -0400223 /* ITU-T X.690 clause 8.1.3.5.c specifies that the value 0xff shall not be
224 * used as the first byte of the length. If this parser encounters that
225 * value, num_bytes will be parsed as 127, which will fail the check below.
226 */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800227 if (num_bytes == 0 || num_bytes > 4) {
228 return 0;
229 }
230 if (!cbs_get_u(&header, &len32, num_bytes)) {
231 return 0;
232 }
David Benjamin4969cc92016-04-22 15:02:23 -0400233 /* ITU-T X.690 section 10.1 (DER length forms) requires encoding the length
234 * with the minimum number of octets. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800235 if (len32 < 128) {
236 /* Length should have used short-form encoding. */
237 return 0;
238 }
239 if ((len32 >> ((num_bytes-1)*8)) == 0) {
240 /* Length should have been at least one byte shorter. */
241 return 0;
242 }
243 len = len32;
244 if (len + 2 + num_bytes < len) {
245 /* Overflow. */
246 return 0;
247 }
248 len += 2 + num_bytes;
249 if (out_header_len != NULL) {
250 *out_header_len = 2 + num_bytes;
251 }
252 }
253
254 return CBS_get_bytes(cbs, out, len);
255}
256
Adam Langleyf4e42722015-06-04 17:45:09 -0700257int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
258 size_t *out_header_len) {
259 return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len,
260 0 /* DER only */);
261}
262
263int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
264 size_t *out_header_len) {
265 return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len,
266 1 /* BER allowed */);
267}
268
Adam Langleyd9e397b2015-01-22 14:27:53 -0800269static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value,
270 int skip_header) {
271 size_t header_len;
272 unsigned tag;
273 CBS throwaway;
274
275 if (out == NULL) {
276 out = &throwaway;
277 }
278
279 if (!CBS_get_any_asn1_element(cbs, out, &tag, &header_len) ||
Adam Langleyf4e42722015-06-04 17:45:09 -0700280 tag != tag_value) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800281 return 0;
282 }
283
284 if (skip_header && !CBS_skip(out, header_len)) {
285 assert(0);
286 return 0;
287 }
288
289 return 1;
290}
291
292int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value) {
293 return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */);
294}
295
296int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value) {
297 return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */);
298}
299
300int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value) {
301 if (CBS_len(cbs) < 1) {
302 return 0;
303 }
304 return CBS_data(cbs)[0] == tag_value;
305}
306
307int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) {
308 CBS bytes;
309 const uint8_t *data;
310 size_t i, len;
311
312 if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER)) {
313 return 0;
314 }
315
316 *out = 0;
317 data = CBS_data(&bytes);
318 len = CBS_len(&bytes);
319
320 if (len == 0) {
321 /* An INTEGER is encoded with at least one octet. */
322 return 0;
323 }
324
325 if ((data[0] & 0x80) != 0) {
Adam Langleye9ada862015-05-11 17:20:37 -0700326 /* Negative number. */
327 return 0;
328 }
329
330 if (data[0] == 0 && len > 1 && (data[1] & 0x80) == 0) {
331 /* Extra leading zeros. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800332 return 0;
333 }
334
335 for (i = 0; i < len; i++) {
336 if ((*out >> 56) != 0) {
337 /* Too large to represent as a uint64_t. */
338 return 0;
339 }
340 *out <<= 8;
341 *out |= data[i];
342 }
343
344 return 1;
345}
346
347int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, unsigned tag) {
Kenny Rootb8494592015-09-25 02:29:14 +0000348 int present = 0;
349
Adam Langleyd9e397b2015-01-22 14:27:53 -0800350 if (CBS_peek_asn1_tag(cbs, tag)) {
351 if (!CBS_get_asn1(cbs, out, tag)) {
352 return 0;
353 }
Kenny Rootb8494592015-09-25 02:29:14 +0000354 present = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800355 }
Kenny Rootb8494592015-09-25 02:29:14 +0000356
357 if (out_present != NULL) {
358 *out_present = present;
359 }
360
Adam Langleyd9e397b2015-01-22 14:27:53 -0800361 return 1;
362}
363
364int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present,
365 unsigned tag) {
366 CBS child;
367 int present;
368 if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
369 return 0;
370 }
371 if (present) {
372 if (!CBS_get_asn1(&child, out, CBS_ASN1_OCTETSTRING) ||
373 CBS_len(&child) != 0) {
374 return 0;
375 }
376 } else {
377 CBS_init(out, NULL, 0);
378 }
379 if (out_present) {
380 *out_present = present;
381 }
382 return 1;
383}
384
385int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, unsigned tag,
386 uint64_t default_value) {
387 CBS child;
388 int present;
389 if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
390 return 0;
391 }
392 if (present) {
393 if (!CBS_get_asn1_uint64(&child, out) ||
394 CBS_len(&child) != 0) {
395 return 0;
396 }
397 } else {
398 *out = default_value;
399 }
400 return 1;
401}
402
403int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag,
404 int default_value) {
405 CBS child, child2;
406 int present;
407 if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
408 return 0;
409 }
410 if (present) {
411 uint8_t boolean;
412
413 if (!CBS_get_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
414 CBS_len(&child2) != 1 ||
415 CBS_len(&child) != 0) {
416 return 0;
417 }
418
419 boolean = CBS_data(&child2)[0];
420 if (boolean == 0) {
421 *out = 0;
422 } else if (boolean == 0xff) {
423 *out = 1;
424 } else {
425 return 0;
426 }
427 } else {
428 *out = default_value;
429 }
430 return 1;
431}