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