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