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 // OpenSSL binding for SSLClientSocket. The class layout and general principle | 5 // OpenSSL binding for SSLClientSocket. The class layout and general principle |
6 // of operation is derived from SSLClientSocketNSS. | 6 // of operation is derived from SSLClientSocketNSS. |
7 | 7 |
8 #include "net/socket/ssl_client_socket_openssl.h" | 8 #include "net/socket/ssl_client_socket_openssl.h" |
9 | 9 |
10 #include <errno.h> | 10 #include <errno.h> |
11 #include <openssl/bio.h> | 11 #include <openssl/bio.h> |
12 #include <openssl/err.h> | 12 #include <openssl/err.h> |
13 #include <openssl/mem.h> | 13 #include <openssl/mem.h> |
14 #include <openssl/ssl.h> | 14 #include <openssl/ssl.h> |
15 #include <string.h> | 15 #include <string.h> |
16 | 16 |
17 #include "base/bind.h" | 17 #include "base/bind.h" |
18 #include "base/callback_helpers.h" | 18 #include "base/callback_helpers.h" |
19 #include "base/environment.h" | 19 #include "base/environment.h" |
20 #include "base/lazy_instance.h" | |
21 #include "base/memory/singleton.h" | 20 #include "base/memory/singleton.h" |
22 #include "base/metrics/histogram_macros.h" | 21 #include "base/metrics/histogram_macros.h" |
23 #include "base/profiler/scoped_tracker.h" | 22 #include "base/profiler/scoped_tracker.h" |
24 #include "base/stl_util.h" | 23 #include "base/stl_util.h" |
25 #include "base/strings/string_piece.h" | 24 #include "base/strings/string_piece.h" |
26 #include "base/synchronization/lock.h" | 25 #include "base/synchronization/lock.h" |
27 #include "base/threading/sequenced_worker_pool.h" | |
28 #include "base/threading/thread_local.h" | 26 #include "base/threading/thread_local.h" |
29 #include "base/values.h" | 27 #include "base/values.h" |
30 #include "crypto/ec_private_key.h" | 28 #include "crypto/ec_private_key.h" |
31 #include "crypto/openssl_util.h" | 29 #include "crypto/openssl_util.h" |
32 #include "crypto/scoped_openssl_types.h" | 30 #include "crypto/scoped_openssl_types.h" |
33 #include "net/base/ip_address_number.h" | 31 #include "net/base/ip_address_number.h" |
34 #include "net/base/net_errors.h" | 32 #include "net/base/net_errors.h" |
35 #include "net/cert/cert_policy_enforcer.h" | 33 #include "net/cert/cert_policy_enforcer.h" |
36 #include "net/cert/cert_verifier.h" | 34 #include "net/cert/cert_verifier.h" |
37 #include "net/cert/ct_ev_whitelist.h" | 35 #include "net/cert/ct_ev_whitelist.h" |
38 #include "net/cert/ct_verifier.h" | 36 #include "net/cert/ct_verifier.h" |
39 #include "net/cert/x509_certificate_net_log_param.h" | 37 #include "net/cert/x509_certificate_net_log_param.h" |
40 #include "net/cert/x509_util_openssl.h" | 38 #include "net/cert/x509_util_openssl.h" |
41 #include "net/http/transport_security_state.h" | 39 #include "net/http/transport_security_state.h" |
42 #include "net/ssl/scoped_openssl_types.h" | 40 #include "net/ssl/scoped_openssl_types.h" |
43 #include "net/ssl/ssl_cert_request_info.h" | 41 #include "net/ssl/ssl_cert_request_info.h" |
44 #include "net/ssl/ssl_client_session_cache_openssl.h" | 42 #include "net/ssl/ssl_client_session_cache_openssl.h" |
45 #include "net/ssl/ssl_connection_status_flags.h" | 43 #include "net/ssl/ssl_connection_status_flags.h" |
46 #include "net/ssl/ssl_failure_state.h" | 44 #include "net/ssl/ssl_failure_state.h" |
47 #include "net/ssl/ssl_info.h" | 45 #include "net/ssl/ssl_info.h" |
48 #include "net/ssl/ssl_private_key.h" | 46 #include "net/ssl/ssl_private_key.h" |
49 | 47 |
50 #if defined(OS_WIN) | 48 #if defined(OS_WIN) |
51 #include "base/win/windows_version.h" | 49 #include "base/win/windows_version.h" |
52 #endif | 50 #endif |
53 | 51 |
54 #if !defined(OS_NACL) | |
55 #include "net/ssl/ssl_platform_key.h" | |
56 #endif | |
57 | |
58 namespace net { | 52 namespace net { |
59 | 53 |
60 namespace { | 54 namespace { |
61 | 55 |
62 // Enable this to see logging for state machine state transitions. | 56 // Enable this to see logging for state machine state transitions. |
63 #if 0 | 57 #if 0 |
64 #define GotoState(s) do { DVLOG(2) << (void *)this << " " << __FUNCTION__ << \ | 58 #define GotoState(s) do { DVLOG(2) << (void *)this << " " << __FUNCTION__ << \ |
65 " jump to state " << s; \ | 59 " jump to state " << s; \ |
66 next_handshake_state_ = s; } while (0) | 60 next_handshake_state_ = s; } while (0) |
67 #else | 61 #else |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 *hash = SSLPrivateKey::Hash::SHA384; | 156 *hash = SSLPrivateKey::Hash::SHA384; |
163 return true; | 157 return true; |
164 case NID_sha512: | 158 case NID_sha512: |
165 *hash = SSLPrivateKey::Hash::SHA512; | 159 *hash = SSLPrivateKey::Hash::SHA512; |
166 return true; | 160 return true; |
167 default: | 161 default: |
168 return false; | 162 return false; |
169 } | 163 } |
170 } | 164 } |
171 | 165 |
172 #if !defined(OS_NACL) | |
173 class PlatformKeyTaskRunner { | |
174 public: | |
175 PlatformKeyTaskRunner() { | |
176 // Serialize all the private key operations on a single background | |
177 // thread to avoid problems with buggy smartcards. | |
178 worker_pool_ = new base::SequencedWorkerPool(1, "Platform Key Thread"); | |
179 task_runner_ = worker_pool_->GetSequencedTaskRunnerWithShutdownBehavior( | |
180 worker_pool_->GetSequenceToken(), | |
181 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | |
182 } | |
183 | |
184 scoped_refptr<base::SequencedTaskRunner> task_runner() { | |
185 return task_runner_; | |
186 } | |
187 | |
188 private: | |
189 scoped_refptr<base::SequencedWorkerPool> worker_pool_; | |
190 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
191 | |
192 DISALLOW_COPY_AND_ASSIGN(PlatformKeyTaskRunner); | |
193 }; | |
194 | |
195 base::LazyInstance<PlatformKeyTaskRunner>::Leaky g_platform_key_task_runner = | |
196 LAZY_INSTANCE_INITIALIZER; | |
197 #endif | |
198 | |
199 } // namespace | 166 } // namespace |
200 | 167 |
201 class SSLClientSocketOpenSSL::SSLContext { | 168 class SSLClientSocketOpenSSL::SSLContext { |
202 public: | 169 public: |
203 static SSLContext* GetInstance() { | 170 static SSLContext* GetInstance() { |
204 return base::Singleton<SSLContext>::get(); | 171 return base::Singleton<SSLContext>::get(); |
205 } | 172 } |
206 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } | 173 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } |
207 SSLClientSessionCacheOpenSSL* session_cache() { return &session_cache_; } | 174 SSLClientSessionCacheOpenSSL* session_cache() { return &session_cache_; } |
208 | 175 |
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
621 | 588 |
622 npn_status_ = kNextProtoUnsupported; | 589 npn_status_ = kNextProtoUnsupported; |
623 npn_proto_.clear(); | 590 npn_proto_.clear(); |
624 | 591 |
625 channel_id_sent_ = false; | 592 channel_id_sent_ = false; |
626 session_pending_ = false; | 593 session_pending_ = false; |
627 certificate_verified_ = false; | 594 certificate_verified_ = false; |
628 channel_id_request_.Cancel(); | 595 channel_id_request_.Cancel(); |
629 ssl_failure_state_ = SSL_FAILURE_NONE; | 596 ssl_failure_state_ = SSL_FAILURE_NONE; |
630 | 597 |
631 private_key_.reset(); | |
632 signature_result_ = kNoPendingResult; | 598 signature_result_ = kNoPendingResult; |
633 signature_.clear(); | 599 signature_.clear(); |
634 } | 600 } |
635 | 601 |
636 bool SSLClientSocketOpenSSL::IsConnected() const { | 602 bool SSLClientSocketOpenSSL::IsConnected() const { |
637 // If the handshake has not yet completed. | 603 // If the handshake has not yet completed. |
638 if (!completed_connect_) | 604 if (!completed_connect_) |
639 return false; | 605 return false; |
640 // If an asynchronous operation is still pending. | 606 // If an asynchronous operation is still pending. |
641 if (user_read_buf_.get() || user_write_buf_.get()) | 607 if (user_read_buf_.get() || user_write_buf_.get()) |
(...skipping 1159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1801 return -1; | 1767 return -1; |
1802 } | 1768 } |
1803 | 1769 |
1804 if (!SSL_use_certificate(ssl_, leaf_x509.get()) || | 1770 if (!SSL_use_certificate(ssl_, leaf_x509.get()) || |
1805 !SSL_set1_chain(ssl_, chain.get())) { | 1771 !SSL_set1_chain(ssl_, chain.get())) { |
1806 LOG(WARNING) << "Failed to set client certificate"; | 1772 LOG(WARNING) << "Failed to set client certificate"; |
1807 return -1; | 1773 return -1; |
1808 } | 1774 } |
1809 | 1775 |
1810 #if defined(OS_NACL) | 1776 #if defined(OS_NACL) |
1811 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); | 1777 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); |
1812 return -1; | 1778 return -1; |
1813 #else | 1779 #else |
1814 // TODO(davidben): Lift this call up to the embedder so we can actually test | 1780 private_key_ = ssl_config_.client_private_key; |
1815 // this code. https://crbug.com/394131 | 1781 |
1816 private_key_ = FetchClientCertPrivateKey( | |
1817 ssl_config_.client_cert.get(), | |
1818 g_platform_key_task_runner.Get().task_runner()); | |
1819 if (!private_key_) { | 1782 if (!private_key_) { |
1820 // Could not find the private key. Fail the handshake and surface an | 1783 // Could not find the private key. Fail the handshake and surface an |
1821 // appropriate error to the caller. | 1784 // appropriate error to the caller. |
1822 LOG(WARNING) << "Client cert found without private key"; | 1785 LOG(WARNING) << "Client cert found without private key"; |
1823 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); | 1786 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); |
1824 return -1; | 1787 return -1; |
1825 } | 1788 } |
davidben
2015/10/13 20:32:16
Oh hrm. That hadn't occurred to me. It's kind of c
svaldez
2015/10/14 15:06:18
Done.
| |
1826 | 1789 |
1827 SSL_set_private_key_method(ssl_, &SSLContext::kPrivateKeyMethod); | 1790 SSL_set_private_key_method(ssl_, &SSLContext::kPrivateKeyMethod); |
1828 #endif | 1791 #endif |
1829 | 1792 |
1830 int cert_count = 1 + sk_X509_num(chain.get()); | 1793 int cert_count = 1 + sk_X509_num(chain.get()); |
1831 net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_PROVIDED, | 1794 net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_PROVIDED, |
1832 NetLog::IntegerCallback("cert_count", cert_count)); | 1795 NetLog::IntegerCallback("cert_count", cert_count)); |
1833 return 1; | 1796 return 1; |
1834 } | 1797 } |
1835 #endif // defined(OS_IOS) | 1798 #endif // defined(OS_IOS) |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2139 OnHandshakeIOComplete(signature_result_); | 2102 OnHandshakeIOComplete(signature_result_); |
2140 return; | 2103 return; |
2141 } | 2104 } |
2142 | 2105 |
2143 // During a renegotiation, either Read or Write calls may be blocked on an | 2106 // During a renegotiation, either Read or Write calls may be blocked on an |
2144 // asynchronous private key operation. | 2107 // asynchronous private key operation. |
2145 PumpReadWriteEvents(); | 2108 PumpReadWriteEvents(); |
2146 } | 2109 } |
2147 | 2110 |
2148 } // namespace net | 2111 } // namespace net |
OLD | NEW |