blob: d63724242a65f7c1f75c378e109b327d5e74f15b [file] [log] [blame]
Benjamin Wright19aab2e2018-04-05 22:39:061/*
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 17:11:0011#include "rtc_base/openssl_session_cache.h"
Jonas Olssona4d87372019-07-05 17:08:3312
Ali Tofigh7fa90572022-03-17 14:47:4913#include "absl/strings/string_view.h"
Benjamin Wright19aab2e2018-04-05 22:39:0614#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 16:29:4227 for (const auto& it : sessions_) {
Benjamin Wright19aab2e2018-04-05 22:39:0628 SSL_SESSION_free(it.second);
29 }
30 SSL_CTX_free(ssl_ctx_);
31}
32
33SSL_SESSION* OpenSSLSessionCache::LookupSession(
Ali Tofigh7fa90572022-03-17 14:47:4934 absl::string_view hostname) const {
Benjamin Wright19aab2e2018-04-05 22:39:0635 auto it = sessions_.find(hostname);
36 return (it != sessions_.end()) ? it->second : nullptr;
37}
38
Ali Tofigh7fa90572022-03-17 14:47:4939void OpenSSLSessionCache::AddSession(absl::string_view hostname,
Benjamin Wright19aab2e2018-04-05 22:39:0640 SSL_SESSION* new_session) {
41 SSL_SESSION* old_session = LookupSession(hostname);
42 SSL_SESSION_free(old_session);
Ali Tofigh7fa90572022-03-17 14:47:4943 sessions_.insert_or_assign(std::string(hostname), new_session);
Benjamin Wright19aab2e2018-04-05 22:39:0644}
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