CTTestUtils.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #ifndef CTTestUtils_h
  6. #define CTTestUtils_h
  7. #include <iostream>
  8. #include "pkix/Input.h"
  9. #include "pkix/Time.h"
  10. #include "seccomon.h"
  11. #include "SignedCertificateTimestamp.h"
  12. #include "SignedTreeHead.h"
  13. namespace mozilla { namespace ct {
  14. // Note: unless specified otherwise, all test data is taken from
  15. // Certificate Transparency test data repository at
  16. // https://github.com/google/certificate-transparency/tree/master/test/testdata
  17. // Fills |entry| with test data for an X.509 entry.
  18. void GetX509CertLogEntry(LogEntry& entry);
  19. // Returns a DER-encoded X509 cert. The SCT provided by
  20. // GetX509CertSCT is signed over this certificate.
  21. Buffer GetDEREncodedX509Cert();
  22. // Fills |entry| with test data for a Precertificate entry.
  23. void GetPrecertLogEntry(LogEntry& entry);
  24. // Returns the binary representation of a test DigitallySigned.
  25. Buffer GetTestDigitallySigned();
  26. // Returns the source data of the test DigitallySigned.
  27. Buffer GetTestDigitallySignedData();
  28. // Returns the binary representation of a test serialized SCT.
  29. Buffer GetTestSignedCertificateTimestamp();
  30. // Test log key.
  31. Buffer GetTestPublicKey();
  32. // ID of test log key.
  33. Buffer GetTestPublicKeyId();
  34. // SCT for the X509Certificate provided above.
  35. void GetX509CertSCT(SignedCertificateTimestamp& sct);
  36. // SCT for the Precertificate log entry provided above.
  37. void GetPrecertSCT(SignedCertificateTimestamp& sct);
  38. // Issuer key hash.
  39. Buffer GetDefaultIssuerKeyHash();
  40. // A sample, valid STH.
  41. void GetSampleSignedTreeHead(SignedTreeHead& sth);
  42. // The SHA256 root hash for the sample STH.
  43. Buffer GetSampleSTHSHA256RootHash();
  44. // The tree head signature for the sample STH.
  45. Buffer GetSampleSTHTreeHeadSignature();
  46. // The same signature as GetSampleSTHTreeHeadSignature, decoded.
  47. void GetSampleSTHTreeHeadDecodedSignature(DigitallySigned& signature);
  48. // Certificate with embedded SCT in an X509v3 extension.
  49. Buffer GetDEREncodedTestEmbeddedCert();
  50. // For the above certificate, the corresponsing TBSCertificate without
  51. // the embedded SCT extension.
  52. Buffer GetDEREncodedTestTbsCert();
  53. // As above, but signed with an intermediate CA certificate containing
  54. // the CT extended key usage OID 1.3.6.1.4.1.11129.2.4.4 for issuing precerts
  55. // (i.e. signed with a "precert CA certificate").
  56. Buffer GetDEREncodedTestEmbeddedWithPreCACert();
  57. // The issuer of the above certificates (self-signed root CA certificate).
  58. Buffer GetDEREncodedCACert();
  59. // An intermediate CA certificate issued by the above CA.
  60. Buffer GetDEREncodedIntermediateCert();
  61. // Certificate with embedded SCT signed by the intermediate certificate above.
  62. Buffer GetDEREncodedTestEmbeddedWithIntermediateCert();
  63. // As above, but signed by the precert CA certificate.
  64. Buffer GetDEREncodedTestEmbeddedWithIntermediatePreCACert();
  65. // Given a DER-encoded certificate, returns its SubjectPublicKeyInfo.
  66. Buffer ExtractCertSPKI(pkix::Input cert);
  67. Buffer ExtractCertSPKI(const Buffer& cert);
  68. // Extracts a SignedCertificateTimestampList from the provided leaf certificate
  69. // (kept in X.509v3 extension with OID 1.3.6.1.4.1.11129.2.4.2).
  70. void ExtractEmbeddedSCTList(pkix::Input cert, Buffer& result);
  71. void ExtractEmbeddedSCTList(const Buffer& cert, Buffer& result);
  72. // Extracts a SignedCertificateTimestampList that has been embedded within
  73. // an OCSP response as an extension with the OID 1.3.6.1.4.1.11129.2.4.5.
  74. // The OCSP response is verified, and the verification must succeed for the
  75. // extension to be extracted.
  76. void ExtractSCTListFromOCSPResponse(pkix::Input cert,
  77. pkix::Input issuerSPKI,
  78. pkix::Input encodedResponse,
  79. pkix::Time time,
  80. Buffer& result);
  81. // We need this in tests code since mozilla::Vector only allows move assignment.
  82. Buffer cloneBuffer(const Buffer& buffer);
  83. // Returns Input for the data stored in the buffer, failing assertion on error.
  84. pkix::Input InputForBuffer(const Buffer& buffer);
  85. // Returns Input for the data stored in the item, failing assertion on error.
  86. pkix::Input InputForSECItem(const SECItem& item);
  87. } } // namespace mozilla::ct
  88. namespace mozilla {
  89. // GTest needs this to be in Buffer's namespace (i.e. in mozilla::Vector's).
  90. std::ostream& operator<<(std::ostream& stream, const ct::Buffer& buf);
  91. } // namespace mozilla
  92. #endif // CTTestUtils_h