[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 | |
mab | b51c514 | 2016-12-07 09:32:40 | [diff] [blame] | 10 | #include <algorithm> |
David Benjamin | f8ebd2b | 2017-12-15 19:22:41 | [diff] [blame] | 11 | #include <map> |
davidben | 752bcf2 | 2015-12-21 22:55:50 | [diff] [blame] | 12 | #include <utility> |
| 13 | |
[email protected] | 0f7804ec | 2011-10-07 20:04:18 | [diff] [blame] | 14 | #include "base/bind.h" |
[email protected] | f2da6ac | 2013-02-04 08:22:53 | [diff] [blame] | 15 | #include "base/callback_helpers.h" |
David Benjamin | 9ba36b0 | 2017-11-10 19:01:53 | [diff] [blame] | 16 | #include "base/containers/span.h" |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 17 | #include "base/lazy_instance.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 18 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 19 | #include "base/memory/singleton.h" |
mmenke | 1beda3d | 2016-07-22 03:33:45 | [diff] [blame] | 20 | #include "base/metrics/field_trial.h" |
Adam Langley | 07416413 | 2018-01-17 03:27:32 | [diff] [blame] | 21 | #include "base/metrics/field_trial_params.h" |
Ilya Sherman | 0eb3980 | 2017-12-08 20:58:18 | [diff] [blame] | 22 | #include "base/metrics/histogram_functions.h" |
asvitkine | c3c9372 | 2015-06-17 14:48:37 | [diff] [blame] | 23 | #include "base/metrics/histogram_macros.h" |
nharper | 49b27d99 | 2016-02-09 18:28:51 | [diff] [blame] | 24 | #include "base/strings/string_number_conversions.h" |
davidben | 018aad6 | 2014-09-12 02:25:19 | [diff] [blame] | 25 | #include "base/strings/string_piece.h" |
xunjieli | 9f8c5fb5 | 2016-12-07 22:59:33 | [diff] [blame] | 26 | #include "base/strings/stringprintf.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 27 | #include "base/synchronization/lock.h" |
xunjieli | 9f8c5fb5 | 2016-12-07 22:59:33 | [diff] [blame] | 28 | #include "base/trace_event/process_memory_dump.h" |
ssid | 6d6b4010 | 2016-04-05 18:59:56 | [diff] [blame] | 29 | #include "base/trace_event/trace_event.h" |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 30 | #include "base/values.h" |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 31 | #include "crypto/ec_private_key.h" |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 32 | #include "crypto/openssl_util.h" |
David Benjamin | 570460e | 2018-10-16 06:01:29 | [diff] [blame] | 33 | #include "net/base/features.h" |
martijn | a2e83bd | 2016-03-18 13:10:45 | [diff] [blame] | 34 | #include "net/base/ip_address.h" |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 35 | #include "net/base/net_errors.h" |
xunjieli | 0b7f5b6 | 2016-12-06 20:43:48 | [diff] [blame] | 36 | #include "net/base/trace_constants.h" |
David Benjamin | d5503c8 | 2018-02-01 20:59:38 | [diff] [blame] | 37 | #include "net/base/url_util.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 38 | #include "net/cert/cert_verifier.h" |
estark | 6f9b3d8 | 2016-01-12 21:37:05 | [diff] [blame] | 39 | #include "net/cert/ct_policy_enforcer.h" |
estark | 723b5eeb | 2016-02-18 21:01:12 | [diff] [blame] | 40 | #include "net/cert/ct_policy_status.h" |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 41 | #include "net/cert/ct_verifier.h" |
David Benjamin | f8ebd2b | 2017-12-15 19:22:41 | [diff] [blame] | 42 | #include "net/cert/internal/parse_certificate.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 43 | #include "net/cert/x509_certificate_net_log_param.h" |
mattm | 316af82 | 2017-02-23 04:05:56 | [diff] [blame] | 44 | #include "net/cert/x509_util.h" |
David Benjamin | f8ebd2b | 2017-12-15 19:22:41 | [diff] [blame] | 45 | #include "net/der/parse_values.h" |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 46 | #include "net/http/transport_security_state.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 47 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 48 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 49 | #include "net/log/net_log_parameters_callback.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 50 | #include "net/ssl/ssl_cert_request_info.h" |
davidben | 281d13f0 | 2016-04-27 20:43:28 | [diff] [blame] | 51 | #include "net/ssl/ssl_cipher_suite_names.h" |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 52 | #include "net/ssl/ssl_client_session_cache.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 53 | #include "net/ssl/ssl_connection_status_flags.h" |
| 54 | #include "net/ssl/ssl_info.h" |
David Benjamin | bd37c17 | 2018-07-11 17:24:57 | [diff] [blame] | 55 | #include "net/ssl/ssl_key_logger.h" |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 56 | #include "net/ssl/ssl_private_key.h" |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 57 | #include "net/traffic_annotation/network_traffic_annotation.h" |
tfarina | e8cb8aa | 2016-10-21 02:44:01 | [diff] [blame] | 58 | #include "third_party/boringssl/src/include/openssl/bio.h" |
| 59 | #include "third_party/boringssl/src/include/openssl/bytestring.h" |
| 60 | #include "third_party/boringssl/src/include/openssl/err.h" |
| 61 | #include "third_party/boringssl/src/include/openssl/evp.h" |
| 62 | #include "third_party/boringssl/src/include/openssl/mem.h" |
| 63 | #include "third_party/boringssl/src/include/openssl/ssl.h" |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 64 | |
Adam Langley | 93cbfad1 | 2018-07-06 22:07:16 | [diff] [blame] | 65 | #if !defined(NET_DISABLE_BROTLI) |
| 66 | #include "third_party/brotli/include/brotli/decode.h" |
| 67 | #endif |
| 68 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 69 | namespace net { |
| 70 | |
| 71 | namespace { |
| 72 | |
[email protected] | 4b76856 | 2013-02-16 04:10:07 | [diff] [blame] | 73 | // This constant can be any non-negative/non-zero value (eg: it does not |
| 74 | // overlap with any value of the net::Error range, including net::OK). |
Oscar Johansson | d49464e | 2018-07-02 09:35:45 | [diff] [blame] | 75 | const int kSSLClientSocketNoPendingResult = 1; |
[email protected] | 4b76856 | 2013-02-16 04:10:07 | [diff] [blame] | 76 | |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 77 | // Default size of the internal BoringSSL buffers. |
mmenke | 1beda3d | 2016-07-22 03:33:45 | [diff] [blame] | 78 | const int kDefaultOpenSSLBufferSize = 17 * 1024; |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 79 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 80 | std::unique_ptr<base::Value> NetLogPrivateKeyOperationCallback( |
David Benjamin | b9bafbe | 2017-11-07 21:41:38 | [diff] [blame] | 81 | uint16_t algorithm, |
David Benjamin | b65b073 | 2018-11-09 20:33:53 | [diff] [blame] | 82 | SSLPrivateKey* key, |
davidben | 752bcf2 | 2015-12-21 22:55:50 | [diff] [blame] | 83 | NetLogCaptureMode mode) { |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 84 | std::unique_ptr<base::DictionaryValue> value(new base::DictionaryValue); |
David Benjamin | b9bafbe | 2017-11-07 21:41:38 | [diff] [blame] | 85 | value->SetString("algorithm", SSL_get_signature_algorithm_name( |
| 86 | algorithm, 0 /* exclude curve */)); |
David Benjamin | b65b073 | 2018-11-09 20:33:53 | [diff] [blame] | 87 | value->SetString("provider", key->GetProviderName()); |
davidben | 752bcf2 | 2015-12-21 22:55:50 | [diff] [blame] | 88 | return std::move(value); |
| 89 | } |
| 90 | |
davidben | 281d13f0 | 2016-04-27 20:43:28 | [diff] [blame] | 91 | std::unique_ptr<base::Value> NetLogSSLInfoCallback( |
| 92 | SSLClientSocketImpl* socket, |
| 93 | NetLogCaptureMode capture_mode) { |
| 94 | SSLInfo ssl_info; |
| 95 | if (!socket->GetSSLInfo(&ssl_info)) |
| 96 | return nullptr; |
| 97 | |
| 98 | std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 99 | const char* version_str; |
| 100 | SSLVersionToString(&version_str, |
| 101 | SSLConnectionStatusToVersion(ssl_info.connection_status)); |
| 102 | dict->SetString("version", version_str); |
| 103 | dict->SetBoolean("is_resumed", |
| 104 | ssl_info.handshake_type == SSLInfo::HANDSHAKE_RESUME); |
| 105 | dict->SetInteger("cipher_suite", SSLConnectionStatusToCipherSuite( |
| 106 | ssl_info.connection_status)); |
| 107 | |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 108 | dict->SetString("next_proto", |
| 109 | NextProtoToString(socket->GetNegotiatedProtocol())); |
davidben | 281d13f0 | 2016-04-27 20:43:28 | [diff] [blame] | 110 | |
| 111 | return std::move(dict); |
| 112 | } |
| 113 | |
davidben | cef9e21 | 2017-04-19 15:00:10 | [diff] [blame] | 114 | std::unique_ptr<base::Value> NetLogSSLAlertCallback( |
| 115 | const void* bytes, |
| 116 | size_t len, |
| 117 | NetLogCaptureMode capture_mode) { |
| 118 | std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 119 | dict->SetString("hex_encoded_bytes", base::HexEncode(bytes, len)); |
| 120 | return std::move(dict); |
| 121 | } |
| 122 | |
| 123 | std::unique_ptr<base::Value> NetLogSSLMessageCallback( |
| 124 | bool is_write, |
| 125 | const void* bytes, |
| 126 | size_t len, |
| 127 | NetLogCaptureMode capture_mode) { |
| 128 | std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 129 | if (len == 0) { |
| 130 | NOTREACHED(); |
| 131 | return std::move(dict); |
| 132 | } |
| 133 | |
| 134 | // The handshake message type is the first byte. Include it so elided messages |
| 135 | // still report their type. |
| 136 | uint8_t type = reinterpret_cast<const uint8_t*>(bytes)[0]; |
| 137 | dict->SetInteger("type", type); |
| 138 | |
| 139 | // Elide client certificate messages unless logging socket bytes. The client |
| 140 | // certificate does not contain information needed to impersonate the user |
| 141 | // (that's the private key which isn't sent over the wire), but it may contain |
| 142 | // information on the user's identity. |
| 143 | if (!is_write || type != SSL3_MT_CERTIFICATE || |
| 144 | capture_mode.include_socket_bytes()) { |
| 145 | dict->SetString("hex_encoded_bytes", base::HexEncode(bytes, len)); |
| 146 | } |
| 147 | |
| 148 | return std::move(dict); |
| 149 | } |
| 150 | |
David Benjamin | f8ebd2b | 2017-12-15 19:22:41 | [diff] [blame] | 151 | // This enum is used in histograms, so values may not be reused. |
| 152 | enum class RSAKeyUsage { |
| 153 | // The TLS cipher suite was not RSA or ECDHE_RSA. |
| 154 | kNotRSA = 0, |
| 155 | // The Key Usage extension is not present, which is consistent with TLS usage. |
| 156 | kOKNoExtension = 1, |
| 157 | // The Key Usage extension has both the digitalSignature and keyEncipherment |
| 158 | // bits, which is consistent with TLS usage. |
| 159 | kOKHaveBoth = 2, |
| 160 | // The Key Usage extension contains only the digitalSignature bit, which is |
| 161 | // consistent with TLS usage. |
| 162 | kOKHaveDigitalSignature = 3, |
| 163 | // The Key Usage extension contains only the keyEncipherment bit, which is |
| 164 | // consistent with TLS usage. |
| 165 | kOKHaveKeyEncipherment = 4, |
| 166 | // The Key Usage extension is missing the digitalSignature bit. |
| 167 | kMissingDigitalSignature = 5, |
| 168 | // The Key Usage extension is missing the keyEncipherment bit. |
| 169 | kMissingKeyEncipherment = 6, |
| 170 | // There was an error processing the certificate. |
| 171 | kError = 7, |
| 172 | |
| 173 | kLastValue = kError, |
| 174 | }; |
| 175 | |
| 176 | RSAKeyUsage CheckRSAKeyUsage(const X509Certificate* cert, |
| 177 | const SSL_CIPHER* cipher) { |
| 178 | bool need_key_encipherment = false; |
| 179 | switch (SSL_CIPHER_get_kx_nid(cipher)) { |
| 180 | case NID_kx_rsa: |
| 181 | need_key_encipherment = true; |
| 182 | break; |
| 183 | case NID_kx_ecdhe: |
| 184 | if (SSL_CIPHER_get_auth_nid(cipher) != NID_auth_rsa) { |
| 185 | return RSAKeyUsage::kNotRSA; |
| 186 | } |
| 187 | break; |
| 188 | default: |
| 189 | return RSAKeyUsage::kNotRSA; |
| 190 | } |
| 191 | |
| 192 | const CRYPTO_BUFFER* buffer = cert->cert_buffer(); |
| 193 | der::Input tbs_certificate_tlv; |
| 194 | der::Input signature_algorithm_tlv; |
| 195 | der::BitString signature_value; |
| 196 | ParsedTbsCertificate tbs; |
| 197 | if (!ParseCertificate( |
| 198 | der::Input(CRYPTO_BUFFER_data(buffer), CRYPTO_BUFFER_len(buffer)), |
| 199 | &tbs_certificate_tlv, &signature_algorithm_tlv, &signature_value, |
| 200 | nullptr) || |
| 201 | !ParseTbsCertificate(tbs_certificate_tlv, |
| 202 | x509_util::DefaultParseCertificateOptions(), &tbs, |
| 203 | nullptr)) { |
| 204 | return RSAKeyUsage::kError; |
| 205 | } |
| 206 | |
| 207 | if (!tbs.has_extensions) { |
| 208 | return RSAKeyUsage::kOKNoExtension; |
| 209 | } |
| 210 | |
| 211 | std::map<der::Input, ParsedExtension> extensions; |
| 212 | if (!ParseExtensions(tbs.extensions_tlv, &extensions)) { |
| 213 | return RSAKeyUsage::kError; |
| 214 | } |
| 215 | ParsedExtension key_usage_ext; |
| 216 | if (!ConsumeExtension(KeyUsageOid(), &extensions, &key_usage_ext)) { |
| 217 | return RSAKeyUsage::kOKNoExtension; |
| 218 | } |
| 219 | der::BitString key_usage; |
| 220 | if (!ParseKeyUsage(key_usage_ext.value, &key_usage)) { |
| 221 | return RSAKeyUsage::kError; |
| 222 | } |
| 223 | |
| 224 | bool have_digital_signature = |
| 225 | key_usage.AssertsBit(KEY_USAGE_BIT_DIGITAL_SIGNATURE); |
| 226 | bool have_key_encipherment = |
| 227 | key_usage.AssertsBit(KEY_USAGE_BIT_KEY_ENCIPHERMENT); |
| 228 | if (have_digital_signature && have_key_encipherment) { |
| 229 | return RSAKeyUsage::kOKHaveBoth; |
| 230 | } |
| 231 | |
| 232 | if (need_key_encipherment) { |
| 233 | return have_key_encipherment ? RSAKeyUsage::kOKHaveKeyEncipherment |
| 234 | : RSAKeyUsage::kMissingKeyEncipherment; |
| 235 | } |
| 236 | return have_digital_signature ? RSAKeyUsage::kOKHaveDigitalSignature |
| 237 | : RSAKeyUsage::kMissingDigitalSignature; |
| 238 | } |
| 239 | |
Adam Langley | 93cbfad1 | 2018-07-06 22:07:16 | [diff] [blame] | 240 | #if !defined(NET_DISABLE_BROTLI) |
| 241 | int DecompressBrotliCert(SSL* ssl, |
| 242 | CRYPTO_BUFFER** out, |
| 243 | size_t uncompressed_len, |
| 244 | const uint8_t* in, |
| 245 | size_t in_len) { |
| 246 | uint8_t* data; |
| 247 | bssl::UniquePtr<CRYPTO_BUFFER> decompressed( |
| 248 | CRYPTO_BUFFER_alloc(&data, uncompressed_len)); |
| 249 | if (!decompressed) { |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | size_t output_size = uncompressed_len; |
| 254 | if (BrotliDecoderDecompress(in_len, in, &output_size, data) != |
| 255 | BROTLI_DECODER_RESULT_SUCCESS || |
| 256 | output_size != uncompressed_len) { |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | *out = decompressed.release(); |
| 261 | return 1; |
| 262 | } |
| 263 | #endif |
| 264 | |
[email protected] | 821e3bb | 2013-11-08 01:06:01 | [diff] [blame] | 265 | } // namespace |
| 266 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 267 | class SSLClientSocketImpl::SSLContext { |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 268 | public: |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 269 | static SSLContext* GetInstance() { |
fdoray | 33e7c3c5 | 2017-01-19 18:37:23 | [diff] [blame] | 270 | return base::Singleton<SSLContext, |
| 271 | base::LeakySingletonTraits<SSLContext>>::get(); |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 272 | } |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 273 | SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 274 | SSLClientSessionCache* session_cache() { return &session_cache_; } |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 275 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 276 | SSLClientSocketImpl* GetClientSocketFromSSL(const SSL* ssl) { |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 277 | DCHECK(ssl); |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 278 | SSLClientSocketImpl* socket = static_cast<SSLClientSocketImpl*>( |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 279 | SSL_get_ex_data(ssl, ssl_socket_data_index_)); |
| 280 | DCHECK(socket); |
| 281 | return socket; |
| 282 | } |
| 283 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 284 | bool SetClientSocketForSSL(SSL* ssl, SSLClientSocketImpl* socket) { |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 285 | return SSL_set_ex_data(ssl, ssl_socket_data_index_, socket) != 0; |
| 286 | } |
| 287 | |
David Benjamin | bd37c17 | 2018-07-11 17:24:57 | [diff] [blame] | 288 | void SetSSLKeyLogger(std::unique_ptr<SSLKeyLogger> logger) { |
davidben | 2a811e4e | 2015-12-01 10:49:34 | [diff] [blame] | 289 | DCHECK(!ssl_key_logger_); |
David Benjamin | bd37c17 | 2018-07-11 17:24:57 | [diff] [blame] | 290 | ssl_key_logger_ = std::move(logger); |
davidben | 2a811e4e | 2015-12-01 10:49:34 | [diff] [blame] | 291 | SSL_CTX_set_keylog_callback(ssl_ctx_.get(), KeyLogCallback); |
| 292 | } |
davidben | 2a811e4e | 2015-12-01 10:49:34 | [diff] [blame] | 293 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 294 | static const SSL_PRIVATE_KEY_METHOD kPrivateKeyMethod; |
| 295 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 296 | private: |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 297 | friend struct base::DefaultSingletonTraits<SSLContext>; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 298 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 299 | SSLContext() : session_cache_(SSLClientSessionCache::Config()) { |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 300 | crypto::EnsureOpenSSLInit(); |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 301 | ssl_socket_data_index_ = SSL_get_ex_new_index(0, 0, 0, 0, 0); |
| 302 | DCHECK_NE(ssl_socket_data_index_, -1); |
davidben | a35b40c3 | 2017-03-09 17:33:45 | [diff] [blame] | 303 | ssl_ctx_.reset(SSL_CTX_new(TLS_with_buffers_method())); |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 304 | SSL_CTX_set_cert_cb(ssl_ctx_.get(), ClientCertRequestCallback, NULL); |
davidben | a35b40c3 | 2017-03-09 17:33:45 | [diff] [blame] | 305 | |
| 306 | // The server certificate is verified after the handshake in DoVerifyCert. |
Steven Valdez | 3eaa996 | 2017-07-18 21:51:05 | [diff] [blame] | 307 | SSL_CTX_set_custom_verify(ssl_ctx_.get(), SSL_VERIFY_PEER, |
| 308 | CertVerifyCallback); |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 309 | |
| 310 | // Disable the internal session cache. Session caching is handled |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 311 | // externally (i.e. by SSLClientSessionCache). |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 312 | SSL_CTX_set_session_cache_mode( |
| 313 | ssl_ctx_.get(), SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL); |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 314 | SSL_CTX_sess_set_new_cb(ssl_ctx_.get(), NewSessionCallback); |
davidben | 99ce630 | 2016-11-09 17:30:28 | [diff] [blame] | 315 | SSL_CTX_set_timeout(ssl_ctx_.get(), 1 * 60 * 60 /* one hour */); |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame] | 316 | |
davidben | facfac7b | 2016-09-27 22:39:53 | [diff] [blame] | 317 | SSL_CTX_set_grease_enabled(ssl_ctx_.get(), 1); |
| 318 | |
davidben | bf0fcf1 | 2017-02-10 21:00:34 | [diff] [blame] | 319 | // Deduplicate all certificates minted from the SSL_CTX in memory. |
| 320 | SSL_CTX_set0_buffer_pool(ssl_ctx_.get(), x509_util::GetBufferPool()); |
| 321 | |
David Benjamin | 7a8e4dfa | 2018-06-12 23:07:21 | [diff] [blame] | 322 | SSL_CTX_set_info_callback(ssl_ctx_.get(), InfoCallback); |
davidben | cef9e21 | 2017-04-19 15:00:10 | [diff] [blame] | 323 | SSL_CTX_set_msg_callback(ssl_ctx_.get(), MessageCallback); |
Adam Langley | 93cbfad1 | 2018-07-06 22:07:16 | [diff] [blame] | 324 | |
| 325 | #if !defined(NET_DISABLE_BROTLI) |
| 326 | SSL_CTX_add_cert_compression_alg( |
| 327 | ssl_ctx_.get(), TLSEXT_cert_compression_brotli, |
| 328 | nullptr /* compression not supported */, DecompressBrotliCert); |
| 329 | #endif |
[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 | |
Steven Valdez | 3eaa996 | 2017-07-18 21:51:05 | [diff] [blame] | 338 | static ssl_verify_result_t CertVerifyCallback(SSL* ssl, uint8_t* out_alert) { |
| 339 | // The certificate is verified after the handshake in DoVerifyCert. |
| 340 | return ssl_verify_ok; |
| 341 | } |
| 342 | |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 343 | static int NewSessionCallback(SSL* ssl, SSL_SESSION* session) { |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 344 | SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 345 | return socket->NewSessionCallback(session); |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 346 | } |
| 347 | |
David Benjamin | b9bafbe | 2017-11-07 21:41:38 | [diff] [blame] | 348 | static ssl_private_key_result_t PrivateKeySignCallback(SSL* ssl, |
| 349 | uint8_t* out, |
| 350 | size_t* out_len, |
| 351 | size_t max_out, |
| 352 | uint16_t algorithm, |
| 353 | const uint8_t* in, |
| 354 | size_t in_len) { |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 355 | SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
David Benjamin | b9bafbe | 2017-11-07 21:41:38 | [diff] [blame] | 356 | return socket->PrivateKeySignCallback(out, out_len, max_out, algorithm, in, |
| 357 | in_len); |
davidben | 0bca07fd | 2016-07-18 15:12:03 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static ssl_private_key_result_t PrivateKeyCompleteCallback(SSL* ssl, |
| 361 | uint8_t* out, |
| 362 | size_t* out_len, |
| 363 | size_t max_out) { |
| 364 | SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
| 365 | return socket->PrivateKeyCompleteCallback(out, out_len, max_out); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 366 | } |
| 367 | |
davidben | 2a811e4e | 2015-12-01 10:49:34 | [diff] [blame] | 368 | static void KeyLogCallback(const SSL* ssl, const char* line) { |
| 369 | GetInstance()->ssl_key_logger_->WriteLine(line); |
| 370 | } |
davidben | 2a811e4e | 2015-12-01 10:49:34 | [diff] [blame] | 371 | |
David Benjamin | 7a8e4dfa | 2018-06-12 23:07:21 | [diff] [blame] | 372 | static void InfoCallback(const SSL* ssl, int type, int value) { |
| 373 | SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
| 374 | socket->InfoCallback(type, value); |
| 375 | } |
| 376 | |
davidben | cef9e21 | 2017-04-19 15:00:10 | [diff] [blame] | 377 | static void MessageCallback(int is_write, |
| 378 | int version, |
| 379 | int content_type, |
| 380 | const void* buf, |
| 381 | size_t len, |
| 382 | SSL* ssl, |
| 383 | void* arg) { |
| 384 | SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
| 385 | return socket->MessageCallback(is_write, content_type, buf, len); |
| 386 | } |
| 387 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 388 | // This is the index used with SSL_get_ex_data to retrieve the owner |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 389 | // SSLClientSocketImpl object from an SSL instance. |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 390 | int ssl_socket_data_index_; |
| 391 | |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 392 | bssl::UniquePtr<SSL_CTX> ssl_ctx_; |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 393 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 394 | std::unique_ptr<SSLKeyLogger> ssl_key_logger_; |
davidben | 2a811e4e | 2015-12-01 10:49:34 | [diff] [blame] | 395 | |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 396 | // TODO(davidben): Use a separate cache per URLRequestContext. |
| 397 | // https://crbug.com/458365 |
| 398 | // |
| 399 | // TODO(davidben): Sessions should be invalidated on fatal |
| 400 | // alerts. https://crbug.com/466352 |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 401 | SSLClientSessionCache session_cache_; |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 402 | }; |
| 403 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 404 | const SSL_PRIVATE_KEY_METHOD |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 405 | SSLClientSocketImpl::SSLContext::kPrivateKeyMethod = { |
David Benjamin | b9bafbe | 2017-11-07 21:41:38 | [diff] [blame] | 406 | &SSLClientSocketImpl::SSLContext::PrivateKeySignCallback, |
davidben | 0bca07fd | 2016-07-18 15:12:03 | [diff] [blame] | 407 | nullptr /* decrypt */, |
| 408 | &SSLClientSocketImpl::SSLContext::PrivateKeyCompleteCallback, |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 409 | }; |
| 410 | |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 411 | // static |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 412 | void SSLClientSocket::ClearSessionCache() { |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 413 | SSLClientSocketImpl::SSLContext* context = |
| 414 | SSLClientSocketImpl::SSLContext::GetInstance(); |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 415 | context->session_cache()->Flush(); |
| 416 | } |
| 417 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 418 | SSLClientSocketImpl::SSLClientSocketImpl( |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 419 | std::unique_ptr<ClientSocketHandle> transport_socket, |
[email protected] | 055d7f2 | 2010-11-15 12:03:12 | [diff] [blame] | 420 | const HostPortPair& host_and_port, |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 421 | const SSLConfig& ssl_config, |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 422 | const SSLClientSocketContext& context) |
Oscar Johansson | d49464e | 2018-07-02 09:35:45 | [diff] [blame] | 423 | : pending_read_error_(kSSLClientSocketNoPendingResult), |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 424 | pending_read_ssl_error_(SSL_ERROR_NONE), |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 425 | completed_connect_(false), |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 426 | was_ever_used_(false), |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 427 | cert_verifier_(context.cert_verifier), |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 428 | cert_transparency_verifier_(context.cert_transparency_verifier), |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 429 | transport_(std::move(transport_socket)), |
[email protected] | 055d7f2 | 2010-11-15 12:03:12 | [diff] [blame] | 430 | host_and_port_(host_and_port), |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 431 | ssl_config_(ssl_config), |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 432 | ssl_session_cache_shard_(context.ssl_session_cache_shard), |
[email protected] | 013c17c | 2012-01-21 19:09:01 | [diff] [blame] | 433 | next_handshake_state_(STATE_NONE), |
Steven Valdez | 6af02df | 2018-07-15 21:52:33 | [diff] [blame] | 434 | in_confirm_handshake_(false), |
svaldez | 4af14d2 | 2015-08-20 13:48:24 | [diff] [blame] | 435 | disconnected_(false), |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 436 | negotiated_protocol_(kProtoUnknown), |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 437 | certificate_verified_(false), |
davidben | fe132d9 | 2016-09-27 18:07:21 | [diff] [blame] | 438 | certificate_requested_(false), |
Oscar Johansson | d49464e | 2018-07-02 09:35:45 | [diff] [blame] | 439 | signature_result_(kSSLClientSocketNoPendingResult), |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 440 | transport_security_state_(context.transport_security_state), |
estark | 6f9b3d8 | 2016-01-12 21:37:05 | [diff] [blame] | 441 | policy_enforcer_(context.ct_policy_enforcer), |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 442 | pkp_bypassed_(false), |
Carlos IL | 8113338 | 2017-12-06 17:18:45 | [diff] [blame] | 443 | is_fatal_cert_error_(false), |
kulkarni.a | cd7b446 | 2014-08-28 07:41:34 | [diff] [blame] | 444 | net_log_(transport_->socket()->NetLog()), |
| 445 | weak_factory_(this) { |
rsleevi | be81cd6 | 2016-06-24 01:38:59 | [diff] [blame] | 446 | CHECK(cert_verifier_); |
| 447 | CHECK(transport_security_state_); |
| 448 | CHECK(cert_transparency_verifier_); |
| 449 | CHECK(policy_enforcer_); |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 450 | } |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 451 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 452 | SSLClientSocketImpl::~SSLClientSocketImpl() { |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 453 | Disconnect(); |
| 454 | } |
| 455 | |
David Benjamin | bd37c17 | 2018-07-11 17:24:57 | [diff] [blame] | 456 | void SSLClientSocketImpl::SetSSLKeyLogger( |
| 457 | std::unique_ptr<SSLKeyLogger> logger) { |
| 458 | SSLContext::GetInstance()->SetSSLKeyLogger(std::move(logger)); |
zhongyi | 81f85c6d9 | 2015-10-16 19:34:14 | [diff] [blame] | 459 | } |
| 460 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 461 | int SSLClientSocketImpl::ExportKeyingMaterial(const base::StringPiece& label, |
| 462 | bool has_context, |
| 463 | const base::StringPiece& context, |
| 464 | unsigned char* out, |
| 465 | unsigned int outlen) { |
davidben | 86935f7 | 2015-05-06 22:24:49 | [diff] [blame] | 466 | if (!IsConnected()) |
| 467 | return ERR_SOCKET_NOT_CONNECTED; |
| 468 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 469 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 470 | |
davidben | f225b26 | 2016-09-15 22:09:22 | [diff] [blame] | 471 | if (!SSL_export_keying_material( |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 472 | ssl_.get(), out, outlen, label.data(), label.size(), |
davidben | f225b26 | 2016-09-15 22:09:22 | [diff] [blame] | 473 | reinterpret_cast<const unsigned char*>(context.data()), |
| 474 | context.length(), has_context ? 1 : 0)) { |
| 475 | LOG(ERROR) << "Failed to export keying material."; |
| 476 | return ERR_FAILED; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 477 | } |
davidben | f225b26 | 2016-09-15 22:09:22 | [diff] [blame] | 478 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 479 | return OK; |
| 480 | } |
| 481 | |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 482 | int SSLClientSocketImpl::Connect(CompletionOnceCallback callback) { |
svaldez | 4af14d2 | 2015-08-20 13:48:24 | [diff] [blame] | 483 | // Although StreamSocket does allow calling Connect() after Disconnect(), |
| 484 | // this has never worked for layered sockets. CHECK to detect any consumers |
| 485 | // reconnecting an SSL socket. |
| 486 | // |
| 487 | // TODO(davidben,mmenke): Remove this API feature. See |
| 488 | // https://crbug.com/499289. |
| 489 | CHECK(!disconnected_); |
| 490 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 491 | net_log_.BeginEvent(NetLogEventType::SSL_CONNECT); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 492 | |
| 493 | // Set up new ssl object. |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 494 | int rv = Init(); |
| 495 | if (rv != OK) { |
davidben | 281d13f0 | 2016-04-27 20:43:28 | [diff] [blame] | 496 | LogConnectEndEvent(rv); |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 497 | return rv; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | // Set SSL to client mode. Handshake happens in the loop below. |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 501 | SSL_set_connect_state(ssl_.get()); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 502 | |
rsleevi | adbd498 | 2016-06-13 22:10:27 | [diff] [blame] | 503 | next_handshake_state_ = STATE_HANDSHAKE; |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 504 | rv = DoHandshakeLoop(OK); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 505 | if (rv == ERR_IO_PENDING) { |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 506 | user_connect_callback_ = std::move(callback); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 507 | } else { |
davidben | 281d13f0 | 2016-04-27 20:43:28 | [diff] [blame] | 508 | LogConnectEndEvent(rv); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | return rv > OK ? OK : rv; |
| 512 | } |
| 513 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 514 | void SSLClientSocketImpl::Disconnect() { |
svaldez | 4af14d2 | 2015-08-20 13:48:24 | [diff] [blame] | 515 | disconnected_ = true; |
| 516 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 517 | // Shut down anything that may call us back. |
eroman | 7f9236a | 2015-05-11 21:23:43 | [diff] [blame] | 518 | cert_verifier_request_.reset(); |
davidben | 67e8391 | 2016-10-12 18:36:32 | [diff] [blame] | 519 | weak_factory_.InvalidateWeakPtrs(); |
davidben | 3418e81f | 2016-10-19 00:09:45 | [diff] [blame] | 520 | transport_adapter_.reset(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 521 | |
davidben | 67e8391 | 2016-10-12 18:36:32 | [diff] [blame] | 522 | // Release user callbacks. |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 523 | user_connect_callback_.Reset(); |
| 524 | user_read_callback_.Reset(); |
| 525 | user_write_callback_.Reset(); |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 526 | user_read_buf_ = NULL; |
| 527 | user_read_buf_len_ = 0; |
| 528 | user_write_buf_ = NULL; |
| 529 | user_write_buf_len_ = 0; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 530 | |
davidben | 67e8391 | 2016-10-12 18:36:32 | [diff] [blame] | 531 | transport_->socket()->Disconnect(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 532 | } |
| 533 | |
Steven Valdez | 6af02df | 2018-07-15 21:52:33 | [diff] [blame] | 534 | // ConfirmHandshake may only be called on a connected socket and, like other |
| 535 | // socket methods, there may only be one ConfirmHandshake operation in progress |
| 536 | // at once. |
| 537 | int SSLClientSocketImpl::ConfirmHandshake(CompletionOnceCallback callback) { |
| 538 | CHECK(completed_connect_); |
| 539 | CHECK(!in_confirm_handshake_); |
| 540 | if (!SSL_in_early_data(ssl_.get())) { |
| 541 | return OK; |
| 542 | } |
| 543 | |
| 544 | net_log_.BeginEvent(NetLogEventType::SSL_CONFIRM_HANDSHAKE); |
| 545 | next_handshake_state_ = STATE_HANDSHAKE; |
| 546 | in_confirm_handshake_ = true; |
| 547 | int rv = DoHandshakeLoop(OK); |
| 548 | if (rv == ERR_IO_PENDING) { |
| 549 | user_connect_callback_ = std::move(callback); |
| 550 | } else { |
| 551 | net_log_.EndEvent(NetLogEventType::SSL_CONFIRM_HANDSHAKE); |
| 552 | in_confirm_handshake_ = false; |
| 553 | } |
| 554 | |
| 555 | return rv > OK ? OK : rv; |
| 556 | } |
| 557 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 558 | bool SSLClientSocketImpl::IsConnected() const { |
davidben | 67e8391 | 2016-10-12 18:36:32 | [diff] [blame] | 559 | // If the handshake has not yet completed or the socket has been explicitly |
| 560 | // disconnected. |
| 561 | if (!completed_connect_ || disconnected_) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 562 | return false; |
| 563 | // If an asynchronous operation is still pending. |
| 564 | if (user_read_buf_.get() || user_write_buf_.get()) |
| 565 | return true; |
| 566 | |
| 567 | return transport_->socket()->IsConnected(); |
| 568 | } |
| 569 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 570 | bool SSLClientSocketImpl::IsConnectedAndIdle() const { |
davidben | 67e8391 | 2016-10-12 18:36:32 | [diff] [blame] | 571 | // If the handshake has not yet completed or the socket has been explicitly |
| 572 | // disconnected. |
| 573 | if (!completed_connect_ || disconnected_) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 574 | return false; |
| 575 | // If an asynchronous operation is still pending. |
| 576 | if (user_read_buf_.get() || user_write_buf_.get()) |
| 577 | return false; |
davidben | fc9a6b8 | 2015-04-15 23:47:32 | [diff] [blame] | 578 | |
| 579 | // If there is data read from the network that has not yet been consumed, do |
| 580 | // not treat the connection as idle. |
| 581 | // |
davidben | 3418e81f | 2016-10-19 00:09:45 | [diff] [blame] | 582 | // Note that this does not check whether there is ciphertext that has not yet |
| 583 | // been flushed to the network. |Write| returns early, so this can cause race |
| 584 | // conditions which cause a socket to not be treated reusable when it should |
| 585 | // be. See https://crbug.com/466147. |
| 586 | if (transport_adapter_->HasPendingReadData()) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 587 | return false; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 588 | |
| 589 | return transport_->socket()->IsConnectedAndIdle(); |
| 590 | } |
| 591 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 592 | int SSLClientSocketImpl::GetPeerAddress(IPEndPoint* addressList) const { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 593 | return transport_->socket()->GetPeerAddress(addressList); |
| 594 | } |
| 595 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 596 | int SSLClientSocketImpl::GetLocalAddress(IPEndPoint* addressList) const { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 597 | return transport_->socket()->GetLocalAddress(addressList); |
| 598 | } |
| 599 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 600 | const NetLogWithSource& SSLClientSocketImpl::NetLog() const { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 601 | return net_log_; |
| 602 | } |
| 603 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 604 | bool SSLClientSocketImpl::WasEverUsed() const { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 605 | return was_ever_used_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 606 | } |
| 607 | |
tfarina | 2846404c | 2016-12-25 14:31:37 | [diff] [blame] | 608 | bool SSLClientSocketImpl::WasAlpnNegotiated() const { |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 609 | return negotiated_protocol_ != kProtoUnknown; |
| 610 | } |
| 611 | |
| 612 | NextProto SSLClientSocketImpl::GetNegotiatedProtocol() const { |
| 613 | return negotiated_protocol_; |
| 614 | } |
| 615 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 616 | bool SSLClientSocketImpl::GetSSLInfo(SSLInfo* ssl_info) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 617 | ssl_info->Reset(); |
davidben | c7e06c9 | 2017-03-07 18:54:11 | [diff] [blame] | 618 | if (!server_cert_) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 619 | return false; |
| 620 | |
| 621 | ssl_info->cert = server_cert_verify_result_.verified_cert; |
estark | 03d644f | 2015-06-13 00:11:32 | [diff] [blame] | 622 | ssl_info->unverified_cert = server_cert_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 623 | ssl_info->cert_status = server_cert_verify_result_.cert_status; |
| 624 | ssl_info->is_issued_by_known_root = |
| 625 | server_cert_verify_result_.is_issued_by_known_root; |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 626 | ssl_info->pkp_bypassed = pkp_bypassed_; |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 627 | ssl_info->public_key_hashes = server_cert_verify_result_.public_key_hashes; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 628 | ssl_info->client_cert_sent = |
| 629 | ssl_config_.send_client_cert && ssl_config_.client_cert.get(); |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 630 | ssl_info->pinning_failure_log = pinning_failure_log_; |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 631 | ssl_info->ocsp_result = server_cert_verify_result_.ocsp_result; |
Carlos IL | 8113338 | 2017-12-06 17:18:45 | [diff] [blame] | 632 | ssl_info->is_fatal_cert_error = is_fatal_cert_error_; |
estark | 723b5eeb | 2016-02-18 21:01:12 | [diff] [blame] | 633 | AddCTInfoToSSLInfo(ssl_info); |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 634 | |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 635 | const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_.get()); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 636 | CHECK(cipher); |
davidben | 3b00e40 | 2016-09-20 14:31:06 | [diff] [blame] | 637 | // Historically, the "group" was known as "curve". |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 638 | ssl_info->key_exchange_group = SSL_get_curve_id(ssl_.get()); |
David Benjamin | e7e073ef | 2018-10-25 01:26:06 | [diff] [blame] | 639 | ssl_info->peer_signature_algorithm = |
| 640 | SSL_get_peer_signature_algorithm(ssl_.get()); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 641 | |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 642 | SSLConnectionStatusSetCipherSuite( |
| 643 | static_cast<uint16_t>(SSL_CIPHER_get_id(cipher)), |
| 644 | &ssl_info->connection_status); |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 645 | SSLConnectionStatusSetVersion(GetNetSSLVersion(ssl_.get()), |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 646 | &ssl_info->connection_status); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 647 | |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 648 | ssl_info->handshake_type = SSL_session_reused(ssl_.get()) |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 649 | ? SSLInfo::HANDSHAKE_RESUME |
| 650 | : SSLInfo::HANDSHAKE_FULL; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 651 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 652 | return true; |
| 653 | } |
| 654 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 655 | void SSLClientSocketImpl::GetConnectionAttempts(ConnectionAttempts* out) const { |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 656 | out->clear(); |
| 657 | } |
| 658 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 659 | int64_t SSLClientSocketImpl::GetTotalReceivedBytes() const { |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 660 | return transport_->socket()->GetTotalReceivedBytes(); |
| 661 | } |
| 662 | |
xunjieli | 998d247 | 2017-01-12 01:12:28 | [diff] [blame] | 663 | void SSLClientSocketImpl::DumpMemoryStats(SocketMemoryStats* stats) const { |
| 664 | if (transport_adapter_) |
| 665 | stats->buffer_size = transport_adapter_->GetAllocationSize(); |
davidben | a35b40c3 | 2017-03-09 17:33:45 | [diff] [blame] | 666 | const STACK_OF(CRYPTO_BUFFER)* server_cert_chain = |
| 667 | SSL_get0_peer_certificates(ssl_.get()); |
davidben | c7e06c9 | 2017-03-07 18:54:11 | [diff] [blame] | 668 | if (server_cert_chain) { |
David Benjamin | 8a4bc32b | 2018-03-30 15:24:53 | [diff] [blame] | 669 | for (const CRYPTO_BUFFER* cert : server_cert_chain) { |
davidben | a35b40c3 | 2017-03-09 17:33:45 | [diff] [blame] | 670 | stats->cert_size += CRYPTO_BUFFER_len(cert); |
xunjieli | 9f8c5fb5 | 2016-12-07 22:59:33 | [diff] [blame] | 671 | } |
davidben | a35b40c3 | 2017-03-09 17:33:45 | [diff] [blame] | 672 | stats->cert_count = sk_CRYPTO_BUFFER_num(server_cert_chain); |
xunjieli | 9f8c5fb5 | 2016-12-07 22:59:33 | [diff] [blame] | 673 | } |
xunjieli | ffe62df6 | 2017-02-23 18:22:41 | [diff] [blame] | 674 | stats->total_size = stats->buffer_size + stats->cert_size; |
xunjieli | 9f8c5fb5 | 2016-12-07 22:59:33 | [diff] [blame] | 675 | } |
| 676 | |
Bence Béky | dae8af5f | 2018-04-13 08:53:17 | [diff] [blame] | 677 | void SSLClientSocketImpl::GetSSLCertRequestInfo( |
| 678 | SSLCertRequestInfo* cert_request_info) const { |
| 679 | if (!ssl_) { |
| 680 | NOTREACHED(); |
| 681 | return; |
| 682 | } |
| 683 | |
| 684 | cert_request_info->host_and_port = host_and_port_; |
| 685 | |
| 686 | cert_request_info->cert_authorities.clear(); |
| 687 | const STACK_OF(CRYPTO_BUFFER)* authorities = |
| 688 | SSL_get0_server_requested_CAs(ssl_.get()); |
| 689 | for (const CRYPTO_BUFFER* ca_name : authorities) { |
| 690 | cert_request_info->cert_authorities.push_back( |
| 691 | std::string(reinterpret_cast<const char*>(CRYPTO_BUFFER_data(ca_name)), |
| 692 | CRYPTO_BUFFER_len(ca_name))); |
| 693 | } |
| 694 | |
| 695 | cert_request_info->cert_key_types.clear(); |
| 696 | const uint8_t* client_cert_types; |
| 697 | size_t num_client_cert_types = |
| 698 | SSL_get0_certificate_types(ssl_.get(), &client_cert_types); |
| 699 | for (size_t i = 0; i < num_client_cert_types; i++) { |
| 700 | cert_request_info->cert_key_types.push_back( |
| 701 | static_cast<SSLClientCertType>(client_cert_types[i])); |
| 702 | } |
| 703 | } |
| 704 | |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 705 | void SSLClientSocketImpl::ApplySocketTag(const SocketTag& tag) { |
| 706 | return transport_->socket()->ApplySocketTag(tag); |
| 707 | } |
| 708 | |
xunjieli | 9f8c5fb5 | 2016-12-07 22:59:33 | [diff] [blame] | 709 | // static |
| 710 | void SSLClientSocketImpl::DumpSSLClientSessionMemoryStats( |
| 711 | base::trace_event::ProcessMemoryDump* pmd) { |
| 712 | SSLContext::GetInstance()->session_cache()->DumpMemoryStats(pmd); |
| 713 | } |
| 714 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 715 | int SSLClientSocketImpl::Read(IOBuffer* buf, |
| 716 | int buf_len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 717 | CompletionOnceCallback callback) { |
| 718 | int rv = ReadIfReady(buf, buf_len, std::move(callback)); |
xunjieli | 321a96f3 | 2017-03-07 19:42:17 | [diff] [blame] | 719 | if (rv == ERR_IO_PENDING) { |
| 720 | user_read_buf_ = buf; |
| 721 | user_read_buf_len_ = buf_len; |
| 722 | } |
| 723 | return rv; |
| 724 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 725 | |
xunjieli | 321a96f3 | 2017-03-07 19:42:17 | [diff] [blame] | 726 | int SSLClientSocketImpl::ReadIfReady(IOBuffer* buf, |
| 727 | int buf_len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 728 | CompletionOnceCallback callback) { |
xunjieli | 321a96f3 | 2017-03-07 19:42:17 | [diff] [blame] | 729 | int rv = DoPayloadRead(buf, buf_len); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 730 | |
| 731 | if (rv == ERR_IO_PENDING) { |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 732 | user_read_callback_ = std::move(callback); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 733 | } else { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 734 | if (rv > 0) |
| 735 | was_ever_used_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 736 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 737 | return rv; |
| 738 | } |
| 739 | |
Helen Li | 5f3d96a | 2018-08-10 20:37:24 | [diff] [blame] | 740 | int SSLClientSocketImpl::CancelReadIfReady() { |
| 741 | int result = transport_->socket()->CancelReadIfReady(); |
| 742 | // Cancel |user_read_callback_|, because caller does not expect the callback |
| 743 | // to be invoked after they have canceled the ReadIfReady. |
| 744 | user_read_callback_.Reset(); |
| 745 | return result; |
| 746 | } |
| 747 | |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 748 | int SSLClientSocketImpl::Write( |
| 749 | IOBuffer* buf, |
| 750 | int buf_len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 751 | CompletionOnceCallback callback, |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 752 | const NetworkTrafficAnnotationTag& traffic_annotation) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 753 | user_write_buf_ = buf; |
| 754 | user_write_buf_len_ = buf_len; |
| 755 | |
davidben | 3418e81f | 2016-10-19 00:09:45 | [diff] [blame] | 756 | int rv = DoPayloadWrite(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 757 | |
| 758 | if (rv == ERR_IO_PENDING) { |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 759 | user_write_callback_ = std::move(callback); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 760 | } else { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 761 | if (rv > 0) |
| 762 | was_ever_used_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 763 | user_write_buf_ = NULL; |
| 764 | user_write_buf_len_ = 0; |
| 765 | } |
| 766 | |
| 767 | return rv; |
| 768 | } |
| 769 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 770 | int SSLClientSocketImpl::SetReceiveBufferSize(int32_t size) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 771 | return transport_->socket()->SetReceiveBufferSize(size); |
| 772 | } |
| 773 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 774 | int SSLClientSocketImpl::SetSendBufferSize(int32_t size) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 775 | return transport_->socket()->SetSendBufferSize(size); |
| 776 | } |
| 777 | |
davidben | 3418e81f | 2016-10-19 00:09:45 | [diff] [blame] | 778 | void SSLClientSocketImpl::OnReadReady() { |
| 779 | // During a renegotiation, either Read or Write calls may be blocked on a |
| 780 | // transport read. |
| 781 | RetryAllOperations(); |
| 782 | } |
| 783 | |
| 784 | void SSLClientSocketImpl::OnWriteReady() { |
| 785 | // During a renegotiation, either Read or Write calls may be blocked on a |
| 786 | // transport read. |
| 787 | RetryAllOperations(); |
| 788 | } |
| 789 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 790 | int SSLClientSocketImpl::Init() { |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 791 | DCHECK(!ssl_); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 792 | |
[email protected] | b29af7d | 2010-12-14 11:52:47 | [diff] [blame] | 793 | SSLContext* context = SSLContext::GetInstance(); |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 794 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 795 | |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 796 | ssl_.reset(SSL_new(context->ssl_ctx())); |
| 797 | if (!ssl_ || !context->SetClientSocketForSSL(ssl_.get(), this)) |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 798 | return ERR_UNEXPECTED; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 799 | |
davidben | 9bc0466f | 2015-06-16 22:21:27 | [diff] [blame] | 800 | // SNI should only contain valid DNS hostnames, not IP addresses (see RFC |
| 801 | // 6066, Section 3). |
| 802 | // |
| 803 | // TODO(rsleevi): Should this code allow hostnames that violate the LDH rule? |
| 804 | // See https://crbug.com/496472 and https://crbug.com/496468 for discussion. |
martijn | a2e83bd | 2016-03-18 13:10:45 | [diff] [blame] | 805 | IPAddress unused; |
| 806 | if (!unused.AssignFromIPLiteral(host_and_port_.host()) && |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 807 | !SSL_set_tlsext_host_name(ssl_.get(), host_and_port_.host().c_str())) { |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 808 | return ERR_UNEXPECTED; |
davidben | 9bc0466f | 2015-06-16 22:21:27 | [diff] [blame] | 809 | } |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 810 | |
David Benjamin | b3840f4 | 2017-08-03 15:50:16 | [diff] [blame] | 811 | if (!ssl_session_cache_shard_.empty()) { |
Steven Valdez | e6112f4 | 2017-10-05 22:20:12 | [diff] [blame] | 812 | bssl::UniquePtr<SSL_SESSION> session = |
| 813 | context->session_cache()->Lookup(GetSessionCacheKey()); |
David Benjamin | b3840f4 | 2017-08-03 15:50:16 | [diff] [blame] | 814 | if (session) |
| 815 | SSL_set_session(ssl_.get(), session.get()); |
| 816 | } |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 817 | |
Helen Li | f3aa962 | 2018-05-24 00:18:07 | [diff] [blame] | 818 | transport_adapter_.reset( |
| 819 | new SocketBIOAdapter(transport_->socket(), kDefaultOpenSSLBufferSize, |
| 820 | kDefaultOpenSSLBufferSize, this)); |
davidben | 3418e81f | 2016-10-19 00:09:45 | [diff] [blame] | 821 | BIO* transport_bio = transport_adapter_->bio(); |
mmenke | 1beda3d | 2016-07-22 03:33:45 | [diff] [blame] | 822 | |
davidben | 3418e81f | 2016-10-19 00:09:45 | [diff] [blame] | 823 | BIO_up_ref(transport_bio); // SSL_set0_rbio takes ownership. |
| 824 | SSL_set0_rbio(ssl_.get(), transport_bio); |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 825 | |
davidben | 3418e81f | 2016-10-19 00:09:45 | [diff] [blame] | 826 | BIO_up_ref(transport_bio); // SSL_set0_wbio takes ownership. |
| 827 | SSL_set0_wbio(ssl_.get(), transport_bio); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 828 | |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 829 | DCHECK_LT(SSL3_VERSION, ssl_config_.version_min); |
| 830 | DCHECK_LT(SSL3_VERSION, ssl_config_.version_max); |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 831 | if (!SSL_set_min_proto_version(ssl_.get(), ssl_config_.version_min) || |
| 832 | !SSL_set_max_proto_version(ssl_.get(), ssl_config_.version_max)) { |
davidben | 952bdf2 | 2016-09-21 23:42:16 | [diff] [blame] | 833 | return ERR_UNEXPECTED; |
| 834 | } |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 835 | |
Steven Valdez | 6af02df | 2018-07-15 21:52:33 | [diff] [blame] | 836 | SSL_set_early_data_enabled(ssl_.get(), ssl_config_.early_data_enabled); |
| 837 | |
Steven Valdez | 50a3d2c9 | 2018-11-14 19:45:59 | [diff] [blame] | 838 | if (!base::FeatureList::IsEnabled(features::kEnforceTLS13Downgrade) || |
| 839 | base::GetFieldTrialParamByFeatureAsBool(features::kEnforceTLS13Downgrade, |
| 840 | "known_roots_only", false)) { |
David Benjamin | 570460e | 2018-10-16 06:01:29 | [diff] [blame] | 841 | SSL_set_ignore_tls13_downgrade(ssl_.get(), 1); |
| 842 | } |
| 843 | |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 844 | // OpenSSL defaults some options to on, others to off. To avoid ambiguity, |
| 845 | // set everything we care about to an absolute value. |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 846 | SslSetClearMask options; |
[email protected] | d0f0049 | 2012-08-03 22:35:13 | [diff] [blame] | 847 | options.ConfigureFlag(SSL_OP_NO_COMPRESSION, true); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 848 | |
| 849 | // TODO(joth): Set this conditionally, see http://crbug.com/55410 |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 850 | options.ConfigureFlag(SSL_OP_LEGACY_SERVER_CONNECT, true); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 851 | |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 852 | SSL_set_options(ssl_.get(), options.set_mask); |
| 853 | SSL_clear_options(ssl_.get(), options.clear_mask); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 854 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 855 | // Same as above, this time for the SSL mode. |
| 856 | SslSetClearMask mode; |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 857 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 858 | mode.ConfigureFlag(SSL_MODE_RELEASE_BUFFERS, true); |
isherman | e5c05e1 | 2014-09-09 20:32:15 | [diff] [blame] | 859 | mode.ConfigureFlag(SSL_MODE_CBC_RECORD_SPLITTING, true); |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 860 | |
davidben | 818d93b | 2015-02-19 22:27:32 | [diff] [blame] | 861 | mode.ConfigureFlag(SSL_MODE_ENABLE_FALSE_START, |
[email protected] | b788de0 | 2014-04-23 18:06:07 | [diff] [blame] | 862 | ssl_config_.false_start_enabled); |
| 863 | |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 864 | SSL_set_mode(ssl_.get(), mode.set_mask); |
| 865 | SSL_clear_mode(ssl_.get(), mode.clear_mask); |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 866 | |
Steven Valdez | 99a85a6 | 2018-05-03 18:13:45 | [diff] [blame] | 867 | // Use BoringSSL defaults, but disable HMAC-SHA1 ciphers in ECDSA. These are |
| 868 | // the remaining CBC-mode ECDSA ciphers. |
| 869 | std::string command("ALL::!aPSK:!ECDSA+SHA1"); |
davidben | 9b4a9b9c | 2015-10-12 18:46:51 | [diff] [blame] | 870 | |
| 871 | if (ssl_config_.require_ecdhe) |
davidben | 1863716b | 2017-05-03 20:06:20 | [diff] [blame] | 872 | command.append(":!kRSA"); |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 873 | |
davidben | 9b4a9b9c | 2015-10-12 18:46:51 | [diff] [blame] | 874 | // Remove any disabled ciphers. |
| 875 | for (uint16_t id : ssl_config_.disabled_cipher_suites) { |
| 876 | const SSL_CIPHER* cipher = SSL_get_cipher_by_value(id); |
| 877 | if (cipher) { |
| 878 | command.append(":!"); |
| 879 | command.append(SSL_CIPHER_get_name(cipher)); |
| 880 | } |
| 881 | } |
| 882 | |
davidben | 1863716b | 2017-05-03 20:06:20 | [diff] [blame] | 883 | if (!SSL_set_strict_cipher_list(ssl_.get(), command.c_str())) { |
| 884 | LOG(ERROR) << "SSL_set_cipher_list('" << command << "') failed"; |
| 885 | return ERR_UNEXPECTED; |
| 886 | } |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 887 | |
bnc | 1f29537 | 2015-10-21 23:24:22 | [diff] [blame] | 888 | if (!ssl_config_.alpn_protos.empty()) { |
bnc | 988e68d | 2016-06-27 14:03:21 | [diff] [blame] | 889 | std::vector<uint8_t> wire_protos = |
| 890 | SerializeNextProtos(ssl_config_.alpn_protos); |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 891 | SSL_set_alpn_protos(ssl_.get(), |
| 892 | wire_protos.empty() ? NULL : &wire_protos[0], |
[email protected] | abc44b75 | 2014-07-30 03:52:15 | [diff] [blame] | 893 | wire_protos.size()); |
| 894 | } |
| 895 | |
Ryan Sleevi | d1a894e | 2018-04-03 20:24:07 | [diff] [blame] | 896 | SSL_enable_signed_cert_timestamps(ssl_.get()); |
| 897 | SSL_enable_ocsp_stapling(ssl_.get()); |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 898 | |
davidben | 971a681a | 2017-02-16 18:57:46 | [diff] [blame] | 899 | // Configure BoringSSL to allow renegotiations. Once the initial handshake |
| 900 | // completes, if renegotiations are not allowed, the default reject value will |
| 901 | // be restored. This is done in this order to permit a BoringSSL |
| 902 | // optimization. See https://crbug.com/boringssl/123. |
| 903 | SSL_set_renegotiate_mode(ssl_.get(), ssl_renegotiate_freely); |
| 904 | |
David Benjamin | 8373dea | 2018-05-07 15:39:10 | [diff] [blame] | 905 | SSL_set_shed_handshake_config(ssl_.get(), 1); |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 906 | return OK; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 907 | } |
| 908 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 909 | void SSLClientSocketImpl::DoReadCallback(int rv) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 910 | // Since Run may result in Read being called, clear |user_read_callback_| |
| 911 | // up front. |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 912 | if (rv > 0) |
| 913 | was_ever_used_ = true; |
xunjieli | 321a96f3 | 2017-03-07 19:42:17 | [diff] [blame] | 914 | user_read_buf_ = nullptr; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 915 | user_read_buf_len_ = 0; |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 916 | std::move(user_read_callback_).Run(rv); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 917 | } |
| 918 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 919 | void SSLClientSocketImpl::DoWriteCallback(int rv) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 920 | // Since Run may result in Write being called, clear |user_write_callback_| |
| 921 | // up front. |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 922 | if (rv > 0) |
| 923 | was_ever_used_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 924 | user_write_buf_ = NULL; |
| 925 | user_write_buf_len_ = 0; |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 926 | std::move(user_write_callback_).Run(rv); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 927 | } |
| 928 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 929 | int SSLClientSocketImpl::DoHandshake() { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 930 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
vadimt | 5a24328 | 2014-12-24 00:26:16 | [diff] [blame] | 931 | |
David Benjamin | 5f98efe | 2018-04-12 07:32:41 | [diff] [blame] | 932 | int rv = SSL_do_handshake(ssl_.get()); |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 933 | int net_error = OK; |
| 934 | if (rv <= 0) { |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 935 | int ssl_error = SSL_get_error(ssl_.get(), rv); |
davidben | ced4aa9b | 2015-05-12 21:22:35 | [diff] [blame] | 936 | if (ssl_error == SSL_ERROR_WANT_X509_LOOKUP && |
| 937 | !ssl_config_.send_client_cert) { |
| 938 | return ERR_SSL_CLIENT_AUTH_CERT_NEEDED; |
| 939 | } |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 940 | if (ssl_error == SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) { |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 941 | DCHECK(ssl_config_.client_private_key); |
Oscar Johansson | d49464e | 2018-07-02 09:35:45 | [diff] [blame] | 942 | DCHECK_NE(kSSLClientSocketNoPendingResult, signature_result_); |
rsleevi | adbd498 | 2016-06-13 22:10:27 | [diff] [blame] | 943 | next_handshake_state_ = STATE_HANDSHAKE; |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 944 | return ERR_IO_PENDING; |
| 945 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 946 | |
davidben | a4409c6 | 2014-08-27 17:05:51 | [diff] [blame] | 947 | OpenSSLErrorInfo error_info; |
davidben | fe132d9 | 2016-09-27 18:07:21 | [diff] [blame] | 948 | net_error = MapLastOpenSSLError(ssl_error, err_tracer, &error_info); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 949 | if (net_error == ERR_IO_PENDING) { |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 950 | // If not done, stay in this state |
rsleevi | adbd498 | 2016-06-13 22:10:27 | [diff] [blame] | 951 | next_handshake_state_ = STATE_HANDSHAKE; |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 952 | return ERR_IO_PENDING; |
| 953 | } |
| 954 | |
| 955 | LOG(ERROR) << "handshake failed; returned " << rv << ", SSL error code " |
| 956 | << ssl_error << ", net_error " << net_error; |
| 957 | net_log_.AddEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 958 | NetLogEventType::SSL_HANDSHAKE_ERROR, |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 959 | CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info)); |
| 960 | } |
| 961 | |
rsleevi | adbd498 | 2016-06-13 22:10:27 | [diff] [blame] | 962 | next_handshake_state_ = STATE_HANDSHAKE_COMPLETE; |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 963 | return net_error; |
| 964 | } |
| 965 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 966 | int SSLClientSocketImpl::DoHandshakeComplete(int result) { |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 967 | if (result < 0) |
| 968 | return result; |
| 969 | |
Steven Valdez | 6af02df | 2018-07-15 21:52:33 | [diff] [blame] | 970 | if (in_confirm_handshake_) { |
| 971 | next_handshake_state_ = STATE_NONE; |
| 972 | return OK; |
| 973 | } |
| 974 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 975 | if (ssl_config_.version_interference_probe) { |
| 976 | DCHECK_LT(ssl_config_.version_max, TLS1_3_VERSION); |
| 977 | return ERR_SSL_VERSION_INTERFERENCE; |
| 978 | } |
| 979 | |
David Benjamin | b3840f4 | 2017-08-03 15:50:16 | [diff] [blame] | 980 | if (!ssl_session_cache_shard_.empty()) { |
| 981 | SSLContext::GetInstance()->session_cache()->ResetLookupCount( |
| 982 | GetSessionCacheKey()); |
| 983 | } |
| 984 | |
bnc | ce6ea24 | 2016-09-15 20:22:32 | [diff] [blame] | 985 | const uint8_t* alpn_proto = NULL; |
| 986 | unsigned alpn_len = 0; |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 987 | SSL_get0_alpn_selected(ssl_.get(), &alpn_proto, &alpn_len); |
bnc | ce6ea24 | 2016-09-15 20:22:32 | [diff] [blame] | 988 | if (alpn_len > 0) { |
| 989 | base::StringPiece proto(reinterpret_cast<const char*>(alpn_proto), |
| 990 | alpn_len); |
| 991 | negotiated_protocol_ = NextProtoFromString(proto); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 992 | } |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 993 | |
bnc | bd442c2 | 2016-09-14 20:49:16 | [diff] [blame] | 994 | RecordNegotiatedProtocol(); |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 995 | |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 996 | const uint8_t* ocsp_response_raw; |
| 997 | size_t ocsp_response_len; |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 998 | SSL_get0_ocsp_response(ssl_.get(), &ocsp_response_raw, &ocsp_response_len); |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 999 | set_stapled_ocsp_response_received(ocsp_response_len != 0); |
| 1000 | UMA_HISTOGRAM_BOOLEAN("Net.OCSPResponseStapled", ocsp_response_len != 0); |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1001 | |
| 1002 | const uint8_t* sct_list; |
| 1003 | size_t sct_list_len; |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 1004 | SSL_get0_signed_cert_timestamp_list(ssl_.get(), &sct_list, &sct_list_len); |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1005 | set_signed_cert_timestamps_received(sct_list_len != 0); |
| 1006 | |
davidben | 971a681a | 2017-02-16 18:57:46 | [diff] [blame] | 1007 | if (!IsRenegotiationAllowed()) |
| 1008 | SSL_set_renegotiate_mode(ssl_.get(), ssl_renegotiate_never); |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1009 | |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 1010 | uint16_t signature_algorithm = SSL_get_peer_signature_algorithm(ssl_.get()); |
davidben | 0653c8d | 2016-07-08 02:16:17 | [diff] [blame] | 1011 | if (signature_algorithm != 0) { |
Ilya Sherman | 0eb3980 | 2017-12-08 20:58:18 | [diff] [blame] | 1012 | base::UmaHistogramSparse("Net.SSLSignatureAlgorithm", signature_algorithm); |
davidben | 4fe4f98 | 2015-11-11 22:00:12 | [diff] [blame] | 1013 | } |
| 1014 | |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1015 | // Verify the certificate. |
rsleevi | adbd498 | 2016-06-13 22:10:27 | [diff] [blame] | 1016 | next_handshake_state_ = STATE_VERIFY_CERT; |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1017 | return OK; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1018 | } |
| 1019 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1020 | int SSLClientSocketImpl::DoVerifyCert(int result) { |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 1021 | DCHECK(start_cert_verification_time_.is_null()); |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1022 | |
David Benjamin | b8ab385 | 2017-08-04 00:17:32 | [diff] [blame] | 1023 | server_cert_ = x509_util::CreateX509CertificateFromBuffers( |
| 1024 | SSL_get0_peer_certificates(ssl_.get())); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1025 | |
Matt Mueller | ba33e86 | 2017-09-28 20:15:52 | [diff] [blame] | 1026 | // OpenSSL decoded the certificate, but the X509Certificate implementation |
| 1027 | // could not. This is treated as a fatal SSL-level protocol error rather than |
| 1028 | // a certificate error. See https://crbug.com/91341. |
rsleevi | 74e9974 | 2016-09-13 20:35:25 | [diff] [blame] | 1029 | if (!server_cert_) |
davidben | c6435a7 | 2015-08-17 18:28:52 | [diff] [blame] | 1030 | return ERR_SSL_SERVER_CERT_BAD_FORMAT; |
| 1031 | |
davidben | c7e06c9 | 2017-03-07 18:54:11 | [diff] [blame] | 1032 | net_log_.AddEvent(NetLogEventType::SSL_CERTIFICATES_RECEIVED, |
| 1033 | base::Bind(&NetLogX509CertificateCallback, |
| 1034 | base::Unretained(server_cert_.get()))); |
| 1035 | |
| 1036 | next_handshake_state_ = STATE_VERIFY_CERT_COMPLETE; |
| 1037 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1038 | // If the certificate is bad and has been previously accepted, use |
| 1039 | // the previous status and bypass the error. |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1040 | CertStatus cert_status; |
rsleevi | 74e9974 | 2016-09-13 20:35:25 | [diff] [blame] | 1041 | if (ssl_config_.IsAllowedBadCert(server_cert_.get(), &cert_status)) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1042 | server_cert_verify_result_.Reset(); |
| 1043 | server_cert_verify_result_.cert_status = cert_status; |
| 1044 | server_cert_verify_result_.verified_cert = server_cert_; |
| 1045 | return OK; |
| 1046 | } |
| 1047 | |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 1048 | start_cert_verification_time_ = base::TimeTicks::Now(); |
| 1049 | |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 1050 | const uint8_t* ocsp_response_raw; |
| 1051 | size_t ocsp_response_len; |
| 1052 | SSL_get0_ocsp_response(ssl_.get(), &ocsp_response_raw, &ocsp_response_len); |
| 1053 | base::StringPiece ocsp_response( |
| 1054 | reinterpret_cast<const char*>(ocsp_response_raw), ocsp_response_len); |
| 1055 | |
eroman | 7f9236a | 2015-05-11 21:23:43 | [diff] [blame] | 1056 | return cert_verifier_->Verify( |
rsleevi | 06bd7855 | 2016-06-08 22:34:46 | [diff] [blame] | 1057 | CertVerifier::RequestParams(server_cert_, host_and_port_.host(), |
| 1058 | ssl_config_.GetCertVerifyFlags(), |
Ryan Sleevi | 73bdca6 | 2018-08-22 16:34:23 | [diff] [blame] | 1059 | ocsp_response.as_string()), |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 1060 | &server_cert_verify_result_, |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1061 | base::Bind(&SSLClientSocketImpl::OnHandshakeIOComplete, |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1062 | base::Unretained(this)), |
eroman | 7f9236a | 2015-05-11 21:23:43 | [diff] [blame] | 1063 | &cert_verifier_request_, net_log_); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1064 | } |
| 1065 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1066 | int SSLClientSocketImpl::DoVerifyCertComplete(int result) { |
eroman | 7f9236a | 2015-05-11 21:23:43 | [diff] [blame] | 1067 | cert_verifier_request_.reset(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1068 | |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 1069 | if (!start_cert_verification_time_.is_null()) { |
| 1070 | base::TimeDelta verify_time = |
| 1071 | base::TimeTicks::Now() - start_cert_verification_time_; |
| 1072 | if (result == OK) { |
| 1073 | UMA_HISTOGRAM_TIMES("Net.SSLCertVerificationTime", verify_time); |
| 1074 | } else { |
| 1075 | UMA_HISTOGRAM_TIMES("Net.SSLCertVerificationTimeError", verify_time); |
| 1076 | } |
| 1077 | } |
| 1078 | |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1079 | // If the connection was good, check HPKP and CT status simultaneously, |
| 1080 | // 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] | 1081 | const CertStatus cert_status = server_cert_verify_result_.cert_status; |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1082 | if ((result == OK || |
dadrian | 8f894665 | 2016-06-21 23:48:31 | [diff] [blame] | 1083 | (IsCertificateError(result) && IsCertStatusMinorError(cert_status)))) { |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1084 | int ct_result = VerifyCT(); |
dadrian | 8f894665 | 2016-06-21 23:48:31 | [diff] [blame] | 1085 | TransportSecurityState::PKPStatus pin_validity = |
| 1086 | transport_security_state_->CheckPublicKeyPins( |
| 1087 | host_and_port_, server_cert_verify_result_.is_issued_by_known_root, |
| 1088 | server_cert_verify_result_.public_key_hashes, server_cert_.get(), |
| 1089 | server_cert_verify_result_.verified_cert.get(), |
| 1090 | TransportSecurityState::ENABLE_PIN_REPORTS, &pinning_failure_log_); |
| 1091 | switch (pin_validity) { |
| 1092 | case TransportSecurityState::PKPStatus::VIOLATED: |
| 1093 | server_cert_verify_result_.cert_status |= |
| 1094 | CERT_STATUS_PINNED_KEY_MISSING; |
| 1095 | result = ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN; |
| 1096 | break; |
| 1097 | case TransportSecurityState::PKPStatus::BYPASSED: |
| 1098 | pkp_bypassed_ = true; |
Nico Weber | 63e0376 | 2018-01-26 17:55:14 | [diff] [blame] | 1099 | FALLTHROUGH; |
dadrian | 8f894665 | 2016-06-21 23:48:31 | [diff] [blame] | 1100 | case TransportSecurityState::PKPStatus::OK: |
| 1101 | // Do nothing. |
| 1102 | break; |
rsleevi | 9545d34 | 2016-06-21 03:17:37 | [diff] [blame] | 1103 | } |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1104 | if (result != ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN && ct_result != OK) |
| 1105 | result = ct_result; |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 1106 | } |
| 1107 | |
Carlos IL | 8113338 | 2017-12-06 17:18:45 | [diff] [blame] | 1108 | is_fatal_cert_error_ = |
| 1109 | IsCertStatusError(cert_status) && !IsCertStatusMinorError(cert_status) && |
| 1110 | transport_security_state_->ShouldSSLErrorsBeFatal(host_and_port_.host()); |
| 1111 | |
Jesse Selover | daf8790 | 2018-12-03 20:44:30 | [diff] [blame] | 1112 | if (IsCertificateError(result) && ssl_config_.ignore_certificate_errors) { |
| 1113 | result = OK; |
| 1114 | } |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 1115 | |
Jesse Selover | daf8790 | 2018-12-03 20:44:30 | [diff] [blame] | 1116 | if (result < 0) { |
| 1117 | return result; |
| 1118 | } |
| 1119 | |
| 1120 | DCHECK(!certificate_verified_); |
| 1121 | certificate_verified_ = true; |
| 1122 | MaybeCacheSession(); |
| 1123 | SSLInfo ssl_info; |
| 1124 | bool ok = GetSSLInfo(&ssl_info); |
| 1125 | DCHECK(ok); |
| 1126 | |
| 1127 | // See how feasible enforcing RSA key usage would be. See |
| 1128 | // https://crbug.com/795089. |
| 1129 | RSAKeyUsage rsa_key_usage = |
| 1130 | CheckRSAKeyUsage(server_cert_.get(), SSL_get_current_cipher(ssl_.get())); |
| 1131 | if (rsa_key_usage != RSAKeyUsage::kNotRSA) { |
| 1132 | if (server_cert_verify_result_.is_issued_by_known_root) { |
| 1133 | UMA_HISTOGRAM_ENUMERATION("Net.SSLRSAKeyUsage.KnownRoot", rsa_key_usage, |
| 1134 | static_cast<int>(RSAKeyUsage::kLastValue) + 1); |
| 1135 | } else { |
| 1136 | UMA_HISTOGRAM_ENUMERATION("Net.SSLRSAKeyUsage.UnknownRoot", rsa_key_usage, |
| 1137 | static_cast<int>(RSAKeyUsage::kLastValue) + 1); |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | bool enforce_tls13_downgrade_known_roots_only = |
| 1142 | base::GetFieldTrialParamByFeatureAsBool(features::kEnforceTLS13Downgrade, |
| 1143 | "known_roots_only", false); |
| 1144 | if (!base::FeatureList::IsEnabled(features::kEnforceTLS13Downgrade) || |
| 1145 | enforce_tls13_downgrade_known_roots_only) { |
| 1146 | // Record metrics on the TLS 1.3 anti-downgrade mechanism. This is only |
| 1147 | // recorded when enforcement is disabled. (When enforcement is enabled, |
| 1148 | // the connection will fail with ERR_TLS13_DOWNGRADE_DETECTED.) See |
| 1149 | // https://crbug.com/boringssl/226. |
| 1150 | // |
| 1151 | // Record metrics for both servers overall and the TLS 1.3 experiment |
| 1152 | // set. These metrics are only useful on TLS 1.3 servers, so the latter |
| 1153 | // is more precise, but there is a large enough TLS 1.3 deployment that |
| 1154 | // the overall numbers may be more robust. In particular, the |
| 1155 | // DowngradeType metrics do not need to be filtered. |
| 1156 | bool is_downgrade = !!SSL_is_tls13_downgrade(ssl_.get()); |
| 1157 | UMA_HISTOGRAM_BOOLEAN("Net.SSLTLS13Downgrade", is_downgrade); |
| 1158 | bool is_tls13_experiment_host = |
| 1159 | IsTLS13ExperimentHost(host_and_port_.host()); |
| 1160 | if (is_tls13_experiment_host) { |
| 1161 | UMA_HISTOGRAM_BOOLEAN("Net.SSLTLS13DowngradeTLS13Experiment", |
| 1162 | is_downgrade); |
| 1163 | } |
| 1164 | |
| 1165 | if (is_downgrade) { |
| 1166 | // Record whether connections which hit the downgrade used known vs |
| 1167 | // unknown roots and which key exchange type. |
| 1168 | |
| 1169 | // This enum is persisted into histograms. Values may not be |
| 1170 | // renumbered. |
| 1171 | enum class DowngradeType { |
| 1172 | kKnownRootRSA = 0, |
| 1173 | kKnownRootECDHE = 1, |
| 1174 | kUnknownRootRSA = 2, |
| 1175 | kUnknownRootECDHE = 3, |
| 1176 | kMaxValue = kUnknownRootECDHE, |
| 1177 | }; |
| 1178 | |
| 1179 | DowngradeType type; |
| 1180 | int kx_nid = SSL_CIPHER_get_kx_nid(SSL_get_current_cipher(ssl_.get())); |
| 1181 | DCHECK(kx_nid == NID_kx_rsa || kx_nid == NID_kx_ecdhe); |
David Benjamin | f8ebd2b | 2017-12-15 19:22:41 | [diff] [blame] | 1182 | if (server_cert_verify_result_.is_issued_by_known_root) { |
Jesse Selover | daf8790 | 2018-12-03 20:44:30 | [diff] [blame] | 1183 | type = kx_nid == NID_kx_rsa ? DowngradeType::kKnownRootRSA |
| 1184 | : DowngradeType::kKnownRootECDHE; |
David Benjamin | f8ebd2b | 2017-12-15 19:22:41 | [diff] [blame] | 1185 | } else { |
Jesse Selover | daf8790 | 2018-12-03 20:44:30 | [diff] [blame] | 1186 | type = kx_nid == NID_kx_rsa ? DowngradeType::kUnknownRootRSA |
| 1187 | : DowngradeType::kUnknownRootECDHE; |
| 1188 | } |
| 1189 | UMA_HISTOGRAM_ENUMERATION("Net.SSLTLS13DowngradeType", type); |
| 1190 | if (is_tls13_experiment_host) { |
| 1191 | UMA_HISTOGRAM_ENUMERATION("Net.SSLTLS13DowngradeTypeTLS13Experiment", |
| 1192 | type); |
David Benjamin | f8ebd2b | 2017-12-15 19:22:41 | [diff] [blame] | 1193 | } |
David Benjamin | 570460e | 2018-10-16 06:01:29 | [diff] [blame] | 1194 | |
Steven Valdez | bf059c75 | 2018-12-12 16:32:53 | [diff] [blame^] | 1195 | if (enforce_tls13_downgrade_known_roots_only && |
| 1196 | server_cert_verify_result_.is_issued_by_known_root) { |
| 1197 | // Exit DoHandshakeLoop and return the result to the caller to |
| 1198 | // Connect. |
| 1199 | DCHECK_EQ(STATE_NONE, next_handshake_state_); |
| 1200 | return ERR_TLS13_DOWNGRADE_DETECTED; |
| 1201 | } |
David Benjamin | 570460e | 2018-10-16 06:01:29 | [diff] [blame] | 1202 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1203 | } |
| 1204 | |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 1205 | completed_connect_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1206 | // Exit DoHandshakeLoop and return the result to the caller to Connect. |
| 1207 | DCHECK_EQ(STATE_NONE, next_handshake_state_); |
Jesse Selover | daf8790 | 2018-12-03 20:44:30 | [diff] [blame] | 1208 | return OK; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1209 | } |
| 1210 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1211 | void SSLClientSocketImpl::DoConnectCallback(int rv) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1212 | if (!user_connect_callback_.is_null()) { |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 1213 | std::move(user_connect_callback_).Run(rv > OK ? OK : rv); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1214 | } |
| 1215 | } |
| 1216 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1217 | void SSLClientSocketImpl::OnHandshakeIOComplete(int result) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1218 | int rv = DoHandshakeLoop(result); |
| 1219 | if (rv != ERR_IO_PENDING) { |
Steven Valdez | 6af02df | 2018-07-15 21:52:33 | [diff] [blame] | 1220 | if (in_confirm_handshake_) { |
| 1221 | in_confirm_handshake_ = false; |
| 1222 | net_log_.EndEvent(NetLogEventType::SSL_CONFIRM_HANDSHAKE); |
| 1223 | } else { |
| 1224 | LogConnectEndEvent(rv); |
| 1225 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1226 | DoConnectCallback(rv); |
| 1227 | } |
| 1228 | } |
| 1229 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1230 | int SSLClientSocketImpl::DoHandshakeLoop(int last_io_result) { |
Alexandr Ilin | 3312663 | 2018-11-14 14:48:17 | [diff] [blame] | 1231 | TRACE_EVENT0(NetTracingCategory(), "SSLClientSocketImpl::DoHandshakeLoop"); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1232 | int rv = last_io_result; |
| 1233 | do { |
| 1234 | // Default to STATE_NONE for next state. |
| 1235 | // (This is a quirk carried over from the windows |
| 1236 | // implementation. It makes reading the logs a bit harder.) |
| 1237 | // State handlers can and often do call GotoState just |
| 1238 | // to stay in the current state. |
| 1239 | State state = next_handshake_state_; |
rsleevi | adbd498 | 2016-06-13 22:10:27 | [diff] [blame] | 1240 | next_handshake_state_ = STATE_NONE; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1241 | switch (state) { |
| 1242 | case STATE_HANDSHAKE: |
| 1243 | rv = DoHandshake(); |
| 1244 | break; |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1245 | case STATE_HANDSHAKE_COMPLETE: |
| 1246 | rv = DoHandshakeComplete(rv); |
| 1247 | break; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1248 | case STATE_VERIFY_CERT: |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1249 | DCHECK_EQ(OK, rv); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1250 | rv = DoVerifyCert(rv); |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1251 | break; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1252 | case STATE_VERIFY_CERT_COMPLETE: |
| 1253 | rv = DoVerifyCertComplete(rv); |
| 1254 | break; |
| 1255 | case STATE_NONE: |
| 1256 | default: |
| 1257 | rv = ERR_UNEXPECTED; |
| 1258 | NOTREACHED() << "unexpected state" << state; |
| 1259 | break; |
| 1260 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1261 | } while (rv != ERR_IO_PENDING && next_handshake_state_ != STATE_NONE); |
| 1262 | return rv; |
| 1263 | } |
| 1264 | |
xunjieli | 321a96f3 | 2017-03-07 19:42:17 | [diff] [blame] | 1265 | int SSLClientSocketImpl::DoPayloadRead(IOBuffer* buf, int buf_len) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1266 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 1267 | |
xunjieli | 321a96f3 | 2017-03-07 19:42:17 | [diff] [blame] | 1268 | DCHECK_LT(0, buf_len); |
| 1269 | DCHECK(buf); |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1270 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1271 | int rv; |
Oscar Johansson | d49464e | 2018-07-02 09:35:45 | [diff] [blame] | 1272 | if (pending_read_error_ != kSSLClientSocketNoPendingResult) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1273 | rv = pending_read_error_; |
Oscar Johansson | d49464e | 2018-07-02 09:35:45 | [diff] [blame] | 1274 | pending_read_error_ = kSSLClientSocketNoPendingResult; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1275 | if (rv == 0) { |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1276 | net_log_.AddByteTransferEvent(NetLogEventType::SSL_SOCKET_BYTES_RECEIVED, |
xunjieli | 321a96f3 | 2017-03-07 19:42:17 | [diff] [blame] | 1277 | rv, buf->data()); |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1278 | } else { |
| 1279 | net_log_.AddEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1280 | NetLogEventType::SSL_READ_ERROR, |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1281 | CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_, |
| 1282 | pending_read_error_info_)); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1283 | } |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1284 | pending_read_ssl_error_ = SSL_ERROR_NONE; |
| 1285 | pending_read_error_info_ = OpenSSLErrorInfo(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1286 | return rv; |
| 1287 | } |
| 1288 | |
| 1289 | int total_bytes_read = 0; |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1290 | int ssl_ret; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1291 | do { |
xunjieli | 321a96f3 | 2017-03-07 19:42:17 | [diff] [blame] | 1292 | ssl_ret = SSL_read(ssl_.get(), buf->data() + total_bytes_read, |
| 1293 | buf_len - total_bytes_read); |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1294 | if (ssl_ret > 0) |
| 1295 | total_bytes_read += ssl_ret; |
davidben | 8ea6b17 | 2017-03-07 23:53:50 | [diff] [blame] | 1296 | // Continue processing records as long as there is more data available |
| 1297 | // synchronously. |
| 1298 | } while (total_bytes_read < buf_len && ssl_ret > 0 && |
| 1299 | transport_adapter_->HasPendingReadData()); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1300 | |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1301 | // Although only the final SSL_read call may have failed, the failure needs to |
| 1302 | // processed immediately, while the information still available in OpenSSL's |
| 1303 | // error queue. |
davidben | ced4aa9b | 2015-05-12 21:22:35 | [diff] [blame] | 1304 | if (ssl_ret <= 0) { |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 1305 | pending_read_ssl_error_ = SSL_get_error(ssl_.get(), ssl_ret); |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1306 | if (pending_read_ssl_error_ == SSL_ERROR_ZERO_RETURN) { |
| 1307 | pending_read_error_ = 0; |
davidben | ced4aa9b | 2015-05-12 21:22:35 | [diff] [blame] | 1308 | } else if (pending_read_ssl_error_ == SSL_ERROR_WANT_X509_LOOKUP && |
| 1309 | !ssl_config_.send_client_cert) { |
| 1310 | pending_read_error_ = ERR_SSL_CLIENT_AUTH_CERT_NEEDED; |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1311 | } else if (pending_read_ssl_error_ == |
| 1312 | SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) { |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 1313 | DCHECK(ssl_config_.client_private_key); |
Oscar Johansson | d49464e | 2018-07-02 09:35:45 | [diff] [blame] | 1314 | DCHECK_NE(kSSLClientSocketNoPendingResult, signature_result_); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1315 | pending_read_error_ = ERR_IO_PENDING; |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1316 | } else { |
davidben | fe132d9 | 2016-09-27 18:07:21 | [diff] [blame] | 1317 | pending_read_error_ = MapLastOpenSSLError( |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1318 | pending_read_ssl_error_, err_tracer, &pending_read_error_info_); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1319 | } |
| 1320 | |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1321 | // Many servers do not reliably send a close_notify alert when shutting down |
| 1322 | // a connection, and instead terminate the TCP connection. This is reported |
| 1323 | // as ERR_CONNECTION_CLOSED. Because of this, map the unclean shutdown to a |
| 1324 | // graceful EOF, instead of treating it as an error as it should be. |
| 1325 | if (pending_read_error_ == ERR_CONNECTION_CLOSED) |
| 1326 | pending_read_error_ = 0; |
| 1327 | } |
davidben | be6ce7ec | 2014-10-20 19:15:56 | [diff] [blame] | 1328 | |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1329 | if (total_bytes_read > 0) { |
| 1330 | // Return any bytes read to the caller. The error will be deferred to the |
| 1331 | // next call of DoPayloadRead. |
| 1332 | rv = total_bytes_read; |
davidben | be6ce7ec | 2014-10-20 19:15:56 | [diff] [blame] | 1333 | |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1334 | // Do not treat insufficient data as an error to return in the next call to |
| 1335 | // DoPayloadRead() - instead, let the call fall through to check SSL_read() |
davidben | 3418e81f | 2016-10-19 00:09:45 | [diff] [blame] | 1336 | // again. The transport may have data available by then. |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1337 | if (pending_read_error_ == ERR_IO_PENDING) |
Oscar Johansson | d49464e | 2018-07-02 09:35:45 | [diff] [blame] | 1338 | pending_read_error_ = kSSLClientSocketNoPendingResult; |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1339 | } else { |
| 1340 | // No bytes were returned. Return the pending read error immediately. |
Oscar Johansson | d49464e | 2018-07-02 09:35:45 | [diff] [blame] | 1341 | DCHECK_NE(kSSLClientSocketNoPendingResult, pending_read_error_); |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1342 | rv = pending_read_error_; |
Oscar Johansson | d49464e | 2018-07-02 09:35:45 | [diff] [blame] | 1343 | pending_read_error_ = kSSLClientSocketNoPendingResult; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | if (rv >= 0) { |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1347 | net_log_.AddByteTransferEvent(NetLogEventType::SSL_SOCKET_BYTES_RECEIVED, |
xunjieli | 321a96f3 | 2017-03-07 19:42:17 | [diff] [blame] | 1348 | rv, buf->data()); |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1349 | } else if (rv != ERR_IO_PENDING) { |
| 1350 | net_log_.AddEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1351 | NetLogEventType::SSL_READ_ERROR, |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1352 | CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_, |
| 1353 | pending_read_error_info_)); |
| 1354 | pending_read_ssl_error_ = SSL_ERROR_NONE; |
| 1355 | pending_read_error_info_ = OpenSSLErrorInfo(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1356 | } |
| 1357 | return rv; |
| 1358 | } |
| 1359 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1360 | int SSLClientSocketImpl::DoPayloadWrite() { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1361 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 1362 | int rv = SSL_write(ssl_.get(), user_write_buf_->data(), user_write_buf_len_); |
rsleevi | f020edc | 2015-03-16 19:31:24 | [diff] [blame] | 1363 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1364 | if (rv >= 0) { |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1365 | net_log_.AddByteTransferEvent(NetLogEventType::SSL_SOCKET_BYTES_SENT, rv, |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1366 | user_write_buf_->data()); |
| 1367 | return rv; |
| 1368 | } |
| 1369 | |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 1370 | int ssl_error = SSL_get_error(ssl_.get(), rv); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1371 | if (ssl_error == SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) |
| 1372 | return ERR_IO_PENDING; |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1373 | OpenSSLErrorInfo error_info; |
davidben | fe132d9 | 2016-09-27 18:07:21 | [diff] [blame] | 1374 | int net_error = MapLastOpenSSLError(ssl_error, err_tracer, &error_info); |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1375 | |
| 1376 | if (net_error != ERR_IO_PENDING) { |
| 1377 | net_log_.AddEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1378 | NetLogEventType::SSL_WRITE_ERROR, |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1379 | CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info)); |
| 1380 | } |
| 1381 | return net_error; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1382 | } |
| 1383 | |
davidben | 3418e81f | 2016-10-19 00:09:45 | [diff] [blame] | 1384 | void SSLClientSocketImpl::RetryAllOperations() { |
| 1385 | // SSL_do_handshake, SSL_read, and SSL_write may all be retried when blocked, |
| 1386 | // so retry all operations for simplicity. (Otherwise, SSL_get_error for each |
| 1387 | // operation may be remembered to retry only the blocked ones.) |
| 1388 | |
Steven Valdez | 6af02df | 2018-07-15 21:52:33 | [diff] [blame] | 1389 | // Performing these callbacks may cause |this| to be deleted. If this |
| 1390 | // happens, the other callbacks should not be invoked. Guard against this by |
| 1391 | // holding a WeakPtr to |this| and ensuring it's still valid. |
| 1392 | base::WeakPtr<SSLClientSocketImpl> guard(weak_factory_.GetWeakPtr()); |
davidben | 3418e81f | 2016-10-19 00:09:45 | [diff] [blame] | 1393 | if (next_handshake_state_ == STATE_HANDSHAKE) { |
| 1394 | // In handshake phase. The parameter to OnHandshakeIOComplete is unused. |
| 1395 | OnHandshakeIOComplete(OK); |
davidben | 3418e81f | 2016-10-19 00:09:45 | [diff] [blame] | 1396 | } |
| 1397 | |
Steven Valdez | 6af02df | 2018-07-15 21:52:33 | [diff] [blame] | 1398 | if (!guard.get()) |
| 1399 | return; |
| 1400 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1401 | int rv_read = ERR_IO_PENDING; |
| 1402 | int rv_write = ERR_IO_PENDING; |
xunjieli | 321a96f3 | 2017-03-07 19:42:17 | [diff] [blame] | 1403 | if (user_read_buf_) { |
| 1404 | rv_read = DoPayloadRead(user_read_buf_.get(), user_read_buf_len_); |
| 1405 | } else if (!user_read_callback_.is_null()) { |
| 1406 | // ReadIfReady() is called by the user. Skip DoPayloadRead() and just let |
| 1407 | // the user know that read can be retried. |
| 1408 | rv_read = OK; |
| 1409 | } |
| 1410 | |
davidben | 3418e81f | 2016-10-19 00:09:45 | [diff] [blame] | 1411 | if (user_write_buf_) |
| 1412 | rv_write = DoPayloadWrite(); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1413 | |
davidben | 3418e81f | 2016-10-19 00:09:45 | [diff] [blame] | 1414 | if (rv_read != ERR_IO_PENDING) |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1415 | DoReadCallback(rv_read); |
| 1416 | |
| 1417 | if (!guard.get()) |
| 1418 | return; |
| 1419 | |
davidben | 3418e81f | 2016-10-19 00:09:45 | [diff] [blame] | 1420 | if (rv_write != ERR_IO_PENDING) |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1421 | DoWriteCallback(rv_write); |
| 1422 | } |
| 1423 | |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1424 | int SSLClientSocketImpl::VerifyCT() { |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1425 | const uint8_t* sct_list_raw; |
| 1426 | size_t sct_list_len; |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 1427 | SSL_get0_signed_cert_timestamp_list(ssl_.get(), &sct_list_raw, &sct_list_len); |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 1428 | base::StringPiece sct_list(reinterpret_cast<const char*>(sct_list_raw), |
| 1429 | sct_list_len); |
| 1430 | |
| 1431 | const uint8_t* ocsp_response_raw; |
| 1432 | size_t ocsp_response_len; |
| 1433 | SSL_get0_ocsp_response(ssl_.get(), &ocsp_response_raw, &ocsp_response_len); |
| 1434 | base::StringPiece ocsp_response( |
| 1435 | reinterpret_cast<const char*>(ocsp_response_raw), ocsp_response_len); |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1436 | |
| 1437 | // Note that this is a completely synchronous operation: The CT Log Verifier |
| 1438 | // gets all the data it needs for SCT verification and does not do any |
| 1439 | // external communication. |
| 1440 | cert_transparency_verifier_->Verify( |
Rob Percival | bc658a2 | 2017-12-13 08:24:42 | [diff] [blame] | 1441 | host_and_port().host(), server_cert_verify_result_.verified_cert.get(), |
| 1442 | ocsp_response, sct_list, &ct_verify_result_.scts, net_log_); |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1443 | |
Ryan Sleevi | 8a9c9c1 | 2018-05-09 02:36:23 | [diff] [blame] | 1444 | ct::SCTList verified_scts = |
eranm | 4bed0b57 | 2016-08-14 21:00:35 | [diff] [blame] | 1445 | ct::SCTsMatchingStatus(ct_verify_result_.scts, ct::SCT_STATUS_OK); |
| 1446 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 1447 | ct_verify_result_.policy_compliance = policy_enforcer_->CheckCompliance( |
| 1448 | server_cert_verify_result_.verified_cert.get(), verified_scts, net_log_); |
Emily Stark | 0d9809e | 2017-10-18 08:29:15 | [diff] [blame] | 1449 | if (server_cert_verify_result_.cert_status & CERT_STATUS_IS_EV) { |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 1450 | if (ct_verify_result_.policy_compliance != |
Ryan Sleevi | 8a9c9c1 | 2018-05-09 02:36:23 | [diff] [blame] | 1451 | ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS && |
| 1452 | ct_verify_result_.policy_compliance != |
| 1453 | ct::CTPolicyCompliance::CT_POLICY_BUILD_NOT_TIMELY) { |
Emily Stark | 0d9809e | 2017-10-18 08:29:15 | [diff] [blame] | 1454 | server_cert_verify_result_.cert_status |= |
| 1455 | CERT_STATUS_CT_COMPLIANCE_FAILED; |
| 1456 | server_cert_verify_result_.cert_status &= ~CERT_STATUS_IS_EV; |
| 1457 | } |
| 1458 | |
| 1459 | // Record the CT compliance status for connections with EV certificates, to |
| 1460 | // distinguish how often EV status is being dropped due to failing CT |
| 1461 | // compliance. |
Emily Stark | efce783 | 2017-11-30 03:16:16 | [diff] [blame] | 1462 | if (server_cert_verify_result_.is_issued_by_known_root) { |
| 1463 | UMA_HISTOGRAM_ENUMERATION("Net.CertificateTransparency.EVCompliance2.SSL", |
| 1464 | ct_verify_result_.policy_compliance, |
Kunihiko Sakamoto | 3646973 | 2018-09-27 03:33:45 | [diff] [blame] | 1465 | ct::CTPolicyCompliance::CT_POLICY_COUNT); |
Emily Stark | efce783 | 2017-11-30 03:16:16 | [diff] [blame] | 1466 | } |
rsleevi | cd7390e | 2017-06-14 10:18:26 | [diff] [blame] | 1467 | } |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1468 | |
Emily Stark | 0d9809e | 2017-10-18 08:29:15 | [diff] [blame] | 1469 | // Record the CT compliance of every connection to get an overall picture of |
| 1470 | // how many connections are CT-compliant. |
Emily Stark | efce783 | 2017-11-30 03:16:16 | [diff] [blame] | 1471 | if (server_cert_verify_result_.is_issued_by_known_root) { |
| 1472 | UMA_HISTOGRAM_ENUMERATION( |
| 1473 | "Net.CertificateTransparency.ConnectionComplianceStatus2.SSL", |
| 1474 | ct_verify_result_.policy_compliance, |
Kunihiko Sakamoto | 3646973 | 2018-09-27 03:33:45 | [diff] [blame] | 1475 | ct::CTPolicyCompliance::CT_POLICY_COUNT); |
Emily Stark | efce783 | 2017-11-30 03:16:16 | [diff] [blame] | 1476 | } |
Emily Stark | c96e9bc | 2017-10-10 00:10:39 | [diff] [blame] | 1477 | |
Emily Stark | 0d9809e | 2017-10-18 08:29:15 | [diff] [blame] | 1478 | TransportSecurityState::CTRequirementsStatus ct_requirement_status = |
| 1479 | transport_security_state_->CheckCTRequirements( |
estark | bf1b5296 | 2017-05-05 17:05:25 | [diff] [blame] | 1480 | host_and_port_, server_cert_verify_result_.is_issued_by_known_root, |
| 1481 | server_cert_verify_result_.public_key_hashes, |
| 1482 | server_cert_verify_result_.verified_cert.get(), server_cert_.get(), |
| 1483 | ct_verify_result_.scts, |
| 1484 | TransportSecurityState::ENABLE_EXPECT_CT_REPORTS, |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 1485 | ct_verify_result_.policy_compliance); |
Emily Stark | 0d9809e | 2017-10-18 08:29:15 | [diff] [blame] | 1486 | if (ct_requirement_status != TransportSecurityState::CT_NOT_REQUIRED) { |
Emily Stark | 8b411de0 | 2017-11-23 20:21:27 | [diff] [blame] | 1487 | ct_verify_result_.policy_compliance_required = true; |
Emily Stark | efce783 | 2017-11-30 03:16:16 | [diff] [blame] | 1488 | if (server_cert_verify_result_.is_issued_by_known_root) { |
| 1489 | // Record the CT compliance of connections for which compliance is |
| 1490 | // required; this helps answer the question: "Of all connections that are |
| 1491 | // supposed to be serving valid CT information, how many fail to do so?" |
| 1492 | UMA_HISTOGRAM_ENUMERATION( |
| 1493 | "Net.CertificateTransparency.CTRequiredConnectionComplianceStatus2." |
| 1494 | "SSL", |
| 1495 | ct_verify_result_.policy_compliance, |
Kunihiko Sakamoto | 3646973 | 2018-09-27 03:33:45 | [diff] [blame] | 1496 | ct::CTPolicyCompliance::CT_POLICY_COUNT); |
Emily Stark | efce783 | 2017-11-30 03:16:16 | [diff] [blame] | 1497 | } |
Emily Stark | 8b411de0 | 2017-11-23 20:21:27 | [diff] [blame] | 1498 | } else { |
| 1499 | ct_verify_result_.policy_compliance_required = false; |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1500 | } |
| 1501 | |
Emily Stark | 0d9809e | 2017-10-18 08:29:15 | [diff] [blame] | 1502 | switch (ct_requirement_status) { |
| 1503 | case TransportSecurityState::CT_REQUIREMENTS_NOT_MET: |
| 1504 | server_cert_verify_result_.cert_status |= |
| 1505 | CERT_STATUS_CERTIFICATE_TRANSPARENCY_REQUIRED; |
| 1506 | return ERR_CERTIFICATE_TRANSPARENCY_REQUIRED; |
| 1507 | case TransportSecurityState::CT_REQUIREMENTS_MET: |
| 1508 | case TransportSecurityState::CT_NOT_REQUIRED: |
| 1509 | return OK; |
| 1510 | } |
| 1511 | |
| 1512 | NOTREACHED(); |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1513 | return OK; |
| 1514 | } |
| 1515 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1516 | int SSLClientSocketImpl::ClientCertRequestCallback(SSL* ssl) { |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 1517 | DCHECK(ssl == ssl_.get()); |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 1518 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1519 | net_log_.AddEvent(NetLogEventType::SSL_CLIENT_CERT_REQUESTED); |
davidben | fe132d9 | 2016-09-27 18:07:21 | [diff] [blame] | 1520 | certificate_requested_ = true; |
davidben | af42cbe | 2014-11-13 03:27:46 | [diff] [blame] | 1521 | |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 1522 | // Clear any currently configured certificates. |
davidben | d80c12c | 2016-10-11 00:13:49 | [diff] [blame] | 1523 | SSL_certs_clear(ssl_.get()); |
[email protected] | 97a854f | 2014-07-29 07:51:36 | [diff] [blame] | 1524 | |
| 1525 | #if defined(OS_IOS) |
| 1526 | // TODO(droger): Support client auth on iOS. See http://crbug.com/145954). |
| 1527 | LOG(WARNING) << "Client auth is not supported"; |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1528 | #else // !defined(OS_IOS) |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1529 | if (!ssl_config_.send_client_cert) { |
[email protected] | 515adc2 | 2013-01-09 16:01:23 | [diff] [blame] | 1530 | // First pass: we know that a client certificate is needed, but we do not |
davidben | b11fd21 | 2017-01-12 17:08:03 | [diff] [blame] | 1531 | // have one at hand. Suspend the handshake. SSL_get_error will return |
| 1532 | // SSL_ERROR_WANT_X509_LOOKUP. |
davidben | ced4aa9b | 2015-05-12 21:22:35 | [diff] [blame] | 1533 | return -1; |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | // Second pass: a client certificate should have been selected. |
[email protected] | 13914c9 | 2013-06-13 22:42:42 | [diff] [blame] | 1537 | if (ssl_config_.client_cert.get()) { |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 1538 | if (!ssl_config_.client_private_key) { |
| 1539 | // The caller supplied a null private key. Fail the handshake and surface |
| 1540 | // an appropriate error to the caller. |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1541 | LOG(WARNING) << "Client cert found without private key"; |
| 1542 | OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); |
| 1543 | return -1; |
| 1544 | } |
| 1545 | |
David Benjamin | b8ab385 | 2017-08-04 00:17:32 | [diff] [blame] | 1546 | if (!SetSSLChainAndKey(ssl_.get(), ssl_config_.client_cert.get(), nullptr, |
| 1547 | &SSLContext::kPrivateKeyMethod)) { |
davidben | a35b40c3 | 2017-03-09 17:33:45 | [diff] [blame] | 1548 | OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); |
| 1549 | return -1; |
| 1550 | } |
svaldez | f3db006f | 2015-09-29 16:43:58 | [diff] [blame] | 1551 | |
David Benjamin | b9bafbe | 2017-11-07 21:41:38 | [diff] [blame] | 1552 | std::vector<uint16_t> preferences = |
| 1553 | ssl_config_.client_private_key->GetAlgorithmPreferences(); |
| 1554 | SSL_set_signing_algorithm_prefs(ssl_.get(), preferences.data(), |
| 1555 | preferences.size()); |
davidben | af42cbe | 2014-11-13 03:27:46 | [diff] [blame] | 1556 | |
David Benjamin | b8ab385 | 2017-08-04 00:17:32 | [diff] [blame] | 1557 | net_log_.AddEvent( |
| 1558 | NetLogEventType::SSL_CLIENT_CERT_PROVIDED, |
| 1559 | NetLog::IntCallback( |
| 1560 | "cert_count", |
Matt Mueller | a419327 | 2017-12-07 00:23:34 | [diff] [blame] | 1561 | 1 + ssl_config_.client_cert->intermediate_buffers().size())); |
[email protected] | 6bad505 | 2014-07-12 01:25:13 | [diff] [blame] | 1562 | return 1; |
[email protected] | c078770 | 2014-05-20 21:51:44 | [diff] [blame] | 1563 | } |
[email protected] | 97a854f | 2014-07-29 07:51:36 | [diff] [blame] | 1564 | #endif // defined(OS_IOS) |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1565 | |
| 1566 | // Send no client certificate. |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1567 | net_log_.AddEvent(NetLogEventType::SSL_CLIENT_CERT_PROVIDED, |
tfarina | 5e24b24 | 2015-10-27 13:11:28 | [diff] [blame] | 1568 | NetLog::IntCallback("cert_count", 0)); |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 1569 | return 1; |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1570 | } |
| 1571 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1572 | void SSLClientSocketImpl::MaybeCacheSession() { |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 1573 | // Only cache the session once both a new session has been established and the |
| 1574 | // certificate has been verified. Due to False Start, these events may happen |
| 1575 | // in either order. |
David Benjamin | b3840f4 | 2017-08-03 15:50:16 | [diff] [blame] | 1576 | if (!pending_session_ || !certificate_verified_ || |
| 1577 | ssl_session_cache_shard_.empty()) { |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 1578 | return; |
David Benjamin | b3840f4 | 2017-08-03 15:50:16 | [diff] [blame] | 1579 | } |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 1580 | |
| 1581 | SSLContext::GetInstance()->session_cache()->Insert(GetSessionCacheKey(), |
davidben | c269cc4b | 2016-07-27 14:55:03 | [diff] [blame] | 1582 | pending_session_.get()); |
| 1583 | pending_session_ = nullptr; |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 1584 | } |
| 1585 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1586 | int SSLClientSocketImpl::NewSessionCallback(SSL_SESSION* session) { |
David Benjamin | b3840f4 | 2017-08-03 15:50:16 | [diff] [blame] | 1587 | if (ssl_session_cache_shard_.empty()) |
| 1588 | return 0; |
| 1589 | |
davidben | c269cc4b | 2016-07-27 14:55:03 | [diff] [blame] | 1590 | // OpenSSL passes a reference to |session|. |
| 1591 | pending_session_.reset(session); |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 1592 | MaybeCacheSession(); |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 1593 | return 1; |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 1594 | } |
| 1595 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1596 | void SSLClientSocketImpl::AddCTInfoToSSLInfo(SSLInfo* ssl_info) const { |
estark | 723b5eeb | 2016-02-18 21:01:12 | [diff] [blame] | 1597 | ssl_info->UpdateCertificateTransparencyInfo(ct_verify_result_); |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 1598 | } |
| 1599 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1600 | std::string SSLClientSocketImpl::GetSessionCacheKey() const { |
David Benjamin | b3840f4 | 2017-08-03 15:50:16 | [diff] [blame] | 1601 | // If there is no session cache shard configured, disable session |
| 1602 | // caching. GetSessionCacheKey may not be called. When |
| 1603 | // https://crbug.com/458365 is fixed, this check will not be needed. |
| 1604 | DCHECK(!ssl_session_cache_shard_.empty()); |
| 1605 | |
rsleevi | f020edc | 2015-03-16 19:31:24 | [diff] [blame] | 1606 | std::string result = host_and_port_.ToString(); |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 1607 | result.push_back('/'); |
rsleevi | f020edc | 2015-03-16 19:31:24 | [diff] [blame] | 1608 | result.append(ssl_session_cache_shard_); |
| 1609 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 1610 | result.push_back('/'); |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 1611 | result.push_back(ssl_config_.version_interference_probe ? '1' : '0'); |
rsleevi | f020edc | 2015-03-16 19:31:24 | [diff] [blame] | 1612 | return result; |
| 1613 | } |
| 1614 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1615 | bool SSLClientSocketImpl::IsRenegotiationAllowed() const { |
bnc | ce6ea24 | 2016-09-15 20:22:32 | [diff] [blame] | 1616 | if (negotiated_protocol_ == kProtoUnknown) |
davidben | 421116c | 2015-05-12 19:56:51 | [diff] [blame] | 1617 | return ssl_config_.renego_allowed_default; |
| 1618 | |
davidben | 421116c | 2015-05-12 19:56:51 | [diff] [blame] | 1619 | for (NextProto allowed : ssl_config_.renego_allowed_for_protos) { |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 1620 | if (negotiated_protocol_ == allowed) |
davidben | 421116c | 2015-05-12 19:56:51 | [diff] [blame] | 1621 | return true; |
| 1622 | } |
| 1623 | return false; |
| 1624 | } |
| 1625 | |
David Benjamin | b9bafbe | 2017-11-07 21:41:38 | [diff] [blame] | 1626 | ssl_private_key_result_t SSLClientSocketImpl::PrivateKeySignCallback( |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1627 | uint8_t* out, |
| 1628 | size_t* out_len, |
| 1629 | size_t max_out, |
David Benjamin | b9bafbe | 2017-11-07 21:41:38 | [diff] [blame] | 1630 | uint16_t algorithm, |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1631 | const uint8_t* in, |
| 1632 | size_t in_len) { |
Oscar Johansson | d49464e | 2018-07-02 09:35:45 | [diff] [blame] | 1633 | DCHECK_EQ(kSSLClientSocketNoPendingResult, signature_result_); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1634 | DCHECK(signature_.empty()); |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 1635 | DCHECK(ssl_config_.client_private_key); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1636 | |
David Benjamin | b9bafbe | 2017-11-07 21:41:38 | [diff] [blame] | 1637 | net_log_.BeginEvent( |
| 1638 | NetLogEventType::SSL_PRIVATE_KEY_OP, |
David Benjamin | b65b073 | 2018-11-09 20:33:53 | [diff] [blame] | 1639 | base::BindRepeating( |
| 1640 | &NetLogPrivateKeyOperationCallback, algorithm, |
| 1641 | // Pass the SSLPrivateKey pointer to avoid making copies of the |
| 1642 | // provider name in the common case with logging disabled. |
| 1643 | base::Unretained(ssl_config_.client_private_key.get()))); |
David Benjamin | b9bafbe | 2017-11-07 21:41:38 | [diff] [blame] | 1644 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1645 | signature_result_ = ERR_IO_PENDING; |
David Benjamin | 9ba36b0 | 2017-11-10 19:01:53 | [diff] [blame] | 1646 | ssl_config_.client_private_key->Sign( |
| 1647 | algorithm, base::make_span(in, in_len), |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 1648 | base::BindOnce(&SSLClientSocketImpl::OnPrivateKeyComplete, |
| 1649 | weak_factory_.GetWeakPtr())); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1650 | return ssl_private_key_retry; |
| 1651 | } |
| 1652 | |
davidben | 0bca07fd | 2016-07-18 15:12:03 | [diff] [blame] | 1653 | ssl_private_key_result_t SSLClientSocketImpl::PrivateKeyCompleteCallback( |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1654 | uint8_t* out, |
| 1655 | size_t* out_len, |
| 1656 | size_t max_out) { |
Oscar Johansson | d49464e | 2018-07-02 09:35:45 | [diff] [blame] | 1657 | DCHECK_NE(kSSLClientSocketNoPendingResult, signature_result_); |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 1658 | DCHECK(ssl_config_.client_private_key); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1659 | |
| 1660 | if (signature_result_ == ERR_IO_PENDING) |
| 1661 | return ssl_private_key_retry; |
| 1662 | if (signature_result_ != OK) { |
| 1663 | OpenSSLPutNetError(FROM_HERE, signature_result_); |
| 1664 | return ssl_private_key_failure; |
| 1665 | } |
| 1666 | if (signature_.size() > max_out) { |
| 1667 | OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED); |
| 1668 | return ssl_private_key_failure; |
| 1669 | } |
davidben | 5f8b6bc | 2015-11-25 03:19:54 | [diff] [blame] | 1670 | memcpy(out, signature_.data(), signature_.size()); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1671 | *out_len = signature_.size(); |
| 1672 | signature_.clear(); |
| 1673 | return ssl_private_key_success; |
| 1674 | } |
| 1675 | |
davidben | 0bca07fd | 2016-07-18 15:12:03 | [diff] [blame] | 1676 | void SSLClientSocketImpl::OnPrivateKeyComplete( |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1677 | Error error, |
| 1678 | const std::vector<uint8_t>& signature) { |
| 1679 | DCHECK_EQ(ERR_IO_PENDING, signature_result_); |
| 1680 | DCHECK(signature_.empty()); |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 1681 | DCHECK(ssl_config_.client_private_key); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1682 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1683 | net_log_.EndEventWithNetErrorCode(NetLogEventType::SSL_PRIVATE_KEY_OP, error); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1684 | |
| 1685 | signature_result_ = error; |
| 1686 | if (signature_result_ == OK) |
| 1687 | signature_ = signature; |
| 1688 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1689 | // During a renegotiation, either Read or Write calls may be blocked on an |
| 1690 | // asynchronous private key operation. |
davidben | 3418e81f | 2016-10-19 00:09:45 | [diff] [blame] | 1691 | RetryAllOperations(); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1692 | } |
| 1693 | |
David Benjamin | 7a8e4dfa | 2018-06-12 23:07:21 | [diff] [blame] | 1694 | void SSLClientSocketImpl::InfoCallback(int type, int value) { |
| 1695 | if (type == SSL_CB_HANDSHAKE_START && completed_connect_) { |
| 1696 | UMA_HISTOGRAM_BOOLEAN("Net.SSLSecureRenegotiation", |
| 1697 | SSL_get_secure_renegotiation_support(ssl_.get())); |
| 1698 | } |
| 1699 | } |
| 1700 | |
davidben | cef9e21 | 2017-04-19 15:00:10 | [diff] [blame] | 1701 | void SSLClientSocketImpl::MessageCallback(int is_write, |
| 1702 | int content_type, |
| 1703 | const void* buf, |
| 1704 | size_t len) { |
| 1705 | switch (content_type) { |
| 1706 | case SSL3_RT_ALERT: |
| 1707 | net_log_.AddEvent(is_write ? NetLogEventType::SSL_ALERT_SENT |
| 1708 | : NetLogEventType::SSL_ALERT_RECEIVED, |
| 1709 | base::Bind(&NetLogSSLAlertCallback, buf, len)); |
| 1710 | break; |
| 1711 | case SSL3_RT_HANDSHAKE: |
| 1712 | net_log_.AddEvent( |
| 1713 | is_write ? NetLogEventType::SSL_HANDSHAKE_MESSAGE_SENT |
| 1714 | : NetLogEventType::SSL_HANDSHAKE_MESSAGE_RECEIVED, |
| 1715 | base::Bind(&NetLogSSLMessageCallback, !!is_write, buf, len)); |
| 1716 | break; |
| 1717 | default: |
| 1718 | return; |
| 1719 | } |
| 1720 | } |
| 1721 | |
davidben | 281d13f0 | 2016-04-27 20:43:28 | [diff] [blame] | 1722 | void SSLClientSocketImpl::LogConnectEndEvent(int rv) { |
| 1723 | if (rv != OK) { |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1724 | net_log_.EndEventWithNetErrorCode(NetLogEventType::SSL_CONNECT, rv); |
davidben | 281d13f0 | 2016-04-27 20:43:28 | [diff] [blame] | 1725 | return; |
| 1726 | } |
| 1727 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1728 | net_log_.EndEvent(NetLogEventType::SSL_CONNECT, |
davidben | 281d13f0 | 2016-04-27 20:43:28 | [diff] [blame] | 1729 | base::Bind(&NetLogSSLInfoCallback, base::Unretained(this))); |
| 1730 | } |
| 1731 | |
bnc | bd442c2 | 2016-09-14 20:49:16 | [diff] [blame] | 1732 | void SSLClientSocketImpl::RecordNegotiatedProtocol() const { |
| 1733 | UMA_HISTOGRAM_ENUMERATION("Net.SSLNegotiatedAlpnProtocol", |
| 1734 | negotiated_protocol_, kProtoLast + 1); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 1735 | } |
| 1736 | |
davidben | fe132d9 | 2016-09-27 18:07:21 | [diff] [blame] | 1737 | int SSLClientSocketImpl::MapLastOpenSSLError( |
| 1738 | int ssl_error, |
| 1739 | const crypto::OpenSSLErrStackTracer& tracer, |
| 1740 | OpenSSLErrorInfo* info) { |
| 1741 | int net_error = MapOpenSSLErrorWithDetails(ssl_error, tracer, info); |
| 1742 | |
| 1743 | if (ssl_error == SSL_ERROR_SSL && |
| 1744 | ERR_GET_LIB(info->error_code) == ERR_LIB_SSL) { |
| 1745 | // TLS does not provide an alert for missing client certificates, so most |
| 1746 | // servers send a generic handshake_failure alert. Detect this case by |
| 1747 | // checking if we have received a CertificateRequest but sent no |
| 1748 | // certificate. See https://crbug.com/646567. |
| 1749 | if (ERR_GET_REASON(info->error_code) == |
| 1750 | SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE && |
| 1751 | certificate_requested_ && ssl_config_.send_client_cert && |
| 1752 | !ssl_config_.client_cert) { |
| 1753 | net_error = ERR_BAD_SSL_CLIENT_AUTH_CERT; |
| 1754 | } |
| 1755 | |
| 1756 | // Per spec, access_denied is only for client-certificate-based access |
| 1757 | // control, but some buggy firewalls use it when blocking a page. To avoid a |
| 1758 | // confusing error, map it to a generic protocol error if no |
| 1759 | // CertificateRequest was sent. See https://crbug.com/630883. |
| 1760 | if (ERR_GET_REASON(info->error_code) == SSL_R_TLSV1_ALERT_ACCESS_DENIED && |
| 1761 | !certificate_requested_) { |
| 1762 | net_error = ERR_SSL_PROTOCOL_ERROR; |
| 1763 | } |
David Benjamin | 5b4410e | 2017-11-10 21:50:23 | [diff] [blame] | 1764 | |
| 1765 | // This error is specific to the client, so map it here. |
| 1766 | if (ERR_GET_REASON(info->error_code) == |
| 1767 | SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS) { |
| 1768 | net_error = ERR_SSL_CLIENT_AUTH_NO_COMMON_ALGORITHMS; |
| 1769 | } |
davidben | fe132d9 | 2016-09-27 18:07:21 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | return net_error; |
| 1773 | } |
| 1774 | |
[email protected] | 7e5dd49f | 2010-12-08 18:33:49 | [diff] [blame] | 1775 | } // namespace net |