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