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> |
| 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "rtc_base/gunit.h" |
| 15 | #include "rtc_base/ipaddress.h" |
| 16 | #include "rtc_base/socketstream.h" |
| 17 | #include "rtc_base/ssladapter.h" |
| 18 | #include "rtc_base/sslidentity.h" |
| 19 | #include "rtc_base/sslstreamadapter.h" |
| 20 | #include "rtc_base/stream.h" |
| 21 | #include "rtc_base/stringencode.h" |
| 22 | #include "rtc_base/virtualsocketserver.h" |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 23 | |
| 24 | static const int kTimeout = 5000; |
| 25 | |
| 26 | static rtc::AsyncSocket* CreateSocket(const rtc::SSLMode& ssl_mode) { |
| 27 | rtc::SocketAddress address(rtc::IPAddress(INADDR_ANY), 0); |
| 28 | |
| 29 | rtc::AsyncSocket* socket = rtc::Thread::Current()-> |
| 30 | socketserver()->CreateAsyncSocket( |
| 31 | address.family(), (ssl_mode == rtc::SSL_MODE_DTLS) ? |
| 32 | SOCK_DGRAM : SOCK_STREAM); |
| 33 | socket->Bind(address); |
| 34 | |
| 35 | return socket; |
| 36 | } |
| 37 | |
| 38 | static std::string GetSSLProtocolName(const rtc::SSLMode& ssl_mode) { |
| 39 | return (ssl_mode == rtc::SSL_MODE_DTLS) ? "DTLS" : "TLS"; |
| 40 | } |
| 41 | |
| 42 | class SSLAdapterTestDummyClient : public sigslot::has_slots<> { |
| 43 | public: |
| 44 | explicit SSLAdapterTestDummyClient(const rtc::SSLMode& ssl_mode) |
| 45 | : ssl_mode_(ssl_mode) { |
| 46 | rtc::AsyncSocket* socket = CreateSocket(ssl_mode_); |
| 47 | |
| 48 | ssl_adapter_.reset(rtc::SSLAdapter::Create(socket)); |
| 49 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 50 | ssl_adapter_->SetMode(ssl_mode_); |
| 51 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 52 | // Ignore any certificate errors for the purpose of testing. |
| 53 | // Note: We do this only because we don't have a real certificate. |
| 54 | // NEVER USE THIS IN PRODUCTION CODE! |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 55 | ssl_adapter_->SetIgnoreBadCert(true); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 56 | |
| 57 | ssl_adapter_->SignalReadEvent.connect(this, |
| 58 | &SSLAdapterTestDummyClient::OnSSLAdapterReadEvent); |
| 59 | ssl_adapter_->SignalCloseEvent.connect(this, |
| 60 | &SSLAdapterTestDummyClient::OnSSLAdapterCloseEvent); |
| 61 | } |
| 62 | |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 63 | void SetAlpnProtocols(const std::vector<std::string>& protos) { |
| 64 | ssl_adapter_->SetAlpnProtocols(protos); |
| 65 | } |
| 66 | |
Diogo Real | 7bd1f1b | 2017-09-08 12:50:41 -0700 | [diff] [blame] | 67 | void SetEllipticCurves(const std::vector<std::string>& curves) { |
| 68 | ssl_adapter_->SetEllipticCurves(curves); |
| 69 | } |
| 70 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 71 | rtc::SocketAddress GetAddress() const { |
| 72 | return ssl_adapter_->GetLocalAddress(); |
| 73 | } |
| 74 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 75 | rtc::AsyncSocket::ConnState GetState() const { |
| 76 | return ssl_adapter_->GetState(); |
| 77 | } |
| 78 | |
| 79 | const std::string& GetReceivedData() const { |
| 80 | return data_; |
| 81 | } |
| 82 | |
| 83 | int Connect(const std::string& hostname, const rtc::SocketAddress& address) { |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame^] | 84 | RTC_LOG(LS_INFO) << "Initiating connection with " << address.ToString(); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 85 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 86 | int rv = ssl_adapter_->Connect(address); |
| 87 | |
| 88 | if (rv == 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 89 | RTC_LOG(LS_INFO) << "Starting " << GetSSLProtocolName(ssl_mode_) |
| 90 | << " handshake with " << hostname; |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 91 | |
| 92 | if (ssl_adapter_->StartSSL(hostname.c_str(), false) != 0) { |
| 93 | return -1; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return rv; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | int Close() { |
| 101 | return ssl_adapter_->Close(); |
| 102 | } |
| 103 | |
| 104 | int Send(const std::string& message) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 105 | RTC_LOG(LS_INFO) << "Client sending '" << message << "'"; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 106 | |
| 107 | return ssl_adapter_->Send(message.data(), message.length()); |
| 108 | } |
| 109 | |
| 110 | void OnSSLAdapterReadEvent(rtc::AsyncSocket* socket) { |
| 111 | char buffer[4096] = ""; |
| 112 | |
| 113 | // 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] | 114 | int read = socket->Recv(buffer, sizeof(buffer) - 1, nullptr); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 115 | if (read != -1) { |
| 116 | buffer[read] = '\0'; |
| 117 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 118 | RTC_LOG(LS_INFO) << "Client received '" << buffer << "'"; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 119 | |
| 120 | data_ += buffer; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void OnSSLAdapterCloseEvent(rtc::AsyncSocket* socket, int error) { |
| 125 | // OpenSSLAdapter signals handshake failure with a close event, but without |
| 126 | // closing the socket! Let's close the socket here. This way GetState() can |
| 127 | // return CS_CLOSED after failure. |
| 128 | if (socket->GetState() != rtc::AsyncSocket::CS_CLOSED) { |
| 129 | socket->Close(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private: |
| 134 | const rtc::SSLMode ssl_mode_; |
| 135 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 136 | std::unique_ptr<rtc::SSLAdapter> ssl_adapter_; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 137 | |
| 138 | std::string data_; |
| 139 | }; |
| 140 | |
| 141 | class SSLAdapterTestDummyServer : public sigslot::has_slots<> { |
| 142 | public: |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 143 | explicit SSLAdapterTestDummyServer(const rtc::SSLMode& ssl_mode, |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 144 | const rtc::KeyParams& key_params) |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 145 | : ssl_mode_(ssl_mode) { |
| 146 | // Generate a key pair and a certificate for this host. |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 147 | ssl_identity_.reset(rtc::SSLIdentity::Generate(GetHostname(), key_params)); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 148 | |
| 149 | server_socket_.reset(CreateSocket(ssl_mode_)); |
| 150 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 151 | if (ssl_mode_ == rtc::SSL_MODE_TLS) { |
| 152 | server_socket_->SignalReadEvent.connect(this, |
| 153 | &SSLAdapterTestDummyServer::OnServerSocketReadEvent); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 154 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 155 | server_socket_->Listen(1); |
| 156 | } |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 157 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 158 | RTC_LOG(LS_INFO) << ((ssl_mode_ == rtc::SSL_MODE_DTLS) ? "UDP" : "TCP") |
| 159 | << " server listening on " |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame^] | 160 | << server_socket_->GetLocalAddress().ToString(); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | rtc::SocketAddress GetAddress() const { |
| 164 | return server_socket_->GetLocalAddress(); |
| 165 | } |
| 166 | |
| 167 | std::string GetHostname() const { |
| 168 | // Since we don't have a real certificate anyway, the value here doesn't |
| 169 | // really matter. |
| 170 | return "example.com"; |
| 171 | } |
| 172 | |
| 173 | const std::string& GetReceivedData() const { |
| 174 | return data_; |
| 175 | } |
| 176 | |
| 177 | int Send(const std::string& message) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 178 | if (ssl_stream_adapter_ == nullptr || |
| 179 | ssl_stream_adapter_->GetState() != rtc::SS_OPEN) { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 180 | // No connection yet. |
| 181 | return -1; |
| 182 | } |
| 183 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 184 | RTC_LOG(LS_INFO) << "Server sending '" << message << "'"; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 185 | |
| 186 | size_t written; |
| 187 | int error; |
| 188 | |
| 189 | rtc::StreamResult r = ssl_stream_adapter_->Write(message.data(), |
| 190 | message.length(), &written, &error); |
| 191 | if (r == rtc::SR_SUCCESS) { |
| 192 | return written; |
| 193 | } else { |
| 194 | return -1; |
| 195 | } |
| 196 | } |
| 197 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 198 | void AcceptConnection(const rtc::SocketAddress& address) { |
| 199 | // Only a single connection is supported. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 200 | ASSERT_TRUE(ssl_stream_adapter_ == nullptr); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 201 | |
| 202 | // This is only for DTLS. |
| 203 | ASSERT_EQ(rtc::SSL_MODE_DTLS, ssl_mode_); |
| 204 | |
| 205 | // Transfer ownership of the socket to the SSLStreamAdapter object. |
| 206 | rtc::AsyncSocket* socket = server_socket_.release(); |
| 207 | |
| 208 | socket->Connect(address); |
| 209 | |
| 210 | DoHandshake(socket); |
| 211 | } |
| 212 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 213 | void OnServerSocketReadEvent(rtc::AsyncSocket* socket) { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 214 | // Only a single connection is supported. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 215 | ASSERT_TRUE(ssl_stream_adapter_ == nullptr); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 216 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 217 | DoHandshake(server_socket_->Accept(nullptr)); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void OnSSLStreamAdapterEvent(rtc::StreamInterface* stream, int sig, int err) { |
| 221 | if (sig & rtc::SE_READ) { |
| 222 | char buffer[4096] = ""; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 223 | size_t read; |
| 224 | int error; |
| 225 | |
| 226 | // Read data received from the client and store it in our internal |
| 227 | // buffer. |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 228 | rtc::StreamResult r = |
| 229 | stream->Read(buffer, sizeof(buffer) - 1, &read, &error); |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 230 | if (r == rtc::SR_SUCCESS) { |
| 231 | buffer[read] = '\0'; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 232 | RTC_LOG(LS_INFO) << "Server received '" << buffer << "'"; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 233 | data_ += buffer; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | private: |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 239 | void DoHandshake(rtc::AsyncSocket* socket) { |
| 240 | rtc::SocketStream* stream = new rtc::SocketStream(socket); |
| 241 | |
| 242 | ssl_stream_adapter_.reset(rtc::SSLStreamAdapter::Create(stream)); |
| 243 | |
| 244 | ssl_stream_adapter_->SetMode(ssl_mode_); |
| 245 | ssl_stream_adapter_->SetServerRole(); |
| 246 | |
| 247 | // SSLStreamAdapter is normally used for peer-to-peer communication, but |
| 248 | // here we're testing communication between a client and a server |
| 249 | // (e.g. a WebRTC-based application and an RFC 5766 TURN server), where |
| 250 | // clients are not required to provide a certificate during handshake. |
| 251 | // Accordingly, we must disable client authentication here. |
| 252 | ssl_stream_adapter_->set_client_auth_enabled(false); |
| 253 | |
| 254 | ssl_stream_adapter_->SetIdentity(ssl_identity_->GetReference()); |
| 255 | |
| 256 | // Set a bogus peer certificate digest. |
| 257 | unsigned char digest[20]; |
| 258 | size_t digest_len = sizeof(digest); |
| 259 | ssl_stream_adapter_->SetPeerCertificateDigest(rtc::DIGEST_SHA_1, digest, |
| 260 | digest_len); |
| 261 | |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 262 | ssl_stream_adapter_->StartSSL(); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 263 | |
| 264 | ssl_stream_adapter_->SignalEvent.connect(this, |
| 265 | &SSLAdapterTestDummyServer::OnSSLStreamAdapterEvent); |
| 266 | } |
| 267 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 268 | const rtc::SSLMode ssl_mode_; |
| 269 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 270 | std::unique_ptr<rtc::AsyncSocket> server_socket_; |
| 271 | std::unique_ptr<rtc::SSLStreamAdapter> ssl_stream_adapter_; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 272 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 273 | std::unique_ptr<rtc::SSLIdentity> ssl_identity_; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 274 | |
| 275 | std::string data_; |
| 276 | }; |
| 277 | |
| 278 | class SSLAdapterTestBase : public testing::Test, |
| 279 | public sigslot::has_slots<> { |
| 280 | public: |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 281 | explicit SSLAdapterTestBase(const rtc::SSLMode& ssl_mode, |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 282 | const rtc::KeyParams& key_params) |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 283 | : ssl_mode_(ssl_mode), |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 284 | vss_(new rtc::VirtualSocketServer()), |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 285 | thread_(vss_.get()), |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 286 | server_(new SSLAdapterTestDummyServer(ssl_mode_, key_params)), |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 287 | client_(new SSLAdapterTestDummyClient(ssl_mode_)), |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 288 | handshake_wait_(kTimeout) {} |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 289 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 290 | void SetHandshakeWait(int wait) { |
| 291 | handshake_wait_ = wait; |
| 292 | } |
| 293 | |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 294 | void SetAlpnProtocols(const std::vector<std::string>& protos) { |
| 295 | client_->SetAlpnProtocols(protos); |
| 296 | } |
| 297 | |
Diogo Real | 7bd1f1b | 2017-09-08 12:50:41 -0700 | [diff] [blame] | 298 | void SetEllipticCurves(const std::vector<std::string>& curves) { |
| 299 | client_->SetEllipticCurves(curves); |
| 300 | } |
| 301 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 302 | void TestHandshake(bool expect_success) { |
| 303 | int rv; |
| 304 | |
| 305 | // The initial state is CS_CLOSED |
| 306 | ASSERT_EQ(rtc::AsyncSocket::CS_CLOSED, client_->GetState()); |
| 307 | |
| 308 | rv = client_->Connect(server_->GetHostname(), server_->GetAddress()); |
| 309 | ASSERT_EQ(0, rv); |
| 310 | |
| 311 | // Now the state should be CS_CONNECTING |
| 312 | ASSERT_EQ(rtc::AsyncSocket::CS_CONNECTING, client_->GetState()); |
| 313 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 314 | if (ssl_mode_ == rtc::SSL_MODE_DTLS) { |
| 315 | // For DTLS, call AcceptConnection() with the client's address. |
| 316 | server_->AcceptConnection(client_->GetAddress()); |
| 317 | } |
| 318 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 319 | if (expect_success) { |
| 320 | // If expecting success, the client should end up in the CS_CONNECTED |
| 321 | // state after handshake. |
| 322 | EXPECT_EQ_WAIT(rtc::AsyncSocket::CS_CONNECTED, client_->GetState(), |
| 323 | handshake_wait_); |
| 324 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 325 | RTC_LOG(LS_INFO) << GetSSLProtocolName(ssl_mode_) |
| 326 | << " handshake complete."; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 327 | |
| 328 | } else { |
| 329 | // On handshake failure the client should end up in the CS_CLOSED state. |
| 330 | EXPECT_EQ_WAIT(rtc::AsyncSocket::CS_CLOSED, client_->GetState(), |
| 331 | handshake_wait_); |
| 332 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 333 | RTC_LOG(LS_INFO) << GetSSLProtocolName(ssl_mode_) << " handshake failed."; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | void TestTransfer(const std::string& message) { |
| 338 | int rv; |
| 339 | |
| 340 | rv = client_->Send(message); |
| 341 | ASSERT_EQ(static_cast<int>(message.length()), rv); |
| 342 | |
| 343 | // The server should have received the client's message. |
| 344 | EXPECT_EQ_WAIT(message, server_->GetReceivedData(), kTimeout); |
| 345 | |
| 346 | rv = server_->Send(message); |
| 347 | ASSERT_EQ(static_cast<int>(message.length()), rv); |
| 348 | |
| 349 | // The client should have received the server's message. |
| 350 | EXPECT_EQ_WAIT(message, client_->GetReceivedData(), kTimeout); |
| 351 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 352 | RTC_LOG(LS_INFO) << "Transfer complete."; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 353 | } |
| 354 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 355 | protected: |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 356 | const rtc::SSLMode ssl_mode_; |
| 357 | |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 358 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 359 | rtc::AutoSocketServerThread thread_; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 360 | std::unique_ptr<SSLAdapterTestDummyServer> server_; |
| 361 | std::unique_ptr<SSLAdapterTestDummyClient> client_; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 362 | |
| 363 | int handshake_wait_; |
| 364 | }; |
| 365 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 366 | class SSLAdapterTestTLS_RSA : public SSLAdapterTestBase { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 367 | public: |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 368 | SSLAdapterTestTLS_RSA() |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 369 | : SSLAdapterTestBase(rtc::SSL_MODE_TLS, rtc::KeyParams::RSA()) {} |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 370 | }; |
| 371 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 372 | class SSLAdapterTestTLS_ECDSA : public SSLAdapterTestBase { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 373 | public: |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 374 | SSLAdapterTestTLS_ECDSA() |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 375 | : SSLAdapterTestBase(rtc::SSL_MODE_TLS, rtc::KeyParams::ECDSA()) {} |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 376 | }; |
| 377 | |
| 378 | class SSLAdapterTestDTLS_RSA : public SSLAdapterTestBase { |
| 379 | public: |
| 380 | SSLAdapterTestDTLS_RSA() |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 381 | : SSLAdapterTestBase(rtc::SSL_MODE_DTLS, rtc::KeyParams::RSA()) {} |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 382 | }; |
| 383 | |
| 384 | class SSLAdapterTestDTLS_ECDSA : public SSLAdapterTestBase { |
| 385 | public: |
| 386 | SSLAdapterTestDTLS_ECDSA() |
torbjorng | 4e57247 | 2015-10-08 09:42:49 -0700 | [diff] [blame] | 387 | : SSLAdapterTestBase(rtc::SSL_MODE_DTLS, rtc::KeyParams::ECDSA()) {} |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 388 | }; |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 389 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 390 | // Basic tests: TLS |
| 391 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 392 | // Test that handshake works, using RSA |
| 393 | TEST_F(SSLAdapterTestTLS_RSA, TestTLSConnect) { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 394 | TestHandshake(true); |
| 395 | } |
| 396 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 397 | // Test that handshake works, using ECDSA |
| 398 | TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSConnect) { |
| 399 | TestHandshake(true); |
| 400 | } |
| 401 | |
| 402 | // Test transfer between client and server, using RSA |
| 403 | TEST_F(SSLAdapterTestTLS_RSA, TestTLSTransfer) { |
| 404 | TestHandshake(true); |
| 405 | TestTransfer("Hello, world!"); |
| 406 | } |
| 407 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 408 | TEST_F(SSLAdapterTestTLS_RSA, TestTLSTransferWithBlockedSocket) { |
| 409 | TestHandshake(true); |
| 410 | |
| 411 | // Tell the underlying socket to simulate being blocked. |
| 412 | vss_->SetSendingBlocked(true); |
| 413 | |
| 414 | std::string expected; |
| 415 | int rv; |
| 416 | // Send messages until the SSL socket adapter starts applying backpressure. |
| 417 | // Note that this may not occur immediately since there may be some amount of |
| 418 | // intermediate buffering (either in our code or in BoringSSL). |
| 419 | for (int i = 0; i < 1024; ++i) { |
| 420 | std::string message = "Hello, world: " + rtc::ToString(i); |
| 421 | rv = client_->Send(message); |
| 422 | if (rv != static_cast<int>(message.size())) { |
| 423 | // This test assumes either the whole message or none of it is sent. |
| 424 | ASSERT_EQ(-1, rv); |
| 425 | break; |
| 426 | } |
| 427 | expected += message; |
| 428 | } |
| 429 | // Assert that the loop above exited due to Send returning -1. |
| 430 | ASSERT_EQ(-1, rv); |
| 431 | |
| 432 | // Try sending another message while blocked. -1 should be returned again and |
| 433 | // it shouldn't end up received by the server later. |
| 434 | EXPECT_EQ(-1, client_->Send("Never sent")); |
| 435 | |
| 436 | // Unblock the underlying socket. All of the buffered messages should be sent |
| 437 | // without any further action. |
| 438 | vss_->SetSendingBlocked(false); |
| 439 | EXPECT_EQ_WAIT(expected, server_->GetReceivedData(), kTimeout); |
| 440 | |
| 441 | // Send another message. This previously wasn't working |
| 442 | std::string final_message = "Fin."; |
| 443 | expected += final_message; |
| 444 | EXPECT_EQ(static_cast<int>(final_message.size()), |
| 445 | client_->Send(final_message)); |
| 446 | EXPECT_EQ_WAIT(expected, server_->GetReceivedData(), kTimeout); |
| 447 | } |
| 448 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 449 | // Test transfer between client and server, using ECDSA |
| 450 | TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSTransfer) { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 451 | TestHandshake(true); |
| 452 | TestTransfer("Hello, world!"); |
| 453 | } |
| 454 | |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 455 | // Test transfer using ALPN with protos as h2 and http/1.1 |
| 456 | TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSALPN) { |
| 457 | std::vector<std::string> alpn_protos{"h2", "http/1.1"}; |
| 458 | SetAlpnProtocols(alpn_protos); |
| 459 | TestHandshake(true); |
| 460 | TestTransfer("Hello, world!"); |
| 461 | } |
| 462 | |
Diogo Real | 7bd1f1b | 2017-09-08 12:50:41 -0700 | [diff] [blame] | 463 | // Test transfer with TLS Elliptic curves set to "X25519:P-256:P-384:P-521" |
| 464 | TEST_F(SSLAdapterTestTLS_ECDSA, TestTLSEllipticCurves) { |
| 465 | std::vector<std::string> elliptic_curves{"X25519", "P-256", "P-384", "P-521"}; |
| 466 | SetEllipticCurves(elliptic_curves); |
| 467 | TestHandshake(true); |
| 468 | TestTransfer("Hello, world!"); |
| 469 | } |
| 470 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 471 | // Basic tests: DTLS |
| 472 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 473 | // Test that handshake works, using RSA |
| 474 | TEST_F(SSLAdapterTestDTLS_RSA, TestDTLSConnect) { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 475 | TestHandshake(true); |
| 476 | } |
| 477 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 478 | // Test that handshake works, using ECDSA |
| 479 | TEST_F(SSLAdapterTestDTLS_ECDSA, TestDTLSConnect) { |
| 480 | TestHandshake(true); |
| 481 | } |
| 482 | |
| 483 | // Test transfer between client and server, using RSA |
| 484 | TEST_F(SSLAdapterTestDTLS_RSA, TestDTLSTransfer) { |
| 485 | TestHandshake(true); |
| 486 | TestTransfer("Hello, world!"); |
| 487 | } |
| 488 | |
| 489 | // Test transfer between client and server, using ECDSA |
| 490 | TEST_F(SSLAdapterTestDTLS_ECDSA, TestDTLSTransfer) { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 491 | TestHandshake(true); |
| 492 | TestTransfer("Hello, world!"); |
| 493 | } |