blob: 75d8d9a0cf37e1175c5da2e1d5ee6a7947465752 [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
Ali Tofigh7fa90572022-03-17 15:47:49 +010019#include "absl/strings/string_view.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/ssl_stream_adapter.h"
Ali Tofigh7fa90572022-03-17 15:47:49 +010021#include "rtc_base/string_utils.h"
Benjamin Wright19aab2e2018-04-05 15:39:06 -070022
Jiawei Ou3b8d48b2018-05-29 16:00:09 -070023#ifndef OPENSSL_IS_BORINGSSL
24typedef struct ssl_session_st SSL_SESSION;
25#endif
26
Benjamin Wright19aab2e2018-04-05 15:39:06 -070027namespace rtc {
28
29// The OpenSSLSessionCache maps hostnames to SSL_SESSIONS. This cache is
30// owned by the OpenSSLAdapterFactory and is passed down to each OpenSSLAdapter
31// created with the factory.
32class OpenSSLSessionCache final {
33 public:
34 // Creates a new OpenSSLSessionCache using the provided the SSL_CTX and
35 // the ssl_mode. The SSL_CTX will be up_refed. ssl_ctx cannot be nullptr,
36 // the constructor immediately dchecks this.
37 OpenSSLSessionCache(SSLMode ssl_mode, SSL_CTX* ssl_ctx);
38 // Frees the cached SSL_SESSIONS and then frees the SSL_CTX.
39 ~OpenSSLSessionCache();
Byoungchan Lee14af7622022-01-12 05:24:58 +090040
41 OpenSSLSessionCache(const OpenSSLSessionCache&) = delete;
42 OpenSSLSessionCache& operator=(const OpenSSLSessionCache&) = delete;
43
Benjamin Wright19aab2e2018-04-05 15:39:06 -070044 // Looks up a session by hostname. The returned SSL_SESSION is not up_refed.
Ali Tofigh7fa90572022-03-17 15:47:49 +010045 SSL_SESSION* LookupSession(absl::string_view hostname) const;
Benjamin Wright19aab2e2018-04-05 15:39:06 -070046 // Adds a session to the cache, and up_refs it. Any existing session with the
47 // same hostname is replaced.
Ali Tofigh7fa90572022-03-17 15:47:49 +010048 void AddSession(absl::string_view hostname, SSL_SESSION* session);
Benjamin Wright19aab2e2018-04-05 15:39:06 -070049 // Returns the true underlying SSL Context that holds these cached sessions.
50 SSL_CTX* GetSSLContext() const;
51 // The SSL Mode tht the OpenSSLSessionCache was constructed with. This cannot
52 // be changed after launch.
53 SSLMode GetSSLMode() const;
54
55 private:
56 // Holds the SSL Mode that the OpenSSLCache was initialized with. This is
57 // immutable after creation and cannot change.
58 const SSLMode ssl_mode_;
59 /// SSL Context for all shared cached sessions. This SSL_CTX is initialized
60 // with SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT); Meaning
61 // all client sessions will be added to the cache internal to the context.
62 SSL_CTX* ssl_ctx_ = nullptr;
63 // Map of hostnames to SSL_SESSIONs; holds references to the SSL_SESSIONs,
64 // which are cleaned up when the factory is destroyed.
65 // TODO(juberti): Add LRU eviction to keep the cache from growing forever.
Ali Tofigh7fa90572022-03-17 15:47:49 +010066 std::map<std::string, SSL_SESSION*, rtc::AbslStringViewCmp> sessions_;
Benjamin Wright19aab2e2018-04-05 15:39:06 -070067 // The cache should never be copied or assigned directly.
Benjamin Wright19aab2e2018-04-05 15:39:06 -070068};
69
70} // namespace rtc
71
Steve Anton10542f22019-01-11 09:11:00 -080072#endif // RTC_BASE_OPENSSL_SESSION_CACHE_H_