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 #include "net/socket/ssl_client_socket.h" | 5 #include "net/socket/ssl_client_socket.h" |
6 | 6 |
7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
351 // underlying transport until UnblockWrite() has been called. Note: if there | 351 // underlying transport until UnblockWrite() has been called. Note: if there |
352 // is a pending asynchronous write, it is NOT blocked. For purposes of | 352 // is a pending asynchronous write, it is NOT blocked. For purposes of |
353 // blocking writes, data is considered to have reached the underlying | 353 // blocking writes, data is considered to have reached the underlying |
354 // transport as soon as Write() is called. | 354 // transport as soon as Write() is called. |
355 void BlockWrite(); | 355 void BlockWrite(); |
356 void UnblockWrite(); | 356 void UnblockWrite(); |
357 | 357 |
358 // Waits for the blocked Write() call to be scheduled. | 358 // Waits for the blocked Write() call to be scheduled. |
359 void WaitForWrite(); | 359 void WaitForWrite(); |
360 | 360 |
361 // Returns the wrapped stream socket. | |
362 StreamSocket* transport() { return transport_.get(); } | |
363 | |
361 private: | 364 private: |
362 // Handles completion from the underlying transport read. | 365 // Handles completion from the underlying transport read. |
363 void OnReadCompleted(int result); | 366 void OnReadCompleted(int result); |
364 | 367 |
365 // True if read callbacks are blocked. | 368 // True if read callbacks are blocked. |
366 bool should_block_read_; | 369 bool should_block_read_; |
367 | 370 |
368 // The user callback for the pending read call. | 371 // The user callback for the pending read call. |
369 CompletionCallback pending_read_callback_; | 372 CompletionCallback pending_read_callback_; |
370 | 373 |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
789 sock->Disconnect(); | 792 sock->Disconnect(); |
790 EXPECT_FALSE(sock->IsConnected()); | 793 EXPECT_FALSE(sock->IsConnected()); |
791 EXPECT_TRUE( | 794 EXPECT_TRUE( |
792 test_server.host_port_pair().Equals(request_info->host_and_port)); | 795 test_server.host_port_pair().Equals(request_info->host_and_port)); |
793 | 796 |
794 return request_info; | 797 return request_info; |
795 } | 798 } |
796 }; | 799 }; |
797 | 800 |
798 class SSLClientSocketFalseStartTest : public SSLClientSocketTest { | 801 class SSLClientSocketFalseStartTest : public SSLClientSocketTest { |
802 public: | |
803 SSLClientSocketFalseStartTest() | |
804 : monitor_handshake_callback_(false), expect_false_start_error_(false) {} | |
805 | |
799 protected: | 806 protected: |
800 // Creates an SSLClientSocket with |client_config| attached to a | 807 // Creates an SSLClientSocket with |client_config| attached to a |
801 // FakeBlockingStreamSocket, returning both in |*out_raw_transport| and | 808 // FakeBlockingStreamSocket, returning both in |*out_raw_transport| and |
802 // |*out_sock|. The FakeBlockingStreamSocket is owned by the SSLClientSocket, | 809 // |*out_sock|. The FakeBlockingStreamSocket is owned by the SSLClientSocket, |
803 // so |*out_raw_transport| is a raw pointer. | 810 // so |*out_raw_transport| is a raw pointer. |
804 // | 811 // |
805 // The client socket will begin a connect using |callback| but stop before the | 812 // The client socket will begin a connect using |callback| but stop before the |
806 // server's finished message is received. The finished message will be blocked | 813 // server's finished message is received. The finished message will be blocked |
807 // in |*out_raw_transport|. To complete the handshake and successfully read | 814 // in |*out_raw_transport|. To complete the handshake and successfully read |
808 // data, the caller must unblock reads on |*out_raw_transport|. (Note that, if | 815 // data, the caller must unblock reads on |*out_raw_transport|. (Note that, if |
809 // the client successfully false started, |callback.WaitForResult()| will | 816 // the client successfully false started, |callback.WaitForResult()| will |
810 // return OK without unblocking transport reads. But Read() will still block.) | 817 // return OK without unblocking transport reads. But Read() will still block.) |
811 // | 818 // |
812 // Must be called after StartTestServer is called. | 819 // Must be called after StartTestServer is called. |
813 void CreateAndConnectUntilServerFinishedReceived( | 820 void CreateAndConnectUntilServerFinishedReceived( |
814 const SSLConfig& client_config, | 821 const SSLConfig& client_config, |
815 TestCompletionCallback* callback, | 822 TestCompletionCallback* callback, |
816 FakeBlockingStreamSocket** out_raw_transport, | 823 FakeBlockingStreamSocket** out_raw_transport, |
824 scoped_ptr<StreamSocket> real_transport, | |
davidben
2014/08/07 21:24:19
Nit: Switch the order of out_raw_transport and rea
| |
817 scoped_ptr<SSLClientSocket>* out_sock) { | 825 scoped_ptr<SSLClientSocket>* out_sock) { |
818 CHECK(test_server()); | 826 CHECK(test_server()); |
819 | 827 |
820 scoped_ptr<StreamSocket> real_transport( | |
821 new TCPClientSocket(addr(), NULL, NetLog::Source())); | |
822 scoped_ptr<FakeBlockingStreamSocket> transport( | 828 scoped_ptr<FakeBlockingStreamSocket> transport( |
823 new FakeBlockingStreamSocket(real_transport.Pass())); | 829 new FakeBlockingStreamSocket(real_transport.Pass())); |
824 int rv = callback->GetResult(transport->Connect(callback->callback())); | 830 int rv = callback->GetResult(transport->Connect(callback->callback())); |
825 EXPECT_EQ(OK, rv); | 831 EXPECT_EQ(OK, rv); |
826 | 832 |
827 FakeBlockingStreamSocket* raw_transport = transport.get(); | 833 FakeBlockingStreamSocket* raw_transport = transport.get(); |
828 scoped_ptr<SSLClientSocket> sock = | 834 scoped_ptr<SSLClientSocket> sock = |
829 CreateSSLClientSocket(transport.PassAs<StreamSocket>(), | 835 CreateSSLClientSocket(transport.PassAs<StreamSocket>(), |
830 test_server()->host_port_pair(), | 836 test_server()->host_port_pair(), |
831 client_config); | 837 client_config); |
832 | 838 |
839 if (monitor_handshake_callback_) { | |
840 sock->SetHandshakeCompletionCallback( | |
841 base::Bind(&SSLClientSocketTest::RecordCompletedHandshake, | |
842 base::Unretained(this))); | |
843 } | |
844 | |
833 // Connect. Stop before the client processes the first server leg | 845 // Connect. Stop before the client processes the first server leg |
834 // (ServerHello, etc.) | 846 // (ServerHello, etc.) |
835 raw_transport->BlockReadResult(); | 847 raw_transport->BlockReadResult(); |
836 rv = sock->Connect(callback->callback()); | 848 rv = sock->Connect(callback->callback()); |
837 EXPECT_EQ(ERR_IO_PENDING, rv); | 849 EXPECT_EQ(ERR_IO_PENDING, rv); |
838 raw_transport->WaitForReadResult(); | 850 raw_transport->WaitForReadResult(); |
839 | 851 |
840 // Release the ServerHello and wait for the client to write | 852 // Release the ServerHello and wait for the client to write |
841 // ClientKeyExchange, etc. (A proxy for waiting for the entirety of the | 853 // ClientKeyExchange, etc. (A proxy for waiting for the entirety of the |
842 // server's leg to complete, since it may span multiple reads.) | 854 // server's leg to complete, since it may span multiple reads.) |
843 EXPECT_FALSE(callback->have_result()); | 855 EXPECT_FALSE(callback->have_result()); |
844 raw_transport->BlockWrite(); | 856 raw_transport->BlockWrite(); |
845 raw_transport->UnblockReadResult(); | 857 raw_transport->UnblockReadResult(); |
846 raw_transport->WaitForWrite(); | 858 raw_transport->WaitForWrite(); |
847 | 859 |
860 if (expect_false_start_error_) { | |
davidben
2014/08/07 21:24:19
So this is a little odd in that, if |expect_false_
| |
861 SynchronousErrorStreamSocket* error_socket = | |
862 static_cast<SynchronousErrorStreamSocket*>( | |
863 raw_transport->transport()); | |
864 error_socket->SetNextReadError(ERR_CONNECTION_RESET); | |
865 } | |
848 // And, finally, release that and block the next server leg | 866 // And, finally, release that and block the next server leg |
849 // (ChangeCipherSpec, Finished). | 867 // (ChangeCipherSpec, Finished). |
850 raw_transport->BlockReadResult(); | 868 raw_transport->BlockReadResult(); |
851 raw_transport->UnblockWrite(); | 869 raw_transport->UnblockWrite(); |
852 | 870 |
853 *out_raw_transport = raw_transport; | 871 *out_raw_transport = raw_transport; |
854 *out_sock = sock.Pass(); | 872 *out_sock = sock.Pass(); |
855 } | 873 } |
856 | 874 |
857 void TestFalseStart(const SpawnedTestServer::SSLOptions& server_options, | 875 void TestFalseStart(const SpawnedTestServer::SSLOptions& server_options, |
858 const SSLConfig& client_config, | 876 const SSLConfig& client_config, |
859 bool expect_false_start) { | 877 bool expect_false_start) { |
860 ASSERT_TRUE(StartTestServer(server_options)); | 878 ASSERT_TRUE(StartTestServer(server_options)); |
861 | 879 |
862 TestCompletionCallback callback; | 880 TestCompletionCallback callback; |
863 FakeBlockingStreamSocket* raw_transport = NULL; | 881 FakeBlockingStreamSocket* raw_transport = NULL; |
864 scoped_ptr<SSLClientSocket> sock; | 882 scoped_ptr<SSLClientSocket> sock; |
865 ASSERT_NO_FATAL_FAILURE(CreateAndConnectUntilServerFinishedReceived( | 883 scoped_ptr<StreamSocket> real_transport = scoped_ptr<StreamSocket>( |
866 client_config, &callback, &raw_transport, &sock)); | 884 new TCPClientSocket(addr(), NULL, NetLog::Source())); |
885 if (expect_false_start_error_) | |
886 real_transport.reset( | |
887 new SynchronousErrorStreamSocket(real_transport.Pass())); | |
888 | |
889 ASSERT_NO_FATAL_FAILURE( | |
890 CreateAndConnectUntilServerFinishedReceived(client_config, | |
891 &callback, | |
892 &raw_transport, | |
893 real_transport.Pass(), | |
894 &sock)); | |
867 | 895 |
868 if (expect_false_start) { | 896 if (expect_false_start) { |
869 // When False Starting, the handshake should complete before receiving the | 897 // When False Starting, the handshake should complete before receiving the |
870 // Change Cipher Spec and Finished messages. | 898 // Change Cipher Spec and Finished messages. |
871 // | 899 // |
872 // Note: callback.have_result() may not be true without waiting. The NSS | 900 // Note: callback.have_result() may not be true without waiting. The NSS |
873 // state machine sometimes lives on a separate thread, so this thread may | 901 // state machine sometimes lives on a separate thread, so this thread may |
874 // not yet have processed the signal that the handshake has completed. | 902 // not yet have processed the signal that the handshake has completed. |
875 int rv = callback.WaitForResult(); | 903 int rv = callback.WaitForResult(); |
876 EXPECT_EQ(OK, rv); | 904 EXPECT_EQ(OK, rv); |
(...skipping 12 matching lines...) Expand all Loading... | |
889 EXPECT_EQ(kRequestTextSize, rv); | 917 EXPECT_EQ(kRequestTextSize, rv); |
890 | 918 |
891 // The read will hang; it's waiting for the peer to complete the | 919 // The read will hang; it's waiting for the peer to complete the |
892 // handshake, and the handshake is still blocked. | 920 // handshake, and the handshake is still blocked. |
893 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); | 921 scoped_refptr<IOBuffer> buf(new IOBuffer(4096)); |
894 rv = sock->Read(buf.get(), 4096, callback.callback()); | 922 rv = sock->Read(buf.get(), 4096, callback.callback()); |
895 | 923 |
896 // After releasing reads, the connection proceeds. | 924 // After releasing reads, the connection proceeds. |
897 raw_transport->UnblockReadResult(); | 925 raw_transport->UnblockReadResult(); |
898 rv = callback.GetResult(rv); | 926 rv = callback.GetResult(rv); |
899 EXPECT_LT(0, rv); | 927 if (expect_false_start_error_) |
928 EXPECT_EQ(ERR_CONNECTION_RESET, rv); | |
929 else | |
930 EXPECT_LT(0, rv); | |
900 } else { | 931 } else { |
901 // False Start is not enabled, so the handshake will not complete because | 932 // False Start is not enabled, so the handshake will not complete because |
902 // the server second leg is blocked. | 933 // the server second leg is blocked. |
903 base::RunLoop().RunUntilIdle(); | 934 base::RunLoop().RunUntilIdle(); |
904 EXPECT_FALSE(callback.have_result()); | 935 EXPECT_FALSE(callback.have_result()); |
905 } | 936 } |
906 } | 937 } |
938 | |
939 // Indicates that the socket's handshake completion callback should | |
940 // be monitored. | |
941 bool monitor_handshake_callback_; | |
942 // Indicates that this test's handshake should fail after the client | |
943 // "finished" message is sent. | |
944 bool expect_false_start_error_; | |
907 }; | 945 }; |
908 | 946 |
909 class SSLClientSocketChannelIDTest : public SSLClientSocketTest { | 947 class SSLClientSocketChannelIDTest : public SSLClientSocketTest { |
910 protected: | 948 protected: |
911 void EnableChannelID() { | 949 void EnableChannelID() { |
912 channel_id_service_.reset( | 950 channel_id_service_.reset( |
913 new ChannelIDService(new DefaultChannelIDStore(NULL), | 951 new ChannelIDService(new DefaultChannelIDStore(NULL), |
914 base::MessageLoopProxy::current())); | 952 base::MessageLoopProxy::current())); |
915 context_.channel_id_service = channel_id_service_.get(); | 953 context_.channel_id_service = channel_id_service_.get(); |
916 } | 954 } |
(...skipping 1846 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2763 | 2801 |
2764 if (sock->IsConnected()) | 2802 if (sock->IsConnected()) |
2765 LOG(ERROR) << "SSL Socket prematurely connected"; | 2803 LOG(ERROR) << "SSL Socket prematurely connected"; |
2766 | 2804 |
2767 rv = callback.GetResult(sock->Connect(callback.callback())); | 2805 rv = callback.GetResult(sock->Connect(callback.callback())); |
2768 | 2806 |
2769 EXPECT_EQ(OK, rv); | 2807 EXPECT_EQ(OK, rv); |
2770 EXPECT_TRUE(sock->IsConnected()); | 2808 EXPECT_TRUE(sock->IsConnected()); |
2771 EXPECT_TRUE(ran_handshake_completion_callback_); | 2809 EXPECT_TRUE(ran_handshake_completion_callback_); |
2772 } | 2810 } |
2811 | |
2812 TEST_F(SSLClientSocketFalseStartTest, | |
2813 HandshakeCallbackIsRun_WithFalseStartFailure) { | |
2814 // False Start requires NPN and a forward-secret cipher suite. | |
2815 SpawnedTestServer::SSLOptions server_options; | |
2816 server_options.key_exchanges = | |
2817 SpawnedTestServer::SSLOptions::KEY_EXCHANGE_DHE_RSA; | |
2818 server_options.enable_npn = true; | |
2819 SSLConfig client_config; | |
2820 client_config.next_protos.push_back("http/1.1"); | |
2821 monitor_handshake_callback_ = true; | |
2822 expect_false_start_error_ = true; | |
2823 ASSERT_NO_FATAL_FAILURE(TestFalseStart(server_options, client_config, true)); | |
2824 ASSERT_TRUE(ran_handshake_completion_callback_); | |
2825 } | |
2826 | |
2827 TEST_F(SSLClientSocketFalseStartTest, | |
2828 HandshakeCallbackIsRun_WithFalseStartSuccess) { | |
2829 // False Start requires NPN and a forward-secret cipher suite. | |
2830 SpawnedTestServer::SSLOptions server_options; | |
2831 server_options.key_exchanges = | |
2832 SpawnedTestServer::SSLOptions::KEY_EXCHANGE_DHE_RSA; | |
2833 server_options.enable_npn = true; | |
2834 SSLConfig client_config; | |
2835 client_config.next_protos.push_back("http/1.1"); | |
2836 monitor_handshake_callback_ = true; | |
2837 ASSERT_NO_FATAL_FAILURE(TestFalseStart(server_options, client_config, true)); | |
2838 ASSERT_TRUE(ran_handshake_completion_callback_); | |
2839 } | |
2773 #endif // defined(USE_OPENSSL) | 2840 #endif // defined(USE_OPENSSL) |
2774 | 2841 |
2775 TEST_F(SSLClientSocketFalseStartTest, FalseStartEnabled) { | 2842 TEST_F(SSLClientSocketFalseStartTest, FalseStartEnabled) { |
2776 // False Start requires NPN and a forward-secret cipher suite. | 2843 // False Start requires NPN and a forward-secret cipher suite. |
2777 SpawnedTestServer::SSLOptions server_options; | 2844 SpawnedTestServer::SSLOptions server_options; |
2778 server_options.key_exchanges = | 2845 server_options.key_exchanges = |
2779 SpawnedTestServer::SSLOptions::KEY_EXCHANGE_DHE_RSA; | 2846 SpawnedTestServer::SSLOptions::KEY_EXCHANGE_DHE_RSA; |
2780 server_options.enable_npn = true; | 2847 server_options.enable_npn = true; |
2781 SSLConfig client_config; | 2848 SSLConfig client_config; |
2782 client_config.next_protos.push_back("http/1.1"); | 2849 client_config.next_protos.push_back("http/1.1"); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2847 server_options.enable_npn = true; | 2914 server_options.enable_npn = true; |
2848 ASSERT_TRUE(StartTestServer(server_options)); | 2915 ASSERT_TRUE(StartTestServer(server_options)); |
2849 | 2916 |
2850 SSLConfig client_config; | 2917 SSLConfig client_config; |
2851 client_config.next_protos.push_back("http/1.1"); | 2918 client_config.next_protos.push_back("http/1.1"); |
2852 | 2919 |
2853 // Start a handshake up to the server Finished message. | 2920 // Start a handshake up to the server Finished message. |
2854 TestCompletionCallback callback; | 2921 TestCompletionCallback callback; |
2855 FakeBlockingStreamSocket* raw_transport1; | 2922 FakeBlockingStreamSocket* raw_transport1; |
2856 scoped_ptr<SSLClientSocket> sock1; | 2923 scoped_ptr<SSLClientSocket> sock1; |
2857 ASSERT_NO_FATAL_FAILURE(CreateAndConnectUntilServerFinishedReceived( | 2924 scoped_ptr<StreamSocket> real_transport( |
2858 client_config, &callback, &raw_transport1, &sock1)); | 2925 new TCPClientSocket(addr(), NULL, NetLog::Source())); |
2926 ASSERT_NO_FATAL_FAILURE( | |
2927 CreateAndConnectUntilServerFinishedReceived(client_config, | |
2928 &callback, | |
2929 &raw_transport1, | |
2930 real_transport.Pass(), | |
2931 &sock1)); | |
2859 // Although raw_transport1 has the server Finished blocked, the handshake | 2932 // Although raw_transport1 has the server Finished blocked, the handshake |
2860 // still completes. | 2933 // still completes. |
2861 EXPECT_EQ(OK, callback.WaitForResult()); | 2934 EXPECT_EQ(OK, callback.WaitForResult()); |
2862 | 2935 |
2863 // Drop the old socket. This is needed because the Python test server can't | 2936 // Drop the old socket. This is needed because the Python test server can't |
2864 // service two sockets in parallel. | 2937 // service two sockets in parallel. |
2865 sock1.reset(); | 2938 sock1.reset(); |
2866 | 2939 |
2867 // Start a second connection. | 2940 // Start a second connection. |
2868 scoped_ptr<StreamSocket> transport2( | 2941 scoped_ptr<StreamSocket> transport2( |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2934 ssl_config.channel_id_enabled = true; | 3007 ssl_config.channel_id_enabled = true; |
2935 | 3008 |
2936 int rv; | 3009 int rv; |
2937 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); | 3010 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); |
2938 | 3011 |
2939 EXPECT_EQ(ERR_UNEXPECTED, rv); | 3012 EXPECT_EQ(ERR_UNEXPECTED, rv); |
2940 EXPECT_FALSE(sock_->IsConnected()); | 3013 EXPECT_FALSE(sock_->IsConnected()); |
2941 } | 3014 } |
2942 | 3015 |
2943 } // namespace net | 3016 } // namespace net |
OLD | NEW |