[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> |
davidben | 018aad6 | 2014-09-12 02:25:19 | [diff] [blame] | 8 | #include <openssl/bio.h> |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame] | 9 | #include <openssl/bytestring.h> |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 10 | #include <openssl/err.h> |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame] | 11 | #include <openssl/evp.h> |
davidben | 121e9c96 | 2015-05-01 00:40:49 | [diff] [blame] | 12 | #include <openssl/mem.h> |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 13 | #include <openssl/ssl.h> |
bnc | 67da3de | 2015-01-15 21:02:26 | [diff] [blame] | 14 | #include <string.h> |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 15 | |
davidben | 752bcf2 | 2015-12-21 22:55:50 | [diff] [blame] | 16 | #include <utility> |
| 17 | |
[email protected] | 0f7804ec | 2011-10-07 20:04:18 | [diff] [blame] | 18 | #include "base/bind.h" |
[email protected] | f2da6ac | 2013-02-04 08:22:53 | [diff] [blame] | 19 | #include "base/callback_helpers.h" |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 20 | #include "base/lazy_instance.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 21 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 22 | #include "base/memory/singleton.h" |
mmenke | 1beda3d | 2016-07-22 03:33:45 | [diff] [blame] | 23 | #include "base/metrics/field_trial.h" |
asvitkine | c3c9372 | 2015-06-17 14:48:37 | [diff] [blame] | 24 | #include "base/metrics/histogram_macros.h" |
davidben | 4fe4f98 | 2015-11-11 22:00:12 | [diff] [blame] | 25 | #include "base/metrics/sparse_histogram.h" |
vadimt | b2a77c76 | 2014-11-21 19:49:22 | [diff] [blame] | 26 | #include "base/profiler/scoped_tracker.h" |
nharper | 49b27d99 | 2016-02-09 18:28:51 | [diff] [blame] | 27 | #include "base/strings/string_number_conversions.h" |
davidben | 018aad6 | 2014-09-12 02:25:19 | [diff] [blame] | 28 | #include "base/strings/string_piece.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 29 | #include "base/synchronization/lock.h" |
vadimt | 6b43dec2 | 2015-01-06 01:59:58 | [diff] [blame] | 30 | #include "base/threading/thread_local.h" |
ssid | 6d6b4010 | 2016-04-05 18:59:56 | [diff] [blame] | 31 | #include "base/trace_event/trace_event.h" |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 32 | #include "base/values.h" |
davidben | 7dad2a3 | 2016-03-01 23:47:47 | [diff] [blame] | 33 | #include "crypto/auto_cbb.h" |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 34 | #include "crypto/ec_private_key.h" |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 35 | #include "crypto/openssl_util.h" |
[email protected] | cd9b75b | 2014-07-10 04:39:38 | [diff] [blame] | 36 | #include "crypto/scoped_openssl_types.h" |
martijn | a2e83bd | 2016-03-18 13:10:45 | [diff] [blame] | 37 | #include "net/base/ip_address.h" |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 38 | #include "net/base/net_errors.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 39 | #include "net/cert/cert_verifier.h" |
eranm | efbd313 | 2014-10-28 16:35:16 | [diff] [blame] | 40 | #include "net/cert/ct_ev_whitelist.h" |
estark | 6f9b3d8 | 2016-01-12 21:37:05 | [diff] [blame] | 41 | #include "net/cert/ct_policy_enforcer.h" |
estark | 723b5eeb | 2016-02-18 21:01:12 | [diff] [blame] | 42 | #include "net/cert/ct_policy_status.h" |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 43 | #include "net/cert/ct_verifier.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 44 | #include "net/cert/x509_certificate_net_log_param.h" |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 45 | #include "net/cert/x509_util_openssl.h" |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 46 | #include "net/http/transport_security_state.h" |
davidben | c879af0 | 2015-02-20 07:57:21 | [diff] [blame] | 47 | #include "net/ssl/scoped_openssl_types.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 48 | #include "net/ssl/ssl_cert_request_info.h" |
davidben | 281d13f0 | 2016-04-27 20:43:28 | [diff] [blame] | 49 | #include "net/ssl/ssl_cipher_suite_names.h" |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 50 | #include "net/ssl/ssl_client_session_cache.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 51 | #include "net/ssl/ssl_connection_status_flags.h" |
| 52 | #include "net/ssl/ssl_info.h" |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 53 | #include "net/ssl/ssl_private_key.h" |
nharper | d5cddca | 2016-02-27 03:37:52 | [diff] [blame] | 54 | #include "net/ssl/token_binding.h" |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 55 | |
davidben | 2a811e4e | 2015-12-01 10:49:34 | [diff] [blame] | 56 | #if !defined(OS_NACL) |
| 57 | #include "net/ssl/ssl_key_logger.h" |
| 58 | #endif |
| 59 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 60 | #if defined(USE_NSS_CERTS) |
davidben | a477a70a | 2015-10-06 04:38:29 | [diff] [blame] | 61 | #include "net/cert_net/nss_ocsp.h" |
| 62 | #endif |
| 63 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 64 | namespace net { |
| 65 | |
| 66 | namespace { |
| 67 | |
[email protected] | 4b76856 | 2013-02-16 04:10:07 | [diff] [blame] | 68 | // This constant can be any non-negative/non-zero value (eg: it does not |
| 69 | // overlap with any value of the net::Error range, including net::OK). |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 70 | const int kNoPendingResult = 1; |
[email protected] | 4b76856 | 2013-02-16 04:10:07 | [diff] [blame] | 71 | |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 72 | // If a client doesn't have a list of protocols that it supports, but |
| 73 | // the server supports NPN, choosing "http/1.1" is the best answer. |
| 74 | const char kDefaultSupportedNPNProtocol[] = "http/1.1"; |
| 75 | |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 76 | // Default size of the internal BoringSSL buffers. |
mmenke | 1beda3d | 2016-07-22 03:33:45 | [diff] [blame] | 77 | const int kDefaultOpenSSLBufferSize = 17 * 1024; |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 78 | |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame] | 79 | // TLS extension number use for Token Binding. |
nharper | b5ad8a80 | 2016-02-05 19:40:00 | [diff] [blame] | 80 | const unsigned int kTbExtNum = 24; |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame] | 81 | |
| 82 | // Token Binding ProtocolVersions supported. |
| 83 | const uint8_t kTbProtocolVersionMajor = 0; |
nharper | 86f0be2 | 2016-07-14 00:49:36 | [diff] [blame] | 84 | const uint8_t kTbProtocolVersionMinor = 8; |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame] | 85 | const uint8_t kTbMinProtocolVersionMajor = 0; |
nharper | 2840f95 | 2016-06-28 01:38:21 | [diff] [blame] | 86 | const uint8_t kTbMinProtocolVersionMinor = 6; |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame] | 87 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 88 | bool EVP_MDToPrivateKeyHash(const EVP_MD* md, SSLPrivateKey::Hash* hash) { |
| 89 | switch (EVP_MD_type(md)) { |
| 90 | case NID_md5_sha1: |
| 91 | *hash = SSLPrivateKey::Hash::MD5_SHA1; |
| 92 | return true; |
| 93 | case NID_sha1: |
| 94 | *hash = SSLPrivateKey::Hash::SHA1; |
| 95 | return true; |
| 96 | case NID_sha256: |
| 97 | *hash = SSLPrivateKey::Hash::SHA256; |
| 98 | return true; |
| 99 | case NID_sha384: |
| 100 | *hash = SSLPrivateKey::Hash::SHA384; |
| 101 | return true; |
| 102 | case NID_sha512: |
| 103 | *hash = SSLPrivateKey::Hash::SHA512; |
| 104 | return true; |
| 105 | default: |
| 106 | return false; |
| 107 | } |
| 108 | } |
| 109 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 110 | std::unique_ptr<base::Value> NetLogPrivateKeyOperationCallback( |
davidben | 752bcf2 | 2015-12-21 22:55:50 | [diff] [blame] | 111 | SSLPrivateKey::Type type, |
| 112 | SSLPrivateKey::Hash hash, |
| 113 | NetLogCaptureMode mode) { |
| 114 | std::string type_str; |
| 115 | switch (type) { |
| 116 | case SSLPrivateKey::Type::RSA: |
| 117 | type_str = "RSA"; |
| 118 | break; |
| 119 | case SSLPrivateKey::Type::ECDSA: |
| 120 | type_str = "ECDSA"; |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | std::string hash_str; |
| 125 | switch (hash) { |
| 126 | case SSLPrivateKey::Hash::MD5_SHA1: |
| 127 | hash_str = "MD5_SHA1"; |
| 128 | break; |
| 129 | case SSLPrivateKey::Hash::SHA1: |
| 130 | hash_str = "SHA1"; |
| 131 | break; |
| 132 | case SSLPrivateKey::Hash::SHA256: |
| 133 | hash_str = "SHA256"; |
| 134 | break; |
| 135 | case SSLPrivateKey::Hash::SHA384: |
| 136 | hash_str = "SHA384"; |
| 137 | break; |
| 138 | case SSLPrivateKey::Hash::SHA512: |
| 139 | hash_str = "SHA512"; |
| 140 | break; |
| 141 | } |
| 142 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 143 | std::unique_ptr<base::DictionaryValue> value(new base::DictionaryValue); |
davidben | 752bcf2 | 2015-12-21 22:55:50 | [diff] [blame] | 144 | value->SetString("type", type_str); |
| 145 | value->SetString("hash", hash_str); |
| 146 | return std::move(value); |
| 147 | } |
| 148 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 149 | std::unique_ptr<base::Value> NetLogChannelIDLookupCallback( |
nharper | 49b27d99 | 2016-02-09 18:28:51 | [diff] [blame] | 150 | ChannelIDService* channel_id_service, |
| 151 | NetLogCaptureMode capture_mode) { |
| 152 | ChannelIDStore* store = channel_id_service->GetChannelIDStore(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 153 | std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
nharper | 49b27d99 | 2016-02-09 18:28:51 | [diff] [blame] | 154 | dict->SetBoolean("ephemeral", store->IsEphemeral()); |
| 155 | dict->SetString("service", base::HexEncode(&channel_id_service, |
| 156 | sizeof(channel_id_service))); |
| 157 | dict->SetString("store", base::HexEncode(&store, sizeof(store))); |
| 158 | return std::move(dict); |
| 159 | } |
| 160 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 161 | std::unique_ptr<base::Value> NetLogChannelIDLookupCompleteCallback( |
nharper | 49b27d99 | 2016-02-09 18:28:51 | [diff] [blame] | 162 | crypto::ECPrivateKey* key, |
| 163 | int result, |
| 164 | NetLogCaptureMode capture_mode) { |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 165 | std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
nharper | 49b27d99 | 2016-02-09 18:28:51 | [diff] [blame] | 166 | dict->SetInteger("net_error", result); |
| 167 | std::string raw_key; |
| 168 | if (result == OK && key && key->ExportRawPublicKey(&raw_key)) { |
| 169 | std::string key_to_log = "redacted"; |
| 170 | if (capture_mode.include_cookies_and_credentials()) { |
| 171 | key_to_log = base::HexEncode(raw_key.data(), raw_key.length()); |
| 172 | } |
| 173 | dict->SetString("key", key_to_log); |
| 174 | } |
| 175 | return std::move(dict); |
| 176 | } |
| 177 | |
davidben | 281d13f0 | 2016-04-27 20:43:28 | [diff] [blame] | 178 | std::unique_ptr<base::Value> NetLogSSLInfoCallback( |
| 179 | SSLClientSocketImpl* socket, |
| 180 | NetLogCaptureMode capture_mode) { |
| 181 | SSLInfo ssl_info; |
| 182 | if (!socket->GetSSLInfo(&ssl_info)) |
| 183 | return nullptr; |
| 184 | |
| 185 | std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 186 | const char* version_str; |
| 187 | SSLVersionToString(&version_str, |
| 188 | SSLConnectionStatusToVersion(ssl_info.connection_status)); |
| 189 | dict->SetString("version", version_str); |
| 190 | dict->SetBoolean("is_resumed", |
| 191 | ssl_info.handshake_type == SSLInfo::HANDSHAKE_RESUME); |
| 192 | dict->SetInteger("cipher_suite", SSLConnectionStatusToCipherSuite( |
| 193 | ssl_info.connection_status)); |
| 194 | |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 195 | dict->SetString("next_proto", SSLClientSocket::NextProtoToString( |
| 196 | socket->GetNegotiatedProtocol())); |
davidben | 281d13f0 | 2016-04-27 20:43:28 | [diff] [blame] | 197 | |
| 198 | return std::move(dict); |
| 199 | } |
| 200 | |
[email protected] | 821e3bb | 2013-11-08 01:06:01 | [diff] [blame] | 201 | } // namespace |
| 202 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 203 | class SSLClientSocketImpl::SSLContext { |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 204 | public: |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 205 | static SSLContext* GetInstance() { |
| 206 | return base::Singleton<SSLContext>::get(); |
| 207 | } |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 208 | SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 209 | SSLClientSessionCache* session_cache() { return &session_cache_; } |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 210 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 211 | SSLClientSocketImpl* GetClientSocketFromSSL(const SSL* ssl) { |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 212 | DCHECK(ssl); |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 213 | SSLClientSocketImpl* socket = static_cast<SSLClientSocketImpl*>( |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 214 | SSL_get_ex_data(ssl, ssl_socket_data_index_)); |
| 215 | DCHECK(socket); |
| 216 | return socket; |
| 217 | } |
| 218 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 219 | bool SetClientSocketForSSL(SSL* ssl, SSLClientSocketImpl* socket) { |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 220 | return SSL_set_ex_data(ssl, ssl_socket_data_index_, socket) != 0; |
| 221 | } |
| 222 | |
davidben | 2a811e4e | 2015-12-01 10:49:34 | [diff] [blame] | 223 | #if !defined(OS_NACL) |
| 224 | void SetSSLKeyLogFile( |
| 225 | const base::FilePath& path, |
| 226 | const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
| 227 | DCHECK(!ssl_key_logger_); |
| 228 | ssl_key_logger_.reset(new SSLKeyLogger(path, task_runner)); |
| 229 | SSL_CTX_set_keylog_callback(ssl_ctx_.get(), KeyLogCallback); |
| 230 | } |
| 231 | #endif |
| 232 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 233 | static const SSL_PRIVATE_KEY_METHOD kPrivateKeyMethod; |
| 234 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 235 | private: |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 236 | friend struct base::DefaultSingletonTraits<SSLContext>; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 237 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 238 | SSLContext() : session_cache_(SSLClientSessionCache::Config()) { |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 239 | crypto::EnsureOpenSSLInit(); |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 240 | ssl_socket_data_index_ = SSL_get_ex_new_index(0, 0, 0, 0, 0); |
| 241 | DCHECK_NE(ssl_socket_data_index_, -1); |
| 242 | ssl_ctx_.reset(SSL_CTX_new(SSLv23_client_method())); |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 243 | SSL_CTX_set_cert_verify_callback(ssl_ctx_.get(), CertVerifyCallback, NULL); |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 244 | SSL_CTX_set_cert_cb(ssl_ctx_.get(), ClientCertRequestCallback, NULL); |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 245 | SSL_CTX_set_verify(ssl_ctx_.get(), SSL_VERIFY_PEER, NULL); |
haavardm | c80b0ee3 | 2015-01-30 09:16:08 | [diff] [blame] | 246 | // This stops |SSL_shutdown| from generating the close_notify message, which |
| 247 | // is currently not sent on the network. |
| 248 | // TODO(haavardm): Remove setting quiet shutdown once 118366 is fixed. |
| 249 | SSL_CTX_set_quiet_shutdown(ssl_ctx_.get(), 1); |
bnc | 2d6bd0f | 2015-10-28 01:52:05 | [diff] [blame] | 250 | // Note that SSL_OP_DISABLE_NPN is used to disable NPN if |
| 251 | // ssl_config_.next_proto is empty. |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 252 | SSL_CTX_set_next_proto_select_cb(ssl_ctx_.get(), SelectNextProtoCallback, |
| 253 | NULL); |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 254 | |
| 255 | // Disable the internal session cache. Session caching is handled |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 256 | // externally (i.e. by SSLClientSessionCache). |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 257 | SSL_CTX_set_session_cache_mode( |
| 258 | ssl_ctx_.get(), SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL); |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 259 | SSL_CTX_sess_set_new_cb(ssl_ctx_.get(), NewSessionCallback); |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame] | 260 | |
| 261 | if (!SSL_CTX_add_client_custom_ext(ssl_ctx_.get(), kTbExtNum, |
| 262 | &TokenBindingAddCallback, |
| 263 | &TokenBindingFreeCallback, nullptr, |
| 264 | &TokenBindingParseCallback, nullptr)) { |
| 265 | NOTREACHED(); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | static int TokenBindingAddCallback(SSL* ssl, |
| 270 | unsigned int extension_value, |
| 271 | const uint8_t** out, |
| 272 | size_t* out_len, |
| 273 | int* out_alert_value, |
| 274 | void* add_arg) { |
| 275 | DCHECK_EQ(extension_value, kTbExtNum); |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 276 | SSLClientSocketImpl* socket = |
| 277 | SSLClientSocketImpl::SSLContext::GetInstance()->GetClientSocketFromSSL( |
| 278 | ssl); |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame] | 279 | return socket->TokenBindingAdd(out, out_len, out_alert_value); |
| 280 | } |
| 281 | |
| 282 | static void TokenBindingFreeCallback(SSL* ssl, |
| 283 | unsigned extension_value, |
| 284 | const uint8_t* out, |
| 285 | void* add_arg) { |
| 286 | DCHECK_EQ(extension_value, kTbExtNum); |
| 287 | OPENSSL_free(const_cast<unsigned char*>(out)); |
| 288 | } |
| 289 | |
| 290 | static int TokenBindingParseCallback(SSL* ssl, |
| 291 | unsigned int extension_value, |
| 292 | const uint8_t* contents, |
| 293 | size_t contents_len, |
| 294 | int* out_alert_value, |
| 295 | void* parse_arg) { |
| 296 | DCHECK_EQ(extension_value, kTbExtNum); |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 297 | SSLClientSocketImpl* socket = |
| 298 | SSLClientSocketImpl::SSLContext::GetInstance()->GetClientSocketFromSSL( |
| 299 | ssl); |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame] | 300 | return socket->TokenBindingParse(contents, contents_len, out_alert_value); |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 301 | } |
| 302 | |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 303 | static int ClientCertRequestCallback(SSL* ssl, void* arg) { |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 304 | SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 305 | DCHECK(socket); |
| 306 | return socket->ClientCertRequestCallback(ssl); |
[email protected] | 718c967 | 2010-12-02 10:04:10 | [diff] [blame] | 307 | } |
| 308 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 309 | static int CertVerifyCallback(X509_STORE_CTX* store_ctx, void* arg) { |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 310 | SSL* ssl = reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data( |
| 311 | store_ctx, SSL_get_ex_data_X509_STORE_CTX_idx())); |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 312 | SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 313 | CHECK(socket); |
| 314 | |
| 315 | return socket->CertVerifyCallback(store_ctx); |
| 316 | } |
| 317 | |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 318 | static int SelectNextProtoCallback(SSL* ssl, |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 319 | unsigned char** out, |
| 320 | unsigned char* outlen, |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 321 | const unsigned char* in, |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 322 | unsigned int inlen, |
| 323 | void* arg) { |
| 324 | SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 325 | return socket->SelectNextProtoCallback(out, outlen, in, inlen); |
| 326 | } |
| 327 | |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 328 | static int NewSessionCallback(SSL* ssl, SSL_SESSION* session) { |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 329 | SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 330 | return socket->NewSessionCallback(session); |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 331 | } |
| 332 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 333 | static int PrivateKeyTypeCallback(SSL* ssl) { |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 334 | SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 335 | return socket->PrivateKeyTypeCallback(); |
| 336 | } |
| 337 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 338 | static size_t PrivateKeyMaxSignatureLenCallback(SSL* ssl) { |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 339 | SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 340 | return socket->PrivateKeyMaxSignatureLenCallback(); |
| 341 | } |
| 342 | |
davidben | 0bca07fd | 2016-07-18 15:12:03 | [diff] [blame] | 343 | static ssl_private_key_result_t PrivateKeySignDigestCallback( |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 344 | SSL* ssl, |
| 345 | uint8_t* out, |
| 346 | size_t* out_len, |
davidben | 0bca07fd | 2016-07-18 15:12:03 | [diff] [blame] | 347 | size_t max_out, |
| 348 | const EVP_MD* md, |
| 349 | const uint8_t* in, |
| 350 | size_t in_len) { |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 351 | SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
davidben | 0bca07fd | 2016-07-18 15:12:03 | [diff] [blame] | 352 | return socket->PrivateKeySignDigestCallback(out, out_len, max_out, md, in, |
| 353 | in_len); |
| 354 | } |
| 355 | |
| 356 | static ssl_private_key_result_t PrivateKeyCompleteCallback(SSL* ssl, |
| 357 | uint8_t* out, |
| 358 | size_t* out_len, |
| 359 | size_t max_out) { |
| 360 | SSLClientSocketImpl* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
| 361 | return socket->PrivateKeyCompleteCallback(out, out_len, max_out); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 362 | } |
| 363 | |
davidben | 2a811e4e | 2015-12-01 10:49:34 | [diff] [blame] | 364 | #if !defined(OS_NACL) |
| 365 | static void KeyLogCallback(const SSL* ssl, const char* line) { |
| 366 | GetInstance()->ssl_key_logger_->WriteLine(line); |
| 367 | } |
| 368 | #endif |
| 369 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 370 | // This is the index used with SSL_get_ex_data to retrieve the owner |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 371 | // SSLClientSocketImpl object from an SSL instance. |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 372 | int ssl_socket_data_index_; |
| 373 | |
davidben | c879af0 | 2015-02-20 07:57:21 | [diff] [blame] | 374 | ScopedSSL_CTX ssl_ctx_; |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 375 | |
davidben | 2a811e4e | 2015-12-01 10:49:34 | [diff] [blame] | 376 | #if !defined(OS_NACL) |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 377 | std::unique_ptr<SSLKeyLogger> ssl_key_logger_; |
davidben | 2a811e4e | 2015-12-01 10:49:34 | [diff] [blame] | 378 | #endif |
| 379 | |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 380 | // TODO(davidben): Use a separate cache per URLRequestContext. |
| 381 | // https://crbug.com/458365 |
| 382 | // |
| 383 | // TODO(davidben): Sessions should be invalidated on fatal |
| 384 | // alerts. https://crbug.com/466352 |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 385 | SSLClientSessionCache session_cache_; |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 386 | }; |
| 387 | |
davidben | 0bca07fd | 2016-07-18 15:12:03 | [diff] [blame] | 388 | // TODO(davidben): Switch from sign_digest to sign. |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 389 | const SSL_PRIVATE_KEY_METHOD |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 390 | SSLClientSocketImpl::SSLContext::kPrivateKeyMethod = { |
| 391 | &SSLClientSocketImpl::SSLContext::PrivateKeyTypeCallback, |
| 392 | &SSLClientSocketImpl::SSLContext::PrivateKeyMaxSignatureLenCallback, |
davidben | 0bca07fd | 2016-07-18 15:12:03 | [diff] [blame] | 393 | nullptr /* sign */, |
| 394 | &SSLClientSocketImpl::SSLContext::PrivateKeySignDigestCallback, |
| 395 | nullptr /* decrypt */, |
| 396 | &SSLClientSocketImpl::SSLContext::PrivateKeyCompleteCallback, |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 397 | }; |
| 398 | |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 399 | // PeerCertificateChain is a helper object which extracts the certificate |
| 400 | // chain, as given by the server, from an OpenSSL socket and performs the needed |
| 401 | // resource management. The first element of the chain is the leaf certificate |
| 402 | // and the other elements are in the order given by the server. |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 403 | class SSLClientSocketImpl::PeerCertificateChain { |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 404 | public: |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 405 | explicit PeerCertificateChain(STACK_OF(X509) * chain) { Reset(chain); } |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 406 | PeerCertificateChain(const PeerCertificateChain& other) { *this = other; } |
| 407 | ~PeerCertificateChain() {} |
| 408 | PeerCertificateChain& operator=(const PeerCertificateChain& other); |
| 409 | |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 410 | // Resets the PeerCertificateChain to the set of certificates in|chain|, |
| 411 | // which may be NULL, indicating to empty the store certificates. |
| 412 | // Note: If an error occurs, such as being unable to parse the certificates, |
| 413 | // this will behave as if Reset(NULL) was called. |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 414 | void Reset(STACK_OF(X509) * chain); |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 415 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 416 | // Note that when USE_OPENSSL_CERTS is defined, OSCertHandle is X509* |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 417 | scoped_refptr<X509Certificate> AsOSChain() const; |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 418 | |
| 419 | size_t size() const { |
| 420 | if (!openssl_chain_.get()) |
| 421 | return 0; |
| 422 | return sk_X509_num(openssl_chain_.get()); |
| 423 | } |
| 424 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 425 | bool empty() const { return size() == 0; } |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 426 | |
| 427 | X509* Get(size_t index) const { |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 428 | DCHECK_LT(index, size()); |
| 429 | return sk_X509_value(openssl_chain_.get(), index); |
| 430 | } |
| 431 | |
| 432 | private: |
[email protected] | cd9b75b | 2014-07-10 04:39:38 | [diff] [blame] | 433 | ScopedX509Stack openssl_chain_; |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 434 | }; |
| 435 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 436 | SSLClientSocketImpl::PeerCertificateChain& |
| 437 | SSLClientSocketImpl::PeerCertificateChain::operator=( |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 438 | const PeerCertificateChain& other) { |
| 439 | if (this == &other) |
| 440 | return *this; |
| 441 | |
[email protected] | 24176af | 2014-08-14 09:31:04 | [diff] [blame] | 442 | openssl_chain_.reset(X509_chain_up_ref(other.openssl_chain_.get())); |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 443 | return *this; |
| 444 | } |
| 445 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 446 | void SSLClientSocketImpl::PeerCertificateChain::Reset(STACK_OF(X509) * chain) { |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 447 | openssl_chain_.reset(chain ? X509_chain_up_ref(chain) : NULL); |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 448 | } |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 449 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 450 | scoped_refptr<X509Certificate> |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 451 | SSLClientSocketImpl::PeerCertificateChain::AsOSChain() const { |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 452 | #if defined(USE_OPENSSL_CERTS) |
| 453 | // When OSCertHandle is typedef'ed to X509, this implementation does a short |
| 454 | // cut to avoid converting back and forth between DER and the X509 struct. |
| 455 | X509Certificate::OSCertHandles intermediates; |
| 456 | for (size_t i = 1; i < sk_X509_num(openssl_chain_.get()); ++i) { |
| 457 | intermediates.push_back(sk_X509_value(openssl_chain_.get(), i)); |
| 458 | } |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 459 | |
svaldez | c4b6f12 | 2016-01-20 22:13:59 | [diff] [blame] | 460 | return X509Certificate::CreateFromHandle( |
| 461 | sk_X509_value(openssl_chain_.get(), 0), intermediates); |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 462 | #else |
| 463 | // DER-encode the chain and convert to a platform certificate handle. |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 464 | std::vector<base::StringPiece> der_chain; |
[email protected] | edfd0f4 | 2014-07-22 18:20:37 | [diff] [blame] | 465 | for (size_t i = 0; i < sk_X509_num(openssl_chain_.get()); ++i) { |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 466 | X509* x = sk_X509_value(openssl_chain_.get(), i); |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 467 | base::StringPiece der; |
| 468 | if (!x509_util::GetDER(x, &der)) |
| 469 | return NULL; |
| 470 | der_chain.push_back(der); |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 471 | } |
| 472 | |
svaldez | c4b6f12 | 2016-01-20 22:13:59 | [diff] [blame] | 473 | return X509Certificate::CreateFromDERCertChain(der_chain); |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 474 | #endif |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 475 | } |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 476 | |
[email protected] | 1279de1 | 2013-12-03 15:13:32 | [diff] [blame] | 477 | // static |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 478 | void SSLClientSocket::ClearSessionCache() { |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 479 | SSLClientSocketImpl::SSLContext* context = |
| 480 | SSLClientSocketImpl::SSLContext::GetInstance(); |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 481 | context->session_cache()->Flush(); |
| 482 | } |
| 483 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 484 | SSLClientSocketImpl::SSLClientSocketImpl( |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 485 | std::unique_ptr<ClientSocketHandle> transport_socket, |
[email protected] | 055d7f2 | 2010-11-15 12:03:12 | [diff] [blame] | 486 | const HostPortPair& host_and_port, |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 487 | const SSLConfig& ssl_config, |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 488 | const SSLClientSocketContext& context) |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 489 | : transport_send_busy_(false), |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 490 | transport_recv_busy_(false), |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 491 | pending_read_error_(kNoPendingResult), |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 492 | pending_read_ssl_error_(SSL_ERROR_NONE), |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 493 | transport_read_error_(OK), |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 494 | transport_write_error_(OK), |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 495 | server_cert_chain_(new PeerCertificateChain(NULL)), |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 496 | completed_connect_(false), |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 497 | was_ever_used_(false), |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 498 | cert_verifier_(context.cert_verifier), |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 499 | cert_transparency_verifier_(context.cert_transparency_verifier), |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 500 | channel_id_service_(context.channel_id_service), |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame] | 501 | tb_was_negotiated_(false), |
| 502 | tb_negotiated_param_(TB_PARAM_ECDSAP256), |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 503 | tb_signed_ekm_map_(10), |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 504 | ssl_(NULL), |
| 505 | transport_bio_(NULL), |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 506 | transport_(std::move(transport_socket)), |
[email protected] | 055d7f2 | 2010-11-15 12:03:12 | [diff] [blame] | 507 | host_and_port_(host_and_port), |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 508 | ssl_config_(ssl_config), |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 509 | ssl_session_cache_shard_(context.ssl_session_cache_shard), |
[email protected] | 013c17c | 2012-01-21 19:09:01 | [diff] [blame] | 510 | next_handshake_state_(STATE_NONE), |
svaldez | 4af14d2 | 2015-08-20 13:48:24 | [diff] [blame] | 511 | disconnected_(false), |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 512 | npn_status_(kNextProtoUnsupported), |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 513 | negotiated_protocol_(kProtoUnknown), |
| 514 | negotiation_extension_(kExtensionUnknown), |
davidben | 52053b38 | 2015-04-27 19:22:29 | [diff] [blame] | 515 | channel_id_sent_(false), |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 516 | certificate_verified_(false), |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 517 | signature_result_(kNoPendingResult), |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 518 | transport_security_state_(context.transport_security_state), |
estark | 6f9b3d8 | 2016-01-12 21:37:05 | [diff] [blame] | 519 | policy_enforcer_(context.ct_policy_enforcer), |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 520 | pkp_bypassed_(false), |
kulkarni.a | cd7b446 | 2014-08-28 07:41:34 | [diff] [blame] | 521 | net_log_(transport_->socket()->NetLog()), |
| 522 | weak_factory_(this) { |
rsleevi | be81cd6 | 2016-06-24 01:38:59 | [diff] [blame] | 523 | CHECK(cert_verifier_); |
| 524 | CHECK(transport_security_state_); |
| 525 | CHECK(cert_transparency_verifier_); |
| 526 | CHECK(policy_enforcer_); |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 527 | } |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 528 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 529 | SSLClientSocketImpl::~SSLClientSocketImpl() { |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 530 | Disconnect(); |
| 531 | } |
| 532 | |
davidben | 2a811e4e | 2015-12-01 10:49:34 | [diff] [blame] | 533 | #if !defined(OS_NACL) |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 534 | void SSLClientSocketImpl::SetSSLKeyLogFile( |
davidben | 2a811e4e | 2015-12-01 10:49:34 | [diff] [blame] | 535 | const base::FilePath& ssl_keylog_file, |
| 536 | const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
| 537 | SSLContext::GetInstance()->SetSSLKeyLogFile(ssl_keylog_file, task_runner); |
zhongyi | 81f85c6d9 | 2015-10-16 19:34:14 | [diff] [blame] | 538 | } |
davidben | 2a811e4e | 2015-12-01 10:49:34 | [diff] [blame] | 539 | #endif |
zhongyi | 81f85c6d9 | 2015-10-16 19:34:14 | [diff] [blame] | 540 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 541 | void SSLClientSocketImpl::GetSSLCertRequestInfo( |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 542 | SSLCertRequestInfo* cert_request_info) { |
[email protected] | 791879c | 2013-12-17 07:22:41 | [diff] [blame] | 543 | cert_request_info->host_and_port = host_and_port_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 544 | cert_request_info->cert_authorities = cert_authorities_; |
[email protected] | c078770 | 2014-05-20 21:51:44 | [diff] [blame] | 545 | cert_request_info->cert_key_types = cert_key_types_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 546 | } |
| 547 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 548 | ChannelIDService* SSLClientSocketImpl::GetChannelIDService() const { |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 549 | return channel_id_service_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 550 | } |
| 551 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 552 | Error SSLClientSocketImpl::GetSignedEKMForTokenBinding( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 553 | crypto::ECPrivateKey* key, |
| 554 | std::vector<uint8_t>* out) { |
| 555 | // The same key will be used across multiple requests to sign the same value, |
| 556 | // so the signature is cached. |
| 557 | std::string raw_public_key; |
| 558 | if (!key->ExportRawPublicKey(&raw_public_key)) |
| 559 | return ERR_FAILED; |
| 560 | SignedEkmMap::iterator it = tb_signed_ekm_map_.Get(raw_public_key); |
| 561 | if (it != tb_signed_ekm_map_.end()) { |
| 562 | *out = it->second; |
| 563 | return OK; |
| 564 | } |
| 565 | |
| 566 | uint8_t tb_ekm_buf[32]; |
| 567 | static const char kTokenBindingExporterLabel[] = "EXPORTER-Token-Binding"; |
| 568 | if (!SSL_export_keying_material(ssl_, tb_ekm_buf, sizeof(tb_ekm_buf), |
| 569 | kTokenBindingExporterLabel, |
| 570 | strlen(kTokenBindingExporterLabel), nullptr, |
| 571 | 0, false /* no context */)) { |
| 572 | return ERR_FAILED; |
| 573 | } |
| 574 | |
nharper | d5cddca | 2016-02-27 03:37:52 | [diff] [blame] | 575 | if (!SignTokenBindingEkm( |
| 576 | base::StringPiece(reinterpret_cast<char*>(tb_ekm_buf), |
| 577 | sizeof(tb_ekm_buf)), |
| 578 | key, out)) |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 579 | return ERR_FAILED; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 580 | |
| 581 | tb_signed_ekm_map_.Put(raw_public_key, *out); |
| 582 | return OK; |
| 583 | } |
| 584 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 585 | crypto::ECPrivateKey* SSLClientSocketImpl::GetChannelIDKey() const { |
nharper | b36644f | 2016-02-22 23:14:43 | [diff] [blame] | 586 | return channel_id_key_.get(); |
| 587 | } |
| 588 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 589 | int SSLClientSocketImpl::ExportKeyingMaterial(const base::StringPiece& label, |
| 590 | bool has_context, |
| 591 | const base::StringPiece& context, |
| 592 | unsigned char* out, |
| 593 | unsigned int outlen) { |
davidben | 86935f7 | 2015-05-06 22:24:49 | [diff] [blame] | 594 | if (!IsConnected()) |
| 595 | return ERR_SOCKET_NOT_CONNECTED; |
| 596 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 597 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 598 | |
| 599 | int rv = SSL_export_keying_material( |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 600 | ssl_, out, outlen, label.data(), label.size(), |
davidben | 866c3d4a7 | 2015-04-06 21:56:43 | [diff] [blame] | 601 | reinterpret_cast<const unsigned char*>(context.data()), context.length(), |
| 602 | has_context ? 1 : 0); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 603 | |
| 604 | if (rv != 1) { |
| 605 | int ssl_error = SSL_get_error(ssl_, rv); |
| 606 | LOG(ERROR) << "Failed to export keying material;" |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 607 | << " returned " << rv << ", SSL error code " << ssl_error; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 608 | return MapOpenSSLError(ssl_error, err_tracer); |
| 609 | } |
| 610 | return OK; |
| 611 | } |
| 612 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 613 | int SSLClientSocketImpl::Connect(const CompletionCallback& callback) { |
svaldez | 4af14d2 | 2015-08-20 13:48:24 | [diff] [blame] | 614 | // Although StreamSocket does allow calling Connect() after Disconnect(), |
| 615 | // this has never worked for layered sockets. CHECK to detect any consumers |
| 616 | // reconnecting an SSL socket. |
| 617 | // |
| 618 | // TODO(davidben,mmenke): Remove this API feature. See |
| 619 | // https://crbug.com/499289. |
| 620 | CHECK(!disconnected_); |
| 621 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 622 | net_log_.BeginEvent(NetLog::TYPE_SSL_CONNECT); |
| 623 | |
| 624 | // Set up new ssl object. |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 625 | int rv = Init(); |
| 626 | if (rv != OK) { |
davidben | 281d13f0 | 2016-04-27 20:43:28 | [diff] [blame] | 627 | LogConnectEndEvent(rv); |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 628 | return rv; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | // Set SSL to client mode. Handshake happens in the loop below. |
| 632 | SSL_set_connect_state(ssl_); |
| 633 | |
rsleevi | adbd498 | 2016-06-13 22:10:27 | [diff] [blame] | 634 | next_handshake_state_ = STATE_HANDSHAKE; |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 635 | rv = DoHandshakeLoop(OK); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 636 | if (rv == ERR_IO_PENDING) { |
| 637 | user_connect_callback_ = callback; |
| 638 | } else { |
davidben | 281d13f0 | 2016-04-27 20:43:28 | [diff] [blame] | 639 | LogConnectEndEvent(rv); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | return rv > OK ? OK : rv; |
| 643 | } |
| 644 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 645 | void SSLClientSocketImpl::Disconnect() { |
davidben | ed3764bb | 2016-03-14 19:53:16 | [diff] [blame] | 646 | crypto::OpenSSLErrStackTracer tracer(FROM_HERE); |
| 647 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 648 | if (ssl_) { |
| 649 | // Calling SSL_shutdown prevents the session from being marked as |
| 650 | // unresumable. |
| 651 | SSL_shutdown(ssl_); |
| 652 | SSL_free(ssl_); |
| 653 | ssl_ = NULL; |
| 654 | } |
| 655 | if (transport_bio_) { |
| 656 | BIO_free_all(transport_bio_); |
| 657 | transport_bio_ = NULL; |
| 658 | } |
| 659 | |
svaldez | 4af14d2 | 2015-08-20 13:48:24 | [diff] [blame] | 660 | disconnected_ = true; |
| 661 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 662 | // Shut down anything that may call us back. |
eroman | 7f9236a | 2015-05-11 21:23:43 | [diff] [blame] | 663 | cert_verifier_request_.reset(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 664 | transport_->socket()->Disconnect(); |
| 665 | |
| 666 | // Null all callbacks, delete all buffers. |
| 667 | transport_send_busy_ = false; |
| 668 | send_buffer_ = NULL; |
| 669 | transport_recv_busy_ = false; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 670 | recv_buffer_ = NULL; |
| 671 | |
| 672 | user_connect_callback_.Reset(); |
| 673 | user_read_callback_.Reset(); |
| 674 | user_write_callback_.Reset(); |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 675 | user_read_buf_ = NULL; |
| 676 | user_read_buf_len_ = 0; |
| 677 | user_write_buf_ = NULL; |
| 678 | user_write_buf_len_ = 0; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 679 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 680 | pending_read_error_ = kNoPendingResult; |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 681 | pending_read_ssl_error_ = SSL_ERROR_NONE; |
| 682 | pending_read_error_info_ = OpenSSLErrorInfo(); |
| 683 | |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 684 | transport_read_error_ = OK; |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 685 | transport_write_error_ = OK; |
| 686 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 687 | server_cert_verify_result_.Reset(); |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 688 | completed_connect_ = false; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 689 | |
| 690 | cert_authorities_.clear(); |
[email protected] | c078770 | 2014-05-20 21:51:44 | [diff] [blame] | 691 | cert_key_types_.clear(); |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 692 | |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 693 | start_cert_verification_time_ = base::TimeTicks(); |
| 694 | |
[email protected] | abc44b75 | 2014-07-30 03:52:15 | [diff] [blame] | 695 | npn_status_ = kNextProtoUnsupported; |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 696 | negotiated_protocol_ = kProtoUnknown; |
[email protected] | abc44b75 | 2014-07-30 03:52:15 | [diff] [blame] | 697 | |
davidben | 52053b38 | 2015-04-27 19:22:29 | [diff] [blame] | 698 | channel_id_sent_ = false; |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame] | 699 | tb_was_negotiated_ = false; |
davidben | c269cc4b | 2016-07-27 14:55:03 | [diff] [blame] | 700 | pending_session_ = nullptr; |
davidben | f2eaaf9 | 2015-05-15 22:18:42 | [diff] [blame] | 701 | certificate_verified_ = false; |
nharper | 75ade89 | 2015-06-10 19:05:35 | [diff] [blame] | 702 | channel_id_request_.Cancel(); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 703 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 704 | signature_result_ = kNoPendingResult; |
| 705 | signature_.clear(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 706 | } |
| 707 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 708 | bool SSLClientSocketImpl::IsConnected() const { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 709 | // If the handshake has not yet completed. |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 710 | if (!completed_connect_) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 711 | return false; |
| 712 | // If an asynchronous operation is still pending. |
| 713 | if (user_read_buf_.get() || user_write_buf_.get()) |
| 714 | return true; |
| 715 | |
| 716 | return transport_->socket()->IsConnected(); |
| 717 | } |
| 718 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 719 | bool SSLClientSocketImpl::IsConnectedAndIdle() const { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 720 | // If the handshake has not yet completed. |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 721 | if (!completed_connect_) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 722 | return false; |
| 723 | // If an asynchronous operation is still pending. |
| 724 | if (user_read_buf_.get() || user_write_buf_.get()) |
| 725 | return false; |
davidben | fc9a6b8 | 2015-04-15 23:47:32 | [diff] [blame] | 726 | |
| 727 | // If there is data read from the network that has not yet been consumed, do |
| 728 | // not treat the connection as idle. |
| 729 | // |
| 730 | // Note that this does not check |BIO_pending|, whether there is ciphertext |
| 731 | // that has not yet been flushed to the network. |Write| returns early, so |
| 732 | // this can cause race conditions which cause a socket to not be treated |
| 733 | // reusable when it should be. See https://crbug.com/466147. |
| 734 | if (BIO_wpending(transport_bio_) > 0) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 735 | return false; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 736 | |
| 737 | return transport_->socket()->IsConnectedAndIdle(); |
| 738 | } |
| 739 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 740 | int SSLClientSocketImpl::GetPeerAddress(IPEndPoint* addressList) const { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 741 | return transport_->socket()->GetPeerAddress(addressList); |
| 742 | } |
| 743 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 744 | int SSLClientSocketImpl::GetLocalAddress(IPEndPoint* addressList) const { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 745 | return transport_->socket()->GetLocalAddress(addressList); |
| 746 | } |
| 747 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 748 | const BoundNetLog& SSLClientSocketImpl::NetLog() const { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 749 | return net_log_; |
| 750 | } |
| 751 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 752 | void SSLClientSocketImpl::SetSubresourceSpeculation() { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 753 | if (transport_.get() && transport_->socket()) { |
| 754 | transport_->socket()->SetSubresourceSpeculation(); |
| 755 | } else { |
| 756 | NOTREACHED(); |
| 757 | } |
| 758 | } |
| 759 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 760 | void SSLClientSocketImpl::SetOmniboxSpeculation() { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 761 | if (transport_.get() && transport_->socket()) { |
| 762 | transport_->socket()->SetOmniboxSpeculation(); |
| 763 | } else { |
| 764 | NOTREACHED(); |
| 765 | } |
| 766 | } |
| 767 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 768 | bool SSLClientSocketImpl::WasEverUsed() const { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 769 | return was_ever_used_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 770 | } |
| 771 | |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 772 | bool SSLClientSocketImpl::WasNpnNegotiated() const { |
| 773 | return negotiated_protocol_ != kProtoUnknown; |
| 774 | } |
| 775 | |
| 776 | NextProto SSLClientSocketImpl::GetNegotiatedProtocol() const { |
| 777 | return negotiated_protocol_; |
| 778 | } |
| 779 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 780 | bool SSLClientSocketImpl::GetSSLInfo(SSLInfo* ssl_info) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 781 | ssl_info->Reset(); |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 782 | if (server_cert_chain_->empty()) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 783 | return false; |
| 784 | |
| 785 | ssl_info->cert = server_cert_verify_result_.verified_cert; |
estark | 03d644f | 2015-06-13 00:11:32 | [diff] [blame] | 786 | ssl_info->unverified_cert = server_cert_; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 787 | ssl_info->cert_status = server_cert_verify_result_.cert_status; |
| 788 | ssl_info->is_issued_by_known_root = |
| 789 | server_cert_verify_result_.is_issued_by_known_root; |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 790 | ssl_info->pkp_bypassed = pkp_bypassed_; |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 791 | ssl_info->public_key_hashes = server_cert_verify_result_.public_key_hashes; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 792 | ssl_info->client_cert_sent = |
| 793 | ssl_config_.send_client_cert && ssl_config_.client_cert.get(); |
davidben | 52053b38 | 2015-04-27 19:22:29 | [diff] [blame] | 794 | ssl_info->channel_id_sent = channel_id_sent_; |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame] | 795 | ssl_info->token_binding_negotiated = tb_was_negotiated_; |
| 796 | ssl_info->token_binding_key_param = tb_negotiated_param_; |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 797 | ssl_info->pinning_failure_log = pinning_failure_log_; |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 798 | ssl_info->ocsp_result = server_cert_verify_result_.ocsp_result; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 799 | |
estark | 723b5eeb | 2016-02-18 21:01:12 | [diff] [blame] | 800 | AddCTInfoToSSLInfo(ssl_info); |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 801 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 802 | const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl_); |
| 803 | CHECK(cipher); |
| 804 | ssl_info->security_bits = SSL_CIPHER_get_bits(cipher, NULL); |
davidben | c269cc4b | 2016-07-27 14:55:03 | [diff] [blame] | 805 | if (SSL_CIPHER_is_ECDHE(cipher)) { |
| 806 | ssl_info->key_exchange_info = SSL_get_curve_id(ssl_); |
| 807 | } else if (SSL_CIPHER_is_DHE(cipher)) { |
| 808 | ssl_info->key_exchange_info = SSL_get_dhe_group_size(ssl_); |
| 809 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 810 | |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 811 | SSLConnectionStatusSetCipherSuite( |
| 812 | static_cast<uint16_t>(SSL_CIPHER_get_id(cipher)), |
| 813 | &ssl_info->connection_status); |
| 814 | SSLConnectionStatusSetVersion(GetNetSSLVersion(ssl_), |
| 815 | &ssl_info->connection_status); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 816 | |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 817 | if (!SSL_get_secure_renegotiation_support(ssl_)) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 818 | ssl_info->connection_status |= SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 819 | |
| 820 | if (ssl_config_.version_fallback) |
| 821 | ssl_info->connection_status |= SSL_CONNECTION_VERSION_FALLBACK; |
| 822 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 823 | ssl_info->handshake_type = SSL_session_reused(ssl_) |
| 824 | ? SSLInfo::HANDSHAKE_RESUME |
| 825 | : SSLInfo::HANDSHAKE_FULL; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 826 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 827 | return true; |
| 828 | } |
| 829 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 830 | void SSLClientSocketImpl::GetConnectionAttempts(ConnectionAttempts* out) const { |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 831 | out->clear(); |
| 832 | } |
| 833 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 834 | int64_t SSLClientSocketImpl::GetTotalReceivedBytes() const { |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 835 | return transport_->socket()->GetTotalReceivedBytes(); |
| 836 | } |
| 837 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 838 | int SSLClientSocketImpl::Read(IOBuffer* buf, |
| 839 | int buf_len, |
| 840 | const CompletionCallback& callback) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 841 | user_read_buf_ = buf; |
| 842 | user_read_buf_len_ = buf_len; |
| 843 | |
davidben | 1b133ad | 2014-10-23 04:23:13 | [diff] [blame] | 844 | int rv = DoReadLoop(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 845 | |
| 846 | if (rv == ERR_IO_PENDING) { |
| 847 | user_read_callback_ = callback; |
| 848 | } else { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 849 | if (rv > 0) |
| 850 | was_ever_used_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 851 | user_read_buf_ = NULL; |
| 852 | user_read_buf_len_ = 0; |
| 853 | } |
| 854 | |
| 855 | return rv; |
| 856 | } |
| 857 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 858 | int SSLClientSocketImpl::Write(IOBuffer* buf, |
| 859 | int buf_len, |
| 860 | const CompletionCallback& callback) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 861 | user_write_buf_ = buf; |
| 862 | user_write_buf_len_ = buf_len; |
| 863 | |
davidben | 1b133ad | 2014-10-23 04:23:13 | [diff] [blame] | 864 | int rv = DoWriteLoop(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 865 | |
| 866 | if (rv == ERR_IO_PENDING) { |
| 867 | user_write_callback_ = callback; |
| 868 | } else { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 869 | if (rv > 0) |
| 870 | was_ever_used_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 871 | user_write_buf_ = NULL; |
| 872 | user_write_buf_len_ = 0; |
| 873 | } |
| 874 | |
| 875 | return rv; |
| 876 | } |
| 877 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 878 | int SSLClientSocketImpl::SetReceiveBufferSize(int32_t size) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 879 | return transport_->socket()->SetReceiveBufferSize(size); |
| 880 | } |
| 881 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 882 | int SSLClientSocketImpl::SetSendBufferSize(int32_t size) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 883 | return transport_->socket()->SetSendBufferSize(size); |
| 884 | } |
| 885 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 886 | int SSLClientSocketImpl::Init() { |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 887 | DCHECK(!ssl_); |
| 888 | DCHECK(!transport_bio_); |
| 889 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 890 | #if defined(USE_NSS_CERTS) |
davidben | a477a70a | 2015-10-06 04:38:29 | [diff] [blame] | 891 | if (ssl_config_.cert_io_enabled) { |
| 892 | // TODO(davidben): Move this out of SSLClientSocket. See |
| 893 | // https://crbug.com/539520. |
| 894 | EnsureNSSHttpIOInit(); |
| 895 | } |
| 896 | #endif |
| 897 | |
[email protected] | b29af7d | 2010-12-14 11:52:47 | [diff] [blame] | 898 | SSLContext* context = SSLContext::GetInstance(); |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 899 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 900 | |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 901 | ssl_ = SSL_new(context->ssl_ctx()); |
| 902 | if (!ssl_ || !context->SetClientSocketForSSL(ssl_, this)) |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 903 | return ERR_UNEXPECTED; |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 904 | |
davidben | 9bc0466f | 2015-06-16 22:21:27 | [diff] [blame] | 905 | // SNI should only contain valid DNS hostnames, not IP addresses (see RFC |
| 906 | // 6066, Section 3). |
| 907 | // |
| 908 | // TODO(rsleevi): Should this code allow hostnames that violate the LDH rule? |
| 909 | // See https://crbug.com/496472 and https://crbug.com/496468 for discussion. |
martijn | a2e83bd | 2016-03-18 13:10:45 | [diff] [blame] | 910 | IPAddress unused; |
| 911 | if (!unused.AssignFromIPLiteral(host_and_port_.host()) && |
davidben | 9bc0466f | 2015-06-16 22:21:27 | [diff] [blame] | 912 | !SSL_set_tlsext_host_name(ssl_, host_and_port_.host().c_str())) { |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 913 | return ERR_UNEXPECTED; |
davidben | 9bc0466f | 2015-06-16 22:21:27 | [diff] [blame] | 914 | } |
[email protected] | fbef1393 | 2010-11-23 12:38:53 | [diff] [blame] | 915 | |
davidben | f4c9a612 | 2015-10-20 02:45:29 | [diff] [blame] | 916 | ScopedSSL_SESSION session = |
| 917 | context->session_cache()->Lookup(GetSessionCacheKey()); |
| 918 | if (session) |
| 919 | SSL_set_session(ssl_, session.get()); |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 920 | |
mmenke | 1beda3d | 2016-07-22 03:33:45 | [diff] [blame] | 921 | // Get read and write buffer sizes from field trials, if possible. If values |
| 922 | // not present, use default. Also make sure values are in reasonable range. |
| 923 | int send_buffer_size = kDefaultOpenSSLBufferSize; |
| 924 | #if !defined(OS_NACL) |
| 925 | int override_send_buffer_size; |
| 926 | if (base::StringToInt(base::FieldTrialList::FindFullName("SSLBufferSizeSend"), |
| 927 | &override_send_buffer_size)) { |
| 928 | send_buffer_size = override_send_buffer_size; |
| 929 | send_buffer_size = std::max(send_buffer_size, 1000); |
| 930 | send_buffer_size = |
| 931 | std::min(send_buffer_size, 2 * kDefaultOpenSSLBufferSize); |
| 932 | } |
| 933 | #endif // !defined(OS_NACL) |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 934 | send_buffer_ = new GrowableIOBuffer(); |
mmenke | 1beda3d | 2016-07-22 03:33:45 | [diff] [blame] | 935 | send_buffer_->SetCapacity(send_buffer_size); |
| 936 | |
| 937 | int recv_buffer_size = kDefaultOpenSSLBufferSize; |
| 938 | #if !defined(OS_NACL) |
| 939 | int override_recv_buffer_size; |
| 940 | if (base::StringToInt(base::FieldTrialList::FindFullName("SSLBufferSizeRecv"), |
| 941 | &override_recv_buffer_size)) { |
| 942 | recv_buffer_size = override_recv_buffer_size; |
| 943 | recv_buffer_size = std::max(recv_buffer_size, 1000); |
| 944 | recv_buffer_size = |
| 945 | std::min(recv_buffer_size, 2 * kDefaultOpenSSLBufferSize); |
| 946 | } |
| 947 | #endif // !defined(OS_NACL) |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 948 | recv_buffer_ = new GrowableIOBuffer(); |
mmenke | 1beda3d | 2016-07-22 03:33:45 | [diff] [blame] | 949 | recv_buffer_->SetCapacity(recv_buffer_size); |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 950 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 951 | BIO* ssl_bio = NULL; |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 952 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 953 | // SSLClientSocketImpl retains ownership of the BIO buffers. |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 954 | if (!BIO_new_bio_pair_external_buf( |
| 955 | &ssl_bio, send_buffer_->capacity(), |
| 956 | reinterpret_cast<uint8_t*>(send_buffer_->data()), &transport_bio_, |
| 957 | recv_buffer_->capacity(), |
| 958 | reinterpret_cast<uint8_t*>(recv_buffer_->data()))) |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 959 | return ERR_UNEXPECTED; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 960 | DCHECK(ssl_bio); |
| 961 | DCHECK(transport_bio_); |
| 962 | |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 963 | // Install a callback on OpenSSL's end to plumb transport errors through. |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 964 | BIO_set_callback(ssl_bio, &SSLClientSocketImpl::BIOCallback); |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 965 | BIO_set_callback_arg(ssl_bio, reinterpret_cast<char*>(this)); |
| 966 | |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 967 | SSL_set_bio(ssl_, ssl_bio, ssl_bio); |
| 968 | |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 969 | DCHECK_LT(SSL3_VERSION, ssl_config_.version_min); |
| 970 | DCHECK_LT(SSL3_VERSION, ssl_config_.version_max); |
| 971 | SSL_set_min_version(ssl_, ssl_config_.version_min); |
| 972 | SSL_set_max_version(ssl_, ssl_config_.version_max); |
| 973 | |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 974 | // OpenSSL defaults some options to on, others to off. To avoid ambiguity, |
| 975 | // set everything we care about to an absolute value. |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 976 | SslSetClearMask options; |
[email protected] | d0f0049 | 2012-08-03 22:35:13 | [diff] [blame] | 977 | options.ConfigureFlag(SSL_OP_NO_COMPRESSION, true); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 978 | |
| 979 | // TODO(joth): Set this conditionally, see http://crbug.com/55410 |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 980 | options.ConfigureFlag(SSL_OP_LEGACY_SERVER_CONNECT, true); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 981 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 982 | SSL_set_options(ssl_, options.set_mask); |
| 983 | SSL_clear_options(ssl_, options.clear_mask); |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 984 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 985 | // Same as above, this time for the SSL mode. |
| 986 | SslSetClearMask mode; |
[email protected] | 9e733f3 | 2010-10-04 18:19:08 | [diff] [blame] | 987 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 988 | mode.ConfigureFlag(SSL_MODE_RELEASE_BUFFERS, true); |
isherman | e5c05e1 | 2014-09-09 20:32:15 | [diff] [blame] | 989 | mode.ConfigureFlag(SSL_MODE_CBC_RECORD_SPLITTING, true); |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 990 | |
davidben | 818d93b | 2015-02-19 22:27:32 | [diff] [blame] | 991 | mode.ConfigureFlag(SSL_MODE_ENABLE_FALSE_START, |
[email protected] | b788de0 | 2014-04-23 18:06:07 | [diff] [blame] | 992 | ssl_config_.false_start_enabled); |
| 993 | |
davidben | 6b8131c | 2015-02-25 23:30:14 | [diff] [blame] | 994 | mode.ConfigureFlag(SSL_MODE_SEND_FALLBACK_SCSV, ssl_config_.version_fallback); |
| 995 | |
[email protected] | fb10e228 | 2010-12-01 17:08:48 | [diff] [blame] | 996 | SSL_set_mode(ssl_, mode.set_mask); |
| 997 | SSL_clear_mode(ssl_, mode.clear_mask); |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 998 | |
davidben | b3c2d97 | 2016-03-11 22:36:17 | [diff] [blame] | 999 | // Use BoringSSL defaults, but disable HMAC-SHA256 and HMAC-SHA384 ciphers |
| 1000 | // (note that SHA256 and SHA384 only select legacy CBC ciphers). Also disable |
| 1001 | // DHE_RSA_WITH_AES_256_GCM_SHA384. Historically, AES_256_GCM was not |
| 1002 | // supported. As DHE is being deprecated, don't add a cipher only to remove it |
| 1003 | // immediately. |
petewil | ec5f5df22 | 2016-06-29 17:21:34 | [diff] [blame] | 1004 | std::string command; |
mab | e0d8c58 | 2016-08-12 02:06:40 | [diff] [blame] | 1005 | if (SSLClientSocket::IsPostQuantumExperimentEnabled()) { |
petewil | ec5f5df22 | 2016-06-29 17:21:34 | [diff] [blame] | 1006 | // These are experimental, non-standard ciphersuites. They are part of an |
| 1007 | // experiment in post-quantum cryptography. They're not intended to |
| 1008 | // represent a de-facto standard, and will be removed from BoringSSL in |
| 1009 | // ~2018. |
| 1010 | if (EVP_has_aes_hardware()) { |
| 1011 | command.append( |
| 1012 | "CECPQ1-RSA-AES256-GCM-SHA384:" |
| 1013 | "CECPQ1-ECDSA-AES256-GCM-SHA384:"); |
| 1014 | } |
| 1015 | command.append( |
| 1016 | "CECPQ1-RSA-CHACHA20-POLY1305-SHA256:" |
| 1017 | "CECPQ1-ECDSA-CHACHA20-POLY1305-SHA256:"); |
| 1018 | if (!EVP_has_aes_hardware()) { |
| 1019 | command.append( |
| 1020 | "CECPQ1-RSA-AES256-GCM-SHA384:" |
| 1021 | "CECPQ1-ECDSA-AES256-GCM-SHA384:"); |
| 1022 | } |
| 1023 | } |
petewil | ec5f5df22 | 2016-06-29 17:21:34 | [diff] [blame] | 1024 | command.append("ALL:!SHA256:!SHA384:!DHE-RSA-AES256-GCM-SHA384:!aPSK:!RC4"); |
davidben | 9b4a9b9c | 2015-10-12 18:46:51 | [diff] [blame] | 1025 | |
| 1026 | if (ssl_config_.require_ecdhe) |
| 1027 | command.append(":!kRSA:!kDHE"); |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 1028 | |
davidben | 68dcc58 | 2016-01-12 22:09:36 | [diff] [blame] | 1029 | if (!ssl_config_.deprecated_cipher_suites_enabled) { |
davidben | 01d8e33 | 2015-11-20 17:08:58 | [diff] [blame] | 1030 | // Only offer DHE on the second handshake. https://crbug.com/538690 |
| 1031 | command.append(":!kDHE"); |
davidben | 9158569 | 2015-09-28 23:46:08 | [diff] [blame] | 1032 | } |
davidben | a4c9d06 | 2015-04-03 22:34:25 | [diff] [blame] | 1033 | |
davidben | 9b4a9b9c | 2015-10-12 18:46:51 | [diff] [blame] | 1034 | // Remove any disabled ciphers. |
| 1035 | for (uint16_t id : ssl_config_.disabled_cipher_suites) { |
| 1036 | const SSL_CIPHER* cipher = SSL_get_cipher_by_value(id); |
| 1037 | if (cipher) { |
| 1038 | command.append(":!"); |
| 1039 | command.append(SSL_CIPHER_get_name(cipher)); |
| 1040 | } |
| 1041 | } |
| 1042 | |
[email protected] | 109805a | 2010-12-07 18:17:06 | [diff] [blame] | 1043 | int rv = SSL_set_cipher_list(ssl_, command.c_str()); |
| 1044 | // If this fails (rv = 0) it means there are no ciphers enabled on this SSL. |
| 1045 | // This will almost certainly result in the socket failing to complete the |
| 1046 | // handshake at which point the appropriate error is bubbled up to the client. |
| 1047 | LOG_IF(WARNING, rv != 1) << "SSL_set_cipher_list('" << command << "') " |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1048 | "returned " |
| 1049 | << rv; |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 1050 | |
| 1051 | // TLS channel ids. |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 1052 | if (IsChannelIDEnabled()) { |
[email protected] | ee0f2aa8 | 2013-10-25 11:59:26 | [diff] [blame] | 1053 | SSL_enable_tls_channel_id(ssl_); |
| 1054 | } |
| 1055 | |
bnc | 1f29537 | 2015-10-21 23:24:22 | [diff] [blame] | 1056 | if (!ssl_config_.alpn_protos.empty()) { |
bnc | 988e68d | 2016-06-27 14:03:21 | [diff] [blame] | 1057 | std::vector<uint8_t> wire_protos = |
| 1058 | SerializeNextProtos(ssl_config_.alpn_protos); |
[email protected] | abc44b75 | 2014-07-30 03:52:15 | [diff] [blame] | 1059 | SSL_set_alpn_protos(ssl_, wire_protos.empty() ? NULL : &wire_protos[0], |
| 1060 | wire_protos.size()); |
| 1061 | } |
| 1062 | |
bnc | 2d6bd0f | 2015-10-28 01:52:05 | [diff] [blame] | 1063 | if (ssl_config_.npn_protos.empty()) |
| 1064 | SSL_set_options(ssl_, SSL_OP_DISABLE_NPN); |
| 1065 | |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 1066 | if (ssl_config_.signed_cert_timestamps_enabled) { |
| 1067 | SSL_enable_signed_cert_timestamps(ssl_); |
| 1068 | SSL_enable_ocsp_stapling(ssl_); |
| 1069 | } |
| 1070 | |
davidben | 15f5713 | 2015-04-27 18:08:36 | [diff] [blame] | 1071 | if (cert_verifier_->SupportsOCSPStapling()) |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 1072 | SSL_enable_ocsp_stapling(ssl_); |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 1073 | |
[email protected] | c8a80e9 | 2014-05-17 16:02:08 | [diff] [blame] | 1074 | return OK; |
[email protected] | d518cd9 | 2010-09-29 12:27:44 | [diff] [blame] | 1075 | } |
| 1076 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1077 | void SSLClientSocketImpl::DoReadCallback(int rv) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1078 | // Since Run may result in Read being called, clear |user_read_callback_| |
| 1079 | // up front. |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 1080 | if (rv > 0) |
| 1081 | was_ever_used_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1082 | user_read_buf_ = NULL; |
| 1083 | user_read_buf_len_ = 0; |
| 1084 | base::ResetAndReturn(&user_read_callback_).Run(rv); |
| 1085 | } |
| 1086 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1087 | void SSLClientSocketImpl::DoWriteCallback(int rv) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1088 | // Since Run may result in Write being called, clear |user_write_callback_| |
| 1089 | // up front. |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 1090 | if (rv > 0) |
| 1091 | was_ever_used_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1092 | user_write_buf_ = NULL; |
| 1093 | user_write_buf_len_ = 0; |
| 1094 | base::ResetAndReturn(&user_write_callback_).Run(rv); |
| 1095 | } |
| 1096 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1097 | bool SSLClientSocketImpl::DoTransportIO() { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1098 | bool network_moved = false; |
| 1099 | int rv; |
| 1100 | // Read and write as much data as possible. The loop is necessary because |
| 1101 | // Write() may return synchronously. |
| 1102 | do { |
| 1103 | rv = BufferSend(); |
| 1104 | if (rv != ERR_IO_PENDING && rv != 0) |
| 1105 | network_moved = true; |
| 1106 | } while (rv > 0); |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 1107 | if (transport_read_error_ == OK && BufferRecv() != ERR_IO_PENDING) |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1108 | network_moved = true; |
| 1109 | return network_moved; |
| 1110 | } |
| 1111 | |
pkasting | 379234c | 2015-04-08 04:42:12 | [diff] [blame] | 1112 | // TODO(cbentzel): Remove including "base/threading/thread_local.h" and |
vadimt | 6b43dec2 | 2015-01-06 01:59:58 | [diff] [blame] | 1113 | // g_first_run_completed once crbug.com/424386 is fixed. |
| 1114 | base::LazyInstance<base::ThreadLocalBoolean>::Leaky g_first_run_completed = |
| 1115 | LAZY_INSTANCE_INITIALIZER; |
| 1116 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1117 | int SSLClientSocketImpl::DoHandshake() { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1118 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
vadimt | 5a24328 | 2014-12-24 00:26:16 | [diff] [blame] | 1119 | |
| 1120 | int rv; |
| 1121 | |
pkasting | 379234c | 2015-04-08 04:42:12 | [diff] [blame] | 1122 | // TODO(cbentzel): Leave only 1 call to SSL_do_handshake once crbug.com/424386 |
vadimt | 5a24328 | 2014-12-24 00:26:16 | [diff] [blame] | 1123 | // is fixed. |
| 1124 | if (ssl_config_.send_client_cert && ssl_config_.client_cert.get()) { |
vadimt | 5a24328 | 2014-12-24 00:26:16 | [diff] [blame] | 1125 | rv = SSL_do_handshake(ssl_); |
| 1126 | } else { |
vadimt | 6b43dec2 | 2015-01-06 01:59:58 | [diff] [blame] | 1127 | if (g_first_run_completed.Get().Get()) { |
pkasting | 379234c | 2015-04-08 04:42:12 | [diff] [blame] | 1128 | // TODO(cbentzel): Remove ScopedTracker below once crbug.com/424386 is |
vadimt | 6b43dec2 | 2015-01-06 01:59:58 | [diff] [blame] | 1129 | // fixed. |
pkasting | 379234c | 2015-04-08 04:42:12 | [diff] [blame] | 1130 | tracked_objects::ScopedTracker tracking_profile( |
| 1131 | FROM_HERE_WITH_EXPLICIT_FUNCTION("424386 SSL_do_handshake()")); |
vadimt | 5a24328 | 2014-12-24 00:26:16 | [diff] [blame] | 1132 | |
vadimt | 6b43dec2 | 2015-01-06 01:59:58 | [diff] [blame] | 1133 | rv = SSL_do_handshake(ssl_); |
| 1134 | } else { |
| 1135 | g_first_run_completed.Get().Set(true); |
vadimt | 6b43dec2 | 2015-01-06 01:59:58 | [diff] [blame] | 1136 | rv = SSL_do_handshake(ssl_); |
| 1137 | } |
vadimt | 5a24328 | 2014-12-24 00:26:16 | [diff] [blame] | 1138 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1139 | |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1140 | int net_error = OK; |
| 1141 | if (rv <= 0) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1142 | int ssl_error = SSL_get_error(ssl_, rv); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1143 | if (ssl_error == SSL_ERROR_WANT_CHANNEL_ID_LOOKUP) { |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1144 | // The server supports channel ID. Stop to look one up before returning to |
| 1145 | // the handshake. |
rsleevi | adbd498 | 2016-06-13 22:10:27 | [diff] [blame] | 1146 | next_handshake_state_ = STATE_CHANNEL_ID_LOOKUP; |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1147 | return OK; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1148 | } |
davidben | ced4aa9b | 2015-05-12 21:22:35 | [diff] [blame] | 1149 | if (ssl_error == SSL_ERROR_WANT_X509_LOOKUP && |
| 1150 | !ssl_config_.send_client_cert) { |
| 1151 | return ERR_SSL_CLIENT_AUTH_CERT_NEEDED; |
| 1152 | } |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1153 | if (ssl_error == SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) { |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 1154 | DCHECK(ssl_config_.client_private_key); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1155 | DCHECK_NE(kNoPendingResult, signature_result_); |
rsleevi | adbd498 | 2016-06-13 22:10:27 | [diff] [blame] | 1156 | next_handshake_state_ = STATE_HANDSHAKE; |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1157 | return ERR_IO_PENDING; |
| 1158 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1159 | |
davidben | a4409c6 | 2014-08-27 17:05:51 | [diff] [blame] | 1160 | OpenSSLErrorInfo error_info; |
| 1161 | net_error = MapOpenSSLErrorWithDetails(ssl_error, err_tracer, &error_info); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1162 | if (net_error == ERR_IO_PENDING) { |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1163 | // If not done, stay in this state |
rsleevi | adbd498 | 2016-06-13 22:10:27 | [diff] [blame] | 1164 | next_handshake_state_ = STATE_HANDSHAKE; |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1165 | return ERR_IO_PENDING; |
| 1166 | } |
| 1167 | |
| 1168 | LOG(ERROR) << "handshake failed; returned " << rv << ", SSL error code " |
| 1169 | << ssl_error << ", net_error " << net_error; |
| 1170 | net_log_.AddEvent( |
| 1171 | NetLog::TYPE_SSL_HANDSHAKE_ERROR, |
| 1172 | CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info)); |
| 1173 | } |
| 1174 | |
rsleevi | adbd498 | 2016-06-13 22:10:27 | [diff] [blame] | 1175 | next_handshake_state_ = STATE_HANDSHAKE_COMPLETE; |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1176 | return net_error; |
| 1177 | } |
| 1178 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1179 | int SSLClientSocketImpl::DoHandshakeComplete(int result) { |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1180 | if (result < 0) |
| 1181 | return result; |
| 1182 | |
| 1183 | if (ssl_config_.version_fallback && |
| 1184 | ssl_config_.version_max < ssl_config_.version_fallback_min) { |
| 1185 | return ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION; |
| 1186 | } |
| 1187 | |
davidben | b4c25b6 | 2016-06-24 02:39:27 | [diff] [blame] | 1188 | // DHE is offered on the deprecated cipher fallback and then rejected |
| 1189 | // afterwards. This is to aid in diagnosing connection failures because a |
| 1190 | // server requires DHE ciphers. |
| 1191 | // |
| 1192 | // TODO(davidben): A few releases after DHE's removal, remove this logic. |
| 1193 | if (!ssl_config_.dhe_enabled && |
| 1194 | SSL_CIPHER_is_DHE(SSL_get_current_cipher(ssl_))) { |
| 1195 | return ERR_SSL_OBSOLETE_CIPHER; |
| 1196 | } |
| 1197 | |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame] | 1198 | // Check that if token binding was negotiated, then extended master secret |
| 1199 | // must also be negotiated. |
| 1200 | if (tb_was_negotiated_ && !SSL_get_extms_support(ssl_)) |
| 1201 | return ERR_SSL_PROTOCOL_ERROR; |
| 1202 | |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1203 | // SSL handshake is completed. If NPN wasn't negotiated, see if ALPN was. |
| 1204 | if (npn_status_ == kNextProtoUnsupported) { |
| 1205 | const uint8_t* alpn_proto = NULL; |
| 1206 | unsigned alpn_len = 0; |
| 1207 | SSL_get0_alpn_selected(ssl_, &alpn_proto, &alpn_len); |
| 1208 | if (alpn_len > 0) { |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 1209 | base::StringPiece proto(reinterpret_cast<const char*>(alpn_proto), |
| 1210 | alpn_len); |
| 1211 | negotiated_protocol_ = NextProtoFromString(proto); |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1212 | npn_status_ = kNextProtoNegotiated; |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 1213 | negotiation_extension_ = kExtensionALPN; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1214 | } |
| 1215 | } |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1216 | |
| 1217 | RecordNegotiationExtension(); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 1218 | RecordChannelIDSupport(); |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1219 | |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 1220 | const uint8_t* ocsp_response_raw; |
| 1221 | size_t ocsp_response_len; |
| 1222 | SSL_get0_ocsp_response(ssl_, &ocsp_response_raw, &ocsp_response_len); |
| 1223 | std::string ocsp_response; |
| 1224 | if (ocsp_response_len > 0) { |
| 1225 | ocsp_response_.assign(reinterpret_cast<const char*>(ocsp_response_raw), |
| 1226 | ocsp_response_len); |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1227 | } |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 1228 | set_stapled_ocsp_response_received(ocsp_response_len != 0); |
| 1229 | UMA_HISTOGRAM_BOOLEAN("Net.OCSPResponseStapled", ocsp_response_len != 0); |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1230 | |
| 1231 | const uint8_t* sct_list; |
| 1232 | size_t sct_list_len; |
| 1233 | SSL_get0_signed_cert_timestamp_list(ssl_, &sct_list, &sct_list_len); |
| 1234 | set_signed_cert_timestamps_received(sct_list_len != 0); |
| 1235 | |
| 1236 | if (IsRenegotiationAllowed()) |
davidben | 0635886ab | 2015-11-04 19:24:04 | [diff] [blame] | 1237 | SSL_set_renegotiate_mode(ssl_, ssl_renegotiate_freely); |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1238 | |
davidben | 0653c8d | 2016-07-08 02:16:17 | [diff] [blame] | 1239 | uint16_t signature_algorithm = SSL_get_peer_signature_algorithm(ssl_); |
| 1240 | if (signature_algorithm != 0) { |
| 1241 | UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SSLSignatureAlgorithm", |
| 1242 | signature_algorithm); |
davidben | 4fe4f98 | 2015-11-11 22:00:12 | [diff] [blame] | 1243 | } |
| 1244 | |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1245 | // Verify the certificate. |
| 1246 | UpdateServerCert(); |
rsleevi | adbd498 | 2016-06-13 22:10:27 | [diff] [blame] | 1247 | next_handshake_state_ = STATE_VERIFY_CERT; |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1248 | return OK; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1249 | } |
| 1250 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1251 | int SSLClientSocketImpl::DoChannelIDLookup() { |
nharper | 49b27d99 | 2016-02-09 18:28:51 | [diff] [blame] | 1252 | NetLog::ParametersCallback callback = base::Bind( |
| 1253 | &NetLogChannelIDLookupCallback, base::Unretained(channel_id_service_)); |
| 1254 | net_log_.BeginEvent(NetLog::TYPE_SSL_GET_CHANNEL_ID, callback); |
rsleevi | adbd498 | 2016-06-13 22:10:27 | [diff] [blame] | 1255 | next_handshake_state_ = STATE_CHANNEL_ID_LOOKUP_COMPLETE; |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 1256 | return channel_id_service_->GetOrCreateChannelID( |
nharper | 2e171cf | 2015-06-01 20:29:23 | [diff] [blame] | 1257 | host_and_port_.host(), &channel_id_key_, |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1258 | base::Bind(&SSLClientSocketImpl::OnHandshakeIOComplete, |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1259 | base::Unretained(this)), |
nharper | 75ade89 | 2015-06-10 19:05:35 | [diff] [blame] | 1260 | &channel_id_request_); |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1261 | } |
| 1262 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1263 | int SSLClientSocketImpl::DoChannelIDLookupComplete(int result) { |
nharper | 49b27d99 | 2016-02-09 18:28:51 | [diff] [blame] | 1264 | net_log_.EndEvent(NetLog::TYPE_SSL_GET_CHANNEL_ID, |
| 1265 | base::Bind(&NetLogChannelIDLookupCompleteCallback, |
| 1266 | channel_id_key_.get(), result)); |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1267 | if (result < 0) |
| 1268 | return result; |
| 1269 | |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1270 | // Hand the key to OpenSSL. Check for error in case OpenSSL rejects the key |
| 1271 | // type. |
davidben | 8a208fc | 2016-01-22 17:08:08 | [diff] [blame] | 1272 | DCHECK(channel_id_key_); |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1273 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
nharper | 2e171cf | 2015-06-01 20:29:23 | [diff] [blame] | 1274 | int rv = SSL_set1_tls_channel_id(ssl_, channel_id_key_->key()); |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1275 | if (!rv) { |
| 1276 | LOG(ERROR) << "Failed to set Channel ID."; |
| 1277 | int err = SSL_get_error(ssl_, rv); |
| 1278 | return MapOpenSSLError(err, err_tracer); |
| 1279 | } |
| 1280 | |
| 1281 | // Return to the handshake. |
davidben | 52053b38 | 2015-04-27 19:22:29 | [diff] [blame] | 1282 | channel_id_sent_ = true; |
rsleevi | adbd498 | 2016-06-13 22:10:27 | [diff] [blame] | 1283 | next_handshake_state_ = STATE_HANDSHAKE; |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1284 | return OK; |
| 1285 | } |
| 1286 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1287 | int SSLClientSocketImpl::DoVerifyCert(int result) { |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1288 | DCHECK(!server_cert_chain_->empty()); |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 1289 | DCHECK(start_cert_verification_time_.is_null()); |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1290 | |
rsleevi | adbd498 | 2016-06-13 22:10:27 | [diff] [blame] | 1291 | next_handshake_state_ = STATE_VERIFY_CERT_COMPLETE; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1292 | |
davidben | c6435a7 | 2015-08-17 18:28:52 | [diff] [blame] | 1293 | // OpenSSL decoded the certificate, but the platform certificate |
| 1294 | // implementation could not. This is treated as a fatal SSL-level protocol |
| 1295 | // error rather than a certificate error. See https://crbug.com/91341. |
| 1296 | if (!server_cert_.get()) |
| 1297 | return ERR_SSL_SERVER_CERT_BAD_FORMAT; |
| 1298 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1299 | // If the certificate is bad and has been previously accepted, use |
| 1300 | // the previous status and bypass the error. |
| 1301 | base::StringPiece der_cert; |
| 1302 | if (!x509_util::GetDER(server_cert_chain_->Get(0), &der_cert)) { |
| 1303 | NOTREACHED(); |
| 1304 | return ERR_CERT_INVALID; |
| 1305 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1306 | CertStatus cert_status; |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1307 | if (ssl_config_.IsAllowedBadCert(der_cert, &cert_status)) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1308 | server_cert_verify_result_.Reset(); |
| 1309 | server_cert_verify_result_.cert_status = cert_status; |
| 1310 | server_cert_verify_result_.verified_cert = server_cert_; |
| 1311 | return OK; |
| 1312 | } |
| 1313 | |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 1314 | start_cert_verification_time_ = base::TimeTicks::Now(); |
| 1315 | |
eroman | 7f9236a | 2015-05-11 21:23:43 | [diff] [blame] | 1316 | return cert_verifier_->Verify( |
rsleevi | 06bd7855 | 2016-06-08 22:34:46 | [diff] [blame] | 1317 | CertVerifier::RequestParams(server_cert_, host_and_port_.host(), |
| 1318 | ssl_config_.GetCertVerifyFlags(), |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 1319 | ocsp_response_, CertificateList()), |
[email protected] | 591cffcd | 2014-08-18 20:02:30 | [diff] [blame] | 1320 | // TODO(davidben): Route the CRLSet through SSLConfig so |
| 1321 | // SSLClientSocket doesn't depend on SSLConfigService. |
davidben | 15f5713 | 2015-04-27 18:08:36 | [diff] [blame] | 1322 | SSLConfigService::GetCRLSet().get(), &server_cert_verify_result_, |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1323 | base::Bind(&SSLClientSocketImpl::OnHandshakeIOComplete, |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1324 | base::Unretained(this)), |
eroman | 7f9236a | 2015-05-11 21:23:43 | [diff] [blame] | 1325 | &cert_verifier_request_, net_log_); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1326 | } |
| 1327 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1328 | int SSLClientSocketImpl::DoVerifyCertComplete(int result) { |
eroman | 7f9236a | 2015-05-11 21:23:43 | [diff] [blame] | 1329 | cert_verifier_request_.reset(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1330 | |
davidben | 09c3d07 | 2014-08-25 20:33:58 | [diff] [blame] | 1331 | if (!start_cert_verification_time_.is_null()) { |
| 1332 | base::TimeDelta verify_time = |
| 1333 | base::TimeTicks::Now() - start_cert_verification_time_; |
| 1334 | if (result == OK) { |
| 1335 | UMA_HISTOGRAM_TIMES("Net.SSLCertVerificationTime", verify_time); |
| 1336 | } else { |
| 1337 | UMA_HISTOGRAM_TIMES("Net.SSLCertVerificationTimeError", verify_time); |
| 1338 | } |
| 1339 | } |
| 1340 | |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1341 | // If the connection was good, check HPKP and CT status simultaneously, |
| 1342 | // 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] | 1343 | const CertStatus cert_status = server_cert_verify_result_.cert_status; |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1344 | if ((result == OK || |
dadrian | 8f894665 | 2016-06-21 23:48:31 | [diff] [blame] | 1345 | (IsCertificateError(result) && IsCertStatusMinorError(cert_status)))) { |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1346 | int ct_result = VerifyCT(); |
dadrian | 8f894665 | 2016-06-21 23:48:31 | [diff] [blame] | 1347 | TransportSecurityState::PKPStatus pin_validity = |
| 1348 | transport_security_state_->CheckPublicKeyPins( |
| 1349 | host_and_port_, server_cert_verify_result_.is_issued_by_known_root, |
| 1350 | server_cert_verify_result_.public_key_hashes, server_cert_.get(), |
| 1351 | server_cert_verify_result_.verified_cert.get(), |
| 1352 | TransportSecurityState::ENABLE_PIN_REPORTS, &pinning_failure_log_); |
| 1353 | switch (pin_validity) { |
| 1354 | case TransportSecurityState::PKPStatus::VIOLATED: |
| 1355 | server_cert_verify_result_.cert_status |= |
| 1356 | CERT_STATUS_PINNED_KEY_MISSING; |
| 1357 | result = ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN; |
| 1358 | break; |
| 1359 | case TransportSecurityState::PKPStatus::BYPASSED: |
| 1360 | pkp_bypassed_ = true; |
| 1361 | // Fall through. |
| 1362 | case TransportSecurityState::PKPStatus::OK: |
| 1363 | // Do nothing. |
| 1364 | break; |
rsleevi | 9545d34 | 2016-06-21 03:17:37 | [diff] [blame] | 1365 | } |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1366 | if (result != ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN && ct_result != OK) |
| 1367 | result = ct_result; |
[email protected] | 8bd4e7a | 2014-08-09 14:49:17 | [diff] [blame] | 1368 | } |
| 1369 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1370 | if (result == OK) { |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 1371 | DCHECK(!certificate_verified_); |
| 1372 | certificate_verified_ = true; |
| 1373 | MaybeCacheSession(); |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 1374 | SSLInfo ssl_info; |
| 1375 | bool ok = GetSSLInfo(&ssl_info); |
| 1376 | DCHECK(ok); |
| 1377 | transport_security_state_->CheckExpectStaple(host_and_port_, ssl_info, |
| 1378 | ocsp_response_); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1379 | } |
| 1380 | |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 1381 | completed_connect_ = true; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1382 | // Exit DoHandshakeLoop and return the result to the caller to Connect. |
| 1383 | DCHECK_EQ(STATE_NONE, next_handshake_state_); |
| 1384 | return result; |
| 1385 | } |
| 1386 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1387 | void SSLClientSocketImpl::DoConnectCallback(int rv) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1388 | if (!user_connect_callback_.is_null()) { |
| 1389 | CompletionCallback c = user_connect_callback_; |
| 1390 | user_connect_callback_.Reset(); |
| 1391 | c.Run(rv > OK ? OK : rv); |
| 1392 | } |
| 1393 | } |
| 1394 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1395 | void SSLClientSocketImpl::UpdateServerCert() { |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 1396 | server_cert_chain_->Reset(SSL_get_peer_cert_chain(ssl_)); |
[email protected] | 7f38da8a | 2014-03-17 16:44:26 | [diff] [blame] | 1397 | server_cert_ = server_cert_chain_->AsOSChain(); |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1398 | if (server_cert_.get()) { |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1399 | net_log_.AddEvent(NetLog::TYPE_SSL_CERTIFICATES_RECEIVED, |
| 1400 | base::Bind(&NetLogX509CertificateCallback, |
| 1401 | base::Unretained(server_cert_.get()))); |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1402 | } |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1403 | } |
| 1404 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1405 | void SSLClientSocketImpl::OnHandshakeIOComplete(int result) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1406 | int rv = DoHandshakeLoop(result); |
| 1407 | if (rv != ERR_IO_PENDING) { |
davidben | 281d13f0 | 2016-04-27 20:43:28 | [diff] [blame] | 1408 | LogConnectEndEvent(rv); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1409 | DoConnectCallback(rv); |
| 1410 | } |
| 1411 | } |
| 1412 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1413 | void SSLClientSocketImpl::OnSendComplete(int result) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1414 | if (next_handshake_state_ == STATE_HANDSHAKE) { |
| 1415 | // In handshake phase. |
| 1416 | OnHandshakeIOComplete(result); |
| 1417 | return; |
| 1418 | } |
| 1419 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1420 | // During a renegotiation, a Read call may also be blocked on a transport |
| 1421 | // write, so retry both operations. |
| 1422 | PumpReadWriteEvents(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1423 | } |
| 1424 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1425 | void SSLClientSocketImpl::OnRecvComplete(int result) { |
| 1426 | TRACE_EVENT0("net", "SSLClientSocketImpl::OnRecvComplete"); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1427 | if (next_handshake_state_ == STATE_HANDSHAKE) { |
| 1428 | // In handshake phase. |
| 1429 | OnHandshakeIOComplete(result); |
| 1430 | return; |
| 1431 | } |
| 1432 | |
| 1433 | // Network layer received some data, check if client requested to read |
| 1434 | // decrypted data. |
| 1435 | if (!user_read_buf_.get()) |
| 1436 | return; |
| 1437 | |
davidben | 1b133ad | 2014-10-23 04:23:13 | [diff] [blame] | 1438 | int rv = DoReadLoop(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1439 | if (rv != ERR_IO_PENDING) |
| 1440 | DoReadCallback(rv); |
| 1441 | } |
| 1442 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1443 | int SSLClientSocketImpl::DoHandshakeLoop(int last_io_result) { |
| 1444 | TRACE_EVENT0("net", "SSLClientSocketImpl::DoHandshakeLoop"); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1445 | int rv = last_io_result; |
| 1446 | do { |
| 1447 | // Default to STATE_NONE for next state. |
| 1448 | // (This is a quirk carried over from the windows |
| 1449 | // implementation. It makes reading the logs a bit harder.) |
| 1450 | // State handlers can and often do call GotoState just |
| 1451 | // to stay in the current state. |
| 1452 | State state = next_handshake_state_; |
rsleevi | adbd498 | 2016-06-13 22:10:27 | [diff] [blame] | 1453 | next_handshake_state_ = STATE_NONE; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1454 | switch (state) { |
| 1455 | case STATE_HANDSHAKE: |
| 1456 | rv = DoHandshake(); |
| 1457 | break; |
davidben | c4212c0 | 2015-05-12 22:30:18 | [diff] [blame] | 1458 | case STATE_HANDSHAKE_COMPLETE: |
| 1459 | rv = DoHandshakeComplete(rv); |
| 1460 | break; |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1461 | case STATE_CHANNEL_ID_LOOKUP: |
| 1462 | DCHECK_EQ(OK, rv); |
| 1463 | rv = DoChannelIDLookup(); |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1464 | break; |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1465 | case STATE_CHANNEL_ID_LOOKUP_COMPLETE: |
| 1466 | rv = DoChannelIDLookupComplete(rv); |
| 1467 | break; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1468 | case STATE_VERIFY_CERT: |
[email protected] | faff985 | 2014-06-21 06:13:46 | [diff] [blame] | 1469 | DCHECK_EQ(OK, rv); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1470 | rv = DoVerifyCert(rv); |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1471 | break; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1472 | case STATE_VERIFY_CERT_COMPLETE: |
| 1473 | rv = DoVerifyCertComplete(rv); |
| 1474 | break; |
| 1475 | case STATE_NONE: |
| 1476 | default: |
| 1477 | rv = ERR_UNEXPECTED; |
| 1478 | NOTREACHED() << "unexpected state" << state; |
| 1479 | break; |
| 1480 | } |
| 1481 | |
| 1482 | bool network_moved = DoTransportIO(); |
| 1483 | if (network_moved && next_handshake_state_ == STATE_HANDSHAKE) { |
| 1484 | // In general we exit the loop if rv is ERR_IO_PENDING. In this |
| 1485 | // special case we keep looping even if rv is ERR_IO_PENDING because |
| 1486 | // the transport IO may allow DoHandshake to make progress. |
| 1487 | rv = OK; // This causes us to stay in the loop. |
| 1488 | } |
| 1489 | } while (rv != ERR_IO_PENDING && next_handshake_state_ != STATE_NONE); |
| 1490 | return rv; |
| 1491 | } |
| 1492 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1493 | int SSLClientSocketImpl::DoReadLoop() { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1494 | bool network_moved; |
| 1495 | int rv; |
| 1496 | do { |
| 1497 | rv = DoPayloadRead(); |
| 1498 | network_moved = DoTransportIO(); |
| 1499 | } while (rv == ERR_IO_PENDING && network_moved); |
| 1500 | |
| 1501 | return rv; |
| 1502 | } |
| 1503 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1504 | int SSLClientSocketImpl::DoWriteLoop() { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1505 | bool network_moved; |
| 1506 | int rv; |
| 1507 | do { |
| 1508 | rv = DoPayloadWrite(); |
| 1509 | network_moved = DoTransportIO(); |
| 1510 | } while (rv == ERR_IO_PENDING && network_moved); |
| 1511 | |
| 1512 | return rv; |
| 1513 | } |
| 1514 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1515 | int SSLClientSocketImpl::DoPayloadRead() { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1516 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 1517 | |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1518 | DCHECK_LT(0, user_read_buf_len_); |
| 1519 | DCHECK(user_read_buf_.get()); |
| 1520 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1521 | int rv; |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1522 | if (pending_read_error_ != kNoPendingResult) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1523 | rv = pending_read_error_; |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1524 | pending_read_error_ = kNoPendingResult; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1525 | if (rv == 0) { |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1526 | net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, rv, |
| 1527 | user_read_buf_->data()); |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1528 | } else { |
| 1529 | net_log_.AddEvent( |
| 1530 | NetLog::TYPE_SSL_READ_ERROR, |
| 1531 | CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_, |
| 1532 | pending_read_error_info_)); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1533 | } |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1534 | pending_read_ssl_error_ = SSL_ERROR_NONE; |
| 1535 | pending_read_error_info_ = OpenSSLErrorInfo(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1536 | return rv; |
| 1537 | } |
| 1538 | |
| 1539 | int total_bytes_read = 0; |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1540 | int ssl_ret; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1541 | do { |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1542 | ssl_ret = SSL_read(ssl_, user_read_buf_->data() + total_bytes_read, |
| 1543 | user_read_buf_len_ - total_bytes_read); |
| 1544 | if (ssl_ret > 0) |
| 1545 | total_bytes_read += ssl_ret; |
| 1546 | } while (total_bytes_read < user_read_buf_len_ && ssl_ret > 0); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1547 | |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1548 | // Although only the final SSL_read call may have failed, the failure needs to |
| 1549 | // processed immediately, while the information still available in OpenSSL's |
| 1550 | // error queue. |
davidben | ced4aa9b | 2015-05-12 21:22:35 | [diff] [blame] | 1551 | if (ssl_ret <= 0) { |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1552 | // A zero return from SSL_read may mean any of: |
| 1553 | // - The underlying BIO_read returned 0. |
| 1554 | // - The peer sent a close_notify. |
| 1555 | // - Any arbitrary error. https://crbug.com/466303 |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1556 | // |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1557 | // TransportReadComplete converts the first to an ERR_CONNECTION_CLOSED |
| 1558 | // error, so it does not occur. The second and third are distinguished by |
| 1559 | // SSL_ERROR_ZERO_RETURN. |
| 1560 | pending_read_ssl_error_ = SSL_get_error(ssl_, ssl_ret); |
| 1561 | if (pending_read_ssl_error_ == SSL_ERROR_ZERO_RETURN) { |
| 1562 | pending_read_error_ = 0; |
davidben | ced4aa9b | 2015-05-12 21:22:35 | [diff] [blame] | 1563 | } else if (pending_read_ssl_error_ == SSL_ERROR_WANT_X509_LOOKUP && |
| 1564 | !ssl_config_.send_client_cert) { |
| 1565 | pending_read_error_ = ERR_SSL_CLIENT_AUTH_CERT_NEEDED; |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1566 | } else if (pending_read_ssl_error_ == |
| 1567 | SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) { |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 1568 | DCHECK(ssl_config_.client_private_key); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1569 | DCHECK_NE(kNoPendingResult, signature_result_); |
| 1570 | pending_read_error_ = ERR_IO_PENDING; |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1571 | } else { |
| 1572 | pending_read_error_ = MapOpenSSLErrorWithDetails( |
| 1573 | pending_read_ssl_error_, err_tracer, &pending_read_error_info_); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1574 | } |
| 1575 | |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1576 | // Many servers do not reliably send a close_notify alert when shutting down |
| 1577 | // a connection, and instead terminate the TCP connection. This is reported |
| 1578 | // as ERR_CONNECTION_CLOSED. Because of this, map the unclean shutdown to a |
| 1579 | // graceful EOF, instead of treating it as an error as it should be. |
| 1580 | if (pending_read_error_ == ERR_CONNECTION_CLOSED) |
| 1581 | pending_read_error_ = 0; |
| 1582 | } |
davidben | be6ce7ec | 2014-10-20 19:15:56 | [diff] [blame] | 1583 | |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1584 | if (total_bytes_read > 0) { |
| 1585 | // Return any bytes read to the caller. The error will be deferred to the |
| 1586 | // next call of DoPayloadRead. |
| 1587 | rv = total_bytes_read; |
davidben | be6ce7ec | 2014-10-20 19:15:56 | [diff] [blame] | 1588 | |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1589 | // Do not treat insufficient data as an error to return in the next call to |
| 1590 | // DoPayloadRead() - instead, let the call fall through to check SSL_read() |
| 1591 | // again. This is because DoTransportIO() may complete in between the next |
| 1592 | // call to DoPayloadRead(), and thus it is important to check SSL_read() on |
| 1593 | // subsequent invocations to see if a complete record may now be read. |
| 1594 | if (pending_read_error_ == ERR_IO_PENDING) |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1595 | pending_read_error_ = kNoPendingResult; |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1596 | } else { |
| 1597 | // No bytes were returned. Return the pending read error immediately. |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1598 | DCHECK_NE(kNoPendingResult, pending_read_error_); |
davidben | 7e555daf | 2015-03-25 17:03:29 | [diff] [blame] | 1599 | rv = pending_read_error_; |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1600 | pending_read_error_ = kNoPendingResult; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | if (rv >= 0) { |
| 1604 | net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, rv, |
| 1605 | user_read_buf_->data()); |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1606 | } else if (rv != ERR_IO_PENDING) { |
| 1607 | net_log_.AddEvent( |
| 1608 | NetLog::TYPE_SSL_READ_ERROR, |
| 1609 | CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_, |
| 1610 | pending_read_error_info_)); |
| 1611 | pending_read_ssl_error_ = SSL_ERROR_NONE; |
| 1612 | pending_read_error_info_ = OpenSSLErrorInfo(); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1613 | } |
| 1614 | return rv; |
| 1615 | } |
| 1616 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1617 | int SSLClientSocketImpl::DoPayloadWrite() { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1618 | crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 1619 | int rv = SSL_write(ssl_, user_write_buf_->data(), user_write_buf_len_); |
rsleevi | f020edc | 2015-03-16 19:31:24 | [diff] [blame] | 1620 | |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1621 | if (rv >= 0) { |
| 1622 | net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv, |
| 1623 | user_write_buf_->data()); |
| 1624 | return rv; |
| 1625 | } |
| 1626 | |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1627 | int ssl_error = SSL_get_error(ssl_, rv); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1628 | if (ssl_error == SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) |
| 1629 | return ERR_IO_PENDING; |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1630 | OpenSSLErrorInfo error_info; |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1631 | int net_error = |
| 1632 | MapOpenSSLErrorWithDetails(ssl_error, err_tracer, &error_info); |
davidben | b8c2321 | 2014-10-28 00:12:16 | [diff] [blame] | 1633 | |
| 1634 | if (net_error != ERR_IO_PENDING) { |
| 1635 | net_log_.AddEvent( |
| 1636 | NetLog::TYPE_SSL_WRITE_ERROR, |
| 1637 | CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info)); |
| 1638 | } |
| 1639 | return net_error; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1640 | } |
| 1641 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1642 | void SSLClientSocketImpl::PumpReadWriteEvents() { |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1643 | int rv_read = ERR_IO_PENDING; |
| 1644 | int rv_write = ERR_IO_PENDING; |
| 1645 | bool network_moved; |
| 1646 | do { |
| 1647 | if (user_read_buf_.get()) |
| 1648 | rv_read = DoPayloadRead(); |
| 1649 | if (user_write_buf_.get()) |
| 1650 | rv_write = DoPayloadWrite(); |
| 1651 | network_moved = DoTransportIO(); |
| 1652 | } while (rv_read == ERR_IO_PENDING && rv_write == ERR_IO_PENDING && |
| 1653 | (user_read_buf_.get() || user_write_buf_.get()) && network_moved); |
| 1654 | |
| 1655 | // Performing the Read callback may cause |this| to be deleted. If this |
| 1656 | // happens, the Write callback should not be invoked. Guard against this by |
| 1657 | // holding a WeakPtr to |this| and ensuring it's still valid. |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1658 | base::WeakPtr<SSLClientSocketImpl> guard(weak_factory_.GetWeakPtr()); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1659 | if (user_read_buf_.get() && rv_read != ERR_IO_PENDING) |
| 1660 | DoReadCallback(rv_read); |
| 1661 | |
| 1662 | if (!guard.get()) |
| 1663 | return; |
| 1664 | |
| 1665 | if (user_write_buf_.get() && rv_write != ERR_IO_PENDING) |
| 1666 | DoWriteCallback(rv_write); |
| 1667 | } |
| 1668 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1669 | int SSLClientSocketImpl::BufferSend(void) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1670 | if (transport_send_busy_) |
| 1671 | return ERR_IO_PENDING; |
| 1672 | |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1673 | size_t buffer_read_offset; |
| 1674 | uint8_t* read_buf; |
| 1675 | size_t max_read; |
| 1676 | int status = BIO_zero_copy_get_read_buf(transport_bio_, &read_buf, |
| 1677 | &buffer_read_offset, &max_read); |
| 1678 | DCHECK_EQ(status, 1); // Should never fail. |
| 1679 | if (!max_read) |
| 1680 | return 0; // Nothing pending in the OpenSSL write BIO. |
| 1681 | CHECK_EQ(read_buf, reinterpret_cast<uint8_t*>(send_buffer_->StartOfBuffer())); |
| 1682 | CHECK_LT(buffer_read_offset, static_cast<size_t>(send_buffer_->capacity())); |
| 1683 | send_buffer_->set_offset(buffer_read_offset); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1684 | |
| 1685 | int rv = transport_->socket()->Write( |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1686 | send_buffer_.get(), max_read, |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1687 | base::Bind(&SSLClientSocketImpl::BufferSendComplete, |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1688 | base::Unretained(this))); |
| 1689 | if (rv == ERR_IO_PENDING) { |
| 1690 | transport_send_busy_ = true; |
| 1691 | } else { |
| 1692 | TransportWriteComplete(rv); |
| 1693 | } |
| 1694 | return rv; |
| 1695 | } |
| 1696 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1697 | int SSLClientSocketImpl::BufferRecv(void) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1698 | if (transport_recv_busy_) |
| 1699 | return ERR_IO_PENDING; |
| 1700 | |
| 1701 | // Determine how much was requested from |transport_bio_| that was not |
| 1702 | // actually available. |
| 1703 | size_t requested = BIO_ctrl_get_read_request(transport_bio_); |
| 1704 | if (requested == 0) { |
| 1705 | // This is not a perfect match of error codes, as no operation is |
| 1706 | // actually pending. However, returning 0 would be interpreted as |
| 1707 | // a possible sign of EOF, which is also an inappropriate match. |
| 1708 | return ERR_IO_PENDING; |
| 1709 | } |
| 1710 | |
| 1711 | // Known Issue: While only reading |requested| data is the more correct |
| 1712 | // implementation, it has the downside of resulting in frequent reads: |
| 1713 | // One read for the SSL record header (~5 bytes) and one read for the SSL |
| 1714 | // record body. Rather than issuing these reads to the underlying socket |
| 1715 | // (and constantly allocating new IOBuffers), a single Read() request to |
| 1716 | // fill |transport_bio_| is issued. As long as an SSL client socket cannot |
| 1717 | // be gracefully shutdown (via SSL close alerts) and re-used for non-SSL |
| 1718 | // traffic, this over-subscribed Read()ing will not cause issues. |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1719 | |
| 1720 | size_t buffer_write_offset; |
| 1721 | uint8_t* write_buf; |
| 1722 | size_t max_write; |
| 1723 | int status = BIO_zero_copy_get_write_buf(transport_bio_, &write_buf, |
| 1724 | &buffer_write_offset, &max_write); |
| 1725 | DCHECK_EQ(status, 1); // Should never fail. |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1726 | if (!max_write) |
| 1727 | return ERR_IO_PENDING; |
| 1728 | |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1729 | CHECK_EQ(write_buf, |
| 1730 | reinterpret_cast<uint8_t*>(recv_buffer_->StartOfBuffer())); |
| 1731 | CHECK_LT(buffer_write_offset, static_cast<size_t>(recv_buffer_->capacity())); |
| 1732 | |
| 1733 | recv_buffer_->set_offset(buffer_write_offset); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1734 | int rv = transport_->socket()->Read( |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1735 | recv_buffer_.get(), max_write, |
| 1736 | base::Bind(&SSLClientSocketImpl::BufferRecvComplete, |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1737 | base::Unretained(this))); |
| 1738 | if (rv == ERR_IO_PENDING) { |
| 1739 | transport_recv_busy_ = true; |
| 1740 | } else { |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1741 | rv = TransportReadComplete(rv); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1742 | } |
| 1743 | return rv; |
| 1744 | } |
| 1745 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1746 | void SSLClientSocketImpl::BufferSendComplete(int result) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1747 | TransportWriteComplete(result); |
| 1748 | OnSendComplete(result); |
| 1749 | } |
| 1750 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1751 | void SSLClientSocketImpl::BufferRecvComplete(int result) { |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1752 | result = TransportReadComplete(result); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1753 | OnRecvComplete(result); |
| 1754 | } |
| 1755 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1756 | void SSLClientSocketImpl::TransportWriteComplete(int result) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1757 | DCHECK(ERR_IO_PENDING != result); |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1758 | int bytes_written = 0; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1759 | if (result < 0) { |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 1760 | // Record the error. Save it to be reported in a future read or write on |
| 1761 | // transport_bio_'s peer. |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1762 | transport_write_error_ = result; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1763 | } else { |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1764 | bytes_written = result; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1765 | } |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1766 | DCHECK_GE(send_buffer_->RemainingCapacity(), bytes_written); |
| 1767 | int ret = BIO_zero_copy_get_read_buf_done(transport_bio_, bytes_written); |
| 1768 | DCHECK_EQ(1, ret); |
| 1769 | transport_send_busy_ = false; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1770 | } |
| 1771 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1772 | int SSLClientSocketImpl::TransportReadComplete(int result) { |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1773 | DCHECK(ERR_IO_PENDING != result); |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 1774 | // If an EOF, canonicalize to ERR_CONNECTION_CLOSED here so MapOpenSSLError |
| 1775 | // does not report success. |
| 1776 | if (result == 0) |
| 1777 | result = ERR_CONNECTION_CLOSED; |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1778 | int bytes_read = 0; |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 1779 | if (result < 0) { |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 1780 | // Received an error. Save it to be reported in a future read on |
| 1781 | // transport_bio_'s peer. |
| 1782 | transport_read_error_ = result; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1783 | } else { |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1784 | bytes_read = result; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1785 | } |
haavardm | 2d92e72 | 2014-12-19 13:45:44 | [diff] [blame] | 1786 | DCHECK_GE(recv_buffer_->RemainingCapacity(), bytes_read); |
| 1787 | int ret = BIO_zero_copy_get_write_buf_done(transport_bio_, bytes_read); |
| 1788 | DCHECK_EQ(1, ret); |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1789 | transport_recv_busy_ = false; |
[email protected] | 3e5c692 | 2014-02-06 02:42:16 | [diff] [blame] | 1790 | return result; |
[email protected] | b9b651f | 2013-11-09 04:32:22 | [diff] [blame] | 1791 | } |
| 1792 | |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1793 | int SSLClientSocketImpl::VerifyCT() { |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1794 | const uint8_t* sct_list_raw; |
| 1795 | size_t sct_list_len; |
| 1796 | SSL_get0_signed_cert_timestamp_list(ssl_, &sct_list_raw, &sct_list_len); |
| 1797 | std::string sct_list; |
| 1798 | if (sct_list_len > 0) |
| 1799 | sct_list.assign(reinterpret_cast<const char*>(sct_list_raw), sct_list_len); |
| 1800 | |
| 1801 | // Note that this is a completely synchronous operation: The CT Log Verifier |
| 1802 | // gets all the data it needs for SCT verification and does not do any |
| 1803 | // external communication. |
| 1804 | cert_transparency_verifier_->Verify( |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 1805 | server_cert_verify_result_.verified_cert.get(), ocsp_response_, sct_list, |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1806 | &ct_verify_result_, net_log_); |
| 1807 | |
| 1808 | ct_verify_result_.ct_policies_applied = true; |
| 1809 | ct_verify_result_.ev_policy_compliance = |
| 1810 | ct::EVPolicyCompliance::EV_POLICY_DOES_NOT_APPLY; |
eranm | 4bed0b57 | 2016-08-14 21:00:35 | [diff] [blame^] | 1811 | |
| 1812 | SCTList verified_scts = |
| 1813 | ct::SCTsMatchingStatus(ct_verify_result_.scts, ct::SCT_STATUS_OK); |
| 1814 | |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1815 | if (server_cert_verify_result_.cert_status & CERT_STATUS_IS_EV) { |
| 1816 | scoped_refptr<ct::EVCertsWhitelist> ev_whitelist = |
| 1817 | SSLConfigService::GetEVCertsWhitelist(); |
| 1818 | ct::EVPolicyCompliance ev_policy_compliance = |
| 1819 | policy_enforcer_->DoesConformToCTEVPolicy( |
| 1820 | server_cert_verify_result_.verified_cert.get(), ev_whitelist.get(), |
eranm | 4bed0b57 | 2016-08-14 21:00:35 | [diff] [blame^] | 1821 | verified_scts, net_log_); |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1822 | ct_verify_result_.ev_policy_compliance = ev_policy_compliance; |
| 1823 | if (ev_policy_compliance != |
| 1824 | ct::EVPolicyCompliance::EV_POLICY_DOES_NOT_APPLY && |
| 1825 | ev_policy_compliance != |
| 1826 | ct::EVPolicyCompliance::EV_POLICY_COMPLIES_VIA_WHITELIST && |
| 1827 | ev_policy_compliance != |
| 1828 | ct::EVPolicyCompliance::EV_POLICY_COMPLIES_VIA_SCTS) { |
| 1829 | server_cert_verify_result_.cert_status |= |
| 1830 | CERT_STATUS_CT_COMPLIANCE_FAILED; |
| 1831 | server_cert_verify_result_.cert_status &= ~CERT_STATUS_IS_EV; |
| 1832 | } |
| 1833 | } |
| 1834 | ct_verify_result_.cert_policy_compliance = |
| 1835 | policy_enforcer_->DoesConformToCertPolicy( |
eranm | 4bed0b57 | 2016-08-14 21:00:35 | [diff] [blame^] | 1836 | server_cert_verify_result_.verified_cert.get(), verified_scts, |
| 1837 | net_log_); |
rsleevi | 4a6ca8c | 2016-06-24 03:05:22 | [diff] [blame] | 1838 | |
| 1839 | if (ct_verify_result_.cert_policy_compliance != |
| 1840 | ct::CertPolicyCompliance::CERT_POLICY_COMPLIES_VIA_SCTS && |
| 1841 | transport_security_state_->ShouldRequireCT( |
| 1842 | host_and_port_.host(), server_cert_verify_result_.verified_cert.get(), |
| 1843 | server_cert_verify_result_.public_key_hashes)) { |
| 1844 | server_cert_verify_result_.cert_status |= |
| 1845 | CERT_STATUS_CERTIFICATE_TRANSPARENCY_REQUIRED; |
| 1846 | return ERR_CERTIFICATE_TRANSPARENCY_REQUIRED; |
| 1847 | } |
| 1848 | |
| 1849 | return OK; |
| 1850 | } |
| 1851 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1852 | int SSLClientSocketImpl::ClientCertRequestCallback(SSL* ssl) { |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1853 | DCHECK(ssl == ssl_); |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 1854 | |
davidben | af42cbe | 2014-11-13 03:27:46 | [diff] [blame] | 1855 | net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_REQUESTED); |
| 1856 | |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 1857 | // Clear any currently configured certificates. |
| 1858 | SSL_certs_clear(ssl_); |
[email protected] | 97a854f | 2014-07-29 07:51:36 | [diff] [blame] | 1859 | |
| 1860 | #if defined(OS_IOS) |
| 1861 | // TODO(droger): Support client auth on iOS. See http://crbug.com/145954). |
| 1862 | LOG(WARNING) << "Client auth is not supported"; |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1863 | #else // !defined(OS_IOS) |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1864 | if (!ssl_config_.send_client_cert) { |
[email protected] | 515adc2 | 2013-01-09 16:01:23 | [diff] [blame] | 1865 | // First pass: we know that a client certificate is needed, but we do not |
| 1866 | // have one at hand. |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1867 | STACK_OF(X509_NAME)* authorities = SSL_get_client_CA_list(ssl); |
[email protected] | edfd0f4 | 2014-07-22 18:20:37 | [diff] [blame] | 1868 | for (size_t i = 0; i < sk_X509_NAME_num(authorities); i++) { |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1869 | X509_NAME* ca_name = (X509_NAME*)sk_X509_NAME_value(authorities, i); |
[email protected] | 515adc2 | 2013-01-09 16:01:23 | [diff] [blame] | 1870 | unsigned char* str = NULL; |
| 1871 | int length = i2d_X509_NAME(ca_name, &str); |
| 1872 | cert_authorities_.push_back(std::string( |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1873 | reinterpret_cast<const char*>(str), static_cast<size_t>(length))); |
[email protected] | 515adc2 | 2013-01-09 16:01:23 | [diff] [blame] | 1874 | OPENSSL_free(str); |
| 1875 | } |
| 1876 | |
[email protected] | c078770 | 2014-05-20 21:51:44 | [diff] [blame] | 1877 | const unsigned char* client_cert_types; |
[email protected] | e7e883e | 2014-07-25 06:03:08 | [diff] [blame] | 1878 | size_t num_client_cert_types = |
| 1879 | SSL_get0_certificate_types(ssl, &client_cert_types); |
[email protected] | c078770 | 2014-05-20 21:51:44 | [diff] [blame] | 1880 | for (size_t i = 0; i < num_client_cert_types; i++) { |
| 1881 | cert_key_types_.push_back( |
| 1882 | static_cast<SSLClientCertType>(client_cert_types[i])); |
| 1883 | } |
| 1884 | |
davidben | ced4aa9b | 2015-05-12 21:22:35 | [diff] [blame] | 1885 | // Suspends handshake. SSL_get_error will return SSL_ERROR_WANT_X509_LOOKUP. |
| 1886 | return -1; |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | // Second pass: a client certificate should have been selected. |
[email protected] | 13914c9 | 2013-06-13 22:42:42 | [diff] [blame] | 1890 | if (ssl_config_.client_cert.get()) { |
[email protected] | 6bad505 | 2014-07-12 01:25:13 | [diff] [blame] | 1891 | ScopedX509 leaf_x509 = |
| 1892 | OSCertHandleToOpenSSL(ssl_config_.client_cert->os_cert_handle()); |
| 1893 | if (!leaf_x509) { |
| 1894 | LOG(WARNING) << "Failed to import certificate"; |
| 1895 | OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); |
| 1896 | return -1; |
| 1897 | } |
| 1898 | |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 1899 | ScopedX509Stack chain = OSCertHandlesToOpenSSL( |
| 1900 | ssl_config_.client_cert->GetIntermediateCertificates()); |
| 1901 | if (!chain) { |
| 1902 | LOG(WARNING) << "Failed to import intermediate certificates"; |
| 1903 | OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); |
| 1904 | return -1; |
| 1905 | } |
| 1906 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1907 | if (!SSL_use_certificate(ssl_, leaf_x509.get()) || |
| 1908 | !SSL_set1_chain(ssl_, chain.get())) { |
| 1909 | LOG(WARNING) << "Failed to set client certificate"; |
| 1910 | return -1; |
| 1911 | } |
| 1912 | |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 1913 | if (!ssl_config_.client_private_key) { |
| 1914 | // The caller supplied a null private key. Fail the handshake and surface |
| 1915 | // an appropriate error to the caller. |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 1916 | LOG(WARNING) << "Client cert found without private key"; |
| 1917 | OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); |
| 1918 | return -1; |
| 1919 | } |
| 1920 | |
| 1921 | SSL_set_private_key_method(ssl_, &SSLContext::kPrivateKeyMethod); |
svaldez | f3db006f | 2015-09-29 16:43:58 | [diff] [blame] | 1922 | |
| 1923 | std::vector<SSLPrivateKey::Hash> digest_prefs = |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 1924 | ssl_config_.client_private_key->GetDigestPreferences(); |
svaldez | f3db006f | 2015-09-29 16:43:58 | [diff] [blame] | 1925 | |
| 1926 | size_t digests_len = digest_prefs.size(); |
| 1927 | std::vector<int> digests; |
| 1928 | for (size_t i = 0; i < digests_len; i++) { |
| 1929 | switch (digest_prefs[i]) { |
| 1930 | case SSLPrivateKey::Hash::SHA1: |
| 1931 | digests.push_back(NID_sha1); |
| 1932 | break; |
| 1933 | case SSLPrivateKey::Hash::SHA256: |
| 1934 | digests.push_back(NID_sha256); |
| 1935 | break; |
| 1936 | case SSLPrivateKey::Hash::SHA384: |
| 1937 | digests.push_back(NID_sha384); |
| 1938 | break; |
| 1939 | case SSLPrivateKey::Hash::SHA512: |
| 1940 | digests.push_back(NID_sha512); |
| 1941 | break; |
| 1942 | case SSLPrivateKey::Hash::MD5_SHA1: |
| 1943 | // MD5-SHA1 is not used in TLS 1.2. |
| 1944 | break; |
| 1945 | } |
| 1946 | } |
| 1947 | |
davidben | 5f8b6bc | 2015-11-25 03:19:54 | [diff] [blame] | 1948 | SSL_set_private_key_digest_prefs(ssl_, digests.data(), digests.size()); |
davidben | af42cbe | 2014-11-13 03:27:46 | [diff] [blame] | 1949 | |
| 1950 | int cert_count = 1 + sk_X509_num(chain.get()); |
| 1951 | net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_PROVIDED, |
tfarina | 5e24b24 | 2015-10-27 13:11:28 | [diff] [blame] | 1952 | NetLog::IntCallback("cert_count", cert_count)); |
[email protected] | 6bad505 | 2014-07-12 01:25:13 | [diff] [blame] | 1953 | return 1; |
[email protected] | c078770 | 2014-05-20 21:51:44 | [diff] [blame] | 1954 | } |
[email protected] | 97a854f | 2014-07-29 07:51:36 | [diff] [blame] | 1955 | #endif // defined(OS_IOS) |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1956 | |
| 1957 | // Send no client certificate. |
davidben | af42cbe | 2014-11-13 03:27:46 | [diff] [blame] | 1958 | net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_PROVIDED, |
tfarina | 5e24b24 | 2015-10-27 13:11:28 | [diff] [blame] | 1959 | NetLog::IntCallback("cert_count", 0)); |
[email protected] | 82c5902 | 2014-08-15 09:38:27 | [diff] [blame] | 1960 | return 1; |
[email protected] | 5ac981e18 | 2010-12-06 17:56:27 | [diff] [blame] | 1961 | } |
| 1962 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1963 | int SSLClientSocketImpl::CertVerifyCallback(X509_STORE_CTX* store_ctx) { |
[email protected] | 64b5c89 | 2014-08-08 09:39:26 | [diff] [blame] | 1964 | if (!completed_connect_) { |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 1965 | // If the first handshake hasn't completed then we accept any certificates |
| 1966 | // because we verify after the handshake. |
| 1967 | return 1; |
| 1968 | } |
| 1969 | |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1970 | // Disallow the server certificate to change in a renegotiation. |
| 1971 | if (server_cert_chain_->empty()) { |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 1972 | LOG(ERROR) << "Received invalid certificate chain between handshakes"; |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1973 | return 0; |
| 1974 | } |
| 1975 | base::StringPiece old_der, new_der; |
| 1976 | if (store_ctx->cert == NULL || |
| 1977 | !x509_util::GetDER(server_cert_chain_->Get(0), &old_der) || |
| 1978 | !x509_util::GetDER(store_ctx->cert, &new_der)) { |
| 1979 | LOG(ERROR) << "Failed to encode certificates"; |
| 1980 | return 0; |
| 1981 | } |
| 1982 | if (old_der != new_der) { |
[email protected] | 76e8539 | 2014-03-20 17:54:14 | [diff] [blame] | 1983 | LOG(ERROR) << "Server certificate changed between handshakes"; |
davidben | 30798ed8 | 2014-09-19 19:28:20 | [diff] [blame] | 1984 | return 0; |
| 1985 | } |
| 1986 | |
| 1987 | return 1; |
[email protected] | b051cdb6 | 2014-02-28 02:20:16 | [diff] [blame] | 1988 | } |
| 1989 | |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1990 | // SelectNextProtoCallback is called by OpenSSL during the handshake. If the |
| 1991 | // server supports NPN, selects a protocol from the list that the server |
thestig | c23b8ec4 | 2016-01-08 01:41:12 | [diff] [blame] | 1992 | // provides. According to third_party/boringssl/src/ssl/ssl_lib.c, the |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 1993 | // callback can assume that |in| is syntactically valid. |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 1994 | int SSLClientSocketImpl::SelectNextProtoCallback(unsigned char** out, |
| 1995 | unsigned char* outlen, |
| 1996 | const unsigned char* in, |
| 1997 | unsigned int inlen) { |
bnc | 1f29537 | 2015-10-21 23:24:22 | [diff] [blame] | 1998 | if (ssl_config_.npn_protos.empty()) { |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 1999 | *out = reinterpret_cast<uint8_t*>( |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 2000 | const_cast<char*>(kDefaultSupportedNPNProtocol)); |
| 2001 | *outlen = arraysize(kDefaultSupportedNPNProtocol) - 1; |
| 2002 | npn_status_ = kNextProtoUnsupported; |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 2003 | return SSL_TLSEXT_ERR_OK; |
| 2004 | } |
| 2005 | |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 2006 | // Assume there's no overlap between our protocols and the server's list. |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 2007 | npn_status_ = kNextProtoNoOverlap; |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 2008 | |
| 2009 | // For each protocol in server preference order, see if we support it. |
| 2010 | for (unsigned int i = 0; i < inlen; i += in[i] + 1) { |
bnc | 1f29537 | 2015-10-21 23:24:22 | [diff] [blame] | 2011 | for (NextProto next_proto : ssl_config_.npn_protos) { |
bnc | 0d23cf4 | 2014-12-11 14:09:46 | [diff] [blame] | 2012 | const std::string proto = NextProtoToString(next_proto); |
| 2013 | if (in[i] == proto.size() && |
| 2014 | memcmp(&in[i + 1], proto.data(), in[i]) == 0) { |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 2015 | // We found a match. |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 2016 | negotiated_protocol_ = next_proto; |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 2017 | *out = const_cast<unsigned char*>(in) + i + 1; |
| 2018 | *outlen = in[i]; |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 2019 | npn_status_ = kNextProtoNegotiated; |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 2020 | break; |
| 2021 | } |
| 2022 | } |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 2023 | if (npn_status_ == kNextProtoNegotiated) |
[email protected] | ae7c9f4 | 2011-11-21 11:41:16 | [diff] [blame] | 2024 | break; |
| 2025 | } |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 2026 | |
bnc | de27dae | 2015-10-06 07:11:02 | [diff] [blame] | 2027 | // If we didn't find a protocol, we select the last one from our list. |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 2028 | if (npn_status_ == kNextProtoNoOverlap) { |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 2029 | negotiated_protocol_ = ssl_config_.npn_protos.back(); |
bnc | 67da3de | 2015-01-15 21:02:26 | [diff] [blame] | 2030 | // NextProtoToString returns a pointer to a static string. |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 2031 | const char* proto = NextProtoToString(negotiated_protocol_); |
bnc | 67da3de | 2015-01-15 21:02:26 | [diff] [blame] | 2032 | *out = reinterpret_cast<unsigned char*>(const_cast<char*>(proto)); |
| 2033 | *outlen = strlen(proto); |
[email protected] | 168a841 | 2012-06-14 05:05:49 | [diff] [blame] | 2034 | } |
| 2035 | |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 2036 | negotiation_extension_ = kExtensionNPN; |
[email protected] | ea4a1c6a | 2010-12-09 13:33:28 | [diff] [blame] | 2037 | return SSL_TLSEXT_ERR_OK; |
| 2038 | } |
| 2039 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 2040 | long SSLClientSocketImpl::MaybeReplayTransportError(BIO* bio, |
| 2041 | int cmd, |
| 2042 | const char* argp, |
| 2043 | int argi, |
| 2044 | long argl, |
| 2045 | long retvalue) { |
| 2046 | if (cmd == (BIO_CB_READ | BIO_CB_RETURN) && retvalue <= 0) { |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 2047 | // If there is no more data in the buffer, report any pending errors that |
| 2048 | // were observed. Note that both the readbuf and the writebuf are checked |
| 2049 | // for errors, since the application may have encountered a socket error |
| 2050 | // while writing that would otherwise not be reported until the application |
| 2051 | // attempted to write again - which it may never do. See |
| 2052 | // https://crbug.com/249848. |
| 2053 | if (transport_read_error_ != OK) { |
| 2054 | OpenSSLPutNetError(FROM_HERE, transport_read_error_); |
| 2055 | return -1; |
| 2056 | } |
| 2057 | if (transport_write_error_ != OK) { |
| 2058 | OpenSSLPutNetError(FROM_HERE, transport_write_error_); |
| 2059 | return -1; |
| 2060 | } |
| 2061 | } else if (cmd == BIO_CB_WRITE) { |
| 2062 | // Because of the write buffer, this reports a failure from the previous |
| 2063 | // write payload. If the current payload fails to write, the error will be |
| 2064 | // reported in a future write or read to |bio|. |
| 2065 | if (transport_write_error_ != OK) { |
| 2066 | OpenSSLPutNetError(FROM_HERE, transport_write_error_); |
| 2067 | return -1; |
| 2068 | } |
| 2069 | } |
| 2070 | return retvalue; |
| 2071 | } |
| 2072 | |
| 2073 | // static |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 2074 | long SSLClientSocketImpl::BIOCallback(BIO* bio, |
| 2075 | int cmd, |
| 2076 | const char* argp, |
| 2077 | int argi, |
| 2078 | long argl, |
| 2079 | long retvalue) { |
| 2080 | SSLClientSocketImpl* socket = |
| 2081 | reinterpret_cast<SSLClientSocketImpl*>(BIO_get_callback_arg(bio)); |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 2082 | CHECK(socket); |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 2083 | return socket->MaybeReplayTransportError(bio, cmd, argp, argi, argl, |
| 2084 | retvalue); |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 2085 | } |
| 2086 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 2087 | void SSLClientSocketImpl::MaybeCacheSession() { |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 2088 | // Only cache the session once both a new session has been established and the |
| 2089 | // certificate has been verified. Due to False Start, these events may happen |
| 2090 | // in either order. |
davidben | c269cc4b | 2016-07-27 14:55:03 | [diff] [blame] | 2091 | if (!pending_session_ || !certificate_verified_) |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 2092 | return; |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 2093 | |
| 2094 | SSLContext::GetInstance()->session_cache()->Insert(GetSessionCacheKey(), |
davidben | c269cc4b | 2016-07-27 14:55:03 | [diff] [blame] | 2095 | pending_session_.get()); |
| 2096 | pending_session_ = nullptr; |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 2097 | } |
| 2098 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 2099 | int SSLClientSocketImpl::NewSessionCallback(SSL_SESSION* session) { |
davidben | c269cc4b | 2016-07-27 14:55:03 | [diff] [blame] | 2100 | // OpenSSL passes a reference to |session|. |
| 2101 | pending_session_.reset(session); |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 2102 | MaybeCacheSession(); |
davidben | 44aeae6 | 2015-06-24 20:47:43 | [diff] [blame] | 2103 | return 1; |
davidben | dafe4e5 | 2015-04-08 22:53:52 | [diff] [blame] | 2104 | } |
| 2105 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 2106 | void SSLClientSocketImpl::AddCTInfoToSSLInfo(SSLInfo* ssl_info) const { |
estark | 723b5eeb | 2016-02-18 21:01:12 | [diff] [blame] | 2107 | ssl_info->UpdateCertificateTransparencyInfo(ct_verify_result_); |
davidben | eb5f8ef3 | 2014-09-04 14:14:32 | [diff] [blame] | 2108 | } |
| 2109 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 2110 | std::string SSLClientSocketImpl::GetSessionCacheKey() const { |
rsleevi | f020edc | 2015-03-16 19:31:24 | [diff] [blame] | 2111 | std::string result = host_and_port_.ToString(); |
| 2112 | result.append("/"); |
| 2113 | result.append(ssl_session_cache_shard_); |
| 2114 | |
| 2115 | // Shard the session cache based on maximum protocol version. This causes |
| 2116 | // fallback connections to use a separate session cache. |
| 2117 | result.append("/"); |
| 2118 | switch (ssl_config_.version_max) { |
rsleevi | f020edc | 2015-03-16 19:31:24 | [diff] [blame] | 2119 | case SSL_PROTOCOL_VERSION_TLS1: |
| 2120 | result.append("tls1"); |
| 2121 | break; |
| 2122 | case SSL_PROTOCOL_VERSION_TLS1_1: |
| 2123 | result.append("tls1.1"); |
| 2124 | break; |
| 2125 | case SSL_PROTOCOL_VERSION_TLS1_2: |
| 2126 | result.append("tls1.2"); |
| 2127 | break; |
svaldez | d3fd3ee | 2016-07-18 16:06:08 | [diff] [blame] | 2128 | case SSL_PROTOCOL_VERSION_TLS1_3: |
| 2129 | result.append("tls1.3"); |
| 2130 | break; |
rsleevi | f020edc | 2015-03-16 19:31:24 | [diff] [blame] | 2131 | default: |
| 2132 | NOTREACHED(); |
| 2133 | } |
| 2134 | |
davidben | a4c9d06 | 2015-04-03 22:34:25 | [diff] [blame] | 2135 | result.append("/"); |
davidben | 14b1a53 | 2015-10-30 16:01:09 | [diff] [blame] | 2136 | if (ssl_config_.deprecated_cipher_suites_enabled) |
davidben | a4c9d06 | 2015-04-03 22:34:25 | [diff] [blame] | 2137 | result.append("deprecated"); |
| 2138 | |
davidben | ee39de0 | 2015-10-16 19:53:18 | [diff] [blame] | 2139 | result.append("/"); |
| 2140 | if (ssl_config_.channel_id_enabled) |
| 2141 | result.append("channelid"); |
| 2142 | |
rsleevi | f020edc | 2015-03-16 19:31:24 | [diff] [blame] | 2143 | return result; |
| 2144 | } |
| 2145 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 2146 | bool SSLClientSocketImpl::IsRenegotiationAllowed() const { |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame] | 2147 | if (tb_was_negotiated_) |
| 2148 | return false; |
| 2149 | |
davidben | 421116c | 2015-05-12 19:56:51 | [diff] [blame] | 2150 | if (npn_status_ == kNextProtoUnsupported) |
| 2151 | return ssl_config_.renego_allowed_default; |
| 2152 | |
davidben | 421116c | 2015-05-12 19:56:51 | [diff] [blame] | 2153 | for (NextProto allowed : ssl_config_.renego_allowed_for_protos) { |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 2154 | if (negotiated_protocol_ == allowed) |
davidben | 421116c | 2015-05-12 19:56:51 | [diff] [blame] | 2155 | return true; |
| 2156 | } |
| 2157 | return false; |
| 2158 | } |
| 2159 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 2160 | int SSLClientSocketImpl::PrivateKeyTypeCallback() { |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 2161 | switch (ssl_config_.client_private_key->GetType()) { |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 2162 | case SSLPrivateKey::Type::RSA: |
| 2163 | return EVP_PKEY_RSA; |
| 2164 | case SSLPrivateKey::Type::ECDSA: |
| 2165 | return EVP_PKEY_EC; |
| 2166 | } |
| 2167 | NOTREACHED(); |
| 2168 | return EVP_PKEY_NONE; |
| 2169 | } |
| 2170 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 2171 | size_t SSLClientSocketImpl::PrivateKeyMaxSignatureLenCallback() { |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 2172 | return ssl_config_.client_private_key->GetMaxSignatureLengthInBytes(); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 2173 | } |
| 2174 | |
davidben | 0bca07fd | 2016-07-18 15:12:03 | [diff] [blame] | 2175 | ssl_private_key_result_t SSLClientSocketImpl::PrivateKeySignDigestCallback( |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 2176 | uint8_t* out, |
| 2177 | size_t* out_len, |
| 2178 | size_t max_out, |
| 2179 | const EVP_MD* md, |
| 2180 | const uint8_t* in, |
| 2181 | size_t in_len) { |
| 2182 | DCHECK_EQ(kNoPendingResult, signature_result_); |
| 2183 | DCHECK(signature_.empty()); |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 2184 | DCHECK(ssl_config_.client_private_key); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 2185 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 2186 | SSLPrivateKey::Hash hash; |
| 2187 | if (!EVP_MDToPrivateKeyHash(md, &hash)) { |
| 2188 | OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED); |
| 2189 | return ssl_private_key_failure; |
| 2190 | } |
| 2191 | |
davidben | 752bcf2 | 2015-12-21 22:55:50 | [diff] [blame] | 2192 | net_log_.BeginEvent( |
| 2193 | NetLog::TYPE_SSL_PRIVATE_KEY_OPERATION, |
| 2194 | base::Bind(&NetLogPrivateKeyOperationCallback, |
| 2195 | ssl_config_.client_private_key->GetType(), hash)); |
| 2196 | |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 2197 | signature_result_ = ERR_IO_PENDING; |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 2198 | ssl_config_.client_private_key->SignDigest( |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 2199 | hash, base::StringPiece(reinterpret_cast<const char*>(in), in_len), |
davidben | 0bca07fd | 2016-07-18 15:12:03 | [diff] [blame] | 2200 | base::Bind(&SSLClientSocketImpl::OnPrivateKeyComplete, |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 2201 | weak_factory_.GetWeakPtr())); |
| 2202 | return ssl_private_key_retry; |
| 2203 | } |
| 2204 | |
davidben | 0bca07fd | 2016-07-18 15:12:03 | [diff] [blame] | 2205 | ssl_private_key_result_t SSLClientSocketImpl::PrivateKeyCompleteCallback( |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 2206 | uint8_t* out, |
| 2207 | size_t* out_len, |
| 2208 | size_t max_out) { |
| 2209 | DCHECK_NE(kNoPendingResult, signature_result_); |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 2210 | DCHECK(ssl_config_.client_private_key); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 2211 | |
| 2212 | if (signature_result_ == ERR_IO_PENDING) |
| 2213 | return ssl_private_key_retry; |
| 2214 | if (signature_result_ != OK) { |
| 2215 | OpenSSLPutNetError(FROM_HERE, signature_result_); |
| 2216 | return ssl_private_key_failure; |
| 2217 | } |
| 2218 | if (signature_.size() > max_out) { |
| 2219 | OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED); |
| 2220 | return ssl_private_key_failure; |
| 2221 | } |
davidben | 5f8b6bc | 2015-11-25 03:19:54 | [diff] [blame] | 2222 | memcpy(out, signature_.data(), signature_.size()); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 2223 | *out_len = signature_.size(); |
| 2224 | signature_.clear(); |
| 2225 | return ssl_private_key_success; |
| 2226 | } |
| 2227 | |
davidben | 0bca07fd | 2016-07-18 15:12:03 | [diff] [blame] | 2228 | void SSLClientSocketImpl::OnPrivateKeyComplete( |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 2229 | Error error, |
| 2230 | const std::vector<uint8_t>& signature) { |
| 2231 | DCHECK_EQ(ERR_IO_PENDING, signature_result_); |
| 2232 | DCHECK(signature_.empty()); |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 2233 | DCHECK(ssl_config_.client_private_key); |
davidben | 1d48952 | 2015-07-01 18:48:46 | [diff] [blame] | 2234 | |
| 2235 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_PRIVATE_KEY_OPERATION, |
| 2236 | error); |
| 2237 | |
| 2238 | signature_result_ = error; |
| 2239 | if (signature_result_ == OK) |
| 2240 | signature_ = signature; |
| 2241 | |
| 2242 | if (next_handshake_state_ == STATE_HANDSHAKE) { |
| 2243 | OnHandshakeIOComplete(signature_result_); |
| 2244 | return; |
| 2245 | } |
| 2246 | |
| 2247 | // During a renegotiation, either Read or Write calls may be blocked on an |
| 2248 | // asynchronous private key operation. |
| 2249 | PumpReadWriteEvents(); |
| 2250 | } |
| 2251 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 2252 | int SSLClientSocketImpl::TokenBindingAdd(const uint8_t** out, |
| 2253 | size_t* out_len, |
| 2254 | int* out_alert_value) { |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame] | 2255 | if (ssl_config_.token_binding_params.empty()) { |
| 2256 | return 0; |
| 2257 | } |
davidben | 7dad2a3 | 2016-03-01 23:47:47 | [diff] [blame] | 2258 | crypto::AutoCBB output; |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame] | 2259 | CBB parameters_list; |
| 2260 | if (!CBB_init(output.get(), 7) || |
| 2261 | !CBB_add_u8(output.get(), kTbProtocolVersionMajor) || |
| 2262 | !CBB_add_u8(output.get(), kTbProtocolVersionMinor) || |
| 2263 | !CBB_add_u8_length_prefixed(output.get(), ¶meters_list)) { |
| 2264 | *out_alert_value = SSL_AD_INTERNAL_ERROR; |
| 2265 | return -1; |
| 2266 | } |
| 2267 | for (size_t i = 0; i < ssl_config_.token_binding_params.size(); ++i) { |
| 2268 | if (!CBB_add_u8(¶meters_list, ssl_config_.token_binding_params[i])) { |
| 2269 | *out_alert_value = SSL_AD_INTERNAL_ERROR; |
| 2270 | return -1; |
| 2271 | } |
| 2272 | } |
| 2273 | // |*out| will be freed by TokenBindingFreeCallback. |
| 2274 | if (!CBB_finish(output.get(), const_cast<uint8_t**>(out), out_len)) { |
| 2275 | *out_alert_value = SSL_AD_INTERNAL_ERROR; |
| 2276 | return -1; |
| 2277 | } |
| 2278 | |
| 2279 | return 1; |
| 2280 | } |
| 2281 | |
svaldez | e83af29 | 2016-04-26 14:33:37 | [diff] [blame] | 2282 | int SSLClientSocketImpl::TokenBindingParse(const uint8_t* contents, |
| 2283 | size_t contents_len, |
| 2284 | int* out_alert_value) { |
nharper | 736ceda | 2015-11-07 00:16:59 | [diff] [blame] | 2285 | if (completed_connect_) { |
| 2286 | // Token Binding may only be negotiated on the initial handshake. |
| 2287 | *out_alert_value = SSL_AD_ILLEGAL_PARAMETER; |
| 2288 | return 0; |
| 2289 | } |
| 2290 | |
| 2291 | CBS extension; |
| 2292 | CBS_init(&extension, contents, contents_len); |
| 2293 | |
| 2294 | CBS parameters_list; |
| 2295 | uint8_t version_major, version_minor, param; |
| 2296 | if (!CBS_get_u8(&extension, &version_major) || |
| 2297 | !CBS_get_u8(&extension, &version_minor) || |
| 2298 | !CBS_get_u8_length_prefixed(&extension, ¶meters_list) || |
| 2299 | !CBS_get_u8(¶meters_list, ¶m) || CBS_len(¶meters_list) > 0 || |
| 2300 | CBS_len(&extension) > 0) { |
| 2301 | *out_alert_value = SSL_AD_DECODE_ERROR; |
| 2302 | return 0; |
| 2303 | } |
| 2304 | // The server-negotiated version must be less than or equal to our version. |
| 2305 | if (version_major > kTbProtocolVersionMajor || |
| 2306 | (version_minor > kTbProtocolVersionMinor && |
| 2307 | version_major == kTbProtocolVersionMajor)) { |
| 2308 | *out_alert_value = SSL_AD_ILLEGAL_PARAMETER; |
| 2309 | return 0; |
| 2310 | } |
| 2311 | // If the version the server negotiated is older than we support, don't fail |
| 2312 | // parsing the extension, but also don't set |negotiated_|. |
| 2313 | if (version_major < kTbMinProtocolVersionMajor || |
| 2314 | (version_minor < kTbMinProtocolVersionMinor && |
| 2315 | version_major == kTbMinProtocolVersionMajor)) { |
| 2316 | return 1; |
| 2317 | } |
| 2318 | |
| 2319 | for (size_t i = 0; i < ssl_config_.token_binding_params.size(); ++i) { |
| 2320 | if (param == ssl_config_.token_binding_params[i]) { |
| 2321 | tb_negotiated_param_ = ssl_config_.token_binding_params[i]; |
| 2322 | tb_was_negotiated_ = true; |
| 2323 | return 1; |
| 2324 | } |
| 2325 | } |
| 2326 | |
| 2327 | *out_alert_value = SSL_AD_ILLEGAL_PARAMETER; |
| 2328 | return 0; |
| 2329 | } |
| 2330 | |
davidben | 281d13f0 | 2016-04-27 20:43:28 | [diff] [blame] | 2331 | void SSLClientSocketImpl::LogConnectEndEvent(int rv) { |
| 2332 | if (rv != OK) { |
| 2333 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); |
| 2334 | return; |
| 2335 | } |
| 2336 | |
| 2337 | net_log_.EndEvent(NetLog::TYPE_SSL_CONNECT, |
| 2338 | base::Bind(&NetLogSSLInfoCallback, base::Unretained(this))); |
| 2339 | } |
| 2340 | |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 2341 | void SSLClientSocketImpl::RecordNegotiationExtension() const { |
| 2342 | if (negotiation_extension_ == kExtensionUnknown) |
| 2343 | return; |
| 2344 | if (npn_status_ == kNextProtoUnsupported) |
| 2345 | return; |
| 2346 | base::HistogramBase::Sample sample = |
| 2347 | static_cast<base::HistogramBase::Sample>(negotiated_protocol_); |
| 2348 | // In addition to the protocol negotiated, we want to record which TLS |
| 2349 | // extension was used, and in case of NPN, whether there was overlap between |
| 2350 | // server and client list of supported protocols. |
| 2351 | if (negotiation_extension_ == kExtensionNPN) { |
| 2352 | if (npn_status_ == kNextProtoNoOverlap) { |
| 2353 | sample += 1000; |
| 2354 | } else { |
| 2355 | sample += 500; |
| 2356 | } |
| 2357 | } else { |
| 2358 | DCHECK_EQ(kExtensionALPN, negotiation_extension_); |
| 2359 | } |
| 2360 | UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SSLProtocolNegotiation", sample); |
| 2361 | } |
| 2362 | |
| 2363 | void SSLClientSocketImpl::RecordChannelIDSupport() const { |
| 2364 | // Since this enum is used for a histogram, do not change or re-use values. |
| 2365 | enum { |
| 2366 | DISABLED = 0, |
| 2367 | CLIENT_ONLY = 1, |
| 2368 | CLIENT_AND_SERVER = 2, |
| 2369 | // CLIENT_NO_ECC is unused now. |
| 2370 | // CLIENT_BAD_SYSTEM_TIME is unused now. |
| 2371 | CLIENT_BAD_SYSTEM_TIME = 4, |
| 2372 | CLIENT_NO_CHANNEL_ID_SERVICE = 5, |
| 2373 | CHANNEL_ID_USAGE_MAX |
| 2374 | } supported = DISABLED; |
| 2375 | if (channel_id_sent_) { |
| 2376 | supported = CLIENT_AND_SERVER; |
| 2377 | } else if (ssl_config_.channel_id_enabled) { |
| 2378 | if (!channel_id_service_) |
| 2379 | supported = CLIENT_NO_CHANNEL_ID_SERVICE; |
| 2380 | else |
| 2381 | supported = CLIENT_ONLY; |
| 2382 | } |
| 2383 | UMA_HISTOGRAM_ENUMERATION("DomainBoundCerts.Support", supported, |
| 2384 | CHANNEL_ID_USAGE_MAX); |
| 2385 | } |
| 2386 | |
| 2387 | bool SSLClientSocketImpl::IsChannelIDEnabled() const { |
| 2388 | return ssl_config_.channel_id_enabled && channel_id_service_; |
| 2389 | } |
| 2390 | |
[email protected] | 7e5dd49f | 2010-12-08 18:33:49 | [diff] [blame] | 2391 | } // namespace net |