21 #include <gtest/gtest.h>
28 #include "../internal.h"
29 #include "../test/test_util.h"
33 static const uint8_t kData[] = {1, 2, 3};
46 static const uint8_t kData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
47 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
81 TEST(CBSTest, GetPrefixed) {
82 static const uint8_t kData[] = {1, 2, 0, 2, 3, 4, 0, 0, 3, 3, 2, 1};
103 TEST(CBSTest, GetPrefixedBad) {
120 static const uint8_t kData[] = {0, 1, 2, 3, 0, 1, 2, 3};
146 static const uint8_t kData5[4 + 0x80] = {0x30, 0x82, 0, 0x80};
271 std::vector<uint8_t>
in;
280 {0xff, 0x81, 0xff, 0xff, 0xff, 0x7f, 0}},
282 {
false, 0, {0xff, 0x82, 0xff, 0xff, 0xff, 0x7f, 0}},
284 {
false, 0, {0xff, 0x90, 0x80, 0x80, 0x80, 0, 0}},
286 {
false, 0, {0x5f, 0x80, 0x1f, 0}},
288 {
false, 0, {0x5f, 0x80, 0x1e, 0}},
290 for (
const auto &t :
kTests) {
315 TEST(CBSTest, GetOptionalASN1Bool) {
341 TEST(CBBTest, InitUninitialized) {
348 static const uint8_t kExpected[] = {
349 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
350 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
351 0x03, 0x02, 0x0a, 0x09, 0x08, 0x07, 0x12, 0x11, 0x10, 0x0f,
352 0x0e, 0x0d, 0x0c, 0x0b, 0x00, 0x00, 0x00, 0x00};
374 bssl::UniquePtr<uint8_t> scoper(
buf);
416 bssl::UniquePtr<uint8_t> scoper(out_buf);
422 static const uint8_t kExpected[] = {0, 1, 1, 0, 2, 2, 3, 0, 0, 3,
423 4, 5, 6, 5, 4, 1, 0, 1, 2};
427 CBB contents, inner_contents, inner_inner_contents;
448 bssl::UniquePtr<uint8_t> scoper(
buf);
454 CBB contents, inner_contents, inner_inner_contents;
483 bssl::UniquePtr<uint8_t> scoper(
buf);
485 static const uint8_t kExpected[] = {
490 0, 0, 3, 0xdd, 0xdd, 0xdd,
518 bssl::UniquePtr<uint8_t> scoper(
buf);
524 static const uint8_t kExpected[] = {
532 0x5f, 0x1f, 3, 10, 11, 12,
534 0xff, 0x81, 0xff, 0xff, 0xff, 0x7f, 3, 13, 14, 15,
561 bssl::UniquePtr<uint8_t> scoper(
buf);
565 std::vector<uint8_t> test_data(100000, 0x42);
599 bssl::Span<const uint8_t> der_expected,
600 bssl::Span<const uint8_t> ber) {
607 bssl::UniquePtr<uint8_t> scoper(
storage);
618 static const uint8_t kSimpleBER[] = {0x01, 0x01, 0x00};
621 static const uint8_t kNonMinimalLengthBER[] = {0x02, 0x82, 0x00, 0x01, 0x01};
622 static const uint8_t kNonMinimalLengthDER[] = {0x02, 0x01, 0x01};
625 static const uint8_t kIndefBER[] = {0x30, 0x80, 0x01, 0x01, 0x02, 0x00, 0x00};
626 static const uint8_t kIndefDER[] = {0x30, 0x03, 0x01, 0x01, 0x02};
630 static const uint8_t kIndefBER2[] = {0x7f, 0x1f, 0x80, 0x01,
631 0x01, 0x02, 0x00, 0x00};
632 static const uint8_t kIndefDER2[] = {0x7f, 0x1f, 0x03, 0x01, 0x01, 0x02};
636 static const uint8_t kOctetStringBER[] = {0x24, 0x80, 0x04, 0x02, 0, 1,
637 0x04, 0x02, 2, 3, 0x00, 0x00};
638 static const uint8_t kOctetStringDER[] = {0x04, 0x04, 0, 1, 2, 3};
642 static const uint8_t kNSSBER[] = {
643 0x30, 0x80, 0x02, 0x01, 0x03, 0x30, 0x80, 0x06, 0x09, 0x2a, 0x86, 0x48,
644 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x80, 0x24, 0x80, 0x04, 0x04,
645 0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x39,
646 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
647 0x00, 0x04, 0x14, 0x84, 0x98, 0xfc, 0x66, 0x33, 0xee, 0xba, 0xe7, 0x90,
648 0xc1, 0xb6, 0xe8, 0x8f, 0xfe, 0x1d, 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04,
649 0x10, 0x38, 0x62, 0xc6, 0x44, 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b,
650 0xf0, 0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0, 0x00, 0x00,
653 static const uint8_t kNSSDER[] = {
654 0x30, 0x53, 0x02, 0x01, 0x03, 0x30, 0x13, 0x06, 0x09, 0x2a, 0x86,
655 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x06, 0x04, 0x04,
656 0x01, 0x02, 0x03, 0x04, 0x30, 0x39, 0x30, 0x21, 0x30, 0x09, 0x06,
657 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x84,
658 0x98, 0xfc, 0x66, 0x33, 0xee, 0xba, 0xe7, 0x90, 0xc1, 0xb6, 0xe8,
659 0x8f, 0xfe, 0x1d, 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04, 0x10, 0x38,
660 0x62, 0xc6, 0x44, 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b, 0xf0,
661 0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0,
666 static const uint8_t kConstructedStringBER[] = {
667 0xa0, 0x10, 0x24, 0x06, 0x04, 0x01, 0x00, 0x04, 0x01,
668 0x01, 0x24, 0x06, 0x04, 0x01, 0x02, 0x04, 0x01, 0x03,
670 static const uint8_t kConstructedStringDER[] = {
671 0xa0, 0x08, 0x04, 0x02, 0x00, 0x01, 0x04, 0x02, 0x02, 0x03,
676 static const uint8_t kConstructedBitStringBER[] = {
677 0x23, 0x0a, 0x03, 0x03, 0x00, 0x12, 0x34, 0x03, 0x03, 0x00, 0x56, 0x78};
681 kNonMinimalLengthBER);
687 kConstructedStringBER);
689 kConstructedBitStringBER);
701 {
"0000",
true,
false, 0},
702 {
"0100",
true,
false, 1},
703 {
"020101",
true,
false, 2},
706 {
"02810101",
true,
true, 2},
707 {
"0282000101",
true,
true, 2},
708 {
"028300000101",
true,
true, 2},
709 {
"02840000000101",
true,
true, 2},
711 {
"02850000000101",
false,
false, 0},
713 {
"0280",
false,
false, 0},
715 {
"3f0000",
false,
false, 0},
716 {
"1f0100",
false,
false, 0},
717 {
"1f4000",
true,
false, 0x40},
718 {
"1f804000",
false,
false, 0},
721 TEST(CBSTest, BERElementTest) {
725 std::vector<uint8_t> in_bytes;
758 {
"\x80\x03\x61\x61\x61", 5,
true,
"aaa", 3},
760 {
"\xa0\x09\x04\x01\x61\x04\x01\x61\x04\x01\x61", 11,
true,
"aaa", 3},
762 {
"\xa0\x0b\x24\x06\x04\x01\x61\x04\x01\x61\x04\x01\x61", 13,
false,
nullptr,
765 {
"\x81\x03\x61\x61\x61", 5,
false,
nullptr, 0},
766 {
"\xa1\x09\x04\x01\x61\x04\x01\x61\x04\x01\x61", 11,
false,
nullptr, 0},
768 {
"\xa1\x09\x0c\x01\x61\x0c\x01\x61\x0c\x01\x61", 11,
false,
nullptr, 0},
771 TEST(CBSTest, ImplicitString) {
780 bssl::UniquePtr<uint8_t> scoper(
storage);
797 {0,
"\x02\x01\x00", 3},
798 {1,
"\x02\x01\x01", 3},
799 {127,
"\x02\x01\x7f", 3},
800 {128,
"\x02\x02\x00\x80", 4},
801 {0xdeadbeef,
"\x02\x05\x00\xde\xad\xbe\xef", 7},
803 "\x02\x08\x01\x02\x03\x04\x05\x06\x07\x08", 10},
805 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff", 11},
816 {
"\x03\x01\x00", 3,
false},
818 {
"\x02\x00", 2,
false},
820 {
"\x02\x01\x80", 3,
false},
822 {
"\x02\x09\x01\x00\x00\x00\x00\x00\x00\x00\x00", 11,
true},
824 {
"\x02\x02\x00\x01", 4,
false},
853 bssl::UniquePtr<uint8_t> scoper(
out);
880 {0,
"\x02\x01\x00", 3},
881 {1,
"\x02\x01\x01", 3},
882 {-1,
"\x02\x01\xff", 3},
883 {127,
"\x02\x01\x7f", 3},
884 {-127,
"\x02\x01\x81", 3},
885 {128,
"\x02\x02\x00\x80", 4},
886 {-128,
"\x02\x01\x80", 3},
887 {129,
"\x02\x02\x00\x81", 4},
888 {-129,
"\x02\x02\xff\x7f", 4},
889 {0xdeadbeef,
"\x02\x05\x00\xde\xad\xbe\xef", 7},
890 {
INT64_C(0x0102030405060708),
"\x02\x08\x01\x02\x03\x04\x05\x06\x07\x08",
892 {
INT64_MIN,
"\x02\x08\x80\x00\x00\x00\x00\x00\x00\x00", 10},
893 {
INT64_MAX,
"\x02\x08\x7f\xff\xff\xff\xff\xff\xff\xff", 10},
904 {
"\x03\x01\x00", 3,
false},
906 {
"\x02\x00", 2,
false},
908 {
"\x02\x09\x01\x00\x00\x00\x00\x00\x00\x00\x00", 11,
true},
910 {
"\x02\x09\x08\xff\xff\xff\xff\xff\xff\xff\xff", 11,
true},
912 {
"\x02\x02\x00\x01", 4,
false},
914 {
"\x02\x02\xff\xff", 4,
false},
943 bssl::UniquePtr<uint8_t> scoper(
out);
996 static const uint8_t kZeros[256] = {0};
1029 static const std::vector<uint8_t> kValidBitStrings[] = {
1034 {0x06, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0},
1036 for (
const auto&
test : kValidBitStrings) {
1043 static const std::vector<uint8_t> kInvalidBitStrings[] = {
1045 std::vector<uint8_t>{},
1054 for (
const auto&
test : kInvalidBitStrings) {
1064 static const struct {
1065 std::vector<uint8_t>
in;
1071 {{0x07, 0x80}, 0,
true},
1072 {{0x06, 0x0f, 0x40}, 0,
false},
1073 {{0x06, 0x0f, 0x40}, 1,
false},
1074 {{0x06, 0x0f, 0x40}, 2,
false},
1075 {{0x06, 0x0f, 0x40}, 3,
false},
1076 {{0x06, 0x0f, 0x40}, 4,
true},
1077 {{0x06, 0x0f, 0x40}, 5,
true},
1078 {{0x06, 0x0f, 0x40}, 6,
true},
1079 {{0x06, 0x0f, 0x40}, 7,
true},
1080 {{0x06, 0x0f, 0x40}, 8,
false},
1081 {{0x06, 0x0f, 0x40}, 9,
true},
1083 {{0x06, 0x0f, 0x40}, 10,
false},
1084 {{0x06, 0x0f, 0x40}, 15,
false},
1085 {{0x06, 0x0f, 0x40}, 16,
false},
1086 {{0x06, 0x0f, 0x40}, 1000,
false},
1088 for (
const auto&
test : kBitTests) {
1101 std::vector<uint8_t> der;
1105 {
"0.2.3.4", {0x2, 0x3, 0x4}},
1106 {
"1.2.3.4", {0x2a, 0x3, 0x4}},
1107 {
"2.2.3.4", {0x52, 0x3, 0x4}},
1108 {
"1.2.840.113554.4.1.72585",
1109 {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09}},
1118 {
"1.2.18446744073709551615",
1119 {0x2a, 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}},
1120 {
"2.18446744073709551535",
1121 {0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}},
1124 const char *kInvalidTexts[] = {
1142 "1.2.18446744073709551616",
1143 "2.18446744073709551536",
1146 const std::vector<uint8_t> kInvalidDER[] = {
1154 {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09,
1155 0x82, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00},
1158 for (
const auto &t : kValidOIDs) {
1161 bssl::ScopedCBB cbb;
1167 bssl::UniquePtr<uint8_t> free_out(
out);
1177 for (
const char *t : kInvalidTexts) {
1179 bssl::ScopedCBB cbb;
1184 for (
const auto &t : kInvalidDER) {
1195 std::vector<uint8_t>
in,
out;
1196 } kValidInputs[] = {
1200 {{0x30, 0x00}, {0x30, 0x00}},
1202 {{0x30, 0x00, 0x30, 0x00}, {0x30, 0x00, 0x30, 0x00}},
1204 {{0x30, 0x02, 0x00, 0x00,
1207 0x30, 0x02, 0x00, 0x00,
1208 0x30, 0x03, 0x00, 0x00, 0x00,
1210 0x30, 0x03, 0x00, 0x00, 0x01,
1218 0x30, 0x02, 0x00, 0x00,
1219 0x30, 0x02, 0x00, 0x00,
1220 0x30, 0x03, 0x00, 0x00, 0x00,
1221 0x30, 0x03, 0x00, 0x00, 0x01}},
1225 for (
const auto &t : kValidInputs) {
1228 bssl::ScopedCBB cbb;
1245 const std::vector<uint8_t> kInvalidInputs[] = {
1248 {0x30, 0x00, 0x30, 0x00, 0x30, 0x01},
1251 for (
const auto &t : kInvalidInputs) {
1254 bssl::ScopedCBB cbb;
1265 std::vector<uint8_t>
ret;
1266 for (; *
str != 0;
str++) {
1267 for (
size_t i = 0;
i <
sizeof(
T);
i++) {
1268 ret.push_back(
static_cast<uint8_t>(*
str >> (8 * (
sizeof(
T) - 1 -
i))));
1275 std::vector<uint32_t>
ret;
1276 for (; *
str != 0;
str++) {
1286 std::vector<uint8_t>
in;
1287 std::vector<uint32_t>
out;
1302 {0xfc, 0x84, 0x80, 0x80, 0x80, 0x80},
1341 {0xed, 0xa0, 0x80, 0xed, 0xb0, 0x80},
1346 {0xed, 0xa0, 0x80, 0xed, 0xbf, 0xbf},
1351 {0xed, 0xad, 0xbf, 0xed, 0xb0, 0x80},
1356 {0xed, 0xad, 0xbf, 0xed, 0xbf, 0xbf},
1361 {0xed, 0xae, 0x80, 0xed, 0xb0, 0x80},
1366 {0xed, 0xae, 0x80, 0xed, 0xbf, 0xbf},
1371 {0xed, 0xaf, 0xbf, 0xed, 0xb0, 0x80},
1376 {0xed, 0xaf, 0xbf, 0xed, 0xbf, 0xbf},
1414 for (
const auto &t :
kTests) {
1420 std::vector<uint32_t>
out;
1424 if (!t.decode(&
cbs, &
u)) {
1435 bssl::ScopedCBB cbb;
1444 static const uint32_t kBadCodePoints[] = {
1457 bssl::ScopedCBB cbb;