OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 <vector> |
| 6 |
5 #include "chrome/browser/ssl/ssl_error_classification.h" | 7 #include "chrome/browser/ssl/ssl_error_classification.h" |
6 | 8 |
7 #include "base/build_time.h" | 9 #include "base/build_time.h" |
8 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
9 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/strings/string_split.h" |
| 13 #include "base/strings/utf_string_conversions.h" |
10 #include "base/time/time.h" | 14 #include "base/time/time.h" |
11 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/ssl/ssl_error_info.h" |
12 #include "components/network_time/network_time_tracker.h" | 16 #include "net/base/net_util.h" |
| 17 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 18 #include "net/cert/x509_cert_types.h" |
13 #include "net/cert/x509_certificate.h" | 19 #include "net/cert/x509_certificate.h" |
| 20 #include "url/gurl.h" |
14 | 21 |
15 using base::Time; | 22 using base::Time; |
16 using base::TimeTicks; | 23 using base::TimeTicks; |
17 using base::TimeDelta; | 24 using base::TimeDelta; |
18 | 25 |
19 #if defined(OS_WIN) | 26 #if defined(OS_WIN) |
20 #include "base/win/windows_version.h" | 27 #include "base/win/windows_version.h" |
21 #endif | 28 #endif |
22 | 29 |
23 namespace { | 30 namespace { |
24 | 31 |
25 // Events for UMA. Do not reorder or change! | 32 // Events for UMA. Do not reorder or change! |
26 enum SSLInterstitialCause { | 33 enum SSLInterstitialCause { |
27 CLOCK_PAST, | 34 CLOCK_PAST, |
28 CLOCK_FUTURE, | 35 CLOCK_FUTURE, |
| 36 WWW_SUBDOMAIN_MATCH, |
| 37 SUBDOMAIN_MATCH, |
| 38 SUBDOMAIN_INVERSE_MATCH, |
| 39 SUBDOMAIN_OUTSIDE_WILDCARD, |
| 40 SELF_SIGNED, |
| 41 HOST_NAME_NOT_KNOWN_TLD, |
29 UNUSED_INTERSTITIAL_CAUSE_ENTRY, | 42 UNUSED_INTERSTITIAL_CAUSE_ENTRY, |
30 }; | 43 }; |
31 | 44 |
| 45 // Scores/weights which will be constant through all the SSL error types. |
| 46 static const float kServerWeight = 0.5f; |
| 47 static const float kClientWeight = 0.5f; |
| 48 |
32 void RecordSSLInterstitialCause(bool overridable, SSLInterstitialCause event) { | 49 void RecordSSLInterstitialCause(bool overridable, SSLInterstitialCause event) { |
33 if (overridable) { | 50 if (overridable) { |
34 UMA_HISTOGRAM_ENUMERATION("interstitial.ssl.cause.overridable", event, | 51 UMA_HISTOGRAM_ENUMERATION("interstitial.ssl.cause.overridable", event, |
35 UNUSED_INTERSTITIAL_CAUSE_ENTRY); | 52 UNUSED_INTERSTITIAL_CAUSE_ENTRY); |
36 } else { | 53 } else { |
37 UMA_HISTOGRAM_ENUMERATION("interstitial.ssl.cause.nonoverridable", event, | 54 UMA_HISTOGRAM_ENUMERATION("interstitial.ssl.cause.nonoverridable", event, |
38 UNUSED_INTERSTITIAL_CAUSE_ENTRY); | 55 UNUSED_INTERSTITIAL_CAUSE_ENTRY); |
39 } | 56 } |
40 } | 57 } |
41 | 58 |
42 } // namespace | 59 } // namespace |
43 | 60 |
44 SSLErrorClassification::SSLErrorClassification( | 61 SSLErrorClassification::SSLErrorClassification( |
45 base::Time current_time, | 62 const base::Time& current_time, |
| 63 const GURL& url, |
46 const net::X509Certificate& cert) | 64 const net::X509Certificate& cert) |
47 : current_time_(current_time), | 65 : current_time_(current_time), |
| 66 request_url_(url), |
48 cert_(cert) { } | 67 cert_(cert) { } |
49 | 68 |
50 SSLErrorClassification::~SSLErrorClassification() { } | 69 SSLErrorClassification::~SSLErrorClassification() { } |
51 | 70 |
52 float SSLErrorClassification::InvalidDateSeverityScore() const { | 71 float SSLErrorClassification::InvalidDateSeverityScore( |
53 // Client-side characterisitics. Check whether the system's clock is wrong or | 72 int cert_error) const { |
54 // not and whether the user has encountered this error before or not. | 73 SSLErrorInfo::ErrorType type = |
| 74 SSLErrorInfo::NetErrorToErrorType(cert_error); |
| 75 DCHECK(type == SSLErrorInfo::CERT_DATE_INVALID); |
| 76 // Client-side characteristics. Check whether or not the system's clock is |
| 77 // wrong and whether or not the user has already encountered this error |
| 78 // before. |
55 float severity_date_score = 0.0f; | 79 float severity_date_score = 0.0f; |
56 | 80 |
57 static const float kClientWeight = 0.5f; | 81 static const float kCertificateExpiredWeight = 0.3f; |
| 82 static const float kNotYetValidWeight = 0.2f; |
| 83 |
58 static const float kSystemClockWeight = 0.75f; | 84 static const float kSystemClockWeight = 0.75f; |
59 static const float kSystemClockWrongWeight = 0.1f; | 85 static const float kSystemClockWrongWeight = 0.1f; |
60 static const float kSystemClockRightWeight = 1.0f; | 86 static const float kSystemClockRightWeight = 1.0f; |
61 | 87 |
62 static const float kServerWeight = 0.5f; | |
63 static const float kCertificateExpiredWeight = 0.3f; | |
64 static const float kNotYetValidWeight = 0.2f; | |
65 | |
66 if (IsUserClockInThePast(current_time_) || | 88 if (IsUserClockInThePast(current_time_) || |
67 IsUserClockInTheFuture(current_time_)) { | 89 IsUserClockInTheFuture(current_time_)) { |
68 severity_date_score = kClientWeight * kSystemClockWeight * | 90 severity_date_score += kClientWeight * kSystemClockWeight * |
69 kSystemClockWrongWeight; | 91 kSystemClockWrongWeight; |
70 } else { | 92 } else { |
71 severity_date_score = kClientWeight * kSystemClockWeight * | 93 severity_date_score += kClientWeight * kSystemClockWeight * |
72 kSystemClockRightWeight; | 94 kSystemClockRightWeight; |
73 } | 95 } |
74 // TODO(radhikabhar): (crbug.com/393262) Check website settings. | 96 // TODO(radhikabhar): (crbug.com/393262) Check website settings. |
75 | 97 |
76 // Server-side characteristics. Check whether the certificate has expired or | 98 // Server-side characteristics. Check whether the certificate has expired or |
77 // is not yet valid. If the certificate has expired then factor the time which | 99 // is not yet valid. If the certificate has expired then factor the time which |
78 // has passed since expiry. | 100 // has passed since expiry. |
79 if (cert_.HasExpired()) { | 101 if (cert_.HasExpired()) { |
80 severity_date_score += kServerWeight * kCertificateExpiredWeight * | 102 severity_date_score += kServerWeight * kCertificateExpiredWeight * |
81 CalculateScoreTimePassedSinceExpiry(); | 103 CalculateScoreTimePassedSinceExpiry(); |
82 } | 104 } |
83 if (current_time_ < cert_.valid_start()) | 105 if (current_time_ < cert_.valid_start()) |
84 severity_date_score += kServerWeight * kNotYetValidWeight; | 106 severity_date_score += kServerWeight * kNotYetValidWeight; |
85 return severity_date_score; | 107 return severity_date_score; |
86 } | 108 } |
87 | 109 |
| 110 float SSLErrorClassification::InvalidCommonNameSeverityScore( |
| 111 int cert_error) const { |
| 112 SSLErrorInfo::ErrorType type = |
| 113 SSLErrorInfo::NetErrorToErrorType(cert_error); |
| 114 DCHECK(type == SSLErrorInfo::CERT_COMMON_NAME_INVALID); |
| 115 float severity_name_score = 0.0f; |
| 116 |
| 117 static const float kWWWDifferenceWeight = 0.3f; |
| 118 static const float kSubDomainWeight = 0.2f; |
| 119 static const float kSubDomainInverseWeight = 1.0f; |
| 120 |
| 121 std::string host_name = request_url_.host(); |
| 122 if (IsHostNameKnownTLD(host_name)) { |
| 123 Tokens host_name_tokens = Tokenize(host_name); |
| 124 if (IsWWWSubDomainMatch()) |
| 125 severity_name_score += kServerWeight * kWWWDifferenceWeight; |
| 126 if (IsSubDomainOutsideWildcard(host_name_tokens)) |
| 127 severity_name_score += kServerWeight * kWWWDifferenceWeight; |
| 128 |
| 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 severity_name_score += kServerWeight * kSubDomainWeight; |
| 134 // Inverse case is more likely to be a MITM attack. |
| 135 if (AnyNamesUnderName(dns_name_tokens, host_name_tokens)) |
| 136 severity_name_score += kServerWeight * kSubDomainInverseWeight; |
| 137 } |
| 138 return severity_name_score; |
| 139 } |
| 140 |
| 141 void SSLErrorClassification::RecordUMAStatistics(bool overridable, |
| 142 int cert_error) { |
| 143 SSLErrorInfo::ErrorType type = |
| 144 SSLErrorInfo::NetErrorToErrorType(cert_error); |
| 145 switch (type) { |
| 146 case SSLErrorInfo::CERT_DATE_INVALID: { |
| 147 if (IsUserClockInThePast(base::Time::NowFromSystemTime())) |
| 148 RecordSSLInterstitialCause(overridable, CLOCK_PAST); |
| 149 if (IsUserClockInTheFuture(base::Time::NowFromSystemTime())) |
| 150 RecordSSLInterstitialCause(overridable, CLOCK_FUTURE); |
| 151 break; |
| 152 } |
| 153 case SSLErrorInfo::CERT_COMMON_NAME_INVALID: { |
| 154 std::string host_name = request_url_.host(); |
| 155 if (IsHostNameKnownTLD(host_name)) { |
| 156 Tokens host_name_tokens = Tokenize(host_name); |
| 157 if (IsWWWSubDomainMatch()) |
| 158 RecordSSLInterstitialCause(overridable, WWW_SUBDOMAIN_MATCH); |
| 159 if (IsSubDomainOutsideWildcard(host_name_tokens)) |
| 160 RecordSSLInterstitialCause(overridable, SUBDOMAIN_OUTSIDE_WILDCARD); |
| 161 std::vector<std::string> dns_names; |
| 162 cert_.GetDNSNames(&dns_names); |
| 163 std::vector<Tokens> dns_name_tokens = GetTokenizedDNSNames(dns_names); |
| 164 if (NameUnderAnyNames(host_name_tokens, dns_name_tokens)) |
| 165 RecordSSLInterstitialCause(overridable, SUBDOMAIN_MATCH); |
| 166 if (AnyNamesUnderName(dns_name_tokens, host_name_tokens)) |
| 167 RecordSSLInterstitialCause(overridable, SUBDOMAIN_INVERSE_MATCH); |
| 168 } else { |
| 169 RecordSSLInterstitialCause(overridable, HOST_NAME_NOT_KNOWN_TLD); |
| 170 } |
| 171 break; |
| 172 } |
| 173 default: { |
| 174 break; |
| 175 } |
| 176 } |
| 177 } |
| 178 |
88 base::TimeDelta SSLErrorClassification::TimePassedSinceExpiry() const { | 179 base::TimeDelta SSLErrorClassification::TimePassedSinceExpiry() const { |
89 base::TimeDelta delta = current_time_ - cert_.valid_expiry(); | 180 base::TimeDelta delta = current_time_ - cert_.valid_expiry(); |
90 return delta; | 181 return delta; |
91 } | 182 } |
92 | 183 |
93 float SSLErrorClassification::CalculateScoreTimePassedSinceExpiry() const { | 184 float SSLErrorClassification::CalculateScoreTimePassedSinceExpiry() const { |
94 base::TimeDelta delta = TimePassedSinceExpiry(); | 185 base::TimeDelta delta = TimePassedSinceExpiry(); |
95 int64 time_passed = delta.InDays(); | 186 int64 time_passed = delta.InDays(); |
96 const int64 kHighThreshold = 7; | 187 const int64 kHighThreshold = 7; |
97 const int64 kLowThreshold = 4; | 188 const int64 kLowThreshold = 4; |
98 static const float kHighThresholdWeight = 0.4f; | 189 static const float kHighThresholdWeight = 0.4f; |
99 static const float kMediumThresholdWeight = 0.3f; | 190 static const float kMediumThresholdWeight = 0.3f; |
100 static const float kLowThresholdWeight = 0.2f; | 191 static const float kLowThresholdWeight = 0.2f; |
101 if (time_passed >= kHighThreshold) | 192 if (time_passed >= kHighThreshold) |
102 return kHighThresholdWeight; | 193 return kHighThresholdWeight; |
103 else if (time_passed >= kLowThreshold) | 194 else if (time_passed >= kLowThreshold) |
104 return kMediumThresholdWeight; | 195 return kMediumThresholdWeight; |
105 else | 196 else |
106 return kLowThresholdWeight; | 197 return kLowThresholdWeight; |
107 } | 198 } |
108 | 199 |
109 bool SSLErrorClassification::IsUserClockInThePast(base::Time time_now) { | 200 bool SSLErrorClassification::IsUserClockInThePast(const base::Time& time_now) { |
110 base::Time build_time = base::GetBuildTime(); | 201 base::Time build_time = base::GetBuildTime(); |
111 if (time_now < build_time - base::TimeDelta::FromDays(2)) | 202 if (time_now < build_time - base::TimeDelta::FromDays(2)) |
112 return true; | 203 return true; |
113 return false; | 204 return false; |
114 } | 205 } |
115 | 206 |
116 bool SSLErrorClassification::IsUserClockInTheFuture(base::Time time_now) { | 207 bool SSLErrorClassification::IsUserClockInTheFuture( |
| 208 const base::Time& time_now) { |
117 base::Time build_time = base::GetBuildTime(); | 209 base::Time build_time = base::GetBuildTime(); |
118 if (time_now > build_time + base::TimeDelta::FromDays(365)) | 210 if (time_now > build_time + base::TimeDelta::FromDays(365)) |
119 return true; | 211 return true; |
120 return false; | 212 return false; |
121 } | 213 } |
122 | 214 |
123 bool SSLErrorClassification::IsWindowsVersionSP3OrLower() { | 215 bool SSLErrorClassification::IsWindowsVersionSP3OrLower() { |
124 #if defined(OS_WIN) | 216 #if defined(OS_WIN) |
125 const base::win::OSInfo* os_info = base::win::OSInfo::GetInstance(); | 217 const base::win::OSInfo* os_info = base::win::OSInfo::GetInstance(); |
126 base::win::OSInfo::ServicePack service_pack = os_info->service_pack(); | 218 base::win::OSInfo::ServicePack service_pack = os_info->service_pack(); |
127 if (os_info->version() < base::win::VERSION_VISTA && service_pack.major < 3) | 219 if (os_info->version() < base::win::VERSION_VISTA && service_pack.major < 3) |
128 return true; | 220 return true; |
129 #endif | 221 #endif |
130 return false; | 222 return false; |
131 } | 223 } |
132 | 224 |
133 void SSLErrorClassification::RecordUMAStatistics(bool overridable) { | 225 bool SSLErrorClassification::IsHostNameKnownTLD(const std::string& host_name) { |
134 if (IsUserClockInThePast(base::Time::NowFromSystemTime())) | 226 size_t tld_length = |
135 RecordSSLInterstitialCause(overridable, CLOCK_PAST); | 227 net::registry_controlled_domains::GetRegistryLength( |
136 if (IsUserClockInTheFuture(base::Time::NowFromSystemTime())) | 228 host_name, |
137 RecordSSLInterstitialCause(overridable, CLOCK_FUTURE); | 229 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, |
| 230 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| 231 if (tld_length == 0 || tld_length == std::string::npos) |
| 232 return false; |
| 233 return true; |
138 } | 234 } |
| 235 |
| 236 std::vector<SSLErrorClassification::Tokens> SSLErrorClassification:: |
| 237 GetTokenizedDNSNames(const std::vector<std::string>& dns_names) { |
| 238 std::vector<std::vector<std::string>> dns_name_tokens; |
| 239 for (size_t i = 0; i < dns_names.size(); ++i) { |
| 240 std::vector<std::string> dns_name_token_single; |
| 241 if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos |
| 242 || !(IsHostNameKnownTLD(dns_names[i]))) { |
| 243 dns_name_token_single.push_back(std::string()); |
| 244 } else { |
| 245 dns_name_token_single = Tokenize(dns_names[i]); |
| 246 } |
| 247 dns_name_tokens.push_back(dns_name_token_single); |
| 248 } |
| 249 return dns_name_tokens; |
| 250 } |
| 251 |
| 252 size_t SSLErrorClassification::FindSubDomainDifference( |
| 253 const Tokens& potential_subdomain, const Tokens& parent) const { |
| 254 // A check to ensure that the number of tokens in the tokenized_parent is |
| 255 // less than the tokenized_potential_subdomain. |
| 256 if (parent.size() >= potential_subdomain.size()) |
| 257 return 0; |
| 258 |
| 259 size_t tokens_match = 0; |
| 260 size_t diff_size = potential_subdomain.size() - parent.size(); |
| 261 for (size_t i = 0; i < parent.size(); ++i) { |
| 262 if (parent[i] == potential_subdomain[i + diff_size]) |
| 263 tokens_match++; |
| 264 } |
| 265 if (tokens_match == parent.size()) |
| 266 return diff_size; |
| 267 return 0; |
| 268 } |
| 269 |
| 270 SSLErrorClassification::Tokens SSLErrorClassification:: |
| 271 Tokenize(const std::string& name) { |
| 272 Tokens name_tokens; |
| 273 base::SplitStringDontTrim(name, '.', &name_tokens); |
| 274 return name_tokens; |
| 275 } |
| 276 |
| 277 // We accept the inverse case for www for historical reasons. |
| 278 bool SSLErrorClassification::IsWWWSubDomainMatch() const { |
| 279 std::string host_name = request_url_.host(); |
| 280 if (IsHostNameKnownTLD(host_name)) { |
| 281 std::vector<std::string> dns_names; |
| 282 cert_.GetDNSNames(&dns_names); |
| 283 bool result = false; |
| 284 // Need to account for all possible domains given in the SSL certificate. |
| 285 for (size_t i = 0; i < dns_names.size(); ++i) { |
| 286 if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos |
| 287 || dns_names[i].length() == host_name.length() |
| 288 || !(IsHostNameKnownTLD(dns_names[i]))) { |
| 289 result = result || false; |
| 290 } else if (dns_names[i].length() > host_name.length()) { |
| 291 result = result || |
| 292 net::StripWWW(base::ASCIIToUTF16(dns_names[i])) == |
| 293 base::ASCIIToUTF16(host_name); |
| 294 } else { |
| 295 result = result || |
| 296 net::StripWWW(base::ASCIIToUTF16(host_name)) == |
| 297 base::ASCIIToUTF16(dns_names[i]); |
| 298 } |
| 299 } |
| 300 return result; |
| 301 } |
| 302 return false; |
| 303 } |
| 304 |
| 305 bool SSLErrorClassification::NameUnderAnyNames( |
| 306 const Tokens& child, |
| 307 const std::vector<Tokens>& potential_parents) const { |
| 308 bool result = false; |
| 309 // Need to account for all the possible domains given in the SSL certificate. |
| 310 for (size_t i = 0; i < potential_parents.size(); ++i) { |
| 311 if (potential_parents[i].empty() || |
| 312 potential_parents[i].size() >= child.size()) { |
| 313 result = result || false; |
| 314 } else { |
| 315 size_t domain_diff = FindSubDomainDifference(child, |
| 316 potential_parents[i]); |
| 317 if (domain_diff == 1 && child[0] != "www") |
| 318 result = result || true; |
| 319 } |
| 320 } |
| 321 return result; |
| 322 } |
| 323 |
| 324 bool SSLErrorClassification::AnyNamesUnderName( |
| 325 const std::vector<Tokens>& potential_children, |
| 326 const Tokens& parent) const { |
| 327 bool result = false; |
| 328 // Need to account for all the possible domains given in the SSL certificate. |
| 329 for (size_t i = 0; i < potential_children.size(); ++i) { |
| 330 if (potential_children[i].empty() || |
| 331 potential_children[i].size() <= parent.size()) { |
| 332 result = result || false; |
| 333 } else { |
| 334 size_t domain_diff = FindSubDomainDifference(potential_children[i], |
| 335 parent); |
| 336 if (domain_diff == 1 && potential_children[i][0] != "www") |
| 337 result = result || true; |
| 338 } |
| 339 } |
| 340 return result; |
| 341 } |
| 342 |
| 343 bool SSLErrorClassification::IsSubDomainOutsideWildcard( |
| 344 const Tokens& host_name_tokens) const { |
| 345 std::string host_name = request_url_.host(); |
| 346 std::vector<std::string> dns_names; |
| 347 cert_.GetDNSNames(&dns_names); |
| 348 bool result = false; |
| 349 |
| 350 // This method requires that the host name be longer than the dns name on |
| 351 // the certificate. |
| 352 for (size_t i = 0; i < dns_names.size(); ++i) { |
| 353 const std::string& name = dns_names[i]; |
| 354 if (name.length() < 2 || name.length() >= host_name.length() || |
| 355 name.find('\0') != std::string::npos || |
| 356 !IsHostNameKnownTLD(name) |
| 357 || name[0] != '*' || name[1] != '.') { |
| 358 continue; |
| 359 } |
| 360 |
| 361 // Move past the "*.". |
| 362 std::string extracted_dns_name = name.substr(2); |
| 363 if (FindSubDomainDifference( |
| 364 host_name_tokens, Tokenize(extracted_dns_name)) == 2) { |
| 365 return true; |
| 366 } |
| 367 } |
| 368 return result; |
| 369 } |
OLD | NEW |