summaryrefslogtreecommitdiff
path: root/chromium/net/http/http_network_transaction_ssl_unittest.cc
blob: bb218498961bed3fb00df5c06392b9c9208754fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "net/base/net_util.h"
#include "net/base/request_priority.h"
#include "net/dns/mock_host_resolver.h"
#include "net/http/http_auth_handler_mock.h"
#include "net/http/http_network_session.h"
#include "net/http/http_network_transaction.h"
#include "net/http/http_request_info.h"
#include "net/http/http_server_properties_impl.h"
#include "net/http/transport_security_state.h"
#include "net/proxy/proxy_service.h"
#include "net/socket/socket_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

namespace {

class TLS10SSLConfigService : public SSLConfigService {
 public:
  TLS10SSLConfigService() {
    ssl_config_.version_min = SSL_PROTOCOL_VERSION_SSL3;
    ssl_config_.version_max = SSL_PROTOCOL_VERSION_TLS1;
  }

  virtual void GetSSLConfig(SSLConfig* config) OVERRIDE {
    *config = ssl_config_;
  }

 private:
  virtual ~TLS10SSLConfigService() {}

  SSLConfig ssl_config_;
};

class TLS11SSLConfigService : public SSLConfigService {
 public:
  TLS11SSLConfigService() {
    ssl_config_.version_min = SSL_PROTOCOL_VERSION_SSL3;
    ssl_config_.version_max = SSL_PROTOCOL_VERSION_TLS1_1;
  }

  virtual void GetSSLConfig(SSLConfig* config) OVERRIDE {
    *config = ssl_config_;
  }

 private:
  virtual ~TLS11SSLConfigService() {}

  SSLConfig ssl_config_;
};

}  // namespace

class HttpNetworkTransactionSSLTest : public testing::Test {
 protected:
  virtual void SetUp() {
    ssl_config_service_ = new TLS10SSLConfigService;
    session_params_.ssl_config_service = ssl_config_service_.get();

    auth_handler_factory_.reset(new HttpAuthHandlerMock::Factory());
    session_params_.http_auth_handler_factory = auth_handler_factory_.get();

    proxy_service_.reset(ProxyService::CreateDirect());
    session_params_.proxy_service = proxy_service_.get();

    session_params_.client_socket_factory = &mock_socket_factory_;
    session_params_.host_resolver = &mock_resolver_;
    session_params_.http_server_properties =
        http_server_properties_.GetWeakPtr();
    session_params_.transport_security_state = &transport_security_state_;
  }

  HttpRequestInfo* GetRequestInfo(const std::string& url) {
    HttpRequestInfo* request_info = new HttpRequestInfo;
    request_info->url = GURL(url);
    request_info->method = "GET";
    request_info_vector_.push_back(request_info);
    return request_info;
  }

  SSLConfig& GetServerSSLConfig(HttpNetworkTransaction* trans) {
    return trans->server_ssl_config_;
  }

  scoped_refptr<SSLConfigService> ssl_config_service_;
  scoped_ptr<HttpAuthHandlerMock::Factory> auth_handler_factory_;
  scoped_ptr<ProxyService> proxy_service_;

  MockClientSocketFactory mock_socket_factory_;
  MockHostResolver mock_resolver_;
  HttpServerPropertiesImpl http_server_properties_;
  TransportSecurityState transport_security_state_;
  HttpNetworkSession::Params session_params_;
  ScopedVector<HttpRequestInfo> request_info_vector_;
};

