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