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.
7 #include "chrome/browser/ssl/ssl_error_classification.h"
9 #include "base/build_time.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/metrics/histogram.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/time/time.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ssl/ssl_error_info.h"
19 #include "content/public/browser/notification_service.h"
20 #include "content/public/browser/web_contents.h"
21 #include "net/base/net_util.h"
22 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
23 #include "net/cert/x509_cert_types.h"
24 #include "net/cert/x509_certificate.h"
27 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
28 #include "chrome/browser/captive_portal/captive_portal_service.h"
29 #include "chrome/browser/captive_portal/captive_portal_service_factory.h"
33 #include "base/win/win_util.h"
34 #include "base/win/windows_version.h"
38 using base::TimeTicks
;
39 using base::TimeDelta
;
43 // Events for UMA. Do not reorder or change!
44 enum SSLInterstitialCause
{
49 SUBDOMAIN_INVERSE_MATCH
,
50 SUBDOMAIN_OUTSIDE_WILDCARD
,
51 HOST_NAME_NOT_KNOWN_TLD
,
52 LIKELY_MULTI_TENANT_HOSTING
,
55 AUTHORITY_ERROR_CAPTIVE_PORTAL
,
59 UNUSED_INTERSTITIAL_CAUSE_ENTRY
,
62 // Events for UMA. Do not reorder or change!
63 enum SSLInterstitialCauseCaptivePortal
{
65 CAPTIVE_PORTAL_DETECTION_ENABLED
,
66 CAPTIVE_PORTAL_DETECTION_ENABLED_OVERRIDABLE
,
67 CAPTIVE_PORTAL_PROBE_COMPLETED
,
68 CAPTIVE_PORTAL_PROBE_COMPLETED_OVERRIDABLE
,
69 CAPTIVE_PORTAL_NO_RESPONSE
,
70 CAPTIVE_PORTAL_NO_RESPONSE_OVERRIDABLE
,
71 CAPTIVE_PORTAL_DETECTED
,
72 CAPTIVE_PORTAL_DETECTED_OVERRIDABLE
,
73 UNUSED_CAPTIVE_PORTAL_EVENT
,
76 void RecordSSLInterstitialCause(bool overridable
, SSLInterstitialCause event
) {
78 UMA_HISTOGRAM_ENUMERATION("interstitial.ssl.cause.overridable", event
,
79 UNUSED_INTERSTITIAL_CAUSE_ENTRY
);
81 UMA_HISTOGRAM_ENUMERATION("interstitial.ssl.cause.nonoverridable", event
,
82 UNUSED_INTERSTITIAL_CAUSE_ENTRY
);
86 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
87 void RecordCaptivePortalEventStats(SSLInterstitialCauseCaptivePortal event
) {
88 UMA_HISTOGRAM_ENUMERATION("interstitial.ssl.captive_portal",
90 UNUSED_CAPTIVE_PORTAL_EVENT
);
94 int GetLevensteinDistance(const std::string
& str1
,
95 const std::string
& str2
) {
100 if (str2
.size() == 0)
102 std::vector
<int> kFirstRow(str2
.size() + 1, 0);
103 std::vector
<int> kSecondRow(str2
.size() + 1, 0);
105 for (size_t i
= 0; i
< kFirstRow
.size(); ++i
)
107 for (size_t i
= 0; i
< str1
.size(); ++i
) {
108 kSecondRow
[0] = i
+ 1;
109 for (size_t j
= 0; j
< str2
.size(); ++j
) {
110 int cost
= str1
[i
] == str2
[j
] ? 0 : 1;
111 kSecondRow
[j
+1] = std::min(std::min(
112 kSecondRow
[j
] + 1, kFirstRow
[j
+ 1] + 1), kFirstRow
[j
] + cost
);
114 for (size_t j
= 0; j
< kFirstRow
.size(); j
++)
115 kFirstRow
[j
] = kSecondRow
[j
];
117 return kSecondRow
[str2
.size()];
122 SSLErrorClassification::SSLErrorClassification(
123 content::WebContents
* web_contents
,
124 const base::Time
& current_time
,
127 const net::X509Certificate
& cert
)
128 : web_contents_(web_contents
),
129 current_time_(current_time
),
131 cert_error_(cert_error
),
133 captive_portal_detection_enabled_(false),
134 captive_portal_probe_completed_(false),
135 captive_portal_no_response_(false),
136 captive_portal_detected_(false) {
137 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
138 Profile
* profile
= Profile::FromBrowserContext(
139 web_contents_
->GetBrowserContext());
140 captive_portal_detection_enabled_
=
141 CaptivePortalServiceFactory::GetForProfile(profile
)->enabled();
143 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT
,
144 content::Source
<Profile
>(profile
));
148 SSLErrorClassification::~SSLErrorClassification() { }
150 void SSLErrorClassification::RecordCaptivePortalUMAStatistics(
151 bool overridable
) const {
152 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
153 RecordCaptivePortalEventStats(CAPTIVE_PORTAL_ALL
);
154 if (captive_portal_detection_enabled_
)
155 RecordCaptivePortalEventStats(
157 CAPTIVE_PORTAL_DETECTION_ENABLED_OVERRIDABLE
:
158 CAPTIVE_PORTAL_DETECTION_ENABLED
);
159 if (captive_portal_probe_completed_
)
160 RecordCaptivePortalEventStats(
162 CAPTIVE_PORTAL_PROBE_COMPLETED_OVERRIDABLE
:
163 CAPTIVE_PORTAL_PROBE_COMPLETED
);
164 // Log only one of portal detected and no response results.
165 if (captive_portal_detected_
)
166 RecordCaptivePortalEventStats(
168 CAPTIVE_PORTAL_DETECTED_OVERRIDABLE
:
169 CAPTIVE_PORTAL_DETECTED
);
170 else if (captive_portal_no_response_
)
171 RecordCaptivePortalEventStats(
173 CAPTIVE_PORTAL_NO_RESPONSE_OVERRIDABLE
:
174 CAPTIVE_PORTAL_NO_RESPONSE
);
178 void SSLErrorClassification::RecordUMAStatistics(
179 bool overridable
) const {
180 SSLErrorInfo::ErrorType type
=
181 SSLErrorInfo::NetErrorToErrorType(cert_error_
);
182 UMA_HISTOGRAM_ENUMERATION(
183 "interstitial.ssl_error_type", type
, SSLErrorInfo::END_OF_ENUM
);
185 case SSLErrorInfo::CERT_DATE_INVALID
: {
186 if (IsUserClockInThePast(base::Time::NowFromSystemTime())) {
187 RecordSSLInterstitialCause(overridable
, CLOCK_PAST
);
188 } else if (IsUserClockInTheFuture(base::Time::NowFromSystemTime())) {
189 RecordSSLInterstitialCause(overridable
, CLOCK_FUTURE
);
190 } else if (cert_
.HasExpired() && TimePassedSinceExpiry().InDays() < 28) {
191 RecordSSLInterstitialCause(overridable
, EXPIRED_RECENTLY
);
195 case SSLErrorInfo::CERT_COMMON_NAME_INVALID
: {
196 std::string host_name
= request_url_
.host();
197 if (IsHostNameKnownTLD(host_name
)) {
198 Tokens host_name_tokens
= Tokenize(host_name
);
199 if (IsWWWSubDomainMatch())
200 RecordSSLInterstitialCause(overridable
, WWW_SUBDOMAIN_MATCH
);
201 if (IsSubDomainOutsideWildcard(host_name_tokens
))
202 RecordSSLInterstitialCause(overridable
, SUBDOMAIN_OUTSIDE_WILDCARD
);
203 std::vector
<std::string
> dns_names
;
204 cert_
.GetDNSNames(&dns_names
);
205 std::vector
<Tokens
> dns_name_tokens
= GetTokenizedDNSNames(dns_names
);
206 if (NameUnderAnyNames(host_name_tokens
, dns_name_tokens
))
207 RecordSSLInterstitialCause(overridable
, SUBDOMAIN_MATCH
);
208 if (AnyNamesUnderName(dns_name_tokens
, host_name_tokens
))
209 RecordSSLInterstitialCause(overridable
, SUBDOMAIN_INVERSE_MATCH
);
210 if (IsCertLikelyFromMultiTenantHosting())
211 RecordSSLInterstitialCause(overridable
, LIKELY_MULTI_TENANT_HOSTING
);
212 if (IsCertLikelyFromSameDomain())
213 RecordSSLInterstitialCause(overridable
, LIKELY_SAME_DOMAIN
);
215 RecordSSLInterstitialCause(overridable
, HOST_NAME_NOT_KNOWN_TLD
);
219 case SSLErrorInfo::CERT_AUTHORITY_INVALID
: {
220 const std::string
& hostname
= request_url_
.HostNoBrackets();
221 if (net::IsLocalhost(hostname
))
222 RecordSSLInterstitialCause(overridable
, LOCALHOST
);
223 if (IsHostnameNonUniqueOrDotless(hostname
))
224 RecordSSLInterstitialCause(overridable
, PRIVATE_URL
);
225 if (captive_portal_probe_completed_
&& captive_portal_detected_
)
226 RecordSSLInterstitialCause(overridable
, AUTHORITY_ERROR_CAPTIVE_PORTAL
);
227 if (net::X509Certificate::IsSelfSigned(cert_
.os_cert_handle()))
228 RecordSSLInterstitialCause(overridable
, SELF_SIGNED
);
234 UMA_HISTOGRAM_ENUMERATION("interstitial.ssl.connection_type",
235 net::NetworkChangeNotifier::GetConnectionType(),
236 net::NetworkChangeNotifier::CONNECTION_LAST
);
239 base::TimeDelta
SSLErrorClassification::TimePassedSinceExpiry() const {
240 base::TimeDelta delta
= current_time_
- cert_
.valid_expiry();
244 bool SSLErrorClassification::IsUserClockInThePast(const base::Time
& time_now
) {
245 #if defined(DONT_EMBED_BUILD_METADATA) && !defined(OFFICIAL_BUILD)
248 base::Time build_time
= base::GetBuildTime();
249 if (time_now
< build_time
- base::TimeDelta::FromDays(2))
255 bool SSLErrorClassification::IsUserClockInTheFuture(
256 const base::Time
& time_now
) {
257 #if defined(DONT_EMBED_BUILD_METADATA) && !defined(OFFICIAL_BUILD)
260 base::Time build_time
= base::GetBuildTime();
261 if (time_now
> build_time
+ base::TimeDelta::FromDays(365))
267 bool SSLErrorClassification::MaybeWindowsLacksSHA256Support() {
269 return !base::win::MaybeHasSHA256Support();
275 bool SSLErrorClassification::IsHostNameKnownTLD(const std::string
& host_name
) {
277 net::registry_controlled_domains::GetRegistryLength(
279 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES
,
280 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES
);
281 if (tld_length
== 0 || tld_length
== std::string::npos
)
286 std::vector
<SSLErrorClassification::Tokens
> SSLErrorClassification::
287 GetTokenizedDNSNames(const std::vector
<std::string
>& dns_names
) {
288 std::vector
<std::vector
<std::string
>> dns_name_tokens
;
289 for (size_t i
= 0; i
< dns_names
.size(); ++i
) {
290 std::vector
<std::string
> dns_name_token_single
;
291 if (dns_names
[i
].empty() || dns_names
[i
].find('\0') != std::string::npos
292 || !(IsHostNameKnownTLD(dns_names
[i
]))) {
293 dns_name_token_single
.push_back(std::string());
295 dns_name_token_single
= Tokenize(dns_names
[i
]);
297 dns_name_tokens
.push_back(dns_name_token_single
);
299 return dns_name_tokens
;
302 size_t SSLErrorClassification::FindSubDomainDifference(
303 const Tokens
& potential_subdomain
, const Tokens
& parent
) const {
304 // A check to ensure that the number of tokens in the tokenized_parent is
305 // less than the tokenized_potential_subdomain.
306 if (parent
.size() >= potential_subdomain
.size())
309 size_t tokens_match
= 0;
310 size_t diff_size
= potential_subdomain
.size() - parent
.size();
311 for (size_t i
= 0; i
< parent
.size(); ++i
) {
312 if (parent
[i
] == potential_subdomain
[i
+ diff_size
])
315 if (tokens_match
== parent
.size())
320 SSLErrorClassification::Tokens
SSLErrorClassification::
321 Tokenize(const std::string
& name
) {
322 return base::SplitString(
323 name
, ".", base::KEEP_WHITESPACE
, base::SPLIT_WANT_ALL
);
326 // We accept the inverse case for www for historical reasons.
327 bool SSLErrorClassification::IsWWWSubDomainMatch() const {
328 std::string host_name
= request_url_
.host();
329 if (IsHostNameKnownTLD(host_name
)) {
330 std::vector
<std::string
> dns_names
;
331 cert_
.GetDNSNames(&dns_names
);
333 // Need to account for all possible domains given in the SSL certificate.
334 for (size_t i
= 0; i
< dns_names
.size(); ++i
) {
335 if (dns_names
[i
].empty() || dns_names
[i
].find('\0') != std::string::npos
336 || dns_names
[i
].length() == host_name
.length()
337 || !(IsHostNameKnownTLD(dns_names
[i
]))) {
338 result
= result
|| false;
339 } else if (dns_names
[i
].length() > host_name
.length()) {
341 net::StripWWW(base::ASCIIToUTF16(dns_names
[i
])) ==
342 base::ASCIIToUTF16(host_name
);
345 net::StripWWW(base::ASCIIToUTF16(host_name
)) ==
346 base::ASCIIToUTF16(dns_names
[i
]);
354 bool SSLErrorClassification::NameUnderAnyNames(
356 const std::vector
<Tokens
>& potential_parents
) const {
358 // Need to account for all the possible domains given in the SSL certificate.
359 for (size_t i
= 0; i
< potential_parents
.size(); ++i
) {
360 if (potential_parents
[i
].empty() ||
361 potential_parents
[i
].size() >= child
.size()) {
362 result
= result
|| false;
364 size_t domain_diff
= FindSubDomainDifference(child
,
365 potential_parents
[i
]);
366 if (domain_diff
== 1 && child
[0] != "www")
367 result
= result
|| true;
373 bool SSLErrorClassification::AnyNamesUnderName(
374 const std::vector
<Tokens
>& potential_children
,
375 const Tokens
& parent
) const {
377 // Need to account for all the possible domains given in the SSL certificate.
378 for (size_t i
= 0; i
< potential_children
.size(); ++i
) {
379 if (potential_children
[i
].empty() ||
380 potential_children
[i
].size() <= parent
.size()) {
381 result
= result
|| false;
383 size_t domain_diff
= FindSubDomainDifference(potential_children
[i
],
385 if (domain_diff
== 1 && potential_children
[i
][0] != "www")
386 result
= result
|| true;
392 bool SSLErrorClassification::IsSubDomainOutsideWildcard(
393 const Tokens
& host_name_tokens
) const {
394 std::string host_name
= request_url_
.host();
395 std::vector
<std::string
> dns_names
;
396 cert_
.GetDNSNames(&dns_names
);
399 // This method requires that the host name be longer than the dns name on
401 for (size_t i
= 0; i
< dns_names
.size(); ++i
) {
402 const std::string
& name
= dns_names
[i
];
403 if (name
.length() < 2 || name
.length() >= host_name
.length() ||
404 name
.find('\0') != std::string::npos
||
405 !IsHostNameKnownTLD(name
)
406 || name
[0] != '*' || name
[1] != '.') {
410 // Move past the "*.".
411 std::string extracted_dns_name
= name
.substr(2);
412 if (FindSubDomainDifference(
413 host_name_tokens
, Tokenize(extracted_dns_name
)) == 2) {
420 bool SSLErrorClassification::IsCertLikelyFromMultiTenantHosting() const {
421 std::string host_name
= request_url_
.host();
422 std::vector
<std::string
> dns_names
;
423 std::vector
<std::string
> dns_names_domain
;
424 cert_
.GetDNSNames(&dns_names
);
425 size_t dns_names_size
= dns_names
.size();
427 // If there is only 1 DNS name then it is definitely not a shared certificate.
428 if (dns_names_size
== 0 || dns_names_size
== 1)
431 // Check to see if all the domains in the SAN field in the SSL certificate are
433 for (size_t i
= 0; i
< dns_names_size
; ++i
) {
434 dns_names_domain
.push_back(
435 net::registry_controlled_domains::
436 GetDomainAndRegistry(
438 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES
));
440 for (size_t i
= 1; i
< dns_names_domain
.size(); ++i
) {
441 if (dns_names_domain
[i
] != dns_names_domain
[0])
445 // If the number of DNS names is more than 5 then assume that it is a shared
447 static const int kDistinctNameThreshold
= 5;
448 if (dns_names_size
> kDistinctNameThreshold
)
451 // Heuristic - The edit distance between all the strings should be at least 5
452 // for it to be counted as a shared SSLCertificate. If even one pair of
453 // strings edit distance is below 5 then the certificate is no longer
454 // considered as a shared certificate. Include the host name in the URL also
456 dns_names
.push_back(host_name
);
457 static const int kMinimumEditDsitance
= 5;
458 for (size_t i
= 0; i
< dns_names_size
; ++i
) {
459 for (size_t j
= i
+ 1; j
< dns_names_size
; ++j
) {
460 int edit_distance
= GetLevensteinDistance(dns_names
[i
], dns_names
[j
]);
461 if (edit_distance
< kMinimumEditDsitance
)
468 bool SSLErrorClassification::IsCertLikelyFromSameDomain() const {
469 std::string host_name
= request_url_
.host();
470 std::vector
<std::string
> dns_names
;
471 cert_
.GetDNSNames(&dns_names
);
473 dns_names
.push_back(host_name
);
474 std::vector
<std::string
> dns_names_domain
;
476 for (const std::string
& dns_name
: dns_names
) {
477 dns_names_domain
.push_back(
478 net::registry_controlled_domains::GetDomainAndRegistry(
480 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES
));
483 DCHECK(!dns_names_domain
.empty());
484 const std::string
& host_name_domain
= dns_names_domain
.back();
486 // Last element is the original domain. So, excluding it.
487 return std::find(dns_names_domain
.begin(), dns_names_domain
.end() - 1,
488 host_name_domain
) != dns_names_domain
.end() - 1;
492 bool SSLErrorClassification::IsHostnameNonUniqueOrDotless(
493 const std::string
& hostname
) {
494 return net::IsHostnameNonUnique(hostname
) ||
495 hostname
.find('.') == std::string::npos
;
498 void SSLErrorClassification::Observe(
500 const content::NotificationSource
& source
,
501 const content::NotificationDetails
& details
) {
502 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
503 // When detection is disabled, captive portal service always sends
504 // RESULT_INTERNET_CONNECTED. Ignore any probe results in that case.
505 if (!captive_portal_detection_enabled_
)
507 if (type
== chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT
) {
508 captive_portal_probe_completed_
= true;
509 CaptivePortalService::Results
* results
=
510 content::Details
<CaptivePortalService::Results
>(details
).ptr();
511 // If a captive portal was detected at any point when the interstitial was
512 // displayed, assume that the interstitial was caused by a captive portal.
514 // 1- Interstitial displayed and captive portal detected, setting the flag.
515 // 2- Captive portal detection automatically opens portal login page.
516 // 3- User logs in on the portal login page.
517 // A notification will be received here for RESULT_INTERNET_CONNECTED. Make
518 // sure we don't clear the captive protal flag, since the interstitial was
519 // potentially caused by the captive portal.
520 captive_portal_detected_
= captive_portal_detected_
||
521 (results
->result
== captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL
);
522 // Also keep track of non-HTTP portals and error cases.
523 captive_portal_no_response_
= captive_portal_no_response_
||
524 (results
->result
== captive_portal::RESULT_NO_RESPONSE
);