Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/bytestring/cbs.c

raw
1 // Copyright (c) 2014, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include <openssl/asn1.h>
5 #include <openssl/bytestring.h>
6 #include <openssl/mem.h>
7
8 #include <assert.h>
9
10 #ifndef __STDC_FORMAT_MACROS
11 #define __STDC_FORMAT_MACROS
12 #endif
13 #include <ctype.h>
14 #include <inttypes.h>
15
16 #include <string.h>
17
18 #include "../asn1/internal.h"
19 #include "../internal.h"
20 #include "internal.h"
21
22
23 void CBS_init(CBS *cbs, const uint8_t *data, size_t len) {
24 cbs->data = data;
25 cbs->len = len;
26 }
27
28 static int cbs_get(CBS *cbs, const uint8_t **p, size_t n) {
29 if (cbs->len < n) {
30 return 0;
31 }
32
33 *p = cbs->data;
34 cbs->data += n;
35 cbs->len -= n;
36 return 1;
37 }
38
39 int CBS_skip(CBS *cbs, size_t len) {
40 const uint8_t *dummy;
41 return cbs_get(cbs, &dummy, len);
42 }
43
44 const uint8_t *CBS_data(const CBS *cbs) { return cbs->data; }
45
46 size_t CBS_len(const CBS *cbs) { return cbs->len; }
47
48 int CBS_stow(const CBS *cbs, uint8_t **out_ptr, size_t *out_len) {
49 OPENSSL_free(*out_ptr);
50 *out_ptr = NULL;
51 *out_len = 0;
52
53 if (cbs->len == 0) {
54 return 1;
55 }
56 *out_ptr = OPENSSL_memdup(cbs->data, cbs->len);
57 if (*out_ptr == NULL) {
58 return 0;
59 }
60 *out_len = cbs->len;
61 return 1;
62 }
63
64 int CBS_strdup(const CBS *cbs, char **out_ptr) {
65 if (*out_ptr != NULL) {
66 OPENSSL_free(*out_ptr);
67 }
68 *out_ptr = OPENSSL_strndup((const char *)cbs->data, cbs->len);
69 return (*out_ptr != NULL);
70 }
71
72 int CBS_contains_zero_byte(const CBS *cbs) {
73 return OPENSSL_memchr(cbs->data, 0, cbs->len) != NULL;
74 }
75
76 int CBS_mem_equal(const CBS *cbs, const uint8_t *data, size_t len) {
77 if (len != cbs->len) {
78 return 0;
79 }
80 return CRYPTO_memcmp(cbs->data, data, len) == 0;
81 }
82
83 static int cbs_get_u(CBS *cbs, uint64_t *out, size_t len) {
84 uint64_t result = 0;
85 const uint8_t *data;
86
87 if (!cbs_get(cbs, &data, len)) {
88 return 0;
89 }
90 for (size_t i = 0; i < len; i++) {
91 result <<= 8;
92 result |= data[i];
93 }
94 *out = result;
95 return 1;
96 }
97
98 int CBS_get_u8(CBS *cbs, uint8_t *out) {
99 const uint8_t *v;
100 if (!cbs_get(cbs, &v, 1)) {
101 return 0;
102 }
103 *out = *v;
104 return 1;
105 }
106
107 int CBS_get_u16(CBS *cbs, uint16_t *out) {
108 uint64_t v;
109 if (!cbs_get_u(cbs, &v, 2)) {
110 return 0;
111 }
112 *out = v;
113 return 1;
114 }
115
116 int CBS_get_u16le(CBS *cbs, uint16_t *out) {
117 if (!CBS_get_u16(cbs, out)) {
118 return 0;
119 }
120 *out = CRYPTO_bswap2(*out);
121 return 1;
122 }
123
124 int CBS_get_u24(CBS *cbs, uint32_t *out) {
125 uint64_t v;
126 if (!cbs_get_u(cbs, &v, 3)) {
127 return 0;
128 }
129 *out = (uint32_t)v;
130 return 1;
131 }
132
133 int CBS_get_u32(CBS *cbs, uint32_t *out) {
134 uint64_t v;
135 if (!cbs_get_u(cbs, &v, 4)) {
136 return 0;
137 }
138 *out = (uint32_t)v;
139 return 1;
140 }
141
142 int CBS_get_u32le(CBS *cbs, uint32_t *out) {
143 if (!CBS_get_u32(cbs, out)) {
144 return 0;
145 }
146 *out = CRYPTO_bswap4(*out);
147 return 1;
148 }
149
150 int CBS_get_u64(CBS *cbs, uint64_t *out) { return cbs_get_u(cbs, out, 8); }
151
152 int CBS_get_u64le(CBS *cbs, uint64_t *out) {
153 if (!cbs_get_u(cbs, out, 8)) {
154 return 0;
155 }
156 *out = CRYPTO_bswap8(*out);
157 return 1;
158 }
159
160 int CBS_get_last_u8(CBS *cbs, uint8_t *out) {
161 if (cbs->len == 0) {
162 return 0;
163 }
164 *out = cbs->data[cbs->len - 1];
165 cbs->len--;
166 return 1;
167 }
168
169 int CBS_get_bytes(CBS *cbs, CBS *out, size_t len) {
170 const uint8_t *v;
171 if (!cbs_get(cbs, &v, len)) {
172 return 0;
173 }
174 CBS_init(out, v, len);
175 return 1;
176 }
177
178 int CBS_copy_bytes(CBS *cbs, uint8_t *out, size_t len) {
179 const uint8_t *v;
180 if (!cbs_get(cbs, &v, len)) {
181 return 0;
182 }
183 OPENSSL_memcpy(out, v, len);
184 return 1;
185 }
186
187 static int cbs_get_length_prefixed(CBS *cbs, CBS *out, size_t len_len) {
188 uint64_t len;
189 if (!cbs_get_u(cbs, &len, len_len)) {
190 return 0;
191 }
192 // If |len_len| <= 3 then we know that |len| will fit into a |size_t|, even on
193 // 32-bit systems.
194 assert(len_len <= 3);
195 return CBS_get_bytes(cbs, out, len);
196 }
197
198 int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out) {
199 return cbs_get_length_prefixed(cbs, out, 1);
200 }
201
202 int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out) {
203 return cbs_get_length_prefixed(cbs, out, 2);
204 }
205
206 int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) {
207 return cbs_get_length_prefixed(cbs, out, 3);
208 }
209
210 int CBS_get_until_first(CBS *cbs, CBS *out, uint8_t c) {
211 const uint8_t *split = OPENSSL_memchr(CBS_data(cbs), c, CBS_len(cbs));
212 if (split == NULL) {
213 return 0;
214 }
215 return CBS_get_bytes(cbs, out, split - CBS_data(cbs));
216 }
217
218 int CBS_get_u64_decimal(CBS *cbs, uint64_t *out) {
219 uint64_t v = 0;
220 int seen_digit = 0;
221 while (CBS_len(cbs) != 0) {
222 uint8_t c = CBS_data(cbs)[0];
223 if (!OPENSSL_isdigit(c)) {
224 break;
225 }
226 CBS_skip(cbs, 1);
227 if ( // Forbid stray leading zeros.
228 (v == 0 && seen_digit) ||
229 // Check for overflow.
230 v > UINT64_MAX / 10 || //
231 v * 10 > UINT64_MAX - (c - '0')) {
232 return 0;
233 }
234 v = v * 10 + (c - '0');
235 seen_digit = 1;
236 }
237
238 *out = v;
239 return seen_digit;
240 }
241
242 // parse_base128_integer reads a big-endian base-128 integer from |cbs| and sets
243 // |*out| to the result. This is the encoding used in DER for both high tag
244 // number form and OID components.
245 static int parse_base128_integer(CBS *cbs, uint64_t *out) {
246 uint64_t v = 0;
247 uint8_t b;
248 do {
249 if (!CBS_get_u8(cbs, &b)) {
250 return 0;
251 }
252 if ((v >> (64 - 7)) != 0) {
253 // The value is too large.
254 return 0;
255 }
256 if (v == 0 && b == 0x80) {
257 // The value must be minimally encoded.
258 return 0;
259 }
260 v = (v << 7) | (b & 0x7f);
261
262 // Values end at an octet with the high bit cleared.
263 } while (b & 0x80);
264
265 *out = v;
266 return 1;
267 }
268
269 static int parse_asn1_tag(CBS *cbs, CBS_ASN1_TAG *out, int universal_tag_ok) {
270 uint8_t tag_byte;
271 if (!CBS_get_u8(cbs, &tag_byte)) {
272 return 0;
273 }
274
275 // ITU-T X.690 section 8.1.2.3 specifies the format for identifiers with a tag
276 // number no greater than 30.
277 //
278 // If the number portion is 31 (0x1f, the largest value that fits in the
279 // allotted bits), then the tag is more than one byte long and the
280 // continuation bytes contain the tag number.
281 CBS_ASN1_TAG tag = ((CBS_ASN1_TAG)tag_byte & 0xe0) << CBS_ASN1_TAG_SHIFT;
282 CBS_ASN1_TAG tag_number = tag_byte & 0x1f;
283 if (tag_number == 0x1f) {
284 uint64_t v;
285 if (!parse_base128_integer(cbs, &v) ||
286 // Check the tag number is within our supported bounds.
287 v > CBS_ASN1_TAG_NUMBER_MASK ||
288 // Small tag numbers should have used low tag number form, even in BER.
289 v < 0x1f) {
290 return 0;
291 }
292 tag_number = (CBS_ASN1_TAG)v;
293 }
294
295 tag |= tag_number;
296
297 // Tag [UNIVERSAL 0] is reserved for use by the encoding. Reject it here to
298 // avoid some ambiguity around ANY values and BER indefinite-length EOCs. See
299 // https://crbug.com/boringssl/455.
300 if (!universal_tag_ok && (tag & ~CBS_ASN1_CONSTRUCTED) == 0) {
301 return 0;
302 }
303
304 *out = tag;
305 return 1;
306 }
307
308 int cbs_get_any_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG *out_tag,
309 size_t *out_header_len, int *out_ber_found,
310 int *out_indefinite, int ber_ok, int universal_tag_ok) {
311 CBS header = *cbs;
312 CBS throwaway;
313
314 if (out == NULL) {
315 out = &throwaway;
316 }
317 if (ber_ok) {
318 *out_ber_found = 0;
319 *out_indefinite = 0;
320 } else {
321 assert(out_ber_found == NULL);
322 assert(out_indefinite == NULL);
323 }
324
325 CBS_ASN1_TAG tag;
326 if (!parse_asn1_tag(&header, &tag, universal_tag_ok)) {
327 return 0;
328 }
329 if (out_tag != NULL) {
330 *out_tag = tag;
331 }
332
333 uint8_t length_byte;
334 if (!CBS_get_u8(&header, &length_byte)) {
335 return 0;
336 }
337
338 size_t header_len = CBS_len(cbs) - CBS_len(&header);
339
340 size_t len;
341 // The format for the length encoding is specified in ITU-T X.690 section
342 // 8.1.3.
343 if ((length_byte & 0x80) == 0) {
344 // Short form length.
345 len = ((size_t)length_byte) + header_len;
346 if (out_header_len != NULL) {
347 *out_header_len = header_len;
348 }
349 } else {
350 // The high bit indicate that this is the long form, while the next 7 bits
351 // encode the number of subsequent octets used to encode the length (ITU-T
352 // X.690 clause 8.1.3.5.b).
353 const size_t num_bytes = length_byte & 0x7f;
354 uint64_t len64;
355
356 if (ber_ok && (tag & CBS_ASN1_CONSTRUCTED) != 0 && num_bytes == 0) {
357 // indefinite length
358 if (out_header_len != NULL) {
359 *out_header_len = header_len;
360 }
361 *out_ber_found = 1;
362 *out_indefinite = 1;
363 return CBS_get_bytes(cbs, out, header_len);
364 }
365
366 // ITU-T X.690 clause 8.1.3.5.c specifies that the value 0xff shall not be
367 // used as the first byte of the length. If this parser encounters that
368 // value, num_bytes will be parsed as 127, which will fail this check.
369 if (num_bytes == 0 || num_bytes > 4) {
370 return 0;
371 }
372 if (!cbs_get_u(&header, &len64, num_bytes)) {
373 return 0;
374 }
375 // ITU-T X.690 section 10.1 (DER length forms) requires encoding the
376 // length with the minimum number of octets. BER could, technically, have
377 // 125 superfluous zero bytes. We do not attempt to handle that and still
378 // require that the length fit in a |uint32_t| for BER.
379 if (len64 < 128) {
380 // Length should have used short-form encoding.
381 if (ber_ok) {
382 *out_ber_found = 1;
383 } else {
384 return 0;
385 }
386 }
387 if ((len64 >> ((num_bytes - 1) * 8)) == 0) {
388 // Length should have been at least one byte shorter.
389 if (ber_ok) {
390 *out_ber_found = 1;
391 } else {
392 return 0;
393 }
394 }
395 len = len64;
396 if (len + header_len + num_bytes < len) {
397 // Overflow.
398 return 0;
399 }
400 len += header_len + num_bytes;
401 if (out_header_len != NULL) {
402 *out_header_len = header_len + num_bytes;
403 }
404 }
405
406 return CBS_get_bytes(cbs, out, len);
407 }
408
409 int CBS_get_any_asn1(CBS *cbs, CBS *out, CBS_ASN1_TAG *out_tag) {
410 size_t header_len;
411 if (!CBS_get_any_asn1_element(cbs, out, out_tag, &header_len)) {
412 return 0;
413 }
414
415 if (!CBS_skip(out, header_len)) {
416 assert(0);
417 return 0;
418 }
419
420 return 1;
421 }
422
423 int CBS_get_any_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG *out_tag,
424 size_t *out_header_len) {
425 return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len, NULL, NULL,
426 /*ber_ok=*/0, /*universal_tag_ok=*/0);
427 }
428
429 int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG *out_tag,
430 size_t *out_header_len, int *out_ber_found,
431 int *out_indefinite) {
432 int ber_found_temp;
433 return cbs_get_any_asn1_element(
434 cbs, out, out_tag, out_header_len,
435 out_ber_found ? out_ber_found : &ber_found_temp, out_indefinite,
436 /*ber_ok=*/1, /*universal_tag_ok=*/0);
437 }
438
439 static int cbs_get_asn1(CBS *cbs, CBS *out, CBS_ASN1_TAG tag_value,
440 int skip_header) {
441 size_t header_len;
442 CBS_ASN1_TAG tag;
443 CBS throwaway;
444
445 if (out == NULL) {
446 out = &throwaway;
447 }
448
449 if (!CBS_get_any_asn1_element(cbs, out, &tag, &header_len) ||
450 tag != tag_value) {
451 return 0;
452 }
453
454 if (skip_header && !CBS_skip(out, header_len)) {
455 assert(0);
456 return 0;
457 }
458
459 return 1;
460 }
461
462 int CBS_get_asn1(CBS *cbs, CBS *out, CBS_ASN1_TAG tag_value) {
463 return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */);
464 }
465
466 int CBS_get_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG tag_value) {
467 return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */);
468 }
469
470 int CBS_peek_asn1_tag(const CBS *cbs, CBS_ASN1_TAG tag_value) {
471 CBS copy = *cbs;
472 CBS_ASN1_TAG actual_tag;
473 return parse_asn1_tag(&copy, &actual_tag, /*universal_tag_ok=*/0) && tag_value == actual_tag;
474 }
475
476 int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) {
477 CBS bytes;
478 if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER) ||
479 !CBS_is_unsigned_asn1_integer(&bytes)) {
480 return 0;
481 }
482
483 *out = 0;
484 const uint8_t *data = CBS_data(&bytes);
485 size_t len = CBS_len(&bytes);
486 for (size_t i = 0; i < len; i++) {
487 if ((*out >> 56) != 0) {
488 // Too large to represent as a uint64_t.
489 return 0;
490 }
491 *out <<= 8;
492 *out |= data[i];
493 }
494
495 return 1;
496 }
497
498 int CBS_get_asn1_int64(CBS *cbs, int64_t *out) {
499 int is_negative;
500 CBS bytes;
501 if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER) ||
502 !CBS_is_valid_asn1_integer(&bytes, &is_negative)) {
503 return 0;
504 }
505 const uint8_t *data = CBS_data(&bytes);
506 const size_t len = CBS_len(&bytes);
507 if (len > sizeof(int64_t)) {
508 return 0;
509 }
510 uint8_t sign_extend[sizeof(int64_t)];
511 memset(sign_extend, is_negative ? 0xff : 0, sizeof(sign_extend));
512 // GCC 12/13 report `stringop-overflow` on the following line
513 // without additional condition: `i < sizeof(int64_t)`
514 for (size_t i = 0; i < len && i < sizeof(int64_t); i++) {
515 // `data` is big-endian.
516 // Values are always shifted toward the "little" end.
517 #ifdef OPENSSL_BIG_ENDIAN
518 // Bytes are written starting at the highest index.
519 sign_extend[sizeof(sign_extend) - i - 1] = data[len - i - 1];
520 #else
521 // Bytes are written starting at the lowest index.
522 sign_extend[i] = data[len - i - 1];
523 #endif
524 }
525
526 memcpy(out, sign_extend, sizeof(sign_extend));
527 return 1;
528 }
529
530 int CBS_get_asn1_bool(CBS *cbs, int *out) {
531 CBS bytes;
532 if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_BOOLEAN) || CBS_len(&bytes) != 1) {
533 return 0;
534 }
535
536 const uint8_t value = *CBS_data(&bytes);
537 if (value != 0 && value != 0xff) {
538 return 0;
539 }
540
541 *out = !!value;
542 return 1;
543 }
544
545 int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present,
546 CBS_ASN1_TAG tag) {
547 int present = 0;
548
549 if (CBS_peek_asn1_tag(cbs, tag)) {
550 if (!CBS_get_asn1(cbs, out, tag)) {
551 return 0;
552 }
553 present = 1;
554 }
555
556 if (out_present != NULL) {
557 *out_present = present;
558 }
559
560 return 1;
561 }
562
563 int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present,
564 CBS_ASN1_TAG tag) {
565 CBS child;
566 int present;
567 if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
568 return 0;
569 }
570 if (present) {
571 assert(out);
572 if (!CBS_get_asn1(&child, out, CBS_ASN1_OCTETSTRING) ||
573 CBS_len(&child) != 0) {
574 return 0;
575 }
576 } else {
577 CBS_init(out, NULL, 0);
578 }
579 if (out_present) {
580 *out_present = present;
581 }
582 return 1;
583 }
584
585 int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, CBS_ASN1_TAG tag,
586 uint64_t default_value) {
587 CBS child;
588 int present;
589 if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
590 return 0;
591 }
592 if (present) {
593 if (!CBS_get_asn1_uint64(&child, out) || CBS_len(&child) != 0) {
594 return 0;
595 }
596 } else {
597 *out = default_value;
598 }
599 return 1;
600 }
601
602 int CBS_get_optional_asn1_bool(CBS *cbs, int *out, CBS_ASN1_TAG tag,
603 int default_value) {
604 CBS child, child2;
605 int present;
606 if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
607 return 0;
608 }
609 if (present) {
610 uint8_t boolean;
611
612 if (!CBS_get_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
613 CBS_len(&child2) != 1 || CBS_len(&child) != 0) {
614 return 0;
615 }
616
617 boolean = CBS_data(&child2)[0];
618 if (boolean == 0) {
619 *out = 0;
620 } else if (boolean == 0xff) {
621 *out = 1;
622 } else {
623 return 0;
624 }
625 } else {
626 *out = default_value;
627 }
628 return 1;
629 }
630
631 int CBS_is_valid_asn1_bitstring(const CBS *cbs) {
632 CBS in = *cbs;
633 uint8_t num_unused_bits;
634 if (!CBS_get_u8(&in, &num_unused_bits) || num_unused_bits > 7) {
635 return 0;
636 }
637
638 if (num_unused_bits == 0) {
639 return 1;
640 }
641
642 // All num_unused_bits bits must exist and be zeros.
643 uint8_t last;
644 if (!CBS_get_last_u8(&in, &last) ||
645 (last & ((1 << num_unused_bits) - 1)) != 0) {
646 return 0;
647 }
648
649 return 1;
650 }
651
652 int CBS_asn1_bitstring_has_bit(const CBS *cbs, unsigned bit) {
653 if (!CBS_is_valid_asn1_bitstring(cbs)) {
654 return 0;
655 }
656
657 const unsigned byte_num = (bit >> 3) + 1;
658 const unsigned bit_num = 7 - (bit & 7);
659
660 // Unused bits are zero, and this function does not distinguish between
661 // missing and unset bits. Thus it is sufficient to do a byte-level length
662 // check.
663 return byte_num < CBS_len(cbs) &&
664 (CBS_data(cbs)[byte_num] & (1 << bit_num)) != 0;
665 }
666
667 int CBS_is_valid_asn1_integer(const CBS *cbs, int *out_is_negative) {
668 CBS copy = *cbs;
669 uint8_t first_byte, second_byte;
670 if (!CBS_get_u8(&copy, &first_byte)) {
671 return 0; // INTEGERs may not be empty.
672 }
673 if (out_is_negative != NULL) {
674 *out_is_negative = (first_byte & 0x80) != 0;
675 }
676 if (!CBS_get_u8(&copy, &second_byte)) {
677 return 1; // One byte INTEGERs are always minimal.
678 }
679 if ((first_byte == 0x00 && (second_byte & 0x80) == 0) ||
680 (first_byte == 0xff && (second_byte & 0x80) != 0)) {
681 return 0; // The value is minimal iff the first 9 bits are not all equal.
682 }
683 return 1;
684 }
685
686 int CBS_is_unsigned_asn1_integer(const CBS *cbs) {
687 int is_negative;
688 return CBS_is_valid_asn1_integer(cbs, &is_negative) && !is_negative;
689 }
690
691 static int add_decimal(CBB *out, uint64_t v) {
692 char buf[DECIMAL_SIZE(uint64_t) + 1];
693 snprintf(buf, sizeof(buf), "%" PRIu64, v);
694 return CBB_add_bytes(out, (const uint8_t *)buf, strlen(buf));
695 }
696
697 int CBS_is_valid_asn1_oid(const CBS *cbs) {
698 if (CBS_len(cbs) == 0) {
699 return 0; // OID encodings cannot be empty.
700 }
701
702 CBS copy = *cbs;
703 uint8_t v, prev = 0;
704 while (CBS_get_u8(&copy, &v)) {
705 // OID encodings are a sequence of minimally-encoded base-128 integers (see
706 // |parse_base128_integer|). If |prev|'s MSB was clear, it was the last byte
707 // of an integer (or |v| is the first byte). |v| is then the first byte of
708 // the next integer. If first byte of an integer is 0x80, it is not
709 // minimally-encoded.
710 if ((prev & 0x80) == 0 && v == 0x80) {
711 return 0;
712 }
713 prev = v;
714 }
715
716 // The last byte should must end an integer encoding.
717 return (prev & 0x80) == 0;
718 }
719
720 char *CBS_asn1_oid_to_text(const CBS *cbs) {
721 CBB cbb;
722 if (!CBB_init(&cbb, 32)) {
723 goto err;
724 }
725
726 CBS copy = *cbs;
727 // The first component is 40 * value1 + value2, where value1 is 0, 1, or 2.
728 uint64_t v;
729 if (!parse_base128_integer(&copy, &v)) {
730 goto err;
731 }
732
733 if (v >= 80) {
734 if (!CBB_add_bytes(&cbb, (const uint8_t *)"2.", 2) ||
735 !add_decimal(&cbb, v - 80)) {
736 goto err;
737 }
738 } else if (!add_decimal(&cbb, v / 40) || !CBB_add_u8(&cbb, '.') ||
739 !add_decimal(&cbb, v % 40)) {
740 goto err;
741 }
742
743 while (CBS_len(&copy) != 0) {
744 if (!parse_base128_integer(&copy, &v) || !CBB_add_u8(&cbb, '.') ||
745 !add_decimal(&cbb, v)) {
746 goto err;
747 }
748 }
749
750 uint8_t *txt;
751 size_t txt_len;
752 if (!CBB_add_u8(&cbb, '\0') || !CBB_finish(&cbb, &txt, &txt_len)) {
753 goto err;
754 }
755
756 return (char *)txt;
757
758 err:
759 CBB_cleanup(&cbb);
760 return NULL;
761 }
762
763 static int cbs_get_two_digits(CBS *cbs, int *out) {
764 uint8_t first_digit, second_digit;
765 if (!CBS_get_u8(cbs, &first_digit)) {
766 return 0;
767 }
768 if (!OPENSSL_isdigit(first_digit)) {
769 return 0;
770 }
771 if (!CBS_get_u8(cbs, &second_digit)) {
772 return 0;
773 }
774 if (!OPENSSL_isdigit(second_digit)) {
775 return 0;
776 }
777 *out = (first_digit - '0') * 10 + (second_digit - '0');
778 return 1;
779 }
780
781 static int is_valid_day(int year, int month, int day) {
782 if (day < 1) {
783 return 0;
784 }
785 switch (month) {
786 case 1:
787 case 3:
788 case 5:
789 case 7:
790 case 8:
791 case 10:
792 case 12:
793 return day <= 31;
794 case 4:
795 case 6:
796 case 9:
797 case 11:
798 return day <= 30;
799 case 2:
800 if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
801 return day <= 29;
802 } else {
803 return day <= 28;
804 }
805 default:
806 return 0;
807 }
808 }
809
810 static int CBS_parse_rfc5280_time_internal(const CBS *cbs, int is_gentime,
811 int allow_timezone_offset,
812 struct tm *out_tm) {
813 int year, month, day, hour, min, sec, tmp;
814 CBS copy = *cbs;
815 uint8_t tz;
816
817 if (is_gentime) {
818 if (!cbs_get_two_digits(&copy, &tmp)) {
819 return 0;
820 }
821 year = tmp * 100;
822 if (!cbs_get_two_digits(&copy, &tmp)) {
823 return 0;
824 }
825 year += tmp;
826 } else {
827 year = 1900;
828 if (!cbs_get_two_digits(&copy, &tmp)) {
829 return 0;
830 }
831 year += tmp;
832 if (year < 1950) {
833 year += 100;
834 }
835 if (year >= 2050) {
836 return 0; // A Generalized time must be used.
837 }
838 }
839 if (!cbs_get_two_digits(&copy, &month) || month < 1 ||
840 month > 12 || // Reject invalid months.
841 !cbs_get_two_digits(&copy, &day) ||
842 !is_valid_day(year, month, day) || // Reject invalid days.
843 !cbs_get_two_digits(&copy, &hour) ||
844 hour > 23 || // Reject invalid hours.
845 !cbs_get_two_digits(&copy, &min) ||
846 min > 59 || // Reject invalid minutes.
847 !cbs_get_two_digits(&copy, &sec) || sec > 59 || !CBS_get_u8(&copy, &tz)) {
848 return 0;
849 }
850
851 int offset_sign = 0;
852 switch (tz) {
853 case 'Z':
854 break; // We correctly have 'Z' on the end as per spec.
855 case '+':
856 offset_sign = 1;
857 break; // Should not be allowed per RFC 5280.
858 case '-':
859 offset_sign = -1;
860 break; // Should not be allowed per RFC 5280.
861 default:
862 return 0; // Reject anything else after the time.
863 }
864
865 // If allow_timezone_offset is non-zero, allow for a four digit timezone
866 // offset to be specified even though this is not allowed by RFC 5280. We are
867 // permissive of this for UTCTimes due to the unfortunate existence of
868 // artisinally rolled long lived certificates that were baked into places that
869 // are now difficult to change. These certificates were generated with the
870 // 'openssl' command that permissively allowed the creation of certificates
871 // with notBefore and notAfter times specified as strings for direct
872 // certificate inclusion on the command line. For context see cl/237068815.
873 //
874 // TODO(bbe): This has been expunged from public web-pki as the ecosystem has
875 // managed to encourage CA compliance with standards. We should find a way to
876 // get rid of this or make it off by default.
877 int offset_seconds = 0;
878 if (offset_sign != 0) {
879 if (!allow_timezone_offset) {
880 return 0;
881 }
882 int offset_hours, offset_minutes;
883 if (!cbs_get_two_digits(&copy, &offset_hours) ||
884 offset_hours > 23 || // Reject invalid hours.
885 !cbs_get_two_digits(&copy, &offset_minutes) ||
886 offset_minutes > 59) { // Reject invalid minutes.
887 return 0;
888 }
889 offset_seconds = offset_sign * (offset_hours * 3600 + offset_minutes * 60);
890 }
891
892 if (CBS_len(&copy) != 0) {
893 return 0; // Reject invalid lengths.
894 }
895
896 if (out_tm != NULL) {
897 // Fill in the tm fields corresponding to what we validated.
898 out_tm->tm_year = year - 1900;
899 out_tm->tm_mon = month - 1;
900 out_tm->tm_mday = day;
901 out_tm->tm_hour = hour;
902 out_tm->tm_min = min;
903 out_tm->tm_sec = sec;
904 if (offset_seconds && !OPENSSL_gmtime_adj(out_tm, 0, offset_seconds)) {
905 return 0;
906 }
907 }
908 return 1;
909 }
910
911 int CBS_parse_generalized_time(const CBS *cbs, struct tm *out_tm,
912 int allow_timezone_offset) {
913 return CBS_parse_rfc5280_time_internal(cbs, 1, allow_timezone_offset, out_tm);
914 }
915
916 int CBS_parse_utc_time(const CBS *cbs, struct tm *out_tm,
917 int allow_timezone_offset) {
918 return CBS_parse_rfc5280_time_internal(cbs, 0, allow_timezone_offset, out_tm);
919 }
920
921 int CBS_get_optional_asn1_int64(CBS *cbs, int64_t *out, CBS_ASN1_TAG tag,
922 int64_t default_value) {
923 CBS child;
924 int present;
925 if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
926 return 0;
927 }
928 if (present) {
929 if (!CBS_get_asn1_int64(&child, out) || CBS_len(&child) != 0) {
930 return 0;
931 }
932 } else {
933 *out = default_value;
934 }
935 return 1;
936 }