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