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, | |
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_subdomain| is a subdomain | |
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 size_t FindSubDomainDifference( | |
60 const std::vector<std::string>& tokenized_potential_subdomain, | |
61 const std::vector<std::string>& tokenized_parent) { | |
62 // A check to ensure that the number of tokens in the tokenized_parent is | |
63 // less than the tokenized_potential_subdomain. | |
64 if (tokenized_parent.size() >= tokenized_potential_subdomain.size()) | |
65 return 0; | |
66 | |
67 size_t tokens_match = 0; | |
68 size_t diff_size = tokenized_potential_subdomain.size() - | |
69 tokenized_parent.size(); | |
70 for (size_t i = 0; i < tokenized_parent.size(); ++i) { | |
71 if (tokenized_parent[i] == tokenized_potential_subdomain[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 std::string host_name = request_url_.host(); | |
134 if (IsHostNameKnownTLD(host_name)) { | |
135 std::vector<std::string> host_name_tokens; | |
136 base::SplitStringDontTrim(host_name, | |
137 '.', | |
138 &host_name_tokens); | |
139 if (IsWWWSubDomainMatch()) | |
140 severity_name_score += kServerWeight * kWWWDifferenceWeight; | |
141 if (IsSubDomainOutsideWildcard(host_name_tokens)) | |
142 severity_name_score += kServerWeight * kWWWDifferenceWeight; | |
143 | |
144 std::vector<std::string> dns_names; | |
145 cert_.GetDNSNames(&dns_names); | |
146 std::vector<std::vector<std::string>> dns_name_tokens = | |
147 GetTokenizedDNSNames(dns_names); | |
148 if (IsSubDomainMatch(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 (IsSubDomainInverseMatch(host_name_tokens, dns_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)) { | |
felt
2014/07/21 21:34:13
Suggestion: you might want to record how often the
radhikabhar
2014/07/22 16:03:38
Done.
| |
172 std::vector<std::string> 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<std::vector<std::string>> dns_name_tokens = | |
184 GetTokenizedDNSNames(dns_names); | |
185 if (IsSubDomainMatch(host_name_tokens, dns_name_tokens)) | |
186 RecordSSLInterstitialCause(overridable, SUBDOMAIN_MATCH); | |
187 if (IsSubDomainInverseMatch(host_name_tokens, dns_name_tokens)) | |
188 RecordSSLInterstitialCause(overridable, SUBDOMAIN_INVERSE_MATCH); | |
189 } | |
190 } | |
191 | |
192 if (type == SSLErrorInfo::CERT_AUTHORITY_INVALID) { | |
193 if (IsSelfSigned()) | |
194 RecordSSLInterstitialCause(overridable, SELF_SIGNED); | |
195 } | |
196 } | |
197 | |
84 base::TimeDelta SSLErrorClassification::TimePassedSinceExpiry() const { | 198 base::TimeDelta SSLErrorClassification::TimePassedSinceExpiry() const { |
85 base::TimeDelta delta = current_time_ - cert_.valid_expiry(); | 199 base::TimeDelta delta = current_time_ - cert_.valid_expiry(); |
86 return delta; | 200 return delta; |
87 } | 201 } |
88 | 202 |
89 float SSLErrorClassification::CalculateScoreTimePassedSinceExpiry() const { | 203 float SSLErrorClassification::CalculateScoreTimePassedSinceExpiry() const { |
90 base::TimeDelta delta = TimePassedSinceExpiry(); | 204 base::TimeDelta delta = TimePassedSinceExpiry(); |
91 int64 time_passed = delta.InDays(); | 205 int64 time_passed = delta.InDays(); |
92 const int64 kHighThreshold = 7; | 206 const int64 kHighThreshold = 7; |
93 const int64 kLowThreshold = 4; | 207 const int64 kLowThreshold = 4; |
(...skipping 15 matching lines...) Expand all Loading... | |
109 return false; | 223 return false; |
110 } | 224 } |
111 | 225 |
112 bool SSLErrorClassification::IsUserClockInTheFuture(base::Time time_now) { | 226 bool SSLErrorClassification::IsUserClockInTheFuture(base::Time time_now) { |
113 base::Time build_time = base::GetBuildTime(); | 227 base::Time build_time = base::GetBuildTime(); |
114 if (time_now > build_time + base::TimeDelta::FromDays(365)) | 228 if (time_now > build_time + base::TimeDelta::FromDays(365)) |
115 return true; | 229 return true; |
116 return false; | 230 return false; |
117 } | 231 } |
118 | 232 |
119 void SSLErrorClassification::RecordUMAStatistics(bool overridable) { | 233 bool SSLErrorClassification::IsHostNameKnownTLD(const std::string& host_name) { |
120 if (IsUserClockInThePast(base::Time::NowFromSystemTime())) | 234 size_t tld_length = |
121 RecordSSLInterstitialCause(overridable, CLOCK_PAST); | 235 net::registry_controlled_domains::GetRegistryLength( |
122 if (IsUserClockInTheFuture(base::Time::NowFromSystemTime())) | 236 host_name, |
123 RecordSSLInterstitialCause(overridable, CLOCK_FUTURE); | 237 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, |
238 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | |
239 if (tld_length == 0 || tld_length == std::string::npos) | |
240 return false; | |
241 return true; | |
124 } | 242 } |
243 | |
244 std::vector<std::vector<std::string>> SSLErrorClassification:: | |
245 GetTokenizedDNSNames(std::vector<std::string>& dns_names) const{ | |
246 std::vector<std::vector<std::string>> dns_name_tokens; | |
247 for (size_t i = 0; i < dns_names.size(); ++i) { | |
248 std::vector<std::string> dns_name_token_single; | |
249 if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos | |
250 || !(IsHostNameKnownTLD(dns_names[i]))) { | |
251 dns_name_token_single.push_back(std::string()); | |
252 } else { | |
253 base::SplitStringDontTrim(dns_names[i], | |
254 '.', | |
255 &dns_name_token_single); | |
256 } | |
257 dns_name_tokens.push_back(dns_name_token_single); | |
258 } | |
259 return dns_name_tokens; | |
260 } | |
261 | |
262 // We accept the inverse case for www for historical reasons. | |
263 bool SSLErrorClassification::IsWWWSubDomainMatch() const { | |
264 std::string host_name = request_url_.host(); | |
265 if (IsHostNameKnownTLD(host_name)) { | |
266 std::vector<std::string> dns_names; | |
267 cert_.GetDNSNames(&dns_names); | |
268 bool result = false; | |
269 // Need to account for all possible domains given in the SSL certificate. | |
270 for (size_t i = 0; i < dns_names.size(); ++i) { | |
271 if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos | |
272 || dns_names[i].length() == host_name.length() | |
273 || !(IsHostNameKnownTLD(dns_names[i]))) { | |
274 result = result || false; | |
275 } else if (dns_names[i].length() > host_name.length()) { | |
276 result = result || | |
277 net::StripWWW(base::ASCIIToUTF16(dns_names[i])) == | |
278 base::ASCIIToUTF16(host_name); | |
279 } else { | |
280 result = result || | |
281 net::StripWWW(base::ASCIIToUTF16(host_name)) == | |
282 base::ASCIIToUTF16(dns_names[i]); | |
283 } | |
284 } | |
285 return result; | |
286 } | |
287 return false; | |
288 } | |
289 | |
290 bool SSLErrorClassification::IsSubDomainMatch( | |
291 const std::vector<std::string>& host_name_tokens, | |
292 const std::vector<std::vector<std::string>>& dns_name_tokens) const { | |
293 bool result = false; | |
294 // Need to account for all the possible domains given in the SSL certificate. | |
295 for (size_t i = 0; i < dns_name_tokens.size(); ++i) { | |
296 if (dns_name_tokens[i].empty() || | |
297 dns_name_tokens[i].size() >= host_name_tokens.size()) { | |
298 result = result || false; | |
299 } else { | |
300 size_t domain_diff = FindSubDomainDifference(host_name_tokens, | |
301 dns_name_tokens[i]); | |
302 if (domain_diff == 1 && host_name_tokens[0] != "www") | |
303 result = result || true; | |
304 } | |
305 } | |
306 return result; | |
307 } | |
308 | |
309 // The inverse case should be treated carefully as this is most likely a MITM | |
310 // attack. We don't want foo.appspot.com to be able to MITM for appspot.com. | |
311 bool SSLErrorClassification::IsSubDomainInverseMatch( | |
312 const std::vector<std::string>& host_name_tokens, | |
313 const std::vector<std::vector<std::string>>& dns_name_tokens) const { | |
314 bool result = false; | |
315 // Need to account for all the possible domains given in the SSL certificate. | |
316 for (size_t i = 0; i < dns_name_tokens.size(); ++i) { | |
317 if (dns_name_tokens[i].empty() || | |
318 dns_name_tokens[i].size() <= host_name_tokens.size()) { | |
319 result = result || false; | |
320 } else { | |
321 size_t domain_diff = FindSubDomainDifference(dns_name_tokens[i], | |
322 host_name_tokens); | |
323 if (domain_diff == 1 && dns_name_tokens[i][0] != "www") | |
324 result = result || true; | |
325 } | |
326 } | |
327 return result; | |
328 } | |
329 | |
330 // This method is valid for wildcard certificates only. | |
331 bool SSLErrorClassification::IsSubDomainOutsideWildcard( | |
332 const std::vector<std::string>& host_name_tokens) const { | |
333 std::string host_name = request_url_.host(); | |
334 std::vector<std::string> dns_names; | |
335 cert_.GetDNSNames(&dns_names); | |
336 bool result = false; | |
337 | |
338 // This method requires that the host name be longer than the dns name on | |
339 // the certificate. | |
340 for (size_t i = 0; i < dns_names.size(); ++i) { | |
341 if (!(dns_names[i][0] == '*' && dns_names[i][1] == '.')) { | |
342 result = result || false; | |
343 } else { | |
344 if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos | |
345 || dns_names[i].length() >= host_name.length() | |
346 || !(IsHostNameKnownTLD(dns_names[i]))) { | |
347 result = result || false; | |
348 } else { | |
349 // Move past the '*.'. | |
350 std::string extracted_dns_name = dns_names[i].substr(2); | |
351 std::vector<std::string> extracted_dns_name_tokens; | |
352 base::SplitStringDontTrim(extracted_dns_name, | |
353 '.', | |
354 &extracted_dns_name_tokens); | |
355 size_t domain_diff = FindSubDomainDifference(host_name_tokens, | |
356 extracted_dns_name_tokens); | |
357 if (domain_diff == 2) | |
358 result = result || true; | |
359 } | |
360 } | |
361 } | |
362 return result; | |
363 } | |
364 | |
365 bool SSLErrorClassification::IsSelfSigned() const { | |
366 // Check whether the issuer and the subject are the same. | |
367 const net::CertPrincipal& subject = cert_.subject(); | |
368 const net::CertPrincipal& issuer = cert_.issuer(); | |
369 bool result = subject.common_name == issuer.common_name && | |
370 subject.locality_name == issuer.locality_name && | |
371 subject.state_or_province_name == issuer.state_or_province_name && | |
372 subject.country_name == issuer.country_name && | |
373 subject.street_addresses == issuer.street_addresses && | |
374 subject.organization_names == issuer.organization_names && | |
375 subject.organization_unit_names == issuer.organization_unit_names && | |
376 subject.domain_components == issuer.domain_components; | |
377 return result; | |
378 } | |
OLD | NEW |