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 #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 <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/location.h" | 14 #include "base/location.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 18 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 20 #include "base/test/scoped_task_scheduler.h" | 20 #include "base/test/scoped_task_scheduler.h" |
| 21 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
| 22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 23 #include "base/values.h" | 23 #include "base/values.h" |
| 24 #include "crypto/rsa_private_key.h" | |
| 24 #include "net/base/address_list.h" | 25 #include "net/base/address_list.h" |
| 25 #include "net/base/io_buffer.h" | 26 #include "net/base/io_buffer.h" |
| 27 #include "net/base/ip_address.h" | |
| 28 #include "net/base/ip_endpoint.h" | |
| 26 #include "net/base/net_errors.h" | 29 #include "net/base/net_errors.h" |
| 27 #include "net/base/test_completion_callback.h" | 30 #include "net/base/test_completion_callback.h" |
| 28 #include "net/cert/asn1_util.h" | 31 #include "net/cert/asn1_util.h" |
| 29 #include "net/cert/ct_policy_enforcer.h" | 32 #include "net/cert/ct_policy_enforcer.h" |
| 30 #include "net/cert/ct_policy_status.h" | 33 #include "net/cert/ct_policy_status.h" |
| 31 #include "net/cert/ct_verifier.h" | 34 #include "net/cert/ct_verifier.h" |
| 32 #include "net/cert/do_nothing_ct_verifier.h" | 35 #include "net/cert/do_nothing_ct_verifier.h" |
| 33 #include "net/cert/mock_cert_verifier.h" | 36 #include "net/cert/mock_cert_verifier.h" |
| 34 #include "net/cert/signed_certificate_timestamp_and_status.h" | 37 #include "net/cert/signed_certificate_timestamp_and_status.h" |
| 35 #include "net/cert/test_root_certs.h" | 38 #include "net/cert/test_root_certs.h" |
| 36 #include "net/der/input.h" | 39 #include "net/der/input.h" |
| 37 #include "net/der/parser.h" | 40 #include "net/der/parser.h" |
| 38 #include "net/der/tag.h" | 41 #include "net/der/tag.h" |
| 39 #include "net/dns/host_resolver.h" | 42 #include "net/dns/host_resolver.h" |
| 40 #include "net/http/transport_security_state.h" | 43 #include "net/http/transport_security_state.h" |
| 41 #include "net/log/net_log_event_type.h" | 44 #include "net/log/net_log_event_type.h" |
| 42 #include "net/log/net_log_source.h" | 45 #include "net/log/net_log_source.h" |
| 43 #include "net/log/test_net_log.h" | 46 #include "net/log/test_net_log.h" |
| 44 #include "net/log/test_net_log_entry.h" | 47 #include "net/log/test_net_log_entry.h" |
| 45 #include "net/log/test_net_log_util.h" | 48 #include "net/log/test_net_log_util.h" |
| 46 #include "net/socket/client_socket_factory.h" | 49 #include "net/socket/client_socket_factory.h" |
| 47 #include "net/socket/client_socket_handle.h" | 50 #include "net/socket/client_socket_handle.h" |
| 48 #include "net/socket/socket_test_util.h" | 51 #include "net/socket/socket_test_util.h" |
| 52 #include "net/socket/ssl_server_socket.h" | |
| 49 #include "net/socket/stream_socket.h" | 53 #include "net/socket/stream_socket.h" |
| 50 #include "net/socket/tcp_client_socket.h" | 54 #include "net/socket/tcp_client_socket.h" |
| 55 #include "net/socket/tcp_server_socket.h" | |
| 51 #include "net/ssl/channel_id_service.h" | 56 #include "net/ssl/channel_id_service.h" |
| 52 #include "net/ssl/default_channel_id_store.h" | 57 #include "net/ssl/default_channel_id_store.h" |
| 53 #include "net/ssl/ssl_cert_request_info.h" | 58 #include "net/ssl/ssl_cert_request_info.h" |
| 54 #include "net/ssl/ssl_config_service.h" | 59 #include "net/ssl/ssl_config_service.h" |
| 55 #include "net/ssl/ssl_connection_status_flags.h" | 60 #include "net/ssl/ssl_connection_status_flags.h" |
| 56 #include "net/ssl/ssl_info.h" | 61 #include "net/ssl/ssl_info.h" |
| 62 #include "net/ssl/ssl_server_config.h" | |
| 57 #include "net/ssl/test_ssl_private_key.h" | 63 #include "net/ssl/test_ssl_private_key.h" |
| 58 #include "net/test/cert_test_util.h" | 64 #include "net/test/cert_test_util.h" |
| 59 #include "net/test/gtest_util.h" | 65 #include "net/test/gtest_util.h" |
| 60 #include "net/test/spawned_test_server/spawned_test_server.h" | 66 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 61 #include "net/test/test_data_directory.h" | 67 #include "net/test/test_data_directory.h" |
| 62 #include "testing/gmock/include/gmock/gmock.h" | 68 #include "testing/gmock/include/gmock/gmock.h" |
| 63 #include "testing/gtest/include/gtest/gtest.h" | 69 #include "testing/gtest/include/gtest/gtest.h" |
| 64 #include "testing/platform_test.h" | 70 #include "testing/platform_test.h" |
| 65 #include "third_party/boringssl/src/include/openssl/bio.h" | 71 #include "third_party/boringssl/src/include/openssl/bio.h" |
| 66 #include "third_party/boringssl/src/include/openssl/evp.h" | 72 #include "third_party/boringssl/src/include/openssl/evp.h" |
| (...skipping 2972 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3039 | 3045 |
| 3040 int rv; | 3046 int rv; |
| 3041 ASSERT_TRUE(CreateAndConnectSSLClientSocket(client_config, &rv)); | 3047 ASSERT_TRUE(CreateAndConnectSSLClientSocket(client_config, &rv)); |
| 3042 EXPECT_THAT(rv, IsOk()); | 3048 EXPECT_THAT(rv, IsOk()); |
| 3043 | 3049 |
| 3044 EXPECT_EQ(kProtoUnknown, sock_->GetNegotiatedProtocol()); | 3050 EXPECT_EQ(kProtoUnknown, sock_->GetNegotiatedProtocol()); |
| 3045 } | 3051 } |
| 3046 | 3052 |
| 3047 namespace { | 3053 namespace { |
| 3048 | 3054 |
| 3049 // Loads a PEM-encoded private key file into a SSLPrivateKey object. | 3055 bssl::UniquePtr<EVP_PKEY> LoadEVP_PKEY(const base::FilePath& filepath) { |
| 3050 // |filepath| is the private key file path. | |
| 3051 // Returns the new SSLPrivateKey. | |
| 3052 scoped_refptr<SSLPrivateKey> LoadPrivateKeyOpenSSL( | |
| 3053 const base::FilePath& filepath) { | |
| 3054 std::string data; | 3056 std::string data; |
| 3055 if (!base::ReadFileToString(filepath, &data)) { | 3057 if (!base::ReadFileToString(filepath, &data)) { |
| 3056 LOG(ERROR) << "Could not read private key file: " << filepath.value(); | 3058 LOG(ERROR) << "Could not read private key file: " << filepath.value(); |
| 3057 return nullptr; | 3059 return nullptr; |
| 3058 } | 3060 } |
| 3059 bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(const_cast<char*>(data.data()), | 3061 bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(const_cast<char*>(data.data()), |
| 3060 static_cast<int>(data.size()))); | 3062 static_cast<int>(data.size()))); |
| 3061 if (!bio) { | 3063 if (!bio) { |
| 3062 LOG(ERROR) << "Could not allocate BIO for buffer?"; | 3064 LOG(ERROR) << "Could not allocate BIO for buffer?"; |
| 3063 return nullptr; | 3065 return nullptr; |
| 3064 } | 3066 } |
| 3065 bssl::UniquePtr<EVP_PKEY> result( | 3067 bssl::UniquePtr<EVP_PKEY> result( |
| 3066 PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr)); | 3068 PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr)); |
| 3067 if (!result) { | 3069 if (!result) { |
| 3068 LOG(ERROR) << "Could not decode private key file: " << filepath.value(); | 3070 LOG(ERROR) << "Could not decode private key file: " << filepath.value(); |
| 3069 return nullptr; | 3071 return nullptr; |
| 3070 } | 3072 } |
| 3071 return WrapOpenSSLPrivateKey(std::move(result)); | 3073 return result; |
| 3074 } | |
| 3075 | |
| 3076 // Loads a PEM-encoded private key file into a SSLPrivateKey object. | |
| 3077 // |filepath| is the private key file path. | |
| 3078 // Returns the new SSLPrivateKey. | |
| 3079 scoped_refptr<SSLPrivateKey> LoadPrivateKeyOpenSSL( | |
| 3080 const base::FilePath& filepath) { | |
| 3081 bssl::UniquePtr<EVP_PKEY> key = LoadEVP_PKEY(filepath); | |
| 3082 if (!key) | |
| 3083 return nullptr; | |
| 3084 return WrapOpenSSLPrivateKey(std::move(key)); | |
| 3072 } | 3085 } |
| 3073 | 3086 |
| 3074 } // namespace | 3087 } // namespace |
| 3075 | 3088 |
| 3076 // Connect to a server requesting client authentication, do not send | 3089 // Connect to a server requesting client authentication, do not send |
| 3077 // any client certificates. It should refuse the connection. | 3090 // any client certificates. It should refuse the connection. |
| 3078 TEST_F(SSLClientSocketTest, NoCert) { | 3091 TEST_F(SSLClientSocketTest, NoCert) { |
| 3079 SpawnedTestServer::SSLOptions ssl_options; | 3092 SpawnedTestServer::SSLOptions ssl_options; |
| 3080 ssl_options.request_client_certificate = true; | 3093 ssl_options.request_client_certificate = true; |
| 3081 ASSERT_TRUE(StartTestServer(ssl_options)); | 3094 ASSERT_TRUE(StartTestServer(ssl_options)); |
| (...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3614 | 3627 |
| 3615 // Dump memory again and check that |buffer_size| contain the read buffer. | 3628 // Dump memory again and check that |buffer_size| contain the read buffer. |
| 3616 StreamSocket::SocketMemoryStats stats2; | 3629 StreamSocket::SocketMemoryStats stats2; |
| 3617 sock_->DumpMemoryStats(&stats2); | 3630 sock_->DumpMemoryStats(&stats2); |
| 3618 EXPECT_EQ(17 * 1024u, stats2.buffer_size); | 3631 EXPECT_EQ(17 * 1024u, stats2.buffer_size); |
| 3619 EXPECT_EQ(1u, stats2.cert_count); | 3632 EXPECT_EQ(1u, stats2.cert_count); |
| 3620 EXPECT_LT(0u, stats2.cert_size); | 3633 EXPECT_LT(0u, stats2.cert_size); |
| 3621 EXPECT_LT(17 * 1024u, stats2.total_size); | 3634 EXPECT_LT(17 * 1024u, stats2.total_size); |
| 3622 } | 3635 } |
| 3623 | 3636 |
| 3637 TEST_F(SSLClientSocketTest, IdleAfterRead) { | |
| 3638 IPAddress lo_address = IPAddress::IPv4Localhost(); | |
| 3639 | |
| 3640 // Set up a TCP server. | |
| 3641 TCPServerSocket server_listener(NULL, NetLogSource()); | |
| 3642 ASSERT_THAT(server_listener.Listen(IPEndPoint(lo_address, 0), 1), IsOk()); | |
|
svaldez
2017/03/07 22:05:54
Any reason not just to inline lo_address (not used
davidben
2017/03/07 22:37:01
Done.
| |
| 3643 IPEndPoint server_address; | |
| 3644 ASSERT_THAT(server_listener.GetLocalAddress(&server_address), IsOk()); | |
| 3645 | |
| 3646 // Connect a TCP client and server socket. | |
| 3647 TestCompletionCallback server_callback; | |
| 3648 std::unique_ptr<StreamSocket> server_transport; | |
| 3649 int server_rv = | |
| 3650 server_listener.Accept(&server_transport, server_callback.callback()); | |
| 3651 | |
| 3652 TestCompletionCallback client_callback; | |
| 3653 std::unique_ptr<TCPClientSocket> client_transport(new TCPClientSocket( | |
| 3654 AddressList(server_address), NULL, NULL, NetLogSource())); | |
| 3655 int client_rv = client_transport->Connect(client_callback.callback()); | |
| 3656 | |
| 3657 EXPECT_THAT(server_callback.GetResult(server_rv), IsOk()); | |
| 3658 EXPECT_THAT(client_callback.GetResult(client_rv), IsOk()); | |
| 3659 | |
| 3660 // Set up an SSL server. | |
| 3661 base::FilePath certs_dir = GetTestCertsDirectory(); | |
| 3662 scoped_refptr<net::X509Certificate> cert = | |
| 3663 ImportCertFromFile(certs_dir, "ok_cert.pem"); | |
| 3664 ASSERT_TRUE(cert); | |
| 3665 bssl::UniquePtr<EVP_PKEY> pkey = | |
| 3666 LoadEVP_PKEY(certs_dir.AppendASCII("ok_cert.pem")); | |
| 3667 ASSERT_TRUE(pkey); | |
| 3668 std::unique_ptr<crypto::RSAPrivateKey> key = | |
| 3669 crypto::RSAPrivateKey::CreateFromKey(pkey.get()); | |
| 3670 ASSERT_TRUE(key); | |
| 3671 std::unique_ptr<SSLServerContext> server_context = | |
| 3672 CreateSSLServerContext(cert.get(), *key.get(), SSLServerConfig()); | |
| 3673 | |
| 3674 // Complete the SSL handshake on both sides. | |
| 3675 std::unique_ptr<SSLClientSocket> client(CreateSSLClientSocket( | |
| 3676 std::move(client_transport), | |
| 3677 HostPortPair("localhost", server_address.port()), SSLConfig())); | |
|
svaldez
2017/03/07 22:05:54
HostPortPair::FromIPEndpoint(server_address)
davidben
2017/03/07 22:37:01
Done.
| |
| 3678 std::unique_ptr<SSLServerSocket> server( | |
| 3679 server_context->CreateSSLServerSocket(std::move(server_transport))); | |
| 3680 | |
| 3681 server_rv = server->Handshake(server_callback.callback()); | |
| 3682 client_rv = client->Connect(client_callback.callback()); | |
| 3683 | |
| 3684 EXPECT_THAT(server_callback.GetResult(server_rv), IsOk()); | |
| 3685 EXPECT_THAT(client_callback.GetResult(client_rv), IsOk()); | |
| 3686 | |
| 3687 // Write a single record on the server. | |
| 3688 scoped_refptr<IOBuffer> write_buf(new StringIOBuffer("a")); | |
| 3689 server_rv = server->Write(write_buf.get(), 1, server_callback.callback()); | |
| 3690 | |
| 3691 // Read that record on the server, but with a much larger buffer than | |
| 3692 // necessary. | |
| 3693 scoped_refptr<IOBuffer> read_buf(new IOBuffer(1024)); | |
| 3694 client_rv = client->Read(read_buf.get(), 1024, client_callback.callback()); | |
| 3695 | |
| 3696 EXPECT_EQ(1, server_callback.GetResult(server_rv)); | |
| 3697 EXPECT_EQ(1, client_callback.GetResult(client_rv)); | |
| 3698 | |
| 3699 // At this point the client socket should be idle. | |
| 3700 EXPECT_TRUE(client->IsConnectedAndIdle()); | |
| 3701 | |
| 3702 // The read buffer should be released. | |
| 3703 StreamSocket::SocketMemoryStats stats; | |
| 3704 client->DumpMemoryStats(&stats); | |
| 3705 EXPECT_EQ(0u, stats.buffer_size); | |
| 3706 EXPECT_EQ(1u, stats.cert_count); | |
| 3707 EXPECT_LT(0u, stats.cert_size); | |
| 3708 EXPECT_EQ(stats.cert_size, stats.total_size); | |
| 3709 } | |
| 3710 | |
| 3624 } // namespace net | 3711 } // namespace net |
| OLD | NEW |