21 #include <gtest/gtest.h>
32 #include "../test/test_util.h"
35 #if defined(OPENSSL_THREADS)
42 0x1f, 0x81, 0x00, 0x01, 0x00,
47 0x1f, 0x82, 0x02, 0x01, 0x00,
51 "V_ASN1_NEG_INTEGER changed. Update kTag258 to collide with it.");
56 0x1f, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x01, 0x00,
83 bssl::UniquePtr<BIGNUM> bn(
BN_new());
85 const std::vector<int64_t> kValues = {
86 LONG_MIN, -2, -1, 0, 1, 2, 0xff, 0x100, 0xffff, 0x10000, LONG_MAX,
88 for (
const auto &
i : kValues) {
110 template <
typename T,
typename U>
112 bssl::Span<const uint8_t> expected) {
114 "incompatible parameter to i2d_func");
122 len = i2d_func(
obj,
nullptr);
126 std::vector<uint8_t>
buf(
len);
135 static const uint8_t kDER[] = {0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
136 0xf7, 0x0d, 0x01, 0x01, 0x01};
142 static const uint8_t kTrue[] = {0x01, 0x01, 0xff};
152 static const uint8_t kFalse[] = {0x01, 0x01, 0x00};
159 const std::vector<uint8_t> kInvalidBooleans[] = {
168 {0x01, 0x02, 0x00, 0x00},
178 for (
const auto &
invalid : kInvalidBooleans) {
192 static const uint8_t kLeaf[] = {0x30, 0x00};
198 static const uint8_t kCA[] = {0x30, 0x03, 0x01, 0x01, 0xff};
210 std::vector<uint8_t> der;
226 {0x06, 0x0c, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7,
237 for (
const auto &t :
kTests) {
256 static const uint8_t kDER[] = {0x04, 0x01, 0x61};
271 static const uint8_t kOID[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12,
272 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02};
274 "short name",
"long name");
278 static const uint8_t kDER[] = {0x06, 0x03, 0x2b, 0x65, 0x70};
293 const std::vector<uint8_t> kInvalidObjects[] = {
303 {0x06, 0x03, 0x2b, 0x65, 0xf0},
305 {0x06, 0x03, 0x2b, 0x65, 0x80, 0x70},
306 {0x06, 0x03, 0x80, 0x2b, 0x65, 0x70},
308 {0x01, 0x03, 0x2b, 0x65, 0x70},
310 {0x86, 0x03, 0x2b, 0x65, 0x70},
312 {0x26, 0x03, 0x2b, 0x65, 0x70},
314 for (
const auto &
invalid : kInvalidObjects) {
325 const size_t kNotWholeBytes =
static_cast<size_t>(-1);
327 std::vector<uint8_t>
in;
331 {{0x03, 0x01, 0x00}, 0},
333 {{0x03, 0x02, 0x07, 0x80}, kNotWholeBytes},
335 {{0x03, 0x02, 0x04, 0xa0}, kNotWholeBytes},
337 {{0x03, 0x02, 0x01, 0xaa}, kNotWholeBytes},
339 {{0x03, 0x02, 0x00, 0xaa}, 1},
341 {{0x03, 0x09, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, 8},
343 {{0x03, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 8},
345 for (
const auto &
test : kValidInputs) {
349 bssl::UniquePtr<ASN1_BIT_STRING> val(
356 if (
test.num_bytes == kNotWholeBytes) {
364 const std::vector<uint8_t> kInvalidInputs[] = {
370 {0x03, 0x02, 0x08, 0x00},
371 {0x03, 0x02, 0xff, 0x00},
375 {0x03, 0x02, 0x06, 0xc1 },
377 for (
const auto &
test : kInvalidInputs) {
380 bssl::UniquePtr<ASN1_BIT_STRING> val(
389 static const uint8_t kBitStringEmpty[] = {0x03, 0x01, 0x00};
399 static const uint8_t kBitString1101[] = {0x03, 0x02, 0x04, 0xd0};
409 static const uint8_t kBitString1001[] = {0x03, 0x02, 0x04, 0x90};
419 static const uint8_t kBitString1[] = {0x03, 0x02, 0x07, 0x80};
429 static const uint8_t kBitStringLong[] = {0x03, 0x09, 0x00, 0x80, 0x00, 0x00,
430 0x00, 0x00, 0x00, 0x00, 0x01};
459 static const uint8_t kBitString10010[] = {0x03, 0x02, 0x03, 0x90};
460 ptr = kBitString10010;
475 static const uint8_t kZeros[64] = {0};
481 static const struct {
482 std::vector<uint8_t>
in;
484 const char *expected;
500 {{0, 0, 0xfe, 0xff, 0, 0, 0, 88},
V_ASN1_UNIVERSALSTRING,
nullptr},
528 if (
test.expected !=
nullptr) {
544 static const struct {
546 const char *generalized;
549 {-631152001,
"19491231235959Z",
nullptr},
550 {-631152000,
"19500101000000Z",
"500101000000Z"},
551 {0,
"19700101000000Z",
"700101000000Z"},
552 {981173106,
"20010203040506Z",
"010203040506Z"},
553 #if defined(OPENSSL_64_BIT)
557 {2524607999,
"20491231235959Z",
"491231235959Z"},
558 {2524608000,
"20500101000000Z",
nullptr},
560 {-62167219200,
"00000101000000Z",
nullptr},
561 {-62167219201,
nullptr,
nullptr},
562 {253402300799,
"99991231235959Z",
nullptr},
563 {253402300800,
nullptr,
nullptr},
566 for (
const auto &t :
kTests) {
568 #if defined(OPENSSL_WINDOWS)
571 if (t.time < 0 ||
int64_t{t.time} > 32535215999) {
585 bssl::UniquePtr<ASN1_GENERALIZEDTIME> generalized(
595 bssl::UniquePtr<ASN1_TIME> choice(
ASN1_TIME_set(
nullptr, t.time));
612 return std::vector<uint8_t>(
str.begin(),
str.end());
618 std::vector<uint8_t>
data;
637 {0,
'\n', 0x80, 0xff,
',',
'+',
'"',
'\\',
'<',
'>',
';'},
645 {0,
'\n', 0x80, 0xff,
',',
'+',
'"',
'\\',
'<',
'>',
';'},
648 std::string(1,
'\0') +
"\n\x80\xff\\,\\+\\\"\\\\\\<\\>\\;"},
650 {0,
'\n', 0x80, 0xff,
',',
'+',
'"',
'\\',
'<',
'>',
';'},
653 "\\00\\0A\x80\xff,+\"\\\\<>;"},
655 {0,
'\n', 0x80, 0xff,
',',
'+',
'"',
'\\',
'<',
'>',
';'},
660 {0,
'\n', 0x80, 0xff,
',',
'+',
'"',
'\\',
'<',
'>',
';'},
663 "\\00\\0A\\80\\FF\\,\\+\\\"\\\\\\<\\>\\;"},
667 {0,
'\n', 0x80, 0xff,
',',
'+',
'"',
'\\',
'<',
'>',
';'},
671 "\"\\00\\0A\\80\\FF,+\\\"\\\\<>;\""},
675 {0,
'\n', 0x80, 0xff,
'"',
'\\'},
679 "\\00\\0A\\80\\FF\\\"\\\\"},
698 {0x00,
'a', 0x00, 0x80, 0x01, 0x00},
703 {0x00, 0x00, 0x00,
'a',
704 0x00, 0x00, 0x00, 0x80,
705 0x00, 0x00, 0x01, 0x00,
706 0x00, 0x01, 0x00, 0x00},
709 "a\\80\\U0100\\W00010000"},
719 "a\\C2\\80\\C4\\80\\F0\\90\\80\\80"},
721 {0x00,
'a', 0x00, 0x80, 0x01, 0x00},
724 "a\\C2\\80\\C4\\80"},
726 {0x00, 0x00, 0x00,
'a',
727 0x00, 0x00, 0x00, 0x80,
728 0x00, 0x00, 0x01, 0x00,
729 0x00, 0x01, 0x00, 0x00},
732 "a\\C2\\80\\C4\\80\\F0\\90\\80\\80"},
742 {0x00,
'a', 0x00, 0x80, 0x01, 0x00},
745 "a\xc2\x80\xc4\x80"},
747 {0x00, 0x00, 0x00,
'a',
748 0x00, 0x00, 0x00, 0x80,
749 0x00, 0x00, 0x01, 0x00,
750 0x00, 0x01, 0x00, 0x00},
753 "a\xc2\x80\xc4\x80\xf0\x90\x80\x80"},
757 {-1, {0xff}, 0, 0,
"\xff"},
758 {100, {0xff}, 0, 0,
"\xff"},
827 for (
const auto &t :
kTests) {
835 str->flags = t.str_flags;
859 std::vector<uint8_t>
data;
862 } kUnprintableTests[] = {
869 for (
const auto &t : kUnprintableTests) {
877 str->flags = t.str_flags;
905 std::vector<uint8_t>
in;
908 std::vector<uint8_t> expected_data;
915 {
'a',
'A',
'0',
'\'',
'(',
')',
'+',
',',
'-',
'.',
'/',
':',
'=',
'?'},
918 {
'a',
'A',
'0',
'\'',
'(',
')',
'+',
',',
'-',
'.',
'/',
':',
'=',
'?'},
935 {0xf0, 0x90, 0x80, 0x80 },
938 {0x00, 0x01, 0x00, 0x00},
941 {0xf0, 0x90, 0x80, 0x80 },
944 {0xf0, 0x90, 0x80, 0x80},
971 {0x00, 0x00, 0x00,
'a'},
981 0xf0, 0x90, 0x80, 0x80,
986 0x00, 0x00, 0x00,
'a',
987 0x00, 0x00, 0x00, 0x80,
988 0x00, 0x00, 0x01, 0x00,
989 0x00, 0x01, 0x00, 0x00,
993 for (
const auto &t :
kTests) {
1031 t.mask, t.num_codepoints,
1036 t.mask, t.num_codepoints,
1046 nullptr, t.in.data(), t.in.size(), t.format, t.mask,
1047 t.num_codepoints + 1, 0));
1050 &
str, t.in.data(), t.in.size(), t.format, t.mask,
1051 t.num_codepoints + 1, 0));
1054 if (t.num_codepoints > 1) {
1056 nullptr, t.in.data(), t.in.size(), t.format, t.mask,
1057 0, t.num_codepoints - 1));
1060 &
str, t.in.data(), t.in.size(), t.format, t.mask,
1061 0, t.num_codepoints - 1));
1069 std::vector<uint8_t>
in;
1071 } kInvalidTests[] = {
1091 {0xf0, 0x90, 0x80, 0x80 },
1098 for (
const auto &t : kInvalidTests) {
1145 "example@example.com"},
1157 for (
const auto &t :
kTests) {
1163 nullptr,
reinterpret_cast<const uint8_t *
>(t.in.data()), t.in.size(),
1175 &old_str,
reinterpret_cast<const uint8_t *
>(t.in.data()), t.in.size(),
1186 } kInvalidTests[] = {
1224 for (
const auto &t : kInvalidTests) {
1228 nullptr,
reinterpret_cast<const uint8_t *
>(t.in.data()), t.in.size(),
1238 int nid1 =
OBJ_create(
"1.2.840.113554.4.1.72585.1000",
"custom OID 1000",
1241 int nid2 =
OBJ_create(
"1.2.840.113554.4.1.72585.1001",
"custom OID 1001",
1263 nullptr,
reinterpret_cast<const uint8_t *
>(
"12345678901"), 11,
1272 reinterpret_cast<const uint8_t *
>(
"12345"),
1285 #if defined(OPENSSL_THREADS)
1289 int nid1 =
OBJ_create(
"1.2.840.113554.4.1.72585.1002",
"custom OID 1002",
1292 int nid2 =
OBJ_create(
"1.2.840.113554.4.1.72585.1003",
"custom OID 1003",
1296 std::vector<std::thread>
threads;
1323 #endif // OPENSSL_THREADS
1328 static const uint8_t kMinusOne[] = {0x0a, 0x01, 0xff};
1331 bssl::UniquePtr<ASN1_STRING>
str(
1339 std::vector<uint8_t>
in;
1343 {{
'a',
'A',
'0',
'\'',
'(',
')',
'+',
',',
'-',
'.',
'/',
':',
'=',
'?'},
1352 for (
const auto &t :
kTests) {
1412 for (
size_t i = 1;
i < table_len;
i++) {
1444 bssl::UniquePtr<ASN1_TYPE> null_type(
1449 EXPECT_EQ(
nullptr, null_type->value.ptr);
1458 static const uint8_t kExpected[] = {0x30, 0x00};
1459 bssl::UniquePtr<ASN1_STRING>
str(
1504 static const uint8_t kTraiilingData[] = {0x30, 0x00, 0x00};
1515 std::vector<uint8_t>
data;
1517 bool equals_previous;
1521 const Input kInputs[] = {
1586 std::vector<bssl::UniquePtr<ASN1_STRING>> strs;
1588 for (
const auto &
input : kInputs) {
1592 input.data.size()));
1593 strs.back()->flags =
input.flags;
1596 for (
size_t i = 0;
i < strs.size();
i++) {
1598 bool expect_equal =
true;
1599 for (
size_t j =
i; j < strs.size(); j++) {
1601 if (j >
i && !kInputs[j].equals_previous) {
1602 expect_equal =
false;
1623 std::vector<uint8_t>
in;
1624 const char *expected;
1627 {{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01},
"rsaEncryption"},
1630 {{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x00},
1631 "1.2.840.113554.4.1.72585.0"},
1634 {{0xff},
"<INVALID>"},
1638 {{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7,
1639 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1640 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1641 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
1642 "1.2.840.113554.4.1.72585.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0."
1645 {{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7,
1646 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1647 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1648 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a},
1649 "1.2.840.113554.4.1.72585.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0."
1652 {{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7,
1653 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1654 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1655 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64},
1656 "1.2.840.113554.4.1.72585.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0."
1659 {{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7,
1660 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1661 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1662 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87, 0x68},
1663 "1.2.840.113554.4.1.72585.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0."
1666 {{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7,
1667 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1668 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1669 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xce, 0x10},
1670 "1.2.840.113554.4.1.72585.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0."
1673 for (
const auto &t : kDataTests) {
1676 NID_undef, t.in.data(), t.in.size(),
nullptr,
nullptr));
1688 std::string(
reinterpret_cast<const char *
>(bio_data), bio_len));
1700 std::string(
reinterpret_cast<const char *
>(bio_data), bio_len));
1705 static const uint8_t kTruncated[] = {0x30, 0x01};
1711 sizeof(kTruncated)));
1713 static const uint8_t kIndefinite[] = {0x30, 0x80, 0x00, 0x00};
1716 sizeof(kIndefinite)));
1721 #if !defined(OPENSSL_WINDOWS) || !defined(BORINGSSL_SHARED_LIBRARY)
1736 static bool MakeLinkedList(bssl::UniquePtr<uint8_t> *
out,
size_t *out_len,
1738 bssl::ScopedCBB cbb;
1739 std::vector<CBB> cbbs(
count);
1744 for (
size_t i = 1;
i <
count;
i++) {
1758 bssl::UniquePtr<uint8_t>
data;
1766 ASN1_LINKED_LIST_free(list);
1771 list = d2i_ASN1_LINKED_LIST(
nullptr, &
ptr,
len);
1775 ASN1_LINKED_LIST_free(list);
1778 struct IMPLICIT_CHOICE {
1785 ASN1_IMP(IMPLICIT_CHOICE,
string, DIRECTORYSTRING, 0),
1794 std::unique_ptr<IMPLICIT_CHOICE, decltype(&IMPLICIT_CHOICE_free)>
obj(
1795 IMPLICIT_CHOICE_new(), IMPLICIT_CHOICE_free);
1796 EXPECT_EQ(-1, i2d_IMPLICIT_CHOICE(
obj.get(),
nullptr));
1803 static const uint8_t kInput1[] = {0x30, 0x02, 0x0c, 0x00};
1805 EXPECT_EQ(
nullptr, d2i_IMPLICIT_CHOICE(
nullptr, &
ptr,
sizeof(kInput1)));
1808 static const uint8_t kInput2[] = {0x30, 0x02, 0x80, 0x00};
1810 EXPECT_EQ(
nullptr, d2i_IMPLICIT_CHOICE(
nullptr, &
ptr,
sizeof(kInput2)));
1813 struct REQUIRED_FIELD {
1842 EXPECT_EQ(-1, i2d_REQUIRED_FIELD(
nullptr,
nullptr));
1844 std::unique_ptr<REQUIRED_FIELD, decltype(&REQUIRED_FIELD_free)>
obj(
1845 nullptr, REQUIRED_FIELD_free);
1847 &REQUIRED_FIELD::value_exp}) {
1848 obj.reset(REQUIRED_FIELD_new());
1851 (*obj).*
field =
nullptr;
1855 for (
auto field : {&REQUIRED_FIELD::seq, &REQUIRED_FIELD::seq_imp,
1856 &REQUIRED_FIELD::seq_exp}) {
1857 obj.reset(REQUIRED_FIELD_new());
1860 (*obj).*
field =
nullptr;
1864 for (
auto field : {&REQUIRED_FIELD::null, &REQUIRED_FIELD::null_imp,
1865 &REQUIRED_FIELD::null_exp}) {
1866 obj.reset(REQUIRED_FIELD_new());
1868 (*obj).*
field =
nullptr;
1873 #endif // !WINDOWS || !SHARED_LIBRARY