Chromium Code Reviews| 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 "chrome/browser/net/chrome_fraudulent_certificate_reporter.h" | 5 #include "chrome/browser/net/chrome_fraudulent_certificate_reporter.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
| 14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 15 #include "chrome/browser/net/cert_logger.pb.h" | |
| 15 #include "content/public/test/test_browser_thread.h" | 16 #include "content/public/test/test_browser_thread.h" |
| 16 #include "net/base/request_priority.h" | 17 #include "net/base/request_priority.h" |
| 17 #include "net/base/test_data_directory.h" | 18 #include "net/base/test_data_directory.h" |
| 18 #include "net/cert/x509_certificate.h" | 19 #include "net/cert/x509_certificate.h" |
| 19 #include "net/http/transport_security_state.h" | 20 #include "net/http/transport_security_state.h" |
| 20 #include "net/ssl/ssl_info.h" | 21 #include "net/ssl/ssl_info.h" |
| 21 #include "net/test/cert_test_util.h" | 22 #include "net/test/cert_test_util.h" |
| 22 #include "net/url_request/fraudulent_certificate_reporter.h" | 23 #include "net/url_request/fraudulent_certificate_reporter.h" |
| 23 #include "net/url_request/url_request.h" | 24 #include "net/url_request/url_request.h" |
| 24 #include "net/url_request/url_request_context.h" | 25 #include "net/url_request/url_request_context.h" |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 55 static SSLInfo GetGoodSSLInfo() { | 56 static SSLInfo GetGoodSSLInfo() { |
| 56 SSLInfo info; | 57 SSLInfo info; |
| 57 | 58 |
| 58 info.cert = net::ImportCertFromFile(net::GetTestCertsDirectory(), | 59 info.cert = net::ImportCertFromFile(net::GetTestCertsDirectory(), |
| 59 "test_mail_google_com.pem"); | 60 "test_mail_google_com.pem"); |
| 60 info.is_issued_by_known_root = true; | 61 info.is_issued_by_known_root = true; |
| 61 | 62 |
| 62 return info; | 63 return info; |
| 63 } | 64 } |
| 64 | 65 |
| 66 static SSLInfo GetExtendedReportingSSLInfo() { | |
| 67 SSLInfo info; | |
| 68 // Use different cert chains for the verified chain and the chain as | |
| 69 // sent by the server, and make sure that they are both included in | |
| 70 // the report. | |
| 71 info.cert = net::ImportCertFromFile(net::GetTestCertsDirectory(), | |
| 72 "test_mail_google_com.pem"); | |
| 73 info.is_issued_by_known_root = true; | |
| 74 info.unverified_server_cert = | |
| 75 net::ImportCertFromFile(net::GetTestCertsDirectory(), "expired_cert.pem"); | |
| 76 return info; | |
| 77 } | |
| 78 | |
| 65 // Checks that |info| is good as required by the SSL checks performed in | 79 // Checks that |info| is good as required by the SSL checks performed in |
| 66 // URLRequestHttpJob::OnStartCompleted, which are enough to trigger pin | 80 // URLRequestHttpJob::OnStartCompleted, which are enough to trigger pin |
| 67 // checking but not sufficient to pass | 81 // checking but not sufficient to pass |
| 68 // DomainState::IsChainOfPublicKeysPermitted. | 82 // DomainState::IsChainOfPublicKeysPermitted. |
| 69 static bool IsGoodSSLInfo(const SSLInfo& info) { | 83 static bool IsGoodSSLInfo(const SSLInfo& info) { |
| 70 return info.is_valid() && info.is_issued_by_known_root; | 84 return info.is_valid() && info.is_issued_by_known_root; |
| 71 } | 85 } |
| 72 | 86 |
| 73 class TestReporter : public ChromeFraudulentCertificateReporter { | 87 class TestReporter : public ChromeFraudulentCertificateReporter { |
| 74 public: | 88 public: |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 } | 146 } |
| 133 | 147 |
| 134 void SendReport(ReportType type, | 148 void SendReport(ReportType type, |
| 135 const std::string& hostname, | 149 const std::string& hostname, |
| 136 const net::SSLInfo& ssl_info) override { | 150 const net::SSLInfo& ssl_info) override { |
| 137 DCHECK(!hostname.empty()); | 151 DCHECK(!hostname.empty()); |
| 138 DCHECK(ssl_info.is_valid()); | 152 DCHECK(ssl_info.is_valid()); |
| 139 ChromeFraudulentCertificateReporter::SendReport(REPORT_TYPE_PIN_VIOLATION, | 153 ChromeFraudulentCertificateReporter::SendReport(REPORT_TYPE_PIN_VIOLATION, |
| 140 hostname, ssl_info); | 154 hostname, ssl_info); |
| 141 } | 155 } |
| 156 | |
| 157 static std::string BuildReportPublic(ReportType type, | |
|
estark
2015/02/21 06:42:23
Is this a weird pattern and/or weird name? My goal
| |
| 158 const std::string& hostname, | |
| 159 const net::SSLInfo& ssl_info) { | |
| 160 return BuildReport(type, hostname, ssl_info); | |
| 161 } | |
| 142 }; | 162 }; |
| 143 | 163 |
| 144 static void DoReportIsSent() { | 164 static void DoReportIsSent() { |
| 145 net::TestURLRequestContext context; | 165 net::TestURLRequestContext context; |
| 146 SendingTestReporter reporter(&context); | 166 SendingTestReporter reporter(&context); |
| 147 SSLInfo info = GetGoodSSLInfo(); | 167 SSLInfo info = GetGoodSSLInfo(); |
| 148 reporter.SendReport(FraudulentCertificateReporter::REPORT_TYPE_PIN_VIOLATION, | 168 reporter.SendReport(FraudulentCertificateReporter::REPORT_TYPE_PIN_VIOLATION, |
| 149 "mail.google.com", info); | 169 "mail.google.com", info); |
| 150 } | 170 } |
| 151 | 171 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 loop.RunUntilIdle(); | 207 loop.RunUntilIdle(); |
| 188 } | 208 } |
| 189 | 209 |
| 190 TEST(ChromeFraudulentCertificateReporterTest, ReportIsNotSent) { | 210 TEST(ChromeFraudulentCertificateReporterTest, ReportIsNotSent) { |
| 191 base::MessageLoopForIO loop; | 211 base::MessageLoopForIO loop; |
| 192 content::TestBrowserThread io_thread(BrowserThread::IO, &loop); | 212 content::TestBrowserThread io_thread(BrowserThread::IO, &loop); |
| 193 loop.PostTask(FROM_HERE, base::Bind(&DoReportIsNotSent)); | 213 loop.PostTask(FROM_HERE, base::Bind(&DoReportIsNotSent)); |
| 194 loop.RunUntilIdle(); | 214 loop.RunUntilIdle(); |
| 195 } | 215 } |
| 196 | 216 |
| 217 TEST(ChromeFraudulentCertificateReporterTest, BuildReportForPinningViolation) { | |
| 218 SSLInfo info = GetGoodSSLInfo(); | |
| 219 std::string report = MockReporter::BuildReportPublic( | |
| 220 ChromeFraudulentCertificateReporter::REPORT_TYPE_EXTENDED_REPORTING, | |
| 221 "mail.google.com", info); | |
| 222 CertLoggerRequest request; | |
| 223 request.ParseFromString(report); | |
| 224 | |
| 225 std::vector<std::string> cert_chain; | |
| 226 info.cert->GetPEMEncodedChain(&cert_chain); | |
| 227 | |
| 228 std::string cert; | |
| 229 for (size_t i = 0; i < cert_chain.size(); ++i) { | |
| 230 cert += cert_chain[i]; | |
| 231 } | |
| 232 | |
| 233 EXPECT_EQ(request.hostname(), "mail.google.com"); | |
| 234 EXPECT_EQ(request.cert_chain(), cert); | |
| 235 } | |
| 236 | |
| 237 TEST(ChromeFraudulentCertificateReporterTest, BuildReportForInvalidCertChain) { | |
| 238 SSLInfo info = GetExtendedReportingSSLInfo(); | |
| 239 std::string report = MockReporter::BuildReportPublic( | |
| 240 ChromeFraudulentCertificateReporter::REPORT_TYPE_EXTENDED_REPORTING, | |
| 241 "mail.google.com", info); | |
| 242 CertLoggerRequest request; | |
| 243 request.ParseFromString(report); | |
| 244 | |
| 245 std::vector<std::string> cert_chain; | |
| 246 info.cert->GetPEMEncodedChain(&cert_chain); | |
| 247 std::vector<std::string> unverified_server_cert_chain; | |
| 248 info.unverified_server_cert->GetPEMEncodedChain( | |
| 249 &unverified_server_cert_chain); | |
| 250 | |
| 251 std::string cert; | |
| 252 std::string unverified_server_cert; | |
| 253 for (size_t i = 0; i < cert_chain.size(); ++i) { | |
| 254 cert += cert_chain[i]; | |
| 255 } | |
| 256 for (size_t i = 0; i < cert_chain.size(); ++i) { | |
| 257 unverified_server_cert += unverified_server_cert_chain[i]; | |
| 258 } | |
| 259 | |
| 260 EXPECT_EQ(request.hostname(), "mail.google.com"); | |
| 261 EXPECT_EQ(request.cert_chain(), cert); | |
| 262 EXPECT_EQ(request.unverified_server_cert_chain(), unverified_server_cert); | |
| 263 } | |
| 264 | |
| 197 } // namespace chrome_browser_net | 265 } // namespace chrome_browser_net |
| OLD | NEW |