CTObjectsExtractorTest.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "CTLogVerifier.h"
  6. #include "CTObjectsExtractor.h"
  7. #include "CTSerialization.h"
  8. #include "CTTestUtils.h"
  9. #include "gtest/gtest.h"
  10. #include "nss.h"
  11. namespace mozilla { namespace ct {
  12. using namespace pkix;
  13. class CTObjectsExtractorTest : public ::testing::Test
  14. {
  15. public:
  16. void SetUp() override
  17. {
  18. // Does nothing if NSS is already initialized.
  19. MOZ_RELEASE_ASSERT(NSS_NoDB_Init(nullptr) == SECSuccess);
  20. mTestCert = GetDEREncodedX509Cert();
  21. mEmbeddedCert = GetDEREncodedTestEmbeddedCert();
  22. mCaCert = GetDEREncodedCACert();
  23. mCaCertSPKI = ExtractCertSPKI(mCaCert);
  24. Buffer logPublicKey = GetTestPublicKey();
  25. ASSERT_EQ(Success, mLog.Init(InputForBuffer(logPublicKey)));
  26. }
  27. protected:
  28. Buffer mTestCert;
  29. Buffer mEmbeddedCert;
  30. Buffer mCaCert;
  31. Buffer mCaCertSPKI;
  32. CTLogVerifier mLog;
  33. };
  34. TEST_F(CTObjectsExtractorTest, ExtractPrecert)
  35. {
  36. LogEntry entry;
  37. ASSERT_EQ(Success,
  38. GetPrecertLogEntry(InputForBuffer(mEmbeddedCert),
  39. InputForBuffer(mCaCertSPKI),
  40. entry));
  41. EXPECT_EQ(LogEntry::Type::Precert, entry.type);
  42. // Should have empty leaf cert for this log entry type.
  43. EXPECT_TRUE(entry.leafCertificate.empty());
  44. EXPECT_EQ(GetDefaultIssuerKeyHash(), entry.issuerKeyHash);
  45. EXPECT_EQ(GetDEREncodedTestTbsCert(), entry.tbsCertificate);
  46. }
  47. TEST_F(CTObjectsExtractorTest, ExtractOrdinaryX509Cert)
  48. {
  49. LogEntry entry;
  50. ASSERT_EQ(Success, GetX509LogEntry(InputForBuffer(mTestCert), entry));
  51. EXPECT_EQ(LogEntry::Type::X509, entry.type);
  52. // Should have empty tbsCertificate / issuerKeyHash for this log entry type.
  53. EXPECT_TRUE(entry.tbsCertificate.empty());
  54. EXPECT_TRUE(entry.issuerKeyHash.empty());
  55. // Length of leafCertificate should be 718, see the CT Serialization tests.
  56. EXPECT_EQ(718U, entry.leafCertificate.length());
  57. }
  58. // Test that an externally-provided SCT verifies over the LogEntry
  59. // of a regular X.509 Certificate
  60. TEST_F(CTObjectsExtractorTest, ComplementarySCTVerifies)
  61. {
  62. SignedCertificateTimestamp sct;
  63. GetX509CertSCT(sct);
  64. LogEntry entry;
  65. ASSERT_EQ(Success, GetX509LogEntry(InputForBuffer(mTestCert), entry));
  66. EXPECT_EQ(Success, mLog.Verify(entry, sct));
  67. }
  68. } } // namespace mozilla::ct