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
3 // found in the LICENSE file.
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/scoped_vector.h"
10 #include "net/base/net_util.h"
11 #include "net/base/request_priority.h"
12 #include "net/dns/mock_host_resolver.h"
13 #include "net/http/http_auth_handler_mock.h"
14 #include "net/http/http_network_session.h"
15 #include "net/http/http_network_transaction.h"
16 #include "net/http/http_request_info.h"
17 #include "net/http/http_server_properties_impl.h"
18 #include "net/http/transport_security_state.h"
19 #include "net/proxy/proxy_service.h"
20 #include "net/socket/socket_test_util.h"
21 #include "testing/gtest/include/gtest/gtest.h"
27 class TLS10SSLConfigService
: public SSLConfigService
{
29 TLS10SSLConfigService() {
30 ssl_config_
.version_min
= SSL_PROTOCOL_VERSION_SSL3
;
31 ssl_config_
.version_max
= SSL_PROTOCOL_VERSION_TLS1
;
34 virtual void GetSSLConfig(SSLConfig
* config
) OVERRIDE
{
35 *config
= ssl_config_
;
39 virtual ~TLS10SSLConfigService() {}
41 SSLConfig ssl_config_
;
44 class TLS11SSLConfigService
: public SSLConfigService
{
46 TLS11SSLConfigService() {
47 ssl_config_
.version_min
= SSL_PROTOCOL_VERSION_SSL3
;
48 ssl_config_
.version_max
= SSL_PROTOCOL_VERSION_TLS1_1
;
51 virtual void GetSSLConfig(SSLConfig
* config
) OVERRIDE
{
52 *config
= ssl_config_
;
56 virtual ~TLS11SSLConfigService() {}
58 SSLConfig ssl_config_
;
63 class HttpNetworkTransactionSSLTest
: public testing::Test
{
65 virtual void SetUp() {
66 ssl_config_service_
= new TLS10SSLConfigService
;
67 session_params_
.ssl_config_service
= ssl_config_service_
.get();
69 auth_handler_factory_
.reset(new HttpAuthHandlerMock::Factory());
70 session_params_
.http_auth_handler_factory
= auth_handler_factory_
.get();
72 proxy_service_
.reset(ProxyService::CreateDirect());
73 session_params_
.proxy_service
= proxy_service_
.get();
75 session_params_
.client_socket_factory
= &mock_socket_factory_
;
76 session_params_
.host_resolver
= &mock_resolver_
;
77 session_params_
.http_server_properties
=
78 http_server_properties_
.GetWeakPtr();
79 session_params_
.transport_security_state
= &transport_security_state_
;
82 HttpRequestInfo
* GetRequestInfo(const std::string
& url
) {
83 HttpRequestInfo
* request_info
= new HttpRequestInfo
;
84 request_info
->url
= GURL(url
);
85 request_info
->method
= "GET";
86 request_info_vector_
.push_back(request_info
);
90 SSLConfig
& GetServerSSLConfig(HttpNetworkTransaction
* trans
) {
91 return trans
->server_ssl_config_
;
94 scoped_refptr
<SSLConfigService
> ssl_config_service_
;
95 scoped_ptr
<HttpAuthHandlerMock::Factory
> auth_handler_factory_
;
96 scoped_ptr
<ProxyService
> proxy_service_
;
98 MockClientSocketFactory mock_socket_factory_
;
99 MockHostResolver mock_resolver_
;
100 HttpServerPropertiesImpl http_server_properties_
;
101 TransportSecurityState transport_security_state_
;
102 HttpNetworkSession::Params session_params_
;
103 ScopedVector
<HttpRequestInfo
> request_info_vector_
;
106 // Tests that HttpNetworkTransaction attempts to fallback from
107 // TLS 1.1 to TLS 1.0, then from TLS 1.0 to SSL 3.0.
108 TEST_F(HttpNetworkTransactionSSLTest
, SSLFallback
) {
109 ssl_config_service_
= new TLS11SSLConfigService
;
110 session_params_
.ssl_config_service
= ssl_config_service_
.get();
111 // |ssl_data1| is for the first handshake (TLS 1.1), which will fail
112 // for protocol reasons (e.g., simulating a version rollback attack).
113 SSLSocketDataProvider
ssl_data1(ASYNC
, ERR_SSL_PROTOCOL_ERROR
);
114 mock_socket_factory_
.AddSSLSocketDataProvider(&ssl_data1
);
115 StaticSocketDataProvider
data1(NULL
, 0, NULL
, 0);
116 mock_socket_factory_
.AddSocketDataProvider(&data1
);
118 // |ssl_data2| contains the handshake result for a TLS 1.0
119 // handshake which will be attempted after the TLS 1.1
121 SSLSocketDataProvider
ssl_data2(ASYNC
, ERR_SSL_PROTOCOL_ERROR
);
122 mock_socket_factory_
.AddSSLSocketDataProvider(&ssl_data2
);
123 StaticSocketDataProvider
data2(NULL
, 0, NULL
, 0);
124 mock_socket_factory_
.AddSocketDataProvider(&data2
);
126 // |ssl_data3| contains the handshake result for a SSL 3.0
127 // handshake which will be attempted after the TLS 1.0
129 SSLSocketDataProvider
ssl_data3(ASYNC
, ERR_SSL_PROTOCOL_ERROR
);
130 mock_socket_factory_
.AddSSLSocketDataProvider(&ssl_data3
);
131 StaticSocketDataProvider
data3(NULL
, 0, NULL
, 0);
132 mock_socket_factory_
.AddSocketDataProvider(&data3
);
134 scoped_refptr
<HttpNetworkSession
> session(
135 new HttpNetworkSession(session_params_
));
136 scoped_ptr
<HttpNetworkTransaction
> trans(
137 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session
.get()));
139 TestCompletionCallback callback
;
140 // This will consume |ssl_data1|, |ssl_data2| and |ssl_data3|.
141 int rv
= callback
.GetResult(
142 trans
->Start(GetRequestInfo("https://www.paypal.com/"),
143 callback
.callback(), BoundNetLog()));
144 EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR
, rv
);
146 SocketDataProviderArray
<SocketDataProvider
>& mock_data
=
147 mock_socket_factory_
.mock_data();
148 // Confirms that |ssl_data1|, |ssl_data2| and |ssl_data3| are consumed.
149 EXPECT_EQ(3u, mock_data
.next_index());
151 SSLConfig
& ssl_config
= GetServerSSLConfig(trans
.get());
152 // |version_max| fallbacks to SSL 3.0.
153 EXPECT_EQ(SSL_PROTOCOL_VERSION_SSL3
, ssl_config
.version_max
);
154 EXPECT_TRUE(ssl_config
.version_fallback
);