tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 11 | #include "rtc_base/ssl_adapter.h" |
| 12 | |
jbauch | 555604a | 2016-04-26 10:13:22 | [diff] [blame] | 13 | #include <memory> |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 14 | #include <string> |
Benjamin Wright | 6e9c3df | 2018-05-22 23:11:56 | [diff] [blame] | 15 | #include <utility> |
Harald Alvestrand | 53c424e | 2024-08-01 06:31:02 | [diff] [blame] | 16 | #include <vector> |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 17 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 18 | #include "absl/strings/string_view.h" |
Evan Shrubsole | d959303 | 2025-01-17 13:19:45 | [diff] [blame] | 19 | #include "api/test/rtc_error_matchers.h" |
| 20 | #include "api/units/time_delta.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 21 | #include "rtc_base/ip_address.h" |
Harald Alvestrand | 53c424e | 2024-08-01 06:31:02 | [diff] [blame] | 22 | #include "rtc_base/logging.h" |
Harald Alvestrand | 53c424e | 2024-08-01 06:31:02 | [diff] [blame] | 23 | #include "rtc_base/socket.h" |
| 24 | #include "rtc_base/socket_address.h" |
| 25 | #include "rtc_base/ssl_certificate.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 26 | #include "rtc_base/ssl_identity.h" |
Evan Shrubsole | d959303 | 2025-01-17 13:19:45 | [diff] [blame] | 27 | #include "rtc_base/ssl_stream_adapter.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 28 | #include "rtc_base/string_encode.h" |
Harald Alvestrand | 53c424e | 2024-08-01 06:31:02 | [diff] [blame] | 29 | #include "rtc_base/third_party/sigslot/sigslot.h" |
| 30 | #include "rtc_base/thread.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 31 | #include "rtc_base/virtual_socket_server.h" |
Benjamin Wright | 6e9c3df | 2018-05-22 23:11:56 | [diff] [blame] | 32 | #include "test/gmock.h" |
Harald Alvestrand | 53c424e | 2024-08-01 06:31:02 | [diff] [blame] | 33 | #include "test/gtest.h" |
Evan Shrubsole | d959303 | 2025-01-17 13:19:45 | [diff] [blame] | 34 | #include "test/wait_until.h" |
Benjamin Wright | 6e9c3df | 2018-05-22 23:11:56 | [diff] [blame] | 35 | |
| 36 | using ::testing::_; |
| 37 | using ::testing::Return; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 38 | |
Evan Shrubsole | d959303 | 2025-01-17 13:19:45 | [diff] [blame] | 39 | static const webrtc::TimeDelta kTimeout = webrtc::TimeDelta::Millis(5000); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 40 | |
Evan Shrubsole | 03b6880 | 2025-03-18 12:23:05 | [diff] [blame] | 41 | static webrtc::Socket* CreateSocket() { |
Evan Shrubsole | 64b076f4 | 2025-03-12 12:56:28 | [diff] [blame] | 42 | webrtc::SocketAddress address(webrtc::IPAddress(INADDR_ANY), 0); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 43 | |
Evan Shrubsole | 03b6880 | 2025-03-18 12:23:05 | [diff] [blame] | 44 | webrtc::Socket* socket = rtc::Thread::Current()->socketserver()->CreateSocket( |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 45 | address.family(), SOCK_STREAM); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 46 | socket->Bind(address); |
| 47 | |
| 48 | return socket; |
| 49 | } |
| 50 | |
Benjamin Wright | 6e9c3df | 2018-05-22 23:11:56 | [diff] [blame] | 51 | // Simple mock for the certificate verifier. |
| 52 | class MockCertVerifier : public rtc::SSLCertificateVerifier { |
| 53 | public: |
| 54 | virtual ~MockCertVerifier() = default; |
Danil Chapovalov | 42748d8 | 2020-05-14 18:42:41 | [diff] [blame] | 55 | MOCK_METHOD(bool, Verify, (const rtc::SSLCertificate&), (override)); |
Benjamin Wright | 6e9c3df | 2018-05-22 23:11:56 | [diff] [blame] | 56 | }; |
| 57 | |
Mirko Bonadei | c84f661 | 2019-01-31 11:20:57 | [diff] [blame] | 58 | // TODO(benwright) - Move to using INSTANTIATE_TEST_SUITE_P instead of using |
Benjamin Wright | 6e9c3df | 2018-05-22 23:11:56 | [diff] [blame] | 59 | // duplicate test cases for simple parameter changes. |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 60 | class SSLAdapterTestDummy : public sigslot::has_slots<> { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 61 | public: |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 62 | explicit SSLAdapterTestDummy() : socket_(CreateSocket()) {} |
| 63 | virtual ~SSLAdapterTestDummy() = default; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 64 | |
Evan Shrubsole | 03b6880 | 2025-03-18 12:23:05 | [diff] [blame] | 65 | void CreateSSLAdapter(webrtc::Socket* socket, webrtc::SSLRole role) { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 66 | ssl_adapter_.reset(rtc::SSLAdapter::Create(socket)); |
| 67 | |
| 68 | // Ignore any certificate errors for the purpose of testing. |
| 69 | // Note: We do this only because we don't have a real certificate. |
| 70 | // NEVER USE THIS IN PRODUCTION CODE! |
Sergey Silkin | 9c147dd | 2018-09-12 10:45:38 | [diff] [blame] | 71 | ssl_adapter_->SetIgnoreBadCert(true); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 72 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 73 | ssl_adapter_->SignalReadEvent.connect( |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 74 | this, &SSLAdapterTestDummy::OnSSLAdapterReadEvent); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 75 | ssl_adapter_->SignalCloseEvent.connect( |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 76 | this, &SSLAdapterTestDummy::OnSSLAdapterCloseEvent); |
| 77 | ssl_adapter_->SetRole(role); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 78 | } |
| 79 | |
Sergey Silkin | 9c147dd | 2018-09-12 10:45:38 | [diff] [blame] | 80 | void SetIgnoreBadCert(bool ignore_bad_cert) { |
| 81 | ssl_adapter_->SetIgnoreBadCert(ignore_bad_cert); |
Benjamin Wright | 6e9c3df | 2018-05-22 23:11:56 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void SetCertVerifier(rtc::SSLCertificateVerifier* ssl_cert_verifier) { |
| 85 | ssl_adapter_->SetCertVerifier(ssl_cert_verifier); |
| 86 | } |
| 87 | |
Sergey Silkin | 9c147dd | 2018-09-12 10:45:38 | [diff] [blame] | 88 | void SetAlpnProtocols(const std::vector<std::string>& protos) { |
| 89 | ssl_adapter_->SetAlpnProtocols(protos); |
| 90 | } |
| 91 | |
| 92 | void SetEllipticCurves(const std::vector<std::string>& curves) { |
| 93 | ssl_adapter_->SetEllipticCurves(curves); |
| 94 | } |
| 95 | |
Evan Shrubsole | 64b076f4 | 2025-03-12 12:56:28 | [diff] [blame] | 96 | webrtc::SocketAddress GetAddress() const { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 | [diff] [blame] | 97 | return ssl_adapter_->GetLocalAddress(); |
| 98 | } |
| 99 | |
Evan Shrubsole | 03b6880 | 2025-03-18 12:23:05 | [diff] [blame] | 100 | webrtc::Socket::ConnState GetState() const { |
| 101 | return ssl_adapter_->GetState(); |
| 102 | } |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 103 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 104 | const std::string& GetReceivedData() const { return data_; } |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 105 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 106 | int Close() { return ssl_adapter_->Close(); } |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 107 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 108 | int Send(absl::string_view message) { |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 109 | RTC_LOG(LS_INFO) << "Sending '" << message << "'"; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 110 | |
| 111 | return ssl_adapter_->Send(message.data(), message.length()); |
| 112 | } |
| 113 | |
Evan Shrubsole | 03b6880 | 2025-03-18 12:23:05 | [diff] [blame] | 114 | void OnSSLAdapterReadEvent(webrtc::Socket* socket) { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 115 | char buffer[4096] = ""; |
| 116 | |
| 117 | // Read data received from the server and store it in our internal buffer. |
Stefan Holmer | 9131efd | 2016-05-23 16:19:26 | [diff] [blame] | 118 | int read = socket->Recv(buffer, sizeof(buffer) - 1, nullptr); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 119 | if (read != -1) { |
| 120 | buffer[read] = '\0'; |
| 121 | |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 122 | RTC_LOG(LS_INFO) << "Received '" << buffer << "'"; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 123 | |
| 124 | data_ += buffer; |
| 125 | } |
| 126 | } |
| 127 | |
Evan Shrubsole | 03b6880 | 2025-03-18 12:23:05 | [diff] [blame] | 128 | void OnSSLAdapterCloseEvent(webrtc::Socket* socket, int error) { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 129 | // OpenSSLAdapter signals handshake failure with a close event, but without |
| 130 | // closing the socket! Let's close the socket here. This way GetState() can |
| 131 | // return CS_CLOSED after failure. |
Evan Shrubsole | 03b6880 | 2025-03-18 12:23:05 | [diff] [blame] | 132 | if (socket->GetState() != webrtc::Socket::CS_CLOSED) { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 133 | socket->Close(); |
| 134 | } |
| 135 | } |
| 136 | |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 137 | protected: |
jbauch | 555604a | 2016-04-26 10:13:22 | [diff] [blame] | 138 | std::unique_ptr<rtc::SSLAdapter> ssl_adapter_; |
Evan Shrubsole | 03b6880 | 2025-03-18 12:23:05 | [diff] [blame] | 139 | std::unique_ptr<webrtc::Socket> socket_; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 140 | |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 141 | private: |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 142 | std::string data_; |
| 143 | }; |
| 144 | |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 145 | class SSLAdapterTestDummyClient : public SSLAdapterTestDummy { |
Tommi | bdb867f | 2024-03-18 09:20:14 | [diff] [blame] | 146 | public: |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 147 | explicit SSLAdapterTestDummyClient() : SSLAdapterTestDummy() { |
Evan Shrubsole | eb835d0 | 2025-03-12 09:41:06 | [diff] [blame] | 148 | CreateSSLAdapter(socket_.release(), webrtc::SSL_CLIENT); |
Tommi | bdb867f | 2024-03-18 09:20:14 | [diff] [blame] | 149 | } |
| 150 | |
Evan Shrubsole | 64b076f4 | 2025-03-12 12:56:28 | [diff] [blame] | 151 | int Connect(absl::string_view hostname, |
| 152 | const webrtc::SocketAddress& address) { |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 153 | RTC_LOG(LS_INFO) << "Initiating connection with " << address.ToString(); |
| 154 | int rv = ssl_adapter_->Connect(address); |
Tommi | bdb867f | 2024-03-18 09:20:14 | [diff] [blame] | 155 | |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 156 | if (rv == 0) { |
| 157 | RTC_LOG(LS_INFO) << "Starting TLS handshake with " << hostname; |
| 158 | |
| 159 | if (ssl_adapter_->StartSSL(hostname) != 0) { |
| 160 | return -1; |
| 161 | } |
Tommi | bdb867f | 2024-03-18 09:20:14 | [diff] [blame] | 162 | } |
Tommi | bdb867f | 2024-03-18 09:20:14 | [diff] [blame] | 163 | |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 164 | return rv; |
Tommi | bdb867f | 2024-03-18 09:20:14 | [diff] [blame] | 165 | } |
Tommi | bdb867f | 2024-03-18 09:20:14 | [diff] [blame] | 166 | }; |
| 167 | |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 168 | class SSLAdapterTestDummyServer : public SSLAdapterTestDummy { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 169 | public: |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 170 | explicit SSLAdapterTestDummyServer(const rtc::KeyParams& key_params) |
| 171 | : SSLAdapterTestDummy(), |
| 172 | ssl_identity_(rtc::SSLIdentity::Create(GetHostname(), key_params)) { |
| 173 | socket_->Listen(1); |
| 174 | socket_->SignalReadEvent.connect(this, |
| 175 | &SSLAdapterTestDummyServer::OnReadEvent); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 176 | |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 177 | RTC_LOG(LS_INFO) << "TCP server listening on " |
| 178 | << socket_->GetLocalAddress().ToString(); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 179 | } |
| 180 | |
Evan Shrubsole | 64b076f4 | 2025-03-12 12:56:28 | [diff] [blame] | 181 | webrtc::SocketAddress GetAddress() const { |
| 182 | return socket_->GetLocalAddress(); |
| 183 | } |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 184 | |
| 185 | std::string GetHostname() const { |
| 186 | // Since we don't have a real certificate anyway, the value here doesn't |
| 187 | // really matter. |
| 188 | return "example.com"; |
| 189 | } |
| 190 | |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 191 | protected: |
Evan Shrubsole | 03b6880 | 2025-03-18 12:23:05 | [diff] [blame] | 192 | void OnReadEvent(webrtc::Socket* socket) { |
Evan Shrubsole | eb835d0 | 2025-03-12 09:41:06 | [diff] [blame] | 193 | CreateSSLAdapter(socket_->Accept(nullptr), webrtc::SSL_SERVER); |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 194 | ssl_adapter_->SetIdentity(ssl_identity_->Clone()); |
| 195 | if (ssl_adapter_->StartSSL(GetHostname()) != 0) { |
| 196 | RTC_LOG(LS_ERROR) << "Starting SSL from server failed."; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | private: |
jbauch | 555604a | 2016-04-26 10:13:22 | [diff] [blame] | 201 | std::unique_ptr<rtc::SSLIdentity> ssl_identity_; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 202 | }; |
| 203 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 13:11:12 | [diff] [blame] | 204 | class SSLAdapterTestBase : public ::testing::Test, public sigslot::has_slots<> { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 205 | public: |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 206 | explicit SSLAdapterTestBase(const rtc::KeyParams& key_params) |
Evan Shrubsole | 03b6880 | 2025-03-18 12:23:05 | [diff] [blame] | 207 | : vss_(new webrtc::VirtualSocketServer()), |
nisse | 7eaa4ea | 2017-05-08 12:25:41 | [diff] [blame] | 208 | thread_(vss_.get()), |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 209 | server_(new SSLAdapterTestDummyServer(key_params)), |
| 210 | client_(new SSLAdapterTestDummyClient()), |
Evan Shrubsole | d959303 | 2025-01-17 13:19:45 | [diff] [blame] | 211 | handshake_wait_(webrtc::TimeDelta::Millis(kTimeout.ms())) {} |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 212 | |
Evan Shrubsole | d959303 | 2025-01-17 13:19:45 | [diff] [blame] | 213 | void SetHandshakeWait(int wait) { |
| 214 | handshake_wait_ = webrtc::TimeDelta::Millis(wait); |
| 215 | } |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 216 | |
Sergey Silkin | 9c147dd | 2018-09-12 10:45:38 | [diff] [blame] | 217 | void SetIgnoreBadCert(bool ignore_bad_cert) { |
| 218 | client_->SetIgnoreBadCert(ignore_bad_cert); |
Benjamin Wright | 6e9c3df | 2018-05-22 23:11:56 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void SetCertVerifier(rtc::SSLCertificateVerifier* ssl_cert_verifier) { |
| 222 | client_->SetCertVerifier(ssl_cert_verifier); |
| 223 | } |
| 224 | |
Sergey Silkin | 9c147dd | 2018-09-12 10:45:38 | [diff] [blame] | 225 | void SetAlpnProtocols(const std::vector<std::string>& protos) { |
| 226 | client_->SetAlpnProtocols(protos); |
| 227 | } |
| 228 | |
| 229 | void SetEllipticCurves(const std::vector<std::string>& curves) { |
| 230 | client_->SetEllipticCurves(curves); |
| 231 | } |
| 232 | |
Benjamin Wright | 6e9c3df | 2018-05-22 23:11:56 | [diff] [blame] | 233 | void SetMockCertVerifier(bool return_value) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 234 | auto mock_verifier = std::make_unique<MockCertVerifier>(); |
Benjamin Wright | 6e9c3df | 2018-05-22 23:11:56 | [diff] [blame] | 235 | EXPECT_CALL(*mock_verifier, Verify(_)).WillRepeatedly(Return(return_value)); |
| 236 | cert_verifier_ = |
| 237 | std::unique_ptr<rtc::SSLCertificateVerifier>(std::move(mock_verifier)); |
| 238 | |
Sergey Silkin | 9c147dd | 2018-09-12 10:45:38 | [diff] [blame] | 239 | SetIgnoreBadCert(false); |
Benjamin Wright | 6e9c3df | 2018-05-22 23:11:56 | [diff] [blame] | 240 | SetCertVerifier(cert_verifier_.get()); |
| 241 | } |
| 242 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 243 | void TestHandshake(bool expect_success) { |
| 244 | int rv; |
| 245 | |
| 246 | // The initial state is CS_CLOSED |
Evan Shrubsole | 03b6880 | 2025-03-18 12:23:05 | [diff] [blame] | 247 | ASSERT_EQ(webrtc::Socket::CS_CLOSED, client_->GetState()); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 248 | |
| 249 | rv = client_->Connect(server_->GetHostname(), server_->GetAddress()); |
| 250 | ASSERT_EQ(0, rv); |
| 251 | |
| 252 | // Now the state should be CS_CONNECTING |
Evan Shrubsole | 03b6880 | 2025-03-18 12:23:05 | [diff] [blame] | 253 | ASSERT_EQ(webrtc::Socket::CS_CONNECTING, client_->GetState()); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 254 | |
| 255 | if (expect_success) { |
| 256 | // If expecting success, the client should end up in the CS_CONNECTED |
| 257 | // state after handshake. |
Evan Shrubsole | d959303 | 2025-01-17 13:19:45 | [diff] [blame] | 258 | EXPECT_THAT(webrtc::WaitUntil([&] { return client_->GetState(); }, |
Evan Shrubsole | 03b6880 | 2025-03-18 12:23:05 | [diff] [blame] | 259 | ::testing::Eq(webrtc::Socket::CS_CONNECTED), |
Evan Shrubsole | d959303 | 2025-01-17 13:19:45 | [diff] [blame] | 260 | {.timeout = handshake_wait_}), |
| 261 | webrtc::IsRtcOk()); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 262 | |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 263 | RTC_LOG(LS_INFO) << "TLS handshake complete."; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 264 | |
| 265 | } else { |
| 266 | // On handshake failure the client should end up in the CS_CLOSED state. |
Evan Shrubsole | d959303 | 2025-01-17 13:19:45 | [diff] [blame] | 267 | EXPECT_THAT(webrtc::WaitUntil([&] { return client_->GetState(); }, |
Evan Shrubsole | 03b6880 | 2025-03-18 12:23:05 | [diff] [blame] | 268 | ::testing::Eq(webrtc::Socket::CS_CLOSED), |
Evan Shrubsole | d959303 | 2025-01-17 13:19:45 | [diff] [blame] | 269 | {.timeout = handshake_wait_}), |
| 270 | webrtc::IsRtcOk()); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 271 | |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 272 | RTC_LOG(LS_INFO) << "TLS handshake failed."; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 276 | void TestTransfer(absl::string_view message) { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 277 | int rv; |
| 278 | |
| 279 | rv = client_->Send(message); |
| 280 | ASSERT_EQ(static_cast<int>(message.length()), rv); |
| 281 | |
| 282 | // The server should have received the client's message. |
Evan Shrubsole | d959303 | 2025-01-17 13:19:45 | [diff] [blame] | 283 | EXPECT_THAT( |
| 284 | webrtc::WaitUntil([&] { return server_->GetReceivedData(); }, |
| 285 | ::testing::Eq(message), {.timeout = kTimeout}), |
| 286 | webrtc::IsRtcOk()); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 287 | |
| 288 | rv = server_->Send(message); |
| 289 | ASSERT_EQ(static_cast<int>(message.length()), rv); |
| 290 | |
| 291 | // The client should have received the server's message. |
Evan Shrubsole | d959303 | 2025-01-17 13:19:45 | [diff] [blame] | 292 | EXPECT_THAT( |
| 293 | webrtc::WaitUntil([&] { return client_->GetReceivedData(); }, |
| 294 | ::testing::Eq(message), {.timeout = kTimeout}), |
| 295 | webrtc::IsRtcOk()); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 296 | |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 297 | RTC_LOG(LS_INFO) << "Transfer complete."; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 298 | } |
| 299 | |
deadbeef | ed3b986 | 2017-06-02 17:33:16 | [diff] [blame] | 300 | protected: |
Evan Shrubsole | 03b6880 | 2025-03-18 12:23:05 | [diff] [blame] | 301 | std::unique_ptr<webrtc::VirtualSocketServer> vss_; |
nisse | 7eaa4ea | 2017-05-08 12:25:41 | [diff] [blame] | 302 | rtc::AutoSocketServerThread thread_; |
jbauch | 555604a | 2016-04-26 10:13:22 | [diff] [blame] | 303 | std::unique_ptr<SSLAdapterTestDummyServer> server_; |
| 304 | std::unique_ptr<SSLAdapterTestDummyClient> client_; |
Benjamin Wright | 6e9c3df | 2018-05-22 23:11:56 | [diff] [blame] | 305 | std::unique_ptr<rtc::SSLCertificateVerifier> cert_verifier_; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 306 | |
Evan Shrubsole | d959303 | 2025-01-17 13:19:45 | [diff] [blame] | 307 | webrtc::TimeDelta handshake_wait_; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 308 | }; |
| 309 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 12:08:59 | [diff] [blame] | 310 | class SSLAdapterTestTLS_RSA : public SSLAdapterTestBase { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 311 | public: |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 312 | SSLAdapterTestTLS_RSA() : SSLAdapterTestBase(rtc::KeyParams::RSA()) {} |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 313 | }; |
| 314 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 12:08:59 | [diff] [blame] | 315 | class SSLAdapterTestTLS_ECDSA : public SSLAdapterTestBase { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 | [diff] [blame] | 316 | public: |
Philipp Hancke | 5d6fa7d | 2024-07-30 23:42:49 | [diff] [blame] | 317 | SSLAdapterTestTLS_ECDSA() : SSLAdapterTestBase(rtc::KeyParams::ECDSA()) {} |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 12:08:59 | [diff] [blame] | 318 | }; |
| 319 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 12:08:59 | [diff] [blame] | 320 | // Test that handshake works, using RSA |
| 321 | TEST_F(SSLAdapterTestTLS_RSA, TestTLSConnect) { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 322 | TestHandshake(true); |
| 323 | } |
| 324 | |
Benjamin Wright | 6e9c3df | 2018-05-22 23:11:56 | [diff] [blame] | 325 | // Test that handshake works with a custom verifier that returns true. RSA. |
| 326 | TEST_F(SSLAdapterTestTLS_RSA, TestTLSConnectCustomCertVerifierSucceeds) { |
| 327 | SetMockCertVerifier(/*return_value=*/true); |
| 328 | TestHandshake(/*expect_success=*/true); |
| 329 | } |
| 330 | |
| 331 | // Test that handshake fails with a custom verifier that returns false. RSA. |
| 332 | TEST_F(SSLAdapterTestTLS_RSA, TestTLSConnectCustomCertVerifierFails) { |
| 333 | SetMockCertVerifier(/*return_value=*/false); |
| 334 | TestHandshake(/*expect_success=*/false); |
| 335 | } |
| 336 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 12:08:59 | [diff] [blame] | 337 | // Test that handshake works, using ECDSA |
| 338 | TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSConnect) { |
Benjamin Wright | 6e9c3df | 2018-05-22 23:11:56 | [diff] [blame] | 339 | SetMockCertVerifier(/*return_value=*/true); |
| 340 | TestHandshake(/*expect_success=*/true); |
| 341 | } |
| 342 | |
| 343 | // Test that handshake works with a custom verifier that returns true. ECDSA. |
| 344 | TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSConnectCustomCertVerifierSucceeds) { |
| 345 | SetMockCertVerifier(/*return_value=*/true); |
| 346 | TestHandshake(/*expect_success=*/true); |
| 347 | } |
| 348 | |
| 349 | // Test that handshake fails with a custom verifier that returns false. ECDSA. |
| 350 | TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSConnectCustomCertVerifierFails) { |
| 351 | SetMockCertVerifier(/*return_value=*/false); |
| 352 | TestHandshake(/*expect_success=*/false); |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 12:08:59 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // Test transfer between client and server, using RSA |
| 356 | TEST_F(SSLAdapterTestTLS_RSA, TestTLSTransfer) { |
| 357 | TestHandshake(true); |
| 358 | TestTransfer("Hello, world!"); |
| 359 | } |
| 360 | |
Benjamin Wright | 6e9c3df | 2018-05-22 23:11:56 | [diff] [blame] | 361 | // Test transfer between client and server, using RSA with custom cert verifier. |
| 362 | TEST_F(SSLAdapterTestTLS_RSA, TestTLSTransferCustomCertVerifier) { |
| 363 | SetMockCertVerifier(/*return_value=*/true); |
| 364 | TestHandshake(/*expect_success=*/true); |
| 365 | TestTransfer("Hello, world!"); |
| 366 | } |
| 367 | |
deadbeef | ed3b986 | 2017-06-02 17:33:16 | [diff] [blame] | 368 | TEST_F(SSLAdapterTestTLS_RSA, TestTLSTransferWithBlockedSocket) { |
| 369 | TestHandshake(true); |
| 370 | |
| 371 | // Tell the underlying socket to simulate being blocked. |
| 372 | vss_->SetSendingBlocked(true); |
| 373 | |
| 374 | std::string expected; |
| 375 | int rv; |
| 376 | // Send messages until the SSL socket adapter starts applying backpressure. |
| 377 | // Note that this may not occur immediately since there may be some amount of |
| 378 | // intermediate buffering (either in our code or in BoringSSL). |
| 379 | for (int i = 0; i < 1024; ++i) { |
| 380 | std::string message = "Hello, world: " + rtc::ToString(i); |
| 381 | rv = client_->Send(message); |
| 382 | if (rv != static_cast<int>(message.size())) { |
| 383 | // This test assumes either the whole message or none of it is sent. |
| 384 | ASSERT_EQ(-1, rv); |
| 385 | break; |
| 386 | } |
| 387 | expected += message; |
| 388 | } |
| 389 | // Assert that the loop above exited due to Send returning -1. |
| 390 | ASSERT_EQ(-1, rv); |
| 391 | |
| 392 | // Try sending another message while blocked. -1 should be returned again and |
| 393 | // it shouldn't end up received by the server later. |
| 394 | EXPECT_EQ(-1, client_->Send("Never sent")); |
| 395 | |
| 396 | // Unblock the underlying socket. All of the buffered messages should be sent |
| 397 | // without any further action. |
| 398 | vss_->SetSendingBlocked(false); |
Evan Shrubsole | d959303 | 2025-01-17 13:19:45 | [diff] [blame] | 399 | EXPECT_THAT(webrtc::WaitUntil([&] { return server_->GetReceivedData(); }, |
| 400 | ::testing::Eq(expected), {.timeout = kTimeout}), |
| 401 | webrtc::IsRtcOk()); |
deadbeef | ed3b986 | 2017-06-02 17:33:16 | [diff] [blame] | 402 | |
| 403 | // Send another message. This previously wasn't working |
| 404 | std::string final_message = "Fin."; |
| 405 | expected += final_message; |
| 406 | EXPECT_EQ(static_cast<int>(final_message.size()), |
| 407 | client_->Send(final_message)); |
Evan Shrubsole | d959303 | 2025-01-17 13:19:45 | [diff] [blame] | 408 | EXPECT_THAT(webrtc::WaitUntil([&] { return server_->GetReceivedData(); }, |
| 409 | ::testing::Eq(expected), {.timeout = kTimeout}), |
| 410 | webrtc::IsRtcOk()); |
deadbeef | ed3b986 | 2017-06-02 17:33:16 | [diff] [blame] | 411 | } |
| 412 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 12:08:59 | [diff] [blame] | 413 | // Test transfer between client and server, using ECDSA |
| 414 | TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSTransfer) { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 | [diff] [blame] | 415 | TestHandshake(true); |
| 416 | TestTransfer("Hello, world!"); |
| 417 | } |
| 418 | |
Benjamin Wright | 6e9c3df | 2018-05-22 23:11:56 | [diff] [blame] | 419 | // Test transfer between client and server, using ECDSA with custom cert |
| 420 | // verifier. |
| 421 | TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSTransferCustomCertVerifier) { |
| 422 | SetMockCertVerifier(/*return_value=*/true); |
| 423 | TestHandshake(/*expect_success=*/true); |
| 424 | TestTransfer("Hello, world!"); |
| 425 | } |
| 426 | |
Diogo Real | 1dca9d5 | 2017-08-29 19:18:32 | [diff] [blame] | 427 | // Test transfer using ALPN with protos as h2 and http/1.1 |
| 428 | TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSALPN) { |
| 429 | std::vector<std::string> alpn_protos{"h2", "http/1.1"}; |
| 430 | SetAlpnProtocols(alpn_protos); |
| 431 | TestHandshake(true); |
| 432 | TestTransfer("Hello, world!"); |
| 433 | } |
| 434 | |
Diogo Real | 7bd1f1b | 2017-09-08 19:50:41 | [diff] [blame] | 435 | // Test transfer with TLS Elliptic curves set to "X25519:P-256:P-384:P-521" |
| 436 | TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSEllipticCurves) { |
| 437 | std::vector<std::string> elliptic_curves{"X25519", "P-256", "P-384", "P-521"}; |
| 438 | SetEllipticCurves(elliptic_curves); |
| 439 | TestHandshake(true); |
| 440 | TestTransfer("Hello, world!"); |
| 441 | } |