Coverage Report

Created: 2025-11-17 06:18

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
1.91k
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
1.91k
  if (in.size() != 1) {
35
45
    return false;
36
45
  }
37
1.86k
  ByteReader data(in);
38
1.86k
  uint8_t byte;
39
1.86k
  if (!data.ReadByte(&byte)) {
40
0
    return false;
41
0
  }
42
1.86k
  if (byte == 0) {
43
10
    *out = false;
44
10
    return true;
45
10
  }
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
1.85k
  if (byte == 0xff || relaxed) {
49
1.82k
    *out = true;
50
1.82k
    return true;
51
1.82k
  }
52
37
  return false;
53
1.85k
}
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
75.1k
bool DecimalStringToUint(ByteReader &in, size_t digits, UINT *out) {
61
75.1k
  UINT value = 0;
62
232k
  for (size_t i = 0; i < digits; ++i) {
63
157k
    uint8_t digit;
64
157k
    if (!in.ReadByte(&digit)) {
65
332
      return false;
66
332
    }
67
157k
    if (digit < '0' || digit > '9') {
68
513
      return false;
69
513
    }
70
156k
    value = (value * 10) + (digit - '0');
71
156k
  }
72
74.3k
  *out = value;
73
74.3k
  return true;
74
75.1k
}
parse_values.cc:bool bssl::der::(anonymous namespace)::DecimalStringToUint<unsigned short>(bssl::der::ByteReader&, unsigned long, unsigned short*)
Line
Count
Source
60
12.9k
bool DecimalStringToUint(ByteReader &in, size_t digits, UINT *out) {
61
12.9k
  UINT value = 0;
62
46.4k
  for (size_t i = 0; i < digits; ++i) {
63
33.6k
    uint8_t digit;
64
33.6k
    if (!in.ReadByte(&digit)) {
65
76
      return false;
66
76
    }
67
33.6k
    if (digit < '0' || digit > '9') {
68
111
      return false;
69
111
    }
70
33.5k
    value = (value * 10) + (digit - '0');
71
33.5k
  }
72
12.7k
  *out = value;
73
12.7k
  return true;
74
12.9k
}
parse_values.cc:bool bssl::der::(anonymous namespace)::DecimalStringToUint<unsigned char>(bssl::der::ByteReader&, unsigned long, unsigned char*)
Line
Count
Source
60
62.2k
bool DecimalStringToUint(ByteReader &in, size_t digits, UINT *out) {
61
62.2k
  UINT value = 0;
62
185k
  for (size_t i = 0; i < digits; ++i) {
63
124k
    uint8_t digit;
64
124k
    if (!in.ReadByte(&digit)) {
65
256
      return false;
66
256
    }
67
123k
    if (digit < '0' || digit > '9') {
68
402
      return false;
69
402
    }
70
123k
    value = (value * 10) + (digit - '0');
71
123k
  }
72
61.5k
  *out = value;
73
61.5k
  return true;
74
62.2k
}
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
11.9k
bool ValidateGeneralizedTime(const GeneralizedTime &time) {
83
11.9k
  if (time.month < 1 || time.month > 12) {
84
23
    return false;
85
23
  }
86
11.8k
  if (time.day < 1) {
87
5
    return false;
88
5
  }
89
11.8k
  if (time.hours > 23) {
90
8
    return false;
91
8
  }
92
11.8k
  if (time.minutes > 59) {
93
12
    return false;
94
12
  }
95
  // Leap seconds are allowed.
96
11.8k
  if (time.seconds > 60) {
97
8
    return false;
98
8
  }
99
100
  // validate upper bound for day of month
101
11.8k
  switch (time.month) {
102
1.57k
    case 4:
103
2.42k
    case 6:
104
3.10k
    case 9:
105
3.80k
    case 11:
106
3.80k
      if (time.day > 30) {
107
27
        return false;
108
27
      }
109
3.77k
      break;
110
3.77k
    case 1:
111
1.79k
    case 3:
112
2.30k
    case 5:
113
3.05k
    case 7:
114
4.23k
    case 8:
115
4.89k
    case 10:
116
5.51k
    case 12:
117
5.51k
      if (time.day > 31) {
118
35
        return false;
119
35
      }
120
5.48k
      break;
121
5.48k
    case 2:
122
2.53k
      if (time.year % 4 == 0 &&
123
1.72k
          (time.year % 100 != 0 || time.year % 400 == 0)) {
124
1.29k
        if (time.day > 29) {
125
22
          return false;
126
22
        }
127
1.29k
      } else {
128
1.23k
        if (time.day > 28) {
129
23
          return false;
130
23
        }
131
1.23k
      }
132
2.48k
      break;
133
2.48k
    default:
134
0
      abort();
135
11.8k
  }
136
11.7k
  return true;
137
11.8k
}
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
2.69k
size_t GetUnsignedIntegerLength(Input in) {
149
2.69k
  der::ByteReader reader(in);
150
2.69k
  uint8_t first_byte;
151
2.69k
  if (!reader.ReadByte(&first_byte)) {
152
0
    return 0;  // Not valid DER  as |in| was empty.
153
0
  }
154
155
2.69k
  if (first_byte == 0 && in.size() > 1) {
156
498
    return in.size() - 1;
157
498
  }
158
2.19k
  return in.size();
159
2.69k
}
160
161
}  // namespace
162
163
1.91k
bool ParseBool(Input in, bool *out) {
164
1.91k
  return ParseBoolInternal(in, out, false /* relaxed */);
165
1.91k
}
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
4.91k
bool IsValidInteger(Input in, bool *negative) {
180
4.91k
  CBS cbs;
181
4.91k
  CBS_init(&cbs, in.data(), in.size());
182
4.91k
  int negative_int;
183
4.91k
  if (!CBS_is_valid_asn1_integer(&cbs, &negative_int)) {
184
40
    return false;
185
40
  }
186
187
4.87k
  *negative = !!negative_int;
188
4.87k
  return true;
189
4.91k
}
190
191
2.76k
bool ParseUint64(Input in, uint64_t *out) {
192
  // Reject non-minimally encoded numbers and negative numbers.
193
2.76k
  bool negative;
194
2.76k
  if (!IsValidInteger(in, &negative) || negative) {
195
66
    return false;
196
66
  }
197
198
  // Reject (non-negative) integers whose value would overflow the output type.
199
2.69k
  if (GetUnsignedIntegerLength(in) > sizeof(*out)) {
200
151
    return false;
201
151
  }
202
203
2.54k
  ByteReader reader(in);
204
2.54k
  uint8_t data;
205
2.54k
  uint64_t value = 0;
206
207
9.14k
  while (reader.ReadByte(&data)) {
208
6.60k
    value <<= 8;
209
6.60k
    value |= data;
210
6.60k
  }
211
2.54k
  *out = value;
212
2.54k
  return true;
213
2.69k
}
214
215
1.09k
bool ParseUint8(Input in, uint8_t *out) {
216
  // TODO(eroman): Implement this more directly.
217
1.09k
  uint64_t value;
218
1.09k
  if (!ParseUint64(in, &value)) {
219
122
    return false;
220
122
  }
221
222
973
  if (value > 0xFF) {
223
423
    return false;
224
423
  }
225
226
550
  *out = static_cast<uint8_t>(value);
227
550
  return true;
228
973
}
229
230
BitString::BitString(Input bytes, uint8_t unused_bits)
231
2.93k
    : bytes_(bytes), unused_bits_(unused_bits) {
232
2.93k
  BSSL_CHECK(unused_bits < 8);
233
2.93k
  BSSL_CHECK(unused_bits == 0 || !bytes.empty());
234
  // The unused bits must be zero.
235
2.93k
  BSSL_CHECK(bytes.empty() || (bytes.back() & ((1u << unused_bits) - 1)) == 0);
236
2.93k
}
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.01k
std::optional<BitString> ParseBitString(Input in) {
261
3.01k
  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.01k
  uint8_t unused_bits;
269
3.01k
  if (!reader.ReadByte(&unused_bits)) {
270
4
    return std::nullopt;
271
4
  }
272
3.00k
  if (unused_bits > 7) {
273
28
    return std::nullopt;
274
28
  }
275
276
2.97k
  Input bytes;
277
2.97k
  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
2.97k
  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
550
    if (bytes.empty()) {
288
9
      return std::nullopt;
289
9
    }
290
541
    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
541
    uint8_t mask = 0xFF >> (8 - unused_bits);
297
541
    if ((mask & last_byte) != 0) {
298
32
      return std::nullopt;
299
32
    }
300
541
  }