// Tests that HttpNetworkTransaction does not attempt to
// fallback to SSL 3.0 when a TLS 1.0 handshake fails and:
// * the site is pinned to the Google pin list (indicating that
//   it is a Google site);
// * unrestricted SSL 3.0 fallback is disabled.
TEST_F(HttpNetworkTransactionSSLTest, SSL3FallbackDisabled_Google) {
  // |ssl_data1| is for the first handshake (TLS 1.0), which will fail for
  // protocol reasons (e.g., simulating a version rollback attack).
  // Because unrestricted SSL 3.0 fallback is disabled, only this simulated
  // SSL handshake is consumed.
  SSLSocketDataProvider ssl_data1(ASYNC, ERR_SSL_PROTOCOL_ERROR);
  mock_socket_factory_.AddSSLSocketDataProvider(&ssl_data1);
  StaticSocketDataProvider data1(NULL, 0, NULL, 0);
  mock_socket_factory_.AddSocketDataProvider(&data1);

  // This extra handshake, which should be unconsumed, is provided to ensure
  // that even if the behaviour being tested here ever breaks (and Google
  // properties begin SSL 3.0 fallbacks), this test will not crash (and bring
  // down all of net_unittests), but it will fail gracefully.
  SSLSocketDataProvider ssl_data2(ASYNC, ERR_SSL_PROTOCOL_ERROR);
  mock_socket_factory_.AddSSLSocketDataProvider(&ssl_data2);
  StaticSocketDataProvider data2(NULL, 0, NULL, 0);
  mock_socket_factory_.AddSocketDataProvider(&data2);

  scoped_refptr<HttpNetworkSession> session(
      new HttpNetworkSession(session_params_));
  scoped_ptr<HttpNetworkTransaction> trans(
      new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get()));

  SSLConfig& ssl_config = GetServerSSLConfig(trans.get());
  ssl_config.unrestricted_ssl3_fallback_enabled = false;

  TestCompletionCallback callback;
  // This will consume only |ssl_data1|. |ssl_data2| will not be consumed.
  int rv = callback.GetResult(
      trans->Start(GetRequestInfo("https://www.google.com/"),
                   callback.callback(), BoundNetLog()));
  EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, rv);

  SocketDataProviderArray<SocketDataProvider>& mock_data =
      mock_socket_factory_.mock_data();
  // Confirms that only |ssl_data1| is consumed.
  EXPECT_EQ(1u, mock_data.next_index());

  // |version_max| never fallbacks to SSLv3 for Google properties.
  EXPECT_EQ(SSL_PROTOCOL_VERSION_TLS1, ssl_config.version_max);
  EXPECT_FALSE(ssl_config.version_fallback);
}

// Tests that HttpNetworkTransaction attempts to fallback to SSL 3.0
// when a TLS 1.0 handshake fails and:
// * the site is pinned to the Google pin list (indicating that
//   it is a Google site);
// * unrestricted SSL 3.0 fallback is enabled.
TEST_F(HttpNetworkTransactionSSLTest, SSL3FallbackEnabled_Google) {
  // |ssl_data1| is for the first handshake (TLS 1.0), which will fail
  // for protocol reasons (e.g., simulating a version rollback attack).
  SSLSocketDataProvider ssl_data1(ASYNC, ERR_SSL_PROTOCOL_ERROR);
  mock_socket_factory_.AddSSLSocketDataProvider(&ssl_data1);
  StaticSocketDataProvider data1(NULL, 0, NULL, 0);
  mock_socket_factory_.AddSocketDataProvider(&data1);

  // |ssl_data2| contains the handshake result for a SSL 3.0
  // handshake which will be attempted after the TLS 1.0
  // handshake fails.
  SSLSocketDataProvider ssl_data2(ASYNC, ERR_SSL_PROTOCOL_ERROR);
  mock_socket_factory_.AddSSLSocketDataProvider(&ssl_data2);
  StaticSocketDataProvider data2(NULL, 0, NULL, 0);
  mock_socket_factory_.AddSocketDataProvider(&data2);

  scoped_refptr<HttpNetworkSession> session(
      new HttpNetworkSession(session_params_));
  scoped_ptr<HttpNetworkTransaction> trans(
      new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get()));

  SSLConfig& ssl_config = GetServerSSLConfig(trans.get());
  ssl_config.unrestricted_ssl3_fallback_enabled = true;

  TestCompletionCallback callback;
  // This will consume |ssl_data1| and |ssl_data2|.
  int rv = callback.GetResult(
      trans->Start(GetRequestInfo("https://www.google.com/"),
                   callback.callback(), BoundNetLog()));
  EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, rv);

  SocketDataProviderArray<SocketDataProvider>& mock_data =
      mock_socket_factory_.mock_data();
  // Confirms that both |ssl_data1| and |ssl_data2| are consumed.
  EXPECT_EQ(2u, mock_data.next_index());

  // |version_max| fallbacks to SSL 3.0 for Google properties when
  // |unrestricted_ssl3_fallback_enabled| is true.
  EXPECT_EQ(SSL_PROTOCOL_VERSION_SSL3, ssl_config.version_max);
  EXPECT_TRUE(ssl_config.version_fallback);
}

