Coverage Report

Created: 2025-12-07 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/bytestring/cbs.cc
Line
Count
Source
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.27G
static int cbs_get(CBS *cbs, const uint8_t **p, size_t n) {
30
1.27G
  if (cbs->len < n) {
31
7.49M
    return 0;
32
7.49M
  }
33
34
1.26G
  *p = cbs->data;
35
1.26G
  cbs->data += n;
36
1.26G
  cbs->len -= n;
37
1.26G
  return 1;
38
1.27G
}
39
40
24.2M
int CBS_skip(CBS *cbs, size_t len) {
41
24.2M
  const uint8_t *dummy;
42
24.2M
  return cbs_get(cbs, &dummy, len);
43
24.2M
}
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 = nullptr;
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 == nullptr) {
55
0
    return 0;
56
0
  }
57
0
  *out_len = cbs->len;
58
0
  return 1;
59
0
}
60
61
14.4k
int CBS_strdup(const CBS *cbs, char **out_ptr) {
62
14.4k
  if (*out_ptr != nullptr) {
63
0
    OPENSSL_free(*out_ptr);
64
0
  }
65
14.4k
  *out_ptr = OPENSSL_strndup((const char *)cbs->data, cbs->len);
66
14.4k
  return (*out_ptr != nullptr);
67
14.4k
}
68
69
14.4k
int CBS_contains_zero_byte(const CBS *cbs) {
70
14.4k
  return OPENSSL_memchr(cbs->data, 0, cbs->len) != nullptr;
71
14.4k
}
72
73
81.5k
int CBS_mem_equal(const CBS *cbs, const uint8_t *data, size_t len) {
74
81.5k
  if (len != cbs->len) {
75
1.22k
    return 0;
76
1.22k
  }
77
80.2k
  return CRYPTO_memcmp(cbs->data, data, len) == 0;
78
81.5k
}
79
80
30.8M
static int cbs_get_u(CBS *cbs, uint64_t *out, size_t len) {
81
30.8M
  uint64_t result = 0;
82
30.8M
  const uint8_t *data;
83
84
30.8M
  if (!cbs_get(cbs, &data, len)) {
85
53.5k
    return 0;
86
53.5k
  }
87
94.3M
  for (size_t i = 0; i < len; i++) {
88
63.4M
    result <<= 8;
89
63.4M
    result |= data[i];
90
63.4M
  }
91
30.8M
  *out = result;
92
30.8M
  return 1;
93
30.8M
}
94
95
1.18G
int CBS_get_u8(CBS *cbs, uint8_t *out) {
96
1.18G
  const uint8_t *v;
97
1.18G
  if (!cbs_get(cbs, &v, 1)) {
98
6.40M
    return 0;
99
6.40M
  }
100
1.17G
  *out = *v;
101
1.17G
  return 1;
102
1.18G
}
103
104
20.7M
int CBS_get_u16(CBS *cbs, uint16_t *out) {
105
20.7M
  uint64_t v;
106
20.7M
  if (!cbs_get_u(cbs, &v, 2)) {
107
19.4k
    return 0;
108
19.4k
  }
109
20.7M
  *out = v;
110
20.7M
  return 1;
111
20.7M
}
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
1.88M
int CBS_get_u24(CBS *cbs, uint32_t *out) {
122
1.88M
  uint64_t v;
123
1.88M
  if (!cbs_get_u(cbs, &v, 3)) {
124
19.9k
    return 0;
125
19.9k
  }
126
1.86M
  *out = (uint32_t)v;
127
1.86M
  return 1;
128
1.88M
}
129
130
301k
int CBS_get_u32(CBS *cbs, uint32_t *out) {
131
301k
  uint64_t v;
132
301k
  if (!cbs_get_u(cbs, &v, 4)) {
133
367
    return 0;
134
367
  }
135
300k
  *out = (uint32_t)v;
136
300k
  return 1;
137
301k
}
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
76.2k
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
293k
int CBS_get_last_u8(CBS *cbs, uint8_t *out) {
158
293k
  if (cbs->len == 0) {
159
88
    return 0;
160
88
  }
161
293k
  *out = cbs->data[cbs->len - 1];
162
293k
  cbs->len--;
163
293k
  return 1;
164
293k
}
165
166
32.3M
int CBS_get_bytes(CBS *cbs, CBS *out, size_t len) {
167
32.3M
  const uint8_t *v;
168
32.3M
  if (!cbs_get(cbs, &v, len)) {
169
1.03M
    return 0;
170
1.03M
  }
171
31.3M
  CBS_init(out, v, len);
172
31.3M
  return 1;
173
32.3M
}
174
175
367
int CBS_copy_bytes(CBS *cbs, uint8_t *out, size_t len) {
176
367
  const uint8_t *v;
177
367
  if (!cbs_get(cbs, &v, len)) {
178
0
    return 0;
179
0
  }
180
367
  OPENSSL_memcpy(out, v, len);
181
367
  return 1;
182
367
}
183
184
4.23M
static int cbs_get_length_prefixed(CBS *cbs, CBS *out, size_t len_len) {
185
4.23M
  uint64_t len;
186
4.23M
  if (!cbs_get_u(cbs, &len, len_len)) {
187
9.96k
    return 0;
188
9.96k
  }
189
  // If |len_len| <= 3 then we know that |len| will fit into a |size_t|, even on
190
  // 32-bit systems.
191
4.23M
  assert(len_len <= 3);
192
4.22M
  return CBS_get_bytes(cbs, out, len);
193
4.22M
}
194
195
426k
int CBS_get_u8_length_prefixed(CBS *cbs, CBS *out) {
196
426k
  return cbs_get_length_prefixed(cbs, out, 1);
197
426k
}
198
199
3.26M
int CBS_get_u16_length_prefixed(CBS *cbs, CBS *out) {
200
3.26M
  return cbs_get_length_prefixed(cbs, out, 2);
201
3.26M
}
202
203
539k
int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) {
204
539k
  return cbs_get_length_prefixed(cbs, out, 3);
205
539k
}
206
207
252k
int CBS_get_until_first(CBS *cbs, CBS *out, uint8_t c) {
208
252k
  const uint8_t *split = reinterpret_cast<const uint8_t *>(
209
252k
      OPENSSL_memchr(CBS_data(cbs), c, CBS_len(cbs)));
210
252k
  if (split == nullptr) {
211
3.53k
    return 0;
212
3.53k
  }
213
249k
  return CBS_get_bytes(cbs, out, split - CBS_data(cbs));
214
252k
}
215
216
58.7k
int CBS_get_u64_decimal(CBS *cbs, uint64_t *out) {
217
58.7k
  uint64_t v = 0;
218
58.7k
  int seen_digit = 0;
219
205k
  while (CBS_len(cbs) != 0) {
220
185k
    uint8_t c = CBS_data(cbs)[0];
221
185k
    if (!OPENSSL_isdigit(c)) {
222
38.1k
      break;
223
38.1k
    }
224
146k
    CBS_skip(cbs, 1);
225
146k
    if (/* Forbid stray leading zeros */
226
146k
        (v == 0 && seen_digit) ||
227
        // Check for overflow.
228
146k
        v > UINT64_MAX / 10 ||  //
229
146k
        v * 10 > UINT64_MAX - (c - '0')) {
230
147
      return 0;
231
147
    }
232
146k
    v = v * 10 + (c - '0');
233
146k
    seen_digit = 1;
234
146k
  }
235
236
58.6k
  *out = v;
237
58.6k
  return seen_digit;
238
58.7k
}
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
243k
static int parse_base128_integer(CBS *cbs, uint64_t *out) {
244
243k
  uint64_t v = 0;
245
243k
  uint8_t b;
246
716k
  do {
247
716k
    if (!CBS_get_u8(cbs, &b)) {
248
29.9k
      return 0;
249
29.9k
    }
250
686k
    if ((v >> (64 - 7)) != 0) {
251
      // The value is too large.
252
2.28k
      return 0;
253
2.28k
    }
254
684k
    if (v == 0 && b == 0x80) {
255
      // The value must be minimally encoded.
256
2.42k
      return 0;
257
2.42k
    }
258
681k
    v = (v << 7) | (b & 0x7f);
259
260
    // Values end at an octet with the high bit cleared.
261
681k
  } while (b & 0x80);
262
263
208k
  *out = v;
264
208k
  return 1;
265
243k
}
266
267
31.6M
static int parse_asn1_tag(CBS *cbs, CBS_ASN1_TAG *out) {
268
31.6M
  uint8_t tag_byte;
269
31.6M
  if (!CBS_get_u8(cbs, &tag_byte)) {
270
1.68M
    return 0;
271
1.68M
  }
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
29.9M
  CBS_ASN1_TAG tag = ((CBS_ASN1_TAG)tag_byte & 0xe0) << CBS_ASN1_TAG_SHIFT;
280
29.9M
  CBS_ASN1_TAG tag_number = tag_byte & 0x1f;
281
29.9M
  if (tag_number == 0x1f) {
282
113k
    uint64_t v;
283
113k
    if (!parse_base128_integer(cbs, &v) ||
284
        // Check the tag number is within our supported bounds.
285
79.3k
        v > CBS_ASN1_TAG_NUMBER_MASK ||
286
        // Small tag numbers should have used low tag number form, even in BER.
287
63.0k
        v < 0x1f) {
288
63.0k
      return 0;
289
63.0k
    }
290
50.5k
    tag_number = (CBS_ASN1_TAG)v;
291
50.5k
  }
292
293
29.8M
  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
29.8M
  if ((tag & ~CBS_ASN1_CONSTRUCTED) == 0) {
299
25.7k
    return 0;
300
25.7k
  }
301
302
29.8M
  *out = tag;
303
29.8M
  return 1;
304
29.8M
}
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
24.7M
                                    int *out_indefinite, int ber_ok) {
309
24.7M
  CBS header = *cbs;
310
24.7M
  CBS throwaway;
311
312
24.7M
  if (out == nullptr) {
313
2.56M
    out = &throwaway;
314
2.56M
  }
315
24.7M
  if (ber_ok) {
316
1.92M
    *out_ber_found = 0;
317
1.92M
    *out_indefinite = 0;
318
22.8M
  } else {
319
22.8M
    assert(out_ber_found == nullptr);
320
22.8M
    assert(out_indefinite == nullptr);
321
22.8M
  }
322
323
24.7M
  CBS_ASN1_TAG tag;
324
24.7M
  if (!parse_asn1_tag(&header, &tag)) {
325
35.8k
    return 0;
326
35.8k
  }
327
24.7M
  if (out_tag != nullptr) {
328
21.4M
    *out_tag = tag;
329
21.4M
  }
330
331
24.7M
  uint8_t length_byte;
332
24.7M
  if (!CBS_get_u8(&header, &length_byte)) {
333
12.5k
    return 0;
334
12.5k
  }
335
336
24.7M
  size_t header_len = CBS_len(cbs) - CBS_len(&header);
337
338
24.7M
  size_t len;
339
  // The format for the length encoding is specified in ITU-T X.690 section
340
  // 8.1.3.
341
24.7M
  if ((length_byte & 0x80) == 0) {
342
    // Short form length.
343
21.1M
    len = ((size_t)length_byte) + header_len;
344
21.1M
    if (out_header_len != nullptr) {
345
18.9M
      *out_header_len = header_len;
346
18.9M
    }
347
21.1M
  } 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
3.60M
    const size_t num_bytes = length_byte & 0x7f;
352
3.60M
    uint64_t len64;
353
354
3.60M
    if (ber_ok && (tag & CBS_ASN1_CONSTRUCTED) != 0 && num_bytes == 0) {
355
      // indefinite length
356
22.5k
      if (out_header_len != nullptr) {
357
22.5k
        *out_header_len = header_len;
358
22.5k
      }
359
22.5k
      *out_ber_found = 1;
360
22.5k
      *out_indefinite = 1;
361
22.5k
      return CBS_get_bytes(cbs, out, header_len);
362
22.5k
    }
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
3.58M
    if (num_bytes == 0 || num_bytes > 4) {
368
14.5k
      return 0;
369
14.5k
    }
370
3.56M
    if (!cbs_get_u(&header, &len64, num_bytes)) {
371
1.54k
      return 0;
372
1.54k
    }
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
3.56M
    if (len64 < 128) {
378
      // Length should have used short-form encoding.
379
4.87k
      if (ber_ok) {
380
2.07k
        *out_ber_found = 1;
381
2.80k
      } else {
382
2.80k
        return 0;
383
2.80k
      }
384
4.87k
    }
385
3.56M
    if ((len64 >> ((num_bytes - 1) * 8)) == 0) {
386
      // Length should have been at least one byte shorter.
387
3.09k
      if (ber_ok) {
388
1.31k
        *out_ber_found = 1;
389
1.78k
      } else {
390
1.78k
        return 0;
391
1.78k
      }
392
3.09k
    }
393
3.56M
    len = len64;
394
3.56M
    if (len + header_len + num_bytes < len) {
395
      // Overflow.
396
0
      return 0;
397
0
    }
398
3.56M
    len += header_len + num_bytes;
399
3.56M
    if (out_header_len != nullptr) {
400
2.42M
      *out_header_len = header_len + num_bytes;
401
2.42M
    }
402
3.56M
  }
