blob: f8fcd473dcce1712f12a5fb2c84b5900b8235bf0 [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#include "rtc_base/openssl_session_cache.h"
Jonas Olssona4d87372019-07-05 19:08:33 +020012
Benjamin Wright19aab2e2018-04-05 15:39:06 -070013#include "rtc_base/checks.h"
14#include "rtc_base/openssl.h"
15
16namespace rtc {
17
18OpenSSLSessionCache::OpenSSLSessionCache(SSLMode ssl_mode, SSL_CTX* ssl_ctx)
19 : ssl_mode_(ssl_mode), ssl_ctx_(ssl_ctx) {
20 // It is invalid to pass in a null context.
21 RTC_DCHECK(ssl_ctx != nullptr);
22 SSL_CTX_up_ref(ssl_ctx);
23}
24
25OpenSSLSessionCache::~OpenSSLSessionCache() {
Mirko Bonadei739baf02019-01-27 17:29:42 +010026 for (const auto& it : sessions_) {
Benjamin Wright19aab2e2018-04-05 15:39:06 -070027 SSL_SESSION_free(it.second);
28 }
29 SSL_CTX_free(ssl_ctx_);
30}
31
32SSL_SESSION* OpenSSLSessionCache::LookupSession(
33 const std::string& hostname) const {
34 auto it = sessions_.find(hostname);
35 return (it != sessions_.end()) ? it->second : nullptr;
36}
37
38void OpenSSLSessionCache::AddSession(const std::string& hostname,
39 SSL_SESSION* new_session) {
40 SSL_SESSION* old_session = LookupSession(hostname);
41 SSL_SESSION_free(old_session);
42 sessions_[hostname] = new_session;
43}
44
45SSL_CTX* OpenSSLSessionCache::GetSSLContext() const {
46 return ssl_ctx_;
47}
48
49SSLMode OpenSSLSessionCache::GetSSLMode() const {
50 return ssl_mode_;
51}
52
53} // namespace rtc