| 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 <openssl/evp.h> |
| 24 #include <openssl/ssl.h> |
| 25 #include <openssl/x509.h> |
| 26 |
| 23 #include "base/callback_helpers.h" | 27 #include "base/callback_helpers.h" |
| 24 #include "base/compiler_specific.h" | 28 #include "base/compiler_specific.h" |
| 25 #include "base/files/file_path.h" | 29 #include "base/files/file_path.h" |
| 26 #include "base/files/file_util.h" | 30 #include "base/files/file_util.h" |
| 27 #include "base/location.h" | 31 #include "base/location.h" |
| 28 #include "base/logging.h" | 32 #include "base/logging.h" |
| 29 #include "base/macros.h" | 33 #include "base/macros.h" |
| 30 #include "base/message_loop/message_loop.h" | 34 #include "base/message_loop/message_loop.h" |
| 31 #include "base/single_thread_task_runner.h" | 35 #include "base/single_thread_task_runner.h" |
| 32 #include "base/thread_task_runner_handle.h" | 36 #include "base/thread_task_runner_handle.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 58 #include "net/ssl/ssl_cipher_suite_names.h" | 62 #include "net/ssl/ssl_cipher_suite_names.h" |
| 59 #include "net/ssl/ssl_connection_status_flags.h" | 63 #include "net/ssl/ssl_connection_status_flags.h" |
| 60 #include "net/ssl/ssl_info.h" | 64 #include "net/ssl/ssl_info.h" |
| 61 #include "net/ssl/ssl_private_key.h" | 65 #include "net/ssl/ssl_private_key.h" |
| 62 #include "net/ssl/ssl_server_config.h" | 66 #include "net/ssl/ssl_server_config.h" |
| 63 #include "net/ssl/test_ssl_private_key.h" | 67 #include "net/ssl/test_ssl_private_key.h" |
| 64 #include "net/test/cert_test_util.h" | 68 #include "net/test/cert_test_util.h" |
| 65 #include "testing/gtest/include/gtest/gtest.h" | 69 #include "testing/gtest/include/gtest/gtest.h" |
| 66 #include "testing/platform_test.h" | 70 #include "testing/platform_test.h" |
| 67 | 71 |
| 68 #if defined(USE_OPENSSL) | |
| 69 #include <openssl/evp.h> | |
| 70 #include <openssl/ssl.h> | |
| 71 #include <openssl/x509.h> | |
| 72 #endif | |
| 73 | |
| 74 namespace net { | 72 namespace net { |
| 75 | 73 |
| 76 namespace { | 74 namespace { |
| 77 | 75 |
| 78 const char kClientCertFileName[] = "client_1.pem"; | 76 const char kClientCertFileName[] = "client_1.pem"; |
| 79 const char kClientPrivateKeyFileName[] = "client_1.pk8"; | 77 const char kClientPrivateKeyFileName[] = "client_1.pk8"; |
| 80 const char kWrongClientCertFileName[] = "client_2.pem"; | 78 const char kWrongClientCertFileName[] = "client_2.pem"; |
| 81 const char kWrongClientPrivateKeyFileName[] = "client_2.pk8"; | 79 const char kWrongClientPrivateKeyFileName[] = "client_2.pk8"; |
| 82 const char kClientCertCAFileName[] = "client_1_ca.pem"; | 80 const char kClientCertCAFileName[] = "client_1_ca.pem"; |
| 83 | 81 |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 client_socket_ = socket_factory_->CreateSSLClientSocket( | 387 client_socket_ = socket_factory_->CreateSSLClientSocket( |
| 390 std::move(client_connection), host_and_pair, client_ssl_config_, | 388 std::move(client_connection), host_and_pair, client_ssl_config_, |
| 391 context); | 389 context); |
| 392 ASSERT_TRUE(client_socket_); | 390 ASSERT_TRUE(client_socket_); |
| 393 | 391 |
| 394 server_socket_ = | 392 server_socket_ = |
| 395 server_context_->CreateSSLServerSocket(std::move(server_socket)); | 393 server_context_->CreateSSLServerSocket(std::move(server_socket)); |
| 396 ASSERT_TRUE(server_socket_); | 394 ASSERT_TRUE(server_socket_); |
| 397 } | 395 } |
| 398 | 396 |
| 399 #if defined(USE_OPENSSL) | |
| 400 void ConfigureClientCertsForClient(const char* cert_file_name, | 397 void ConfigureClientCertsForClient(const char* cert_file_name, |
| 401 const char* private_key_file_name) { | 398 const char* private_key_file_name) { |
| 402 client_ssl_config_.send_client_cert = true; | 399 client_ssl_config_.send_client_cert = true; |
| 403 client_ssl_config_.client_cert = | 400 client_ssl_config_.client_cert = |
| 404 ImportCertFromFile(GetTestCertsDirectory(), cert_file_name); | 401 ImportCertFromFile(GetTestCertsDirectory(), cert_file_name); |
| 405 ASSERT_TRUE(client_ssl_config_.client_cert); | 402 ASSERT_TRUE(client_ssl_config_.client_cert); |
| 406 | 403 |
| 407 scoped_ptr<crypto::RSAPrivateKey> key = ReadTestKey(private_key_file_name); | 404 scoped_ptr<crypto::RSAPrivateKey> key = ReadTestKey(private_key_file_name); |
| 408 ASSERT_TRUE(key); | 405 ASSERT_TRUE(key); |
| 409 | 406 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 if (!base::ReadFileToString(key_path, &key_string)) | 445 if (!base::ReadFileToString(key_path, &key_string)) |
| 449 return nullptr; | 446 return nullptr; |
| 450 std::vector<uint8_t> key_vector( | 447 std::vector<uint8_t> key_vector( |
| 451 reinterpret_cast<const uint8_t*>(key_string.data()), | 448 reinterpret_cast<const uint8_t*>(key_string.data()), |
| 452 reinterpret_cast<const uint8_t*>(key_string.data() + | 449 reinterpret_cast<const uint8_t*>(key_string.data() + |
| 453 key_string.length())); | 450 key_string.length())); |
| 454 scoped_ptr<crypto::RSAPrivateKey> key( | 451 scoped_ptr<crypto::RSAPrivateKey> key( |
| 455 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); | 452 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); |
| 456 return key; | 453 return key; |
| 457 } | 454 } |
| 458 #endif | |
| 459 | 455 |
| 460 scoped_ptr<FakeDataChannel> channel_1_; | 456 scoped_ptr<FakeDataChannel> channel_1_; |
| 461 scoped_ptr<FakeDataChannel> channel_2_; | 457 scoped_ptr<FakeDataChannel> channel_2_; |
| 462 SSLConfig client_ssl_config_; | 458 SSLConfig client_ssl_config_; |
| 463 SSLServerConfig server_ssl_config_; | 459 SSLServerConfig server_ssl_config_; |
| 464 scoped_ptr<SSLClientSocket> client_socket_; | 460 scoped_ptr<SSLClientSocket> client_socket_; |
| 465 scoped_ptr<SSLServerSocket> server_socket_; | 461 scoped_ptr<SSLServerSocket> server_socket_; |
| 466 ClientSocketFactory* socket_factory_; | 462 ClientSocketFactory* socket_factory_; |
| 467 scoped_ptr<MockCertVerifier> cert_verifier_; | 463 scoped_ptr<MockCertVerifier> cert_verifier_; |
| 468 scoped_ptr<MockClientCertVerifier> client_cert_verifier_; | 464 scoped_ptr<MockClientCertVerifier> client_cert_verifier_; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 510 SSLConnectionStatusToCipherSuite(ssl_info.connection_status); | 506 SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
| 511 const char* key_exchange; | 507 const char* key_exchange; |
| 512 const char* cipher; | 508 const char* cipher; |
| 513 const char* mac; | 509 const char* mac; |
| 514 bool is_aead; | 510 bool is_aead; |
| 515 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite); | 511 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite); |
| 516 EXPECT_STREQ("ECDHE_RSA", key_exchange); | 512 EXPECT_STREQ("ECDHE_RSA", key_exchange); |
| 517 EXPECT_TRUE(is_aead); | 513 EXPECT_TRUE(is_aead); |
| 518 } | 514 } |
| 519 | 515 |
| 520 // NSS ports don't support client certificates and have a global session cache. | |
| 521 #if defined(USE_OPENSSL) | |
| 522 | |
| 523 // This test makes sure the session cache is working. | 516 // This test makes sure the session cache is working. |
| 524 TEST_F(SSLServerSocketTest, HandshakeCached) { | 517 TEST_F(SSLServerSocketTest, HandshakeCached) { |
| 525 ASSERT_NO_FATAL_FAILURE(CreateContext()); | 518 ASSERT_NO_FATAL_FAILURE(CreateContext()); |
| 526 ASSERT_NO_FATAL_FAILURE(CreateSockets()); | 519 ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
| 527 | 520 |
| 528 TestCompletionCallback handshake_callback; | 521 TestCompletionCallback handshake_callback; |
| 529 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 522 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 530 | 523 |
| 531 TestCompletionCallback connect_callback; | 524 TestCompletionCallback connect_callback; |
| 532 int client_ret = client_socket_->Connect(connect_callback.callback()); | 525 int client_ret = client_socket_->Connect(connect_callback.callback()); |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 855 int server_ret2 = server_socket_->Handshake(handshake_callback2.callback()); | 848 int server_ret2 = server_socket_->Handshake(handshake_callback2.callback()); |
| 856 | 849 |
| 857 TestCompletionCallback connect_callback2; | 850 TestCompletionCallback connect_callback2; |
| 858 int client_ret2 = client_socket_->Connect(connect_callback2.callback()); | 851 int client_ret2 = client_socket_->Connect(connect_callback2.callback()); |
| 859 | 852 |
| 860 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, | 853 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, |
| 861 connect_callback2.GetResult(client_ret2)); | 854 connect_callback2.GetResult(client_ret2)); |
| 862 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, | 855 EXPECT_EQ(ERR_BAD_SSL_CLIENT_AUTH_CERT, |
| 863 handshake_callback2.GetResult(server_ret2)); | 856 handshake_callback2.GetResult(server_ret2)); |
| 864 } | 857 } |
| 865 #endif // defined(USE_OPENSSL) | |
| 866 | 858 |
| 867 TEST_F(SSLServerSocketTest, DataTransfer) { | 859 TEST_F(SSLServerSocketTest, DataTransfer) { |
| 868 ASSERT_NO_FATAL_FAILURE(CreateContext()); | 860 ASSERT_NO_FATAL_FAILURE(CreateContext()); |
| 869 ASSERT_NO_FATAL_FAILURE(CreateSockets()); | 861 ASSERT_NO_FATAL_FAILURE(CreateSockets()); |
| 870 | 862 |
| 871 // Establish connection. | 863 // Establish connection. |
| 872 TestCompletionCallback connect_callback; | 864 TestCompletionCallback connect_callback; |
| 873 int client_ret = client_socket_->Connect(connect_callback.callback()); | 865 int client_ret = client_socket_->Connect(connect_callback.callback()); |
| 874 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); | 866 ASSERT_TRUE(client_ret == OK || client_ret == ERR_IO_PENDING); |
| 875 | 867 |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1071 int server_ret = server_socket_->Handshake(handshake_callback.callback()); | 1063 int server_ret = server_socket_->Handshake(handshake_callback.callback()); |
| 1072 | 1064 |
| 1073 client_ret = connect_callback.GetResult(client_ret); | 1065 client_ret = connect_callback.GetResult(client_ret); |
| 1074 server_ret = handshake_callback.GetResult(server_ret); | 1066 server_ret = handshake_callback.GetResult(server_ret); |
| 1075 | 1067 |
| 1076 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, client_ret); | 1068 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, client_ret); |
| 1077 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, server_ret); | 1069 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, server_ret); |
| 1078 } | 1070 } |
| 1079 | 1071 |
| 1080 } // namespace net | 1072 } // namespace net |
| OLD | NEW |