• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "parse_values.h"
6 
7 #include <stdint.h>
8 
9 #include <gtest/gtest.h>
10 
11 namespace bssl::der::test {
12 
13 namespace {
14 
15 template <size_t N>
FromStringLiteral(const char (& data)[N])16 Input FromStringLiteral(const char(&data)[N]) {
17   // Strings are null-terminated. The null terminating byte shouldn't be
18   // included in the Input, so the size is N - 1 instead of N.
19   return Input(reinterpret_cast<const uint8_t*>(data), N - 1);
20 }
21 
22 }  // namespace
23 
TEST(ParseValuesTest,ParseBool)24 TEST(ParseValuesTest, ParseBool) {
25   uint8_t buf[] = {0xFF, 0x00};
26   Input value(buf, 1);
27   bool out;
28   EXPECT_TRUE(ParseBool(value, &out));
29   EXPECT_TRUE(out);
30 
31   buf[0] = 0;
32   EXPECT_TRUE(ParseBool(value, &out));
33   EXPECT_FALSE(out);
34 
35   buf[0] = 1;
36   EXPECT_FALSE(ParseBool(value, &out));
37   EXPECT_TRUE(ParseBoolRelaxed(value, &out));
38   EXPECT_TRUE(out);
39 
40   buf[0] = 0xFF;
41   value = Input(buf, 2);
42   EXPECT_FALSE(ParseBool(value, &out));
43   value = Input(buf, 0);
44   EXPECT_FALSE(ParseBool(value, &out));
45 }
46 
TEST(ParseValuesTest,ParseTimes)47 TEST(ParseValuesTest, ParseTimes) {
48   GeneralizedTime out;
49 
50   EXPECT_TRUE(ParseUTCTime(FromStringLiteral("140218161200Z"), &out));
51 
52   // DER-encoded UTCTime must end with 'Z'.
53   EXPECT_FALSE(ParseUTCTime(FromStringLiteral("140218161200X"), &out));
54 
55   // Check that a negative number (-4 in this case) doesn't get parsed as
56   // a 2-digit number.
57   EXPECT_FALSE(ParseUTCTime(FromStringLiteral("-40218161200Z"), &out));
58 
59   // Check that numbers with a leading 0 don't get parsed in octal by making
60   // the second digit an invalid octal digit (e.g. 09).
61   EXPECT_TRUE(ParseUTCTime(FromStringLiteral("090218161200Z"), &out));
62 
63   // Check that the length is validated.
64   EXPECT_FALSE(ParseUTCTime(FromStringLiteral("140218161200"), &out));
65   EXPECT_FALSE(ParseUTCTime(FromStringLiteral("140218161200Z0"), &out));
66 
67   // Check strictness of UTCTime parsers.
68   EXPECT_FALSE(ParseUTCTime(FromStringLiteral("1402181612Z"), &out));
69 
70   // Check format of GeneralizedTime.
71 
72   // Years 0 and 9999 are allowed.
73   EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("00000101000000Z"), &out));
74   EXPECT_EQ(0, out.year);
75   EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("99991231235960Z"), &out));
76   EXPECT_EQ(9999, out.year);
77 
78   // Leap seconds are allowed.
79   EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("20140218161260Z"), &out));
80 
81   // But nothing larger than a leap second.
82   EXPECT_FALSE(
83       ParseGeneralizedTime(FromStringLiteral("20140218161261Z"), &out));
84 
85   // Minutes only go up to 59.
86   EXPECT_FALSE(
87       ParseGeneralizedTime(FromStringLiteral("20140218166000Z"), &out));
88 
89   // Hours only go up to 23.
90   EXPECT_FALSE(
91       ParseGeneralizedTime(FromStringLiteral("20140218240000Z"), &out));
92   // The 0th day of a month is invalid.
93   EXPECT_FALSE(
94       ParseGeneralizedTime(FromStringLiteral("20140200161200Z"), &out));
95   // The 0th month is invalid.
96   EXPECT_FALSE(
97       ParseGeneralizedTime(FromStringLiteral("20140018161200Z"), &out));
98   // Months greater than 12 are invalid.
99   EXPECT_FALSE(
100       ParseGeneralizedTime(FromStringLiteral("20141318161200Z"), &out));
101 
102   // Some months have 31 days.
103   EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("20140131000000Z"), &out));
104 
105   // September has only 30 days.
106   EXPECT_FALSE(
107       ParseGeneralizedTime(FromStringLiteral("20140931000000Z"), &out));
108 
109   // February has only 28 days...
110   EXPECT_FALSE(
111       ParseGeneralizedTime(FromStringLiteral("20140229000000Z"), &out));
112 
113   // ... unless it's a leap year.
114   EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("20160229000000Z"), &out));
115 
116   // There aren't any leap days in years divisible by 100...
117   EXPECT_FALSE(
118       ParseGeneralizedTime(FromStringLiteral("21000229000000Z"), &out));
119 
120   // ...unless it's also divisible by 400.
121   EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("20000229000000Z"), &out));
122 
123   // Check more perverse invalid inputs.
124 
125   // Check that trailing null bytes are not ignored.
126   EXPECT_FALSE(
127       ParseGeneralizedTime(FromStringLiteral("20001231010203Z\0"), &out));
128 
129   // Check what happens when a null byte is in the middle of the input.
130   EXPECT_FALSE(ParseGeneralizedTime(FromStringLiteral(
131                                         "200\0"
132                                         "1231010203Z"),
133                                     &out));
134 
135   // The year can't be in hex.
136   EXPECT_FALSE(
137       ParseGeneralizedTime(FromStringLiteral("0x201231000000Z"), &out));
138 
139   // The last byte must be 'Z'.
140   EXPECT_FALSE(
141       ParseGeneralizedTime(FromStringLiteral("20001231000000X"), &out));
142 
143   // Check that the length is validated.
144   EXPECT_FALSE(ParseGeneralizedTime(FromStringLiteral("20140218161200"), &out));
145   EXPECT_FALSE(
146       ParseGeneralizedTime(FromStringLiteral("20140218161200Z0"), &out));
147 }
148 
TEST(ParseValuesTest,TimesCompare)149 TEST(ParseValuesTest, TimesCompare) {
150   GeneralizedTime time1;
151   GeneralizedTime time2;
152   GeneralizedTime time3;
153 
154   ASSERT_TRUE(
155       ParseGeneralizedTime(FromStringLiteral("20140218161200Z"), &time1));
156   // Test that ParseUTCTime correctly normalizes the year.
157   ASSERT_TRUE(ParseUTCTime(FromStringLiteral("150218161200Z"), &time2));
158   ASSERT_TRUE(
159       ParseGeneralizedTime(FromStringLiteral("20160218161200Z"), &time3));
160   EXPECT_TRUE(time1 < time2);
161   EXPECT_TRUE(time2 < time3);
162 
163   EXPECT_TRUE(time2 > time1);
164   EXPECT_TRUE(time2 >= time1);
165   EXPECT_TRUE(time2 <= time3);
166   EXPECT_TRUE(time1 <= time1);
167   EXPECT_TRUE(time1 >= time1);
168 }
169 
TEST(ParseValuesTest,UTCTimeRange)170 TEST(ParseValuesTest, UTCTimeRange) {
171   GeneralizedTime time;
172   ASSERT_TRUE(
173       ParseGeneralizedTime(FromStringLiteral("20140218161200Z"), &time));
174   EXPECT_TRUE(time.InUTCTimeRange());
175 
176   time.year = 1950;
177   EXPECT_TRUE(time.InUTCTimeRange());
178 
179   time.year = 1949;
180   EXPECT_FALSE(time.InUTCTimeRange());
181 
182   time.year = 2049;
183   EXPECT_TRUE(time.InUTCTimeRange());
184 
185   time.year = 2050;
186   EXPECT_FALSE(time.InUTCTimeRange());
187 }
188 
189 struct Uint64TestData {
190   bool should_pass;
191   const uint8_t input[9];
192   size_t length;
193   uint64_t expected_value = 0;
194 };
195 
196 const Uint64TestData kUint64TestData[] = {
197     {true, {0x00}, 1, 0},
198     // This number fails because it is not a minimal representation.
199     {false, {0x00, 0x00}, 2},
200     {true, {0x01}, 1, 1},
201     {false, {0xFF}, 1},
202     {true, {0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 8, INT64_MAX},
203     {true,
204      {0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
205      9,
206      UINT64_MAX},
207     // This number fails because it is negative.
208     {false, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 8},
209     {false, {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8},
210     {false, {0x00, 0x01}, 2},
211     {false, {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}, 9},
212     {false, {0}, 0},
213 };
214 
TEST(ParseValuesTest,ParseUint64)215 TEST(ParseValuesTest, ParseUint64) {
216   for (size_t i = 0; i < std::size(kUint64TestData); i++) {
217     const Uint64TestData& test_case = kUint64TestData[i];
218     SCOPED_TRACE(i);
219 
220     uint64_t result;
221     EXPECT_EQ(test_case.should_pass,
222               ParseUint64(Input(test_case.input, test_case.length), &result));
223     if (test_case.should_pass) {
224       EXPECT_EQ(test_case.expected_value, result);
225     }
226   }
227 }
228 
229 struct Uint8TestData {
230   bool should_pass;
231   const uint8_t input[9];
232   size_t length;
233   uint8_t expected_value = 0;
234 };
235 
236 const Uint8TestData kUint8TestData[] = {
237     {true, {0x00}, 1, 0},
238     // This number fails because it is not a minimal representation.
239     {false, {0x00, 0x00}, 2},
240     {true, {0x01}, 1, 1},
241     {false, {0x01, 0xFF}, 2},
242     {false, {0x03, 0x83}, 2},
243     {true, {0x7F}, 1, 0x7F},
244     {true, {0x00, 0xFF}, 2, 0xFF},
245     // This number fails because it is negative.
246     {false, {0xFF}, 1},
247     {false, {0x80}, 1},
248     {false, {0x00, 0x01}, 2},
249     {false, {0}, 0},
250 };
251 
TEST(ParseValuesTest,ParseUint8)252 TEST(ParseValuesTest, ParseUint8) {
253   for (size_t i = 0; i < std::size(kUint8TestData); i++) {
254     const Uint8TestData& test_case = kUint8TestData[i];
255     SCOPED_TRACE(i);
256 
257     uint8_t result;
258     EXPECT_EQ(test_case.should_pass,
259               ParseUint8(Input(test_case.input, test_case.length), &result));
260     if (test_case.should_pass) {
261       EXPECT_EQ(test_case.expected_value, result);
262     }
263   }
264 }
265 
266 struct IsValidIntegerTestData {
267   bool should_pass;
268   const uint8_t input[2];
269   size_t length;
270   bool negative = false;
271 };
272 
273 const IsValidIntegerTestData kIsValidIntegerTestData[] = {
274     // Empty input (invalid DER).
275     {false, {0x00}, 0},
276 
277     // The correct encoding for zero.
278     {true, {0x00}, 1, false},
279 
280     // Invalid representation of zero (not minimal)
281     {false, {0x00, 0x00}, 2},
282 
283     // Valid single byte negative numbers.
284     {true, {0x80}, 1, true},
285     {true, {0xFF}, 1, true},
286 
287     // Non-minimal negative number.
288     {false, {0xFF, 0x80}, 2},
289 
290     // Positive number with a legitimate leading zero.
291     {true, {0x00, 0x80}, 2, false},
292 
293     // A legitimate negative number that starts with FF (MSB of second byte is
294     // 0 so OK).
295     {true, {0xFF, 0x7F}, 2, true},
296 };
297 
TEST(ParseValuesTest,IsValidInteger)298 TEST(ParseValuesTest, IsValidInteger) {
299   for (size_t i = 0; i < std::size(kIsValidIntegerTestData); i++) {
300     const auto& test_case = kIsValidIntegerTestData[i];
301     SCOPED_TRACE(i);
302 
303     bool negative;
304     EXPECT_EQ(
305         test_case.should_pass,
306         IsValidInteger(Input(test_case.input, test_case.length), &negative));
307     if (test_case.should_pass) {
308       EXPECT_EQ(test_case.negative, negative);
309     }
310   }
311 }
312 
313 // Tests parsing an empty BIT STRING.
TEST(ParseValuesTest,ParseBitStringEmptyNoUnusedBits)314 TEST(ParseValuesTest, ParseBitStringEmptyNoUnusedBits) {
315   const uint8_t kData[] = {0x00};
316 
317   std::optional<BitString> bit_string = ParseBitString(Input(kData));
318   ASSERT_TRUE(bit_string.has_value());
319 
320   EXPECT_EQ(0u, bit_string->unused_bits());
321   EXPECT_EQ(0u, bit_string->bytes().Length());
322 
323   EXPECT_FALSE(bit_string->AssertsBit(0));
324   EXPECT_FALSE(bit_string->AssertsBit(1));
325   EXPECT_FALSE(bit_string->AssertsBit(3));
326 }
327 
328 // Tests parsing an empty BIT STRING that incorrectly claims one unused bit.
TEST(ParseValuesTest,ParseBitStringEmptyOneUnusedBit)329 TEST(ParseValuesTest, ParseBitStringEmptyOneUnusedBit) {
330   const uint8_t kData[] = {0x01};
331 
332   std::optional<BitString> bit_string = ParseBitString(Input(kData));
333   EXPECT_FALSE(bit_string.has_value());
334 }
335 
336 // Tests parsing an empty BIT STRING that is not minmally encoded (the entire
337 // last byte is comprised of unused bits).
TEST(ParseValuesTest,ParseBitStringNonEmptyTooManyUnusedBits)338 TEST(ParseValuesTest, ParseBitStringNonEmptyTooManyUnusedBits) {
339   const uint8_t kData[] = {0x08, 0x00};
340 
341   std::optional<BitString> bit_string = ParseBitString(Input(kData));
342   EXPECT_FALSE(bit_string.has_value());
343 }
344 
345 // Tests parsing a BIT STRING of 7 bits each of which are 1.
TEST(ParseValuesTest,ParseBitStringSevenOneBits)346 TEST(ParseValuesTest, ParseBitStringSevenOneBits) {
347   const uint8_t kData[] = {0x01, 0xFE};
348 
349   std::optional<BitString> bit_string = ParseBitString(Input(kData));
350   ASSERT_TRUE(bit_string.has_value());
351 
352   EXPECT_EQ(1u, bit_string->unused_bits());
353   EXPECT_EQ(1u, bit_string->bytes().Length());
354   EXPECT_EQ(0xFE, bit_string->bytes()[0]);
355 
356   EXPECT_TRUE(bit_string->AssertsBit(0));
357   EXPECT_TRUE(bit_string->AssertsBit(1));
358   EXPECT_TRUE(bit_string->AssertsBit(2));
359   EXPECT_TRUE(bit_string->AssertsBit(3));
360   EXPECT_TRUE(bit_string->AssertsBit(4));
361   EXPECT_TRUE(bit_string->AssertsBit(5));
362   EXPECT_TRUE(bit_string->AssertsBit(6));
363   EXPECT_FALSE(bit_string->AssertsBit(7));
364   EXPECT_FALSE(bit_string->AssertsBit(8));
365 }
366 
367 // Tests parsing a BIT STRING of 7 bits each of which are 1. The unused bit
368 // however is set to 1, which is an invalid encoding.
TEST(ParseValuesTest,ParseBitStringSevenOneBitsUnusedBitIsOne)369 TEST(ParseValuesTest, ParseBitStringSevenOneBitsUnusedBitIsOne) {
370   const uint8_t kData[] = {0x01, 0xFF};
371 
372   std::optional<BitString> bit_string = ParseBitString(Input(kData));
373   EXPECT_FALSE(bit_string.has_value());
374 }
375 
TEST(ParseValuesTest,ParseIA5String)376 TEST(ParseValuesTest, ParseIA5String) {
377   const uint8_t valid_der[] = {0x46, 0x6f, 0x6f, 0x20, 0x62,
378                                0x61, 0x72, 0x01, 0x7f};
379   std::string s;
380   EXPECT_TRUE(ParseIA5String(der::Input(valid_der), &s));
381   EXPECT_EQ("Foo bar\x01\x7f", s);
382 
383   // 0x80 is not a valid character in IA5String.
384   const uint8_t invalid_der[] = {0x46, 0x6f, 0x80, 0x20, 0x62, 0x61, 0x72};
385   EXPECT_FALSE(ParseIA5String(der::Input(invalid_der), &s));
386 }
387 
TEST(ParseValuesTest,ParseVisibleString)388 TEST(ParseValuesTest, ParseVisibleString) {
389   const uint8_t valid_der[] = {0x46, 0x6f, 0x6f, 0x20, 0x62, 0x61, 0x72, 0x7e};
390   std::string s;
391   EXPECT_TRUE(ParseVisibleString(der::Input(valid_der), &s));
392   EXPECT_EQ("Foo bar\x7e", s);
393 
394   // 0x7f is not a valid character in VisibleString
395   const uint8_t invalid_der[] = {0x46, 0x6f, 0x7f, 0x20, 0x62, 0x61, 0x72};
396   EXPECT_FALSE(ParseVisibleString(der::Input(invalid_der), &s));
397 
398   // 0x1f is not a valid character in VisibleString
399   const uint8_t invalid_der2[] = {0x46, 0x6f, 0x1f, 0x20, 0x62, 0x61, 0x72};
400   EXPECT_FALSE(ParseVisibleString(der::Input(invalid_der2), &s));
401 }
402 
TEST(ParseValuesTest,ParsePrintableString)403 TEST(ParseValuesTest, ParsePrintableString) {
404   const uint8_t valid_der[] = {0x46, 0x6f, 0x6f, 0x20, 0x62, 0x61, 0x72};
405   std::string s;
406   EXPECT_TRUE(ParsePrintableString(der::Input(valid_der), &s));
407   EXPECT_EQ("Foo bar", s);
408 
409   // 0x5f '_' is not a valid character in PrintableString.
410   const uint8_t invalid_der[] = {0x46, 0x6f, 0x5f, 0x20, 0x62, 0x61, 0x72};
411   EXPECT_FALSE(ParsePrintableString(der::Input(invalid_der), &s));
412 }
413 
TEST(ParseValuesTest,ParseTeletexStringAsLatin1)414 TEST(ParseValuesTest, ParseTeletexStringAsLatin1) {
415   const uint8_t valid_der[] = {0x46, 0x6f, 0xd6, 0x20, 0x62, 0x61, 0x72};
416   std::string s;
417   EXPECT_TRUE(ParseTeletexStringAsLatin1(der::Input(valid_der), &s));
418   EXPECT_EQ("FoÖ bar", s);
419 }
420 
TEST(ParseValuesTest,ParseBmpString)421 TEST(ParseValuesTest, ParseBmpString) {
422   const uint8_t valid_der[] = {0x00, 0x66, 0x00, 0x6f, 0x00, 0x6f,
423                                0x00, 0x62, 0x00, 0x61, 0x00, 0x72};
424   std::string s;
425   EXPECT_TRUE(ParseBmpString(der::Input(valid_der), &s));
426   EXPECT_EQ("foobar", s);
427 
428   const uint8_t valid_nonascii_der[] = {0x27, 0x28, 0x26, 0xa1, 0x2b, 0x50};
429   EXPECT_TRUE(ParseBmpString(der::Input(valid_nonascii_der), &s));
430   EXPECT_EQ("✨⚡⭐", s);
431 
432   // BmpString must encode characters in pairs of 2 bytes.
433   const uint8_t invalid_odd_der[] = {0x00, 0x66, 0x00, 0x6f, 0x00};
434   EXPECT_FALSE(ParseBmpString(der::Input(invalid_odd_der), &s));
435 
436   // UTF-16BE encoding of U+1D11E, MUSICAL SYMBOL G CLEF, which is not valid in
437   // UCS-2.
438   const uint8_t invalid_bmp_valid_utf16_with_surrogate[] = {0xd8, 0x34, 0xdd,
439                                                             0x1e};
440   EXPECT_FALSE(
441       ParseBmpString(der::Input(invalid_bmp_valid_utf16_with_surrogate), &s));
442 }
443 
TEST(ParseValuesTest,ParseUniversalString)444 TEST(ParseValuesTest, ParseUniversalString) {
445   const uint8_t valid_der[] = {0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x6f,
446                                0x00, 0x00, 0x00, 0x6f, 0x00, 0x00, 0x00, 0x62,
447                                0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x72};
448   std::string s;
449   EXPECT_TRUE(ParseUniversalString(der::Input(valid_der), &s));
450   EXPECT_EQ("foobar", s);
451 
452   const uint8_t valid_non_ascii_der[] = {0x0,  0x1,  0xf4, 0xe,  0x0,  0x0, 0x0,
453                                          0x20, 0x0,  0x1,  0xd1, 0x1e, 0x0, 0x0,
454                                          0x26, 0x69, 0x0,  0x0,  0x26, 0x6b};
455   EXPECT_TRUE(ParseUniversalString(der::Input(valid_non_ascii_der), &s));
456   EXPECT_EQ("�� ��♩♫", s);
457 
458   // UniversalString must encode characters in groups of 4 bytes.
459   const uint8_t invalid_non_4_multiple_der[] = {0x00, 0x00, 0x00,
460                                                 0x66, 0x00, 0x00};
461   EXPECT_FALSE(
462       ParseUniversalString(der::Input(invalid_non_4_multiple_der), &s));
463 }
464 
465 }  // namespace bssl::der::test
466