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