[email protected] | 013c17c | 2012-01-21 19:09:01 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // OpenSSL binding for SSLClientSocket. The class layout and general principle |
| 6 | // of operation is derived from SSLClientSocketNSS. |
| 7 | |
| 8 | #include "net/socket/ssl_client_socket_openssl.h" |
| 9 | |
[email protected] | edfd0f4 | 2014-07-22 18:20:37 | [diff] [blame] | 10 | #include <errno.h> |
davidben | 018aad6 | 2014-09-12 02:25:19 | [diff] [blame] | 11 | #include <openssl/bio.h> |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame^] | 12 | #include <openssl/bytestring.h> |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 13 | #include <openssl/err.h> |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame^] | 14 | #include <openssl/evp.h> |
davidben | 121e9c96 | 2015-05-01 00:40:49 | [diff] [blame] | 15 | #include <openssl/mem.h> |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 16 | #include <openssl/ssl.h> |
bnc | 67da3de | 2015-01-15 21:02:26 | [diff] [blame] | 17 | #include <string.h> |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 18 | |
[email protected] | 0f7804ec | 2011-10-07 20:04:18 | [diff] [blame] | 19 | #include "base/bind.h" |
[email protected] | f2da6ac | 2013-02-04 08:22:53 | [diff] [blame] | 20 | #include "base/callback_helpers.h" |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 21 | #include "base/lazy_instance.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 22 | #include "base/memory/singleton.h" |
asvitkine | c3c9372 | 2015-06-17 14:48:37 | [diff] [blame] | 23 | #include "base/metrics/histogram_macros.h" |
vadimt | b2a77c76 | 2014-11-21 19:49:22 | [diff] [blame] | 24 | #include "base/profiler/scoped_tracker.h" |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 25 | #include "base/stl_util.h" |
davidben | 018aad6 | 2014-09-12 02:25:19 | [diff] [blame] | 26 | #include "base/strings/string_piece.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 27 | #include "base/synchronization/lock.h" |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 28 | #include "base/threading/sequenced_worker_pool.h" |
vadimt | 6b43dec2 | 2015-01-06 01:59:58 | [diff] [blame] | 29 | #include "base/threading/thread_local.h" |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 30 | #include "base/values.h" |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 31 | #include "crypto/ec_private_key.h" |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 32 | #include "crypto/openssl_util.h" |
[email protected] | cd9b75b | 2014-07-10 04:39:38 | [diff] [blame] | 33 | #include "crypto/scoped_openssl_types.h" |
davidben | 9bc0466f | 2015-06-16 22:21:27 | [diff] [blame] | 34 | #include "net/base/ip_address_number.h" |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 35 | #include "net/base/net_errors.h" |
eranm | 6571b2b | 2014-12-03 15:53:23 | [diff] [blame] | 36 | #include "net/cert/cert_policy_enforcer.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 37 | #include "net/cert/cert_verifier.h" |
eranm | efbd313 | 2014-10-28 16:35:16 | [diff] [blame] | 38 | #include "net/cert/ct_ev_whitelist.h" |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 39 | #include "net/cert/ct_verifier.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 40 | #include "net/cert/x509_certificate_net_log_param.h" |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 41 | #include "net/cert/x509_util_openssl.h" |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 42 | #include "net/http/transport_security_state.h" |
davidben | c879af0 | 2015-02-20 07:57:21 | [diff] [blame] | 43 | #include "net/ssl/scoped_openssl_types.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 44 | #include "net/ssl/ssl_cert_request_info.h" |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 45 | #include "net/ssl/ssl_client_session_cache_openssl.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 46 | #include "net/ssl/ssl_connection_status_flags.h" |
davidben | f2eaaf9 | 2015-05-15 22:18:42 | [diff] [blame] | 47 | #include "net/ssl/ssl_failure_state.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 48 | #include "net/ssl/ssl_info.h" |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 49 | #include "net/ssl/ssl_private_key.h" |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 50 | |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 51 | #if defined(OS_WIN) |
| 52 | #include "base/win/windows_version.h" |
| 53 | #endif |
| 54 | |
svaldez | b92efb3 | 2015-08-28 16:38:44 | [diff] [blame] | 55 | #if !defined(OS_NACL) |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 56 | #include "net/ssl/ssl_platform_key.h" |
[email protected] | 97a854f | 2014-07-29 07:51:36 | [diff] [blame] | 57 | #endif |
| 58 | |
davidben | a477a70a | 2015-10-06 04:38:29 | [diff] [blame] | 59 | #if defined(USE_NSS_CERTS) || defined(OS_IOS) |
| 60 | #include "net/cert_net/nss_ocsp.h" |
| 61 | #endif |
| 62 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 63 | namespace net { |
| 64 | |
| 65 | namespace { |
| 66 | |
| 67 | // Enable this to see logging for state machine state transitions. |
| 68 | #if 0 |
[email protected] | 3b11277 | 2010-10-04 10:54:49 | [diff] [blame] | 69 | #define GotoState(s) do { DVLOG(2) << (void *)this << " " << __FUNCTION__ << \ |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 70 | " jump to state " << s; \ |
| 71 | next_handshake_state_ = s; } while (0) |
| 72 | #else |
| 73 | #define GotoState(s) next_handshake_state_ = s |
| 74 | #endif |
| 75 | |
[email protected] | 4b76856 | 2013-02-16 04:10:07 | [diff] [blame] | 76 | // This constant can be any non-negative/non-zero value (eg: it does not |
| 77 | // overlap with any value of the net::Error range, including net::OK). |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 78 | const int kNoPendingResult = 1; |
[email protected] | 4b76856 | 2013-02-16 04:10:07 | [diff] [blame] | 79 | |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 80 | // If a client doesn't have a list of protocols that it supports, but |
| 81 | // the server supports NPN, choosing "http/1.1" is the best answer. |
| 82 | const char kDefaultSupportedNPNProtocol[] = "http/1.1"; |
| 83 | |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 84 | // Default size of the internal BoringSSL buffers. |
| 85 | const int KDefaultOpenSSLBufferSize = 17 * 1024; |
| 86 | |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame^] | 87 | // TLS extension number use for Token Binding. |
| 88 | const unsigned int kTbExtNum = 30033; |
| 89 | |
| 90 | // Token Binding ProtocolVersions supported. |
| 91 | const uint8_t kTbProtocolVersionMajor = 0; |
| 92 | const uint8_t kTbProtocolVersionMinor = 3; |
| 93 | const uint8_t kTbMinProtocolVersionMajor = 0; |
| 94 | const uint8_t kTbMinProtocolVersionMinor = 2; |
| 95 | |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 96 | void FreeX509Stack(STACK_OF(X509)* ptr) { |
| 97 | sk_X509_pop_free(ptr, X509_free); |
| 98 | } |
| 99 | |
davidben | e94fe096 | 2015-02-21 00:51:33 | [diff] [blame] | 100 | using ScopedX509Stack = crypto::ScopedOpenSSL<STACK_OF(X509), FreeX509Stack>; |
[email protected] | 6bad505 | 2014-07-12 01:25:13 | [diff] [blame] | 101 | |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 102 | // Used for encoding the |connection_status| field of an SSLInfo object. |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 103 | int EncodeSSLConnectionStatus(uint16 cipher_suite, |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 104 | int compression, |
| 105 | int version) { |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 106 | return cipher_suite | |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 107 | ((compression & SSL_CONNECTION_COMPRESSION_MASK) << |
| 108 | SSL_CONNECTION_COMPRESSION_SHIFT) | |
| 109 | ((version & SSL_CONNECTION_VERSION_MASK) << |
| 110 | SSL_CONNECTION_VERSION_SHIFT); |
| 111 | } |
| 112 | |
| 113 | // Returns the net SSL version number (see ssl_connection_status_flags.h) for |
| 114 | // this SSL connection. |
| 115 | int GetNetSSLVersion(SSL* ssl) { |
[email protected] | 7e5dd49f | 2010-12-08 18:33:49 | [diff] [blame] | 116 | switch (SSL_version(ssl)) { |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 117 | case TLS1_VERSION: |
| 118 | return SSL_CONNECTION_VERSION_TLS1; |
davidben | 1d09402 | 2014-11-05 18:55:47 | [diff] [blame] | 119 | case TLS1_1_VERSION: |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 120 | return SSL_CONNECTION_VERSION_TLS1_1; |
davidben | 1d09402 | 2014-11-05 18:55:47 | [diff] [blame] | 121 | case TLS1_2_VERSION: |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 122 | return SSL_CONNECTION_VERSION_TLS1_2; |
| 123 | default: |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 124 | NOTREACHED(); |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 125 | return SSL_CONNECTION_VERSION_UNKNOWN; |
| 126 | } |
| 127 | } |
| 128 | |
[email protected] | 6bad505 | 2014-07-12 01:25:13 | [diff] [blame] | 129 | ScopedX509 OSCertHandleToOpenSSL( |
| 130 | X509Certificate::OSCertHandle os_handle) { |
| 131 | #if defined(USE_OPENSSL_CERTS) |
| 132 | return ScopedX509(X509Certificate::DupOSCertHandle(os_handle)); |
| 133 | #else // !defined(USE_OPENSSL_CERTS) |
| 134 | std::string der_encoded; |
| 135 | if (!X509Certificate::GetDEREncoded(os_handle, &der_encoded)) |
| 136 | return ScopedX509(); |
| 137 | const uint8_t* bytes = reinterpret_cast<const uint8_t*>(der_encoded.data()); |
| 138 | return ScopedX509(d2i_X509(NULL, &bytes, der_encoded.size())); |
| 139 | #endif // defined(USE_OPENSSL_CERTS) |
| 140 | } |
| 141 | |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 142 | ScopedX509Stack OSCertHandlesToOpenSSL( |
| 143 | const X509Certificate::OSCertHandles& os_handles) { |
| 144 | ScopedX509Stack stack(sk_X509_new_null()); |
| 145 | for (size_t i = 0; i < os_handles.size(); i++) { |
| 146 | ScopedX509 x509 = OSCertHandleToOpenSSL(os_handles[i]); |
| 147 | if (!x509) |
| 148 | return ScopedX509Stack(); |
| 149 | sk_X509_push(stack.get(), x509.release()); |
| 150 | } |
| 151 | return stack.Pass(); |
| 152 | } |
| 153 | |
davidben | 018aad6 | 2014-09-12 02:25:19 | [diff] [blame] | 154 | int LogErrorCallback(const char* str, size_t len, void* context) { |
| 155 | LOG(ERROR) << base::StringPiece(str, len); |
| 156 | return 1; |
| 157 | } |
| 158 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 159 | bool EVP_MDToPrivateKeyHash(const EVP_MD* md, SSLPrivateKey::Hash* hash) { |
| 160 | switch (EVP_MD_type(md)) { |
| 161 | case NID_md5_sha1: |
| 162 | *hash = SSLPrivateKey::Hash::MD5_SHA1; |
| 163 | return true; |
| 164 | case NID_sha1: |
| 165 | *hash = SSLPrivateKey::Hash::SHA1; |
| 166 | return true; |
| 167 | case NID_sha256: |
| 168 | *hash = SSLPrivateKey::Hash::SHA256; |
| 169 | return true; |
| 170 | case NID_sha384: |
| 171 | *hash = SSLPrivateKey::Hash::SHA384; |
| 172 | return true; |
| 173 | case NID_sha512: |
| 174 | *hash = SSLPrivateKey::Hash::SHA512; |
| 175 | return true; |
| 176 | default: |
| 177 | return false; |
| 178 | } |
| 179 | } |
| 180 | |
svaldez | b92efb3 | 2015-08-28 16:38:44 | [diff] [blame] | 181 | #if !defined(OS_NACL) |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 182 | class PlatformKeyTaskRunner { |
| 183 | public: |
| 184 | PlatformKeyTaskRunner() { |
| 185 | // Serialize all the private key operations on a single background |
| 186 | // thread to avoid problems with buggy smartcards. |
| 187 | worker_pool_ = new base::SequencedWorkerPool(1, "Platform Key Thread"); |
| 188 | task_runner_ = worker_pool_->GetSequencedTaskRunnerWithShutdownBehavior( |
| 189 | worker_pool_->GetSequenceToken(), |
| 190 | base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); |
| 191 | } |
| 192 | |
| 193 | scoped_refptr<base::SequencedTaskRunner> task_runner() { |
| 194 | return task_runner_; |
| 195 | } |
| 196 | |
| 197 | private: |
| 198 | scoped_refptr<base::SequencedWorkerPool> worker_pool_; |
| 199 | scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 200 | |
| 201 | DISALLOW_COPY_AND_ASSIGN(PlatformKeyTaskRunner); |
| 202 | }; |
| 203 | |
| 204 | base::LazyInstance<PlatformKeyTaskRunner>::Leaky g_platform_key_task_runner = |
| 205 | LAZY_INSTANCE_INITIALIZER; |
svaldez | b92efb3 | 2015-08-28 16:38:44 | [diff] [blame] | 206 | #endif |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 207 | |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame^] | 208 | class ScopedCBB { |
| 209 | public: |
| 210 | ScopedCBB() { CBB_zero(&cbb_); } |
| 211 | ~ScopedCBB() { CBB_cleanup(&cbb_); } |
| 212 | |
| 213 | CBB* get() { return &cbb_; } |
| 214 | |
| 215 | private: |
| 216 | CBB cbb_; |
| 217 | DISALLOW_COPY_AND_ASSIGN(ScopedCBB); |
| 218 | }; |
| 219 | |
[email protected] | 821e3bb | 2013-11-08 01:06:01 | [diff] [blame] | 220 | } // namespace |
| 221 | |
| 222 | class SSLClientSocketOpenSSL::SSLContext { |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 223 | public: |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 224 | static SSLContext* GetInstance() { |
| 225 | return base::Singleton<SSLContext>::get(); |
| 226 | } |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 227 | SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 228 | SSLClientSessionCacheOpenSSL* session_cache() { return &session_cache_; } |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 229 | |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 230 | SSLClientSocketOpenSSL* GetClientSocketFromSSL(const SSL* ssl) { |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 231 | DCHECK(ssl); |
| 232 | SSLClientSocketOpenSSL* socket = static_cast<SSLClientSocketOpenSSL*>( |
| 233 | SSL_get_ex_data(ssl, ssl_socket_data_index_)); |
| 234 | DCHECK(socket); |
| 235 | return socket; |
| 236 | } |
| 237 | |
| 238 | bool SetClientSocketForSSL(SSL* ssl, SSLClientSocketOpenSSL* socket) { |
| 239 | return SSL_set_ex_data(ssl, ssl_socket_data_index_, socket) != 0; |
| 240 | } |
| 241 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 242 | static const SSL_PRIVATE_KEY_METHOD kPrivateKeyMethod; |
| 243 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 244 | private: |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 245 | friend struct base::DefaultSingletonTraits<SSLContext>; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 246 | |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 247 | SSLContext() : session_cache_(SSLClientSessionCacheOpenSSL::Config()) { |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 248 | crypto::EnsureOpenSSLInit(); |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 249 | ssl_socket_data_index_ = SSL_get_ex_new_index(0, 0, 0, 0, 0); |
| 250 | DCHECK_NE(ssl_socket_data_index_, -1); |
| 251 | ssl_ctx_.reset(SSL_CTX_new(SSLv23_client_method())); |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 252 | SSL_CTX_set_cert_verify_callback(ssl_ctx_.get(), CertVerifyCallback, NULL); |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 253 | SSL_CTX_set_cert_cb(ssl_ctx_.get(), ClientCertRequestCallback, NULL); |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 254 | SSL_CTX_set_verify(ssl_ctx_.get(), SSL_VERIFY_PEER, NULL); |
haavardm | c80b0ee3 | 2015-01-30 09:16:08 | [diff] [blame] | 255 | // This stops |SSL_shutdown| from generating the close_notify message, which |
| 256 | // is currently not sent on the network. |
| 257 | // TODO(haavardm): Remove setting quiet shutdown once 118366 is fixed. |
| 258 | SSL_CTX_set_quiet_shutdown(ssl_ctx_.get(), 1); |
bnc | 2d6bd0f | 2015-10-28 01:52:05 | [diff] [blame] | 259 | // Note that SSL_OP_DISABLE_NPN is used to disable NPN if |
| 260 | // ssl_config_.next_proto is empty. |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 261 | SSL_CTX_set_next_proto_select_cb(ssl_ctx_.get(), SelectNextProtoCallback, |
| 262 | NULL); |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 263 | |
| 264 | // Disable the internal session cache. Session caching is handled |
| 265 | // externally (i.e. by SSLClientSessionCacheOpenSSL). |
| 266 | SSL_CTX_set_session_cache_mode( |
| 267 | ssl_ctx_.get(), SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL); |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 268 | SSL_CTX_sess_set_new_cb(ssl_ctx_.get(), NewSessionCallback); |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame^] | 269 | |
| 270 | if (!SSL_CTX_add_client_custom_ext(ssl_ctx_.get(), kTbExtNum, |
| 271 | &TokenBindingAddCallback, |
| 272 | &TokenBindingFreeCallback, nullptr, |
| 273 | &TokenBindingParseCallback, nullptr)) { |
| 274 | NOTREACHED(); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | static int TokenBindingAddCallback(SSL* ssl, |
| 279 | unsigned int extension_value, |
| 280 | const uint8_t** out, |
| 281 | size_t* out_len, |
| 282 | int* out_alert_value, |
| 283 | void* add_arg) { |
| 284 | DCHECK_EQ(extension_value, kTbExtNum); |
| 285 | SSLClientSocketOpenSSL* socket = |
| 286 | SSLClientSocketOpenSSL::SSLContext::GetInstance() |
| 287 | ->GetClientSocketFromSSL(ssl); |
| 288 | return socket->TokenBindingAdd(out, out_len, out_alert_value); |
| 289 | } |
| 290 | |
| 291 | static void TokenBindingFreeCallback(SSL* ssl, |
| 292 | unsigned extension_value, |
| 293 | const uint8_t* out, |
| 294 | void* add_arg) { |
| 295 | DCHECK_EQ(extension_value, kTbExtNum); |
| 296 | OPENSSL_free(const_cast<unsigned char*>(out)); |
| 297 | } |
| 298 | |
| 299 | static int TokenBindingParseCallback(SSL* ssl, |
| 300 | unsigned int extension_value, |
| 301 | const uint8_t* contents, |
| 302 | size_t contents_len, |
| 303 | int* out_alert_value, |
| 304 | void* parse_arg) { |
| 305 | DCHECK_EQ(extension_value, kTbExtNum); |
| 306 | SSLClientSocketOpenSSL* socket = |
| 307 | SSLClientSocketOpenSSL::SSLContext::GetInstance() |
| 308 | ->GetClientSocketFromSSL(ssl); |
| 309 | return socket->TokenBindingParse(contents, contents_len, out_alert_value); |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 310 | } |
| 311 | |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 312 | static int ClientCertRequestCallback(SSL* ssl, void* arg) { |
[email protected] | b29af7d | 2010-12-14 11:52:47 | [diff] [blame] | 313 | SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 314 | DCHECK(socket); |
| 315 | return socket->ClientCertRequestCallback(ssl); |
[email protected] | 718c967 | 2010-12-02 10:04:10 | [diff] [blame] | 316 | } |
| 317 | |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 318 | static int CertVerifyCallback(X509_STORE_CTX *store_ctx, void *arg) { |
| 319 | SSL* ssl = reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data( |
| 320 | store_ctx, SSL_get_ex_data_X509_STORE_CTX_idx())); |
| 321 | SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
| 322 | CHECK(socket); |
| 323 | |
| 324 | return socket->CertVerifyCallback(store_ctx); |
| 325 | } |
| 326 | |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 327 | static int SelectNextProtoCallback(SSL* ssl, |
| 328 | unsigned char** out, unsigned char* outlen, |
| 329 | const unsigned char* in, |
| 330 | unsigned int inlen, void* arg) { |
[email protected] | b29af7d | 2010-12-14 11:52:47 | [diff] [blame] | 331 | SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 332 | return socket->SelectNextProtoCallback(out, outlen, in, inlen); |
| 333 | } |
| 334 | |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 335 | static int NewSessionCallback(SSL* ssl, SSL_SESSION* session) { |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 336 | SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 337 | return socket->NewSessionCallback(session); |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 338 | } |
| 339 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 340 | static int PrivateKeyTypeCallback(SSL* ssl) { |
| 341 | SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
| 342 | return socket->PrivateKeyTypeCallback(); |
| 343 | } |
| 344 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 345 | static size_t PrivateKeyMaxSignatureLenCallback(SSL* ssl) { |
| 346 | SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
| 347 | return socket->PrivateKeyMaxSignatureLenCallback(); |
| 348 | } |
| 349 | |
| 350 | static ssl_private_key_result_t PrivateKeySignCallback(SSL* ssl, |
| 351 | uint8_t* out, |
| 352 | size_t* out_len, |
| 353 | size_t max_out, |
| 354 | const EVP_MD* md, |
| 355 | const uint8_t* in, |
| 356 | size_t in_len) { |
| 357 | SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
| 358 | return socket->PrivateKeySignCallback(out, out_len, max_out, md, in, |
| 359 | in_len); |
| 360 | } |
| 361 | |
| 362 | static ssl_private_key_result_t PrivateKeySignCompleteCallback( |
| 363 | SSL* ssl, |
| 364 | uint8_t* out, |
| 365 | size_t* out_len, |
| 366 | size_t max_out) { |
| 367 | SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
| 368 | return socket->PrivateKeySignCompleteCallback(out, out_len, max_out); |
| 369 | } |
| 370 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 371 | // This is the index used with SSL_get_ex_data to retrieve the owner |
| 372 | // SSLClientSocketOpenSSL object from an SSL instance. |
| 373 | int ssl_socket_data_index_; |
| 374 | |
davidben | c879af0 | 2015-02-20 07:57:21 | [diff] [blame] | 375 | ScopedSSL_CTX ssl_ctx_; |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 376 | |
| 377 | // TODO(davidben): Use a separate cache per URLRequestContext. |
| 378 | // https://crbug.com/458365 |
| 379 | // |
| 380 | // TODO(davidben): Sessions should be invalidated on fatal |
| 381 | // alerts. https://crbug.com/466352 |
| 382 | SSLClientSessionCacheOpenSSL session_cache_; |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 383 | }; |
| 384 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 385 | const SSL_PRIVATE_KEY_METHOD |
| 386 | SSLClientSocketOpenSSL::SSLContext::kPrivateKeyMethod = { |
| 387 | &SSLClientSocketOpenSSL::SSLContext::PrivateKeyTypeCallback, |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 388 | &SSLClientSocketOpenSSL::SSLContext::PrivateKeyMaxSignatureLenCallback, |
| 389 | &SSLClientSocketOpenSSL::SSLContext::PrivateKeySignCallback, |
| 390 | &SSLClientSocketOpenSSL::SSLContext::PrivateKeySignCompleteCallback, |
| 391 | }; |
| 392 | |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 393 | // PeerCertificateChain is a helper object which extracts the certificate |
| 394 | // chain, as given by the server, from an OpenSSL socket and performs the needed |
| 395 | // resource management. The first element of the chain is the leaf certificate |
| 396 | // and the other elements are in the order given by the server. |
| 397 | class SSLClientSocketOpenSSL::PeerCertificateChain { |
| 398 | public: |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 399 | explicit PeerCertificateChain(STACK_OF(X509)* chain) { Reset(chain); } |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 400 | PeerCertificateChain(const PeerCertificateChain& other) { *this = other; } |
| 401 | ~PeerCertificateChain() {} |
| 402 | PeerCertificateChain& operator=(const PeerCertificateChain& other); |
| 403 | |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 404 | // Resets the PeerCertificateChain to the set of certificates in|chain|, |
| 405 | // which may be NULL, indicating to empty the store certificates. |
| 406 | // Note: If an error occurs, such as being unable to parse the certificates, |
| 407 | // this will behave as if Reset(NULL) was called. |
| 408 | void Reset(STACK_OF(X509)* chain); |
| 409 | |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 410 | // Note that when USE_OPENSSL is defined, OSCertHandle is X509* |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 411 | scoped_refptr<X509Certificate> AsOSChain() const; |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 412 | |
| 413 | size_t size() const { |
| 414 | if (!openssl_chain_.get()) |
| 415 | return 0; |
| 416 | return sk_X509_num(openssl_chain_.get()); |
| 417 | } |
| 418 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 419 | bool empty() const { |
| 420 | return size() == 0; |
| 421 | } |
| 422 | |
| 423 | X509* Get(size_t index) const { |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 424 | DCHECK_LT(index, size()); |
| 425 | return sk_X509_value(openssl_chain_.get(), index); |
| 426 | } |
| 427 | |
| 428 | private: |
[email protected] | cd9b75b | 2014-07-10 04:39:38 | [diff] [blame] | 429 | ScopedX509Stack openssl_chain_; |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 430 | }; |
| 431 | |
| 432 | SSLClientSocketOpenSSL::PeerCertificateChain& |
| 433 | SSLClientSocketOpenSSL::PeerCertificateChain::operator=( |
| 434 | const PeerCertificateChain& other) { |
| 435 | if (this == &other) |
| 436 | return *this; |
| 437 | |
[email protected] | 24176af | 2014-08-14 09:31:04 | [diff] [blame] | 438 | openssl_chain_.reset(X509_chain_up_ref(other.openssl_chain_.get())); |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 439 | return *this; |
| 440 | } |
| 441 | |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 442 | void SSLClientSocketOpenSSL::PeerCertificateChain::Reset( |
| 443 | STACK_OF(X509)* chain) { |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 444 | openssl_chain_.reset(chain ? X509_chain_up_ref(chain) : NULL); |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 445 | } |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 446 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 447 | scoped_refptr<X509Certificate> |
| 448 | SSLClientSocketOpenSSL::PeerCertificateChain::AsOSChain() const { |
| 449 | #if defined(USE_OPENSSL_CERTS) |
| 450 | // When OSCertHandle is typedef'ed to X509, this implementation does a short |
| 451 | // cut to avoid converting back and forth between DER and the X509 struct. |
| 452 | X509Certificate::OSCertHandles intermediates; |
| 453 | for (size_t i = 1; i < sk_X509_num(openssl_chain_.get()); ++i) { |
| 454 | intermediates.push_back(sk_X509_value(openssl_chain_.get(), i)); |
| 455 | } |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 456 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 457 | return make_scoped_refptr(X509Certificate::CreateFromHandle( |
| 458 | sk_X509_value(openssl_chain_.get(), 0), intermediates)); |
| 459 | #else |
| 460 | // DER-encode the chain and convert to a platform certificate handle. |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 461 | std::vector<base::StringPiece> der_chain; |
[email protected] | edfd0f4 | 2014-07-22 18:20:37 | [diff] [blame] | 462 | for (size_t i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 463 | X509* x = sk_X509_value(openssl_chain_.get(), i); |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 464 | base::StringPiece der; |
| 465 | if (!x509_util::GetDER(x, &der)) |
| 466 | return NULL; |
| 467 | der_chain.push_back(der); |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 468 | } |
| 469 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 470 | return make_scoped_refptr(X509Certificate::CreateFromDERCertChain(der_chain)); |
| 471 | #endif |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 472 | } |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 473 | |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 474 | // static |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 475 | void SSLClientSocket::ClearSessionCache() { |
[email protected] | 821e3bb | 2013-11-08 01:06:01 | [diff] [blame] | 476 | SSLClientSocketOpenSSL::SSLContext* context = |
| 477 | SSLClientSocketOpenSSL::SSLContext::GetInstance(); |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 478 | context->session_cache()->Flush(); |
| 479 | } |
| 480 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 481 | SSLClientSocketOpenSSL::SSLClientSocketOpenSSL( |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 482 | scoped_ptr<ClientSocketHandle> transport_socket, |
[email protected] | 055d7f2 | 2010-11-15 12:03:12 | [diff] [blame] | 483 | const HostPortPair& host_and_port, |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 484 | const SSLConfig& ssl_config, |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 485 | const SSLClientSocketContext& context) |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 486 | : transport_send_busy_(false), |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 487 | transport_recv_busy_(false), |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 488 | pending_read_error_(kNoPendingResult), |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 489 | pending_read_ssl_error_(SSL_ERROR_NONE), |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 490 | transport_read_error_(OK), |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 491 | transport_write_error_(OK), |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 492 | server_cert_chain_(new PeerCertificateChain(NULL)), |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 493 | completed_connect_(false), |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 494 | was_ever_used_(false), |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 495 | cert_verifier_(context.cert_verifier), |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 496 | cert_transparency_verifier_(context.cert_transparency_verifier), |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 497 | channel_id_service_(context.channel_id_service), |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame^] | 498 | tb_was_negotiated_(false), |
| 499 | tb_negotiated_param_(TB_PARAM_ECDSAP256), |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 500 | ssl_(NULL), |
| 501 | transport_bio_(NULL), |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 502 | transport_(transport_socket.Pass()), |
[email protected] | 055d7f2 | 2010-11-15 12:03:12 | [diff] [blame] | 503 | host_and_port_(host_and_port), |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 504 | ssl_config_(ssl_config), |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 505 | ssl_session_cache_shard_(context.ssl_session_cache_shard), |
[email protected] | 013c17c | 2012-01-21 19:09:01 | [diff] [blame] | 506 | next_handshake_state_(STATE_NONE), |
svaldez | 4af14d2 | 2015-08-20 13:48:24 | [diff] [blame] | 507 | disconnected_(false), |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 508 | npn_status_(kNextProtoUnsupported), |
davidben | 52053b38 | 2015-04-27 19:22:29 | [diff] [blame] | 509 | channel_id_sent_(false), |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 510 | session_pending_(false), |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 511 | certificate_verified_(false), |
davidben | f2eaaf9 | 2015-05-15 22:18:42 | [diff] [blame] | 512 | ssl_failure_state_(SSL_FAILURE_NONE), |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 513 | signature_result_(kNoPendingResult), |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 514 | transport_security_state_(context.transport_security_state), |
eranm | 6571b2b | 2014-12-03 15:53:23 | [diff] [blame] | 515 | policy_enforcer_(context.cert_policy_enforcer), |
kulkarni.a | cd7b446 | 2014-08-28 07:41:34 | [diff] [blame] | 516 | net_log_(transport_->socket()->NetLog()), |
| 517 | weak_factory_(this) { |
davidben | 9bbf329 | 2015-04-24 21:50:06 | [diff] [blame] | 518 | DCHECK(cert_verifier_); |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 519 | } |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 520 | |
| 521 | SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { |
| 522 | Disconnect(); |
| 523 | } |
| 524 | |
zhongyi | 81f85c6d9 | 2015-10-16 19:34:14 | [diff] [blame] | 525 | void SSLClientSocketOpenSSL::SetSSLKeyLogFile( |
| 526 | const std::string& ssl_keylog_file) { |
| 527 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 528 | BIO* bio = BIO_new_file(ssl_keylog_file.c_str(), "a"); |
| 529 | if (!bio) { |
| 530 | LOG(ERROR) << "Failed to open " << ssl_keylog_file; |
| 531 | ERR_print_errors_cb(&LogErrorCallback, NULL); |
| 532 | } else { |
| 533 | SSL_CTX_set_keylog_bio(SSLContext::GetInstance()->ssl_ctx(), bio); |
| 534 | } |
| 535 | } |
| 536 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 537 | void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( |
| 538 | SSLCertRequestInfo* cert_request_info) { |
[email protected] | 791879c | 2013-12-17 07:22:41 | [diff] [blame] | 539 | cert_request_info->host_and_port = host_and_port_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 540 | cert_request_info->cert_authorities = cert_authorities_; |
[email protected] | c078770 | 2014-05-20 21:51:44 | [diff] [blame] | 541 | cert_request_info->cert_key_types = cert_key_types_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | SSLClientSocket::NextProtoStatus SSLClientSocketOpenSSL::GetNextProto( |
davidben | 6974bf7 | 2015-04-27 17:52:48 | [diff] [blame] | 545 | std::string* proto) const { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 546 | *proto = npn_proto_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 547 | return npn_status_; |
| 548 | } |
| 549 | |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 550 | ChannelIDService* |
| 551 | SSLClientSocketOpenSSL::GetChannelIDService() const { |
| 552 | return channel_id_service_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 553 | } |
| 554 | |
davidben | f2eaaf9 | 2015-05-15 22:18:42 | [diff] [blame] | 555 | SSLFailureState SSLClientSocketOpenSSL::GetSSLFailureState() const { |
| 556 | return ssl_failure_state_; |
| 557 | } |
| 558 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 559 | int SSLClientSocketOpenSSL::ExportKeyingMaterial( |
| 560 | const base::StringPiece& label, |
| 561 | bool has_context, const base::StringPiece& context, |
| 562 | unsigned char* out, unsigned int outlen) { |
davidben | 86935f7 | 2015-05-06 22:24:49 | [diff] [blame] | 563 | if (!IsConnected()) |
| 564 | return ERR_SOCKET_NOT_CONNECTED; |
| 565 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 566 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 567 | |
| 568 | int rv = SSL_export_keying_material( |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 569 | ssl_, out, outlen, label.data(), label.size(), |
davidben | 866c3d4a7 | 2015-04-06 21:56:43 | [diff] [blame] | 570 | reinterpret_cast<const unsigned char*>(context.data()), context.length(), |
| 571 | has_context ? 1 : 0); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 572 | |
| 573 | if (rv != 1) { |
| 574 | int ssl_error = SSL_get_error(ssl_, rv); |
| 575 | LOG(ERROR) << "Failed to export keying material;" |
| 576 | << " returned " << rv |
| 577 | << ", SSL error code " << ssl_error; |
| 578 | return MapOpenSSLError(ssl_error, err_tracer); |
| 579 | } |
| 580 | return OK; |
| 581 | } |
| 582 | |
| 583 | int SSLClientSocketOpenSSL::GetTLSUniqueChannelBinding(std::string* out) { |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 584 | NOTIMPLEMENTED(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 585 | return ERR_NOT_IMPLEMENTED; |
| 586 | } |
| 587 | |
| 588 | int SSLClientSocketOpenSSL::Connect(const CompletionCallback& callback) { |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 589 | // It is an error to create an SSLClientSocket whose context has no |
| 590 | // TransportSecurityState. |
| 591 | DCHECK(transport_security_state_); |
| 592 | |
svaldez | 4af14d2 | 2015-08-20 13:48:24 | [diff] [blame] | 593 | // Although StreamSocket does allow calling Connect() after Disconnect(), |
| 594 | // this has never worked for layered sockets. CHECK to detect any consumers |
| 595 | // reconnecting an SSL socket. |
| 596 | // |
| 597 | // TODO(davidben,mmenke): Remove this API feature. See |
| 598 | // https://crbug.com/499289. |
| 599 | CHECK(!disconnected_); |
| 600 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 601 | net_log_.BeginEvent(NetLog::TYPE_SSL_CONNECT); |
| 602 | |
| 603 | // Set up new ssl object. |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 604 | int rv = Init(); |
| 605 | if (rv != OK) { |
| 606 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); |
| 607 | return rv; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | // Set SSL to client mode. Handshake happens in the loop below. |
| 611 | SSL_set_connect_state(ssl_); |
| 612 | |
| 613 | GotoState(STATE_HANDSHAKE); |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 614 | rv = DoHandshakeLoop(OK); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 615 | if (rv == ERR_IO_PENDING) { |
| 616 | user_connect_callback_ = callback; |
| 617 | } else { |
| 618 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); |
| 619 | } |
| 620 | |
| 621 | return rv > OK ? OK : rv; |
| 622 | } |
| 623 | |
| 624 | void SSLClientSocketOpenSSL::Disconnect() { |
| 625 | if (ssl_) { |
| 626 | // Calling SSL_shutdown prevents the session from being marked as |
| 627 | // unresumable. |
| 628 | SSL_shutdown(ssl_); |
| 629 | SSL_free(ssl_); |
| 630 | ssl_ = NULL; |
| 631 | } |
| 632 | if (transport_bio_) { |
| 633 | BIO_free_all(transport_bio_); |
| 634 | transport_bio_ = NULL; |
| 635 | } |
| 636 | |
svaldez | 4af14d2 | 2015-08-20 13:48:24 | [diff] [blame] | 637 | disconnected_ = true; |
| 638 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 639 | // Shut down anything that may call us back. |
eroman | 7f9236a | 2015-05-11 21:23:43 | [diff] [blame] | 640 | cert_verifier_request_.reset(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 641 | transport_->socket()->Disconnect(); |
| 642 | |
| 643 | // Null all callbacks, delete all buffers. |
| 644 | transport_send_busy_ = false; |
| 645 | send_buffer_ = NULL; |
| 646 | transport_recv_busy_ = false; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 647 | recv_buffer_ = NULL; |
| 648 | |
| 649 | user_connect_callback_.Reset(); |
| 650 | user_read_callback_.Reset(); |
| 651 | user_write_callback_.Reset(); |
| 652 | user_read_buf_ = NULL; |
| 653 | user_read_buf_len_ = 0; |
| 654 | user_write_buf_ = NULL; |
| 655 | user_write_buf_len_ = 0; |
| 656 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 657 | pending_read_error_ = kNoPendingResult; |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 658 | pending_read_ssl_error_ = SSL_ERROR_NONE; |
| 659 | pending_read_error_info_ = OpenSSLErrorInfo(); |
| 660 | |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 661 | transport_read_error_ = OK; |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 662 | transport_write_error_ = OK; |
| 663 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 664 | server_cert_verify_result_.Reset(); |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 665 | completed_connect_ = false; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 666 | |
| 667 | cert_authorities_.clear(); |
[email protected] | c078770 | 2014-05-20 21:51:44 | [diff] [blame] | 668 | cert_key_types_.clear(); |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 669 | |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 670 | start_cert_verification_time_ = base::TimeTicks(); |
| 671 | |
[email protected] | abc44b75 | 2014-07-30 03:52:15 | [diff] [blame] | 672 | npn_status_ = kNextProtoUnsupported; |
| 673 | npn_proto_.clear(); |
| 674 | |
davidben | 52053b38 | 2015-04-27 19:22:29 | [diff] [blame] | 675 | channel_id_sent_ = false; |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame^] | 676 | tb_was_negotiated_ = false; |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 677 | session_pending_ = false; |
davidben | f2eaaf9 | 2015-05-15 22:18:42 | [diff] [blame] | 678 | certificate_verified_ = false; |
nharper | 75ade89 | 2015-06-10 19:05:35 | [diff] [blame] | 679 | channel_id_request_.Cancel(); |
davidben | f2eaaf9 | 2015-05-15 22:18:42 | [diff] [blame] | 680 | ssl_failure_state_ = SSL_FAILURE_NONE; |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 681 | |
| 682 | private_key_.reset(); |
| 683 | signature_result_ = kNoPendingResult; |
| 684 | signature_.clear(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | bool SSLClientSocketOpenSSL::IsConnected() const { |
| 688 | // If the handshake has not yet completed. |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 689 | if (!completed_connect_) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 690 | return false; |
| 691 | // If an asynchronous operation is still pending. |
| 692 | if (user_read_buf_.get() || user_write_buf_.get()) |
| 693 | return true; |
| 694 | |
| 695 | return transport_->socket()->IsConnected(); |
| 696 | } |
| 697 | |
| 698 | bool SSLClientSocketOpenSSL::IsConnectedAndIdle() const { |
| 699 | // If the handshake has not yet completed. |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 700 | if (!completed_connect_) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 701 | return false; |
| 702 | // If an asynchronous operation is still pending. |
| 703 | if (user_read_buf_.get() || user_write_buf_.get()) |
| 704 | return false; |
davidben | fc9a6b8 | 2015-04-15 23:47:32 | [diff] [blame] | 705 | |
| 706 | // If there is data read from the network that has not yet been consumed, do |
| 707 | // not treat the connection as idle. |
| 708 | // |
| 709 | // Note that this does not check |BIO_pending|, whether there is ciphertext |
| 710 | // that has not yet been flushed to the network. |Write| returns early, so |
| 711 | // this can cause race conditions which cause a socket to not be treated |
| 712 | // reusable when it should be. See https://crbug.com/466147. |
| 713 | if (BIO_wpending(transport_bio_) > 0) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 714 | return false; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 715 | |
| 716 | return transport_->socket()->IsConnectedAndIdle(); |
| 717 | } |
| 718 | |
| 719 | int SSLClientSocketOpenSSL::GetPeerAddress(IPEndPoint* addressList) const { |
| 720 | return transport_->socket()->GetPeerAddress(addressList); |
| 721 | } |
| 722 | |
| 723 | int SSLClientSocketOpenSSL::GetLocalAddress(IPEndPoint* addressList) const { |
| 724 | return transport_->socket()->GetLocalAddress(addressList); |
| 725 | } |
| 726 | |
| 727 | const BoundNetLog& SSLClientSocketOpenSSL::NetLog() const { |
| 728 | return net_log_; |
| 729 | } |
| 730 | |
| 731 | void SSLClientSocketOpenSSL::SetSubresourceSpeculation() { |
| 732 | if (transport_.get() && transport_->socket()) { |
| 733 | transport_->socket()->SetSubresourceSpeculation(); |
| 734 | } else { |
| 735 | NOTREACHED(); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | void SSLClientSocketOpenSSL::SetOmniboxSpeculation() { |
| 740 | if (transport_.get() && transport_->socket()) { |
| 741 | transport_->socket()->SetOmniboxSpeculation(); |
| 742 | } else { |
| 743 | NOTREACHED(); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | bool SSLClientSocketOpenSSL::WasEverUsed() const { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 748 | return was_ever_used_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | bool SSLClientSocketOpenSSL::UsingTCPFastOpen() const { |
| 752 | if (transport_.get() && transport_->socket()) |
| 753 | return transport_->socket()->UsingTCPFastOpen(); |
| 754 | |
| 755 | NOTREACHED(); |
| 756 | return false; |
| 757 | } |
| 758 | |
| 759 | bool SSLClientSocketOpenSSL::GetSSLInfo(SSLInfo* ssl_info) { |
| 760 | ssl_info->Reset(); |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 761 | if (server_cert_chain_->empty()) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 762 | return false; |
| 763 | |
| 764 | ssl_info->cert = server_cert_verify_result_.verified_cert; |
estark | 03d644f | 2015-06-13 00:11:32 | [diff] [blame] | 765 | ssl_info->unverified_cert = server_cert_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 766 | ssl_info->cert_status = server_cert_verify_result_.cert_status; |
| 767 | ssl_info->is_issued_by_known_root = |
| 768 | server_cert_verify_result_.is_issued_by_known_root; |
| 769 | ssl_info->public_key_hashes = |
| 770 | server_cert_verify_result_.public_key_hashes; |
| 771 | ssl_info->client_cert_sent = |
| 772 | ssl_config_.send_client_cert && ssl_config_.client_cert.get(); |
davidben | 52053b38 | 2015-04-27 19:22:29 | [diff] [blame] | 773 | ssl_info->channel_id_sent = channel_id_sent_; |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame^] | 774 | ssl_info->token_binding_negotiated = tb_was_negotiated_; |
| 775 | ssl_info->token_binding_key_param = tb_negotiated_param_; |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 776 | ssl_info->pinning_failure_log = pinning_failure_log_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 777 | |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 778 | AddSCTInfoToSSLInfo(ssl_info); |
| 779 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 780 | const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_); |
| 781 | CHECK(cipher); |
| 782 | ssl_info->security_bits = SSL_CIPHER_get_bits(cipher, NULL); |
sigbjorn | 79cf372 | 2015-09-18 09:15:20 | [diff] [blame] | 783 | ssl_info->key_exchange_info = |
| 784 | SSL_SESSION_get_key_exchange_info(SSL_get_session(ssl_)); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 785 | |
| 786 | ssl_info->connection_status = EncodeSSLConnectionStatus( |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 787 | static_cast<uint16>(SSL_CIPHER_get_id(cipher)), 0 /* no compression */, |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 788 | GetNetSSLVersion(ssl_)); |
| 789 | |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 790 | if (!SSL_get_secure_renegotiation_support(ssl_)) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 791 | ssl_info->connection_status |= SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 792 | |
| 793 | if (ssl_config_.version_fallback) |
| 794 | ssl_info->connection_status |= SSL_CONNECTION_VERSION_FALLBACK; |
| 795 | |
| 796 | ssl_info->handshake_type = SSL_session_reused(ssl_) ? |
| 797 | SSLInfo::HANDSHAKE_RESUME : SSLInfo::HANDSHAKE_FULL; |
| 798 | |
| 799 | DVLOG(3) << "Encoded connection status: cipher suite = " |
| 800 | << SSLConnectionStatusToCipherSuite(ssl_info->connection_status) |
| 801 | << " version = " |
| 802 | << SSLConnectionStatusToVersion(ssl_info->connection_status); |
| 803 | return true; |
| 804 | } |
| 805 | |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 806 | void SSLClientSocketOpenSSL::GetConnectionAttempts( |
| 807 | ConnectionAttempts* out) const { |
| 808 | out->clear(); |
| 809 | } |
| 810 | |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 811 | int64_t SSLClientSocketOpenSSL::GetTotalReceivedBytes() const { |
| 812 | return transport_->socket()->GetTotalReceivedBytes(); |
| 813 | } |
| 814 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 815 | int SSLClientSocketOpenSSL::Read(IOBuffer* buf, |
| 816 | int buf_len, |
| 817 | const CompletionCallback& callback) { |
| 818 | user_read_buf_ = buf; |
| 819 | user_read_buf_len_ = buf_len; |
| 820 | |
davidben | 1b133ad | 2014-10-23 04:23:13 | [diff] [blame] | 821 | int rv = DoReadLoop(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 822 | |
| 823 | if (rv == ERR_IO_PENDING) { |
| 824 | user_read_callback_ = callback; |
| 825 | } else { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 826 | if (rv > 0) |
| 827 | was_ever_used_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 828 | user_read_buf_ = NULL; |
| 829 | user_read_buf_len_ = 0; |
| 830 | } |
| 831 | |
| 832 | return rv; |
| 833 | } |
| 834 | |
| 835 | int SSLClientSocketOpenSSL::Write(IOBuffer* buf, |
| 836 | int buf_len, |
| 837 | const CompletionCallback& callback) { |
| 838 | user_write_buf_ = buf; |
| 839 | user_write_buf_len_ = buf_len; |
| 840 | |
davidben | 1b133ad | 2014-10-23 04:23:13 | [diff] [blame] | 841 | int rv = DoWriteLoop(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 842 | |
| 843 | if (rv == ERR_IO_PENDING) { |
| 844 | user_write_callback_ = callback; |
| 845 | } else { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 846 | if (rv > 0) |
| 847 | was_ever_used_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 848 | user_write_buf_ = NULL; |
| 849 | user_write_buf_len_ = 0; |
| 850 | } |
| 851 | |
| 852 | return rv; |
| 853 | } |
| 854 | |
[email protected] | 28b96d1c | 2014-04-09 12:21:15 | [diff] [blame] | 855 | int SSLClientSocketOpenSSL::SetReceiveBufferSize(int32 size) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 856 | return transport_->socket()->SetReceiveBufferSize(size); |
| 857 | } |
| 858 | |
[email protected] | 28b96d1c | 2014-04-09 12:21:15 | [diff] [blame] | 859 | int SSLClientSocketOpenSSL::SetSendBufferSize(int32 size) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 860 | return transport_->socket()->SetSendBufferSize(size); |
| 861 | } |
| 862 | |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 863 | int SSLClientSocketOpenSSL::Init() { |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 864 | DCHECK(!ssl_); |
| 865 | DCHECK(!transport_bio_); |
| 866 | |
davidben | a477a70a | 2015-10-06 04:38:29 | [diff] [blame] | 867 | #if defined(USE_NSS_CERTS) || defined(OS_IOS) |
| 868 | if (ssl_config_.cert_io_enabled) { |
| 869 | // TODO(davidben): Move this out of SSLClientSocket. See |
| 870 | // https://crbug.com/539520. |
| 871 | EnsureNSSHttpIOInit(); |
| 872 | } |
| 873 | #endif |
| 874 | |
[email protected] | b29af7d | 2010-12-14 11:52:47 | [diff] [blame] | 875 | SSLContext* context = SSLContext::GetInstance(); |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 876 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 877 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 878 | ssl_ = SSL_new(context->ssl_ctx()); |
| 879 | if (!ssl_ || !context->SetClientSocketForSSL(ssl_, this)) |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 880 | return ERR_UNEXPECTED; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 881 | |
davidben | 9bc0466f | 2015-06-16 22:21:27 | [diff] [blame] | 882 | // SNI should only contain valid DNS hostnames, not IP addresses (see RFC |
| 883 | // 6066, Section 3). |
| 884 | // |
| 885 | // TODO(rsleevi): Should this code allow hostnames that violate the LDH rule? |
| 886 | // See https://crbug.com/496472 and https://crbug.com/496468 for discussion. |
| 887 | IPAddressNumber unused; |
| 888 | if (!ParseIPLiteralToNumber(host_and_port_.host(), &unused) && |
| 889 | !SSL_set_tlsext_host_name(ssl_, host_and_port_.host().c_str())) { |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 890 | return ERR_UNEXPECTED; |
davidben | 9bc0466f | 2015-06-16 22:21:27 | [diff] [blame] | 891 | } |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 892 | |
davidben | f4c9a612 | 2015-10-20 02:45:29 | [diff] [blame] | 893 | ScopedSSL_SESSION session = |
| 894 | context->session_cache()->Lookup(GetSessionCacheKey()); |
| 895 | if (session) |
| 896 | SSL_set_session(ssl_, session.get()); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 897 | |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 898 | send_buffer_ = new GrowableIOBuffer(); |
| 899 | send_buffer_->SetCapacity(KDefaultOpenSSLBufferSize); |
| 900 | recv_buffer_ = new GrowableIOBuffer(); |
| 901 | recv_buffer_->SetCapacity(KDefaultOpenSSLBufferSize); |
| 902 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 903 | BIO* ssl_bio = NULL; |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 904 | |
| 905 | // SSLClientSocketOpenSSL retains ownership of the BIO buffers. |
| 906 | if (!BIO_new_bio_pair_external_buf( |
| 907 | &ssl_bio, send_buffer_->capacity(), |
| 908 | reinterpret_cast<uint8_t*>(send_buffer_->data()), &transport_bio_, |
| 909 | recv_buffer_->capacity(), |
| 910 | reinterpret_cast<uint8_t*>(recv_buffer_->data()))) |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 911 | return ERR_UNEXPECTED; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 912 | DCHECK(ssl_bio); |
| 913 | DCHECK(transport_bio_); |
| 914 | |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 915 | // Install a callback on OpenSSL's end to plumb transport errors through. |
rsleevi | f020edc | 2015-03-16 19:31:24 | [diff] [blame] | 916 | BIO_set_callback(ssl_bio, &SSLClientSocketOpenSSL::BIOCallback); |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 917 | BIO_set_callback_arg(ssl_bio, reinterpret_cast<char*>(this)); |
| 918 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 919 | SSL_set_bio(ssl_, ssl_bio, ssl_bio); |
| 920 | |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 921 | DCHECK_LT(SSL3_VERSION, ssl_config_.version_min); |
| 922 | DCHECK_LT(SSL3_VERSION, ssl_config_.version_max); |
| 923 | SSL_set_min_version(ssl_, ssl_config_.version_min); |
| 924 | SSL_set_max_version(ssl_, ssl_config_.version_max); |
| 925 | |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 926 | // OpenSSL defaults some options to on, others to off. To avoid ambiguity, |
| 927 | // set everything we care about to an absolute value. |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 928 | SslSetClearMask options; |
[email protected] | d0f0049 | 2012-08-03 22:35:13 | [diff] [blame] | 929 | options.ConfigureFlag(SSL_OP_NO_COMPRESSION, true); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 930 | |
| 931 | // TODO(joth): Set this conditionally, see http://crbug.com/55410 |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 932 | options.ConfigureFlag(SSL_OP_LEGACY_SERVER_CONNECT, true); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 933 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 934 | SSL_set_options(ssl_, options.set_mask); |
| 935 | SSL_clear_options(ssl_, options.clear_mask); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 936 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 937 | // Same as above, this time for the SSL mode. |
| 938 | SslSetClearMask mode; |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 939 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 940 | mode.ConfigureFlag(SSL_MODE_RELEASE_BUFFERS, true); |
isherman | e5c05e1 | 2014-09-09 20:32:15 | [diff] [blame] | 941 | mode.ConfigureFlag(SSL_MODE_CBC_RECORD_SPLITTING, true); |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 942 | |
davidben | 818d93b | 2015-02-19 22:27:32 | [diff] [blame] | 943 | mode.ConfigureFlag(SSL_MODE_ENABLE_FALSE_START, |
[email protected] | b788de0 | 2014-04-23 18:06:07 | [diff] [blame] | 944 | ssl_config_.false_start_enabled); |
| 945 | |
davidben | 6b8131c | 2015-02-25 23:30:14 | [diff] [blame] | 946 | mode.ConfigureFlag(SSL_MODE_SEND_FALLBACK_SCSV, ssl_config_.version_fallback); |
| 947 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 948 | SSL_set_mode(ssl_, mode.set_mask); |
| 949 | SSL_clear_mode(ssl_, mode.clear_mask); |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 950 | |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 951 | // See SSLConfig::disabled_cipher_suites for description of the suites |
davidben | 9158569 | 2015-09-28 23:46:08 | [diff] [blame] | 952 | // disabled by default. Note that SHA256 and SHA384 only select HMAC-SHA256 |
[email protected] | 9b4bc4a9 | 2013-08-20 22:59:07 | [diff] [blame] | 953 | // and HMAC-SHA384 cipher suites, not GCM cipher suites with SHA256 or SHA384 |
| 954 | // as the handshake hash. |
davidben | 9158569 | 2015-09-28 23:46:08 | [diff] [blame] | 955 | std::string command("DEFAULT:!SHA256:-SHA384:!AESGCM+AES256:!aPSK"); |
davidben | 9b4a9b9c | 2015-10-12 18:46:51 | [diff] [blame] | 956 | |
| 957 | if (ssl_config_.require_ecdhe) |
| 958 | command.append(":!kRSA:!kDHE"); |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 959 | |
davidben | 14b1a53 | 2015-10-30 16:01:09 | [diff] [blame] | 960 | if (!ssl_config_.rc4_enabled) |
davidben | a4c9d06 | 2015-04-03 22:34:25 | [diff] [blame] | 961 | command.append(":!RC4"); |
davidben | 14b1a53 | 2015-10-30 16:01:09 | [diff] [blame] | 962 | |
| 963 | if (ssl_config_.deprecated_cipher_suites_enabled) { |
davidben | 9158569 | 2015-09-28 23:46:08 | [diff] [blame] | 964 | // Add TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 under a fallback. This is |
| 965 | // believed to work around a bug in some out-of-date Microsoft IIS servers |
| 966 | // which cause them to require the version downgrade |
| 967 | // (https://crbug.com/433406). |
| 968 | command.append(":ECDHE-RSA-AES256-SHA384"); |
| 969 | } |
davidben | a4c9d06 | 2015-04-03 22:34:25 | [diff] [blame] | 970 | |
davidben | 9b4a9b9c | 2015-10-12 18:46:51 | [diff] [blame] | 971 | // Remove any disabled ciphers. |
| 972 | for (uint16_t id : ssl_config_.disabled_cipher_suites) { |
| 973 | const SSL_CIPHER* cipher = SSL_get_cipher_by_value(id); |
| 974 | if (cipher) { |
| 975 | command.append(":!"); |
| 976 | command.append(SSL_CIPHER_get_name(cipher)); |
| 977 | } |
| 978 | } |
| 979 | |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 980 | // Disable ECDSA cipher suites on platforms that do not support ECDSA |
| 981 | // signed certificates, as servers may use the presence of such |
| 982 | // ciphersuites as a hint to send an ECDSA certificate. |
| 983 | #if defined(OS_WIN) |
| 984 | if (base::win::GetVersion() < base::win::VERSION_VISTA) |
| 985 | command.append(":!ECDSA"); |
| 986 | #endif |
| 987 | |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 988 | int rv = SSL_set_cipher_list(ssl_, command.c_str()); |
| 989 | // If this fails (rv = 0) it means there are no ciphers enabled on this SSL. |
| 990 | // This will almost certainly result in the socket failing to complete the |
| 991 | // handshake at which point the appropriate error is bubbled up to the client. |
| 992 | LOG_IF(WARNING, rv != 1) << "SSL_set_cipher_list('" << command << "') " |
| 993 | "returned " << rv; |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 994 | |
| 995 | // TLS channel ids. |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 996 | if (IsChannelIDEnabled(ssl_config_, channel_id_service_)) { |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 997 | SSL_enable_tls_channel_id(ssl_); |
| 998 | } |
| 999 | |
bnc | 1f29537 | 2015-10-21 23:24:22 | [diff] [blame] | 1000 | if (!ssl_config_.alpn_protos.empty()) { |
bnc | 1e75750 | 2014-12-13 02:20:16 | [diff] [blame] | 1001 | // Get list of ciphers that are enabled. |
| 1002 | STACK_OF(SSL_CIPHER)* enabled_ciphers = SSL_get_ciphers(ssl_); |
| 1003 | DCHECK(enabled_ciphers); |
| 1004 | std::vector<uint16> enabled_ciphers_vector; |
| 1005 | for (size_t i = 0; i < sk_SSL_CIPHER_num(enabled_ciphers); ++i) { |
| 1006 | const SSL_CIPHER* cipher = sk_SSL_CIPHER_value(enabled_ciphers, i); |
| 1007 | const uint16 id = static_cast<uint16>(SSL_CIPHER_get_id(cipher)); |
| 1008 | enabled_ciphers_vector.push_back(id); |
| 1009 | } |
| 1010 | |
bnc | 1f29537 | 2015-10-21 23:24:22 | [diff] [blame] | 1011 | NextProtoVector alpn_protos = ssl_config_.alpn_protos; |
bnc | f76254d62 | 2015-09-29 00:03:44 | [diff] [blame] | 1012 | if (!HasCipherAdequateForHTTP2(enabled_ciphers_vector) || |
| 1013 | !IsTLSVersionAdequateForHTTP2(ssl_config_)) { |
bnc | 1f29537 | 2015-10-21 23:24:22 | [diff] [blame] | 1014 | DisableHTTP2(&alpn_protos); |
bnc | f76254d62 | 2015-09-29 00:03:44 | [diff] [blame] | 1015 | } |
bnc | 1f29537 | 2015-10-21 23:24:22 | [diff] [blame] | 1016 | std::vector<uint8_t> wire_protos = SerializeNextProtos(alpn_protos); |
[email protected] | abc44b75 | 2014-07-30 03:52:15 | [diff] [blame] | 1017 | SSL_set_alpn_protos(ssl_, wire_protos.empty() ? NULL : &wire_protos[0], |
| 1018 | wire_protos.size()); |
| 1019 | } |
| 1020 | |
bnc | 2d6bd0f | 2015-10-28 01:52:05 | [diff] [blame] | 1021 | if (ssl_config_.npn_protos.empty()) |
| 1022 | SSL_set_options(ssl_, SSL_OP_DISABLE_NPN); |
| 1023 | |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 1024 | if (ssl_config_.signed_cert_timestamps_enabled) { |
| 1025 | SSL_enable_signed_cert_timestamps(ssl_); |
| 1026 | SSL_enable_ocsp_stapling(ssl_); |
| 1027 | } |
| 1028 | |
davidben | 15f5713 | 2015-04-27 18:08:36 | [diff] [blame] | 1029 | if (cert_verifier_->SupportsOCSPStapling()) |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 1030 | SSL_enable_ocsp_stapling(ssl_); |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 1031 | |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 1032 | return OK; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 1033 | } |
| 1034 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1035 | void SSLClientSocketOpenSSL::DoReadCallback(int rv) { |
| 1036 | // Since Run may result in Read being called, clear |user_read_callback_| |
| 1037 | // up front. |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 1038 | if (rv > 0) |
| 1039 | was_ever_used_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1040 | user_read_buf_ = NULL; |
| 1041 | user_read_buf_len_ = 0; |
| 1042 | base::ResetAndReturn(&user_read_callback_).Run(rv); |
| 1043 | } |
| 1044 | |
| 1045 | void SSLClientSocketOpenSSL::DoWriteCallback(int rv) { |
| 1046 | // Since Run may result in Write being called, clear |user_write_callback_| |
| 1047 | // up front. |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 1048 | if (rv > 0) |
| 1049 | was_ever_used_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1050 | user_write_buf_ = NULL; |
| 1051 | user_write_buf_len_ = 0; |
| 1052 | base::ResetAndReturn(&user_write_callback_).Run(rv); |
| 1053 | } |
| 1054 | |
| 1055 | bool SSLClientSocketOpenSSL::DoTransportIO() { |
| 1056 | bool network_moved = false; |
| 1057 | int rv; |
| 1058 | // Read and write as much data as possible. The loop is necessary because |
| 1059 | // Write() may return synchronously. |
| 1060 | do { |
| 1061 | rv = BufferSend(); |
| 1062 | if (rv != ERR_IO_PENDING && rv != 0) |
| 1063 | network_moved = true; |
| 1064 | } while (rv > 0); |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 1065 | if (transport_read_error_ == OK && BufferRecv() != ERR_IO_PENDING) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1066 | network_moved = true; |
| 1067 | return network_moved; |
| 1068 | } |
| 1069 | |
pkasting | 379234c | 2015-04-08 04:42:12 | [diff] [blame] | 1070 | // TODO(cbentzel): Remove including "base/threading/thread_local.h" and |
vadimt | 6b43dec2 | 2015-01-06 01:59:58 | [diff] [blame] | 1071 | // g_first_run_completed once crbug.com/424386 is fixed. |
| 1072 | base::LazyInstance<base::ThreadLocalBoolean>::Leaky g_first_run_completed = |
| 1073 | LAZY_INSTANCE_INITIALIZER; |
| 1074 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1075 | int SSLClientSocketOpenSSL::DoHandshake() { |
| 1076 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
vadimt | 5a24328 | 2014-12-24 00:26:16 | [diff] [blame] | 1077 | |
| 1078 | int rv; |
| 1079 | |
pkasting | 379234c | 2015-04-08 04:42:12 | [diff] [blame] | 1080 | // TODO(cbentzel): Leave only 1 call to SSL_do_handshake once crbug.com/424386 |
vadimt | 5a24328 | 2014-12-24 00:26:16 | [diff] [blame] | 1081 | // is fixed. |
| 1082 | if (ssl_config_.send_client_cert && ssl_config_.client_cert.get()) { |
vadimt | 5a24328 | 2014-12-24 00:26:16 | [diff] [blame] | 1083 | rv = SSL_do_handshake(ssl_); |
| 1084 | } else { |
vadimt | 6b43dec2 | 2015-01-06 01:59:58 | [diff] [blame] | 1085 | if (g_first_run_completed.Get().Get()) { |
pkasting | 379234c | 2015-04-08 04:42:12 | [diff] [blame] | 1086 | // TODO(cbentzel): Remove ScopedTracker below once crbug.com/424386 is |
vadimt | 6b43dec2 | 2015-01-06 01:59:58 | [diff] [blame] | 1087 | // fixed. |
pkasting | 379234c | 2015-04-08 04:42:12 | [diff] [blame] | 1088 | tracked_objects::ScopedTracker tracking_profile( |
| 1089 | FROM_HERE_WITH_EXPLICIT_FUNCTION("424386 SSL_do_handshake()")); |
vadimt | 5a24328 | 2014-12-24 00:26:16 | [diff] [blame] | 1090 | |
vadimt | 6b43dec2 | 2015-01-06 01:59:58 | [diff] [blame] | 1091 | rv = SSL_do_handshake(ssl_); |
| 1092 | } else { |
| 1093 | g_first_run_completed.Get().Set(true); |
vadimt | 6b43dec2 | 2015-01-06 01:59:58 | [diff] [blame] | 1094 | rv = SSL_do_handshake(ssl_); |
| 1095 | } |
vadimt | 5a24328 | 2014-12-24 00:26:16 | [diff] [blame] | 1096 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1097 | |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1098 | int net_error = OK; |
| 1099 | if (rv <= 0) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1100 | int ssl_error = SSL_get_error(ssl_, rv); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1101 | if (ssl_error == SSL_ERROR_WANT_CHANNEL_ID_LOOKUP) { |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1102 | // The server supports channel ID. Stop to look one up before returning to |
| 1103 | // the handshake. |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1104 | GotoState(STATE_CHANNEL_ID_LOOKUP); |
| 1105 | return OK; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1106 | } |
davidben | ced4aa9b | 2015-05-12 21:22:35 | [diff] [blame] | 1107 | if (ssl_error == SSL_ERROR_WANT_X509_LOOKUP && |
| 1108 | !ssl_config_.send_client_cert) { |
| 1109 | return ERR_SSL_CLIENT_AUTH_CERT_NEEDED; |
| 1110 | } |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1111 | if (ssl_error == SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) { |
| 1112 | DCHECK(private_key_); |
| 1113 | DCHECK_NE(kNoPendingResult, signature_result_); |
| 1114 | GotoState(STATE_HANDSHAKE); |
| 1115 | return ERR_IO_PENDING; |
| 1116 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1117 | |
davidben | a4409c6 | 2014-08-27 17:05:51 | [diff] [blame] | 1118 | OpenSSLErrorInfo error_info; |
| 1119 | net_error = MapOpenSSLErrorWithDetails(ssl_error, err_tracer, &error_info); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1120 | if (net_error == ERR_IO_PENDING) { |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1121 | // If not done, stay in this state |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1122 | GotoState(STATE_HANDSHAKE); |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1123 | return ERR_IO_PENDING; |
| 1124 | } |
| 1125 | |
| 1126 | LOG(ERROR) << "handshake failed; returned " << rv << ", SSL error code " |
| 1127 | << ssl_error << ", net_error " << net_error; |
| 1128 | net_log_.AddEvent( |
| 1129 | NetLog::TYPE_SSL_HANDSHAKE_ERROR, |
| 1130 | CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info)); |
davidben | f2eaaf9 | 2015-05-15 22:18:42 | [diff] [blame] | 1131 | |
| 1132 | // Classify the handshake failure. This is used to determine causes of the |
| 1133 | // TLS version fallback. |
| 1134 | |
| 1135 | // |cipher| is the current outgoing cipher suite, so it is non-null iff |
| 1136 | // ChangeCipherSpec was sent. |
| 1137 | const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_); |
| 1138 | if (SSL_get_state(ssl_) == SSL3_ST_CR_SRVR_HELLO_A) { |
| 1139 | ssl_failure_state_ = SSL_FAILURE_CLIENT_HELLO; |
| 1140 | } else if (cipher && (SSL_CIPHER_get_id(cipher) == |
| 1141 | TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256 || |
| 1142 | SSL_CIPHER_get_id(cipher) == |
| 1143 | TLS1_CK_RSA_WITH_AES_128_GCM_SHA256)) { |
| 1144 | ssl_failure_state_ = SSL_FAILURE_BUGGY_GCM; |
| 1145 | } else if (cipher && ssl_config_.send_client_cert) { |
| 1146 | ssl_failure_state_ = SSL_FAILURE_CLIENT_AUTH; |
| 1147 | } else if (ERR_GET_LIB(error_info.error_code) == ERR_LIB_SSL && |
| 1148 | ERR_GET_REASON(error_info.error_code) == |
| 1149 | SSL_R_OLD_SESSION_VERSION_NOT_RETURNED) { |
| 1150 | ssl_failure_state_ = SSL_FAILURE_SESSION_MISMATCH; |
| 1151 | } else if (cipher && npn_status_ != kNextProtoUnsupported) { |
| 1152 | ssl_failure_state_ = SSL_FAILURE_NEXT_PROTO; |
| 1153 | } else { |
| 1154 | ssl_failure_state_ = SSL_FAILURE_UNKNOWN; |
| 1155 | } |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | GotoState(STATE_HANDSHAKE_COMPLETE); |
| 1159 | return net_error; |
| 1160 | } |
| 1161 | |
| 1162 | int SSLClientSocketOpenSSL::DoHandshakeComplete(int result) { |
| 1163 | if (result < 0) |
| 1164 | return result; |
| 1165 | |
| 1166 | if (ssl_config_.version_fallback && |
| 1167 | ssl_config_.version_max < ssl_config_.version_fallback_min) { |
| 1168 | return ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION; |
| 1169 | } |
| 1170 | |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame^] | 1171 | // Check that if token binding was negotiated, then extended master secret |
| 1172 | // must also be negotiated. |
| 1173 | if (tb_was_negotiated_ && !SSL_get_extms_support(ssl_)) |
| 1174 | return ERR_SSL_PROTOCOL_ERROR; |
| 1175 | |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1176 | // SSL handshake is completed. If NPN wasn't negotiated, see if ALPN was. |
| 1177 | if (npn_status_ == kNextProtoUnsupported) { |
| 1178 | const uint8_t* alpn_proto = NULL; |
| 1179 | unsigned alpn_len = 0; |
| 1180 | SSL_get0_alpn_selected(ssl_, &alpn_proto, &alpn_len); |
| 1181 | if (alpn_len > 0) { |
| 1182 | npn_proto_.assign(reinterpret_cast<const char*>(alpn_proto), alpn_len); |
| 1183 | npn_status_ = kNextProtoNegotiated; |
| 1184 | set_negotiation_extension(kExtensionALPN); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1185 | } |
| 1186 | } |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1187 | |
| 1188 | RecordNegotiationExtension(); |
| 1189 | RecordChannelIDSupport(channel_id_service_, channel_id_sent_, |
davidben | 24bb5a4 | 2015-10-15 22:13:22 | [diff] [blame] | 1190 | ssl_config_.channel_id_enabled); |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1191 | |
| 1192 | // Only record OCSP histograms if OCSP was requested. |
| 1193 | if (ssl_config_.signed_cert_timestamps_enabled || |
| 1194 | cert_verifier_->SupportsOCSPStapling()) { |
| 1195 | const uint8_t* ocsp_response; |
| 1196 | size_t ocsp_response_len; |
| 1197 | SSL_get0_ocsp_response(ssl_, &ocsp_response, &ocsp_response_len); |
| 1198 | |
| 1199 | set_stapled_ocsp_response_received(ocsp_response_len != 0); |
| 1200 | UMA_HISTOGRAM_BOOLEAN("Net.OCSPResponseStapled", ocsp_response_len != 0); |
| 1201 | } |
| 1202 | |
| 1203 | const uint8_t* sct_list; |
| 1204 | size_t sct_list_len; |
| 1205 | SSL_get0_signed_cert_timestamp_list(ssl_, &sct_list, &sct_list_len); |
| 1206 | set_signed_cert_timestamps_received(sct_list_len != 0); |
| 1207 | |
| 1208 | if (IsRenegotiationAllowed()) |
davidben | 0635886ab | 2015-11-04 19:24:04 | [diff] [blame] | 1209 | SSL_set_renegotiate_mode(ssl_, ssl_renegotiate_freely); |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1210 | |
| 1211 | // Verify the certificate. |
| 1212 | UpdateServerCert(); |
| 1213 | GotoState(STATE_VERIFY_CERT); |
| 1214 | return OK; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1215 | } |
| 1216 | |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1217 | int SSLClientSocketOpenSSL::DoChannelIDLookup() { |
rch | 98cf447 | 2015-02-13 00:14:14 | [diff] [blame] | 1218 | net_log_.AddEvent(NetLog::TYPE_SSL_CHANNEL_ID_REQUESTED); |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1219 | GotoState(STATE_CHANNEL_ID_LOOKUP_COMPLETE); |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 1220 | return channel_id_service_->GetOrCreateChannelID( |
nharper | 2e171cf | 2015-06-01 20:29:23 | [diff] [blame] | 1221 | host_and_port_.host(), &channel_id_key_, |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1222 | base::Bind(&SSLClientSocketOpenSSL::OnHandshakeIOComplete, |
| 1223 | base::Unretained(this)), |
nharper | 75ade89 | 2015-06-10 19:05:35 | [diff] [blame] | 1224 | &channel_id_request_); |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | int SSLClientSocketOpenSSL::DoChannelIDLookupComplete(int result) { |
| 1228 | if (result < 0) |
| 1229 | return result; |
| 1230 | |
nharper | 2e171cf | 2015-06-01 20:29:23 | [diff] [blame] | 1231 | if (!channel_id_key_) { |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1232 | LOG(ERROR) << "Failed to import Channel ID."; |
| 1233 | return ERR_CHANNEL_ID_IMPORT_FAILED; |
| 1234 | } |
| 1235 | |
| 1236 | // Hand the key to OpenSSL. Check for error in case OpenSSL rejects the key |
| 1237 | // type. |
| 1238 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
nharper | 2e171cf | 2015-06-01 20:29:23 | [diff] [blame] | 1239 | int rv = SSL_set1_tls_channel_id(ssl_, channel_id_key_->key()); |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1240 | if (!rv) { |
| 1241 | LOG(ERROR) << "Failed to set Channel ID."; |
| 1242 | int err = SSL_get_error(ssl_, rv); |
| 1243 | return MapOpenSSLError(err, err_tracer); |
| 1244 | } |
| 1245 | |
| 1246 | // Return to the handshake. |
davidben | 52053b38 | 2015-04-27 19:22:29 | [diff] [blame] | 1247 | channel_id_sent_ = true; |
rch | 98cf447 | 2015-02-13 00:14:14 | [diff] [blame] | 1248 | net_log_.AddEvent(NetLog::TYPE_SSL_CHANNEL_ID_PROVIDED); |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1249 | GotoState(STATE_HANDSHAKE); |
| 1250 | return OK; |
| 1251 | } |
| 1252 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1253 | int SSLClientSocketOpenSSL::DoVerifyCert(int result) { |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1254 | DCHECK(!server_cert_chain_->empty()); |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 1255 | DCHECK(start_cert_verification_time_.is_null()); |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1256 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1257 | GotoState(STATE_VERIFY_CERT_COMPLETE); |
| 1258 | |
davidben | c6435a7 | 2015-08-17 18:28:52 | [diff] [blame] | 1259 | // OpenSSL decoded the certificate, but the platform certificate |
| 1260 | // implementation could not. This is treated as a fatal SSL-level protocol |
| 1261 | // error rather than a certificate error. See https://crbug.com/91341. |
| 1262 | if (!server_cert_.get()) |
| 1263 | return ERR_SSL_SERVER_CERT_BAD_FORMAT; |
| 1264 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1265 | // If the certificate is bad and has been previously accepted, use |
| 1266 | // the previous status and bypass the error. |
| 1267 | base::StringPiece der_cert; |
| 1268 | if (!x509_util::GetDER(server_cert_chain_->Get(0), &der_cert)) { |
| 1269 | NOTREACHED(); |
| 1270 | return ERR_CERT_INVALID; |
| 1271 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1272 | CertStatus cert_status; |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1273 | if (ssl_config_.IsAllowedBadCert(der_cert, &cert_status)) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1274 | VLOG(1) << "Received an expected bad cert with status: " << cert_status; |
| 1275 | server_cert_verify_result_.Reset(); |
| 1276 | server_cert_verify_result_.cert_status = cert_status; |
| 1277 | server_cert_verify_result_.verified_cert = server_cert_; |
| 1278 | return OK; |
| 1279 | } |
| 1280 | |
davidben | 15f5713 | 2015-04-27 18:08:36 | [diff] [blame] | 1281 | std::string ocsp_response; |
| 1282 | if (cert_verifier_->SupportsOCSPStapling()) { |
| 1283 | const uint8_t* ocsp_response_raw; |
| 1284 | size_t ocsp_response_len; |
| 1285 | SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); |
| 1286 | ocsp_response.assign(reinterpret_cast<const char*>(ocsp_response_raw), |
| 1287 | ocsp_response_len); |
| 1288 | } |
| 1289 | |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 1290 | start_cert_verification_time_ = base::TimeTicks::Now(); |
| 1291 | |
eroman | 7f9236a | 2015-05-11 21:23:43 | [diff] [blame] | 1292 | return cert_verifier_->Verify( |
rtenneti | 807f9ea | 2015-06-24 16:26:31 | [diff] [blame] | 1293 | server_cert_.get(), host_and_port_.host(), ocsp_response, |
| 1294 | ssl_config_.GetCertVerifyFlags(), |
[email protected] | 591cffcd | 2014-08-18 20:02:30 | [diff] [blame] | 1295 | // TODO(davidben): Route the CRLSet through SSLConfig so |
| 1296 | // SSLClientSocket doesn't depend on SSLConfigService. |
davidben | 15f5713 | 2015-04-27 18:08:36 | [diff] [blame] | 1297 | SSLConfigService::GetCRLSet().get(), &server_cert_verify_result_, |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1298 | base::Bind(&SSLClientSocketOpenSSL::OnHandshakeIOComplete, |
| 1299 | base::Unretained(this)), |
eroman | 7f9236a | 2015-05-11 21:23:43 | [diff] [blame] | 1300 | &cert_verifier_request_, net_log_); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | int SSLClientSocketOpenSSL::DoVerifyCertComplete(int result) { |
eroman | 7f9236a | 2015-05-11 21:23:43 | [diff] [blame] | 1304 | cert_verifier_request_.reset(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1305 | |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 1306 | if (!start_cert_verification_time_.is_null()) { |
| 1307 | base::TimeDelta verify_time = |
| 1308 | base::TimeTicks::Now() - start_cert_verification_time_; |
| 1309 | if (result == OK) { |
| 1310 | UMA_HISTOGRAM_TIMES("Net.SSLCertVerificationTime", verify_time); |
| 1311 | } else { |
| 1312 | UMA_HISTOGRAM_TIMES("Net.SSLCertVerificationTimeError", verify_time); |
| 1313 | } |
| 1314 | } |
| 1315 | |
davidben | 85574eb | 2014-12-12 03:01:57 | [diff] [blame] | 1316 | if (result == OK) { |
davidben | 85574eb | 2014-12-12 03:01:57 | [diff] [blame] | 1317 | if (SSL_session_reused(ssl_)) { |
| 1318 | // Record whether or not the server tried to resume a session for a |
| 1319 | // different version. See https://crbug.com/441456. |
| 1320 | UMA_HISTOGRAM_BOOLEAN( |
| 1321 | "Net.SSLSessionVersionMatch", |
| 1322 | SSL_version(ssl_) == SSL_get_session(ssl_)->ssl_version); |
| 1323 | } |
| 1324 | } |
| 1325 | |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 1326 | const CertStatus cert_status = server_cert_verify_result_.cert_status; |
| 1327 | if (transport_security_state_ && |
| 1328 | (result == OK || |
| 1329 | (IsCertificateError(result) && IsCertStatusMinorError(cert_status))) && |
| 1330 | !transport_security_state_->CheckPublicKeyPins( |
estark | 1a66df7 | 2015-07-28 15:24:00 | [diff] [blame] | 1331 | host_and_port_, server_cert_verify_result_.is_issued_by_known_root, |
| 1332 | server_cert_verify_result_.public_key_hashes, server_cert_.get(), |
| 1333 | server_cert_verify_result_.verified_cert.get(), |
| 1334 | TransportSecurityState::ENABLE_PIN_REPORTS, &pinning_failure_log_)) { |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 1335 | result = ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN; |
| 1336 | } |
| 1337 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1338 | if (result == OK) { |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 1339 | // Only check Certificate Transparency if there were no other errors with |
| 1340 | // the connection. |
| 1341 | VerifyCT(); |
| 1342 | |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 1343 | DCHECK(!certificate_verified_); |
| 1344 | certificate_verified_ = true; |
| 1345 | MaybeCacheSession(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1346 | } else { |
| 1347 | DVLOG(1) << "DoVerifyCertComplete error " << ErrorToString(result) |
| 1348 | << " (" << result << ")"; |
| 1349 | } |
| 1350 | |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 1351 | completed_connect_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1352 | // Exit DoHandshakeLoop and return the result to the caller to Connect. |
| 1353 | DCHECK_EQ(STATE_NONE, next_handshake_state_); |
| 1354 | return result; |
| 1355 | } |
| 1356 | |
| 1357 | void SSLClientSocketOpenSSL::DoConnectCallback(int rv) { |
| 1358 | if (!user_connect_callback_.is_null()) { |
| 1359 | CompletionCallback c = user_connect_callback_; |
| 1360 | user_connect_callback_.Reset(); |
| 1361 | c.Run(rv > OK ? OK : rv); |
| 1362 | } |
| 1363 | } |
| 1364 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1365 | void SSLClientSocketOpenSSL::UpdateServerCert() { |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 1366 | server_cert_chain_->Reset(SSL_get_peer_cert_chain(ssl_)); |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 1367 | server_cert_ = server_cert_chain_->AsOSChain(); |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1368 | if (server_cert_.get()) { |
| 1369 | net_log_.AddEvent( |
| 1370 | NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, |
| 1371 | base::Bind(&NetLogX509CertificateCallback, |
| 1372 | base::Unretained(server_cert_.get()))); |
| 1373 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1374 | } |
| 1375 | |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 1376 | void SSLClientSocketOpenSSL::VerifyCT() { |
| 1377 | if (!cert_transparency_verifier_) |
| 1378 | return; |
| 1379 | |
davidben | 54015aa | 2014-12-02 22:16:23 | [diff] [blame] | 1380 | const uint8_t* ocsp_response_raw; |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 1381 | size_t ocsp_response_len; |
| 1382 | SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); |
| 1383 | std::string ocsp_response; |
| 1384 | if (ocsp_response_len > 0) { |
| 1385 | ocsp_response.assign(reinterpret_cast<const char*>(ocsp_response_raw), |
| 1386 | ocsp_response_len); |
| 1387 | } |
| 1388 | |
davidben | 54015aa | 2014-12-02 22:16:23 | [diff] [blame] | 1389 | const uint8_t* sct_list_raw; |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 1390 | size_t sct_list_len; |
| 1391 | SSL_get0_signed_cert_timestamp_list(ssl_, &sct_list_raw, &sct_list_len); |
| 1392 | std::string sct_list; |
| 1393 | if (sct_list_len > 0) |
| 1394 | sct_list.assign(reinterpret_cast<const char*>(sct_list_raw), sct_list_len); |
| 1395 | |
| 1396 | // Note that this is a completely synchronous operation: The CT Log Verifier |
| 1397 | // gets all the data it needs for SCT verification and does not do any |
| 1398 | // external communication. |
eranm | 6571b2b | 2014-12-03 15:53:23 | [diff] [blame] | 1399 | cert_transparency_verifier_->Verify( |
| 1400 | server_cert_verify_result_.verified_cert.get(), ocsp_response, sct_list, |
| 1401 | &ct_verify_result_, net_log_); |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 1402 | |
haavardm | 2553ff05 | 2015-06-29 09:27:47 | [diff] [blame] | 1403 | if (policy_enforcer_ && |
| 1404 | (server_cert_verify_result_.cert_status & CERT_STATUS_IS_EV)) { |
| 1405 | scoped_refptr<ct::EVCertsWhitelist> ev_whitelist = |
| 1406 | SSLConfigService::GetEVCertsWhitelist(); |
| 1407 | if (!policy_enforcer_->DoesConformToCTEVPolicy( |
| 1408 | server_cert_verify_result_.verified_cert.get(), ev_whitelist.get(), |
| 1409 | ct_verify_result_, net_log_)) { |
| 1410 | // TODO(eranm): Log via the BoundNetLog, see crbug.com/437766 |
| 1411 | VLOG(1) << "EV certificate for " |
| 1412 | << server_cert_verify_result_.verified_cert->subject() |
| 1413 | .GetDisplayName() |
| 1414 | << " does not conform to CT policy, removing EV status."; |
haavardm | 533c27b | 2015-08-13 09:27:29 | [diff] [blame] | 1415 | server_cert_verify_result_.cert_status |= |
| 1416 | CERT_STATUS_CT_COMPLIANCE_FAILED; |
haavardm | 2553ff05 | 2015-06-29 09:27:47 | [diff] [blame] | 1417 | server_cert_verify_result_.cert_status &= ~CERT_STATUS_IS_EV; |
eranm | 6571b2b | 2014-12-03 15:53:23 | [diff] [blame] | 1418 | } |
| 1419 | } |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 1420 | } |
| 1421 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1422 | void SSLClientSocketOpenSSL::OnHandshakeIOComplete(int result) { |
| 1423 | int rv = DoHandshakeLoop(result); |
| 1424 | if (rv != ERR_IO_PENDING) { |
| 1425 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); |
| 1426 | DoConnectCallback(rv); |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | void SSLClientSocketOpenSSL::OnSendComplete(int result) { |
| 1431 | if (next_handshake_state_ == STATE_HANDSHAKE) { |
| 1432 | // In handshake phase. |
| 1433 | OnHandshakeIOComplete(result); |
| 1434 | return; |
| 1435 | } |
| 1436 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1437 | // During a renegotiation, a Read call may also be blocked on a transport |
| 1438 | // write, so retry both operations. |
| 1439 | PumpReadWriteEvents(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | void SSLClientSocketOpenSSL::OnRecvComplete(int result) { |
| 1443 | if (next_handshake_state_ == STATE_HANDSHAKE) { |
| 1444 | // In handshake phase. |
| 1445 | OnHandshakeIOComplete(result); |
| 1446 | return; |
| 1447 | } |
| 1448 | |
| 1449 | // Network layer received some data, check if client requested to read |
| 1450 | // decrypted data. |
| 1451 | if (!user_read_buf_.get()) |
| 1452 | return; |
| 1453 | |
davidben | 1b133ad | 2014-10-23 04:23:13 | [diff] [blame] | 1454 | int rv = DoReadLoop(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1455 | if (rv != ERR_IO_PENDING) |
| 1456 | DoReadCallback(rv); |
| 1457 | } |
| 1458 | |
| 1459 | int SSLClientSocketOpenSSL::DoHandshakeLoop(int last_io_result) { |
| 1460 | int rv = last_io_result; |
| 1461 | do { |
| 1462 | // Default to STATE_NONE for next state. |
| 1463 | // (This is a quirk carried over from the windows |
| 1464 | // implementation. It makes reading the logs a bit harder.) |
| 1465 | // State handlers can and often do call GotoState just |
| 1466 | // to stay in the current state. |
| 1467 | State state = next_handshake_state_; |
| 1468 | GotoState(STATE_NONE); |
| 1469 | switch (state) { |
| 1470 | case STATE_HANDSHAKE: |
| 1471 | rv = DoHandshake(); |
| 1472 | break; |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1473 | case STATE_HANDSHAKE_COMPLETE: |
| 1474 | rv = DoHandshakeComplete(rv); |
| 1475 | break; |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1476 | case STATE_CHANNEL_ID_LOOKUP: |
| 1477 | DCHECK_EQ(OK, rv); |
| 1478 | rv = DoChannelIDLookup(); |
| 1479 | break; |
| 1480 | case STATE_CHANNEL_ID_LOOKUP_COMPLETE: |
| 1481 | rv = DoChannelIDLookupComplete(rv); |
| 1482 | break; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1483 | case STATE_VERIFY_CERT: |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1484 | DCHECK_EQ(OK, rv); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1485 | rv = DoVerifyCert(rv); |
| 1486 | break; |
| 1487 | case STATE_VERIFY_CERT_COMPLETE: |
| 1488 | rv = DoVerifyCertComplete(rv); |
| 1489 | break; |
| 1490 | case STATE_NONE: |
| 1491 | default: |
| 1492 | rv = ERR_UNEXPECTED; |
| 1493 | NOTREACHED() << "unexpected state" << state; |
| 1494 | break; |
| 1495 | } |
| 1496 | |
| 1497 | bool network_moved = DoTransportIO(); |
| 1498 | if (network_moved && next_handshake_state_ == STATE_HANDSHAKE) { |
| 1499 | // In general we exit the loop if rv is ERR_IO_PENDING. In this |
| 1500 | // special case we keep looping even if rv is ERR_IO_PENDING because |
| 1501 | // the transport IO may allow DoHandshake to make progress. |
| 1502 | rv = OK; // This causes us to stay in the loop. |
| 1503 | } |
| 1504 | } while (rv != ERR_IO_PENDING && next_handshake_state_ != STATE_NONE); |
| 1505 | return rv; |
| 1506 | } |
| 1507 | |
davidben | 1b133ad | 2014-10-23 04:23:13 | [diff] [blame] | 1508 | int SSLClientSocketOpenSSL::DoReadLoop() { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1509 | bool network_moved; |
| 1510 | int rv; |
| 1511 | do { |
| 1512 | rv = DoPayloadRead(); |
| 1513 | network_moved = DoTransportIO(); |
| 1514 | } while (rv == ERR_IO_PENDING && network_moved); |
| 1515 | |
| 1516 | return rv; |
| 1517 | } |
| 1518 | |
davidben | 1b133ad | 2014-10-23 04:23:13 | [diff] [blame] | 1519 | int SSLClientSocketOpenSSL::DoWriteLoop() { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1520 | bool network_moved; |
| 1521 | int rv; |
| 1522 | do { |
| 1523 | rv = DoPayloadWrite(); |
| 1524 | network_moved = DoTransportIO(); |
| 1525 | } while (rv == ERR_IO_PENDING && network_moved); |
| 1526 | |
| 1527 | return rv; |
| 1528 | } |
| 1529 | |
| 1530 | int SSLClientSocketOpenSSL::DoPayloadRead() { |
| 1531 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 1532 | |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1533 | DCHECK_LT(0, user_read_buf_len_); |
| 1534 | DCHECK(user_read_buf_.get()); |
| 1535 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1536 | int rv; |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1537 | if (pending_read_error_ != kNoPendingResult) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1538 | rv = pending_read_error_; |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1539 | pending_read_error_ = kNoPendingResult; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1540 | if (rv == 0) { |
| 1541 | net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, |
| 1542 | rv, user_read_buf_->data()); |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1543 | } else { |
| 1544 | net_log_.AddEvent( |
| 1545 | NetLog::TYPE_SSL_READ_ERROR, |
| 1546 | CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_, |
| 1547 | pending_read_error_info_)); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1548 | } |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1549 | pending_read_ssl_error_ = SSL_ERROR_NONE; |
| 1550 | pending_read_error_info_ = OpenSSLErrorInfo(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1551 | return rv; |
| 1552 | } |
| 1553 | |
| 1554 | int total_bytes_read = 0; |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1555 | int ssl_ret; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1556 | do { |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1557 | ssl_ret = SSL_read(ssl_, user_read_buf_->data() + total_bytes_read, |
| 1558 | user_read_buf_len_ - total_bytes_read); |
| 1559 | if (ssl_ret > 0) |
| 1560 | total_bytes_read += ssl_ret; |
| 1561 | } while (total_bytes_read < user_read_buf_len_ && ssl_ret > 0); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1562 | |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1563 | // Although only the final SSL_read call may have failed, the failure needs to |
| 1564 | // processed immediately, while the information still available in OpenSSL's |
| 1565 | // error queue. |
davidben | ced4aa9b | 2015-05-12 21:22:35 | [diff] [blame] | 1566 | if (ssl_ret <= 0) { |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1567 | // A zero return from SSL_read may mean any of: |
| 1568 | // - The underlying BIO_read returned 0. |
| 1569 | // - The peer sent a close_notify. |
| 1570 | // - Any arbitrary error. https://crbug.com/466303 |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1571 | // |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1572 | // TransportReadComplete converts the first to an ERR_CONNECTION_CLOSED |
| 1573 | // error, so it does not occur. The second and third are distinguished by |
| 1574 | // SSL_ERROR_ZERO_RETURN. |
| 1575 | pending_read_ssl_error_ = SSL_get_error(ssl_, ssl_ret); |
| 1576 | if (pending_read_ssl_error_ == SSL_ERROR_ZERO_RETURN) { |
| 1577 | pending_read_error_ = 0; |
davidben | ced4aa9b | 2015-05-12 21:22:35 | [diff] [blame] | 1578 | } else if (pending_read_ssl_error_ == SSL_ERROR_WANT_X509_LOOKUP && |
| 1579 | !ssl_config_.send_client_cert) { |
| 1580 | pending_read_error_ = ERR_SSL_CLIENT_AUTH_CERT_NEEDED; |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1581 | } else if (pending_read_ssl_error_ == |
| 1582 | SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) { |
| 1583 | DCHECK(private_key_); |
| 1584 | DCHECK_NE(kNoPendingResult, signature_result_); |
| 1585 | pending_read_error_ = ERR_IO_PENDING; |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1586 | } else { |
| 1587 | pending_read_error_ = MapOpenSSLErrorWithDetails( |
| 1588 | pending_read_ssl_error_, err_tracer, &pending_read_error_info_); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1589 | } |
| 1590 | |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1591 | // Many servers do not reliably send a close_notify alert when shutting down |
| 1592 | // a connection, and instead terminate the TCP connection. This is reported |
| 1593 | // as ERR_CONNECTION_CLOSED. Because of this, map the unclean shutdown to a |
| 1594 | // graceful EOF, instead of treating it as an error as it should be. |
| 1595 | if (pending_read_error_ == ERR_CONNECTION_CLOSED) |
| 1596 | pending_read_error_ = 0; |
| 1597 | } |
davidben | be6ce7ec | 2014-10-20 19:15:56 | [diff] [blame] | 1598 | |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1599 | if (total_bytes_read > 0) { |
| 1600 | // Return any bytes read to the caller. The error will be deferred to the |
| 1601 | // next call of DoPayloadRead. |
| 1602 | rv = total_bytes_read; |
davidben | be6ce7ec | 2014-10-20 19:15:56 | [diff] [blame] | 1603 | |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1604 | // Do not treat insufficient data as an error to return in the next call to |
| 1605 | // DoPayloadRead() - instead, let the call fall through to check SSL_read() |
| 1606 | // again. This is because DoTransportIO() may complete in between the next |
| 1607 | // call to DoPayloadRead(), and thus it is important to check SSL_read() on |
| 1608 | // subsequent invocations to see if a complete record may now be read. |
| 1609 | if (pending_read_error_ == ERR_IO_PENDING) |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1610 | pending_read_error_ = kNoPendingResult; |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1611 | } else { |
| 1612 | // No bytes were returned. Return the pending read error immediately. |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1613 | DCHECK_NE(kNoPendingResult, pending_read_error_); |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1614 | rv = pending_read_error_; |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1615 | pending_read_error_ = kNoPendingResult; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | if (rv >= 0) { |
| 1619 | net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, rv, |
| 1620 | user_read_buf_->data()); |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1621 | } else if (rv != ERR_IO_PENDING) { |
| 1622 | net_log_.AddEvent( |
| 1623 | NetLog::TYPE_SSL_READ_ERROR, |
| 1624 | CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_, |
| 1625 | pending_read_error_info_)); |
| 1626 | pending_read_ssl_error_ = SSL_ERROR_NONE; |
| 1627 | pending_read_error_info_ = OpenSSLErrorInfo(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1628 | } |
| 1629 | return rv; |
| 1630 | } |
| 1631 | |
| 1632 | int SSLClientSocketOpenSSL::DoPayloadWrite() { |
| 1633 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 1634 | int rv = SSL_write(ssl_, user_write_buf_->data(), user_write_buf_len_); |
rsleevi | f020edc | 2015-03-16 19:31:24 | [diff] [blame] | 1635 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1636 | if (rv >= 0) { |
| 1637 | net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv, |
| 1638 | user_write_buf_->data()); |
| 1639 | return rv; |
| 1640 | } |
| 1641 | |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1642 | int ssl_error = SSL_get_error(ssl_, rv); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1643 | if (ssl_error == SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) |
| 1644 | return ERR_IO_PENDING; |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1645 | OpenSSLErrorInfo error_info; |
| 1646 | int net_error = MapOpenSSLErrorWithDetails(ssl_error, err_tracer, |
| 1647 | &error_info); |
| 1648 | |
| 1649 | if (net_error != ERR_IO_PENDING) { |
| 1650 | net_log_.AddEvent( |
| 1651 | NetLog::TYPE_SSL_WRITE_ERROR, |
| 1652 | CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info)); |
| 1653 | } |
| 1654 | return net_error; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1655 | } |
| 1656 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1657 | void SSLClientSocketOpenSSL::PumpReadWriteEvents() { |
| 1658 | int rv_read = ERR_IO_PENDING; |
| 1659 | int rv_write = ERR_IO_PENDING; |
| 1660 | bool network_moved; |
| 1661 | do { |
| 1662 | if (user_read_buf_.get()) |
| 1663 | rv_read = DoPayloadRead(); |
| 1664 | if (user_write_buf_.get()) |
| 1665 | rv_write = DoPayloadWrite(); |
| 1666 | network_moved = DoTransportIO(); |
| 1667 | } while (rv_read == ERR_IO_PENDING && rv_write == ERR_IO_PENDING && |
| 1668 | (user_read_buf_.get() || user_write_buf_.get()) && network_moved); |
| 1669 | |
| 1670 | // Performing the Read callback may cause |this| to be deleted. If this |
| 1671 | // happens, the Write callback should not be invoked. Guard against this by |
| 1672 | // holding a WeakPtr to |this| and ensuring it's still valid. |
| 1673 | base::WeakPtr<SSLClientSocketOpenSSL> guard(weak_factory_.GetWeakPtr()); |
| 1674 | if (user_read_buf_.get() && rv_read != ERR_IO_PENDING) |
| 1675 | DoReadCallback(rv_read); |
| 1676 | |
| 1677 | if (!guard.get()) |
| 1678 | return; |
| 1679 | |
| 1680 | if (user_write_buf_.get() && rv_write != ERR_IO_PENDING) |
| 1681 | DoWriteCallback(rv_write); |
| 1682 | } |
| 1683 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1684 | int SSLClientSocketOpenSSL::BufferSend(void) { |
| 1685 | if (transport_send_busy_) |
| 1686 | return ERR_IO_PENDING; |
| 1687 | |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1688 | size_t buffer_read_offset; |
| 1689 | uint8_t* read_buf; |
| 1690 | size_t max_read; |
| 1691 | int status = BIO_zero_copy_get_read_buf(transport_bio_, &read_buf, |
| 1692 | &buffer_read_offset, &max_read); |
| 1693 | DCHECK_EQ(status, 1); // Should never fail. |
| 1694 | if (!max_read) |
| 1695 | return 0; // Nothing pending in the OpenSSL write BIO. |
| 1696 | CHECK_EQ(read_buf, reinterpret_cast<uint8_t*>(send_buffer_->StartOfBuffer())); |
| 1697 | CHECK_LT(buffer_read_offset, static_cast<size_t>(send_buffer_->capacity())); |
| 1698 | send_buffer_->set_offset(buffer_read_offset); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1699 | |
| 1700 | int rv = transport_->socket()->Write( |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1701 | send_buffer_.get(), max_read, |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1702 | base::Bind(&SSLClientSocketOpenSSL::BufferSendComplete, |
| 1703 | base::Unretained(this))); |
| 1704 | if (rv == ERR_IO_PENDING) { |
| 1705 | transport_send_busy_ = true; |
| 1706 | } else { |
| 1707 | TransportWriteComplete(rv); |
| 1708 | } |
| 1709 | return rv; |
| 1710 | } |
| 1711 | |
| 1712 | int SSLClientSocketOpenSSL::BufferRecv(void) { |
| 1713 | if (transport_recv_busy_) |
| 1714 | return ERR_IO_PENDING; |
| 1715 | |
| 1716 | // Determine how much was requested from |transport_bio_| that was not |
| 1717 | // actually available. |
| 1718 | size_t requested = BIO_ctrl_get_read_request(transport_bio_); |
| 1719 | if (requested == 0) { |
| 1720 | // This is not a perfect match of error codes, as no operation is |
| 1721 | // actually pending. However, returning 0 would be interpreted as |
| 1722 | // a possible sign of EOF, which is also an inappropriate match. |
| 1723 | return ERR_IO_PENDING; |
| 1724 | } |
| 1725 | |
| 1726 | // Known Issue: While only reading |requested| data is the more correct |
| 1727 | // implementation, it has the downside of resulting in frequent reads: |
| 1728 | // One read for the SSL record header (~5 bytes) and one read for the SSL |
| 1729 | // record body. Rather than issuing these reads to the underlying socket |
| 1730 | // (and constantly allocating new IOBuffers), a single Read() request to |
| 1731 | // fill |transport_bio_| is issued. As long as an SSL client socket cannot |
| 1732 | // be gracefully shutdown (via SSL close alerts) and re-used for non-SSL |
| 1733 | // traffic, this over-subscribed Read()ing will not cause issues. |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1734 | |
| 1735 | size_t buffer_write_offset; |
| 1736 | uint8_t* write_buf; |
| 1737 | size_t max_write; |
| 1738 | int status = BIO_zero_copy_get_write_buf(transport_bio_, &write_buf, |
| 1739 | &buffer_write_offset, &max_write); |
| 1740 | DCHECK_EQ(status, 1); // Should never fail. |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1741 | if (!max_write) |
| 1742 | return ERR_IO_PENDING; |
| 1743 | |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1744 | CHECK_EQ(write_buf, |
| 1745 | reinterpret_cast<uint8_t*>(recv_buffer_->StartOfBuffer())); |
| 1746 | CHECK_LT(buffer_write_offset, static_cast<size_t>(recv_buffer_->capacity())); |
| 1747 | |
| 1748 | recv_buffer_->set_offset(buffer_write_offset); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1749 | int rv = transport_->socket()->Read( |
| 1750 | recv_buffer_.get(), |
| 1751 | max_write, |
| 1752 | base::Bind(&SSLClientSocketOpenSSL::BufferRecvComplete, |
| 1753 | base::Unretained(this))); |
| 1754 | if (rv == ERR_IO_PENDING) { |
| 1755 | transport_recv_busy_ = true; |
| 1756 | } else { |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1757 | rv = TransportReadComplete(rv); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1758 | } |
| 1759 | return rv; |
| 1760 | } |
| 1761 | |
| 1762 | void SSLClientSocketOpenSSL::BufferSendComplete(int result) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1763 | TransportWriteComplete(result); |
| 1764 | OnSendComplete(result); |
| 1765 | } |
| 1766 | |
| 1767 | void SSLClientSocketOpenSSL::BufferRecvComplete(int result) { |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1768 | result = TransportReadComplete(result); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1769 | OnRecvComplete(result); |
| 1770 | } |
| 1771 | |
| 1772 | void SSLClientSocketOpenSSL::TransportWriteComplete(int result) { |
| 1773 | DCHECK(ERR_IO_PENDING != result); |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1774 | int bytes_written = 0; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1775 | if (result < 0) { |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 1776 | // Record the error. Save it to be reported in a future read or write on |
| 1777 | // transport_bio_'s peer. |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1778 | transport_write_error_ = result; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1779 | } else { |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1780 | bytes_written = result; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1781 | } |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1782 | DCHECK_GE(send_buffer_->RemainingCapacity(), bytes_written); |
| 1783 | int ret = BIO_zero_copy_get_read_buf_done(transport_bio_, bytes_written); |
| 1784 | DCHECK_EQ(1, ret); |
| 1785 | transport_send_busy_ = false; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1786 | } |
| 1787 | |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1788 | int SSLClientSocketOpenSSL::TransportReadComplete(int result) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1789 | DCHECK(ERR_IO_PENDING != result); |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 1790 | // If an EOF, canonicalize to ERR_CONNECTION_CLOSED here so MapOpenSSLError |
| 1791 | // does not report success. |
| 1792 | if (result == 0) |
| 1793 | result = ERR_CONNECTION_CLOSED; |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1794 | int bytes_read = 0; |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 1795 | if (result < 0) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1796 | DVLOG(1) << "TransportReadComplete result " << result; |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 1797 | // Received an error. Save it to be reported in a future read on |
| 1798 | // transport_bio_'s peer. |
| 1799 | transport_read_error_ = result; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1800 | } else { |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1801 | bytes_read = result; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1802 | } |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1803 | DCHECK_GE(recv_buffer_->RemainingCapacity(), bytes_read); |
| 1804 | int ret = BIO_zero_copy_get_write_buf_done(transport_bio_, bytes_read); |
| 1805 | DCHECK_EQ(1, ret); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1806 | transport_recv_busy_ = false; |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1807 | return result; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1808 | } |
| 1809 | |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 1810 | int SSLClientSocketOpenSSL::ClientCertRequestCallback(SSL* ssl) { |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1811 | DVLOG(3) << "OpenSSL ClientCertRequestCallback called"; |
| 1812 | DCHECK(ssl == ssl_); |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 1813 | |
davidben | af42cbe | 2014-11-13 03:27:46 | [diff] [blame] | 1814 | net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_REQUESTED); |
| 1815 | |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 1816 | // Clear any currently configured certificates. |
| 1817 | SSL_certs_clear(ssl_); |
[email protected] | 97a854f | 2014-07-29 07:51:36 | [diff] [blame] | 1818 | |
| 1819 | #if defined(OS_IOS) |
| 1820 | // TODO(droger): Support client auth on iOS. See http://crbug.com/145954). |
| 1821 | LOG(WARNING) << "Client auth is not supported"; |
| 1822 | #else // !defined(OS_IOS) |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1823 | if (!ssl_config_.send_client_cert) { |
[email protected] | 515adc2 | 2013-01-09 16:01:23 | [diff] [blame] | 1824 | // First pass: we know that a client certificate is needed, but we do not |
| 1825 | // have one at hand. |
[email protected] | 515adc2 | 2013-01-09 16:01:23 | [diff] [blame] | 1826 | STACK_OF(X509_NAME) *authorities = SSL_get_client_CA_list(ssl); |
[email protected] | edfd0f4 | 2014-07-22 18:20:37 | [diff] [blame] | 1827 | for (size_t i = 0; i < sk_X509_NAME_num(authorities); i++) { |
[email protected] | 515adc2 | 2013-01-09 16:01:23 | [diff] [blame] | 1828 | X509_NAME *ca_name = (X509_NAME *)sk_X509_NAME_value(authorities, i); |
| 1829 | unsigned char* str = NULL; |
| 1830 | int length = i2d_X509_NAME(ca_name, &str); |
| 1831 | cert_authorities_.push_back(std::string( |
| 1832 | reinterpret_cast<const char*>(str), |
| 1833 | static_cast<size_t>(length))); |
| 1834 | OPENSSL_free(str); |
| 1835 | } |
| 1836 | |
[email protected] | c078770 | 2014-05-20 21:51:44 | [diff] [blame] | 1837 | const unsigned char* client_cert_types; |
[email protected] | e7e883e | 2014-07-25 06:03:08 | [diff] [blame] | 1838 | size_t num_client_cert_types = |
| 1839 | SSL_get0_certificate_types(ssl, &client_cert_types); |
[email protected] | c078770 | 2014-05-20 21:51:44 | [diff] [blame] | 1840 | for (size_t i = 0; i < num_client_cert_types; i++) { |
| 1841 | cert_key_types_.push_back( |
| 1842 | static_cast<SSLClientCertType>(client_cert_types[i])); |
| 1843 | } |
| 1844 | |
davidben | ced4aa9b | 2015-05-12 21:22:35 | [diff] [blame] | 1845 | // Suspends handshake. SSL_get_error will return SSL_ERROR_WANT_X509_LOOKUP. |
| 1846 | return -1; |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | // Second pass: a client certificate should have been selected. |
[email protected] | 13914c9 | 2013-06-13 22:42:42 | [diff] [blame] | 1850 | if (ssl_config_.client_cert.get()) { |
[email protected] | 6bad505 | 2014-07-12 01:25:13 | [diff] [blame] | 1851 | ScopedX509 leaf_x509 = |
| 1852 | OSCertHandleToOpenSSL(ssl_config_.client_cert->os_cert_handle()); |
| 1853 | if (!leaf_x509) { |
| 1854 | LOG(WARNING) << "Failed to import certificate"; |
| 1855 | OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); |
| 1856 | return -1; |
| 1857 | } |
| 1858 | |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 1859 | ScopedX509Stack chain = OSCertHandlesToOpenSSL( |
| 1860 | ssl_config_.client_cert->GetIntermediateCertificates()); |
| 1861 | if (!chain) { |
| 1862 | LOG(WARNING) << "Failed to import intermediate certificates"; |
| 1863 | OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); |
| 1864 | return -1; |
| 1865 | } |
| 1866 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1867 | if (!SSL_use_certificate(ssl_, leaf_x509.get()) || |
| 1868 | !SSL_set1_chain(ssl_, chain.get())) { |
| 1869 | LOG(WARNING) << "Failed to set client certificate"; |
| 1870 | return -1; |
| 1871 | } |
| 1872 | |
svaldez | b92efb3 | 2015-08-28 16:38:44 | [diff] [blame] | 1873 | #if defined(OS_NACL) |
[email protected] | 6bad505 | 2014-07-12 01:25:13 | [diff] [blame] | 1874 | OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); |
| 1875 | return -1; |
svaldez | b92efb3 | 2015-08-28 16:38:44 | [diff] [blame] | 1876 | #else |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1877 | // TODO(davidben): Lift this call up to the embedder so we can actually test |
| 1878 | // this code. https://crbug.com/394131 |
| 1879 | private_key_ = FetchClientCertPrivateKey( |
| 1880 | ssl_config_.client_cert.get(), |
| 1881 | g_platform_key_task_runner.Get().task_runner()); |
| 1882 | if (!private_key_) { |
| 1883 | // Could not find the private key. Fail the handshake and surface an |
| 1884 | // appropriate error to the caller. |
| 1885 | LOG(WARNING) << "Client cert found without private key"; |
| 1886 | OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); |
| 1887 | return -1; |
| 1888 | } |
| 1889 | |
| 1890 | SSL_set_private_key_method(ssl_, &SSLContext::kPrivateKeyMethod); |
svaldez | f3db006f | 2015-09-29 16:43:58 | [diff] [blame] | 1891 | |
| 1892 | std::vector<SSLPrivateKey::Hash> digest_prefs = |
| 1893 | private_key_->GetDigestPreferences(); |
| 1894 | |
| 1895 | size_t digests_len = digest_prefs.size(); |
| 1896 | std::vector<int> digests; |
| 1897 | for (size_t i = 0; i < digests_len; i++) { |
| 1898 | switch (digest_prefs[i]) { |
| 1899 | case SSLPrivateKey::Hash::SHA1: |
| 1900 | digests.push_back(NID_sha1); |
| 1901 | break; |
| 1902 | case SSLPrivateKey::Hash::SHA256: |
| 1903 | digests.push_back(NID_sha256); |
| 1904 | break; |
| 1905 | case SSLPrivateKey::Hash::SHA384: |
| 1906 | digests.push_back(NID_sha384); |
| 1907 | break; |
| 1908 | case SSLPrivateKey::Hash::SHA512: |
| 1909 | digests.push_back(NID_sha512); |
| 1910 | break; |
| 1911 | case SSLPrivateKey::Hash::MD5_SHA1: |
| 1912 | // MD5-SHA1 is not used in TLS 1.2. |
| 1913 | break; |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | SSL_set_private_key_digest_prefs(ssl_, vector_as_array(&digests), |
| 1918 | digests.size()); |
| 1919 | #endif // !OS_NACL |
davidben | af42cbe | 2014-11-13 03:27:46 | [diff] [blame] | 1920 | |
| 1921 | int cert_count = 1 + sk_X509_num(chain.get()); |
| 1922 | net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_PROVIDED, |
tfarina | 5e24b24 | 2015-10-27 13:11:28 | [diff] [blame] | 1923 | NetLog::IntCallback("cert_count", cert_count)); |
[email protected] | 6bad505 | 2014-07-12 01:25:13 | [diff] [blame] | 1924 | return 1; |
[email protected] | c078770 | 2014-05-20 21:51:44 | [diff] [blame] | 1925 | } |
[email protected] | 97a854f | 2014-07-29 07:51:36 | [diff] [blame] | 1926 | #endif // defined(OS_IOS) |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1927 | |
| 1928 | // Send no client certificate. |
davidben | af42cbe | 2014-11-13 03:27:46 | [diff] [blame] | 1929 | net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_PROVIDED, |
tfarina | 5e24b24 | 2015-10-27 13:11:28 | [diff] [blame] | 1930 | NetLog::IntCallback("cert_count", 0)); |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 1931 | return 1; |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1932 | } |
| 1933 | |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 1934 | int SSLClientSocketOpenSSL::CertVerifyCallback(X509_STORE_CTX* store_ctx) { |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 1935 | if (!completed_connect_) { |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 1936 | // If the first handshake hasn't completed then we accept any certificates |
| 1937 | // because we verify after the handshake. |
| 1938 | return 1; |
| 1939 | } |
| 1940 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1941 | // Disallow the server certificate to change in a renegotiation. |
| 1942 | if (server_cert_chain_->empty()) { |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 1943 | LOG(ERROR) << "Received invalid certificate chain between handshakes"; |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1944 | return 0; |
| 1945 | } |
| 1946 | base::StringPiece old_der, new_der; |
| 1947 | if (store_ctx->cert == NULL || |
| 1948 | !x509_util::GetDER(server_cert_chain_->Get(0), &old_der) || |
| 1949 | !x509_util::GetDER(store_ctx->cert, &new_der)) { |
| 1950 | LOG(ERROR) << "Failed to encode certificates"; |
| 1951 | return 0; |
| 1952 | } |
| 1953 | if (old_der != new_der) { |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 1954 | LOG(ERROR) << "Server certificate changed between handshakes"; |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1955 | return 0; |
| 1956 | } |
| 1957 | |
| 1958 | return 1; |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 1959 | } |
| 1960 | |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1961 | // SelectNextProtoCallback is called by OpenSSL during the handshake. If the |
| 1962 | // server supports NPN, selects a protocol from the list that the server |
| 1963 | // provides. According to third_party/openssl/openssl/ssl/ssl_lib.c, the |
| 1964 | // callback can assume that |in| is syntactically valid. |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 1965 | int SSLClientSocketOpenSSL::SelectNextProtoCallback(unsigned char** out, |
| 1966 | unsigned char* outlen, |
| 1967 | const unsigned char* in, |
| 1968 | unsigned int inlen) { |
bnc | 1f29537 | 2015-10-21 23:24:22 | [diff] [blame] | 1969 | if (ssl_config_.npn_protos.empty()) { |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 1970 | *out = reinterpret_cast<uint8*>( |
| 1971 | const_cast<char*>(kDefaultSupportedNPNProtocol)); |
| 1972 | *outlen = arraysize(kDefaultSupportedNPNProtocol) - 1; |
| 1973 | npn_status_ = kNextProtoUnsupported; |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 1974 | return SSL_TLSEXT_ERR_OK; |
| 1975 | } |
| 1976 | |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1977 | // Assume there's no overlap between our protocols and the server's list. |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 1978 | npn_status_ = kNextProtoNoOverlap; |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1979 | |
| 1980 | // For each protocol in server preference order, see if we support it. |
| 1981 | for (unsigned int i = 0; i < inlen; i += in[i] + 1) { |
bnc | 1f29537 | 2015-10-21 23:24:22 | [diff] [blame] | 1982 | for (NextProto next_proto : ssl_config_.npn_protos) { |
bnc | 0d23cf4 | 2014-12-11 14:09:46 | [diff] [blame] | 1983 | const std::string proto = NextProtoToString(next_proto); |
| 1984 | if (in[i] == proto.size() && |
| 1985 | memcmp(&in[i + 1], proto.data(), in[i]) == 0) { |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 1986 | // We found a match. |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1987 | *out = const_cast<unsigned char*>(in) + i + 1; |
| 1988 | *outlen = in[i]; |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 1989 | npn_status_ = kNextProtoNegotiated; |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1990 | break; |
| 1991 | } |
| 1992 | } |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 1993 | if (npn_status_ == kNextProtoNegotiated) |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1994 | break; |
| 1995 | } |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 1996 | |
bnc | de27dae | 2015-10-06 07:11:02 | [diff] [blame] | 1997 | // If we didn't find a protocol, we select the last one from our list. |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 1998 | if (npn_status_ == kNextProtoNoOverlap) { |
bnc | 67da3de | 2015-01-15 21:02:26 | [diff] [blame] | 1999 | // NextProtoToString returns a pointer to a static string. |
bnc | 1f29537 | 2015-10-21 23:24:22 | [diff] [blame] | 2000 | const char* proto = NextProtoToString(ssl_config_.npn_protos.back()); |
bnc | 67da3de | 2015-01-15 21:02:26 | [diff] [blame] | 2001 | *out = reinterpret_cast<unsigned char*>(const_cast<char*>(proto)); |
| 2002 | *outlen = strlen(proto); |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 2003 | } |
| 2004 | |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 2005 | npn_proto_.assign(reinterpret_cast<const char*>(*out), *outlen); |
[email protected] | 32e1dee | 2010-12-09 18:36:24 | [diff] [blame] | 2006 | DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; |
bnc | 0d28ea5 | 2014-10-13 15:15:38 | [diff] [blame] | 2007 | set_negotiation_extension(kExtensionNPN); |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 2008 | return SSL_TLSEXT_ERR_OK; |
| 2009 | } |
| 2010 | |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 2011 | long SSLClientSocketOpenSSL::MaybeReplayTransportError( |
| 2012 | BIO *bio, |
| 2013 | int cmd, |
| 2014 | const char *argp, int argi, long argl, |
| 2015 | long retvalue) { |
| 2016 | if (cmd == (BIO_CB_READ|BIO_CB_RETURN) && retvalue <= 0) { |
| 2017 | // If there is no more data in the buffer, report any pending errors that |
| 2018 | // were observed. Note that both the readbuf and the writebuf are checked |
| 2019 | // for errors, since the application may have encountered a socket error |
| 2020 | // while writing that would otherwise not be reported until the application |
| 2021 | // attempted to write again - which it may never do. See |
| 2022 | // https://crbug.com/249848. |
| 2023 | if (transport_read_error_ != OK) { |
| 2024 | OpenSSLPutNetError(FROM_HERE, transport_read_error_); |
| 2025 | return -1; |
| 2026 | } |
| 2027 | if (transport_write_error_ != OK) { |
| 2028 | OpenSSLPutNetError(FROM_HERE, transport_write_error_); |
| 2029 | return -1; |
| 2030 | } |
| 2031 | } else if (cmd == BIO_CB_WRITE) { |
| 2032 | // Because of the write buffer, this reports a failure from the previous |
| 2033 | // write payload. If the current payload fails to write, the error will be |
| 2034 | // reported in a future write or read to |bio|. |
| 2035 | if (transport_write_error_ != OK) { |
| 2036 | OpenSSLPutNetError(FROM_HERE, transport_write_error_); |
| 2037 | return -1; |
| 2038 | } |
| 2039 | } |
| 2040 | return retvalue; |
| 2041 | } |
| 2042 | |
| 2043 | // static |
| 2044 | long SSLClientSocketOpenSSL::BIOCallback( |
| 2045 | BIO *bio, |
| 2046 | int cmd, |
| 2047 | const char *argp, int argi, long argl, |
| 2048 | long retvalue) { |
| 2049 | SSLClientSocketOpenSSL* socket = reinterpret_cast<SSLClientSocketOpenSSL*>( |
| 2050 | BIO_get_callback_arg(bio)); |
| 2051 | CHECK(socket); |
| 2052 | return socket->MaybeReplayTransportError( |
| 2053 | bio, cmd, argp, argi, argl, retvalue); |
| 2054 | } |
| 2055 | |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 2056 | void SSLClientSocketOpenSSL::MaybeCacheSession() { |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 2057 | // Only cache the session once both a new session has been established and the |
| 2058 | // certificate has been verified. Due to False Start, these events may happen |
| 2059 | // in either order. |
| 2060 | if (!session_pending_ || !certificate_verified_) |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 2061 | return; |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 2062 | |
| 2063 | SSLContext::GetInstance()->session_cache()->Insert(GetSessionCacheKey(), |
| 2064 | SSL_get_session(ssl_)); |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 2065 | session_pending_ = false; |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 2066 | } |
| 2067 | |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 2068 | int SSLClientSocketOpenSSL::NewSessionCallback(SSL_SESSION* session) { |
| 2069 | DCHECK_EQ(session, SSL_get_session(ssl_)); |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 2070 | |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 2071 | // Only sessions from the initial handshake get cached. Note this callback may |
| 2072 | // be signaled on abbreviated handshakes if the ticket was renewed. |
| 2073 | session_pending_ = true; |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 2074 | MaybeCacheSession(); |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 2075 | |
| 2076 | // OpenSSL passes a reference to |session|, but the session cache does not |
| 2077 | // take this reference, so release it. |
| 2078 | SSL_SESSION_free(session); |
| 2079 | return 1; |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 2080 | } |
| 2081 | |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 2082 | void SSLClientSocketOpenSSL::AddSCTInfoToSSLInfo(SSLInfo* ssl_info) const { |
| 2083 | for (ct::SCTList::const_iterator iter = |
| 2084 | ct_verify_result_.verified_scts.begin(); |
| 2085 | iter != ct_verify_result_.verified_scts.end(); ++iter) { |
| 2086 | ssl_info->signed_certificate_timestamps.push_back( |
| 2087 | SignedCertificateTimestampAndStatus(*iter, ct::SCT_STATUS_OK)); |
| 2088 | } |
| 2089 | for (ct::SCTList::const_iterator iter = |
| 2090 | ct_verify_result_.invalid_scts.begin(); |
| 2091 | iter != ct_verify_result_.invalid_scts.end(); ++iter) { |
| 2092 | ssl_info->signed_certificate_timestamps.push_back( |
| 2093 | SignedCertificateTimestampAndStatus(*iter, ct::SCT_STATUS_INVALID)); |
| 2094 | } |
| 2095 | for (ct::SCTList::const_iterator iter = |
| 2096 | ct_verify_result_.unknown_logs_scts.begin(); |
| 2097 | iter != ct_verify_result_.unknown_logs_scts.end(); ++iter) { |
| 2098 | ssl_info->signed_certificate_timestamps.push_back( |
| 2099 | SignedCertificateTimestampAndStatus(*iter, |
| 2100 | ct::SCT_STATUS_LOG_UNKNOWN)); |
| 2101 | } |
| 2102 | } |
| 2103 | |
rsleevi | f020edc | 2015-03-16 19:31:24 | [diff] [blame] | 2104 | std::string SSLClientSocketOpenSSL::GetSessionCacheKey() const { |
| 2105 | std::string result = host_and_port_.ToString(); |
| 2106 | result.append("/"); |
| 2107 | result.append(ssl_session_cache_shard_); |
| 2108 | |
| 2109 | // Shard the session cache based on maximum protocol version. This causes |
| 2110 | // fallback connections to use a separate session cache. |
| 2111 | result.append("/"); |
| 2112 | switch (ssl_config_.version_max) { |
rsleevi | f020edc | 2015-03-16 19:31:24 | [diff] [blame] | 2113 | case SSL_PROTOCOL_VERSION_TLS1: |
| 2114 | result.append("tls1"); |
| 2115 | break; |
| 2116 | case SSL_PROTOCOL_VERSION_TLS1_1: |
| 2117 | result.append("tls1.1"); |
| 2118 | break; |
| 2119 | case SSL_PROTOCOL_VERSION_TLS1_2: |
| 2120 | result.append("tls1.2"); |
| 2121 | break; |
| 2122 | default: |
| 2123 | NOTREACHED(); |
| 2124 | } |
| 2125 | |
davidben | a4c9d06 | 2015-04-03 22:34:25 | [diff] [blame] | 2126 | result.append("/"); |
davidben | 14b1a53 | 2015-10-30 16:01:09 | [diff] [blame] | 2127 | if (ssl_config_.deprecated_cipher_suites_enabled) |
davidben | a4c9d06 | 2015-04-03 22:34:25 | [diff] [blame] | 2128 | result.append("deprecated"); |
| 2129 | |
davidben | ee39de0 | 2015-10-16 19:53:18 | [diff] [blame] | 2130 | result.append("/"); |
| 2131 | if (ssl_config_.channel_id_enabled) |
| 2132 | result.append("channelid"); |
| 2133 | |
rsleevi | f020edc | 2015-03-16 19:31:24 | [diff] [blame] | 2134 | return result; |
| 2135 | } |
| 2136 | |
davidben | 421116c | 2015-05-12 19:56:51 | [diff] [blame] | 2137 | bool SSLClientSocketOpenSSL::IsRenegotiationAllowed() const { |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame^] | 2138 | if (tb_was_negotiated_) |
| 2139 | return false; |
| 2140 | |
davidben | 421116c | 2015-05-12 19:56:51 | [diff] [blame] | 2141 | if (npn_status_ == kNextProtoUnsupported) |
| 2142 | return ssl_config_.renego_allowed_default; |
| 2143 | |
| 2144 | NextProto next_proto = NextProtoFromString(npn_proto_); |
| 2145 | for (NextProto allowed : ssl_config_.renego_allowed_for_protos) { |
| 2146 | if (next_proto == allowed) |
| 2147 | return true; |
| 2148 | } |
| 2149 | return false; |
| 2150 | } |
| 2151 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 2152 | int SSLClientSocketOpenSSL::PrivateKeyTypeCallback() { |
| 2153 | switch (private_key_->GetType()) { |
| 2154 | case SSLPrivateKey::Type::RSA: |
| 2155 | return EVP_PKEY_RSA; |
| 2156 | case SSLPrivateKey::Type::ECDSA: |
| 2157 | return EVP_PKEY_EC; |
| 2158 | } |
| 2159 | NOTREACHED(); |
| 2160 | return EVP_PKEY_NONE; |
| 2161 | } |
| 2162 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 2163 | size_t SSLClientSocketOpenSSL::PrivateKeyMaxSignatureLenCallback() { |
| 2164 | return private_key_->GetMaxSignatureLengthInBytes(); |
| 2165 | } |
| 2166 | |
| 2167 | ssl_private_key_result_t SSLClientSocketOpenSSL::PrivateKeySignCallback( |
| 2168 | uint8_t* out, |
| 2169 | size_t* out_len, |
| 2170 | size_t max_out, |
| 2171 | const EVP_MD* md, |
| 2172 | const uint8_t* in, |
| 2173 | size_t in_len) { |
| 2174 | DCHECK_EQ(kNoPendingResult, signature_result_); |
| 2175 | DCHECK(signature_.empty()); |
| 2176 | DCHECK(private_key_); |
| 2177 | |
| 2178 | net_log_.BeginEvent(NetLog::TYPE_SSL_PRIVATE_KEY_OPERATION); |
| 2179 | |
| 2180 | SSLPrivateKey::Hash hash; |
| 2181 | if (!EVP_MDToPrivateKeyHash(md, &hash)) { |
| 2182 | OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED); |
| 2183 | return ssl_private_key_failure; |
| 2184 | } |
| 2185 | |
| 2186 | signature_result_ = ERR_IO_PENDING; |
| 2187 | private_key_->SignDigest( |
| 2188 | hash, base::StringPiece(reinterpret_cast<const char*>(in), in_len), |
| 2189 | base::Bind(&SSLClientSocketOpenSSL::OnPrivateKeySignComplete, |
| 2190 | weak_factory_.GetWeakPtr())); |
| 2191 | return ssl_private_key_retry; |
| 2192 | } |
| 2193 | |
| 2194 | ssl_private_key_result_t SSLClientSocketOpenSSL::PrivateKeySignCompleteCallback( |
| 2195 | uint8_t* out, |
| 2196 | size_t* out_len, |
| 2197 | size_t max_out) { |
| 2198 | DCHECK_NE(kNoPendingResult, signature_result_); |
| 2199 | DCHECK(private_key_); |
| 2200 | |
| 2201 | if (signature_result_ == ERR_IO_PENDING) |
| 2202 | return ssl_private_key_retry; |
| 2203 | if (signature_result_ != OK) { |
| 2204 | OpenSSLPutNetError(FROM_HERE, signature_result_); |
| 2205 | return ssl_private_key_failure; |
| 2206 | } |
| 2207 | if (signature_.size() > max_out) { |
| 2208 | OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED); |
| 2209 | return ssl_private_key_failure; |
| 2210 | } |
| 2211 | memcpy(out, vector_as_array(&signature_), signature_.size()); |
| 2212 | *out_len = signature_.size(); |
| 2213 | signature_.clear(); |
| 2214 | return ssl_private_key_success; |
| 2215 | } |
| 2216 | |
| 2217 | void SSLClientSocketOpenSSL::OnPrivateKeySignComplete( |
| 2218 | Error error, |
| 2219 | const std::vector<uint8_t>& signature) { |
| 2220 | DCHECK_EQ(ERR_IO_PENDING, signature_result_); |
| 2221 | DCHECK(signature_.empty()); |
| 2222 | DCHECK(private_key_); |
| 2223 | |
| 2224 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_PRIVATE_KEY_OPERATION, |
| 2225 | error); |
| 2226 | |
| 2227 | signature_result_ = error; |
| 2228 | if (signature_result_ == OK) |
| 2229 | signature_ = signature; |
| 2230 | |
| 2231 | if (next_handshake_state_ == STATE_HANDSHAKE) { |
| 2232 | OnHandshakeIOComplete(signature_result_); |
| 2233 | return; |
| 2234 | } |
| 2235 | |
| 2236 | // During a renegotiation, either Read or Write calls may be blocked on an |
| 2237 | // asynchronous private key operation. |
| 2238 | PumpReadWriteEvents(); |
| 2239 | } |
| 2240 | |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame^] | 2241 | int SSLClientSocketOpenSSL::TokenBindingAdd(const uint8_t** out, |
| 2242 | size_t* out_len, |
| 2243 | int* out_alert_value) { |
| 2244 | if (ssl_config_.token_binding_params.empty()) { |
| 2245 | return 0; |
| 2246 | } |
| 2247 | ScopedCBB output; |
| 2248 | CBB parameters_list; |
| 2249 | if (!CBB_init(output.get(), 7) || |
| 2250 | !CBB_add_u8(output.get(), kTbProtocolVersionMajor) || |
| 2251 | !CBB_add_u8(output.get(), kTbProtocolVersionMinor) || |
| 2252 | !CBB_add_u8_length_prefixed(output.get(), ¶meters_list)) { |
| 2253 | *out_alert_value = SSL_AD_INTERNAL_ERROR; |
| 2254 | return -1; |
| 2255 | } |
| 2256 | for (size_t i = 0; i < ssl_config_.token_binding_params.size(); ++i) { |
| 2257 | if (!CBB_add_u8(¶meters_list, ssl_config_.token_binding_params[i])) { |
| 2258 | *out_alert_value = SSL_AD_INTERNAL_ERROR; |
| 2259 | return -1; |
| 2260 | } |
| 2261 | } |
| 2262 | // |*out| will be freed by TokenBindingFreeCallback. |
| 2263 | if (!CBB_finish(output.get(), const_cast<uint8_t**>(out), out_len)) { |
| 2264 | *out_alert_value = SSL_AD_INTERNAL_ERROR; |
| 2265 | return -1; |
| 2266 | } |
| 2267 | |
| 2268 | return 1; |
| 2269 | } |
| 2270 | |
| 2271 | int SSLClientSocketOpenSSL::TokenBindingParse(const uint8_t* contents, |
| 2272 | size_t contents_len, |
| 2273 | int* out_alert_value) { |
| 2274 | if (completed_connect_) { |
| 2275 | // Token Binding may only be negotiated on the initial handshake. |
| 2276 | *out_alert_value = SSL_AD_ILLEGAL_PARAMETER; |
| 2277 | return 0; |
| 2278 | } |
| 2279 | |
| 2280 | CBS extension; |
| 2281 | CBS_init(&extension, contents, contents_len); |
| 2282 | |
| 2283 | CBS parameters_list; |
| 2284 | uint8_t version_major, version_minor, param; |
| 2285 | if (!CBS_get_u8(&extension, &version_major) || |
| 2286 | !CBS_get_u8(&extension, &version_minor) || |
| 2287 | !CBS_get_u8_length_prefixed(&extension, ¶meters_list) || |
| 2288 | !CBS_get_u8(¶meters_list, ¶m) || CBS_len(¶meters_list) > 0 || |
| 2289 | CBS_len(&extension) > 0) { |
| 2290 | *out_alert_value = SSL_AD_DECODE_ERROR; |
| 2291 | return 0; |
| 2292 | } |
| 2293 | // The server-negotiated version must be less than or equal to our version. |
| 2294 | if (version_major > kTbProtocolVersionMajor || |
| 2295 | (version_minor > kTbProtocolVersionMinor && |
| 2296 | version_major == kTbProtocolVersionMajor)) { |
| 2297 | *out_alert_value = SSL_AD_ILLEGAL_PARAMETER; |
| 2298 | return 0; |
| 2299 | } |
| 2300 | // If the version the server negotiated is older than we support, don't fail |
| 2301 | // parsing the extension, but also don't set |negotiated_|. |
| 2302 | if (version_major < kTbMinProtocolVersionMajor || |
| 2303 | (version_minor < kTbMinProtocolVersionMinor && |
| 2304 | version_major == kTbMinProtocolVersionMajor)) { |
| 2305 | return 1; |
| 2306 | } |
| 2307 | |
| 2308 | for (size_t i = 0; i < ssl_config_.token_binding_params.size(); ++i) { |
| 2309 | if (param == ssl_config_.token_binding_params[i]) { |
| 2310 | tb_negotiated_param_ = ssl_config_.token_binding_params[i]; |
| 2311 | tb_was_negotiated_ = true; |
| 2312 | return 1; |
| 2313 | } |
| 2314 | } |
| 2315 | |
| 2316 | *out_alert_value = SSL_AD_ILLEGAL_PARAMETER; |
| 2317 | return 0; |
| 2318 | } |
| 2319 | |
[email protected] | 7e5dd49f | 2010-12-08 18:33:49 | [diff] [blame] | 2320 | } // namespace net |