Ryan Sleevi | e5574e0 | 2018-05-15 04:37:23 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef COMPONENTS_CERTIFICATE_TRANSPARENCY_CHROME_REQUIRE_CT_DELEGATE_H_ |
| 6 | #define COMPONENTS_CERTIFICATE_TRANSPARENCY_CHROME_REQUIRE_CT_DELEGATE_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/macros.h" |
| 13 | #include "base/memory/ref_counted.h" |
| 14 | #include "components/url_matcher/url_matcher.h" |
| 15 | #include "net/base/hash_value.h" |
| 16 | #include "net/http/transport_security_state.h" |
| 17 | |
| 18 | namespace net { |
| 19 | class X509Certificate; |
| 20 | } // namespace net |
| 21 | |
| 22 | namespace certificate_transparency { |
| 23 | |
| 24 | // ChromeRequireCTDelegate implements the policies used by Chrome to determine |
| 25 | // when to require Certificate Transparency for a host or certificate. Combined |
| 26 | // with ChromeCTPolicyEnforcer, these two classes implement the |
| 27 | // "Certificate Transparency in Chrome" policy from |
| 28 | // https://goo.gl/chrome/ct-policy - PolicyEnforcer imposing the policies on |
| 29 | // the SCTs to determine whether or not a certificate complies, and |
| 30 | // RequireCTDelegate to determine whether or not compliance is required for the |
| 31 | // connection to succeed. |
| 32 | // |
| 33 | // To support Enterprise configuration, additional requirements or exceptions |
| 34 | // can be provided via |UpdateCTPolicies()|, which uses the configuration |
| 35 | // syntax documented in pref_names.h for each of the options. |
| 36 | class ChromeRequireCTDelegate |
| 37 | : public net::TransportSecurityState::RequireCTDelegate { |
| 38 | public: |
| 39 | explicit ChromeRequireCTDelegate(); |
| 40 | ~ChromeRequireCTDelegate() override; |
| 41 | |
| 42 | // RequireCTDelegate implementation |
| 43 | CTRequirementLevel IsCTRequiredForHost( |
| 44 | const std::string& hostname, |
| 45 | const net::X509Certificate* chain, |
| 46 | const net::HashValueVector& spki_hashes) override; |
| 47 | |
| 48 | // Updates the CTDelegate to require CT for |required_hosts|, and exclude |
| 49 | // |excluded_hosts| from CT policies. In addtion, this method updates |
| 50 | // |excluded_spkis| and |excluded_legacy_spkis| intended for use within an |
| 51 | // Enterprise (see https://crbug.com/824184). |
| 52 | void UpdateCTPolicies(const std::vector<std::string>& required_hosts, |
| 53 | const std::vector<std::string>& excluded_hosts, |
| 54 | const std::vector<std::string>& excluded_spkis, |
| 55 | const std::vector<std::string>& excluded_legacy_spkis); |
| 56 | |
| 57 | private: |
| 58 | struct Filter { |
| 59 | bool ct_required = false; |
| 60 | bool match_subdomains = false; |
| 61 | size_t host_length = 0; |
| 62 | }; |
| 63 | |
| 64 | // Returns true if a policy for |hostname| is found, setting |
| 65 | // |*ct_required| to indicate whether or not Certificate Transparency is |
| 66 | // required for the host. |
| 67 | bool MatchHostname(const std::string& hostname, bool* ct_required) const; |
| 68 | |
| 69 | // Returns true if a policy for |chain|, which contains the SPKI hashes |
| 70 | // |hashes|, is found, setting |*ct_required| to indicate whether or not |
| 71 | // Certificate Transparency is required for the certificate. |
| 72 | bool MatchSPKI(const net::X509Certificate* chain, |
| 73 | const net::HashValueVector& hashes, |
| 74 | bool* ct_required) const; |
| 75 | |
| 76 | // Parses the filters from |host_patterns|, adding them as filters to |
| 77 | // |filters_| (with |ct_required| indicating whether or not CT is required |
| 78 | // for that host), and updating |*conditions| with the corresponding |
| 79 | // URLMatcher::Conditions to match the host. |
| 80 | void AddFilters(bool ct_required, |
| 81 | const std::vector<std::string>& host_patterns, |
| 82 | url_matcher::URLMatcherConditionSet::Vector* conditions); |
| 83 | |
| 84 | // Parses the SPKIs from |spki_list|, setting |*hashes| to the sorted set of |
| 85 | // all valid SPKIs. |
| 86 | void ParseSpkiHashes(const std::vector<std::string> spki_list, |
| 87 | net::HashValueVector* hashes) const; |
| 88 | |
| 89 | // Returns true if |lhs| has greater precedence than |rhs|. Filters of |
| 90 | // higher precedence are consulted first when determining if a given filter |
| 91 | // matches. |
| 92 | bool FilterTakesPrecedence(const Filter& lhs, const Filter& rhs) const; |
| 93 | |
| 94 | std::unique_ptr<url_matcher::URLMatcher> url_matcher_; |
| 95 | url_matcher::URLMatcherConditionSet::ID next_id_; |
| 96 | std::map<url_matcher::URLMatcherConditionSet::ID, Filter> filters_; |
| 97 | |
| 98 | // Both SPKI lists are sorted. |
| 99 | net::HashValueVector spkis_; |
| 100 | net::HashValueVector legacy_spkis_; |
| 101 | |
| 102 | DISALLOW_COPY_AND_ASSIGN(ChromeRequireCTDelegate); |
| 103 | }; |
| 104 | |
| 105 | } // namespace certificate_transparency |
| 106 | |
| 107 | #endif // COMPONENTS_CERTIFICATE_TRANSPARENCY_CHROME_REQUIRE_CT_DELEGATE_H_ |