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_BASE_TRANSPORT_SECURITY_STATE_H_ | 5 #ifndef NET_BASE_TRANSPORT_SECURITY_STATE_H_ |
6 #define NET_BASE_TRANSPORT_SECURITY_STATE_H_ | 6 #define NET_BASE_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 10 matching lines...) Expand all Loading... | |
21 namespace net { | 21 namespace net { |
22 | 22 |
23 class SSLInfo; | 23 class SSLInfo; |
24 | 24 |
25 // Tracks which hosts have enabled strict transport security and/or public | 25 // Tracks which hosts have enabled strict transport security and/or public |
26 // key pins. | 26 // key pins. |
27 // | 27 // |
28 // This object manages the in-memory store. Register a Delegate with | 28 // This object manages the in-memory store. Register a Delegate with |
29 // |SetDelegate| to persist the state to disk. | 29 // |SetDelegate| to persist the state to disk. |
30 // | 30 // |
31 // HTTP strict transport security (HSTS) is defined in | 31 // HTTP strict transport security (HSTS) is defined in RFC 6797, and |
32 // http://tools.ietf.org/html/ietf-websec-strict-transport-sec, and | |
33 // HTTP-based dynamic public key pinning (HPKP) is defined in | 32 // HTTP-based dynamic public key pinning (HPKP) is defined in |
34 // http://tools.ietf.org/html/ietf-websec-key-pinning. | 33 // http://tools.ietf.org/html/ietf-websec-key-pinning. |
35 class NET_EXPORT TransportSecurityState | 34 class NET_EXPORT TransportSecurityState |
36 : NON_EXPORTED_BASE(public base::NonThreadSafe) { | 35 : NON_EXPORTED_BASE(public base::NonThreadSafe) { |
37 public: | 36 public: |
38 class 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 | 37 |
44 protected: | 38 // A DomainState describes the entire set of applicable transport security |
45 virtual ~Delegate() {} | 39 // state for a domain at a particular point in time (required upgrade to |
46 }; | 40 // HTTPS, public key pins, etc). DomainStates are not stored directly |
47 | 41 // but are calculated by searching through the dynamic and preload entries |
48 TransportSecurityState(); | 42 // and merging all relevant and nonexpired data into the DomainState. |
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 { | 43 class NET_EXPORT DomainState { |
54 public: | 44 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(); | 45 DomainState(); |
62 ~DomainState(); | 46 ~DomainState(); |
63 | 47 |
48 // ShouldUpgradeToSSL returns true iff HTTP requests should be internally | |
49 // redirected to HTTPS (also if the "ws" WebSocket request should be | |
50 // upgraded to "wss"). | |
51 bool ShouldUpgradeToSSL() const; | |
52 | |
53 // ShouldSSLErrorsBeFatal returns true iff HTTPS errors should cause | |
54 // hard-fail behavior (e.g. if HSTS is set for the domain) | |
55 bool ShouldSSLErrorsBeFatal() const; | |
56 | |
57 // Returns true if CheckPublicKeyPins() should be called to verify | |
58 // SSL/TLS connections. | |
59 bool HasPublicKeyPins() const; | |
60 | |
64 // Takes a set of SubjectPublicKeyInfo |hashes| and returns true if: | 61 // Takes a set of SubjectPublicKeyInfo |hashes| and returns true if: |
65 // 1) |bad_static_spki_hashes| does not intersect |hashes|; AND | 62 // 1) |public_key_pins_bad_hashes_| does not intersect |hashes|; AND |
66 // 2) Both |static_spki_hashes| and |dynamic_spki_hashes| are empty | 63 // 2) |public_key_pins_good_hashes_| is either empty or intersects |
67 // or at least one of them intersects |hashes|. | 64 // |hashes|. |
68 // | 65 // |
69 // |{dynamic,static}_spki_hashes| contain trustworthy public key hashes, | 66 // |public_key_pins_good_hashes_| contain trustworthy public key hashes, |
70 // any one of which is sufficient to validate the certificate chain in | 67 // 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 | 68 // question. The public keys could be of a root CA, intermediate CA, or |
72 // leaf certificate, depending on the security vs. disaster recovery | 69 // leaf certificate, depending on the security vs. disaster recovery |
73 // tradeoff selected. (Pinning only to leaf certifiates increases | 70 // tradeoff selected. (Pinning only to leaf certifiates increases |
74 // security because you no longer trust any CAs, but it hampers disaster | 71 // 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 | 72 // recovery because you can't just get a new certificate signed by the |
76 // CA.) | 73 // CA.) |
77 // | 74 // |
78 // |bad_static_spki_hashes| contains public keys that we don't want to | 75 // |public_key_pins_bad_hashes_| contains public keys that we don't want |
79 // trust. | 76 // to trust. |
80 bool CheckPublicKeyPins(const HashValueVector& hashes) const; | 77 bool CheckPublicKeyPins(const HashValueVector& hashes) const; |
81 | 78 |
82 // Returns true if any of the HashValueVectors |static_spki_hashes|, | 79 // Returns true iff we have any preloaded public key pins for the domain |
83 // |bad_static_spki_hashes|, or |dynamic_spki_hashes| contains any | 80 // and iff its set of required pins is the set we expect for Google |
84 // items. | 81 // properties. |
85 bool HasPublicKeyPins() const; | 82 bool IsGooglePinnedProperty() const; |
86 | 83 |
87 // ShouldUpgradeToSSL returns true iff, given the |mode| of this | 84 // Send an UMA report on pin validation failure, if the host is in a |
88 // DomainState, HTTP requests should be internally redirected to HTTPS | 85 // statically-defined list of domains. |
89 // (also if the "ws" WebSocket request should be upgraded to "wss") | 86 void ReportUMAOnPinFailure() const; |
90 bool ShouldUpgradeToSSL() const; | |
91 | 87 |
92 // ShouldSSLErrorsBeFatal returns true iff HTTPS errors should cause | 88 // Provide direct read-only access for net-internals |
93 // hard-fail behavior (e.g. if HSTS is set for the domain) | 89 const HashValueVector& GetPublicKeyPinsGoodHashes() const; |
94 bool ShouldSSLErrorsBeFatal() const; | 90 const HashValueVector& GetPublicKeyPinsBadHashes() const; |
95 | 91 |
96 bool Equals(const DomainState& other) const; | 92 private: |
93 friend TransportSecurityState; | |
97 | 94 |
98 UpgradeMode upgrade_mode; | 95 bool should_upgrade_; |
96 bool has_public_key_pins_; | |
97 bool is_google_pinned_property_; | |
98 bool report_uma_on_pin_failure_; | |
99 | 99 |
100 // The absolute time (UTC) when this DomainState was first created. | 100 HashValueVector public_key_pins_good_hashes_; // if has_public_key_pins_ |
101 // | 101 HashValueVector public_key_pins_bad_hashes_; // if has_public_key_pins_ |
102 // Static entries do not have a created time. | |
103 base::Time created; | |
104 | 102 |
105 // The absolute time (UTC) when the |upgrade_mode|, if set to | 103 size_t second_level_domain_name_; // if report_uma_on_pin_failure_ |
106 // UPGRADE_ALWAYS, downgrades to UPGRADE_NEVER. | |
107 base::Time upgrade_expiry; | |
108 | |
109 // Are subdomains subject to this DomainState? | |
110 // | |
111 // TODO(palmer): Decide if we should have separate |pin_subdomains| and | |
112 // |upgrade_subdomains|. Alternately, and perhaps better, is to separate | |
113 // DomainState into UpgradeState and PinState (requiring also changing the | |
114 // serialization format?). | |
115 bool include_subdomains; | |
116 | |
117 // Optional; hashes of static pinned SubjectPublicKeyInfos. Unless both | |
118 // are empty, at least one of |static_spki_hashes| and | |
119 // |dynamic_spki_hashes| MUST intersect with the set of SPKIs in the TLS | |
120 // server's certificate chain. | |
121 // | |
122 // |dynamic_spki_hashes| take precedence over |static_spki_hashes|. | |
123 // That is, |IsChainOfPublicKeysPermitted| first checks dynamic pins and | |
124 // then checks static pins. | |
125 HashValueVector static_spki_hashes; | |
126 | |
127 // Optional; hashes of dynamically pinned SubjectPublicKeyInfos. | |
128 HashValueVector dynamic_spki_hashes; | |
129 | |
130 // The absolute time (UTC) when the |dynamic_spki_hashes| expire. | |
131 base::Time dynamic_spki_hashes_expiry; | |
132 | |
133 // Optional; hashes of static known-bad SubjectPublicKeyInfos which | |
134 // MUST NOT intersect with the set of SPKIs in the TLS server's | |
135 // certificate chain. | |
136 HashValueVector bad_static_spki_hashes; | |
137 | |
138 // The following members are not valid when stored in |enabled_hosts_|: | |
139 | |
140 // The domain which matched during a search for this DomainState entry. | |
141 // Updated by |GetDomainState| and |GetStaticDomainState|. | |
142 std::string domain; | |
143 }; | 104 }; |
144 | 105 |
145 class NET_EXPORT Iterator { | 106 // DynamicEntry stores the HSTS data for a single domain. |
107 struct DynamicEntry { | |
108 DynamicEntry(); | |
109 ~DynamicEntry(); | |
110 DynamicEntry(bool include_subdomains, const base::Time& created, | |
111 const base::Time& expiry); | |
112 | |
113 bool include_subdomains_; | |
114 base::Time created_; | |
115 base::Time expiry_; | |
116 }; | |
117 | |
118 // HPKPEntry stores the HPKP data for a single domain. | |
119 struct HPKPEntry : public DynamicEntry { | |
120 HPKPEntry(); | |
121 ~HPKPEntry(); | |
122 HPKPEntry(bool include_subdomains, const base::Time& created, | |
123 const base::Time& expiry, | |
124 const HashValueVector& good_hashes); | |
125 HashValueVector good_hashes_; | |
126 }; | |
127 | |
128 class Delegate { | |
146 public: | 129 public: |
147 explicit Iterator(const TransportSecurityState& state); | 130 // This function may not block and may be called with internal locks held. |
148 ~Iterator(); | 131 // Thus it must not reenter the TransportSecurityState object. |
132 virtual void StateIsDirty(TransportSecurityState* state) = 0; | |
149 | 133 |
150 bool HasNext() const { return iterator_ != end_; } | 134 protected: |
151 void Advance() { ++iterator_; } | 135 virtual ~Delegate() {} |
152 const std::string& hostname() const { return iterator_->first; } | 136 }; |
153 const DomainState& domain_state() const { return iterator_->second; } | |
154 | 137 |
155 private: | 138 TransportSecurityState(); |
156 std::map<std::string, DomainState>::const_iterator iterator_; | 139 ~TransportSecurityState(); |
157 std::map<std::string, DomainState>::const_iterator end_; | |
158 }; | |
159 | 140 |
160 // Assign a |Delegate| for persisting the transport security state. If | 141 // Assign a |Delegate| for persisting the transport security state. If |
161 // |NULL|, state will not be persisted. The caller retains | 142 // |NULL|, state will not be persisted. The caller retains |
162 // ownership of |delegate|. | 143 // ownership of |delegate|. |
163 // Note: This is only used for serializing/deserializing the | 144 // Note: This is only used for serializing/deserializing the |
164 // TransportSecurityState. | 145 // TransportSecurityState. |
165 void SetDelegate(Delegate* delegate); | 146 void SetDelegate(Delegate* delegate); |
166 | 147 |
167 // Clears all dynamic data (e.g. HSTS and HPKP data). | 148 // Clears all dynamic data (e.g. HSTS and HPKP data). |
168 // | 149 // |
169 // Does NOT persist changes using the Delegate, as this function is only | 150 // Does NOT persist changes using the Delegate, as this function is only |
170 // used to clear any dynamic data prior to re-loading it from a file. | 151 // used to clear any dynamic data prior to re-loading it from a file. |
171 // Note: This is only used for serializing/deserializing the | 152 // Note: This is only used for serializing/deserializing the |
172 // TransportSecurityState. | 153 // TransportSecurityState. |
173 void ClearDynamicData(); | 154 void ClearDynamicData(); |
174 | 155 |
175 // Inserts |state| into |enabled_hosts_| under the key |hashed_host|. | 156 // Returns true and updates |*result| iff there is DomainState for |host|. |
176 // |hashed_host| is already in the internal representation | 157 // |
177 // HashHost(CanonicalizeHost(host)). | 158 // If |sni_enabled| is true, searches the preload data defined for |
178 // Note: This is only used for serializing/deserializing the | 159 // SNI-only hosts as well as the other preload data. |
179 // TransportSecurityState. | 160 bool GetDomainState(const std::string& host, |
180 void AddOrUpdateEnabledHosts(const std::string& hashed_host, | 161 bool sni_enabled, |
181 const DomainState& state); | 162 DomainState* result) const; |
182 | 163 |
183 // Inserts |state| into |forced_hosts_| under the key |hashed_host|. | 164 // Processes an HSTS header value from the host, adds/deletes entries |
184 // |hashed_host| is already in the internal representation | 165 // in dynamic state if necessary. |
185 // HashHost(CanonicalizeHost(host)). | 166 void AddHSTSHeader(const std::string& host, const std::string& value); |
186 // Note: This is only used for serializing/deserializing the | 167 |
187 // TransportSecurityState. | 168 // Processes an HPKP header value from the host, adds/deletes entries |
188 void AddOrUpdateForcedHosts(const std::string& hashed_host, | 169 // in dynamic state if necessary. ssl_info is used to check that |
189 const DomainState& state); | 170 // the specified pins overlap with the certificate chain. |
171 void AddHPKPHeader(const std::string& host, const std::string& value, | |
172 const SSLInfo& ssl_info); | |
173 | |
174 // Adds explicitly-specified data as if it was processed from an | |
175 // HSTS/HPKP header (used for net-internals and unit tests). | |
176 // Returns true iff an entry was succesfully added, false if | |
177 // expiry < now or created > now. | |
178 bool AddHSTS(const std::string& host, const base::Time& created, | |
179 const base::Time& expiry, bool include_subdomains); | |
180 bool AddHPKP(const std::string& host, const base::Time& created, | |
181 const base::Time& expiry, bool include_subdomains, | |
182 const HashValueVector& hashes); | |
183 | |
184 // As AddHSTS()/AddHPKP() but uses the internal "hashed" representation of | |
185 // a hostname. This is used internally by AddHSTS()/AddHPKP() and is also | |
186 // used for deserializing the TransportSecurityState (since the JSON stores | |
187 // entries in hashed form). | |
188 bool AddHSTSHashedHost(const std::string& hashed_host, | |
189 const base::Time& created, const base::Time& expiry, | |
190 bool include_subdomains); | |
191 bool AddHPKPHashedHost(const std::string& hashed_host, | |
192 const base::Time& created, const base::Time& expiry, | |
193 bool include_subdomains, | |
194 const HashValueVector& hashes); | |
195 | |
196 // Deletes HSTS/HPKP data for the specified |host|. | |
197 // Returns true iff an entry was succesfully deleted. | |
198 bool DeleteHSTS(const std::string& host); | |
199 bool DeleteHPKP(const std::string& host); | |
200 | |
201 // Deletes any dynamic data stored for |host| (e.g. HSTS or HPKP data). | |
202 // If |host| doesn't have an exact entry then no action is taken. Does | |
203 // not delete preload data. Returns true iff an entry | |
204 // was deleted. | |
205 // | |
206 // If an entry is deleted, the new state will be persisted through | |
207 // the Delegate (if any). | |
208 bool DeleteDynamicDataForHost(const std::string& host); | |
190 | 209 |
191 // Deletes all dynamic data (e.g. HSTS or HPKP data) created since a given | 210 // Deletes all dynamic data (e.g. HSTS or HPKP data) created since a given |
192 // time. | 211 // time. |
193 // | 212 // |
194 // If any entries are deleted, the new state will be persisted through | 213 // If any entries are deleted, the new state will be persisted through |
195 // the Delegate (if any). | 214 // the Delegate (if any). |
196 void DeleteAllDynamicDataSince(const base::Time& time); | 215 void DeleteAllDynamicDataSince(const base::Time& time); |
197 | 216 |
198 // Deletes any dynamic data stored for |host| (e.g. HSTS or HPKP data). | 217 // Direct (read-only) access to HSTS and HPKP entries. Used for |
199 // If |host| doesn't have an exact entry then no action is taken. Does | 218 // serializing. |
200 // not delete static (i.e. preloaded) data. Returns true iff an entry | 219 const std::map<std::string, DynamicEntry>& GetHSTSEntries() const; |
201 // was deleted. | 220 const std::map<std::string, HPKPEntry>& GetHPKPEntries() const; |
202 // | |
203 // If an entry is deleted, the new state will be persisted through | |
204 // the Delegate (if any). | |
205 bool DeleteDynamicDataForHost(const std::string& host); | |
206 | |
207 // Returns true and updates |*result| iff there is a DomainState for | |
208 // |host|. | |
209 // | |
210 // If |sni_enabled| is true, searches the static pins defined for | |
211 // SNI-using hosts as well as the rest of the pins. | |
212 // | |
213 // If |host| matches both an exact entry and is a subdomain of another | |
214 // entry, the exact match determines the return value. | |
215 // | |
216 // Note that this method is not const because it opportunistically removes | |
217 // entries that have expired. | |
218 bool GetDomainState(const std::string& host, | |
219 bool sni_enabled, | |
220 DomainState* result); | |
221 | |
222 // Processes an HSTS header value from the host, adding entries to | |
223 // dynamic state if necessary. | |
224 bool AddHSTSHeader(const std::string& host, const std::string& value); | |
225 | |
226 // Processes an HPKP header value from the host, adding entries to | |
227 // dynamic state if necessary. ssl_info is used to check that | |
228 // the specified pins overlap with the certificate chain. | |
229 bool AddHPKPHeader(const std::string& host, const std::string& value, | |
230 const SSLInfo& ssl_info); | |
231 | |
232 // Adds explicitly-specified data as if it was processed from an | |
233 // HSTS header (used for net-internals and unit tests). | |
234 bool AddHSTS(const std::string& host, const base::Time& expiry, | |
235 bool include_subdomains); | |
236 | |
237 // Adds explicitly-specified data as if it was processed from an | |
238 // HPKP header (used for net-internals and unit tests). | |
239 bool AddHPKP(const std::string& host, const base::Time& expiry, | |
240 bool include_subdomains, const HashValueVector& hashes); | |
241 | |
242 // Returns true iff we have any static public key pins for the |host| and | |
243 // iff its set of required pins is the set we expect for Google | |
244 // properties. | |
245 // | |
246 // If |sni_enabled| is true, searches the static pins defined for | |
247 // SNI-using hosts as well as the rest of the pins. | |
248 // | |
249 // If |host| matches both an exact entry and is a subdomain of another | |
250 // entry, the exact match determines the return value. | |
251 static bool IsGooglePinnedProperty(const std::string& host, | |
252 bool sni_enabled); | |
253 | |
254 // The maximum number of seconds for which we'll cache an HSTS request. | |
255 static const long int kMaxHSTSAgeSecs; | |
256 | |
257 // Send an UMA report on pin validation failure, if the host is in a | |
258 // statically-defined list of domains. | |
259 // | |
260 // TODO(palmer): This doesn't really belong here, and should be moved into | |
261 // the exactly one call site. This requires unifying |struct HSTSPreload| | |
262 // (an implementation detail of this class) with a more generic | |
263 // representation of first-class DomainStates, and exposing the preloads | |
264 // to the caller with |GetStaticDomainState|. | |
265 static void ReportUMAOnPinFailure(const std::string& host); | |
266 | 221 |
267 // IsBuildTimely returns true if the current build is new enough ensure that | 222 // IsBuildTimely returns true if the current build is new enough ensure that |
268 // built in security information (i.e. HSTS preloading and pinning | 223 // built in security information (i.e. HSTS preloading and pinning |
269 // information) is timely. | 224 // information) is timely. |
270 static bool IsBuildTimely(); | 225 static bool IsBuildTimely(); |
271 | 226 |
227 // The maximum number of seconds for which we'll cache an HSTS request. | |
228 static const long int kMaxHSTSAgeSecs; | |
229 | |
272 private: | 230 private: |
273 friend class TransportSecurityStateTest; | 231 friend class TransportSecurityStateTest; |
274 | 232 |
275 typedef std::map<std::string, DomainState> DomainStateMap; | 233 // If a Delegate is present and any of the dynamic entry maps have become |
276 | 234 // dirty, notify the Delegate. |
277 // If a Delegate is present, notify it that the internal state has | 235 void CheckDirty(); |
278 // changed. | 236 |
279 void DirtyNotify(); | 237 // Returns true iff there is any DomainState data in preload entries |
280 | 238 bool GetPreloadDomainState(bool sni_enabled, const base::Time& now, |
281 // Enable TransportSecurity for |host|. |state| supercedes any previous | 239 const std::string& host, DomainState* result) const; |
282 // state for the |host|, including static entries. | 240 |
283 // | 241 // Returns true iff there is any DomainState data in dynamic entries |
284 // The new state for |host| is persisted using the Delegate (if any). | 242 bool GetDynamicDomainState(const base::Time& now, const std::string& host, |
285 void EnableHost(const std::string& host, const DomainState& state); | 243 DomainState* result) const; |
286 | 244 |
287 // Converts |hostname| from dotted form ("www.google.com") to the form | 245 template<typename T> |
288 // used in DNS: "\x03www\x06google\x03com", lowercases that, and returns | 246 class DynamicEntryMap : public std::map<std::string, T> { |
Ryan Sleevi
2013/03/23 04:24:13
It's not "legal" C++ to derive from containers wit
unsafe
2013/03/23 08:19:39
Hmmm (going to stackoverflow, coming back)... so,
| |
289 // the result. | 247 public: |
290 static std::string CanonicalizeHost(const std::string& hostname); | 248 DynamicEntryMap<T>(); |
291 | 249 |
292 // Returns true and updates |*result| iff there is a static DomainState for | 250 // True if an entry is returned |
293 // |host|. | 251 bool GetEntry(const base::Time& now, const std::string& hashed_host, |
294 // | 252 bool is_full_hostname, T* result_entry) const; |
295 // |GetStaticDomainState| is identical to |GetDomainState| except that it | 253 |
296 // searches only the statically-defined transport security state, ignoring | 254 // True if an entry is successfully added |
297 // all dynamically-added DomainStates. | 255 bool AddEntry(const std::string& hashed_host, |
298 // | 256 const T& new_entry); |
299 // If |sni_enabled| is true, searches the static pins defined for | 257 |
300 // SNI-using hosts as well as the rest of the pins. | 258 // True if any entries are deleted |
301 // | 259 bool DeleteEntry(const std::string& hashed_host); |
302 // If |host| matches both an exact entry and is a subdomain of another | 260 |
303 // entry, the exact match determines the return value. | 261 void DeleteEntriesSince(const base::Time& time); |
304 // | 262 |
305 // Note that this method is not const because it opportunistically removes | 263 // Set by any of the non-const member functions when map is changed |
306 // entries that have expired. | 264 bool dirty; |
307 bool GetStaticDomainState(const std::string& host, | 265 }; |
308 bool sni_enabled, | 266 |
309 DomainState* result); | 267 DynamicEntryMap<DynamicEntry> hsts_entries_; |
310 | 268 DynamicEntryMap<HPKPEntry> hpkp_entries_; |
311 // The set of hosts that have enabled TransportSecurity. | |
312 DomainStateMap enabled_hosts_; | |
313 | |
314 // Extra entries, provided by the user at run-time, to treat as if they | |
315 // were static. | |
316 DomainStateMap forced_hosts_; | |
317 | 269 |
318 Delegate* delegate_; | 270 Delegate* delegate_; |
319 | 271 |
320 DISALLOW_COPY_AND_ASSIGN(TransportSecurityState); | 272 DISALLOW_COPY_AND_ASSIGN(TransportSecurityState); |
321 }; | 273 }; |
322 | 274 |
275 // DynamicEntryMap template functions | |
276 | |
277 #define DynamicEntryMapConstIter \ | |
278 typename std::map<std::string, T>::const_iterator | |
279 #define DynamicEntryMapIter \ | |
280 typename std::map<std::string, T>::iterator | |
Ryan Sleevi
2013/03/23 04:24:13
Per http://google-styleguide.googlecode.com/svn/tr
unsafe
2013/03/23 08:19:39
Can't typedef this, due to templates.. perhaps a "
| |
281 | |
282 template <typename T> | |
283 TransportSecurityState::DynamicEntryMap<T>::DynamicEntryMap():dirty(false) { | |
284 } | |
285 | |
286 template <typename T> | |
287 bool TransportSecurityState::DynamicEntryMap<T>::GetEntry( | |
288 const base::Time& now, const std::string& hashed_host, | |
289 bool is_full_hostname, T* result_entry) const { | |
290 // Find the entry, and return if relevant and nonexpired | |
291 DynamicEntryMapConstIter find_iter = this->find(hashed_host); | |
292 if (find_iter != this->end()) { | |
293 const T& find_entry = find_iter->second; | |
294 if ((is_full_hostname || find_entry.include_subdomains_) && | |
295 find_entry.expiry_ > now) { | |
296 *result_entry = find_entry; | |
297 return true; | |
298 } | |
299 } | |
300 return false; | |
301 } | |
302 | |
303 template<typename T> | |
304 bool TransportSecurityState::DynamicEntryMap<T>::AddEntry( | |
305 const std::string& hashed_host, const T& new_entry) { | |
306 // Don't add entries with invalid/expired time values | |
307 const base::Time now = base::Time::Now(); | |
308 if (new_entry.created_ > now) | |
309 return false; | |
310 if (new_entry.expiry_ < now) | |
311 return false; | |
312 | |
313 DynamicEntryMapIter find_iter = this->find(hashed_host); | |
314 if (find_iter != this->end()) { | |
315 // Leave 'created' unchanged | |
316 T& find_entry = find_iter->second; | |
317 find_entry.expiry_ = new_entry.expiry_; | |
318 find_entry.include_subdomains_ = new_entry.include_subdomains_; | |
319 dirty = true; | |
320 } else { | |
321 this->operator[](hashed_host) = new_entry; | |
322 dirty = true; | |
323 } | |
324 return true; | |
325 } | |
326 | |
327 template<typename T> | |
328 bool TransportSecurityState::DynamicEntryMap<T>::DeleteEntry( | |
329 const std::string& hashed_host) { | |
330 DynamicEntryMapIter find_iter = this->find(hashed_host); | |
331 if (find_iter != this->end()) { | |
332 this->erase(find_iter); | |
333 dirty = true; | |
334 return true; | |
335 } | |
336 return false; | |
337 } | |
338 | |
339 template<typename T> | |
340 void TransportSecurityState::DynamicEntryMap<T>::DeleteEntriesSince( | |
341 const base::Time& time) { | |
342 DynamicEntryMapIter iter = this->begin(); | |
343 while (iter != this->end()) { | |
344 if (iter->second.created_ >= time) { | |
345 this->erase(iter++); | |
346 dirty = true; | |
347 } else { | |
348 iter++; | |
349 } | |
350 } | |
351 } | |
352 | |
323 } // namespace net | 353 } // namespace net |
324 | 354 |
325 #endif // NET_BASE_TRANSPORT_SECURITY_STATE_H_ | 355 #endif // NET_BASE_TRANSPORT_SECURITY_STATE_H_ |
OLD | NEW |