OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 5 #ifndef NET_CERT_CACHING_CERT_VERIFIER_H_ |
| 6 #define NET_CERT_CACHING_CERT_VERIFIER_H_ |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "net/base/expiring_cache.h" |
| 11 #include "net/base/net_export.h" |
| 12 #include "net/cert/cert_database.h" |
| 13 #include "net/cert/cert_verifier.h" |
| 14 #include "net/cert/cert_verify_result.h" |
| 15 |
| 16 namespace net { |
| 17 |
| 18 class CertTrustAnchorProvider; |
| 19 |
| 20 // CertVerifier that caches the results of certificate verifications. |
| 21 // |
| 22 // In general, certificate verification results will vary on only three |
| 23 // parameters: |
| 24 // - The time of validation (as certificates are only valid for a period of |
| 25 // time) |
| 26 // - The revocation status (a certificate may be revoked at any time, but |
| 27 // revocation statuses themselves have validity period, so a 'good' result |
| 28 // may be reused for a period of time) |
| 29 // - The trust settings (a user may change trust settings at any time) |
| 30 // |
| 31 // This class tries to optimize by allowing certificate verification results |
| 32 // to be cached for a limited amount of time (presently, 30 minutes), which |
| 33 // tries to balance the implementation complexity of needing to monitor the |
| 34 // above for meaningful changes and the practical utility of being able to |
| 35 // cache results when they're not expected to change. |
| 36 class NET_EXPORT CachingCertVerifier : public CertVerifier, |
| 37 public CertDatabase::Observer { |
| 38 public: |
| 39 // Creates a CachingCertVerifier that will use |verifier| to perform the |
| 40 // actual verifications if they're not already cached or if the cached |
| 41 // item has expired. |
| 42 explicit CachingCertVerifier(std::unique_ptr<CertVerifier> verifier); |
| 43 |
| 44 ~CachingCertVerifier() override; |
| 45 |
| 46 // Configures a source of additional certificates that should be treated as |
| 47 // trust anchors during verification, provided that the underlying |
| 48 // CertVerifyProc supports additional trust beyond the default implementation. |
| 49 // It must outlive the CachingCertVerifier. |
| 50 void SetCertTrustAnchorProvider( |
| 51 CertTrustAnchorProvider* trust_anchor_provider); |
| 52 |
| 53 // CertVerifier implementation: |
| 54 int Verify(const RequestParams& params, |
| 55 CRLSet* crl_set, |
| 56 CertVerifyResult* verify_result, |
| 57 const CompletionCallback& callback, |
| 58 std::unique_ptr<Request>* out_req, |
| 59 const BoundNetLog& net_log) override; |
| 60 bool SupportsOCSPStapling() override; |
| 61 |
| 62 private: |
| 63 FRIEND_TEST_ALL_PREFIXES(CachingCertVerifierTest, CacheHit); |
| 64 FRIEND_TEST_ALL_PREFIXES(CachingCertVerifierTest, DifferentCACerts); |
| 65 FRIEND_TEST_ALL_PREFIXES(CachingCertVerifierTest, CertTrustAnchorProvider); |
| 66 |
| 67 // CachedResult contains the result of a certificate verification. |
| 68 struct NET_EXPORT_PRIVATE CachedResult { |
| 69 CachedResult(); |
| 70 ~CachedResult(); |
| 71 |
| 72 int error; // The return value of CertVerifier::Verify. |
| 73 CertVerifyResult result; // The output of CertVerifier::Verify. |
| 74 }; |
| 75 |
| 76 // Rather than having a single validity point along a monotonically increasing |
| 77 // timeline, certificate verification is based on falling within a range of |
| 78 // the certificate's NotBefore and NotAfter and based on what the current |
| 79 // system clock says (which may advance forwards or backwards as users correct |
| 80 // clock skew). CacheValidityPeriod and CacheExpirationFunctor are helpers to |
| 81 // ensure that expiration is measured both by the 'general' case (now + cache |
| 82 // TTL) and by whether or not significant enough clock skew was introduced |
| 83 // since the last verification. |
| 84 struct CacheValidityPeriod { |
| 85 explicit CacheValidityPeriod(base::Time now); |
| 86 CacheValidityPeriod(base::Time now, base::Time expiration); |
| 87 |
| 88 base::Time verification_time; |
| 89 base::Time expiration_time; |
| 90 }; |
| 91 |
| 92 struct CacheExpirationFunctor { |
| 93 // Returns true iff |now| is within the validity period of |expiration|. |
| 94 bool operator()(const CacheValidityPeriod& now, |
| 95 const CacheValidityPeriod& expiration) const; |
| 96 }; |
| 97 |
| 98 using CertVerificationCache = ExpiringCache<RequestParams, |
| 99 CachedResult, |
| 100 CacheValidityPeriod, |
| 101 CacheExpirationFunctor>; |
| 102 |
| 103 // Handles completion of the request matching |params|, which started at |
| 104 // |start_time|, completing. |verify_result| and |result| are added to the |
| 105 // cache, and then |callback| (the original caller's callback) is invoked. |
| 106 void OnRequestFinished(const RequestParams& params, |
| 107 base::Time start_time, |
| 108 const CompletionCallback& callback, |
| 109 CertVerifyResult* verify_result, |
| 110 int error); |
| 111 |
| 112 // Adds |verify_result| and |error| to the cache for |params|, whose |
| 113 // verification attempt began at |start_time|. See the implementation |
| 114 // for more details about the necessity of |start_time|. |
| 115 void AddResultToCache(const RequestParams& params, |
| 116 base::Time start_time, |
| 117 const CertVerifyResult& verify_result, |
| 118 int error); |
| 119 |
| 120 // CertDatabase::Observer methods: |
| 121 void OnCACertChanged(const X509Certificate* cert) override; |
| 122 |
| 123 // For unit testing. |
| 124 void ClearCache(); |
| 125 size_t GetCacheSize() const; |
| 126 uint64_t cache_hits() const { return cache_hits_; } |
| 127 uint64_t requests() const { return requests_; } |
| 128 |
| 129 std::unique_ptr<CertVerifier> verifier_; |
| 130 |
| 131 CertTrustAnchorProvider* trust_anchor_provider_; |
| 132 |
| 133 CertVerificationCache cache_; |
| 134 |
| 135 uint64_t requests_; |
| 136 uint64_t cache_hits_; |
| 137 |
| 138 DISALLOW_COPY_AND_ASSIGN(CachingCertVerifier); |
| 139 }; |
| 140 |
| 141 } // namespace net |
| 142 |
| 143 #endif // NET_CERT_CACHING_CERT_VERIFIER_H_ |
OLD | NEW |