403
404
24.7M
  return CBS_get_bytes(cbs, out, len);
405
24.7M
}
406
407
1.42M
int CBS_get_any_asn1(CBS *cbs, CBS *out, CBS_ASN1_TAG *out_tag) {
408
1.42M
  size_t header_len;
409
1.42M
  if (!CBS_get_any_asn1_element(cbs, out, out_tag, &header_len)) {
410
4.67k
    return 0;
411
4.67k
  }
412
413
1.41M
  if (out && !CBS_skip(out, header_len)) {
414
0
    assert(0);
415
0
    return 0;
416
0
  }
417
418
1.41M
  return 1;
419
1.41M
}
420
421
int CBS_get_any_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG *out_tag,
422
22.8M
                             size_t *out_header_len) {
423
22.8M
  return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len, nullptr,
424
22.8M
                                  nullptr,
425
22.8M
                                  /*ber_ok=*/0);
426
22.8M
}
427
428
int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG *out_tag,
429
                                 size_t *out_header_len, int *out_ber_found,
430
1.92M
                                 int *out_indefinite) {
431
1.92M
  int ber_found_temp;
432
1.92M
  return cbs_get_any_asn1_element(
433
1.92M
      cbs, out, out_tag, out_header_len,
434
1.92M
      out_ber_found ? out_ber_found : &ber_found_temp, out_indefinite,
435
1.92M
      /*ber_ok=*/1);
436
1.92M
}
437
438
static int cbs_get_asn1(CBS *cbs, CBS *out, CBS_ASN1_TAG tag_value,
439
14.7M
                        int skip_header) {
440
14.7M
  size_t header_len;
441
14.7M
  CBS_ASN1_TAG tag;
442
14.7M
  CBS throwaway;
443
444
14.7M
  if (out == nullptr) {
445
806k
    out = &throwaway;
446
806k
  }
447
448
14.7M
  if (!CBS_get_any_asn1_element(cbs, out, &tag, &header_len) ||
449
14.7M
      tag != tag_value) {
450
116k
    return 0;
451
116k
  }
452
453
14.6M
  if (skip_header && !CBS_skip(out, header_len)) {
454
0
    assert(0);
455
0
    return 0;
456
0
  }
457
458
14.6M
  return 1;
459
14.6M
}
460
461
14.6M
int CBS_get_asn1(CBS *cbs, CBS *out, CBS_ASN1_TAG tag_value) {
462
14.6M
  return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */);
