Chromium Code Reviews| 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 "chromeos/cryptohome/system_salt_getter.h" | 5 #include "chromeos/cryptohome/system_salt_getter.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "chromeos/dbus/cryptohome_client.h" | 12 #include "chromeos/dbus/cryptohome_client.h" |
| 13 #include "chromeos/dbus/dbus_thread_manager.h" | 13 #include "chromeos/dbus/dbus_thread_manager.h" |
| 14 | 14 |
| 15 namespace chromeos { | 15 namespace chromeos { |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 SystemSaltGetter* g_system_salt_getter = NULL; | 18 SystemSaltGetter* g_system_salt_getter = NULL; |
| 19 | 19 |
| 20 } // namespace | 20 } // namespace |
| 21 | 21 |
| 22 SystemSaltGetter::SystemSaltGetter() : weak_ptr_factory_(this) { | 22 SystemSaltGetter::SystemSaltGetter() : weak_ptr_factory_(this) { |
| 23 } | 23 } |
| 24 | 24 |
| 25 SystemSaltGetter::~SystemSaltGetter() { | 25 SystemSaltGetter::~SystemSaltGetter() { |
| 26 } | 26 } |
| 27 | 27 |
| 28 void SystemSaltGetter::GetSystemSalt( | 28 void SystemSaltGetter::GetSystemSalt( |
| 29 const GetSystemSaltCallback& callback) { | 29 const GetSystemSaltCallback& callback) { |
| 30 if (!system_salt_.empty()) { | |
| 31 base::MessageLoopProxy::current()->PostTask( | |
| 32 FROM_HERE, base::Bind(callback, system_salt_)); | |
| 33 return; | |
| 34 } | |
| 35 | |
| 30 DBusThreadManager::Get()->GetCryptohomeClient()->WaitForServiceToBeAvailable( | 36 DBusThreadManager::Get()->GetCryptohomeClient()->WaitForServiceToBeAvailable( |
| 31 base::Bind(&SystemSaltGetter::GetSystemSaltInternal, | 37 base::Bind(&SystemSaltGetter::DidWaitForServiceToBeAvailable, |
| 32 weak_ptr_factory_.GetWeakPtr(), | 38 weak_ptr_factory_.GetWeakPtr(), |
| 33 callback)); | 39 callback)); |
| 34 } | 40 } |
| 35 | 41 |
| 36 std::string SystemSaltGetter::GetSystemSaltSync() { | 42 void SystemSaltGetter::DidWaitForServiceToBeAvailable( |
| 37 LoadSystemSalt(); // no-op if it's already loaded. | |
| 38 return system_salt_; | |
| 39 } | |
| 40 | |
| 41 void SystemSaltGetter::GetSystemSaltInternal( | |
| 42 const GetSystemSaltCallback& callback, | 43 const GetSystemSaltCallback& callback, |
| 43 bool service_is_available) { | 44 bool service_is_available) { |
| 44 LOG_IF(ERROR, !service_is_available) << "WaitForServiceToBeAvailable failed."; | 45 LOG_IF(ERROR, !service_is_available) << "WaitForServiceToBeAvailable failed."; |
|
satorux1
2013/10/25 07:59:17
I think we should not proceed if this is false. ma
hashimoto
2013/10/25 08:01:51
Done.
| |
| 45 // TODO(hashimoto): Stop using GetSystemSaltSync(). crbug.com/141009 | 46 DBusThreadManager::Get()->GetCryptohomeClient()->GetSystemSalt( |
| 46 callback.Run(GetSystemSaltSync()); | 47 base::Bind(&SystemSaltGetter::DidGetSystemSalt, |
| 48 weak_ptr_factory_.GetWeakPtr(), | |
| 49 callback)); | |
| 47 } | 50 } |
| 48 | 51 |
| 49 void SystemSaltGetter::LoadSystemSalt() { | 52 void SystemSaltGetter::DidGetSystemSalt(const GetSystemSaltCallback& callback, |
| 50 if (!system_salt_.empty()) | 53 DBusMethodCallStatus call_status, |
| 51 return; | 54 const std::vector<uint8>& system_salt) { |
| 52 std::vector<uint8> salt; | 55 if (call_status == DBUS_METHOD_CALL_SUCCESS && |
| 53 DBusThreadManager::Get()->GetCryptohomeClient()->GetSystemSalt(&salt); | 56 !system_salt.empty() && |
| 54 if (salt.empty() || salt.size() % 2 != 0U) { | 57 system_salt.size() % 2 == 0U) |
| 58 system_salt_ = ConvertRawSaltToHexString(system_salt); | |
| 59 else | |
| 55 LOG(WARNING) << "System salt not available"; | 60 LOG(WARNING) << "System salt not available"; |
| 56 return; | 61 |
| 57 } | 62 callback.Run(system_salt_); |
| 58 system_salt_ = ConvertRawSaltToHexString(salt); | |
| 59 } | 63 } |
| 60 | 64 |
| 61 // static | 65 // static |
| 62 void SystemSaltGetter::Initialize() { | 66 void SystemSaltGetter::Initialize() { |
| 63 CHECK(!g_system_salt_getter); | 67 CHECK(!g_system_salt_getter); |
| 64 g_system_salt_getter = new SystemSaltGetter(); | 68 g_system_salt_getter = new SystemSaltGetter(); |
| 65 } | 69 } |
| 66 | 70 |
| 67 // static | 71 // static |
| 68 bool SystemSaltGetter::IsInitialized() { | 72 bool SystemSaltGetter::IsInitialized() { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 84 } | 88 } |
| 85 | 89 |
| 86 // static | 90 // static |
| 87 std::string SystemSaltGetter::ConvertRawSaltToHexString( | 91 std::string SystemSaltGetter::ConvertRawSaltToHexString( |
| 88 const std::vector<uint8>& salt) { | 92 const std::vector<uint8>& salt) { |
| 89 return StringToLowerASCII(base::HexEncode( | 93 return StringToLowerASCII(base::HexEncode( |
| 90 reinterpret_cast<const void*>(salt.data()), salt.size())); | 94 reinterpret_cast<const void*>(salt.data()), salt.size())); |
| 91 } | 95 } |
| 92 | 96 |
| 93 } // namespace chromeos | 97 } // namespace chromeos |
| OLD | NEW |