blob: b801ec7b0b1dd8f5eeb9d782cdcabfd3b872b607 [file] [log] [blame]
Benjamin Wright19aab2e2018-04-05 15:39:06 -07001/*
2 * Copyright 2018 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_OPENSSL_SESSION_CACHE_H_
12#define RTC_BASE_OPENSSL_SESSION_CACHE_H_
Benjamin Wright19aab2e2018-04-05 15:39:06 -070013
14#include <openssl/ossl_typ.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Benjamin Wright19aab2e2018-04-05 15:39:06 -070016#include <map>
17#include <string>
18
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/ssl_stream_adapter.h"
Benjamin Wright19aab2e2018-04-05 15:39:06 -070020
Jiawei Ou3b8d48b2018-05-29 16:00:09 -070021#ifndef OPENSSL_IS_BORINGSSL
22typedef struct ssl_session_st SSL_SESSION;
23#endif
24
Benjamin Wright19aab2e2018-04-05 15:39:06 -070025namespace rtc {
26
27// The OpenSSLSessionCache maps hostnames to SSL_SESSIONS. This cache is
28// owned by the OpenSSLAdapterFactory and is passed down to each OpenSSLAdapter
29// created with the factory.
30class OpenSSLSessionCache final {
31 public:
32 // Creates a new OpenSSLSessionCache using the provided the SSL_CTX and
33 // the ssl_mode. The SSL_CTX will be up_refed. ssl_ctx cannot be nullptr,
34 // the constructor immediately dchecks this.
35 OpenSSLSessionCache(SSLMode ssl_mode, SSL_CTX* ssl_ctx);
36 // Frees the cached SSL_SESSIONS and then frees the SSL_CTX.
37 ~OpenSSLSessionCache();
Byoungchan Lee14af7622022-01-12 05:24:58 +090038
39 OpenSSLSessionCache(const OpenSSLSessionCache&) = delete;
40 OpenSSLSessionCache& operator=(const OpenSSLSessionCache&) = delete;
41
Benjamin Wright19aab2e2018-04-05 15:39:06 -070042 // Looks up a session by hostname. The returned SSL_SESSION is not up_refed.
43 SSL_SESSION* LookupSession(const std::string& hostname) const;
44 // Adds a session to the cache, and up_refs it. Any existing session with the
45 // same hostname is replaced.
46 void AddSession(const std::string& hostname, SSL_SESSION* session);
47 // Returns the true underlying SSL Context that holds these cached sessions.
48 SSL_CTX* GetSSLContext() const;
49 // The SSL Mode tht the OpenSSLSessionCache was constructed with. This cannot
50 // be changed after launch.
51 SSLMode GetSSLMode() const;
52
53 private:
54 // Holds the SSL Mode that the OpenSSLCache was initialized with. This is
55 // immutable after creation and cannot change.
56 const SSLMode ssl_mode_;
57 /// SSL Context for all shared cached sessions. This SSL_CTX is initialized
58 // with SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT); Meaning
59 // all client sessions will be added to the cache internal to the context.
60 SSL_CTX* ssl_ctx_ = nullptr;
61 // Map of hostnames to SSL_SESSIONs; holds references to the SSL_SESSIONs,
62 // which are cleaned up when the factory is destroyed.
63 // TODO(juberti): Add LRU eviction to keep the cache from growing forever.
64 std::map<std::string, SSL_SESSION*> sessions_;
65 // The cache should never be copied or assigned directly.
Benjamin Wright19aab2e2018-04-05 15:39:06 -070066};
67
68} // namespace rtc
69
Steve Anton10542f22019-01-11 09:11:00 -080070#endif // RTC_BASE_OPENSSL_SESSION_CACHE_H_