You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

608 lines
19 KiB

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