463
14.6M
}
464
465
131k
int CBS_get_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG tag_value) {
466
131k
  return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */);
467
131k
}
468
469
6.82M
int CBS_peek_asn1_tag(const CBS *cbs, CBS_ASN1_TAG tag_value) {
470
6.82M
  CBS copy = *cbs;
471
6.82M
  CBS_ASN1_TAG actual_tag;
472
6.82M
  return parse_asn1_tag(&copy, &actual_tag) && tag_value == actual_tag;
473
6.82M
}
474
475
971k
int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) {
476
971k
  return CBS_get_asn1_uint64_with_tag(cbs, out, CBS_ASN1_INTEGER);
477
971k
}
478
479
971k
int CBS_get_asn1_uint64_with_tag(CBS *cbs, uint64_t *out, CBS_ASN1_TAG tag) {
480
971k
  CBS bytes;
481
971k
  if (!CBS_get_asn1(cbs, &bytes, tag) ||
482
963k
      !CBS_is_unsigned_asn1_integer(&bytes)) {
483
13.6k
    return 0;
484
13.6k
  }
485
486
957k
  *out = 0;
487
957k
  const uint8_t *data = CBS_data(&bytes);
488
957k
  size_t len = CBS_len(&bytes);
489
2.55M
  for (size_t i = 0; i < len; i++) {
490
1.60M
    if ((*out >> 56) != 0) {
491
      // Too large to represent as a uint64_t.
492
1.23k
      return 0;
493
1.23k
    }
494
1.60M
    *out <<= 8;
495
1.60M
    *out |= data[i];
496
1.60M
  }
497
498
956k
  return 1;
499
957k
}
500
501
0
int CBS_get_asn1_int64(CBS *cbs, int64_t *out) {
502
0
  return CBS_get_asn1_int64_with_tag(cbs, out, CBS_ASN1_INTEGER);
503
0
}
504
505
0
int CBS_get_asn1_int64_with_tag(CBS *cbs, int64_t *out, CBS_ASN1_TAG tag) {
506
0
  int is_negative;
507
0
  CBS bytes;
508
0
  if (!CBS_get_asn1(cbs, &bytes, tag) ||
509
0
      !CBS_is_valid_asn1_integer(&bytes, &is_negative)) {
510
0
    return 0;
511
0
  }
512
0
  const uint8_t *data = CBS_data(&bytes);
513
0
  const size_t len = CBS_len(&bytes);
514
0
  if (len > sizeof(int64_t)) {
515
0
    return 0;
516
0
  }
517
0
  uint8_t sign_extend[sizeof(int64_t)];
518
0
  OPENSSL_memset(sign_extend, is_negative ? 0xff : 0, sizeof(sign_extend));
519
0
  OPENSSL_memcpy(sign_extend + sizeof(int64_t) - len, data, len);
520
0
  *out = CRYPTO_load_u64_be(sign_extend);
521
0
  return 1;
522
0
}
523
524
1.17k
int CBS_get_asn1_bool(CBS *cbs, int *out) {
525
1.17k
  CBS bytes;
526
1.17k
  if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_BOOLEAN) || CBS_len(&bytes) != 1) {
527
192
    return 0;
528
192
  }
