Coverage Report

Created: 2026-06-15 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/pki/parse_values.cc
Line
Count
Source
1
// Copyright 2015 The Chromium 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 "parse_values.h"
16
17
#include <stdlib.h>
18
19
#include <tuple>
20
21
#include <openssl/base.h>
22
#include <openssl/bytestring.h>
23
#include <openssl/mem.h>
24
25
BSSL_NAMESPACE_BEGIN
26
namespace der {
27
28
namespace {
29
30
2.35k
bool ParseBoolInternal(Input in, bool *out, bool relaxed) {
31
  // According to ITU-T X.690 section 8.2, a bool is encoded as a single octet
32
  // where the octet of all zeroes is FALSE and a non-zero value for the octet
33
  // is TRUE.
34
2.35k
  if (in.size() != 1) {
35
52
    return false;
36
52
  }
37
2.30k
  ByteReader data(in);
38
2.30k
  uint8_t byte;
39
2.30k
  if (!data.ReadByte(&byte)) {
40
0
    return false;
41
0
  }
42
2.30k
  if (byte == 0) {
43
11
    *out = false;
44
11
    return true;
45
11
  }
46
  // ITU-T X.690 section 11.1 specifies that for DER, the TRUE value must be
47
  // encoded as an octet of all ones.
48
2.29k
  if (byte == 0xff || relaxed) {
49
2.25k
    *out = true;
50
2.25k
    return true;
51
2.25k
  }
52
37
  return false;
53
2.29k
}
54
55
// Reads a positive decimal number with `digits` digits and stores it in
56
// `*out`. This function does not check that the type of `*out` is large
57
// enough to hold 10^digits - 1; the caller must choose an appropriate type
58
// based on the number of digits they wish to parse.
59
template <typename UINT>
60
82.1k
bool DecimalStringToUint(ByteReader &in, size_t digits, UINT *out) {
61
82.1k
  UINT value = 0;
62
254k
  for (size_t i = 0; i < digits; ++i) {
63
172k
    uint8_t digit;
64
172k
    if (!in.ReadByte(&digit)) {
65
366
      return false;
66
366
    }
67
172k
    if (digit < '0' || digit > '9') {
68
560
      return false;
69
560
    }
70
172k
    value = (value * 10) + (digit - '0');
71
172k
  }
72
81.2k
  *out = value;
73
81.2k
  return true;
74
82.1k
}
parse_values.cc:bool bssl::der::(anonymous namespace)::DecimalStringToUint<unsigned short>(bssl::der::ByteReader&, unsigned long, unsigned short*)
Line
Count
Source
60
14.1k
bool DecimalStringToUint(ByteReader &in, size_t digits, UINT *out) {
61
14.1k
  UINT value = 0;
62
51.2k
  for (size_t i = 0; i < digits; ++i) {
63
37.3k
    uint8_t digit;
64
37.3k
    if (!in.ReadByte(&digit)) {
65
81
      return false;
66
81
    }
67
37.2k
    if (digit < '0' || digit > '9') {
68
119
      return false;
69
119
    }
70
37.1k
    value = (value * 10) + (digit - '0');
71
37.1k
  }
72
13.9k
  *out = value;
73
13.9k
  return true;
74
14.1k
}
parse_values.cc:bool bssl::der::(anonymous namespace)::DecimalStringToUint<unsigned char>(bssl::der::ByteReader&, unsigned long, unsigned char*)
Line
Count
Source
60
68.0k
bool DecimalStringToUint(ByteReader &in, size_t digits, UINT *out) {
61
68.0k
  UINT value = 0;
62
202k
  for (size_t i = 0; i < digits; ++i) {
63
135k
    uint8_t digit;
64
135k
    if (!in.ReadByte(&digit)) {
65
285
      return false;
66
285
    }
67
135k
    if (digit < '0' || digit > '9') {
68
441
      return false;
69
441
    }
70
134k
    value = (value * 10) + (digit - '0');
71
134k
  }
72
67.3k
  *out = value;
73
67.3k
  return true;
74
68.0k
}
75
76
// Checks that the values in a GeneralizedTime struct are valid. This involves
77
// checking that the year is 4 digits, the month is between 1 and 12, the day
78
// is a day that exists in that month (following current leap year rules),
79
// hours are between 0 and 23, minutes between 0 and 59, and seconds between
80
// 0 and 60 (to allow for leap seconds; no validation is done that a leap
81
// second is on a day that could be a leap second).
82
13.0k
bool ValidateGeneralizedTime(const GeneralizedTime &time) {
83
13.0k
  if (time.month < 1 || time.month > 12) {
84
37
    return false;
85
37
  }
86
12.9k
  if (time.day < 1) {
87
8
    return false;
88
8
  }
89
12.9k
  if (time.hours > 23) {
90
17
    return false;
91
17
  }
92
12.9k
  if (time.minutes > 59) {
93
15
    return false;
94
15
  }
95
  // Leap seconds are allowed.
96
12.9k
  if (time.seconds > 60) {
97
15
    return false;
98
15
  }
99
100
  // validate upper bound for day of month
101
12.9k
  switch (time.month) {
102
1.77k
    case 4:
103
2.69k
    case 6:
104
3.41k
    case 9:
105
4.38k
    case 11:
106
4.38k
      if (time.day > 30) {
107
37
        return false;
108
37
      }
109
4.34k
      break;
110
4.34k
    case 1:
111
1.73k
    case 3:
112
2.38k
    case 5:
113
3.05k
    case 7:
114
4.23k
    case 8:
115
4.97k
    case 10:
116
5.66k
    case 12:
117
5.66k
      if (time.day > 31) {
118
39
        return false;
119
39
      }
120
5.62k
      break;
121
5.62k
    case 2:
122
2.87k
      if (time.year % 4 == 0 &&
123
1.82k
          (time.year % 100 != 0 || time.year % 400 == 0)) {
124
1.21k
        if (time.day > 29) {
125
22
          return false;
126
22
        }
127
1.65k
      } else {
128
1.65k
        if (time.day > 28) {
129
33
          return false;
130
33
        }
131
1.65k
      }
132
2.82k
      break;
133
2.82k
    default:
134
0
      abort();
135
12.9k
  }
136
12.7k
  return true;
137
12.9k
}
138
139
// Returns the number of bytes of numeric precision in a DER encoded INTEGER
140
// value. `in` must be a valid DER encoding of an INTEGER for this to work.
141
//
142
// Normally the precision of the number is exactly in.size(). However when
143
// encoding positive numbers using DER it is possible to have a leading zero
144
// (to prevent number from being interpreted as negative).
145
//
146
// For instance a 160-bit positive number might take 21 bytes to encode. This
147
// function will return 20 in such a case.
148
3.23k
size_t GetUnsignedIntegerLength(Input in) {
149
3.23k
  der::ByteReader reader(in);
150
3.23k
  uint8_t first_byte;
151
3.23k
  if (!reader.ReadByte(&first_byte)) {
152
0
    return 0;  // Not valid DER  as `in` was empty.
153
0
  }
154
155
3.23k
  if (first_byte == 0 && in.size() > 1) {
156
522
    return in.size() - 1;
157
522
  }
158
2.71k
  return in.size();
159
3.23k
}
160
161
}  // namespace
162
163
2.35k
bool ParseBool(Input in, bool *out) {
164
2.35k
  return ParseBoolInternal(in, out, false /* relaxed */);
165
2.35k
}
166
167
// BER interprets any non-zero value as true, while DER requires a bool to
168
// have either all bits zero (false) or all bits one (true). To support
169
// malformed certs, we recognized the BER encoding instead of failing to
170
// parse.
171
0
bool ParseBoolRelaxed(Input in, bool *out) {
172
0
  return ParseBoolInternal(in, out, true /* relaxed */);
173
0
}
174
175
// ITU-T X.690 section 8.3.2 specifies that an integer value must be encoded
176
// in the smallest number of octets. If the encoding consists of more than
177
// one octet, then the bits of the first octet and the most significant bit
178
// of the second octet must not be all zeroes or all ones.
179
5.87k
bool IsValidInteger(Input in, bool *negative) {
180
5.87k
  CBS cbs;
181
5.87k
  CBS_init(&cbs, in.data(), in.size());
182
5.87k
  int negative_int;
183
5.87k
  if (!CBS_is_valid_asn1_integer(&cbs, &negative_int)) {
184
37
    return false;
185
37
  }
186
187
5.83k
  *negative = !!negative_int;
188
5.83k
  return true;
189
5.87k
}
190
191
3.31k
bool ParseUint64(Input in, uint64_t *out) {
192
  // Reject non-minimally encoded numbers and negative numbers.
193
3.31k
  bool negative;
194
3.31k
  if (!IsValidInteger(in, &negative) || negative) {
195
78
    return false;
196
78
  }
197
198
  // Reject (non-negative) integers whose value would overflow the output type.
199
3.23k
  if (GetUnsignedIntegerLength(in) > sizeof(*out)) {
200
169
    return false;
201
169
  }
202
203
3.06k
  ByteReader reader(in);
204
3.06k
  uint8_t data;
205
3.06k
  uint64_t value = 0;
206
207
10.5k
  while (reader.ReadByte(&data)) {
208
7.43k
    value <<= 8;
209
7.43k
    value |= data;
210
7.43k
  }
211
3.06k
  *out = value;
212
3.06k
  return true;
213
3.23k
}
214
215
1.25k
bool ParseUint8(Input in, uint8_t *out) {
216
  // TODO(eroman): Implement this more directly.
217
1.25k
  uint64_t value;
218
1.25k
  if (!ParseUint64(in, &value)) {
219
144
    return false;
220
144
  }
221
222
1.11k
  if (value > 0xFF) {
223
449
    return false;
224
449
  }
225
226
665
  *out = static_cast<uint8_t>(value);
227
665
  return true;
228
1.11k
}
229
230
BitString::BitString(Input bytes, uint8_t unused_bits)
231
3.34k
    : bytes_(bytes), unused_bits_(unused_bits) {
232
3.34k
  BSSL_CHECK(unused_bits < 8);
233
3.34k
  BSSL_CHECK(unused_bits == 0 || !bytes.empty());
234
  // The unused bits must be zero.
235
3.34k
  BSSL_CHECK(bytes.empty() || (bytes.back() & ((1u << unused_bits) - 1)) == 0);
236
3.34k
}
237
238
0
bool BitString::AssertsBit(size_t bit_index) const {
239
  // Index of the byte that contains the bit.
240
0
  size_t byte_index = bit_index / 8;
241
242
  // If the bit is outside of the bitstring, by definition it is not
243
  // asserted.
244
0
  if (byte_index >= bytes_.size()) {
245
0
    return false;
246
0
  }
247
248
  // Within a byte, bits are ordered from most significant to least significant.
249
  // Convert `bit_index` to an index within the `byte_index` byte, measured from
250
  // its least significant bit.
251
0
  uint8_t bit_index_in_byte = 7 - (bit_index - byte_index * 8);
252
253
  // BIT STRING parsing already guarantees that unused bits in a byte are zero
254
  // (otherwise it wouldn't be valid DER). Therefore it isn't necessary to check
255
  // `unused_bits_`
256
0
  uint8_t byte = bytes_[byte_index];
257
0
  return 0 != (byte & (1 << bit_index_in_byte));
258
0
}
259
260
3.41k
std::optional<BitString> ParseBitString(Input in) {
261
3.41k
  ByteReader reader(in);
262
263
  // From ITU-T X.690, section 8.6.2.2 (applies to BER, CER, DER):
264
  //
265
  // The initial octet shall encode, as an unsigned binary integer with
266
  // bit 1 as the least significant bit, the number of unused bits in the final
267
  // subsequent octet. The number shall be in the range zero to seven.
268
3.41k
  uint8_t unused_bits;
269
3.41k
  if (!reader.ReadByte(&unused_bits)) {
270
6
    return std::nullopt;
271
6
  }
272
3.41k
  if (unused_bits > 7) {
273
21
    return std::nullopt;
274
21
  }
275
276
3.39k
  Input bytes;
277
3.39k
  if (!reader.ReadBytes(reader.BytesLeft(), &bytes)) {
278
0
    return std::nullopt;  // Not reachable.
279
0
  }
280
281
  // Ensure that unused bits in the last byte are set to 0.
282
3.39k
  if (unused_bits > 0) {
283
    // From ITU-T X.690, section 8.6.2.3 (applies to BER, CER, DER):
284
    //
285
    // If the bitstring is empty, there shall be no subsequent octets,
286
    // and the initial octet shall be zero.
287
611
    if (bytes.empty()) {
288
11
      return std::nullopt;
289
11
    }
290
600
    uint8_t last_byte = bytes.back();
291
292
    // From ITU-T X.690, section 11.2.1 (applies to CER and DER, but not BER):
293
    //
294
    // Each unused bit in the final octet of the encoding of a bit string value
295
    // shall be set to zero.
296
600
    uint8_t mask = 0xFF >> (8 - unused_bits);
297
600
    if ((mask & last_byte) != 0) {
298
34
      return std::nullopt;
299
34
    }
300
600
  }
301
302
3.34k
  return BitString(bytes, unused_bits);
303
3.39k
}
304
305
0
bool GeneralizedTime::InUTCTimeRange() const {
306
0
  return 1950 <= year && year < 2050;
307
0
}
308
309
0
bool operator<(const GeneralizedTime &lhs, const GeneralizedTime &rhs) {
310
0
  return std::tie(lhs.year, lhs.month, lhs.day, lhs.hours, lhs.minutes,
311
0
                  lhs.seconds) < std::tie(rhs.year, rhs.month, rhs.day,
312
0
                                          rhs.hours, rhs.minutes, rhs.seconds);
313
0
}
314
315
0
bool operator>(const GeneralizedTime &lhs, const GeneralizedTime &rhs) {
316
0
  return rhs < lhs;
317
0
}
318
319
0
bool operator<=(const GeneralizedTime &lhs, const GeneralizedTime &rhs) {
320
0
  return !(lhs > rhs);
321
0
}
322
323
0
bool operator>=(const GeneralizedTime &lhs, const GeneralizedTime &rhs) {
324
0
  return !(lhs < rhs);
325
0
}
326
327
9.41k
bool ParseUTCTime(Input in, GeneralizedTime *value) {
328
9.41k
  ByteReader reader(in);
329
9.41k
  GeneralizedTime time;
330
9.41k
  if (!DecimalStringToUint(reader, 2, &time.year) ||
331
9.34k
      !DecimalStringToUint(reader, 2, &time.month) ||
332
9.28k
      !DecimalStringToUint(reader, 2, &time.day) ||
333
9.22k
      !DecimalStringToUint(reader, 2, &time.hours) ||
334
9.16k
      !DecimalStringToUint(reader, 2, &time.minutes) ||
335
9.10k
      !DecimalStringToUint(reader, 2, &time.seconds)) {
336
361
    return false;
337
361
  }
338
9.04k
  uint8_t zulu;
339
9.04k
  if (!reader.ReadByte(&zulu) || zulu != 'Z' || reader.HasMore()) {
340
76
    return false;
341
76
  }
342
8.97k
  if (time.year < 50) {
343
5.51k
    time.year += 2000;
344
5.51k
  } else {
345
3.46k
    time.year += 1900;
346
3.46k
  }
347
8.97k
  if (!ValidateGeneralizedTime(time)) {
348
98
    return false;
349
98
  }
350
8.87k
  *value = time;
351
8.87k
  return true;
352
8.97k
}
353
354
4.71k
bool ParseGeneralizedTime(Input in, GeneralizedTime *value) {
355
4.71k
  ByteReader reader(in);
356
4.71k
  GeneralizedTime time;
357
4.71k
  if (!DecimalStringToUint(reader, 4, &time.year) ||
358
4.57k
      !DecimalStringToUint(reader, 2, &time.month) ||
359
4.47k
      !DecimalStringToUint(reader, 2, &time.day) ||
360
4.37k
      !DecimalStringToUint(reader, 2, &time.hours) ||
361
4.29k
      !DecimalStringToUint(reader, 2, &time.minutes) ||
362
4.21k
      !DecimalStringToUint(reader, 2, &time.seconds)) {
363
565
    return false;
364
565
  }
365
4.14k
  uint8_t zulu;
366
4.14k
  if (!reader.ReadByte(&zulu) || zulu != 'Z' || reader.HasMore()) {
367
104
    return false;
368
104
  }
369
4.04k
  if (!ValidateGeneralizedTime(time)) {
370
125
    return false;
371
125
  }
372
3.91k
  *value = time;
373
3.91k
  return true;
374
4.04k
}
375
376
0
bool ParseIA5String(Input in, std::string *out) {
377
0
  for (uint8_t c : in) {
378
0
    if (c > 127) {
379
0
      return false;
380
0
    }
381
0
  }
382
0
  *out = BytesAsStringView(in);
383
0
  return true;
384
0
}
385
386
0
bool ParseVisibleString(Input in, std::string *out) {
387
  // ITU-T X.680:
388
  // VisibleString : "Defining registration number 6" + SPACE
389
  // 6 includes all the characters from '!' .. '~' (33 .. 126), space is 32.
390
  // Also ITU-T X.691 says it much more clearly:
391
  // "for VisibleString [the range] is 32 to 126 ... For VisibleString .. all
392
  // the values in the range are present."
393
0
  for (uint8_t c : in) {
394
0
    if (c < 32 || c > 126) {
395
0
      return false;
396
0
    }
397
0
  }
398
0
  *out = BytesAsStringView(in);
399
0
  return true;
400
0
}
401
402
0
bool ParsePrintableString(Input in, std::string *out) {
403
0
  for (uint8_t c : in) {
404
0
    if (!(OPENSSL_isalpha(c) || c == ' ' || (c >= '\'' && c <= ':') ||
405
0
          c == '=' || c == '?')) {
406
0
      return false;
407
0
    }
408
0
  }
409
0
  *out = BytesAsStringView(in);
410
0
  return true;
411
0
}
412
413
0
bool ParseTeletexStringAsLatin1(Input in, std::string *out) {
414
0
  out->clear();
415
  // Convert from Latin-1 to UTF-8.
416
0
  size_t utf8_length = in.size();
417
0
  for (size_t i = 0; i < in.size(); i++) {
418
0
    if (in[i] > 0x7f) {
419
0
      utf8_length++;
420
0
    }
421
0
  }
422
0
  out->reserve(utf8_length);
423
0
  for (size_t i = 0; i < in.size(); i++) {
424
0
    uint8_t u = in[i];
425
0
    if (u <= 0x7f) {
426
0
      out->push_back(u);
427
0
    } else {
428
0
      out->push_back(0xc0 | (u >> 6));
429
0
      out->push_back(0x80 | (u & 0x3f));
430
0
    }
431
0
  }
432
0
  BSSL_CHECK(utf8_length == out->size());
433
0
  return true;
434
0
}
435
436
8.66k
bool ParseUniversalString(Input in, std::string *out) {
437
8.66k
  if (in.size() % 4 != 0) {
438
200
    return false;
439
200
  }
440
441
8.46k
  CBS cbs;
442
8.46k
  CBS_init(&cbs, in.data(), in.size());
443
8.46k
  bssl::ScopedCBB cbb;
444
8.46k
  if (!CBB_init(cbb.get(), in.size())) {
445
0
    return false;
446
0
  }
447
448
18.3k
  while (CBS_len(&cbs) != 0) {
449
10.6k
    uint32_t c;
450
10.6k
    if (!CBS_get_utf32_be(&cbs, &c) ||  //
451
9.89k
        !CBB_add_utf8(cbb.get(), c)) {
452
768
      return false;
453
768
    }
454
10.6k
  }
455
456
7.69k
  out->assign(CBB_data(cbb.get()), CBB_data(cbb.get()) + CBB_len(cbb.get()));
457
7.69k
  return true;
458
8.46k
}
459
460
139k
bool ParseBmpString(Input in, std::string *out) {
461
139k
  if (in.size() % 2 != 0) {
462
193
    return false;
463
193
  }
464
465
139k
  CBS cbs;
466
139k
  CBS_init(&cbs, in.data(), in.size());
467
139k
  bssl::ScopedCBB cbb;
468
139k
  if (!CBB_init(cbb.get(), in.size())) {
469
0
    return false;
470
0
  }
471
472
5.51M
  while (CBS_len(&cbs) != 0) {
473
5.37M
    uint32_t c;
474
5.37M
    if (!CBS_get_ucs2_be(&cbs, &c) ||  //
475
5.37M
        !CBB_add_utf8(cbb.get(), c)) {
476
634
      return false;
477
634
    }
478
5.37M
  }
479
480
138k
  out->assign(CBB_data(cbb.get()), CBB_data(cbb.get()) + CBB_len(cbb.get()));
481
138k
  return true;
482
139k
}
483
484
}  // namespace der
485
BSSL_NAMESPACE_END