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 "net/cert/cert_policy_enforcer.h" | 5 #include "net/cert/cert_policy_enforcer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/build_time.h" | 10 #include "base/build_time.h" |
(...skipping 25 matching lines...) Expand all Loading... | |
36 bool IsBuildTimely() { | 36 bool IsBuildTimely() { |
37 #if defined(DONT_EMBED_BUILD_METADATA) && !defined(OFFICIAL_BUILD) | 37 #if defined(DONT_EMBED_BUILD_METADATA) && !defined(OFFICIAL_BUILD) |
38 return true; | 38 return true; |
39 #else | 39 #else |
40 const base::Time build_time = base::GetBuildTime(); | 40 const base::Time build_time = base::GetBuildTime(); |
41 // We consider built-in information to be timely for 10 weeks. | 41 // We consider built-in information to be timely for 10 weeks. |
42 return (base::Time::Now() - build_time).InDays() < 70 /* 10 weeks */; | 42 return (base::Time::Now() - build_time).InDays() < 70 /* 10 weeks */; |
43 #endif | 43 #endif |
44 } | 44 } |
45 | 45 |
46 uint32_t ApproximateMonthDifference(const base::Time& start, | 46 struct MonthsDiff { |
47 const base::Time& end) { | 47 MonthsDiff() : num_months(0), last_month_is_full(false) {} |
48 size_t num_months; | |
49 bool last_month_is_full; | |
50 }; | |
davidben
2015/03/26 15:05:06
This struct should go away now.
Eran Messeri
2015/03/26 22:58:52
Oops, Done.
| |
51 | |
52 // Returns a rounded-down months difference of |start| and |end|, | |
53 // together with an indication of whether the last month was | |
54 // a full month, because the range starts specified in the policy | |
55 // are not consistent in terms of including the range start value. | |
56 void RoundedDownMonthDifference(const base::Time& start, | |
57 const base::Time& end, | |
58 size_t* rounded_months_difference, | |
59 bool* has_partial_month) { | |
60 DCHECK(rounded_months_difference); | |
61 DCHECK(has_partial_month); | |
48 base::Time::Exploded exploded_start; | 62 base::Time::Exploded exploded_start; |
49 base::Time::Exploded exploded_expiry; | 63 base::Time::Exploded exploded_expiry; |
50 start.UTCExplode(&exploded_start); | 64 start.UTCExplode(&exploded_start); |
51 end.UTCExplode(&exploded_expiry); | 65 end.UTCExplode(&exploded_expiry); |
66 if (end < start) { | |
67 *rounded_months_difference = 0; | |
68 *has_partial_month = false; | |
69 } | |
70 | |
71 *has_partial_month = true; | |
52 uint32_t month_diff = (exploded_expiry.year - exploded_start.year) * 12 + | 72 uint32_t month_diff = (exploded_expiry.year - exploded_start.year) * 12 + |
53 (exploded_expiry.month - exploded_start.month); | 73 (exploded_expiry.month - exploded_start.month); |
74 if (exploded_expiry.day_of_month < exploded_start.day_of_month) | |
75 --month_diff; | |
76 else if (exploded_expiry.day_of_month == exploded_start.day_of_month) | |
77 *has_partial_month = false; | |
54 | 78 |
55 // Add any remainder as a full month. | 79 *rounded_months_difference = month_diff; |
56 if (exploded_expiry.day_of_month > exploded_start.day_of_month) | |
57 ++month_diff; | |
58 | |
59 return month_diff; | |
60 } | 80 } |
61 | 81 |
62 bool HasRequiredNumberOfSCTs(const X509Certificate& cert, | 82 bool HasRequiredNumberOfSCTs(const X509Certificate& cert, |
63 const ct::CTVerifyResult& ct_result) { | 83 const ct::CTVerifyResult& ct_result) { |
64 // TODO(eranm): Count the number of *independent* SCTs once the information | 84 // TODO(eranm): Count the number of *independent* SCTs once the information |
65 // about log operators is available, crbug.com/425174 | 85 // about log operators is available, crbug.com/425174 |
66 size_t num_valid_scts = ct_result.verified_scts.size(); | 86 size_t num_valid_scts = ct_result.verified_scts.size(); |
67 size_t num_embedded_scts = | 87 size_t num_embedded_scts = |
68 std::count_if(ct_result.verified_scts.begin(), | 88 std::count_if(ct_result.verified_scts.begin(), |
69 ct_result.verified_scts.end(), IsEmbeddedSCT); | 89 ct_result.verified_scts.end(), IsEmbeddedSCT); |
70 | 90 |
71 size_t num_non_embedded_scts = num_valid_scts - num_embedded_scts; | 91 size_t num_non_embedded_scts = num_valid_scts - num_embedded_scts; |
72 // If at least two valid SCTs were delivered by means other than embedding | 92 // If at least two valid SCTs were delivered by means other than embedding |
73 // (i.e. in a TLS extension or OCSP), then the certificate conforms to bullet | 93 // (i.e. in a TLS extension or OCSP), then the certificate conforms to bullet |
74 // number 3 of the "Qualifying Certificate" section of the CT/EV policy. | 94 // number 3 of the "Qualifying Certificate" section of the CT/EV policy. |
75 if (num_non_embedded_scts >= 2) | 95 if (num_non_embedded_scts >= 2) |
76 return true; | 96 return true; |
77 | 97 |
78 if (cert.valid_start().is_null() || cert.valid_expiry().is_null() || | 98 if (cert.valid_start().is_null() || cert.valid_expiry().is_null() || |
79 cert.valid_start().is_max() || cert.valid_expiry().is_max()) { | 99 cert.valid_start().is_max() || cert.valid_expiry().is_max()) { |
80 // Will not be able to calculate the certificate's validity period. | 100 // Will not be able to calculate the certificate's validity period. |
81 return false; | 101 return false; |
82 } | 102 } |
83 | 103 |
84 uint32_t expiry_in_months_approx = | 104 size_t lifetime; |
85 ApproximateMonthDifference(cert.valid_start(), cert.valid_expiry()); | 105 bool has_partial_month; |
106 RoundedDownMonthDifference(cert.valid_start(), cert.valid_expiry(), &lifetime, | |
107 &has_partial_month); | |
86 | 108 |
87 // For embedded SCTs, if the certificate has the number of SCTs specified in | 109 // For embedded SCTs, if the certificate has the number of SCTs specified in |
88 // table 1 of the "Qualifying Certificate" section of the CT/EV policy, then | 110 // table 1 of the "Qualifying Certificate" section of the CT/EV policy, then |
89 // it qualifies. | 111 // it qualifies. |
90 size_t num_required_embedded_scts; | 112 size_t num_required_embedded_scts; |
91 if (expiry_in_months_approx > 39) { | 113 if ((lifetime > 39) || (lifetime == 39 && has_partial_month)) { |
davidben
2015/03/26 15:05:06
Nit: parens around (lifetime > 39) are unnecessary
Eran Messeri
2015/03/26 22:58:52
Done.
| |
92 num_required_embedded_scts = 5; | 114 num_required_embedded_scts = 5; |
93 } else if (expiry_in_months_approx > 27) { | 115 } else if ((lifetime > 27) || (lifetime == 27 && has_partial_month)) { |
94 num_required_embedded_scts = 4; | 116 num_required_embedded_scts = 4; |
95 } else if (expiry_in_months_approx >= 15) { | 117 } else if (lifetime >= 15) { |
96 num_required_embedded_scts = 3; | 118 num_required_embedded_scts = 3; |
97 } else { | 119 } else { |
98 num_required_embedded_scts = 2; | 120 num_required_embedded_scts = 2; |
99 } | 121 } |
100 | 122 |
101 return num_embedded_scts >= num_required_embedded_scts; | 123 return num_embedded_scts >= num_required_embedded_scts; |
102 } | 124 } |
103 | 125 |
104 enum CTComplianceStatus { | 126 enum CTComplianceStatus { |
105 CT_NOT_COMPLIANT = 0, | 127 CT_NOT_COMPLIANT = 0, |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
266 | 288 |
267 LogCTComplianceStatusToUMA(details.status, ev_whitelist); | 289 LogCTComplianceStatusToUMA(details.status, ev_whitelist); |
268 | 290 |
269 if (details.status == CT_IN_WHITELIST || details.status == CT_ENOUGH_SCTS) | 291 if (details.status == CT_IN_WHITELIST || details.status == CT_ENOUGH_SCTS) |
270 return true; | 292 return true; |
271 | 293 |
272 return false; | 294 return false; |
273 } | 295 } |
274 | 296 |
275 } // namespace net | 297 } // namespace net |
OLD | NEW |