OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/net/certificate_error_reporter.h" | 5 #include "chrome/browser/net/certificate_error_reporter.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
18 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
19 #include "chrome/browser/net/cert_logger.pb.h" | 19 #include "chrome/browser/net/cert_logger.pb.h" |
20 #include "chrome/common/chrome_paths.h" | 20 #include "chrome/common/chrome_paths.h" |
21 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
22 #include "crypto/curve25519.h" | |
23 #include "net/base/load_flags.h" | 22 #include "net/base/load_flags.h" |
24 #include "net/base/network_delegate_impl.h" | 23 #include "net/base/network_delegate_impl.h" |
25 #include "net/base/test_data_directory.h" | 24 #include "net/base/test_data_directory.h" |
26 #include "net/base/upload_bytes_element_reader.h" | 25 #include "net/base/upload_bytes_element_reader.h" |
27 #include "net/base/upload_data_stream.h" | 26 #include "net/base/upload_data_stream.h" |
28 #include "net/base/upload_element_reader.h" | 27 #include "net/base/upload_element_reader.h" |
29 #include "net/test/cert_test_util.h" | 28 #include "net/test/cert_test_util.h" |
30 #include "net/test/url_request/url_request_failed_job.h" | 29 #include "net/test/url_request/url_request_failed_job.h" |
31 #include "net/test/url_request/url_request_mock_data_job.h" | 30 #include "net/test/url_request/url_request_mock_data_job.h" |
32 #include "net/test/url_request/url_request_mock_http_job.h" | 31 #include "net/test/url_request/url_request_mock_http_job.h" |
33 #include "net/url_request/url_request_filter.h" | 32 #include "net/url_request/url_request_filter.h" |
34 #include "net/url_request/url_request_test_util.h" | 33 #include "net/url_request/url_request_test_util.h" |
35 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
36 | 35 |
37 using chrome_browser_net::CertificateErrorReporter; | 36 using chrome_browser_net::CertificateErrorReporter; |
38 using content::BrowserThread; | 37 using content::BrowserThread; |
39 using net::CompletionCallback; | 38 using net::CompletionCallback; |
40 using net::SSLInfo; | 39 using net::SSLInfo; |
41 using net::NetworkDelegateImpl; | 40 using net::NetworkDelegateImpl; |
42 using net::TestURLRequestContext; | 41 using net::TestURLRequestContext; |
43 using net::URLRequest; | 42 using net::URLRequest; |
44 | 43 |
45 namespace { | 44 namespace { |
46 | 45 |
47 const char kHostname[] = "test.mail.google.com"; | 46 const char kHostname[] = "test.mail.google.com"; |
48 const char kSecondRequestHostname[] = "test2.mail.google.com"; | 47 const char kSecondRequestHostname[] = "test2.mail.google.com"; |
49 const char kDummyFailureLog[] = "dummy failure log"; | 48 const char kDummyFailureLog[] = "dummy failure log"; |
50 const char kTestCertFilename[] = "test_mail_google_com.pem"; | 49 const char kTestCertFilename[] = "test_mail_google_com.pem"; |
51 const uint32 kServerPublicKeyVersion = 1; | |
52 | 50 |
53 SSLInfo GetTestSSLInfo() { | 51 SSLInfo GetTestSSLInfo() { |
54 SSLInfo info; | 52 SSLInfo info; |
55 info.cert = | 53 info.cert = |
56 net::ImportCertFromFile(net::GetTestCertsDirectory(), kTestCertFilename); | 54 net::ImportCertFromFile(net::GetTestCertsDirectory(), kTestCertFilename); |
57 info.is_issued_by_known_root = true; | 55 info.is_issued_by_known_root = true; |
58 info.pinning_failure_log = kDummyFailureLog; | 56 info.pinning_failure_log = kDummyFailureLog; |
59 return info; | 57 return info; |
60 } | 58 } |
61 | 59 |
(...skipping 16 matching lines...) Expand all Loading... |
78 net::URLRequestMockHTTPJob::AddUrlHandlers(root_http, | 76 net::URLRequestMockHTTPJob::AddUrlHandlers(root_http, |
79 BrowserThread::GetBlockingPool()); | 77 BrowserThread::GetBlockingPool()); |
80 net::URLRequestMockDataJob::AddUrlHandler(); | 78 net::URLRequestMockDataJob::AddUrlHandler(); |
81 } | 79 } |
82 | 80 |
83 // Check that data uploaded in the request matches the test data (an SSL | 81 // Check that data uploaded in the request matches the test data (an SSL |
84 // report for one of the given hostnames, with the info returned by | 82 // report for one of the given hostnames, with the info returned by |
85 // |GetTestSSLInfo()|). The hostname sent in the report will be erased | 83 // |GetTestSSLInfo()|). The hostname sent in the report will be erased |
86 // from |expect_hostnames|. | 84 // from |expect_hostnames|. |
87 void CheckUploadData(URLRequest* request, | 85 void CheckUploadData(URLRequest* request, |
88 std::set<std::string>* expect_hostnames, | 86 std::set<std::string>* expect_hostnames) { |
89 bool encrypted, | |
90 const uint8* server_private_key) { | |
91 const net::UploadDataStream* upload = request->get_upload(); | 87 const net::UploadDataStream* upload = request->get_upload(); |
92 ASSERT_TRUE(upload); | 88 ASSERT_TRUE(upload); |
93 ASSERT_TRUE(upload->GetElementReaders()); | 89 ASSERT_TRUE(upload->GetElementReaders()); |
94 EXPECT_EQ(1u, upload->GetElementReaders()->size()); | 90 EXPECT_EQ(1u, upload->GetElementReaders()->size()); |
95 | 91 |
96 const net::UploadBytesElementReader* reader = | 92 const net::UploadBytesElementReader* reader = |
97 (*upload->GetElementReaders())[0]->AsBytesReader(); | 93 (*upload->GetElementReaders())[0]->AsBytesReader(); |
98 ASSERT_TRUE(reader); | 94 ASSERT_TRUE(reader); |
99 std::string upload_data(reader->bytes(), reader->length()); | 95 std::string upload_data(reader->bytes(), reader->length()); |
| 96 chrome_browser_net::CertLoggerRequest uploaded_request; |
100 | 97 |
101 chrome_browser_net::CertLoggerRequest uploaded_request; | 98 uploaded_request.ParseFromString(upload_data); |
102 #if defined(USE_OPENSSL) | |
103 if (encrypted) { | |
104 chrome_browser_net::EncryptedCertLoggerRequest encrypted_request; | |
105 encrypted_request.ParseFromString(upload_data); | |
106 EXPECT_EQ(kServerPublicKeyVersion, | |
107 encrypted_request.server_public_key_version()); | |
108 EXPECT_EQ(chrome_browser_net::EncryptedCertLoggerRequest:: | |
109 AEAD_ECDH_AES_128_CTR_HMAC_SHA256, | |
110 encrypted_request.algorithm()); | |
111 ASSERT_TRUE( | |
112 chrome_browser_net::CertificateErrorReporter:: | |
113 DecryptCertificateErrorReport(server_private_key, encrypted_request, | |
114 &uploaded_request)); | |
115 } else { | |
116 ASSERT_TRUE(uploaded_request.ParseFromString(upload_data)); | |
117 } | |
118 #else | |
119 ASSERT_TRUE(uploaded_request.ParseFromString(upload_data)); | |
120 #endif | |
121 | 99 |
122 EXPECT_EQ(1u, expect_hostnames->count(uploaded_request.hostname())); | 100 EXPECT_EQ(1u, expect_hostnames->count(uploaded_request.hostname())); |
123 expect_hostnames->erase(uploaded_request.hostname()); | 101 expect_hostnames->erase(uploaded_request.hostname()); |
124 | 102 |
125 EXPECT_EQ(GetPEMEncodedChain(), uploaded_request.cert_chain()); | 103 EXPECT_EQ(GetPEMEncodedChain(), uploaded_request.cert_chain()); |
126 EXPECT_EQ(1, uploaded_request.pin().size()); | 104 EXPECT_EQ(1, uploaded_request.pin().size()); |
127 EXPECT_EQ(kDummyFailureLog, uploaded_request.pin().Get(0)); | 105 EXPECT_EQ(kDummyFailureLog, uploaded_request.pin().Get(0)); |
128 } | 106 } |
129 | 107 |
130 // A network delegate that lets tests check that a certificate error | 108 // A network delegate that lets tests check that a certificate error |
131 // report was sent. It counts the number of requests and lets tests | 109 // report was sent. It counts the number of requests and lets tests |
132 // register a callback to run when the request is destroyed. It also | 110 // register a callback to run when the request is destroyed. It also |
133 // checks that the uploaded data is as expected (a report for | 111 // checks that the uploaded data is as expected (a report for |
134 // |kHostname| and |GetTestSSLInfo()|). | 112 // |kHostname| and |GetTestSSLInfo()|). |
135 class TestCertificateErrorReporterNetworkDelegate : public NetworkDelegateImpl { | 113 class TestCertificateErrorReporterNetworkDelegate : public NetworkDelegateImpl { |
136 public: | 114 public: |
137 TestCertificateErrorReporterNetworkDelegate() | 115 TestCertificateErrorReporterNetworkDelegate() |
138 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)), | 116 : url_request_destroyed_callback_(base::Bind(&base::DoNothing)), |
139 all_url_requests_destroyed_callback_(base::Bind(&base::DoNothing)), | 117 all_url_requests_destroyed_callback_(base::Bind(&base::DoNothing)), |
140 num_requests_(0), | 118 num_requests_(0), |
141 expect_cookies_(false), | 119 expect_cookies_(false) {} |
142 expect_request_encrypted_(false) { | |
143 memset(server_private_key_, 1, sizeof(server_private_key_)); | |
144 crypto::curve25519::ScalarBaseMult(server_private_key_, server_public_key_); | |
145 } | |
146 | 120 |
147 ~TestCertificateErrorReporterNetworkDelegate() override {} | 121 ~TestCertificateErrorReporterNetworkDelegate() override {} |
148 | 122 |
149 void ExpectHostname(const std::string& hostname) { | 123 void ExpectHostname(const std::string& hostname) { |
150 expect_hostnames_.insert(hostname); | 124 expect_hostnames_.insert(hostname); |
151 } | 125 } |
152 | 126 |
153 void set_all_url_requests_destroyed_callback( | 127 void set_all_url_requests_destroyed_callback( |
154 const base::Closure& all_url_requests_destroyed_callback) { | 128 const base::Closure& all_url_requests_destroyed_callback) { |
155 all_url_requests_destroyed_callback_ = all_url_requests_destroyed_callback; | 129 all_url_requests_destroyed_callback_ = all_url_requests_destroyed_callback; |
156 } | 130 } |
157 | 131 |
158 void set_url_request_destroyed_callback( | 132 void set_url_request_destroyed_callback( |
159 const base::Closure& url_request_destroyed_callback) { | 133 const base::Closure& url_request_destroyed_callback) { |
160 url_request_destroyed_callback_ = url_request_destroyed_callback; | 134 url_request_destroyed_callback_ = url_request_destroyed_callback; |
161 } | 135 } |
162 | 136 |
163 void set_expect_url(const GURL& expect_url) { expect_url_ = expect_url; } | 137 void set_expect_url(const GURL& expect_url) { expect_url_ = expect_url; } |
164 | 138 |
165 int num_requests() const { return num_requests_; } | 139 int num_requests() const { return num_requests_; } |
166 | 140 |
167 // Sets whether cookies are expected to be sent on requests. If set to | 141 // Sets whether cookies are expected to be sent on requests. If set to |
168 // true, then |OnHeadersReceived| will expect a cookie | 142 // true, then |OnHeadersReceived| will expect a cookie |
169 // "cookie_name=cookie_value". | 143 // "cookie_name=cookie_value". |
170 void set_expect_cookies(bool expect_cookies) { | 144 void set_expect_cookies(bool expect_cookies) { |
171 expect_cookies_ = expect_cookies; | 145 expect_cookies_ = expect_cookies; |
172 } | 146 } |
173 | 147 |
174 void set_expect_request_encrypted(bool expect_request_encrypted) { | |
175 expect_request_encrypted_ = expect_request_encrypted; | |
176 } | |
177 | |
178 // NetworkDelegateImpl implementation | 148 // NetworkDelegateImpl implementation |
179 int OnBeforeURLRequest(URLRequest* request, | 149 int OnBeforeURLRequest(URLRequest* request, |
180 const CompletionCallback& callback, | 150 const CompletionCallback& callback, |
181 GURL* new_url) override { | 151 GURL* new_url) override { |
182 num_requests_++; | 152 num_requests_++; |
183 EXPECT_EQ(expect_url_, request->url()); | 153 EXPECT_EQ(expect_url_, request->url()); |
184 EXPECT_EQ("POST", request->method()); | 154 EXPECT_EQ("POST", request->method()); |
185 | 155 |
186 if (expect_cookies_) { | 156 if (expect_cookies_) { |
187 EXPECT_FALSE(request->load_flags() & net::LOAD_DO_NOT_SEND_COOKIES); | 157 EXPECT_FALSE(request->load_flags() & net::LOAD_DO_NOT_SEND_COOKIES); |
188 EXPECT_FALSE(request->load_flags() & net::LOAD_DO_NOT_SAVE_COOKIES); | 158 EXPECT_FALSE(request->load_flags() & net::LOAD_DO_NOT_SAVE_COOKIES); |
189 } else { | 159 } else { |
190 EXPECT_TRUE(request->load_flags() & net::LOAD_DO_NOT_SEND_COOKIES); | 160 EXPECT_TRUE(request->load_flags() & net::LOAD_DO_NOT_SEND_COOKIES); |
191 EXPECT_TRUE(request->load_flags() & net::LOAD_DO_NOT_SAVE_COOKIES); | 161 EXPECT_TRUE(request->load_flags() & net::LOAD_DO_NOT_SAVE_COOKIES); |
192 } | 162 } |
193 | 163 |
194 std::string uploaded_request_hostname; | 164 std::string uploaded_request_hostname; |
195 CheckUploadData(request, &expect_hostnames_, expect_request_encrypted_, | 165 CheckUploadData(request, &expect_hostnames_); |
196 server_private_key_); | |
197 expect_hostnames_.erase(uploaded_request_hostname); | 166 expect_hostnames_.erase(uploaded_request_hostname); |
198 return net::OK; | 167 return net::OK; |
199 } | 168 } |
200 | 169 |
201 void OnURLRequestDestroyed(URLRequest* request) override { | 170 void OnURLRequestDestroyed(URLRequest* request) override { |
202 url_request_destroyed_callback_.Run(); | 171 url_request_destroyed_callback_.Run(); |
203 if (expect_hostnames_.empty()) | 172 if (expect_hostnames_.empty()) |
204 all_url_requests_destroyed_callback_.Run(); | 173 all_url_requests_destroyed_callback_.Run(); |
205 } | 174 } |
206 | 175 |
207 const uint8* server_public_key() { return server_public_key_; } | |
208 | |
209 private: | 176 private: |
210 base::Closure url_request_destroyed_callback_; | 177 base::Closure url_request_destroyed_callback_; |
211 base::Closure all_url_requests_destroyed_callback_; | 178 base::Closure all_url_requests_destroyed_callback_; |
212 int num_requests_; | 179 int num_requests_; |
213 GURL expect_url_; | 180 GURL expect_url_; |
214 std::set<std::string> expect_hostnames_; | 181 std::set<std::string> expect_hostnames_; |
215 bool expect_cookies_; | 182 bool expect_cookies_; |
216 bool expect_request_encrypted_; | |
217 | |
218 uint8 server_public_key_[32]; | |
219 uint8 server_private_key_[32]; | |
220 | 183 |
221 DISALLOW_COPY_AND_ASSIGN(TestCertificateErrorReporterNetworkDelegate); | 184 DISALLOW_COPY_AND_ASSIGN(TestCertificateErrorReporterNetworkDelegate); |
222 }; | 185 }; |
223 | 186 |
224 class CertificateErrorReporterTest : public ::testing::Test { | 187 class CertificateErrorReporterTest : public ::testing::Test { |
225 public: | 188 public: |
226 CertificateErrorReporterTest() : context_(true) { | 189 CertificateErrorReporterTest() : context_(true) { |
227 EnableUrlRequestMocks(true); | 190 EnableUrlRequestMocks(true); |
228 context_.set_network_delegate(&network_delegate_); | 191 context_.set_network_delegate(&network_delegate_); |
229 context_.Init(); | 192 context_.Init(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 // for the endpoint and sends the expected data. | 230 // for the endpoint and sends the expected data. |
268 TEST_F(CertificateErrorReporterTest, PinningViolationSendReportSendsRequest) { | 231 TEST_F(CertificateErrorReporterTest, PinningViolationSendReportSendsRequest) { |
269 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 232 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
270 CertificateErrorReporter reporter( | 233 CertificateErrorReporter reporter( |
271 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); | 234 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); |
272 SendReport(&reporter, network_delegate(), kHostname, url, 0, | 235 SendReport(&reporter, network_delegate(), kHostname, url, 0, |
273 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); | 236 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); |
274 } | 237 } |
275 | 238 |
276 TEST_F(CertificateErrorReporterTest, ExtendedReportingSendReportSendsRequest) { | 239 TEST_F(CertificateErrorReporterTest, ExtendedReportingSendReportSendsRequest) { |
277 // Data should not be encrypted when sent to an HTTPS URL. | 240 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
278 GURL https_url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 241 CertificateErrorReporter reporter( |
279 CertificateErrorReporter https_reporter( | 242 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); |
280 context(), https_url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); | 243 SendReport(&reporter, network_delegate(), kHostname, url, 0, |
281 network_delegate()->set_expect_request_encrypted(false); | |
282 SendReport(&https_reporter, network_delegate(), kHostname, https_url, 0, | |
283 CertificateErrorReporter::REPORT_TYPE_EXTENDED_REPORTING); | 244 CertificateErrorReporter::REPORT_TYPE_EXTENDED_REPORTING); |
284 | |
285 // Data should be encrypted when sent to an HTTP URL. | |
286 if (CertificateErrorReporter::IsHttpUploadUrlSupported()) { | |
287 GURL http_url = net::URLRequestMockDataJob::GetMockHttpUrl("dummy data", 1); | |
288 CertificateErrorReporter http_reporter( | |
289 context(), http_url, CertificateErrorReporter::DO_NOT_SEND_COOKIES, | |
290 network_delegate()->server_public_key(), kServerPublicKeyVersion); | |
291 network_delegate()->set_expect_request_encrypted(true); | |
292 SendReport(&http_reporter, network_delegate(), kHostname, http_url, 1, | |
293 CertificateErrorReporter::REPORT_TYPE_EXTENDED_REPORTING); | |
294 } | |
295 } | 245 } |
296 | 246 |
297 TEST_F(CertificateErrorReporterTest, SendMultipleReportsSequentially) { | 247 TEST_F(CertificateErrorReporterTest, SendMultipleReportsSequentially) { |
298 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 248 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
299 CertificateErrorReporter reporter( | 249 CertificateErrorReporter reporter( |
300 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); | 250 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); |
301 SendReport(&reporter, network_delegate(), kHostname, url, 0, | 251 SendReport(&reporter, network_delegate(), kHostname, url, 0, |
302 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); | 252 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); |
303 SendReport(&reporter, network_delegate(), kSecondRequestHostname, url, 1, | 253 SendReport(&reporter, network_delegate(), kSecondRequestHostname, url, 1, |
304 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); | 254 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); | 331 GURL url = net::URLRequestMockDataJob::GetMockHttpsUrl("dummy data", 1); |
382 CertificateErrorReporter reporter( | 332 CertificateErrorReporter reporter( |
383 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); | 333 context(), url, CertificateErrorReporter::DO_NOT_SEND_COOKIES); |
384 | 334 |
385 network_delegate()->set_expect_cookies(false); | 335 network_delegate()->set_expect_cookies(false); |
386 SendReport(&reporter, network_delegate(), kHostname, url, 0, | 336 SendReport(&reporter, network_delegate(), kHostname, url, 0, |
387 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); | 337 CertificateErrorReporter::REPORT_TYPE_PINNING_VIOLATION); |
388 } | 338 } |
389 | 339 |
390 } // namespace | 340 } // namespace |
OLD | NEW |