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