xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/crypto/quic_client_session_cache.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2021 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_CLIENT_SESSION_CACHE_H_
6 #define QUICHE_QUIC_CORE_CRYPTO_QUIC_CLIENT_SESSION_CACHE_H_
7 
8 #include <memory>
9 
10 #include "quiche/quic/core/crypto/quic_crypto_client_config.h"
11 #include "quiche/quic/core/quic_lru_cache.h"
12 #include "quiche/quic/core/quic_server_id.h"
13 
14 namespace quic {
15 
16 namespace test {
17 class QuicClientSessionCachePeer;
18 }  // namespace test
19 
20 // QuicClientSessionCache maps from QuicServerId to information used to resume
21 // TLS sessions for that server.
22 class QUICHE_EXPORT QuicClientSessionCache : public SessionCache {
23  public:
24   QuicClientSessionCache();
25   explicit QuicClientSessionCache(size_t max_entries);
26   ~QuicClientSessionCache() override;
27 
28   void Insert(const QuicServerId& server_id,
29               bssl::UniquePtr<SSL_SESSION> session,
30               const TransportParameters& params,
31               const ApplicationState* application_state) override;
32 
33   std::unique_ptr<QuicResumptionState> Lookup(const QuicServerId& server_id,
34                                               QuicWallTime now,
35                                               const SSL_CTX* ctx) override;
36 
37   void ClearEarlyData(const QuicServerId& server_id) override;
38 
39   void OnNewTokenReceived(const QuicServerId& server_id,
40                           absl::string_view token) override;
41 
42   void RemoveExpiredEntries(QuicWallTime now) override;
43 
44   void Clear() override;
45 
size()46   size_t size() const { return cache_.Size(); }
47 
48  private:
49   friend class test::QuicClientSessionCachePeer;
50 
51   struct QUICHE_EXPORT Entry {
52     Entry();
53     Entry(Entry&&);
54     ~Entry();
55 
56     // Adds a new |session| onto sessions, dropping the oldest one if two are
57     // already stored.
58     void PushSession(bssl::UniquePtr<SSL_SESSION> session);
59 
60     // Retrieves the latest session from the entry, meanwhile removing it.
61     bssl::UniquePtr<SSL_SESSION> PopSession();
62 
63     SSL_SESSION* PeekSession();
64 
65     bssl::UniquePtr<SSL_SESSION> sessions[2];
66     std::unique_ptr<TransportParameters> params;
67     std::unique_ptr<ApplicationState> application_state;
68     std::string token;  // An opaque string received in NEW_TOKEN frame.
69   };
70 
71   // Creates a new entry and insert into |cache_|.
72   void CreateAndInsertEntry(const QuicServerId& server_id,
73                             bssl::UniquePtr<SSL_SESSION> session,
74                             const TransportParameters& params,
75                             const ApplicationState* application_state);
76 
77   QuicLRUCache<QuicServerId, Entry, QuicServerIdHash> cache_;
78 };
79 
80 }  // namespace quic
81 
82 #endif  // QUICHE_QUIC_CORE_CRYPTO_QUIC_CLIENT_SESSION_CACHE_H_
83