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_policy.h"
7 #include "base/base_switches.h"
9 #include "base/command_line.h"
10 #include "base/memory/singleton.h"
11 #include "base/strings/string_piece.h"
12 #include "base/strings/string_util.h"
13 #include "content/browser/frame_host/navigation_entry_impl.h"
14 #include "content/browser/renderer_host/render_process_host_impl.h"
15 #include "content/browser/renderer_host/render_view_host_impl.h"
16 #include "content/browser/site_instance_impl.h"
17 #include "content/browser/ssl/ssl_cert_error_handler.h"
18 #include "content/browser/ssl/ssl_request_info.h"
19 #include "content/browser/web_contents/web_contents_impl.h"
20 #include "content/public/browser/content_browser_client.h"
21 #include "content/public/common/ssl_status.h"
22 #include "content/public/common/url_constants.h"
23 #include "net/ssl/ssl_info.h"
24 #include "webkit/common/resource_type.h"
29 SSLPolicy::SSLPolicy(SSLPolicyBackend* backend)
34 void SSLPolicy::OnCertError(SSLCertErrorHandler* handler) {
35 // First we check if we know the policy for this error.
36 net::CertPolicy::Judgment judgment = backend_->QueryPolicy(
37 handler->ssl_info().cert.get(),
38 handler->request_url().host(),
39 handler->cert_error());
41 if (judgment == net::CertPolicy::ALLOWED) {
42 handler->ContinueRequest();
46 // The judgment is either DENIED or UNKNOWN.
47 // For now we handle the DENIED as the UNKNOWN, which means a blocking
48 // page is shown to the user every time he comes back to the page.
50 switch (handler->cert_error()) {
51 case net::ERR_CERT_COMMON_NAME_INVALID:
52 case net::ERR_CERT_DATE_INVALID:
53 case net::ERR_CERT_AUTHORITY_INVALID:
54 case net::ERR_CERT_WEAK_SIGNATURE_ALGORITHM:
55 case net::ERR_CERT_WEAK_KEY:
56 OnCertErrorInternal(handler, !handler->fatal(), handler->fatal());
58 case net::ERR_CERT_NO_REVOCATION_MECHANISM:
60 handler->ContinueRequest();
62 case net::ERR_CERT_UNABLE_TO_CHECK_REVOCATION:
63 // We ignore this error but will show a warning status in the location
65 handler->ContinueRequest();
67 case net::ERR_CERT_CONTAINS_ERRORS:
68 case net::ERR_CERT_REVOKED:
69 case net::ERR_CERT_INVALID:
70 case net::ERR_SSL_WEAK_SERVER_EPHEMERAL_DH_KEY:
71 case net::ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN:
72 OnCertErrorInternal(handler, false, handler->fatal());
76 handler->CancelRequest();
81 void SSLPolicy::DidRunInsecureContent(NavigationEntryImpl* entry,
82 const std::string& security_origin) {
86 SiteInstance* site_instance = entry->site_instance();
90 backend_->HostRanInsecureContent(GURL(security_origin).host(),
91 site_instance->GetProcess()->GetID());
94 void SSLPolicy::OnRequestStarted(SSLRequestInfo* info) {
95 // TODO(abarth): This mechanism is wrong. What we should be doing is sending
96 // this information back through WebKit and out some FrameLoaderClient
99 if (net::IsCertStatusError(info->ssl_cert_status()))
100 backend_->HostRanInsecureContent(info->url().host(), info->child_id());
103 void SSLPolicy::UpdateEntry(NavigationEntryImpl* entry,
104 WebContentsImpl* web_contents) {
107 InitializeEntryIfNeeded(entry);
109 if (!entry->GetURL().SchemeIsSecure())
112 // An HTTPS response may not have a certificate for some reason. When that
113 // happens, use the unauthenticated (HTTP) rather than the authentication
114 // broken security style so that we can detect this error condition.
115 if (!entry->GetSSL().cert_id) {
116 entry->GetSSL().security_style = SECURITY_STYLE_UNAUTHENTICATED;
120 if (net::IsCertStatusError(entry->GetSSL().cert_status)) {
121 // Minor errors don't lower the security style to
122 // SECURITY_STYLE_AUTHENTICATION_BROKEN.
123 if (!net::IsCertStatusMinorError(entry->GetSSL().cert_status)) {
124 entry->GetSSL().security_style =
125 SECURITY_STYLE_AUTHENTICATION_BROKEN;
130 SiteInstance* site_instance = entry->site_instance();
131 // Note that |site_instance| can be NULL here because NavigationEntries don't
132 // necessarily have site instances. Without a process, the entry can't
133 // possibly have insecure content. See bug http://crbug.com/12423.
135 backend_->DidHostRunInsecureContent(
136 entry->GetURL().host(), site_instance->GetProcess()->GetID())) {
137 entry->GetSSL().security_style =
138 SECURITY_STYLE_AUTHENTICATION_BROKEN;
139 entry->GetSSL().content_status |= SSLStatus::RAN_INSECURE_CONTENT;
143 if (web_contents->DisplayedInsecureContent())
144 entry->GetSSL().content_status |= SSLStatus::DISPLAYED_INSECURE_CONTENT;
146 entry->GetSSL().content_status &= ~SSLStatus::DISPLAYED_INSECURE_CONTENT;
149 void SSLPolicy::OnAllowCertificate(scoped_refptr<SSLCertErrorHandler> handler,
152 // Default behavior for accepting a certificate.
153 // Note that we should not call SetMaxSecurityStyle here, because the active
154 // NavigationEntry has just been deleted (in HideInterstitialPage) and the
155 // new NavigationEntry will not be set until DidNavigate. This is ok,
156 // because the new NavigationEntry will have its max security style set
157 // within DidNavigate.
159 // While AllowCertForHost() executes synchronously on this thread,
160 // ContinueRequest() gets posted to a different thread. Calling
161 // AllowCertForHost() first ensures deterministic ordering.
162 backend_->AllowCertForHost(handler->ssl_info().cert.get(),
163 handler->request_url().host(),
164 handler->cert_error());
165 handler->ContinueRequest();
167 // Default behavior for rejecting a certificate.
169 // While DenyCertForHost() executes synchronously on this thread,
170 // CancelRequest() gets posted to a different thread. Calling
171 // DenyCertForHost() first ensures deterministic ordering.
172 backend_->DenyCertForHost(handler->ssl_info().cert.get(),
173 handler->request_url().host(),
174 handler->cert_error());
175 handler->CancelRequest();
179 ////////////////////////////////////////////////////////////////////////////////
180 // Certificate Error Routines
182 void SSLPolicy::OnCertErrorInternal(SSLCertErrorHandler* handler,
184 bool strict_enforcement) {
185 CertificateRequestResultType result =
186 CERTIFICATE_REQUEST_RESULT_TYPE_CONTINUE;
187 GetContentClient()->browser()->AllowCertificateError(
188 handler->render_process_id(),
189 handler->render_view_id(),
190 handler->cert_error(),
192 handler->request_url(),
193 handler->resource_type(),
196 base::Bind(&SSLPolicy::OnAllowCertificate, base::Unretained(this),
197 make_scoped_refptr(handler)),
200 case CERTIFICATE_REQUEST_RESULT_TYPE_CONTINUE:
202 case CERTIFICATE_REQUEST_RESULT_TYPE_CANCEL:
203 handler->CancelRequest();
205 case CERTIFICATE_REQUEST_RESULT_TYPE_DENY:
206 handler->DenyRequest();
213 void SSLPolicy::InitializeEntryIfNeeded(NavigationEntryImpl* entry) {
214 if (entry->GetSSL().security_style != SECURITY_STYLE_UNKNOWN)
217 entry->GetSSL().security_style = entry->GetURL().SchemeIsSecure() ?
218 SECURITY_STYLE_AUTHENTICATED : SECURITY_STYLE_UNAUTHENTICATED;
221 void SSLPolicy::OriginRanInsecureContent(const std::string& origin, int pid) {
222 GURL parsed_origin(origin);
223 if (parsed_origin.SchemeIsSecure())
224 backend_->HostRanInsecureContent(parsed_origin.host(), pid);
227 } // namespace content