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 <openssl/err.h> | 10 #include <openssl/err.h> |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 return SSL_CONNECTION_VERSION_TLS1; | 80 return SSL_CONNECTION_VERSION_TLS1; |
81 case 0x0302: | 81 case 0x0302: |
82 return SSL_CONNECTION_VERSION_TLS1_1; | 82 return SSL_CONNECTION_VERSION_TLS1_1; |
83 case 0x0303: | 83 case 0x0303: |
84 return SSL_CONNECTION_VERSION_TLS1_2; | 84 return SSL_CONNECTION_VERSION_TLS1_2; |
85 default: | 85 default: |
86 return SSL_CONNECTION_VERSION_UNKNOWN; | 86 return SSL_CONNECTION_VERSION_UNKNOWN; |
87 } | 87 } |
88 } | 88 } |
89 | 89 |
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 | 90 |
mmenke
2014/07/17 19:21:32
nit: Remove extra blank line.
mshelley
2014/07/17 21:12:48
Done.
| |
99 } // namespace | 91 } // namespace |
100 | 92 |
101 class SSLClientSocketOpenSSL::SSLContext { | 93 class SSLClientSocketOpenSSL::SSLContext { |
102 public: | 94 public: |
103 static SSLContext* GetInstance() { return Singleton<SSLContext>::get(); } | 95 static SSLContext* GetInstance() { return Singleton<SSLContext>::get(); } |
104 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } | 96 SSL_CTX* ssl_ctx() { return ssl_ctx_.get(); } |
105 SSLSessionCacheOpenSSL* session_cache() { return &session_cache_; } | 97 SSLSessionCacheOpenSSL* session_cache() { return &session_cache_; } |
106 | 98 |
107 SSLClientSocketOpenSSL* GetClientSocketFromSSL(const SSL* ssl) { | 99 SSLClientSocketOpenSSL* GetClientSocketFromSSL(const SSL* ssl) { |
108 DCHECK(ssl); | 100 DCHECK(ssl); |
(...skipping 23 matching lines...) Expand all Loading... | |
132 // TODO(kristianm): Only select this if ssl_config_.next_proto is not empty. | 124 // 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, | 125 // It would be better if the callback were not a global setting, |
134 // but that is an OpenSSL issue. | 126 // but that is an OpenSSL issue. |
135 SSL_CTX_set_next_proto_select_cb(ssl_ctx_.get(), SelectNextProtoCallback, | 127 SSL_CTX_set_next_proto_select_cb(ssl_ctx_.get(), SelectNextProtoCallback, |
136 NULL); | 128 NULL); |
137 } | 129 } |
138 | 130 |
139 static std::string GetSessionCacheKey(const SSL* ssl) { | 131 static std::string GetSessionCacheKey(const SSL* ssl) { |
140 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | 132 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
141 DCHECK(socket); | 133 DCHECK(socket); |
142 return GetSocketSessionCacheKey(*socket); | 134 return socket->GetSessionCacheKey(); |
143 } | 135 } |
144 | 136 |
145 static SSLSessionCacheOpenSSL::Config kDefaultSessionCacheConfig; | 137 static SSLSessionCacheOpenSSL::Config kDefaultSessionCacheConfig; |
146 | 138 |
147 static int ClientCertCallback(SSL* ssl, X509** x509, EVP_PKEY** pkey) { | 139 static int ClientCertCallback(SSL* ssl, X509** x509, EVP_PKEY** pkey) { |
148 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); | 140 SSLClientSocketOpenSSL* socket = GetInstance()->GetClientSocketFromSSL(ssl); |
149 CHECK(socket); | 141 CHECK(socket); |
150 return socket->ClientCertRequestCallback(ssl, x509, pkey); | 142 return socket->ClientCertRequestCallback(ssl, x509, pkey); |
151 } | 143 } |
152 | 144 |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
353 transport_bio_(NULL), | 345 transport_bio_(NULL), |
354 transport_(transport_socket.Pass()), | 346 transport_(transport_socket.Pass()), |
355 host_and_port_(host_and_port), | 347 host_and_port_(host_and_port), |
356 ssl_config_(ssl_config), | 348 ssl_config_(ssl_config), |
357 ssl_session_cache_shard_(context.ssl_session_cache_shard), | 349 ssl_session_cache_shard_(context.ssl_session_cache_shard), |
358 trying_cached_session_(false), | 350 trying_cached_session_(false), |
359 next_handshake_state_(STATE_NONE), | 351 next_handshake_state_(STATE_NONE), |
360 npn_status_(kNextProtoUnsupported), | 352 npn_status_(kNextProtoUnsupported), |
361 channel_id_request_return_value_(ERR_UNEXPECTED), | 353 channel_id_request_return_value_(ERR_UNEXPECTED), |
362 channel_id_xtn_negotiated_(false), | 354 channel_id_xtn_negotiated_(false), |
363 net_log_(transport_->socket()->NetLog()) {} | 355 net_log_(transport_->socket()->NetLog()) { |
356 } | |
364 | 357 |
365 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { | 358 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { |
366 Disconnect(); | 359 Disconnect(); |
367 } | 360 } |
368 | 361 |
362 bool SSLClientSocketOpenSSL::InSessionCache() const { | |
363 SSLContext* context = SSLContext::GetInstance(); | |
364 std::string cache_key = GetSessionCacheKey(); | |
365 return context->session_cache()->SSLSessionIsInCache(cache_key); | |
366 } | |
367 | |
368 void SSLClientSocketOpenSSL::SetHandshakeSuccessCallback( | |
369 const base::Closure& callback) { | |
370 success_callback_ = callback; | |
371 SSLContext* context = SSLContext::GetInstance(); | |
372 context->session_cache()->SetSessionAddedCallback( | |
373 ssl_, | |
374 base::Bind(&SSLClientSocketOpenSSL::OnHandshakeSuccess, | |
375 base::Unretained(this))); | |
376 } | |
377 | |
378 void SSLClientSocketOpenSSL::SetHandshakeFailureCallback( | |
379 const base::Closure& callback) { | |
380 error_callback_ = callback; | |
381 } | |
382 | |
369 void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( | 383 void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( |
370 SSLCertRequestInfo* cert_request_info) { | 384 SSLCertRequestInfo* cert_request_info) { |
371 cert_request_info->host_and_port = host_and_port_; | 385 cert_request_info->host_and_port = host_and_port_; |
372 cert_request_info->cert_authorities = cert_authorities_; | 386 cert_request_info->cert_authorities = cert_authorities_; |
373 cert_request_info->cert_key_types = cert_key_types_; | 387 cert_request_info->cert_key_types = cert_key_types_; |
374 } | 388 } |
375 | 389 |
376 SSLClientSocket::NextProtoStatus SSLClientSocketOpenSSL::GetNextProto( | 390 SSLClientSocket::NextProtoStatus SSLClientSocketOpenSSL::GetNextProto( |
377 std::string* proto, std::string* server_protos) { | 391 std::string* proto, std::string* server_protos) { |
378 *proto = npn_proto_; | 392 *proto = npn_proto_; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
425 SSL_set_connect_state(ssl_); | 439 SSL_set_connect_state(ssl_); |
426 | 440 |
427 GotoState(STATE_HANDSHAKE); | 441 GotoState(STATE_HANDSHAKE); |
428 rv = DoHandshakeLoop(OK); | 442 rv = DoHandshakeLoop(OK); |
429 if (rv == ERR_IO_PENDING) { | 443 if (rv == ERR_IO_PENDING) { |
430 user_connect_callback_ = callback; | 444 user_connect_callback_ = callback; |
431 } else { | 445 } else { |
432 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); | 446 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); |
433 } | 447 } |
434 | 448 |
449 if (rv < OK) | |
450 OnHandshakeFailure(); | |
451 | |
435 return rv > OK ? OK : rv; | 452 return rv > OK ? OK : rv; |
436 } | 453 } |
437 | 454 |
438 void SSLClientSocketOpenSSL::Disconnect() { | 455 void SSLClientSocketOpenSSL::Disconnect() { |
456 OnHandshakeFailure(); | |
439 if (ssl_) { | 457 if (ssl_) { |
458 SSLContext* context = SSLContext::GetInstance(); | |
459 context->session_cache()->RemoveSessionAddedCallback(ssl_); | |
440 // Calling SSL_shutdown prevents the session from being marked as | 460 // Calling SSL_shutdown prevents the session from being marked as |
441 // unresumable. | 461 // unresumable. |
442 SSL_shutdown(ssl_); | 462 SSL_shutdown(ssl_); |
443 SSL_free(ssl_); | 463 SSL_free(ssl_); |
444 ssl_ = NULL; | 464 ssl_ = NULL; |
445 } | 465 } |
446 if (transport_bio_) { | 466 if (transport_bio_) { |
447 BIO_free_all(transport_bio_); | 467 BIO_free_all(transport_bio_); |
448 transport_bio_ = NULL; | 468 transport_bio_ = NULL; |
449 } | 469 } |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
651 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 671 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
652 | 672 |
653 ssl_ = SSL_new(context->ssl_ctx()); | 673 ssl_ = SSL_new(context->ssl_ctx()); |
654 if (!ssl_ || !context->SetClientSocketForSSL(ssl_, this)) | 674 if (!ssl_ || !context->SetClientSocketForSSL(ssl_, this)) |
655 return ERR_UNEXPECTED; | 675 return ERR_UNEXPECTED; |
656 | 676 |
657 if (!SSL_set_tlsext_host_name(ssl_, host_and_port_.host().c_str())) | 677 if (!SSL_set_tlsext_host_name(ssl_, host_and_port_.host().c_str())) |
658 return ERR_UNEXPECTED; | 678 return ERR_UNEXPECTED; |
659 | 679 |
660 trying_cached_session_ = context->session_cache()->SetSSLSessionWithKey( | 680 trying_cached_session_ = context->session_cache()->SetSSLSessionWithKey( |
661 ssl_, GetSocketSessionCacheKey(*this)); | 681 ssl_, GetSessionCacheKey()); |
662 | 682 |
663 BIO* ssl_bio = NULL; | 683 BIO* ssl_bio = NULL; |
664 // 0 => use default buffer sizes. | 684 // 0 => use default buffer sizes. |
665 if (!BIO_new_bio_pair(&ssl_bio, 0, &transport_bio_, 0)) | 685 if (!BIO_new_bio_pair(&ssl_bio, 0, &transport_bio_, 0)) |
666 return ERR_UNEXPECTED; | 686 return ERR_UNEXPECTED; |
667 DCHECK(ssl_bio); | 687 DCHECK(ssl_bio); |
668 DCHECK(transport_bio_); | 688 DCHECK(transport_bio_); |
669 | 689 |
670 SSL_set_bio(ssl_, ssl_bio, ssl_bio); | 690 SSL_set_bio(ssl_, ssl_bio, ssl_bio); |
671 | 691 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
768 void SSLClientSocketOpenSSL::DoWriteCallback(int rv) { | 788 void SSLClientSocketOpenSSL::DoWriteCallback(int rv) { |
769 // Since Run may result in Write being called, clear |user_write_callback_| | 789 // Since Run may result in Write being called, clear |user_write_callback_| |
770 // up front. | 790 // up front. |
771 if (rv > 0) | 791 if (rv > 0) |
772 was_ever_used_ = true; | 792 was_ever_used_ = true; |
773 user_write_buf_ = NULL; | 793 user_write_buf_ = NULL; |
774 user_write_buf_len_ = 0; | 794 user_write_buf_len_ = 0; |
775 base::ResetAndReturn(&user_write_callback_).Run(rv); | 795 base::ResetAndReturn(&user_write_callback_).Run(rv); |
776 } | 796 } |
777 | 797 |
798 std::string SSLClientSocketOpenSSL::GetSessionCacheKey() const { | |
799 return FormatSessionCacheKey(host_and_port_.ToString(), | |
800 ssl_session_cache_shard_); | |
801 } | |
802 | |
803 void SSLClientSocketOpenSSL::OnHandshakeSuccess() { | |
804 error_callback_.Reset(); | |
805 base::ResetAndReturn(&success_callback_).Run(); | |
806 } | |
807 | |
808 void SSLClientSocketOpenSSL::OnHandshakeFailure() { | |
809 if (!error_callback_.is_null()) | |
810 base::ResetAndReturn(&error_callback_).Run(); | |
mmenke
2014/07/17 19:21:32
Clear the success_callback_, too? Admittedly, sho
mshelley
2014/07/17 21:12:49
Done.
| |
811 } | |
812 | |
778 bool SSLClientSocketOpenSSL::DoTransportIO() { | 813 bool SSLClientSocketOpenSSL::DoTransportIO() { |
779 bool network_moved = false; | 814 bool network_moved = false; |
780 int rv; | 815 int rv; |
781 // Read and write as much data as possible. The loop is necessary because | 816 // Read and write as much data as possible. The loop is necessary because |
782 // Write() may return synchronously. | 817 // Write() may return synchronously. |
783 do { | 818 do { |
784 rv = BufferSend(); | 819 rv = BufferSend(); |
785 if (rv != ERR_IO_PENDING && rv != 0) | 820 if (rv != ERR_IO_PENDING && rv != 0) |
786 network_moved = true; | 821 network_moved = true; |
787 } while (rv > 0); | 822 } while (rv > 0); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
918 | 953 |
919 return server_cert_.get(); | 954 return server_cert_.get(); |
920 } | 955 } |
921 | 956 |
922 void SSLClientSocketOpenSSL::OnHandshakeIOComplete(int result) { | 957 void SSLClientSocketOpenSSL::OnHandshakeIOComplete(int result) { |
923 int rv = DoHandshakeLoop(result); | 958 int rv = DoHandshakeLoop(result); |
924 if (rv != ERR_IO_PENDING) { | 959 if (rv != ERR_IO_PENDING) { |
925 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); | 960 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_SSL_CONNECT, rv); |
926 DoConnectCallback(rv); | 961 DoConnectCallback(rv); |
927 } | 962 } |
963 if (rv < OK) | |
964 OnHandShakeFailure(); | |
mmenke
2014/07/17 19:21:32
Instead of having two places that call into OnHand
mmenke
2014/07/17 19:21:32
If rv is ERR_IO_PENDING, we're still waiting for a
mshelley
2014/07/17 21:12:49
Done.
| |
928 } | 965 } |
929 | 966 |
930 void SSLClientSocketOpenSSL::OnSendComplete(int result) { | 967 void SSLClientSocketOpenSSL::OnSendComplete(int result) { |
931 if (next_handshake_state_ == STATE_HANDSHAKE) { | 968 if (next_handshake_state_ == STATE_HANDSHAKE) { |
932 // In handshake phase. | 969 // In handshake phase. |
933 OnHandshakeIOComplete(result); | 970 OnHandshakeIOComplete(result); |
934 return; | 971 return; |
935 } | 972 } |
936 | 973 |
937 // OnSendComplete may need to call DoPayloadRead while the renegotiation | 974 // OnSendComplete may need to call DoPayloadRead while the renegotiation |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1059 } | 1096 } |
1060 return rv; | 1097 return rv; |
1061 } | 1098 } |
1062 | 1099 |
1063 int total_bytes_read = 0; | 1100 int total_bytes_read = 0; |
1064 do { | 1101 do { |
1065 rv = SSL_read(ssl_, user_read_buf_->data() + total_bytes_read, | 1102 rv = SSL_read(ssl_, user_read_buf_->data() + total_bytes_read, |
1066 user_read_buf_len_ - total_bytes_read); | 1103 user_read_buf_len_ - total_bytes_read); |
1067 if (rv > 0) | 1104 if (rv > 0) |
1068 total_bytes_read += rv; | 1105 total_bytes_read += rv; |
1106 // Failure of a read attempt indicates a failed false start | |
1107 // connection. | |
mmenke
2014/07/17 19:21:32
This should be indented two more, and go after the
mshelley
2014/07/17 21:12:49
Done.
| |
1108 else | |
1109 OnHandshakeFailure(); | |
mmenke
2014/07/17 19:21:32
nit: Use braces on both parts of an if statement,
mshelley
2014/07/17 21:12:49
Done.
| |
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 |
1077 // only be returned if no application data was already read; if it was, the | 1118 // only be returned if no application data was already read; if it was, the |
1078 // error code should be deferred until the next call of DoPayloadRead. | 1119 // error code should be deferred until the next call of DoPayloadRead. |
(...skipping 30 matching lines...) Expand all Loading... | |
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. | |
1120 if (rv >= 0) { | 1162 if (rv >= 0) { |
1121 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv, | 1163 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv, |
1122 user_write_buf_->data()); | 1164 user_write_buf_->data()); |
1123 return rv; | 1165 return rv; |
1124 } | 1166 } |
1125 | 1167 |
1168 OnHandshakeFailure(); | |
1126 int err = SSL_get_error(ssl_, rv); | 1169 int err = SSL_get_error(ssl_, rv); |
1127 return MapOpenSSLError(err, err_tracer); | 1170 return MapOpenSSLError(err, err_tracer); |
1128 } | 1171 } |
1129 | 1172 |
1130 int SSLClientSocketOpenSSL::BufferSend(void) { | 1173 int SSLClientSocketOpenSSL::BufferSend(void) { |
1131 if (transport_send_busy_) | 1174 if (transport_send_busy_) |
1132 return ERR_IO_PENDING; | 1175 return ERR_IO_PENDING; |
1133 | 1176 |
1134 if (!send_buffer_.get()) { | 1177 if (!send_buffer_.get()) { |
1135 // Get a fresh send buffer out of the send BIO. | 1178 // Get a fresh send buffer out of the send BIO. |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1430 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; | 1473 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; |
1431 return SSL_TLSEXT_ERR_OK; | 1474 return SSL_TLSEXT_ERR_OK; |
1432 } | 1475 } |
1433 | 1476 |
1434 scoped_refptr<X509Certificate> | 1477 scoped_refptr<X509Certificate> |
1435 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { | 1478 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { |
1436 return server_cert_; | 1479 return server_cert_; |
1437 } | 1480 } |
1438 | 1481 |
1439 } // namespace net | 1482 } // namespace net |
OLD | NEW |