SignedCertificateTimestamp.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 SignedCertificateTimestamp_h
  6. #define SignedCertificateTimestamp_h
  7. #include "mozilla/Vector.h"
  8. #include "pkix/Input.h"
  9. #include "pkix/Result.h"
  10. // Structures related to Certificate Transparency (RFC 6962).
  11. namespace mozilla { namespace ct {
  12. typedef Vector<uint8_t> Buffer;
  13. // LogEntry struct in RFC 6962, Section 3.1.
  14. struct LogEntry
  15. {
  16. // LogEntryType enum in RFC 6962, Section 3.1.
  17. enum class Type {
  18. X509 = 0,
  19. Precert = 1
  20. };
  21. void Reset();
  22. Type type;
  23. // Set if type == X509.
  24. Buffer leafCertificate;
  25. // Set if type == Precert.
  26. Buffer issuerKeyHash;
  27. Buffer tbsCertificate;
  28. };
  29. // Helper structure to represent Digitally Signed data, as described in
  30. // Sections 4.7 and 7.4.1.4.1 of RFC 5246.
  31. struct DigitallySigned
  32. {
  33. enum class HashAlgorithm {
  34. None = 0,
  35. MD5 = 1,
  36. SHA1 = 2,
  37. SHA224 = 3,
  38. SHA256 = 4,
  39. SHA384 = 5,
  40. SHA512 = 6,
  41. };
  42. enum class SignatureAlgorithm {
  43. Anonymous = 0,
  44. RSA = 1,
  45. DSA = 2,
  46. ECDSA = 3
  47. };
  48. // Returns true if |aHashAlgorithm| and |aSignatureAlgorithm|
  49. // match this DigitallySigned hash and signature algorithms.
  50. bool SignatureParametersMatch(HashAlgorithm aHashAlgorithm,
  51. SignatureAlgorithm aSignatureAlgorithm) const;
  52. HashAlgorithm hashAlgorithm;
  53. SignatureAlgorithm signatureAlgorithm;
  54. // 'signature' field.
  55. Buffer signatureData;
  56. };
  57. // SignedCertificateTimestamp struct in RFC 6962, Section 3.2.
  58. struct SignedCertificateTimestamp
  59. {
  60. // Version enum in RFC 6962, Section 3.2.
  61. enum class Version {
  62. V1 = 0,
  63. };
  64. Version version;
  65. Buffer logId;
  66. // "timestamp" is the current time in milliseconds, measured since the epoch,
  67. // ignoring leap seconds. See RFC 6962, Section 3.2.
  68. uint64_t timestamp;
  69. Buffer extensions;
  70. DigitallySigned signature;
  71. // Supplementary fields, not defined in CT RFC. Set during the various
  72. // stages of processing the received SCTs.
  73. enum class Origin {
  74. Unknown,
  75. Embedded,
  76. TLSExtension,
  77. OCSPResponse
  78. };
  79. enum class VerificationStatus {
  80. None,
  81. // The SCT is from a known log, and the signature is valid.
  82. OK,
  83. // The SCT is from an unknown log and can not be verified.
  84. UnknownLog,
  85. // The SCT is from a known log, but the signature is invalid.
  86. InvalidSignature,
  87. // The SCT signature is valid, but the timestamp is in the future.
  88. // Such SCT are considered invalid (see RFC 6962, Section 5.2).
  89. InvalidTimestamp
  90. };
  91. Origin origin;
  92. VerificationStatus verificationStatus;
  93. };
  94. inline pkix::Result BufferToInput(const Buffer& buffer, pkix::Input& input)
  95. {
  96. return input.Init(buffer.begin(), buffer.length());
  97. }
  98. inline pkix::Result InputToBuffer(pkix::Input input, Buffer& buffer)
  99. {
  100. buffer.clear();
  101. if (!buffer.append(input.UnsafeGetData(), input.GetLength())) {
  102. return pkix::Result::FATAL_ERROR_NO_MEMORY;
  103. }
  104. return pkix::Success;
  105. }
  106. } } // namespace mozilla::ct
  107. namespace mozilla {
  108. // Comparison operators are placed under mozilla namespace since
  109. // mozilla::ct::Buffer is actually mozilla::Vector.
  110. bool operator==(const ct::Buffer& a, const ct::Buffer& b);
  111. bool operator!=(const ct::Buffer& a, const ct::Buffer& b);
  112. } // namespace mozilla
  113. #endif // SignedCertificateTimestamp_h