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