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