Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: chrome/browser/ssl/security_state_model.cc

Issue 1314843007: Refactor connection_security into SecurityStateModel (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: pkasting comments Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ssl/connection_security.h" 5 #include "chrome/browser/ssl/security_state_model.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/metrics/field_trial.h" 8 #include "base/metrics/field_trial.h"
9 #include "base/metrics/histogram_macros.h" 9 #include "base/metrics/histogram_macros.h"
10 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
11 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ssl/ssl_error_info.h"
13 #include "chrome/common/chrome_constants.h" 12 #include "chrome/common/chrome_constants.h"
14 #include "chrome/common/chrome_switches.h" 13 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/pref_names.h" 14 #include "chrome/common/pref_names.h"
15 #include "chrome/common/pref_names.h"
16 #include "content/public/browser/cert_store.h" 16 #include "content/public/browser/cert_store.h"
17 #include "content/public/browser/navigation_controller.h"
18 #include "content/public/browser/navigation_entry.h" 17 #include "content/public/browser/navigation_entry.h"
19 #include "content/public/browser/web_contents.h" 18 #include "content/public/browser/web_contents.h"
20 #include "content/public/common/origin_util.h" 19 #include "content/public/common/origin_util.h"
21 #include "content/public/common/ssl_status.h"
22 #include "net/base/net_util.h"
23 #include "net/cert/cert_status_flags.h"
24 #include "net/cert/x509_certificate.h"
25 #include "net/ssl/ssl_connection_status_flags.h" 20 #include "net/ssl/ssl_connection_status_flags.h"
26 21
27 #if defined(OS_CHROMEOS) 22 #if defined(OS_CHROMEOS)
28 #include "chrome/browser/chromeos/policy/policy_cert_service.h" 23 #include "chrome/browser/chromeos/policy/policy_cert_service.h"
29 #include "chrome/browser/chromeos/policy/policy_cert_service_factory.h" 24 #include "chrome/browser/chromeos/policy/policy_cert_service_factory.h"
30 #endif 25 #endif
31 26
32 namespace { 27 namespace {
33 28
34 connection_security::SecurityLevel GetSecurityLevelForNonSecureFieldTrial() { 29 SecurityStateModel::SecurityLevel GetSecurityLevelForNonSecureFieldTrial() {
35 std::string choice = 30 std::string choice =
36 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 31 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
37 switches::kMarkNonSecureAs); 32 switches::kMarkNonSecureAs);
38 std::string group = base::FieldTrialList::FindFullName("MarkNonSecureAs"); 33 std::string group = base::FieldTrialList::FindFullName("MarkNonSecureAs");
39 34
40 // Do not change this enum. It is used in the histogram. 35 // Do not change this enum. It is used in the histogram.
41 enum MarkNonSecureStatus { NEUTRAL, DUBIOUS, NON_SECURE, LAST_STATUS }; 36 enum MarkNonSecureStatus { NEUTRAL, DUBIOUS, NON_SECURE, LAST_STATUS };
42 const char kEnumeration[] = "MarkNonSecureAs"; 37 const char kEnumeration[] = "MarkNonSecureAs";
43 38
44 connection_security::SecurityLevel level; 39 SecurityStateModel::SecurityLevel level = SecurityStateModel::NONE;
45 MarkNonSecureStatus status; 40 MarkNonSecureStatus status;
46 41
47 if (choice == switches::kMarkNonSecureAsNeutral) { 42 if (choice == switches::kMarkNonSecureAsNeutral) {
48 status = NEUTRAL; 43 status = NEUTRAL;
49 level = connection_security::NONE; 44 level = SecurityStateModel::NONE;
50 } else if (choice == switches::kMarkNonSecureAsNonSecure) { 45 } else if (choice == switches::kMarkNonSecureAsNonSecure) {
51 status = NON_SECURE; 46 status = NON_SECURE;
52 level = connection_security::SECURITY_ERROR; 47 level = SecurityStateModel::SECURITY_ERROR;
53 } else if (group == switches::kMarkNonSecureAsNeutral) { 48 } else if (group == switches::kMarkNonSecureAsNeutral) {
54 status = NEUTRAL; 49 status = NEUTRAL;
55 level = connection_security::NONE; 50 level = SecurityStateModel::NONE;
56 } else if (group == switches::kMarkNonSecureAsNonSecure) { 51 } else if (group == switches::kMarkNonSecureAsNonSecure) {
57 status = NON_SECURE; 52 status = NON_SECURE;
58 level = connection_security::SECURITY_ERROR; 53 level = SecurityStateModel::SECURITY_ERROR;
59 } else { 54 } else {
60 status = NEUTRAL; 55 status = NEUTRAL;
61 level = connection_security::NONE; 56 level = SecurityStateModel::NONE;
62 } 57 }
63 58
64 UMA_HISTOGRAM_ENUMERATION(kEnumeration, status, LAST_STATUS); 59 UMA_HISTOGRAM_ENUMERATION(kEnumeration, status, LAST_STATUS);
65 return level; 60 return level;
66 } 61 }
67 62
68 scoped_refptr<net::X509Certificate> GetCertForSSLStatus( 63 scoped_refptr<net::X509Certificate> GetCertForSSLStatus(
69 const content::SSLStatus& ssl) { 64 const content::SSLStatus& ssl) {
70 scoped_refptr<net::X509Certificate> cert; 65 scoped_refptr<net::X509Certificate> cert;
71 return content::CertStore::GetInstance()->RetrieveCert(ssl.cert_id, &cert) 66 return content::CertStore::GetInstance()->RetrieveCert(ssl.cert_id, &cert)
72 ? cert 67 ? cert
73 : nullptr; 68 : nullptr;
74 } 69 }
75 70
76 connection_security::SHA1DeprecationStatus GetSHA1DeprecationStatus( 71 SecurityStateModel::SHA1DeprecationStatus GetSHA1DeprecationStatus(
77 scoped_refptr<net::X509Certificate> cert, 72 scoped_refptr<net::X509Certificate> cert,
78 const content::SSLStatus& ssl) { 73 const content::SSLStatus& ssl) {
79 if (!cert || !(ssl.cert_status & net::CERT_STATUS_SHA1_SIGNATURE_PRESENT)) 74 if (!cert || !(ssl.cert_status & net::CERT_STATUS_SHA1_SIGNATURE_PRESENT))
80 return connection_security::NO_DEPRECATED_SHA1; 75 return SecurityStateModel::NO_DEPRECATED_SHA1;
81 76
82 // The internal representation of the dates for UI treatment of SHA-1. 77 // The internal representation of the dates for UI treatment of SHA-1.
83 // See http://crbug.com/401365 for details. 78 // See http://crbug.com/401365 for details.
84 static const int64_t kJanuary2017 = INT64_C(13127702400000000); 79 static const int64_t kJanuary2017 = INT64_C(13127702400000000);
85 if (cert->valid_expiry() >= base::Time::FromInternalValue(kJanuary2017)) 80 if (cert->valid_expiry() >= base::Time::FromInternalValue(kJanuary2017))
86 return connection_security::DEPRECATED_SHA1_BROKEN; 81 return SecurityStateModel::DEPRECATED_SHA1_BROKEN;
87 // kJanuary2016 needs to be kept in sync with
88 // ToolbarModelAndroid::IsDeprecatedSHA1Present().
89 static const int64_t kJanuary2016 = INT64_C(13096080000000000); 82 static const int64_t kJanuary2016 = INT64_C(13096080000000000);
90 if (cert->valid_expiry() >= base::Time::FromInternalValue(kJanuary2016)) 83 if (cert->valid_expiry() >= base::Time::FromInternalValue(kJanuary2016))
91 return connection_security::DEPRECATED_SHA1_WARNING; 84 return SecurityStateModel::DEPRECATED_SHA1_WARNING;
92 85
93 return connection_security::NO_DEPRECATED_SHA1; 86 return SecurityStateModel::NO_DEPRECATED_SHA1;
94 } 87 }
95 88
96 connection_security::MixedContentStatus GetMixedContentStatus( 89 SecurityStateModel::MixedContentStatus GetMixedContentStatus(
97 const content::SSLStatus& ssl) { 90 const content::SSLStatus& ssl) {
98 bool ran_insecure_content = false; 91 bool ran_insecure_content = false;
99 bool displayed_insecure_content = false; 92 bool displayed_insecure_content = false;
100 if (ssl.content_status & content::SSLStatus::RAN_INSECURE_CONTENT) 93 if (ssl.content_status & content::SSLStatus::RAN_INSECURE_CONTENT)
101 ran_insecure_content = true; 94 ran_insecure_content = true;
102 if (ssl.content_status & content::SSLStatus::DISPLAYED_INSECURE_CONTENT) 95 if (ssl.content_status & content::SSLStatus::DISPLAYED_INSECURE_CONTENT)
103 displayed_insecure_content = true; 96 displayed_insecure_content = true;
104 97
105 if (ran_insecure_content && displayed_insecure_content) 98 if (ran_insecure_content && displayed_insecure_content)
106 return connection_security::RAN_AND_DISPLAYED_MIXED_CONTENT; 99 return SecurityStateModel::RAN_AND_DISPLAYED_MIXED_CONTENT;
107 if (ran_insecure_content) 100 if (ran_insecure_content)
108 return connection_security::RAN_MIXED_CONTENT; 101 return SecurityStateModel::RAN_MIXED_CONTENT;
109 if (displayed_insecure_content) 102 if (displayed_insecure_content)
110 return connection_security::DISPLAYED_MIXED_CONTENT; 103 return SecurityStateModel::DISPLAYED_MIXED_CONTENT;
111 104
112 return connection_security::NO_MIXED_CONTENT; 105 return SecurityStateModel::NO_MIXED_CONTENT;
113 } 106 }
114 107
115 } // namespace 108 SecurityStateModel::SecurityLevel GetSecurityLevelForRequest(
116 109 const GURL& url,
117 namespace connection_security { 110 const content::SSLStatus& ssl,
118 111 Profile* profile,
119 SecurityLevel GetSecurityLevelForWebContents( 112 scoped_refptr<net::X509Certificate> cert,
120 const content::WebContents* web_contents) { 113 SecurityStateModel::SHA1DeprecationStatus sha1_status,
121 if (!web_contents) 114 SecurityStateModel::MixedContentStatus mixed_content_status) {
122 return NONE;
123
124 content::NavigationEntry* entry =
125 web_contents->GetController().GetVisibleEntry();
126 if (!entry)
127 return NONE;
128
129 const content::SSLStatus& ssl = entry->GetSSL();
130 switch (ssl.security_style) { 115 switch (ssl.security_style) {
131 case content::SECURITY_STYLE_UNKNOWN: 116 case content::SECURITY_STYLE_UNKNOWN:
132 return NONE; 117 return SecurityStateModel::NONE;
133 118
134 case content::SECURITY_STYLE_UNAUTHENTICATED: { 119 case content::SECURITY_STYLE_UNAUTHENTICATED: {
135 const GURL& url = entry->GetURL();
136 if (!content::IsOriginSecure(url) && url.IsStandard()) 120 if (!content::IsOriginSecure(url) && url.IsStandard())
137 return GetSecurityLevelForNonSecureFieldTrial(); 121 return GetSecurityLevelForNonSecureFieldTrial();
138 return NONE; 122 return SecurityStateModel::NONE;
139 } 123 }
140 124
141 case content::SECURITY_STYLE_AUTHENTICATION_BROKEN: 125 case content::SECURITY_STYLE_AUTHENTICATION_BROKEN:
142 return SECURITY_ERROR; 126 return SecurityStateModel::SECURITY_ERROR;
127
128 case content::SECURITY_STYLE_WARNING:
129 NOTREACHED();
130 return SecurityStateModel::SECURITY_WARNING;
143 131
144 case content::SECURITY_STYLE_AUTHENTICATED: { 132 case content::SECURITY_STYLE_AUTHENTICATED: {
145 #if defined(OS_CHROMEOS) 133 #if defined(OS_CHROMEOS)
146 // Report if there is a policy cert first, before reporting any other 134 // Report if there is a policy cert first, before reporting any other
147 // authenticated-but-with-errors cases. A policy cert is a strong 135 // authenticated-but-with-errors cases. A policy cert is a strong
148 // indicator of a MITM being present (the enterprise), while the 136 // indicator of a MITM being present (the enterprise), while the
149 // other authenticated-but-with-errors indicate something may 137 // other authenticated-but-with-errors indicate something may
150 // be wrong, or may be wrong in the future, but is unclear now. 138 // be wrong, or may be wrong in the future, but is unclear now.
151 policy::PolicyCertService* service = 139 policy::PolicyCertService* service =
152 policy::PolicyCertServiceFactory::GetForProfile( 140 policy::PolicyCertServiceFactory::GetForProfile(profile);
153 Profile::FromBrowserContext(web_contents->GetBrowserContext()));
154 if (service && service->UsedPolicyCertificates()) 141 if (service && service->UsedPolicyCertificates())
155 return SECURITY_POLICY_WARNING; 142 return SecurityStateModel::SECURITY_POLICY_WARNING;
156 #endif 143 #endif
157 144
158 scoped_refptr<net::X509Certificate> cert = GetCertForSSLStatus(ssl); 145 if (sha1_status == SecurityStateModel::DEPRECATED_SHA1_BROKEN)
159 SHA1DeprecationStatus sha1_status = GetSHA1DeprecationStatus(cert, ssl); 146 return SecurityStateModel::SECURITY_ERROR;
160 if (sha1_status == DEPRECATED_SHA1_BROKEN) 147 if (sha1_status == SecurityStateModel::DEPRECATED_SHA1_WARNING)
161 return SECURITY_ERROR; 148 return SecurityStateModel::NONE;
162 if (sha1_status == DEPRECATED_SHA1_WARNING)
163 return NONE;
164 149
165 MixedContentStatus mixed_content_status = GetMixedContentStatus(ssl);
166 // Active mixed content is downgraded to the BROKEN style and 150 // Active mixed content is downgraded to the BROKEN style and
167 // handled above. 151 // handled above.
168 DCHECK_NE(RAN_MIXED_CONTENT, mixed_content_status); 152 DCHECK_NE(SecurityStateModel::RAN_MIXED_CONTENT, mixed_content_status);
169 DCHECK_NE(RAN_AND_DISPLAYED_MIXED_CONTENT, mixed_content_status); 153 DCHECK_NE(SecurityStateModel::RAN_AND_DISPLAYED_MIXED_CONTENT,
154 mixed_content_status);
170 // This should be kept in sync with 155 // This should be kept in sync with
171 // |kDisplayedInsecureContentStyle|. That is: the treatment 156 // |kDisplayedInsecureContentStyle|. That is: the treatment
172 // given to passive mixed content here should be expressed by 157 // given to passive mixed content here should be expressed by
173 // |kDisplayedInsecureContentStyle|, which is used to coordinate 158 // |kDisplayedInsecureContentStyle|, which is used to coordinate
174 // the treatment of passive mixed content with other security UI 159 // the treatment of passive mixed content with other security UI
175 // elements. 160 // elements outside of //chrome.
176 if (mixed_content_status == DISPLAYED_MIXED_CONTENT) 161 if (mixed_content_status == SecurityStateModel::DISPLAYED_MIXED_CONTENT)
177 return NONE; 162 return SecurityStateModel::NONE;
178 163
179 if (net::IsCertStatusError(ssl.cert_status)) { 164 if (net::IsCertStatusError(ssl.cert_status)) {
180 DCHECK(net::IsCertStatusMinorError(ssl.cert_status)); 165 DCHECK(net::IsCertStatusMinorError(ssl.cert_status));
181 return NONE; 166 return SecurityStateModel::NONE;
182 } 167 }
183 if (net::SSLConnectionStatusToVersion(ssl.connection_status) == 168 if (net::SSLConnectionStatusToVersion(ssl.connection_status) ==
184 net::SSL_CONNECTION_VERSION_SSL3) { 169 net::SSL_CONNECTION_VERSION_SSL3) {
185 // SSLv3 will be removed in the future. 170 // SSLv3 will be removed in the future.
186 return SECURITY_WARNING; 171 return SecurityStateModel::SECURITY_WARNING;
187 } 172 }
188 if ((ssl.cert_status & net::CERT_STATUS_IS_EV) && cert) 173 if ((ssl.cert_status & net::CERT_STATUS_IS_EV) && cert)
189 return EV_SECURE; 174 return SecurityStateModel::EV_SECURE;
190 return SECURE; 175 return SecurityStateModel::SECURE;
191 } 176 }
177 }
192 178
193 default: 179 return SecurityStateModel::NONE;
194 NOTREACHED();
195 return NONE;
196 }
197 } 180 }
198 181
199 void GetSecurityInfoForWebContents(const content::WebContents* web_contents, 182 } // namespace
200 SecurityInfo* security_info) { 183
184 const content::SecurityStyle
185 SecurityStateModel::kDisplayedInsecureContentStyle =
186 content::SECURITY_STYLE_UNAUTHENTICATED;
187 const content::SecurityStyle SecurityStateModel::kRanInsecureContentStyle =
188 content::SECURITY_STYLE_AUTHENTICATION_BROKEN;
189
190 SecurityStateModel::SecurityInfo::SecurityInfo()
191 : security_level(SecurityStateModel::NONE),
192 sha1_deprecation_status(SecurityStateModel::NO_DEPRECATED_SHA1),
193 mixed_content_status(SecurityStateModel::NO_MIXED_CONTENT),
194 scheme_is_cryptographic(false),
195 cert_status(0),
196 security_bits(-1),
197 connection_status(0) {}
198
199 SecurityStateModel::SecurityInfo::~SecurityInfo() {}
200
201 SecurityStateModel::~SecurityStateModel() {}
202
203 void SecurityStateModel::SecurityStateChanged() {
204 DCHECK(web_contents_);
201 content::NavigationEntry* entry = 205 content::NavigationEntry* entry =
202 web_contents ? web_contents->GetController().GetVisibleEntry() : nullptr; 206 web_contents_->GetController().GetVisibleEntry();
203 if (!entry) { 207 if (!entry)
204 security_info->security_style = content::SECURITY_STYLE_UNKNOWN;
205 return; 208 return;
209
210 SecurityInfoForRequest(
211 entry->GetURL(), entry->GetSSL(),
212 Profile::FromBrowserContext(web_contents_->GetBrowserContext()),
213 &security_info_);
214 }
215
216 const SecurityStateModel::SecurityInfo& SecurityStateModel::security_info()
217 const {
218 return security_info_;
219 }
220
221 // static
222 void SecurityStateModel::SecurityInfoForRequest(const GURL& url,
223 const content::SSLStatus& ssl,
224 Profile* profile,
225 SecurityInfo* security_info) {
226 scoped_refptr<net::X509Certificate> cert = GetCertForSSLStatus(ssl);
227 security_info->cert_id = ssl.cert_id;
228 security_info->sha1_deprecation_status = GetSHA1DeprecationStatus(cert, ssl);
229 security_info->mixed_content_status = GetMixedContentStatus(ssl);
230 security_info->security_bits = ssl.security_bits;
231 security_info->connection_status = ssl.connection_status;
232 security_info->cert_status = ssl.cert_status;
233 security_info->scheme_is_cryptographic = url.SchemeIsCryptographic();
234
235 security_info->sct_verify_statuses.clear();
236 for (const auto& sct : ssl.signed_certificate_timestamp_ids) {
237 security_info->sct_verify_statuses.push_back(sct.status);
206 } 238 }
207 239
208 security_info->scheme_is_cryptographic = 240 security_info->security_level = GetSecurityLevelForRequest(
209 entry->GetURL().SchemeIsCryptographic(); 241 url, ssl, profile, cert, security_info->sha1_deprecation_status,
210 242 security_info->mixed_content_status);
211 SecurityLevel security_level = GetSecurityLevelForWebContents(web_contents);
212 switch (security_level) {
213 case SECURITY_WARNING:
214 case NONE:
215 security_info->security_style = content::SECURITY_STYLE_UNAUTHENTICATED;
216 break;
217 case EV_SECURE:
218 case SECURE:
219 security_info->security_style = content::SECURITY_STYLE_AUTHENTICATED;
220 break;
221 case SECURITY_POLICY_WARNING:
222 security_info->security_style = content::SECURITY_STYLE_WARNING;
223 break;
224 case SECURITY_ERROR:
225 security_info->security_style =
226 content::SECURITY_STYLE_AUTHENTICATION_BROKEN;
227 break;
228 }
229
230 const content::SSLStatus& ssl = entry->GetSSL();
231 scoped_refptr<net::X509Certificate> cert = GetCertForSSLStatus(ssl);
232 security_info->sha1_deprecation_status = GetSHA1DeprecationStatus(cert, ssl);
233 security_info->mixed_content_status = GetMixedContentStatus(ssl);
234 security_info->cert_status = ssl.cert_status;
235 } 243 }
236 244
237 } // namespace connection_security 245 SecurityStateModel::SecurityStateModel(content::WebContents* web_contents)
246 : web_contents_(web_contents) {}
247
248 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SecurityStateModel);
Avi (use Gerrit) 2015/09/03 22:57:36 put this line at the top
estark 2015/09/03 23:16:40 Done.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698