blob: 8af2e0126d4658f884785e55c91bca3e856a6463 [file] [log] [blame]
[email protected]1bc6f5e2012-03-15 00:20:581// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]f61c3972010-12-23 09:54:152// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This test suite uses SSLClientSocket to test the implementation of
6// SSLServerSocket. In order to establish connections between the sockets
7// we need two additional classes:
8// 1. FakeSocket
9// Connects SSL socket to FakeDataChannel. This class is just a stub.
10//
11// 2. FakeDataChannel
12// Implements the actual exchange of data between two FakeSockets.
13//
14// Implementations of these two classes are included in this file.
15
16#include "net/socket/ssl_server_socket.h"
17
[email protected]55ee0e52011-07-21 18:29:4418#include <stdlib.h>
19
[email protected]f61c3972010-12-23 09:54:1520#include <queue>
21
[email protected]55ee0e52011-07-21 18:29:4422#include "base/compiler_specific.h"
[email protected]57999812013-02-24 05:40:5223#include "base/files/file_path.h"
thestigd8df0332014-09-04 06:33:2924#include "base/files/file_util.h"
[email protected]18b577412013-07-18 04:19:1525#include "base/message_loop/message_loop.h"
[email protected]4b559b4d2011-04-14 17:37:1426#include "crypto/nss_util.h"
27#include "crypto/rsa_private_key.h"
[email protected]f61c3972010-12-23 09:54:1528#include "net/base/address_list.h"
[email protected]6ea7b152011-12-21 21:21:1329#include "net/base/completion_callback.h"
[email protected]f61c3972010-12-23 09:54:1530#include "net/base/host_port_pair.h"
31#include "net/base/io_buffer.h"
[email protected]e7f74da2011-04-19 23:49:3532#include "net/base/ip_endpoint.h"
[email protected]f61c3972010-12-23 09:54:1533#include "net/base/net_errors.h"
[email protected]42fdb452012-11-01 12:44:4034#include "net/base/test_data_directory.h"
[email protected]6e7845ae2013-03-29 21:48:1135#include "net/cert/cert_status_flags.h"
36#include "net/cert/mock_cert_verifier.h"
37#include "net/cert/x509_certificate.h"
[email protected]b1c988b2013-06-13 06:48:1138#include "net/http/transport_security_state.h"
eroman87c53d62015-04-02 06:51:0739#include "net/log/net_log.h"
[email protected]f61c3972010-12-23 09:54:1540#include "net/socket/client_socket_factory.h"
41#include "net/socket/socket_test_util.h"
42#include "net/socket/ssl_client_socket.h"
[email protected]3268023f2011-05-05 00:08:1043#include "net/socket/stream_socket.h"
davidben9dd84872015-05-02 00:22:5844#include "net/ssl/ssl_cipher_suite_names.h"
[email protected]536fd0b2013-03-14 17:41:5745#include "net/ssl/ssl_config_service.h"
davidben9dd84872015-05-02 00:22:5846#include "net/ssl/ssl_connection_status_flags.h"
[email protected]536fd0b2013-03-14 17:41:5747#include "net/ssl/ssl_info.h"
[email protected]6e7845ae2013-03-29 21:48:1148#include "net/test/cert_test_util.h"
[email protected]f61c3972010-12-23 09:54:1549#include "testing/gtest/include/gtest/gtest.h"
50#include "testing/platform_test.h"
51
davidben9dd84872015-05-02 00:22:5852#if !defined(USE_OPENSSL)
53#include <pk11pub.h>
54
55#if !defined(CKM_AES_GCM)
56#define CKM_AES_GCM 0x00001087
57#endif
davidben299e3072015-05-04 19:06:4958#if !defined(CKM_NSS_TLS_MASTER_KEY_DERIVE_DH_SHA256)
59#define CKM_NSS_TLS_MASTER_KEY_DERIVE_DH_SHA256 (CKM_NSS + 24)
60#endif
davidben9dd84872015-05-02 00:22:5861#endif
62
[email protected]f61c3972010-12-23 09:54:1563namespace net {
64
65namespace {
66
67class FakeDataChannel {
68 public:
[email protected]55ee0e52011-07-21 18:29:4469 FakeDataChannel()
[email protected]83039bb2011-12-09 18:43:5570 : read_buf_len_(0),
[email protected]c0e4dd12012-05-16 19:36:3171 closed_(false),
[email protected]d5492c52013-11-10 20:44:3972 write_called_after_close_(false),
73 weak_factory_(this) {
[email protected]f61c3972010-12-23 09:54:1574 }
75
[email protected]47a12862012-04-10 01:00:4976 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
[email protected]4da82282014-07-16 18:40:4377 DCHECK(read_callback_.is_null());
dcheng08ea2af02014-08-25 23:38:0978 DCHECK(!read_buf_.get());
[email protected]c0e4dd12012-05-16 19:36:3179 if (closed_)
80 return 0;
[email protected]3f55aa12011-12-07 02:03:3381 if (data_.empty()) {
[email protected]f61c3972010-12-23 09:54:1582 read_callback_ = callback;
83 read_buf_ = buf;
84 read_buf_len_ = buf_len;
[email protected]fa6ce922014-07-17 04:27:0485 return ERR_IO_PENDING;
[email protected]f61c3972010-12-23 09:54:1586 }
87 return PropogateData(buf, buf_len);
88 }
89
[email protected]47a12862012-04-10 01:00:4990 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
[email protected]4da82282014-07-16 18:40:4391 DCHECK(write_callback_.is_null());
[email protected]c0e4dd12012-05-16 19:36:3192 if (closed_) {
93 if (write_called_after_close_)
[email protected]fa6ce922014-07-17 04:27:0494 return ERR_CONNECTION_RESET;
[email protected]c0e4dd12012-05-16 19:36:3195 write_called_after_close_ = true;
96 write_callback_ = callback;
[email protected]2da659e2013-05-23 20:51:3497 base::MessageLoop::current()->PostTask(
[email protected]c0e4dd12012-05-16 19:36:3198 FROM_HERE, base::Bind(&FakeDataChannel::DoWriteCallback,
99 weak_factory_.GetWeakPtr()));
[email protected]fa6ce922014-07-17 04:27:04100 return ERR_IO_PENDING;
[email protected]c0e4dd12012-05-16 19:36:31101 }
[email protected]4da82282014-07-16 18:40:43102 // This function returns synchronously, so make a copy of the buffer.
[email protected]fa6ce922014-07-17 04:27:04103 data_.push(new DrainableIOBuffer(
104 new StringIOBuffer(std::string(buf->data(), buf_len)),
[email protected]4da82282014-07-16 18:40:43105 buf_len));
[email protected]2da659e2013-05-23 20:51:34106 base::MessageLoop::current()->PostTask(
[email protected]83039bb2011-12-09 18:43:55107 FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback,
108 weak_factory_.GetWeakPtr()));
[email protected]f61c3972010-12-23 09:54:15109 return buf_len;
110 }
111
[email protected]c0e4dd12012-05-16 19:36:31112 // Closes the FakeDataChannel. After Close() is called, Read() returns 0,
113 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that
114 // after the FakeDataChannel is closed, the first Write() call completes
115 // asynchronously, which is necessary to reproduce bug 127822.
116 void Close() {
117 closed_ = true;
118 }
119
[email protected]f61c3972010-12-23 09:54:15120 private:
121 void DoReadCallback() {
[email protected]83039bb2011-12-09 18:43:55122 if (read_callback_.is_null() || data_.empty())
[email protected]f61c3972010-12-23 09:54:15123 return;
124
125 int copied = PropogateData(read_buf_, read_buf_len_);
[email protected]83039bb2011-12-09 18:43:55126 CompletionCallback callback = read_callback_;
127 read_callback_.Reset();
128 read_buf_ = NULL;
129 read_buf_len_ = 0;
130 callback.Run(copied);
[email protected]f61c3972010-12-23 09:54:15131 }
132
[email protected]c0e4dd12012-05-16 19:36:31133 void DoWriteCallback() {
134 if (write_callback_.is_null())
135 return;
136
137 CompletionCallback callback = write_callback_;
138 write_callback_.Reset();
[email protected]fa6ce922014-07-17 04:27:04139 callback.Run(ERR_CONNECTION_RESET);
[email protected]c0e4dd12012-05-16 19:36:31140 }
141
[email protected]fa6ce922014-07-17 04:27:04142 int PropogateData(scoped_refptr<IOBuffer> read_buf, int read_buf_len) {
143 scoped_refptr<DrainableIOBuffer> buf = data_.front();
[email protected]f61c3972010-12-23 09:54:15144 int copied = std::min(buf->BytesRemaining(), read_buf_len);
145 memcpy(read_buf->data(), buf->data(), copied);
146 buf->DidConsume(copied);
147
148 if (!buf->BytesRemaining())
149 data_.pop();
150 return copied;
151 }
152
[email protected]83039bb2011-12-09 18:43:55153 CompletionCallback read_callback_;
[email protected]fa6ce922014-07-17 04:27:04154 scoped_refptr<IOBuffer> read_buf_;
[email protected]f61c3972010-12-23 09:54:15155 int read_buf_len_;
156
[email protected]c0e4dd12012-05-16 19:36:31157 CompletionCallback write_callback_;
158
[email protected]fa6ce922014-07-17 04:27:04159 std::queue<scoped_refptr<DrainableIOBuffer> > data_;
[email protected]f61c3972010-12-23 09:54:15160
[email protected]c0e4dd12012-05-16 19:36:31161 // True if Close() has been called.
162 bool closed_;
163
164 // Controls the completion of Write() after the FakeDataChannel is closed.
165 // After the FakeDataChannel is closed, the first Write() call completes
166 // asynchronously.
167 bool write_called_after_close_;
168
[email protected]d5492c52013-11-10 20:44:39169 base::WeakPtrFactory<FakeDataChannel> weak_factory_;
170
[email protected]f61c3972010-12-23 09:54:15171 DISALLOW_COPY_AND_ASSIGN(FakeDataChannel);
172};
173
[email protected]3268023f2011-05-05 00:08:10174class FakeSocket : public StreamSocket {
[email protected]f61c3972010-12-23 09:54:15175 public:
176 FakeSocket(FakeDataChannel* incoming_channel,
177 FakeDataChannel* outgoing_channel)
178 : incoming_(incoming_channel),
179 outgoing_(outgoing_channel) {
180 }
181
dchengb03027d2014-10-21 12:00:20182 ~FakeSocket() override {}
[email protected]f61c3972010-12-23 09:54:15183
dchengb03027d2014-10-21 12:00:20184 int Read(IOBuffer* buf,
185 int buf_len,
186 const CompletionCallback& callback) override {
[email protected]3f55aa12011-12-07 02:03:33187 // Read random number of bytes.
188 buf_len = rand() % buf_len + 1;
189 return incoming_->Read(buf, buf_len, callback);
190 }
[email protected]f61c3972010-12-23 09:54:15191
dchengb03027d2014-10-21 12:00:20192 int Write(IOBuffer* buf,
193 int buf_len,
194 const CompletionCallback& callback) override {
[email protected]55ee0e52011-07-21 18:29:44195 // Write random number of bytes.
196 buf_len = rand() % buf_len + 1;
[email protected]f61c3972010-12-23 09:54:15197 return outgoing_->Write(buf, buf_len, callback);
198 }
199
dchengb03027d2014-10-21 12:00:20200 int SetReceiveBufferSize(int32 size) override { return OK; }
[email protected]f61c3972010-12-23 09:54:15201
dchengb03027d2014-10-21 12:00:20202 int SetSendBufferSize(int32 size) override { return OK; }
[email protected]f61c3972010-12-23 09:54:15203
dchengb03027d2014-10-21 12:00:20204 int Connect(const CompletionCallback& callback) override { return OK; }
[email protected]f61c3972010-12-23 09:54:15205
dchengb03027d2014-10-21 12:00:20206 void Disconnect() override {
[email protected]c0e4dd12012-05-16 19:36:31207 incoming_->Close();
208 outgoing_->Close();
209 }
[email protected]f61c3972010-12-23 09:54:15210
dchengb03027d2014-10-21 12:00:20211 bool IsConnected() const override { return true; }
[email protected]f61c3972010-12-23 09:54:15212
dchengb03027d2014-10-21 12:00:20213 bool IsConnectedAndIdle() const override { return true; }
[email protected]f61c3972010-12-23 09:54:15214
dchengb03027d2014-10-21 12:00:20215 int GetPeerAddress(IPEndPoint* address) const override {
[email protected]fa6ce922014-07-17 04:27:04216 IPAddressNumber ip_address(kIPv4AddressSize);
217 *address = IPEndPoint(ip_address, 0 /*port*/);
218 return OK;
[email protected]f61c3972010-12-23 09:54:15219 }
220
dchengb03027d2014-10-21 12:00:20221 int GetLocalAddress(IPEndPoint* address) const override {
[email protected]fa6ce922014-07-17 04:27:04222 IPAddressNumber ip_address(4);
223 *address = IPEndPoint(ip_address, 0);
224 return OK;
[email protected]e7f74da2011-04-19 23:49:35225 }
226
dchengb03027d2014-10-21 12:00:20227 const BoundNetLog& NetLog() const override { return net_log_; }
[email protected]f61c3972010-12-23 09:54:15228
dchengb03027d2014-10-21 12:00:20229 void SetSubresourceSpeculation() override {}
230 void SetOmniboxSpeculation() override {}
[email protected]f61c3972010-12-23 09:54:15231
dchengb03027d2014-10-21 12:00:20232 bool WasEverUsed() const override { return true; }
[email protected]f61c3972010-12-23 09:54:15233
dchengb03027d2014-10-21 12:00:20234 bool UsingTCPFastOpen() const override { return false; }
[email protected]f61c3972010-12-23 09:54:15235
dchengb03027d2014-10-21 12:00:20236 bool WasNpnNegotiated() const override { return false; }
[email protected]5e6efa52011-06-27 17:26:41237
dchengb03027d2014-10-21 12:00:20238 NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; }
[email protected]2d88e7d2012-07-19 17:55:17239
dchengb03027d2014-10-21 12:00:20240 bool GetSSLInfo(SSLInfo* ssl_info) override { return false; }
[email protected]2d88e7d2012-07-19 17:55:17241
[email protected]f61c3972010-12-23 09:54:15242 private:
[email protected]fa6ce922014-07-17 04:27:04243 BoundNetLog net_log_;
[email protected]f61c3972010-12-23 09:54:15244 FakeDataChannel* incoming_;
245 FakeDataChannel* outgoing_;
246
247 DISALLOW_COPY_AND_ASSIGN(FakeSocket);
248};
249
250} // namespace
251
252// Verify the correctness of the test helper classes first.
253TEST(FakeSocketTest, DataTransfer) {
254 // Establish channels between two sockets.
255 FakeDataChannel channel_1;
256 FakeDataChannel channel_2;
257 FakeSocket client(&channel_1, &channel_2);
258 FakeSocket server(&channel_2, &channel_1);
259
260 const char kTestData[] = "testing123";
261 const int kTestDataSize = strlen(kTestData);
262 const int kReadBufSize = 1024;
[email protected]fa6ce922014-07-17 04:27:04263 scoped_refptr<IOBuffer> write_buf = new StringIOBuffer(kTestData);
264 scoped_refptr<IOBuffer> read_buf = new IOBuffer(kReadBufSize);
[email protected]f61c3972010-12-23 09:54:15265
266 // Write then read.
[email protected]90499482013-06-01 00:39:50267 int written =
268 server.Write(write_buf.get(), kTestDataSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44269 EXPECT_GT(written, 0);
270 EXPECT_LE(written, kTestDataSize);
271
[email protected]90499482013-06-01 00:39:50272 int read = client.Read(read_buf.get(), kReadBufSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44273 EXPECT_GT(read, 0);
274 EXPECT_LE(read, written);
275 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read));
[email protected]f61c3972010-12-23 09:54:15276
277 // Read then write.
[email protected]83039bb2011-12-09 18:43:55278 TestCompletionCallback callback;
[email protected]fa6ce922014-07-17 04:27:04279 EXPECT_EQ(ERR_IO_PENDING,
[email protected]90499482013-06-01 00:39:50280 server.Read(read_buf.get(), kReadBufSize, callback.callback()));
[email protected]55ee0e52011-07-21 18:29:44281
[email protected]90499482013-06-01 00:39:50282 written = client.Write(write_buf.get(), kTestDataSize, CompletionCallback());
[email protected]55ee0e52011-07-21 18:29:44283 EXPECT_GT(written, 0);
284 EXPECT_LE(written, kTestDataSize);
285
286 read = callback.WaitForResult();
287 EXPECT_GT(read, 0);
288 EXPECT_LE(read, written);
289 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read));
[email protected]f61c3972010-12-23 09:54:15290}
291
292class SSLServerSocketTest : public PlatformTest {
293 public:
294 SSLServerSocketTest()
[email protected]fa6ce922014-07-17 04:27:04295 : socket_factory_(ClientSocketFactory::GetDefaultFactory()),
[email protected]b1c988b2013-06-13 06:48:11296 cert_verifier_(new MockCertVerifier()),
297 transport_security_state_(new TransportSecurityState) {
[email protected]fa6ce922014-07-17 04:27:04298 cert_verifier_->set_default_result(CERT_STATUS_AUTHORITY_INVALID);
[email protected]f61c3972010-12-23 09:54:15299 }
300
301 protected:
302 void Initialize() {
[email protected]18ccfdb2013-08-15 00:13:44303 scoped_ptr<ClientSocketHandle> client_connection(new ClientSocketHandle);
304 client_connection->SetSocket(
305 scoped_ptr<StreamSocket>(new FakeSocket(&channel_1_, &channel_2_)));
306 scoped_ptr<StreamSocket> server_socket(
307 new FakeSocket(&channel_2_, &channel_1_));
[email protected]f61c3972010-12-23 09:54:15308
[email protected]6cdfd7f2013-02-08 20:40:15309 base::FilePath certs_dir(GetTestCertsDirectory());
[email protected]f61c3972010-12-23 09:54:15310
[email protected]6cdfd7f2013-02-08 20:40:15311 base::FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der");
[email protected]f61c3972010-12-23 09:54:15312 std::string cert_der;
[email protected]82f84b92013-08-30 18:23:50313 ASSERT_TRUE(base::ReadFileToString(cert_path, &cert_der));
[email protected]f61c3972010-12-23 09:54:15314
[email protected]fa6ce922014-07-17 04:27:04315 scoped_refptr<X509Certificate> cert =
[email protected]f61c3972010-12-23 09:54:15316 X509Certificate::CreateFromBytes(cert_der.data(), cert_der.size());
317
[email protected]6cdfd7f2013-02-08 20:40:15318 base::FilePath key_path = certs_dir.AppendASCII("unittest.key.bin");
[email protected]f61c3972010-12-23 09:54:15319 std::string key_string;
[email protected]82f84b92013-08-30 18:23:50320 ASSERT_TRUE(base::ReadFileToString(key_path, &key_string));
[email protected]f61c3972010-12-23 09:54:15321 std::vector<uint8> key_vector(
322 reinterpret_cast<const uint8*>(key_string.data()),
323 reinterpret_cast<const uint8*>(key_string.data() +
324 key_string.length()));
325
[email protected]4b559b4d2011-04-14 17:37:14326 scoped_ptr<crypto::RSAPrivateKey> private_key(
327 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector));
[email protected]f61c3972010-12-23 09:54:15328
sergeyuff826d5e2015-05-13 20:35:22329 client_ssl_config_.false_start_enabled = false;
330 client_ssl_config_.channel_id_enabled = false;
[email protected]f61c3972010-12-23 09:54:15331
332 // Certificate provided by the host doesn't need authority.
[email protected]fa6ce922014-07-17 04:27:04333 SSLConfig::CertAndStatus cert_and_status;
[email protected]4dc832e2011-04-28 22:04:24334 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID;
[email protected]3d5c1bd2011-07-20 02:14:01335 cert_and_status.der_cert = cert_der;
sergeyuff826d5e2015-05-13 20:35:22336 client_ssl_config_.allowed_bad_certs.push_back(cert_and_status);
[email protected]f61c3972010-12-23 09:54:15337
[email protected]fa6ce922014-07-17 04:27:04338 HostPortPair host_and_pair("unittest", 0);
339 SSLClientSocketContext context;
[email protected]9f59fac2012-03-21 23:18:11340 context.cert_verifier = cert_verifier_.get();
[email protected]b1c988b2013-06-13 06:48:11341 context.transport_security_state = transport_security_state_.get();
sergeyuff826d5e2015-05-13 20:35:22342 client_socket_ = socket_factory_->CreateSSLClientSocket(
343 client_connection.Pass(), host_and_pair, client_ssl_config_, context);
344 server_socket_ =
345 CreateSSLServerSocket(server_socket.Pass(), cert.get(),
346 private_key.get(), server_ssl_config_);
[email protected]f61c3972010-12-23 09:54:15347 }
348
349 FakeDataChannel channel_1_;
350 FakeDataChannel channel_2_;
sergeyuff826d5e2015-05-13 20:35:22351 SSLConfig client_ssl_config_;
352 SSLConfig server_ssl_config_;
[email protected]fa6ce922014-07-17 04:27:04353 scoped_ptr<SSLClientSocket> client_socket_;
354 scoped_ptr<SSLServerSocket> server_socket_;
355 ClientSocketFactory* socket_factory_;
356 scoped_ptr<MockCertVerifier> cert_verifier_;
357 scoped_ptr<TransportSecurityState> transport_security_state_;
[email protected]f61c3972010-12-23 09:54:15358};
359
[email protected]f61c3972010-12-23 09:54:15360// This test only executes creation of client and server sockets. This is to
361// test that creation of sockets doesn't crash and have minimal code to run
362// under valgrind in order to help debugging memory problems.
363TEST_F(SSLServerSocketTest, Initialize) {
364 Initialize();
365}
366
[email protected]a7ac3c32011-06-17 19:10:15367// This test executes Connect() on SSLClientSocket and Handshake() on
368// SSLServerSocket to make sure handshaking between the two sockets is
[email protected]f61c3972010-12-23 09:54:15369// completed successfully.
370TEST_F(SSLServerSocketTest, Handshake) {
371 Initialize();
372
[email protected]83039bb2011-12-09 18:43:55373 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13374 TestCompletionCallback handshake_callback;
[email protected]f61c3972010-12-23 09:54:15375
[email protected]6ea7b152011-12-21 21:21:13376 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04377 EXPECT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15378
[email protected]83039bb2011-12-09 18:43:55379 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04380 EXPECT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15381
[email protected]fa6ce922014-07-17 04:27:04382 if (client_ret == ERR_IO_PENDING) {
383 EXPECT_EQ(OK, connect_callback.WaitForResult());
[email protected]f61c3972010-12-23 09:54:15384 }
[email protected]fa6ce922014-07-17 04:27:04385 if (server_ret == ERR_IO_PENDING) {
386 EXPECT_EQ(OK, handshake_callback.WaitForResult());
[email protected]f61c3972010-12-23 09:54:15387 }
[email protected]4dc832e2011-04-28 22:04:24388
389 // Make sure the cert status is expected.
390 SSLInfo ssl_info;
davidben9dd84872015-05-02 00:22:58391 ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info));
[email protected]4dc832e2011-04-28 22:04:24392 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status);
davidben9dd84872015-05-02 00:22:58393
394 // The default cipher suite should be ECDHE and, unless on NSS and the
395 // platform doesn't support it, an AEAD.
396 uint16_t cipher_suite =
397 SSLConnectionStatusToCipherSuite(ssl_info.connection_status);
398 const char* key_exchange;
399 const char* cipher;
400 const char* mac;
401 bool is_aead;
402 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite);
403 EXPECT_STREQ("ECDHE_RSA", key_exchange);
404#if defined(USE_OPENSSL)
405 bool supports_aead = true;
406#else
davidben299e3072015-05-04 19:06:49407 bool supports_aead =
408 PK11_TokenExists(CKM_AES_GCM) &&
409 PK11_TokenExists(CKM_NSS_TLS_MASTER_KEY_DERIVE_DH_SHA256);
davidben9dd84872015-05-02 00:22:58410#endif
411 EXPECT_TRUE(!supports_aead || is_aead);
[email protected]f61c3972010-12-23 09:54:15412}
413
414TEST_F(SSLServerSocketTest, DataTransfer) {
415 Initialize();
416
[email protected]83039bb2011-12-09 18:43:55417 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13418 TestCompletionCallback handshake_callback;
[email protected]f61c3972010-12-23 09:54:15419
420 // Establish connection.
[email protected]83039bb2011-12-09 18:43:55421 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04422 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15423
[email protected]6ea7b152011-12-21 21:21:13424 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04425 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15426
[email protected]febbbb52011-08-17 04:59:23427 client_ret = connect_callback.GetResult(client_ret);
[email protected]fa6ce922014-07-17 04:27:04428 ASSERT_EQ(OK, client_ret);
[email protected]febbbb52011-08-17 04:59:23429 server_ret = handshake_callback.GetResult(server_ret);
[email protected]fa6ce922014-07-17 04:27:04430 ASSERT_EQ(OK, server_ret);
[email protected]f61c3972010-12-23 09:54:15431
432 const int kReadBufSize = 1024;
[email protected]fa6ce922014-07-17 04:27:04433 scoped_refptr<StringIOBuffer> write_buf =
434 new StringIOBuffer("testing123");
435 scoped_refptr<DrainableIOBuffer> read_buf =
436 new DrainableIOBuffer(new IOBuffer(kReadBufSize), kReadBufSize);
[email protected]f61c3972010-12-23 09:54:15437
438 // Write then read.
[email protected]83039bb2011-12-09 18:43:55439 TestCompletionCallback write_callback;
440 TestCompletionCallback read_callback;
[email protected]90499482013-06-01 00:39:50441 server_ret = server_socket_->Write(
442 write_buf.get(), write_buf->size(), write_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04443 EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING);
[email protected]90499482013-06-01 00:39:50444 client_ret = client_socket_->Read(
445 read_buf.get(), read_buf->BytesRemaining(), read_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04446 EXPECT_TRUE(client_ret > 0 || client_ret == ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15447
[email protected]febbbb52011-08-17 04:59:23448 server_ret = write_callback.GetResult(server_ret);
449 EXPECT_GT(server_ret, 0);
450 client_ret = read_callback.GetResult(client_ret);
451 ASSERT_GT(client_ret, 0);
452
453 read_buf->DidConsume(client_ret);
454 while (read_buf->BytesConsumed() < write_buf->size()) {
[email protected]90499482013-06-01 00:39:50455 client_ret = client_socket_->Read(
456 read_buf.get(), read_buf->BytesRemaining(), read_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04457 EXPECT_TRUE(client_ret > 0 || client_ret == ERR_IO_PENDING);
[email protected]febbbb52011-08-17 04:59:23458 client_ret = read_callback.GetResult(client_ret);
459 ASSERT_GT(client_ret, 0);
460 read_buf->DidConsume(client_ret);
[email protected]f61c3972010-12-23 09:54:15461 }
[email protected]febbbb52011-08-17 04:59:23462 EXPECT_EQ(write_buf->size(), read_buf->BytesConsumed());
463 read_buf->SetOffset(0);
[email protected]f61c3972010-12-23 09:54:15464 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size()));
465
466 // Read then write.
[email protected]fa6ce922014-07-17 04:27:04467 write_buf = new StringIOBuffer("hello123");
[email protected]90499482013-06-01 00:39:50468 server_ret = server_socket_->Read(
469 read_buf.get(), read_buf->BytesRemaining(), read_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04470 EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING);
[email protected]90499482013-06-01 00:39:50471 client_ret = client_socket_->Write(
472 write_buf.get(), write_buf->size(), write_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04473 EXPECT_TRUE(client_ret > 0 || client_ret == ERR_IO_PENDING);
[email protected]f61c3972010-12-23 09:54:15474
[email protected]febbbb52011-08-17 04:59:23475 server_ret = read_callback.GetResult(server_ret);
476 ASSERT_GT(server_ret, 0);
477 client_ret = write_callback.GetResult(client_ret);
478 EXPECT_GT(client_ret, 0);
479
480 read_buf->DidConsume(server_ret);
481 while (read_buf->BytesConsumed() < write_buf->size()) {
[email protected]90499482013-06-01 00:39:50482 server_ret = server_socket_->Read(
483 read_buf.get(), read_buf->BytesRemaining(), read_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04484 EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING);
[email protected]febbbb52011-08-17 04:59:23485 server_ret = read_callback.GetResult(server_ret);
486 ASSERT_GT(server_ret, 0);
487 read_buf->DidConsume(server_ret);
[email protected]f61c3972010-12-23 09:54:15488 }
[email protected]febbbb52011-08-17 04:59:23489 EXPECT_EQ(write_buf->size(), read_buf->BytesConsumed());
490 read_buf->SetOffset(0);
[email protected]f61c3972010-12-23 09:54:15491 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size()));
492}
[email protected]b0ff3f82011-07-23 05:12:39493
[email protected]c0e4dd12012-05-16 19:36:31494// A regression test for bug 127822 (http://crbug.com/127822).
495// If the server closes the connection after the handshake is finished,
496// the client's Write() call should not cause an infinite loop.
497// NOTE: this is a test for SSLClientSocket rather than SSLServerSocket.
[email protected]4da82282014-07-16 18:40:43498TEST_F(SSLServerSocketTest, ClientWriteAfterServerClose) {
[email protected]c0e4dd12012-05-16 19:36:31499 Initialize();
500
501 TestCompletionCallback connect_callback;
502 TestCompletionCallback handshake_callback;
503
504 // Establish connection.
505 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04506 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING);
[email protected]c0e4dd12012-05-16 19:36:31507
508 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04509 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING);
[email protected]c0e4dd12012-05-16 19:36:31510
511 client_ret = connect_callback.GetResult(client_ret);
[email protected]fa6ce922014-07-17 04:27:04512 ASSERT_EQ(OK, client_ret);
[email protected]c0e4dd12012-05-16 19:36:31513 server_ret = handshake_callback.GetResult(server_ret);
[email protected]fa6ce922014-07-17 04:27:04514 ASSERT_EQ(OK, server_ret);
[email protected]c0e4dd12012-05-16 19:36:31515
[email protected]fa6ce922014-07-17 04:27:04516 scoped_refptr<StringIOBuffer> write_buf = new StringIOBuffer("testing123");
[email protected]c0e4dd12012-05-16 19:36:31517
518 // The server closes the connection. The server needs to write some
519 // data first so that the client's Read() calls from the transport
520 // socket won't return ERR_IO_PENDING. This ensures that the client
521 // will call Read() on the transport socket again.
522 TestCompletionCallback write_callback;
523
[email protected]90499482013-06-01 00:39:50524 server_ret = server_socket_->Write(
525 write_buf.get(), write_buf->size(), write_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04526 EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING);
[email protected]c0e4dd12012-05-16 19:36:31527
528 server_ret = write_callback.GetResult(server_ret);
529 EXPECT_GT(server_ret, 0);
530
531 server_socket_->Disconnect();
532
533 // The client writes some data. This should not cause an infinite loop.
[email protected]90499482013-06-01 00:39:50534 client_ret = client_socket_->Write(
535 write_buf.get(), write_buf->size(), write_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04536 EXPECT_TRUE(client_ret > 0 || client_ret == ERR_IO_PENDING);
[email protected]c0e4dd12012-05-16 19:36:31537
538 client_ret = write_callback.GetResult(client_ret);
539 EXPECT_GT(client_ret, 0);
540
[email protected]2da659e2013-05-23 20:51:34541 base::MessageLoop::current()->PostDelayedTask(
542 FROM_HERE, base::MessageLoop::QuitClosure(),
[email protected]c0e4dd12012-05-16 19:36:31543 base::TimeDelta::FromMilliseconds(10));
[email protected]2da659e2013-05-23 20:51:34544 base::MessageLoop::current()->Run();
[email protected]c0e4dd12012-05-16 19:36:31545}
546
[email protected]b0ff3f82011-07-23 05:12:39547// This test executes ExportKeyingMaterial() on the client and server sockets,
548// after connecting them, and verifies that the results match.
549// This test will fail if False Start is enabled (see crbug.com/90208).
550TEST_F(SSLServerSocketTest, ExportKeyingMaterial) {
551 Initialize();
552
[email protected]83039bb2011-12-09 18:43:55553 TestCompletionCallback connect_callback;
[email protected]6ea7b152011-12-21 21:21:13554 TestCompletionCallback handshake_callback;
[email protected]b0ff3f82011-07-23 05:12:39555
[email protected]83039bb2011-12-09 18:43:55556 int client_ret = client_socket_->Connect(connect_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04557 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING);
[email protected]b0ff3f82011-07-23 05:12:39558
[email protected]6ea7b152011-12-21 21:21:13559 int server_ret = server_socket_->Handshake(handshake_callback.callback());
[email protected]fa6ce922014-07-17 04:27:04560 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING);
[email protected]b0ff3f82011-07-23 05:12:39561
[email protected]fa6ce922014-07-17 04:27:04562 if (client_ret == ERR_IO_PENDING) {
563 ASSERT_EQ(OK, connect_callback.WaitForResult());
[email protected]b0ff3f82011-07-23 05:12:39564 }
[email protected]fa6ce922014-07-17 04:27:04565 if (server_ret == ERR_IO_PENDING) {
566 ASSERT_EQ(OK, handshake_callback.WaitForResult());
[email protected]b0ff3f82011-07-23 05:12:39567 }
568
569 const int kKeyingMaterialSize = 32;
thestig9d3bb0c2015-01-24 00:49:51570 const char kKeyingLabel[] = "EXPERIMENTAL-server-socket-test";
571 const char kKeyingContext[] = "";
[email protected]b0ff3f82011-07-23 05:12:39572 unsigned char server_out[kKeyingMaterialSize];
[email protected]1bc6f5e2012-03-15 00:20:58573 int rv = server_socket_->ExportKeyingMaterial(kKeyingLabel,
574 false, kKeyingContext,
[email protected]b0ff3f82011-07-23 05:12:39575 server_out, sizeof(server_out));
[email protected]fa6ce922014-07-17 04:27:04576 ASSERT_EQ(OK, rv);
[email protected]b0ff3f82011-07-23 05:12:39577
578 unsigned char client_out[kKeyingMaterialSize];
[email protected]1bc6f5e2012-03-15 00:20:58579 rv = client_socket_->ExportKeyingMaterial(kKeyingLabel,
580 false, kKeyingContext,
[email protected]b0ff3f82011-07-23 05:12:39581 client_out, sizeof(client_out));
[email protected]fa6ce922014-07-17 04:27:04582 ASSERT_EQ(OK, rv);
[email protected]47a12862012-04-10 01:00:49583 EXPECT_EQ(0, memcmp(server_out, client_out, sizeof(server_out)));
[email protected]b0ff3f82011-07-23 05:12:39584
thestig9d3bb0c2015-01-24 00:49:51585 const char kKeyingLabelBad[] = "EXPERIMENTAL-server-socket-test-bad";
[email protected]b0ff3f82011-07-23 05:12:39586 unsigned char client_bad[kKeyingMaterialSize];
[email protected]1bc6f5e2012-03-15 00:20:58587 rv = client_socket_->ExportKeyingMaterial(kKeyingLabelBad,
588 false, kKeyingContext,
[email protected]b0ff3f82011-07-23 05:12:39589 client_bad, sizeof(client_bad));
[email protected]fa6ce922014-07-17 04:27:04590 ASSERT_EQ(rv, OK);
[email protected]47a12862012-04-10 01:00:49591 EXPECT_NE(0, memcmp(server_out, client_bad, sizeof(server_out)));
[email protected]b0ff3f82011-07-23 05:12:39592}
[email protected]f61c3972010-12-23 09:54:15593
sergeyuff826d5e2015-05-13 20:35:22594// Verifies that SSLConfig::require_ecdhe flags works properly.
595TEST_F(SSLServerSocketTest, RequireEcdheFlag) {
596 // Disable all ECDHE suites on the client side.
597 uint16_t kEcdheCiphers[] = {
598 0xc007, // ECDHE_ECDSA_WITH_RC4_128_SHA
599 0xc009, // ECDHE_ECDSA_WITH_AES_128_CBC_SHA
600 0xc00a, // ECDHE_ECDSA_WITH_AES_256_CBC_SHA
601 0xc011, // ECDHE_RSA_WITH_RC4_128_SHA
602 0xc013, // ECDHE_RSA_WITH_AES_128_CBC_SHA
603 0xc014, // ECDHE_RSA_WITH_AES_256_CBC_SHA
604 0xc02b, // ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
605 0xc02f, // ECDHE_RSA_WITH_AES_128_GCM_SHA256
606 0xcc13, // ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
607 0xcc14, // ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
608 };
609 client_ssl_config_.disabled_cipher_suites.assign(
610 kEcdheCiphers, kEcdheCiphers + arraysize(kEcdheCiphers));
611
612 // Require ECDHE on the server.
613 server_ssl_config_.require_ecdhe = true;
614
615 Initialize();
616
617 TestCompletionCallback connect_callback;
618 TestCompletionCallback handshake_callback;
619
620 int client_ret = client_socket_->Connect(connect_callback.callback());
621 int server_ret = server_socket_->Handshake(handshake_callback.callback());
622
623 client_ret = connect_callback.GetResult(client_ret);
624 server_ret = handshake_callback.GetResult(server_ret);
625
626 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, client_ret);
627 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, server_ret);
628}
629
[email protected]f61c3972010-12-23 09:54:15630} // namespace net