| 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 client_ssl_config_.send_client_cert = true; |
| 387 client_ssl_config_.client_cert = |
| 388 ImportCertFromFile(GetTestCertsDirectory(), cert_file_name); |
| 389 ASSERT_TRUE(client_ssl_config_.client_cert); |
| 390 scoped_ptr<crypto::RSAPrivateKey> key = ReadTestKey(private_key_file_name); |
| 391 ASSERT_TRUE(key); |
| 392 client_ssl_config_.client_private_key = WrapOpenSSLPrivateKey( |
| 393 crypto::ScopedEVP_PKEY(EVP_PKEY_up_ref(key->key()))); |
| 394 } |
| 395 |
| 396 void ConfigureClientCertsForServer() { |
| 397 server_ssl_config_.client_cert_type = |
| 398 SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 399 |
| 400 ScopedX509NameStack cert_names( |
| 401 SSL_load_client_CA_file(GetTestCertsDirectory() |
| 402 .AppendASCII(kClientCertCAFileName) |
| 403 .MaybeAsASCII() |
| 404 .c_str())); |
| 405 ASSERT_TRUE(cert_names); |
| 406 for (size_t i = 0; i < sk_X509_NAME_num(cert_names.get()); ++i) { |
| 407 uint8_t* str = nullptr; |
| 408 int length = i2d_X509_NAME(sk_X509_NAME_value(cert_names.get(), i), &str); |
| 409 server_ssl_config_.cert_authorities_.push_back(std::string( |
| 410 reinterpret_cast<const char*>(str), static_cast<size_t>(length))); |
| 411 OPENSSL_free(str); |
| 412 } |
| 413 |
| 414 scoped_refptr<X509Certificate> expected_client_cert( |
| 415 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName)); |
| 416 client_cert_verifier_->AddResultForCert(expected_client_cert.get(), OK); |
| 417 |
| 418 server_ssl_config_.client_cert_verifier = client_cert_verifier_.get(); |
| 419 } |
| 420 |
| 421 scoped_ptr<crypto::RSAPrivateKey> ReadTestKey(const base::StringPiece& name) { |
| 422 base::FilePath certs_dir(GetTestCertsDirectory()); |
| 423 base::FilePath key_path = certs_dir.AppendASCII(name); |
| 424 std::string key_string; |
| 425 if (!base::ReadFileToString(key_path, &key_string)) |
| 426 return nullptr; |
| 427 std::vector<uint8_t> key_vector( |
| 428 reinterpret_cast<const uint8_t*>(key_string.data()), |
| 429 reinterpret_cast<const uint8_t*>(key_string.data() + |
| 430 key_string.length())); |
| 431 scoped_ptr<crypto::RSAPrivateKey> key( |
| 432 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); |
| 433 return key; |
| 434 } |
| 435 #endif |
| 436 |
| 357 FakeDataChannel channel_1_; | 437 FakeDataChannel channel_1_; |
| 358 FakeDataChannel channel_2_; | 438 FakeDataChannel channel_2_; |
| 359 SSLConfig client_ssl_config_; | 439 SSLConfig client_ssl_config_; |
| 360 SSLServerConfig server_ssl_config_; | 440 SSLServerConfig server_ssl_config_; |
| 361 scoped_ptr<SSLClientSocket> client_socket_; | 441 scoped_ptr<SSLClientSocket> client_socket_; |
| 362 scoped_ptr<SSLServerSocket> server_socket_; | 442 scoped_ptr<SSLServerSocket> server_socket_; |
| 363 ClientSocketFactory* socket_factory_; | 443 ClientSocketFactory* socket_factory_; |
| 364 scoped_ptr<MockCertVerifier> cert_verifier_; | 444 scoped_ptr<MockCertVerifier> cert_verifier_; |
| 445 scoped_ptr<MockClientCertVerifier> client_cert_verifier_; |
| 365 scoped_ptr<TransportSecurityState> transport_security_state_; | 446 scoped_ptr<TransportSecurityState> transport_security_state_; |
| 366 }; | 447 }; |
| 367 | 448 |
| 368 // This test only executes creation of client and server sockets. This is to | 449 // 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 | 450 // test that creation of sockets doesn't crash and have minimal code to run |
| 370 // under valgrind in order to help debugging memory problems. | 451 // under valgrind in order to help debugging memory problems. |
| 371 TEST_F(SSLServerSocketTest, Initialize) { | 452 TEST_F(SSLServerSocketTest, Initialize) { |
| 372 Initialize(); | 453 Initialize(); |
| 373 } | 454 } |
| 374 | 455 |
| 375 // This test executes Connect() on SSLClientSocket and Handshake() on | 456 // This test executes Connect() on SSLClientSocket and Handshake() on |
| 376 // SSLServerSocket to make sure handshaking between the two sockets is | 457 // SSLServerSocket to make sure handshaking between the two sockets is |
| 377 // completed successfully. | 458 // completed successfully. |
| 378 TEST_F(SSLServerSocketTest, Handshake) { | 459 TEST_F(SSLServerSocketTest, Handshake) { |
| 379 Initialize(); | 460 Initialize(); |
| 380 | 461 |
| 462 TestCompletionCallback handshake_callback; |
| 463 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 464 |
| 381 TestCompletionCallback connect_callback; | 465 TestCompletionCallback connect_callback; |
| 382 TestCompletionCallback handshake_callback; | 466 int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 383 | 467 |
| 384 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 468 client_ret = connect_callback.GetResult(client_ret); |
| 385 EXPECT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); | 469 server_ret = handshake_callback.GetResult(server_ret); |
| 386 | 470 |
| 387 int client_ret = client_socket_->Connect(connect_callback.callback()); | 471 ASSERT_EQ(OK, client_ret); |
| 388 EXPECT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | 472 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 | 473 |
| 397 // Make sure the cert status is expected. | 474 // Make sure the cert status is expected. |
| 398 SSLInfo ssl_info; | 475 SSLInfo ssl_info; |
| 399 ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info)); | 476 ASSERT_TRUE(client_socket_->GetSSLInfo(&ssl_info)); |
| 400 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); | 477 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); |
| 401 | 478 |
| 402 // The default cipher suite should be ECDHE and, unless on NSS and the | 479 // The default cipher suite should be ECDHE and, unless on NSS and the |
| 403 // platform doesn't support it, an AEAD. | 480 // platform doesn't support it, an AEAD. |
| 404 uint16_t cipher_suite = | 481 uint16_t cipher_suite = |
| 405 SSLConnectionStatusToCipherSuite(ssl_info.connection_status); | 482 SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
| 406 const char* key_exchange; | 483 const char* key_exchange; |
| 407 const char* cipher; | 484 const char* cipher; |
| 408 const char* mac; | 485 const char* mac; |
| 409 bool is_aead; | 486 bool is_aead; |
| 410 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite); | 487 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite); |
| 411 EXPECT_STREQ("ECDHE_RSA", key_exchange); | 488 EXPECT_STREQ("ECDHE_RSA", key_exchange); |
| 412 EXPECT_TRUE(is_aead); | 489 EXPECT_TRUE(is_aead); |
| 413 } | 490 } |
| 414 | 491 |
| 492 // NSS ports don't support client certificates. |
| 493 #if defined(USE_OPENSSL) |
| 494 |
| 495 // This test executes Connect() on SSLClientSocket and Handshake() on |
| 496 // SSLServerSocket to make sure handshaking between the two sockets is |
| 497 // completed successfully, using client certificate. |
| 498 TEST_F(SSLServerSocketTest, HandshakeWithClientCert) { |
| 499 scoped_refptr<X509Certificate> client_cert = |
| 500 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); |
| 501 ConfigureClientCertsForClient(kClientCertFileName, kClientPrivateKeyFileName); |
| 502 ConfigureClientCertsForServer(); |
| 503 Initialize(); |
| 504 |
| 505 TestCompletionCallback handshake_callback; |
| 506 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 507 |
| 508 TestCompletionCallback connect_callback; |
| 509 int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 510 |
| 511 client_ret = connect_callback.GetResult(client_ret); |
| 512 server_ret = handshake_callback.GetResult(server_ret); |
| 513 |
| 514 ASSERT_EQ(OK, client_ret); |
| 515 ASSERT_EQ(OK, server_ret); |
| 516 |
| 517 // Make sure the cert status is expected. |
| 518 SSLInfo ssl_info; |
| 519 client_socket_->GetSSLInfo(&ssl_info); |
| 520 EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, ssl_info.cert_status); |
| 521 server_socket_->GetSSLInfo(&ssl_info); |
| 522 EXPECT_TRUE(ssl_info.cert.get()); |
| 523 EXPECT_TRUE(client_cert->Equals(ssl_info.cert.get())); |
| 524 } |
| 525 |
| 526 TEST_F(SSLServerSocketTest, HandshakeWithClientCertRequiredNotSupplied) { |
| 527 ConfigureClientCertsForServer(); |
| 528 Initialize(); |
| 529 // Use the default setting for the client socket, which is to not send |
| 530 // a client certificate. This will cause the client to receive an |
| 531 // ERR_SSL_CLIENT_AUTH_CERT_NEEDED error, and allow for inspecting the |
| 532 // requested cert_authorities from the CertificateRequest sent by the |
| 533 // server. |
| 534 |
| 535 TestCompletionCallback handshake_callback; |
| 536 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 537 |
| 538 TestCompletionCallback connect_callback; |
| 539 EXPECT_EQ(ERR_SSL_CLIENT_AUTH_CERT_NEEDED, |
| 540 connect_callback.GetResult( |
| 541 client_socket_->Connect(connect_callback.callback()))); |
| 542 |
| 543 scoped_refptr<SSLCertRequestInfo> request_info = new SSLCertRequestInfo(); |
| 544 client_socket_->GetSSLCertRequestInfo(request_info.get()); |
| 545 |
| 546 // Check that the authority name that arrived in the CertificateRequest |
| 547 // handshake message is as expected. |
| 548 scoped_refptr<X509Certificate> client_cert = |
| 549 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); |
| 550 EXPECT_TRUE(client_cert->IsIssuedByEncoded(request_info->cert_authorities)); |
| 551 |
| 552 client_socket_->Disconnect(); |
| 553 |
| 554 EXPECT_EQ(ERR_FAILED, handshake_callback.GetResult(server_ret)); |
| 555 } |
| 556 |
| 557 TEST_F(SSLServerSocketTest, HandshakeWithWrongClientCertSupplied) { |
| 558 scoped_refptr<X509Certificate> client_cert = |
| 559 ImportCertFromFile(GetTestCertsDirectory(), kClientCertFileName); |
| 560 ConfigureClientCertsForClient(kWrongClientCertFileName, |
| 561 kWrongClientPrivateKeyFileName); |
| 562 ConfigureClientCertsForServer(); |
| 563 Initialize(); |
| 564 |
| 565 TestCompletionCallback handshake_callback; |
| 566 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 567 |
| 568 TestCompletionCallback connect_callback; |
| 569 int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 570 |
| 571 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, |
| 572 connect_callback.GetResult(client_ret)); |
| 573 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, |
| 574 handshake_callback.GetResult(server_ret)); |
| 575 } |
| 576 #endif // defined(USE_OPENSSL) |
| 577 |
| 415 TEST_F(SSLServerSocketTest, DataTransfer) { | 578 TEST_F(SSLServerSocketTest, DataTransfer) { |
| 416 Initialize(); | 579 Initialize(); |
| 417 | 580 |
| 581 // Establish connection. |
| 418 TestCompletionCallback connect_callback; | 582 TestCompletionCallback connect_callback; |
| 419 TestCompletionCallback handshake_callback; | |
| 420 | |
| 421 // Establish connection. | |
| 422 int client_ret = client_socket_->Connect(connect_callback.callback()); | 583 int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 423 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | 584 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); |
| 424 | 585 |
| 586 TestCompletionCallback handshake_callback; |
| 425 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 587 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 426 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); | 588 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); |
| 427 | 589 |
| 428 client_ret = connect_callback.GetResult(client_ret); | 590 client_ret = connect_callback.GetResult(client_ret); |
| 429 ASSERT_EQ(OK, client_ret); | 591 ASSERT_EQ(OK, client_ret); |
| 430 server_ret = handshake_callback.GetResult(server_ret); | 592 server_ret = handshake_callback.GetResult(server_ret); |
| 431 ASSERT_EQ(OK, server_ret); | 593 ASSERT_EQ(OK, server_ret); |
| 432 | 594 |
| 433 const int kReadBufSize = 1024; | 595 const int kReadBufSize = 1024; |
| 434 scoped_refptr<StringIOBuffer> write_buf = | 596 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())); | 654 EXPECT_EQ(0, memcmp(write_buf->data(), read_buf->data(), write_buf->size())); |
| 493 } | 655 } |
| 494 | 656 |
| 495 // A regression test for bug 127822 (http://crbug.com/127822). | 657 // A regression test for bug 127822 (http://crbug.com/127822). |
| 496 // If the server closes the connection after the handshake is finished, | 658 // If the server closes the connection after the handshake is finished, |
| 497 // the client's Write() call should not cause an infinite loop. | 659 // the client's Write() call should not cause an infinite loop. |
| 498 // NOTE: this is a test for SSLClientSocket rather than SSLServerSocket. | 660 // NOTE: this is a test for SSLClientSocket rather than SSLServerSocket. |
| 499 TEST_F(SSLServerSocketTest, ClientWriteAfterServerClose) { | 661 TEST_F(SSLServerSocketTest, ClientWriteAfterServerClose) { |
| 500 Initialize(); | 662 Initialize(); |
| 501 | 663 |
| 502 TestCompletionCallback connect_callback; | |
| 503 TestCompletionCallback handshake_callback; | |
| 504 | 664 |
| 505 // Establish connection. | 665 // Establish connection. |
| 666 TestCompletionCallback connect_callback; |
| 506 int client_ret = client_socket_->Connect(connect_callback.callback()); | 667 int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 507 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | 668 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); |
| 508 | 669 |
| 670 TestCompletionCallback handshake_callback; |
| 509 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 671 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 510 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); | 672 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); |
| 511 | 673 |
| 512 client_ret = connect_callback.GetResult(client_ret); | 674 client_ret = connect_callback.GetResult(client_ret); |
| 513 ASSERT_EQ(OK, client_ret); | 675 ASSERT_EQ(OK, client_ret); |
| 514 server_ret = handshake_callback.GetResult(server_ret); | 676 server_ret = handshake_callback.GetResult(server_ret); |
| 515 ASSERT_EQ(OK, server_ret); | 677 ASSERT_EQ(OK, server_ret); |
| 516 | 678 |
| 517 scoped_refptr<StringIOBuffer> write_buf = new StringIOBuffer("testing123"); | 679 scoped_refptr<StringIOBuffer> write_buf = new StringIOBuffer("testing123"); |
| 518 | 680 |
| 519 // The server closes the connection. The server needs to write some | 681 // The server closes the connection. The server needs to write some |
| 520 // data first so that the client's Read() calls from the transport | 682 // 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 | 683 // socket won't return ERR_IO_PENDING. This ensures that the client |
| 522 // will call Read() on the transport socket again. | 684 // will call Read() on the transport socket again. |
| 523 TestCompletionCallback write_callback; | 685 TestCompletionCallback write_callback; |
| 524 | |
| 525 server_ret = server_socket_->Write( | 686 server_ret = server_socket_->Write( |
| 526 write_buf.get(), write_buf->size(), write_callback.callback()); | 687 write_buf.get(), write_buf->size(), write_callback.callback()); |
| 527 EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING); | 688 EXPECT_TRUE(server_ret > 0 || server_ret == ERR_IO_PENDING); |
| 528 | 689 |
| 529 server_ret = write_callback.GetResult(server_ret); | 690 server_ret = write_callback.GetResult(server_ret); |
| 530 EXPECT_GT(server_ret, 0); | 691 EXPECT_GT(server_ret, 0); |
| 531 | 692 |
| 532 server_socket_->Disconnect(); | 693 server_socket_->Disconnect(); |
| 533 | 694 |
| 534 // The client writes some data. This should not cause an infinite loop. | 695 // The client writes some data. This should not cause an infinite loop. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 545 base::MessageLoop::current()->Run(); | 706 base::MessageLoop::current()->Run(); |
| 546 } | 707 } |
| 547 | 708 |
| 548 // This test executes ExportKeyingMaterial() on the client and server sockets, | 709 // This test executes ExportKeyingMaterial() on the client and server sockets, |
| 549 // after connecting them, and verifies that the results match. | 710 // after connecting them, and verifies that the results match. |
| 550 // This test will fail if False Start is enabled (see crbug.com/90208). | 711 // This test will fail if False Start is enabled (see crbug.com/90208). |
| 551 TEST_F(SSLServerSocketTest, ExportKeyingMaterial) { | 712 TEST_F(SSLServerSocketTest, ExportKeyingMaterial) { |
| 552 Initialize(); | 713 Initialize(); |
| 553 | 714 |
| 554 TestCompletionCallback connect_callback; | 715 TestCompletionCallback connect_callback; |
| 555 TestCompletionCallback handshake_callback; | |
| 556 | |
| 557 int client_ret = client_socket_->Connect(connect_callback.callback()); | 716 int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 558 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | 717 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); |
| 559 | 718 |
| 719 TestCompletionCallback handshake_callback; |
| 560 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 720 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 561 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); | 721 ASSERT_TRUE(server_ret == OK || server_ret == ERR_IO_PENDING); |
| 562 | 722 |
| 563 if (client_ret == ERR_IO_PENDING) { | 723 if (client_ret == ERR_IO_PENDING) { |
| 564 ASSERT_EQ(OK, connect_callback.WaitForResult()); | 724 ASSERT_EQ(OK, connect_callback.WaitForResult()); |
| 565 } | 725 } |
| 566 if (server_ret == ERR_IO_PENDING) { | 726 if (server_ret == ERR_IO_PENDING) { |
| 567 ASSERT_EQ(OK, handshake_callback.WaitForResult()); | 727 ASSERT_EQ(OK, handshake_callback.WaitForResult()); |
| 568 } | 728 } |
| 569 | 729 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 609 }; | 769 }; |
| 610 client_ssl_config_.disabled_cipher_suites.assign( | 770 client_ssl_config_.disabled_cipher_suites.assign( |
| 611 kEcdheCiphers, kEcdheCiphers + arraysize(kEcdheCiphers)); | 771 kEcdheCiphers, kEcdheCiphers + arraysize(kEcdheCiphers)); |
| 612 | 772 |
| 613 // Require ECDHE on the server. | 773 // Require ECDHE on the server. |
| 614 server_ssl_config_.require_ecdhe = true; | 774 server_ssl_config_.require_ecdhe = true; |
| 615 | 775 |
| 616 Initialize(); | 776 Initialize(); |
| 617 | 777 |
| 618 TestCompletionCallback connect_callback; | 778 TestCompletionCallback connect_callback; |
| 779 int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 780 |
| 619 TestCompletionCallback handshake_callback; | 781 TestCompletionCallback handshake_callback; |
| 620 | |
| 621 int client_ret = client_socket_->Connect(connect_callback.callback()); | |
| 622 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 782 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 623 | 783 |
| 624 client_ret = connect_callback.GetResult(client_ret); | 784 client_ret = connect_callback.GetResult(client_ret); |
| 625 server_ret = handshake_callback.GetResult(server_ret); | 785 server_ret = handshake_callback.GetResult(server_ret); |
| 626 | 786 |
| 627 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, client_ret); | 787 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, client_ret); |
| 628 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, server_ret); | 788 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, server_ret); |
| 629 } | 789 } |
| 630 | 790 |
| 631 } // namespace net | 791 } // namespace net |
| OLD | NEW |