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

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

Issue 1355413003: Move error classification into the ssl_errors component (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleanup Created 5 years, 2 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
(Empty)
1 // Copyright 2014 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 <vector>
6
7 #include "chrome/browser/ssl/ssl_error_classification.h"
8
9 #include "base/build_time.h"
10 #include "base/metrics/histogram_macros.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/time/time.h"
14 #include "components/ssl_errors/error_info.h"
15 #include "net/base/net_util.h"
16 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
17 #include "net/cert/x509_cert_types.h"
18 #include "net/cert/x509_certificate.h"
19 #include "url/gurl.h"
20
21 #if defined(OS_WIN)
22 #include "base/win/win_util.h"
23 #include "base/win/windows_version.h"
24 #endif
25
26 using base::Time;
27 using base::TimeTicks;
28 using base::TimeDelta;
29
30 namespace {
31
32 // Events for UMA. Do not reorder or change!
33 enum SSLInterstitialCause {
34 CLOCK_PAST,
35 CLOCK_FUTURE,
36 WWW_SUBDOMAIN_MATCH,
37 SUBDOMAIN_MATCH,
38 SUBDOMAIN_INVERSE_MATCH,
39 SUBDOMAIN_OUTSIDE_WILDCARD,
40 HOST_NAME_NOT_KNOWN_TLD,
41 LIKELY_MULTI_TENANT_HOSTING,
42 LOCALHOST,
43 PRIVATE_URL,
44 AUTHORITY_ERROR_CAPTIVE_PORTAL, // Deprecated in M47.
45 SELF_SIGNED,
46 EXPIRED_RECENTLY,
47 LIKELY_SAME_DOMAIN,
48 UNUSED_INTERSTITIAL_CAUSE_ENTRY,
49 };
50
51 void RecordSSLInterstitialCause(bool overridable, SSLInterstitialCause event) {
52 if (overridable) {
53 UMA_HISTOGRAM_ENUMERATION("interstitial.ssl.cause.overridable", event,
54 UNUSED_INTERSTITIAL_CAUSE_ENTRY);
55 } else {
56 UMA_HISTOGRAM_ENUMERATION("interstitial.ssl.cause.nonoverridable", event,
57 UNUSED_INTERSTITIAL_CAUSE_ENTRY);
58 }
59 }
60
61 int GetLevensteinDistance(const std::string& str1,
62 const std::string& str2) {
63 if (str1 == str2)
64 return 0;
65 if (str1.size() == 0)
66 return str2.size();
67 if (str2.size() == 0)
68 return str1.size();
69 std::vector<int> kFirstRow(str2.size() + 1, 0);
70 std::vector<int> kSecondRow(str2.size() + 1, 0);
71
72 for (size_t i = 0; i < kFirstRow.size(); ++i)
73 kFirstRow[i] = i;
74 for (size_t i = 0; i < str1.size(); ++i) {
75 kSecondRow[0] = i + 1;
76 for (size_t j = 0; j < str2.size(); ++j) {
77 int cost = str1[i] == str2[j] ? 0 : 1;
78 kSecondRow[j+1] = std::min(std::min(
79 kSecondRow[j] + 1, kFirstRow[j + 1] + 1), kFirstRow[j] + cost);
80 }
81 for (size_t j = 0; j < kFirstRow.size(); j++)
82 kFirstRow[j] = kSecondRow[j];
83 }
84 return kSecondRow[str2.size()];
85 }
86
87 // The time to use when doing build time operations in browser tests.
88 base::Time g_testing_build_time;
89
90 } // namespace
91
92 SSLErrorClassification::SSLErrorClassification(const base::Time& current_time,
93 const GURL& url,
94 int cert_error,
95 const net::X509Certificate& cert)
96 : current_time_(current_time),
97 request_url_(url),
98 cert_error_(cert_error),
99 cert_(cert) {}
100
101 SSLErrorClassification::~SSLErrorClassification() { }
102
103 void SSLErrorClassification::RecordUMAStatistics(
104 bool overridable) const {
105 ssl_errors::ErrorInfo::ErrorType type =
106 ssl_errors::ErrorInfo::NetErrorToErrorType(cert_error_);
107 UMA_HISTOGRAM_ENUMERATION("interstitial.ssl_error_type", type,
108 ssl_errors::ErrorInfo::END_OF_ENUM);
109 switch (type) {
110 case ssl_errors::ErrorInfo::CERT_DATE_INVALID: {
111 if (IsUserClockInThePast(base::Time::NowFromSystemTime())) {
112 RecordSSLInterstitialCause(overridable, CLOCK_PAST);
113 } else if (IsUserClockInTheFuture(base::Time::NowFromSystemTime())) {
114 RecordSSLInterstitialCause(overridable, CLOCK_FUTURE);
115 } else if (cert_.HasExpired() &&
116 (current_time_ - cert_.valid_expiry()).InDays() < 28) {
117 RecordSSLInterstitialCause(overridable, EXPIRED_RECENTLY);
118 }
119 break;
120 }
121 case ssl_errors::ErrorInfo::CERT_COMMON_NAME_INVALID: {
122 std::string host_name = request_url_.host();
123 if (IsHostNameKnownTLD(host_name)) {
124 Tokens host_name_tokens = Tokenize(host_name);
125 if (IsWWWSubDomainMatch())
126 RecordSSLInterstitialCause(overridable, WWW_SUBDOMAIN_MATCH);
127 if (IsSubDomainOutsideWildcard(host_name_tokens))
128 RecordSSLInterstitialCause(overridable, SUBDOMAIN_OUTSIDE_WILDCARD);
129 std::vector<std::string> dns_names;
130 cert_.GetDNSNames(&dns_names);
131 std::vector<Tokens> dns_name_tokens = GetTokenizedDNSNames(dns_names);
132 if (NameUnderAnyNames(host_name_tokens, dns_name_tokens))
133 RecordSSLInterstitialCause(overridable, SUBDOMAIN_MATCH);
134 if (AnyNamesUnderName(dns_name_tokens, host_name_tokens))
135 RecordSSLInterstitialCause(overridable, SUBDOMAIN_INVERSE_MATCH);
136 if (IsCertLikelyFromMultiTenantHosting())
137 RecordSSLInterstitialCause(overridable, LIKELY_MULTI_TENANT_HOSTING);
138 if (IsCertLikelyFromSameDomain())
139 RecordSSLInterstitialCause(overridable, LIKELY_SAME_DOMAIN);
140 } else {
141 RecordSSLInterstitialCause(overridable, HOST_NAME_NOT_KNOWN_TLD);
142 }
143 break;
144 }
145 case ssl_errors::ErrorInfo::CERT_AUTHORITY_INVALID: {
146 const std::string& hostname = request_url_.HostNoBrackets();
147 if (net::IsLocalhost(hostname))
148 RecordSSLInterstitialCause(overridable, LOCALHOST);
149 if (IsHostnameNonUniqueOrDotless(hostname))
150 RecordSSLInterstitialCause(overridable, PRIVATE_URL);
151 if (net::X509Certificate::IsSelfSigned(cert_.os_cert_handle()))
152 RecordSSLInterstitialCause(overridable, SELF_SIGNED);
153 break;
154 }
155 default:
156 break;
157 }
158 UMA_HISTOGRAM_ENUMERATION("interstitial.ssl.connection_type",
159 net::NetworkChangeNotifier::GetConnectionType(),
160 net::NetworkChangeNotifier::CONNECTION_LAST);
161 }
162
163 bool SSLErrorClassification::IsUserClockInThePast(const base::Time& time_now) {
164 base::Time build_time;
165 if (!g_testing_build_time.is_null()) {
166 build_time = g_testing_build_time;
167 } else {
168 #if defined(DONT_EMBED_BUILD_METADATA) && !defined(OFFICIAL_BUILD)
169 return false;
170 #else
171 build_time = base::GetBuildTime();
172 #endif
173 }
174
175 if (time_now < build_time - base::TimeDelta::FromDays(2))
176 return true;
177 return false;
178 }
179
180 bool SSLErrorClassification::IsUserClockInTheFuture(
181 const base::Time& time_now) {
182 base::Time build_time;
183 if (!g_testing_build_time.is_null()) {
184 build_time = g_testing_build_time;
185 } else {
186 #if defined(DONT_EMBED_BUILD_METADATA) && !defined(OFFICIAL_BUILD)
187 return false;
188 #else
189 build_time = base::GetBuildTime();
190 #endif
191 }
192
193 if (time_now > build_time + base::TimeDelta::FromDays(365))
194 return true;
195 return false;
196 }
197
198 // static
199 void SSLErrorClassification::SetBuildTimeForTesting(
200 const base::Time& testing_time) {
201 g_testing_build_time = testing_time;
202 }
203
204 bool SSLErrorClassification::MaybeWindowsLacksSHA256Support() {
205 #if defined(OS_WIN)
206 return !base::win::MaybeHasSHA256Support();
207 #else
208 return false;
209 #endif
210 }
211
212 bool SSLErrorClassification::IsHostNameKnownTLD(const std::string& host_name) {
213 size_t tld_length =
214 net::registry_controlled_domains::GetRegistryLength(
215 host_name,
216 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
217 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
218 if (tld_length == 0 || tld_length == std::string::npos)
219 return false;
220 return true;
221 }
222
223 std::vector<SSLErrorClassification::Tokens> SSLErrorClassification::
224 GetTokenizedDNSNames(const std::vector<std::string>& dns_names) {
225 std::vector<std::vector<std::string>> dns_name_tokens;
226 for (size_t i = 0; i < dns_names.size(); ++i) {
227 std::vector<std::string> dns_name_token_single;
228 if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos
229 || !(IsHostNameKnownTLD(dns_names[i]))) {
230 dns_name_token_single.push_back(std::string());
231 } else {
232 dns_name_token_single = Tokenize(dns_names[i]);
233 }
234 dns_name_tokens.push_back(dns_name_token_single);
235 }
236 return dns_name_tokens;
237 }
238
239 size_t SSLErrorClassification::FindSubDomainDifference(
240 const Tokens& potential_subdomain, const Tokens& parent) const {
241 // A check to ensure that the number of tokens in the tokenized_parent is
242 // less than the tokenized_potential_subdomain.
243 if (parent.size() >= potential_subdomain.size())
244 return 0;
245
246 size_t tokens_match = 0;
247 size_t diff_size = potential_subdomain.size() - parent.size();
248 for (size_t i = 0; i < parent.size(); ++i) {
249 if (parent[i] == potential_subdomain[i + diff_size])
250 tokens_match++;
251 }
252 if (tokens_match == parent.size())
253 return diff_size;
254 return 0;
255 }
256
257 SSLErrorClassification::Tokens SSLErrorClassification::
258 Tokenize(const std::string& name) {
259 return base::SplitString(
260 name, ".", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
261 }
262
263 // We accept the inverse case for www for historical reasons.
264 bool SSLErrorClassification::GetWWWSubDomainMatch(
265 const std::string& host_name,
266 const std::vector<std::string>& dns_names,
267 std::string* www_match_host_name) {
268 if (IsHostNameKnownTLD(host_name)) {
269 // Need to account for all possible domains given in the SSL certificate.
270 for (size_t i = 0; i < dns_names.size(); ++i) {
271 if (dns_names[i].empty() ||
272 dns_names[i].find('\0') != std::string::npos ||
273 dns_names[i].length() == host_name.length() ||
274 !IsHostNameKnownTLD(dns_names[i])) {
275 continue;
276 } else if (dns_names[i].length() > host_name.length()) {
277 if (net::StripWWW(base::ASCIIToUTF16(dns_names[i])) ==
278 base::ASCIIToUTF16(host_name)) {
279 *www_match_host_name = dns_names[i];
280 return true;
281 }
282 } else {
283 if (net::StripWWW(base::ASCIIToUTF16(host_name)) ==
284 base::ASCIIToUTF16(dns_names[i])) {
285 *www_match_host_name = dns_names[i];
286 return true;
287 }
288 }
289 }
290 }
291 return false;
292 }
293
294 bool SSLErrorClassification::IsWWWSubDomainMatch() const {
295 const std::string& host_name = request_url_.host();
296 std::vector<std::string> dns_names;
297 cert_.GetDNSNames(&dns_names);
298 std::string www_host;
299 return GetWWWSubDomainMatch(host_name, dns_names, &www_host);
300 }
301
302 bool SSLErrorClassification::NameUnderAnyNames(
303 const Tokens& child,
304 const std::vector<Tokens>& potential_parents) const {
305 bool result = false;
306 // Need to account for all the possible domains given in the SSL certificate.
307 for (size_t i = 0; i < potential_parents.size(); ++i) {
308 if (potential_parents[i].empty() ||
309 potential_parents[i].size() >= child.size()) {
310 result = result || false;
311 } else {
312 size_t domain_diff = FindSubDomainDifference(child,
313 potential_parents[i]);
314 if (domain_diff == 1 && child[0] != "www")
315 result = result || true;
316 }
317 }
318 return result;
319 }
320
321 bool SSLErrorClassification::AnyNamesUnderName(
322 const std::vector<Tokens>& potential_children,
323 const Tokens& parent) const {
324 bool result = false;
325 // Need to account for all the possible domains given in the SSL certificate.
326 for (size_t i = 0; i < potential_children.size(); ++i) {
327 if (potential_children[i].empty() ||
328 potential_children[i].size() <= parent.size()) {
329 result = result || false;
330 } else {
331 size_t domain_diff = FindSubDomainDifference(potential_children[i],
332 parent);
333 if (domain_diff == 1 && potential_children[i][0] != "www")
334 result = result || true;
335 }
336 }
337 return result;
338 }
339
340 bool SSLErrorClassification::IsSubDomainOutsideWildcard(
341 const Tokens& host_name_tokens) const {
342 std::string host_name = request_url_.host();
343 std::vector<std::string> dns_names;
344 cert_.GetDNSNames(&dns_names);
345 bool result = false;
346
347 // This method requires that the host name be longer than the dns name on
348 // the certificate.
349 for (size_t i = 0; i < dns_names.size(); ++i) {
350 const std::string& name = dns_names[i];
351 if (name.length() < 2 || name.length() >= host_name.length() ||
352 name.find('\0') != std::string::npos ||
353 !IsHostNameKnownTLD(name)
354 || name[0] != '*' || name[1] != '.') {
355 continue;
356 }
357
358 // Move past the "*.".
359 std::string extracted_dns_name = name.substr(2);
360 if (FindSubDomainDifference(
361 host_name_tokens, Tokenize(extracted_dns_name)) == 2) {
362 return true;
363 }
364 }
365 return result;
366 }
367
368 bool SSLErrorClassification::IsCertLikelyFromMultiTenantHosting() const {
369 std::string host_name = request_url_.host();
370 std::vector<std::string> dns_names;
371 std::vector<std::string> dns_names_domain;
372 cert_.GetDNSNames(&dns_names);
373 size_t dns_names_size = dns_names.size();
374
375 // If there is only 1 DNS name then it is definitely not a shared certificate.
376 if (dns_names_size == 0 || dns_names_size == 1)
377 return false;
378
379 // Check to see if all the domains in the SAN field in the SSL certificate are
380 // the same or not.
381 for (size_t i = 0; i < dns_names_size; ++i) {
382 dns_names_domain.push_back(
383 net::registry_controlled_domains::
384 GetDomainAndRegistry(
385 dns_names[i],
386 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
387 }
388 for (size_t i = 1; i < dns_names_domain.size(); ++i) {
389 if (dns_names_domain[i] != dns_names_domain[0])
390 return false;
391 }
392
393 // If the number of DNS names is more than 5 then assume that it is a shared
394 // certificate.
395 static const int kDistinctNameThreshold = 5;
396 if (dns_names_size > kDistinctNameThreshold)
397 return true;
398
399 // Heuristic - The edit distance between all the strings should be at least 5
400 // for it to be counted as a shared SSLCertificate. If even one pair of
401 // strings edit distance is below 5 then the certificate is no longer
402 // considered as a shared certificate. Include the host name in the URL also
403 // while comparing.
404 dns_names.push_back(host_name);
405 static const int kMinimumEditDsitance = 5;
406 for (size_t i = 0; i < dns_names_size; ++i) {
407 for (size_t j = i + 1; j < dns_names_size; ++j) {
408 int edit_distance = GetLevensteinDistance(dns_names[i], dns_names[j]);
409 if (edit_distance < kMinimumEditDsitance)
410 return false;
411 }
412 }
413 return true;
414 }
415
416 bool SSLErrorClassification::IsCertLikelyFromSameDomain() const {
417 std::string host_name = request_url_.host();
418 std::vector<std::string> dns_names;
419 cert_.GetDNSNames(&dns_names);
420
421 dns_names.push_back(host_name);
422 std::vector<std::string> dns_names_domain;
423
424 for (const std::string& dns_name : dns_names) {
425 dns_names_domain.push_back(
426 net::registry_controlled_domains::GetDomainAndRegistry(
427 dns_name,
428 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
429 }
430
431 DCHECK(!dns_names_domain.empty());
432 const std::string& host_name_domain = dns_names_domain.back();
433
434 // Last element is the original domain. So, excluding it.
435 return std::find(dns_names_domain.begin(), dns_names_domain.end() - 1,
436 host_name_domain) != dns_names_domain.end() - 1;
437 }
438
439 // static
440 bool SSLErrorClassification::IsHostnameNonUniqueOrDotless(
441 const std::string& hostname) {
442 return net::IsHostnameNonUnique(hostname) ||
443 hostname.find('.') == std::string::npos;
444 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698