blob: c86afbda919b284ce6ee5803e814e6742453b61c [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
David Benjamin6e899c72016-06-09 18:02:18 -0400131int CBS_get_last_u8(CBS *cbs, uint8_t *out) {
132 if (cbs->len == 0) {
133 return 0;
134 }
135 *out = cbs->data[cbs->len - 1];
136 cbs->len--;
137 return 1;
138}
139
Adam Langleyd9e397b2015-01-22 14:27:53 -0800140int CBS_get_bytes(CBS *cbs, CBS *out, size_t len) {
141 const uint8_t *v;
142 if (!cbs_get(cbs, &v, len)) {
143 return 0;
144 }
145 CBS_init(out, v, len);
146 return 1;
147}
148
Kenny Rootb8494592015-09-25 02:29:14 +0000149int CBS_copy_bytes(CBS *cbs, uint8_t *out, size_t len) {
150 const uint8_t *v;
151 if (!cbs_get(cbs, &v, len)) {
152 return 0;
153 }
154 memcpy(out, v, len);
155 return 1;
156}
157
Adam Langleyd9e397b2015-01-22 14:27:53 -0800158static int cbs_get_length_prefixed(CBS *cbs, CBS *out, size_t len_len) {
159 uint32_t len;
160 if (!cbs_get_u(cbs, &len, len_len)) {
161 return 0;
162 }
163 return CBS_get_bytes(cbs, out, len);
164}
165
166int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out) {
167 return cbs_get_length_prefixed(cbs, out, 1);
168}
169
170int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out) {
171 return cbs_get_length_prefixed(cbs, out, 2);
172}
173
174int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) {
175 return cbs_get_length_prefixed(cbs, out, 3);
176}
177
Adam Langleyf4e42722015-06-04 17:45:09 -0700178static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
179 size_t *out_header_len, int ber_ok) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800180 uint8_t tag, length_byte;
181 CBS header = *cbs;
182 CBS throwaway;
183
184 if (out == NULL) {
185 out = &throwaway;
186 }
187
188 if (!CBS_get_u8(&header, &tag) ||
189 !CBS_get_u8(&header, &length_byte)) {
190 return 0;
191 }
192
David Benjamin4969cc92016-04-22 15:02:23 -0400193 /* ITU-T X.690 section 8.1.2.3 specifies the format for identifiers with a tag
194 * number no greater than 30.
195 *
196 * If the number portion is 31 (0x1f, the largest value that fits in the
197 * allotted bits), then the tag is more than one byte long and the
198 * continuation bytes contain the tag number. This parser only supports tag
199 * numbers less than 31 (and thus single-byte tags). */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800200 if ((tag & 0x1f) == 0x1f) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800201 return 0;
202 }
203
204 if (out_tag != NULL) {
205 *out_tag = tag;
206 }
207
208 size_t len;
David Benjamin4969cc92016-04-22 15:02:23 -0400209 /* The format for the length encoding is specified in ITU-T X.690 section
210 * 8.1.3. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800211 if ((length_byte & 0x80) == 0) {
212 /* Short form length. */
213 len = ((size_t) length_byte) + 2;
214 if (out_header_len != NULL) {
215 *out_header_len = 2;
216 }
217 } else {
David Benjamin4969cc92016-04-22 15:02:23 -0400218 /* The high bit indicate that this is the long form, while the next 7 bits
219 * encode the number of subsequent octets used to encode the length (ITU-T
220 * X.690 clause 8.1.3.5.b). */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800221 const size_t num_bytes = length_byte & 0x7f;
222 uint32_t len32;
223
Adam Langleyf4e42722015-06-04 17:45:09 -0700224 if (ber_ok && (tag & CBS_ASN1_CONSTRUCTED) != 0 && num_bytes == 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800225 /* indefinite length */
Adam Langleyf4e42722015-06-04 17:45:09 -0700226 if (out_header_len != NULL) {
227 *out_header_len = 2;
228 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800229 return CBS_get_bytes(cbs, out, 2);
230 }
231
David Benjamin4969cc92016-04-22 15:02:23 -0400232 /* ITU-T X.690 clause 8.1.3.5.c specifies that the value 0xff shall not be
233 * used as the first byte of the length. If this parser encounters that
234 * value, num_bytes will be parsed as 127, which will fail the check below.
235 */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800236 if (num_bytes == 0 || num_bytes > 4) {
237 return 0;
238 }
239 if (!cbs_get_u(&header, &len32, num_bytes)) {
240 return 0;
241 }
David Benjamin4969cc92016-04-22 15:02:23 -0400242 /* ITU-T X.690 section 10.1 (DER length forms) requires encoding the length
243 * with the minimum number of octets. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800244 if (len32 < 128) {
245 /* Length should have used short-form encoding. */
246 return 0;
247 }
248 if ((len32 >> ((num_bytes-1)*8)) == 0) {
249 /* Length should have been at least one byte shorter. */
250 return 0;
251 }
252 len = len32;
253 if (len + 2 + num_bytes < len) {
254 /* Overflow. */
255 return 0;
256 }
257 len += 2 + num_bytes;
258 if (out_header_len != NULL) {
259 *out_header_len = 2 + num_bytes;
260 }
261 }
262
263 return CBS_get_bytes(cbs, out, len);
264}
265
Adam Langleyf4e42722015-06-04 17:45:09 -0700266int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
267 size_t *out_header_len) {
268 return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len,
269 0 /* DER only */);
270}
271
272int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
273 size_t *out_header_len) {
274 return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len,
275 1 /* BER allowed */);
276}
277
Adam Langleyd9e397b2015-01-22 14:27:53 -0800278static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value,
279 int skip_header) {
280 size_t header_len;
281 unsigned tag;
282 CBS throwaway;
283
284 if (out == NULL) {
285 out = &throwaway;
286 }
287
288 if (!CBS_get_any_asn1_element(cbs, out, &tag, &header_len) ||
Adam Langleyf4e42722015-06-04 17:45:09 -0700289 tag != tag_value) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800290 return 0;
291 }
292
293 if (skip_header && !CBS_skip(out, header_len)) {
294 assert(0);
295 return 0;
296 }
297
298 return 1;
299}
300
301int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value) {
302 return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */);
303}
304
305int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value) {
306 return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */);
307}
308
309int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value) {
310 if (CBS_len(cbs) < 1) {
311 return 0;
312 }
313 return CBS_data(cbs)[0] == tag_value;
314}
315
316int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) {
317 CBS bytes;
318 const uint8_t *data;
319 size_t i, len;
320
321 if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER)) {
322 return 0;
323 }
324
325 *out = 0;
326 data = CBS_data(&bytes);
327 len = CBS_len(&bytes);
328
329 if (len == 0) {
330 /* An INTEGER is encoded with at least one octet. */
331 return 0;
332 }
333
334 if ((data[0] & 0x80) != 0) {
Adam Langleye9ada862015-05-11 17:20:37 -0700335 /* Negative number. */
336 return 0;
337 }
338
339 if (data[0] == 0 && len > 1 && (data[1] & 0x80) == 0) {
340 /* Extra leading zeros. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800341 return 0;
342 }
343
344 for (i = 0; i < len; i++) {
345 if ((*out >> 56) != 0) {
346 /* Too large to represent as a uint64_t. */
347 return 0;
348 }
349 *out <<= 8;
350 *out |= data[i];
351 }
352
353 return 1;
354}
355
356int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, unsigned tag) {
Kenny Rootb8494592015-09-25 02:29:14 +0000357 int present = 0;
358
Adam Langleyd9e397b2015-01-22 14:27:53 -0800359 if (CBS_peek_asn1_tag(cbs, tag)) {
360 if (!CBS_get_asn1(cbs, out, tag)) {
361 return 0;
362 }
Kenny Rootb8494592015-09-25 02:29:14 +0000363 present = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800364 }
Kenny Rootb8494592015-09-25 02:29:14 +0000365
366 if (out_present != NULL) {
367 *out_present = present;
368 }
369
Adam Langleyd9e397b2015-01-22 14:27:53 -0800370 return 1;
371}
372
373int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present,
374 unsigned tag) {
375 CBS child;
376 int present;
377 if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
378 return 0;
379 }
380 if (present) {
381 if (!CBS_get_asn1(&child, out, CBS_ASN1_OCTETSTRING) ||
382 CBS_len(&child) != 0) {
383 return 0;
384 }
385 } else {
386 CBS_init(out, NULL, 0);
387 }
388 if (out_present) {
389 *out_present = present;
390 }
391 return 1;
392}
393
394int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, unsigned tag,
395 uint64_t default_value) {
396 CBS child;
397 int present;
398 if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
399 return 0;
400 }
401 if (present) {
402 if (!CBS_get_asn1_uint64(&child, out) ||
403 CBS_len(&child) != 0) {
404 return 0;
405 }
406 } else {
407 *out = default_value;
408 }
409 return 1;
410}
411
412int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag,
413 int default_value) {
414 CBS child, child2;
415 int present;
416 if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
417 return 0;
418 }
419 if (present) {
420 uint8_t boolean;
421
422 if (!CBS_get_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
423 CBS_len(&child2) != 1 ||
424 CBS_len(&child) != 0) {
425 return 0;
426 }
427
428 boolean = CBS_data(&child2)[0];
429 if (boolean == 0) {
430 *out = 0;
431 } else if (boolean == 0xff) {
432 *out = 1;
433 } else {
434 return 0;
435 }
436 } else {
437 *out = default_value;
438 }
439 return 1;
440}