Chromium Code Reviews| 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 <stdint.h> | 18 #include <stdint.h> |
| 19 #include <stdlib.h> | 19 #include <stdlib.h> |
| 20 #include <queue> | 20 #include <queue> |
| 21 #include <utility> | 21 #include <utility> |
| 22 | 22 |
| 23 #include "base/callback_helpers.h" | |
| 23 #include "base/compiler_specific.h" | 24 #include "base/compiler_specific.h" |
| 24 #include "base/files/file_path.h" | 25 #include "base/files/file_path.h" |
| 25 #include "base/files/file_util.h" | 26 #include "base/files/file_util.h" |
| 26 #include "base/location.h" | 27 #include "base/location.h" |
| 27 #include "base/logging.h" | 28 #include "base/logging.h" |
| 28 #include "base/macros.h" | 29 #include "base/macros.h" |
| 29 #include "base/message_loop/message_loop.h" | 30 #include "base/message_loop/message_loop.h" |
| 30 #include "base/single_thread_task_runner.h" | 31 #include "base/single_thread_task_runner.h" |
| 31 #include "base/thread_task_runner_handle.h" | 32 #include "base/thread_task_runner_handle.h" |
| 33 #include "build/build_config.h" | |
| 32 #include "crypto/nss_util.h" | 34 #include "crypto/nss_util.h" |
| 33 #include "crypto/rsa_private_key.h" | 35 #include "crypto/rsa_private_key.h" |
| 36 #include "crypto/scoped_openssl_types.h" | |
| 37 #include "crypto/signature_creator.h" | |
| 34 #include "net/base/address_list.h" | 38 #include "net/base/address_list.h" |
| 35 #include "net/base/completion_callback.h" | 39 #include "net/base/completion_callback.h" |
| 36 #include "net/base/host_port_pair.h" | 40 #include "net/base/host_port_pair.h" |
| 37 #include "net/base/io_buffer.h" | 41 #include "net/base/io_buffer.h" |
| 38 #include "net/base/ip_endpoint.h" | 42 #include "net/base/ip_endpoint.h" |
| 39 #include "net/base/net_errors.h" | 43 #include "net/base/net_errors.h" |
| 40 #include "net/base/test_data_directory.h" | 44 #include "net/base/test_data_directory.h" |
| 41 #include "net/cert/cert_status_flags.h" | 45 #include "net/cert/cert_status_flags.h" |
| 42 #include "net/cert/mock_cert_verifier.h" | 46 #include "net/cert/mock_cert_verifier.h" |
| 47 #include "net/cert/mock_client_cert_verifier.h" | |
| 43 #include "net/cert/x509_certificate.h" | 48 #include "net/cert/x509_certificate.h" |
| 44 #include "net/http/transport_security_state.h" | 49 #include "net/http/transport_security_state.h" |
| 45 #include "net/log/net_log.h" | 50 #include "net/log/net_log.h" |
| 46 #include "net/socket/client_socket_factory.h" | 51 #include "net/socket/client_socket_factory.h" |
| 47 #include "net/socket/socket_test_util.h" | 52 #include "net/socket/socket_test_util.h" |
| 48 #include "net/socket/ssl_client_socket.h" | 53 #include "net/socket/ssl_client_socket.h" |
| 49 #include "net/socket/stream_socket.h" | 54 #include "net/socket/stream_socket.h" |
| 55 #include "net/ssl/scoped_openssl_types.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" |
| 62 #include "net/ssl/test_ssl_private_key.h" | |
| 54 #include "net/test/cert_test_util.h" | 63 #include "net/test/cert_test_util.h" |
| 55 #include "testing/gtest/include/gtest/gtest.h" | 64 #include "testing/gtest/include/gtest/gtest.h" |
| 56 #include "testing/platform_test.h" | 65 #include "testing/platform_test.h" |
| 57 | 66 |
| 67 #if defined(USE_OPENSSL) | |
| 68 #include <openssl/evp.h> | |
| 69 #include <openssl/ssl.h> | |
| 70 #include <openssl/x509.h> | |
| 71 #endif | |
| 72 | |
| 58 namespace net { | 73 namespace net { |
| 59 | 74 |
| 60 namespace { | 75 namespace { |
| 61 | 76 |
| 77 const char kClientCertFileName[] = "client_1.pem"; | |
| 78 const char kClientPrivateKeyFileName[] = "client_1.pk8"; | |
| 79 const char kWrongClientCertFileName[] = "client_2.pem"; | |
| 80 const char kWrongClientPrivateKeyFileName[] = "client_2.pk8"; | |
| 81 const char kClientCertCAFileName[] = "client_1_ca.pem"; | |
| 82 | |
| 62 class FakeDataChannel { | 83 class FakeDataChannel { |
| 63 public: | 84 public: |
| 64 FakeDataChannel() | 85 FakeDataChannel() |
| 65 : read_buf_len_(0), | 86 : read_buf_len_(0), |
| 66 closed_(false), | 87 closed_(false), |
| 67 write_called_after_close_(false), | 88 write_called_after_close_(false), |
| 68 weak_factory_(this) { | 89 weak_factory_(this) { |
| 69 } | 90 } |
| 70 | 91 |
| 71 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { | 92 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())); | 124 weak_factory_.GetWeakPtr())); |
| 104 return buf_len; | 125 return buf_len; |
| 105 } | 126 } |
| 106 | 127 |
| 107 // Closes the FakeDataChannel. After Close() is called, Read() returns 0, | 128 // Closes the FakeDataChannel. After Close() is called, Read() returns 0, |
| 108 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that | 129 // indicating EOF, and Write() fails with ERR_CONNECTION_RESET. Note that |
| 109 // after the FakeDataChannel is closed, the first Write() call completes | 130 // after the FakeDataChannel is closed, the first Write() call completes |
| 110 // asynchronously, which is necessary to reproduce bug 127822. | 131 // asynchronously, which is necessary to reproduce bug 127822. |
| 111 void Close() { | 132 void Close() { |
| 112 closed_ = true; | 133 closed_ = true; |
| 134 if (!read_callback_.is_null()) { | |
| 135 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 136 FROM_HERE, base::Bind(&FakeDataChannel::DoReadCallback, | |
| 137 weak_factory_.GetWeakPtr())); | |
| 138 } | |
| 113 } | 139 } |
| 114 | 140 |
| 115 private: | 141 private: |
| 116 void DoReadCallback() { | 142 void DoReadCallback() { |
| 117 if (read_callback_.is_null() || data_.empty()) | 143 if (read_callback_.is_null()) |
| 144 return; | |
| 145 | |
| 146 if (closed_) { | |
| 147 base::ResetAndReturn(&read_callback_).Run(ERR_CONNECTION_CLOSED); | |
| 148 return; | |
| 149 } | |
| 150 | |
| 151 if (data_.empty()) | |
| 118 return; | 152 return; |
| 119 | 153 |
| 120 int copied = PropagateData(read_buf_, read_buf_len_); | 154 int copied = PropagateData(read_buf_, read_buf_len_); |
| 121 CompletionCallback callback = read_callback_; | 155 CompletionCallback callback = read_callback_; |
| 122 read_callback_.Reset(); | 156 read_callback_.Reset(); |
| 123 read_buf_ = NULL; | 157 read_buf_ = NULL; |
| 124 read_buf_len_ = 0; | 158 read_buf_len_ = 0; |
| 125 callback.Run(copied); | 159 callback.Run(copied); |
| 126 } | 160 } |
| 127 | 161 |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 295 EXPECT_GT(read, 0); | 329 EXPECT_GT(read, 0); |
| 296 EXPECT_LE(read, written); | 330 EXPECT_LE(read, written); |
| 297 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read)); | 331 EXPECT_EQ(0, memcmp(kTestData, read_buf->data(), read)); |
| 298 } | 332 } |
| 299 | 333 |
| 300 class SSLServerSocketTest : public PlatformTest { | 334 class SSLServerSocketTest : public PlatformTest { |
| 301 public: | 335 public: |
| 302 SSLServerSocketTest() | 336 SSLServerSocketTest() |
| 303 : socket_factory_(ClientSocketFactory::GetDefaultFactory()), | 337 : socket_factory_(ClientSocketFactory::GetDefaultFactory()), |
| 304 cert_verifier_(new MockCertVerifier()), | 338 cert_verifier_(new MockCertVerifier()), |
| 339 client_cert_verifier_(new MockClientCertVerifier()), | |
| 305 transport_security_state_(new TransportSecurityState) { | 340 transport_security_state_(new TransportSecurityState) { |
| 306 cert_verifier_->set_default_result(CERT_STATUS_AUTHORITY_INVALID); | 341 cert_verifier_->set_default_result(ERR_CERT_AUTHORITY_INVALID); |
| 342 client_cert_verifier_->set_default_result(ERR_CERT_AUTHORITY_INVALID); | |
| 307 } | 343 } |
| 308 | 344 |
| 309 protected: | 345 protected: |
| 310 void Initialize() { | 346 void Initialize() { |
| 311 scoped_ptr<ClientSocketHandle> client_connection(new ClientSocketHandle); | 347 scoped_ptr<ClientSocketHandle> client_connection(new ClientSocketHandle); |
| 312 client_connection->SetSocket( | 348 client_connection->SetSocket( |
| 313 scoped_ptr<StreamSocket>(new FakeSocket(&channel_1_, &channel_2_))); | 349 scoped_ptr<StreamSocket>(new FakeSocket(&channel_1_, &channel_2_))); |
| 314 scoped_ptr<StreamSocket> server_socket( | 350 scoped_ptr<StreamSocket> server_socket( |
| 315 new FakeSocket(&channel_2_, &channel_1_)); | 351 new FakeSocket(&channel_2_, &channel_1_)); |
| 316 | 352 |
| 317 base::FilePath certs_dir(GetTestCertsDirectory()); | 353 scoped_refptr<X509Certificate> server_cert( |
| 318 | 354 ImportCertFromFile(GetTestCertsDirectory(), "unittest.selfsigned.der")); |
| 319 base::FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); | 355 scoped_ptr<crypto::RSAPrivateKey> server_private_key( |
| 320 std::string cert_der; | 356 ReadTestKey("unittest.key.bin")); |
| 321 ASSERT_TRUE(base::ReadFileToString(cert_path, &cert_der)); | |
| 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 | 357 |
| 337 client_ssl_config_.false_start_enabled = false; | 358 client_ssl_config_.false_start_enabled = false; |
| 338 client_ssl_config_.channel_id_enabled = false; | 359 client_ssl_config_.channel_id_enabled = false; |
| 339 | 360 |
| 340 // Certificate provided by the host doesn't need authority. | 361 // Certificate provided by the host doesn't need authority. |
| 341 SSLConfig::CertAndStatus cert_and_status; | 362 SSLConfig::CertAndStatus cert_and_status; |
| 342 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID; | 363 cert_and_status.cert_status = CERT_STATUS_AUTHORITY_INVALID; |
| 343 cert_and_status.der_cert = cert_der; | 364 std::string server_cert_der; |
| 365 CHECK(X509Certificate::GetDEREncoded(server_cert->os_cert_handle(), | |
| 366 &server_cert_der)); | |
| 367 cert_and_status.der_cert = server_cert_der; | |
| 344 client_ssl_config_.allowed_bad_certs.push_back(cert_and_status); | 368 client_ssl_config_.allowed_bad_certs.push_back(cert_and_status); |
| 345 | 369 |
| 346 HostPortPair host_and_pair("unittest", 0); | 370 HostPortPair host_and_pair("unittest", 0); |
| 347 SSLClientSocketContext context; | 371 SSLClientSocketContext context; |
| 348 context.cert_verifier = cert_verifier_.get(); | 372 context.cert_verifier = cert_verifier_.get(); |
| 349 context.transport_security_state = transport_security_state_.get(); | 373 context.transport_security_state = transport_security_state_.get(); |
| 374 socket_factory_->ClearSSLSessionCache(); | |
| 350 client_socket_ = socket_factory_->CreateSSLClientSocket( | 375 client_socket_ = socket_factory_->CreateSSLClientSocket( |
| 351 std::move(client_connection), host_and_pair, client_ssl_config_, | 376 std::move(client_connection), host_and_pair, client_ssl_config_, |
| 352 context); | 377 context); |
| 353 server_socket_ = CreateSSLServerSocket(std::move(server_socket), cert.get(), | 378 server_socket_ = |
| 354 *private_key, server_ssl_config_); | 379 CreateSSLServerSocket(std::move(server_socket), server_cert.get(), |
| 380 *server_private_key, server_ssl_config_); | |
| 355 } | 381 } |
| 356 | 382 |
| 383 #if defined(USE_OPENSSL) | |
| 384 void ConfigureClientCertsForClient(const char* cert_file_name, | |
| 385 const char* private_key_file_name) { | |
| 386 scoped_refptr<X509Certificate> cert; | |
| 387 scoped_refptr<net::SSLPrivateKey> key; | |
| 388 if (cert_file_name && private_key_file_name) { | |
|
davidben
2016/02/17 22:46:03
No need for the null checks here. I think you coul
ryanchung
2016/02/18 01:07:26
Done.
| |
| 389 cert = ImportCertFromFile(GetTestCertsDirectory(), cert_file_name); | |
| 390 key = WrapOpenSSLPrivateKey(crypto::ScopedEVP_PKEY( | |
| 391 EVP_PKEY_up_ref(ReadTestKey(private_key_file_name)->key()))); | |
| 392 } | |
| 393 client_ssl_config_.send_client_cert = true; | |
| 394 client_ssl_config_.client_cert = cert; | |
| 395 client_ssl_config_.client_private_key = key; | |
| 396 } | |
| 397 | |
| 398 void ConfigureClientCertsForServer() { | |
| 399 server_ssl_config_.client_cert_type = | |
| 400 SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; | |
| 401 | |
| 402 ScopedX509NameStack cert_names( | |
| 403 SSL_load_client_CA_file(GetTestCertsDirectory() | |
| 404 .AppendASCII(kClientCertCAFileName) | |
| 405 .MaybeAsASCII() | |
| 406 .c_str())); | |
| 407 ASSERT_TRUE(cert_names); | |
| 408 for (size_t i = 0; i < sk_X509_NAME_num(cert_names.get()); ++i) { | |
| 409 uint8_t* str = nullptr; | |
| 410 int length = i2d_X509_NAME(sk_X509_NAME_value(cert_names.get(), i), &str); | |
| 411 server_ssl_config_.cert_authorities_.push_back(std::string( | |
| 412 reinterpret_cast<const char*>(str), static_cast<size_t>(length))); | |
| 413 OPENSSL_free(str); | |
| 414 } | |
| 415 | |
| 416 scoped_refptr<X509Certificate> expected_client_cert( | |
| 417 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName)); | |
| 418 client_cert_verifier_->AddResultForCert(expected_client_cert.get(), OK); | |
| 419 | |
| 420 server_ssl_config_.client_cert_verifier = client_cert_verifier_.get(); | |
| 421 } | |
| 422 | |
| 423 scoped_ptr<crypto::RSAPrivateKey> ReadTestKey(const base::StringPiece& name) { | |
| 424 base::FilePath certs_dir(GetTestCertsDirectory()); | |
| 425 base::FilePath key_path = certs_dir.AppendASCII(name); | |
| 426 std::string key_string; | |
| 427 if (!base::ReadFileToString(key_path, &key_string)) | |
| 428 return nullptr; | |
| 429 std::vector<uint8_t> key_vector( | |
| 430 reinterpret_cast<const uint8_t*>(key_string.data()), | |
| 431 reinterpret_cast<const uint8_t*>(key_string.data() + | |
| 432 key_string.length())); | |
|
davidben
2016/02/17 22:46:03
[Ah, our constant switching for char and uint8_t.
| |
| 433 scoped_ptr<crypto::RSAPrivateKey> key( | |
| 434 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); | |
| 435 return key; | |
| 436 } | |
| 437 #endif | |
| 438 | |
| 357 FakeDataChannel channel_1_; | 439 FakeDataChannel channel_1_; |
| 358 FakeDataChannel channel_2_; | 440 FakeDataChannel channel_2_; |
| 359 SSLConfig client_ssl_config_; | 441 SSLConfig client_ssl_config_; |
| 360 SSLServerConfig server_ssl_config_; | 442 SSLServerConfig server_ssl_config_; |
| 361 scoped_ptr<SSLClientSocket> client_socket_; | 443 scoped_ptr<SSLClientSocket> client_socket_; |
| 362 scoped_ptr<SSLServerSocket> server_socket_; | 444 scoped_ptr<SSLServerSocket> server_socket_; |
| 363 ClientSocketFactory* socket_factory_; | 445 ClientSocketFactory* socket_factory_; |
| 364 scoped_ptr<MockCertVerifier> cert_verifier_; | 446 scoped_ptr<MockCertVerifier> cert_verifier_; |
| 447 scoped_ptr<MockClientCertVerifier> client_cert_verifier_; | |
| 365 scoped_ptr<TransportSecurityState> transport_security_state_; | 448 scoped_ptr<TransportSecurityState> transport_security_state_; |
| 366 }; | 449 }; |
| 367 | 450 |
| 368 // This test only executes creation of client and server sockets. This is to | 451 // 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 | 452 // test that creation of sockets doesn't crash and have minimal code to run |
| 370 // under valgrind in order to help debugging memory problems. | 453 // under valgrind in order to help debugging memory problems. |
| 371 TEST_F(SSLServerSocketTest, Initialize) { | 454 TEST_F(SSLServerSocketTest, Initialize) { |
| 372 Initialize(); | 455 Initialize(); |
| 373 } | 456 } |
| 374 | 457 |
| 375 // This test executes Connect() on SSLClientSocket and Handshake() on | 458 // This test executes Connect() on SSLClientSocket and Handshake() on |
| 376 // SSLServerSocket to make sure handshaking between the two sockets is | 459 // SSLServerSocket to make sure handshaking between the two sockets is |
| 377 // completed successfully. | 460 // completed successfully. |
| 378 TEST_F(SSLServerSocketTest, Handshake) { | 461 TEST_F(SSLServerSocketTest, Handshake) { |
| 379 Initialize(); | 462 Initialize(); |
| 380 | 463 |
| 464 TestCompletionCallback handshake_callback; | |
| 465 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | |
| 466 | |
| 381 TestCompletionCallback connect_callback; | 467 TestCompletionCallback connect_callback; |
| 382 TestCompletionCallback handshake_callback; | 468 int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 383 | 469 |
| 384 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 470 client_ret = connect_callback.GetResult(client_ret); |
| 385 EXPECT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); | 471 server_ret = handshake_callback.GetResult(server_ret); |
| 386 | 472 |
| 387 int client_ret = client_socket_->Connect(connect_callback.callback()); | 473 ASSERT_EQ(OK, client_ret); |
| 388 EXPECT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | 474 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 | 475 |
| 397 // Make sure the cert status is expected. | 476 // Make sure the cert status is expected. |
| 398 SSLInfo ssl_info; | 477 SSLInfo ssl_info; |
| 399 ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info)); | 478 ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info)); |
| 400 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); | 479 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); |
| 401 | 480 |
| 402 // The default cipher suite should be ECDHE and, unless on NSS and the | 481 // The default cipher suite should be ECDHE and, unless on NSS and the |
| 403 // platform doesn't support it, an AEAD. | 482 // platform doesn't support it, an AEAD. |
| 404 uint16_t cipher_suite = | 483 uint16_t cipher_suite = |
| 405 SSLConnectionStatusToCipherSuite(ssl_info.connection_status); | 484 SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
| 406 const char* key_exchange; | 485 const char* key_exchange; |
| 407 const char* cipher; | 486 const char* cipher; |
| 408 const char* mac; | 487 const char* mac; |
| 409 bool is_aead; | 488 bool is_aead; |
| 410 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite); | 489 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite); |
| 411 EXPECT_STREQ("ECDHE_RSA", key_exchange); | 490 EXPECT_STREQ("ECDHE_RSA", key_exchange); |
| 412 EXPECT_TRUE(is_aead); | 491 EXPECT_TRUE(is_aead); |
| 413 } | 492 } |
| 414 | 493 |
| 494 // NSS ports don't support client certificates. | |
| 495 #if defined(USE_OPENSSL) | |
| 496 | |
| 497 // This test executes Connect() on SSLClientSocket and Handshake() on | |
| 498 // SSLServerSocket to make sure handshaking between the two sockets is | |
| 499 // completed successfully, using client certificate. | |
| 500 TEST_F(SSLServerSocketTest, HandshakeWithClientCert) { | |
| 501 scoped_refptr<X509Certificate> client_cert = | |
| 502 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); | |
| 503 ConfigureClientCertsForClient(kClientCertFileName, kClientPrivateKeyFileName); | |
| 504 ConfigureClientCertsForServer(); | |
| 505 Initialize(); | |
| 506 | |
| 507 TestCompletionCallback handshake_callback; | |
| 508 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | |
| 509 | |
| 510 TestCompletionCallback connect_callback; | |
| 511 int client_ret = client_socket_->Connect(connect_callback.callback()); | |
| 512 | |
| 513 client_ret = connect_callback.GetResult(client_ret); | |
| 514 server_ret = handshake_callback.GetResult(server_ret); | |
| 515 | |
| 516 ASSERT_EQ(OK, client_ret); | |
| 517 ASSERT_EQ(OK, server_ret); | |
| 518 | |
| 519 // Make sure the cert status is expected. | |
| 520 SSLInfo ssl_info; | |
| 521 client_socket_->GetSSLInfo(&ssl_info); | |
| 522 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); | |
| 523 server_socket_->GetSSLInfo(&ssl_info); | |
| 524 EXPECT_TRUE(ssl_info.cert.get()); | |
| 525 EXPECT_TRUE(client_cert->Equals(ssl_info.cert.get())); | |
| 526 } | |
| 527 | |
| 528 TEST_F(SSLServerSocketTest, HandshakeWithClientCertRequiredNotSupplied) { | |
| 529 ConfigureClientCertsForServer(); | |
| 530 Initialize(); | |
| 531 // Use the default setting for the client socket, which is to not send | |
| 532 // a client certificate. This will cause the client to receive an | |
| 533 // ERR_SSL_CLIENT_AUTH_CERT_NEEDED error, and allow for inspecting the | |
| 534 // requested cert_authorities from the CertificateRequest sent by the | |
| 535 // server. | |
| 536 | |
| 537 TestCompletionCallback handshake_callback; | |
| 538 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | |
| 539 | |
| 540 TestCompletionCallback connect_callback; | |
| 541 EXPECT_EQ(ERR_SSL_CLIENT_AUTH_CERT_NEEDED, | |
| 542 connect_callback.GetResult( | |
| 543 client_socket_->Connect(connect_callback.callback()))); | |
| 544 | |
| 545 scoped_refptr<SSLCertRequestInfo> request_info = new SSLCertRequestInfo(); | |
| 546 client_socket_->GetSSLCertRequestInfo(request_info.get()); | |
| 547 | |
| 548 // Check that the authority name that arrived in the CertificateRequest | |
| 549 // handshake message is as expected. | |
| 550 scoped_refptr<X509Certificate> client_cert = | |
| 551 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); | |
| 552 EXPECT_TRUE(client_cert->IsIssuedByEncoded(request_info->cert_authorities)); | |
| 553 | |
| 554 client_socket_->Disconnect(); | |
| 555 | |
| 556 EXPECT_EQ(ERR_FAILED, handshake_callback.GetResult(server_ret)); | |
| 557 } | |
| 558 | |
| 559 TEST_F(SSLServerSocketTest, HandshakeWithWrongClientCertSupplied) { | |
| 560 scoped_refptr<X509Certificate> client_cert = | |
| 561 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); | |
| 562 ConfigureClientCertsForClient(kWrongClientCertFileName, | |
| 563 kWrongClientPrivateKeyFileName); | |
| 564 ConfigureClientCertsForServer(); | |
| 565 Initialize(); | |
| 566 | |
| 567 TestCompletionCallback handshake_callback; | |
| 568 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | |
| 569 | |
| 570 TestCompletionCallback connect_callback; | |
| 571 int client_ret = client_socket_->Connect(connect_callback.callback()); | |
| 572 | |
| 573 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, | |
| 574 connect_callback.GetResult(client_ret)); | |
| 575 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, | |
| 576 handshake_callback.GetResult(server_ret)); | |
| 577 } | |
| 578 #endif // defined(USE_OPENSSL) | |
| 579 | |
| 415 TEST_F(SSLServerSocketTest, DataTransfer) { | 580 TEST_F(SSLServerSocketTest, DataTransfer) { |
| 416 Initialize(); | 581 Initialize(); |
| 417 | 582 |
| 583 // Establish connection. | |
| 418 TestCompletionCallback connect_callback; | 584 TestCompletionCallback connect_callback; |
| 419 TestCompletionCallback handshake_callback; | |
| 420 | |
| 421 // Establish connection. | |
| 422 int client_ret = client_socket_->Connect(connect_callback.callback()); | 585 int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 423 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | 586 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); |
| 424 | 587 |
| 588 TestCompletionCallback handshake_callback; | |
| 425 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 589 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 426 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); | 590 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); |
| 427 | 591 |
| 428 client_ret = connect_callback.GetResult(client_ret); | 592 client_ret = connect_callback.GetResult(client_ret); |
| 429 ASSERT_EQ(OK, client_ret); | 593 ASSERT_EQ(OK, client_ret); |
| 430 server_ret = handshake_callback.GetResult(server_ret); | 594 server_ret = handshake_callback.GetResult(server_ret); |
| 431 ASSERT_EQ(OK, server_ret); | 595 ASSERT_EQ(OK, server_ret); |
| 432 | 596 |
| 433 const int kReadBufSize = 1024; | 597 const int kReadBufSize = 1024; |
| 434 scoped_refptr<StringIOBuffer> write_buf = | 598 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())); | 656 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size())); |
| 493 } | 657 } |
| 494 | 658 |
| 495 // A regression test for bug 127822 (http://crbug.com/127822). | 659 // A regression test for bug 127822 (http://crbug.com/127822). |
| 496 // If the server closes the connection after the handshake is finished, | 660 // If the server closes the connection after the handshake is finished, |
| 497 // the client's Write() call should not cause an infinite loop. | 661 // the client's Write() call should not cause an infinite loop. |
| 498 // NOTE: this is a test for SSLClientSocket rather than SSLServerSocket. | 662 // NOTE: this is a test for SSLClientSocket rather than SSLServerSocket. |
| 499 TEST_F(SSLServerSocketTest, ClientWriteAfterServerClose) { | 663 TEST_F(SSLServerSocketTest, ClientWriteAfterServerClose) { |
| 500 Initialize(); | 664 Initialize(); |
| 501 | 665 |
| 502 TestCompletionCallback connect_callback; | |
| 503 TestCompletionCallback handshake_callback; | |
| 504 | 666 |
| 505 // Establish connection. | 667 // Establish connection. |
| 668 TestCompletionCallback connect_callback; | |
| 506 int client_ret = client_socket_->Connect(connect_callback.callback()); | 669 int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 507 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | 670 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); |
| 508 | 671 |
| 672 TestCompletionCallback handshake_callback; | |
| 509 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 673 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 510 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); | 674 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); |
| 511 | 675 |
| 512 client_ret = connect_callback.GetResult(client_ret); | 676 client_ret = connect_callback.GetResult(client_ret); |
| 513 ASSERT_EQ(OK, client_ret); | 677 ASSERT_EQ(OK, client_ret); |
| 514 server_ret = handshake_callback.GetResult(server_ret); | 678 server_ret = handshake_callback.GetResult(server_ret); |
| 515 ASSERT_EQ(OK, server_ret); | 679 ASSERT_EQ(OK, server_ret); |
| 516 | 680 |
| 517 scoped_refptr<StringIOBuffer> write_buf = new StringIOBuffer("testing123"); | 681 scoped_refptr<StringIOBuffer> write_buf = new StringIOBuffer("testing123"); |
| 518 | 682 |
| 519 // The server closes the connection. The server needs to write some | 683 // The server closes the connection. The server needs to write some |
| 520 // data first so that the client's Read() calls from the transport | 684 // 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 | 685 // socket won't return ERR_IO_PENDING. This ensures that the client |
| 522 // will call Read() on the transport socket again. | 686 // will call Read() on the transport socket again. |
| 523 TestCompletionCallback write_callback; | 687 TestCompletionCallback write_callback; |
| 524 | |
| 525 server_ret = server_socket_->Write( | 688 server_ret = server_socket_->Write( |
| 526 write_buf.get(), write_buf->size(), write_callback.callback()); | 689 write_buf.get(), write_buf->size(), write_callback.callback()); |
| 527 EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING); | 690 EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING); |
| 528 | 691 |
| 529 server_ret = write_callback.GetResult(server_ret); | 692 server_ret = write_callback.GetResult(server_ret); |
| 530 EXPECT_GT(server_ret, 0); | 693 EXPECT_GT(server_ret, 0); |
| 531 | 694 |
| 532 server_socket_->Disconnect(); | 695 server_socket_->Disconnect(); |
| 533 | 696 |
| 534 // The client writes some data. This should not cause an infinite loop. | 697 // The client writes some data. This should not cause an infinite loop. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 545 base::MessageLoop::current()->Run(); | 708 base::MessageLoop::current()->Run(); |
| 546 } | 709 } |
| 547 | 710 |
| 548 // This test executes ExportKeyingMaterial() on the client and server sockets, | 711 // This test executes ExportKeyingMaterial() on the client and server sockets, |
| 549 // after connecting them, and verifies that the results match. | 712 // after connecting them, and verifies that the results match. |
| 550 // This test will fail if False Start is enabled (see crbug.com/90208). | 713 // This test will fail if False Start is enabled (see crbug.com/90208). |
| 551 TEST_F(SSLServerSocketTest, ExportKeyingMaterial) { | 714 TEST_F(SSLServerSocketTest, ExportKeyingMaterial) { |
| 552 Initialize(); | 715 Initialize(); |
| 553 | 716 |
| 554 TestCompletionCallback connect_callback; | 717 TestCompletionCallback connect_callback; |
| 555 TestCompletionCallback handshake_callback; | |
| 556 | |
| 557 int client_ret = client_socket_->Connect(connect_callback.callback()); | 718 int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 558 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | 719 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); |
| 559 | 720 |
| 721 TestCompletionCallback handshake_callback; | |
| 560 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 722 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 561 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); | 723 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); |
| 562 | 724 |
| 563 if (client_ret == ERR_IO_PENDING) { | 725 if (client_ret == ERR_IO_PENDING) { |
| 564 ASSERT_EQ(OK, connect_callback.WaitForResult()); | 726 ASSERT_EQ(OK, connect_callback.WaitForResult()); |
| 565 } | 727 } |
| 566 if (server_ret == ERR_IO_PENDING) { | 728 if (server_ret == ERR_IO_PENDING) { |
| 567 ASSERT_EQ(OK, handshake_callback.WaitForResult()); | 729 ASSERT_EQ(OK, handshake_callback.WaitForResult()); |
| 568 } | 730 } |
| 569 | 731 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 609 }; | 771 }; |
| 610 client_ssl_config_.disabled_cipher_suites.assign( | 772 client_ssl_config_.disabled_cipher_suites.assign( |
| 611 kEcdheCiphers, kEcdheCiphers + arraysize(kEcdheCiphers)); | 773 kEcdheCiphers, kEcdheCiphers + arraysize(kEcdheCiphers)); |
| 612 | 774 |
| 613 // Require ECDHE on the server. | 775 // Require ECDHE on the server. |
| 614 server_ssl_config_.require_ecdhe = true; | 776 server_ssl_config_.require_ecdhe = true; |
| 615 | 777 |
| 616 Initialize(); | 778 Initialize(); |
| 617 | 779 |
| 618 TestCompletionCallback connect_callback; | 780 TestCompletionCallback connect_callback; |
| 781 int client_ret = client_socket_->Connect(connect_callback.callback()); | |
| 782 | |
| 619 TestCompletionCallback handshake_callback; | 783 TestCompletionCallback handshake_callback; |
| 620 | |
| 621 int client_ret = client_socket_->Connect(connect_callback.callback()); | |
| 622 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 784 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 623 | 785 |
| 624 client_ret = connect_callback.GetResult(client_ret); | 786 client_ret = connect_callback.GetResult(client_ret); |
| 625 server_ret = handshake_callback.GetResult(server_ret); | 787 server_ret = handshake_callback.GetResult(server_ret); |
| 626 | 788 |
| 627 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, client_ret); | 789 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, client_ret); |
| 628 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, server_ret); | 790 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, server_ret); |
| 629 } | 791 } |
| 630 | 792 |
| 631 } // namespace net | 793 } // namespace net |
| OLD | NEW |