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 11 #ifndef RTC_BASE_OPENSSL_SESSION_CACHE_H_ 12 #define RTC_BASE_OPENSSL_SESSION_CACHE_H_ 13 14 #include <openssl/ossl_typ.h> 15 16 #include <map> 17 #include <string> 18 19 #include "absl/strings/string_view.h" 20 #include "rtc_base/ssl_stream_adapter.h" 21 #include "rtc_base/string_utils.h" 22 23 #ifndef OPENSSL_IS_BORINGSSL 24 typedef struct ssl_session_st SSL_SESSION; 25 #endif 26 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(); 40 41 OpenSSLSessionCache(const OpenSSLSessionCache&) = delete; 42 OpenSSLSessionCache& operator=(const OpenSSLSessionCache&) = delete; 43 44 // Looks up a session by hostname. The returned SSL_SESSION is not up_refed. 45 SSL_SESSION* LookupSession(absl::string_view hostname) const; 46 // Adds a session to the cache, and up_refs it. Any existing session with the 47 // same hostname is replaced. 48 void AddSession(absl::string_view hostname, SSL_SESSION* session); 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. 66 std::map<std::string, SSL_SESSION*, rtc::AbslStringViewCmp> sessions_; 67 // The cache should never be copied or assigned directly. 68 }; 69 70 } // namespace rtc 71 72 #endif // RTC_BASE_OPENSSL_SESSION_CACHE_H_ 73