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 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "rtc_base/openssl_session_cache.h" |
| 12 | |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 13 | #include <openssl/ssl.h> |
| 14 | #include <stdlib.h> |
| 15 | |
| 16 | #include <map> |
| 17 | #include <memory> |
| 18 | |
| 19 | #include "rtc_base/gunit.h" |
| 20 | #include "rtc_base/openssl.h" |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 21 | |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame^] | 22 | namespace { |
| 23 | // Use methods that avoid X509 objects if possible. |
| 24 | SSL_CTX* NewDtlsContext() { |
| 25 | #ifdef OPENSSL_IS_BORINGSSL |
| 26 | return SSL_CTX_new(DTLS_with_buffers_method()); |
| 27 | #else |
| 28 | return SSL_CTX_new(DTLS_method()); |
| 29 | #endif |
| 30 | } |
| 31 | SSL_CTX* NewTlsContext() { |
| 32 | #ifdef OPENSSL_IS_BORINGSSL |
| 33 | return SSL_CTX_new(TLS_with_buffers_method()); |
| 34 | #else |
| 35 | return SSL_CTX_new(TLS_method()); |
| 36 | #endif |
| 37 | } |
| 38 | } // namespace |
| 39 | |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 40 | namespace rtc { |
| 41 | |
| 42 | TEST(OpenSSLSessionCache, DTLSModeSetCorrectly) { |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame^] | 43 | SSL_CTX* ssl_ctx = NewDtlsContext(); |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 44 | |
| 45 | OpenSSLSessionCache session_cache(SSL_MODE_DTLS, ssl_ctx); |
| 46 | EXPECT_EQ(session_cache.GetSSLMode(), SSL_MODE_DTLS); |
| 47 | |
| 48 | SSL_CTX_free(ssl_ctx); |
| 49 | } |
| 50 | |
| 51 | TEST(OpenSSLSessionCache, TLSModeSetCorrectly) { |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame^] | 52 | SSL_CTX* ssl_ctx = NewTlsContext(); |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 53 | |
| 54 | OpenSSLSessionCache session_cache(SSL_MODE_TLS, ssl_ctx); |
| 55 | EXPECT_EQ(session_cache.GetSSLMode(), SSL_MODE_TLS); |
| 56 | |
| 57 | SSL_CTX_free(ssl_ctx); |
| 58 | } |
| 59 | |
| 60 | TEST(OpenSSLSessionCache, SSLContextSetCorrectly) { |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame^] | 61 | SSL_CTX* ssl_ctx = NewDtlsContext(); |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 62 | |
| 63 | OpenSSLSessionCache session_cache(SSL_MODE_DTLS, ssl_ctx); |
| 64 | EXPECT_EQ(session_cache.GetSSLContext(), ssl_ctx); |
| 65 | |
| 66 | SSL_CTX_free(ssl_ctx); |
| 67 | } |
| 68 | |
| 69 | TEST(OpenSSLSessionCache, InvalidLookupReturnsNullptr) { |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame^] | 70 | SSL_CTX* ssl_ctx = NewDtlsContext(); |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 71 | |
| 72 | OpenSSLSessionCache session_cache(SSL_MODE_DTLS, ssl_ctx); |
| 73 | EXPECT_EQ(session_cache.LookupSession("Invalid"), nullptr); |
| 74 | EXPECT_EQ(session_cache.LookupSession(""), nullptr); |
| 75 | EXPECT_EQ(session_cache.LookupSession("."), nullptr); |
| 76 | |
| 77 | SSL_CTX_free(ssl_ctx); |
| 78 | } |
| 79 | |
| 80 | TEST(OpenSSLSessionCache, SimpleValidSessionLookup) { |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame^] | 81 | SSL_CTX* ssl_ctx = NewDtlsContext(); |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 82 | SSL_SESSION* ssl_session = SSL_SESSION_new(ssl_ctx); |
| 83 | |
| 84 | OpenSSLSessionCache session_cache(SSL_MODE_DTLS, ssl_ctx); |
| 85 | session_cache.AddSession("webrtc.org", ssl_session); |
| 86 | EXPECT_EQ(session_cache.LookupSession("webrtc.org"), ssl_session); |
| 87 | |
| 88 | SSL_CTX_free(ssl_ctx); |
| 89 | } |
| 90 | |
| 91 | TEST(OpenSSLSessionCache, AddToExistingReplacesPrevious) { |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame^] | 92 | SSL_CTX* ssl_ctx = NewDtlsContext(); |
Benjamin Wright | 19aab2e | 2018-04-05 15:39:06 -0700 | [diff] [blame] | 93 | SSL_SESSION* ssl_session_1 = SSL_SESSION_new(ssl_ctx); |
| 94 | SSL_SESSION* ssl_session_2 = SSL_SESSION_new(ssl_ctx); |
| 95 | |
| 96 | OpenSSLSessionCache session_cache(SSL_MODE_DTLS, ssl_ctx); |
| 97 | session_cache.AddSession("webrtc.org", ssl_session_1); |
| 98 | session_cache.AddSession("webrtc.org", ssl_session_2); |
| 99 | EXPECT_EQ(session_cache.LookupSession("webrtc.org"), ssl_session_2); |
| 100 | |
| 101 | SSL_CTX_free(ssl_ctx); |
| 102 | } |
| 103 | |
| 104 | } // namespace rtc |