multi_log_ct_verifier.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "net/cert/multi_log_ct_verifier.h"
  5. #include <vector>
  6. #include "base/bind.h"
  7. #include "base/callback.h"
  8. #include "base/logging.h"
  9. #include "base/metrics/histogram_macros.h"
  10. #include "base/values.h"
  11. #include "net/base/net_errors.h"
  12. #include "net/cert/ct_log_verifier.h"
  13. #include "net/cert/ct_objects_extractor.h"
  14. #include "net/cert/ct_serialization.h"
  15. #include "net/cert/ct_signed_certificate_timestamp_log_param.h"
  16. #include "net/cert/sct_status_flags.h"
  17. #include "net/cert/signed_certificate_timestamp_and_status.h"
  18. #include "net/cert/x509_certificate.h"
  19. #include "net/log/net_log_event_type.h"
  20. #include "net/log/net_log_with_source.h"
  21. namespace net {
  22. namespace {
  23. // Record SCT verification status. This metric would help detecting presence
  24. // of unknown CT logs as well as bad deployments (invalid SCTs).
  25. void LogSCTStatusToUMA(ct::SCTVerifyStatus status) {
  26. // Note SCT_STATUS_MAX + 1 is passed to the UMA_HISTOGRAM_ENUMERATION as that
  27. // macro requires the values to be strictly less than the boundary value,
  28. // and SCT_STATUS_MAX is the last valid value of the SCTVerifyStatus enum
  29. // (since that enum is used for IPC as well).
  30. UMA_HISTOGRAM_ENUMERATION("Net.CertificateTransparency.SCTStatus", status,
  31. ct::SCT_STATUS_MAX + 1);
  32. }
  33. // Record SCT origin enum. This metric measure the popularity
  34. // of the various channels of providing SCTs for a certificate.
  35. void LogSCTOriginToUMA(ct::SignedCertificateTimestamp::Origin origin) {
  36. UMA_HISTOGRAM_ENUMERATION("Net.CertificateTransparency.SCTOrigin",
  37. origin,
  38. ct::SignedCertificateTimestamp::SCT_ORIGIN_MAX);
  39. }
  40. void AddSCTAndLogStatus(scoped_refptr<ct::SignedCertificateTimestamp> sct,
  41. ct::SCTVerifyStatus status,
  42. SignedCertificateTimestampAndStatusList* sct_list) {
  43. LogSCTStatusToUMA(status);
  44. sct_list->push_back(SignedCertificateTimestampAndStatus(sct, status));
  45. }
  46. } // namespace
  47. base::CallbackListSubscription
  48. MultiLogCTVerifier::CTLogProvider::RegisterLogsListCallback(
  49. LogListCallbackList::CallbackType callback) {
  50. return callback_list_.Add(std::move(callback));
  51. }
  52. void MultiLogCTVerifier::CTLogProvider::NotifyCallbacks(
  53. const std::vector<scoped_refptr<const net::CTLogVerifier>>& log_verifiers) {
  54. callback_list_.Notify(log_verifiers);
  55. }
  56. MultiLogCTVerifier::CTLogProvider::CTLogProvider() = default;
  57. MultiLogCTVerifier::CTLogProvider::~CTLogProvider() = default;
  58. MultiLogCTVerifier::MultiLogCTVerifier(CTLogProvider* notifier) {
  59. // base::Unretained is safe since we are using a CallbackListSubscription that
  60. // won't outlive |this|.
  61. log_provider_subscription_ =
  62. notifier->RegisterLogsListCallback(base::BindRepeating(
  63. &MultiLogCTVerifier::SetLogs, base::Unretained(this)));
  64. }
  65. MultiLogCTVerifier::~MultiLogCTVerifier() = default;
  66. void MultiLogCTVerifier::SetLogs(
  67. const std::vector<scoped_refptr<const CTLogVerifier>>& log_verifiers) {
  68. logs_.clear();
  69. for (const auto& log_verifier : log_verifiers) {
  70. std::string key_id = log_verifier->key_id();
  71. logs_[key_id] = log_verifier;
  72. }
  73. }
  74. void MultiLogCTVerifier::Verify(
  75. base::StringPiece hostname,
  76. X509Certificate* cert,
  77. base::StringPiece stapled_ocsp_response,
  78. base::StringPiece sct_list_from_tls_extension,
  79. SignedCertificateTimestampAndStatusList* output_scts,
  80. const NetLogWithSource& net_log) {
  81. DCHECK(cert);
  82. DCHECK(output_scts);
  83. base::TimeTicks start = base::TimeTicks::Now();
  84. output_scts->clear();
  85. std::string embedded_scts;
  86. if (!cert->intermediate_buffers().empty() &&
  87. ct::ExtractEmbeddedSCTList(cert->cert_buffer(), &embedded_scts)) {
  88. ct::SignedEntryData precert_entry;
  89. if (ct::GetPrecertSignedEntry(cert->cert_buffer(),
  90. cert->intermediate_buffers().front().get(),
  91. &precert_entry)) {
  92. VerifySCTs(hostname, embedded_scts, precert_entry,
  93. ct::SignedCertificateTimestamp::SCT_EMBEDDED, cert,
  94. output_scts);
  95. }
  96. }
  97. std::string sct_list_from_ocsp;
  98. if (!stapled_ocsp_response.empty() && !cert->intermediate_buffers().empty()) {
  99. ct::ExtractSCTListFromOCSPResponse(
  100. cert->intermediate_buffers().front().get(), cert->serial_number(),
  101. stapled_ocsp_response, &sct_list_from_ocsp);
  102. }
  103. // Log to Net Log, after extracting SCTs but before possibly failing on
  104. // X.509 entry creation.
  105. net_log.AddEvent(
  106. NetLogEventType::SIGNED_CERTIFICATE_TIMESTAMPS_RECEIVED, [&] {
  107. return NetLogRawSignedCertificateTimestampParams(
  108. embedded_scts, sct_list_from_ocsp, sct_list_from_tls_extension);
  109. });
  110. ct::SignedEntryData x509_entry;
  111. if (ct::GetX509SignedEntry(cert->cert_buffer(), &x509_entry)) {
  112. VerifySCTs(hostname, sct_list_from_ocsp, x509_entry,
  113. ct::SignedCertificateTimestamp::SCT_FROM_OCSP_RESPONSE, cert,
  114. output_scts);
  115. VerifySCTs(hostname, sct_list_from_tls_extension, x509_entry,
  116. ct::SignedCertificateTimestamp::SCT_FROM_TLS_EXTENSION, cert,
  117. output_scts);
  118. }
  119. // Only log the verification time if SCTs were provided.
  120. if (!output_scts->empty()) {
  121. base::TimeDelta verify_time = base::TimeTicks::Now() - start;
  122. UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES(
  123. "Net.CertificateTransparency.SCT.VerificationTime", verify_time,
  124. base::Microseconds(1), base::Milliseconds(100), 50);
  125. }
  126. net_log.AddEvent(NetLogEventType::SIGNED_CERTIFICATE_TIMESTAMPS_CHECKED, [&] {
  127. return NetLogSignedCertificateTimestampParams(output_scts);
  128. });
  129. }
  130. void MultiLogCTVerifier::VerifySCTs(
  131. base::StringPiece hostname,
  132. base::StringPiece encoded_sct_list,
  133. const ct::SignedEntryData& expected_entry,
  134. ct::SignedCertificateTimestamp::Origin origin,
  135. X509Certificate* cert,
  136. SignedCertificateTimestampAndStatusList* output_scts) {
  137. if (logs_.empty())
  138. return;
  139. std::vector<base::StringPiece> sct_list;
  140. if (!ct::DecodeSCTList(encoded_sct_list, &sct_list))
  141. return;
  142. for (std::vector<base::StringPiece>::const_iterator it = sct_list.begin();
  143. it != sct_list.end(); ++it) {
  144. base::StringPiece encoded_sct(*it);
  145. LogSCTOriginToUMA(origin);
  146. scoped_refptr<ct::SignedCertificateTimestamp> decoded_sct;
  147. if (!DecodeSignedCertificateTimestamp(&encoded_sct, &decoded_sct)) {
  148. LogSCTStatusToUMA(ct::SCT_STATUS_NONE);
  149. continue;
  150. }
  151. decoded_sct->origin = origin;
  152. VerifySingleSCT(hostname, decoded_sct, expected_entry, cert, output_scts);
  153. }
  154. }
  155. bool MultiLogCTVerifier::VerifySingleSCT(
  156. base::StringPiece hostname,
  157. scoped_refptr<ct::SignedCertificateTimestamp> sct,
  158. const ct::SignedEntryData& expected_entry,
  159. X509Certificate* cert,
  160. SignedCertificateTimestampAndStatusList* output_scts) {
  161. // Assume this SCT is untrusted until proven otherwise.
  162. const auto& it = logs_.find(sct->log_id);
  163. if (it == logs_.end()) {
  164. AddSCTAndLogStatus(sct, ct::SCT_STATUS_LOG_UNKNOWN, output_scts);
  165. return false;
  166. }
  167. sct->log_description = it->second->description();
  168. if (!it->second->Verify(expected_entry, *sct.get())) {
  169. AddSCTAndLogStatus(sct, ct::SCT_STATUS_INVALID_SIGNATURE, output_scts);
  170. return false;
  171. }
  172. // SCT verified ok, just make sure the timestamp is legitimate.
  173. if (sct->timestamp > base::Time::Now()) {
  174. AddSCTAndLogStatus(sct, ct::SCT_STATUS_INVALID_TIMESTAMP, output_scts);
  175. return false;
  176. }
  177. AddSCTAndLogStatus(sct, ct::SCT_STATUS_OK, output_scts);
  178. return true;
  179. }
  180. } // namespace net