Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / ssl / ssl_manager.cc
blob865ca6027a2e631637056b803fff0a9b7f724c20
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 "content/browser/ssl/ssl_manager.h"
7 #include <set>
9 #include "base/bind.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/supports_user_data.h"
12 #include "content/browser/frame_host/navigation_entry_impl.h"
13 #include "content/browser/loader/resource_dispatcher_host_impl.h"
14 #include "content/browser/loader/resource_request_info_impl.h"
15 #include "content/browser/ssl/ssl_cert_error_handler.h"
16 #include "content/browser/ssl/ssl_policy.h"
17 #include "content/browser/ssl/ssl_request_info.h"
18 #include "content/browser/web_contents/web_contents_impl.h"
19 #include "content/common/ssl_status_serialization.h"
20 #include "content/public/browser/browser_context.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/load_from_memory_cache_details.h"
23 #include "content/public/browser/navigation_details.h"
24 #include "content/public/browser/resource_request_details.h"
25 #include "content/public/common/ssl_status.h"
26 #include "net/url_request/url_request.h"
28 namespace content {
30 namespace {
32 const char kSSLManagerKeyName[] = "content_ssl_manager";
34 class SSLManagerSet : public base::SupportsUserData::Data {
35 public:
36 SSLManagerSet() {
39 std::set<SSLManager*>& get() { return set_; }
41 private:
42 std::set<SSLManager*> set_;
44 DISALLOW_COPY_AND_ASSIGN(SSLManagerSet);
47 } // namespace
49 // static
50 void SSLManager::OnSSLCertificateError(
51 const base::WeakPtr<SSLErrorHandler::Delegate>& delegate,
52 const ResourceType resource_type,
53 const GURL& url,
54 int render_process_id,
55 int render_frame_id,
56 const net::SSLInfo& ssl_info,
57 bool fatal) {
58 DCHECK(delegate.get());
59 DVLOG(1) << "OnSSLCertificateError() cert_error: "
60 << net::MapCertStatusToNetError(ssl_info.cert_status)
61 << " resource_type: " << resource_type
62 << " url: " << url.spec()
63 << " render_process_id: " << render_process_id
64 << " render_frame_id: " << render_frame_id
65 << " cert_status: " << std::hex << ssl_info.cert_status;
67 // A certificate error occurred. Construct a SSLCertErrorHandler object and
68 // hand it over to the UI thread for processing.
69 BrowserThread::PostTask(
70 BrowserThread::UI, FROM_HERE,
71 base::Bind(&SSLCertErrorHandler::Dispatch,
72 new SSLCertErrorHandler(delegate,
73 resource_type,
74 url,
75 render_process_id,
76 render_frame_id,
77 ssl_info,
78 fatal)));
81 // static
82 void SSLManager::NotifySSLInternalStateChanged(BrowserContext* context) {
83 SSLManagerSet* managers = static_cast<SSLManagerSet*>(
84 context->GetUserData(kSSLManagerKeyName));
86 for (std::set<SSLManager*>::iterator i = managers->get().begin();
87 i != managers->get().end(); ++i) {
88 (*i)->UpdateEntry((*i)->controller()->GetLastCommittedEntry());
92 SSLManager::SSLManager(NavigationControllerImpl* controller)
93 : backend_(controller),
94 policy_(new SSLPolicy(&backend_)),
95 controller_(controller) {
96 DCHECK(controller_);
98 SSLManagerSet* managers = static_cast<SSLManagerSet*>(
99 controller_->GetBrowserContext()->GetUserData(kSSLManagerKeyName));
100 if (!managers) {
101 managers = new SSLManagerSet;
102 controller_->GetBrowserContext()->SetUserData(kSSLManagerKeyName, managers);
104 managers->get().insert(this);
107 SSLManager::~SSLManager() {
108 SSLManagerSet* managers = static_cast<SSLManagerSet*>(
109 controller_->GetBrowserContext()->GetUserData(kSSLManagerKeyName));
110 managers->get().erase(this);
113 void SSLManager::DidCommitProvisionalLoad(const LoadCommittedDetails& details) {
114 NavigationEntryImpl* entry = controller_->GetLastCommittedEntry();
116 if (details.is_main_frame) {
117 if (entry) {
118 // Decode the security details.
119 int ssl_cert_id;
120 net::CertStatus ssl_cert_status;
121 int ssl_security_bits;
122 int ssl_connection_status;
123 SignedCertificateTimestampIDStatusList
124 ssl_signed_certificate_timestamp_ids;
125 DeserializeSecurityInfo(details.serialized_security_info,
126 &ssl_cert_id,
127 &ssl_cert_status,
128 &ssl_security_bits,
129 &ssl_connection_status,
130 &ssl_signed_certificate_timestamp_ids);
132 // We may not have an entry if this is a navigation to an initial blank
133 // page. Reset the SSL information and add the new data we have.
134 entry->GetSSL() = SSLStatus();
135 entry->GetSSL().cert_id = ssl_cert_id;
136 entry->GetSSL().cert_status = ssl_cert_status;
137 entry->GetSSL().security_bits = ssl_security_bits;
138 entry->GetSSL().connection_status = ssl_connection_status;
139 entry->GetSSL().signed_certificate_timestamp_ids =
140 ssl_signed_certificate_timestamp_ids;
144 UpdateEntry(entry);
147 void SSLManager::DidDisplayInsecureContent() {
148 UpdateEntry(controller_->GetLastCommittedEntry());
151 void SSLManager::DidRunInsecureContent(const std::string& security_origin) {
152 NavigationEntryImpl* navigation_entry = controller_->GetLastCommittedEntry();
153 policy()->DidRunInsecureContent(navigation_entry, security_origin);
154 UpdateEntry(navigation_entry);
157 void SSLManager::DidLoadFromMemoryCache(
158 const LoadFromMemoryCacheDetails& details) {
159 // Simulate loading this resource through the usual path.
160 // Note that we specify SUB_RESOURCE as the resource type as WebCore only
161 // caches sub-resources.
162 // This resource must have been loaded with no filtering because filtered
163 // resouces aren't cachable.
164 scoped_refptr<SSLRequestInfo> info(new SSLRequestInfo(
165 details.url,
166 RESOURCE_TYPE_SUB_RESOURCE,
167 details.pid,
168 details.cert_id,
169 details.cert_status));
171 // Simulate loading this resource through the usual path.
172 policy()->OnRequestStarted(info.get());
175 void SSLManager::DidStartResourceResponse(
176 const ResourceRequestDetails& details) {
177 scoped_refptr<SSLRequestInfo> info(new SSLRequestInfo(
178 details.url,
179 details.resource_type,
180 details.origin_child_id,
181 details.ssl_cert_id,
182 details.ssl_cert_status));
184 // Notify our policy that we started a resource request. Ideally, the
185 // policy should have the ability to cancel the request, but we can't do
186 // that yet.
187 policy()->OnRequestStarted(info.get());
190 void SSLManager::DidReceiveResourceRedirect(
191 const ResourceRedirectDetails& details) {
192 // TODO(abarth): Make sure our redirect behavior is correct. If we ever see a
193 // non-HTTPS resource in the redirect chain, we want to trigger
194 // insecure content, even if the redirect chain goes back to
195 // HTTPS. This is because the network attacker can redirect the
196 // HTTP request to https://attacker.com/payload.js.
199 void SSLManager::UpdateEntry(NavigationEntryImpl* entry) {
200 // We don't always have a navigation entry to update, for example in the
201 // case of the Web Inspector.
202 if (!entry)
203 return;
205 SSLStatus original_ssl_status = entry->GetSSL(); // Copy!
207 WebContentsImpl* contents =
208 static_cast<WebContentsImpl*>(controller_->delegate()->GetWebContents());
209 policy()->UpdateEntry(entry, contents);
211 if (!entry->GetSSL().Equals(original_ssl_status))
212 contents->DidChangeVisibleSSLState();
215 } // namespace content