Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 1 | /* |
| 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 Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_OPENSSL_SESSION_CACHE_H_ |
| 12 | #define RTC_BASE_OPENSSL_SESSION_CACHE_H_ |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 13 | |
| 14 | #include <openssl/ossl_typ.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 16 | #include <map> |
| 17 | #include <string> |
| 18 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 19 | #include "absl/strings/string_view.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "rtc_base/ssl_stream_adapter.h" |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 21 | #include "rtc_base/string_utils.h" |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 22 | |
Jiawei Ou | 3b8d48b | 2018-05-29 16:00:09 -0700 | [diff] [blame] | 23 | #ifndef OPENSSL_IS_BORINGSSL |
| 24 | typedef struct ssl_session_st SSL_SESSION; |
| 25 | #endif |
| 26 | |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 27 | namespace 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. |
| 32 | class 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 Lee | 14af762 | 2022-01-12 05:24:58 +0900 | [diff] [blame] | 40 | |
| 41 | OpenSSLSessionCache(const OpenSSLSessionCache&) = delete; |
| 42 | OpenSSLSessionCache& operator=(const OpenSSLSessionCache&) = delete; |
| 43 | |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 44 | // Looks up a session by hostname. The returned SSL_SESSION is not up_refed. |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 45 | SSL_SESSION* LookupSession(absl::string_view hostname) const; |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 46 | // Adds a session to the cache, and up_refs it. Any existing session with the |
| 47 | // same hostname is replaced. |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 48 | void AddSession(absl::string_view hostname, SSL_SESSION* session); |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 49 | // 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 Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 66 | std::map<std::string, SSL_SESSION*, rtc::AbslStringViewCmp> sessions_; |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 67 | // The cache should never be copied or assigned directly. |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | } // namespace rtc |
| 71 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 72 | #endif // RTC_BASE_OPENSSL_SESSION_CACHE_H_ |