xref: /aosp_15_r20/external/cronet/net/cert/internal/revocation_checker.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2017 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_INTERNAL_REVOCATION_CHECKER_H_
6*6777b538SAndroid Build Coastguard Worker #define NET_CERT_INTERNAL_REVOCATION_CHECKER_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <string_view>
9*6777b538SAndroid Build Coastguard Worker 
10*6777b538SAndroid Build Coastguard Worker #include "base/time/time.h"
11*6777b538SAndroid Build Coastguard Worker #include "net/base/net_export.h"
12*6777b538SAndroid Build Coastguard Worker #include "net/cert/crl_set.h"
13*6777b538SAndroid Build Coastguard Worker #include "third_party/boringssl/src/pki/cert_errors.h"
14*6777b538SAndroid Build Coastguard Worker #include "third_party/boringssl/src/pki/ocsp.h"
15*6777b538SAndroid Build Coastguard Worker #include "third_party/boringssl/src/pki/parsed_certificate.h"
16*6777b538SAndroid Build Coastguard Worker 
17*6777b538SAndroid Build Coastguard Worker namespace net {
18*6777b538SAndroid Build Coastguard Worker 
19*6777b538SAndroid Build Coastguard Worker class CertNetFetcher;
20*6777b538SAndroid Build Coastguard Worker 
21*6777b538SAndroid Build Coastguard Worker // Baseline Requirements 1.6.5, section 4.9.7:
22*6777b538SAndroid Build Coastguard Worker //     For the status of Subscriber Certificates: If the CA publishes a CRL,
23*6777b538SAndroid Build Coastguard Worker //     then the CA SHALL update and reissue CRLs at least once every seven
24*6777b538SAndroid Build Coastguard Worker //     days, and the value of the nextUpdate field MUST NOT be more than ten
25*6777b538SAndroid Build Coastguard Worker //     days beyond the value of the thisUpdate field.
26*6777b538SAndroid Build Coastguard Worker //
27*6777b538SAndroid Build Coastguard Worker // Baseline Requirements 1.6.5, section 4.9.10:
28*6777b538SAndroid Build Coastguard Worker //     For the status of Subscriber Certificates: The CA SHALL update
29*6777b538SAndroid Build Coastguard Worker //     information provided via an Online Certificate Status Protocol at least
30*6777b538SAndroid Build Coastguard Worker //     every four days.  OCSP responses from this service MUST have a maximum
31*6777b538SAndroid Build Coastguard Worker //     expiration time of ten days.
32*6777b538SAndroid Build Coastguard Worker //
33*6777b538SAndroid Build Coastguard Worker // Use 7 days as the max allowable leaf revocation status age, which is
34*6777b538SAndroid Build Coastguard Worker // sufficient for both CRL and OCSP, and which aligns with Microsoft policies.
35*6777b538SAndroid Build Coastguard Worker constexpr base::TimeDelta kMaxRevocationLeafUpdateAge = base::Days(7);
36*6777b538SAndroid Build Coastguard Worker 
37*6777b538SAndroid Build Coastguard Worker // Baseline Requirements 1.6.5, section 4.9.7:
38*6777b538SAndroid Build Coastguard Worker //     For the status of Subordinate CA Certificates: The CA SHALL update and
39*6777b538SAndroid Build Coastguard Worker //     reissue CRLs at least (i) once every twelve months and (ii) within 24
40*6777b538SAndroid Build Coastguard Worker //     hours after revoking a Subordinate CA Certificate, and the value of the
41*6777b538SAndroid Build Coastguard Worker //     nextUpdate field MUST NOT be more than twelve months beyond the value of
42*6777b538SAndroid Build Coastguard Worker //     the thisUpdate field.
43*6777b538SAndroid Build Coastguard Worker //
44*6777b538SAndroid Build Coastguard Worker // Baseline Requirements 1.6.5, section 4.9.10:
45*6777b538SAndroid Build Coastguard Worker //     For the status of Subordinate CA Certificates: The CA SHALL update
46*6777b538SAndroid Build Coastguard Worker //     information provided via an Online Certificate Status Protocol at least
47*6777b538SAndroid Build Coastguard Worker //     (i) every twelve months and (ii) within 24 hours after revoking a
48*6777b538SAndroid Build Coastguard Worker //     Subordinate CA Certificate.
49*6777b538SAndroid Build Coastguard Worker //
50*6777b538SAndroid Build Coastguard Worker // Use 366 days to allow for leap years, though it is overly permissive in
51*6777b538SAndroid Build Coastguard Worker // other years.
52*6777b538SAndroid Build Coastguard Worker constexpr base::TimeDelta kMaxRevocationIntermediateUpdateAge = base::Days(366);
53*6777b538SAndroid Build Coastguard Worker 
54*6777b538SAndroid Build Coastguard Worker // RevocationPolicy describes how revocation should be carried out for a
55*6777b538SAndroid Build Coastguard Worker // particular chain.
56*6777b538SAndroid Build Coastguard Worker // Callers should not rely on the default-initialized value, but should fully
57*6777b538SAndroid Build Coastguard Worker // specify all the parameters. The default values specify a strict revocation
58*6777b538SAndroid Build Coastguard Worker // checking mode, in case users fail to fully set the parameters.
59*6777b538SAndroid Build Coastguard Worker struct NET_EXPORT_PRIVATE RevocationPolicy {
60*6777b538SAndroid Build Coastguard Worker   // If |check_revocation| is true, then revocation checking is mandatory. This
61*6777b538SAndroid Build Coastguard Worker   // means that every certificate in the chain (excluding trust anchors) must
62*6777b538SAndroid Build Coastguard Worker   // have valid (unexpired) revocation information proving it to be unrevoked.
63*6777b538SAndroid Build Coastguard Worker   //
64*6777b538SAndroid Build Coastguard Worker   // The mechanisms used for checking revocation may include stapled OCSP,
65*6777b538SAndroid Build Coastguard Worker   // cached OCSP, online OCSP, cached CRL, online CRL.
66*6777b538SAndroid Build Coastguard Worker   //
67*6777b538SAndroid Build Coastguard Worker   // The other properties of RevocationPolicy place further constraints on how
68*6777b538SAndroid Build Coastguard Worker   // revocation checking may proceed.
69*6777b538SAndroid Build Coastguard Worker   bool check_revocation = true;
70*6777b538SAndroid Build Coastguard Worker 
71*6777b538SAndroid Build Coastguard Worker   // If |networking_allowed| is true then revocation checking is allowed to
72*6777b538SAndroid Build Coastguard Worker   // issue network requests in order to fetch fresh OCSP/CRL. Otherwise
73*6777b538SAndroid Build Coastguard Worker   // networking is not permitted in the course of revocation checking.
74*6777b538SAndroid Build Coastguard Worker   bool networking_allowed = false;
75*6777b538SAndroid Build Coastguard Worker 
76*6777b538SAndroid Build Coastguard Worker   // If |crl_allowed| is true then CRLs will be checked as a fallback when an
77*6777b538SAndroid Build Coastguard Worker   // OCSP URL is not present or OCSP results are indeterminate.
78*6777b538SAndroid Build Coastguard Worker   bool crl_allowed = true;
79*6777b538SAndroid Build Coastguard Worker 
80*6777b538SAndroid Build Coastguard Worker   // If set to true, considers certificates lacking URLs for OCSP/CRL to be
81*6777b538SAndroid Build Coastguard Worker   // unrevoked. Otherwise will fail for certificates lacking revocation
82*6777b538SAndroid Build Coastguard Worker   // mechanisms.
83*6777b538SAndroid Build Coastguard Worker   bool allow_missing_info = false;
84*6777b538SAndroid Build Coastguard Worker 
85*6777b538SAndroid Build Coastguard Worker   // If set to true, other failure to perform revocation checks (e.g. due to a
86*6777b538SAndroid Build Coastguard Worker   // network level failure, OCSP response error status, failure parsing or
87*6777b538SAndroid Build Coastguard Worker   // evaluating the OCSP/CRL response, etc) is considered equivalent to a
88*6777b538SAndroid Build Coastguard Worker   // successful revocation check.
89*6777b538SAndroid Build Coastguard Worker   bool allow_unable_to_check = false;
90*6777b538SAndroid Build Coastguard Worker 
91*6777b538SAndroid Build Coastguard Worker   // If set to true, enforce requirements specified in the Baseline
92*6777b538SAndroid Build Coastguard Worker   // Requirements such as maximum age of revocation responses.
93*6777b538SAndroid Build Coastguard Worker   bool enforce_baseline_requirements = true;
94*6777b538SAndroid Build Coastguard Worker };
95*6777b538SAndroid Build Coastguard Worker 
96*6777b538SAndroid Build Coastguard Worker // Checks the revocation status of |certs| according to |policy|, and adds
97*6777b538SAndroid Build Coastguard Worker // any failures to |errors|. On failure errors are added to |errors|. On success
98*6777b538SAndroid Build Coastguard Worker // no errors are added.
99*6777b538SAndroid Build Coastguard Worker //
100*6777b538SAndroid Build Coastguard Worker // |deadline|, if not null, will limit the overall amount of time spent doing
101*6777b538SAndroid Build Coastguard Worker // online revocation checks. If |base::TimeTicks::Now()| exceeds |deadline|, no
102*6777b538SAndroid Build Coastguard Worker // more revocation checks will be attempted. Note that this is not a hard
103*6777b538SAndroid Build Coastguard Worker // limit, the deadline may be exceeded by the individual request timetout of a
104*6777b538SAndroid Build Coastguard Worker // single CertNetFetcher.
105*6777b538SAndroid Build Coastguard Worker //
106*6777b538SAndroid Build Coastguard Worker // |certs| must be a successfully validated chain according to RFC 5280 section
107*6777b538SAndroid Build Coastguard Worker // 6.1, in order from leaf to trust anchor.
108*6777b538SAndroid Build Coastguard Worker //
109*6777b538SAndroid Build Coastguard Worker // |net_fetcher| may be null, however this may lead to failed revocation checks
110*6777b538SAndroid Build Coastguard Worker // depending on |policy|.
111*6777b538SAndroid Build Coastguard Worker //
112*6777b538SAndroid Build Coastguard Worker // |stapled_ocsp_verify_result|, if non-null, will be filled with the result of
113*6777b538SAndroid Build Coastguard Worker // checking the leaf certificate against |stapled_leaf_ocsp_response|.
114*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE void CheckValidatedChainRevocation(
115*6777b538SAndroid Build Coastguard Worker     const bssl::ParsedCertificateList& certs,
116*6777b538SAndroid Build Coastguard Worker     const RevocationPolicy& policy,
117*6777b538SAndroid Build Coastguard Worker     base::TimeTicks deadline,
118*6777b538SAndroid Build Coastguard Worker     std::string_view stapled_leaf_ocsp_response,
119*6777b538SAndroid Build Coastguard Worker     CertNetFetcher* net_fetcher,
120*6777b538SAndroid Build Coastguard Worker     bssl::CertPathErrors* errors,
121*6777b538SAndroid Build Coastguard Worker     bssl::OCSPVerifyResult* stapled_ocsp_verify_result);
122*6777b538SAndroid Build Coastguard Worker 
123*6777b538SAndroid Build Coastguard Worker // Checks the revocation status of a certificate chain using the CRLSet and adds
124*6777b538SAndroid Build Coastguard Worker // revocation errors to |errors|.
125*6777b538SAndroid Build Coastguard Worker //
126*6777b538SAndroid Build Coastguard Worker // Returns the revocation status of the leaf certificate:
127*6777b538SAndroid Build Coastguard Worker //
128*6777b538SAndroid Build Coastguard Worker // * CRLSet::REVOKED if any certificate in the chain is revoked. Also adds a
129*6777b538SAndroid Build Coastguard Worker //   corresponding error for the certificate in |errors|.
130*6777b538SAndroid Build Coastguard Worker //
131*6777b538SAndroid Build Coastguard Worker // * CRLSet::GOOD if the leaf certificate is covered as GOOD by the CRLSet, and
132*6777b538SAndroid Build Coastguard Worker //   none of the intermediates were revoked according to the CRLSet.
133*6777b538SAndroid Build Coastguard Worker //
134*6777b538SAndroid Build Coastguard Worker // * CRLSet::UNKNOWN if none of the certificates are known to be revoked, and
135*6777b538SAndroid Build Coastguard Worker //   the revocation status of leaf certificate was UNKNOWN by the CRLSet.
136*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE CRLSet::Result CheckChainRevocationUsingCRLSet(
137*6777b538SAndroid Build Coastguard Worker     const CRLSet* crl_set,
138*6777b538SAndroid Build Coastguard Worker     const bssl::ParsedCertificateList& certs,
139*6777b538SAndroid Build Coastguard Worker     bssl::CertPathErrors* errors);
140*6777b538SAndroid Build Coastguard Worker 
141*6777b538SAndroid Build Coastguard Worker }  // namespace net
142*6777b538SAndroid Build Coastguard Worker 
143*6777b538SAndroid Build Coastguard Worker #endif  // NET_CERT_INTERNAL_REVOCATION_CHECKER_H_
144