Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(267)

Side by Side Diff: net/socket/ssl_client_socket_openssl.cc

Issue 353713005: Implements new, more robust design for communicating between SSLConnectJobs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changed location of enable_connect_job_waiting_ and added documentation. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <openssl/err.h> 10 #include <openssl/err.h>
11 #include <openssl/opensslv.h> 11 #include <openssl/opensslv.h>
12 #include <openssl/ssl.h> 12 #include <openssl/ssl.h>
13 13
14 #include "base/bind.h" 14 #include "base/bind.h"
15 #include "base/callback_helpers.h" 15 #include "base/callback_helpers.h"
16 #include "base/memory/singleton.h" 16 #include "base/memory/singleton.h"
17 #include "base/metrics/histogram.h" 17 #include "base/metrics/histogram.h"
18 #include "base/synchronization/lock.h" 18 #include "base/synchronization/lock.h"
19 #include "crypto/ec_private_key.h" 19 #include "crypto/ec_private_key.h"
20 #include "crypto/openssl_util.h" 20 #include "crypto/openssl_util.h"
21 #include "net/base/net_errors.h" 21 #include "net/base/net_errors.h"
22 #include "net/cert/cert_verifier.h" 22 #include "net/cert/cert_verifier.h"
23 #include "net/cert/single_request_cert_verifier.h" 23 #include "net/cert/single_request_cert_verifier.h"
24 #include "net/cert/x509_certificate_net_log_param.h" 24 #include "net/cert/x509_certificate_net_log_param.h"
25 #include "net/socket/openssl_ssl_util.h" 25 #include "net/socket/openssl_ssl_util.h"
26 #include "net/socket/ssl_client_socket_pool.h"
26 #include "net/socket/ssl_error_params.h" 27 #include "net/socket/ssl_error_params.h"
27 #include "net/socket/ssl_session_cache_openssl.h" 28 #include "net/socket/ssl_session_cache_openssl.h"
28 #include "net/ssl/openssl_client_key_store.h" 29 #include "net/ssl/openssl_client_key_store.h"
29 #include "net/ssl/ssl_cert_request_info.h" 30 #include "net/ssl/ssl_cert_request_info.h"
30 #include "net/ssl/ssl_connection_status_flags.h" 31 #include "net/ssl/ssl_connection_status_flags.h"
31 #include "net/ssl/ssl_info.h" 32 #include "net/ssl/ssl_info.h"
32 33
33 namespace net { 34 namespace net {
34 35
35 namespace { 36 namespace {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 return SSL_CONNECTION_VERSION_TLS1; 81 return SSL_CONNECTION_VERSION_TLS1;
81 case 0x0302: 82 case 0x0302:
82 return SSL_CONNECTION_VERSION_TLS1_1; 83 return SSL_CONNECTION_VERSION_TLS1_1;
83 case 0x0303: 84 case 0x0303:
84 return SSL_CONNECTION_VERSION_TLS1_2; 85 return SSL_CONNECTION_VERSION_TLS1_2;
85 default: 86 default:
86 return SSL_CONNECTION_VERSION_UNKNOWN; 87 return SSL_CONNECTION_VERSION_UNKNOWN;
87 } 88 }
88 } 89 }
89 90
90 // Compute a unique key string for the SSL session cache. |socket| is an
91 // input socket object. Return a string.
92 std::string GetSocketSessionCacheKey(const SSLClientSocketOpenSSL& socket) {
93 std::string result = socket.host_and_port().ToString();
94 result.append("/");
95 result.append(socket.ssl_session_cache_shard());
96 return result;
97 }
98 91
99 } // namespace 92 } // namespace
100 93
101 class SSLClientSocketOpenSSL::SSLContext { 94 class SSLClientSocketOpenSSL::SSLContext {
102 public: 95 public:
103 static SSLContext* GetInstance() { return Singleton<SSLContext>::get(); } 96 static SSLContext* GetInstance() { return Singleton<SSLContext>::get(); }
104 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } 97 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); }
105 SSLSessionCacheOpenSSL* session_cache() { return &session_cache_; } 98 SSLSessionCacheOpenSSL* session_cache() { return &session_cache_; }
106 99
107 SSLClientSocketOpenSSL* GetClientSocketFromSSL(const SSL* ssl) { 100 SSLClientSocketOpenSSL* GetClientSocketFromSSL(const SSL* ssl) {
(...skipping 24 matching lines...) Expand all
132 // TODO(kristianm): Only select this if ssl_config_.next_proto is not empty. 125 // TODO(kristianm): Only select this if ssl_config_.next_proto is not empty.
133 // It would be better if the callback were not a global setting, 126 // It would be better if the callback were not a global setting,
134 // but that is an OpenSSL issue. 127 // but that is an OpenSSL issue.
135 SSL_CTX_set_next_proto_select_cb(ssl_ctx_.get(), SelectNextProtoCallback, 128 SSL_CTX_set_next_proto_select_cb(ssl_ctx_.get(), SelectNextProtoCallback,
136 NULL); 129 NULL);
137 } 130 }
138 131
139 static std::string GetSessionCacheKey(const SSL* ssl) { 132 static std::string GetSessionCacheKey(const SSL* ssl) {
140 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); 133 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl);
141 DCHECK(socket); 134 DCHECK(socket);
142 return GetSocketSessionCacheKey(*socket); 135 return socket->GetSessionCacheKey();
143 } 136 }
144 137
145 static SSLSessionCacheOpenSSL::Config kDefaultSessionCacheConfig; 138 static SSLSessionCacheOpenSSL::Config kDefaultSessionCacheConfig;
146 139
147 static int ClientCertCallback(SSL* ssl, X509** x509, EVP_PKEY** pkey) { 140 static int ClientCertCallback(SSL* ssl, X509** x509, EVP_PKEY** pkey) {
148 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); 141 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl);
149 CHECK(socket); 142 CHECK(socket);
150 return socket->ClientCertRequestCallback(ssl, x509, pkey); 143 return socket->ClientCertRequestCallback(ssl, x509, pkey);
151 } 144 }
152 145
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 } 326 }
334 327
335 SSLClientSocketOpenSSL::SSLClientSocketOpenSSL( 328 SSLClientSocketOpenSSL::SSLClientSocketOpenSSL(
336 scoped_ptr<ClientSocketHandle> transport_socket, 329 scoped_ptr<ClientSocketHandle> transport_socket,
337 const HostPortPair& host_and_port, 330 const HostPortPair& host_and_port,
338 const SSLConfig& ssl_config, 331 const SSLConfig& ssl_config,
339 const SSLClientSocketContext& context) 332 const SSLClientSocketContext& context)
340 : transport_send_busy_(false), 333 : transport_send_busy_(false),
341 transport_recv_busy_(false), 334 transport_recv_busy_(false),
342 transport_recv_eof_(false), 335 transport_recv_eof_(false),
336 has_read_(false),
337 has_written_(false),
wtc 2014/07/11 00:48:54 Initialize has_written_ to 0 because it is of the
mshelley 2014/07/11 23:26:26 Done.
343 weak_factory_(this), 338 weak_factory_(this),
344 pending_read_error_(kNoPendingReadResult), 339 pending_read_error_(kNoPendingReadResult),
345 transport_write_error_(OK), 340 transport_write_error_(OK),
346 server_cert_chain_(new PeerCertificateChain(NULL)), 341 server_cert_chain_(new PeerCertificateChain(NULL)),
347 completed_handshake_(false), 342 completed_handshake_(false),
348 was_ever_used_(false), 343 was_ever_used_(false),
349 client_auth_cert_needed_(false), 344 client_auth_cert_needed_(false),
350 cert_verifier_(context.cert_verifier), 345 cert_verifier_(context.cert_verifier),
351 server_bound_cert_service_(context.server_bound_cert_service), 346 server_bound_cert_service_(context.server_bound_cert_service),
347 is_leader_(false),
352 ssl_(NULL), 348 ssl_(NULL),
353 transport_bio_(NULL), 349 transport_bio_(NULL),
354 transport_(transport_socket.Pass()), 350 transport_(transport_socket.Pass()),
355 host_and_port_(host_and_port), 351 host_and_port_(host_and_port),
356 ssl_config_(ssl_config), 352 ssl_config_(ssl_config),
357 ssl_session_cache_shard_(context.ssl_session_cache_shard), 353 ssl_session_cache_shard_(context.ssl_session_cache_shard),
358 trying_cached_session_(false), 354 trying_cached_session_(false),
359 next_handshake_state_(STATE_NONE), 355 next_handshake_state_(STATE_NONE),
360 npn_status_(kNextProtoUnsupported), 356 npn_status_(kNextProtoUnsupported),
361 channel_id_request_return_value_(ERR_UNEXPECTED), 357 channel_id_request_return_value_(ERR_UNEXPECTED),
362 channel_id_xtn_negotiated_(false), 358 channel_id_xtn_negotiated_(false),
363 net_log_(transport_->socket()->NetLog()) {} 359 net_log_(transport_->socket()->NetLog()) {
360 }
364 361
365 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { 362 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() {
363 SSLContext* context = SSLContext::GetInstance();
364 context->session_cache()->RemoveFromSSLToCallbackMap(ssl_);
wtc 2014/07/11 00:48:54 It may be better to do this in the Disconnect() me
mshelley 2014/07/11 23:26:26 Done.
366 Disconnect(); 365 Disconnect();
367 } 366 }
368 367
368 // Compute a unique key string for the SSL session cache.
369 // Return a string.
wtc 2014/07/11 00:48:54 Move this comment to the .h file. Delete "Return
mshelley 2014/07/11 23:26:26 Done.
370 std::string SSLClientSocketOpenSSL::GetSessionCacheKey() const {
371 return FormatSessionCacheKey(host_and_port_.ToString(),
372 ssl_session_cache_shard_);
373 }
374
375 bool SSLClientSocketOpenSSL::InSessionCache() const {
376 SSLContext* context = SSLContext::GetInstance();
377 std::string cache_key = GetSessionCacheKey();
378 return context->session_cache()->SSLSessionIsInCache(cache_key);
379 }
380
381 void SSLClientSocketOpenSSL::WatchSessionForCompletion(
382 const base::Closure& callback) {
383 SSLContext* context = SSLContext::GetInstance();
384 context->session_cache()->RegisterSessionAddedCallback(ssl_, callback);
385 }
386
387 void SSLClientSocketOpenSSL::SetSocketFailureCallback(
388 const base::Closure& callback) {
389 error_callback_ = callback;
390 }
391
392 void SSLClientSocketOpenSSL::SetIsLeader() {
393 is_leader_ = true;
394 }
395
396 void SSLClientSocketOpenSSL::OnSocketFailure() {
397 if (is_leader_) {
398 error_callback_.Run();
399 error_callback_ = base::Closure();
400 is_leader_ = false;
401 }
wtc 2014/07/11 00:48:54 Change this to: if (!error_callback_.is_null())
mshelley 2014/07/11 23:26:26 Done.
402 }
403
369 void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( 404 void SSLClientSocketOpenSSL::GetSSLCertRequestInfo(
370 SSLCertRequestInfo* cert_request_info) { 405 SSLCertRequestInfo* cert_request_info) {
371 cert_request_info->host_and_port = host_and_port_; 406 cert_request_info->host_and_port = host_and_port_;
372 cert_request_info->cert_authorities = cert_authorities_; 407 cert_request_info->cert_authorities = cert_authorities_;
373 cert_request_info->cert_key_types = cert_key_types_; 408 cert_request_info->cert_key_types = cert_key_types_;
374 } 409 }
375 410
376 SSLClientSocket::NextProtoStatus SSLClientSocketOpenSSL::GetNextProto( 411 SSLClientSocket::NextProtoStatus SSLClientSocketOpenSSL::GetNextProto(
377 std::string* proto, std::string* server_protos) { 412 std::string* proto, std::string* server_protos) {
378 *proto = npn_proto_; 413 *proto = npn_proto_;
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
651 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); 686 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
652 687
653 ssl_ = SSL_new(context->ssl_ctx()); 688 ssl_ = SSL_new(context->ssl_ctx());
654 if (!ssl_ || !context->SetClientSocketForSSL(ssl_, this)) 689 if (!ssl_ || !context->SetClientSocketForSSL(ssl_, this))
655 return ERR_UNEXPECTED; 690 return ERR_UNEXPECTED;
656 691
657 if (!SSL_set_tlsext_host_name(ssl_, host_and_port_.host().c_str())) 692 if (!SSL_set_tlsext_host_name(ssl_, host_and_port_.host().c_str()))
658 return ERR_UNEXPECTED; 693 return ERR_UNEXPECTED;
659 694
660 trying_cached_session_ = context->session_cache()->SetSSLSessionWithKey( 695 trying_cached_session_ = context->session_cache()->SetSSLSessionWithKey(
661 ssl_, GetSocketSessionCacheKey(*this)); 696 ssl_, GetSessionCacheKey());
662 697
663 BIO* ssl_bio = NULL; 698 BIO* ssl_bio = NULL;
664 // 0 => use default buffer sizes. 699 // 0 => use default buffer sizes.
665 if (!BIO_new_bio_pair(&ssl_bio, 0, &transport_bio_, 0)) 700 if (!BIO_new_bio_pair(&ssl_bio, 0, &transport_bio_, 0))
666 return ERR_UNEXPECTED; 701 return ERR_UNEXPECTED;
667 DCHECK(ssl_bio); 702 DCHECK(ssl_bio);
668 DCHECK(transport_bio_); 703 DCHECK(transport_bio_);
669 704
670 SSL_set_bio(ssl_, ssl_bio, ssl_bio); 705 SSL_set_bio(ssl_, ssl_bio, ssl_bio);
671 706
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
786 network_moved = true; 821 network_moved = true;
787 } while (rv > 0); 822 } while (rv > 0);
788 if (!transport_recv_eof_ && BufferRecv() != ERR_IO_PENDING) 823 if (!transport_recv_eof_ && BufferRecv() != ERR_IO_PENDING)
789 network_moved = true; 824 network_moved = true;
790 return network_moved; 825 return network_moved;
791 } 826 }
792 827
793 int SSLClientSocketOpenSSL::DoHandshake() { 828 int SSLClientSocketOpenSSL::DoHandshake() {
794 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); 829 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
795 int net_error = OK; 830 int net_error = OK;
796 int rv = SSL_do_handshake(ssl_); 831 int rv = SSL_do_handshake(ssl_);
wtc 2014/07/11 00:48:54 IMPORTANT: if rv <= 0, we should also call OnSocke
mshelley 2014/07/11 23:26:26 Done.
797 832
798 if (client_auth_cert_needed_) { 833 if (client_auth_cert_needed_) {
799 net_error = ERR_SSL_CLIENT_AUTH_CERT_NEEDED; 834 net_error = ERR_SSL_CLIENT_AUTH_CERT_NEEDED;
800 // If the handshake already succeeded (because the server requests but 835 // If the handshake already succeeded (because the server requests but
801 // doesn't require a client cert), we need to invalidate the SSL session 836 // doesn't require a client cert), we need to invalidate the SSL session
802 // so that we won't try to resume the non-client-authenticated session in 837 // so that we won't try to resume the non-client-authenticated session in
803 // the next handshake. This will cause the server to ask for a client 838 // the next handshake. This will cause the server to ask for a client
804 // cert again. 839 // cert again.
805 if (rv == 1) { 840 if (rv == 1) {
806 // Remove from session cache but don't clear this connection. 841 // Remove from session cache but don't clear this connection.
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
1057 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, 1092 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED,
1058 rv, user_read_buf_->data()); 1093 rv, user_read_buf_->data());
1059 } 1094 }
1060 return rv; 1095 return rv;
1061 } 1096 }
1062 1097
1063 int total_bytes_read = 0; 1098 int total_bytes_read = 0;
1064 do { 1099 do {
1065 rv = SSL_read(ssl_, user_read_buf_->data() + total_bytes_read, 1100 rv = SSL_read(ssl_, user_read_buf_->data() + total_bytes_read,
1066 user_read_buf_len_ - total_bytes_read); 1101 user_read_buf_len_ - total_bytes_read);
1102 // Failure of the first read attempt indicates a failed false start
wtc 2014/07/11 00:48:54 IMPORTANT: understanding this code requires intima
mshelley 2014/07/11 23:26:26 Done.
1103 // connection.
1104 if (!has_read_ && rv != OK &&
wtc 2014/07/11 00:48:54 IMPORTANT: SSL_read is an OpenSSL function, so its
mshelley 2014/07/11 23:26:27 Done.
1105 SSLClientSocketPool::GetEnableConnectJobWaiting())
wtc 2014/07/11 00:48:54 Delete the SSLClientSocketPool::GetEnableConnectJo
mshelley 2014/07/11 23:26:26 Done.
1106 OnSocketFailure();
1107 has_read_ = true;
1067 if (rv > 0) 1108 if (rv > 0)
1068 total_bytes_read += rv; 1109 total_bytes_read += rv;
1069 } while (total_bytes_read < user_read_buf_len_ && rv > 0); 1110 } while (total_bytes_read < user_read_buf_len_ && rv > 0);
1070 1111
1071 if (total_bytes_read == user_read_buf_len_) { 1112 if (total_bytes_read == user_read_buf_len_) {
1072 rv = total_bytes_read; 1113 rv = total_bytes_read;
1073 } else { 1114 } else {
1074 // Otherwise, an error occurred (rv <= 0). The error needs to be handled 1115 // Otherwise, an error occurred (rv <= 0). The error needs to be handled
1075 // immediately, while the OpenSSL errors are still available in 1116 // immediately, while the OpenSSL errors are still available in
1076 // thread-local storage. However, the handled/remapped error code should 1117 // thread-local storage. However, the handled/remapped error code should
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1109 if (rv >= 0) { 1150 if (rv >= 0) {
1110 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, rv, 1151 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, rv,
1111 user_read_buf_->data()); 1152 user_read_buf_->data());
1112 } 1153 }
1113 return rv; 1154 return rv;
1114 } 1155 }
1115 1156
1116 int SSLClientSocketOpenSSL::DoPayloadWrite() { 1157 int SSLClientSocketOpenSSL::DoPayloadWrite() {
1117 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); 1158 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
1118 int rv = SSL_write(ssl_, user_write_buf_->data(), user_write_buf_len_); 1159 int rv = SSL_write(ssl_, user_write_buf_->data(), user_write_buf_len_);
1119 1160 // Failure of the second write attempt indicates a failed false start
1161 // connection.
1162 if (has_written_ == 1 && rv != OK &&
wtc 2014/07/11 00:48:54 1. Change has_written_ to has_written_ <= 1. 2. C
mshelley 2014/07/11 23:26:27 Done.
1163 SSLClientSocketPool::GetEnableConnectJobWaiting()) {
1164 OnSocketFailure();
1165 }
1166 has_written_++;
wtc 2014/07/11 00:48:54 Nit: in theory this may overflow.
mshelley 2014/07/11 23:26:26 Done.
1120 if (rv >= 0) { 1167 if (rv >= 0) {
1121 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv, 1168 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv,
1122 user_write_buf_->data()); 1169 user_write_buf_->data());
1123 return rv; 1170 return rv;
1124 } 1171 }
1125 1172
1126 int err = SSL_get_error(ssl_, rv); 1173 int err = SSL_get_error(ssl_, rv);
1127 return MapOpenSSLError(err, err_tracer); 1174 return MapOpenSSLError(err, err_tracer);
1128 } 1175 }
1129 1176
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after
1430 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; 1477 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_;
1431 return SSL_TLSEXT_ERR_OK; 1478 return SSL_TLSEXT_ERR_OK;
1432 } 1479 }
1433 1480
1434 scoped_refptr<X509Certificate> 1481 scoped_refptr<X509Certificate>
1435 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { 1482 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const {
1436 return server_cert_; 1483 return server_cert_;
1437 } 1484 }
1438 1485
1439 } // namespace net 1486 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698