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