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