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 #include "quiche/quic/core/crypto/quic_compressed_certs_cache.h"
6
7 #include <string>
8
9 namespace quic {
10
11 namespace {
12
13 // Inline helper function for extending a 64-bit |seed| in-place with a 64-bit
14 // |value|. Based on Boost's hash_combine function.
hash_combine(uint64_t * seed,const uint64_t & val)15 inline void hash_combine(uint64_t* seed, const uint64_t& val) {
16 (*seed) ^= val + 0x9e3779b9 + ((*seed) << 6) + ((*seed) >> 2);
17 }
18
19 } // namespace
20
21 const size_t QuicCompressedCertsCache::kQuicCompressedCertsCacheSize = 225;
22
UncompressedCerts()23 QuicCompressedCertsCache::UncompressedCerts::UncompressedCerts()
24 : chain(nullptr), client_cached_cert_hashes(nullptr) {}
25
UncompressedCerts(const quiche::QuicheReferenceCountedPointer<ProofSource::Chain> & chain,const std::string * client_cached_cert_hashes)26 QuicCompressedCertsCache::UncompressedCerts::UncompressedCerts(
27 const quiche::QuicheReferenceCountedPointer<ProofSource::Chain>& chain,
28 const std::string* client_cached_cert_hashes)
29 : chain(chain), client_cached_cert_hashes(client_cached_cert_hashes) {}
30
~UncompressedCerts()31 QuicCompressedCertsCache::UncompressedCerts::~UncompressedCerts() {}
32
CachedCerts()33 QuicCompressedCertsCache::CachedCerts::CachedCerts() {}
34
CachedCerts(const UncompressedCerts & uncompressed_certs,const std::string & compressed_cert)35 QuicCompressedCertsCache::CachedCerts::CachedCerts(
36 const UncompressedCerts& uncompressed_certs,
37 const std::string& compressed_cert)
38 : chain_(uncompressed_certs.chain),
39 client_cached_cert_hashes_(*uncompressed_certs.client_cached_cert_hashes),
40 compressed_cert_(compressed_cert) {}
41
42 QuicCompressedCertsCache::CachedCerts::CachedCerts(const CachedCerts& other) =
43 default;
44
~CachedCerts()45 QuicCompressedCertsCache::CachedCerts::~CachedCerts() {}
46
MatchesUncompressedCerts(const UncompressedCerts & uncompressed_certs) const47 bool QuicCompressedCertsCache::CachedCerts::MatchesUncompressedCerts(
48 const UncompressedCerts& uncompressed_certs) const {
49 return (client_cached_cert_hashes_ ==
50 *uncompressed_certs.client_cached_cert_hashes &&
51 chain_ == uncompressed_certs.chain);
52 }
53
compressed_cert() const54 const std::string* QuicCompressedCertsCache::CachedCerts::compressed_cert()
55 const {
56 return &compressed_cert_;
57 }
58
QuicCompressedCertsCache(int64_t max_num_certs)59 QuicCompressedCertsCache::QuicCompressedCertsCache(int64_t max_num_certs)
60 : certs_cache_(max_num_certs) {}
61
~QuicCompressedCertsCache()62 QuicCompressedCertsCache::~QuicCompressedCertsCache() {
63 // Underlying cache must be cleared before destruction.
64 certs_cache_.Clear();
65 }
66
GetCompressedCert(const quiche::QuicheReferenceCountedPointer<ProofSource::Chain> & chain,const std::string & client_cached_cert_hashes)67 const std::string* QuicCompressedCertsCache::GetCompressedCert(
68 const quiche::QuicheReferenceCountedPointer<ProofSource::Chain>& chain,
69 const std::string& client_cached_cert_hashes) {
70 UncompressedCerts uncompressed_certs(chain, &client_cached_cert_hashes);
71
72 uint64_t key = ComputeUncompressedCertsHash(uncompressed_certs);
73
74 CachedCerts* cached_value = nullptr;
75 auto iter = certs_cache_.Lookup(key);
76 if (iter != certs_cache_.end()) {
77 cached_value = iter->second.get();
78 }
79 if (cached_value != nullptr &&
80 cached_value->MatchesUncompressedCerts(uncompressed_certs)) {
81 return cached_value->compressed_cert();
82 }
83 return nullptr;
84 }
85
Insert(const quiche::QuicheReferenceCountedPointer<ProofSource::Chain> & chain,const std::string & client_cached_cert_hashes,const std::string & compressed_cert)86 void QuicCompressedCertsCache::Insert(
87 const quiche::QuicheReferenceCountedPointer<ProofSource::Chain>& chain,
88 const std::string& client_cached_cert_hashes,
89 const std::string& compressed_cert) {
90 UncompressedCerts uncompressed_certs(chain, &client_cached_cert_hashes);
91
92 uint64_t key = ComputeUncompressedCertsHash(uncompressed_certs);
93
94 // Insert one unit to the cache.
95 std::unique_ptr<CachedCerts> cached_certs(
96 new CachedCerts(uncompressed_certs, compressed_cert));
97 certs_cache_.Insert(key, std::move(cached_certs));
98 }
99
MaxSize()100 size_t QuicCompressedCertsCache::MaxSize() { return certs_cache_.MaxSize(); }
101
Size()102 size_t QuicCompressedCertsCache::Size() { return certs_cache_.Size(); }
103
ComputeUncompressedCertsHash(const UncompressedCerts & uncompressed_certs)104 uint64_t QuicCompressedCertsCache::ComputeUncompressedCertsHash(
105 const UncompressedCerts& uncompressed_certs) {
106 uint64_t hash =
107 std::hash<std::string>()(*uncompressed_certs.client_cached_cert_hashes);
108
109 hash_combine(&hash,
110 reinterpret_cast<uint64_t>(uncompressed_certs.chain.get()));
111 return hash;
112 }
113
114 } // namespace quic
115