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. | 43 const GetSystemSaltCallback& callback, |
38 return system_salt_; | 44 bool service_is_available) { |
| 45 if (!service_is_available) { |
| 46 LOG(ERROR) << "WaitForServiceToBeAvailable failed."; |
| 47 callback.Run(std::string()); |
| 48 return; |
| 49 } |
| 50 DBusThreadManager::Get()->GetCryptohomeClient()->GetSystemSalt( |
| 51 base::Bind(&SystemSaltGetter::DidGetSystemSalt, |
| 52 weak_ptr_factory_.GetWeakPtr(), |
| 53 callback)); |
39 } | 54 } |
40 | 55 |
41 void SystemSaltGetter::GetSystemSaltInternal( | 56 void SystemSaltGetter::DidGetSystemSalt(const GetSystemSaltCallback& callback, |
42 const GetSystemSaltCallback& callback, | 57 DBusMethodCallStatus call_status, |
43 bool service_is_available) { | 58 const std::vector<uint8>& system_salt) { |
44 LOG_IF(ERROR, !service_is_available) << "WaitForServiceToBeAvailable failed."; | 59 if (call_status == DBUS_METHOD_CALL_SUCCESS && |
45 // TODO(hashimoto): Stop using GetSystemSaltSync(). crbug.com/141009 | 60 !system_salt.empty() && |
46 callback.Run(GetSystemSaltSync()); | 61 system_salt.size() % 2 == 0U) |
47 } | 62 system_salt_ = ConvertRawSaltToHexString(system_salt); |
| 63 else |
| 64 LOG(WARNING) << "System salt not available"; |
48 | 65 |
49 void SystemSaltGetter::LoadSystemSalt() { | 66 callback.Run(system_salt_); |
50 if (!system_salt_.empty()) | |
51 return; | |
52 std::vector<uint8> salt; | |
53 DBusThreadManager::Get()->GetCryptohomeClient()->GetSystemSalt(&salt); | |
54 if (salt.empty() || salt.size() % 2 != 0U) { | |
55 LOG(WARNING) << "System salt not available"; | |
56 return; | |
57 } | |
58 system_salt_ = ConvertRawSaltToHexString(salt); | |
59 } | 67 } |
60 | 68 |
61 // static | 69 // static |
62 void SystemSaltGetter::Initialize() { | 70 void SystemSaltGetter::Initialize() { |
63 CHECK(!g_system_salt_getter); | 71 CHECK(!g_system_salt_getter); |
64 g_system_salt_getter = new SystemSaltGetter(); | 72 g_system_salt_getter = new SystemSaltGetter(); |
65 } | 73 } |
66 | 74 |
67 // static | 75 // static |
68 bool SystemSaltGetter::IsInitialized() { | 76 bool SystemSaltGetter::IsInitialized() { |
(...skipping 15 matching lines...) Expand all Loading... |
84 } | 92 } |
85 | 93 |
86 // static | 94 // static |
87 std::string SystemSaltGetter::ConvertRawSaltToHexString( | 95 std::string SystemSaltGetter::ConvertRawSaltToHexString( |
88 const std::vector<uint8>& salt) { | 96 const std::vector<uint8>& salt) { |
89 return StringToLowerASCII(base::HexEncode( | 97 return StringToLowerASCII(base::HexEncode( |
90 reinterpret_cast<const void*>(salt.data()), salt.size())); | 98 reinterpret_cast<const void*>(salt.data()), salt.size())); |
91 } | 99 } |
92 | 100 |
93 } // namespace chromeos | 101 } // namespace chromeos |
OLD | NEW |