known_roots.cc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2017 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/known_roots.h"
  5. #include <string.h>
  6. #include <algorithm>
  7. #include "base/check_op.h"
  8. #include "net/base/hash_value.h"
  9. #include "net/cert/root_cert_list_generated.h"
  10. namespace net {
  11. namespace {
  12. // Comparator-predicate that serves as a < function for comparing a
  13. // RootCertData to a HashValue
  14. struct HashValueToRootCertDataComp {
  15. bool operator()(const HashValue& hash, const RootCertData& root_cert) {
  16. DCHECK_EQ(HASH_VALUE_SHA256, hash.tag());
  17. return memcmp(hash.data(), root_cert.sha256_spki_hash, 32) < 0;
  18. }
  19. bool operator()(const RootCertData& root_cert, const HashValue& hash) {
  20. DCHECK_EQ(HASH_VALUE_SHA256, hash.tag());
  21. return memcmp(root_cert.sha256_spki_hash, hash.data(), 32) < 0;
  22. }
  23. };
  24. const RootCertData* GetRootCertData(const HashValue& spki_hash) {
  25. if (spki_hash.tag() != HASH_VALUE_SHA256)
  26. return nullptr;
  27. auto* it = std::lower_bound(std::begin(kRootCerts), std::end(kRootCerts),
  28. spki_hash, HashValueToRootCertDataComp());
  29. if (it == std::end(kRootCerts) ||
  30. HashValueToRootCertDataComp()(spki_hash, *it)) {
  31. return nullptr;
  32. }
  33. return it;
  34. }
  35. } // namespace
  36. int32_t GetNetTrustAnchorHistogramIdForSPKI(const HashValue& spki_hash) {
  37. const RootCertData* root_data = GetRootCertData(spki_hash);
  38. if (!root_data)
  39. return 0;
  40. return root_data->histogram_id;
  41. }
  42. bool IsLegacyPubliclyTrustedCA(const HashValue& spki_hash) {
  43. const RootCertData* root_data = GetRootCertData(spki_hash);
  44. return root_data && root_data->legacy_ca;
  45. }
  46. } // namespace net