Chromium Code Reviews| 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 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 | |
| 146 if (type == SSLErrorInfo::CERT_DATE_INVALID) { | |
|
palmer
2014/08/04 23:48:29
Nit: A switch/case might be cleaner.
switch (type
radhikabhar
2014/08/05 02:17:36
Done.
| |
| 147 if (IsUserClockInThePast(base::Time::NowFromSystemTime())) | |
| 148 RecordSSLInterstitialCause(overridable, CLOCK_PAST); | |
| 149 if (IsUserClockInTheFuture(base::Time::NowFromSystemTime())) | |
| 150 RecordSSLInterstitialCause(overridable, CLOCK_FUTURE); | |
| 151 } | |
| 152 | |
| 153 if (type == 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 | |
| 162 std::vector<std::string> dns_names; | |
| 163 cert_.GetDNSNames(&dns_names); | |
| 164 std::vector<Tokens> dns_name_tokens = GetTokenizedDNSNames(dns_names); | |
| 165 if (NameUnderAnyNames(host_name_tokens, dns_name_tokens)) | |
| 166 RecordSSLInterstitialCause(overridable, SUBDOMAIN_MATCH); | |
| 167 if (AnyNamesUnderName(dns_name_tokens, host_name_tokens)) | |
| 168 RecordSSLInterstitialCause(overridable, SUBDOMAIN_INVERSE_MATCH); | |
| 169 } else { | |
| 170 RecordSSLInterstitialCause(overridable, HOST_NAME_NOT_KNOWN_TLD); | |
| 171 } | |
| 172 } | |
| 173 } | |
| 174 | |
| 88 base::TimeDelta SSLErrorClassification::TimePassedSinceExpiry() const { | 175 base::TimeDelta SSLErrorClassification::TimePassedSinceExpiry() const { |
| 89 base::TimeDelta delta = current_time_ - cert_.valid_expiry(); | 176 base::TimeDelta delta = current_time_ - cert_.valid_expiry(); |
| 90 return delta; | 177 return delta; |
| 91 } | 178 } |
| 92 | 179 |
| 93 float SSLErrorClassification::CalculateScoreTimePassedSinceExpiry() const { | 180 float SSLErrorClassification::CalculateScoreTimePassedSinceExpiry() const { |
| 94 base::TimeDelta delta = TimePassedSinceExpiry(); | 181 base::TimeDelta delta = TimePassedSinceExpiry(); |
| 95 int64 time_passed = delta.InDays(); | 182 int64 time_passed = delta.InDays(); |
| 96 const int64 kHighThreshold = 7; | 183 const int64 kHighThreshold = 7; |
| 97 const int64 kLowThreshold = 4; | 184 const int64 kLowThreshold = 4; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 123 bool SSLErrorClassification::IsWindowsVersionSP3OrLower() { | 210 bool SSLErrorClassification::IsWindowsVersionSP3OrLower() { |
| 124 #if defined(OS_WIN) | 211 #if defined(OS_WIN) |
| 125 const base::win::OSInfo* os_info = base::win::OSInfo::GetInstance(); | 212 const base::win::OSInfo* os_info = base::win::OSInfo::GetInstance(); |
| 126 base::win::OSInfo::ServicePack service_pack = os_info->service_pack(); | 213 base::win::OSInfo::ServicePack service_pack = os_info->service_pack(); |
| 127 if (os_info->version() < base::win::VERSION_VISTA && service_pack.major < 3) | 214 if (os_info->version() < base::win::VERSION_VISTA && service_pack.major < 3) |
| 128 return true; | 215 return true; |
| 129 #endif | 216 #endif |
| 130 return false; | 217 return false; |
| 131 } | 218 } |
| 132 | 219 |
| 133 void SSLErrorClassification::RecordUMAStatistics(bool overridable) { | 220 bool SSLErrorClassification::IsHostNameKnownTLD(const std::string& host_name) { |
| 134 if (IsUserClockInThePast(base::Time::NowFromSystemTime())) | 221 size_t tld_length = |
| 135 RecordSSLInterstitialCause(overridable, CLOCK_PAST); | 222 net::registry_controlled_domains::GetRegistryLength( |
| 136 if (IsUserClockInTheFuture(base::Time::NowFromSystemTime())) | 223 host_name, |
| 137 RecordSSLInterstitialCause(overridable, CLOCK_FUTURE); | 224 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, |
| 225 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | |
| 226 if (tld_length == 0 || tld_length == std::string::npos) | |
| 227 return false; | |
| 228 return true; | |
| 138 } | 229 } |
| 230 | |
| 231 std::vector<std::vector<std::string>> SSLErrorClassification:: | |
|
palmer
2014/08/04 23:48:29
Nit: std::vector<Tokens> is easier to read.
radhikabhar
2014/08/05 02:17:36
Done.
| |
| 232 GetTokenizedDNSNames(const std::vector<std::string>& dns_names) { | |
| 233 std::vector<std::vector<std::string>> dns_name_tokens; | |
| 234 for (size_t i = 0; i < dns_names.size(); ++i) { | |
| 235 std::vector<std::string> dns_name_token_single; | |
| 236 if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos | |
| 237 || !(IsHostNameKnownTLD(dns_names[i]))) { | |
| 238 dns_name_token_single.push_back(std::string()); | |
| 239 } else { | |
| 240 dns_name_token_single = Tokenize(dns_names[i]); | |
| 241 } | |
| 242 dns_name_tokens.push_back(dns_name_token_single); | |
| 243 } | |
| 244 return dns_name_tokens; | |
| 245 } | |
| 246 | |
| 247 size_t SSLErrorClassification::FindSubDomainDifference( | |
| 248 const Tokens& potential_subdomain, const Tokens& parent) const { | |
| 249 // A check to ensure that the number of tokens in the tokenized_parent is | |
| 250 // less than the tokenized_potential_subdomain. | |
| 251 if (parent.size() >= potential_subdomain.size()) | |
| 252 return 0; | |
| 253 | |
| 254 size_t tokens_match = 0; | |
| 255 size_t diff_size = potential_subdomain.size() - parent.size(); | |
| 256 for (size_t i = 0; i < parent.size(); ++i) { | |
| 257 if (parent[i] == potential_subdomain[i + diff_size]) | |
| 258 tokens_match++; | |
| 259 } | |
| 260 if (tokens_match == parent.size()) | |
| 261 return diff_size; | |
| 262 return 0; | |
| 263 } | |
| 264 | |
| 265 std::vector<std::string> SSLErrorClassification:: | |
|
palmer
2014/08/04 23:48:29
Nit: Use the Tokens typedef here too
radhikabhar
2014/08/05 02:17:36
Done.
| |
| 266 Tokenize(const std::string& name) { | |
| 267 Tokens name_tokens; | |
| 268 base::SplitStringDontTrim(name, '.', &name_tokens); | |
| 269 return name_tokens; | |
| 270 } | |
| 271 | |
| 272 // We accept the inverse case for www for historical reasons. | |
| 273 bool SSLErrorClassification::IsWWWSubDomainMatch() const { | |
| 274 std::string host_name = request_url_.host(); | |
| 275 if (IsHostNameKnownTLD(host_name)) { | |
| 276 std::vector<std::string> dns_names; | |
| 277 cert_.GetDNSNames(&dns_names); | |
| 278 bool result = false; | |
| 279 // Need to account for all possible domains given in the SSL certificate. | |
| 280 for (size_t i = 0; i < dns_names.size(); ++i) { | |
| 281 if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos | |
| 282 || dns_names[i].length() == host_name.length() | |
| 283 || !(IsHostNameKnownTLD(dns_names[i]))) { | |
| 284 result = result || false; | |
| 285 } else if (dns_names[i].length() > host_name.length()) { | |
| 286 result = result || | |
| 287 net::StripWWW(base::ASCIIToUTF16(dns_names[i])) == | |
| 288 base::ASCIIToUTF16(host_name); | |
| 289 } else { | |
| 290 result = result || | |
| 291 net::StripWWW(base::ASCIIToUTF16(host_name)) == | |
| 292 base::ASCIIToUTF16(dns_names[i]); | |
| 293 } | |
| 294 } | |
| 295 return result; | |
| 296 } | |
| 297 return false; | |
| 298 } | |
| 299 | |
| 300 bool SSLErrorClassification::NameUnderAnyNames( | |
| 301 const Tokens& child, | |
| 302 const std::vector<Tokens>& potential_parents) const { | |
| 303 bool result = false; | |
| 304 // Need to account for all the possible domains given in the SSL certificate. | |
| 305 for (size_t i = 0; i < potential_parents.size(); ++i) { | |
| 306 if (potential_parents[i].empty() || | |
| 307 potential_parents[i].size() >= child.size()) { | |
| 308 result = result || false; | |
| 309 } else { | |
| 310 size_t domain_diff = FindSubDomainDifference(child, | |
| 311 potential_parents[i]); | |
| 312 if (domain_diff == 1 && child[0] != "www") | |
| 313 result = result || true; | |
| 314 } | |
| 315 } | |
| 316 return result; | |
| 317 } | |
| 318 | |
| 319 // The inverse case should be treated carefully as this is most likely a MITM | |
| 320 // attack. We don't want foo.appspot.com to be able to MITM for appspot.com. | |
|
palmer
2014/08/04 23:48:29
This documentation should be in the header file.
radhikabhar
2014/08/05 02:17:36
Done.
| |
| 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 if (!(dns_names[i][0] == '*' && dns_names[i][1] == '.')) { | |
|
palmer
2014/08/04 23:48:29
I think (?) this is equivalent, and easier to read
radhikabhar
2014/08/05 02:17:36
Done.
| |
| 351 result = result || false; | |
| 352 } else { | |
| 353 if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos | |
| 354 || dns_names[i].length() >= host_name.length() | |
| 355 || !(IsHostNameKnownTLD(dns_names[i]))) { | |
| 356 result = result || false; | |
| 357 } else { | |
| 358 // Move past the '*.'. | |
| 359 std::string extracted_dns_name = dns_names[i].substr(2); | |
| 360 size_t domain_diff = FindSubDomainDifference( | |
| 361 host_name_tokens, | |
| 362 Tokenize(extracted_dns_name)); | |
| 363 if (domain_diff == 2) | |
| 364 result = result || true; | |
| 365 } | |
| 366 } | |
| 367 } | |
| 368 return result; | |
| 369 } | |
| OLD | NEW |