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 |
59 // Utility function - For two unequal strings which have been tokenized, this | |
60 // method checks to see whether |tokenized_potential_subdomain| is a subdomain | |
61 // of |tokenized_parent| and if it is then it returns the difference in the | |
62 // number of tokens between both the vectors, i.e. the difference in the vector | |
63 // size. | |
64 size_t FindSubDomainDifference( | |
65 const std::vector<std::string>& tokenized_potential_subdomain, | |
66 const std::vector<std::string>& tokenized_parent) { | |
67 // A check to ensure that the number of tokens in the tokenized_parent is | |
68 // less than the tokenized_potential_subdomain. | |
69 if (tokenized_parent.size() >= tokenized_potential_subdomain.size()) | |
70 return 0; | |
71 | |
72 size_t tokens_match = 0; | |
73 size_t diff_size = tokenized_potential_subdomain.size() - | |
74 tokenized_parent.size(); | |
75 for (size_t i = 0; i < tokenized_parent.size(); ++i) { | |
76 if (tokenized_parent[i] == tokenized_potential_subdomain[i + diff_size]) | |
77 tokens_match++; | |
78 } | |
79 if (tokens_match == tokenized_parent.size()) | |
80 return diff_size; | |
81 return 0; | |
82 } | |
83 | |
42 } // namespace | 84 } // namespace |
43 | 85 |
44 SSLErrorClassification::SSLErrorClassification( | 86 SSLErrorClassification::SSLErrorClassification( |
45 base::Time current_time, | 87 base::Time current_time, |
88 const GURL& url, | |
46 const net::X509Certificate& cert) | 89 const net::X509Certificate& cert) |
47 : current_time_(current_time), | 90 : current_time_(current_time), |
91 request_url_(url), | |
48 cert_(cert) { } | 92 cert_(cert) { } |
49 | 93 |
50 SSLErrorClassification::~SSLErrorClassification() { } | 94 SSLErrorClassification::~SSLErrorClassification() { } |
51 | 95 |
52 float SSLErrorClassification::InvalidDateSeverityScore() const { | 96 float SSLErrorClassification::InvalidDateSeverityScore() const{ |
53 // Client-side characterisitics. Check whether the system's clock is wrong or | 97 // Client-side characteristics. Check whether or not the system's clock is |
54 // not and whether the user has encountered this error before or not. | 98 // wrong and whether or not the user has already encountered this error |
99 // before. | |
55 float severity_date_score = 0.0f; | 100 float severity_date_score = 0.0f; |
56 | 101 |
57 static const float kClientWeight = 0.5f; | 102 static const float kCertificateExpiredWeight = 0.3f; |
103 static const float kNotYetValidWeight = 0.2f; | |
104 | |
58 static const float kSystemClockWeight = 0.75f; | 105 static const float kSystemClockWeight = 0.75f; |
59 static const float kSystemClockWrongWeight = 0.1f; | 106 static const float kSystemClockWrongWeight = 0.1f; |
60 static const float kSystemClockRightWeight = 1.0f; | 107 static const float kSystemClockRightWeight = 1.0f; |
61 | 108 |
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_) || | 109 if (IsUserClockInThePast(current_time_) || |
67 IsUserClockInTheFuture(current_time_)) { | 110 IsUserClockInTheFuture(current_time_)) { |
68 severity_date_score = kClientWeight * kSystemClockWeight * | 111 severity_date_score += kClientWeight * kSystemClockWeight * |
69 kSystemClockWrongWeight; | 112 kSystemClockWrongWeight; |
70 } else { | 113 } else { |
71 severity_date_score = kClientWeight * kSystemClockWeight * | 114 severity_date_score += kClientWeight * kSystemClockWeight * |
72 kSystemClockRightWeight; | 115 kSystemClockRightWeight; |
73 } | 116 } |
74 // TODO(radhikabhar): (crbug.com/393262) Check website settings. | 117 // TODO(radhikabhar): (crbug.com/393262) Check website settings. |
75 | 118 |
76 // Server-side characteristics. Check whether the certificate has expired or | 119 // 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 | 120 // is not yet valid. If the certificate has expired then factor the time which |
78 // has passed since expiry. | 121 // has passed since expiry. |
79 if (cert_.HasExpired()) { | 122 if (cert_.HasExpired()) { |
80 severity_date_score += kServerWeight * kCertificateExpiredWeight * | 123 severity_date_score += kServerWeight * kCertificateExpiredWeight * |
81 CalculateScoreTimePassedSinceExpiry(); | 124 CalculateScoreTimePassedSinceExpiry(); |
82 } | 125 } |
83 if (current_time_ < cert_.valid_start()) | 126 if (current_time_ < cert_.valid_start()) |
84 severity_date_score += kServerWeight * kNotYetValidWeight; | 127 severity_date_score += kServerWeight * kNotYetValidWeight; |
85 return severity_date_score; | 128 return severity_date_score; |
86 } | 129 } |
87 | 130 |
131 float SSLErrorClassification::InvalidCommonNameSeverityScore() const { | |
132 float severity_name_score = 0.0f; | |
133 | |
134 static const float kWWWDifferenceWeight = 0.3f; | |
135 static const float kSubDomainWeight = 0.2f; | |
136 static const float kSubDomainInverseWeight = 1.0f; | |
137 | |
138 std::string host_name = request_url_.host(); | |
139 if (IsHostNameKnownTLD(host_name)) { | |
140 Tokens host_name_tokens; | |
141 base::SplitStringDontTrim(host_name, | |
142 '.', | |
143 &host_name_tokens); | |
144 if (IsWWWSubDomainMatch()) | |
145 severity_name_score += kServerWeight * kWWWDifferenceWeight; | |
146 if (IsSubDomainOutsideWildcard(host_name_tokens)) | |
147 severity_name_score += kServerWeight * kWWWDifferenceWeight; | |
148 | |
149 std::vector<std::string> dns_names; | |
150 cert_.GetDNSNames(&dns_names); | |
151 std::vector<Tokens> dns_name_tokens = GetTokenizedDNSNames(dns_names); | |
152 if (NameUnderAnyNames(host_name_tokens, dns_name_tokens)) | |
153 severity_name_score += kServerWeight * kSubDomainWeight; | |
154 // Inverse case is more likely to be a MITM attack. | |
155 if (AnyNamesUnderName(dns_name_tokens, host_name_tokens)) | |
156 severity_name_score += kServerWeight * kSubDomainInverseWeight; | |
157 } | |
158 return severity_name_score; | |
159 } | |
160 | |
161 void SSLErrorClassification::RecordUMAStatistics(bool overridable, | |
162 int cert_error) { | |
163 SSLErrorInfo::ErrorType type = | |
164 SSLErrorInfo::NetErrorToErrorType(cert_error); | |
165 | |
166 if (type == SSLErrorInfo::CERT_DATE_INVALID) { | |
167 if (IsUserClockInThePast(base::Time::NowFromSystemTime())) | |
168 RecordSSLInterstitialCause(overridable, CLOCK_PAST); | |
169 if (IsUserClockInTheFuture(base::Time::NowFromSystemTime())) | |
170 RecordSSLInterstitialCause(overridable, CLOCK_FUTURE); | |
171 } | |
172 | |
173 if (type == SSLErrorInfo::CERT_COMMON_NAME_INVALID) { | |
174 std::string host_name = request_url_.host(); | |
175 if (IsHostNameKnownTLD(host_name)) { | |
176 Tokens host_name_tokens; | |
177 base::SplitStringDontTrim(host_name, | |
178 '.', | |
179 &host_name_tokens); | |
180 if (IsWWWSubDomainMatch()) | |
181 RecordSSLInterstitialCause(overridable, WWW_SUBDOMAIN_MATCH); | |
182 if (IsSubDomainOutsideWildcard(host_name_tokens)) | |
183 RecordSSLInterstitialCause(overridable, SUBDOMAIN_OUTSIDE_WILDCARD); | |
184 | |
185 std::vector<std::string> dns_names; | |
186 cert_.GetDNSNames(&dns_names); | |
187 std::vector<Tokens> dns_name_tokens = GetTokenizedDNSNames(dns_names); | |
188 if (NameUnderAnyNames(host_name_tokens, dns_name_tokens)) | |
189 RecordSSLInterstitialCause(overridable, SUBDOMAIN_MATCH); | |
190 if (AnyNamesUnderName(dns_name_tokens, host_name_tokens)) | |
191 RecordSSLInterstitialCause(overridable, SUBDOMAIN_INVERSE_MATCH); | |
192 } else { | |
193 RecordSSLInterstitialCause(overridable, HOST_NAME_NOT_KNOWN_TLD); | |
194 } | |
195 } | |
196 } | |
197 | |
88 base::TimeDelta SSLErrorClassification::TimePassedSinceExpiry() const { | 198 base::TimeDelta SSLErrorClassification::TimePassedSinceExpiry() const { |
89 base::TimeDelta delta = current_time_ - cert_.valid_expiry(); | 199 base::TimeDelta delta = current_time_ - cert_.valid_expiry(); |
90 return delta; | 200 return delta; |
91 } | 201 } |
92 | 202 |
93 float SSLErrorClassification::CalculateScoreTimePassedSinceExpiry() const { | 203 float SSLErrorClassification::CalculateScoreTimePassedSinceExpiry() const { |
94 base::TimeDelta delta = TimePassedSinceExpiry(); | 204 base::TimeDelta delta = TimePassedSinceExpiry(); |
95 int64 time_passed = delta.InDays(); | 205 int64 time_passed = delta.InDays(); |
96 const int64 kHighThreshold = 7; | 206 const int64 kHighThreshold = 7; |
97 const int64 kLowThreshold = 4; | 207 const int64 kLowThreshold = 4; |
(...skipping 25 matching lines...) Expand all Loading... | |
123 bool SSLErrorClassification::IsWindowsVersionSP3OrLower() { | 233 bool SSLErrorClassification::IsWindowsVersionSP3OrLower() { |
124 #if defined(OS_WIN) | 234 #if defined(OS_WIN) |
125 const base::win::OSInfo* os_info = base::win::OSInfo::GetInstance(); | 235 const base::win::OSInfo* os_info = base::win::OSInfo::GetInstance(); |
126 base::win::OSInfo::ServicePack service_pack = os_info->service_pack(); | 236 base::win::OSInfo::ServicePack service_pack = os_info->service_pack(); |
127 if (os_info->version() < base::win::VERSION_VISTA && service_pack.major < 3) | 237 if (os_info->version() < base::win::VERSION_VISTA && service_pack.major < 3) |
128 return true; | 238 return true; |
129 #endif | 239 #endif |
130 return false; | 240 return false; |
131 } | 241 } |
132 | 242 |
133 void SSLErrorClassification::RecordUMAStatistics(bool overridable) { | 243 bool SSLErrorClassification::IsHostNameKnownTLD(const std::string& host_name) { |
134 if (IsUserClockInThePast(base::Time::NowFromSystemTime())) | 244 size_t tld_length = |
135 RecordSSLInterstitialCause(overridable, CLOCK_PAST); | 245 net::registry_controlled_domains::GetRegistryLength( |
136 if (IsUserClockInTheFuture(base::Time::NowFromSystemTime())) | 246 host_name, |
137 RecordSSLInterstitialCause(overridable, CLOCK_FUTURE); | 247 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, |
248 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | |
249 if (tld_length == 0 || tld_length == std::string::npos) | |
250 return false; | |
251 return true; | |
138 } | 252 } |
253 | |
254 std::vector<std::vector<std::string>> SSLErrorClassification:: | |
255 GetTokenizedDNSNames(std::vector<std::string>& dns_names) const{ | |
256 std::vector<std::vector<std::string>> dns_name_tokens; | |
257 for (size_t i = 0; i < dns_names.size(); ++i) { | |
258 std::vector<std::string> dns_name_token_single; | |
259 if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos | |
260 || !(IsHostNameKnownTLD(dns_names[i]))) { | |
261 dns_name_token_single.push_back(std::string()); | |
262 } else { | |
263 base::SplitStringDontTrim(dns_names[i], | |
264 '.', | |
265 &dns_name_token_single); | |
266 } | |
267 dns_name_tokens.push_back(dns_name_token_single); | |
268 } | |
269 return dns_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. | |
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 // This method is valid for wildcard certificates only. | |
palmer
2014/07/31 22:40:30
All documentation for functions should be in the .
radhikabhar
2014/08/01 23:06:56
Done.
| |
341 bool SSLErrorClassification::IsSubDomainOutsideWildcard( | |
342 const Tokens& host_name_tokens) const { | |
343 std::string host_name = request_url_.host(); | |
344 std::vector<std::string> dns_names; | |
345 cert_.GetDNSNames(&dns_names); | |
346 bool result = false; | |
347 | |
348 // This method requires that the host name be longer than the dns name on | |
349 // the certificate. | |
350 for (size_t i = 0; i < dns_names.size(); ++i) { | |
351 if (!(dns_names[i][0] == '*' && dns_names[i][1] == '.')) { | |
352 result = result || false; | |
353 } else { | |
354 if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos | |
355 || dns_names[i].length() >= host_name.length() | |
356 || !(IsHostNameKnownTLD(dns_names[i]))) { | |
357 result = result || false; | |
358 } else { | |
359 // Move past the '*.'. | |
360 std::string extracted_dns_name = dns_names[i].substr(2); | |
361 Tokens extracted_dns_name_tokens; | |
362 base::SplitStringDontTrim(extracted_dns_name, | |
palmer
2014/07/31 22:40:30
As I think I said before, this repeated code block
radhikabhar
2014/08/01 23:06:56
It was in another CL. Forgot to merge it with this
| |
363 '.', | |
364 &extracted_dns_name_tokens); | |
365 size_t domain_diff = FindSubDomainDifference(host_name_tokens, | |
366 extracted_dns_name_tokens); | |
367 if (domain_diff == 2) | |
368 result = result || true; | |
369 } | |
370 } | |
371 } | |
372 return result; | |
373 } | |
OLD | NEW |