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