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 <openssl/bio.h> | 10 #include <openssl/bio.h> |
11 #include <openssl/bn.h> | 11 #include <openssl/bn.h> |
12 #include <openssl/evp.h> | 12 #include <openssl/evp.h> |
13 #include <openssl/pem.h> | 13 #include <openssl/pem.h> |
14 #include <openssl/rsa.h> | 14 #include <openssl/rsa.h> |
15 | 15 |
16 #include "base/file_util.h" | 16 #include "base/file_util.h" |
17 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
19 #include "base/message_loop/message_loop_proxy.h" | 19 #include "base/message_loop/message_loop_proxy.h" |
20 #include "base/values.h" | 20 #include "base/values.h" |
21 #include "crypto/openssl_util.h" | 21 #include "crypto/openssl_util.h" |
| 22 #include "crypto/scoped_openssl_types.h" |
22 #include "net/base/address_list.h" | 23 #include "net/base/address_list.h" |
23 #include "net/base/io_buffer.h" | 24 #include "net/base/io_buffer.h" |
24 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
25 #include "net/base/net_log.h" | 26 #include "net/base/net_log.h" |
26 #include "net/base/net_log_unittest.h" | 27 #include "net/base/net_log_unittest.h" |
27 #include "net/base/test_completion_callback.h" | 28 #include "net/base/test_completion_callback.h" |
28 #include "net/base/test_data_directory.h" | 29 #include "net/base/test_data_directory.h" |
29 #include "net/cert/mock_cert_verifier.h" | 30 #include "net/cert/mock_cert_verifier.h" |
30 #include "net/cert/test_root_certs.h" | 31 #include "net/cert/test_root_certs.h" |
31 #include "net/dns/host_resolver.h" | 32 #include "net/dns/host_resolver.h" |
32 #include "net/http/transport_security_state.h" | 33 #include "net/http/transport_security_state.h" |
33 #include "net/socket/client_socket_factory.h" | 34 #include "net/socket/client_socket_factory.h" |
34 #include "net/socket/client_socket_handle.h" | 35 #include "net/socket/client_socket_handle.h" |
35 #include "net/socket/socket_test_util.h" | 36 #include "net/socket/socket_test_util.h" |
36 #include "net/socket/tcp_client_socket.h" | 37 #include "net/socket/tcp_client_socket.h" |
37 #include "net/ssl/openssl_client_key_store.h" | 38 #include "net/ssl/openssl_client_key_store.h" |
38 #include "net/ssl/ssl_cert_request_info.h" | 39 #include "net/ssl/ssl_cert_request_info.h" |
39 #include "net/ssl/ssl_config_service.h" | 40 #include "net/ssl/ssl_config_service.h" |
40 #include "net/test/cert_test_util.h" | 41 #include "net/test/cert_test_util.h" |
41 #include "net/test/spawned_test_server/spawned_test_server.h" | 42 #include "net/test/spawned_test_server/spawned_test_server.h" |
42 #include "testing/gtest/include/gtest/gtest.h" | 43 #include "testing/gtest/include/gtest/gtest.h" |
43 #include "testing/platform_test.h" | 44 #include "testing/platform_test.h" |
44 | 45 |
45 namespace net { | 46 namespace net { |
46 | 47 |
47 namespace { | 48 namespace { |
48 | 49 |
49 // These client auth tests are currently dependent on OpenSSL's struct X509. | 50 // These client auth tests are currently dependent on OpenSSL's struct X509. |
50 #if defined(USE_OPENSSL_CERTS) | 51 #if defined(USE_OPENSSL_CERTS) |
51 typedef OpenSSLClientKeyStore::ScopedEVP_PKEY ScopedEVP_PKEY; | |
52 | |
53 // BIO_free is a macro, it can't be used as a template parameter. | |
54 void BIO_free_func(BIO* bio) { | |
55 BIO_free(bio); | |
56 } | |
57 | |
58 typedef crypto::ScopedOpenSSL<BIO, BIO_free_func> ScopedBIO; | |
59 typedef crypto::ScopedOpenSSL<RSA, RSA_free> ScopedRSA; | |
60 typedef crypto::ScopedOpenSSL<BIGNUM, BN_free> ScopedBIGNUM; | |
61 | 52 |
62 const SSLConfig kDefaultSSLConfig; | 53 const SSLConfig kDefaultSSLConfig; |
63 | 54 |
64 // Loads a PEM-encoded private key file into a scoped EVP_PKEY object. | 55 // Loads a PEM-encoded private key file into a scoped EVP_PKEY object. |
65 // |filepath| is the private key file path. | 56 // |filepath| is the private key file path. |
66 // |*pkey| is reset to the new EVP_PKEY on success, untouched otherwise. | 57 // |*pkey| is reset to the new EVP_PKEY on success, untouched otherwise. |
67 // Returns true on success, false on failure. | 58 // Returns true on success, false on failure. |
68 bool LoadPrivateKeyOpenSSL( | 59 bool LoadPrivateKeyOpenSSL( |
69 const base::FilePath& filepath, | 60 const base::FilePath& filepath, |
70 OpenSSLClientKeyStore::ScopedEVP_PKEY* pkey) { | 61 OpenSSLClientKeyStore::ScopedEVP_PKEY* pkey) { |
71 std::string data; | 62 std::string data; |
72 if (!base::ReadFileToString(filepath, &data)) { | 63 if (!base::ReadFileToString(filepath, &data)) { |
73 LOG(ERROR) << "Could not read private key file: " | 64 LOG(ERROR) << "Could not read private key file: " |
74 << filepath.value() << ": " << strerror(errno); | 65 << filepath.value() << ": " << strerror(errno); |
75 return false; | 66 return false; |
76 } | 67 } |
77 ScopedBIO bio( | 68 crypto::ScopedBIO bio(BIO_new_mem_buf( |
78 BIO_new_mem_buf( | 69 const_cast<char*>(reinterpret_cast<const char*>(data.data())), |
79 const_cast<char*>(reinterpret_cast<const char*>(data.data())), | 70 static_cast<int>(data.size()))); |
80 static_cast<int>(data.size()))); | |
81 if (!bio.get()) { | 71 if (!bio.get()) { |
82 LOG(ERROR) << "Could not allocate BIO for buffer?"; | 72 LOG(ERROR) << "Could not allocate BIO for buffer?"; |
83 return false; | 73 return false; |
84 } | 74 } |
85 EVP_PKEY* result = PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL); | 75 EVP_PKEY* result = PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL); |
86 if (result == NULL) { | 76 if (result == NULL) { |
87 LOG(ERROR) << "Could not decode private key file: " | 77 LOG(ERROR) << "Could not decode private key file: " |
88 << filepath.value(); | 78 << filepath.value(); |
89 return false; | 79 return false; |
90 } | 80 } |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 | 263 |
274 EXPECT_TRUE(CheckSSLClientSocketSentCert()); | 264 EXPECT_TRUE(CheckSSLClientSocketSentCert()); |
275 | 265 |
276 sock_->Disconnect(); | 266 sock_->Disconnect(); |
277 EXPECT_FALSE(sock_->IsConnected()); | 267 EXPECT_FALSE(sock_->IsConnected()); |
278 } | 268 } |
279 #endif // defined(USE_OPENSSL_CERTS) | 269 #endif // defined(USE_OPENSSL_CERTS) |
280 | 270 |
281 } // namespace | 271 } // namespace |
282 } // namespace net | 272 } // namespace net |
OLD | NEW |