blob: e53e41a8167cce2a6720af14e2961d6601a02a49 [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>
15#include <map>
16#include <string>
17
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/constructor_magic.h"
19#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();
38 // Looks up a session by hostname. The returned SSL_SESSION is not up_refed.
39 SSL_SESSION* LookupSession(const std::string& hostname) const;
40 // Adds a session to the cache, and up_refs it. Any existing session with the
41 // same hostname is replaced.
42 void AddSession(const std::string& hostname, SSL_SESSION* session);
43 // Returns the true underlying SSL Context that holds these cached sessions.
44 SSL_CTX* GetSSLContext() const;
45 // The SSL Mode tht the OpenSSLSessionCache was constructed with. This cannot
46 // be changed after launch.
47 SSLMode GetSSLMode() const;
48
49 private:
50 // Holds the SSL Mode that the OpenSSLCache was initialized with. This is
51 // immutable after creation and cannot change.
52 const SSLMode ssl_mode_;
53 /// SSL Context for all shared cached sessions. This SSL_CTX is initialized
54 // with SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT); Meaning
55 // all client sessions will be added to the cache internal to the context.
56 SSL_CTX* ssl_ctx_ = nullptr;
57 // Map of hostnames to SSL_SESSIONs; holds references to the SSL_SESSIONs,
58 // which are cleaned up when the factory is destroyed.
59 // TODO(juberti): Add LRU eviction to keep the cache from growing forever.
60 std::map<std::string, SSL_SESSION*> sessions_;
61 // The cache should never be copied or assigned directly.
62 RTC_DISALLOW_COPY_AND_ASSIGN(OpenSSLSessionCache);
63};
64
65} // namespace rtc
66
Steve Anton10542f22019-01-11 09:11:00 -080067#endif // RTC_BASE_OPENSSL_SESSION_CACHE_H_