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/string16.h" | |
13 #include "base/strings/string_split.h" | |
14 #include "base/strings/utf_string_conversions.h" | |
10 #include "base/time/time.h" | 15 #include "base/time/time.h" |
11 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/ssl/ssl_error_info.h" |
12 #include "components/network_time/network_time_tracker.h" | 17 #include "net/base/net_util.h" |
18 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | |
19 #include "net/cert/x509_cert_types.h" | |
13 #include "net/cert/x509_certificate.h" | 20 #include "net/cert/x509_certificate.h" |
21 #include "url/gurl.h" | |
14 | 22 |
15 using base::Time; | 23 using base::Time; |
16 using base::TimeTicks; | 24 using base::TimeTicks; |
17 using base::TimeDelta; | 25 using base::TimeDelta; |
18 | 26 |
19 namespace { | 27 namespace { |
20 | 28 |
21 // Events for UMA. Do not reorder or change! | 29 // Events for UMA. Do not reorder or change! |
22 enum SSLInterstitialCause { | 30 enum SSLInterstitialCause { |
23 CLOCK_PAST, | 31 CLOCK_PAST, |
24 CLOCK_FUTURE, | 32 CLOCK_FUTURE, |
33 WWW_SUBDOMAIN_MATCH, | |
34 SUBDOMAIN_MATCH, | |
35 SUBDOMAIN_INVERSE_MATCH, | |
36 SUBDOMAIN_OUTSIDE_WILDCARD, | |
37 SELF_SIGNED, | |
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 int FindSubDomainDifference( | |
61 const std::vector<base::string16>& tokenized_potential_subdomian, | |
palmer
2014/07/17 20:11:19
Why base::string16? GURL::domain returns std::stri
palmer
2014/07/17 20:11:19
Typo: "domian" should be "domain".
radhikabhar
2014/07/18 16:29:28
Done.
radhikabhar
2014/07/18 16:29:29
Done.
| |
62 const std::vector<base::string16>& 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_subdomian.size()) | |
66 return 0; | |
67 | |
68 size_t tokens_match = 0; | |
69 size_t diff_size = tokenized_potential_subdomian.size() - | |
70 tokenized_parent.size(); | |
71 for (size_t i = 0; i < tokenized_parent.size(); ++i) { | |
72 if (tokenized_parent[i] == tokenized_potential_subdomian[i + diff_size]) | |
73 tokens_match++; | |
74 } | |
75 if (tokens_match == tokenized_parent.size()) | |
76 return diff_size; | |
palmer
2014/07/17 20:11:19
|diff_size| is a size_t, but you've declared this
radhikabhar
2014/07/18 16:29:29
Done.
| |
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 if (IsWWWSubDomainMatch()) | |
135 severity_name_score += kServerWeight * kWWWDifferenceWeight; | |
136 if (IsSubDomainMatch()) | |
137 severity_name_score += kServerWeight * kSubDomainWeight; | |
138 // Inverse case is more likely to be a MITM attack. | |
139 if (IsSubDomainInverseMatch()) | |
140 severity_name_score += kServerWeight * kSubDomainInverseWeight; | |
141 return severity_name_score; | |
142 } | |
143 | |
144 void SSLErrorClassification::RecordUMAStatistics(bool overridable, | |
145 int cert_error) { | |
146 SSLErrorInfo::ErrorType type = | |
147 SSLErrorInfo::NetErrorToErrorType(cert_error); | |
148 | |
149 if (type == SSLErrorInfo::CERT_DATE_INVALID) { | |
150 if (IsUserClockInThePast(base::Time::NowFromSystemTime())) | |
151 RecordSSLInterstitialCause(overridable, CLOCK_PAST); | |
152 if (IsUserClockInTheFuture(base::Time::NowFromSystemTime())) | |
153 RecordSSLInterstitialCause(overridable, CLOCK_FUTURE); | |
154 } | |
155 | |
156 if (type == SSLErrorInfo::CERT_COMMON_NAME_INVALID) { | |
157 if (IsWWWSubDomainMatch()) | |
158 RecordSSLInterstitialCause(overridable, WWW_SUBDOMAIN_MATCH); | |
159 if (IsSubDomainMatch()) | |
160 RecordSSLInterstitialCause(overridable, SUBDOMAIN_MATCH); | |
161 if (IsSubDomainInverseMatch()) | |
162 RecordSSLInterstitialCause(overridable, SUBDOMAIN_INVERSE_MATCH); | |
163 } | |
164 | |
165 if (type == SSLErrorInfo::CERT_AUTHORITY_INVALID) { | |
166 if (IsSelfSigned()) | |
167 RecordSSLInterstitialCause(overridable, SELF_SIGNED); | |
168 } | |
169 } | |
170 | |
84 base::TimeDelta SSLErrorClassification::TimePassedSinceExpiry() const { | 171 base::TimeDelta SSLErrorClassification::TimePassedSinceExpiry() const { |
85 base::TimeDelta delta = current_time_ - cert_.valid_expiry(); | 172 base::TimeDelta delta = current_time_ - cert_.valid_expiry(); |
86 return delta; | 173 return delta; |
87 } | 174 } |
88 | 175 |
89 float SSLErrorClassification::CalculateScoreTimePassedSinceExpiry() const { | 176 float SSLErrorClassification::CalculateScoreTimePassedSinceExpiry() const { |
90 base::TimeDelta delta = TimePassedSinceExpiry(); | 177 base::TimeDelta delta = TimePassedSinceExpiry(); |
91 int64 time_passed = delta.InDays(); | 178 int64 time_passed = delta.InDays(); |
92 const int64 kHighThreshold = 7; | 179 const int64 kHighThreshold = 7; |
93 const int64 kLowThreshold = 4; | 180 const int64 kLowThreshold = 4; |
(...skipping 15 matching lines...) Expand all Loading... | |
109 return false; | 196 return false; |
110 } | 197 } |
111 | 198 |
112 bool SSLErrorClassification::IsUserClockInTheFuture(base::Time time_now) { | 199 bool SSLErrorClassification::IsUserClockInTheFuture(base::Time time_now) { |
113 base::Time build_time = base::GetBuildTime(); | 200 base::Time build_time = base::GetBuildTime(); |
114 if (time_now > build_time + base::TimeDelta::FromDays(365)) | 201 if (time_now > build_time + base::TimeDelta::FromDays(365)) |
115 return true; | 202 return true; |
116 return false; | 203 return false; |
117 } | 204 } |
118 | 205 |
119 void SSLErrorClassification::RecordUMAStatistics(bool overridable) { | 206 bool SSLErrorClassification::IsNotValidURL(const std::string& host_name) { |
120 if (IsUserClockInThePast(base::Time::NowFromSystemTime())) | 207 size_t tld_length = |
121 RecordSSLInterstitialCause(overridable, CLOCK_PAST); | 208 net::registry_controlled_domains::GetRegistryLength( |
122 if (IsUserClockInTheFuture(base::Time::NowFromSystemTime())) | 209 host_name, |
123 RecordSSLInterstitialCause(overridable, CLOCK_FUTURE); | 210 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, |
211 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | |
212 if (tld_length == 0 || tld_length == std::string::npos) | |
213 return true; | |
214 return false; | |
124 } | 215 } |
216 | |
217 // We accept the inverse case for www for historical reasons. | |
218 bool SSLErrorClassification::IsWWWSubDomainMatch() const { | |
219 std::string host_name = request_url_.host(); | |
220 if (IsNotValidURL(host_name)) { | |
221 return false; | |
222 } | |
223 std::vector<std::string> dns_names; | |
224 cert_.GetDNSNames(&dns_names); | |
225 bool result = false; | |
226 | |
227 // Need to account for all possible domains given in the SSL certificate. | |
228 for (size_t i = 0; i < dns_names.size(); ++i) { | |
229 if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos | |
230 || dns_names[i].length() == host_name.length() | |
231 || IsNotValidURL(dns_names[i])) { | |
232 result = result || false; | |
233 } else if (dns_names[i].length() > host_name.length()) { | |
234 result = result || | |
235 net::StripWWW(base::ASCIIToUTF16(dns_names[i])) == | |
236 base::ASCIIToUTF16(host_name); | |
237 } else { | |
238 result = result || | |
239 net::StripWWW(base::ASCIIToUTF16(host_name)) == | |
240 base::ASCIIToUTF16(dns_names[i]); | |
241 } | |
242 } | |
243 return result; | |
244 } | |
245 | |
246 bool SSLErrorClassification::IsSubDomainMatch() const { | |
247 std::string host_name = request_url_.host(); | |
248 if (IsNotValidURL(host_name)) { | |
249 return false; | |
250 } | |
251 std::vector<std::string> dns_names; | |
252 cert_.GetDNSNames(&dns_names); | |
253 bool result = false; | |
254 | |
255 // Need to account for all the possible domains given in the SSL certificate. | |
256 for (size_t i = 0; i < dns_names.size(); ++i) { | |
257 if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos | |
258 || dns_names[i].length() >= host_name.length() | |
259 || IsNotValidURL(dns_names[i])) { | |
260 result = result || false; | |
261 } else { | |
262 std::vector<base::string16> dns_name_tokens; | |
263 std::vector<base::string16> host_name_tokens; | |
264 base::SplitStringDontTrim(base::ASCIIToUTF16(dns_names[i]), | |
265 '.', | |
266 &dns_name_tokens); | |
267 base::SplitStringDontTrim(base::ASCIIToUTF16(host_name), | |
268 '.', | |
269 &host_name_tokens); | |
270 int domain_diff = FindSubDomainDifference(host_name_tokens, | |
271 dns_name_tokens); | |
272 if (domain_diff == 1 && host_name_tokens[0] != base::ASCIIToUTF16("www")) | |
273 result = result || true; | |
274 } | |
275 } | |
276 return result; | |
277 } | |
278 | |
279 // The inverse case should be treated carefully as this is most likely a MITM | |
280 // attack. We don't want foo.appspot.com to be able to MITM for appspot.com. | |
281 bool SSLErrorClassification::IsSubDomainInverseMatch() const { | |
282 std::string host_name = request_url_.host(); | |
283 if (IsNotValidURL(host_name)) { | |
284 return false; | |
285 } | |
286 std::vector<std::string> dns_names; | |
287 cert_.GetDNSNames(&dns_names); | |
288 bool result = false; | |
289 | |
290 // Need to account for all the possible domains given in the SSL certificate. | |
291 for (size_t i = 0; i < dns_names.size(); ++i) { | |
292 if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos | |
293 || dns_names[i].length() <= host_name.length() | |
294 || IsNotValidURL(dns_names[i])) { | |
295 result = result || false; | |
296 } else { | |
297 std::vector<base::string16> dns_name_tokens; | |
palmer
2014/07/17 20:11:19
Also, this tokenizing code is repeated in all thes
radhikabhar
2014/07/18 16:29:29
Done.
| |
298 std::vector<base::string16> host_name_tokens; | |
299 base::SplitStringDontTrim(base::ASCIIToUTF16(dns_names[i]), | |
palmer
2014/07/17 20:11:19
Don't convert std::string to string16; it's unnec
radhikabhar
2014/07/18 16:29:29
Done.
| |
300 '.', | |
301 &dns_name_tokens); | |
302 base::SplitStringDontTrim(base::ASCIIToUTF16(host_name), | |
303 '.', | |
304 &host_name_tokens); | |
305 int domain_diff = FindSubDomainDifference(dns_name_tokens, | |
palmer
2014/07/17 20:11:20
size_t
radhikabhar
2014/07/18 16:29:29
Done.
| |
306 host_name_tokens); | |
307 if (domain_diff == 1 && dns_name_tokens[0] != base::ASCIIToUTF16("www")) | |
308 result = result || true; | |
309 } | |
310 } | |
311 return result; | |
312 } | |
313 | |
314 // This method is valid for wildcard certificates only. | |
315 bool SSLErrorClassification::IsSubDomainOutsideWildcard() const { | |
316 std::string host_name = request_url_.host(); | |
317 if (IsNotValidURL(host_name)) | |
318 return false; | |
319 | |
320 std::vector<std::string> dns_names; | |
321 cert_.GetDNSNames(&dns_names); | |
322 bool result = false; | |
323 | |
324 // This method requires that the host name be longer than the dns name on | |
325 // the certificate. | |
326 for (size_t i = 0; i < dns_names.size(); ++i) { | |
327 if (!(dns_names[i][0] == '*' && dns_names[i][1] == '.')) { | |
328 result = result || false; | |
329 } else { | |
330 if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos | |
331 || dns_names[i].length() >= host_name.length() | |
332 || IsNotValidURL(dns_names[i])) { | |
333 result = result || false; | |
334 } else { | |
335 // Move past the '*.'. | |
336 std::string extracted_dns_name = dns_names[i].substr(2); | |
337 std::vector<base::string16> extracted_dns_name_tokens; | |
338 std::vector<base::string16> host_name_tokens; | |
339 base::SplitStringDontTrim(base::ASCIIToUTF16(extracted_dns_name), | |
palmer
2014/07/17 20:11:19
same thing here, don't convert to string16.
radhikabhar
2014/07/18 16:29:29
Done.
| |
340 '.', | |
341 &extracted_dns_name_tokens); | |
342 base::SplitStringDontTrim(base::ASCIIToUTF16(host_name), | |
343 '.', | |
344 &host_name_tokens); | |
345 int domain_diff = FindSubDomainDifference(host_name_tokens, | |
346 extracted_dns_name_tokens); | |
347 if (domain_diff == 2) | |
348 result = result || true; | |
349 } | |
350 } | |
351 } | |
352 return result; | |
353 } | |
354 | |
355 bool SSLErrorClassification::IsSelfSigned() const { | |
356 // Check whether the issuer and the subject are the same. | |
357 const net::CertPrincipal& subject = cert_.subject(); | |
358 const net::CertPrincipal& issuer = cert_.issuer(); | |
359 bool result = subject.common_name == issuer.common_name && | |
360 subject.locality_name == issuer.locality_name && | |
361 subject.state_or_province_name == issuer.state_or_province_name && | |
362 subject.country_name == issuer.country_name && | |
363 subject.street_addresses == issuer.street_addresses && | |
364 subject.organization_names == issuer.organization_names && | |
365 subject.organization_unit_names == issuer.organization_names && | |
366 subject.domain_components == issuer.domain_components; | |
367 return result; | |
368 } | |
OLD | NEW |