Coverage Report

Created: 2025-06-11 06:40

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