529
530
984
  const uint8_t value = *CBS_data(&bytes);
531
984
  if (value != 0 && value != 0xff) {
532
95
    return 0;
533
95
  }
534
535
889
  *out = !!value;
536
889
  return 1;
537
984
}
538
539
int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present,
540
3.72M
                          CBS_ASN1_TAG tag) {
541
3.72M
  int present = 0;
542
543
3.72M
  if (CBS_peek_asn1_tag(cbs, tag)) {
544
940k
    if (!CBS_get_asn1(cbs, out, tag)) {
545
11.4k
      return 0;
546
11.4k
    }
547
928k
    present = 1;
548
928k
  }
549
550
3.71M
  if (out_present != nullptr) {
551
3.35M
    *out_present = present;
552
3.35M
  }
553
554
3.71M
  return 1;
555
3.72M
}
556
557
int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present,
558
1.14M
                                       CBS_ASN1_TAG tag) {
559
1.14M
  CBS child;
560
1.14M
  int present;
561
1.14M
  if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
562
69
    return 0;
563
69
  }
564
1.14M
  if (present) {
565
255k
    assert(out);
566
255k
    if (!CBS_get_asn1(&child, out, CBS_ASN1_OCTETSTRING) ||
567
255k
        CBS_len(&child) != 0) {
568
80
      return 0;
569
80
    }
570
890k
  } else {
571
890k
    CBS_init(out, nullptr, 0);
572
890k
  }
