blob: 18bf51ca7dd91b3e017b49b2c27795a3a9dadb05 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]326e6792009-12-11 21:04:422// 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]32b76ef2010-07-26 23:08:247#pragma once
[email protected]326e6792009-12-11 21:04:428
9#include <map>
10#include <string>
11
12#include "base/basictypes.h"
[email protected]8822f382010-07-30 21:49:0313#include "base/gtest_prod_util.h"
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/ref_counted.h"
[email protected]326e6792009-12-11 21:04:4215#include "base/time.h"
16
[email protected]326e6792009-12-11 21:04:4217namespace 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.
24class 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]326e6792009-12-11 21:04:4246
47 DomainState()
48 : mode(MODE_STRICT),
[email protected]4d0d8082010-02-23 01:03:1049 created(base::Time::Now()),
[email protected]f060be32011-02-17 17:20:2850 include_subdomains(false),
51 preloaded(false) { }
[email protected]326e6792009-12-11 21:04:4252
[email protected]4b3c95dd2011-01-07 23:02:1153 Mode mode;
[email protected]4d0d8082010-02-23 01:03:1054 base::Time created; // when this host entry was first created
[email protected]326e6792009-12-11 21:04:4255 base::Time expiry; // the absolute time (UTC) when this record expires
56 bool include_subdomains; // subdomains included?
[email protected]f060be32011-02-17 17:20:2857
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]326e6792009-12-11 21:04:4261 };
62
63 // Enable TransportSecurity for |host|.
64 void EnableHost(const std::string& host, const DomainState& state);
65
[email protected]f060be32011-02-17 17:20:2866 // 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]326e6792009-12-11 21:04:4270 // 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]4d0d8082010-02-23 01:03:1074 // Deletes all records created since a given time.
75 void DeleteSince(const base::Time& time);
76
[email protected]326e6792009-12-11 21:04:4277 // 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]dfabc132010-06-25 23:20:2990
91 protected:
92 virtual ~Delegate() {}
[email protected]326e6792009-12-11 21:04:4293 };
94
95 void SetDelegate(Delegate*);
96
97 bool Serialise(std::string* output);
[email protected]4d0d8082010-02-23 01:03:1098 bool Deserialise(const std::string& state, bool* dirty);
[email protected]326e6792009-12-11 21:04:4299
[email protected]337a4052010-11-30 15:09:33100 // The maximum number of seconds for which we'll cache an HSTS request.
101 static const long int kMaxHSTSAgeSecs;
102
[email protected]326e6792009-12-11 21:04:42103 private:
104 friend class base::RefCountedThreadSafe<TransportSecurityState>;
[email protected]8822f382010-07-30 21:49:03105 FRIEND_TEST_ALL_PREFIXES(TransportSecurityStateTest, IsPreloaded);
[email protected]326e6792009-12-11 21:04:42106
[email protected]7e4468d52010-09-22 19:42:00107 ~TransportSecurityState();
[email protected]326e6792009-12-11 21:04:42108
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]f060be32011-02-17 17:20:28113 static std::string CanonicalizeHost(const std::string& host);
114 static bool IsPreloadedSTS(const std::string& canonicalized_host,
[email protected]4b3c95dd2011-01-07 23:02:11115 bool* out_include_subdomains);
116
[email protected]326e6792009-12-11 21:04:42117 // 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]326e6792009-12-11 21:04:42122 // Our delegate who gets notified when we are dirtied, or NULL.
123 Delegate* delegate_;
124
[email protected]326e6792009-12-11 21:04:42125 DISALLOW_COPY_AND_ASSIGN(TransportSecurityState);
126};
127
128} // namespace net
129
130#endif // NET_BASE_TRANSPORT_SECURITY_STATE_H_