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 | |
| 11 | #ifndef RTC_BASE_OPENSSLSESSIONCACHE_H_ |
| 12 | #define RTC_BASE_OPENSSLSESSIONCACHE_H_ |
| 13 | |
| 14 | #include <openssl/ossl_typ.h> |
| 15 | #include <map> |
| 16 | #include <string> |
| 17 | |
| 18 | #include "rtc_base/constructormagic.h" |
| 19 | #include "rtc_base/sslstreamadapter.h" |
| 20 | |
| 21 | namespace rtc { |
| 22 | |
| 23 | // The OpenSSLSessionCache maps hostnames to SSL_SESSIONS. This cache is |
| 24 | // owned by the OpenSSLAdapterFactory and is passed down to each OpenSSLAdapter |
| 25 | // created with the factory. |
| 26 | class OpenSSLSessionCache final { |
| 27 | public: |
| 28 | // Creates a new OpenSSLSessionCache using the provided the SSL_CTX and |
| 29 | // the ssl_mode. The SSL_CTX will be up_refed. ssl_ctx cannot be nullptr, |
| 30 | // the constructor immediately dchecks this. |
| 31 | OpenSSLSessionCache(SSLMode ssl_mode, SSL_CTX* ssl_ctx); |
| 32 | // Frees the cached SSL_SESSIONS and then frees the SSL_CTX. |
| 33 | ~OpenSSLSessionCache(); |
| 34 | // Looks up a session by hostname. The returned SSL_SESSION is not up_refed. |
| 35 | SSL_SESSION* LookupSession(const std::string& hostname) const; |
| 36 | // Adds a session to the cache, and up_refs it. Any existing session with the |
| 37 | // same hostname is replaced. |
| 38 | void AddSession(const std::string& hostname, SSL_SESSION* session); |
| 39 | // Returns the true underlying SSL Context that holds these cached sessions. |
| 40 | SSL_CTX* GetSSLContext() const; |
| 41 | // The SSL Mode tht the OpenSSLSessionCache was constructed with. This cannot |
| 42 | // be changed after launch. |
| 43 | SSLMode GetSSLMode() const; |
| 44 | |
| 45 | private: |
| 46 | // Holds the SSL Mode that the OpenSSLCache was initialized with. This is |
| 47 | // immutable after creation and cannot change. |
| 48 | const SSLMode ssl_mode_; |
| 49 | /// SSL Context for all shared cached sessions. This SSL_CTX is initialized |
| 50 | // with SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT); Meaning |
| 51 | // all client sessions will be added to the cache internal to the context. |
| 52 | SSL_CTX* ssl_ctx_ = nullptr; |
| 53 | // Map of hostnames to SSL_SESSIONs; holds references to the SSL_SESSIONs, |
| 54 | // which are cleaned up when the factory is destroyed. |
| 55 | // TODO(juberti): Add LRU eviction to keep the cache from growing forever. |
| 56 | std::map<std::string, SSL_SESSION*> sessions_; |
| 57 | // The cache should never be copied or assigned directly. |
| 58 | RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLSessionCache); |
| 59 | }; |
| 60 | |
| 61 | } // namespace rtc |
| 62 | |
| 63 | #endif // RTC_BASE_OPENSSLSESSIONCACHE_H_ |