301
302
2.93k
  return BitString(bytes, unused_bits);
303
2.97k
}
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
8.81k
bool ParseUTCTime(Input in, GeneralizedTime *value) {
328
8.81k
  ByteReader reader(in);
329
8.81k
  GeneralizedTime time;
330
8.81k
  if (!DecimalStringToUint(reader, 2, &time.year) ||
331
8.74k
      !DecimalStringToUint(reader, 2, &time.month) ||
332
8.69k
      !DecimalStringToUint(reader, 2, &time.day) ||
333
8.63k
      !DecimalStringToUint(reader, 2, &time.hours) ||
334
8.58k
      !DecimalStringToUint(reader, 2, &time.minutes) ||
335
8.53k
      !DecimalStringToUint(reader, 2, &time.seconds)) {
336
336
    return false;
337
336
  }
338
8.47k
  uint8_t zulu;
339
8.47k
  if (!reader.ReadByte(&zulu) || zulu != 'Z' || reader.HasMore()) {
340
68
    return false;
341
68
  }
342
8.40k
  if (time.year < 50) {
343
5.11k
    time.year += 2000;
344
5.11k
  } else {
345
3.29k
    time.year += 1900;
346
3.29k
  }
347
8.40k
  if (!ValidateGeneralizedTime(time)) {
348
89
    return false;
349
89
  }
350
8.32k
  *value = time;
351
8.32k
  return true;
352
8.40k
}
353
354
4.09k
bool ParseGeneralizedTime(Input in, GeneralizedTime *value) {
355
4.09k
  ByteReader reader(in);
356
4.09k
  GeneralizedTime time;
357
4.09k
  if (!DecimalStringToUint(reader, 4, &time.year) ||
358
3.97k
      !DecimalStringToUint(reader, 2, &time.month) ||
359
3.88k
      !DecimalStringToUint(reader, 2, &time.day) ||
360
3.79k
      !DecimalStringToUint(reader, 2, &time.hours) ||
361
3.72k
      !DecimalStringToUint(reader, 2, &time.minutes) ||
362
3.66k
      !DecimalStringToUint(reader, 2, &time.seconds)) {
363
509
    return false;
364
509
  }
365
3.59k
  uint8_t zulu;
366
3.59k
  if (!reader.ReadByte(&zulu) || zulu != 'Z' || reader.HasMore()) {
367
98
    return false;
368
98
  }
369
3.49k
  if (!ValidateGeneralizedTime(time)) {
370
74
    return false;
371
74
  }
372
3.41k
  *value = time;
373
3.41k
  return true;
374
3.49k
}
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
7.79k
bool ParseUniversalString(Input in, std::string *out) {
437
7.79k
  if (in.size() % 4 != 0) {
438
237
    return false;
439
237
  }
440
441
7.55k
  CBS cbs;
442
7.55k
  CBS_init(&cbs, in.data(), in.size());
443
7.55k
  bssl::ScopedCBB cbb;
444
7.55k
  if (!CBB_init(cbb.get(), in.size())) {
445
0
    return false;
446
0
  }
447
448
15.6k
  while (CBS_len(&cbs) != 0) {
449
8.83k
    uint32_t c;
450
8.83k
    if (!CBS_get_utf32_be(&cbs, &c) ||  //
451
8.12k
        !CBB_add_utf8(cbb.get(), c)) {
452
714
      return false;
453
714
    }
454
8.83k
  }
455
456
6.84k
  out->assign(CBB_data(cbb.get()), CBB_data(cbb.get()) + CBB_len(cbb.get()));
457
6.84k
  return true;
458
7.55k
}
459
460
143k
bool ParseBmpString(Input in, std::string *out) {
461
143k
  if (in.size() % 2 != 0) {
462
171
    return false;
463
171
  }
464
465
143k
  CBS cbs;
466
143k
  CBS_init(&cbs, in.data(), in.size());
467
143k
  bssl::ScopedCBB cbb;
468
143k
  if (!CBB_init(cbb.get(), in.size())) {
469
0
    return false;
470
0
  }
471
472
6.61M
  while (CBS_len(&cbs) != 0) {
473
6.47M
    uint32_t c;
474
6.47M
    if (!CBS_get_ucs2_be(&cbs, &c) ||  //
475
6.47M
        !CBB_add_utf8(cbb.get(), c)) {
476
634
      return false;
477
634
    }
478
6.47M
  }
479
480
142k
  out->assign(CBB_data(cbb.get()), CBB_data(cbb.get()) + CBB_len(cbb.get()));
481
142k
  return true;
482
143k
}
483
484
}  // namespace der
485
BSSL_NAMESPACE_END