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" | |
davidben
2015/11/04 00:17:38
(Stray change? Guessing a funny rebase.)
svaldez
2015/11/04 16:55:52
Done.
| |
19 #include "base/lazy_instance.h" | 20 #include "base/lazy_instance.h" |
20 #include "base/memory/singleton.h" | 21 #include "base/memory/singleton.h" |
21 #include "base/metrics/histogram_macros.h" | 22 #include "base/metrics/histogram_macros.h" |
22 #include "base/profiler/scoped_tracker.h" | 23 #include "base/profiler/scoped_tracker.h" |
23 #include "base/stl_util.h" | 24 #include "base/stl_util.h" |
24 #include "base/strings/string_piece.h" | 25 #include "base/strings/string_piece.h" |
25 #include "base/synchronization/lock.h" | 26 #include "base/synchronization/lock.h" |
26 #include "base/threading/sequenced_worker_pool.h" | |
27 #include "base/threading/thread_local.h" | 27 #include "base/threading/thread_local.h" |
28 #include "base/values.h" | 28 #include "base/values.h" |
29 #include "crypto/ec_private_key.h" | 29 #include "crypto/ec_private_key.h" |
30 #include "crypto/openssl_util.h" | 30 #include "crypto/openssl_util.h" |
31 #include "crypto/scoped_openssl_types.h" | 31 #include "crypto/scoped_openssl_types.h" |
32 #include "net/base/ip_address_number.h" | 32 #include "net/base/ip_address_number.h" |
33 #include "net/base/net_errors.h" | 33 #include "net/base/net_errors.h" |
34 #include "net/cert/cert_policy_enforcer.h" | 34 #include "net/cert/cert_policy_enforcer.h" |
35 #include "net/cert/cert_verifier.h" | 35 #include "net/cert/cert_verifier.h" |
36 #include "net/cert/ct_ev_whitelist.h" | 36 #include "net/cert/ct_ev_whitelist.h" |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
160 *hash = SSLPrivateKey::Hash::SHA384; | 160 *hash = SSLPrivateKey::Hash::SHA384; |
161 return true; | 161 return true; |
162 case NID_sha512: | 162 case NID_sha512: |
163 *hash = SSLPrivateKey::Hash::SHA512; | 163 *hash = SSLPrivateKey::Hash::SHA512; |
164 return true; | 164 return true; |
165 default: | 165 default: |
166 return false; | 166 return false; |
167 } | 167 } |
168 } | 168 } |
169 | 169 |
170 #if !defined(OS_NACL) | |
171 class PlatformKeyTaskRunner { | |
172 public: | |
173 PlatformKeyTaskRunner() { | |
174 // Serialize all the private key operations on a single background | |
175 // thread to avoid problems with buggy smartcards. | |
176 worker_pool_ = new base::SequencedWorkerPool(1, "Platform Key Thread"); | |
177 task_runner_ = worker_pool_->GetSequencedTaskRunnerWithShutdownBehavior( | |
178 worker_pool_->GetSequenceToken(), | |
179 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | |
180 } | |
181 | |
182 scoped_refptr<base::SequencedTaskRunner> task_runner() { | |
183 return task_runner_; | |
184 } | |
185 | |
186 private: | |
187 scoped_refptr<base::SequencedWorkerPool> worker_pool_; | |
188 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
189 | |
190 DISALLOW_COPY_AND_ASSIGN(PlatformKeyTaskRunner); | |
191 }; | |
192 | |
193 base::LazyInstance<PlatformKeyTaskRunner>::Leaky g_platform_key_task_runner = | |
194 LAZY_INSTANCE_INITIALIZER; | |
195 #endif | |
196 | |
197 } // namespace | 170 } // namespace |
198 | 171 |
199 class SSLClientSocketOpenSSL::SSLContext { | 172 class SSLClientSocketOpenSSL::SSLContext { |
200 public: | 173 public: |
201 static SSLContext* GetInstance() { | 174 static SSLContext* GetInstance() { |
202 return base::Singleton<SSLContext>::get(); | 175 return base::Singleton<SSLContext>::get(); |
203 } | 176 } |
204 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } | 177 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } |
205 SSLClientSessionCacheOpenSSL* session_cache() { return &session_cache_; } | 178 SSLClientSessionCacheOpenSSL* session_cache() { return &session_cache_; } |
206 | 179 |
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
605 | 578 |
606 npn_status_ = kNextProtoUnsupported; | 579 npn_status_ = kNextProtoUnsupported; |
607 npn_proto_.clear(); | 580 npn_proto_.clear(); |
608 | 581 |
609 channel_id_sent_ = false; | 582 channel_id_sent_ = false; |
610 session_pending_ = false; | 583 session_pending_ = false; |
611 certificate_verified_ = false; | 584 certificate_verified_ = false; |
612 channel_id_request_.Cancel(); | 585 channel_id_request_.Cancel(); |
613 ssl_failure_state_ = SSL_FAILURE_NONE; | 586 ssl_failure_state_ = SSL_FAILURE_NONE; |
614 | 587 |
615 private_key_.reset(); | |
davidben
2015/11/04 00:17:38
Hrm. Actually, I think you can do away with this f
svaldez
2015/11/04 16:55:52
Done.
| |
616 signature_result_ = kNoPendingResult; | 588 signature_result_ = kNoPendingResult; |
617 signature_.clear(); | 589 signature_.clear(); |
618 } | 590 } |
619 | 591 |
620 bool SSLClientSocketOpenSSL::IsConnected() const { | 592 bool SSLClientSocketOpenSSL::IsConnected() const { |
621 // If the handshake has not yet completed. | 593 // If the handshake has not yet completed. |
622 if (!completed_connect_) | 594 if (!completed_connect_) |
623 return false; | 595 return false; |
624 // If an asynchronous operation is still pending. | 596 // If an asynchronous operation is still pending. |
625 if (user_read_buf_.get() || user_write_buf_.get()) | 597 if (user_read_buf_.get() || user_write_buf_.get()) |
(...skipping 1167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1793 return -1; | 1765 return -1; |
1794 } | 1766 } |
1795 | 1767 |
1796 if (!SSL_use_certificate(ssl_, leaf_x509.get()) || | 1768 if (!SSL_use_certificate(ssl_, leaf_x509.get()) || |
1797 !SSL_set1_chain(ssl_, chain.get())) { | 1769 !SSL_set1_chain(ssl_, chain.get())) { |
1798 LOG(WARNING) << "Failed to set client certificate"; | 1770 LOG(WARNING) << "Failed to set client certificate"; |
1799 return -1; | 1771 return -1; |
1800 } | 1772 } |
1801 | 1773 |
1802 #if defined(OS_NACL) | 1774 #if defined(OS_NACL) |
1803 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); | 1775 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); |
1804 return -1; | 1776 return -1; |
1805 #else | 1777 #else |
1806 // TODO(davidben): Lift this call up to the embedder so we can actually test | 1778 private_key_ = ssl_config_.client_private_key; |
1807 // this code. https://crbug.com/394131 | 1779 |
1808 private_key_ = FetchClientCertPrivateKey( | |
1809 ssl_config_.client_cert.get(), | |
1810 g_platform_key_task_runner.Get().task_runner()); | |
1811 if (!private_key_) { | 1780 if (!private_key_) { |
1812 // Could not find the private key. Fail the handshake and surface an | 1781 // The caller supplied a null private key. Fail the handshake and surface |
1813 // appropriate error to the caller. | 1782 // an appropriate error to the caller. |
1814 LOG(WARNING) << "Client cert found without private key"; | 1783 LOG(WARNING) << "Client cert found without private key"; |
1815 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); | 1784 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); |
1816 return -1; | 1785 return -1; |
1817 } | 1786 } |
1818 | 1787 |
1819 SSL_set_private_key_method(ssl_, &SSLContext::kPrivateKeyMethod); | 1788 SSL_set_private_key_method(ssl_, &SSLContext::kPrivateKeyMethod); |
1820 | 1789 |
1821 std::vector<SSLPrivateKey::Hash> digest_prefs = | 1790 std::vector<SSLPrivateKey::Hash> digest_prefs = |
1822 private_key_->GetDigestPreferences(); | 1791 private_key_->GetDigestPreferences(); |
1823 | 1792 |
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2158 OnHandshakeIOComplete(signature_result_); | 2127 OnHandshakeIOComplete(signature_result_); |
2159 return; | 2128 return; |
2160 } | 2129 } |
2161 | 2130 |
2162 // During a renegotiation, either Read or Write calls may be blocked on an | 2131 // During a renegotiation, either Read or Write calls may be blocked on an |
2163 // asynchronous private key operation. | 2132 // asynchronous private key operation. |
2164 PumpReadWriteEvents(); | 2133 PumpReadWriteEvents(); |
2165 } | 2134 } |
2166 | 2135 |
2167 } // namespace net | 2136 } // namespace net |
OLD | NEW |