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