| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef NET_HTTP_TRANSPORT_SECURITY_STATE_H_ | 5 #ifndef NET_HTTP_TRANSPORT_SECURITY_STATE_H_ |
| 6 #define NET_HTTP_TRANSPORT_SECURITY_STATE_H_ | 6 #define NET_HTTP_TRANSPORT_SECURITY_STATE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 public: | 54 public: |
| 55 enum UpgradeMode { | 55 enum UpgradeMode { |
| 56 // These numbers must match those in hsts_view.js, function modeToString. | 56 // These numbers must match those in hsts_view.js, function modeToString. |
| 57 MODE_FORCE_HTTPS = 0, | 57 MODE_FORCE_HTTPS = 0, |
| 58 MODE_DEFAULT = 1, | 58 MODE_DEFAULT = 1, |
| 59 }; | 59 }; |
| 60 | 60 |
| 61 DomainState(); | 61 DomainState(); |
| 62 ~DomainState(); | 62 ~DomainState(); |
| 63 | 63 |
| 64 struct STSState { |
| 65 // The absolute time (UTC) when the |upgrade_mode| (and other state) was |
| 66 // observed. |
| 67 base::Time last_observed; |
| 68 |
| 69 // The absolute time (UTC) when the |upgrade_mode|, if set to |
| 70 // UPGRADE_ALWAYS, downgrades to UPGRADE_NEVER. |
| 71 base::Time expiry; |
| 72 |
| 73 UpgradeMode upgrade_mode; |
| 74 |
| 75 // Are subdomains subject to this policy state? |
| 76 bool include_subdomains; |
| 77 }; |
| 78 |
| 79 struct PKPState { |
| 80 // The absolute time (UTC) when the |spki_hashes| (and other state) were |
| 81 // observed. |
| 82 base::Time last_observed; |
| 83 |
| 84 // The absolute time (UTC) when the |spki_hashes| expire. |
| 85 base::Time expiry; |
| 86 |
| 87 // Optional; hashes of pinned SubjectPublicKeyInfos. |
| 88 HashValueVector spki_hashes; |
| 89 |
| 90 // Optional; hashes of static known-bad SubjectPublicKeyInfos which MUST |
| 91 // NOT intersect with the set of SPKIs in the TLS server's certificate |
| 92 // chain. |
| 93 HashValueVector bad_spki_hashes; |
| 94 |
| 95 // Are subdomains subject to this policy state? |
| 96 bool include_subdomains; |
| 97 }; |
| 98 |
| 64 // Takes a set of SubjectPublicKeyInfo |hashes| and returns true if: | 99 // Takes a set of SubjectPublicKeyInfo |hashes| and returns true if: |
| 65 // 1) |bad_static_spki_hashes| does not intersect |hashes|; AND | 100 // 1) |bad_static_spki_hashes| does not intersect |hashes|; AND |
| 66 // 2) Both |static_spki_hashes| and |dynamic_spki_hashes| are empty | 101 // 2) Both |static_spki_hashes| and |dynamic_spki_hashes| are empty |
| 67 // or at least one of them intersects |hashes|. | 102 // or at least one of them intersects |hashes|. |
| 68 // | 103 // |
| 69 // |{dynamic,static}_spki_hashes| contain trustworthy public key hashes, | 104 // |{dynamic,static}_spki_hashes| contain trustworthy public key hashes, |
| 70 // any one of which is sufficient to validate the certificate chain in | 105 // any one of which is sufficient to validate the certificate chain in |
| 71 // question. The public keys could be of a root CA, intermediate CA, or | 106 // question. The public keys could be of a root CA, intermediate CA, or |
| 72 // leaf certificate, depending on the security vs. disaster recovery | 107 // leaf certificate, depending on the security vs. disaster recovery |
| 73 // tradeoff selected. (Pinning only to leaf certifiates increases | 108 // tradeoff selected. (Pinning only to leaf certifiates increases |
| 74 // security because you no longer trust any CAs, but it hampers disaster | 109 // security because you no longer trust any CAs, but it hampers disaster |
| 75 // recovery because you can't just get a new certificate signed by the | 110 // recovery because you can't just get a new certificate signed by the |
| 76 // CA.) | 111 // CA.) |
| 77 // | 112 // |
| 78 // |bad_static_spki_hashes| contains public keys that we don't want to | 113 // |bad_static_spki_hashes| contains public keys that we don't want to |
| 79 // trust. | 114 // trust. |
| 80 bool CheckPublicKeyPins(const HashValueVector& hashes, | 115 bool CheckPublicKeyPins(const HashValueVector& hashes, |
| 81 std::string* failure_log) const; | 116 std::string* failure_log) const; |
| 82 | 117 |
| 83 // Returns true if any of the HashValueVectors |static_spki_hashes|, | 118 // Returns true if any of the HashValueVectors |static_spki_hashes|, |
| 84 // |bad_static_spki_hashes|, or |dynamic_spki_hashes| contains any | 119 // |bad_static_spki_hashes|, or |dynamic_spki_hashes| contains any |
| 85 // items. | 120 // items. |
| 86 bool HasPublicKeyPins() const; | 121 bool HasPublicKeyPins() const; |
| 87 | 122 |
| 88 // ShouldUpgradeToSSL returns true iff, given the |mode| of this | 123 // ShouldUpgradeToSSL returns true iff HTTP requests should be internally |
| 89 // DomainState, HTTP requests should be internally redirected to HTTPS | 124 // redirected to HTTPS (also if WS should be upgraded to WSS). |
| 90 // (also if the "ws" WebSocket request should be upgraded to "wss") | |
| 91 bool ShouldUpgradeToSSL() const; | 125 bool ShouldUpgradeToSSL() const; |
| 92 | 126 |
| 93 // ShouldSSLErrorsBeFatal returns true iff HTTPS errors should cause | 127 // ShouldSSLErrorsBeFatal returns true iff HTTPS errors should cause |
| 94 // hard-fail behavior (e.g. if HSTS is set for the domain) | 128 // hard-fail behavior (e.g. if HSTS is set for the domain). |
| 95 bool ShouldSSLErrorsBeFatal() const; | 129 bool ShouldSSLErrorsBeFatal() const; |
| 96 | 130 |
| 97 UpgradeMode upgrade_mode; | 131 STSState sts; |
| 98 | 132 PKPState pkp; |
| 99 // The absolute time (UTC) when the |upgrade_mode| was observed. | |
| 100 // | |
| 101 // TODO(palmer): Perhaps static entries should have an "observed" time. | |
| 102 base::Time sts_observed; | |
| 103 | |
| 104 // The absolute time (UTC) when the |dynamic_spki_hashes| (and other | |
| 105 // |dynamic_*| state) were observed. | |
| 106 // | |
| 107 // TODO(palmer): Perhaps static entries should have an "observed" time. | |
| 108 base::Time pkp_observed; | |
| 109 | |
| 110 // The absolute time (UTC) when the |upgrade_mode|, if set to | |
| 111 // UPGRADE_ALWAYS, downgrades to UPGRADE_NEVER. | |
| 112 base::Time upgrade_expiry; | |
| 113 | |
| 114 // Are subdomains subject to this DomainState, for the purposes of | |
| 115 // upgrading to HTTPS? | |
| 116 bool sts_include_subdomains; | |
| 117 | |
| 118 // Are subdomains subject to this DomainState, for the purposes of | |
| 119 // Pin Validation? | |
| 120 bool pkp_include_subdomains; | |
| 121 | |
| 122 // Optional; hashes of static pinned SubjectPublicKeyInfos. Unless both | |
| 123 // are empty, at least one of |static_spki_hashes| and | |
| 124 // |dynamic_spki_hashes| MUST intersect with the set of SPKIs in the TLS | |
| 125 // server's certificate chain. | |
| 126 // | |
| 127 // |dynamic_spki_hashes| take precedence over |static_spki_hashes|. | |
| 128 // That is, |IsChainOfPublicKeysPermitted| first checks dynamic pins and | |
| 129 // then checks static pins. | |
| 130 HashValueVector static_spki_hashes; | |
| 131 | |
| 132 // Optional; hashes of dynamically pinned SubjectPublicKeyInfos. | |
| 133 HashValueVector dynamic_spki_hashes; | |
| 134 | |
| 135 // The absolute time (UTC) when the |dynamic_spki_hashes| expire. | |
| 136 base::Time dynamic_spki_hashes_expiry; | |
| 137 | |
| 138 // Optional; hashes of static known-bad SubjectPublicKeyInfos which | |
| 139 // MUST NOT intersect with the set of SPKIs in the TLS server's | |
| 140 // certificate chain. | |
| 141 HashValueVector bad_static_spki_hashes; | |
| 142 | 133 |
| 143 // The following members are not valid when stored in |enabled_hosts_|: | 134 // The following members are not valid when stored in |enabled_hosts_|: |
| 144 | 135 |
| 145 // The domain which matched during a search for this DomainState entry. | 136 // The domain which matched during a search for this DomainState entry. |
| 146 // Updated by |GetDomainState|, |GetDynamicDomainState|, and | 137 // Updated by |GetDynamicDomainState| and |GetStaticDomainState|. |
| 147 // |GetStaticDomainState|. | |
| 148 std::string domain; | 138 std::string domain; |
| 149 }; | 139 }; |
| 150 | 140 |
| 151 class NET_EXPORT Iterator { | 141 class NET_EXPORT Iterator { |
| 152 public: | 142 public: |
| 153 explicit Iterator(const TransportSecurityState& state); | 143 explicit Iterator(const TransportSecurityState& state); |
| 154 ~Iterator(); | 144 ~Iterator(); |
| 155 | 145 |
| 156 bool HasNext() const { return iterator_ != end_; } | 146 bool HasNext() const { return iterator_ != end_; } |
| 157 void Advance() { ++iterator_; } | 147 void Advance() { ++iterator_; } |
| 158 const std::string& hostname() const { return iterator_->first; } | 148 const std::string& hostname() const { return iterator_->first; } |
| 159 const DomainState& domain_state() const { return iterator_->second; } | 149 const DomainState& domain_state() const { return iterator_->second; } |
| 160 | 150 |
| 161 private: | 151 private: |
| 162 std::map<std::string, DomainState>::const_iterator iterator_; | 152 std::map<std::string, DomainState>::const_iterator iterator_; |
| 163 std::map<std::string, DomainState>::const_iterator end_; | 153 std::map<std::string, DomainState>::const_iterator end_; |
| 164 }; | 154 }; |
| 165 | 155 |
| 156 // These functions search for static and dynamic DomainStates, and invoke the |
| 157 // functions of the same name on them. These functions are the primary public |
| 158 // interface; direct access to DomainStates is best left to tests. |
| 159 bool ShouldSSLErrorsBeFatal(const std::string& host, bool sni_enabled); |
| 160 bool ShouldUpgradeToSSL(const std::string& host, bool sni_enabled); |
| 161 bool CheckPublicKeyPins(const std::string& host, |
| 162 bool sni_enabled, |
| 163 const HashValueVector& hashes, |
| 164 std::string* failure_log); |
| 165 bool HasPublicKeyPins(const std::string& host, bool sni_enabled); |
| 166 |
| 166 // Assign a |Delegate| for persisting the transport security state. If | 167 // Assign a |Delegate| for persisting the transport security state. If |
| 167 // |NULL|, state will not be persisted. The caller retains | 168 // |NULL|, state will not be persisted. The caller retains |
| 168 // ownership of |delegate|. | 169 // ownership of |delegate|. |
| 169 // Note: This is only used for serializing/deserializing the | 170 // Note: This is only used for serializing/deserializing the |
| 170 // TransportSecurityState. | 171 // TransportSecurityState. |
| 171 void SetDelegate(Delegate* delegate); | 172 void SetDelegate(Delegate* delegate); |
| 172 | 173 |
| 173 // Clears all dynamic data (e.g. HSTS and HPKP data). | 174 // Clears all dynamic data (e.g. HSTS and HPKP data). |
| 174 // | 175 // |
| 175 // Does NOT persist changes using the Delegate, as this function is only | 176 // Does NOT persist changes using the Delegate, as this function is only |
| (...skipping 30 matching lines...) Expand all Loading... |
| 206 // |host|. | 207 // |host|. |
| 207 // | 208 // |
| 208 // If |sni_enabled| is true, searches the static pins defined for | 209 // If |sni_enabled| is true, searches the static pins defined for |
| 209 // SNI-using hosts as well as the rest of the pins. | 210 // SNI-using hosts as well as the rest of the pins. |
| 210 // | 211 // |
| 211 // If |host| matches both an exact entry and is a subdomain of another | 212 // If |host| matches both an exact entry and is a subdomain of another |
| 212 // entry, the exact match determines the return value. | 213 // entry, the exact match determines the return value. |
| 213 // | 214 // |
| 214 // Note that this method is not const because it opportunistically removes | 215 // Note that this method is not const because it opportunistically removes |
| 215 // entries that have expired. | 216 // entries that have expired. |
| 216 bool GetDomainState(const std::string& host, | 217 bool GetStaticDomainState(const std::string& host, |
| 217 bool sni_enabled, | 218 bool sni_enabled, |
| 218 DomainState* result); | 219 DomainState* result); |
| 220 bool GetDynamicDomainState(const std::string& host, DomainState* result); |
| 219 | 221 |
| 220 // Processes an HSTS header value from the host, adding entries to | 222 // Processes an HSTS header value from the host, adding entries to |
| 221 // dynamic state if necessary. | 223 // dynamic state if necessary. |
| 222 bool AddHSTSHeader(const std::string& host, const std::string& value); | 224 bool AddHSTSHeader(const std::string& host, const std::string& value); |
| 223 | 225 |
| 224 // Processes an HPKP header value from the host, adding entries to | 226 // Processes an HPKP header value from the host, adding entries to |
| 225 // dynamic state if necessary. ssl_info is used to check that | 227 // dynamic state if necessary. ssl_info is used to check that |
| 226 // the specified pins overlap with the certificate chain. | 228 // the specified pins overlap with the certificate chain. |
| 227 bool AddHPKPHeader(const std::string& host, const std::string& value, | 229 bool AddHPKPHeader(const std::string& host, const std::string& value, |
| 228 const SSLInfo& ssl_info); | 230 const SSLInfo& ssl_info); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 // state for the |host|, including static entries. | 284 // state for the |host|, including static entries. |
| 283 // | 285 // |
| 284 // The new state for |host| is persisted using the Delegate (if any). | 286 // The new state for |host| is persisted using the Delegate (if any). |
| 285 void EnableHost(const std::string& host, const DomainState& state); | 287 void EnableHost(const std::string& host, const DomainState& state); |
| 286 | 288 |
| 287 // Converts |hostname| from dotted form ("www.google.com") to the form | 289 // Converts |hostname| from dotted form ("www.google.com") to the form |
| 288 // used in DNS: "\x03www\x06google\x03com", lowercases that, and returns | 290 // used in DNS: "\x03www\x06google\x03com", lowercases that, and returns |
| 289 // the result. | 291 // the result. |
| 290 static std::string CanonicalizeHost(const std::string& hostname); | 292 static std::string CanonicalizeHost(const std::string& hostname); |
| 291 | 293 |
| 292 // Returns true and updates |*result| iff there is a static DomainState for | |
| 293 // |host|. | |
| 294 // | |
| 295 // |GetStaticDomainState| is identical to |GetDomainState| except that it | |
| 296 // searches only the statically-defined transport security state, ignoring | |
| 297 // all dynamically-added DomainStates. | |
| 298 // | |
| 299 // If |sni_enabled| is true, searches the static pins defined for | |
| 300 // SNI-using hosts as well as the rest of the pins. | |
| 301 // | |
| 302 // If |host| matches both an exact entry and is a subdomain of another | |
| 303 // entry, the exact match determines the return value. | |
| 304 // | |
| 305 // Note that this method is not const because it opportunistically removes | |
| 306 // entries that have expired. | |
| 307 bool GetStaticDomainState(const std::string& host, | |
| 308 bool sni_enabled, | |
| 309 DomainState* result); | |
| 310 | |
| 311 // Returns true and updates |*result| iff there is a dynamic DomainState for | |
| 312 // |host|. | |
| 313 // | |
| 314 // |GetDynamicDomainState| is identical to |GetDomainState| except that it | |
| 315 // searches only the dynamically-added transport security state, ignoring | |
| 316 // all statically-defined DomainStates. | |
| 317 // | |
| 318 // If |host| matches both an exact entry and is a subdomain of another | |
| 319 // entry, the exact match determines the return value. | |
| 320 // | |
| 321 // Note that this method is not const because it opportunistically removes | |
| 322 // entries that have expired. | |
| 323 bool GetDynamicDomainState(const std::string& host, DomainState* result); | |
| 324 | |
| 325 // The set of hosts that have enabled TransportSecurity. | 294 // The set of hosts that have enabled TransportSecurity. |
| 326 DomainStateMap enabled_hosts_; | 295 DomainStateMap enabled_hosts_; |
| 327 | 296 |
| 328 Delegate* delegate_; | 297 Delegate* delegate_; |
| 329 | 298 |
| 330 DISALLOW_COPY_AND_ASSIGN(TransportSecurityState); | 299 DISALLOW_COPY_AND_ASSIGN(TransportSecurityState); |
| 331 }; | 300 }; |
| 332 | 301 |
| 333 } // namespace net | 302 } // namespace net |
| 334 | 303 |
| 335 #endif // NET_HTTP_TRANSPORT_SECURITY_STATE_H_ | 304 #endif // NET_HTTP_TRANSPORT_SECURITY_STATE_H_ |
| OLD | NEW |