You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.5 KiB

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