| 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 #include "net/socket/ssl_client_socket.h" | 5 #include "net/socket/ssl_client_socket.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/callback_helpers.h" | 13 #include "base/callback_helpers.h" |
| 14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 15 #include "base/location.h" | 15 #include "base/location.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
| 19 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
| 20 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
| 21 #include "base/test/scoped_feature_list.h" | 21 #include "base/test/scoped_feature_list.h" |
| 22 #include "base/test/scoped_task_scheduler.h" | 22 #include "base/test/scoped_task_scheduler.h" |
| 23 #include "base/threading/thread_task_runner_handle.h" | 23 #include "base/threading/thread_task_runner_handle.h" |
| 24 #include "base/time/time.h" | 24 #include "base/time/time.h" |
| 25 #include "base/values.h" | 25 #include "base/values.h" |
| 26 #include "crypto/rsa_private_key.h" |
| 26 #include "net/base/address_list.h" | 27 #include "net/base/address_list.h" |
| 27 #include "net/base/io_buffer.h" | 28 #include "net/base/io_buffer.h" |
| 29 #include "net/base/ip_address.h" |
| 30 #include "net/base/ip_endpoint.h" |
| 28 #include "net/base/net_errors.h" | 31 #include "net/base/net_errors.h" |
| 29 #include "net/base/test_completion_callback.h" | 32 #include "net/base/test_completion_callback.h" |
| 30 #include "net/cert/asn1_util.h" | 33 #include "net/cert/asn1_util.h" |
| 31 #include "net/cert/ct_policy_enforcer.h" | 34 #include "net/cert/ct_policy_enforcer.h" |
| 32 #include "net/cert/ct_policy_status.h" | 35 #include "net/cert/ct_policy_status.h" |
| 33 #include "net/cert/ct_verifier.h" | 36 #include "net/cert/ct_verifier.h" |
| 34 #include "net/cert/do_nothing_ct_verifier.h" | 37 #include "net/cert/do_nothing_ct_verifier.h" |
| 35 #include "net/cert/mock_cert_verifier.h" | 38 #include "net/cert/mock_cert_verifier.h" |
| 36 #include "net/cert/signed_certificate_timestamp_and_status.h" | 39 #include "net/cert/signed_certificate_timestamp_and_status.h" |
| 37 #include "net/cert/test_root_certs.h" | 40 #include "net/cert/test_root_certs.h" |
| 38 #include "net/der/input.h" | 41 #include "net/der/input.h" |
| 39 #include "net/der/parser.h" | 42 #include "net/der/parser.h" |
| 40 #include "net/der/tag.h" | 43 #include "net/der/tag.h" |
| 41 #include "net/dns/host_resolver.h" | 44 #include "net/dns/host_resolver.h" |
| 42 #include "net/http/transport_security_state.h" | 45 #include "net/http/transport_security_state.h" |
| 43 #include "net/log/net_log_event_type.h" | 46 #include "net/log/net_log_event_type.h" |
| 44 #include "net/log/net_log_source.h" | 47 #include "net/log/net_log_source.h" |
| 45 #include "net/log/test_net_log.h" | 48 #include "net/log/test_net_log.h" |
| 46 #include "net/log/test_net_log_entry.h" | 49 #include "net/log/test_net_log_entry.h" |
| 47 #include "net/log/test_net_log_util.h" | 50 #include "net/log/test_net_log_util.h" |
| 48 #include "net/socket/client_socket_factory.h" | 51 #include "net/socket/client_socket_factory.h" |
| 49 #include "net/socket/client_socket_handle.h" | 52 #include "net/socket/client_socket_handle.h" |
| 50 #include "net/socket/socket_test_util.h" | 53 #include "net/socket/socket_test_util.h" |
| 54 #include "net/socket/ssl_server_socket.h" |
| 51 #include "net/socket/stream_socket.h" | 55 #include "net/socket/stream_socket.h" |
| 52 #include "net/socket/tcp_client_socket.h" | 56 #include "net/socket/tcp_client_socket.h" |
| 57 #include "net/socket/tcp_server_socket.h" |
| 53 #include "net/ssl/channel_id_service.h" | 58 #include "net/ssl/channel_id_service.h" |
| 54 #include "net/ssl/default_channel_id_store.h" | 59 #include "net/ssl/default_channel_id_store.h" |
| 55 #include "net/ssl/ssl_cert_request_info.h" | 60 #include "net/ssl/ssl_cert_request_info.h" |
| 56 #include "net/ssl/ssl_config_service.h" | 61 #include "net/ssl/ssl_config_service.h" |
| 57 #include "net/ssl/ssl_connection_status_flags.h" | 62 #include "net/ssl/ssl_connection_status_flags.h" |
| 58 #include "net/ssl/ssl_info.h" | 63 #include "net/ssl/ssl_info.h" |
| 64 #include "net/ssl/ssl_server_config.h" |
| 59 #include "net/ssl/test_ssl_private_key.h" | 65 #include "net/ssl/test_ssl_private_key.h" |
| 60 #include "net/test/cert_test_util.h" | 66 #include "net/test/cert_test_util.h" |
| 61 #include "net/test/gtest_util.h" | 67 #include "net/test/gtest_util.h" |
| 62 #include "net/test/spawned_test_server/spawned_test_server.h" | 68 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 63 #include "net/test/test_data_directory.h" | 69 #include "net/test/test_data_directory.h" |
| 64 #include "testing/gmock/include/gmock/gmock.h" | 70 #include "testing/gmock/include/gmock/gmock.h" |
| 65 #include "testing/gtest/include/gtest/gtest.h" | 71 #include "testing/gtest/include/gtest/gtest.h" |
| 66 #include "testing/platform_test.h" | 72 #include "testing/platform_test.h" |
| 67 #include "third_party/boringssl/src/include/openssl/bio.h" | 73 #include "third_party/boringssl/src/include/openssl/bio.h" |
| 68 #include "third_party/boringssl/src/include/openssl/evp.h" | 74 #include "third_party/boringssl/src/include/openssl/evp.h" |
| (...skipping 3129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3198 | 3204 |
| 3199 int rv; | 3205 int rv; |
| 3200 ASSERT_TRUE(CreateAndConnectSSLClientSocket(client_config, &rv)); | 3206 ASSERT_TRUE(CreateAndConnectSSLClientSocket(client_config, &rv)); |
| 3201 EXPECT_THAT(rv, IsOk()); | 3207 EXPECT_THAT(rv, IsOk()); |
| 3202 | 3208 |
| 3203 EXPECT_EQ(kProtoUnknown, sock_->GetNegotiatedProtocol()); | 3209 EXPECT_EQ(kProtoUnknown, sock_->GetNegotiatedProtocol()); |
| 3204 } | 3210 } |
| 3205 | 3211 |
| 3206 namespace { | 3212 namespace { |
| 3207 | 3213 |
| 3208 // Loads a PEM-encoded private key file into a SSLPrivateKey object. | 3214 bssl::UniquePtr<EVP_PKEY> LoadEVP_PKEY(const base::FilePath& filepath) { |
| 3209 // |filepath| is the private key file path. | |
| 3210 // Returns the new SSLPrivateKey. | |
| 3211 scoped_refptr<SSLPrivateKey> LoadPrivateKeyOpenSSL( | |
| 3212 const base::FilePath& filepath) { | |
| 3213 std::string data; | 3215 std::string data; |
| 3214 if (!base::ReadFileToString(filepath, &data)) { | 3216 if (!base::ReadFileToString(filepath, &data)) { |
| 3215 LOG(ERROR) << "Could not read private key file: " << filepath.value(); | 3217 LOG(ERROR) << "Could not read private key file: " << filepath.value(); |
| 3216 return nullptr; | 3218 return nullptr; |
| 3217 } | 3219 } |
| 3218 bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(const_cast<char*>(data.data()), | 3220 bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(const_cast<char*>(data.data()), |
| 3219 static_cast<int>(data.size()))); | 3221 static_cast<int>(data.size()))); |
| 3220 if (!bio) { | 3222 if (!bio) { |
| 3221 LOG(ERROR) << "Could not allocate BIO for buffer?"; | 3223 LOG(ERROR) << "Could not allocate BIO for buffer?"; |
| 3222 return nullptr; | 3224 return nullptr; |
| 3223 } | 3225 } |
| 3224 bssl::UniquePtr<EVP_PKEY> result( | 3226 bssl::UniquePtr<EVP_PKEY> result( |
| 3225 PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr)); | 3227 PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr)); |
| 3226 if (!result) { | 3228 if (!result) { |
| 3227 LOG(ERROR) << "Could not decode private key file: " << filepath.value(); | 3229 LOG(ERROR) << "Could not decode private key file: " << filepath.value(); |
| 3228 return nullptr; | 3230 return nullptr; |
| 3229 } | 3231 } |
| 3230 return WrapOpenSSLPrivateKey(std::move(result)); | 3232 return result; |
| 3233 } |
| 3234 |
| 3235 // Loads a PEM-encoded private key file into a SSLPrivateKey object. |
| 3236 // |filepath| is the private key file path. |
| 3237 // Returns the new SSLPrivateKey. |
| 3238 scoped_refptr<SSLPrivateKey> LoadPrivateKeyOpenSSL( |
| 3239 const base::FilePath& filepath) { |
| 3240 bssl::UniquePtr<EVP_PKEY> key = LoadEVP_PKEY(filepath); |
| 3241 if (!key) |
| 3242 return nullptr; |
| 3243 return WrapOpenSSLPrivateKey(std::move(key)); |
| 3231 } | 3244 } |
| 3232 | 3245 |
| 3233 } // namespace | 3246 } // namespace |
| 3234 | 3247 |
| 3235 // Connect to a server requesting client authentication, do not send | 3248 // Connect to a server requesting client authentication, do not send |
| 3236 // any client certificates. It should refuse the connection. | 3249 // any client certificates. It should refuse the connection. |
| 3237 TEST_F(SSLClientSocketTest, NoCert) { | 3250 TEST_F(SSLClientSocketTest, NoCert) { |
| 3238 SpawnedTestServer::SSLOptions ssl_options; | 3251 SpawnedTestServer::SSLOptions ssl_options; |
| 3239 ssl_options.request_client_certificate = true; | 3252 ssl_options.request_client_certificate = true; |
| 3240 ASSERT_TRUE(StartTestServer(ssl_options)); | 3253 ASSERT_TRUE(StartTestServer(ssl_options)); |
| (...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3786 EXPECT_EQ(0u, stats2.buffer_size); | 3799 EXPECT_EQ(0u, stats2.buffer_size); |
| 3787 EXPECT_EQ(stats.cert_size, stats2.total_size); | 3800 EXPECT_EQ(stats.cert_size, stats2.total_size); |
| 3788 } else { | 3801 } else { |
| 3789 EXPECT_EQ(17 * 1024u, stats2.buffer_size); | 3802 EXPECT_EQ(17 * 1024u, stats2.buffer_size); |
| 3790 EXPECT_LT(17 * 1024u, stats2.total_size); | 3803 EXPECT_LT(17 * 1024u, stats2.total_size); |
| 3791 } | 3804 } |
| 3792 EXPECT_EQ(1u, stats2.cert_count); | 3805 EXPECT_EQ(1u, stats2.cert_count); |
| 3793 EXPECT_LT(0u, stats2.cert_size); | 3806 EXPECT_LT(0u, stats2.cert_size); |
| 3794 } | 3807 } |
| 3795 | 3808 |
| 3809 TEST_P(SSLClientSocketReadTest, IdleAfterRead) { |
| 3810 // Set up a TCP server. |
| 3811 TCPServerSocket server_listener(NULL, NetLogSource()); |
| 3812 ASSERT_THAT( |
| 3813 server_listener.Listen(IPEndPoint(IPAddress::IPv4Localhost(), 0), 1), |
| 3814 IsOk()); |
| 3815 IPEndPoint server_address; |
| 3816 ASSERT_THAT(server_listener.GetLocalAddress(&server_address), IsOk()); |
| 3817 |
| 3818 // Connect a TCP client and server socket. |
| 3819 TestCompletionCallback server_callback; |
| 3820 std::unique_ptr<StreamSocket> server_transport; |
| 3821 int server_rv = |
| 3822 server_listener.Accept(&server_transport, server_callback.callback()); |
| 3823 |
| 3824 TestCompletionCallback client_callback; |
| 3825 std::unique_ptr<TCPClientSocket> client_transport(new TCPClientSocket( |
| 3826 AddressList(server_address), NULL, NULL, NetLogSource())); |
| 3827 int client_rv = client_transport->Connect(client_callback.callback()); |
| 3828 |
| 3829 EXPECT_THAT(server_callback.GetResult(server_rv), IsOk()); |
| 3830 EXPECT_THAT(client_callback.GetResult(client_rv), IsOk()); |
| 3831 |
| 3832 // Set up an SSL server. |
| 3833 base::FilePath certs_dir = GetTestCertsDirectory(); |
| 3834 scoped_refptr<net::X509Certificate> cert = |
| 3835 ImportCertFromFile(certs_dir, "ok_cert.pem"); |
| 3836 ASSERT_TRUE(cert); |
| 3837 bssl::UniquePtr<EVP_PKEY> pkey = |
| 3838 LoadEVP_PKEY(certs_dir.AppendASCII("ok_cert.pem")); |
| 3839 ASSERT_TRUE(pkey); |
| 3840 std::unique_ptr<crypto::RSAPrivateKey> key = |
| 3841 crypto::RSAPrivateKey::CreateFromKey(pkey.get()); |
| 3842 ASSERT_TRUE(key); |
| 3843 std::unique_ptr<SSLServerContext> server_context = |
| 3844 CreateSSLServerContext(cert.get(), *key.get(), SSLServerConfig()); |
| 3845 |
| 3846 // Complete the SSL handshake on both sides. |
| 3847 std::unique_ptr<SSLClientSocket> client(CreateSSLClientSocket( |
| 3848 std::move(client_transport), HostPortPair::FromIPEndPoint(server_address), |
| 3849 SSLConfig())); |
| 3850 std::unique_ptr<SSLServerSocket> server( |
| 3851 server_context->CreateSSLServerSocket(std::move(server_transport))); |
| 3852 |
| 3853 server_rv = server->Handshake(server_callback.callback()); |
| 3854 client_rv = client->Connect(client_callback.callback()); |
| 3855 |
| 3856 EXPECT_THAT(server_callback.GetResult(server_rv), IsOk()); |
| 3857 EXPECT_THAT(client_callback.GetResult(client_rv), IsOk()); |
| 3858 |
| 3859 // Write a single record on the server. |
| 3860 scoped_refptr<IOBuffer> write_buf(new StringIOBuffer("a")); |
| 3861 server_rv = server->Write(write_buf.get(), 1, server_callback.callback()); |
| 3862 |
| 3863 // Read that record on the server, but with a much larger buffer than |
| 3864 // necessary. |
| 3865 scoped_refptr<IOBuffer> read_buf(new IOBuffer(1024)); |
| 3866 client_rv = |
| 3867 Read(client.get(), read_buf.get(), 1024, client_callback.callback()); |
| 3868 |
| 3869 EXPECT_EQ(1, server_callback.GetResult(server_rv)); |
| 3870 EXPECT_EQ(1, WaitForReadCompletion(client.get(), read_buf.get(), 1024, |
| 3871 &client_callback, client_rv)); |
| 3872 |
| 3873 // At this point the client socket should be idle. |
| 3874 EXPECT_TRUE(client->IsConnectedAndIdle()); |
| 3875 |
| 3876 // The read buffer should be released. |
| 3877 StreamSocket::SocketMemoryStats stats; |
| 3878 client->DumpMemoryStats(&stats); |
| 3879 EXPECT_EQ(0u, stats.buffer_size); |
| 3880 EXPECT_EQ(1u, stats.cert_count); |
| 3881 EXPECT_LT(0u, stats.cert_size); |
| 3882 EXPECT_EQ(stats.cert_size, stats.total_size); |
| 3883 } |
| 3884 |
| 3796 } // namespace net | 3885 } // namespace net |
| OLD | NEW |