xref: /aosp_15_r20/external/webrtc/rtc_base/openssl_session_cache.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 #include "rtc_base/openssl_session_cache.h"
12 
13 #include "absl/strings/string_view.h"
14 #include "rtc_base/checks.h"
15 #include "rtc_base/openssl.h"
16 
17 namespace rtc {
18 
OpenSSLSessionCache(SSLMode ssl_mode,SSL_CTX * ssl_ctx)19 OpenSSLSessionCache::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 
~OpenSSLSessionCache()26 OpenSSLSessionCache::~OpenSSLSessionCache() {
27   for (const auto& it : sessions_) {
28     SSL_SESSION_free(it.second);
29   }
30   SSL_CTX_free(ssl_ctx_);
31 }
32 
LookupSession(absl::string_view hostname) const33 SSL_SESSION* OpenSSLSessionCache::LookupSession(
34     absl::string_view hostname) const {
35   auto it = sessions_.find(hostname);
36   return (it != sessions_.end()) ? it->second : nullptr;
37 }
38 
AddSession(absl::string_view hostname,SSL_SESSION * new_session)39 void OpenSSLSessionCache::AddSession(absl::string_view hostname,
40                                      SSL_SESSION* new_session) {
41   SSL_SESSION* old_session = LookupSession(hostname);
42   SSL_SESSION_free(old_session);
43   sessions_.insert_or_assign(std::string(hostname), new_session);
44 }
45 
GetSSLContext() const46 SSL_CTX* OpenSSLSessionCache::GetSSLContext() const {
47   return ssl_ctx_;
48 }
49 
GetSSLMode() const50 SSLMode OpenSSLSessionCache::GetSSLMode() const {
51   return ssl_mode_;
52 }
53 
54 }  // namespace rtc
55