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/cryptohome_library.h" | 5 #include "chromeos/cryptohome/cryptohome_library.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 | 30 |
31 // This class handles the interaction with the ChromeOS cryptohome library APIs. | 31 // This class handles the interaction with the ChromeOS cryptohome library APIs. |
32 class CryptohomeLibraryImpl : public CryptohomeLibrary { | 32 class CryptohomeLibraryImpl : public CryptohomeLibrary { |
33 public: | 33 public: |
34 CryptohomeLibraryImpl() { | 34 CryptohomeLibraryImpl() { |
35 } | 35 } |
36 | 36 |
37 virtual ~CryptohomeLibraryImpl() { | 37 virtual ~CryptohomeLibraryImpl() { |
38 } | 38 } |
39 | 39 |
40 virtual bool TpmIsEnabled() OVERRIDE { | |
41 bool result = false; | |
42 DBusThreadManager::Get()->GetCryptohomeClient()->CallTpmIsEnabledAndBlock( | |
43 &result); | |
44 return result; | |
45 } | |
46 | |
47 virtual bool TpmIsOwned() OVERRIDE { | |
48 bool result = false; | |
49 DBusThreadManager::Get()->GetCryptohomeClient()->CallTpmIsOwnedAndBlock( | |
50 &result); | |
51 return result; | |
52 } | |
53 | |
54 virtual bool TpmIsBeingOwned() OVERRIDE { | |
55 bool result = false; | |
56 DBusThreadManager::Get()->GetCryptohomeClient()-> | |
57 CallTpmIsBeingOwnedAndBlock(&result); | |
58 return result; | |
59 } | |
60 | |
61 virtual bool InstallAttributesGet( | |
62 const std::string& name, std::string* value) OVERRIDE { | |
63 std::vector<uint8> buf; | |
64 bool success = false; | |
65 DBusThreadManager::Get()->GetCryptohomeClient()-> | |
66 InstallAttributesGet(name, &buf, &success); | |
67 if (success) { | |
68 // Cryptohome returns 'buf' with a terminating '\0' character. | |
69 DCHECK(!buf.empty()); | |
70 DCHECK_EQ(buf.back(), 0); | |
71 value->assign(reinterpret_cast<char*>(buf.data()), buf.size() - 1); | |
72 } | |
73 return success; | |
74 } | |
75 | |
76 virtual bool InstallAttributesSet( | |
77 const std::string& name, const std::string& value) OVERRIDE { | |
78 std::vector<uint8> buf(value.c_str(), value.c_str() + value.size() + 1); | |
79 bool success = false; | |
80 DBusThreadManager::Get()->GetCryptohomeClient()-> | |
81 InstallAttributesSet(name, buf, &success); | |
82 return success; | |
83 } | |
84 | |
85 virtual bool InstallAttributesFinalize() OVERRIDE { | |
86 bool success = false; | |
87 DBusThreadManager::Get()->GetCryptohomeClient()-> | |
88 InstallAttributesFinalize(&success); | |
89 return success; | |
90 } | |
91 | |
92 virtual bool InstallAttributesIsInvalid() OVERRIDE { | |
93 bool result = false; | |
94 DBusThreadManager::Get()->GetCryptohomeClient()-> | |
95 InstallAttributesIsInvalid(&result); | |
96 return result; | |
97 } | |
98 | |
99 virtual bool InstallAttributesIsFirstInstall() OVERRIDE { | |
100 bool result = false; | |
101 DBusThreadManager::Get()->GetCryptohomeClient()-> | |
102 InstallAttributesIsFirstInstall(&result); | |
103 return result; | |
104 } | |
105 | |
106 virtual std::string GetSystemSalt() OVERRIDE { | 40 virtual std::string GetSystemSalt() OVERRIDE { |
107 LoadSystemSalt(); // no-op if it's already loaded. | 41 LoadSystemSalt(); // no-op if it's already loaded. |
108 return system_salt_; | 42 return system_salt_; |
109 } | 43 } |
110 | 44 |
111 virtual std::string EncryptWithSystemSalt(const std::string& token) OVERRIDE { | 45 virtual std::string EncryptWithSystemSalt(const std::string& token) OVERRIDE { |
112 // Don't care about token encryption while debugging. | 46 // Don't care about token encryption while debugging. |
113 if (!base::SysInfo::IsRunningOnChromeOS()) | 47 if (!base::SysInfo::IsRunningOnChromeOS()) |
114 return token; | 48 return token; |
115 | 49 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 | 160 |
227 DISALLOW_COPY_AND_ASSIGN(CryptohomeLibraryImpl); | 161 DISALLOW_COPY_AND_ASSIGN(CryptohomeLibraryImpl); |
228 }; | 162 }; |
229 | 163 |
230 class CryptohomeLibraryStubImpl : public CryptohomeLibrary { | 164 class CryptohomeLibraryStubImpl : public CryptohomeLibrary { |
231 public: | 165 public: |
232 CryptohomeLibraryStubImpl() | 166 CryptohomeLibraryStubImpl() |
233 : locked_(false) {} | 167 : locked_(false) {} |
234 virtual ~CryptohomeLibraryStubImpl() {} | 168 virtual ~CryptohomeLibraryStubImpl() {} |
235 | 169 |
236 virtual bool TpmIsEnabled() OVERRIDE { | |
237 return true; | |
238 } | |
239 | |
240 virtual bool TpmIsOwned() OVERRIDE { | |
241 return true; | |
242 } | |
243 | |
244 virtual bool TpmIsBeingOwned() OVERRIDE { | |
245 return true; | |
246 } | |
247 | |
248 virtual bool InstallAttributesGet( | |
249 const std::string& name, std::string* value) OVERRIDE { | |
250 if (install_attrs_.find(name) != install_attrs_.end()) { | |
251 *value = install_attrs_[name]; | |
252 return true; | |
253 } | |
254 return false; | |
255 } | |
256 | |
257 virtual bool InstallAttributesSet( | |
258 const std::string& name, const std::string& value) OVERRIDE { | |
259 install_attrs_[name] = value; | |
260 return true; | |
261 } | |
262 | |
263 virtual bool InstallAttributesFinalize() OVERRIDE { | |
264 locked_ = true; | |
265 return true; | |
266 } | |
267 | |
268 virtual bool InstallAttributesIsInvalid() OVERRIDE { | |
269 return false; | |
270 } | |
271 | |
272 virtual bool InstallAttributesIsFirstInstall() OVERRIDE { | |
273 return !locked_; | |
274 } | |
275 | |
276 virtual std::string GetSystemSalt() OVERRIDE { | 170 virtual std::string GetSystemSalt() OVERRIDE { |
277 return kStubSystemSalt; | 171 return kStubSystemSalt; |
278 } | 172 } |
279 | 173 |
280 virtual std::string EncryptWithSystemSalt(const std::string& token) OVERRIDE { | 174 virtual std::string EncryptWithSystemSalt(const std::string& token) OVERRIDE { |
281 return token; | 175 return token; |
282 } | 176 } |
283 | 177 |
284 virtual std::string DecryptWithSystemSalt( | 178 virtual std::string DecryptWithSystemSalt( |
285 const std::string& encrypted_token_hex) OVERRIDE { | 179 const std::string& encrypted_token_hex) OVERRIDE { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 void CryptohomeLibrary::SetForTest(CryptohomeLibrary* impl) { | 226 void CryptohomeLibrary::SetForTest(CryptohomeLibrary* impl) { |
333 CHECK(!g_test_cryptohome_library || !impl); | 227 CHECK(!g_test_cryptohome_library || !impl); |
334 g_test_cryptohome_library = impl; | 228 g_test_cryptohome_library = impl; |
335 } | 229 } |
336 | 230 |
337 // static | 231 // static |
338 CryptohomeLibrary* CryptohomeLibrary::GetTestImpl() { | 232 CryptohomeLibrary* CryptohomeLibrary::GetTestImpl() { |
339 return new CryptohomeLibraryStubImpl(); | 233 return new CryptohomeLibraryStubImpl(); |
340 } | 234 } |
341 | 235 |
342 } // namespace chromeos | 236 namespace cryptohome_util { |
| 237 |
| 238 bool TpmIsEnabled() { |
| 239 bool result = false; |
| 240 DBusThreadManager::Get()->GetCryptohomeClient()->CallTpmIsEnabledAndBlock( |
| 241 &result); |
| 242 return result; |
| 243 } |
| 244 |
| 245 bool TpmIsOwned() { |
| 246 bool result = false; |
| 247 DBusThreadManager::Get()->GetCryptohomeClient()->CallTpmIsOwnedAndBlock( |
| 248 &result); |
| 249 return result; |
| 250 } |
| 251 |
| 252 bool TpmIsBeingOwned() { |
| 253 bool result = false; |
| 254 DBusThreadManager::Get()->GetCryptohomeClient()-> |
| 255 CallTpmIsBeingOwnedAndBlock(&result); |
| 256 return result; |
| 257 } |
| 258 |
| 259 bool InstallAttributesGet( |
| 260 const std::string& name, std::string* value) { |
| 261 std::vector<uint8> buf; |
| 262 bool success = false; |
| 263 DBusThreadManager::Get()->GetCryptohomeClient()-> |
| 264 InstallAttributesGet(name, &buf, &success); |
| 265 if (success) { |
| 266 // Cryptohome returns 'buf' with a terminating '\0' character. |
| 267 DCHECK(!buf.empty()); |
| 268 DCHECK_EQ(buf.back(), 0); |
| 269 value->assign(reinterpret_cast<char*>(buf.data()), buf.size() - 1); |
| 270 } |
| 271 return success; |
| 272 } |
| 273 |
| 274 bool InstallAttributesSet( |
| 275 const std::string& name, const std::string& value) { |
| 276 std::vector<uint8> buf(value.c_str(), value.c_str() + value.size() + 1); |
| 277 bool success = false; |
| 278 DBusThreadManager::Get()->GetCryptohomeClient()-> |
| 279 InstallAttributesSet(name, buf, &success); |
| 280 return success; |
| 281 } |
| 282 |
| 283 bool InstallAttributesFinalize() { |
| 284 bool success = false; |
| 285 DBusThreadManager::Get()->GetCryptohomeClient()-> |
| 286 InstallAttributesFinalize(&success); |
| 287 return success; |
| 288 } |
| 289 |
| 290 bool InstallAttributesIsInvalid() { |
| 291 bool result = false; |
| 292 DBusThreadManager::Get()->GetCryptohomeClient()-> |
| 293 InstallAttributesIsInvalid(&result); |
| 294 return result; |
| 295 } |
| 296 |
| 297 bool InstallAttributesIsFirstInstall() { |
| 298 bool result = false; |
| 299 DBusThreadManager::Get()->GetCryptohomeClient()-> |
| 300 InstallAttributesIsFirstInstall(&result); |
| 301 return result; |
| 302 } |
| 303 |
| 304 } // namespace cryptohome_util |
| 305 } // namespace chromeos |
OLD | NEW |