tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [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 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 11 | #include <memory> |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 12 | #include <string> |
Benjamin Wright | 6e9c3df | 2018-05-22 16:11:56 -0700 | [diff] [blame] | 13 | #include <utility> |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 14 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 15 | #include "absl/memory/memory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #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 Wright | 6e9c3df | 2018-05-22 16:11:56 -0700 | [diff] [blame] | 25 | #include "test/gmock.h" |
| 26 | |
| 27 | using ::testing::_; |
| 28 | using ::testing::Return; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 29 | |
| 30 | static const int kTimeout = 5000; |
| 31 | |
| 32 | static rtc::AsyncSocket* CreateSocket(const rtc::SSLMode& ssl_mode) { |
| 33 | rtc::SocketAddress address(rtc::IPAddress(INADDR_ANY), 0); |
| 34 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 35 | 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.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 39 | socket->Bind(address); |
| 40 | |
| 41 | return socket; |
| 42 | } |
| 43 | |
| 44 | static std::string GetSSLProtocolName(const rtc::SSLMode& ssl_mode) { |
| 45 | return (ssl_mode == rtc::SSL_MODE_DTLS) ? "DTLS" : "TLS"; |
| 46 | } |
| 47 | |
Benjamin Wright | 6e9c3df | 2018-05-22 16:11:56 -0700 | [diff] [blame] | 48 | // Simple mock for the certificate verifier. |
| 49 | class 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.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 57 | class 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.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 65 | ssl_adapter_->SetMode(ssl_mode_); |
| 66 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 67 | // 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 Real | 4f08543 | 2018-09-11 16:00:22 -0700 | [diff] [blame] | 70 | ssl_config_.tls_cert_policy = |
| 71 | rtc::TlsCertPolicy::TLS_CERT_POLICY_INSECURE_NO_CHECK; |
| 72 | ssl_adapter_->SetSSLConfig(ssl_config_); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 73 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 74 | ssl_adapter_->SignalReadEvent.connect( |
| 75 | this, &SSLAdapterTestDummyClient::OnSSLAdapterReadEvent); |
| 76 | ssl_adapter_->SignalCloseEvent.connect( |
| 77 | this, &SSLAdapterTestDummyClient::OnSSLAdapterCloseEvent); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Diogo Real | 4f08543 | 2018-09-11 16:00:22 -0700 | [diff] [blame] | 80 | 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 Wright | 6e9c3df | 2018-05-22 16:11:56 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void SetCertVerifier(rtc::SSLCertificateVerifier* ssl_cert_verifier) { |
| 123 | ssl_adapter_->SetCertVerifier(ssl_cert_verifier); |
| 124 | } |
| 125 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 126 | rtc::SocketAddress GetAddress() const { |
| 127 | return ssl_adapter_->GetLocalAddress(); |
| 128 | } |
| 129 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 130 | rtc::AsyncSocket::ConnState GetState() const { |
| 131 | return ssl_adapter_->GetState(); |
| 132 | } |
| 133 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 134 | const std::string& GetReceivedData() const { return data_; } |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 135 | |
| 136 | int Connect(const std::string& hostname, const rtc::SocketAddress& address) { |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 137 | RTC_LOG(LS_INFO) << "Initiating connection with " << address.ToString(); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 138 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 139 | int rv = ssl_adapter_->Connect(address); |
| 140 | |
| 141 | if (rv == 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 142 | RTC_LOG(LS_INFO) << "Starting " << GetSSLProtocolName(ssl_mode_) |
| 143 | << " handshake with " << hostname; |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 144 | |
| 145 | if (ssl_adapter_->StartSSL(hostname.c_str(), false) != 0) { |
| 146 | return -1; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return rv; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 153 | int Close() { return ssl_adapter_->Close(); } |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 154 | |
| 155 | int Send(const std::string& message) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 156 | RTC_LOG(LS_INFO) << "Client sending '" << message << "'"; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 157 | |
| 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 Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 165 | int read = socket->Recv(buffer, sizeof(buffer) - 1, nullptr); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 166 | if (read != -1) { |
| 167 | buffer[read] = '\0'; |
| 168 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 169 | RTC_LOG(LS_INFO) << "Client received '" << buffer << "'"; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 170 | |
| 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 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 187 | std::unique_ptr<rtc::SSLAdapter> ssl_adapter_; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 188 | |
Diogo Real | 4f08543 | 2018-09-11 16:00:22 -0700 | [diff] [blame] | 189 | rtc::SSLConfig ssl_config_; |
| 190 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 191 | std::string data_; |
| 192 | }; |
| 193 | |
| 194 | class SSLAdapterTestDummyServer : public sigslot::has_slots<> { |
| 195 | public: |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 196 | explicit SSLAdapterTestDummyServer(const rtc::SSLMode& ssl_mode, |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 197 | const rtc::KeyParams& key_params) |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 198 | : ssl_mode_(ssl_mode) { |
| 199 | // Generate a key pair and a certificate for this host. |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 200 | ssl_identity_.reset(rtc::SSLIdentity::Generate(GetHostname(), key_params)); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 201 | |
| 202 | server_socket_.reset(CreateSocket(ssl_mode_)); |
| 203 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 204 | if (ssl_mode_ == rtc::SSL_MODE_TLS) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 205 | server_socket_->SignalReadEvent.connect( |
| 206 | this, &SSLAdapterTestDummyServer::OnServerSocketReadEvent); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 207 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 208 | server_socket_->Listen(1); |
| 209 | } |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 210 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 211 | RTC_LOG(LS_INFO) << ((ssl_mode_ == rtc::SSL_MODE_DTLS) ? "UDP" : "TCP") |
| 212 | << " server listening on " |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 213 | << server_socket_->GetLocalAddress().ToString(); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 214 | } |
| 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 226 | const std::string& GetReceivedData() const { return data_; } |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 227 | |
| 228 | int Send(const std::string& message) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 229 | if (ssl_stream_adapter_ == nullptr || |
| 230 | ssl_stream_adapter_->GetState() != rtc::SS_OPEN) { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 231 | // No connection yet. |
| 232 | return -1; |
| 233 | } |
| 234 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 235 | RTC_LOG(LS_INFO) << "Server sending '" << message << "'"; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 236 | |
| 237 | size_t written; |
| 238 | int error; |
| 239 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 240 | rtc::StreamResult r = ssl_stream_adapter_->Write( |
| 241 | message.data(), message.length(), &written, &error); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 242 | if (r == rtc::SR_SUCCESS) { |
| 243 | return written; |
| 244 | } else { |
| 245 | return -1; |
| 246 | } |
| 247 | } |
| 248 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 249 | void AcceptConnection(const rtc::SocketAddress& address) { |
| 250 | // Only a single connection is supported. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 251 | ASSERT_TRUE(ssl_stream_adapter_ == nullptr); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 252 | |
| 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.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 264 | void OnServerSocketReadEvent(rtc::AsyncSocket* socket) { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 265 | // Only a single connection is supported. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 266 | ASSERT_TRUE(ssl_stream_adapter_ == nullptr); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 267 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 268 | DoHandshake(server_socket_->Accept(nullptr)); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void OnSSLStreamAdapterEvent(rtc::StreamInterface* stream, int sig, int err) { |
| 272 | if (sig & rtc::SE_READ) { |
| 273 | char buffer[4096] = ""; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 274 | size_t read; |
| 275 | int error; |
| 276 | |
| 277 | // Read data received from the client and store it in our internal |
| 278 | // buffer. |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 279 | rtc::StreamResult r = |
| 280 | stream->Read(buffer, sizeof(buffer) - 1, &read, &error); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 281 | if (r == rtc::SR_SUCCESS) { |
| 282 | buffer[read] = '\0'; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 283 | RTC_LOG(LS_INFO) << "Server received '" << buffer << "'"; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 284 | data_ += buffer; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | private: |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 290 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 311 | digest_len); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 312 | |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 313 | ssl_stream_adapter_->StartSSL(); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 314 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 315 | ssl_stream_adapter_->SignalEvent.connect( |
| 316 | this, &SSLAdapterTestDummyServer::OnSSLStreamAdapterEvent); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 317 | } |
| 318 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 319 | const rtc::SSLMode ssl_mode_; |
| 320 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 321 | std::unique_ptr<rtc::AsyncSocket> server_socket_; |
| 322 | std::unique_ptr<rtc::SSLStreamAdapter> ssl_stream_adapter_; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 323 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 324 | std::unique_ptr<rtc::SSLIdentity> ssl_identity_; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 325 | |
| 326 | std::string data_; |
| 327 | }; |
| 328 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 329 | class SSLAdapterTestBase : public testing::Test, public sigslot::has_slots<> { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 330 | public: |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 331 | explicit SSLAdapterTestBase(const rtc::SSLMode& ssl_mode, |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 332 | const rtc::KeyParams& key_params) |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 333 | : ssl_mode_(ssl_mode), |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 334 | vss_(new rtc::VirtualSocketServer()), |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 335 | thread_(vss_.get()), |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 336 | server_(new SSLAdapterTestDummyServer(ssl_mode_, key_params)), |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 337 | client_(new SSLAdapterTestDummyClient(ssl_mode_)), |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 338 | handshake_wait_(kTimeout) {} |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 339 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 340 | void SetHandshakeWait(int wait) { handshake_wait_ = wait; } |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 341 | |
Diogo Real | 4f08543 | 2018-09-11 16:00:22 -0700 | [diff] [blame] | 342 | 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 Wright | 6e9c3df | 2018-05-22 16:11:56 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | void SetCertVerifier(rtc::SSLCertificateVerifier* ssl_cert_verifier) { |
| 377 | client_->SetCertVerifier(ssl_cert_verifier); |
| 378 | } |
| 379 | |
Benjamin Wright | 6e9c3df | 2018-05-22 16:11:56 -0700 | [diff] [blame] | 380 | void SetMockCertVerifier(bool return_value) { |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 381 | auto mock_verifier = absl::make_unique<MockCertVerifier>(); |
Benjamin Wright | 6e9c3df | 2018-05-22 16:11:56 -0700 | [diff] [blame] | 382 | EXPECT_CALL(*mock_verifier, Verify(_)).WillRepeatedly(Return(return_value)); |
| 383 | cert_verifier_ = |
| 384 | std::unique_ptr<rtc::SSLCertificateVerifier>(std::move(mock_verifier)); |
| 385 | |
Diogo Real | 4f08543 | 2018-09-11 16:00:22 -0700 | [diff] [blame] | 386 | SetTlsCertPolicy(rtc::TlsCertPolicy::TLS_CERT_POLICY_SECURE); |
Benjamin Wright | 6e9c3df | 2018-05-22 16:11:56 -0700 | [diff] [blame] | 387 | SetCertVerifier(cert_verifier_.get()); |
| 388 | } |
| 389 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 390 | 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.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 402 | 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.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 407 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 411 | handshake_wait_); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 412 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 413 | RTC_LOG(LS_INFO) << GetSSLProtocolName(ssl_mode_) |
| 414 | << " handshake complete."; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 415 | |
| 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 419 | handshake_wait_); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 420 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 421 | RTC_LOG(LS_INFO) << GetSSLProtocolName(ssl_mode_) << " handshake failed."; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 422 | } |
| 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 440 | RTC_LOG(LS_INFO) << "Transfer complete."; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 441 | } |
| 442 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 443 | protected: |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 444 | const rtc::SSLMode ssl_mode_; |
| 445 | |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 446 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 447 | rtc::AutoSocketServerThread thread_; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 448 | std::unique_ptr<SSLAdapterTestDummyServer> server_; |
| 449 | std::unique_ptr<SSLAdapterTestDummyClient> client_; |
Benjamin Wright | 6e9c3df | 2018-05-22 16:11:56 -0700 | [diff] [blame] | 450 | std::unique_ptr<rtc::SSLCertificateVerifier> cert_verifier_; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 451 | |
| 452 | int handshake_wait_; |
| 453 | }; |
| 454 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 455 | class SSLAdapterTestTLS_RSA : public SSLAdapterTestBase { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 456 | public: |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 457 | SSLAdapterTestTLS_RSA() |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 458 | : SSLAdapterTestBase(rtc::SSL_MODE_TLS, rtc::KeyParams::RSA()) {} |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 459 | }; |
| 460 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 461 | class SSLAdapterTestTLS_ECDSA : public SSLAdapterTestBase { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 462 | public: |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 463 | SSLAdapterTestTLS_ECDSA() |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 464 | : SSLAdapterTestBase(rtc::SSL_MODE_TLS, rtc::KeyParams::ECDSA()) {} |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 465 | }; |
| 466 | |
| 467 | class SSLAdapterTestDTLS_RSA : public SSLAdapterTestBase { |
| 468 | public: |
| 469 | SSLAdapterTestDTLS_RSA() |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 470 | : SSLAdapterTestBase(rtc::SSL_MODE_DTLS, rtc::KeyParams::RSA()) {} |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 471 | }; |
| 472 | |
| 473 | class SSLAdapterTestDTLS_ECDSA : public SSLAdapterTestBase { |
| 474 | public: |
| 475 | SSLAdapterTestDTLS_ECDSA() |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 476 | : SSLAdapterTestBase(rtc::SSL_MODE_DTLS, rtc::KeyParams::ECDSA()) {} |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 477 | }; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 478 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 479 | // Basic tests: TLS |
| 480 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 481 | // Test that handshake works, using RSA |
| 482 | TEST_F(SSLAdapterTestTLS_RSA, TestTLSConnect) { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 483 | TestHandshake(true); |
| 484 | } |
| 485 | |
Benjamin Wright | 6e9c3df | 2018-05-22 16:11:56 -0700 | [diff] [blame] | 486 | // Test that handshake works with a custom verifier that returns true. RSA. |
| 487 | TEST_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. |
| 493 | TEST_F(SSLAdapterTestTLS_RSA, TestTLSConnectCustomCertVerifierFails) { |
| 494 | SetMockCertVerifier(/*return_value=*/false); |
| 495 | TestHandshake(/*expect_success=*/false); |
| 496 | } |
| 497 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 498 | // Test that handshake works, using ECDSA |
| 499 | TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSConnect) { |
Benjamin Wright | 6e9c3df | 2018-05-22 16:11:56 -0700 | [diff] [blame] | 500 | SetMockCertVerifier(/*return_value=*/true); |
| 501 | TestHandshake(/*expect_success=*/true); |
| 502 | } |
| 503 | |
| 504 | // Test that handshake works with a custom verifier that returns true. ECDSA. |
| 505 | TEST_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. |
| 511 | TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSConnectCustomCertVerifierFails) { |
| 512 | SetMockCertVerifier(/*return_value=*/false); |
| 513 | TestHandshake(/*expect_success=*/false); |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | // Test transfer between client and server, using RSA |
| 517 | TEST_F(SSLAdapterTestTLS_RSA, TestTLSTransfer) { |
| 518 | TestHandshake(true); |
| 519 | TestTransfer("Hello, world!"); |
| 520 | } |
| 521 | |
Benjamin Wright | 6e9c3df | 2018-05-22 16:11:56 -0700 | [diff] [blame] | 522 | // Test transfer between client and server, using RSA with custom cert verifier. |
| 523 | TEST_F(SSLAdapterTestTLS_RSA, TestTLSTransferCustomCertVerifier) { |
| 524 | SetMockCertVerifier(/*return_value=*/true); |
| 525 | TestHandshake(/*expect_success=*/true); |
| 526 | TestTransfer("Hello, world!"); |
| 527 | } |
| 528 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 529 | TEST_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 Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 570 | // Test transfer between client and server, using ECDSA |
| 571 | TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSTransfer) { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 572 | TestHandshake(true); |
| 573 | TestTransfer("Hello, world!"); |
| 574 | } |
| 575 | |
Benjamin Wright | 6e9c3df | 2018-05-22 16:11:56 -0700 | [diff] [blame] | 576 | // Test transfer between client and server, using ECDSA with custom cert |
| 577 | // verifier. |
| 578 | TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSTransferCustomCertVerifier) { |
| 579 | SetMockCertVerifier(/*return_value=*/true); |
| 580 | TestHandshake(/*expect_success=*/true); |
| 581 | TestTransfer("Hello, world!"); |
| 582 | } |
| 583 | |
Diogo Real | 4f08543 | 2018-09-11 16:00:22 -0700 | [diff] [blame] | 584 | // Test transfer with OCSP stapling enabled |
| 585 | TEST_F(SSLAdapterTestTLS_ECDSA, TestOcspStaplingEnabled) { |
| 586 | SetEnableOcspStapling(true); |
| 587 | TestHandshake(true); |
| 588 | TestTransfer("Hello, world!"); |
| 589 | } |
| 590 | |
| 591 | // Test transfer with OCSP stapling disabled |
| 592 | TEST_F(SSLAdapterTestTLS_ECDSA, TestOcspStaplingDisabled) { |
| 593 | SetEnableOcspStapling(false); |
| 594 | TestHandshake(true); |
| 595 | TestTransfer("Hello, world!"); |
| 596 | } |
| 597 | |
| 598 | // test transfer with signed cert timestamp enabled |
| 599 | TEST_F(SSLAdapterTestTLS_ECDSA, TestSignedCertTimestampEnabled) { |
| 600 | SetEnableSignedCertTimestamp(true); |
| 601 | TestHandshake(true); |
| 602 | TestTransfer("Hello, world!"); |
| 603 | } |
| 604 | |
| 605 | // Test transfer with signed cert timestamp disabled |
| 606 | TEST_F(SSLAdapterTestTLS_ECDSA, TestSignedCertTimestampDisabled) { |
| 607 | SetEnableSignedCertTimestamp(false); |
| 608 | TestHandshake(true); |
| 609 | TestTransfer("Hello, world!"); |
| 610 | } |
| 611 | |
| 612 | // Test transfer with TLS channel ID enabled |
| 613 | TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSChannelIdEnabled) { |
| 614 | SetEnableTlsChannelId(true); |
| 615 | TestHandshake(true); |
| 616 | TestTransfer("Hello, world!"); |
| 617 | } |
| 618 | |
| 619 | // Test transfer with TLS channel ID disabled |
| 620 | TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSChannelIdDisabled) { |
| 621 | SetEnableTlsChannelId(false); |
| 622 | TestHandshake(true); |
| 623 | TestTransfer("Hello, world!"); |
| 624 | } |
| 625 | |
| 626 | // Test transfer with GREASE enabled |
| 627 | TEST_F(SSLAdapterTestTLS_ECDSA, TestGreaseEnabled) { |
| 628 | SetEnableGrease(true); |
| 629 | TestHandshake(true); |
| 630 | TestTransfer("Hello, world!"); |
| 631 | } |
| 632 | |
| 633 | // Test transfer with GREASE disabled |
| 634 | TEST_F(SSLAdapterTestTLS_ECDSA, TestGreaseDisabled) { |
| 635 | SetEnableGrease(false); |
| 636 | TestHandshake(true); |
| 637 | TestTransfer("Hello, world!"); |
| 638 | } |
| 639 | |
| 640 | // Test transfer with TLS1_3. |
| 641 | TEST_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. |
| 648 | TEST_F(SSLAdapterTestTLS_ECDSA, TestMaxSSLVersionTLS1_2) { |
| 649 | SetMaxSslVersion(0x0303 /* TLS1_2 */); |
| 650 | TestHandshake(true); |
| 651 | TestTransfer("Hello, world!"); |
| 652 | } |
| 653 | |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 654 | // Test transfer using ALPN with protos as h2 and http/1.1 |
| 655 | TEST_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 Real | 7bd1f1b | 2017-09-08 12:50:41 -0700 | [diff] [blame] | 662 | // Test transfer with TLS Elliptic curves set to "X25519:P-256:P-384:P-521" |
| 663 | TEST_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.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 670 | // Basic tests: DTLS |
| 671 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 672 | // Test that handshake works, using RSA |
| 673 | TEST_F(SSLAdapterTestDTLS_RSA, TestDTLSConnect) { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 674 | TestHandshake(true); |
| 675 | } |
| 676 | |
Benjamin Wright | 6e9c3df | 2018-05-22 16:11:56 -0700 | [diff] [blame] | 677 | // Test that handshake works with a custom verifier that returns true. DTLS_RSA. |
| 678 | TEST_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. |
| 685 | TEST_F(SSLAdapterTestDTLS_RSA, TestTLSConnectCustomCertVerifierFails) { |
| 686 | SetMockCertVerifier(/*return_value=*/false); |
| 687 | TestHandshake(/*expect_success=*/false); |
| 688 | } |
| 689 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 690 | // Test that handshake works, using ECDSA |
| 691 | TEST_F(SSLAdapterTestDTLS_ECDSA, TestDTLSConnect) { |
| 692 | TestHandshake(true); |
| 693 | } |
| 694 | |
Benjamin Wright | 6e9c3df | 2018-05-22 16:11:56 -0700 | [diff] [blame] | 695 | // Test that handshake works with a custom verifier that returns true. |
| 696 | // DTLS_ECDSA. |
| 697 | TEST_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. |
| 704 | TEST_F(SSLAdapterTestDTLS_ECDSA, TestTLSConnectCustomCertVerifierFails) { |
| 705 | SetMockCertVerifier(/*return_value=*/false); |
| 706 | TestHandshake(/*expect_success=*/false); |
| 707 | } |
| 708 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 709 | // Test transfer between client and server, using RSA |
| 710 | TEST_F(SSLAdapterTestDTLS_RSA, TestDTLSTransfer) { |
| 711 | TestHandshake(true); |
| 712 | TestTransfer("Hello, world!"); |
| 713 | } |
| 714 | |
Benjamin Wright | 6e9c3df | 2018-05-22 16:11:56 -0700 | [diff] [blame] | 715 | // Test transfer between client and server, using RSA with custom cert verifier. |
| 716 | TEST_F(SSLAdapterTestDTLS_RSA, TestDTLSTransferCustomCertVerifier) { |
| 717 | SetMockCertVerifier(/*return_value=*/true); |
| 718 | TestHandshake(/*expect_success=*/true); |
| 719 | TestTransfer("Hello, world!"); |
| 720 | } |
| 721 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 722 | // Test transfer between client and server, using ECDSA |
| 723 | TEST_F(SSLAdapterTestDTLS_ECDSA, TestDTLSTransfer) { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 724 | TestHandshake(true); |
| 725 | TestTransfer("Hello, world!"); |
| 726 | } |
Benjamin Wright | 6e9c3df | 2018-05-22 16:11:56 -0700 | [diff] [blame] | 727 | |
| 728 | // Test transfer between client and server, using ECDSA with custom cert |
| 729 | // verifier. |
| 730 | TEST_F(SSLAdapterTestDTLS_ECDSA, TestDTLSTransferCustomCertVerifier) { |
| 731 | SetMockCertVerifier(/*return_value=*/true); |
| 732 | TestHandshake(/*expect_success=*/true); |
| 733 | TestTransfer("Hello, world!"); |
| 734 | } |