blob: 270abe89037d31aaef07d6268eabf46a0154f725 [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"
Benjamin Wright19aab2e2018-04-05 22:39:0612#include "rtc_base/checks.h"
13#include "rtc_base/openssl.h"
14
15namespace rtc {
16
17OpenSSLSessionCache::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
24OpenSSLSessionCache::~OpenSSLSessionCache() {
Mirko Bonadei739baf02019-01-27 16:29:4225 for (const auto& it : sessions_) {
Benjamin Wright19aab2e2018-04-05 22:39:0626 SSL_SESSION_free(it.second);
27 }
28 SSL_CTX_free(ssl_ctx_);
29}
30
31SSL_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
37void 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
44SSL_CTX* OpenSSLSessionCache::GetSSLContext() const {
45 return ssl_ctx_;
46}
47
48SSLMode OpenSSLSessionCache::GetSSLMode() const {
49 return ssl_mode_;
50}
51
52} // namespace rtc