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 std::vector<std::string> 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<std::vector<std::string>> dns_name_tokens = | |
148 GetTokenizedDNSNames(dns_names); | |
149 if (IsSubDomainMatch(host_name_tokens, dns_name_tokens)) | |
150 severity_name_score += kServerWeight * kSubDomainWeight; | |
151 // Inverse case is more likely to be a MITM attack. | |
152 if (IsSubDomainInverseMatch(host_name_tokens, dns_name_tokens)) | |
153 severity_name_score += kServerWeight * kSubDomainInverseWeight; | |
154 } | |
155 return severity_name_score; | |
156 } | |
157 | |
158 void SSLErrorClassification::RecordUMAStatistics(bool overridable, | |
159 int cert_error) { | |
160 SSLErrorInfo::ErrorType type = | |
161 SSLErrorInfo::NetErrorToErrorType(cert_error); | |
162 | |
163 if (type == SSLErrorInfo::CERT_DATE_INVALID) { | |
164 if (IsUserClockInThePast(base::Time::NowFromSystemTime())) | |
165 RecordSSLInterstitialCause(overridable, CLOCK_PAST); | |
166 if (IsUserClockInTheFuture(base::Time::NowFromSystemTime())) | |
167 RecordSSLInterstitialCause(overridable, CLOCK_FUTURE); | |
168 } | |
169 | |
170 if (type == SSLErrorInfo::CERT_COMMON_NAME_INVALID) { | |
171 std::string host_name = request_url_.host(); | |
172 if (IsHostNameKnownTLD(host_name)) { | |
173 std::vector<std::string> host_name_tokens; | |
174 base::SplitStringDontTrim(host_name, | |
175 '.', | |
176 &host_name_tokens); | |
177 if (IsWWWSubDomainMatch()) | |
178 RecordSSLInterstitialCause(overridable, WWW_SUBDOMAIN_MATCH); | |
179 if (IsSubDomainOutsideWildcard(host_name_tokens)) | |
180 RecordSSLInterstitialCause(overridable, SUBDOMAIN_OUTSIDE_WILDCARD); | |
181 | |
182 std::vector<std::string> dns_names; | |
183 cert_.GetDNSNames(&dns_names); | |
184 std::vector<std::vector<std::string>> dns_name_tokens = | |
185 GetTokenizedDNSNames(dns_names); | |
186 if (IsSubDomainMatch(host_name_tokens, dns_name_tokens)) | |
187 RecordSSLInterstitialCause(overridable, SUBDOMAIN_MATCH); | |
188 if (IsSubDomainInverseMatch(host_name_tokens, dns_name_tokens)) | |
189 RecordSSLInterstitialCause(overridable, SUBDOMAIN_INVERSE_MATCH); | |
190 } else { | |
191 RecordSSLInterstitialCause(overridable, HOST_NAME_NOT_KNOWN_TLD); | |
192 } | |
193 } | |
194 | |
195 if (type == SSLErrorInfo::CERT_AUTHORITY_INVALID) { | |
196 if (IsSelfSigned()) | |
197 RecordSSLInterstitialCause(overridable, SELF_SIGNED); | |
198 } | |
199 } | |
200 | |
84 base::TimeDelta SSLErrorClassification::TimePassedSinceExpiry() const { | 201 base::TimeDelta SSLErrorClassification::TimePassedSinceExpiry() const { |
85 base::TimeDelta delta = current_time_ - cert_.valid_expiry(); | 202 base::TimeDelta delta = current_time_ - cert_.valid_expiry(); |
86 return delta; | 203 return delta; |
87 } | 204 } |
88 | 205 |
89 float SSLErrorClassification::CalculateScoreTimePassedSinceExpiry() const { | 206 float SSLErrorClassification::CalculateScoreTimePassedSinceExpiry() const { |
90 base::TimeDelta delta = TimePassedSinceExpiry(); | 207 base::TimeDelta delta = TimePassedSinceExpiry(); |
91 int64 time_passed = delta.InDays(); | 208 int64 time_passed = delta.InDays(); |
92 const int64 kHighThreshold = 7; | 209 const int64 kHighThreshold = 7; |
93 const int64 kLowThreshold = 4; | 210 const int64 kLowThreshold = 4; |
(...skipping 15 matching lines...) Expand all Loading... | |
109 return false; | 226 return false; |
110 } | 227 } |
111 | 228 |
112 bool SSLErrorClassification::IsUserClockInTheFuture(base::Time time_now) { | 229 bool SSLErrorClassification::IsUserClockInTheFuture(base::Time time_now) { |
113 base::Time build_time = base::GetBuildTime(); | 230 base::Time build_time = base::GetBuildTime(); |
114 if (time_now > build_time + base::TimeDelta::FromDays(365)) | 231 if (time_now > build_time + base::TimeDelta::FromDays(365)) |
115 return true; | 232 return true; |
116 return false; | 233 return false; |
117 } | 234 } |
118 | 235 |
119 void SSLErrorClassification::RecordUMAStatistics(bool overridable) { | 236 bool SSLErrorClassification::IsHostNameKnownTLD(const std::string& host_name) { |
120 if (IsUserClockInThePast(base::Time::NowFromSystemTime())) | 237 size_t tld_length = |
121 RecordSSLInterstitialCause(overridable, CLOCK_PAST); | 238 net::registry_controlled_domains::GetRegistryLength( |
122 if (IsUserClockInTheFuture(base::Time::NowFromSystemTime())) | 239 host_name, |
123 RecordSSLInterstitialCause(overridable, CLOCK_FUTURE); | 240 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, |
241 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | |
242 if (tld_length == 0 || tld_length == std::string::npos) | |
243 return false; | |
244 return true; | |
124 } | 245 } |
246 | |
247 std::vector<std::vector<std::string>> SSLErrorClassification:: | |
248 GetTokenizedDNSNames(std::vector<std::string>& dns_names) const{ | |
249 std::vector<std::vector<std::string>> dns_name_tokens; | |
250 for (size_t i = 0; i < dns_names.size(); ++i) { | |
251 std::vector<std::string> dns_name_token_single; | |
252 if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos | |
253 || !(IsHostNameKnownTLD(dns_names[i]))) { | |
254 dns_name_token_single.push_back(std::string()); | |
255 } else { | |
256 base::SplitStringDontTrim(dns_names[i], | |
257 '.', | |
258 &dns_name_token_single); | |
259 } | |
260 dns_name_tokens.push_back(dns_name_token_single); | |
261 } | |
262 return dns_name_tokens; | |
263 } | |
264 | |
265 // We accept the inverse case for www for historical reasons. | |
266 bool SSLErrorClassification::IsWWWSubDomainMatch() const { | |
267 std::string host_name = request_url_.host(); | |
268 if (IsHostNameKnownTLD(host_name)) { | |
269 std::vector<std::string> dns_names; | |
270 cert_.GetDNSNames(&dns_names); | |
271 bool result = false; | |
272 // Need to account for all possible domains given in the SSL certificate. | |
273 for (size_t i = 0; i < dns_names.size(); ++i) { | |
274 if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos | |
275 || dns_names[i].length() == host_name.length() | |
276 || !(IsHostNameKnownTLD(dns_names[i]))) { | |
277 result = result || false; | |
278 } else if (dns_names[i].length() > host_name.length()) { | |
279 result = result || | |
280 net::StripWWW(base::ASCIIToUTF16(dns_names[i])) == | |
281 base::ASCIIToUTF16(host_name); | |
282 } else { | |
283 result = result || | |
284 net::StripWWW(base::ASCIIToUTF16(host_name)) == | |
285 base::ASCIIToUTF16(dns_names[i]); | |
286 } | |
287 } | |
288 return result; | |
289 } | |
290 return false; | |
291 } | |
292 | |
293 bool SSLErrorClassification::IsSubDomainMatch( | |
294 const std::vector<std::string>& host_name_tokens, | |
295 const std::vector<std::vector<std::string>>& dns_name_tokens) const { | |
296 bool result = false; | |
297 // Need to account for all the possible domains given in the SSL certificate. | |
298 for (size_t i = 0; i < dns_name_tokens.size(); ++i) { | |
299 if (dns_name_tokens[i].empty() || | |
300 dns_name_tokens[i].size() >= host_name_tokens.size()) { | |
301 result = result || false; | |
302 } else { | |
303 size_t domain_diff = FindSubDomainDifference(host_name_tokens, | |
304 dns_name_tokens[i]); | |
305 if (domain_diff == 1 && host_name_tokens[0] != "www") | |
306 result = result || true; | |
307 } | |
308 } | |
309 return result; | |
310 } | |
311 | |
312 // The inverse case should be treated carefully as this is most likely a MITM | |
313 // attack. We don't want foo.appspot.com to be able to MITM for appspot.com. | |
314 bool SSLErrorClassification::IsSubDomainInverseMatch( | |
315 const std::vector<std::string>& host_name_tokens, | |
316 const std::vector<std::vector<std::string>>& dns_name_tokens) const { | |
317 bool result = false; | |
318 // Need to account for all the possible domains given in the SSL certificate. | |
319 for (size_t i = 0; i < dns_name_tokens.size(); ++i) { | |
320 if (dns_name_tokens[i].empty() || | |
321 dns_name_tokens[i].size() <= host_name_tokens.size()) { | |
322 result = result || false; | |
323 } else { | |
324 size_t domain_diff = FindSubDomainDifference(dns_name_tokens[i], | |
325 host_name_tokens); | |
326 if (domain_diff == 1 && dns_name_tokens[i][0] != "www") | |
327 result = result || true; | |
328 } | |
329 } | |
330 return result; | |
331 } | |
332 | |
333 // This method is valid for wildcard certificates only. | |
334 bool SSLErrorClassification::IsSubDomainOutsideWildcard( | |
palmer
2014/07/23 01:22:06
It just seems like there has got to already be a f
palmer
2014/07/23 01:53:22
Yeah, see if you can call X509Certificate::VerifyN
radhikabhar
2014/07/24 18:05:13
I actually did check both of those functions befor
| |
335 const std::vector<std::string>& host_name_tokens) const { | |
336 std::string host_name = request_url_.host(); | |
337 std::vector<std::string> dns_names; | |
338 cert_.GetDNSNames(&dns_names); | |
339 bool result = false; | |
340 | |
341 // This method requires that the host name be longer than the dns name on | |
342 // the certificate. | |
343 for (size_t i = 0; i < dns_names.size(); ++i) { | |
344 if (!(dns_names[i][0] == '*' && dns_names[i][1] == '.')) { | |
345 result = result || false; | |
346 } else { | |
347 if (dns_names[i].empty() || dns_names[i].find('\0') != std::string::npos | |
348 || dns_names[i].length() >= host_name.length() | |
349 || !(IsHostNameKnownTLD(dns_names[i]))) { | |
350 result = result || false; | |
351 } else { | |
352 // Move past the '*.'. | |
353 std::string extracted_dns_name = dns_names[i].substr(2); | |
354 std::vector<std::string> extracted_dns_name_tokens; | |
355 base::SplitStringDontTrim(extracted_dns_name, | |
356 '.', | |
357 &extracted_dns_name_tokens); | |
358 size_t domain_diff = FindSubDomainDifference(host_name_tokens, | |
359 extracted_dns_name_tokens); | |
360 if (domain_diff == 2) | |
361 result = result || true; | |
362 } | |
363 } | |
364 } | |
365 return result; | |
366 } | |
367 | |
368 bool SSLErrorClassification::IsSelfSigned() const { | |
palmer
2014/07/23 01:53:22
Comparing CertPrincipals is not a real way to chec
radhikabhar
2014/07/24 18:05:13
I think so the correct way to check whether a cert
| |
369 // Check whether the issuer and the subject are the same. | |
370 const net::CertPrincipal& subject = cert_.subject(); | |
371 const net::CertPrincipal& issuer = cert_.issuer(); | |
372 bool result = subject.common_name == issuer.common_name && | |
373 subject.locality_name == issuer.locality_name && | |
374 subject.state_or_province_name == issuer.state_or_province_name && | |
375 subject.country_name == issuer.country_name && | |
376 subject.street_addresses == issuer.street_addresses && | |
377 subject.organization_names == issuer.organization_names && | |
378 subject.organization_unit_names == issuer.organization_unit_names && | |
379 subject.domain_components == issuer.domain_components; | |
380 return result; | |
381 } | |
OLD | NEW |