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 | |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 5 | #include <string> |
| 6 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7 | #include "net/third_party/quiche/src/quic/core/crypto/quic_compressed_certs_cache.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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. |
| 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 | |
| 23 | QuicCompressedCertsCache::UncompressedCerts::UncompressedCerts() |
| 24 | : chain(nullptr), |
| 25 | client_common_set_hashes(nullptr), |
| 26 | client_cached_cert_hashes(nullptr) {} |
| 27 | |
| 28 | QuicCompressedCertsCache::UncompressedCerts::UncompressedCerts( |
| 29 | const QuicReferenceCountedPointer<ProofSource::Chain>& chain, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 30 | const std::string* client_common_set_hashes, |
| 31 | const std::string* client_cached_cert_hashes) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 32 | : chain(chain), |
| 33 | client_common_set_hashes(client_common_set_hashes), |
| 34 | client_cached_cert_hashes(client_cached_cert_hashes) {} |
| 35 | |
| 36 | QuicCompressedCertsCache::UncompressedCerts::~UncompressedCerts() {} |
| 37 | |
| 38 | QuicCompressedCertsCache::CachedCerts::CachedCerts() {} |
| 39 | |
| 40 | QuicCompressedCertsCache::CachedCerts::CachedCerts( |
| 41 | const UncompressedCerts& uncompressed_certs, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 42 | const std::string& compressed_cert) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 43 | : chain_(uncompressed_certs.chain), |
| 44 | client_common_set_hashes_(*uncompressed_certs.client_common_set_hashes), |
| 45 | client_cached_cert_hashes_(*uncompressed_certs.client_cached_cert_hashes), |
| 46 | compressed_cert_(compressed_cert) {} |
| 47 | |
| 48 | QuicCompressedCertsCache::CachedCerts::CachedCerts(const CachedCerts& other) = |
| 49 | default; |
| 50 | |
| 51 | QuicCompressedCertsCache::CachedCerts::~CachedCerts() {} |
| 52 | |
| 53 | bool QuicCompressedCertsCache::CachedCerts::MatchesUncompressedCerts( |
| 54 | const UncompressedCerts& uncompressed_certs) const { |
| 55 | return (client_common_set_hashes_ == |
| 56 | *uncompressed_certs.client_common_set_hashes && |
| 57 | client_cached_cert_hashes_ == |
| 58 | *uncompressed_certs.client_cached_cert_hashes && |
| 59 | chain_ == uncompressed_certs.chain); |
| 60 | } |
| 61 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 62 | const std::string* QuicCompressedCertsCache::CachedCerts::compressed_cert() |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 63 | const { |
| 64 | return &compressed_cert_; |
| 65 | } |
| 66 | |
| 67 | QuicCompressedCertsCache::QuicCompressedCertsCache(int64_t max_num_certs) |
| 68 | : certs_cache_(max_num_certs) {} |
| 69 | |
| 70 | QuicCompressedCertsCache::~QuicCompressedCertsCache() { |
| 71 | // Underlying cache must be cleared before destruction. |
| 72 | certs_cache_.Clear(); |
| 73 | } |
| 74 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 75 | const std::string* QuicCompressedCertsCache::GetCompressedCert( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 76 | const QuicReferenceCountedPointer<ProofSource::Chain>& chain, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 77 | const std::string& client_common_set_hashes, |
| 78 | const std::string& client_cached_cert_hashes) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 79 | UncompressedCerts uncompressed_certs(chain, &client_common_set_hashes, |
| 80 | &client_cached_cert_hashes); |
| 81 | |
| 82 | uint64_t key = ComputeUncompressedCertsHash(uncompressed_certs); |
| 83 | |
| 84 | CachedCerts* cached_value = certs_cache_.Lookup(key); |
| 85 | if (cached_value != nullptr && |
| 86 | cached_value->MatchesUncompressedCerts(uncompressed_certs)) { |
| 87 | return cached_value->compressed_cert(); |
| 88 | } |
| 89 | return nullptr; |
| 90 | } |
| 91 | |
| 92 | void QuicCompressedCertsCache::Insert( |
| 93 | const QuicReferenceCountedPointer<ProofSource::Chain>& chain, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 94 | const std::string& client_common_set_hashes, |
| 95 | const std::string& client_cached_cert_hashes, |
| 96 | const std::string& compressed_cert) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 97 | UncompressedCerts uncompressed_certs(chain, &client_common_set_hashes, |
| 98 | &client_cached_cert_hashes); |
| 99 | |
| 100 | uint64_t key = ComputeUncompressedCertsHash(uncompressed_certs); |
| 101 | |
| 102 | // Insert one unit to the cache. |
| 103 | std::unique_ptr<CachedCerts> cached_certs( |
| 104 | new CachedCerts(uncompressed_certs, compressed_cert)); |
| 105 | certs_cache_.Insert(key, std::move(cached_certs)); |
| 106 | } |
| 107 | |
| 108 | size_t QuicCompressedCertsCache::MaxSize() { |
| 109 | return certs_cache_.MaxSize(); |
| 110 | } |
| 111 | |
| 112 | size_t QuicCompressedCertsCache::Size() { |
| 113 | return certs_cache_.Size(); |
| 114 | } |
| 115 | |
| 116 | uint64_t QuicCompressedCertsCache::ComputeUncompressedCertsHash( |
| 117 | const UncompressedCerts& uncompressed_certs) { |
| 118 | uint64_t hash = |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 119 | std::hash<std::string>()(*uncompressed_certs.client_common_set_hashes); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 120 | uint64_t h = |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 121 | std::hash<std::string>()(*uncompressed_certs.client_cached_cert_hashes); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 122 | hash_combine(&hash, h); |
| 123 | |
| 124 | hash_combine(&hash, |
| 125 | reinterpret_cast<uint64_t>(uncompressed_certs.chain.get())); |
| 126 | return hash; |
| 127 | } |
| 128 | |
| 129 | } // namespace quic |