xref: /aosp_15_r20/external/cronet/net/cert/known_roots.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/cert/known_roots.h"
6 
7 #include <string.h>
8 
9 #include <algorithm>
10 
11 #include "base/check_op.h"
12 #include "net/base/hash_value.h"
13 #include "net/cert/root_cert_list_generated.h"
14 
15 namespace net {
16 
17 namespace {
18 
19 // Comparator-predicate that serves as a < function for comparing a
20 // RootCertData to a HashValue
21 struct HashValueToRootCertDataComp {
operator ()net::__anon3bccda4b0111::HashValueToRootCertDataComp22   bool operator()(const HashValue& hash, const RootCertData& root_cert) {
23     DCHECK_EQ(HASH_VALUE_SHA256, hash.tag());
24     return memcmp(hash.data(), root_cert.sha256_spki_hash, 32) < 0;
25   }
26 
operator ()net::__anon3bccda4b0111::HashValueToRootCertDataComp27   bool operator()(const RootCertData& root_cert, const HashValue& hash) {
28     DCHECK_EQ(HASH_VALUE_SHA256, hash.tag());
29     return memcmp(root_cert.sha256_spki_hash, hash.data(), 32) < 0;
30   }
31 };
32 
GetRootCertData(const HashValue & spki_hash)33 const RootCertData* GetRootCertData(const HashValue& spki_hash) {
34   if (spki_hash.tag() != HASH_VALUE_SHA256)
35     return nullptr;
36 
37   auto* it = std::lower_bound(std::begin(kRootCerts), std::end(kRootCerts),
38                               spki_hash, HashValueToRootCertDataComp());
39   if (it == std::end(kRootCerts) ||
40       HashValueToRootCertDataComp()(spki_hash, *it)) {
41     return nullptr;
42   }
43   return it;
44 }
45 
46 }  // namespace
47 
GetNetTrustAnchorHistogramIdForSPKI(const HashValue & spki_hash)48 int32_t GetNetTrustAnchorHistogramIdForSPKI(const HashValue& spki_hash) {
49   const RootCertData* root_data = GetRootCertData(spki_hash);
50   if (!root_data)
51     return 0;
52   return root_data->histogram_id;
53 }
54 
IsLegacyPubliclyTrustedCA(const HashValue & spki_hash)55 bool IsLegacyPubliclyTrustedCA(const HashValue& spki_hash) {
56   const RootCertData* root_data = GetRootCertData(spki_hash);
57   return root_data && root_data->legacy_ca;
58 }
59 
60 }  // namespace net
61