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