Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: net/http/transport_security_state.h

Issue 103803012: Make HSTS headers not clobber preloaded pins. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Respond to comments and fix compilation errors. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 // 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
64 // Takes a set of SubjectPublicKeyInfo |hashes| and returns true if: 102 // Takes a set of SubjectPublicKeyInfo |hashes| and returns true if:
65 // 1) |bad_static_spki_hashes| does not intersect |hashes|; AND 103 // 1) |bad_static_spki_hashes| does not intersect |hashes|; AND
66 // 2) Both |static_spki_hashes| and |dynamic_spki_hashes| are empty 104 // 2) Both |static_spki_hashes| and |dynamic_spki_hashes| are empty
67 // or at least one of them intersects |hashes|. 105 // or at least one of them intersects |hashes|.
68 // 106 //
69 // |{dynamic,static}_spki_hashes| contain trustworthy public key hashes, 107 // |{dynamic,static}_spki_hashes| contain trustworthy public key hashes,
70 // any one of which is sufficient to validate the certificate chain in 108 // 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 109 // question. The public keys could be of a root CA, intermediate CA, or
72 // leaf certificate, depending on the security vs. disaster recovery 110 // leaf certificate, depending on the security vs. disaster recovery
73 // tradeoff selected. (Pinning only to leaf certifiates increases 111 // tradeoff selected. (Pinning only to leaf certifiates increases
74 // security because you no longer trust any CAs, but it hampers disaster 112 // 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 113 // recovery because you can't just get a new certificate signed by the
76 // CA.) 114 // CA.)
77 // 115 //
78 // |bad_static_spki_hashes| contains public keys that we don't want to 116 // |bad_static_spki_hashes| contains public keys that we don't want to
79 // trust. 117 // trust.
80 bool CheckPublicKeyPins(const HashValueVector& hashes, 118 bool CheckPublicKeyPins(const HashValueVector& hashes,
81 std::string* failure_log) const; 119 std::string* failure_log) const;
82 120
83 // Returns true if any of the HashValueVectors |static_spki_hashes|, 121 // Returns true if any of the HashValueVectors |static_spki_hashes|,
84 // |bad_static_spki_hashes|, or |dynamic_spki_hashes| contains any 122 // |bad_static_spki_hashes|, or |dynamic_spki_hashes| contains any
85 // items. 123 // items.
86 bool HasPublicKeyPins() const; 124 bool HasPublicKeyPins() const;
87 125
88 // ShouldUpgradeToSSL returns true iff, given the |mode| of this 126 // ShouldUpgradeToSSL returns true iff HTTP requests should be internally
89 // DomainState, HTTP requests should be internally redirected to HTTPS 127 // 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; 128 bool ShouldUpgradeToSSL() const;
92 129
93 // ShouldSSLErrorsBeFatal returns true iff HTTPS errors should cause 130 // ShouldSSLErrorsBeFatal returns true iff HTTPS errors should cause
94 // hard-fail behavior (e.g. if HSTS is set for the domain) 131 // hard-fail behavior (e.g. if HSTS is set for the domain).
95 bool ShouldSSLErrorsBeFatal() const; 132 bool ShouldSSLErrorsBeFatal() const;
96 133
97 UpgradeMode upgrade_mode; 134 STSState sts;
98 135 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 136
143 // The following members are not valid when stored in |enabled_hosts_|: 137 // The following members are not valid when stored in |enabled_hosts_|:
144 138
145 // The domain which matched during a search for this DomainState entry. 139 // The domain which matched during a search for this DomainState entry.
146 // Updated by |GetDomainState|, |GetDynamicDomainState|, and 140 // Updated by |GetDynamicDomainState| and |GetStaticDomainState|.
147 // |GetStaticDomainState|.
148 std::string domain; 141 std::string domain;
149 }; 142 };
150 143
151 class NET_EXPORT Iterator { 144 class NET_EXPORT Iterator {
152 public: 145 public:
153 explicit Iterator(const TransportSecurityState& state); 146 explicit Iterator(const TransportSecurityState& state);
154 ~Iterator(); 147 ~Iterator();
155 148
156 bool HasNext() const { return iterator_ != end_; } 149 bool HasNext() const { return iterator_ != end_; }
157 void Advance() { ++iterator_; } 150 void Advance() { ++iterator_; }
158 const std::string& hostname() const { return iterator_->first; } 151 const std::string& hostname() const { return iterator_->first; }
159 const DomainState& domain_state() const { return iterator_->second; } 152 const DomainState& domain_state() const { return iterator_->second; }
160 153
161 private: 154 private:
162 std::map<std::string, DomainState>::const_iterator iterator_; 155 std::map<std::string, DomainState>::const_iterator iterator_;
163 std::map<std::string, DomainState>::const_iterator end_; 156 std::map<std::string, DomainState>::const_iterator end_;
164 }; 157 };
165 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, bool sni_enabled);
163 bool ShouldUpgradeToSSL(const std::string& host, bool sni_enabled);
164 bool CheckPublicKeyPins(const std::string& host,
165 bool sni_enabled,
166 const HashValueVector& hashes,
167 std::string* failure_log);
168 bool HasPublicKeyPins(const std::string& host, bool sni_enabled);
169
166 // Assign a |Delegate| for persisting the transport security state. If 170 // Assign a |Delegate| for persisting the transport security state. If
167 // |NULL|, state will not be persisted. The caller retains 171 // |NULL|, state will not be persisted. The caller retains
168 // ownership of |delegate|. 172 // ownership of |delegate|.
169 // Note: This is only used for serializing/deserializing the 173 // Note: This is only used for serializing/deserializing the
170 // TransportSecurityState. 174 // TransportSecurityState.
171 void SetDelegate(Delegate* delegate); 175 void SetDelegate(Delegate* delegate);
172 176
173 // Clears all dynamic data (e.g. HSTS and HPKP data). 177 // Clears all dynamic data (e.g. HSTS and HPKP data).
174 // 178 //
175 // Does NOT persist changes using the Delegate, as this function is only 179 // Does NOT persist changes using the Delegate, as this function is only
(...skipping 19 matching lines...) Expand all
195 199
196 // Deletes any dynamic data stored for |host| (e.g. HSTS or HPKP data). 200 // Deletes any dynamic data stored for |host| (e.g. HSTS or HPKP data).
197 // If |host| doesn't have an exact entry then no action is taken. Does 201 // If |host| doesn't have an exact entry then no action is taken. Does
198 // not delete static (i.e. preloaded) data. Returns true iff an entry 202 // not delete static (i.e. preloaded) data. Returns true iff an entry
199 // was deleted. 203 // was deleted.
200 // 204 //
201 // If an entry is deleted, the new state will be persisted through 205 // If an entry is deleted, the new state will be persisted through
202 // the Delegate (if any). 206 // the Delegate (if any).
203 bool DeleteDynamicDataForHost(const std::string& host); 207 bool DeleteDynamicDataForHost(const std::string& host);
204 208
205 // Returns true and updates |*result| iff there is a DomainState for 209 // Returns true and updates |*result| iff there is a static (built-in)
206 // |host|. 210 // DomainState for |host|.
207 // 211 //
208 // If |sni_enabled| is true, searches the static pins defined for 212 // If |sni_enabled| is true, searches the static pins defined for SNI-using
209 // SNI-using hosts as well as the rest of the pins. 213 // hosts as well as the rest of the pins.
210 // 214 //
211 // If |host| matches both an exact entry and is a subdomain of another 215 // If |host| matches both an exact entry and is a subdomain of another entry,
212 // entry, the exact match determines the return value. 216 // the exact match determines the return value.
213 // 217 //
214 // Note that this method is not const because it opportunistically removes 218 // Note that this method is not const because it opportunistically removes
215 // entries that have expired. 219 // entries that have expired.
216 bool GetDomainState(const std::string& host, 220 bool GetStaticDomainState(const std::string& host,
217 bool sni_enabled, 221 bool sni_enabled,
218 DomainState* result); 222 DomainState* result) const;
223
224 // Returns true and updates |*result| iff there is a dynamic DomainState
225 // (learned from HSTS or HPKP headers, or set by the user, or other means) for
226 // |host|.
227 //
228 // If |host| matches both an exact entry and is a subdomain of another entry,
229 // the exact match determines the return value.
230 //
231 // Note that this method is not const because it opportunistically removes
232 // entries that have expired.
233 bool GetDynamicDomainState(const std::string& host, DomainState* result);
219 234
220 // Processes an HSTS header value from the host, adding entries to 235 // Processes an HSTS header value from the host, adding entries to
221 // dynamic state if necessary. 236 // dynamic state if necessary.
222 bool AddHSTSHeader(const std::string& host, const std::string& value); 237 bool AddHSTSHeader(const std::string& host, const std::string& value);
223 238
224 // Processes an HPKP header value from the host, adding entries to 239 // Processes an HPKP header value from the host, adding entries to
225 // dynamic state if necessary. ssl_info is used to check that 240 // dynamic state if necessary. ssl_info is used to check that
226 // the specified pins overlap with the certificate chain. 241 // the specified pins overlap with the certificate chain.
227 bool AddHPKPHeader(const std::string& host, const std::string& value, 242 bool AddHPKPHeader(const std::string& host, const std::string& value,
228 const SSLInfo& ssl_info); 243 const SSLInfo& ssl_info);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 // state for the |host|, including static entries. 297 // state for the |host|, including static entries.
283 // 298 //
284 // The new state for |host| is persisted using the Delegate (if any). 299 // The new state for |host| is persisted using the Delegate (if any).
285 void EnableHost(const std::string& host, const DomainState& state); 300 void EnableHost(const std::string& host, const DomainState& state);
286 301
287 // Converts |hostname| from dotted form ("www.google.com") to the form 302 // Converts |hostname| from dotted form ("www.google.com") to the form
288 // used in DNS: "\x03www\x06google\x03com", lowercases that, and returns 303 // used in DNS: "\x03www\x06google\x03com", lowercases that, and returns
289 // the result. 304 // the result.
290 static std::string CanonicalizeHost(const std::string& hostname); 305 static std::string CanonicalizeHost(const std::string& hostname);
291 306
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. 307 // The set of hosts that have enabled TransportSecurity.
326 DomainStateMap enabled_hosts_; 308 DomainStateMap enabled_hosts_;
327 309
328 Delegate* delegate_; 310 Delegate* delegate_;
329 311
330 DISALLOW_COPY_AND_ASSIGN(TransportSecurityState); 312 DISALLOW_COPY_AND_ASSIGN(TransportSecurityState);
331 }; 313 };
332 314
333 } // namespace net 315 } // namespace net
334 316
335 #endif // NET_HTTP_TRANSPORT_SECURITY_STATE_H_ 317 #endif // NET_HTTP_TRANSPORT_SECURITY_STATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698