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/net/chrome_fraudulent_certificate_reporter.h"
9 #include "base/base64.h"
10 #include "base/logging.h"
11 #include "base/profiler/scoped_tracker.h"
12 #include "base/stl_util.h"
13 #include "base/time/time.h"
14 #include "chrome/browser/net/cert_logger.pb.h"
15 #include "net/base/elements_upload_data_stream.h"
16 #include "net/base/load_flags.h"
17 #include "net/base/request_priority.h"
18 #include "net/base/upload_bytes_element_reader.h"
19 #include "net/cert/x509_certificate.h"
20 #include "net/ssl/ssl_info.h"
21 #include "net/url_request/url_request_context.h"
23 namespace chrome_browser_net
{
25 // TODO(palmer): Switch to HTTPS when the error handling delegate is more
26 // sophisticated. Ultimately we plan to attempt the report on many transports.
27 static const char kFraudulentCertificateUploadEndpoint
[] =
28 "http://clients3.google.com/log_cert_error";
30 ChromeFraudulentCertificateReporter::ChromeFraudulentCertificateReporter(
31 net::URLRequestContext
* request_context
)
32 : request_context_(request_context
),
33 upload_url_(kFraudulentCertificateUploadEndpoint
) {
36 ChromeFraudulentCertificateReporter::~ChromeFraudulentCertificateReporter() {
37 STLDeleteElements(&inflight_requests_
);
40 static std::string
BuildReport(const std::string
& hostname
,
41 const net::SSLInfo
& ssl_info
) {
42 CertLoggerRequest request
;
43 base::Time now
= base::Time::Now();
44 request
.set_time_usec(now
.ToInternalValue());
45 request
.set_hostname(hostname
);
47 std::vector
<std::string
> pem_encoded_chain
;
48 if (!ssl_info
.cert
->GetPEMEncodedChain(&pem_encoded_chain
)) {
49 LOG(ERROR
) << "Could not get PEM encoded chain.";
51 std::string
* cert_chain
= request
.mutable_cert_chain();
52 for (size_t i
= 0; i
< pem_encoded_chain
.size(); ++i
)
53 *cert_chain
+= pem_encoded_chain
[i
];
55 request
.add_pin(ssl_info
.pinning_failure_log
);
58 request
.SerializeToString(&out
);
62 scoped_ptr
<net::URLRequest
>
63 ChromeFraudulentCertificateReporter::CreateURLRequest(
64 net::URLRequestContext
* context
) {
65 scoped_ptr
<net::URLRequest
> request
=
66 context
->CreateRequest(upload_url_
, net::DEFAULT_PRIORITY
, this, NULL
);
67 request
->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES
|
68 net::LOAD_DO_NOT_SAVE_COOKIES
);
69 return request
.Pass();
72 void ChromeFraudulentCertificateReporter::SendReport(
73 const std::string
& hostname
,
74 const net::SSLInfo
& ssl_info
) {
75 // We do silent/automatic reporting ONLY for Google properties. For other
76 // domains (when we start supporting that), we will ask for user permission.
77 if (!net::TransportSecurityState::IsGooglePinnedProperty(hostname
)) {
81 std::string report
= BuildReport(hostname
, ssl_info
);
83 scoped_ptr
<net::URLRequest
> url_request
= CreateURLRequest(request_context_
);
84 url_request
->set_method("POST");
86 scoped_ptr
<net::UploadElementReader
> reader(
87 net::UploadOwnedBytesElementReader::CreateWithString(report
));
88 url_request
->set_upload(
89 net::ElementsUploadDataStream::CreateWithReader(reader
.Pass(), 0));
91 net::HttpRequestHeaders headers
;
92 headers
.SetHeader(net::HttpRequestHeaders::kContentType
,
93 "x-application/chrome-fraudulent-cert-report");
94 url_request
->SetExtraRequestHeaders(headers
);
96 net::URLRequest
* raw_url_request
= url_request
.get();
97 inflight_requests_
.insert(url_request
.release());
98 raw_url_request
->Start();
101 void ChromeFraudulentCertificateReporter::RequestComplete(
102 net::URLRequest
* request
) {
103 std::set
<net::URLRequest
*>::iterator i
= inflight_requests_
.find(request
);
104 DCHECK(i
!= inflight_requests_
.end());
105 scoped_ptr
<net::URLRequest
> url_request(*i
);
106 inflight_requests_
.erase(i
);
109 // TODO(palmer): Currently, the upload is fire-and-forget but soon we will
110 // try to recover by retrying, and trying different endpoints, and
111 // appealing to the user.
112 void ChromeFraudulentCertificateReporter::OnResponseStarted(
113 net::URLRequest
* request
) {
114 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
115 tracked_objects::ScopedTracker
tracking_profile(
116 FROM_HERE_WITH_EXPLICIT_FUNCTION(
117 "422516 ChromeFraudulentCertificateReporter::OnResponseStarted"));
119 const net::URLRequestStatus
& status(request
->status());
120 if (!status
.is_success()) {
121 LOG(WARNING
) << "Certificate upload failed"
122 << " status:" << status
.status()
123 << " error:" << status
.error();
124 } else if (request
->GetResponseCode() != 200) {
125 LOG(WARNING
) << "Certificate upload HTTP status: "
126 << request
->GetResponseCode();
128 RequestComplete(request
);
131 void ChromeFraudulentCertificateReporter::OnReadCompleted(
132 net::URLRequest
* request
, int bytes_read
) {}
134 } // namespace chrome_browser_net