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

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

Issue 2593063003: Add Socket::ReadIfReady() (Closed)
Patch Set: Self Created 3 years, 10 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 #include "net/socket/ssl_client_socket_impl.h" 5 #include "net/socket/ssl_client_socket_impl.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 842 matching lines...) Expand 10 before | Expand all | Expand 10 after
853 853
854 // static 854 // static
855 void SSLClientSocketImpl::DumpSSLClientSessionMemoryStats( 855 void SSLClientSocketImpl::DumpSSLClientSessionMemoryStats(
856 base::trace_event::ProcessMemoryDump* pmd) { 856 base::trace_event::ProcessMemoryDump* pmd) {
857 SSLContext::GetInstance()->session_cache()->DumpMemoryStats(pmd); 857 SSLContext::GetInstance()->session_cache()->DumpMemoryStats(pmd);
858 } 858 }
859 859
860 int SSLClientSocketImpl::Read(IOBuffer* buf, 860 int SSLClientSocketImpl::Read(IOBuffer* buf,
861 int buf_len, 861 int buf_len,
862 const CompletionCallback& callback) { 862 const CompletionCallback& callback) {
863 user_read_buf_ = buf; 863 int rv = ReadIfReady(buf, buf_len, callback);
864 user_read_buf_len_ = buf_len; 864 if (rv == ERR_IO_PENDING) {
865 user_read_buf_ = buf;
866 user_read_buf_len_ = buf_len;
867 }
868 return rv;
869 }
865 870
866 int rv = DoPayloadRead(); 871 int SSLClientSocketImpl::ReadIfReady(IOBuffer* buf,
872 int buf_len,
873 const CompletionCallback& callback) {
874 int rv = DoPayloadRead(buf, buf_len);
867 875
868 if (rv == ERR_IO_PENDING) { 876 if (rv == ERR_IO_PENDING) {
869 user_read_callback_ = callback; 877 user_read_callback_ = callback;
870 } else { 878 } else {
871 if (rv > 0) 879 if (rv > 0)
872 was_ever_used_ = true; 880 was_ever_used_ = true;
873 user_read_buf_ = NULL;
874 user_read_buf_len_ = 0;
875 } 881 }
876
877 return rv; 882 return rv;
878 } 883 }
879 884
880 int SSLClientSocketImpl::Write(IOBuffer* buf, 885 int SSLClientSocketImpl::Write(IOBuffer* buf,
881 int buf_len, 886 int buf_len,
882 const CompletionCallback& callback) { 887 const CompletionCallback& callback) {
883 user_write_buf_ = buf; 888 user_write_buf_ = buf;
884 user_write_buf_len_ = buf_len; 889 user_write_buf_len_ = buf_len;
885 890
886 int rv = DoPayloadWrite(); 891 int rv = DoPayloadWrite();
(...skipping 11 matching lines...) Expand all
898 } 903 }
899 904
900 int SSLClientSocketImpl::SetReceiveBufferSize(int32_t size) { 905 int SSLClientSocketImpl::SetReceiveBufferSize(int32_t size) {
901 return transport_->socket()->SetReceiveBufferSize(size); 906 return transport_->socket()->SetReceiveBufferSize(size);
902 } 907 }
903 908
904 int SSLClientSocketImpl::SetSendBufferSize(int32_t size) { 909 int SSLClientSocketImpl::SetSendBufferSize(int32_t size) {
905 return transport_->socket()->SetSendBufferSize(size); 910 return transport_->socket()->SetSendBufferSize(size);
906 } 911 }
907 912
908 void SSLClientSocketImpl::OnReadReady() { 913 void SSLClientSocketImpl::OnReadReady(int rv) {
909 // During a renegotiation, either Read or Write calls may be blocked on a 914 // During a renegotiation, either Read or Write calls may be blocked on a
910 // transport read. 915 // transport read.
911 RetryAllOperations(); 916 RetryAllOperations(rv);
912 } 917 }
913 918
914 void SSLClientSocketImpl::OnWriteReady() { 919 void SSLClientSocketImpl::OnWriteReady(int rv) {
915 // During a renegotiation, either Read or Write calls may be blocked on a 920 // During a renegotiation, either Read or Write calls may be blocked on a
916 // transport read. 921 // transport read.
917 RetryAllOperations(); 922 RetryAllOperations(rv);
918 } 923 }
919 924
920 int SSLClientSocketImpl::Init() { 925 int SSLClientSocketImpl::Init() {
921 DCHECK(!ssl_); 926 DCHECK(!ssl_);
922 927
923 #if defined(USE_NSS_CERTS) 928 #if defined(USE_NSS_CERTS)
924 if (ssl_config_.cert_io_enabled) { 929 if (ssl_config_.cert_io_enabled) {
925 // TODO(davidben): Move this out of SSLClientSocket. See 930 // TODO(davidben): Move this out of SSLClientSocket. See
926 // https://crbug.com/539520. 931 // https://crbug.com/539520.
927 EnsureNSSHttpIOInit(); 932 EnsureNSSHttpIOInit();
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
1043 SSL_enable_ocsp_stapling(ssl_.get()); 1048 SSL_enable_ocsp_stapling(ssl_.get());
1044 1049
1045 return OK; 1050 return OK;
1046 } 1051 }
1047 1052
1048 void SSLClientSocketImpl::DoReadCallback(int rv) { 1053 void SSLClientSocketImpl::DoReadCallback(int rv) {
1049 // Since Run may result in Read being called, clear |user_read_callback_| 1054 // Since Run may result in Read being called, clear |user_read_callback_|
1050 // up front. 1055 // up front.
1051 if (rv > 0) 1056 if (rv > 0)
1052 was_ever_used_ = true; 1057 was_ever_used_ = true;
1053 user_read_buf_ = NULL; 1058 user_read_buf_ = nullptr;
1054 user_read_buf_len_ = 0; 1059 user_read_buf_len_ = 0;
1055 base::ResetAndReturn(&user_read_callback_).Run(rv); 1060 base::ResetAndReturn(&user_read_callback_).Run(rv);
1056 } 1061 }
1057 1062
1058 void SSLClientSocketImpl::DoWriteCallback(int rv) { 1063 void SSLClientSocketImpl::DoWriteCallback(int rv) {
1059 // Since Run may result in Write being called, clear |user_write_callback_| 1064 // Since Run may result in Write being called, clear |user_write_callback_|
1060 // up front. 1065 // up front.
1061 if (rv > 0) 1066 if (rv > 0)
1062 was_ever_used_ = true; 1067 was_ever_used_ = true;
1063 user_write_buf_ = NULL; 1068 user_write_buf_ = NULL;
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after
1398 case STATE_NONE: 1403 case STATE_NONE:
1399 default: 1404 default:
1400 rv = ERR_UNEXPECTED; 1405 rv = ERR_UNEXPECTED;
1401 NOTREACHED() << "unexpected state" << state; 1406 NOTREACHED() << "unexpected state" << state;
1402 break; 1407 break;
1403 } 1408 }
1404 } while (rv != ERR_IO_PENDING && next_handshake_state_ != STATE_NONE); 1409 } while (rv != ERR_IO_PENDING && next_handshake_state_ != STATE_NONE);
1405 return rv; 1410 return rv;
1406 } 1411 }
1407 1412
1408 int SSLClientSocketImpl::DoPayloadRead() { 1413 int SSLClientSocketImpl::DoPayloadRead(IOBuffer* buf, int buf_len) {
1409 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); 1414 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
1410 1415
1411 DCHECK_LT(0, user_read_buf_len_); 1416 DCHECK_LT(0, buf_len);
1412 DCHECK(user_read_buf_.get()); 1417 DCHECK(buf);
1413 1418
1414 int rv; 1419 int rv;
1415 if (pending_read_error_ != kNoPendingResult) { 1420 if (pending_read_error_ != kNoPendingResult) {
1416 rv = pending_read_error_; 1421 rv = pending_read_error_;
1417 pending_read_error_ = kNoPendingResult; 1422 pending_read_error_ = kNoPendingResult;
1418 if (rv == 0) { 1423 if (rv == 0) {
1419 net_log_.AddByteTransferEvent(NetLogEventType::SSL_SOCKET_BYTES_RECEIVED, 1424 net_log_.AddByteTransferEvent(NetLogEventType::SSL_SOCKET_BYTES_RECEIVED,
1420 rv, user_read_buf_->data()); 1425 rv, buf->data());
1421 } else { 1426 } else {
1422 net_log_.AddEvent( 1427 net_log_.AddEvent(
1423 NetLogEventType::SSL_READ_ERROR, 1428 NetLogEventType::SSL_READ_ERROR,
1424 CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_, 1429 CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_,
1425 pending_read_error_info_)); 1430 pending_read_error_info_));
1426 } 1431 }
1427 pending_read_ssl_error_ = SSL_ERROR_NONE; 1432 pending_read_ssl_error_ = SSL_ERROR_NONE;
1428 pending_read_error_info_ = OpenSSLErrorInfo(); 1433 pending_read_error_info_ = OpenSSLErrorInfo();
1429 return rv; 1434 return rv;
1430 } 1435 }
1431 1436
1432 int total_bytes_read = 0; 1437 int total_bytes_read = 0;
1433 int ssl_ret; 1438 int ssl_ret;
1434 do { 1439 do {
1435 ssl_ret = SSL_read(ssl_.get(), user_read_buf_->data() + total_bytes_read, 1440 ssl_ret = SSL_read(ssl_.get(), buf->data() + total_bytes_read,
1436 user_read_buf_len_ - total_bytes_read); 1441 buf_len - total_bytes_read);
1437 if (ssl_ret > 0) 1442 if (ssl_ret > 0)
1438 total_bytes_read += ssl_ret; 1443 total_bytes_read += ssl_ret;
1439 } while (total_bytes_read < user_read_buf_len_ && ssl_ret > 0); 1444 } while (total_bytes_read < buf_len && ssl_ret > 0);
1440 1445
1441 // Although only the final SSL_read call may have failed, the failure needs to 1446 // Although only the final SSL_read call may have failed, the failure needs to
1442 // processed immediately, while the information still available in OpenSSL's 1447 // processed immediately, while the information still available in OpenSSL's
1443 // error queue. 1448 // error queue.
1444 if (ssl_ret <= 0) { 1449 if (ssl_ret <= 0) {
1445 // A zero return from SSL_read may mean any of: 1450 // A zero return from SSL_read may mean any of:
1446 // - The underlying BIO_read returned 0. 1451 // - The underlying BIO_read returned 0.
1447 // - The peer sent a close_notify. 1452 // - The peer sent a close_notify.
1448 // - Any arbitrary error. https://crbug.com/466303 1453 // - Any arbitrary error. https://crbug.com/466303
1449 // 1454 //
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1486 pending_read_error_ = kNoPendingResult; 1491 pending_read_error_ = kNoPendingResult;
1487 } else { 1492 } else {
1488 // No bytes were returned. Return the pending read error immediately. 1493 // No bytes were returned. Return the pending read error immediately.
1489 DCHECK_NE(kNoPendingResult, pending_read_error_); 1494 DCHECK_NE(kNoPendingResult, pending_read_error_);
1490 rv = pending_read_error_; 1495 rv = pending_read_error_;
1491 pending_read_error_ = kNoPendingResult; 1496 pending_read_error_ = kNoPendingResult;
1492 } 1497 }
1493 1498
1494 if (rv >= 0) { 1499 if (rv >= 0) {
1495 net_log_.AddByteTransferEvent(NetLogEventType::SSL_SOCKET_BYTES_RECEIVED, 1500 net_log_.AddByteTransferEvent(NetLogEventType::SSL_SOCKET_BYTES_RECEIVED,
1496 rv, user_read_buf_->data()); 1501 rv, buf->data());
1497 } else if (rv != ERR_IO_PENDING) { 1502 } else if (rv != ERR_IO_PENDING) {
1498 net_log_.AddEvent( 1503 net_log_.AddEvent(
1499 NetLogEventType::SSL_READ_ERROR, 1504 NetLogEventType::SSL_READ_ERROR,
1500 CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_, 1505 CreateNetLogOpenSSLErrorCallback(rv, pending_read_ssl_error_,
1501 pending_read_error_info_)); 1506 pending_read_error_info_));
1502 pending_read_ssl_error_ = SSL_ERROR_NONE; 1507 pending_read_ssl_error_ = SSL_ERROR_NONE;
1503 pending_read_error_info_ = OpenSSLErrorInfo(); 1508 pending_read_error_info_ = OpenSSLErrorInfo();
1504 } 1509 }
1505 return rv; 1510 return rv;
1506 } 1511 }
(...skipping 15 matching lines...) Expand all
1522 int net_error = MapLastOpenSSLError(ssl_error, err_tracer, &error_info); 1527 int net_error = MapLastOpenSSLError(ssl_error, err_tracer, &error_info);
1523 1528
1524 if (net_error != ERR_IO_PENDING) { 1529 if (net_error != ERR_IO_PENDING) {
1525 net_log_.AddEvent( 1530 net_log_.AddEvent(
1526 NetLogEventType::SSL_WRITE_ERROR, 1531 NetLogEventType::SSL_WRITE_ERROR,
1527 CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info)); 1532 CreateNetLogOpenSSLErrorCallback(net_error, ssl_error, error_info));
1528 } 1533 }
1529 return net_error; 1534 return net_error;
1530 } 1535 }
1531 1536
1532 void SSLClientSocketImpl::RetryAllOperations() { 1537 void SSLClientSocketImpl::RetryAllOperations(int rv) {
1538 DCHECK_NE(ERR_IO_PENDING, rv);
1539 DCHECK_GE(OK, rv);
1540
1533 // SSL_do_handshake, SSL_read, and SSL_write may all be retried when blocked, 1541 // SSL_do_handshake, SSL_read, and SSL_write may all be retried when blocked,
1534 // so retry all operations for simplicity. (Otherwise, SSL_get_error for each 1542 // so retry all operations for simplicity. (Otherwise, SSL_get_error for each
1535 // operation may be remembered to retry only the blocked ones.) 1543 // operation may be remembered to retry only the blocked ones.)
1536 1544
1537 if (next_handshake_state_ == STATE_HANDSHAKE) { 1545 if (next_handshake_state_ == STATE_HANDSHAKE) {
1538 // In handshake phase. The parameter to OnHandshakeIOComplete is unused. 1546 // In handshake phase. The parameter to OnHandshakeIOComplete is unused.
1539 OnHandshakeIOComplete(OK); 1547 OnHandshakeIOComplete(OK);
1540 return; 1548 return;
1541 } 1549 }
1542 1550
1543 int rv_read = ERR_IO_PENDING; 1551 int rv_read = ERR_IO_PENDING;
1544 int rv_write = ERR_IO_PENDING; 1552 int rv_write = ERR_IO_PENDING;
1545 if (user_read_buf_) 1553 if (user_read_buf_) {
1546 rv_read = DoPayloadRead(); 1554 rv_read = DoPayloadRead(user_read_buf_.get(), user_read_buf_len_);
1555 } else if (!user_read_callback_.is_null()) {
1556 // When ReadIfReady() is used, skip DoPayloadRead().
1557 rv_read = rv;
davidben 2017/02/10 23:33:48 Hrm. I'm not sure this is quite right. Suppose we
xunjieli 2017/02/13 20:28:18 Done.
1558 }
1559
1547 if (user_write_buf_) 1560 if (user_write_buf_)
1548 rv_write = DoPayloadWrite(); 1561 rv_write = DoPayloadWrite();
1549 1562
1550 // Performing the Read callback may cause |this| to be deleted. If this 1563 // Performing the Read callback may cause |this| to be deleted. If this
1551 // happens, the Write callback should not be invoked. Guard against this by 1564 // happens, the Write callback should not be invoked. Guard against this by
1552 // holding a WeakPtr to |this| and ensuring it's still valid. 1565 // holding a WeakPtr to |this| and ensuring it's still valid.
1553 base::WeakPtr<SSLClientSocketImpl> guard(weak_factory_.GetWeakPtr()); 1566 base::WeakPtr<SSLClientSocketImpl> guard(weak_factory_.GetWeakPtr());
1554 if (rv_read != ERR_IO_PENDING) 1567 if (rv_read != ERR_IO_PENDING)
1555 DoReadCallback(rv_read); 1568 DoReadCallback(rv_read);
1556 1569
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
1882 DCHECK(ssl_config_.client_private_key); 1895 DCHECK(ssl_config_.client_private_key);
1883 1896
1884 net_log_.EndEventWithNetErrorCode(NetLogEventType::SSL_PRIVATE_KEY_OP, error); 1897 net_log_.EndEventWithNetErrorCode(NetLogEventType::SSL_PRIVATE_KEY_OP, error);
1885 1898
1886 signature_result_ = error; 1899 signature_result_ = error;
1887 if (signature_result_ == OK) 1900 if (signature_result_ == OK)
1888 signature_ = signature; 1901 signature_ = signature;
1889 1902
1890 // During a renegotiation, either Read or Write calls may be blocked on an 1903 // During a renegotiation, either Read or Write calls may be blocked on an
1891 // asynchronous private key operation. 1904 // asynchronous private key operation.
1892 RetryAllOperations(); 1905 RetryAllOperations(error);
1893 } 1906 }
1894 1907
1895 int SSLClientSocketImpl::TokenBindingAdd(const uint8_t** out, 1908 int SSLClientSocketImpl::TokenBindingAdd(const uint8_t** out,
1896 size_t* out_len, 1909 size_t* out_len,
1897 int* out_alert_value) { 1910 int* out_alert_value) {
1898 if (ssl_config_.token_binding_params.empty()) { 1911 if (ssl_config_.token_binding_params.empty()) {
1899 return 0; 1912 return 0;
1900 } 1913 }
1901 bssl::ScopedCBB output; 1914 bssl::ScopedCBB output;
1902 CBB parameters_list; 1915 CBB parameters_list;
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
2040 if (ERR_GET_REASON(info->error_code) == SSL_R_TLSV1_ALERT_ACCESS_DENIED && 2053 if (ERR_GET_REASON(info->error_code) == SSL_R_TLSV1_ALERT_ACCESS_DENIED &&
2041 !certificate_requested_) { 2054 !certificate_requested_) {
2042 net_error = ERR_SSL_PROTOCOL_ERROR; 2055 net_error = ERR_SSL_PROTOCOL_ERROR;
2043 } 2056 }
2044 } 2057 }
2045 2058
2046 return net_error; 2059 return net_error;
2047 } 2060 }
2048 2061
2049 } // namespace net 2062 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698