QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 QUICHE_QUIC_CORE_CRYPTO_QUIC_COMPRESSED_CERTS_CACHE_H_ |
| 6 | #define QUICHE_QUIC_CORE_CRYPTO_QUIC_COMPRESSED_CERTS_CACHE_H_ |
| 7 | |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 8 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
| 11 | #include "net/third_party/quiche/src/quic/core/crypto/proof_source.h" |
| 12 | #include "net/third_party/quiche/src/quic/core/quic_lru_cache.h" |
| 13 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 14 | |
| 15 | namespace quic { |
| 16 | |
| 17 | // QuicCompressedCertsCache is a cache to track most recently compressed certs. |
| 18 | class QUIC_EXPORT_PRIVATE QuicCompressedCertsCache { |
| 19 | public: |
| 20 | explicit QuicCompressedCertsCache(int64_t max_num_certs); |
| 21 | ~QuicCompressedCertsCache(); |
| 22 | |
| 23 | // Returns the pointer to the cached compressed cert if |
| 24 | // |chain, client_common_set_hashes, client_cached_cert_hashes| hits cache. |
| 25 | // Otherwise, return nullptr. |
| 26 | // Returned pointer might become invalid on the next call to Insert(). |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 27 | const std::string* GetCompressedCert( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 28 | const QuicReferenceCountedPointer<ProofSource::Chain>& chain, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 29 | const std::string& client_common_set_hashes, |
| 30 | const std::string& client_cached_cert_hashes); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 31 | |
| 32 | // Inserts the specified |
| 33 | // |chain, client_common_set_hashes, |
| 34 | // client_cached_cert_hashes, compressed_cert| tuple to the cache. |
| 35 | // If the insertion causes the cache to become overfull, entries will |
| 36 | // be deleted in an LRU order to make room. |
| 37 | void Insert(const QuicReferenceCountedPointer<ProofSource::Chain>& chain, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 38 | const std::string& client_common_set_hashes, |
| 39 | const std::string& client_cached_cert_hashes, |
| 40 | const std::string& compressed_cert); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 41 | |
| 42 | // Returns max number of cache entries the cache can carry. |
| 43 | size_t MaxSize(); |
| 44 | |
| 45 | // Returns current number of cache entries in the cache. |
| 46 | size_t Size(); |
| 47 | |
| 48 | // Default size of the QuicCompressedCertsCache per server side investigation. |
| 49 | static const size_t kQuicCompressedCertsCacheSize; |
| 50 | |
| 51 | private: |
| 52 | // A wrapper of the tuple: |
| 53 | // |chain, client_common_set_hashes, client_cached_cert_hashes| |
| 54 | // to identify uncompressed representation of certs. |
| 55 | struct UncompressedCerts { |
| 56 | UncompressedCerts(); |
| 57 | UncompressedCerts( |
| 58 | const QuicReferenceCountedPointer<ProofSource::Chain>& chain, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 59 | const std::string* client_common_set_hashes, |
| 60 | const std::string* client_cached_cert_hashes); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 61 | ~UncompressedCerts(); |
| 62 | |
| 63 | const QuicReferenceCountedPointer<ProofSource::Chain> chain; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 64 | const std::string* client_common_set_hashes; |
| 65 | const std::string* client_cached_cert_hashes; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | // Certs stored by QuicCompressedCertsCache where uncompressed certs data is |
| 69 | // used to identify the uncompressed representation of certs and |
| 70 | // |compressed_cert| is the cached compressed representation. |
| 71 | class CachedCerts { |
| 72 | public: |
| 73 | CachedCerts(); |
| 74 | CachedCerts(const UncompressedCerts& uncompressed_certs, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 75 | const std::string& compressed_cert); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 76 | CachedCerts(const CachedCerts& other); |
| 77 | ~CachedCerts(); |
| 78 | |
| 79 | // Returns true if the |uncompressed_certs| matches uncompressed |
| 80 | // representation of this cert. |
| 81 | bool MatchesUncompressedCerts( |
| 82 | const UncompressedCerts& uncompressed_certs) const; |
| 83 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 84 | const std::string* compressed_cert() const; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 85 | |
| 86 | private: |
| 87 | // Uncompressed certs data. |
| 88 | QuicReferenceCountedPointer<ProofSource::Chain> chain_; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 89 | const std::string client_common_set_hashes_; |
| 90 | const std::string client_cached_cert_hashes_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 91 | |
| 92 | // Cached compressed representation derived from uncompressed certs. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 93 | const std::string compressed_cert_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | // Computes a uint64_t hash for |uncompressed_certs|. |
| 97 | uint64_t ComputeUncompressedCertsHash( |
| 98 | const UncompressedCerts& uncompressed_certs); |
| 99 | |
| 100 | // Key is a unit64_t hash for UncompressedCerts. Stored associated value is |
| 101 | // CachedCerts which has both original uncompressed certs data and the |
| 102 | // compressed representation of the certs. |
| 103 | QuicLRUCache<uint64_t, CachedCerts> certs_cache_; |
| 104 | }; |
| 105 | |
| 106 | } // namespace quic |
| 107 | |
| 108 | #endif // QUICHE_QUIC_CORE_CRYPTO_QUIC_COMPRESSED_CERTS_CACHE_H_ |