Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / http / transport_security_state.h
blob2f97d9d34e5a6a20be27b1bcb1b96bbbad56c187
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"
20 #include "url/gurl.h"
22 namespace net {
24 class SSLInfo;
26 // Tracks which hosts have enabled strict transport security and/or public
27 // key pins.
29 // This object manages the in-memory store. Register a Delegate with
30 // |SetDelegate| to persist the state to disk.
32 // HTTP strict transport security (HSTS) is defined in
33 // http://tools.ietf.org/html/ietf-websec-strict-transport-sec, and
34 // HTTP-based dynamic public key pinning (HPKP) is defined in
35 // http://tools.ietf.org/html/ietf-websec-key-pinning.
36 class NET_EXPORT TransportSecurityState
37 : NON_EXPORTED_BASE(public base::NonThreadSafe) {
38 public:
39 class NET_EXPORT Delegate {
40 public:
41 // This function may not block and may be called with internal locks held.
42 // Thus it must not reenter the TransportSecurityState object.
43 virtual void StateIsDirty(TransportSecurityState* state) = 0;
45 protected:
46 virtual ~Delegate() {}
49 TransportSecurityState();
50 ~TransportSecurityState();
52 // A STSState describes the strict transport security state (required
53 // upgrade to HTTPS).
54 class NET_EXPORT STSState {
55 public:
56 enum UpgradeMode {
57 // These numbers must match those in hsts_view.js, function modeToString.
58 MODE_FORCE_HTTPS = 0,
59 MODE_DEFAULT = 1,
62 STSState();
63 ~STSState();
65 // The absolute time (UTC) when the |upgrade_mode| (and other state) was
66 // observed.
67 base::Time last_observed;
69 // The absolute time (UTC) when |upgrade_mode| (and other state)
70 // expires.
71 base::Time expiry;
73 UpgradeMode upgrade_mode;
75 // Are subdomains subject to this policy state?
76 bool include_subdomains;
78 // The domain which matched during a search for this STSState entry.
79 // Updated by |GetDynamicSTSState| and |GetStaticDomainState|.
80 std::string domain;
82 // ShouldUpgradeToSSL returns true iff HTTP requests should be internally
83 // redirected to HTTPS (also if WS should be upgraded to WSS).
84 bool ShouldUpgradeToSSL() const;
86 // ShouldSSLErrorsBeFatal returns true iff HTTPS errors should cause
87 // hard-fail behavior (e.g. if HSTS is set for the domain).
88 bool ShouldSSLErrorsBeFatal() const;
91 class NET_EXPORT STSStateIterator {
92 public:
93 explicit STSStateIterator(const TransportSecurityState& state);
94 ~STSStateIterator();
96 bool HasNext() const { return iterator_ != end_; }
97 void Advance() { ++iterator_; }
98 const std::string& hostname() const { return iterator_->first; }
99 const STSState& domain_state() const { return iterator_->second; }
101 private:
102 std::map<std::string, STSState>::const_iterator iterator_;
103 std::map<std::string, STSState>::const_iterator end_;
106 // A PKPState describes the public key pinning state.
107 class NET_EXPORT PKPState {
108 public:
109 PKPState();
110 ~PKPState();
112 // The absolute time (UTC) when the |spki_hashes| (and other state) were
113 // observed.
114 base::Time last_observed;
116 // The absolute time (UTC) when the |spki_hashes| expire.
117 base::Time expiry;
119 // Optional; hashes of pinned SubjectPublicKeyInfos.
120 HashValueVector spki_hashes;
122 // Optional; hashes of static known-bad SubjectPublicKeyInfos which MUST
123 // NOT intersect with the set of SPKIs in the TLS server's certificate
124 // chain.
125 HashValueVector bad_spki_hashes;
127 // Are subdomains subject to this policy state?
128 bool include_subdomains;
130 // The domain which matched during a search for this DomainState entry.
131 // Updated by |GetDynamicPKPState| and |GetStaticDomainState|.
132 std::string domain;
134 // An optional URI indicating where reports should be sent when this
135 // pin is violated, or empty when omitted.
136 GURL report_uri;
138 // Takes a set of SubjectPublicKeyInfo |hashes| and returns true if:
139 // 1) |bad_static_spki_hashes| does not intersect |hashes|; AND
140 // 2) Both |static_spki_hashes| and |dynamic_spki_hashes| are empty
141 // or at least one of them intersects |hashes|.
143 // |{dynamic,static}_spki_hashes| contain trustworthy public key hashes,
144 // any one of which is sufficient to validate the certificate chain in
145 // question. The public keys could be of a root CA, intermediate CA, or
146 // leaf certificate, depending on the security vs. disaster recovery
147 // tradeoff selected. (Pinning only to leaf certifiates increases
148 // security because you no longer trust any CAs, but it hampers disaster
149 // recovery because you can't just get a new certificate signed by the
150 // CA.)
152 // |bad_static_spki_hashes| contains public keys that we don't want to
153 // trust.
154 bool CheckPublicKeyPins(const HashValueVector& hashes,
155 std::string* failure_log) const;
157 // Returns true if any of the HashValueVectors |static_spki_hashes|,
158 // |bad_static_spki_hashes|, or |dynamic_spki_hashes| contains any
159 // items.
160 bool HasPublicKeyPins() const;
162 // ShouldSSLErrorsBeFatal returns true iff HTTPS errors should cause
163 // hard-fail behavior (e.g. if HSTS is set for the domain).
164 bool ShouldSSLErrorsBeFatal() const;
167 class NET_EXPORT PKPStateIterator {
168 public:
169 explicit PKPStateIterator(const TransportSecurityState& state);
170 ~PKPStateIterator();
172 bool HasNext() const { return iterator_ != end_; }
173 void Advance() { ++iterator_; }
174 const std::string& hostname() const { return iterator_->first; }
175 const PKPState& domain_state() const { return iterator_->second; }
177 private:
178 std::map<std::string, PKPState>::const_iterator iterator_;
179 std::map<std::string, PKPState>::const_iterator end_;
182 // These functions search for static and dynamic STS and PKP states, and
183 // invoke the
184 // functions of the same name on them. These functions are the primary public
185 // interface; direct access to STS and PKP states is best left to tests.
186 bool ShouldSSLErrorsBeFatal(const std::string& host);
187 bool ShouldUpgradeToSSL(const std::string& host);
188 bool CheckPublicKeyPins(const std::string& host,
189 bool is_issued_by_known_root,
190 const HashValueVector& hashes,
191 std::string* failure_log);
192 bool HasPublicKeyPins(const std::string& host);
194 // Assign a |Delegate| for persisting the transport security state. If
195 // |NULL|, state will not be persisted. The caller retains
196 // ownership of |delegate|.
197 // Note: This is only used for serializing/deserializing the
198 // TransportSecurityState.
199 void SetDelegate(Delegate* delegate);
201 // Clears all dynamic data (e.g. HSTS and HPKP data).
203 // Does NOT persist changes using the Delegate, as this function is only
204 // used to clear any dynamic data prior to re-loading it from a file.
205 // Note: This is only used for serializing/deserializing the
206 // TransportSecurityState.
207 void ClearDynamicData();
209 // Inserts |state| into |enabled_sts_hosts_| under the key |hashed_host|.
210 // |hashed_host| is already in the internal representation.
211 // Note: This is only used for serializing/deserializing the
212 // TransportSecurityState.
213 void AddOrUpdateEnabledSTSHosts(const std::string& hashed_host,
214 const STSState& state);
216 // Inserts |state| into |enabled_pkp_hosts_| under the key |hashed_host|.
217 // |hashed_host| is already in the internal representation.
218 // Note: This is only used for serializing/deserializing the
219 // TransportSecurityState.
220 void AddOrUpdateEnabledPKPHosts(const std::string& hashed_host,
221 const PKPState& state);
223 // Deletes all dynamic data (e.g. HSTS or HPKP data) created since a given
224 // time.
226 // If any entries are deleted, the new state will be persisted through
227 // the Delegate (if any).
228 void DeleteAllDynamicDataSince(const base::Time& time);
230 // Deletes any dynamic data stored for |host| (e.g. HSTS or HPKP data).
231 // If |host| doesn't have an exact entry then no action is taken. Does
232 // not delete static (i.e. preloaded) data. Returns true iff an entry
233 // was deleted.
235 // If an entry is deleted, the new state will be persisted through
236 // the Delegate (if any).
237 bool DeleteDynamicDataForHost(const std::string& host);
239 // Returns true and updates |*sts_result| and |*pkp_result| iff there is a
240 // static (built-in) state for |host|. If multiple entries match |host|,
241 // the most specific match determines the return value.
242 bool GetStaticDomainState(const std::string& host,
243 STSState* sts_result,
244 PKPState* pkp_result) const;
246 // Returns true and updates |*result| iff |host| has HSTS (respectively, HPKP)
247 // state. If multiple HSTS (respectively, HPKP) entries match |host|, the
248 // most specific match determines the HSTS (respectively, HPKP) return value.
250 // Note that these methods are not const because they opportunistically remove
251 // entries that have expired.
252 bool GetDynamicSTSState(const std::string& host, STSState* result);
253 bool GetDynamicPKPState(const std::string& host, PKPState* result);
255 // Processes an HSTS header value from the host, adding entries to
256 // dynamic state if necessary.
257 bool AddHSTSHeader(const std::string& host, const std::string& value);
259 // Processes an HPKP header value from the host, adding entries to
260 // dynamic state if necessary. ssl_info is used to check that
261 // the specified pins overlap with the certificate chain.
262 bool AddHPKPHeader(const std::string& host, const std::string& value,
263 const SSLInfo& ssl_info);
265 // Adds explicitly-specified data as if it was processed from an
266 // HSTS header (used for net-internals and unit tests).
267 void AddHSTS(const std::string& host,
268 const base::Time& expiry,
269 bool include_subdomains);
271 // Adds explicitly-specified data as if it was processed from an
272 // HPKP header (used for net-internals and unit tests).
273 void AddHPKP(const std::string& host,
274 const base::Time& expiry,
275 bool include_subdomains,
276 const HashValueVector& hashes,
277 const GURL& report_uri);
279 // Returns true iff we have any static public key pins for the |host| and
280 // iff its set of required pins is the set we expect for Google
281 // properties.
283 // If |host| matches both an exact entry and is a subdomain of another
284 // entry, the exact match determines the return value.
285 static bool IsGooglePinnedProperty(const std::string& host);
287 // The maximum number of seconds for which we'll cache an HSTS request.
288 static const long int kMaxHSTSAgeSecs;
290 private:
291 friend class TransportSecurityStateTest;
292 FRIEND_TEST_ALL_PREFIXES(HttpSecurityHeadersTest, UpdateDynamicPKPOnly);
293 FRIEND_TEST_ALL_PREFIXES(HttpSecurityHeadersTest, UpdateDynamicPKPMaxAge0);
294 FRIEND_TEST_ALL_PREFIXES(HttpSecurityHeadersTest, NoClobberPins);
296 typedef std::map<std::string, STSState> STSStateMap;
297 typedef std::map<std::string, PKPState> PKPStateMap;
299 // Send an UMA report on pin validation failure, if the host is in a
300 // statically-defined list of domains.
302 // TODO(palmer): This doesn't really belong here, and should be moved into
303 // the exactly one call site. This requires unifying |struct HSTSPreload|
304 // (an implementation detail of this class) with a more generic
305 // representation of first-class DomainStates, and exposing the preloads
306 // to the caller with |GetStaticDomainState|.
307 static void ReportUMAOnPinFailure(const std::string& host);
309 // IsBuildTimely returns true if the current build is new enough ensure that
310 // built in security information (i.e. HSTS preloading and pinning
311 // information) is timely.
312 static bool IsBuildTimely();
314 // Helper method for actually checking pins.
315 bool CheckPublicKeyPinsImpl(const std::string& host,
316 const HashValueVector& hashes,
317 std::string* failure_log);
319 // If a Delegate is present, notify it that the internal state has
320 // changed.
321 void DirtyNotify();
323 // Adds HSTS state to |host|.
324 void AddHSTSInternal(const std::string& host,
325 STSState::UpgradeMode upgrade_mode,
326 const base::Time& expiry,
327 bool include_subdomains);
329 // Adds HPKP state to |host|.
330 void AddHPKPInternal(const std::string& host,
331 const base::Time& last_observed,
332 const base::Time& expiry,
333 bool include_subdomains,
334 const HashValueVector& hashes,
335 const GURL& report_uri);
337 // Enable TransportSecurity for |host|. |state| supercedes any previous
338 // state for the |host|, including static entries.
340 // The new state for |host| is persisted using the Delegate (if any).
341 void EnableSTSHost(const std::string& host, const STSState& state);
342 void EnablePKPHost(const std::string& host, const PKPState& state);
344 // The sets of hosts that have enabled TransportSecurity. |domain| will always
345 // be empty for a STSState or PKPState in these maps; the domain
346 // comes from the map keys instead. In addition, |upgrade_mode| in the
347 // STSState is never MODE_DEFAULT and |HasPublicKeyPins| in the PKPState
348 // always returns true.
349 STSStateMap enabled_sts_hosts_;
350 PKPStateMap enabled_pkp_hosts_;
352 Delegate* delegate_;
354 // True if static pins should be used.
355 bool enable_static_pins_;
357 DISALLOW_COPY_AND_ASSIGN(TransportSecurityState);
360 } // namespace net
362 #endif // NET_HTTP_TRANSPORT_SECURITY_STATE_H_