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