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 3 // found in the LICENSE file. 4 5 #ifndef NET_HTTP_TRANSPORT_SECURITY_STATE_H_ 6 #define NET_HTTP_TRANSPORT_SECURITY_STATE_H_ 7 8 #include <map> 9 #include <string> 10 #include <utility> 11 #include <vector> 12 13 #include "base/basictypes.h" 14 #include "base/gtest_prod_util.h" 15 #include "base/threading/non_thread_safe.h" 16 #include "base/time/time.h" 17 #include "net/base/net_export.h" 18 #include "net/cert/x509_cert_types.h" 19 #include "net/cert/x509_certificate.h" 20 21 namespace net { 22 23 class SSLInfo; 24 25 // Tracks which hosts have enabled strict transport security and/or public 26 // key pins. 27 // 28 // This object manages the in-memory store. Register a Delegate with 29 // |SetDelegate| to persist the state to disk. 30 // 31 // HTTP strict transport security (HSTS) is defined in 32 // http://tools.ietf.org/html/ietf-websec-strict-transport-sec, and 33 // HTTP-based dynamic public key pinning (HPKP) is defined in 34 // http://tools.ietf.org/html/ietf-websec-key-pinning. 35 class NET_EXPORT TransportSecurityState NON_EXPORTED_BASE(public base::NonThreadSafe)36 : NON_EXPORTED_BASE(public base::NonThreadSafe) { 37 public: 38 class NET_EXPORT Delegate { 39 public: 40 // This function may not block and may be called with internal locks held. 41 // Thus it must not reenter the TransportSecurityState object. 42 virtual void StateIsDirty(TransportSecurityState* state) = 0; 43 44 protected: 45 virtual ~Delegate() {} 46 }; 47 48 TransportSecurityState(); 49 ~TransportSecurityState(); 50 51 // A DomainState describes the transport security state (required upgrade 52 // to HTTPS, and/or any public key pins). 53 class NET_EXPORT DomainState { 54 public: 55 enum UpgradeMode { 56 // These numbers must match those in hsts_view.js, function modeToString. 57 MODE_FORCE_HTTPS = 0, 58 MODE_DEFAULT = 1, 59 }; 60 61 DomainState(); 62 ~DomainState(); 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 // MODE_FORCE_HTTPS, downgrades to MODE_DEFAULT. 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 PKPState(); 81 ~PKPState(); 82 83 // The absolute time (UTC) when the |spki_hashes| (and other state) were 84 // observed. 85 base::Time last_observed; 86 87 // The absolute time (UTC) when the |spki_hashes| expire. 88 base::Time expiry; 89 90 // Optional; hashes of pinned SubjectPublicKeyInfos. 91 HashValueVector spki_hashes; 92 93 // Optional; hashes of static known-bad SubjectPublicKeyInfos which MUST 94 // NOT intersect with the set of SPKIs in the TLS server's certificate 95 // chain. 96 HashValueVector bad_spki_hashes; 97 98 // Are subdomains subject to this policy state? 99 bool include_subdomains; 100 }; 101 102 // Takes a set of SubjectPublicKeyInfo |hashes| and returns true if: 103 // 1) |bad_static_spki_hashes| does not intersect |hashes|; AND 104 // 2) Both |static_spki_hashes| and |dynamic_spki_hashes| are empty 105 // or at least one of them intersects |hashes|. 106 // 107 // |{dynamic,static}_spki_hashes| contain trustworthy public key hashes, 108 // any one of which is sufficient to validate the certificate chain in 109 // question. The public keys could be of a root CA, intermediate CA, or 110 // leaf certificate, depending on the security vs. disaster recovery 111 // tradeoff selected. (Pinning only to leaf certifiates increases 112 // security because you no longer trust any CAs, but it hampers disaster 113 // recovery because you can't just get a new certificate signed by the 114 // CA.) 115 // 116 // |bad_static_spki_hashes| contains public keys that we don't want to 117 // trust. 118 bool CheckPublicKeyPins(const HashValueVector& hashes, 119 std::string* failure_log) const; 120 121 // Returns true if any of the HashValueVectors |static_spki_hashes|, 122 // |bad_static_spki_hashes|, or |dynamic_spki_hashes| contains any 123 // items. 124 bool HasPublicKeyPins() const; 125 126 // ShouldUpgradeToSSL returns true iff HTTP requests should be internally 127 // redirected to HTTPS (also if WS should be upgraded to WSS). 128 bool ShouldUpgradeToSSL() const; 129 130 // ShouldSSLErrorsBeFatal returns true iff HTTPS errors should cause 131 // hard-fail behavior (e.g. if HSTS is set for the domain). 132 bool ShouldSSLErrorsBeFatal() const; 133 134 STSState sts; 135 PKPState pkp; 136 137 // The following members are not valid when stored in |enabled_hosts_|: 138 139 // The domain which matched during a search for this DomainState entry. 140 // Updated by |GetDynamicDomainState| and |GetStaticDomainState|. 141 std::string domain; 142 }; 143 144 class NET_EXPORT Iterator { 145 public: 146 explicit Iterator(const TransportSecurityState& state); 147 ~Iterator(); 148 149 bool HasNext() const { return iterator_ != end_; } 150 void Advance() { ++iterator_; } 151 const std::string& hostname() const { return iterator_->first; } 152 const DomainState& domain_state() const { return iterator_->second; } 153 154 private: 155 std::map<std::string, DomainState>::const_iterator iterator_; 156 std::map<std::string, DomainState>::const_iterator end_; 157 }; 158 159 // These functions search for static and dynamic DomainStates, and invoke the 160 // functions of the same name on them. These functions are the primary public 161 // interface; direct access to DomainStates is best left to tests. 162 bool ShouldSSLErrorsBeFatal(const std::string& host); 163 bool ShouldUpgradeToSSL(const std::string& host); 164 bool CheckPublicKeyPins(const std::string& host, 165 bool is_issued_by_known_root, 166 const HashValueVector& hashes, 167 std::string* failure_log); 168 bool HasPublicKeyPins(const std::string& host); 169 170 // Assign a |Delegate| for persisting the transport security state. If 171 // |NULL|, state will not be persisted. The caller retains 172 // ownership of |delegate|. 173 // Note: This is only used for serializing/deserializing the 174 // TransportSecurityState. 175 void SetDelegate(Delegate* delegate); 176 177 // Clears all dynamic data (e.g. HSTS and HPKP data). 178 // 179 // Does NOT persist changes using the Delegate, as this function is only 180 // used to clear any dynamic data prior to re-loading it from a file. 181 // Note: This is only used for serializing/deserializing the 182 // TransportSecurityState. 183 void ClearDynamicData(); 184 185 // Inserts |state| into |enabled_hosts_| under the key |hashed_host|. 186 // |hashed_host| is already in the internal representation 187 // HashHost(CanonicalizeHost(host)). 188 // Note: This is only used for serializing/deserializing the 189 // TransportSecurityState. 190 void AddOrUpdateEnabledHosts(const std::string& hashed_host, 191 const DomainState& state); 192 193 // Deletes all dynamic data (e.g. HSTS or HPKP data) created since a given 194 // time. 195 // 196 // If any entries are deleted, the new state will be persisted through 197 // the Delegate (if any). 198 void DeleteAllDynamicDataSince(const base::Time& time); 199 200 // Deletes any dynamic data stored for |host| (e.g. HSTS or HPKP data). 201 // If |host| doesn't have an exact entry then no action is taken. Does 202 // not delete static (i.e. preloaded) data. Returns true iff an entry 203 // was deleted. 204 // 205 // If an entry is deleted, the new state will be persisted through 206 // the Delegate (if any). 207 bool DeleteDynamicDataForHost(const std::string& host); 208 209 // Returns true and updates |*result| iff there is a static (built-in) 210 // DomainState for |host|. 211 // 212 // If |host| matches both an exact entry and is a subdomain of another entry, 213 // the exact match determines the return value. 214 // 215 // Note that this method is not const because it opportunistically removes 216 // entries that have expired. 217 bool GetStaticDomainState(const std::string& host, DomainState* result) const; 218 219 // Returns true and updates |*result| iff there is a dynamic DomainState 220 // (learned from HSTS or HPKP headers, or set by the user, or other means) for 221 // |host|. 222 // 223 // If |host| matches both an exact entry and is a subdomain of another entry, 224 // the exact match determines the return value. 225 // 226 // Note that this method is not const because it opportunistically removes 227 // entries that have expired. 228 bool GetDynamicDomainState(const std::string& host, DomainState* result); 229 230 // Processes an HSTS header value from the host, adding entries to 231 // dynamic state if necessary. 232 bool AddHSTSHeader(const std::string& host, const std::string& value); 233 234 // Processes an HPKP header value from the host, adding entries to 235 // dynamic state if necessary. ssl_info is used to check that 236 // the specified pins overlap with the certificate chain. 237 bool AddHPKPHeader(const std::string& host, const std::string& value, 238 const SSLInfo& ssl_info); 239 240 // Adds explicitly-specified data as if it was processed from an 241 // HSTS header (used for net-internals and unit tests). 242 bool AddHSTS(const std::string& host, const base::Time& expiry, 243 bool include_subdomains); 244 245 // Adds explicitly-specified data as if it was processed from an 246 // HPKP header (used for net-internals and unit tests). 247 bool AddHPKP(const std::string& host, const base::Time& expiry, 248 bool include_subdomains, const HashValueVector& hashes); 249 250 // Returns true iff we have any static public key pins for the |host| and 251 // iff its set of required pins is the set we expect for Google 252 // properties. 253 // 254 // If |host| matches both an exact entry and is a subdomain of another 255 // entry, the exact match determines the return value. 256 static bool IsGooglePinnedProperty(const std::string& host); 257 258 // The maximum number of seconds for which we'll cache an HSTS request. 259 static const long int kMaxHSTSAgeSecs; 260 261 private: 262 friend class TransportSecurityStateTest; 263 FRIEND_TEST_ALL_PREFIXES(HttpSecurityHeadersTest, UpdateDynamicPKPOnly); 264 FRIEND_TEST_ALL_PREFIXES(HttpSecurityHeadersTest, UpdateDynamicPKPMaxAge0); 265 FRIEND_TEST_ALL_PREFIXES(HttpSecurityHeadersTest, NoClobberPins); 266 267 typedef std::map<std::string, DomainState> DomainStateMap; 268 269 // Send an UMA report on pin validation failure, if the host is in a 270 // statically-defined list of domains. 271 // 272 // TODO(palmer): This doesn't really belong here, and should be moved into 273 // the exactly one call site. This requires unifying |struct HSTSPreload| 274 // (an implementation detail of this class) with a more generic 275 // representation of first-class DomainStates, and exposing the preloads 276 // to the caller with |GetStaticDomainState|. 277 static void ReportUMAOnPinFailure(const std::string& host); 278 279 // IsBuildTimely returns true if the current build is new enough ensure that 280 // built in security information (i.e. HSTS preloading and pinning 281 // information) is timely. 282 static bool IsBuildTimely(); 283 284 // Helper method for actually checking pins. 285 bool CheckPublicKeyPinsImpl(const std::string& host, 286 const HashValueVector& hashes, 287 std::string* failure_log); 288 289 // If a Delegate is present, notify it that the internal state has 290 // changed. 291 void DirtyNotify(); 292 293 // Enable TransportSecurity for |host|. |state| supercedes any previous 294 // state for the |host|, including static entries. 295 // 296 // The new state for |host| is persisted using the Delegate (if any). 297 void EnableHost(const std::string& host, const DomainState& state); 298 299 // Converts |hostname| from dotted form ("www.google.com") to the form 300 // used in DNS: "\x03www\x06google\x03com", lowercases that, and returns 301 // the result. 302 static std::string CanonicalizeHost(const std::string& hostname); 303 304 // The set of hosts that have enabled TransportSecurity. 305 DomainStateMap enabled_hosts_; 306 307 Delegate* delegate_; 308 309 // True if static pins should be used. 310 bool enable_static_pins_; 311 312 DISALLOW_COPY_AND_ASSIGN(TransportSecurityState); 313 }; 314 315 } // namespace net 316 317 #endif // NET_HTTP_TRANSPORT_SECURITY_STATE_H_ 318