573
1.14M
  if (out_present) {
574
507k
    *out_present = present;
575
507k
  }
576
1.14M
  return 1;
577
1.14M
}
578
579
int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, CBS_ASN1_TAG tag,
580
764k
                                 uint64_t default_value) {
581
764k
  CBS child;
582
764k
  int present;
583
764k
  if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
584
39
    return 0;
585
39
  }
586
764k
  if (present) {
587
201k
    if (!CBS_get_asn1_uint64(&child, out) || CBS_len(&child) != 0) {
588
46
      return 0;
589
46
    }
590
563k
  } else {
591
563k
    *out = default_value;
592
563k
  }
593
764k
  return 1;
594
764k
}
595
596
int CBS_get_optional_asn1_bool(CBS *cbs, int *out, CBS_ASN1_TAG tag,
597
507k
                               int default_value) {
598
507k
  CBS child, child2;
599
507k
  int present;
600
507k
  if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
601
32
    return 0;
602
32
  }
603
507k
  if (present) {
604
141k
    uint8_t boolean;
605
606
141k
    if (!CBS_get_asn1(&child, &child2, CBS_ASN1_BOOLEAN) ||
607
141k
        CBS_len(&child2) != 1 || CBS_len(&child) != 0) {
608
430
      return 0;
609
430
    }
610
611
141k
    boolean = CBS_data(&child2)[0];
612
141k
    if (boolean == 0) {
613
67.2k
      *out = 0;
614
73.7k
    } else if (boolean == 0xff) {
615
73.6k
      *out = 1;
616
73.6k
    } else {
617
64
      return 0;
618
64
    }
619
366k
  } else {
620
366k
    *out = default_value;
621
366k
  }
622
507k
  return 1;
