blob: a7adc436ac3c56a8c601f55389ad80ad833f077f [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;
Adam Langleyd9e397b2015-01-22 14:27:53 -080091 const uint8_t *data;
92
93 if (!cbs_get(cbs, &data, len)) {
94 return 0;
95 }
David Benjamin7c0d06c2016-08-11 13:26:41 -040096 for (size_t i = 0; i < len; i++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -080097 result <<= 8;
98 result |= data[i];
99 }
100 *out = result;
101 return 1;
102}
103
104int CBS_get_u8(CBS *cbs, uint8_t *out) {
105 const uint8_t *v;
106 if (!cbs_get(cbs, &v, 1)) {
107 return 0;
108 }
109 *out = *v;
110 return 1;
111}
112
113int CBS_get_u16(CBS *cbs, uint16_t *out) {
114 uint32_t v;
115 if (!cbs_get_u(cbs, &v, 2)) {
116 return 0;
117 }
118 *out = v;
119 return 1;
120}
121
122int CBS_get_u24(CBS *cbs, uint32_t *out) {
123 return cbs_get_u(cbs, out, 3);
124}
125
126int CBS_get_u32(CBS *cbs, uint32_t *out) {
127 return cbs_get_u(cbs, out, 4);
128}
129
David Benjamin6e899c72016-06-09 18:02:18 -0400130int CBS_get_last_u8(CBS *cbs, uint8_t *out) {
131 if (cbs->len == 0) {
132 return 0;
133 }
134 *out = cbs->data[cbs->len - 1];
135 cbs->len--;
136 return 1;
137}
138
Adam Langleyd9e397b2015-01-22 14:27:53 -0800139int CBS_get_bytes(CBS *cbs, CBS *out, size_t len) {
140 const uint8_t *v;
141 if (!cbs_get(cbs, &v, len)) {
142 return 0;
143 }
144 CBS_init(out, v, len);
145 return 1;
146}
147
Kenny Rootb8494592015-09-25 02:29:14 +0000148int CBS_copy_bytes(CBS *cbs, uint8_t *out, size_t len) {
149 const uint8_t *v;
150 if (!cbs_get(cbs, &v, len)) {
151 return 0;
152 }
153 memcpy(out, v, len);
154 return 1;
155}
156
Adam Langleyd9e397b2015-01-22 14:27:53 -0800157static int cbs_get_length_prefixed(CBS *cbs, CBS *out, size_t len_len) {
158 uint32_t len;
159 if (!cbs_get_u(cbs, &len, len_len)) {
160 return 0;
161 }
162 return CBS_get_bytes(cbs, out, len);
163}
164
165int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out) {
166 return cbs_get_length_prefixed(cbs, out, 1);
167}
168
169int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out) {
170 return cbs_get_length_prefixed(cbs, out, 2);
171}
172
173int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) {
174 return cbs_get_length_prefixed(cbs, out, 3);
175}
176
Adam Langleyf4e42722015-06-04 17:45:09 -0700177static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
178 size_t *out_header_len, int ber_ok) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800179 uint8_t tag, length_byte;
180 CBS header = *cbs;
181 CBS throwaway;
182
183 if (out == NULL) {
184 out = &throwaway;
185 }
186
187 if (!CBS_get_u8(&header, &tag) ||
188 !CBS_get_u8(&header, &length_byte)) {
189 return 0;
190 }
191
David Benjamin4969cc92016-04-22 15:02:23 -0400192 /* ITU-T X.690 section 8.1.2.3 specifies the format for identifiers with a tag
193 * number no greater than 30.
194 *
195 * If the number portion is 31 (0x1f, the largest value that fits in the
196 * allotted bits), then the tag is more than one byte long and the
197 * continuation bytes contain the tag number. This parser only supports tag
198 * numbers less than 31 (and thus single-byte tags). */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800199 if ((tag & 0x1f) == 0x1f) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800200 return 0;
201 }
202
203 if (out_tag != NULL) {
204 *out_tag = tag;
205 }
206
207 size_t len;
David Benjamin4969cc92016-04-22 15:02:23 -0400208 /* The format for the length encoding is specified in ITU-T X.690 section
209 * 8.1.3. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800210 if ((length_byte & 0x80) == 0) {
211 /* Short form length. */
212 len = ((size_t) length_byte) + 2;
213 if (out_header_len != NULL) {
214 *out_header_len = 2;
215 }
216 } else {
David Benjamin4969cc92016-04-22 15:02:23 -0400217 /* The high bit indicate that this is the long form, while the next 7 bits
218 * encode the number of subsequent octets used to encode the length (ITU-T
219 * X.690 clause 8.1.3.5.b). */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800220 const size_t num_bytes = length_byte & 0x7f;
221 uint32_t len32;
222
Adam Langleyf4e42722015-06-04 17:45:09 -0700223 if (ber_ok && (tag & CBS_ASN1_CONSTRUCTED) != 0 && num_bytes == 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800224 /* indefinite length */
Adam Langleyf4e42722015-06-04 17:45:09 -0700225 if (out_header_len != NULL) {
226 *out_header_len = 2;
227 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800228 return CBS_get_bytes(cbs, out, 2);
229 }
230
David Benjamin4969cc92016-04-22 15:02:23 -0400231 /* ITU-T X.690 clause 8.1.3.5.c specifies that the value 0xff shall not be
232 * used as the first byte of the length. If this parser encounters that
233 * value, num_bytes will be parsed as 127, which will fail the check below.
234 */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800235 if (num_bytes == 0 || num_bytes > 4) {
236 return 0;
237 }
238 if (!cbs_get_u(&header, &len32, num_bytes)) {
239 return 0;
240 }
David Benjamin4969cc92016-04-22 15:02:23 -0400241 /* ITU-T X.690 section 10.1 (DER length forms) requires encoding the length
242 * with the minimum number of octets. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800243 if (len32 < 128) {
244 /* Length should have used short-form encoding. */
245 return 0;
246 }
247 if ((len32 >> ((num_bytes-1)*8)) == 0) {
248 /* Length should have been at least one byte shorter. */
249 return 0;
250 }
251 len = len32;
252 if (len + 2 + num_bytes < len) {
253 /* Overflow. */
254 return 0;
255 }
256 len += 2 + num_bytes;
257 if (out_header_len != NULL) {
258 *out_header_len = 2 + num_bytes;
259 }
260 }
261
262 return CBS_get_bytes(cbs, out, len);
263}
264
Adam Langleyf4e42722015-06-04 17:45:09 -0700265int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
266 size_t *out_header_len) {
267 return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len,
268 0 /* DER only */);
269}
270
271int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
272 size_t *out_header_len) {
273 return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len,
274 1 /* BER allowed */);
275}
276
Adam Langleyd9e397b2015-01-22 14:27:53 -0800277static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value,
278 int skip_header) {
279 size_t header_len;
280 unsigned tag;
281 CBS throwaway;
282
283 if (out == NULL) {
284 out = &throwaway;
285 }
286
287 if (!CBS_get_any_asn1_element(cbs, out, &tag, &header_len) ||
Adam Langleyf4e42722015-06-04 17:45:09 -0700288 tag != tag_value) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800289 return 0;
290 }
291
292 if (skip_header && !CBS_skip(out, header_len)) {
293 assert(0);
294 return 0;
295 }
296
297 return 1;
298}
299
300int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value) {
301 return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */);
302}
303
304int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value) {
305 return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */);
306}
307
308int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value) {
309 if (CBS_len(cbs) < 1) {
310 return 0;
311 }
312 return CBS_data(cbs)[0] == tag_value;
313}
314
315int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) {
316 CBS bytes;
317 const uint8_t *data;
318 size_t i, len;
319
320 if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER)) {
321 return 0;
322 }
323
324 *out = 0;
325 data = CBS_data(&bytes);
326 len = CBS_len(&bytes);
327
328 if (len == 0) {
329 /* An INTEGER is encoded with at least one octet. */
330 return 0;
331 }
332
333 if ((data[0] & 0x80) != 0) {
Adam Langleye9ada862015-05-11 17:20:37 -0700334 /* Negative number. */
335 return 0;
336 }
337
338 if (data[0] == 0 && len > 1 && (data[1] & 0x80) == 0) {
339 /* Extra leading zeros. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800340 return 0;
341 }
342
343 for (i = 0; i < len; i++) {
344 if ((*out >> 56) != 0) {
345 /* Too large to represent as a uint64_t. */
346 return 0;
347 }
348 *out <<= 8;
349 *out |= data[i];
350 }
351
352 return 1;
353}
354
355int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, unsigned tag) {
Kenny Rootb8494592015-09-25 02:29:14 +0000356 int present = 0;
357
Adam Langleyd9e397b2015-01-22 14:27:53 -0800358 if (CBS_peek_asn1_tag(cbs, tag)) {
359 if (!CBS_get_asn1(cbs, out, tag)) {
360 return 0;
361 }
Kenny Rootb8494592015-09-25 02:29:14 +0000362 present = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800363 }
Kenny Rootb8494592015-09-25 02:29:14 +0000364
365 if (out_present != NULL) {
366 *out_present = present;
367 }
368
Adam Langleyd9e397b2015-01-22 14:27:53 -0800369 return 1;
370}
371
372int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present,
373 unsigned tag) {
374 CBS child;
375 int present;
376 if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
377 return 0;
378 }
379 if (present) {
380 if (!CBS_get_asn1(&child, out, CBS_ASN1_OCTETSTRING) ||
381 CBS_len(&child) != 0) {
382 return 0;
383 }
384 } else {
385 CBS_init(out, NULL, 0);
386 }
387 if (out_present) {
388 *out_present = present;
389 }
390 return 1;
391}
392
393int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, unsigned tag,
394 uint64_t default_value) {
395 CBS child;
396 int present;
397 if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
398 return 0;
399 }
400 if (present) {
401 if (!CBS_get_asn1_uint64(&child, out) ||
402 CBS_len(&child) != 0) {
403 return 0;
404 }
405 } else {
406 *out = default_value;
407 }
408 return 1;
409}
410
411int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag,
412 int default_value) {
413 CBS child, child2;
414 int present;
415 if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
416 return 0;
417 }
418 if (present) {
419 uint8_t boolean;
420
421 if (!CBS_get_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
422 CBS_len(&child2) != 1 ||
423 CBS_len(&child) != 0) {
424 return 0;
425 }
426
427 boolean = CBS_data(&child2)[0];
428 if (boolean == 0) {
429 *out = 0;
430 } else if (boolean == 0xff) {
431 *out = 1;
432 } else {
433 return 0;
434 }
435 } else {
436 *out = default_value;
437 }
438 return 1;
439}