Benjamin Wright | 19aab2e | 2018-04-05 22:39:06 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "rtc_base/openssl_session_cache.h" |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 12 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 13 | #include "absl/strings/string_view.h" |
Benjamin Wright | 19aab2e | 2018-04-05 22:39:06 | [diff] [blame] | 14 | #include "rtc_base/checks.h" |
| 15 | #include "rtc_base/openssl.h" |
| 16 | |
| 17 | namespace rtc { |
| 18 | |
| 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 | |
| 26 | OpenSSLSessionCache::~OpenSSLSessionCache() { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 27 | for (const auto& it : sessions_) { |
Benjamin Wright | 19aab2e | 2018-04-05 22:39:06 | [diff] [blame] | 28 | SSL_SESSION_free(it.second); |
| 29 | } |
| 30 | SSL_CTX_free(ssl_ctx_); |
| 31 | } |
| 32 | |
| 33 | SSL_SESSION* OpenSSLSessionCache::LookupSession( |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 34 | absl::string_view hostname) const { |
Benjamin Wright | 19aab2e | 2018-04-05 22:39:06 | [diff] [blame] | 35 | auto it = sessions_.find(hostname); |
| 36 | return (it != sessions_.end()) ? it->second : nullptr; |
| 37 | } |
| 38 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 39 | void OpenSSLSessionCache::AddSession(absl::string_view hostname, |
Benjamin Wright | 19aab2e | 2018-04-05 22:39:06 | [diff] [blame] | 40 | SSL_SESSION* new_session) { |
| 41 | SSL_SESSION* old_session = LookupSession(hostname); |
| 42 | SSL_SESSION_free(old_session); |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 43 | sessions_.insert_or_assign(std::string(hostname), new_session); |
Benjamin Wright | 19aab2e | 2018-04-05 22:39:06 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | SSL_CTX* OpenSSLSessionCache::GetSSLContext() const { |
| 47 | return ssl_ctx_; |
| 48 | } |
| 49 | |
| 50 | SSLMode OpenSSLSessionCache::GetSSLMode() const { |
| 51 | return ssl_mode_; |
| 52 | } |
| 53 | |
| 54 | } // namespace rtc |