623
507k
}
624
625
720
int CBS_is_valid_asn1_bitstring(const CBS *cbs) {
626
720
  CBS in = *cbs;
627
720
  uint8_t num_unused_bits;
628
720
  if (!CBS_get_u8(&in, &num_unused_bits) || num_unused_bits > 7) {
629
10
    return 0;
630
10
  }
631
632
710
  if (num_unused_bits == 0) {
633
48
    return 1;
634
48
  }
635
636
  // All num_unused_bits bits must exist and be zeros.
637
662
  uint8_t last;
638
662
  if (!CBS_get_last_u8(&in, &last) ||
639
662
      (last & ((1 << num_unused_bits) - 1)) != 0) {
640
14
    return 0;
641
14
  }
642
643
648
  return 1;
644
662
}
645
646
348
int CBS_asn1_bitstring_has_bit(const CBS *cbs, unsigned bit) {
647
348
  if (!CBS_is_valid_asn1_bitstring(cbs)) {
648
0
    return 0;
649
0
  }
650
651
348
  const unsigned byte_num = (bit >> 3) + 1;
652
348
  const unsigned bit_num = 7 - (bit & 7);
653
654
  // Unused bits are zero, and this function does not distinguish between
655
  // missing and unset bits. Thus it is sufficient to do a byte-level length
656
  // check.
657
348
  return byte_num < CBS_len(cbs) &&
658
348
         (CBS_data(cbs)[byte_num] & (1 << bit_num)) != 0;
659
348
}
660
661
1.54M
int CBS_is_valid_asn1_integer(const CBS *cbs, int *out_is_negative) {
662
1.54M
  CBS copy = *cbs;
663
1.54M
  uint8_t first_byte, second_byte;
664
1.54M
  if (!CBS_get_u8(&copy, &first_byte)) {
665
6.83k
    return 0;  // INTEGERs may not be empty.
666
6.83k
  }
667
1.53M
  if (out_is_negative != nullptr) {
668
1.53M
    *out_is_negative = (first_byte & 0x80) != 0;
669
1.53M
  }
670
1.53M
  if (!CBS_get_u8(&copy, &second_byte)) {
671
514k
    return 1;  // One byte INTEGERs are always minimal.
672
514k
  }
673
1.02M
  if ((first_byte == 0x00 && (second_byte & 0x80) == 0) ||
674
1.02M
      (first_byte == 0xff && (second_byte & 0x80) != 0)) {
675
4.56k
    return 0;  // The value is minimal iff the first 9 bits are not all equal.
676
4.56k
  }
677
1.01M
  return 1;
678
1.02M
}
679
680
966k
int CBS_is_unsigned_asn1_integer(const CBS *cbs) {
681
966k
  int is_negative;
682
966k
  return CBS_is_valid_asn1_integer(cbs, &is_negative) && !is_negative;
683
966k
}
684
685
139k
static int add_decimal(CBB *out, uint64_t v) {
686
139k
  char buf[DECIMAL_SIZE(uint64_t) + 1];
687
139k
  snprintf(buf, sizeof(buf), "%" PRIu64, v);
688
139k
  return CBB_add_bytes(out, (const uint8_t *)buf, strlen(buf));
689
139k
}
690
691
1.96M
int CBS_is_valid_asn1_oid(const CBS *cbs) {
692
1.96M
  if (CBS_len(cbs) == 0) {
693
90
    return 0;  // OID encodings cannot be empty.
694
90
  }
695
696
1.96M
  CBS copy = *cbs;
697
1.96M
  uint8_t v, prev = 0;
698
17.5M
  while (CBS_get_u8(&copy, &v)) {
699
    // OID encodings are a sequence of minimally-encoded base-128 integers (see
700
    // |parse_base128_integer|). If |prev|'s MSB was clear, it was the last byte
701
    // of an integer (or |v| is the first byte). |v| is then the first byte of
702
    // the next integer. If first byte of an integer is 0x80, it is not
703
    // minimally-encoded.
704
15.6M
    if ((prev & 0x80) == 0 && v == 0x80) {
705
76
      return 0;
706
76
    }
707
15.6M
    prev = v;
708
15.6M
  }
709
710
  // The last byte should must end an integer encoding.
711
1.96M
  return (prev & 0x80) == 0;
712
1.96M
}
713
714
26.0k
char *CBS_asn1_oid_to_text(const CBS *cbs) {
715
26.0k
  CBS copy = *cbs;
716
26.0k
  CBB cbb;
717
26.0k
  if (!CBB_init(&cbb, 32)) {
718
0
    goto err;
719
0
  }
720
721
  // The first component is 40 * value1 + value2, where value1 is 0, 1, or 2.
722
26.0k
  uint64_t v;
723
26.0k
  if (!parse_base128_integer(&copy, &v)) {
724
219
    goto err;
725
219
  }
726
727
25.8k
  if (v >= 80) {
728
14.9k
    if (!CBB_add_bytes(&cbb, (const uint8_t *)"2.", 2) ||
729
14.9k
        !add_decimal(&cbb, v - 80)) {
730
0
      goto err;
731
0
    }
732
14.9k
  } else if (!add_decimal(&cbb, v / 40) || !CBB_add_u8(&cbb, '.') ||
733
10.8k
             !add_decimal(&cbb, v % 40)) {
734
0
    goto err;
735
0
  }
736
737
129k
  while (CBS_len(&copy) != 0) {
738
103k
    if (!parse_base128_integer(&copy, &v) || !CBB_add_u8(&cbb, '.') ||
739
103k
        !add_decimal(&cbb, v)) {
740
290
      goto err;
741
290
    }
742
103k
  }
743
744
25.5k
  uint8_t *txt;
745
25.5k
  size_t txt_len;
746
25.5k
  if (!CBB_add_u8(&cbb, '\0') || !CBB_finish(&cbb, &txt, &txt_len)) {
747
0
    goto err;
748
0
  }
749
750
25.5k
  return (char *)txt;
751
752
509
err:
753
509
  CBB_cleanup(&cbb);
754
509
  return nullptr;
755
25.5k
}
756
757
0
int CBS_is_valid_asn1_relative_oid(const CBS *cbs) {
758
0
  return CBS_is_valid_asn1_oid(cbs);
759
0
}
760
761
0
char *CBS_asn1_relative_oid_to_text(const CBS *cbs) {
762
0
  CBS copy = *cbs;
763
0
  bssl::ScopedCBB cbb;
764
0
  if (!CBB_init(cbb.get(), 32)) {
765
0
    return nullptr;
766
0
  }
767
768
  // Relative OIDs must have at least one component.
769
0
  uint64_t v;
770
0
  if (!parse_base128_integer(&copy, &v) || !add_decimal(cbb.get(), v)) {
771
0
    return nullptr;
772
0
  }
773
774
0
  while (CBS_len(&copy) != 0) {
775
0
    if (!parse_base128_integer(&copy, &v) || !CBB_add_u8(cbb.get(), '.') ||
776
0
        !add_decimal(cbb.get(), v)) {
777
0
      return nullptr;
778
0
    }
779
0
  }
780
781
0
  uint8_t *txt;
782
0
  size_t txt_len;
783
0
  if (!CBB_add_u8(cbb.get(), '\0') || !CBB_finish(cbb.get(), &txt, &txt_len)) {
784
0
    return nullptr;
785
0
  }
786
787
0
  return reinterpret_cast<char *>(txt);
788
0
}
789
790
2.22M
static int cbs_get_two_digits(CBS *cbs, int *out) {
791
2.22M
  uint8_t first_digit, second_digit;
792
2.22M
  if (!CBS_get_u8(cbs, &first_digit)) {
793
402
    return 0;
794
402
  }
795
2.22M
  if (!OPENSSL_isdigit(first_digit)) {
796
1.04k
    return 0;
797
1.04k
  }
798
2.22M
  if (!CBS_get_u8(cbs, &second_digit)) {
799
315
    return 0;
800
315
  }
801
2.22M
  if (!OPENSSL_isdigit(second_digit)) {
802
809
    return 0;
803
809
  }
804
2.22M
  *out = (first_digit - '0') * 10 + (second_digit - '0');
805
2.22M
  return 1;
806
2.22M
}
807
808
370k
static int is_valid_day(int year, int month, int day) {
809
370k
  if (day < 1) {
810
38
    return 0;
811
38
  }
812
370k
  switch (month) {
813
4.52k
    case 1:
814
9.49k
    case 3:
815
58.9k
    case 5:
816
64.5k
    case 7:
817
70.2k
    case 8:
818
74.8k
    case 10:
819
132k
    case 12:
820
132k
      return day <= 31;
821
118k
    case 4:
822
121k
    case 6:
823
123k
    case 9:
824
170k
    case 11:
825
170k
      return day <= 30;
826
67.6k
    case 2:
827
67.6k
      if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
828
33.0k
        return day <= 29;
829
34.5k
      } else {
830
34.5k
        return day <= 28;
831
34.5k
      }
832
0
    default:
833
0
      return 0;
834
370k
  }
