| 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 #include "net/quic/crypto/quic_compressed_certs_cache.h" | |
| 6 | |
| 7 using std::string; | |
| 8 | |
| 9 namespace net { | |
| 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. | |
| 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 QuicCompressedCertsCache::UncompressedCerts::UncompressedCerts() {} | |
| 22 | |
| 23 QuicCompressedCertsCache::UncompressedCerts::UncompressedCerts( | |
| 24 const scoped_refptr<ProofSource::Chain>& chain, | |
| 25 const string* client_common_set_hashes, | |
| 26 const string* client_cached_cert_hashes) | |
| 27 : chain(chain), | |
| 28 client_common_set_hashes(client_common_set_hashes), | |
| 29 client_cached_cert_hashes(client_cached_cert_hashes) {} | |
| 30 | |
| 31 QuicCompressedCertsCache::UncompressedCerts::~UncompressedCerts() {} | |
| 32 | |
| 33 QuicCompressedCertsCache::CachedCerts::CachedCerts() {} | |
| 34 | |
| 35 QuicCompressedCertsCache::CachedCerts::CachedCerts( | |
| 36 const UncompressedCerts& uncompressed_certs, | |
| 37 const string& compressed_cert) | |
| 38 : chain_(uncompressed_certs.chain), | |
| 39 client_common_set_hashes_(*uncompressed_certs.client_common_set_hashes), | |
| 40 client_cached_cert_hashes_(*uncompressed_certs.client_cached_cert_hashes), | |
| 41 compressed_cert_(compressed_cert) {} | |
| 42 | |
| 43 QuicCompressedCertsCache::CachedCerts::CachedCerts(const CachedCerts& other) = | |
| 44 default; | |
| 45 | |
| 46 QuicCompressedCertsCache::CachedCerts::~CachedCerts() {} | |
| 47 | |
| 48 bool QuicCompressedCertsCache::CachedCerts::MatchesUncompressedCerts( | |
| 49 const UncompressedCerts& uncompressed_certs) const { | |
| 50 return (client_common_set_hashes_ == | |
| 51 *uncompressed_certs.client_common_set_hashes && | |
| 52 client_cached_cert_hashes_ == | |
| 53 *uncompressed_certs.client_cached_cert_hashes && | |
| 54 chain_ == uncompressed_certs.chain); | |
| 55 } | |
| 56 | |
| 57 const string* QuicCompressedCertsCache::CachedCerts::compressed_cert() const { | |
| 58 return &compressed_cert_; | |
| 59 } | |
| 60 | |
| 61 QuicCompressedCertsCache::QuicCompressedCertsCache(int64_t max_num_certs) | |
| 62 : certs_cache_(max_num_certs) {} | |
| 63 | |
| 64 QuicCompressedCertsCache::~QuicCompressedCertsCache() { | |
| 65 // Underlying cache must be cleared before destruction. | |
| 66 certs_cache_.Clear(); | |
| 67 } | |
| 68 | |
| 69 const string* QuicCompressedCertsCache::GetCompressedCert( | |
| 70 const scoped_refptr<ProofSource::Chain>& chain, | |
| 71 const string& client_common_set_hashes, | |
| 72 const string& client_cached_cert_hashes) { | |
| 73 UncompressedCerts uncompressed_certs(chain, &client_common_set_hashes, | |
| 74 &client_cached_cert_hashes); | |
| 75 | |
| 76 uint64_t key = ComputeUncompressedCertsHash(uncompressed_certs); | |
| 77 | |
| 78 auto cached_it = certs_cache_.Get(key); | |
| 79 | |
| 80 if (cached_it != certs_cache_.end()) { | |
| 81 const CachedCerts& cached_value = cached_it->second; | |
| 82 if (cached_value.MatchesUncompressedCerts(uncompressed_certs)) { | |
| 83 return cached_value.compressed_cert(); | |
| 84 } | |
| 85 } | |
| 86 return nullptr; | |
| 87 } | |
| 88 | |
| 89 void QuicCompressedCertsCache::Insert( | |
| 90 const scoped_refptr<ProofSource::Chain>& chain, | |
| 91 const string& client_common_set_hashes, | |
| 92 const string& client_cached_cert_hashes, | |
| 93 const string& compressed_cert) { | |
| 94 UncompressedCerts uncompressed_certs(chain, &client_common_set_hashes, | |
| 95 &client_cached_cert_hashes); | |
| 96 | |
| 97 uint64_t key = ComputeUncompressedCertsHash(uncompressed_certs); | |
| 98 | |
| 99 // Insert one unit to the cache. | |
| 100 certs_cache_.Put(key, CachedCerts(uncompressed_certs, compressed_cert)); | |
| 101 } | |
| 102 | |
| 103 size_t QuicCompressedCertsCache::MaxSize() { | |
| 104 return certs_cache_.max_size(); | |
| 105 } | |
| 106 | |
| 107 size_t QuicCompressedCertsCache::Size() { | |
| 108 return certs_cache_.size(); | |
| 109 } | |
| 110 | |
| 111 uint64_t QuicCompressedCertsCache::ComputeUncompressedCertsHash( | |
| 112 const UncompressedCerts& uncompressed_certs) { | |
| 113 uint64_t hash = | |
| 114 std::hash<string>()(*uncompressed_certs.client_common_set_hashes); | |
| 115 uint64_t h = | |
| 116 std::hash<string>()(*uncompressed_certs.client_cached_cert_hashes); | |
| 117 hash_combine(&hash, h); | |
| 118 | |
| 119 hash_combine(&hash, | |
| 120 reinterpret_cast<uint64_t>(uncompressed_certs.chain.get())); | |
| 121 return hash; | |
| 122 } | |
| 123 | |
| 124 } // namespace net | |
| OLD | NEW |