blob: d63724242a65f7c1f75c378e109b327d5e74f15b [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
Ali Tofigh7fa90572022-03-17 15:47:49 +010013#include "absl/strings/string_view.h"
Benjamin Wright19aab2e2018-04-05 15:39:06 -070014#include "rtc_base/checks.h"
15#include "rtc_base/openssl.h"
16
17namespace rtc {
18
19OpenSSLSessionCache::OpenSSLSessionCache(SSLMode ssl_mode, SSL_CTX* ssl_ctx)
20 : ssl_mode_(ssl_mode), ssl_ctx_(ssl_ctx) {
21 // It is invalid to pass in a null context.
22 RTC_DCHECK(ssl_ctx != nullptr);
23 SSL_CTX_up_ref(ssl_ctx);
24}
25
26OpenSSLSessionCache::~OpenSSLSessionCache() {
Mirko Bonadei739baf02019-01-27 17:29:42 +010027 for (const auto& it : sessions_) {
Benjamin Wright19aab2e2018-04-05 15:39:06 -070028 SSL_SESSION_free(it.second);
29 }
30 SSL_CTX_free(ssl_ctx_);
31}
32
33SSL_SESSION* OpenSSLSessionCache::LookupSession(
Ali Tofigh7fa90572022-03-17 15:47:49 +010034 absl::string_view hostname) const {
Benjamin Wright19aab2e2018-04-05 15:39:06 -070035 auto it = sessions_.find(hostname);
36 return (it != sessions_.end()) ? it->second : nullptr;
37}
38
Ali Tofigh7fa90572022-03-17 15:47:49 +010039void OpenSSLSessionCache::AddSession(absl::string_view hostname,
Benjamin Wright19aab2e2018-04-05 15:39:06 -070040 SSL_SESSION* new_session) {
41 SSL_SESSION* old_session = LookupSession(hostname);
42 SSL_SESSION_free(old_session);
Ali Tofigh7fa90572022-03-17 15:47:49 +010043 sessions_.insert_or_assign(std::string(hostname), new_session);
Benjamin Wright19aab2e2018-04-05 15:39:06 -070044}
45
46SSL_CTX* OpenSSLSessionCache::GetSSLContext() const {
47 return ssl_ctx_;
48}
49
50SSLMode OpenSSLSessionCache::GetSSLMode() const {
51 return ssl_mode_;
52}
53
54} // namespace rtc