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 <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 |
| 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) { |
| (...skipping 24 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->GetSocketSessionCacheKey(); |
| 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 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 333 } | 325 } |
| 334 | 326 |
| 335 SSLClientSocketOpenSSL::SSLClientSocketOpenSSL( | 327 SSLClientSocketOpenSSL::SSLClientSocketOpenSSL( |
| 336 scoped_ptr<ClientSocketHandle> transport_socket, | 328 scoped_ptr<ClientSocketHandle> transport_socket, |
| 337 const HostPortPair& host_and_port, | 329 const HostPortPair& host_and_port, |
| 338 const SSLConfig& ssl_config, | 330 const SSLConfig& ssl_config, |
| 339 const SSLClientSocketContext& context) | 331 const SSLClientSocketContext& context) |
| 340 : transport_send_busy_(false), | 332 : transport_send_busy_(false), |
| 341 transport_recv_busy_(false), | 333 transport_recv_busy_(false), |
| 342 transport_recv_eof_(false), | 334 transport_recv_eof_(false), |
| 335 has_read_(false), | |
| 336 has_written_(false), | |
| 343 weak_factory_(this), | 337 weak_factory_(this), |
| 344 pending_read_error_(kNoPendingReadResult), | 338 pending_read_error_(kNoPendingReadResult), |
| 345 transport_write_error_(OK), | 339 transport_write_error_(OK), |
| 346 server_cert_chain_(new PeerCertificateChain(NULL)), | 340 server_cert_chain_(new PeerCertificateChain(NULL)), |
| 347 completed_handshake_(false), | 341 completed_handshake_(false), |
| 348 was_ever_used_(false), | 342 was_ever_used_(false), |
| 349 client_auth_cert_needed_(false), | 343 client_auth_cert_needed_(false), |
| 350 cert_verifier_(context.cert_verifier), | 344 cert_verifier_(context.cert_verifier), |
| 351 server_bound_cert_service_(context.server_bound_cert_service), | 345 server_bound_cert_service_(context.server_bound_cert_service), |
| 346 is_leader_(false), | |
| 352 ssl_(NULL), | 347 ssl_(NULL), |
| 353 transport_bio_(NULL), | 348 transport_bio_(NULL), |
| 354 transport_(transport_socket.Pass()), | 349 transport_(transport_socket.Pass()), |
| 355 host_and_port_(host_and_port), | 350 host_and_port_(host_and_port), |
| 356 ssl_config_(ssl_config), | 351 ssl_config_(ssl_config), |
| 357 ssl_session_cache_shard_(context.ssl_session_cache_shard), | 352 ssl_session_cache_shard_(context.ssl_session_cache_shard), |
| 358 trying_cached_session_(false), | 353 trying_cached_session_(false), |
| 359 next_handshake_state_(STATE_NONE), | 354 next_handshake_state_(STATE_NONE), |
| 360 npn_status_(kNextProtoUnsupported), | 355 npn_status_(kNextProtoUnsupported), |
| 361 channel_id_request_return_value_(ERR_UNEXPECTED), | 356 channel_id_request_return_value_(ERR_UNEXPECTED), |
| 362 channel_id_xtn_negotiated_(false), | 357 channel_id_xtn_negotiated_(false), |
| 363 net_log_(transport_->socket()->NetLog()) {} | 358 net_log_(transport_->socket()->NetLog()) {} |
| 364 | 359 |
| 365 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { | 360 SSLClientSocketOpenSSL::~SSLClientSocketOpenSSL() { |
| 366 Disconnect(); | 361 Disconnect(); |
| 367 } | 362 } |
| 368 | 363 |
| 364 // Compute a unique key string for the SSL session cache. | |
| 365 // Return a string. | |
| 366 std::string SSLClientSocketOpenSSL::GetSocketSessionCacheKey() const { | |
| 367 std::string result = host_and_port_.ToString(); | |
| 368 result.append("/"); | |
| 369 result.append(ssl_session_cache_shard_); | |
| 370 return result; | |
| 371 } | |
| 372 | |
| 373 bool SSLClientSocketOpenSSL::InSessionCache() const { | |
| 374 SSLContext* context = SSLContext::GetInstance(); | |
| 375 std::string cache_key = GetSocketSessionCacheKey(); | |
| 376 return context->session_cache()->SSLSessionIsInCache(cache_key); | |
| 377 } | |
| 378 | |
| 379 void SSLClientSocketOpenSSL::WatchSessionForCompletion( | |
| 380 const base::Closure& callback) const { | |
| 381 SSLContext* context = SSLContext::GetInstance(); | |
| 382 context->session_cache()->NotifyOnSessionAdded(ssl_, callback); | |
|
wtc
2014/07/08 01:25:42
IMPORTANT: in the destructor, we should remove the
mshelley
2014/07/09 19:51:00
Done.
| |
| 383 } | |
| 384 | |
| 385 void SSLClientSocketOpenSSL::SetSocketFailureCallback( | |
| 386 const base::Closure& callback) { | |
| 387 error_callback_ = callback; | |
| 388 } | |
| 389 | |
| 390 void SSLClientSocketOpenSSL::SetIsLeader() { | |
| 391 is_leader_ = true; | |
| 392 } | |
| 393 | |
| 394 void SSLClientSocketOpenSSL::OnSocketFailure() { | |
| 395 if (is_leader_) | |
| 396 error_callback_.Run(); | |
|
wtc
2014/07/08 01:25:42
Should we reset error_callback_?
mshelley
2014/07/09 19:51:00
Done.
| |
| 397 } | |
| 398 | |
| 369 void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( | 399 void SSLClientSocketOpenSSL::GetSSLCertRequestInfo( |
| 370 SSLCertRequestInfo* cert_request_info) { | 400 SSLCertRequestInfo* cert_request_info) { |
| 371 cert_request_info->host_and_port = host_and_port_; | 401 cert_request_info->host_and_port = host_and_port_; |
| 372 cert_request_info->cert_authorities = cert_authorities_; | 402 cert_request_info->cert_authorities = cert_authorities_; |
| 373 cert_request_info->cert_key_types = cert_key_types_; | 403 cert_request_info->cert_key_types = cert_key_types_; |
| 374 } | 404 } |
| 375 | 405 |
| 376 SSLClientSocket::NextProtoStatus SSLClientSocketOpenSSL::GetNextProto( | 406 SSLClientSocket::NextProtoStatus SSLClientSocketOpenSSL::GetNextProto( |
| 377 std::string* proto, std::string* server_protos) { | 407 std::string* proto, std::string* server_protos) { |
| 378 *proto = npn_proto_; | 408 *proto = npn_proto_; |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 651 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 681 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 652 | 682 |
| 653 ssl_ = SSL_new(context->ssl_ctx()); | 683 ssl_ = SSL_new(context->ssl_ctx()); |
| 654 if (!ssl_ || !context->SetClientSocketForSSL(ssl_, this)) | 684 if (!ssl_ || !context->SetClientSocketForSSL(ssl_, this)) |
| 655 return ERR_UNEXPECTED; | 685 return ERR_UNEXPECTED; |
| 656 | 686 |
| 657 if (!SSL_set_tlsext_host_name(ssl_, host_and_port_.host().c_str())) | 687 if (!SSL_set_tlsext_host_name(ssl_, host_and_port_.host().c_str())) |
| 658 return ERR_UNEXPECTED; | 688 return ERR_UNEXPECTED; |
| 659 | 689 |
| 660 trying_cached_session_ = context->session_cache()->SetSSLSessionWithKey( | 690 trying_cached_session_ = context->session_cache()->SetSSLSessionWithKey( |
| 661 ssl_, GetSocketSessionCacheKey(*this)); | 691 ssl_, GetSocketSessionCacheKey()); |
| 662 | 692 |
| 663 BIO* ssl_bio = NULL; | 693 BIO* ssl_bio = NULL; |
| 664 // 0 => use default buffer sizes. | 694 // 0 => use default buffer sizes. |
| 665 if (!BIO_new_bio_pair(&ssl_bio, 0, &transport_bio_, 0)) | 695 if (!BIO_new_bio_pair(&ssl_bio, 0, &transport_bio_, 0)) |
| 666 return ERR_UNEXPECTED; | 696 return ERR_UNEXPECTED; |
| 667 DCHECK(ssl_bio); | 697 DCHECK(ssl_bio); |
| 668 DCHECK(transport_bio_); | 698 DCHECK(transport_bio_); |
| 669 | 699 |
| 670 SSL_set_bio(ssl_, ssl_bio, ssl_bio); | 700 SSL_set_bio(ssl_, ssl_bio, ssl_bio); |
| 671 | 701 |
| (...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1011 if (network_moved && next_handshake_state_ == STATE_HANDSHAKE) { | 1041 if (network_moved && next_handshake_state_ == STATE_HANDSHAKE) { |
| 1012 // In general we exit the loop if rv is ERR_IO_PENDING. In this | 1042 // In general we exit the loop if rv is ERR_IO_PENDING. In this |
| 1013 // special case we keep looping even if rv is ERR_IO_PENDING because | 1043 // special case we keep looping even if rv is ERR_IO_PENDING because |
| 1014 // the transport IO may allow DoHandshake to make progress. | 1044 // the transport IO may allow DoHandshake to make progress. |
| 1015 rv = OK; // This causes us to stay in the loop. | 1045 rv = OK; // This causes us to stay in the loop. |
| 1016 } | 1046 } |
| 1017 } while (rv != ERR_IO_PENDING && next_handshake_state_ != STATE_NONE); | 1047 } while (rv != ERR_IO_PENDING && next_handshake_state_ != STATE_NONE); |
| 1018 return rv; | 1048 return rv; |
| 1019 } | 1049 } |
| 1020 | 1050 |
| 1021 int SSLClientSocketOpenSSL::DoReadLoop(int result) { | 1051 int SSLClientSocketOpenSSL::DoReadLoop(int result) {if (result < 0) |
|
wtc
2014/07/08 01:25:42
Move "if (result < 0)" to the next line.
mshelley
2014/07/09 19:51:00
Done.
| |
| 1022 if (result < 0) | |
| 1023 return result; | 1052 return result; |
| 1024 | 1053 |
| 1025 bool network_moved; | 1054 bool network_moved; |
| 1026 int rv; | 1055 int rv; |
| 1027 do { | 1056 do { |
| 1028 rv = DoPayloadRead(); | 1057 rv = DoPayloadRead(); |
| 1029 network_moved = DoTransportIO(); | 1058 network_moved = DoTransportIO(); |
| 1030 } while (rv == ERR_IO_PENDING && network_moved); | 1059 } while (rv == ERR_IO_PENDING && network_moved); |
| 1031 | 1060 |
| 1032 return rv; | 1061 return rv; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 1057 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, | 1086 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, |
| 1058 rv, user_read_buf_->data()); | 1087 rv, user_read_buf_->data()); |
| 1059 } | 1088 } |
| 1060 return rv; | 1089 return rv; |
| 1061 } | 1090 } |
| 1062 | 1091 |
| 1063 int total_bytes_read = 0; | 1092 int total_bytes_read = 0; |
| 1064 do { | 1093 do { |
| 1065 rv = SSL_read(ssl_, user_read_buf_->data() + total_bytes_read, | 1094 rv = SSL_read(ssl_, user_read_buf_->data() + total_bytes_read, |
| 1066 user_read_buf_len_ - total_bytes_read); | 1095 user_read_buf_len_ - total_bytes_read); |
| 1096 if (!has_read_ && rv != OK && SSLClientSocket::GetEnableJobWaiting()) | |
| 1097 OnSocketFailure(); | |
|
wtc
2014/07/08 01:25:42
Why do we need to call OnSocketFailure after the f
mshelley
2014/07/09 19:51:00
Done.
| |
| 1098 has_read_ = true; | |
| 1067 if (rv > 0) | 1099 if (rv > 0) |
| 1068 total_bytes_read += rv; | 1100 total_bytes_read += rv; |
| 1069 } while (total_bytes_read < user_read_buf_len_ && rv > 0); | 1101 } while (total_bytes_read < user_read_buf_len_ && rv > 0); |
| 1070 | 1102 |
| 1071 if (total_bytes_read == user_read_buf_len_) { | 1103 if (total_bytes_read == user_read_buf_len_) { |
| 1072 rv = total_bytes_read; | 1104 rv = total_bytes_read; |
| 1073 } else { | 1105 } else { |
| 1074 // Otherwise, an error occurred (rv <= 0). The error needs to be handled | 1106 // Otherwise, an error occurred (rv <= 0). The error needs to be handled |
| 1075 // immediately, while the OpenSSL errors are still available in | 1107 // immediately, while the OpenSSL errors are still available in |
| 1076 // thread-local storage. However, the handled/remapped error code should | 1108 // thread-local storage. However, the handled/remapped error code should |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1109 if (rv >= 0) { | 1141 if (rv >= 0) { |
| 1110 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, rv, | 1142 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, rv, |
| 1111 user_read_buf_->data()); | 1143 user_read_buf_->data()); |
| 1112 } | 1144 } |
| 1113 return rv; | 1145 return rv; |
| 1114 } | 1146 } |
| 1115 | 1147 |
| 1116 int SSLClientSocketOpenSSL::DoPayloadWrite() { | 1148 int SSLClientSocketOpenSSL::DoPayloadWrite() { |
| 1117 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 1149 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 1118 int rv = SSL_write(ssl_, user_write_buf_->data(), user_write_buf_len_); | 1150 int rv = SSL_write(ssl_, user_write_buf_->data(), user_write_buf_len_); |
| 1119 | 1151 if (!has_written_ && rv != OK && SSLClientSocket::GetEnableJobWaiting()) |
| 1152 OnSocketFailure(); | |
| 1153 has_written_ = true; | |
| 1120 if (rv >= 0) { | 1154 if (rv >= 0) { |
| 1121 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv, | 1155 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_SENT, rv, |
| 1122 user_write_buf_->data()); | 1156 user_write_buf_->data()); |
| 1123 return rv; | 1157 return rv; |
| 1124 } | 1158 } |
| 1125 | 1159 |
| 1126 int err = SSL_get_error(ssl_, rv); | 1160 int err = SSL_get_error(ssl_, rv); |
| 1127 return MapOpenSSLError(err, err_tracer); | 1161 return MapOpenSSLError(err, err_tracer); |
| 1128 } | 1162 } |
| 1129 | 1163 |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1430 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; | 1464 DVLOG(2) << "next protocol: '" << npn_proto_ << "' status: " << npn_status_; |
| 1431 return SSL_TLSEXT_ERR_OK; | 1465 return SSL_TLSEXT_ERR_OK; |
| 1432 } | 1466 } |
| 1433 | 1467 |
| 1434 scoped_refptr<X509Certificate> | 1468 scoped_refptr<X509Certificate> |
| 1435 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { | 1469 SSLClientSocketOpenSSL::GetUnverifiedServerCertificateChain() const { |
| 1436 return server_cert_; | 1470 return server_cert_; |
| 1437 } | 1471 } |
| 1438 | 1472 |
| 1439 } // namespace net | 1473 } // namespace net |
| OLD | NEW |