CTLogVerifier.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 "CTSerialization.h"
  7. #include "hasht.h"
  8. #include "mozilla/ArrayUtils.h"
  9. #include "mozilla/Assertions.h"
  10. #include "pkix/pkixnss.h"
  11. #include "pkix/pkixutil.h"
  12. namespace mozilla { namespace ct {
  13. using namespace mozilla::pkix;
  14. // A TrustDomain used to extract the SCT log signature parameters
  15. // given its subjectPublicKeyInfo.
  16. // Only RSASSA-PKCS1v15 with SHA-256 and ECDSA (using the NIST P-256 curve)
  17. // with SHA-256 are allowed.
  18. // RSA keys must be at least 2048 bits.
  19. // See See RFC 6962, Section 2.1.4.
  20. class SignatureParamsTrustDomain final : public TrustDomain
  21. {
  22. public:
  23. SignatureParamsTrustDomain()
  24. : mSignatureAlgorithm(DigitallySigned::SignatureAlgorithm::Anonymous)
  25. {
  26. }
  27. Result GetCertTrust(EndEntityOrCA, const CertPolicyId&, Input,
  28. TrustLevel&) override
  29. {
  30. return Result::FATAL_ERROR_LIBRARY_FAILURE;
  31. }
  32. Result FindIssuer(Input, IssuerChecker&, Time) override
  33. {
  34. return Result::FATAL_ERROR_LIBRARY_FAILURE;
  35. }
  36. Result CheckRevocation(EndEntityOrCA, const CertID&, Time, Duration,
  37. const Input*, const Input*, const Input*) override
  38. {
  39. return Result::FATAL_ERROR_LIBRARY_FAILURE;
  40. }
  41. Result IsChainValid(const DERArray&, Time, const CertPolicyId&) override
  42. {
  43. return Result::FATAL_ERROR_LIBRARY_FAILURE;
  44. }
  45. Result DigestBuf(Input, DigestAlgorithm, uint8_t*, size_t) override
  46. {
  47. return Result::FATAL_ERROR_LIBRARY_FAILURE;
  48. }
  49. Result CheckSignatureDigestAlgorithm(DigestAlgorithm, EndEntityOrCA,
  50. Time) override
  51. {
  52. return Result::FATAL_ERROR_LIBRARY_FAILURE;
  53. }
  54. Result CheckECDSACurveIsAcceptable(EndEntityOrCA, NamedCurve curve) override
  55. {
  56. MOZ_ASSERT(mSignatureAlgorithm ==
  57. DigitallySigned::SignatureAlgorithm::Anonymous);
  58. if (curve != NamedCurve::secp256r1) {
  59. return Result::ERROR_UNSUPPORTED_ELLIPTIC_CURVE;
  60. }
  61. mSignatureAlgorithm = DigitallySigned::SignatureAlgorithm::ECDSA;
  62. return Success;
  63. }
  64. Result VerifyECDSASignedDigest(const SignedDigest&, Input) override
  65. {
  66. return Result::FATAL_ERROR_LIBRARY_FAILURE;
  67. }
  68. Result CheckRSAPublicKeyModulusSizeInBits(EndEntityOrCA,
  69. unsigned int modulusSizeInBits)
  70. override
  71. {
  72. MOZ_ASSERT(mSignatureAlgorithm ==
  73. DigitallySigned::SignatureAlgorithm::Anonymous);
  74. // Require RSA keys of at least 2048 bits. See RFC 6962, Section 2.1.4.
  75. if (modulusSizeInBits < 2048) {
  76. return Result::ERROR_INADEQUATE_KEY_SIZE;
  77. }
  78. mSignatureAlgorithm = DigitallySigned::SignatureAlgorithm::RSA;
  79. return Success;
  80. }
  81. Result VerifyRSAPKCS1SignedDigest(const SignedDigest&, Input) override
  82. {
  83. return Result::FATAL_ERROR_LIBRARY_FAILURE;
  84. }
  85. Result CheckValidityIsAcceptable(Time, Time, EndEntityOrCA,
  86. KeyPurposeId) override
  87. {
  88. return Result::FATAL_ERROR_LIBRARY_FAILURE;
  89. }
  90. Result NetscapeStepUpMatchesServerAuth(Time, bool&) override
  91. {
  92. return Result::FATAL_ERROR_LIBRARY_FAILURE;
  93. }
  94. void NoteAuxiliaryExtension(AuxiliaryExtension, Input) override
  95. {
  96. }
  97. DigitallySigned::SignatureAlgorithm mSignatureAlgorithm;
  98. };
  99. CTLogVerifier::CTLogVerifier()
  100. : mSignatureAlgorithm(DigitallySigned::SignatureAlgorithm::Anonymous)
  101. {
  102. }
  103. Result
  104. CTLogVerifier::Init(Input subjectPublicKeyInfo)
  105. {
  106. SignatureParamsTrustDomain trustDomain;
  107. Result rv = CheckSubjectPublicKeyInfo(subjectPublicKeyInfo, trustDomain,
  108. EndEntityOrCA::MustBeEndEntity);
  109. if (rv != Success) {
  110. return rv;
  111. }
  112. mSignatureAlgorithm = trustDomain.mSignatureAlgorithm;
  113. rv = InputToBuffer(subjectPublicKeyInfo, mSubjectPublicKeyInfo);
  114. if (rv != Success) {
  115. return rv;
  116. }
  117. if (!mKeyId.resizeUninitialized(SHA256_LENGTH)) {
  118. return Result::FATAL_ERROR_NO_MEMORY;
  119. }
  120. rv = DigestBufNSS(subjectPublicKeyInfo, DigestAlgorithm::sha256,
  121. mKeyId.begin(), mKeyId.length());
  122. if (rv != Success) {
  123. return rv;
  124. }
  125. return Success;
  126. }
  127. Result
  128. CTLogVerifier::Verify(const LogEntry& entry,
  129. const SignedCertificateTimestamp& sct)
  130. {
  131. if (mKeyId.empty() || sct.logId != mKeyId) {
  132. return Result::FATAL_ERROR_INVALID_ARGS;
  133. }
  134. if (!SignatureParametersMatch(sct.signature)) {
  135. return Result::FATAL_ERROR_INVALID_ARGS;
  136. }
  137. Buffer serializedLogEntry;
  138. Result rv = EncodeLogEntry(entry, serializedLogEntry);
  139. if (rv != Success) {
  140. return rv;
  141. }
  142. Input logEntryInput;
  143. rv = BufferToInput(serializedLogEntry, logEntryInput);
  144. if (rv != Success) {
  145. return rv;
  146. }
  147. Input sctExtensionsInput;
  148. rv = BufferToInput(sct.extensions, sctExtensionsInput);
  149. if (rv != Success) {
  150. return rv;
  151. }
  152. Buffer serializedData;
  153. rv = EncodeV1SCTSignedData(sct.timestamp, logEntryInput, sctExtensionsInput,
  154. serializedData);
  155. if (rv != Success) {
  156. return rv;
  157. }
  158. return VerifySignature(serializedData, sct.signature.signatureData);
  159. }
  160. Result
  161. CTLogVerifier::VerifySignedTreeHead(const SignedTreeHead& sth)
  162. {
  163. if (!SignatureParametersMatch(sth.signature)) {
  164. return Result::FATAL_ERROR_INVALID_ARGS;
  165. }
  166. Buffer serializedData;
  167. Result rv = EncodeTreeHeadSignature(sth, serializedData);
  168. if (rv != Success) {
  169. return rv;
  170. }
  171. return VerifySignature(serializedData, sth.signature.signatureData);
  172. }
  173. bool
  174. CTLogVerifier::SignatureParametersMatch(const DigitallySigned& signature)
  175. {
  176. return signature.SignatureParametersMatch(
  177. DigitallySigned::HashAlgorithm::SHA256, mSignatureAlgorithm);
  178. }
  179. Result
  180. CTLogVerifier::VerifySignature(Input data, Input signature)
  181. {
  182. uint8_t digest[SHA256_LENGTH];
  183. Result rv = DigestBufNSS(data, DigestAlgorithm::sha256, digest,
  184. ArrayLength(digest));
  185. if (rv != Success) {
  186. return rv;
  187. }
  188. SignedDigest signedDigest;
  189. signedDigest.digestAlgorithm = DigestAlgorithm::sha256;
  190. rv = signedDigest.digest.Init(digest, ArrayLength(digest));
  191. if (rv != Success) {
  192. return rv;
  193. }
  194. rv = signedDigest.signature.Init(signature);
  195. if (rv != Success) {
  196. return rv;
  197. }
  198. Input spki;
  199. rv = BufferToInput(mSubjectPublicKeyInfo, spki);
  200. if (rv != Success) {
  201. return rv;
  202. }
  203. switch (mSignatureAlgorithm) {
  204. case DigitallySigned::SignatureAlgorithm::RSA:
  205. rv = VerifyRSAPKCS1SignedDigestNSS(signedDigest, spki, nullptr);
  206. break;
  207. case DigitallySigned::SignatureAlgorithm::ECDSA:
  208. rv = VerifyECDSASignedDigestNSS(signedDigest, spki, nullptr);
  209. break;
  210. // We do not expect new values added to this enum any time soon,
  211. // so just listing all the available ones seems to be the easiest way
  212. // to suppress warning C4061 on MSVC (which expects all values of the
  213. // enum to be explicitly handled).
  214. case DigitallySigned::SignatureAlgorithm::Anonymous:
  215. case DigitallySigned::SignatureAlgorithm::DSA:
  216. default:
  217. MOZ_ASSERT_UNREACHABLE("RSA/ECDSA expected");
  218. return Result::FATAL_ERROR_INVALID_ARGS;
  219. }
  220. if (rv != Success) {
  221. if (IsFatalError(rv)) {
  222. return rv;
  223. }
  224. // If the error is non-fatal, we assume the signature was invalid.
  225. return Result::ERROR_BAD_SIGNATURE;
  226. }
  227. return Success;
  228. }
  229. Result
  230. CTLogVerifier::VerifySignature(const Buffer& data, const Buffer& signature)
  231. {
  232. Input dataInput;
  233. Result rv = BufferToInput(data, dataInput);
  234. if (rv != Success) {
  235. return rv;
  236. }
  237. Input signatureInput;
  238. rv = BufferToInput(signature, signatureInput);
  239. if (rv != Success) {
  240. return rv;
  241. }
  242. return VerifySignature(dataInput, signatureInput);
  243. }
  244. } } // namespace mozilla::ct