| OLD | NEW |
| 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 #include "chrome/browser/net/transport_security_persister.h" | 5 #include "chrome/browser/net/transport_security_persister.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/path_service.h" | 14 #include "base/message_loop/message_loop_proxy.h" |
| 15 #include "base/sequenced_task_runner.h" |
| 16 #include "base/task_runner_util.h" |
| 15 #include "base/values.h" | 17 #include "base/values.h" |
| 16 #include "chrome/common/chrome_paths.h" | |
| 17 #include "content/public/browser/browser_thread.h" | |
| 18 #include "crypto/sha2.h" | 18 #include "crypto/sha2.h" |
| 19 #include "net/cert/x509_certificate.h" | 19 #include "net/cert/x509_certificate.h" |
| 20 #include "net/http/transport_security_state.h" | 20 #include "net/http/transport_security_state.h" |
| 21 | 21 |
| 22 using content::BrowserThread; | |
| 23 using net::HashValue; | 22 using net::HashValue; |
| 24 using net::HashValueTag; | 23 using net::HashValueTag; |
| 25 using net::HashValueVector; | 24 using net::HashValueVector; |
| 26 using net::TransportSecurityState; | 25 using net::TransportSecurityState; |
| 27 | 26 |
| 28 namespace { | 27 namespace { |
| 29 | 28 |
| 30 ListValue* SPKIHashesToListValue(const HashValueVector& hashes) { | 29 ListValue* SPKIHashesToListValue(const HashValueVector& hashes) { |
| 31 ListValue* pins = new ListValue; | 30 ListValue* pins = new ListValue; |
| 32 for (size_t i = 0; i != hashes.size(); i++) | 31 for (size_t i = 0; i != hashes.size(); i++) |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 const char kDynamicSPKIHashesExpiry[] = "dynamic_spki_hashes_expiry"; | 73 const char kDynamicSPKIHashesExpiry[] = "dynamic_spki_hashes_expiry"; |
| 75 const char kStaticSPKIHashes[] = "static_spki_hashes"; | 74 const char kStaticSPKIHashes[] = "static_spki_hashes"; |
| 76 const char kPreloadedSPKIHashes[] = "preloaded_spki_hashes"; | 75 const char kPreloadedSPKIHashes[] = "preloaded_spki_hashes"; |
| 77 const char kDynamicSPKIHashes[] = "dynamic_spki_hashes"; | 76 const char kDynamicSPKIHashes[] = "dynamic_spki_hashes"; |
| 78 const char kForceHTTPS[] = "force-https"; | 77 const char kForceHTTPS[] = "force-https"; |
| 79 const char kStrict[] = "strict"; | 78 const char kStrict[] = "strict"; |
| 80 const char kDefault[] = "default"; | 79 const char kDefault[] = "default"; |
| 81 const char kPinningOnly[] = "pinning-only"; | 80 const char kPinningOnly[] = "pinning-only"; |
| 82 const char kCreated[] = "created"; | 81 const char kCreated[] = "created"; |
| 83 | 82 |
| 83 std::string LoadState(const base::FilePath& path) { |
| 84 std::string result; |
| 85 if (!base::ReadFileToString(path, &result)) { |
| 86 return ""; |
| 87 } |
| 88 return result; |
| 89 } |
| 90 |
| 84 } // namespace | 91 } // namespace |
| 85 | 92 |
| 86 class TransportSecurityPersister::Loader { | |
| 87 public: | |
| 88 Loader(const base::WeakPtr<TransportSecurityPersister>& persister, | |
| 89 const base::FilePath& path) | |
| 90 : persister_(persister), | |
| 91 path_(path), | |
| 92 state_valid_(false) { | |
| 93 } | |
| 94 | |
| 95 void Load() { | |
| 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 97 state_valid_ = base::ReadFileToString(path_, &state_); | |
| 98 } | |
| 99 | |
| 100 void CompleteLoad() { | |
| 101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 102 | |
| 103 // Make sure we're deleted. | |
| 104 scoped_ptr<Loader> deleter(this); | |
| 105 | |
| 106 if (!persister_.get() || !state_valid_) | |
| 107 return; | |
| 108 persister_->CompleteLoad(state_); | |
| 109 } | |
| 110 | |
| 111 private: | |
| 112 base::WeakPtr<TransportSecurityPersister> persister_; | |
| 113 | |
| 114 base::FilePath path_; | |
| 115 | |
| 116 std::string state_; | |
| 117 bool state_valid_; | |
| 118 | |
| 119 DISALLOW_COPY_AND_ASSIGN(Loader); | |
| 120 }; | |
| 121 | |
| 122 TransportSecurityPersister::TransportSecurityPersister( | 93 TransportSecurityPersister::TransportSecurityPersister( |
| 123 TransportSecurityState* state, | 94 TransportSecurityState* state, |
| 124 const base::FilePath& profile_path, | 95 const base::FilePath& profile_path, |
| 96 base::SequencedTaskRunner* background_runner, |
| 125 bool readonly) | 97 bool readonly) |
| 126 : transport_security_state_(state), | 98 : transport_security_state_(state), |
| 127 writer_(profile_path.AppendASCII("TransportSecurity"), | 99 writer_(profile_path.AppendASCII("TransportSecurity"), background_runner), |
| 128 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE) | 100 foreground_runner_(base::MessageLoop::current()->message_loop_proxy()), |
| 129 .get()), | 101 background_runner_(background_runner), |
| 130 readonly_(readonly), | 102 readonly_(readonly), |
| 131 weak_ptr_factory_(this) { | 103 weak_ptr_factory_(this) { |
| 132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 133 | |
| 134 transport_security_state_->SetDelegate(this); | 104 transport_security_state_->SetDelegate(this); |
| 135 | 105 |
| 136 Loader* loader = new Loader(weak_ptr_factory_.GetWeakPtr(), writer_.path()); | 106 base::PostTaskAndReplyWithResult( |
| 137 BrowserThread::PostTaskAndReply( | 107 background_runner_, |
| 138 BrowserThread::FILE, FROM_HERE, | 108 FROM_HERE, |
| 139 base::Bind(&Loader::Load, base::Unretained(loader)), | 109 base::Bind(&::LoadState, writer_.path()), |
| 140 base::Bind(&Loader::CompleteLoad, base::Unretained(loader))); | 110 base::Bind(&TransportSecurityPersister::CompleteLoad, |
| 111 weak_ptr_factory_.GetWeakPtr())); |
| 141 } | 112 } |
| 142 | 113 |
| 143 TransportSecurityPersister::~TransportSecurityPersister() { | 114 TransportSecurityPersister::~TransportSecurityPersister() { |
| 144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 115 DCHECK(foreground_runner_->RunsTasksOnCurrentThread()); |
| 145 | 116 |
| 146 if (writer_.HasPendingWrite()) | 117 if (writer_.HasPendingWrite()) |
| 147 writer_.DoScheduledWrite(); | 118 writer_.DoScheduledWrite(); |
| 148 | 119 |
| 149 transport_security_state_->SetDelegate(NULL); | 120 transport_security_state_->SetDelegate(NULL); |
| 150 } | 121 } |
| 151 | 122 |
| 152 void TransportSecurityPersister::StateIsDirty( | 123 void TransportSecurityPersister::StateIsDirty( |
| 153 TransportSecurityState* state) { | 124 TransportSecurityState* state) { |
| 154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 125 DCHECK(foreground_runner_->RunsTasksOnCurrentThread()); |
| 155 DCHECK_EQ(transport_security_state_, state); | 126 DCHECK_EQ(transport_security_state_, state); |
| 156 | 127 |
| 157 if (!readonly_) | 128 if (!readonly_) |
| 158 writer_.ScheduleWrite(this); | 129 writer_.ScheduleWrite(this); |
| 159 } | 130 } |
| 160 | 131 |
| 161 bool TransportSecurityPersister::SerializeData(std::string* output) { | 132 bool TransportSecurityPersister::SerializeData(std::string* output) { |
| 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 133 DCHECK(foreground_runner_->RunsTasksOnCurrentThread()); |
| 163 | 134 |
| 164 DictionaryValue toplevel; | 135 DictionaryValue toplevel; |
| 165 base::Time now = base::Time::Now(); | 136 base::Time now = base::Time::Now(); |
| 166 TransportSecurityState::Iterator state(*transport_security_state_); | 137 TransportSecurityState::Iterator state(*transport_security_state_); |
| 167 for (; state.HasNext(); state.Advance()) { | 138 for (; state.HasNext(); state.Advance()) { |
| 168 const std::string& hostname = state.hostname(); | 139 const std::string& hostname = state.hostname(); |
| 169 const TransportSecurityState::DomainState& domain_state = | 140 const TransportSecurityState::DomainState& domain_state = |
| 170 state.domain_state(); | 141 state.domain_state(); |
| 171 | 142 |
| 172 DictionaryValue* serialized = new DictionaryValue; | 143 DictionaryValue* serialized = new DictionaryValue; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 } | 175 } |
| 205 | 176 |
| 206 base::JSONWriter::WriteWithOptions(&toplevel, | 177 base::JSONWriter::WriteWithOptions(&toplevel, |
| 207 base::JSONWriter::OPTIONS_PRETTY_PRINT, | 178 base::JSONWriter::OPTIONS_PRETTY_PRINT, |
| 208 output); | 179 output); |
| 209 return true; | 180 return true; |
| 210 } | 181 } |
| 211 | 182 |
| 212 bool TransportSecurityPersister::LoadEntries(const std::string& serialized, | 183 bool TransportSecurityPersister::LoadEntries(const std::string& serialized, |
| 213 bool* dirty) { | 184 bool* dirty) { |
| 214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 185 DCHECK(foreground_runner_->RunsTasksOnCurrentThread()); |
| 215 | 186 |
| 216 transport_security_state_->ClearDynamicData(); | 187 transport_security_state_->ClearDynamicData(); |
| 217 return Deserialize(serialized, dirty, transport_security_state_); | 188 return Deserialize(serialized, dirty, transport_security_state_); |
| 218 } | 189 } |
| 219 | 190 |
| 220 // static | 191 // static |
| 221 bool TransportSecurityPersister::Deserialize(const std::string& serialized, | 192 bool TransportSecurityPersister::Deserialize(const std::string& serialized, |
| 222 bool* dirty, | 193 bool* dirty, |
| 223 TransportSecurityState* state) { | 194 TransportSecurityState* state) { |
| 224 scoped_ptr<Value> value(base::JSONReader::Read(serialized)); | 195 scoped_ptr<Value> value(base::JSONReader::Read(serialized)); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 } | 291 } |
| 321 | 292 |
| 322 state->AddOrUpdateEnabledHosts(hashed, domain_state); | 293 state->AddOrUpdateEnabledHosts(hashed, domain_state); |
| 323 } | 294 } |
| 324 | 295 |
| 325 *dirty = dirtied; | 296 *dirty = dirtied; |
| 326 return true; | 297 return true; |
| 327 } | 298 } |
| 328 | 299 |
| 329 void TransportSecurityPersister::CompleteLoad(const std::string& state) { | 300 void TransportSecurityPersister::CompleteLoad(const std::string& state) { |
| 330 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 301 DCHECK(foreground_runner_->RunsTasksOnCurrentThread()); |
| 331 | 302 |
| 332 bool dirty = false; | 303 bool dirty = false; |
| 333 if (!LoadEntries(state, &dirty)) { | 304 if (!LoadEntries(state, &dirty)) { |
| 334 LOG(ERROR) << "Failed to deserialize state: " << state; | 305 LOG(ERROR) << "Failed to deserialize state: " << state; |
| 335 return; | 306 return; |
| 336 } | 307 } |
| 337 if (dirty) | 308 if (dirty) |
| 338 StateIsDirty(transport_security_state_); | 309 StateIsDirty(transport_security_state_); |
| 339 } | 310 } |
| OLD | NEW |