xref: /aosp_15_r20/external/cronet/net/cert/crl_set.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef NET_CERT_CRL_SET_H_
6*6777b538SAndroid Build Coastguard Worker #define NET_CERT_CRL_SET_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <stddef.h>
9*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
10*6777b538SAndroid Build Coastguard Worker 
11*6777b538SAndroid Build Coastguard Worker #include <string>
12*6777b538SAndroid Build Coastguard Worker #include <string_view>
13*6777b538SAndroid Build Coastguard Worker #include <unordered_map>
14*6777b538SAndroid Build Coastguard Worker #include <utility>
15*6777b538SAndroid Build Coastguard Worker #include <vector>
16*6777b538SAndroid Build Coastguard Worker 
17*6777b538SAndroid Build Coastguard Worker #include "base/memory/ref_counted.h"
18*6777b538SAndroid Build Coastguard Worker #include "net/base/hash_value.h"
19*6777b538SAndroid Build Coastguard Worker #include "net/base/net_export.h"
20*6777b538SAndroid Build Coastguard Worker 
21*6777b538SAndroid Build Coastguard Worker namespace net {
22*6777b538SAndroid Build Coastguard Worker 
23*6777b538SAndroid Build Coastguard Worker // A CRLSet is a structure that lists the serial numbers of revoked
24*6777b538SAndroid Build Coastguard Worker // certificates from a number of issuers where issuers are identified by the
25*6777b538SAndroid Build Coastguard Worker // SHA256 of their SubjectPublicKeyInfo.
26*6777b538SAndroid Build Coastguard Worker class NET_EXPORT CRLSet : public base::RefCountedThreadSafe<CRLSet> {
27*6777b538SAndroid Build Coastguard Worker  public:
28*6777b538SAndroid Build Coastguard Worker   enum Result {
29*6777b538SAndroid Build Coastguard Worker     REVOKED,  // the certificate should be rejected.
30*6777b538SAndroid Build Coastguard Worker     UNKNOWN,  // the CRL for the certificate is not included in the set.
31*6777b538SAndroid Build Coastguard Worker     GOOD,     // the certificate is not listed.
32*6777b538SAndroid Build Coastguard Worker   };
33*6777b538SAndroid Build Coastguard Worker 
34*6777b538SAndroid Build Coastguard Worker   // Parses the bytes in |data| and, on success, puts a new CRLSet in
35*6777b538SAndroid Build Coastguard Worker   // |out_crl_set| and returns true.
36*6777b538SAndroid Build Coastguard Worker   static bool Parse(std::string_view data, scoped_refptr<CRLSet>* out_crl_set);
37*6777b538SAndroid Build Coastguard Worker 
38*6777b538SAndroid Build Coastguard Worker   // CheckSPKI checks whether the given SPKI has been listed as blocked.
39*6777b538SAndroid Build Coastguard Worker   //   spki_hash: the SHA256 of the SubjectPublicKeyInfo of the certificate.
40*6777b538SAndroid Build Coastguard Worker   Result CheckSPKI(std::string_view spki_hash) const;
41*6777b538SAndroid Build Coastguard Worker 
42*6777b538SAndroid Build Coastguard Worker   // CheckSerial returns the information contained in the set for a given
43*6777b538SAndroid Build Coastguard Worker   // certificate:
44*6777b538SAndroid Build Coastguard Worker   //   serial_number: the serial number of the certificate, as the DER-encoded
45*6777b538SAndroid Build Coastguard Worker   //       value
46*6777b538SAndroid Build Coastguard Worker   //   issuer_spki_hash: the SHA256 of the SubjectPublicKeyInfo of the CRL
47*6777b538SAndroid Build Coastguard Worker   //       signer
48*6777b538SAndroid Build Coastguard Worker   Result CheckSerial(std::string_view serial_number,
49*6777b538SAndroid Build Coastguard Worker                      std::string_view issuer_spki_hash) const;
50*6777b538SAndroid Build Coastguard Worker 
51*6777b538SAndroid Build Coastguard Worker   // CheckSubject returns the information contained in the set for a given,
52*6777b538SAndroid Build Coastguard Worker   // encoded subject name and SPKI SHA-256 hash. The subject name is encoded as
53*6777b538SAndroid Build Coastguard Worker   // a DER X.501 Name (see https://tools.ietf.org/html/rfc5280#section-4.1.2.4).
54*6777b538SAndroid Build Coastguard Worker   Result CheckSubject(std::string_view asn1_subject,
55*6777b538SAndroid Build Coastguard Worker                       std::string_view spki_hash) const;
56*6777b538SAndroid Build Coastguard Worker 
57*6777b538SAndroid Build Coastguard Worker   // Returns true if |spki_hash|, the SHA256 of the SubjectPublicKeyInfo,
58*6777b538SAndroid Build Coastguard Worker   // is known to be used for interception by a party other than the device
59*6777b538SAndroid Build Coastguard Worker   // or machine owner.
60*6777b538SAndroid Build Coastguard Worker   bool IsKnownInterceptionKey(std::string_view spki_hash) const;
61*6777b538SAndroid Build Coastguard Worker 
62*6777b538SAndroid Build Coastguard Worker   // IsExpired returns true iff the current time is past the NotAfter time
63*6777b538SAndroid Build Coastguard Worker   // specified in the CRLSet.
64*6777b538SAndroid Build Coastguard Worker   bool IsExpired() const;
65*6777b538SAndroid Build Coastguard Worker 
66*6777b538SAndroid Build Coastguard Worker   // sequence returns the sequence number of this CRL set. CRL sets generated
67*6777b538SAndroid Build Coastguard Worker   // by the same source are given strictly monotonically increasing sequence
68*6777b538SAndroid Build Coastguard Worker   // numbers.
69*6777b538SAndroid Build Coastguard Worker   uint32_t sequence() const;
70*6777b538SAndroid Build Coastguard Worker 
71*6777b538SAndroid Build Coastguard Worker   // CRLList contains a map of (issuer SPKI hash, revoked serial numbers)
72*6777b538SAndroid Build Coastguard Worker   // pairs.
73*6777b538SAndroid Build Coastguard Worker   using CRLList = std::unordered_map<std::string, std::vector<std::string>>;
74*6777b538SAndroid Build Coastguard Worker 
75*6777b538SAndroid Build Coastguard Worker   // crls returns the internal state of this CRLSet. It should only be used in
76*6777b538SAndroid Build Coastguard Worker   // testing.
77*6777b538SAndroid Build Coastguard Worker   const CRLList& CrlsForTesting() const;
78*6777b538SAndroid Build Coastguard Worker 
79*6777b538SAndroid Build Coastguard Worker   // BuiltinCRLSet() returns the default CRLSet, to be used when no CRLSet is
80*6777b538SAndroid Build Coastguard Worker   // available from the network.  The default CRLSet includes a statically-
81*6777b538SAndroid Build Coastguard Worker   // configured block list.
82*6777b538SAndroid Build Coastguard Worker   static scoped_refptr<CRLSet> BuiltinCRLSet();
83*6777b538SAndroid Build Coastguard Worker 
84*6777b538SAndroid Build Coastguard Worker   // EmptyCRLSetForTesting returns a valid, but empty, CRLSet for unit tests.
85*6777b538SAndroid Build Coastguard Worker   static scoped_refptr<CRLSet> EmptyCRLSetForTesting();
86*6777b538SAndroid Build Coastguard Worker 
87*6777b538SAndroid Build Coastguard Worker   // ExpiredCRLSetForTesting returns a expired, empty CRLSet for unit tests.
88*6777b538SAndroid Build Coastguard Worker   static scoped_refptr<CRLSet> ExpiredCRLSetForTesting();
89*6777b538SAndroid Build Coastguard Worker 
90*6777b538SAndroid Build Coastguard Worker   // ForTesting returns a CRLSet for testing. If |is_expired| is true, calling
91*6777b538SAndroid Build Coastguard Worker   // IsExpired on the result will return true. If |issuer_spki| is not NULL,
92*6777b538SAndroid Build Coastguard Worker   // the CRLSet will cover certificates issued by that SPKI. If |serial_number|
93*6777b538SAndroid Build Coastguard Worker   // is not empty, then that DER-encoded serial number will be considered to
94*6777b538SAndroid Build Coastguard Worker   // have been revoked by |issuer_spki|. If |utf8_common_name| is not empty
95*6777b538SAndroid Build Coastguard Worker   // then the CRLSet will consider certificates with a subject consisting only
96*6777b538SAndroid Build Coastguard Worker   // of that common name as a UTF8String to be revoked unless they match an
97*6777b538SAndroid Build Coastguard Worker   // SPKI hash from |acceptable_spki_hashes_for_cn|.
98*6777b538SAndroid Build Coastguard Worker   static scoped_refptr<CRLSet> ForTesting(
99*6777b538SAndroid Build Coastguard Worker       bool is_expired,
100*6777b538SAndroid Build Coastguard Worker       const SHA256HashValue* issuer_spki,
101*6777b538SAndroid Build Coastguard Worker       std::string_view serial_number,
102*6777b538SAndroid Build Coastguard Worker       std::string_view utf8_common_name,
103*6777b538SAndroid Build Coastguard Worker       const std::vector<std::string>& acceptable_spki_hashes_for_cn);
104*6777b538SAndroid Build Coastguard Worker 
105*6777b538SAndroid Build Coastguard Worker  private:
106*6777b538SAndroid Build Coastguard Worker   CRLSet();
107*6777b538SAndroid Build Coastguard Worker   ~CRLSet();
108*6777b538SAndroid Build Coastguard Worker 
109*6777b538SAndroid Build Coastguard Worker   friend class base::RefCountedThreadSafe<CRLSet>;
110*6777b538SAndroid Build Coastguard Worker 
111*6777b538SAndroid Build Coastguard Worker   uint32_t sequence_ = 0;
112*6777b538SAndroid Build Coastguard Worker   // not_after_ contains the time, in UNIX epoch seconds, after which the
113*6777b538SAndroid Build Coastguard Worker   // CRLSet should be considered stale, or 0 if no such time was given.
114*6777b538SAndroid Build Coastguard Worker   uint64_t not_after_ = 0;
115*6777b538SAndroid Build Coastguard Worker   // crls_ is a map from the SHA-256 hash of an X.501 subject name to a list
116*6777b538SAndroid Build Coastguard Worker   // of revoked serial numbers.
117*6777b538SAndroid Build Coastguard Worker   CRLList crls_;
118*6777b538SAndroid Build Coastguard Worker   // blocked_spkis_ contains the SHA256 hashes of SPKIs which are to be blocked
119*6777b538SAndroid Build Coastguard Worker   // no matter where in a certificate chain they might appear.
120*6777b538SAndroid Build Coastguard Worker   std::vector<std::string> blocked_spkis_;
121*6777b538SAndroid Build Coastguard Worker   // known_interception_spkis_ contains the SHA256 hashes of SPKIs which are
122*6777b538SAndroid Build Coastguard Worker   // known to be used for interception by a party other than the device or
123*6777b538SAndroid Build Coastguard Worker   // machine owner.
124*6777b538SAndroid Build Coastguard Worker   std::vector<std::string> known_interception_spkis_;
125*6777b538SAndroid Build Coastguard Worker   // limited_subjects_ is a map from the SHA256 hash of an X.501 subject name
126*6777b538SAndroid Build Coastguard Worker   // to a list of allowed SPKI hashes for certificates with that subject name.
127*6777b538SAndroid Build Coastguard Worker   std::unordered_map<std::string, std::vector<std::string>> limited_subjects_;
128*6777b538SAndroid Build Coastguard Worker };
129*6777b538SAndroid Build Coastguard Worker 
130*6777b538SAndroid Build Coastguard Worker }  // namespace net
131*6777b538SAndroid Build Coastguard Worker 
132*6777b538SAndroid Build Coastguard Worker #endif  // NET_CERT_CRL_SET_H_
133