[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame^] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 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_BASE_TRANSPORT_SECURITY_STATE_H_ |
| 6 | #define NET_BASE_TRANSPORT_SECURITY_STATE_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 8 | |
| 9 | #include <map> |
| 10 | #include <string> |
| 11 | |
| 12 | #include "base/basictypes.h" |
[email protected] | 8822f38 | 2010-07-30 21:49:03 | [diff] [blame] | 13 | #include "base/gtest_prod_util.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame^] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 15 | #include "base/time.h" |
| 16 | |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 17 | namespace net { |
| 18 | |
| 19 | // TransportSecurityState |
| 20 | // |
| 21 | // Tracks which hosts have enabled *-Transport-Security. This object manages |
| 22 | // the in-memory store. A separate object must register itself with this object |
| 23 | // in order to persist the state to disk. |
| 24 | class TransportSecurityState : |
| 25 | public base::RefCountedThreadSafe<TransportSecurityState> { |
| 26 | public: |
| 27 | TransportSecurityState(); |
| 28 | |
| 29 | // A DomainState is the information that we persist about a given domain. |
| 30 | struct DomainState { |
| 31 | enum Mode { |
| 32 | // Strict mode implies: |
| 33 | // * We generate internal redirects from HTTP -> HTTPS. |
| 34 | // * Certificate issues are fatal. |
| 35 | MODE_STRICT = 0, |
| 36 | // Opportunistic mode implies: |
| 37 | // * We'll request HTTP URLs over HTTPS |
| 38 | // * Certificate issues are ignored. |
| 39 | MODE_OPPORTUNISTIC = 1, |
| 40 | // SPDY_ONLY (aka X-Bodge-Transport-Security) is a hopefully temporary |
| 41 | // measure. It implies: |
| 42 | // * We'll request HTTP URLs over HTTPS iff we have SPDY support. |
| 43 | // * Certificate issues are fatal. |
| 44 | MODE_SPDY_ONLY = 2, |
| 45 | }; |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 46 | |
| 47 | DomainState() |
| 48 | : mode(MODE_STRICT), |
[email protected] | 4d0d808 | 2010-02-23 01:03:10 | [diff] [blame] | 49 | created(base::Time::Now()), |
[email protected] | f060be3 | 2011-02-17 17:20:28 | [diff] [blame] | 50 | include_subdomains(false), |
| 51 | preloaded(false) { } |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 52 | |
[email protected] | 4b3c95dd | 2011-01-07 23:02:11 | [diff] [blame] | 53 | Mode mode; |
[email protected] | 4d0d808 | 2010-02-23 01:03:10 | [diff] [blame] | 54 | base::Time created; // when this host entry was first created |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 55 | base::Time expiry; // the absolute time (UTC) when this record expires |
| 56 | bool include_subdomains; // subdomains included? |
[email protected] | f060be3 | 2011-02-17 17:20:28 | [diff] [blame] | 57 | |
| 58 | // The follow members are not valid when stored in |enabled_hosts_|. |
| 59 | bool preloaded; // is this a preloaded entry? |
| 60 | std::string domain; // the domain which matched |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | // Enable TransportSecurity for |host|. |
| 64 | void EnableHost(const std::string& host, const DomainState& state); |
| 65 | |
[email protected] | f060be3 | 2011-02-17 17:20:28 | [diff] [blame] | 66 | // Delete any entry for |host|. If |host| doesn't have an exact entry then no |
| 67 | // action is taken. Returns true iff an entry was deleted. |
| 68 | bool DeleteHost(const std::string& host); |
| 69 | |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 70 | // Returns true if |host| has TransportSecurity enabled. If that case, |
| 71 | // *result is filled out. |
| 72 | bool IsEnabledForHost(DomainState* result, const std::string& host); |
| 73 | |
[email protected] | 4d0d808 | 2010-02-23 01:03:10 | [diff] [blame] | 74 | // Deletes all records created since a given time. |
| 75 | void DeleteSince(const base::Time& time); |
| 76 | |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 77 | // Returns |true| if |value| parses as a valid *-Transport-Security |
| 78 | // header value. The values of max-age and and includeSubDomains are |
| 79 | // returned in |max_age| and |include_subdomains|, respectively. The out |
| 80 | // parameters are not modified if the function returns |false|. |
| 81 | static bool ParseHeader(const std::string& value, |
| 82 | int* max_age, |
| 83 | bool* include_subdomains); |
| 84 | |
| 85 | class Delegate { |
| 86 | public: |
| 87 | // This function may not block and may be called with internal locks held. |
| 88 | // Thus it must not reenter the TransportSecurityState object. |
| 89 | virtual void StateIsDirty(TransportSecurityState* state) = 0; |
[email protected] | dfabc13 | 2010-06-25 23:20:29 | [diff] [blame] | 90 | |
| 91 | protected: |
| 92 | virtual ~Delegate() {} |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | void SetDelegate(Delegate*); |
| 96 | |
| 97 | bool Serialise(std::string* output); |
[email protected] | 4d0d808 | 2010-02-23 01:03:10 | [diff] [blame] | 98 | bool Deserialise(const std::string& state, bool* dirty); |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 99 | |
[email protected] | 337a405 | 2010-11-30 15:09:33 | [diff] [blame] | 100 | // The maximum number of seconds for which we'll cache an HSTS request. |
| 101 | static const long int kMaxHSTSAgeSecs; |
| 102 | |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 103 | private: |
| 104 | friend class base::RefCountedThreadSafe<TransportSecurityState>; |
[email protected] | 8822f38 | 2010-07-30 21:49:03 | [diff] [blame] | 105 | FRIEND_TEST_ALL_PREFIXES(TransportSecurityStateTest, IsPreloaded); |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 106 | |
[email protected] | 7e4468d5 | 2010-09-22 19:42:00 | [diff] [blame] | 107 | ~TransportSecurityState(); |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 108 | |
| 109 | // If we have a callback configured, call it to let our serialiser know that |
| 110 | // our state is dirty. |
| 111 | void DirtyNotify(); |
| 112 | |
[email protected] | f060be3 | 2011-02-17 17:20:28 | [diff] [blame] | 113 | static std::string CanonicalizeHost(const std::string& host); |
| 114 | static bool IsPreloadedSTS(const std::string& canonicalized_host, |
[email protected] | 4b3c95dd | 2011-01-07 23:02:11 | [diff] [blame] | 115 | bool* out_include_subdomains); |
| 116 | |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 117 | // The set of hosts that have enabled TransportSecurity. The keys here |
| 118 | // are SHA256(DNSForm(domain)) where DNSForm converts from dotted form |
| 119 | // ('www.google.com') to the form used in DNS: "\x03www\x06google\x03com" |
| 120 | std::map<std::string, DomainState> enabled_hosts_; |
| 121 | |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 122 | // Our delegate who gets notified when we are dirtied, or NULL. |
| 123 | Delegate* delegate_; |
| 124 | |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 125 | DISALLOW_COPY_AND_ASSIGN(TransportSecurityState); |
| 126 | }; |
| 127 | |
| 128 | } // namespace net |
| 129 | |
| 130 | #endif // NET_BASE_TRANSPORT_SECURITY_STATE_H_ |