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"
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"
32 const char kSSLManagerKeyName
[] = "content_ssl_manager";
34 class SSLManagerSet
: public base::SupportsUserData::Data
{
39 std::set
<SSLManager
*>& get() { return set_
; }
42 std::set
<SSLManager
*> set_
;
44 DISALLOW_COPY_AND_ASSIGN(SSLManagerSet
);
50 void SSLManager::OnSSLCertificateError(
51 const base::WeakPtr
<SSLErrorHandler::Delegate
>& delegate
,
52 const ResourceType resource_type
,
54 int render_process_id
,
56 const net::SSLInfo
& ssl_info
,
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
,
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
) {
98 SSLManagerSet
* managers
= static_cast<SSLManagerSet
*>(
99 controller_
->GetBrowserContext()->GetUserData(kSSLManagerKeyName
));
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
) {
118 // We may not have an entry if this is a navigation to an initial blank
119 // page. Add the new data we have.
120 entry
->GetSSL() = details
.ssl_status
;
124 policy()->UpdateEntry(entry
, controller_
->delegate()->GetWebContents());
125 // Always notify the WebContents that the SSL state changed when a
126 // load is committed, in case the active navigation entry has changed.
127 NotifyDidChangeVisibleSSLState();
130 void SSLManager::DidDisplayInsecureContent() {
131 UpdateEntry(controller_
->GetLastCommittedEntry());
134 void SSLManager::DidRunInsecureContent(const std::string
& security_origin
) {
135 NavigationEntryImpl
* navigation_entry
= controller_
->GetLastCommittedEntry();
136 policy()->DidRunInsecureContent(navigation_entry
, security_origin
);
137 UpdateEntry(navigation_entry
);
140 void SSLManager::DidLoadFromMemoryCache(
141 const LoadFromMemoryCacheDetails
& details
) {
142 // Simulate loading this resource through the usual path.
143 // Note that we specify SUB_RESOURCE as the resource type as WebCore only
144 // caches sub-resources.
145 // This resource must have been loaded with no filtering because filtered
146 // resouces aren't cachable.
147 scoped_refptr
<SSLRequestInfo
> info(new SSLRequestInfo(
149 RESOURCE_TYPE_SUB_RESOURCE
,
152 details
.cert_status
));
154 // Simulate loading this resource through the usual path.
155 policy()->OnRequestStarted(info
.get());
158 void SSLManager::DidStartResourceResponse(
159 const ResourceRequestDetails
& details
) {
160 scoped_refptr
<SSLRequestInfo
> info(new SSLRequestInfo(
162 details
.resource_type
,
163 details
.origin_child_id
,
165 details
.ssl_cert_status
));
167 // Notify our policy that we started a resource request. Ideally, the
168 // policy should have the ability to cancel the request, but we can't do
170 policy()->OnRequestStarted(info
.get());
173 void SSLManager::DidReceiveResourceRedirect(
174 const ResourceRedirectDetails
& details
) {
175 // TODO(abarth): Make sure our redirect behavior is correct. If we ever see a
176 // non-HTTPS resource in the redirect chain, we want to trigger
177 // insecure content, even if the redirect chain goes back to
178 // HTTPS. This is because the network attacker can redirect the
179 // HTTP request to https://attacker.com/payload.js.
182 void SSLManager::UpdateEntry(NavigationEntryImpl
* entry
) {
183 // We don't always have a navigation entry to update, for example in the
184 // case of the Web Inspector.
188 SSLStatus original_ssl_status
= entry
->GetSSL(); // Copy!
190 policy()->UpdateEntry(entry
, controller_
->delegate()->GetWebContents());
192 if (!entry
->GetSSL().Equals(original_ssl_status
))
193 NotifyDidChangeVisibleSSLState();
196 void SSLManager::NotifyDidChangeVisibleSSLState() {
197 WebContentsImpl
* contents
=
198 static_cast<WebContentsImpl
*>(controller_
->delegate()->GetWebContents());
199 contents
->DidChangeVisibleSSLState();
202 } // namespace content