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