blob: 130cf1f08ee5e7f61566ab53cde5abaef921645f [file] [log] [blame]
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +00001/*
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
jbauch555604a2016-04-26 03:13:22 -070011#include <memory>
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +000012#include <string>
Benjamin Wright6e9c3df2018-05-22 16:11:56 -070013#include <utility>
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +000014
Karl Wiberg918f50c2018-07-05 11:40:33 +020015#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/gunit.h"
17#include "rtc_base/ipaddress.h"
18#include "rtc_base/socketstream.h"
19#include "rtc_base/ssladapter.h"
20#include "rtc_base/sslidentity.h"
21#include "rtc_base/sslstreamadapter.h"
22#include "rtc_base/stream.h"
23#include "rtc_base/stringencode.h"
24#include "rtc_base/virtualsocketserver.h"
Benjamin Wright6e9c3df2018-05-22 16:11:56 -070025#include "test/gmock.h"
26
27using ::testing::_;
28using ::testing::Return;
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +000029
30static const int kTimeout = 5000;
31
32static rtc::AsyncSocket* CreateSocket(const rtc::SSLMode& ssl_mode) {
33 rtc::SocketAddress address(rtc::IPAddress(INADDR_ANY), 0);
34
Yves Gerey665174f2018-06-19 15:03:05 +020035 rtc::AsyncSocket* socket =
36 rtc::Thread::Current()->socketserver()->CreateAsyncSocket(
37 address.family(),
38 (ssl_mode == rtc::SSL_MODE_DTLS) ? SOCK_DGRAM : SOCK_STREAM);
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +000039 socket->Bind(address);
40
41 return socket;
42}
43
44static std::string GetSSLProtocolName(const rtc::SSLMode& ssl_mode) {
45 return (ssl_mode == rtc::SSL_MODE_DTLS) ? "DTLS" : "TLS";
46}
47
Benjamin Wright6e9c3df2018-05-22 16:11:56 -070048// Simple mock for the certificate verifier.
49class MockCertVerifier : public rtc::SSLCertificateVerifier {
50 public:
51 virtual ~MockCertVerifier() = default;
52 MOCK_METHOD1(Verify, bool(const rtc::SSLCertificate&));
53};
54
55// TODO(benwright) - Move to using INSTANTIATE_TEST_CASE_P instead of using
56// duplicate test cases for simple parameter changes.
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +000057class SSLAdapterTestDummyClient : public sigslot::has_slots<> {
58 public:
59 explicit SSLAdapterTestDummyClient(const rtc::SSLMode& ssl_mode)
60 : ssl_mode_(ssl_mode) {
61 rtc::AsyncSocket* socket = CreateSocket(ssl_mode_);
62
63 ssl_adapter_.reset(rtc::SSLAdapter::Create(socket));
64
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +000065 ssl_adapter_->SetMode(ssl_mode_);
66
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +000067 // Ignore any certificate errors for the purpose of testing.
68 // Note: We do this only because we don't have a real certificate.
69 // NEVER USE THIS IN PRODUCTION CODE!
Diogo Real4f085432018-09-11 16:00:22 -070070 ssl_config_.tls_cert_policy =
71 rtc::TlsCertPolicy::TLS_CERT_POLICY_INSECURE_NO_CHECK;
72 ssl_adapter_->SetSSLConfig(ssl_config_);
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +000073
Yves Gerey665174f2018-06-19 15:03:05 +020074 ssl_adapter_->SignalReadEvent.connect(
75 this, &SSLAdapterTestDummyClient::OnSSLAdapterReadEvent);
76 ssl_adapter_->SignalCloseEvent.connect(
77 this, &SSLAdapterTestDummyClient::OnSSLAdapterCloseEvent);
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +000078 }
79
Diogo Real4f085432018-09-11 16:00:22 -070080 void SetTlsCertPolicy(rtc::TlsCertPolicy tls_cert_policy) {
81 ssl_config_.tls_cert_policy = tls_cert_policy;
82 ssl_adapter_->SetSSLConfig(ssl_config_);
83 }
84
85 void SetEnableOcspStapling(bool enable_ocsp_stapling) {
86 ssl_config_.enable_ocsp_stapling = enable_ocsp_stapling;
87 ssl_adapter_->SetSSLConfig(ssl_config_);
88 }
89
90 void SetEnableSignedCertTimestamp(bool enable_signed_cert_timestamp) {
91 ssl_config_.enable_signed_cert_timestamp = enable_signed_cert_timestamp;
92 ssl_adapter_->SetSSLConfig(ssl_config_);
93 }
94
95 void SetEnableTlsChannelId(bool enable_tls_channel_id) {
96 ssl_config_.enable_tls_channel_id = enable_tls_channel_id;
97 ssl_adapter_->SetSSLConfig(ssl_config_);
98 }
99
100 void SetEnableGrease(bool enable_grease) {
101 ssl_config_.enable_grease = enable_grease;
102 ssl_adapter_->SetSSLConfig(ssl_config_);
103 }
104
105 void SetMaxSslVersion(const absl::optional<int>& max_ssl_version) {
106 ssl_config_.max_ssl_version = max_ssl_version;
107 ssl_adapter_->SetSSLConfig(ssl_config_);
108 }
109
110 void SetAlpnProtocols(
111 const absl::optional<std::vector<std::string>>& tls_alpn_protocols) {
112 ssl_config_.tls_alpn_protocols = tls_alpn_protocols;
113 ssl_adapter_->SetSSLConfig(ssl_config_);
114 }
115
116 void SetEllipticCurves(
117 const absl::optional<std::vector<std::string>>& tls_elliptic_curves) {
118 ssl_config_.tls_elliptic_curves = tls_elliptic_curves;
119 ssl_adapter_->SetSSLConfig(ssl_config_);
Benjamin Wright6e9c3df2018-05-22 16:11:56 -0700120 }
121
122 void SetCertVerifier(rtc::SSLCertificateVerifier* ssl_cert_verifier) {
123 ssl_adapter_->SetCertVerifier(ssl_cert_verifier);
124 }
125
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +0000126 rtc::SocketAddress GetAddress() const {
127 return ssl_adapter_->GetLocalAddress();
128 }
129
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000130 rtc::AsyncSocket::ConnState GetState() const {
131 return ssl_adapter_->GetState();
132 }
133
Yves Gerey665174f2018-06-19 15:03:05 +0200134 const std::string& GetReceivedData() const { return data_; }
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000135
136 int Connect(const std::string& hostname, const rtc::SocketAddress& address) {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200137 RTC_LOG(LS_INFO) << "Initiating connection with " << address.ToString();
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000138
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +0000139 int rv = ssl_adapter_->Connect(address);
140
141 if (rv == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100142 RTC_LOG(LS_INFO) << "Starting " << GetSSLProtocolName(ssl_mode_)
143 << " handshake with " << hostname;
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +0000144
145 if (ssl_adapter_->StartSSL(hostname.c_str(), false) != 0) {
146 return -1;
147 }
148 }
149
150 return rv;
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000151 }
152
Yves Gerey665174f2018-06-19 15:03:05 +0200153 int Close() { return ssl_adapter_->Close(); }
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000154
155 int Send(const std::string& message) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100156 RTC_LOG(LS_INFO) << "Client sending '" << message << "'";
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000157
158 return ssl_adapter_->Send(message.data(), message.length());
159 }
160
161 void OnSSLAdapterReadEvent(rtc::AsyncSocket* socket) {
162 char buffer[4096] = "";
163
164 // Read data received from the server and store it in our internal buffer.
Stefan Holmer9131efd2016-05-23 18:19:26 +0200165 int read = socket->Recv(buffer, sizeof(buffer) - 1, nullptr);
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000166 if (read != -1) {
167 buffer[read] = '\0';
168
Mirko Bonadei675513b2017-11-09 11:09:25 +0100169 RTC_LOG(LS_INFO) << "Client received '" << buffer << "'";
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000170
171 data_ += buffer;
172 }
173 }
174
175 void OnSSLAdapterCloseEvent(rtc::AsyncSocket* socket, int error) {
176 // OpenSSLAdapter signals handshake failure with a close event, but without
177 // closing the socket! Let's close the socket here. This way GetState() can
178 // return CS_CLOSED after failure.
179 if (socket->GetState() != rtc::AsyncSocket::CS_CLOSED) {
180 socket->Close();
181 }
182 }
183
184 private:
185 const rtc::SSLMode ssl_mode_;
186
jbauch555604a2016-04-26 03:13:22 -0700187 std::unique_ptr<rtc::SSLAdapter> ssl_adapter_;
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000188
Diogo Real4f085432018-09-11 16:00:22 -0700189 rtc::SSLConfig ssl_config_;
190
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000191 std::string data_;
192};
193
194class SSLAdapterTestDummyServer : public sigslot::has_slots<> {
195 public:
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200196 explicit SSLAdapterTestDummyServer(const rtc::SSLMode& ssl_mode,
torbjorng4e572472015-10-08 09:42:49 -0700197 const rtc::KeyParams& key_params)
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000198 : ssl_mode_(ssl_mode) {
199 // Generate a key pair and a certificate for this host.
torbjorng4e572472015-10-08 09:42:49 -0700200 ssl_identity_.reset(rtc::SSLIdentity::Generate(GetHostname(), key_params));
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000201
202 server_socket_.reset(CreateSocket(ssl_mode_));
203
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +0000204 if (ssl_mode_ == rtc::SSL_MODE_TLS) {
Yves Gerey665174f2018-06-19 15:03:05 +0200205 server_socket_->SignalReadEvent.connect(
206 this, &SSLAdapterTestDummyServer::OnServerSocketReadEvent);
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000207
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +0000208 server_socket_->Listen(1);
209 }
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000210
Mirko Bonadei675513b2017-11-09 11:09:25 +0100211 RTC_LOG(LS_INFO) << ((ssl_mode_ == rtc::SSL_MODE_DTLS) ? "UDP" : "TCP")
212 << " server listening on "
Jonas Olssonabbe8412018-04-03 13:40:05 +0200213 << server_socket_->GetLocalAddress().ToString();
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000214 }
215
216 rtc::SocketAddress GetAddress() const {
217 return server_socket_->GetLocalAddress();
218 }
219
220 std::string GetHostname() const {
221 // Since we don't have a real certificate anyway, the value here doesn't
222 // really matter.
223 return "example.com";
224 }
225
Yves Gerey665174f2018-06-19 15:03:05 +0200226 const std::string& GetReceivedData() const { return data_; }
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000227
228 int Send(const std::string& message) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800229 if (ssl_stream_adapter_ == nullptr ||
230 ssl_stream_adapter_->GetState() != rtc::SS_OPEN) {
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000231 // No connection yet.
232 return -1;
233 }
234
Mirko Bonadei675513b2017-11-09 11:09:25 +0100235 RTC_LOG(LS_INFO) << "Server sending '" << message << "'";
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000236
237 size_t written;
238 int error;
239
Yves Gerey665174f2018-06-19 15:03:05 +0200240 rtc::StreamResult r = ssl_stream_adapter_->Write(
241 message.data(), message.length(), &written, &error);
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000242 if (r == rtc::SR_SUCCESS) {
243 return written;
244 } else {
245 return -1;
246 }
247 }
248
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +0000249 void AcceptConnection(const rtc::SocketAddress& address) {
250 // Only a single connection is supported.
deadbeef37f5ecf2017-02-27 14:06:41 -0800251 ASSERT_TRUE(ssl_stream_adapter_ == nullptr);
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +0000252
253 // This is only for DTLS.
254 ASSERT_EQ(rtc::SSL_MODE_DTLS, ssl_mode_);
255
256 // Transfer ownership of the socket to the SSLStreamAdapter object.
257 rtc::AsyncSocket* socket = server_socket_.release();
258
259 socket->Connect(address);
260
261 DoHandshake(socket);
262 }
263
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000264 void OnServerSocketReadEvent(rtc::AsyncSocket* socket) {
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +0000265 // Only a single connection is supported.
deadbeef37f5ecf2017-02-27 14:06:41 -0800266 ASSERT_TRUE(ssl_stream_adapter_ == nullptr);
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000267
deadbeef37f5ecf2017-02-27 14:06:41 -0800268 DoHandshake(server_socket_->Accept(nullptr));
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000269 }
270
271 void OnSSLStreamAdapterEvent(rtc::StreamInterface* stream, int sig, int err) {
272 if (sig & rtc::SE_READ) {
273 char buffer[4096] = "";
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000274 size_t read;
275 int error;
276
277 // Read data received from the client and store it in our internal
278 // buffer.
deadbeefed3b9862017-06-02 10:33:16 -0700279 rtc::StreamResult r =
280 stream->Read(buffer, sizeof(buffer) - 1, &read, &error);
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000281 if (r == rtc::SR_SUCCESS) {
282 buffer[read] = '\0';
Mirko Bonadei675513b2017-11-09 11:09:25 +0100283 RTC_LOG(LS_INFO) << "Server received '" << buffer << "'";
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000284 data_ += buffer;
285 }
286 }
287 }
288
289 private:
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +0000290 void DoHandshake(rtc::AsyncSocket* socket) {
291 rtc::SocketStream* stream = new rtc::SocketStream(socket);
292
293 ssl_stream_adapter_.reset(rtc::SSLStreamAdapter::Create(stream));
294
295 ssl_stream_adapter_->SetMode(ssl_mode_);
296 ssl_stream_adapter_->SetServerRole();
297
298 // SSLStreamAdapter is normally used for peer-to-peer communication, but
299 // here we're testing communication between a client and a server
300 // (e.g. a WebRTC-based application and an RFC 5766 TURN server), where
301 // clients are not required to provide a certificate during handshake.
302 // Accordingly, we must disable client authentication here.
303 ssl_stream_adapter_->set_client_auth_enabled(false);
304
305 ssl_stream_adapter_->SetIdentity(ssl_identity_->GetReference());
306
307 // Set a bogus peer certificate digest.
308 unsigned char digest[20];
309 size_t digest_len = sizeof(digest);
310 ssl_stream_adapter_->SetPeerCertificateDigest(rtc::DIGEST_SHA_1, digest,
Yves Gerey665174f2018-06-19 15:03:05 +0200311 digest_len);
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +0000312
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700313 ssl_stream_adapter_->StartSSL();
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +0000314
Yves Gerey665174f2018-06-19 15:03:05 +0200315 ssl_stream_adapter_->SignalEvent.connect(
316 this, &SSLAdapterTestDummyServer::OnSSLStreamAdapterEvent);
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +0000317 }
318
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000319 const rtc::SSLMode ssl_mode_;
320
jbauch555604a2016-04-26 03:13:22 -0700321 std::unique_ptr<rtc::AsyncSocket> server_socket_;
322 std::unique_ptr<rtc::SSLStreamAdapter> ssl_stream_adapter_;
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000323
jbauch555604a2016-04-26 03:13:22 -0700324 std::unique_ptr<rtc::SSLIdentity> ssl_identity_;
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000325
326 std::string data_;
327};
328
Yves Gerey665174f2018-06-19 15:03:05 +0200329class SSLAdapterTestBase : public testing::Test, public sigslot::has_slots<> {
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000330 public:
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200331 explicit SSLAdapterTestBase(const rtc::SSLMode& ssl_mode,
torbjorng4e572472015-10-08 09:42:49 -0700332 const rtc::KeyParams& key_params)
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000333 : ssl_mode_(ssl_mode),
deadbeef98e186c2017-05-16 18:00:06 -0700334 vss_(new rtc::VirtualSocketServer()),
nisse7eaa4ea2017-05-08 05:25:41 -0700335 thread_(vss_.get()),
torbjorng4e572472015-10-08 09:42:49 -0700336 server_(new SSLAdapterTestDummyServer(ssl_mode_, key_params)),
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000337 client_(new SSLAdapterTestDummyClient(ssl_mode_)),
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200338 handshake_wait_(kTimeout) {}
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000339
Yves Gerey665174f2018-06-19 15:03:05 +0200340 void SetHandshakeWait(int wait) { handshake_wait_ = wait; }
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000341
Diogo Real4f085432018-09-11 16:00:22 -0700342 void SetTlsCertPolicy(rtc::TlsCertPolicy tls_cert_policy) {
343 client_->SetTlsCertPolicy(tls_cert_policy);
344 }
345
346 void SetEnableOcspStapling(bool enable_ocsp_stapling) {
347 client_->SetEnableOcspStapling(enable_ocsp_stapling);
348 }
349
350 void SetEnableSignedCertTimestamp(bool enable_signed_cert_timestamp) {
351 client_->SetEnableSignedCertTimestamp(enable_signed_cert_timestamp);
352 }
353
354 void SetEnableTlsChannelId(bool enable_tls_channel_id) {
355 client_->SetEnableTlsChannelId(enable_tls_channel_id);
356 }
357
358 void SetEnableGrease(bool enable_grease) {
359 client_->SetEnableGrease(enable_grease);
360 }
361
362 void SetMaxSslVersion(const absl::optional<int>& max_ssl_version) {
363 client_->SetMaxSslVersion(max_ssl_version);
364 }
365
366 void SetAlpnProtocols(
367 const absl::optional<std::vector<std::string>>& tls_alpn_protocols) {
368 client_->SetAlpnProtocols(tls_alpn_protocols);
369 }
370
371 void SetEllipticCurves(
372 const absl::optional<std::vector<std::string>>& tls_elliptic_curves) {
373 client_->SetEllipticCurves(tls_elliptic_curves);
Benjamin Wright6e9c3df2018-05-22 16:11:56 -0700374 }
375
376 void SetCertVerifier(rtc::SSLCertificateVerifier* ssl_cert_verifier) {
377 client_->SetCertVerifier(ssl_cert_verifier);
378 }
379
Benjamin Wright6e9c3df2018-05-22 16:11:56 -0700380 void SetMockCertVerifier(bool return_value) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200381 auto mock_verifier = absl::make_unique<MockCertVerifier>();
Benjamin Wright6e9c3df2018-05-22 16:11:56 -0700382 EXPECT_CALL(*mock_verifier, Verify(_)).WillRepeatedly(Return(return_value));
383 cert_verifier_ =
384 std::unique_ptr<rtc::SSLCertificateVerifier>(std::move(mock_verifier));
385
Diogo Real4f085432018-09-11 16:00:22 -0700386 SetTlsCertPolicy(rtc::TlsCertPolicy::TLS_CERT_POLICY_SECURE);
Benjamin Wright6e9c3df2018-05-22 16:11:56 -0700387 SetCertVerifier(cert_verifier_.get());
388 }
389
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000390 void TestHandshake(bool expect_success) {
391 int rv;
392
393 // The initial state is CS_CLOSED
394 ASSERT_EQ(rtc::AsyncSocket::CS_CLOSED, client_->GetState());
395
396 rv = client_->Connect(server_->GetHostname(), server_->GetAddress());
397 ASSERT_EQ(0, rv);
398
399 // Now the state should be CS_CONNECTING
400 ASSERT_EQ(rtc::AsyncSocket::CS_CONNECTING, client_->GetState());
401
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +0000402 if (ssl_mode_ == rtc::SSL_MODE_DTLS) {
403 // For DTLS, call AcceptConnection() with the client's address.
404 server_->AcceptConnection(client_->GetAddress());
405 }
406
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000407 if (expect_success) {
408 // If expecting success, the client should end up in the CS_CONNECTED
409 // state after handshake.
410 EXPECT_EQ_WAIT(rtc::AsyncSocket::CS_CONNECTED, client_->GetState(),
Yves Gerey665174f2018-06-19 15:03:05 +0200411 handshake_wait_);
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000412
Mirko Bonadei675513b2017-11-09 11:09:25 +0100413 RTC_LOG(LS_INFO) << GetSSLProtocolName(ssl_mode_)
414 << " handshake complete.";
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000415
416 } else {
417 // On handshake failure the client should end up in the CS_CLOSED state.
418 EXPECT_EQ_WAIT(rtc::AsyncSocket::CS_CLOSED, client_->GetState(),
Yves Gerey665174f2018-06-19 15:03:05 +0200419 handshake_wait_);
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000420
Mirko Bonadei675513b2017-11-09 11:09:25 +0100421 RTC_LOG(LS_INFO) << GetSSLProtocolName(ssl_mode_) << " handshake failed.";
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000422 }
423 }
424
425 void TestTransfer(const std::string& message) {
426 int rv;
427
428 rv = client_->Send(message);
429 ASSERT_EQ(static_cast<int>(message.length()), rv);
430
431 // The server should have received the client's message.
432 EXPECT_EQ_WAIT(message, server_->GetReceivedData(), kTimeout);
433
434 rv = server_->Send(message);
435 ASSERT_EQ(static_cast<int>(message.length()), rv);
436
437 // The client should have received the server's message.
438 EXPECT_EQ_WAIT(message, client_->GetReceivedData(), kTimeout);
439
Mirko Bonadei675513b2017-11-09 11:09:25 +0100440 RTC_LOG(LS_INFO) << "Transfer complete.";
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000441 }
442
deadbeefed3b9862017-06-02 10:33:16 -0700443 protected:
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000444 const rtc::SSLMode ssl_mode_;
445
nisse7eaa4ea2017-05-08 05:25:41 -0700446 std::unique_ptr<rtc::VirtualSocketServer> vss_;
447 rtc::AutoSocketServerThread thread_;
jbauch555604a2016-04-26 03:13:22 -0700448 std::unique_ptr<SSLAdapterTestDummyServer> server_;
449 std::unique_ptr<SSLAdapterTestDummyClient> client_;
Benjamin Wright6e9c3df2018-05-22 16:11:56 -0700450 std::unique_ptr<rtc::SSLCertificateVerifier> cert_verifier_;
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000451
452 int handshake_wait_;
453};
454
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200455class SSLAdapterTestTLS_RSA : public SSLAdapterTestBase {
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000456 public:
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200457 SSLAdapterTestTLS_RSA()
torbjorng4e572472015-10-08 09:42:49 -0700458 : SSLAdapterTestBase(rtc::SSL_MODE_TLS, rtc::KeyParams::RSA()) {}
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000459};
460
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200461class SSLAdapterTestTLS_ECDSA : public SSLAdapterTestBase {
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +0000462 public:
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200463 SSLAdapterTestTLS_ECDSA()
torbjorng4e572472015-10-08 09:42:49 -0700464 : SSLAdapterTestBase(rtc::SSL_MODE_TLS, rtc::KeyParams::ECDSA()) {}
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200465};
466
467class SSLAdapterTestDTLS_RSA : public SSLAdapterTestBase {
468 public:
469 SSLAdapterTestDTLS_RSA()
torbjorng4e572472015-10-08 09:42:49 -0700470 : SSLAdapterTestBase(rtc::SSL_MODE_DTLS, rtc::KeyParams::RSA()) {}
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200471};
472
473class SSLAdapterTestDTLS_ECDSA : public SSLAdapterTestBase {
474 public:
475 SSLAdapterTestDTLS_ECDSA()
torbjorng4e572472015-10-08 09:42:49 -0700476 : SSLAdapterTestBase(rtc::SSL_MODE_DTLS, rtc::KeyParams::ECDSA()) {}
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +0000477};
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000478
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000479// Basic tests: TLS
480
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200481// Test that handshake works, using RSA
482TEST_F(SSLAdapterTestTLS_RSA, TestTLSConnect) {
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000483 TestHandshake(true);
484}
485
Benjamin Wright6e9c3df2018-05-22 16:11:56 -0700486// Test that handshake works with a custom verifier that returns true. RSA.
487TEST_F(SSLAdapterTestTLS_RSA, TestTLSConnectCustomCertVerifierSucceeds) {
488 SetMockCertVerifier(/*return_value=*/true);
489 TestHandshake(/*expect_success=*/true);
490}
491
492// Test that handshake fails with a custom verifier that returns false. RSA.
493TEST_F(SSLAdapterTestTLS_RSA, TestTLSConnectCustomCertVerifierFails) {
494 SetMockCertVerifier(/*return_value=*/false);
495 TestHandshake(/*expect_success=*/false);
496}
497
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200498// Test that handshake works, using ECDSA
499TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSConnect) {
Benjamin Wright6e9c3df2018-05-22 16:11:56 -0700500 SetMockCertVerifier(/*return_value=*/true);
501 TestHandshake(/*expect_success=*/true);
502}
503
504// Test that handshake works with a custom verifier that returns true. ECDSA.
505TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSConnectCustomCertVerifierSucceeds) {
506 SetMockCertVerifier(/*return_value=*/true);
507 TestHandshake(/*expect_success=*/true);
508}
509
510// Test that handshake fails with a custom verifier that returns false. ECDSA.
511TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSConnectCustomCertVerifierFails) {
512 SetMockCertVerifier(/*return_value=*/false);
513 TestHandshake(/*expect_success=*/false);
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200514}
515
516// Test transfer between client and server, using RSA
517TEST_F(SSLAdapterTestTLS_RSA, TestTLSTransfer) {
518 TestHandshake(true);
519 TestTransfer("Hello, world!");
520}
521
Benjamin Wright6e9c3df2018-05-22 16:11:56 -0700522// Test transfer between client and server, using RSA with custom cert verifier.
523TEST_F(SSLAdapterTestTLS_RSA, TestTLSTransferCustomCertVerifier) {
524 SetMockCertVerifier(/*return_value=*/true);
525 TestHandshake(/*expect_success=*/true);
526 TestTransfer("Hello, world!");
527}
528
deadbeefed3b9862017-06-02 10:33:16 -0700529TEST_F(SSLAdapterTestTLS_RSA, TestTLSTransferWithBlockedSocket) {
530 TestHandshake(true);
531
532 // Tell the underlying socket to simulate being blocked.
533 vss_->SetSendingBlocked(true);
534
535 std::string expected;
536 int rv;
537 // Send messages until the SSL socket adapter starts applying backpressure.
538 // Note that this may not occur immediately since there may be some amount of
539 // intermediate buffering (either in our code or in BoringSSL).
540 for (int i = 0; i < 1024; ++i) {
541 std::string message = "Hello, world: " + rtc::ToString(i);
542 rv = client_->Send(message);
543 if (rv != static_cast<int>(message.size())) {
544 // This test assumes either the whole message or none of it is sent.
545 ASSERT_EQ(-1, rv);
546 break;
547 }
548 expected += message;
549 }
550 // Assert that the loop above exited due to Send returning -1.
551 ASSERT_EQ(-1, rv);
552
553 // Try sending another message while blocked. -1 should be returned again and
554 // it shouldn't end up received by the server later.
555 EXPECT_EQ(-1, client_->Send("Never sent"));
556
557 // Unblock the underlying socket. All of the buffered messages should be sent
558 // without any further action.
559 vss_->SetSendingBlocked(false);
560 EXPECT_EQ_WAIT(expected, server_->GetReceivedData(), kTimeout);
561
562 // Send another message. This previously wasn't working
563 std::string final_message = "Fin.";
564 expected += final_message;
565 EXPECT_EQ(static_cast<int>(final_message.size()),
566 client_->Send(final_message));
567 EXPECT_EQ_WAIT(expected, server_->GetReceivedData(), kTimeout);
568}
569
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200570// Test transfer between client and server, using ECDSA
571TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSTransfer) {
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +0000572 TestHandshake(true);
573 TestTransfer("Hello, world!");
574}
575
Benjamin Wright6e9c3df2018-05-22 16:11:56 -0700576// Test transfer between client and server, using ECDSA with custom cert
577// verifier.
578TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSTransferCustomCertVerifier) {
579 SetMockCertVerifier(/*return_value=*/true);
580 TestHandshake(/*expect_success=*/true);
581 TestTransfer("Hello, world!");
582}
583
Diogo Real4f085432018-09-11 16:00:22 -0700584// Test transfer with OCSP stapling enabled
585TEST_F(SSLAdapterTestTLS_ECDSA, TestOcspStaplingEnabled) {
586 SetEnableOcspStapling(true);
587 TestHandshake(true);
588 TestTransfer("Hello, world!");
589}
590
591// Test transfer with OCSP stapling disabled
592TEST_F(SSLAdapterTestTLS_ECDSA, TestOcspStaplingDisabled) {
593 SetEnableOcspStapling(false);
594 TestHandshake(true);
595 TestTransfer("Hello, world!");
596}
597
598// test transfer with signed cert timestamp enabled
599TEST_F(SSLAdapterTestTLS_ECDSA, TestSignedCertTimestampEnabled) {
600 SetEnableSignedCertTimestamp(true);
601 TestHandshake(true);
602 TestTransfer("Hello, world!");
603}
604
605// Test transfer with signed cert timestamp disabled
606TEST_F(SSLAdapterTestTLS_ECDSA, TestSignedCertTimestampDisabled) {
607 SetEnableSignedCertTimestamp(false);
608 TestHandshake(true);
609 TestTransfer("Hello, world!");
610}
611
612// Test transfer with TLS channel ID enabled
613TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSChannelIdEnabled) {
614 SetEnableTlsChannelId(true);
615 TestHandshake(true);
616 TestTransfer("Hello, world!");
617}
618
619// Test transfer with TLS channel ID disabled
620TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSChannelIdDisabled) {
621 SetEnableTlsChannelId(false);
622 TestHandshake(true);
623 TestTransfer("Hello, world!");
624}
625
626// Test transfer with GREASE enabled
627TEST_F(SSLAdapterTestTLS_ECDSA, TestGreaseEnabled) {
628 SetEnableGrease(true);
629 TestHandshake(true);
630 TestTransfer("Hello, world!");
631}
632
633// Test transfer with GREASE disabled
634TEST_F(SSLAdapterTestTLS_ECDSA, TestGreaseDisabled) {
635 SetEnableGrease(false);
636 TestHandshake(true);
637 TestTransfer("Hello, world!");
638}
639
640// Test transfer with TLS1_3.
641TEST_F(SSLAdapterTestTLS_ECDSA, TestMaxSSLVersionTLS1_3) {
642 SetMaxSslVersion(0x0304 /* TLS1_3 */);
643 TestHandshake(true);
644 TestTransfer("Hello, world!");
645}
646
647// Test transfer with TLS1_2.
648TEST_F(SSLAdapterTestTLS_ECDSA, TestMaxSSLVersionTLS1_2) {
649 SetMaxSslVersion(0x0303 /* TLS1_2 */);
650 TestHandshake(true);
651 TestTransfer("Hello, world!");
652}
653
Diogo Real1dca9d52017-08-29 12:18:32 -0700654// Test transfer using ALPN with protos as h2 and http/1.1
655TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSALPN) {
656 std::vector<std::string> alpn_protos{"h2", "http/1.1"};
657 SetAlpnProtocols(alpn_protos);
658 TestHandshake(true);
659 TestTransfer("Hello, world!");
660}
661
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700662// Test transfer with TLS Elliptic curves set to "X25519:P-256:P-384:P-521"
663TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSEllipticCurves) {
664 std::vector<std::string> elliptic_curves{"X25519", "P-256", "P-384", "P-521"};
665 SetEllipticCurves(elliptic_curves);
666 TestHandshake(true);
667 TestTransfer("Hello, world!");
668}
669
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +0000670// Basic tests: DTLS
671
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200672// Test that handshake works, using RSA
673TEST_F(SSLAdapterTestDTLS_RSA, TestDTLSConnect) {
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +0000674 TestHandshake(true);
675}
676
Benjamin Wright6e9c3df2018-05-22 16:11:56 -0700677// Test that handshake works with a custom verifier that returns true. DTLS_RSA.
678TEST_F(SSLAdapterTestDTLS_RSA, TestDTLSConnectCustomCertVerifierSucceeds) {
679 SetMockCertVerifier(/*return_value=*/true);
680 TestHandshake(/*expect_success=*/true);
681}
682
683// Test that handshake fails with a custom verifier that returns false.
684// DTLS_RSA.
685TEST_F(SSLAdapterTestDTLS_RSA, TestTLSConnectCustomCertVerifierFails) {
686 SetMockCertVerifier(/*return_value=*/false);
687 TestHandshake(/*expect_success=*/false);
688}
689
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200690// Test that handshake works, using ECDSA
691TEST_F(SSLAdapterTestDTLS_ECDSA, TestDTLSConnect) {
692 TestHandshake(true);
693}
694
Benjamin Wright6e9c3df2018-05-22 16:11:56 -0700695// Test that handshake works with a custom verifier that returns true.
696// DTLS_ECDSA.
697TEST_F(SSLAdapterTestDTLS_ECDSA, TestDTLSConnectCustomCertVerifierSucceeds) {
698 SetMockCertVerifier(/*return_value=*/true);
699 TestHandshake(/*expect_success=*/true);
700}
701
702// Test that handshake fails with a custom verifier that returns false.
703// DTLS_ECDSA.
704TEST_F(SSLAdapterTestDTLS_ECDSA, TestTLSConnectCustomCertVerifierFails) {
705 SetMockCertVerifier(/*return_value=*/false);
706 TestHandshake(/*expect_success=*/false);
707}
708
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200709// Test transfer between client and server, using RSA
710TEST_F(SSLAdapterTestDTLS_RSA, TestDTLSTransfer) {
711 TestHandshake(true);
712 TestTransfer("Hello, world!");
713}
714
Benjamin Wright6e9c3df2018-05-22 16:11:56 -0700715// Test transfer between client and server, using RSA with custom cert verifier.
716TEST_F(SSLAdapterTestDTLS_RSA, TestDTLSTransferCustomCertVerifier) {
717 SetMockCertVerifier(/*return_value=*/true);
718 TestHandshake(/*expect_success=*/true);
719 TestTransfer("Hello, world!");
720}
721
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200722// Test transfer between client and server, using ECDSA
723TEST_F(SSLAdapterTestDTLS_ECDSA, TestDTLSTransfer) {
pthatcher@webrtc.orga9b1ec02014-12-29 23:00:14 +0000724 TestHandshake(true);
725 TestTransfer("Hello, world!");
726}
Benjamin Wright6e9c3df2018-05-22 16:11:56 -0700727
728// Test transfer between client and server, using ECDSA with custom cert
729// verifier.
730TEST_F(SSLAdapterTestDTLS_ECDSA, TestDTLSTransferCustomCertVerifier) {
731 SetMockCertVerifier(/*return_value=*/true);
732 TestHandshake(/*expect_success=*/true);
733 TestTransfer("Hello, world!");
734}