diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /security/nss/gtests/util_gtest/util_secasn1d_unittest.cc | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'security/nss/gtests/util_gtest/util_secasn1d_unittest.cc')
-rw-r--r-- | security/nss/gtests/util_gtest/util_secasn1d_unittest.cc | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/security/nss/gtests/util_gtest/util_secasn1d_unittest.cc b/security/nss/gtests/util_gtest/util_secasn1d_unittest.cc new file mode 100644 index 0000000000..c6cd0c0448 --- /dev/null +++ b/security/nss/gtests/util_gtest/util_secasn1d_unittest.cc @@ -0,0 +1,69 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "secasn1.h" + +#include "gtest/gtest.h" + +namespace nss_test { + +class SECASN1DTest : public ::testing::Test {}; + +struct InnerSequenceItem { + SECItem value; +}; + +struct OuterSequence { + InnerSequenceItem *item; +}; + +static const SEC_ASN1Template InnerSequenceTemplate[] = { + {SEC_ASN1_SEQUENCE, 0, NULL, sizeof(InnerSequenceItem)}, + {SEC_ASN1_ANY, offsetof(InnerSequenceItem, value)}, + {0}}; + +static const SEC_ASN1Template OuterSequenceTemplate[] = { + {SEC_ASN1_SEQUENCE_OF, offsetof(OuterSequence, item), InnerSequenceTemplate, + sizeof(OuterSequence)}}; + +TEST_F(SECASN1DTest, IndefiniteSequenceInIndefiniteGroup) { + PLArenaPool *arena = PORT_NewArena(4096); + OuterSequence *outer = nullptr; + SECStatus rv; + + // echo "SEQUENCE indefinite { + // SEQUENCE indefinite { + // PrintableString { \"Test for Bug 1387919\" } + // } + // }" | ascii2der | xxd -i + unsigned char ber[] = {0x30, 0x80, 0x30, 0x80, 0x13, 0x14, 0x54, 0x65, + 0x73, 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x42, + 0x75, 0x67, 0x20, 0x31, 0x33, 0x38, 0x37, 0x39, + 0x31, 0x39, 0x00, 0x00, 0x00, 0x00}; + + // Decoding should fail if the trailing EOC is omitted (Bug 1387919) + SECItem missingEOC = {siBuffer, ber, sizeof(ber) - 2}; + rv = SEC_ASN1DecodeItem(arena, &outer, OuterSequenceTemplate, &missingEOC); + EXPECT_EQ(SECFailure, rv); + + // With the trailing EOC, this is well-formed BER. + SECItem goodEncoding = {siBuffer, ber, sizeof(ber)}; + rv = SEC_ASN1DecodeItem(arena, &outer, OuterSequenceTemplate, &goodEncoding); + EXPECT_EQ(SECSuccess, rv); + + // |outer| should now be a null terminated array of InnerSequenceItems + + // The first item is PrintableString { \"Test for Bug 1387919\" } + EXPECT_EQ(outer[0].item->value.len, 22U); + EXPECT_EQ(0, memcmp(outer[0].item->value.data, ber + 4, 22)); + + // The second item is the null terminator + EXPECT_EQ(outer[1].item, nullptr); + + PORT_FreeArena(arena, PR_FALSE); +} + +} // namespace nss_test |