OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This test suite uses SSLClientSocket to test the implementation of | 5 // This test suite uses SSLClientSocket to test the implementation of |
6 // SSLServerSocket. In order to establish connections between the sockets | 6 // SSLServerSocket. In order to establish connections between the sockets |
7 // we need two additional classes: | 7 // we need two additional classes: |
8 // 1. FakeSocket | 8 // 1. FakeSocket |
9 // Connects SSL socket to FakeDataChannel. This class is just a stub. | 9 // Connects SSL socket to FakeDataChannel. This class is just a stub. |
10 // | 10 // |
11 // 2. FakeDataChannel | 11 // 2. FakeDataChannel |
12 // Implements the actual exchange of data between two FakeSockets. | 12 // Implements the actual exchange of data between two FakeSockets. |
13 // | 13 // |
14 // Implementations of these two classes are included in this file. | 14 // Implementations of these two classes are included in this file. |
15 | 15 |
16 #include "net/socket/ssl_server_socket.h" | 16 #include "net/socket/ssl_server_socket.h" |
17 | 17 |
18 #include <openssl/evp.h> | |
19 #include <openssl/ssl.h> | |
20 #include <openssl/x509.h> | |
davidben
2016/01/25 20:56:11
Needs to be guarded by USE_OPENSSL (and ordered af
ryanchung
2016/01/29 23:22:13
Done.
| |
21 | |
18 #include <stdint.h> | 22 #include <stdint.h> |
19 #include <stdlib.h> | 23 #include <stdlib.h> |
20 #include <queue> | 24 #include <queue> |
21 #include <utility> | 25 #include <utility> |
22 | 26 |
23 #include "base/compiler_specific.h" | 27 #include "base/compiler_specific.h" |
24 #include "base/files/file_path.h" | 28 #include "base/files/file_path.h" |
25 #include "base/files/file_util.h" | 29 #include "base/files/file_util.h" |
26 #include "base/location.h" | 30 #include "base/location.h" |
27 #include "base/logging.h" | 31 #include "base/logging.h" |
28 #include "base/macros.h" | 32 #include "base/macros.h" |
29 #include "base/message_loop/message_loop.h" | 33 #include "base/message_loop/message_loop.h" |
30 #include "base/single_thread_task_runner.h" | 34 #include "base/single_thread_task_runner.h" |
31 #include "base/thread_task_runner_handle.h" | 35 #include "base/thread_task_runner_handle.h" |
32 #include "crypto/nss_util.h" | 36 #include "crypto/nss_util.h" |
33 #include "crypto/rsa_private_key.h" | 37 #include "crypto/rsa_private_key.h" |
38 #include "crypto/signature_creator.h" | |
34 #include "net/base/address_list.h" | 39 #include "net/base/address_list.h" |
35 #include "net/base/completion_callback.h" | 40 #include "net/base/completion_callback.h" |
36 #include "net/base/host_port_pair.h" | 41 #include "net/base/host_port_pair.h" |
37 #include "net/base/io_buffer.h" | 42 #include "net/base/io_buffer.h" |
38 #include "net/base/ip_endpoint.h" | 43 #include "net/base/ip_endpoint.h" |
39 #include "net/base/net_errors.h" | 44 #include "net/base/net_errors.h" |
40 #include "net/base/test_data_directory.h" | 45 #include "net/base/test_data_directory.h" |
41 #include "net/cert/cert_status_flags.h" | 46 #include "net/cert/cert_status_flags.h" |
42 #include "net/cert/mock_cert_verifier.h" | 47 #include "net/cert/mock_cert_verifier.h" |
48 #include "net/cert/mock_client_cert_verifier.h" | |
43 #include "net/cert/x509_certificate.h" | 49 #include "net/cert/x509_certificate.h" |
44 #include "net/http/transport_security_state.h" | 50 #include "net/http/transport_security_state.h" |
45 #include "net/log/net_log.h" | 51 #include "net/log/net_log.h" |
46 #include "net/socket/client_socket_factory.h" | 52 #include "net/socket/client_socket_factory.h" |
47 #include "net/socket/socket_test_util.h" | 53 #include "net/socket/socket_test_util.h" |
48 #include "net/socket/ssl_client_socket.h" | 54 #include "net/socket/ssl_client_socket.h" |
49 #include "net/socket/stream_socket.h" | 55 #include "net/socket/stream_socket.h" |
56 #include "net/ssl/ssl_cert_request_info.h" | |
50 #include "net/ssl/ssl_cipher_suite_names.h" | 57 #include "net/ssl/ssl_cipher_suite_names.h" |
51 #include "net/ssl/ssl_connection_status_flags.h" | 58 #include "net/ssl/ssl_connection_status_flags.h" |
52 #include "net/ssl/ssl_info.h" | 59 #include "net/ssl/ssl_info.h" |
60 #include "net/ssl/ssl_private_key.h" | |
53 #include "net/ssl/ssl_server_config.h" | 61 #include "net/ssl/ssl_server_config.h" |
54 #include "net/test/cert_test_util.h" | 62 #include "net/test/cert_test_util.h" |
55 #include "testing/gtest/include/gtest/gtest.h" | 63 #include "testing/gtest/include/gtest/gtest.h" |
56 #include "testing/platform_test.h" | 64 #include "testing/platform_test.h" |
57 | 65 |
58 namespace net { | 66 namespace net { |
59 | 67 |
60 namespace { | 68 namespace { |
61 | 69 |
70 const char kClientCertFileName[] = "client_1.pem"; | |
71 const char kClientPrivateKeyFileName[] = "client_1.pk8"; | |
72 const char kWrongClientCertFileName[] = "client_2.pem"; | |
73 const char kWrongClientPrivateKeyFileName[] = "client_2.pk8"; | |
74 const char kClientCertCAFileName[] = "client_1_ca.pem"; | |
75 | |
62 class FakeDataChannel { | 76 class FakeDataChannel { |
63 public: | 77 public: |
64 FakeDataChannel() | 78 FakeDataChannel() |
65 : read_buf_len_(0), | 79 : read_buf_len_(0), |
66 closed_(false), | 80 closed_(false), |
67 write_called_after_close_(false), | 81 write_called_after_close_(false), |
68 weak_factory_(this) { | 82 weak_factory_(this) { |
69 } | 83 } |
70 | 84 |
71 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { | 85 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 weak_factory_.GetWeakPtr())); | 117 weak_factory_.GetWeakPtr())); |
104 return buf_len; | 118 return buf_len; |
105 } | 119 } |
106 | 120 |
107 // Closes the FakeDataChannel. After Close() is called, Read() returns 0, | 121 // Closes the FakeDataChannel. After Close() is called, Read() returns 0, |
108 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that | 122 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that |
109 // after the FakeDataChannel is closed, the first Write() call completes | 123 // after the FakeDataChannel is closed, the first Write() call completes |
110 // asynchronously, which is necessary to reproduce bug 127822. | 124 // asynchronously, which is necessary to reproduce bug 127822. |
111 void Close() { | 125 void Close() { |
112 closed_ = true; | 126 closed_ = true; |
127 data_.push( | |
128 new DrainableIOBuffer(new StringIOBuffer(std::string("0", 1)), 1)); | |
129 if (!read_callback_.is_null()) { | |
130 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
131 FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback, | |
132 weak_factory_.GetWeakPtr())); | |
133 } | |
113 } | 134 } |
114 | 135 |
115 private: | 136 private: |
116 void DoReadCallback() { | 137 void DoReadCallback() { |
117 if (read_callback_.is_null() || data_.empty()) | 138 if (read_callback_.is_null() || data_.empty()) |
118 return; | 139 return; |
119 | |
120 int copied = PropagateData(read_buf_, read_buf_len_); | 140 int copied = PropagateData(read_buf_, read_buf_len_); |
121 CompletionCallback callback = read_callback_; | 141 CompletionCallback callback = read_callback_; |
122 read_callback_.Reset(); | 142 read_callback_.Reset(); |
123 read_buf_ = NULL; | 143 read_buf_ = NULL; |
124 read_buf_len_ = 0; | 144 read_buf_len_ = 0; |
125 callback.Run(copied); | 145 callback.Run(copied); |
126 } | 146 } |
127 | 147 |
128 void DoWriteCallback() { | 148 void DoWriteCallback() { |
129 if (write_callback_.is_null()) | 149 if (write_callback_.is_null()) |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
248 } | 268 } |
249 | 269 |
250 private: | 270 private: |
251 BoundNetLog net_log_; | 271 BoundNetLog net_log_; |
252 FakeDataChannel* incoming_; | 272 FakeDataChannel* incoming_; |
253 FakeDataChannel* outgoing_; | 273 FakeDataChannel* outgoing_; |
254 | 274 |
255 DISALLOW_COPY_AND_ASSIGN(FakeSocket); | 275 DISALLOW_COPY_AND_ASSIGN(FakeSocket); |
256 }; | 276 }; |
257 | 277 |
278 class TestSSLPrivateKey : public SSLPrivateKey { | |
279 public: | |
280 TestSSLPrivateKey(crypto::RSAPrivateKey* rsa_private_key) | |
281 : rsa_private_key_(rsa_private_key) {} | |
282 | |
283 Type GetType() override { return SSLPrivateKey::Type::RSA; } | |
284 | |
285 std::vector<SSLPrivateKey::Hash> GetDigestPreferences() override { | |
286 static const SSLPrivateKey::Hash kHashes[] = {SSLPrivateKey::Hash::SHA256, | |
287 SSLPrivateKey::Hash::SHA1}; | |
288 return std::vector<SSLPrivateKey::Hash>(std::begin(kHashes), | |
289 std::end(kHashes)); | |
290 } | |
291 | |
292 // NOTE: The following algorithm assumes the answer is a power of 2, which is | |
293 // true for the test keys in use. | |
davidben
2016/01/25 20:56:11
This comment is no longer accurate.
ryanchung
2016/01/29 23:22:13
Done. Thx.
| |
294 size_t GetMaxSignatureLengthInBytes() override { | |
295 #if defined(USE_OPENSSL) | |
davidben
2016/01/25 20:56:11
I would just wrap the whole thing in USE_OPENSSL.
davidben
2016/01/25 20:56:11
#include "build/build_config.h" to condition on US
ryanchung
2016/01/29 23:22:14
I assume you mean wrapping the whole class. Done.
ryanchung
2016/01/29 23:22:14
Done.
| |
296 return EVP_PKEY_size(rsa_private_key_->key()); | |
297 #else | |
298 NOTIMPLEMENTED(); | |
299 return 0; | |
300 #endif | |
301 } | |
302 | |
303 void SignDigest(Hash hash, | |
304 const base::StringPiece& input, | |
305 const SignCallback& callback) override { | |
davidben
2016/01/25 20:56:11
FYI, this implementation will not work for TLS 1.1
ryanchung
2016/01/29 23:22:13
Ok. Thanks.
| |
306 std::vector<uint8_t> signature; | |
307 crypto::SignatureCreator::HashAlgorithm hash_alg; | |
308 switch (hash) { | |
309 case Hash::SHA1: | |
310 hash_alg = crypto::SignatureCreator::SHA1; | |
311 break; | |
312 | |
313 case Hash::SHA256: | |
314 hash_alg = crypto::SignatureCreator::SHA256; | |
315 break; | |
316 | |
317 default: | |
318 FAIL() << "Unsupported hash function"; | |
319 } | |
320 crypto::SignatureCreator::Sign( | |
321 rsa_private_key_.get(), hash_alg, | |
322 reinterpret_cast<const uint8_t*>(input.data()), input.size(), | |
323 &signature); | |
324 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
325 FROM_HERE, base::Bind(callback, OK, signature)); | |
326 } | |
327 | |
328 private: | |
329 ~TestSSLPrivateKey() override {} | |
330 | |
331 scoped_ptr<crypto::RSAPrivateKey> rsa_private_key_; | |
332 | |
333 DISALLOW_COPY_AND_ASSIGN(TestSSLPrivateKey); | |
334 }; | |
335 | |
258 } // namespace | 336 } // namespace |
259 | 337 |
260 // Verify the correctness of the test helper classes first. | 338 // Verify the correctness of the test helper classes first. |
261 TEST(FakeSocketTest, DataTransfer) { | 339 TEST(FakeSocketTest, DataTransfer) { |
262 // Establish channels between two sockets. | 340 // Establish channels between two sockets. |
263 FakeDataChannel channel_1; | 341 FakeDataChannel channel_1; |
264 FakeDataChannel channel_2; | 342 FakeDataChannel channel_2; |
265 FakeSocket client(&channel_1, &channel_2); | 343 FakeSocket client(&channel_1, &channel_2); |
266 FakeSocket server(&channel_2, &channel_1); | 344 FakeSocket server(&channel_2, &channel_1); |
267 | 345 |
(...skipping 27 matching lines...) Expand all Loading... | |
295 EXPECT_GT(read, 0); | 373 EXPECT_GT(read, 0); |
296 EXPECT_LE(read, written); | 374 EXPECT_LE(read, written); |
297 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read)); | 375 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read)); |
298 } | 376 } |
299 | 377 |
300 class SSLServerSocketTest : public PlatformTest { | 378 class SSLServerSocketTest : public PlatformTest { |
301 public: | 379 public: |
302 SSLServerSocketTest() | 380 SSLServerSocketTest() |
303 : socket_factory_(ClientSocketFactory::GetDefaultFactory()), | 381 : socket_factory_(ClientSocketFactory::GetDefaultFactory()), |
304 cert_verifier_(new MockCertVerifier()), | 382 cert_verifier_(new MockCertVerifier()), |
383 client_cert_verifier_(new MockClientCertVerifier()), | |
305 transport_security_state_(new TransportSecurityState) { | 384 transport_security_state_(new TransportSecurityState) { |
306 cert_verifier_->set_default_result(CERT_STATUS_AUTHORITY_INVALID); | 385 cert_verifier_->set_default_result(ERR_CERT_AUTHORITY_INVALID); |
386 client_cert_verifier_->set_default_result(ERR_CERT_AUTHORITY_INVALID); | |
307 } | 387 } |
308 | 388 |
309 protected: | 389 protected: |
310 void Initialize() { | 390 void Initialize() { |
311 scoped_ptr<ClientSocketHandle> client_connection(new ClientSocketHandle); | 391 scoped_ptr<ClientSocketHandle> client_connection(new ClientSocketHandle); |
312 client_connection->SetSocket( | 392 client_connection->SetSocket( |
313 scoped_ptr<StreamSocket>(new FakeSocket(&channel_1_, &channel_2_))); | 393 scoped_ptr<StreamSocket>(new FakeSocket(&channel_1_, &channel_2_))); |
314 scoped_ptr<StreamSocket> server_socket( | 394 scoped_ptr<StreamSocket> server_socket( |
315 new FakeSocket(&channel_2_, &channel_1_)); | 395 new FakeSocket(&channel_2_, &channel_1_)); |
316 | 396 |
317 base::FilePath certs_dir(GetTestCertsDirectory()); | 397 std::string server_cert_der; |
318 | 398 scoped_refptr<X509Certificate> server_cert( |
319 base::FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); | 399 ReadTestCert("unittest.selfsigned.der", &server_cert_der)); |
320 std::string cert_der; | 400 scoped_ptr<crypto::RSAPrivateKey> server_private_key( |
321 ASSERT_TRUE(base::ReadFileToString(cert_path, &cert_der)); | 401 ReadTestKey("unittest.key.bin")); |
322 | |
323 scoped_refptr<X509Certificate> cert = | |
324 X509Certificate::CreateFromBytes(cert_der.data(), cert_der.size()); | |
325 | |
326 base::FilePath key_path = certs_dir.AppendASCII("unittest.key.bin"); | |
327 std::string key_string; | |
328 ASSERT_TRUE(base::ReadFileToString(key_path, &key_string)); | |
329 std::vector<uint8_t> key_vector( | |
330 reinterpret_cast<const uint8_t*>(key_string.data()), | |
331 reinterpret_cast<const uint8_t*>(key_string.data() + | |
332 key_string.length())); | |
333 | |
334 scoped_ptr<crypto::RSAPrivateKey> private_key( | |
335 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); | |
336 | 402 |
337 client_ssl_config_.false_start_enabled = false; | 403 client_ssl_config_.false_start_enabled = false; |
338 client_ssl_config_.channel_id_enabled = false; | 404 client_ssl_config_.channel_id_enabled = false; |
339 | 405 |
340 // Certificate provided by the host doesn't need authority. | 406 // Certificate provided by the host doesn't need authority. |
341 SSLConfig::CertAndStatus cert_and_status; | 407 SSLConfig::CertAndStatus cert_and_status; |
342 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID; | 408 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID; |
343 cert_and_status.der_cert = cert_der; | 409 cert_and_status.der_cert = server_cert_der; |
344 client_ssl_config_.allowed_bad_certs.push_back(cert_and_status); | 410 client_ssl_config_.allowed_bad_certs.push_back(cert_and_status); |
345 | 411 |
346 HostPortPair host_and_pair("unittest", 0); | 412 HostPortPair host_and_pair("unittest", 0); |
347 SSLClientSocketContext context; | 413 SSLClientSocketContext context; |
348 context.cert_verifier = cert_verifier_.get(); | 414 context.cert_verifier = cert_verifier_.get(); |
349 context.transport_security_state = transport_security_state_.get(); | 415 context.transport_security_state = transport_security_state_.get(); |
416 socket_factory_->ClearSSLSessionCache(); | |
350 client_socket_ = socket_factory_->CreateSSLClientSocket( | 417 client_socket_ = socket_factory_->CreateSSLClientSocket( |
351 std::move(client_connection), host_and_pair, client_ssl_config_, | 418 std::move(client_connection), host_and_pair, client_ssl_config_, |
352 context); | 419 context); |
353 server_socket_ = CreateSSLServerSocket(std::move(server_socket), cert.get(), | 420 server_socket_ = |
354 *private_key, server_ssl_config_); | 421 CreateSSLServerSocket(std::move(server_socket), server_cert.get(), |
422 *server_private_key, server_ssl_config_); | |
423 } | |
424 | |
425 void ConfigureClientCertsForClient(const char* cert_file_name, | |
426 const char* private_key_file_name) { | |
427 scoped_refptr<X509Certificate> cert; | |
428 scoped_refptr<net::SSLPrivateKey> key; | |
429 if (cert_file_name && private_key_file_name) { | |
430 cert = ImportCertFromFile(GetTestCertsDirectory(), cert_file_name); | |
431 key = new TestSSLPrivateKey(ReadTestKey(private_key_file_name)); | |
432 } | |
433 client_ssl_config_.send_client_cert = true; | |
434 client_ssl_config_.client_cert = cert; | |
435 client_ssl_config_.client_private_key = key; | |
436 } | |
437 | |
438 void ConfigureClientCertsForServer(bool cert_expected) { | |
davidben
2016/01/25 20:56:11
This parameter seems to be always true. (And indee
ryanchung
2016/01/29 23:22:13
True. Removed parameter.
| |
439 if (!cert_expected) | |
440 return; | |
441 | |
442 server_ssl_config_.require_client_cert = true; | |
443 | |
444 #if defined(USE_OPENSSL) | |
davidben
2016/01/25 20:56:11
This isn't even going to work if we're not USE_OPE
ryanchung
2016/01/29 23:22:14
Done.
| |
445 STACK_OF(X509_NAME)* cert_names; | |
446 cert_names = SSL_load_client_CA_file(GetTestCertsDirectory() | |
447 .AppendASCII(kClientCertCAFileName) | |
448 .MaybeAsASCII() | |
449 .data()); | |
davidben
2016/01/25 20:56:11
Nit: data -> c_str.
In C++11, it doesn't actually
ryanchung
2016/01/29 23:22:13
Done.
| |
450 if (cert_names != NULL) { | |
davidben
2016/01/25 20:56:11
NULL -> nullptr throughout this file.
davidben
2016/01/25 20:56:11
This should be an ASSERT_TRUE or CHECK or somethin
ryanchung
2016/01/29 23:22:14
Done. Good point.
ryanchung
2016/01/29 23:22:14
Done.
| |
451 for (size_t i = 0; i < sk_X509_NAME_num(cert_names); ++i) { | |
452 unsigned char* str = NULL; | |
davidben
2016/01/25 20:56:10
uint8_t
ryanchung
2016/01/29 23:22:14
Done.
| |
453 int length = i2d_X509_NAME(sk_X509_NAME_value(cert_names, i), &str); | |
davidben
2016/01/25 20:56:11
This leaks memory. You need to OPENSSL_free(str) a
ryanchung
2016/01/29 23:22:13
Done. Thanks.
| |
454 server_ssl_config_.cert_authorities_.push_back(std::string( | |
455 reinterpret_cast<const char*>(str), static_cast<size_t>(length))); | |
456 } | |
457 } | |
458 #endif | |
459 scoped_refptr<X509Certificate> expected_client_cert( | |
460 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName)); | |
461 client_cert_verifier_->AddResultForCert(expected_client_cert.get(), OK); | |
462 | |
463 server_ssl_config_.client_cert_verifier = client_cert_verifier_.get(); | |
464 } | |
465 | |
466 X509Certificate* ReadTestCert(const base::StringPiece& name, | |
davidben
2016/01/25 20:56:11
Should return a scoped_refptr, not a raw pointer.
ryanchung
2016/01/29 23:22:13
Done.
| |
467 std::string* cert_der) { | |
davidben
2016/01/25 20:56:11
Why do you sometimes use ReadTestCert and sometime
ryanchung
2016/01/29 23:22:13
I'll remove this function.
I believe it was to als
| |
468 base::FilePath certs_dir(GetTestCertsDirectory()); | |
469 base::FilePath cert_path = certs_dir.AppendASCII(name); | |
470 std::string unused; | |
471 if (!cert_der) | |
472 cert_der = &unused; | |
473 if (!base::ReadFileToString(cert_path, cert_der)) | |
474 return NULL; | |
475 return X509Certificate::CreateFromBytes(cert_der->data(), cert_der->size()); | |
476 } | |
477 | |
478 crypto::RSAPrivateKey* ReadTestKey(const base::StringPiece& name) { | |
davidben
2016/01/25 20:56:10
Should return a scoped_ptr, not a raw pointer.
ryanchung
2016/01/29 23:22:13
Done.
| |
479 base::FilePath certs_dir(GetTestCertsDirectory()); | |
480 base::FilePath key_path = certs_dir.AppendASCII(name); | |
481 std::string key_string; | |
482 if (!base::ReadFileToString(key_path, &key_string)) | |
483 return NULL; | |
484 std::vector<uint8_t> key_vector( | |
485 reinterpret_cast<const uint8_t*>(key_string.data()), | |
486 reinterpret_cast<const uint8_t*>(key_string.data() + | |
487 key_string.length())); | |
488 return crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector); | |
355 } | 489 } |
356 | 490 |
357 FakeDataChannel channel_1_; | 491 FakeDataChannel channel_1_; |
358 FakeDataChannel channel_2_; | 492 FakeDataChannel channel_2_; |
359 SSLConfig client_ssl_config_; | 493 SSLConfig client_ssl_config_; |
360 SSLServerConfig server_ssl_config_; | 494 SSLServerConfig server_ssl_config_; |
361 scoped_ptr<SSLClientSocket> client_socket_; | 495 scoped_ptr<SSLClientSocket> client_socket_; |
362 scoped_ptr<SSLServerSocket> server_socket_; | 496 scoped_ptr<SSLServerSocket> server_socket_; |
363 ClientSocketFactory* socket_factory_; | 497 ClientSocketFactory* socket_factory_; |
364 scoped_ptr<MockCertVerifier> cert_verifier_; | 498 scoped_ptr<MockCertVerifier> cert_verifier_; |
499 scoped_ptr<MockClientCertVerifier> client_cert_verifier_; | |
365 scoped_ptr<TransportSecurityState> transport_security_state_; | 500 scoped_ptr<TransportSecurityState> transport_security_state_; |
501 CertificateList trusted_certs_; | |
davidben
2016/01/25 20:56:11
Unused?
ryanchung
2016/01/29 23:22:14
Done.
| |
366 }; | 502 }; |
367 | 503 |
368 // This test only executes creation of client and server sockets. This is to | 504 // This test only executes creation of client and server sockets. This is to |
369 // test that creation of sockets doesn't crash and have minimal code to run | 505 // test that creation of sockets doesn't crash and have minimal code to run |
370 // under valgrind in order to help debugging memory problems. | 506 // under valgrind in order to help debugging memory problems. |
371 TEST_F(SSLServerSocketTest, Initialize) { | 507 TEST_F(SSLServerSocketTest, Initialize) { |
372 Initialize(); | 508 Initialize(); |
373 } | 509 } |
374 | 510 |
375 // This test executes Connect() on SSLClientSocket and Handshake() on | 511 // This test executes Connect() on SSLClientSocket and Handshake() on |
376 // SSLServerSocket to make sure handshaking between the two sockets is | 512 // SSLServerSocket to make sure handshaking between the two sockets is |
377 // completed successfully. | 513 // completed successfully. |
378 TEST_F(SSLServerSocketTest, Handshake) { | 514 TEST_F(SSLServerSocketTest, Handshake) { |
379 Initialize(); | 515 Initialize(); |
380 | 516 |
517 TestCompletionCallback handshake_callback; | |
518 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | |
519 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); | |
davidben
2016/01/25 20:56:11
(It's in the original too, but I wouldn't bother w
ryanchung
2016/01/29 23:22:13
Done.
| |
520 | |
381 TestCompletionCallback connect_callback; | 521 TestCompletionCallback connect_callback; |
382 TestCompletionCallback handshake_callback; | 522 int client_ret = client_socket_->Connect(connect_callback.callback()); |
523 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | |
davidben
2016/01/25 20:56:11
(Ditto.)
ryanchung
2016/01/29 23:22:13
Done.
| |
383 | 524 |
384 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 525 client_ret = connect_callback.GetResult(client_ret); |
385 EXPECT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); | 526 server_ret = handshake_callback.GetResult(server_ret); |
386 | 527 |
387 int client_ret = client_socket_->Connect(connect_callback.callback()); | 528 ASSERT_EQ(OK, client_ret); |
388 EXPECT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | 529 ASSERT_EQ(OK, server_ret); |
389 | |
390 if (client_ret == ERR_IO_PENDING) { | |
391 EXPECT_EQ(OK, connect_callback.WaitForResult()); | |
392 } | |
393 if (server_ret == ERR_IO_PENDING) { | |
394 EXPECT_EQ(OK, handshake_callback.WaitForResult()); | |
395 } | |
396 | 530 |
397 // Make sure the cert status is expected. | 531 // Make sure the cert status is expected. |
398 SSLInfo ssl_info; | 532 SSLInfo ssl_info; |
399 ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info)); | 533 ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info)); |
400 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); | 534 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); |
401 | 535 |
402 // The default cipher suite should be ECDHE and, unless on NSS and the | 536 // The default cipher suite should be ECDHE and, unless on NSS and the |
403 // platform doesn't support it, an AEAD. | 537 // platform doesn't support it, an AEAD. |
404 uint16_t cipher_suite = | 538 uint16_t cipher_suite = |
405 SSLConnectionStatusToCipherSuite(ssl_info.connection_status); | 539 SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
406 const char* key_exchange; | 540 const char* key_exchange; |
407 const char* cipher; | 541 const char* cipher; |
408 const char* mac; | 542 const char* mac; |
409 bool is_aead; | 543 bool is_aead; |
410 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite); | 544 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite); |
411 EXPECT_STREQ("ECDHE_RSA", key_exchange); | 545 EXPECT_STREQ("ECDHE_RSA", key_exchange); |
412 EXPECT_TRUE(is_aead); | 546 EXPECT_TRUE(is_aead); |
413 } | 547 } |
414 | 548 |
549 // NSS ports don't support client certificates | |
davidben
2016/01/25 20:56:11
Nit: period at end.
ryanchung
2016/01/29 23:22:13
Done.
| |
550 #if defined(USE_OPENSSL) | |
551 | |
552 // This test executes Connect() on SSLClientSocket and Handshake() on | |
553 // SSLServerSocket to make sure handshaking between the two sockets is | |
554 // completed successfully, using client certificate. | |
555 TEST_F(SSLServerSocketTest, HandshakeWithClientCert) { | |
556 scoped_refptr<X509Certificate> client_cert = | |
557 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); | |
558 ConfigureClientCertsForClient(kClientCertFileName, kClientPrivateKeyFileName); | |
559 ConfigureClientCertsForServer(true); | |
560 Initialize(); | |
561 | |
562 TestCompletionCallback handshake_callback; | |
563 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | |
564 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); | |
davidben
2016/01/25 20:56:11
(I wouldn't bother with this line. Redundant with
ryanchung
2016/01/29 23:22:13
Done.
| |
565 | |
566 TestCompletionCallback connect_callback; | |
567 int client_ret = client_socket_->Connect(connect_callback.callback()); | |
568 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | |
davidben
2016/01/25 20:56:11
(Ditto.)
ryanchung
2016/01/29 23:22:13
Done.
| |
569 | |
570 client_ret = connect_callback.GetResult(client_ret); | |
571 server_ret = handshake_callback.GetResult(server_ret); | |
572 | |
573 ASSERT_EQ(OK, client_ret); | |
574 ASSERT_EQ(OK, server_ret); | |
575 | |
576 // Make sure the cert status is expected. | |
577 SSLInfo ssl_info; | |
578 client_socket_->GetSSLInfo(&ssl_info); | |
579 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); | |
580 server_socket_->GetSSLInfo(&ssl_info); | |
581 EXPECT_TRUE(ssl_info.cert.get()); | |
582 EXPECT_TRUE(client_cert->Equals(ssl_info.cert.get())); | |
583 } | |
584 | |
585 TEST_F(SSLServerSocketTest, HandshakeWithClientCertRequiredNotSupplied) { | |
586 ConfigureClientCertsForServer(true); | |
587 Initialize(); | |
588 // Use the default setting for the client socket, which is to not send | |
589 // a client certificate. This will cause the client to receive an | |
590 // ERR_SSL_CLIENT_AUTH_CERT_NEEDED error, and allow for inspecting the | |
591 // requested cert_authorities from the CertificateRequest sent by the | |
592 // server. | |
593 | |
594 TestCompletionCallback handshake_callback; | |
595 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | |
596 ASSERT_EQ(server_ret, ERR_IO_PENDING); | |
davidben
2016/01/25 20:56:11
(Strictly speaking, not redundant this time, but I
ryanchung
2016/01/29 23:22:13
Done.
| |
597 | |
598 TestCompletionCallback connect_callback; | |
599 EXPECT_EQ(ERR_SSL_CLIENT_AUTH_CERT_NEEDED, | |
600 connect_callback.GetResult( | |
601 client_socket_->Connect(connect_callback.callback()))); | |
602 | |
603 scoped_refptr<SSLCertRequestInfo> request_info = new SSLCertRequestInfo(); | |
604 client_socket_->GetSSLCertRequestInfo(request_info.get()); | |
605 | |
606 // Check that the authority name that arrived in the CertificateRequest | |
607 // handshake message is as expected. | |
608 scoped_refptr<X509Certificate> client_cert = | |
609 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); | |
610 EXPECT_TRUE(client_cert->IsIssuedByEncoded(request_info->cert_authorities)); | |
611 | |
612 client_socket_->Disconnect(); | |
613 | |
614 EXPECT_EQ(ERR_FAILED, handshake_callback.GetResult(server_ret)); | |
davidben
2016/01/25 20:56:11
Why is this mapping to such a generic error code?
ryanchung
2016/01/29 23:22:13
This is the error code when the connection is clos
davidben
2016/02/04 00:40:11
Oh, right. I haven't ported the better error handl
| |
615 } | |
616 | |
617 TEST_F(SSLServerSocketTest, HandshakeWithWrongClientCertSupplied) { | |
618 scoped_refptr<X509Certificate> client_cert = | |
619 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); | |
620 ConfigureClientCertsForClient(kWrongClientCertFileName, | |
621 kWrongClientPrivateKeyFileName); | |
622 ConfigureClientCertsForServer(true); | |
623 Initialize(); | |
624 | |
625 TestCompletionCallback handshake_callback; | |
626 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | |
627 EXPECT_EQ(server_ret, ERR_IO_PENDING); | |
davidben
2016/01/25 20:56:11
(Ditto.)
ryanchung
2016/01/29 23:22:13
Done.
| |
628 | |
629 TestCompletionCallback connect_callback; | |
630 int client_ret = client_socket_->Connect(connect_callback.callback()); | |
631 | |
632 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, | |
633 connect_callback.GetResult(client_ret)); | |
634 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, | |
635 handshake_callback.GetResult(server_ret)); | |
636 } | |
637 #endif // defined(USE_OPENSSL) | |
638 | |
415 TEST_F(SSLServerSocketTest, DataTransfer) { | 639 TEST_F(SSLServerSocketTest, DataTransfer) { |
416 Initialize(); | 640 Initialize(); |
417 | 641 |
642 // Establish connection. | |
418 TestCompletionCallback connect_callback; | 643 TestCompletionCallback connect_callback; |
419 TestCompletionCallback handshake_callback; | |
420 | |
421 // Establish connection. | |
422 int client_ret = client_socket_->Connect(connect_callback.callback()); | 644 int client_ret = client_socket_->Connect(connect_callback.callback()); |
423 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | 645 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); |
424 | 646 |
647 TestCompletionCallback handshake_callback; | |
425 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 648 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
426 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); | 649 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); |
427 | 650 |
428 client_ret = connect_callback.GetResult(client_ret); | 651 client_ret = connect_callback.GetResult(client_ret); |
429 ASSERT_EQ(OK, client_ret); | 652 ASSERT_EQ(OK, client_ret); |
430 server_ret = handshake_callback.GetResult(server_ret); | 653 server_ret = handshake_callback.GetResult(server_ret); |
431 ASSERT_EQ(OK, server_ret); | 654 ASSERT_EQ(OK, server_ret); |
432 | 655 |
433 const int kReadBufSize = 1024; | 656 const int kReadBufSize = 1024; |
434 scoped_refptr<StringIOBuffer> write_buf = | 657 scoped_refptr<StringIOBuffer> write_buf = |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
492 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size())); | 715 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size())); |
493 } | 716 } |
494 | 717 |
495 // A regression test for bug 127822 (http://crbug.com/127822). | 718 // A regression test for bug 127822 (http://crbug.com/127822). |
496 // If the server closes the connection after the handshake is finished, | 719 // If the server closes the connection after the handshake is finished, |
497 // the client's Write() call should not cause an infinite loop. | 720 // the client's Write() call should not cause an infinite loop. |
498 // NOTE: this is a test for SSLClientSocket rather than SSLServerSocket. | 721 // NOTE: this is a test for SSLClientSocket rather than SSLServerSocket. |
499 TEST_F(SSLServerSocketTest, ClientWriteAfterServerClose) { | 722 TEST_F(SSLServerSocketTest, ClientWriteAfterServerClose) { |
500 Initialize(); | 723 Initialize(); |
501 | 724 |
502 TestCompletionCallback connect_callback; | |
503 TestCompletionCallback handshake_callback; | |
504 | 725 |
505 // Establish connection. | 726 // Establish connection. |
727 TestCompletionCallback connect_callback; | |
506 int client_ret = client_socket_->Connect(connect_callback.callback()); | 728 int client_ret = client_socket_->Connect(connect_callback.callback()); |
507 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | 729 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); |
508 | 730 |
731 TestCompletionCallback handshake_callback; | |
509 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 732 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
510 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); | 733 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); |
511 | 734 |
512 client_ret = connect_callback.GetResult(client_ret); | 735 client_ret = connect_callback.GetResult(client_ret); |
513 ASSERT_EQ(OK, client_ret); | 736 ASSERT_EQ(OK, client_ret); |
514 server_ret = handshake_callback.GetResult(server_ret); | 737 server_ret = handshake_callback.GetResult(server_ret); |
515 ASSERT_EQ(OK, server_ret); | 738 ASSERT_EQ(OK, server_ret); |
516 | 739 |
517 scoped_refptr<StringIOBuffer> write_buf = new StringIOBuffer("testing123"); | 740 scoped_refptr<StringIOBuffer> write_buf = new StringIOBuffer("testing123"); |
518 | 741 |
519 // The server closes the connection. The server needs to write some | 742 // The server closes the connection. The server needs to write some |
520 // data first so that the client's Read() calls from the transport | 743 // data first so that the client's Read() calls from the transport |
521 // socket won't return ERR_IO_PENDING. This ensures that the client | 744 // socket won't return ERR_IO_PENDING. This ensures that the client |
522 // will call Read() on the transport socket again. | 745 // will call Read() on the transport socket again. |
523 TestCompletionCallback write_callback; | 746 TestCompletionCallback write_callback; |
524 | |
525 server_ret = server_socket_->Write( | 747 server_ret = server_socket_->Write( |
526 write_buf.get(), write_buf->size(), write_callback.callback()); | 748 write_buf.get(), write_buf->size(), write_callback.callback()); |
527 EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING); | 749 EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING); |
528 | 750 |
529 server_ret = write_callback.GetResult(server_ret); | 751 server_ret = write_callback.GetResult(server_ret); |
530 EXPECT_GT(server_ret, 0); | 752 EXPECT_GT(server_ret, 0); |
531 | 753 |
532 server_socket_->Disconnect(); | 754 server_socket_->Disconnect(); |
533 | 755 |
534 // The client writes some data. This should not cause an infinite loop. | 756 // The client writes some data. This should not cause an infinite loop. |
(...skipping 10 matching lines...) Expand all Loading... | |
545 base::MessageLoop::current()->Run(); | 767 base::MessageLoop::current()->Run(); |
546 } | 768 } |
547 | 769 |
548 // This test executes ExportKeyingMaterial() on the client and server sockets, | 770 // This test executes ExportKeyingMaterial() on the client and server sockets, |
549 // after connecting them, and verifies that the results match. | 771 // after connecting them, and verifies that the results match. |
550 // This test will fail if False Start is enabled (see crbug.com/90208). | 772 // This test will fail if False Start is enabled (see crbug.com/90208). |
551 TEST_F(SSLServerSocketTest, ExportKeyingMaterial) { | 773 TEST_F(SSLServerSocketTest, ExportKeyingMaterial) { |
552 Initialize(); | 774 Initialize(); |
553 | 775 |
554 TestCompletionCallback connect_callback; | 776 TestCompletionCallback connect_callback; |
555 TestCompletionCallback handshake_callback; | |
556 | |
557 int client_ret = client_socket_->Connect(connect_callback.callback()); | 777 int client_ret = client_socket_->Connect(connect_callback.callback()); |
558 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | 778 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); |
559 | 779 |
780 TestCompletionCallback handshake_callback; | |
560 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 781 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
561 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); | 782 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); |
562 | 783 |
563 if (client_ret == ERR_IO_PENDING) { | 784 if (client_ret == ERR_IO_PENDING) { |
564 ASSERT_EQ(OK, connect_callback.WaitForResult()); | 785 ASSERT_EQ(OK, connect_callback.WaitForResult()); |
565 } | 786 } |
566 if (server_ret == ERR_IO_PENDING) { | 787 if (server_ret == ERR_IO_PENDING) { |
567 ASSERT_EQ(OK, handshake_callback.WaitForResult()); | 788 ASSERT_EQ(OK, handshake_callback.WaitForResult()); |
568 } | 789 } |
569 | 790 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
609 }; | 830 }; |
610 client_ssl_config_.disabled_cipher_suites.assign( | 831 client_ssl_config_.disabled_cipher_suites.assign( |
611 kEcdheCiphers, kEcdheCiphers + arraysize(kEcdheCiphers)); | 832 kEcdheCiphers, kEcdheCiphers + arraysize(kEcdheCiphers)); |
612 | 833 |
613 // Require ECDHE on the server. | 834 // Require ECDHE on the server. |
614 server_ssl_config_.require_ecdhe = true; | 835 server_ssl_config_.require_ecdhe = true; |
615 | 836 |
616 Initialize(); | 837 Initialize(); |
617 | 838 |
618 TestCompletionCallback connect_callback; | 839 TestCompletionCallback connect_callback; |
840 int client_ret = client_socket_->Connect(connect_callback.callback()); | |
841 | |
619 TestCompletionCallback handshake_callback; | 842 TestCompletionCallback handshake_callback; |
620 | |
621 int client_ret = client_socket_->Connect(connect_callback.callback()); | |
622 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 843 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
623 | 844 |
624 client_ret = connect_callback.GetResult(client_ret); | 845 client_ret = connect_callback.GetResult(client_ret); |
625 server_ret = handshake_callback.GetResult(server_ret); | 846 server_ret = handshake_callback.GetResult(server_ret); |
626 | 847 |
627 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, client_ret); | 848 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, client_ret); |
628 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, server_ret); | 849 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, server_ret); |
629 } | 850 } |
630 | 851 |
631 } // namespace net | 852 } // namespace net |
OLD | NEW |