tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

util_secasn1d_unittest.cc (2364B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "secasn1.h"
      8 
      9 #include "gtest/gtest.h"
     10 
     11 namespace nss_test {
     12 
     13 class SECASN1DTest : public ::testing::Test {};
     14 
     15 struct InnerSequenceItem {
     16  SECItem value;
     17 };
     18 
     19 struct OuterSequence {
     20  InnerSequenceItem *item;
     21 };
     22 
     23 static const SEC_ASN1Template InnerSequenceTemplate[] = {
     24    {SEC_ASN1_SEQUENCE, 0, NULL, sizeof(InnerSequenceItem)},
     25    {SEC_ASN1_ANY, offsetof(InnerSequenceItem, value)},
     26    {0}};
     27 
     28 static const SEC_ASN1Template OuterSequenceTemplate[] = {
     29    {SEC_ASN1_SEQUENCE_OF, offsetof(OuterSequence, item), InnerSequenceTemplate,
     30     sizeof(OuterSequence)}};
     31 
     32 TEST_F(SECASN1DTest, IndefiniteSequenceInIndefiniteGroup) {
     33  PLArenaPool *arena = PORT_NewArena(4096);
     34  OuterSequence *outer = nullptr;
     35  SECStatus rv;
     36 
     37  // echo "SEQUENCE indefinite {
     38  //         SEQUENCE indefinite {
     39  //            PrintableString { \"Test for Bug 1387919\" }
     40  //         }
     41  //       }" | ascii2der | xxd -i
     42  unsigned char ber[] = {0x30, 0x80, 0x30, 0x80, 0x13, 0x14, 0x54, 0x65,
     43                         0x73, 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x42,
     44                         0x75, 0x67, 0x20, 0x31, 0x33, 0x38, 0x37, 0x39,
     45                         0x31, 0x39, 0x00, 0x00, 0x00, 0x00};
     46 
     47  // Decoding should fail if the trailing EOC is omitted (Bug 1387919)
     48  SECItem missingEOC = {siBuffer, ber, sizeof(ber) - 2};
     49  rv = SEC_ASN1DecodeItem(arena, &outer, OuterSequenceTemplate, &missingEOC);
     50  EXPECT_EQ(SECFailure, rv);
     51 
     52  // With the trailing EOC, this is well-formed BER.
     53  SECItem goodEncoding = {siBuffer, ber, sizeof(ber)};
     54  rv = SEC_ASN1DecodeItem(arena, &outer, OuterSequenceTemplate, &goodEncoding);
     55  EXPECT_EQ(SECSuccess, rv);
     56 
     57  // |outer| should now be a null terminated array of InnerSequenceItems
     58 
     59  // The first item is PrintableString { \"Test for Bug 1387919\" }
     60  EXPECT_EQ(outer[0].item->value.len, 22U);
     61  EXPECT_EQ(0, memcmp(outer[0].item->value.data, ber + 4, 22));
     62 
     63  // The second item is the null terminator
     64  EXPECT_EQ(outer[1].item, nullptr);
     65 
     66  PORT_FreeArena(arena, PR_FALSE);
     67 }
     68 
     69 }  // namespace nss_test