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