// Tests that HttpNetworkTransaction attempts to fallback to SSL 3.0
// when a TLS 1.0 handshake fails and the site is not a Google domain,
// even if unrestricted SSL 3.0 fallback is disabled.
// TODO(thaidn): revise the above comment and this test when the
// SSL 3.0 fallback experiment is applied for non-Google domains.
TEST_F(HttpNetworkTransactionSSLTest, SSL3FallbackDisabled_Paypal) {
  // |ssl_data1| is for the first handshake (TLS 1.0), which will fail
  // for protocol reasons (e.g., simulating a version rollback attack).
  SSLSocketDataProvider ssl_data1(ASYNC, ERR_SSL_PROTOCOL_ERROR);
  mock_socket_factory_.AddSSLSocketDataProvider(&ssl_data1);
  StaticSocketDataProvider data1(NULL, 0, NULL, 0);
  mock_socket_factory_.AddSocketDataProvider(&data1);

  // |ssl_data2| contains the handshake result for a SSL 3.0
  // handshake which will be attempted after the TLS 1.0
  // handshake fails.
  SSLSocketDataProvider ssl_data2(ASYNC, ERR_SSL_PROTOCOL_ERROR);
  mock_socket_factory_.AddSSLSocketDataProvider(&ssl_data2);
  StaticSocketDataProvider data2(NULL, 0, NULL, 0);
  mock_socket_factory_.AddSocketDataProvider(&data2);

  scoped_refptr<HttpNetworkSession> session(
      new HttpNetworkSession(session_params_));
  scoped_ptr<HttpNetworkTransaction> trans(
      new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get()));

  SSLConfig& ssl_config = GetServerSSLConfig(trans.get());
  ssl_config.unrestricted_ssl3_fallback_enabled = false;

  TestCompletionCallback callback;
  // This will consume |ssl_data1| and |ssl_data2|.
  int rv = callback.GetResult(
      trans->Start(GetRequestInfo("https://www.paypal.com/"),
                   callback.callback(), BoundNetLog()));
  EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, rv);

  SocketDataProviderArray<SocketDataProvider>& mock_data =
      mock_socket_factory_.mock_data();
  // Confirms that both |ssl_data1| and |ssl_data2| are consumed.
  EXPECT_EQ(2u, mock_data.next_index());

  // |version_max| fallbacks to SSL 3.0.
  EXPECT_EQ(SSL_PROTOCOL_VERSION_SSL3, ssl_config.version_max);
  EXPECT_TRUE(ssl_config.version_fallback);
}

// Tests that HttpNetworkTransaction attempts to fallback from
// TLS 1.1 to TLS 1.0, then from TLS 1.0 to SSL 3.0.
TEST_F(HttpNetworkTransactionSSLTest, SSLFallback) {
  ssl_config_service_ = new TLS11SSLConfigService;
  session_params_.ssl_config_service = ssl_config_service_.get();
  // |ssl_data1| is for the first handshake (TLS 1.1), which will fail
  // for protocol reasons (e.g., simulating a version rollback attack).
  SSLSocketDataProvider ssl_data1(ASYNC, ERR_SSL_PROTOCOL_ERROR);
  mock_socket_factory_.AddSSLSocketDataProvider(&ssl_data1);
  StaticSocketDataProvider data1(NULL, 0, NULL, 0);
  mock_socket_factory_.AddSocketDataProvider(&data1);

  // |ssl_data2| contains the handshake result for a TLS 1.0
  // handshake which will be attempted after the TLS 1.1
  // handshake fails.
  SSLSocketDataProvider ssl_data2(ASYNC, ERR_SSL_PROTOCOL_ERROR);
  mock_socket_factory_.AddSSLSocketDataProvider(&ssl_data2);
  StaticSocketDataProvider data2(NULL, 0, NULL, 0);
  mock_socket_factory_.AddSocketDataProvider(&data2);

  // |ssl_data3| contains the handshake result for a SSL 3.0
  // handshake which will be attempted after the TLS 1.0
  // handshake fails.
  SSLSocketDataProvider ssl_data3(ASYNC, ERR_SSL_PROTOCOL_ERROR);
  mock_socket_factory_.AddSSLSocketDataProvider(&ssl_data3);
  StaticSocketDataProvider data3(NULL, 0, NULL, 0);
  mock_socket_factory_.AddSocketDataProvider(&data3);

  scoped_refptr<HttpNetworkSession> session(
      new HttpNetworkSession(session_params_));
  scoped_ptr<HttpNetworkTransaction> trans(
      new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get()));

  SSLConfig& ssl_config = GetServerSSLConfig(trans.get());
  ssl_config.unrestricted_ssl3_fallback_enabled = true;

  TestCompletionCallback callback;
  // This will consume |ssl_data1|, |ssl_data2| and |ssl_data3|.
  int rv = callback.GetResult(
      trans->Start(GetRequestInfo("https://www.paypal.com/"),
                   callback.callback(), BoundNetLog()));
  EXPECT_EQ(ERR_SSL_PROTOCOL_ERROR, rv);

  SocketDataProviderArray<SocketDataProvider>& mock_data =
      mock_socket_factory_.mock_data();
  // Confirms that |ssl_data1|, |ssl_data2| and |ssl_data3| are consumed.
  EXPECT_EQ(3u, mock_data.next_index());

  // |version_max| fallbacks to SSL 3.0.
  EXPECT_EQ(SSL_PROTOCOL_VERSION_SSL3, ssl_config.version_max);
  EXPECT_TRUE(ssl_config.version_fallback);
}

}  // namespace net