| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chromeos/dbus/fake_cryptohome_client.h" | 5 #include "chromeos/dbus/fake_cryptohome_client.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | |
| 9 #include "base/location.h" | 8 #include "base/location.h" |
| 10 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 11 #include "base/path_service.h" | |
| 12 #include "base/threading/worker_pool.h" | |
| 13 #include "chromeos/chromeos_paths.h" | |
| 14 #include "chromeos/dbus/cryptohome/key.pb.h" | 10 #include "chromeos/dbus/cryptohome/key.pb.h" |
| 15 #include "chromeos/dbus/cryptohome/rpc.pb.h" | 11 #include "chromeos/dbus/cryptohome/rpc.pb.h" |
| 16 #include "crypto/nss_util.h" | 12 #include "crypto/nss_util.h" |
| 17 #include "third_party/cros_system_api/dbus/service_constants.h" | 13 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 18 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" | |
| 19 #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream.h" | |
| 20 #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite
.h" | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // Helper to asynchronously write a file in the WorkerPool. | |
| 25 void PersistFile(const base::FilePath& path, const std::string& content) { | |
| 26 base::WriteFile(path, content.data(), content.size()); | |
| 27 } | |
| 28 | |
| 29 } // namespace | |
| 30 | 14 |
| 31 namespace chromeos { | 15 namespace chromeos { |
| 32 | 16 |
| 33 FakeCryptohomeClient::FakeCryptohomeClient() | 17 FakeCryptohomeClient::FakeCryptohomeClient() |
| 34 : service_is_available_(true), | 18 : service_is_available_(true), |
| 35 async_call_id_(1), | 19 async_call_id_(1), |
| 36 tpm_is_ready_counter_(0), | 20 tpm_is_ready_counter_(0), |
| 37 unmount_result_(true), | 21 unmount_result_(true), |
| 38 system_salt_(GetStubSystemSalt()), | 22 system_salt_(GetStubSystemSalt()), |
| 39 weak_ptr_factory_(this) { | 23 locked_(false), |
| 40 base::FilePath cache_path; | 24 weak_ptr_factory_(this) {} |
| 41 locked_ = PathService::Get(chromeos::FILE_INSTALL_ATTRIBUTES, &cache_path) && | |
| 42 base::PathExists(cache_path); | |
| 43 } | |
| 44 | 25 |
| 45 FakeCryptohomeClient::~FakeCryptohomeClient() {} | 26 FakeCryptohomeClient::~FakeCryptohomeClient() {} |
| 46 | 27 |
| 47 void FakeCryptohomeClient::Init(dbus::Bus* bus) { | 28 void FakeCryptohomeClient::Init(dbus::Bus* bus) { |
| 48 } | 29 } |
| 49 | 30 |
| 50 void FakeCryptohomeClient::SetAsyncCallStatusHandlers( | 31 void FakeCryptohomeClient::SetAsyncCallStatusHandlers( |
| 51 const AsyncCallStatusHandler& handler, | 32 const AsyncCallStatusHandler& handler, |
| 52 const AsyncCallStatusWithDataHandler& data_handler) { | 33 const AsyncCallStatusWithDataHandler& data_handler) { |
| 53 async_call_status_handler_ = handler; | 34 async_call_status_handler_ = handler; |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 const std::vector<uint8>& value, | 239 const std::vector<uint8>& value, |
| 259 bool* successful) { | 240 bool* successful) { |
| 260 install_attrs_[name] = value; | 241 install_attrs_[name] = value; |
| 261 *successful = true; | 242 *successful = true; |
| 262 return true; | 243 return true; |
| 263 } | 244 } |
| 264 | 245 |
| 265 bool FakeCryptohomeClient::InstallAttributesFinalize(bool* successful) { | 246 bool FakeCryptohomeClient::InstallAttributesFinalize(bool* successful) { |
| 266 locked_ = true; | 247 locked_ = true; |
| 267 *successful = true; | 248 *successful = true; |
| 268 | |
| 269 // Persist the install attributes so that they can be reloaded if the | |
| 270 // browser is restarted. This is used for ease of development when device | |
| 271 // enrollment is required. | |
| 272 // The cryptohome::SerializedInstallAttributes protobuf lives in | |
| 273 // chrome/browser/chromeos, so it can't be used directly here; use the | |
| 274 // low-level protobuf API instead to just write the name-value pairs. | |
| 275 // The cache file is read by EnterpriseInstallAttributes::ReadCacheFile. | |
| 276 base::FilePath cache_path; | |
| 277 if (!PathService::Get(chromeos::FILE_INSTALL_ATTRIBUTES, &cache_path)) | |
| 278 return false; | |
| 279 | |
| 280 std::string result; | |
| 281 { | |
| 282 // |result| can be used only after the StringOutputStream goes out of | |
| 283 // scope. | |
| 284 google::protobuf::io::StringOutputStream result_stream(&result); | |
| 285 google::protobuf::io::CodedOutputStream result_output(&result_stream); | |
| 286 | |
| 287 // These tags encode a variable-length value on the wire, which can be | |
| 288 // used to encode strings, bytes and messages. We only needs constants | |
| 289 // for tag numbers 1 and 2 (see install_attributes.proto). | |
| 290 const int kVarLengthTag1 = (1 << 3) | 0x2; | |
| 291 const int kVarLengthTag2 = (2 << 3) | 0x2; | |
| 292 | |
| 293 typedef std::map<std::string, std::vector<uint8> >::const_iterator Iter; | |
| 294 for (Iter it = install_attrs_.begin(); it != install_attrs_.end(); ++it) { | |
| 295 std::string attr; | |
| 296 { | |
| 297 google::protobuf::io::StringOutputStream attr_stream(&attr); | |
| 298 google::protobuf::io::CodedOutputStream attr_output(&attr_stream); | |
| 299 | |
| 300 attr_output.WriteVarint32(kVarLengthTag1); | |
| 301 attr_output.WriteVarint32(it->first.size()); | |
| 302 attr_output.WriteString(it->first); | |
| 303 attr_output.WriteVarint32(kVarLengthTag2); | |
| 304 attr_output.WriteVarint32(it->second.size()); | |
| 305 attr_output.WriteRaw(it->second.data(), it->second.size()); | |
| 306 } | |
| 307 | |
| 308 // Two CodedOutputStreams are needed because inner messages must be | |
| 309 // prefixed by their total length, which can't be easily computed before | |
| 310 // writing their tags and values. | |
| 311 result_output.WriteVarint32(kVarLengthTag2); | |
| 312 result_output.WriteVarint32(attr.size()); | |
| 313 result_output.WriteRaw(attr.data(), attr.size()); | |
| 314 } | |
| 315 } | |
| 316 | |
| 317 base::WorkerPool::PostTask( | |
| 318 FROM_HERE, base::Bind(&PersistFile, cache_path, result), false); | |
| 319 | |
| 320 return true; | 249 return true; |
| 321 } | 250 } |
| 322 | 251 |
| 323 void FakeCryptohomeClient::InstallAttributesIsReady( | 252 void FakeCryptohomeClient::InstallAttributesIsReady( |
| 324 const BoolDBusMethodCallback& callback) { | 253 const BoolDBusMethodCallback& callback) { |
| 325 base::MessageLoop::current()->PostTask( | 254 base::MessageLoop::current()->PostTask( |
| 326 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true)); | 255 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true)); |
| 327 } | 256 } |
| 328 | 257 |
| 329 bool FakeCryptohomeClient::InstallAttributesIsInvalid(bool* is_invalid) { | 258 bool FakeCryptohomeClient::InstallAttributesIsInvalid(bool* is_invalid) { |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 567 FROM_HERE, | 496 FROM_HERE, |
| 568 base::Bind(async_call_status_data_handler_, | 497 base::Bind(async_call_status_data_handler_, |
| 569 async_call_id_, | 498 async_call_id_, |
| 570 true, | 499 true, |
| 571 std::string())); | 500 std::string())); |
| 572 } | 501 } |
| 573 ++async_call_id_; | 502 ++async_call_id_; |
| 574 } | 503 } |
| 575 | 504 |
| 576 } // namespace chromeos | 505 } // namespace chromeos |
| OLD | NEW |