• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/http/transport_security_persister.h"
6 
7 #include <algorithm>
8 #include <cstdint>
9 #include <memory>
10 #include <utility>
11 #include <vector>
12 
13 #include "base/base64.h"
14 #include "base/feature_list.h"
15 #include "base/files/file_path.h"
16 #include "base/files/file_util.h"
17 #include "base/functional/bind.h"
18 #include "base/functional/callback.h"
19 #include "base/json/json_reader.h"
20 #include "base/json/json_writer.h"
21 #include "base/location.h"
22 #include "base/task/sequenced_task_runner.h"
23 #include "base/task/single_thread_task_runner.h"
24 #include "base/values.h"
25 #include "net/base/features.h"
26 #include "net/base/network_anonymization_key.h"
27 #include "net/cert/x509_certificate.h"
28 #include "net/http/transport_security_state.h"
29 #include "third_party/abseil-cpp/absl/types/optional.h"
30 
31 namespace net {
32 
33 namespace {
34 
35 // This function converts the binary hashes to a base64 string which we can
36 // include in a JSON file.
HashedDomainToExternalString(const TransportSecurityState::HashedHost & hashed)37 std::string HashedDomainToExternalString(
38     const TransportSecurityState::HashedHost& hashed) {
39   return base::Base64Encode(hashed);
40 }
41 
42 // This inverts |HashedDomainToExternalString|, above. It turns an external
43 // string (from a JSON file) into an internal (binary) array.
ExternalStringToHashedDomain(const std::string & external)44 absl::optional<TransportSecurityState::HashedHost> ExternalStringToHashedDomain(
45     const std::string& external) {
46   TransportSecurityState::HashedHost out;
47   absl::optional<std::vector<uint8_t>> hashed = base::Base64Decode(external);
48   if (!hashed.has_value() || hashed.value().size() != out.size()) {
49     return absl::nullopt;
50   }
51 
52   std::copy_n(hashed.value().begin(), out.size(), out.begin());
53   return out;
54 }
55 
56 // Version 2 of the on-disk format consists of a single JSON object. The
57 // top-level dictionary has "version", "sts", and "expect_ct" entries. The first
58 // is an integer, the latter two are unordered lists of dictionaries, each
59 // representing cached data for a single host.
60 
61 // Stored in serialized dictionary values to distinguish incompatible versions.
62 // Version 1 is distinguished by the lack of an integer version value.
63 const char kVersionKey[] = "version";
64 const int kCurrentVersionValue = 2;
65 
66 // Keys in top level serialized dictionary, for lists of STS and Expect-CT
67 // entries, respectively. The Expect-CT key is legacy and deleted when read.
68 const char kSTSKey[] = "sts";
69 const char kExpectCTKey[] = "expect_ct";
70 
71 // Hostname entry, used in serialized STS dictionaries. Value is produced by
72 // passing hashed hostname strings to HashedDomainToExternalString().
73 const char kHostname[] = "host";
74 
75 // Key values in serialized STS entries.
76 const char kStsIncludeSubdomains[] = "sts_include_subdomains";
77 const char kStsObserved[] = "sts_observed";
78 const char kExpiry[] = "expiry";
79 const char kMode[] = "mode";
80 
81 // Values for "mode" used in serialized STS entries.
82 const char kForceHTTPS[] = "force-https";
83 const char kDefault[] = "default";
84 
LoadState(const base::FilePath & path)85 std::string LoadState(const base::FilePath& path) {
86   std::string result;
87   if (!base::ReadFileToString(path, &result)) {
88     return "";
89   }
90   return result;
91 }
92 
93 // Serializes STS data from |state| to a Value.
SerializeSTSData(const TransportSecurityState * state)94 base::Value::List SerializeSTSData(const TransportSecurityState* state) {
95   base::Value::List sts_list;
96 
97   TransportSecurityState::STSStateIterator sts_iterator(*state);
98   for (; sts_iterator.HasNext(); sts_iterator.Advance()) {
99     const TransportSecurityState::STSState& sts_state =
100         sts_iterator.domain_state();
101 
102     base::Value::Dict serialized;
103     serialized.Set(kHostname,
104                    HashedDomainToExternalString(sts_iterator.hostname()));
105     serialized.Set(kStsIncludeSubdomains, sts_state.include_subdomains);
106     serialized.Set(kStsObserved, sts_state.last_observed.ToDoubleT());
107     serialized.Set(kExpiry, sts_state.expiry.ToDoubleT());
108 
109     switch (sts_state.upgrade_mode) {
110       case TransportSecurityState::STSState::MODE_FORCE_HTTPS:
111         serialized.Set(kMode, kForceHTTPS);
112         break;
113       case TransportSecurityState::STSState::MODE_DEFAULT:
114         serialized.Set(kMode, kDefault);
115         break;
116     }
117 
118     sts_list.Append(std::move(serialized));
119   }
120   return sts_list;
121 }
122 
123 // Deserializes STS data from a Value created by the above method.
DeserializeSTSData(const base::Value & sts_list,TransportSecurityState * state)124 void DeserializeSTSData(const base::Value& sts_list,
125                         TransportSecurityState* state) {
126   if (!sts_list.is_list())
127     return;
128 
129   base::Time current_time(base::Time::Now());
130 
131   for (const base::Value& sts_entry : sts_list.GetList()) {
132     const base::Value::Dict* sts_dict = sts_entry.GetIfDict();
133     if (!sts_dict)
134       continue;
135 
136     const std::string* hostname = sts_dict->FindString(kHostname);
137     absl::optional<bool> sts_include_subdomains =
138         sts_dict->FindBool(kStsIncludeSubdomains);
139     absl::optional<double> sts_observed = sts_dict->FindDouble(kStsObserved);
140     absl::optional<double> expiry = sts_dict->FindDouble(kExpiry);
141     const std::string* mode = sts_dict->FindString(kMode);
142 
143     if (!hostname || !sts_include_subdomains.has_value() ||
144         !sts_observed.has_value() || !expiry.has_value() || !mode) {
145       continue;
146     }
147 
148     TransportSecurityState::STSState sts_state;
149     sts_state.include_subdomains = *sts_include_subdomains;
150     sts_state.last_observed = base::Time::FromDoubleT(*sts_observed);
151     sts_state.expiry = base::Time::FromDoubleT(*expiry);
152 
153     if (*mode == kForceHTTPS) {
154       sts_state.upgrade_mode =
155           TransportSecurityState::STSState::MODE_FORCE_HTTPS;
156     } else if (*mode == kDefault) {
157       sts_state.upgrade_mode = TransportSecurityState::STSState::MODE_DEFAULT;
158     } else {
159       continue;
160     }
161 
162     if (sts_state.expiry < current_time || !sts_state.ShouldUpgradeToSSL())
163       continue;
164 
165     absl::optional<TransportSecurityState::HashedHost> hashed =
166         ExternalStringToHashedDomain(*hostname);
167     if (!hashed.has_value())
168       continue;
169 
170     state->AddOrUpdateEnabledSTSHosts(hashed.value(), sts_state);
171   }
172 }
173 
OnWriteFinishedTask(scoped_refptr<base::SequencedTaskRunner> task_runner,base::OnceClosure callback,bool result)174 void OnWriteFinishedTask(scoped_refptr<base::SequencedTaskRunner> task_runner,
175                          base::OnceClosure callback,
176                          bool result) {
177   task_runner->PostTask(FROM_HERE, std::move(callback));
178 }
179 
180 }  // namespace
181 
TransportSecurityPersister(TransportSecurityState * state,const scoped_refptr<base::SequencedTaskRunner> & background_runner,const base::FilePath & data_path)182 TransportSecurityPersister::TransportSecurityPersister(
183     TransportSecurityState* state,
184     const scoped_refptr<base::SequencedTaskRunner>& background_runner,
185     const base::FilePath& data_path)
186     : transport_security_state_(state),
187       writer_(data_path, background_runner),
188       foreground_runner_(base::SingleThreadTaskRunner::GetCurrentDefault()),
189       background_runner_(background_runner) {
190   transport_security_state_->SetDelegate(this);
191 
192   background_runner_->PostTaskAndReplyWithResult(
193       FROM_HERE, base::BindOnce(&LoadState, writer_.path()),
194       base::BindOnce(&TransportSecurityPersister::CompleteLoad,
195                      weak_ptr_factory_.GetWeakPtr()));
196 }
197 
~TransportSecurityPersister()198 TransportSecurityPersister::~TransportSecurityPersister() {
199   DCHECK(foreground_runner_->RunsTasksInCurrentSequence());
200 
201   if (writer_.HasPendingWrite())
202     writer_.DoScheduledWrite();
203 
204   transport_security_state_->SetDelegate(nullptr);
205 }
206 
StateIsDirty(TransportSecurityState * state)207 void TransportSecurityPersister::StateIsDirty(TransportSecurityState* state) {
208   DCHECK(foreground_runner_->RunsTasksInCurrentSequence());
209   DCHECK_EQ(transport_security_state_, state);
210 
211   writer_.ScheduleWrite(this);
212 }
213 
WriteNow(TransportSecurityState * state,base::OnceClosure callback)214 void TransportSecurityPersister::WriteNow(TransportSecurityState* state,
215                                           base::OnceClosure callback) {
216   DCHECK(foreground_runner_->RunsTasksInCurrentSequence());
217   DCHECK_EQ(transport_security_state_, state);
218 
219   writer_.RegisterOnNextWriteCallbacks(
220       base::OnceClosure(),
221       base::BindOnce(
222           &OnWriteFinishedTask, foreground_runner_,
223           base::BindOnce(&TransportSecurityPersister::OnWriteFinished,
224                          weak_ptr_factory_.GetWeakPtr(), std::move(callback))));
225   absl::optional<std::string> data = SerializeData();
226   if (data) {
227     writer_.WriteNow(std::move(data).value());
228   } else {
229     writer_.WriteNow(std::string());
230   }
231 }
232 
OnWriteFinished(base::OnceClosure callback)233 void TransportSecurityPersister::OnWriteFinished(base::OnceClosure callback) {
234   DCHECK(foreground_runner_->RunsTasksInCurrentSequence());
235   std::move(callback).Run();
236 }
237 
SerializeData()238 absl::optional<std::string> TransportSecurityPersister::SerializeData() {
239   CHECK(foreground_runner_->RunsTasksInCurrentSequence());
240 
241   base::Value::Dict toplevel;
242   toplevel.Set(kVersionKey, kCurrentVersionValue);
243   toplevel.Set(kSTSKey, SerializeSTSData(transport_security_state_));
244 
245   std::string output;
246   if (!base::JSONWriter::Write(toplevel, &output)) {
247     return absl::nullopt;
248   }
249   return output;
250 }
251 
LoadEntries(const std::string & serialized)252 void TransportSecurityPersister::LoadEntries(const std::string& serialized) {
253   DCHECK(foreground_runner_->RunsTasksInCurrentSequence());
254 
255   transport_security_state_->ClearDynamicData();
256   bool contains_legacy_expect_ct_data = false;
257   Deserialize(serialized, transport_security_state_,
258               contains_legacy_expect_ct_data);
259   if (contains_legacy_expect_ct_data) {
260     StateIsDirty(transport_security_state_);
261   }
262 }
263 
Deserialize(const std::string & serialized,TransportSecurityState * state,bool & contains_legacy_expect_ct_data)264 void TransportSecurityPersister::Deserialize(
265     const std::string& serialized,
266     TransportSecurityState* state,
267     bool& contains_legacy_expect_ct_data) {
268   absl::optional<base::Value> value = base::JSONReader::Read(serialized);
269   if (!value || !value->is_dict())
270     return;
271 
272   base::Value::Dict& dict = value->GetDict();
273   absl::optional<int> version = dict.FindInt(kVersionKey);
274 
275   // Stop if the data is out of date (or in the previous format that didn't have
276   // a version number).
277   if (!version || *version != kCurrentVersionValue)
278     return;
279 
280   base::Value* sts_value = dict.Find(kSTSKey);
281   if (sts_value)
282     DeserializeSTSData(*sts_value, state);
283 
284   // If an Expect-CT key is found on deserialization, record this so that a
285   // write can be scheduled to clear it from disk.
286   contains_legacy_expect_ct_data = !!dict.Find(kExpectCTKey);
287 }
288 
CompleteLoad(const std::string & state)289 void TransportSecurityPersister::CompleteLoad(const std::string& state) {
290   DCHECK(foreground_runner_->RunsTasksInCurrentSequence());
291 
292   if (state.empty())
293     return;
294 
295   LoadEntries(state);
296 }
297 
298 }  // namespace net
299