| 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 // Returns a rounded-down months difference of |start| and |end|, |
| 47 const base::Time& end) { | 47 // together with an indication of whether the last month was |
| 48 // a full month, because the range starts specified in the policy |
| 49 // are not consistent in terms of including the range start value. |
| 50 void RoundedDownMonthDifference(const base::Time& start, |
| 51 const base::Time& end, |
| 52 size_t* rounded_months_difference, |
| 53 bool* has_partial_month) { |
| 54 DCHECK(rounded_months_difference); |
| 55 DCHECK(has_partial_month); |
| 48 base::Time::Exploded exploded_start; | 56 base::Time::Exploded exploded_start; |
| 49 base::Time::Exploded exploded_expiry; | 57 base::Time::Exploded exploded_expiry; |
| 50 start.UTCExplode(&exploded_start); | 58 start.UTCExplode(&exploded_start); |
| 51 end.UTCExplode(&exploded_expiry); | 59 end.UTCExplode(&exploded_expiry); |
| 60 if (end < start) { |
| 61 *rounded_months_difference = 0; |
| 62 *has_partial_month = false; |
| 63 } |
| 64 |
| 65 *has_partial_month = true; |
| 52 uint32_t month_diff = (exploded_expiry.year - exploded_start.year) * 12 + | 66 uint32_t month_diff = (exploded_expiry.year - exploded_start.year) * 12 + |
| 53 (exploded_expiry.month - exploded_start.month); | 67 (exploded_expiry.month - exploded_start.month); |
| 68 if (exploded_expiry.day_of_month < exploded_start.day_of_month) |
| 69 --month_diff; |
| 70 else if (exploded_expiry.day_of_month == exploded_start.day_of_month) |
| 71 *has_partial_month = false; |
| 54 | 72 |
| 55 // Add any remainder as a full month. | 73 *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 } | 74 } |
| 61 | 75 |
| 62 bool HasRequiredNumberOfSCTs(const X509Certificate& cert, | 76 bool HasRequiredNumberOfSCTs(const X509Certificate& cert, |
| 63 const ct::CTVerifyResult& ct_result) { | 77 const ct::CTVerifyResult& ct_result) { |
| 64 // TODO(eranm): Count the number of *independent* SCTs once the information | 78 // TODO(eranm): Count the number of *independent* SCTs once the information |
| 65 // about log operators is available, crbug.com/425174 | 79 // about log operators is available, crbug.com/425174 |
| 66 size_t num_valid_scts = ct_result.verified_scts.size(); | 80 size_t num_valid_scts = ct_result.verified_scts.size(); |
| 67 size_t num_embedded_scts = | 81 size_t num_embedded_scts = |
| 68 std::count_if(ct_result.verified_scts.begin(), | 82 std::count_if(ct_result.verified_scts.begin(), |
| 69 ct_result.verified_scts.end(), IsEmbeddedSCT); | 83 ct_result.verified_scts.end(), IsEmbeddedSCT); |
| 70 | 84 |
| 71 size_t num_non_embedded_scts = num_valid_scts - num_embedded_scts; | 85 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 | 86 // 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 | 87 // (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. | 88 // number 3 of the "Qualifying Certificate" section of the CT/EV policy. |
| 75 if (num_non_embedded_scts >= 2) | 89 if (num_non_embedded_scts >= 2) |
| 76 return true; | 90 return true; |
| 77 | 91 |
| 78 if (cert.valid_start().is_null() || cert.valid_expiry().is_null() || | 92 if (cert.valid_start().is_null() || cert.valid_expiry().is_null() || |
| 79 cert.valid_start().is_max() || cert.valid_expiry().is_max()) { | 93 cert.valid_start().is_max() || cert.valid_expiry().is_max()) { |
| 80 // Will not be able to calculate the certificate's validity period. | 94 // Will not be able to calculate the certificate's validity period. |
| 81 return false; | 95 return false; |
| 82 } | 96 } |
| 83 | 97 |
| 84 uint32_t expiry_in_months_approx = | 98 size_t lifetime; |
| 85 ApproximateMonthDifference(cert.valid_start(), cert.valid_expiry()); | 99 bool has_partial_month; |
| 100 RoundedDownMonthDifference(cert.valid_start(), cert.valid_expiry(), &lifetime, |
| 101 &has_partial_month); |
| 86 | 102 |
| 87 // For embedded SCTs, if the certificate has the number of SCTs specified in | 103 // 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 | 104 // table 1 of the "Qualifying Certificate" section of the CT/EV policy, then |
| 89 // it qualifies. | 105 // it qualifies. |
| 90 size_t num_required_embedded_scts; | 106 size_t num_required_embedded_scts; |
| 91 if (expiry_in_months_approx > 39) { | 107 if (lifetime > 39 || (lifetime == 39 && has_partial_month)) { |
| 92 num_required_embedded_scts = 5; | 108 num_required_embedded_scts = 5; |
| 93 } else if (expiry_in_months_approx > 27) { | 109 } else if (lifetime > 27 || (lifetime == 27 && has_partial_month)) { |
| 94 num_required_embedded_scts = 4; | 110 num_required_embedded_scts = 4; |
| 95 } else if (expiry_in_months_approx >= 15) { | 111 } else if (lifetime >= 15) { |
| 96 num_required_embedded_scts = 3; | 112 num_required_embedded_scts = 3; |
| 97 } else { | 113 } else { |
| 98 num_required_embedded_scts = 2; | 114 num_required_embedded_scts = 2; |
| 99 } | 115 } |
| 100 | 116 |
| 101 return num_embedded_scts >= num_required_embedded_scts; | 117 return num_embedded_scts >= num_required_embedded_scts; |
| 102 } | 118 } |
| 103 | 119 |
| 104 enum CTComplianceStatus { | 120 enum CTComplianceStatus { |
| 105 CT_NOT_COMPLIANT = 0, | 121 CT_NOT_COMPLIANT = 0, |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 | 282 |
| 267 LogCTComplianceStatusToUMA(details.status, ev_whitelist); | 283 LogCTComplianceStatusToUMA(details.status, ev_whitelist); |
| 268 | 284 |
| 269 if (details.status == CT_IN_WHITELIST || details.status == CT_ENOUGH_SCTS) | 285 if (details.status == CT_IN_WHITELIST || details.status == CT_ENOUGH_SCTS) |
| 270 return true; | 286 return true; |
| 271 | 287 |
| 272 return false; | 288 return false; |
| 273 } | 289 } |
| 274 | 290 |
| 275 } // namespace net | 291 } // namespace net |
| OLD | NEW |