Coverage Report

Created: 2023-06-07 07:11

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