Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // OpenSSL binding for SSLClientSocket. The class layout and general principle | 5 // OpenSSL binding for SSLClientSocket. The class layout and general principle |
| 6 // of operation is derived from SSLClientSocketNSS. | 6 // of operation is derived from SSLClientSocketNSS. |
| 7 | 7 |
| 8 #include "net/socket/ssl_client_socket_openssl.h" | 8 #include "net/socket/ssl_client_socket_openssl.h" |
| 9 | 9 |
| 10 #include <errno.h> | 10 #include <errno.h> |
| 11 #include <openssl/bio.h> | 11 #include <openssl/bio.h> |
| 12 #include <openssl/err.h> | 12 #include <openssl/err.h> |
| 13 #include <openssl/mem.h> | 13 #include <openssl/mem.h> |
| 14 #include <openssl/ssl.h> | 14 #include <openssl/ssl.h> |
| 15 #include <string.h> | 15 #include <string.h> |
| 16 | 16 |
| 17 #include "base/bind.h" | 17 #include "base/bind.h" |
| 18 #include "base/callback_helpers.h" | 18 #include "base/callback_helpers.h" |
| 19 #include "base/environment.h" | 19 #include "base/environment.h" |
| 20 #include "base/lazy_instance.h" | |
| 20 #include "base/memory/singleton.h" | 21 #include "base/memory/singleton.h" |
| 21 #include "base/metrics/histogram_macros.h" | 22 #include "base/metrics/histogram_macros.h" |
| 22 #include "base/profiler/scoped_tracker.h" | 23 #include "base/profiler/scoped_tracker.h" |
| 24 #include "base/stl_util.h" | |
| 23 #include "base/strings/string_piece.h" | 25 #include "base/strings/string_piece.h" |
| 24 #include "base/synchronization/lock.h" | 26 #include "base/synchronization/lock.h" |
| 27 #include "base/threading/sequenced_worker_pool.h" | |
| 25 #include "base/threading/thread_local.h" | 28 #include "base/threading/thread_local.h" |
| 26 #include "base/values.h" | 29 #include "base/values.h" |
| 27 #include "crypto/ec_private_key.h" | 30 #include "crypto/ec_private_key.h" |
| 28 #include "crypto/openssl_util.h" | 31 #include "crypto/openssl_util.h" |
| 29 #include "crypto/scoped_openssl_types.h" | 32 #include "crypto/scoped_openssl_types.h" |
| 30 #include "net/base/ip_address_number.h" | 33 #include "net/base/ip_address_number.h" |
| 31 #include "net/base/net_errors.h" | 34 #include "net/base/net_errors.h" |
| 32 #include "net/cert/cert_policy_enforcer.h" | 35 #include "net/cert/cert_policy_enforcer.h" |
| 33 #include "net/cert/cert_verifier.h" | 36 #include "net/cert/cert_verifier.h" |
| 34 #include "net/cert/ct_ev_whitelist.h" | 37 #include "net/cert/ct_ev_whitelist.h" |
| 35 #include "net/cert/ct_verifier.h" | 38 #include "net/cert/ct_verifier.h" |
| 36 #include "net/cert/x509_certificate_net_log_param.h" | 39 #include "net/cert/x509_certificate_net_log_param.h" |
| 37 #include "net/cert/x509_util_openssl.h" | 40 #include "net/cert/x509_util_openssl.h" |
| 38 #include "net/http/transport_security_state.h" | 41 #include "net/http/transport_security_state.h" |
| 39 #include "net/ssl/scoped_openssl_types.h" | 42 #include "net/ssl/scoped_openssl_types.h" |
| 40 #include "net/ssl/ssl_cert_request_info.h" | 43 #include "net/ssl/ssl_cert_request_info.h" |
| 41 #include "net/ssl/ssl_client_session_cache_openssl.h" | 44 #include "net/ssl/ssl_client_session_cache_openssl.h" |
| 42 #include "net/ssl/ssl_connection_status_flags.h" | 45 #include "net/ssl/ssl_connection_status_flags.h" |
| 43 #include "net/ssl/ssl_failure_state.h" | 46 #include "net/ssl/ssl_failure_state.h" |
| 44 #include "net/ssl/ssl_info.h" | 47 #include "net/ssl/ssl_info.h" |
| 48 #include "net/ssl/ssl_private_key.h" | |
| 45 | 49 |
| 46 #if defined(OS_WIN) | 50 #if defined(OS_WIN) |
| 47 #include "base/win/windows_version.h" | 51 #include "base/win/windows_version.h" |
| 48 #endif | 52 #endif |
| 49 | 53 |
| 50 #if defined(USE_OPENSSL_CERTS) | 54 #if defined(USE_OPENSSL_CERTS) |
| 51 #include "net/ssl/openssl_client_key_store.h" | 55 #include "net/ssl/openssl_client_key_store.h" |
| 52 #else | 56 #else |
| 53 #include "net/ssl/openssl_platform_key.h" | 57 #include "net/ssl/ssl_platform_key.h" |
| 54 #endif | 58 #endif |
| 55 | 59 |
| 56 namespace net { | 60 namespace net { |
| 57 | 61 |
| 58 namespace { | 62 namespace { |
| 59 | 63 |
| 60 // Enable this to see logging for state machine state transitions. | 64 // Enable this to see logging for state machine state transitions. |
| 61 #if 0 | 65 #if 0 |
| 62 #define GotoState(s) do { DVLOG(2) << (void *)this << " " << __FUNCTION__ << \ | 66 #define GotoState(s) do { DVLOG(2) << (void *)this << " " << __FUNCTION__ << \ |
| 63 " jump to state " << s; \ | 67 " jump to state " << s; \ |
| 64 next_handshake_state_ = s; } while (0) | 68 next_handshake_state_ = s; } while (0) |
| 65 #else | 69 #else |
| 66 #define GotoState(s) next_handshake_state_ = s | 70 #define GotoState(s) next_handshake_state_ = s |
| 67 #endif | 71 #endif |
| 68 | 72 |
| 69 // This constant can be any non-negative/non-zero value (eg: it does not | 73 // This constant can be any non-negative/non-zero value (eg: it does not |
| 70 // overlap with any value of the net::Error range, including net::OK). | 74 // overlap with any value of the net::Error range, including net::OK). |
| 71 const int kNoPendingReadResult = 1; | 75 const int kNoPendingResult = 1; |
| 72 | 76 |
| 73 // If a client doesn't have a list of protocols that it supports, but | 77 // If a client doesn't have a list of protocols that it supports, but |
| 74 // the server supports NPN, choosing "http/1.1" is the best answer. | 78 // the server supports NPN, choosing "http/1.1" is the best answer. |
| 75 const char kDefaultSupportedNPNProtocol[] = "http/1.1"; | 79 const char kDefaultSupportedNPNProtocol[] = "http/1.1"; |
| 76 | 80 |
| 77 // Default size of the internal BoringSSL buffers. | 81 // Default size of the internal BoringSSL buffers. |
| 78 const int KDefaultOpenSSLBufferSize = 17 * 1024; | 82 const int KDefaultOpenSSLBufferSize = 17 * 1024; |
| 79 | 83 |
| 80 void FreeX509Stack(STACK_OF(X509)* ptr) { | 84 void FreeX509Stack(STACK_OF(X509)* ptr) { |
| 81 sk_X509_pop_free(ptr, X509_free); | 85 sk_X509_pop_free(ptr, X509_free); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 sk_X509_push(stack.get(), x509.release()); | 142 sk_X509_push(stack.get(), x509.release()); |
| 139 } | 143 } |
| 140 return stack.Pass(); | 144 return stack.Pass(); |
| 141 } | 145 } |
| 142 | 146 |
| 143 int LogErrorCallback(const char* str, size_t len, void* context) { | 147 int LogErrorCallback(const char* str, size_t len, void* context) { |
| 144 LOG(ERROR) << base::StringPiece(str, len); | 148 LOG(ERROR) << base::StringPiece(str, len); |
| 145 return 1; | 149 return 1; |
| 146 } | 150 } |
| 147 | 151 |
| 152 bool EVP_MDToPrivateKeyHash(const EVP_MD* md, SSLPrivateKey::Hash* hash) { | |
| 153 switch (EVP_MD_type(md)) { | |
| 154 case NID_md5_sha1: | |
| 155 *hash = SSLPrivateKey::Hash::MD5_SHA1; | |
| 156 return true; | |
| 157 case NID_md5: | |
| 158 *hash = SSLPrivateKey::Hash::MD5; | |
| 159 return true; | |
| 160 case NID_sha1: | |
| 161 *hash = SSLPrivateKey::Hash::SHA1; | |
| 162 return true; | |
| 163 case NID_sha224: | |
| 164 *hash = SSLPrivateKey::Hash::SHA224; | |
|
Ryan Sleevi
2015/06/23 15:27:28
There are no plans to ever support this, right? Pr
davidben
2015/06/24 21:43:12
Oh hrm. Yes, you're right. In fact ssl_platform_ke
| |
| 165 return true; | |
| 166 case NID_sha256: | |
| 167 *hash = SSLPrivateKey::Hash::SHA256; | |
| 168 return true; | |
| 169 case NID_sha384: | |
| 170 *hash = SSLPrivateKey::Hash::SHA384; | |
| 171 return true; | |
| 172 case NID_sha512: | |
| 173 *hash = SSLPrivateKey::Hash::SHA512; | |
| 174 return true; | |
| 175 default: | |
| 176 return false; | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 #if !defined(USE_OPENSSL_CERTS) | |
| 181 class PlatformKeyTaskRunner { | |
| 182 public: | |
| 183 PlatformKeyTaskRunner() { | |
| 184 // Serialize all the private key operations on a single background | |
| 185 // thread to avoid problems with buggy smartcards. | |
| 186 worker_pool_ = new base::SequencedWorkerPool(1, "Platform Key Thread"); | |
| 187 task_runner_ = worker_pool_->GetSequencedTaskRunnerWithShutdownBehavior( | |
| 188 worker_pool_->GetSequenceToken(), | |
| 189 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); | |
| 190 } | |
| 191 | |
| 192 base::SequencedTaskRunner* task_runner() { return task_runner_.get(); } | |
|
Ryan Sleevi
2015/06/23 15:27:28
DANGER: There's a slight 'danger' in this API shap
davidben
2015/06/24 21:43:12
Pass() doesn't work unless we pass parameters in a
Ryan Sleevi
2015/06/30 10:18:22
Yes :)
| |
| 193 | |
| 194 private: | |
| 195 scoped_refptr<base::SequencedWorkerPool> worker_pool_; | |
| 196 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
| 197 | |
| 198 DISALLOW_COPY_AND_ASSIGN(PlatformKeyTaskRunner); | |
| 199 }; | |
| 200 | |
| 201 base::LazyInstance<PlatformKeyTaskRunner>::Leaky g_platform_key_task_runner = | |
| 202 LAZY_INSTANCE_INITIALIZER; | |
| 203 #endif // !USE_OPENSSL_CERTS | |
| 204 | |
| 148 } // namespace | 205 } // namespace |
| 149 | 206 |
| 150 class SSLClientSocketOpenSSL::SSLContext { | 207 class SSLClientSocketOpenSSL::SSLContext { |
| 151 public: | 208 public: |
| 152 static SSLContext* GetInstance() { return Singleton<SSLContext>::get(); } | 209 static SSLContext* GetInstance() { return Singleton<SSLContext>::get(); } |
| 153 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } | 210 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } |
| 154 SSLClientSessionCacheOpenSSL* session_cache() { return &session_cache_; } | 211 SSLClientSessionCacheOpenSSL* session_cache() { return &session_cache_; } |
| 155 | 212 |
| 156 SSLClientSocketOpenSSL* GetClientSocketFromSSL(const SSL* ssl) { | 213 SSLClientSocketOpenSSL* GetClientSocketFromSSL(const SSL* ssl) { |
| 157 DCHECK(ssl); | 214 DCHECK(ssl); |
| 158 SSLClientSocketOpenSSL* socket = static_cast<SSLClientSocketOpenSSL*>( | 215 SSLClientSocketOpenSSL* socket = static_cast<SSLClientSocketOpenSSL*>( |
| 159 SSL_get_ex_data(ssl, ssl_socket_data_index_)); | 216 SSL_get_ex_data(ssl, ssl_socket_data_index_)); |
| 160 DCHECK(socket); | 217 DCHECK(socket); |
| 161 return socket; | 218 return socket; |
| 162 } | 219 } |
| 163 | 220 |
| 164 bool SetClientSocketForSSL(SSL* ssl, SSLClientSocketOpenSSL* socket) { | 221 bool SetClientSocketForSSL(SSL* ssl, SSLClientSocketOpenSSL* socket) { |
| 165 return SSL_set_ex_data(ssl, ssl_socket_data_index_, socket) != 0; | 222 return SSL_set_ex_data(ssl, ssl_socket_data_index_, socket) != 0; |
| 166 } | 223 } |
| 167 | 224 |
| 225 static const SSL_PRIVATE_KEY_METHOD kPrivateKeyMethod; | |
| 226 | |
| 168 private: | 227 private: |
| 169 friend struct DefaultSingletonTraits<SSLContext>; | 228 friend struct DefaultSingletonTraits<SSLContext>; |
| 170 | 229 |
| 171 SSLContext() : session_cache_(SSLClientSessionCacheOpenSSL::Config()) { | 230 SSLContext() : session_cache_(SSLClientSessionCacheOpenSSL::Config()) { |
| 172 crypto::EnsureOpenSSLInit(); | 231 crypto::EnsureOpenSSLInit(); |
| 173 ssl_socket_data_index_ = SSL_get_ex_new_index(0, 0, 0, 0, 0); | 232 ssl_socket_data_index_ = SSL_get_ex_new_index(0, 0, 0, 0, 0); |
| 174 DCHECK_NE(ssl_socket_data_index_, -1); | 233 DCHECK_NE(ssl_socket_data_index_, -1); |
| 175 ssl_ctx_.reset(SSL_CTX_new(SSLv23_client_method())); | 234 ssl_ctx_.reset(SSL_CTX_new(SSLv23_client_method())); |
| 176 SSL_CTX_set_cert_verify_callback(ssl_ctx_.get(), CertVerifyCallback, NULL); | 235 SSL_CTX_set_cert_verify_callback(ssl_ctx_.get(), CertVerifyCallback, NULL); |
| 177 SSL_CTX_set_cert_cb(ssl_ctx_.get(), ClientCertRequestCallback, NULL); | 236 SSL_CTX_set_cert_cb(ssl_ctx_.get(), ClientCertRequestCallback, NULL); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 229 unsigned int inlen, void* arg) { | 288 unsigned int inlen, void* arg) { |
| 230 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | 289 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
| 231 return socket->SelectNextProtoCallback(out, outlen, in, inlen); | 290 return socket->SelectNextProtoCallback(out, outlen, in, inlen); |
| 232 } | 291 } |
| 233 | 292 |
| 234 static void InfoCallback(const SSL* ssl, int type, int val) { | 293 static void InfoCallback(const SSL* ssl, int type, int val) { |
| 235 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | 294 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
| 236 socket->InfoCallback(type, val); | 295 socket->InfoCallback(type, val); |
| 237 } | 296 } |
| 238 | 297 |
| 298 static int PrivateKeyTypeCallback(SSL* ssl) { | |
| 299 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | |
| 300 return socket->PrivateKeyTypeCallback(); | |
| 301 } | |
| 302 | |
| 303 static int PrivateKeySupportsDigestCallback(SSL* ssl, const EVP_MD* md) { | |
| 304 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | |
| 305 return socket->PrivateKeySupportsDigestCallback(md); | |
| 306 } | |
| 307 | |
| 308 static size_t PrivateKeyMaxSignatureLenCallback(SSL* ssl) { | |
| 309 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | |
| 310 return socket->PrivateKeyMaxSignatureLenCallback(); | |
| 311 } | |
| 312 | |
| 313 static ssl_private_key_result_t PrivateKeySignCallback(SSL* ssl, | |
| 314 uint8_t* out, | |
| 315 size_t* out_len, | |
| 316 size_t max_out, | |
| 317 const EVP_MD* md, | |
| 318 const uint8_t* in, | |
| 319 size_t in_len) { | |
| 320 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | |
| 321 return socket->PrivateKeySignCallback(out, out_len, max_out, md, in, | |
| 322 in_len); | |
| 323 } | |
| 324 | |
| 325 static ssl_private_key_result_t PrivateKeySignCompleteCallback( | |
| 326 SSL* ssl, | |
| 327 uint8_t* out, | |
| 328 size_t* out_len, | |
| 329 size_t max_out) { | |
| 330 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | |
| 331 return socket->PrivateKeySignCompleteCallback(out, out_len, max_out); | |
| 332 } | |
| 333 | |
| 239 // This is the index used with SSL_get_ex_data to retrieve the owner | 334 // This is the index used with SSL_get_ex_data to retrieve the owner |
| 240 // SSLClientSocketOpenSSL object from an SSL instance. | 335 // SSLClientSocketOpenSSL object from an SSL instance. |
| 241 int ssl_socket_data_index_; | 336 int ssl_socket_data_index_; |
| 242 | 337 |
| 243 ScopedSSL_CTX ssl_ctx_; | 338 ScopedSSL_CTX ssl_ctx_; |
| 244 | 339 |
| 245 // TODO(davidben): Use a separate cache per URLRequestContext. | 340 // TODO(davidben): Use a separate cache per URLRequestContext. |
| 246 // https://crbug.com/458365 | 341 // https://crbug.com/458365 |
| 247 // | 342 // |
| 248 // TODO(davidben): Sessions should be invalidated on fatal | 343 // TODO(davidben): Sessions should be invalidated on fatal |
| 249 // alerts. https://crbug.com/466352 | 344 // alerts. https://crbug.com/466352 |
| 250 SSLClientSessionCacheOpenSSL session_cache_; | 345 SSLClientSessionCacheOpenSSL session_cache_; |
| 251 }; | 346 }; |
| 252 | 347 |
| 348 const SSL_PRIVATE_KEY_METHOD | |
| 349 SSLClientSocketOpenSSL::SSLContext::kPrivateKeyMethod = { | |
| 350 &SSLClientSocketOpenSSL::SSLContext::PrivateKeyTypeCallback, | |
| 351 &SSLClientSocketOpenSSL::SSLContext::PrivateKeySupportsDigestCallback, | |
| 352 &SSLClientSocketOpenSSL::SSLContext::PrivateKeyMaxSignatureLenCallback, | |
| 353 &SSLClientSocketOpenSSL::SSLContext::PrivateKeySignCallback, | |
| 354 &SSLClientSocketOpenSSL::SSLContext::PrivateKeySignCompleteCallback, | |
| 355 }; | |
| 356 | |
| 253 // PeerCertificateChain is a helper object which extracts the certificate | 357 // PeerCertificateChain is a helper object which extracts the certificate |
| 254 // chain, as given by the server, from an OpenSSL socket and performs the needed | 358 // chain, as given by the server, from an OpenSSL socket and performs the needed |
| 255 // resource management. The first element of the chain is the leaf certificate | 359 // resource management. The first element of the chain is the leaf certificate |
| 256 // and the other elements are in the order given by the server. | 360 // and the other elements are in the order given by the server. |
| 257 class SSLClientSocketOpenSSL::PeerCertificateChain { | 361 class SSLClientSocketOpenSSL::PeerCertificateChain { |
| 258 public: | 362 public: |
| 259 explicit PeerCertificateChain(STACK_OF(X509)* chain) { Reset(chain); } | 363 explicit PeerCertificateChain(STACK_OF(X509)* chain) { Reset(chain); } |
| 260 PeerCertificateChain(const PeerCertificateChain& other) { *this = other; } | 364 PeerCertificateChain(const PeerCertificateChain& other) { *this = other; } |
| 261 ~PeerCertificateChain() {} | 365 ~PeerCertificateChain() {} |
| 262 PeerCertificateChain& operator=(const PeerCertificateChain& other); | 366 PeerCertificateChain& operator=(const PeerCertificateChain& other); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 return SSL_PROTOCOL_VERSION_TLS1_2; | 447 return SSL_PROTOCOL_VERSION_TLS1_2; |
| 344 } | 448 } |
| 345 | 449 |
| 346 SSLClientSocketOpenSSL::SSLClientSocketOpenSSL( | 450 SSLClientSocketOpenSSL::SSLClientSocketOpenSSL( |
| 347 scoped_ptr<ClientSocketHandle> transport_socket, | 451 scoped_ptr<ClientSocketHandle> transport_socket, |
| 348 const HostPortPair& host_and_port, | 452 const HostPortPair& host_and_port, |
| 349 const SSLConfig& ssl_config, | 453 const SSLConfig& ssl_config, |
| 350 const SSLClientSocketContext& context) | 454 const SSLClientSocketContext& context) |
| 351 : transport_send_busy_(false), | 455 : transport_send_busy_(false), |
| 352 transport_recv_busy_(false), | 456 transport_recv_busy_(false), |
| 353 pending_read_error_(kNoPendingReadResult), | 457 pending_read_error_(kNoPendingResult), |
| 354 pending_read_ssl_error_(SSL_ERROR_NONE), | 458 pending_read_ssl_error_(SSL_ERROR_NONE), |
| 355 transport_read_error_(OK), | 459 transport_read_error_(OK), |
| 356 transport_write_error_(OK), | 460 transport_write_error_(OK), |
| 357 server_cert_chain_(new PeerCertificateChain(NULL)), | 461 server_cert_chain_(new PeerCertificateChain(NULL)), |
| 358 completed_connect_(false), | 462 completed_connect_(false), |
| 359 was_ever_used_(false), | 463 was_ever_used_(false), |
| 360 cert_verifier_(context.cert_verifier), | 464 cert_verifier_(context.cert_verifier), |
| 361 cert_transparency_verifier_(context.cert_transparency_verifier), | 465 cert_transparency_verifier_(context.cert_transparency_verifier), |
| 362 channel_id_service_(context.channel_id_service), | 466 channel_id_service_(context.channel_id_service), |
| 363 ssl_(NULL), | 467 ssl_(NULL), |
| 364 transport_bio_(NULL), | 468 transport_bio_(NULL), |
| 365 transport_(transport_socket.Pass()), | 469 transport_(transport_socket.Pass()), |
| 366 host_and_port_(host_and_port), | 470 host_and_port_(host_and_port), |
| 367 ssl_config_(ssl_config), | 471 ssl_config_(ssl_config), |
| 368 ssl_session_cache_shard_(context.ssl_session_cache_shard), | 472 ssl_session_cache_shard_(context.ssl_session_cache_shard), |
| 369 next_handshake_state_(STATE_NONE), | 473 next_handshake_state_(STATE_NONE), |
| 370 npn_status_(kNextProtoUnsupported), | 474 npn_status_(kNextProtoUnsupported), |
| 371 channel_id_sent_(false), | 475 channel_id_sent_(false), |
| 372 handshake_completed_(false), | 476 handshake_completed_(false), |
| 373 certificate_verified_(false), | 477 certificate_verified_(false), |
| 374 ssl_failure_state_(SSL_FAILURE_NONE), | 478 ssl_failure_state_(SSL_FAILURE_NONE), |
| 479 signature_result_(kNoPendingResult), | |
| 375 transport_security_state_(context.transport_security_state), | 480 transport_security_state_(context.transport_security_state), |
| 376 policy_enforcer_(context.cert_policy_enforcer), | 481 policy_enforcer_(context.cert_policy_enforcer), |
| 377 net_log_(transport_->socket()->NetLog()), | 482 net_log_(transport_->socket()->NetLog()), |
| 378 weak_factory_(this) { | 483 weak_factory_(this) { |
| 379 DCHECK(cert_verifier_); | 484 DCHECK(cert_verifier_); |
| 380 } | 485 } |
| 381 | 486 |
| 382 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { | 487 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { |
| 383 Disconnect(); | 488 Disconnect(); |
| 384 } | 489 } |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 486 recv_buffer_ = NULL; | 591 recv_buffer_ = NULL; |
| 487 | 592 |
| 488 user_connect_callback_.Reset(); | 593 user_connect_callback_.Reset(); |
| 489 user_read_callback_.Reset(); | 594 user_read_callback_.Reset(); |
| 490 user_write_callback_.Reset(); | 595 user_write_callback_.Reset(); |
| 491 user_read_buf_ = NULL; | 596 user_read_buf_ = NULL; |
| 492 user_read_buf_len_ = 0; | 597 user_read_buf_len_ = 0; |
| 493 user_write_buf_ = NULL; | 598 user_write_buf_ = NULL; |
| 494 user_write_buf_len_ = 0; | 599 user_write_buf_len_ = 0; |
| 495 | 600 |
| 496 pending_read_error_ = kNoPendingReadResult; | 601 pending_read_error_ = kNoPendingResult; |
| 497 pending_read_ssl_error_ = SSL_ERROR_NONE; | 602 pending_read_ssl_error_ = SSL_ERROR_NONE; |
| 498 pending_read_error_info_ = OpenSSLErrorInfo(); | 603 pending_read_error_info_ = OpenSSLErrorInfo(); |
| 499 | 604 |
| 500 transport_read_error_ = OK; | 605 transport_read_error_ = OK; |
| 501 transport_write_error_ = OK; | 606 transport_write_error_ = OK; |
| 502 | 607 |
| 503 server_cert_verify_result_.Reset(); | 608 server_cert_verify_result_.Reset(); |
| 504 completed_connect_ = false; | 609 completed_connect_ = false; |
| 505 | 610 |
| 506 cert_authorities_.clear(); | 611 cert_authorities_.clear(); |
| 507 cert_key_types_.clear(); | 612 cert_key_types_.clear(); |
| 508 | 613 |
| 509 start_cert_verification_time_ = base::TimeTicks(); | 614 start_cert_verification_time_ = base::TimeTicks(); |
| 510 | 615 |
| 511 npn_status_ = kNextProtoUnsupported; | 616 npn_status_ = kNextProtoUnsupported; |
| 512 npn_proto_.clear(); | 617 npn_proto_.clear(); |
| 513 | 618 |
| 514 channel_id_sent_ = false; | 619 channel_id_sent_ = false; |
| 515 handshake_completed_ = false; | 620 handshake_completed_ = false; |
| 516 certificate_verified_ = false; | 621 certificate_verified_ = false; |
| 517 channel_id_request_.Cancel(); | 622 channel_id_request_.Cancel(); |
| 518 ssl_failure_state_ = SSL_FAILURE_NONE; | 623 ssl_failure_state_ = SSL_FAILURE_NONE; |
| 624 | |
| 625 private_key_.reset(); | |
| 626 signature_result_ = kNoPendingResult; | |
| 627 signature_.clear(); | |
| 519 } | 628 } |
| 520 | 629 |
| 521 bool SSLClientSocketOpenSSL::IsConnected() const { | 630 bool SSLClientSocketOpenSSL::IsConnected() const { |
| 522 // If the handshake has not yet completed. | 631 // If the handshake has not yet completed. |
| 523 if (!completed_connect_) | 632 if (!completed_connect_) |
| 524 return false; | 633 return false; |
| 525 // If an asynchronous operation is still pending. | 634 // If an asynchronous operation is still pending. |
| 526 if (user_read_buf_.get() || user_write_buf_.get()) | 635 if (user_read_buf_.get() || user_write_buf_.get()) |
| 527 return true; | 636 return true; |
| 528 | 637 |
| (...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 930 if (ssl_error == SSL_ERROR_WANT_CHANNEL_ID_LOOKUP) { | 1039 if (ssl_error == SSL_ERROR_WANT_CHANNEL_ID_LOOKUP) { |
| 931 // The server supports channel ID. Stop to look one up before returning to | 1040 // The server supports channel ID. Stop to look one up before returning to |
| 932 // the handshake. | 1041 // the handshake. |
| 933 GotoState(STATE_CHANNEL_ID_LOOKUP); | 1042 GotoState(STATE_CHANNEL_ID_LOOKUP); |
| 934 return OK; | 1043 return OK; |
| 935 } | 1044 } |
| 936 if (ssl_error == SSL_ERROR_WANT_X509_LOOKUP && | 1045 if (ssl_error == SSL_ERROR_WANT_X509_LOOKUP && |
| 937 !ssl_config_.send_client_cert) { | 1046 !ssl_config_.send_client_cert) { |
| 938 return ERR_SSL_CLIENT_AUTH_CERT_NEEDED; | 1047 return ERR_SSL_CLIENT_AUTH_CERT_NEEDED; |
| 939 } | 1048 } |
| 1049 if (ssl_error == SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) { | |
| 1050 DCHECK(private_key_); | |
| 1051 DCHECK_NE(kNoPendingResult, signature_result_); | |
| 1052 GotoState(STATE_HANDSHAKE); | |
| 1053 return ERR_IO_PENDING; | |
| 1054 } | |
| 940 | 1055 |
| 941 OpenSSLErrorInfo error_info; | 1056 OpenSSLErrorInfo error_info; |
| 942 net_error = MapOpenSSLErrorWithDetails(ssl_error, err_tracer, &error_info); | 1057 net_error = MapOpenSSLErrorWithDetails(ssl_error, err_tracer, &error_info); |
| 943 if (net_error == ERR_IO_PENDING) { | 1058 if (net_error == ERR_IO_PENDING) { |
| 944 // If not done, stay in this state | 1059 // If not done, stay in this state |
| 945 GotoState(STATE_HANDSHAKE); | 1060 GotoState(STATE_HANDSHAKE); |
| 946 return ERR_IO_PENDING; | 1061 return ERR_IO_PENDING; |
| 947 } | 1062 } |
| 948 | 1063 |
| 949 LOG(ERROR) << "handshake failed; returned " << rv << ", SSL error code " | 1064 LOG(ERROR) << "handshake failed; returned " << rv << ", SSL error code " |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1258 } | 1373 } |
| 1259 } | 1374 } |
| 1260 | 1375 |
| 1261 void SSLClientSocketOpenSSL::OnSendComplete(int result) { | 1376 void SSLClientSocketOpenSSL::OnSendComplete(int result) { |
| 1262 if (next_handshake_state_ == STATE_HANDSHAKE) { | 1377 if (next_handshake_state_ == STATE_HANDSHAKE) { |
| 1263 // In handshake phase. | 1378 // In handshake phase. |
| 1264 OnHandshakeIOComplete(result); | 1379 OnHandshakeIOComplete(result); |
| 1265 return; | 1380 return; |
| 1266 } | 1381 } |
| 1267 | 1382 |
| 1268 // OnSendComplete may need to call DoPayloadRead while the renegotiation | 1383 // OnSendComplete may need to run the Read state machine while a |
| 1269 // handshake is in progress. | 1384 // renegotiation handshake is in progress. |
| 1270 int rv_read = ERR_IO_PENDING; | 1385 RunReadWriteLoops(); |
| 1271 int rv_write = ERR_IO_PENDING; | |
| 1272 bool network_moved; | |
| 1273 do { | |
| 1274 if (user_read_buf_.get()) | |
| 1275 rv_read = DoPayloadRead(); | |
| 1276 if (user_write_buf_.get()) | |
| 1277 rv_write = DoPayloadWrite(); | |
| 1278 network_moved = DoTransportIO(); | |
| 1279 } while (rv_read == ERR_IO_PENDING && rv_write == ERR_IO_PENDING && | |
| 1280 (user_read_buf_.get() || user_write_buf_.get()) && network_moved); | |
| 1281 | |
| 1282 // Performing the Read callback may cause |this| to be deleted. If this | |
| 1283 // happens, the Write callback should not be invoked. Guard against this by | |
| 1284 // holding a WeakPtr to |this| and ensuring it's still valid. | |
| 1285 base::WeakPtr<SSLClientSocketOpenSSL> guard(weak_factory_.GetWeakPtr()); | |
| 1286 if (user_read_buf_.get() && rv_read != ERR_IO_PENDING) | |
| 1287 DoReadCallback(rv_read); | |
| 1288 | |
| 1289 if (!guard.get()) | |
| 1290 return; | |
| 1291 | |
| 1292 if (user_write_buf_.get() && rv_write != ERR_IO_PENDING) | |
| 1293 DoWriteCallback(rv_write); | |
| 1294 } | 1386 } |
| 1295 | 1387 |
| 1296 void SSLClientSocketOpenSSL::OnRecvComplete(int result) { | 1388 void SSLClientSocketOpenSSL::OnRecvComplete(int result) { |
| 1297 if (next_handshake_state_ == STATE_HANDSHAKE) { | 1389 if (next_handshake_state_ == STATE_HANDSHAKE) { |
| 1298 // In handshake phase. | 1390 // In handshake phase. |
| 1299 OnHandshakeIOComplete(result); | 1391 OnHandshakeIOComplete(result); |
| 1300 return; | 1392 return; |
| 1301 } | 1393 } |
| 1302 | 1394 |
| 1303 // Network layer received some data, check if client requested to read | 1395 // Network layer received some data, check if client requested to read |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1381 return rv; | 1473 return rv; |
| 1382 } | 1474 } |
| 1383 | 1475 |
| 1384 int SSLClientSocketOpenSSL::DoPayloadRead() { | 1476 int SSLClientSocketOpenSSL::DoPayloadRead() { |
| 1385 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 1477 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 1386 | 1478 |
| 1387 DCHECK_LT(0, user_read_buf_len_); | 1479 DCHECK_LT(0, user_read_buf_len_); |
| 1388 DCHECK(user_read_buf_.get()); | 1480 DCHECK(user_read_buf_.get()); |
| 1389 | 1481 |
| 1390 int rv; | 1482 int rv; |
| 1391 if (pending_read_error_ != kNoPendingReadResult) { | 1483 if (pending_read_error_ != kNoPendingResult) { |
| 1392 rv = pending_read_error_; | 1484 rv = pending_read_error_; |
| 1393 pending_read_error_ = kNoPendingReadResult; | 1485 pending_read_error_ = kNoPendingResult; |
| 1394 if (rv == 0) { | 1486 if (rv == 0) { |
| 1395 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, | 1487 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, |
| 1396 rv, user_read_buf_->data()); | 1488 rv, user_read_buf_->data()); |
| 1397 } else { | 1489 } else { |
| 1398 net_log_.AddEvent( | 1490 net_log_.AddEvent( |
| 1399 NetLog::TYPE_SSL_READ_ERROR, | 1491 NetLog::TYPE_SSL_READ_ERROR, |
| 1400 CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_, | 1492 CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_, |
| 1401 pending_read_error_info_)); | 1493 pending_read_error_info_)); |
| 1402 } | 1494 } |
| 1403 pending_read_ssl_error_ = SSL_ERROR_NONE; | 1495 pending_read_ssl_error_ = SSL_ERROR_NONE; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 1425 // | 1517 // |
| 1426 // TransportReadComplete converts the first to an ERR_CONNECTION_CLOSED | 1518 // TransportReadComplete converts the first to an ERR_CONNECTION_CLOSED |
| 1427 // error, so it does not occur. The second and third are distinguished by | 1519 // error, so it does not occur. The second and third are distinguished by |
| 1428 // SSL_ERROR_ZERO_RETURN. | 1520 // SSL_ERROR_ZERO_RETURN. |
| 1429 pending_read_ssl_error_ = SSL_get_error(ssl_, ssl_ret); | 1521 pending_read_ssl_error_ = SSL_get_error(ssl_, ssl_ret); |
| 1430 if (pending_read_ssl_error_ == SSL_ERROR_ZERO_RETURN) { | 1522 if (pending_read_ssl_error_ == SSL_ERROR_ZERO_RETURN) { |
| 1431 pending_read_error_ = 0; | 1523 pending_read_error_ = 0; |
| 1432 } else if (pending_read_ssl_error_ == SSL_ERROR_WANT_X509_LOOKUP && | 1524 } else if (pending_read_ssl_error_ == SSL_ERROR_WANT_X509_LOOKUP && |
| 1433 !ssl_config_.send_client_cert) { | 1525 !ssl_config_.send_client_cert) { |
| 1434 pending_read_error_ = ERR_SSL_CLIENT_AUTH_CERT_NEEDED; | 1526 pending_read_error_ = ERR_SSL_CLIENT_AUTH_CERT_NEEDED; |
| 1527 } else if (pending_read_ssl_error_ == | |
| 1528 SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) { | |
| 1529 DCHECK(private_key_); | |
| 1530 DCHECK_NE(kNoPendingResult, signature_result_); | |
| 1531 pending_read_error_ = ERR_IO_PENDING; | |
| 1435 } else { | 1532 } else { |
| 1436 pending_read_error_ = MapOpenSSLErrorWithDetails( | 1533 pending_read_error_ = MapOpenSSLErrorWithDetails( |
| 1437 pending_read_ssl_error_, err_tracer, &pending_read_error_info_); | 1534 pending_read_ssl_error_, err_tracer, &pending_read_error_info_); |
| 1438 } | 1535 } |
| 1439 | 1536 |
| 1440 // Many servers do not reliably send a close_notify alert when shutting down | 1537 // Many servers do not reliably send a close_notify alert when shutting down |
| 1441 // a connection, and instead terminate the TCP connection. This is reported | 1538 // a connection, and instead terminate the TCP connection. This is reported |
| 1442 // as ERR_CONNECTION_CLOSED. Because of this, map the unclean shutdown to a | 1539 // as ERR_CONNECTION_CLOSED. Because of this, map the unclean shutdown to a |
| 1443 // graceful EOF, instead of treating it as an error as it should be. | 1540 // graceful EOF, instead of treating it as an error as it should be. |
| 1444 if (pending_read_error_ == ERR_CONNECTION_CLOSED) | 1541 if (pending_read_error_ == ERR_CONNECTION_CLOSED) |
| 1445 pending_read_error_ = 0; | 1542 pending_read_error_ = 0; |
| 1446 } | 1543 } |
| 1447 | 1544 |
| 1448 if (total_bytes_read > 0) { | 1545 if (total_bytes_read > 0) { |
| 1449 // Return any bytes read to the caller. The error will be deferred to the | 1546 // Return any bytes read to the caller. The error will be deferred to the |
| 1450 // next call of DoPayloadRead. | 1547 // next call of DoPayloadRead. |
| 1451 rv = total_bytes_read; | 1548 rv = total_bytes_read; |
| 1452 | 1549 |
| 1453 // Do not treat insufficient data as an error to return in the next call to | 1550 // Do not treat insufficient data as an error to return in the next call to |
| 1454 // DoPayloadRead() - instead, let the call fall through to check SSL_read() | 1551 // DoPayloadRead() - instead, let the call fall through to check SSL_read() |
| 1455 // again. This is because DoTransportIO() may complete in between the next | 1552 // again. This is because DoTransportIO() may complete in between the next |
| 1456 // call to DoPayloadRead(), and thus it is important to check SSL_read() on | 1553 // call to DoPayloadRead(), and thus it is important to check SSL_read() on |
| 1457 // subsequent invocations to see if a complete record may now be read. | 1554 // subsequent invocations to see if a complete record may now be read. |
| 1458 if (pending_read_error_ == ERR_IO_PENDING) | 1555 if (pending_read_error_ == ERR_IO_PENDING) |
| 1459 pending_read_error_ = kNoPendingReadResult; | 1556 pending_read_error_ = kNoPendingResult; |
| 1460 } else { | 1557 } else { |
| 1461 // No bytes were returned. Return the pending read error immediately. | 1558 // No bytes were returned. Return the pending read error immediately. |
| 1462 DCHECK_NE(kNoPendingReadResult, pending_read_error_); | 1559 DCHECK_NE(kNoPendingResult, pending_read_error_); |
| 1463 rv = pending_read_error_; | 1560 rv = pending_read_error_; |
| 1464 pending_read_error_ = kNoPendingReadResult; | 1561 pending_read_error_ = kNoPendingResult; |
| 1465 } | 1562 } |
| 1466 | 1563 |
| 1467 if (rv >= 0) { | 1564 if (rv >= 0) { |
| 1468 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, rv, | 1565 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, rv, |
| 1469 user_read_buf_->data()); | 1566 user_read_buf_->data()); |
| 1470 } else if (rv != ERR_IO_PENDING) { | 1567 } else if (rv != ERR_IO_PENDING) { |
| 1471 net_log_.AddEvent( | 1568 net_log_.AddEvent( |
| 1472 NetLog::TYPE_SSL_READ_ERROR, | 1569 NetLog::TYPE_SSL_READ_ERROR, |
| 1473 CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_, | 1570 CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_, |
| 1474 pending_read_error_info_)); | 1571 pending_read_error_info_)); |
| 1475 pending_read_ssl_error_ = SSL_ERROR_NONE; | 1572 pending_read_ssl_error_ = SSL_ERROR_NONE; |
| 1476 pending_read_error_info_ = OpenSSLErrorInfo(); | 1573 pending_read_error_info_ = OpenSSLErrorInfo(); |
| 1477 } | 1574 } |
| 1478 return rv; | 1575 return rv; |
| 1479 } | 1576 } |
| 1480 | 1577 |
| 1481 int SSLClientSocketOpenSSL::DoPayloadWrite() { | 1578 int SSLClientSocketOpenSSL::DoPayloadWrite() { |
| 1482 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 1579 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 1483 int rv = SSL_write(ssl_, user_write_buf_->data(), user_write_buf_len_); | 1580 int rv = SSL_write(ssl_, user_write_buf_->data(), user_write_buf_len_); |
| 1484 | 1581 |
| 1485 if (rv >= 0) { | 1582 if (rv >= 0) { |
| 1486 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv, | 1583 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv, |
| 1487 user_write_buf_->data()); | 1584 user_write_buf_->data()); |
| 1488 return rv; | 1585 return rv; |
| 1489 } | 1586 } |
| 1490 | 1587 |
| 1491 int ssl_error = SSL_get_error(ssl_, rv); | 1588 int ssl_error = SSL_get_error(ssl_, rv); |
| 1589 if (ssl_error == SSL_ERROR_WANT_PRIVATE_KEY_OPERATION) | |
| 1590 return ERR_IO_PENDING; | |
| 1492 OpenSSLErrorInfo error_info; | 1591 OpenSSLErrorInfo error_info; |
| 1493 int net_error = MapOpenSSLErrorWithDetails(ssl_error, err_tracer, | 1592 int net_error = MapOpenSSLErrorWithDetails(ssl_error, err_tracer, |
| 1494 &error_info); | 1593 &error_info); |
| 1495 | 1594 |
| 1496 if (net_error != ERR_IO_PENDING) { | 1595 if (net_error != ERR_IO_PENDING) { |
| 1497 net_log_.AddEvent( | 1596 net_log_.AddEvent( |
| 1498 NetLog::TYPE_SSL_WRITE_ERROR, | 1597 NetLog::TYPE_SSL_WRITE_ERROR, |
| 1499 CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info)); | 1598 CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info)); |
| 1500 } | 1599 } |
| 1501 return net_error; | 1600 return net_error; |
| 1502 } | 1601 } |
| 1503 | 1602 |
| 1603 void SSLClientSocketOpenSSL::RunReadWriteLoops() { | |
| 1604 int rv_read = ERR_IO_PENDING; | |
| 1605 int rv_write = ERR_IO_PENDING; | |
| 1606 bool network_moved; | |
| 1607 do { | |
| 1608 if (user_read_buf_.get()) | |
| 1609 rv_read = DoPayloadRead(); | |
| 1610 if (user_write_buf_.get()) | |
| 1611 rv_write = DoPayloadWrite(); | |
| 1612 network_moved = DoTransportIO(); | |
| 1613 } while (rv_read == ERR_IO_PENDING && rv_write == ERR_IO_PENDING && | |
| 1614 (user_read_buf_.get() || user_write_buf_.get()) && network_moved); | |
| 1615 | |
| 1616 // Performing the Read callback may cause |this| to be deleted. If this | |
| 1617 // happens, the Write callback should not be invoked. Guard against this by | |
| 1618 // holding a WeakPtr to |this| and ensuring it's still valid. | |
| 1619 base::WeakPtr<SSLClientSocketOpenSSL> guard(weak_factory_.GetWeakPtr()); | |
| 1620 if (user_read_buf_.get() && rv_read != ERR_IO_PENDING) | |
| 1621 DoReadCallback(rv_read); | |
| 1622 | |
| 1623 if (!guard.get()) | |
| 1624 return; | |
| 1625 | |
| 1626 if (user_write_buf_.get() && rv_write != ERR_IO_PENDING) | |
| 1627 DoWriteCallback(rv_write); | |
| 1628 } | |
| 1629 | |
| 1504 int SSLClientSocketOpenSSL::BufferSend(void) { | 1630 int SSLClientSocketOpenSSL::BufferSend(void) { |
| 1505 if (transport_send_busy_) | 1631 if (transport_send_busy_) |
| 1506 return ERR_IO_PENDING; | 1632 return ERR_IO_PENDING; |
| 1507 | 1633 |
| 1508 size_t buffer_read_offset; | 1634 size_t buffer_read_offset; |
| 1509 uint8_t* read_buf; | 1635 uint8_t* read_buf; |
| 1510 size_t max_read; | 1636 size_t max_read; |
| 1511 int status = BIO_zero_copy_get_read_buf(transport_bio_, &read_buf, | 1637 int status = BIO_zero_copy_get_read_buf(transport_bio_, &read_buf, |
| 1512 &buffer_read_offset, &max_read); | 1638 &buffer_read_offset, &max_read); |
| 1513 DCHECK_EQ(status, 1); // Should never fail. | 1639 DCHECK_EQ(status, 1); // Should never fail. |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1677 } | 1803 } |
| 1678 | 1804 |
| 1679 ScopedX509Stack chain = OSCertHandlesToOpenSSL( | 1805 ScopedX509Stack chain = OSCertHandlesToOpenSSL( |
| 1680 ssl_config_.client_cert->GetIntermediateCertificates()); | 1806 ssl_config_.client_cert->GetIntermediateCertificates()); |
| 1681 if (!chain) { | 1807 if (!chain) { |
| 1682 LOG(WARNING) << "Failed to import intermediate certificates"; | 1808 LOG(WARNING) << "Failed to import intermediate certificates"; |
| 1683 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); | 1809 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_BAD_FORMAT); |
| 1684 return -1; | 1810 return -1; |
| 1685 } | 1811 } |
| 1686 | 1812 |
| 1687 // TODO(davidben): With Linux client auth support, this should be | 1813 if (!SSL_use_certificate(ssl_, leaf_x509.get()) || |
| 1688 // conditioned on OS_ANDROID and then, with https://crbug.com/394131, | 1814 !SSL_set1_chain(ssl_, chain.get())) { |
| 1689 // removed altogether. OpenSSLClientKeyStore is mostly an artifact of the | 1815 LOG(WARNING) << "Failed to set client certificate"; |
| 1690 // net/ client auth API lacking a private key handle. | 1816 return -1; |
| 1817 } | |
| 1818 | |
| 1691 #if defined(USE_OPENSSL_CERTS) | 1819 #if defined(USE_OPENSSL_CERTS) |
| 1692 crypto::ScopedEVP_PKEY privkey = | 1820 crypto::ScopedEVP_PKEY privkey = |
| 1693 OpenSSLClientKeyStore::GetInstance()->FetchClientCertPrivateKey( | 1821 OpenSSLClientKeyStore::GetInstance()->FetchClientCertPrivateKey( |
| 1694 ssl_config_.client_cert.get()); | 1822 ssl_config_.client_cert.get()); |
| 1695 #else // !defined(USE_OPENSSL_CERTS) | |
| 1696 crypto::ScopedEVP_PKEY privkey = | |
| 1697 FetchClientCertPrivateKey(ssl_config_.client_cert.get()); | |
| 1698 #endif // defined(USE_OPENSSL_CERTS) | |
| 1699 if (!privkey) { | 1823 if (!privkey) { |
| 1700 // Could not find the private key. Fail the handshake and surface an | 1824 // Could not find the private key. Fail the handshake and surface an |
| 1701 // appropriate error to the caller. | 1825 // appropriate error to the caller. |
| 1702 LOG(WARNING) << "Client cert found without private key"; | 1826 LOG(WARNING) << "Client cert found without private key"; |
| 1703 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); | 1827 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); |
| 1704 return -1; | 1828 return -1; |
| 1705 } | 1829 } |
| 1830 if (!SSL_use_PrivateKey(ssl_, privkey.get())) { | |
| 1831 LOG(WARNING) << "Failed to set private key"; | |
| 1832 return -1; | |
| 1833 } | |
| 1834 #else // !USE_OPENSSL_CERTS | |
| 1835 // TODO(davidben): Move Android to the SSLPrivateKey codepath and disable | |
| 1836 // client auth on NaCl altogether. | |
|
Ryan Sleevi
2015/06/23 15:27:28
Does this TODO really belong here, or on line 1820
davidben
2015/06/24 21:43:12
Done.
| |
| 1837 // | |
| 1838 // TODO(davidben): Lift this call up to the embedder so we can actually test | |
| 1839 // this code. https://crbug.com/394131 | |
| 1840 private_key_ = FetchClientCertPrivateKey( | |
| 1841 ssl_config_.client_cert.get(), | |
| 1842 g_platform_key_task_runner.Get().task_runner()); | |
| 1843 if (!private_key_) { | |
| 1844 // Could not find the private key. Fail the handshake and surface an | |
| 1845 // appropriate error to the caller. | |
| 1846 LOG(WARNING) << "Client cert found without private key"; | |
| 1847 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY); | |
| 1848 return -1; | |
| 1849 } | |
| 1706 | 1850 |
| 1707 if (!SSL_use_certificate(ssl_, leaf_x509.get()) || | 1851 SSL_set_private_key_method(ssl_, &SSLContext::kPrivateKeyMethod); |
| 1708 !SSL_use_PrivateKey(ssl_, privkey.get()) || | 1852 #endif // USE_OPENSSL_CERTS |
| 1709 !SSL_set1_chain(ssl_, chain.get())) { | |
| 1710 LOG(WARNING) << "Failed to set client certificate"; | |
| 1711 return -1; | |
| 1712 } | |
| 1713 | 1853 |
| 1714 int cert_count = 1 + sk_X509_num(chain.get()); | 1854 int cert_count = 1 + sk_X509_num(chain.get()); |
| 1715 net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_PROVIDED, | 1855 net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_PROVIDED, |
| 1716 NetLog::IntegerCallback("cert_count", cert_count)); | 1856 NetLog::IntegerCallback("cert_count", cert_count)); |
| 1717 return 1; | 1857 return 1; |
| 1718 } | 1858 } |
| 1719 #endif // defined(OS_IOS) | 1859 #endif // defined(OS_IOS) |
| 1720 | 1860 |
| 1721 // Send no client certificate. | 1861 // Send no client certificate. |
| 1722 net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_PROVIDED, | 1862 net_log_.AddEvent(NetLog::TYPE_SSL_CLIENT_CERT_PROVIDED, |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1924 return ssl_config_.renego_allowed_default; | 2064 return ssl_config_.renego_allowed_default; |
| 1925 | 2065 |
| 1926 NextProto next_proto = NextProtoFromString(npn_proto_); | 2066 NextProto next_proto = NextProtoFromString(npn_proto_); |
| 1927 for (NextProto allowed : ssl_config_.renego_allowed_for_protos) { | 2067 for (NextProto allowed : ssl_config_.renego_allowed_for_protos) { |
| 1928 if (next_proto == allowed) | 2068 if (next_proto == allowed) |
| 1929 return true; | 2069 return true; |
| 1930 } | 2070 } |
| 1931 return false; | 2071 return false; |
| 1932 } | 2072 } |
| 1933 | 2073 |
| 2074 int SSLClientSocketOpenSSL::PrivateKeyTypeCallback() { | |
| 2075 switch (private_key_->GetType()) { | |
| 2076 case SSLPrivateKey::Type::RSA: | |
| 2077 return EVP_PKEY_RSA; | |
| 2078 case SSLPrivateKey::Type::ECDSA: | |
| 2079 return EVP_PKEY_EC; | |
| 2080 } | |
| 2081 NOTREACHED(); | |
| 2082 return EVP_PKEY_NONE; | |
| 2083 } | |
| 2084 | |
| 2085 int SSLClientSocketOpenSSL::PrivateKeySupportsDigestCallback(const EVP_MD* md) { | |
| 2086 SSLPrivateKey::Hash hash; | |
| 2087 return EVP_MDToPrivateKeyHash(md, &hash) && private_key_->SupportsHash(hash); | |
| 2088 } | |
| 2089 | |
| 2090 size_t SSLClientSocketOpenSSL::PrivateKeyMaxSignatureLenCallback() { | |
| 2091 return private_key_->GetMaxSignatureLength(); | |
| 2092 } | |
| 2093 | |
| 2094 ssl_private_key_result_t SSLClientSocketOpenSSL::PrivateKeySignCallback( | |
| 2095 uint8_t* out, | |
| 2096 size_t* out_len, | |
| 2097 size_t max_out, | |
| 2098 const EVP_MD* md, | |
| 2099 const uint8_t* in, | |
| 2100 size_t in_len) { | |
| 2101 DCHECK_EQ(kNoPendingResult, signature_result_); | |
| 2102 DCHECK(signature_.empty()); | |
| 2103 DCHECK(private_key_); | |
| 2104 | |
| 2105 net_log_.BeginEvent(NetLog::TYPE_SSL_PRIVATE_KEY_OPERATION); | |
| 2106 | |
| 2107 SSLPrivateKey::Hash hash; | |
| 2108 if (!EVP_MDToPrivateKeyHash(md, &hash)) { | |
| 2109 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED); | |
| 2110 return ssl_private_key_failure; | |
| 2111 } | |
| 2112 | |
| 2113 signature_result_ = ERR_IO_PENDING; | |
| 2114 private_key_->SignDigest( | |
| 2115 hash, base::StringPiece(reinterpret_cast<const char*>(in), in_len), | |
| 2116 base::Bind(&SSLClientSocketOpenSSL::OnPrivateKeySignComplete, | |
| 2117 weak_factory_.GetWeakPtr())); | |
| 2118 return ssl_private_key_retry; | |
| 2119 } | |
| 2120 | |
| 2121 ssl_private_key_result_t SSLClientSocketOpenSSL::PrivateKeySignCompleteCallback( | |
| 2122 uint8_t* out, | |
| 2123 size_t* out_len, | |
| 2124 size_t max_out) { | |
| 2125 DCHECK_NE(kNoPendingResult, signature_result_); | |
| 2126 DCHECK(private_key_); | |
| 2127 | |
| 2128 if (signature_result_ == ERR_IO_PENDING) | |
| 2129 return ssl_private_key_retry; | |
| 2130 if (signature_result_ != OK) { | |
| 2131 OpenSSLPutNetError(FROM_HERE, signature_result_); | |
| 2132 return ssl_private_key_failure; | |
| 2133 } | |
| 2134 if (signature_.size() > max_out) { | |
| 2135 LOG(ERROR) << "Buffer too small"; | |
|
Ryan Sleevi
2015/06/23 15:27:28
Is the message necessary? I generally grump about
davidben
2015/06/24 21:43:12
Removed.
| |
| 2136 OpenSSLPutNetError(FROM_HERE, ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED); | |
| 2137 return ssl_private_key_failure; | |
| 2138 } | |
| 2139 memcpy(out, vector_as_array(&signature_), signature_.size()); | |
| 2140 *out_len = signature_.size(); | |
| 2141 signature_.clear(); | |
| 2142 return ssl_private_key_success; | |
| 2143 } | |
| 2144 | |
| 2145 void SSLClientSocketOpenSSL::OnPrivateKeySignComplete( | |
| 2146 Error error, | |
| 2147 const std::vector<uint8_t>& signature) { | |
| 2148 DCHECK_EQ(ERR_IO_PENDING, signature_result_); | |
| 2149 DCHECK(signature_.empty()); | |
| 2150 DCHECK(private_key_); | |
| 2151 | |
| 2152 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_PRIVATE_KEY_OPERATION, | |
| 2153 error); | |
| 2154 | |
| 2155 signature_result_ = error; | |
| 2156 if (signature_result_ == OK) | |
| 2157 signature_ = signature; | |
| 2158 | |
| 2159 if (next_handshake_state_ == STATE_HANDSHAKE) { | |
| 2160 OnHandshakeIOComplete(signature_result_); | |
| 2161 return; | |
| 2162 } | |
| 2163 | |
| 2164 // Either Read or Write may be blocked on an asynchronous private | |
| 2165 // key operation during a renegotiation. | |
| 2166 RunReadWriteLoops(); | |
| 2167 } | |
| 2168 | |
| 1934 } // namespace net | 2169 } // namespace net |
| OLD | NEW |