blob: 20031874c830f0ba403221835c7d836c12fd0f28 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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
vasilvv872e7a32019-03-12 16:42:44 -07008#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -05009#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 teama6ef0a62019-03-07 20:34:33 -050014
15namespace quic {
16
17// QuicCompressedCertsCache is a cache to track most recently compressed certs.
18class 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().
vasilvvc48c8712019-03-11 13:38:16 -070027 const std::string* GetCompressedCert(
QUICHE teama6ef0a62019-03-07 20:34:33 -050028 const QuicReferenceCountedPointer<ProofSource::Chain>& chain,
vasilvvc48c8712019-03-11 13:38:16 -070029 const std::string& client_common_set_hashes,
30 const std::string& client_cached_cert_hashes);
QUICHE teama6ef0a62019-03-07 20:34:33 -050031
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,
vasilvvc48c8712019-03-11 13:38:16 -070038 const std::string& client_common_set_hashes,
39 const std::string& client_cached_cert_hashes,
40 const std::string& compressed_cert);
QUICHE teama6ef0a62019-03-07 20:34:33 -050041
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,
vasilvvc48c8712019-03-11 13:38:16 -070059 const std::string* client_common_set_hashes,
60 const std::string* client_cached_cert_hashes);
QUICHE teama6ef0a62019-03-07 20:34:33 -050061 ~UncompressedCerts();
62
63 const QuicReferenceCountedPointer<ProofSource::Chain> chain;
vasilvvc48c8712019-03-11 13:38:16 -070064 const std::string* client_common_set_hashes;
65 const std::string* client_cached_cert_hashes;
QUICHE teama6ef0a62019-03-07 20:34:33 -050066 };
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,
vasilvvc48c8712019-03-11 13:38:16 -070075 const std::string& compressed_cert);
QUICHE teama6ef0a62019-03-07 20:34:33 -050076 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
vasilvvc48c8712019-03-11 13:38:16 -070084 const std::string* compressed_cert() const;
QUICHE teama6ef0a62019-03-07 20:34:33 -050085
86 private:
87 // Uncompressed certs data.
88 QuicReferenceCountedPointer<ProofSource::Chain> chain_;
vasilvvc48c8712019-03-11 13:38:16 -070089 const std::string client_common_set_hashes_;
90 const std::string client_cached_cert_hashes_;
QUICHE teama6ef0a62019-03-07 20:34:33 -050091
92 // Cached compressed representation derived from uncompressed certs.
vasilvvc48c8712019-03-11 13:38:16 -070093 const std::string compressed_cert_;
QUICHE teama6ef0a62019-03-07 20:34:33 -050094 };
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_