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.
5 #include "chrome/browser/ssl/chrome_fraudulent_certificate_reporter.h"
10 #include "base/files/file_path.h"
11 #include "base/location.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/synchronization/waitable_event.h"
16 #include "base/threading/thread.h"
17 #include "chrome/browser/net/certificate_error_reporter.h"
18 #include "content/public/test/test_browser_thread.h"
19 #include "net/base/request_priority.h"
20 #include "net/base/test_data_directory.h"
21 #include "net/cert/x509_certificate.h"
22 #include "net/http/transport_security_state.h"
23 #include "net/ssl/ssl_info.h"
24 #include "net/test/cert_test_util.h"
25 #include "net/url_request/certificate_report_sender.h"
26 #include "net/url_request/fraudulent_certificate_reporter.h"
27 #include "net/url_request/url_request.h"
28 #include "net/url_request/url_request_context.h"
29 #include "net/url_request/url_request_test_util.h"
30 #include "testing/gtest/include/gtest/gtest.h"
32 using chrome_browser_net::CertificateErrorReporter
;
33 using content::BrowserThread
;
38 const uint32 kServerPublicKeyVersion
= 1;
39 const uint8 kServerPublicKey
[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
40 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
41 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
43 // Builds an SSLInfo from an invalid cert chain. In this case, the cert is
44 // expired; what matters is that the cert would not pass even a normal
45 // sanity check. We test that we DO NOT send a fraudulent certificate report
47 static SSLInfo
GetBadSSLInfo() {
51 net::ImportCertFromFile(net::GetTestCertsDirectory(), "expired_cert.pem");
52 info
.cert_status
= net::CERT_STATUS_DATE_INVALID
;
53 info
.is_issued_by_known_root
= false;
58 // Builds an SSLInfo from a "good" cert chain, as defined by IsGoodSSLInfo,
59 // but which does not pass DomainState::IsChainOfPublicKeysPermitted. In this
60 // case, the certificate is for mail.google.com, signed by our Chrome test
61 // CA. During testing, Chrome believes this CA is part of the root system
62 // store. But, this CA is not in the pin list; we test that we DO send a
63 // fraudulent certicate report in this case.
64 static SSLInfo
GetGoodSSLInfo() {
67 info
.cert
= net::ImportCertFromFile(net::GetTestCertsDirectory(),
68 "test_mail_google_com.pem");
69 info
.is_issued_by_known_root
= true;
74 // Checks that |info| is good as required by the SSL checks performed in
75 // URLRequestHttpJob::OnStartCompleted, which are enough to trigger pin
76 // checking but not sufficient to pass
77 // DomainState::IsChainOfPublicKeysPermitted.
78 static bool IsGoodSSLInfo(const SSLInfo
& info
) {
79 return info
.is_valid() && info
.is_issued_by_known_root
;
82 class TestReporter
: public ChromeFraudulentCertificateReporter
{
84 explicit TestReporter(net::URLRequestContext
* request_context
)
85 : ChromeFraudulentCertificateReporter(request_context
) {}
88 class SendingTestReporter
: public TestReporter
{
90 explicit SendingTestReporter(net::URLRequestContext
* request_context
)
91 : TestReporter(request_context
), passed_(false) {}
93 // Passes if invoked with a good SSLInfo and for a hostname that is a Google
95 void SendReport(const std::string
& hostname
,
96 const SSLInfo
& ssl_info
) override
{
97 EXPECT_TRUE(IsGoodSSLInfo(ssl_info
));
98 EXPECT_TRUE(net::TransportSecurityState::IsGooglePinnedProperty(hostname
));
102 ~SendingTestReporter() override
{
103 // If the object is destroyed without having its SendReport method invoked,
105 EXPECT_TRUE(passed_
);
111 class NotSendingTestReporter
: public TestReporter
{
113 explicit NotSendingTestReporter(net::URLRequestContext
* request_context
)
114 : TestReporter(request_context
) {}
116 // Passes if invoked with a bad SSLInfo and for a hostname that is not a
117 // Google pinned property.
118 void SendReport(const std::string
& hostname
,
119 const SSLInfo
& ssl_info
) override
{
120 EXPECT_FALSE(IsGoodSSLInfo(ssl_info
));
121 EXPECT_FALSE(net::TransportSecurityState::IsGooglePinnedProperty(hostname
));
125 class MockCertificateReportSender
: public net::CertificateReportSender
{
127 MockCertificateReportSender(
128 net::URLRequestContext
* request_context
,
129 net::CertificateReportSender::CookiesPreference cookies_preference
)
130 : net::CertificateReportSender(request_context
, cookies_preference
) {}
133 scoped_ptr
<net::URLRequest
> CreateURLRequest(
134 net::URLRequestContext
* context
,
135 const GURL
& report_uri
) override
{
136 return context
->CreateRequest(GURL(std::string()), net::DEFAULT_PRIORITY
,
141 // A CertificateErrorReporter that uses a MockURLRequest, but is
142 // otherwise normal: reports are constructed and sent in the usual way.
143 class MockReporter
: public CertificateErrorReporter
{
145 explicit MockReporter(net::URLRequestContext
* request_context
)
146 : CertificateErrorReporter(
147 GURL("http://example.com"),
149 kServerPublicKeyVersion
,
150 scoped_ptr
<net::CertificateReportSender
>(
151 new MockCertificateReportSender(
153 net::CertificateReportSender::DO_NOT_SEND_COOKIES
))) {}
155 void SendReport(ReportType type
,
156 const std::string
& serialized_report
) override
{
157 EXPECT_EQ(type
, REPORT_TYPE_PINNING_VIOLATION
);
158 EXPECT_FALSE(serialized_report
.empty());
159 CertificateErrorReporter::SendReport(type
, serialized_report
);
163 static void DoReportIsSent() {
164 net::TestURLRequestContext context
;
165 SendingTestReporter
reporter(&context
);
166 SSLInfo info
= GetGoodSSLInfo();
167 reporter
.SendReport("mail.google.com", info
);
170 static void DoReportIsNotSent() {
171 net::TestURLRequestContext context
;
172 NotSendingTestReporter
reporter(&context
);
173 SSLInfo info
= GetBadSSLInfo();
174 reporter
.SendReport("www.example.com", info
);
177 static void DoMockReportIsSent() {
178 net::TestURLRequestContext context
;
179 scoped_ptr
<MockReporter
> error_reporter(new MockReporter(&context
));
180 ChromeFraudulentCertificateReporter
reporter(error_reporter
.Pass());
181 SSLInfo info
= GetGoodSSLInfo();
182 reporter
.SendReport("mail.google.com", info
);
185 TEST(ChromeFraudulentCertificateReporterTest
, GoodBadInfo
) {
186 SSLInfo good
= GetGoodSSLInfo();
187 EXPECT_TRUE(IsGoodSSLInfo(good
));
189 SSLInfo bad
= GetBadSSLInfo();
190 EXPECT_FALSE(IsGoodSSLInfo(bad
));
193 TEST(ChromeFraudulentCertificateReporterTest
, ReportIsSent
) {
194 base::MessageLoopForIO loop
;
195 content::TestBrowserThread
io_thread(BrowserThread::IO
, &loop
);
196 loop
.task_runner()->PostTask(FROM_HERE
, base::Bind(&DoReportIsSent
));
200 TEST(ChromeFraudulentCertificateReporterTest
, MockReportIsSent
) {
201 base::MessageLoopForIO loop
;
202 content::TestBrowserThread
io_thread(BrowserThread::IO
, &loop
);
203 loop
.task_runner()->PostTask(FROM_HERE
, base::Bind(&DoMockReportIsSent
));
207 TEST(ChromeFraudulentCertificateReporterTest
, ReportIsNotSent
) {
208 base::MessageLoopForIO loop
;
209 content::TestBrowserThread
io_thread(BrowserThread::IO
, &loop
);
210 loop
.task_runner()->PostTask(FROM_HERE
, base::Bind(&DoReportIsNotSent
));