inet6: only mark autoconf addresses tentative if detached
[dragonfly.git] / crypto / libressl / ssl / bs_cbs.c
blob63c078c9b05f611c9c1aed79182e407e8b1d3b34
1 /* $OpenBSD: bs_cbs.c,v 1.24 2021/12/15 17:36:49 jsing Exp $ */
2 /*
3 * Copyright (c) 2014, Google Inc.
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.
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.
18 #include <stdlib.h>
19 #include <string.h>
21 #include "bytestring.h"
23 void
24 CBS_init(CBS *cbs, const uint8_t *data, size_t len)
26 cbs->data = data;
27 cbs->initial_len = len;
28 cbs->len = len;
31 void
32 CBS_dup(const CBS *cbs, CBS *out)
34 CBS_init(out, CBS_data(cbs), CBS_len(cbs));
35 out->initial_len = cbs->initial_len;
38 static int
39 cbs_get(CBS *cbs, const uint8_t **p, size_t n)
41 if (cbs->len < n)
42 return 0;
44 *p = cbs->data;
45 cbs->data += n;
46 cbs->len -= n;
47 return 1;
50 static int
51 cbs_peek(CBS *cbs, const uint8_t **p, size_t n)
53 if (cbs->len < n)
54 return 0;
56 *p = cbs->data;
57 return 1;
60 size_t
61 CBS_offset(const CBS *cbs)
63 return cbs->initial_len - cbs->len;
66 int
67 CBS_skip(CBS *cbs, size_t len)
69 const uint8_t *dummy;
70 return cbs_get(cbs, &dummy, len);
73 const uint8_t *
74 CBS_data(const CBS *cbs)
76 return cbs->data;
79 size_t
80 CBS_len(const CBS *cbs)
82 return cbs->len;
85 int
86 CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len)
88 free(*out_ptr);
89 *out_ptr = NULL;
90 *out_len = 0;
92 if (cbs->len == 0)
93 return 1;
95 if ((*out_ptr = malloc(cbs->len)) == NULL)
96 return 0;
98 memcpy(*out_ptr, cbs->data, cbs->len);
100 *out_len = cbs->len;
101 return 1;
105 CBS_strdup(const CBS *cbs, char **out_ptr)
107 free(*out_ptr);
108 *out_ptr = NULL;
110 if (CBS_contains_zero_byte(cbs))
111 return 0;
113 *out_ptr = strndup((const char *)cbs->data, cbs->len);
114 return (*out_ptr != NULL);
118 CBS_write_bytes(const CBS *cbs, uint8_t *dst, size_t dst_len, size_t *copied)
120 if (dst_len < cbs->len)
121 return 0;
123 memmove(dst, cbs->data, cbs->len);
125 if (copied != NULL)
126 *copied = cbs->len;
128 return 1;
132 CBS_contains_zero_byte(const CBS *cbs)
134 return memchr(cbs->data, 0, cbs->len) != NULL;
138 CBS_mem_equal(const CBS *cbs, const uint8_t *data, size_t len)
140 if (len != cbs->len)
141 return 0;
143 return timingsafe_memcmp(cbs->data, data, len) == 0;
146 static int
147 cbs_get_u(CBS *cbs, uint32_t *out, size_t len)
149 uint32_t result = 0;
150 size_t i;
151 const uint8_t *data;
153 if (len < 1 || len > 4)
154 return 0;
156 if (!cbs_get(cbs, &data, len))
157 return 0;
159 for (i = 0; i < len; i++) {
160 result <<= 8;
161 result |= data[i];
163 *out = result;
164 return 1;
168 CBS_get_u8(CBS *cbs, uint8_t *out)
170 const uint8_t *v;
172 if (!cbs_get(cbs, &v, 1))
173 return 0;
175 *out = *v;
176 return 1;
180 CBS_get_u16(CBS *cbs, uint16_t *out)
182 uint32_t v;
184 if (!cbs_get_u(cbs, &v, 2))
185 return 0;
187 *out = v;
188 return 1;
192 CBS_get_u24(CBS *cbs, uint32_t *out)
194 return cbs_get_u(cbs, out, 3);
198 CBS_get_u32(CBS *cbs, uint32_t *out)
200 return cbs_get_u(cbs, out, 4);
204 CBS_get_u64(CBS *cbs, uint64_t *out)
206 uint32_t a, b;
208 if (cbs->len < 8)
209 return 0;
211 if (!CBS_get_u32(cbs, &a))
212 return 0;
213 if (!CBS_get_u32(cbs, &b))
214 return 0;
216 *out = (uint64_t)a << 32 | b;
217 return 1;
221 CBS_get_last_u8(CBS *cbs, uint8_t *out)
223 if (cbs->len == 0)
224 return 0;
226 *out = cbs->data[cbs->len - 1];
227 cbs->len--;
228 return 1;
232 CBS_get_bytes(CBS *cbs, CBS *out, size_t len)
234 const uint8_t *v;
236 if (!cbs_get(cbs, &v, len))
237 return 0;
239 CBS_init(out, v, len);
240 return 1;
243 static int
244 cbs_get_length_prefixed(CBS *cbs, CBS *out, size_t len_len)
246 uint32_t len;
248 if (!cbs_get_u(cbs, &len, len_len))
249 return 0;
251 return CBS_get_bytes(cbs, out, len);
255 CBS_get_u8_length_prefixed(CBS *cbs, CBS *out)
257 return cbs_get_length_prefixed(cbs, out, 1);
261 CBS_get_u16_length_prefixed(CBS *cbs, CBS *out)
263 return cbs_get_length_prefixed(cbs, out, 2);
267 CBS_get_u24_length_prefixed(CBS *cbs, CBS *out)
269 return cbs_get_length_prefixed(cbs, out, 3);
272 static int
273 cbs_peek_u(CBS *cbs, uint32_t *out, size_t len)
275 uint32_t result = 0;
276 size_t i;
277 const uint8_t *data;
279 if (len < 1 || len > 4)
280 return 0;
282 if (!cbs_peek(cbs, &data, len))
283 return 0;
285 for (i = 0; i < len; i++) {
286 result <<= 8;
287 result |= data[i];
289 *out = result;
290 return 1;
294 CBS_peek_u8(CBS *cbs, uint8_t *out)
296 const uint8_t *v;
298 if (!cbs_peek(cbs, &v, 1))
299 return 0;
301 *out = *v;
302 return 1;
306 CBS_peek_u16(CBS *cbs, uint16_t *out)
308 uint32_t v;
310 if (!cbs_peek_u(cbs, &v, 2))
311 return 0;
313 *out = v;
314 return 1;
318 CBS_peek_u24(CBS *cbs, uint32_t *out)
320 return cbs_peek_u(cbs, out, 3);
324 CBS_peek_u32(CBS *cbs, uint32_t *out)
326 return cbs_peek_u(cbs, out, 4);
330 CBS_peek_last_u8(CBS *cbs, uint8_t *out)
332 if (cbs->len == 0)
333 return 0;
335 *out = cbs->data[cbs->len - 1];
336 return 1;
340 CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned int *out_tag,
341 size_t *out_header_len)
343 return cbs_get_any_asn1_element_internal(cbs, out, out_tag,
344 out_header_len, 1);
348 * Review X.690 for details on ASN.1 DER encoding.
350 * If non-strict mode is enabled, then DER rules are relaxed
351 * for indefinite constructs (violates DER but a little closer to BER).
352 * Non-strict mode should only be used by bs_ber.c
354 * Sections 8, 10 and 11 for DER encoding
357 cbs_get_any_asn1_element_internal(CBS *cbs, CBS *out, unsigned int *out_tag,
358 size_t *out_header_len, int strict)
360 uint8_t tag, length_byte;
361 CBS header = *cbs;
362 CBS throwaway;
363 size_t len;
365 if (out == NULL)
366 out = &throwaway;
369 * Get identifier octet and length octet. Only 1 octet for each
370 * is a CBS limitation.
372 if (!CBS_get_u8(&header, &tag) || !CBS_get_u8(&header, &length_byte))
373 return 0;
375 /* CBS limitation: long form tags are not supported. */
376 if ((tag & 0x1f) == 0x1f)
377 return 0;
379 if (out_tag != NULL)
380 *out_tag = tag;
382 if ((length_byte & 0x80) == 0) {
383 /* Short form length. */
384 len = ((size_t) length_byte) + 2;
385 if (out_header_len != NULL)
386 *out_header_len = 2;
388 } else {
389 /* Long form length. */
390 const size_t num_bytes = length_byte & 0x7f;
391 uint32_t len32;
393 /* ASN.1 reserved value for future extensions */
394 if (num_bytes == 0x7f)
395 return 0;
397 /* Handle indefinite form length */
398 if (num_bytes == 0) {
399 /* DER encoding doesn't allow for indefinite form. */
400 if (strict)
401 return 0;
403 /* Primitive cannot use indefinite in BER or DER. */
404 if ((tag & CBS_ASN1_CONSTRUCTED) == 0)
405 return 0;
407 /* Constructed, indefinite length allowed in BER. */
408 if (out_header_len != NULL)
409 *out_header_len = 2;
410 return CBS_get_bytes(cbs, out, 2);
413 /* CBS limitation. */
414 if (num_bytes > 4)
415 return 0;
417 if (!cbs_get_u(&header, &len32, num_bytes))
418 return 0;
420 /* DER has a minimum length octet requirement. */
421 if (len32 < 128)
422 /* Should have used short form instead */
423 return 0;
425 if ((len32 >> ((num_bytes - 1) * 8)) == 0)
426 /* Length should have been at least one byte shorter. */
427 return 0;
429 len = len32;
430 if (len + 2 + num_bytes < len)
431 /* Overflow. */
432 return 0;
434 len += 2 + num_bytes;
435 if (out_header_len != NULL)
436 *out_header_len = 2 + num_bytes;
439 return CBS_get_bytes(cbs, out, len);
442 static int
443 cbs_get_asn1(CBS *cbs, CBS *out, unsigned int tag_value, int skip_header)
445 size_t header_len;
446 unsigned int tag;
447 CBS throwaway;
449 if (out == NULL)
450 out = &throwaway;
452 if (!CBS_get_any_asn1_element(cbs, out, &tag, &header_len) ||
453 tag != tag_value)
454 return 0;
456 if (skip_header && !CBS_skip(out, header_len))
457 return 0;
459 return 1;
463 CBS_get_asn1(CBS *cbs, CBS *out, unsigned int tag_value)
465 return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */);
469 CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned int tag_value)
471 return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */);
475 CBS_peek_asn1_tag(const CBS *cbs, unsigned int tag_value)
477 if (CBS_len(cbs) < 1)
478 return 0;
481 * Tag number 31 indicates the start of a long form number.
482 * This is valid in ASN.1, but CBS only supports short form.
484 if ((tag_value & 0x1f) == 0x1f)
485 return 0;
487 return CBS_data(cbs)[0] == tag_value;
490 /* Encoding details are in ASN.1: X.690 section 8.3 */
492 CBS_get_asn1_uint64(CBS *cbs, uint64_t *out)
494 CBS bytes;
495 const uint8_t *data;
496 size_t i, len;
498 if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER))
499 return 0;
501 *out = 0;
502 data = CBS_data(&bytes);
503 len = CBS_len(&bytes);
505 if (len == 0)
506 /* An INTEGER is encoded with at least one content octet. */
507 return 0;
509 if ((data[0] & 0x80) != 0)
510 /* Negative number. */
511 return 0;
513 if (data[0] == 0 && len > 1 && (data[1] & 0x80) == 0)
514 /* Violates smallest encoding rule: excessive leading zeros. */
515 return 0;
517 for (i = 0; i < len; i++) {
518 if ((*out >> 56) != 0)
519 /* Too large to represent as a uint64_t. */
520 return 0;
522 *out <<= 8;
523 *out |= data[i];
526 return 1;
530 CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, unsigned int tag)
532 if (CBS_peek_asn1_tag(cbs, tag)) {
533 if (!CBS_get_asn1(cbs, out, tag))
534 return 0;
536 *out_present = 1;
537 } else {
538 *out_present = 0;
540 return 1;
544 CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present,
545 unsigned int tag)
547 CBS child;
548 int present;
550 if (!CBS_get_optional_asn1(cbs, &child, &present, tag))
551 return 0;
553 if (present) {
554 if (!CBS_get_asn1(&child, out, CBS_ASN1_OCTETSTRING) ||
555 CBS_len(&child) != 0)
556 return 0;
557 } else {
558 CBS_init(out, NULL, 0);
560 if (out_present)
561 *out_present = present;
563 return 1;
567 CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, unsigned int tag,
568 uint64_t default_value)
570 CBS child;
571 int present;
573 if (!CBS_get_optional_asn1(cbs, &child, &present, tag))
574 return 0;
576 if (present) {
577 if (!CBS_get_asn1_uint64(&child, out) ||
578 CBS_len(&child) != 0)
579 return 0;
580 } else {
581 *out = default_value;
583 return 1;
587 CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned int tag,
588 int default_value)
590 CBS child, child2;
591 int present;
593 if (!CBS_get_optional_asn1(cbs, &child, &present, tag))
594 return 0;
596 if (present) {
597 uint8_t boolean;
599 if (!CBS_get_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
600 CBS_len(&child2) != 1 || CBS_len(&child) != 0)
601 return 0;
603 boolean = CBS_data(&child2)[0];
604 if (boolean == 0)
605 *out = 0;
606 else if (boolean == 0xff)
607 *out = 1;
608 else
609 return 0;
611 } else {
612 *out = default_value;
614 return 1;