Revert of Pepper: Fix reentrancy problem in PepperPluginInstanceImpl. (patchset ...
[chromium-blink-merge.git] / net / http / transport_security_state.h
blob14fe0d7084ef168005f73fe1c2791197f273799c
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.
5 #ifndef NET_HTTP_TRANSPORT_SECURITY_STATE_H_
6 #define NET_HTTP_TRANSPORT_SECURITY_STATE_H_
8 #include <map>
9 #include <string>
10 #include <utility>
11 #include <vector>
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"
21 namespace net {
23 class SSLInfo;
25 // Tracks which hosts have enabled strict transport security and/or public
26 // key pins.
28 // This object manages the in-memory store. Register a Delegate with
29 // |SetDelegate| to persist the state to disk.
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
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;
44 protected:
45 virtual ~Delegate() {}
48 TransportSecurityState();
49 ~TransportSecurityState();
51 // A DomainState describes the transport security state (required upgrade
52 // to HTTPS, and/or any public key pins).
54 // TODO(davidben): STSState and PKPState are queried and processed
55 // independently (with the exception of ShouldSSLErrorsBeFatal triggering on
56 // both and on-disk storage). DomainState should be split into the
57 // two. https://crbug.com/470295.
58 class NET_EXPORT DomainState {
59 public:
60 enum UpgradeMode {
61 // These numbers must match those in hsts_view.js, function modeToString.
62 MODE_FORCE_HTTPS = 0,
63 MODE_DEFAULT = 1,
66 DomainState();
67 ~DomainState();
69 struct STSState {
70 STSState();
71 ~STSState();
73 // The absolute time (UTC) when the |upgrade_mode| (and other state) was
74 // observed.
75 base::Time last_observed;
77 // The absolute time (UTC) when the |upgrade_mode|, if set to
78 // MODE_FORCE_HTTPS, downgrades to MODE_DEFAULT.
79 base::Time expiry;
81 UpgradeMode upgrade_mode;
83 // Are subdomains subject to this policy state?
84 bool include_subdomains;
86 // The domain which matched during a search for this DomainState entry.
87 // Updated by |GetDynamicDomainState| and |GetStaticDomainState|.
88 std::string domain;
91 struct PKPState {
92 PKPState();
93 ~PKPState();
95 // The absolute time (UTC) when the |spki_hashes| (and other state) were
96 // observed.
97 base::Time last_observed;
99 // The absolute time (UTC) when the |spki_hashes| expire.
100 base::Time expiry;
102 // Optional; hashes of pinned SubjectPublicKeyInfos.
103 HashValueVector spki_hashes;
105 // Optional; hashes of static known-bad SubjectPublicKeyInfos which MUST
106 // NOT intersect with the set of SPKIs in the TLS server's certificate
107 // chain.
108 HashValueVector bad_spki_hashes;
110 // Are subdomains subject to this policy state?
111 bool include_subdomains;
113 // The domain which matched during a search for this DomainState entry.
114 // Updated by |GetDynamicDomainState| and |GetStaticDomainState|.
115 std::string domain;
118 // Takes a set of SubjectPublicKeyInfo |hashes| and returns true if:
119 // 1) |bad_static_spki_hashes| does not intersect |hashes|; AND
120 // 2) Both |static_spki_hashes| and |dynamic_spki_hashes| are empty
121 // or at least one of them intersects |hashes|.
123 // |{dynamic,static}_spki_hashes| contain trustworthy public key hashes,
124 // any one of which is sufficient to validate the certificate chain in
125 // question. The public keys could be of a root CA, intermediate CA, or
126 // leaf certificate, depending on the security vs. disaster recovery
127 // tradeoff selected. (Pinning only to leaf certifiates increases
128 // security because you no longer trust any CAs, but it hampers disaster
129 // recovery because you can't just get a new certificate signed by the
130 // CA.)
132 // |bad_static_spki_hashes| contains public keys that we don't want to
133 // trust.
134 bool CheckPublicKeyPins(const HashValueVector& hashes,
135 std::string* failure_log) const;
137 // Returns true if any of the HashValueVectors |static_spki_hashes|,
138 // |bad_static_spki_hashes|, or |dynamic_spki_hashes| contains any
139 // items.
140 bool HasPublicKeyPins() const;
142 // ShouldUpgradeToSSL returns true iff HTTP requests should be internally
143 // redirected to HTTPS (also if WS should be upgraded to WSS).
144 bool ShouldUpgradeToSSL() const;
146 // ShouldSSLErrorsBeFatal returns true iff HTTPS errors should cause
147 // hard-fail behavior (e.g. if HSTS is set for the domain).
148 bool ShouldSSLErrorsBeFatal() const;
150 STSState sts;
151 PKPState pkp;
154 class NET_EXPORT Iterator {
155 public:
156 explicit Iterator(const TransportSecurityState& state);
157 ~Iterator();
159 bool HasNext() const { return iterator_ != end_; }
160 void Advance() { ++iterator_; }
161 const std::string& hostname() const { return iterator_->first; }
162 const DomainState& domain_state() const { return iterator_->second; }
164 private:
165 std::map<std::string, DomainState>::const_iterator iterator_;
166 std::map<std::string, DomainState>::const_iterator end_;
169 // These functions search for static and dynamic DomainStates, and invoke the
170 // functions of the same name on them. These functions are the primary public
171 // interface; direct access to DomainStates is best left to tests.
172 bool ShouldSSLErrorsBeFatal(const std::string& host);
173 bool ShouldUpgradeToSSL(const std::string& host);
174 bool CheckPublicKeyPins(const std::string& host,
175 bool is_issued_by_known_root,
176 const HashValueVector& hashes,
177 std::string* failure_log);
178 bool HasPublicKeyPins(const std::string& host);
180 // Assign a |Delegate| for persisting the transport security state. If
181 // |NULL|, state will not be persisted. The caller retains
182 // ownership of |delegate|.
183 // Note: This is only used for serializing/deserializing the
184 // TransportSecurityState.
185 void SetDelegate(Delegate* delegate);
187 // Clears all dynamic data (e.g. HSTS and HPKP data).
189 // Does NOT persist changes using the Delegate, as this function is only
190 // used to clear any dynamic data prior to re-loading it from a file.
191 // Note: This is only used for serializing/deserializing the
192 // TransportSecurityState.
193 void ClearDynamicData();
195 // Inserts |state| into |enabled_hosts_| under the key |hashed_host|.
196 // |hashed_host| is already in the internal representation
197 // HashHost(CanonicalizeHost(host)).
198 // Note: This is only used for serializing/deserializing the
199 // TransportSecurityState.
200 void AddOrUpdateEnabledHosts(const std::string& hashed_host,
201 const DomainState& state);
203 // Deletes all dynamic data (e.g. HSTS or HPKP data) created since a given
204 // time.
206 // If any entries are deleted, the new state will be persisted through
207 // the Delegate (if any).
208 void DeleteAllDynamicDataSince(const base::Time& time);
210 // Deletes any dynamic data stored for |host| (e.g. HSTS or HPKP data).
211 // If |host| doesn't have an exact entry then no action is taken. Does
212 // not delete static (i.e. preloaded) data. Returns true iff an entry
213 // was deleted.
215 // If an entry is deleted, the new state will be persisted through
216 // the Delegate (if any).
217 bool DeleteDynamicDataForHost(const std::string& host);
219 // Returns true and updates |*result| iff there is a static (built-in)
220 // DomainState for |host|. If multiple entries match |host|, the most specific
221 // match determines the return value.
222 bool GetStaticDomainState(const std::string& host, DomainState* result) const;
224 // Returns true and updates |*result| iff |host| has HSTS or HPKP state (or
225 // both). The two are queried independently and combined into a single
226 // DomainState. If multiple HSTS (respectively, HPKP) entries match |host|,
227 // the most specific match determines the HSTS (respectively, HPKP) portion of
228 // the return value.
230 // Note that this method is not const because it opportunistically removes
231 // entries that have expired.
233 // TODO(davidben): STSState and PKPState should be queried independently at
234 // the API level too.
235 bool GetDynamicDomainState(const std::string& host, DomainState* result);
237 // Processes an HSTS header value from the host, adding entries to
238 // dynamic state if necessary.
239 bool AddHSTSHeader(const std::string& host, const std::string& value);
241 // Processes an HPKP header value from the host, adding entries to
242 // dynamic state if necessary. ssl_info is used to check that
243 // the specified pins overlap with the certificate chain.
244 bool AddHPKPHeader(const std::string& host, const std::string& value,
245 const SSLInfo& ssl_info);
247 // Adds explicitly-specified data as if it was processed from an
248 // HSTS header (used for net-internals and unit tests).
249 void AddHSTS(const std::string& host,
250 const base::Time& expiry,
251 bool include_subdomains);
253 // Adds explicitly-specified data as if it was processed from an
254 // HPKP header (used for net-internals and unit tests).
255 void AddHPKP(const std::string& host,
256 const base::Time& expiry,
257 bool include_subdomains,
258 const HashValueVector& hashes);
260 // Returns true iff we have any static public key pins for the |host| and
261 // iff its set of required pins is the set we expect for Google
262 // properties.
264 // If |host| matches both an exact entry and is a subdomain of another
265 // entry, the exact match determines the return value.
266 static bool IsGooglePinnedProperty(const std::string& host);
268 // The maximum number of seconds for which we'll cache an HSTS request.
269 static const long int kMaxHSTSAgeSecs;
271 private:
272 friend class TransportSecurityStateTest;
273 FRIEND_TEST_ALL_PREFIXES(HttpSecurityHeadersTest, UpdateDynamicPKPOnly);
274 FRIEND_TEST_ALL_PREFIXES(HttpSecurityHeadersTest, UpdateDynamicPKPMaxAge0);
275 FRIEND_TEST_ALL_PREFIXES(HttpSecurityHeadersTest, NoClobberPins);
277 typedef std::map<std::string, DomainState> DomainStateMap;
279 // Send an UMA report on pin validation failure, if the host is in a
280 // statically-defined list of domains.
282 // TODO(palmer): This doesn't really belong here, and should be moved into
283 // the exactly one call site. This requires unifying |struct HSTSPreload|
284 // (an implementation detail of this class) with a more generic
285 // representation of first-class DomainStates, and exposing the preloads
286 // to the caller with |GetStaticDomainState|.
287 static void ReportUMAOnPinFailure(const std::string& host);
289 // IsBuildTimely returns true if the current build is new enough ensure that
290 // built in security information (i.e. HSTS preloading and pinning
291 // information) is timely.
292 static bool IsBuildTimely();
294 // Helper method for actually checking pins.
295 bool CheckPublicKeyPinsImpl(const std::string& host,
296 const HashValueVector& hashes,
297 std::string* failure_log);
299 // If a Delegate is present, notify it that the internal state has
300 // changed.
301 void DirtyNotify();
303 // Adds HSTS state to |host|.
304 void AddHSTSInternal(const std::string& host,
305 DomainState::UpgradeMode upgrade_mode,
306 const base::Time& expiry,
307 bool include_subdomains);
309 // Adds HPKP state to |host|.
310 void AddHPKPInternal(const std::string& host,
311 const base::Time& last_observed,
312 const base::Time& expiry,
313 bool include_subdomains,
314 const HashValueVector& hashes);
316 // Enable TransportSecurity for |host|. |state| supercedes any previous
317 // state for the |host|, including static entries.
319 // The new state for |host| is persisted using the Delegate (if any).
320 void EnableHost(const std::string& host, const DomainState& state);
322 // Converts |hostname| from dotted form ("www.google.com") to the form
323 // used in DNS: "\x03www\x06google\x03com", lowercases that, and returns
324 // the result.
325 static std::string CanonicalizeHost(const std::string& hostname);
327 // The set of hosts that have enabled TransportSecurity. |sts.domain| and
328 // |pkp.domain| will always be empty for a DomainState in this map; the domain
329 // comes from the map key instead.
330 DomainStateMap enabled_hosts_;
332 Delegate* delegate_;
334 // True if static pins should be used.
335 bool enable_static_pins_;
337 DISALLOW_COPY_AND_ASSIGN(TransportSecurityState);
340 } // namespace net
342 #endif // NET_HTTP_TRANSPORT_SECURITY_STATE_H_