OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_win.h" | 5 #include "net/socket/ssl_client_socket_win.h" |
6 | 6 |
7 #include <schnlsp.h> | 7 #include <schnlsp.h> |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 handshake_io_callback_(this, | 391 handshake_io_callback_(this, |
392 &SSLClientSocketWin::OnHandshakeIOComplete)), | 392 &SSLClientSocketWin::OnHandshakeIOComplete)), |
393 ALLOW_THIS_IN_INITIALIZER_LIST( | 393 ALLOW_THIS_IN_INITIALIZER_LIST( |
394 read_callback_(this, &SSLClientSocketWin::OnReadComplete)), | 394 read_callback_(this, &SSLClientSocketWin::OnReadComplete)), |
395 ALLOW_THIS_IN_INITIALIZER_LIST( | 395 ALLOW_THIS_IN_INITIALIZER_LIST( |
396 write_callback_(this, &SSLClientSocketWin::OnWriteComplete)), | 396 write_callback_(this, &SSLClientSocketWin::OnWriteComplete)), |
397 transport_(transport_socket), | 397 transport_(transport_socket), |
398 host_and_port_(host_and_port), | 398 host_and_port_(host_and_port), |
399 ssl_config_(ssl_config), | 399 ssl_config_(ssl_config), |
400 old_user_connect_callback_(NULL), | 400 old_user_connect_callback_(NULL), |
401 user_read_callback_(NULL), | 401 old_user_read_callback_(NULL), |
402 user_read_buf_len_(0), | 402 user_read_buf_len_(0), |
403 user_write_callback_(NULL), | 403 user_write_callback_(NULL), |
404 user_write_buf_len_(0), | 404 user_write_buf_len_(0), |
405 next_state_(STATE_NONE), | 405 next_state_(STATE_NONE), |
406 cert_verifier_(context.cert_verifier), | 406 cert_verifier_(context.cert_verifier), |
407 creds_(NULL), | 407 creds_(NULL), |
408 isc_status_(SEC_E_OK), | 408 isc_status_(SEC_E_OK), |
409 payload_send_buffer_len_(0), | 409 payload_send_buffer_len_(0), |
410 bytes_sent_(0), | 410 bytes_sent_(0), |
411 decrypted_ptr_(NULL), | 411 decrypted_ptr_(NULL), |
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
777 if (transport_.get() && transport_->socket()) { | 777 if (transport_.get() && transport_->socket()) { |
778 return transport_->socket()->GetConnectTimeMicros(); | 778 return transport_->socket()->GetConnectTimeMicros(); |
779 } | 779 } |
780 NOTREACHED(); | 780 NOTREACHED(); |
781 return base::TimeDelta::FromMicroseconds(-1); | 781 return base::TimeDelta::FromMicroseconds(-1); |
782 } | 782 } |
783 | 783 |
784 int SSLClientSocketWin::Read(IOBuffer* buf, int buf_len, | 784 int SSLClientSocketWin::Read(IOBuffer* buf, int buf_len, |
785 OldCompletionCallback* callback) { | 785 OldCompletionCallback* callback) { |
786 DCHECK(completed_handshake()); | 786 DCHECK(completed_handshake()); |
787 DCHECK(!user_read_callback_); | 787 DCHECK(!old_user_read_callback_ && user_read_callback_.is_null()); |
788 | 788 |
789 // If we have surplus decrypted plaintext, satisfy the Read with it without | 789 // If we have surplus decrypted plaintext, satisfy the Read with it without |
790 // reading more ciphertext from the transport socket. | 790 // reading more ciphertext from the transport socket. |
| 791 if (bytes_decrypted_ != 0) { |
| 792 int len = std::min(buf_len, bytes_decrypted_); |
| 793 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, len, |
| 794 decrypted_ptr_); |
| 795 memcpy(buf->data(), decrypted_ptr_, len); |
| 796 decrypted_ptr_ += len; |
| 797 bytes_decrypted_ -= len; |
| 798 if (bytes_decrypted_ == 0) { |
| 799 decrypted_ptr_ = NULL; |
| 800 if (bytes_received_ != 0) { |
| 801 memmove(recv_buffer_.get(), received_ptr_, bytes_received_); |
| 802 received_ptr_ = recv_buffer_.get(); |
| 803 } |
| 804 } |
| 805 return len; |
| 806 } |
| 807 |
| 808 DCHECK(!user_read_buf_); |
| 809 // http://crbug.com/16371: We're seeing |buf->data()| return NULL. See if the |
| 810 // user is passing in an IOBuffer with a NULL |data_|. |
| 811 CHECK(buf); |
| 812 CHECK(buf->data()); |
| 813 user_read_buf_ = buf; |
| 814 user_read_buf_len_ = buf_len; |
| 815 |
| 816 int rv = DoPayloadRead(); |
| 817 if (rv == ERR_IO_PENDING) { |
| 818 old_user_read_callback_ = callback; |
| 819 } else { |
| 820 user_read_buf_ = NULL; |
| 821 user_read_buf_len_ = 0; |
| 822 } |
| 823 return rv; |
| 824 } |
| 825 int SSLClientSocketWin::Read(IOBuffer* buf, int buf_len, |
| 826 const CompletionCallback& callback) { |
| 827 DCHECK(completed_handshake()); |
| 828 DCHECK(!old_user_read_callback_ && user_read_callback_.is_null()); |
| 829 |
| 830 // If we have surplus decrypted plaintext, satisfy the Read with it without |
| 831 // reading more ciphertext from the transport socket. |
791 if (bytes_decrypted_ != 0) { | 832 if (bytes_decrypted_ != 0) { |
792 int len = std::min(buf_len, bytes_decrypted_); | 833 int len = std::min(buf_len, bytes_decrypted_); |
793 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, len, | 834 net_log_.AddByteTransferEvent(NetLog::TYPE_SSL_SOCKET_BYTES_RECEIVED, len, |
794 decrypted_ptr_); | 835 decrypted_ptr_); |
795 memcpy(buf->data(), decrypted_ptr_, len); | 836 memcpy(buf->data(), decrypted_ptr_, len); |
796 decrypted_ptr_ += len; | 837 decrypted_ptr_ += len; |
797 bytes_decrypted_ -= len; | 838 bytes_decrypted_ -= len; |
798 if (bytes_decrypted_ == 0) { | 839 if (bytes_decrypted_ == 0) { |
799 decrypted_ptr_ = NULL; | 840 decrypted_ptr_ = NULL; |
800 if (bytes_received_ != 0) { | 841 if (bytes_received_ != 0) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
857 void SSLClientSocketWin::OnHandshakeIOComplete(int result) { | 898 void SSLClientSocketWin::OnHandshakeIOComplete(int result) { |
858 int rv = DoLoop(result); | 899 int rv = DoLoop(result); |
859 | 900 |
860 // The SSL handshake has some round trips. We need to notify the caller of | 901 // The SSL handshake has some round trips. We need to notify the caller of |
861 // success or any error, other than waiting for IO. | 902 // success or any error, other than waiting for IO. |
862 if (rv != ERR_IO_PENDING) { | 903 if (rv != ERR_IO_PENDING) { |
863 // If there is no connect callback available to call, we are renegotiating | 904 // If there is no connect callback available to call, we are renegotiating |
864 // (which occurs because we are in the middle of a Read when the | 905 // (which occurs because we are in the middle of a Read when the |
865 // renegotiation process starts). So we complete the Read here. | 906 // renegotiation process starts). So we complete the Read here. |
866 if (!old_user_connect_callback_ && user_connect_callback_.is_null()) { | 907 if (!old_user_connect_callback_ && user_connect_callback_.is_null()) { |
867 OldCompletionCallback* c = user_read_callback_; | 908 if (old_user_read_callback_) { |
868 user_read_callback_ = NULL; | 909 OldCompletionCallback* c = old_user_read_callback_; |
869 user_read_buf_ = NULL; | 910 old_user_read_callback_ = NULL; |
870 user_read_buf_len_ = 0; | 911 user_read_buf_ = NULL; |
871 c->Run(rv); | 912 user_read_buf_len_ = 0; |
| 913 c->Run(rv); |
| 914 } else { |
| 915 CompletionCallback c = user_read_callback_; |
| 916 user_read_callback_.Reset(); |
| 917 user_read_buf_ = NULL; |
| 918 user_read_buf_len_ = 0; |
| 919 c.Run(rv); |
| 920 } |
872 return; | 921 return; |
873 } | 922 } |
874 net_log_.EndEvent(NetLog::TYPE_SSL_CONNECT, NULL); | 923 net_log_.EndEvent(NetLog::TYPE_SSL_CONNECT, NULL); |
875 if (old_user_connect_callback_) { | 924 if (old_user_connect_callback_) { |
876 OldCompletionCallback* c = old_user_connect_callback_; | 925 OldCompletionCallback* c = old_user_connect_callback_; |
877 old_user_connect_callback_ = NULL; | 926 old_user_connect_callback_ = NULL; |
878 c->Run(rv); | 927 c->Run(rv); |
879 } else { | 928 } else { |
880 CompletionCallback c = user_connect_callback_; | 929 CompletionCallback c = user_connect_callback_; |
881 user_connect_callback_.Reset(); | 930 user_connect_callback_.Reset(); |
882 c.Run(rv); | 931 c.Run(rv); |
883 } | 932 } |
884 } | 933 } |
885 } | 934 } |
886 | 935 |
887 void SSLClientSocketWin::OnReadComplete(int result) { | 936 void SSLClientSocketWin::OnReadComplete(int result) { |
888 DCHECK(completed_handshake()); | 937 DCHECK(completed_handshake()); |
889 | 938 |
890 result = DoPayloadReadComplete(result); | 939 result = DoPayloadReadComplete(result); |
891 if (result > 0) | 940 if (result > 0) |
892 result = DoPayloadDecrypt(); | 941 result = DoPayloadDecrypt(); |
893 if (result != ERR_IO_PENDING) { | 942 if (result != ERR_IO_PENDING) { |
894 DCHECK(user_read_callback_); | 943 DCHECK(old_user_read_callback_ || !user_read_callback_.is_null()); |
895 OldCompletionCallback* c = user_read_callback_; | 944 if (old_user_read_callback_) { |
896 user_read_callback_ = NULL; | 945 OldCompletionCallback* c = old_user_read_callback_; |
897 user_read_buf_ = NULL; | 946 old_user_read_callback_ = NULL; |
898 user_read_buf_len_ = 0; | 947 user_read_buf_ = NULL; |
899 c->Run(result); | 948 user_read_buf_len_ = 0; |
| 949 c->Run(result); |
| 950 } else { |
| 951 CompletionCallback c = user_read_callback_; |
| 952 user_read_callback_.Reset(); |
| 953 user_read_buf_ = NULL; |
| 954 user_read_buf_len_ = 0; |
| 955 c.Run(result); |
| 956 } |
900 } | 957 } |
901 } | 958 } |
902 | 959 |
903 void SSLClientSocketWin::OnWriteComplete(int result) { | 960 void SSLClientSocketWin::OnWriteComplete(int result) { |
904 DCHECK(completed_handshake()); | 961 DCHECK(completed_handshake()); |
905 | 962 |
906 int rv = DoPayloadWriteComplete(result); | 963 int rv = DoPayloadWriteComplete(result); |
907 if (rv != ERR_IO_PENDING) { | 964 if (rv != ERR_IO_PENDING) { |
908 DCHECK(user_write_callback_); | 965 DCHECK(user_write_callback_); |
909 OldCompletionCallback* c = user_write_callback_; | 966 OldCompletionCallback* c = user_write_callback_; |
(...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1570 next_state_ = STATE_VERIFY_CERT; | 1627 next_state_ = STATE_VERIFY_CERT; |
1571 } | 1628 } |
1572 CertFreeCertificateContext(server_cert_handle); | 1629 CertFreeCertificateContext(server_cert_handle); |
1573 return OK; | 1630 return OK; |
1574 } | 1631 } |
1575 | 1632 |
1576 // Called when a renegotiation is completed. |result| is the verification | 1633 // Called when a renegotiation is completed. |result| is the verification |
1577 // result of the server certificate received during renegotiation. | 1634 // result of the server certificate received during renegotiation. |
1578 void SSLClientSocketWin::DidCompleteRenegotiation() { | 1635 void SSLClientSocketWin::DidCompleteRenegotiation() { |
1579 DCHECK(!old_user_connect_callback_ && user_connect_callback_.is_null()); | 1636 DCHECK(!old_user_connect_callback_ && user_connect_callback_.is_null()); |
1580 DCHECK(user_read_callback_); | 1637 DCHECK(old_user_read_callback_ || !user_read_callback_.is_null()); |
1581 renegotiating_ = false; | 1638 renegotiating_ = false; |
1582 next_state_ = STATE_COMPLETED_RENEGOTIATION; | 1639 next_state_ = STATE_COMPLETED_RENEGOTIATION; |
1583 } | 1640 } |
1584 | 1641 |
1585 void SSLClientSocketWin::LogConnectionTypeMetrics() const { | 1642 void SSLClientSocketWin::LogConnectionTypeMetrics() const { |
1586 UpdateConnectionTypeHistograms(CONNECTION_SSL); | 1643 UpdateConnectionTypeHistograms(CONNECTION_SSL); |
1587 if (server_cert_verify_result_.has_md5) | 1644 if (server_cert_verify_result_.has_md5) |
1588 UpdateConnectionTypeHistograms(CONNECTION_SSL_MD5); | 1645 UpdateConnectionTypeHistograms(CONNECTION_SSL_MD5); |
1589 if (server_cert_verify_result_.has_md2) | 1646 if (server_cert_verify_result_.has_md2) |
1590 UpdateConnectionTypeHistograms(CONNECTION_SSL_MD2); | 1647 UpdateConnectionTypeHistograms(CONNECTION_SSL_MD2); |
1591 if (server_cert_verify_result_.has_md4) | 1648 if (server_cert_verify_result_.has_md4) |
1592 UpdateConnectionTypeHistograms(CONNECTION_SSL_MD4); | 1649 UpdateConnectionTypeHistograms(CONNECTION_SSL_MD4); |
1593 if (server_cert_verify_result_.has_md5_ca) | 1650 if (server_cert_verify_result_.has_md5_ca) |
1594 UpdateConnectionTypeHistograms(CONNECTION_SSL_MD5_CA); | 1651 UpdateConnectionTypeHistograms(CONNECTION_SSL_MD5_CA); |
1595 if (server_cert_verify_result_.has_md2_ca) | 1652 if (server_cert_verify_result_.has_md2_ca) |
1596 UpdateConnectionTypeHistograms(CONNECTION_SSL_MD2_CA); | 1653 UpdateConnectionTypeHistograms(CONNECTION_SSL_MD2_CA); |
1597 } | 1654 } |
1598 | 1655 |
1599 void SSLClientSocketWin::FreeSendBuffer() { | 1656 void SSLClientSocketWin::FreeSendBuffer() { |
1600 SECURITY_STATUS status = FreeContextBuffer(send_buffer_.pvBuffer); | 1657 SECURITY_STATUS status = FreeContextBuffer(send_buffer_.pvBuffer); |
1601 DCHECK(status == SEC_E_OK); | 1658 DCHECK(status == SEC_E_OK); |
1602 memset(&send_buffer_, 0, sizeof(send_buffer_)); | 1659 memset(&send_buffer_, 0, sizeof(send_buffer_)); |
1603 } | 1660 } |
1604 | 1661 |
1605 } // namespace net | 1662 } // namespace net |
OLD | NEW |