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