| 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 2181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2192 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket( | 2192 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
| 2193 transport.Pass(), test_server.host_port_pair(), SSLConfig())); | 2193 transport.Pass(), test_server.host_port_pair(), SSLConfig())); |
| 2194 | 2194 |
| 2195 rv = sock->Connect(callback.callback()); | 2195 rv = sock->Connect(callback.callback()); |
| 2196 if (rv == ERR_IO_PENDING) | 2196 if (rv == ERR_IO_PENDING) |
| 2197 rv = callback.WaitForResult(); | 2197 rv = callback.WaitForResult(); |
| 2198 EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, rv); | 2198 EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, rv); |
| 2199 } | 2199 } |
| 2200 | 2200 |
| 2201 TEST_F(SSLClientSocketTest, CipherSuiteDisables) { | 2201 TEST_F(SSLClientSocketTest, CipherSuiteDisables) { |
| 2202 // Rather than exhaustively disabling every RC4 ciphersuite defined at | 2202 // Rather than exhaustively disabling every AES_128_CBC ciphersuite defined at |
| 2203 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml, | 2203 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml, only |
| 2204 // only disabling those cipher suites that the test server actually | 2204 // disabling those cipher suites that the test server actually implements. |
| 2205 // implements. | |
| 2206 const uint16 kCiphersToDisable[] = { | 2205 const uint16 kCiphersToDisable[] = { |
| 2207 0x0005, // TLS_RSA_WITH_RC4_128_SHA | 2206 0x002f, // TLS_RSA_WITH_AES_128_CBC_SHA |
| 2208 0xc011, // TLS_ECDHE_RSA_WITH_RC4_128_SHA | 2207 0x0033, // TLS_DHE_RSA_WITH_AES_128_CBC_SHA |
| 2208 0xc013, // TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA |
| 2209 }; | 2209 }; |
| 2210 | 2210 |
| 2211 SpawnedTestServer::SSLOptions ssl_options; | 2211 SpawnedTestServer::SSLOptions ssl_options; |
| 2212 // Enable only RC4 on the test server. | 2212 // Enable only AES_128_CBC on the test server. |
| 2213 ssl_options.bulk_ciphers = SpawnedTestServer::SSLOptions::BULK_CIPHER_RC4; | 2213 ssl_options.bulk_ciphers = SpawnedTestServer::SSLOptions::BULK_CIPHER_AES128; |
| 2214 SpawnedTestServer test_server( | 2214 SpawnedTestServer test_server( |
| 2215 SpawnedTestServer::TYPE_HTTPS, ssl_options, base::FilePath()); | 2215 SpawnedTestServer::TYPE_HTTPS, ssl_options, base::FilePath()); |
| 2216 ASSERT_TRUE(test_server.Start()); | 2216 ASSERT_TRUE(test_server.Start()); |
| 2217 | 2217 |
| 2218 AddressList addr; | 2218 AddressList addr; |
| 2219 ASSERT_TRUE(test_server.GetAddressList(&addr)); | 2219 ASSERT_TRUE(test_server.GetAddressList(&addr)); |
| 2220 | 2220 |
| 2221 TestCompletionCallback callback; | 2221 TestCompletionCallback callback; |
| 2222 CapturingNetLog log; | 2222 CapturingNetLog log; |
| 2223 scoped_ptr<StreamSocket> transport( | 2223 scoped_ptr<StreamSocket> transport( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 2234 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket( | 2234 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
| 2235 transport.Pass(), test_server.host_port_pair(), ssl_config)); | 2235 transport.Pass(), test_server.host_port_pair(), ssl_config)); |
| 2236 | 2236 |
| 2237 EXPECT_FALSE(sock->IsConnected()); | 2237 EXPECT_FALSE(sock->IsConnected()); |
| 2238 | 2238 |
| 2239 rv = sock->Connect(callback.callback()); | 2239 rv = sock->Connect(callback.callback()); |
| 2240 CapturingNetLog::CapturedEntryList entries; | 2240 CapturingNetLog::CapturedEntryList entries; |
| 2241 log.GetEntries(&entries); | 2241 log.GetEntries(&entries); |
| 2242 EXPECT_TRUE(LogContainsBeginEvent(entries, 5, NetLog::TYPE_SSL_CONNECT)); | 2242 EXPECT_TRUE(LogContainsBeginEvent(entries, 5, NetLog::TYPE_SSL_CONNECT)); |
| 2243 | 2243 |
| 2244 // NSS has special handling that maps a handshake_failure alert received | |
| 2245 // immediately after a client_hello to be a mismatched cipher suite error, | |
| 2246 // leading to ERR_SSL_VERSION_OR_CIPHER_MISMATCH. When using OpenSSL or | |
| 2247 // Secure Transport (OS X), the handshake_failure is bubbled up without any | |
| 2248 // interpretation, leading to ERR_SSL_PROTOCOL_ERROR. Either way, a failure | |
| 2249 // indicates that no cipher suite was negotiated with the test server. | |
| 2250 if (rv == ERR_IO_PENDING) | 2244 if (rv == ERR_IO_PENDING) |
| 2251 rv = callback.WaitForResult(); | 2245 rv = callback.WaitForResult(); |
| 2252 EXPECT_TRUE(rv == ERR_SSL_VERSION_OR_CIPHER_MISMATCH || | 2246 EXPECT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, rv); |
| 2253 rv == ERR_SSL_PROTOCOL_ERROR); | 2247 // The exact ordering depends no whether an extra read is issued. Just check |
| 2254 // The exact ordering differs between SSLClientSocketNSS (which issues an | 2248 // the error is somewhere in the log. |
| 2255 // extra read) and SSLClientSocketMac (which does not). Just make sure the | |
| 2256 // error appears somewhere in the log. | |
| 2257 log.GetEntries(&entries); | 2249 log.GetEntries(&entries); |
| 2258 ExpectLogContainsSomewhere( | 2250 ExpectLogContainsSomewhere( |
| 2259 entries, 0, NetLog::TYPE_SSL_HANDSHAKE_ERROR, NetLog::PHASE_NONE); | 2251 entries, 0, NetLog::TYPE_SSL_HANDSHAKE_ERROR, NetLog::PHASE_NONE); |
| 2260 | 2252 |
| 2261 // We cannot test sock->IsConnected(), as the NSS implementation disconnects | 2253 // We cannot test sock->IsConnected(), as the NSS implementation disconnects |
| 2262 // the socket when it encounters an error, whereas other implementations | 2254 // the socket when it encounters an error, whereas other implementations |
| 2263 // leave it connected. | 2255 // leave it connected. |
| 2264 // Because this an error that the test server is mutually aware of, as opposed | 2256 // Because this an error that the test server is mutually aware of, as opposed |
| 2265 // to being an error such as a certificate name mismatch, which is | 2257 // to being an error such as a certificate name mismatch, which is |
| 2266 // client-only, the exact index of the SSL connect end depends on how | 2258 // client-only, the exact index of the SSL connect end depends on how |
| (...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2870 EXPECT_EQ(OK, callback.GetResult(transport->Connect(callback.callback()))); | 2862 EXPECT_EQ(OK, callback.GetResult(transport->Connect(callback.callback()))); |
| 2871 sock = CreateSSLClientSocket( | 2863 sock = CreateSSLClientSocket( |
| 2872 transport.Pass(), test_server()->host_port_pair(), fallback_ssl_config); | 2864 transport.Pass(), test_server()->host_port_pair(), fallback_ssl_config); |
| 2873 EXPECT_EQ(OK, callback.GetResult(sock->Connect(callback.callback()))); | 2865 EXPECT_EQ(OK, callback.GetResult(sock->Connect(callback.callback()))); |
| 2874 EXPECT_TRUE(sock->GetSSLInfo(&ssl_info)); | 2866 EXPECT_TRUE(sock->GetSSLInfo(&ssl_info)); |
| 2875 EXPECT_EQ(SSLInfo::HANDSHAKE_RESUME, ssl_info.handshake_type); | 2867 EXPECT_EQ(SSLInfo::HANDSHAKE_RESUME, ssl_info.handshake_type); |
| 2876 EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1, | 2868 EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1, |
| 2877 SSLConnectionStatusToVersion(ssl_info.connection_status)); | 2869 SSLConnectionStatusToVersion(ssl_info.connection_status)); |
| 2878 } | 2870 } |
| 2879 | 2871 |
| 2872 // Test that RC4 is only enabled if enable_deprecated_cipher_suites is set. |
| 2873 TEST_F(SSLClientSocketTest, DeprecatedRC4) { |
| 2874 SpawnedTestServer::SSLOptions ssl_options; |
| 2875 ssl_options.bulk_ciphers = SpawnedTestServer::SSLOptions::BULK_CIPHER_RC4; |
| 2876 ASSERT_TRUE(StartTestServer(ssl_options)); |
| 2877 |
| 2878 // Normal handshakes with RC4 do not work. |
| 2879 SSLConfig ssl_config; |
| 2880 TestCompletionCallback callback; |
| 2881 scoped_ptr<StreamSocket> transport( |
| 2882 new TCPClientSocket(addr(), &log_, NetLog::Source())); |
| 2883 ASSERT_EQ(OK, callback.GetResult(transport->Connect(callback.callback()))); |
| 2884 scoped_ptr<SSLClientSocket> sock(CreateSSLClientSocket( |
| 2885 transport.Pass(), test_server()->host_port_pair(), ssl_config)); |
| 2886 ASSERT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, |
| 2887 callback.GetResult(sock->Connect(callback.callback()))); |
| 2888 |
| 2889 // Enabling deprecated ciphers works fine. |
| 2890 ssl_config.enable_deprecated_cipher_suites = true; |
| 2891 transport.reset(new TCPClientSocket(addr(), &log_, NetLog::Source())); |
| 2892 ASSERT_EQ(OK, callback.GetResult(transport->Connect(callback.callback()))); |
| 2893 sock = CreateSSLClientSocket(transport.Pass(), |
| 2894 test_server()->host_port_pair(), ssl_config); |
| 2895 ASSERT_EQ(OK, callback.GetResult(sock->Connect(callback.callback()))); |
| 2896 } |
| 2897 |
| 2898 // Tests that enabling deprecated ciphers shards the session cache. |
| 2899 TEST_F(SSLClientSocketTest, DeprecatedShardSessionCache) { |
| 2900 SpawnedTestServer::SSLOptions ssl_options; |
| 2901 ASSERT_TRUE(StartTestServer(ssl_options)); |
| 2902 |
| 2903 // Prepare a normal and deprecated SSL config. |
| 2904 SSLConfig ssl_config; |
| 2905 SSLConfig deprecated_ssl_config; |
| 2906 deprecated_ssl_config.enable_deprecated_cipher_suites = true; |
| 2907 |
| 2908 // Connect with deprecated ciphers enabled to warm the session cache cache. |
| 2909 TestCompletionCallback callback; |
| 2910 scoped_ptr<StreamSocket> transport( |
| 2911 new TCPClientSocket(addr(), &log_, NetLog::Source())); |
| 2912 EXPECT_EQ(OK, callback.GetResult(transport->Connect(callback.callback()))); |
| 2913 scoped_ptr<SSLClientSocket> sock( |
| 2914 CreateSSLClientSocket(transport.Pass(), test_server()->host_port_pair(), |
| 2915 deprecated_ssl_config)); |
| 2916 EXPECT_EQ(OK, callback.GetResult(sock->Connect(callback.callback()))); |
| 2917 SSLInfo ssl_info; |
| 2918 EXPECT_TRUE(sock->GetSSLInfo(&ssl_info)); |
| 2919 EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, ssl_info.handshake_type); |
| 2920 |
| 2921 // Test that re-connecting with deprecated ciphers enabled still resumes. |
| 2922 transport.reset(new TCPClientSocket(addr(), &log_, NetLog::Source())); |
| 2923 EXPECT_EQ(OK, callback.GetResult(transport->Connect(callback.callback()))); |
| 2924 sock = CreateSSLClientSocket( |
| 2925 transport.Pass(), test_server()->host_port_pair(), deprecated_ssl_config); |
| 2926 EXPECT_EQ(OK, callback.GetResult(sock->Connect(callback.callback()))); |
| 2927 EXPECT_TRUE(sock->GetSSLInfo(&ssl_info)); |
| 2928 EXPECT_EQ(SSLInfo::HANDSHAKE_RESUME, ssl_info.handshake_type); |
| 2929 |
| 2930 // However, a normal connection needs a full handshake. |
| 2931 transport.reset(new TCPClientSocket(addr(), &log_, NetLog::Source())); |
| 2932 EXPECT_EQ(OK, callback.GetResult(transport->Connect(callback.callback()))); |
| 2933 sock = CreateSSLClientSocket(transport.Pass(), |
| 2934 test_server()->host_port_pair(), ssl_config); |
| 2935 EXPECT_EQ(OK, callback.GetResult(sock->Connect(callback.callback()))); |
| 2936 EXPECT_TRUE(sock->GetSSLInfo(&ssl_info)); |
| 2937 EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, ssl_info.handshake_type); |
| 2938 |
| 2939 // Clear the session cache for the inverse test. |
| 2940 SSLClientSocket::ClearSessionCache(); |
| 2941 |
| 2942 // Now make a normal connection to prime the session cache. |
| 2943 transport.reset(new TCPClientSocket(addr(), &log_, NetLog::Source())); |
| 2944 EXPECT_EQ(OK, callback.GetResult(transport->Connect(callback.callback()))); |
| 2945 sock = CreateSSLClientSocket(transport.Pass(), |
| 2946 test_server()->host_port_pair(), ssl_config); |
| 2947 EXPECT_EQ(OK, callback.GetResult(sock->Connect(callback.callback()))); |
| 2948 EXPECT_TRUE(sock->GetSSLInfo(&ssl_info)); |
| 2949 EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, ssl_info.handshake_type); |
| 2950 |
| 2951 // A normal connection should be able to resume. |
| 2952 transport.reset(new TCPClientSocket(addr(), &log_, NetLog::Source())); |
| 2953 EXPECT_EQ(OK, callback.GetResult(transport->Connect(callback.callback()))); |
| 2954 sock = CreateSSLClientSocket(transport.Pass(), |
| 2955 test_server()->host_port_pair(), ssl_config); |
| 2956 EXPECT_EQ(OK, callback.GetResult(sock->Connect(callback.callback()))); |
| 2957 EXPECT_TRUE(sock->GetSSLInfo(&ssl_info)); |
| 2958 EXPECT_EQ(SSLInfo::HANDSHAKE_RESUME, ssl_info.handshake_type); |
| 2959 |
| 2960 // However, enabling deprecated ciphers connects fresh. |
| 2961 transport.reset(new TCPClientSocket(addr(), &log_, NetLog::Source())); |
| 2962 EXPECT_EQ(OK, callback.GetResult(transport->Connect(callback.callback()))); |
| 2963 sock = CreateSSLClientSocket( |
| 2964 transport.Pass(), test_server()->host_port_pair(), deprecated_ssl_config); |
| 2965 EXPECT_EQ(OK, callback.GetResult(sock->Connect(callback.callback()))); |
| 2966 EXPECT_TRUE(sock->GetSSLInfo(&ssl_info)); |
| 2967 EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, ssl_info.handshake_type); |
| 2968 } |
| 2969 |
| 2880 TEST_F(SSLClientSocketFalseStartTest, FalseStartEnabled) { | 2970 TEST_F(SSLClientSocketFalseStartTest, FalseStartEnabled) { |
| 2881 if (!SupportsAESGCM()) { | 2971 if (!SupportsAESGCM()) { |
| 2882 LOG(WARNING) << "Skipping test because AES-GCM is not supported."; | 2972 LOG(WARNING) << "Skipping test because AES-GCM is not supported."; |
| 2883 return; | 2973 return; |
| 2884 } | 2974 } |
| 2885 | 2975 |
| 2886 // False Start requires NPN/ALPN, ECDHE, and an AEAD. | 2976 // False Start requires NPN/ALPN, ECDHE, and an AEAD. |
| 2887 SpawnedTestServer::SSLOptions server_options; | 2977 SpawnedTestServer::SSLOptions server_options; |
| 2888 server_options.key_exchanges = | 2978 server_options.key_exchanges = |
| 2889 SpawnedTestServer::SSLOptions::KEY_EXCHANGE_ECDHE_RSA; | 2979 SpawnedTestServer::SSLOptions::KEY_EXCHANGE_ECDHE_RSA; |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3119 ssl_config.channel_id_enabled = true; | 3209 ssl_config.channel_id_enabled = true; |
| 3120 | 3210 |
| 3121 int rv; | 3211 int rv; |
| 3122 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); | 3212 ASSERT_TRUE(CreateAndConnectSSLClientSocket(ssl_config, &rv)); |
| 3123 | 3213 |
| 3124 EXPECT_EQ(ERR_UNEXPECTED, rv); | 3214 EXPECT_EQ(ERR_UNEXPECTED, rv); |
| 3125 EXPECT_FALSE(sock_->IsConnected()); | 3215 EXPECT_FALSE(sock_->IsConnected()); |
| 3126 } | 3216 } |
| 3127 | 3217 |
| 3128 } // namespace net | 3218 } // namespace net |
| OLD | NEW |