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