Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / ssl / ssl_manager.cc
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.
4
5 #include "content/browser/ssl/ssl_manager.h"
6
7 #include <set>
8
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"
27
28 namespace content {
29
30 namespace {
31
32 const char kSSLManagerKeyName[] = "content_ssl_manager";
33
34 class SSLManagerSet : public base::SupportsUserData::Data {
35  public:
36   SSLManagerSet() {
37   }
38
39   std::set<SSLManager*>& get() { return set_; }
40
41  private:
42   std::set<SSLManager*> set_;
43
44   DISALLOW_COPY_AND_ASSIGN(SSLManagerSet);
45 };
46
47 }  // namespace
48
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;
66
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)));
79 }
80
81 // static
82 void SSLManager::NotifySSLInternalStateChanged(BrowserContext* context) {
83   SSLManagerSet* managers = static_cast<SSLManagerSet*>(
84       context->GetUserData(kSSLManagerKeyName));
85
86   for (std::set<SSLManager*>::iterator i = managers->get().begin();
87        i != managers->get().end(); ++i) {
88     (*i)->UpdateEntry(NavigationEntryImpl::FromNavigationEntry(
89                           (*i)->controller()->GetLastCommittedEntry()));
90   }
91 }
92
93 SSLManager::SSLManager(NavigationControllerImpl* controller)
94     : backend_(controller),
95       policy_(new SSLPolicy(&backend_)),
96       controller_(controller) {
97   DCHECK(controller_);
98
99   SSLManagerSet* managers = static_cast<SSLManagerSet*>(
100       controller_->GetBrowserContext()->GetUserData(kSSLManagerKeyName));
101   if (!managers) {
102     managers = new SSLManagerSet;
103     controller_->GetBrowserContext()->SetUserData(kSSLManagerKeyName, managers);
104   }
105   managers->get().insert(this);
106 }
107
108 SSLManager::~SSLManager() {
109   SSLManagerSet* managers = static_cast<SSLManagerSet*>(
110       controller_->GetBrowserContext()->GetUserData(kSSLManagerKeyName));
111   managers->get().erase(this);
112 }
113
114 void SSLManager::DidCommitProvisionalLoad(const LoadCommittedDetails& details) {
115   NavigationEntryImpl* entry =
116       NavigationEntryImpl::FromNavigationEntry(
117           controller_->GetLastCommittedEntry());
118
119   if (details.is_main_frame) {
120     if (entry) {
121       // Decode the security details.
122       int ssl_cert_id;
123       net::CertStatus ssl_cert_status;
124       int ssl_security_bits;
125       int ssl_connection_status;
126       SignedCertificateTimestampIDStatusList
127           ssl_signed_certificate_timestamp_ids;
128       DeserializeSecurityInfo(details.serialized_security_info,
129                               &ssl_cert_id,
130                               &ssl_cert_status,
131                               &ssl_security_bits,
132                               &ssl_connection_status,
133                               &ssl_signed_certificate_timestamp_ids);
134
135       // We may not have an entry if this is a navigation to an initial blank
136       // page. Reset the SSL information and add the new data we have.
137       entry->GetSSL() = SSLStatus();
138       entry->GetSSL().cert_id = ssl_cert_id;
139       entry->GetSSL().cert_status = ssl_cert_status;
140       entry->GetSSL().security_bits = ssl_security_bits;
141       entry->GetSSL().connection_status = ssl_connection_status;
142       entry->GetSSL().signed_certificate_timestamp_ids =
143           ssl_signed_certificate_timestamp_ids;
144     }
145   }
146
147   UpdateEntry(entry);
148 }
149
150 void SSLManager::DidDisplayInsecureContent() {
151   UpdateEntry(
152       NavigationEntryImpl::FromNavigationEntry(
153           controller_->GetLastCommittedEntry()));
154 }
155
156 void SSLManager::DidRunInsecureContent(const std::string& security_origin) {
157   NavigationEntryImpl* navigation_entry =
158       NavigationEntryImpl::FromNavigationEntry(
159           controller_->GetLastCommittedEntry());
160   policy()->DidRunInsecureContent(navigation_entry, security_origin);
161   UpdateEntry(navigation_entry);
162 }
163
164 void SSLManager::DidLoadFromMemoryCache(
165     const LoadFromMemoryCacheDetails& details) {
166   // Simulate loading this resource through the usual path.
167   // Note that we specify SUB_RESOURCE as the resource type as WebCore only
168   // caches sub-resources.
169   // This resource must have been loaded with no filtering because filtered
170   // resouces aren't cachable.
171   scoped_refptr<SSLRequestInfo> info(new SSLRequestInfo(
172       details.url,
173       RESOURCE_TYPE_SUB_RESOURCE,
174       details.pid,
175       details.cert_id,
176       details.cert_status));
177
178   // Simulate loading this resource through the usual path.
179   policy()->OnRequestStarted(info.get());
180 }
181
182 void SSLManager::DidStartResourceResponse(
183     const ResourceRequestDetails& details) {
184   scoped_refptr<SSLRequestInfo> info(new SSLRequestInfo(
185       details.url,
186       details.resource_type,
187       details.origin_child_id,
188       details.ssl_cert_id,
189       details.ssl_cert_status));
190
191   // Notify our policy that we started a resource request.  Ideally, the
192   // policy should have the ability to cancel the request, but we can't do
193   // that yet.
194   policy()->OnRequestStarted(info.get());
195 }
196
197 void SSLManager::DidReceiveResourceRedirect(
198     const ResourceRedirectDetails& details) {
199   // TODO(abarth): Make sure our redirect behavior is correct.  If we ever see a
200   //               non-HTTPS resource in the redirect chain, we want to trigger
201   //               insecure content, even if the redirect chain goes back to
202   //               HTTPS.  This is because the network attacker can redirect the
203   //               HTTP request to https://attacker.com/payload.js.
204 }
205
206 void SSLManager::UpdateEntry(NavigationEntryImpl* entry) {
207   // We don't always have a navigation entry to update, for example in the
208   // case of the Web Inspector.
209   if (!entry)
210     return;
211
212   SSLStatus original_ssl_status = entry->GetSSL();  // Copy!
213
214   WebContentsImpl* contents =
215       static_cast<WebContentsImpl*>(controller_->delegate()->GetWebContents());
216   policy()->UpdateEntry(entry, contents);
217
218   if (!entry->GetSSL().Equals(original_ssl_status))
219     contents->DidChangeVisibleSSLState();
220 }
221
222 }  // namespace content