CTLogVerifierTest.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 "CTTestUtils.h"
  7. #include "nss.h"
  8. #include "gtest/gtest.h"
  9. namespace mozilla { namespace ct {
  10. using namespace pkix;
  11. class CTLogVerifierTest : public ::testing::Test
  12. {
  13. public:
  14. void SetUp() override
  15. {
  16. // Does nothing if NSS is already initialized.
  17. MOZ_RELEASE_ASSERT(NSS_NoDB_Init(nullptr) == SECSuccess);
  18. ASSERT_EQ(Success, mLog.Init(InputForBuffer(GetTestPublicKey())));
  19. ASSERT_EQ(GetTestPublicKeyId(), mLog.keyId());
  20. }
  21. protected:
  22. CTLogVerifier mLog;
  23. };
  24. TEST_F(CTLogVerifierTest, VerifiesCertSCT)
  25. {
  26. LogEntry certEntry;
  27. GetX509CertLogEntry(certEntry);
  28. SignedCertificateTimestamp certSct;
  29. GetX509CertSCT(certSct);
  30. EXPECT_EQ(Success, mLog.Verify(certEntry, certSct));
  31. }
  32. TEST_F(CTLogVerifierTest, VerifiesPrecertSCT)
  33. {
  34. LogEntry precertEntry;
  35. GetPrecertLogEntry(precertEntry);
  36. SignedCertificateTimestamp precertSct;
  37. GetPrecertSCT(precertSct);
  38. EXPECT_EQ(Success, mLog.Verify(precertEntry, precertSct));
  39. }
  40. TEST_F(CTLogVerifierTest, FailsInvalidTimestamp)
  41. {
  42. LogEntry certEntry;
  43. GetX509CertLogEntry(certEntry);
  44. SignedCertificateTimestamp certSct;
  45. GetX509CertSCT(certSct);
  46. // Mangle the timestamp, so that it should fail signature validation.
  47. certSct.timestamp = 0;
  48. EXPECT_EQ(Result::ERROR_BAD_SIGNATURE, mLog.Verify(certEntry, certSct));
  49. }
  50. TEST_F(CTLogVerifierTest, FailsInvalidSignature)
  51. {
  52. LogEntry certEntry;
  53. GetX509CertLogEntry(certEntry);
  54. // Mangle the signature, making VerifyECDSASignedDigestNSS (used by
  55. // CTLogVerifier) return ERROR_BAD_SIGNATURE.
  56. SignedCertificateTimestamp certSct;
  57. GetX509CertSCT(certSct);
  58. certSct.signature.signatureData[20] ^= '\xFF';
  59. EXPECT_EQ(Result::ERROR_BAD_SIGNATURE, mLog.Verify(certEntry, certSct));
  60. // Make VerifyECDSASignedDigestNSS return ERROR_BAD_DER. We still expect
  61. // the verifier to return ERROR_BAD_SIGNATURE.
  62. SignedCertificateTimestamp certSct2;
  63. GetX509CertSCT(certSct2);
  64. certSct2.signature.signatureData[0] ^= '\xFF';
  65. EXPECT_EQ(Result::ERROR_BAD_SIGNATURE, mLog.Verify(certEntry, certSct2));
  66. }
  67. TEST_F(CTLogVerifierTest, FailsInvalidLogID)
  68. {
  69. LogEntry certEntry;
  70. GetX509CertLogEntry(certEntry);
  71. SignedCertificateTimestamp certSct;
  72. GetX509CertSCT(certSct);
  73. // Mangle the log ID, which should cause it to match a different log before
  74. // attempting signature validation.
  75. MOZ_RELEASE_ASSERT(certSct.logId.append('\x0'));
  76. EXPECT_EQ(Result::FATAL_ERROR_INVALID_ARGS, mLog.Verify(certEntry, certSct));
  77. }
  78. TEST_F(CTLogVerifierTest, VerifiesValidSTH)
  79. {
  80. SignedTreeHead sth;
  81. GetSampleSignedTreeHead(sth);
  82. EXPECT_EQ(Success, mLog.VerifySignedTreeHead(sth));
  83. }
  84. TEST_F(CTLogVerifierTest, DoesNotVerifyInvalidSTH)
  85. {
  86. SignedTreeHead sth;
  87. GetSampleSignedTreeHead(sth);
  88. sth.sha256RootHash[0] ^= '\xFF';
  89. EXPECT_EQ(Result::ERROR_BAD_SIGNATURE, mLog.VerifySignedTreeHead(sth));
  90. }
  91. // Test that excess data after the public key is rejected.
  92. TEST_F(CTLogVerifierTest, ExcessDataInPublicKey)
  93. {
  94. Buffer key = GetTestPublicKey();
  95. MOZ_RELEASE_ASSERT(key.append("extra", 5));
  96. CTLogVerifier log;
  97. EXPECT_NE(Success, log.Init(InputForBuffer(key)));
  98. }
  99. } } // namespace mozilla::ct