835
370k
}
836
837
static int CBS_parse_rfc5280_time_internal(const CBS *cbs, int is_gentime,
838
                                           int allow_timezone_offset,
839
372k
                                           struct tm *out_tm) {
840
372k
  int year, month, day, hour, min, sec, tmp;
841
372k
  CBS copy = *cbs;
842
372k
  uint8_t tz;
843
844
372k
  if (is_gentime) {
845
4.00k
    if (!cbs_get_two_digits(&copy, &tmp)) {
846
466
      return 0;
847
466
    }
848
3.53k
    year = tmp * 100;
849
3.53k
    if (!cbs_get_two_digits(&copy, &tmp)) {
850
452
      return 0;
851
452
    }
852
3.08k
    year += tmp;
853
368k
  } else {
854
368k
    year = 1900;
855
368k
    if (!cbs_get_two_digits(&copy, &tmp)) {
856
229
      return 0;
857
229
    }
858
367k
    year += tmp;
859
367k
    if (year < 1950) {
860
353k
      year += 100;
861
353k
    }
862
367k
    if (year >= 2050) {
863
0
      return 0;  // A Generalized time must be used.
864
0
    }
865
367k
  }
866
371k
  if (!cbs_get_two_digits(&copy, &month) || month < 1 ||
867
370k
      month > 12 ||  // Reject invalid months.
868
370k
      !cbs_get_two_digits(&copy, &day) ||
869
370k
      !is_valid_day(year, month, day) ||  // Reject invalid days.
870
369k
      !cbs_get_two_digits(&copy, &hour) ||
871
369k
      hour > 23 ||  // Reject invalid hours.
872
369k
      !cbs_get_two_digits(&copy, &min) ||
873
369k
      min > 59 ||  // Reject invalid minutes.
874
369k
      !cbs_get_two_digits(&copy, &sec) || sec > 59 || !CBS_get_u8(&copy, &tz)) {
875
2.05k
    return 0;
876
2.05k
  }
877
878
369k
  int offset_sign = 0;
879
369k
  switch (tz) {
880
367k
    case 'Z':
881
367k
      break;  // We correctly have 'Z' on the end as per spec.
882
894
    case '+':
883
894
      offset_sign = 1;
884
894
      break;  // Should not be allowed per RFC 5280.
885
564
    case '-':
886
564
      offset_sign = -1;
887
564
      break;  // Should not be allowed per RFC 5280.
888
140
    default:
889
140
      return 0;  // Reject anything else after the time.
890
369k
  }
891
892
  // If allow_timezone_offset is non-zero, allow for a four digit timezone
893
  // offset to be specified even though this is not allowed by RFC 5280. We are
894
  // permissive of this for UTCTimes due to the unfortunate existence of
895
  // artisinally rolled long lived certificates that were baked into places that
896
  // are now difficult to change. These certificates were generated with the
897
  // 'openssl' command that permissively allowed the creation of certificates
898
  // with notBefore and notAfter times specified as strings for direct
899
  // certificate inclusion on the command line. For context see cl/237068815.
900
  //
901
  // TODO(bbe): This has been expunged from public web-pki as the ecosystem has
902
  // managed to encourage CA compliance with standards. We should find a way to
903
  // get rid of this or make it off by default.
904
368k
  int offset_seconds = 0;
905
368k
  if (offset_sign != 0) {
906
1.45k
    if (!allow_timezone_offset) {
907
22
      return 0;
908
22
    }
909
1.43k
    int offset_hours, offset_minutes;
910
1.43k
    if (!cbs_get_two_digits(&copy, &offset_hours) ||
911
1.37k
        offset_hours > 23 ||  // Reject invalid hours.
912
1.35k
        !cbs_get_two_digits(&copy, &offset_minutes) ||
913
1.32k
        offset_minutes > 59) {  // Reject invalid minutes.
914
143
      return 0;
915
143
    }
916
1.29k
    offset_seconds = offset_sign * (offset_hours * 3600 + offset_minutes * 60);
917
1.29k
  }
918
919
368k
  if (CBS_len(&copy) != 0) {
920
122
    return 0;  // Reject invalid lengths.
921
122
  }
922
923
368k
  if (out_tm != nullptr) {
924
    // Fill in the tm fields corresponding to what we validated.
925
5.21k
    out_tm->tm_year = year - 1900;
926
5.21k
    out_tm->tm_mon = month - 1;
927
5.21k
    out_tm->tm_mday = day;
928
5.21k
    out_tm->tm_hour = hour;
929
5.21k
    out_tm->tm_min = min;
930
5.21k
    out_tm->tm_sec = sec;
931
5.21k
    if (offset_seconds && !OPENSSL_gmtime_adj(out_tm, 0, offset_seconds)) {
932
0
      return 0;
933
0
    }
934
5.21k
  }
935
368k
  return 1;
936
368k
}
937
938
int CBS_parse_generalized_time(const CBS *cbs, struct tm *out_tm,
939
4.00k
                               int allow_timezone_offset) {
940
4.00k
  return CBS_parse_rfc5280_time_internal(cbs, 1, allow_timezone_offset, out_tm);
941
4.00k
}
942
943
int CBS_parse_utc_time(const CBS *cbs, struct tm *out_tm,
944
368k
                       int allow_timezone_offset) {
945
368k
  return CBS_parse_rfc5280_time_internal(cbs, 0, allow_timezone_offset, out_tm);
946
368k
}