OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/dbus/cryptohome_client.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop.h" | |
9 #include "chrome/browser/chromeos/dbus/blocking_method_caller.h" | |
10 #include "dbus/bus.h" | |
11 #include "dbus/message.h" | |
12 #include "dbus/object_path.h" | |
13 #include "dbus/object_proxy.h" | |
14 #include "third_party/cros_system_api/dbus/service_constants.h" | |
15 | |
16 namespace chromeos { | |
17 | |
18 namespace { | |
19 | |
20 // A convenient macro to initialize a dbus::MethodCall while checking the dbus | |
21 // method name matches to the C++ method name. | |
22 #define INITIALIZE_METHOD_CALL(method_call_name, method_name) \ | |
23 DCHECK_EQ(std::string(method_name), __FUNCTION__); \ | |
24 dbus::MethodCall method_call_name(cryptohome::kCryptohomeInterface, \ | |
25 method_name); | |
26 // The CryptohomeClient implementation. | |
27 class CryptohomeClientImpl : public CryptohomeClient { | |
28 public: | |
29 explicit CryptohomeClientImpl(dbus::Bus* bus) | |
30 : proxy_(bus->GetObjectProxy( | |
31 cryptohome::kCryptohomeServiceName, | |
32 dbus::ObjectPath(cryptohome::kCryptohomeServicePath))), | |
33 weak_ptr_factory_(this), | |
34 blocking_method_caller_(bus, proxy_) { | |
35 proxy_->ConnectToSignal( | |
36 cryptohome::kCryptohomeInterface, | |
37 cryptohome::kSignalAsyncCallStatus, | |
38 base::Bind(&CryptohomeClientImpl::OnAsyncCallStatus, | |
39 weak_ptr_factory_.GetWeakPtr()), | |
40 base::Bind(&CryptohomeClientImpl::OnSignalConnected, | |
41 weak_ptr_factory_.GetWeakPtr())); | |
42 } | |
43 | |
44 // CryptohomeClient override. | |
45 virtual void SetAsyncCallStatusHandler(AsyncCallStatusHandler handler) | |
46 OVERRIDE { | |
47 async_call_status_handler_ = handler; | |
48 } | |
49 | |
50 // CryptohomeClient override. | |
51 virtual void ResetAsyncCallStatusHandler() OVERRIDE { | |
52 async_call_status_handler_.Reset(); | |
53 } | |
54 | |
55 // CryptohomeClient override. | |
56 virtual bool IsMounted(bool* is_mounted) OVERRIDE { | |
57 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeIsMounted); | |
58 return CallBoolMethodAndBlock(&method_call, is_mounted); | |
59 } | |
60 | |
61 // CryptohomeClient override. | |
62 virtual bool Unmount(bool *success) OVERRIDE { | |
63 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeUnmount); | |
64 return CallBoolMethodAndBlock(&method_call, success); | |
65 } | |
66 | |
67 // CryptohomeClient override. | |
68 virtual void AsyncCheckKey(const std::string& username, | |
69 const std::string& key, | |
70 AsyncMethodCallback callback) OVERRIDE { | |
71 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeAsyncCheckKey); | |
72 dbus::MessageWriter writer(&method_call); | |
73 writer.AppendString(username); | |
74 writer.AppendString(key); | |
75 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
76 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall, | |
77 weak_ptr_factory_.GetWeakPtr(), | |
78 callback)); | |
79 } | |
80 | |
81 // CryptohomeClient override. | |
82 virtual void AsyncMigrateKey(const std::string& username, | |
83 const std::string& from_key, | |
84 const std::string& to_key, | |
85 AsyncMethodCallback callback) OVERRIDE { | |
86 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeAsyncMigrateKey); | |
87 dbus::MessageWriter writer(&method_call); | |
88 writer.AppendString(username); | |
89 writer.AppendString(from_key); | |
90 writer.AppendString(to_key); | |
91 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
92 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall, | |
93 weak_ptr_factory_.GetWeakPtr(), | |
94 callback)); | |
95 } | |
96 | |
97 // CryptohomeClient override. | |
98 virtual void AsyncRemove(const std::string& username, | |
99 AsyncMethodCallback callback) OVERRIDE { | |
100 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeAsyncRemove); | |
101 dbus::MessageWriter writer(&method_call); | |
102 writer.AppendString(username); | |
103 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
104 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall, | |
105 weak_ptr_factory_.GetWeakPtr(), | |
106 callback)); | |
107 } | |
108 | |
109 // CryptohomeClient override. | |
110 virtual bool GetSystemSalt(std::vector<uint8>* salt) OVERRIDE { | |
111 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeGetSystemSalt); | |
112 scoped_ptr<dbus::Response> response( | |
113 blocking_method_caller_.CallMethodAndBlock(&method_call)); | |
114 if (!response.get()) | |
115 return false; | |
116 dbus::MessageReader reader(response.get()); | |
117 uint8* bytes = NULL; | |
118 size_t length = 0; | |
119 if (!reader.PopArrayOfBytes(&bytes, &length)) | |
120 return false; | |
121 salt->assign(bytes, bytes + length); | |
122 return true; | |
123 } | |
124 | |
125 // CryptohomeClient override. | |
126 virtual void AsyncMount(const std::string& username, | |
127 const std::string& key, | |
128 const bool create_if_missing, | |
129 AsyncMethodCallback callback) OVERRIDE { | |
130 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeAsyncMount); | |
131 dbus::MessageWriter writer(&method_call); | |
132 writer.AppendString(username); | |
133 writer.AppendString(key); | |
134 writer.AppendBool(create_if_missing); | |
135 writer.AppendBool(false); // deprecated_replace_tracked_subdirectories | |
136 // deprecated_tracked_subdirectories | |
137 writer.AppendArrayOfStrings(std::vector<std::string>()); | |
138 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
139 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall, | |
140 weak_ptr_factory_.GetWeakPtr(), | |
141 callback)); | |
142 } | |
143 | |
144 // CryptohomeClient override. | |
145 virtual void AsyncMountGuest(AsyncMethodCallback callback) OVERRIDE { | |
146 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeAsyncMountGuest); | |
147 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
148 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall, | |
149 weak_ptr_factory_.GetWeakPtr(), | |
150 callback)); | |
151 } | |
152 | |
153 // CryptohomeClient override. | |
154 virtual bool TpmIsReady(bool* ready) OVERRIDE { | |
155 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeTpmIsReady); | |
156 return CallBoolMethodAndBlock(&method_call, ready); | |
157 } | |
158 | |
159 // CryptohomeClient override. | |
160 virtual void TpmIsEnabled(BoolMethodCallback callback) OVERRIDE { | |
161 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeTpmIsEnabled); | |
162 proxy_->CallMethod( | |
163 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
164 base::Bind( | |
165 &CryptohomeClientImpl::OnBoolMethod, | |
166 weak_ptr_factory_.GetWeakPtr(), | |
167 callback)); | |
168 } | |
169 | |
170 // CryptohomeClient override. | |
171 // TODO(hashimoto): Remove this method. crosbug.com/28500 | |
172 virtual bool CallTpmIsEnabledAndBlock(bool* enabled) OVERRIDE { | |
173 // We don't use INITIALIZE_METHOD_CALL here because the C++ method name is | |
174 // different from the D-Bus method name (TpmIsEnabled). | |
175 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface, | |
176 cryptohome::kCryptohomeTpmIsEnabled); | |
177 return CallBoolMethodAndBlock(&method_call, enabled); | |
178 } | |
179 | |
180 // CryptohomeClient override. | |
181 virtual bool TpmGetPassword(std::string* password) OVERRIDE { | |
182 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeTpmGetPassword); | |
183 scoped_ptr<dbus::Response> response( | |
184 blocking_method_caller_.CallMethodAndBlock(&method_call)); | |
185 if (!response.get()) | |
186 return false; | |
187 dbus::MessageReader reader(response.get()); | |
188 return reader.PopString(password); | |
189 } | |
190 | |
191 // CryptohomeClient override. | |
192 virtual bool TpmIsOwned(bool* owned) OVERRIDE { | |
193 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeTpmIsOwned); | |
194 return CallBoolMethodAndBlock(&method_call, owned); | |
195 } | |
196 | |
197 // CryptohomeClient override. | |
198 virtual bool TpmIsBeingOwned(bool* owning) OVERRIDE { | |
199 INITIALIZE_METHOD_CALL(method_call, cryptohome::kCryptohomeTpmIsBeingOwned); | |
200 return CallBoolMethodAndBlock(&method_call, owning); | |
201 } | |
202 | |
203 // CryptohomeClient override. | |
204 virtual bool TpmCanAttemptOwnership() OVERRIDE { | |
205 INITIALIZE_METHOD_CALL(method_call, | |
206 cryptohome::kCryptohomeTpmCanAttemptOwnership); | |
207 scoped_ptr<dbus::Response> response( | |
208 blocking_method_caller_.CallMethodAndBlock(&method_call)); | |
209 return response.get() != NULL; | |
210 } | |
211 | |
212 // CryptohomeClient override. | |
213 virtual bool TpmClearStoredPassword() OVERRIDE { | |
214 INITIALIZE_METHOD_CALL(method_call, | |
215 cryptohome::kCryptohomeTpmClearStoredPassword); | |
216 scoped_ptr<dbus::Response> response( | |
217 blocking_method_caller_.CallMethodAndBlock(&method_call)); | |
218 return response.get() != NULL; | |
219 } | |
220 | |
221 // CryptohomeClient override. | |
222 virtual void Pkcs11IsTpmTokenReady(BoolMethodCallback callback) | |
223 OVERRIDE { | |
224 INITIALIZE_METHOD_CALL(method_call, | |
225 cryptohome::kCryptohomePkcs11IsTpmTokenReady); | |
226 proxy_->CallMethod( | |
227 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
228 base::Bind( | |
229 &CryptohomeClientImpl::OnBoolMethod, | |
230 weak_ptr_factory_.GetWeakPtr(), | |
231 callback)); | |
232 } | |
233 | |
234 // CryptohomeClient override. | |
235 virtual void Pkcs11GetTpmTokenInfo(Pkcs11GetTpmTokenInfoCallback callback) | |
236 OVERRIDE { | |
237 INITIALIZE_METHOD_CALL(method_call, | |
238 cryptohome::kCryptohomePkcs11GetTpmTokenInfo); | |
239 proxy_->CallMethod( | |
240 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
241 base::Bind( | |
242 &CryptohomeClientImpl::OnPkcs11GetTpmTokenInfo, | |
243 weak_ptr_factory_.GetWeakPtr(), | |
244 callback)); | |
245 } | |
246 | |
247 // CryptohomeClient override. | |
248 virtual bool InstallAttributesGet(const std::string& name, | |
249 std::vector<uint8>* value, | |
250 bool* successful) OVERRIDE { | |
251 INITIALIZE_METHOD_CALL(method_call, | |
252 cryptohome::kCryptohomeInstallAttributesGet); | |
253 dbus::MessageWriter writer(&method_call); | |
254 writer.AppendString(name); | |
255 scoped_ptr<dbus::Response> response( | |
256 blocking_method_caller_.CallMethodAndBlock(&method_call)); | |
257 if (!response.get()) | |
258 return false; | |
259 dbus::MessageReader reader(response.get()); | |
260 uint8* bytes = NULL; | |
261 size_t length = 0; | |
262 if (!reader.PopArrayOfBytes(&bytes, &length) || | |
263 !reader.PopBool(successful)) | |
264 return false; | |
265 value->assign(bytes, bytes + length); | |
266 return true; | |
267 } | |
268 | |
269 // CryptohomeClient override. | |
270 virtual bool InstallAttributesSet(const std::string& name, | |
271 const std::vector<uint8>& value, | |
272 bool* successful) OVERRIDE { | |
273 INITIALIZE_METHOD_CALL(method_call, | |
274 cryptohome::kCryptohomeInstallAttributesSet); | |
275 dbus::MessageWriter writer(&method_call); | |
276 writer.AppendString(name); | |
277 writer.AppendArrayOfBytes(value.data(), value.size()); | |
278 return CallBoolMethodAndBlock(&method_call, successful); | |
279 } | |
280 | |
281 // CryptohomeClient override. | |
282 virtual bool InstallAttributesFinalize(bool* successful) OVERRIDE { | |
283 INITIALIZE_METHOD_CALL(method_call, | |
284 cryptohome::kCryptohomeInstallAttributesFinalize); | |
285 return CallBoolMethodAndBlock(&method_call, successful); | |
286 } | |
287 | |
288 // CryptohomeClient override. | |
289 virtual bool InstallAttributesIsReady(bool* is_ready) OVERRIDE { | |
290 INITIALIZE_METHOD_CALL(method_call, | |
291 cryptohome::kCryptohomeInstallAttributesIsReady); | |
292 return CallBoolMethodAndBlock(&method_call, is_ready); | |
293 } | |
294 | |
295 // CryptohomeClient override. | |
296 virtual bool InstallAttributesIsInvalid(bool* is_invalid) OVERRIDE { | |
297 INITIALIZE_METHOD_CALL(method_call, | |
298 cryptohome::kCryptohomeInstallAttributesIsInvalid); | |
299 return CallBoolMethodAndBlock(&method_call, is_invalid); | |
300 } | |
301 | |
302 // CryptohomeClient override. | |
303 virtual bool InstallAttributesIsFirstInstall(bool* is_first_install) OVERRIDE | |
304 { | |
305 INITIALIZE_METHOD_CALL( | |
306 method_call, cryptohome::kCryptohomeInstallAttributesIsFirstInstall); | |
307 return CallBoolMethodAndBlock(&method_call, is_first_install); | |
308 } | |
309 | |
310 private: | |
311 // Handles the result of AsyncXXX methods. | |
312 void OnAsyncMethodCall(AsyncMethodCallback callback, | |
313 dbus::Response* response) { | |
314 if (!response) | |
315 return; | |
316 dbus::MessageReader reader(response); | |
317 int async_id = 0; | |
318 if (!reader.PopInt32(&async_id)) { | |
319 LOG(ERROR) << "Invalid response: " << response->ToString(); | |
320 return; | |
321 } | |
322 callback.Run(async_id); | |
323 } | |
324 | |
325 // Calls a method with a bool value reult and block. | |
326 bool CallBoolMethodAndBlock(dbus::MethodCall* method_call, | |
327 bool* result) { | |
328 scoped_ptr<dbus::Response> response( | |
329 blocking_method_caller_.CallMethodAndBlock(method_call)); | |
330 if (!response.get()) | |
331 return false; | |
332 dbus::MessageReader reader(response.get()); | |
333 return reader.PopBool(result); | |
334 } | |
335 | |
336 // Handles responses for methods with a bool value result. | |
337 void OnBoolMethod(BoolMethodCallback callback, | |
338 dbus::Response* response) { | |
339 if (!response) { | |
340 callback.Run(DBUS_METHOD_CALL_FAILURE, false); | |
341 return; | |
342 } | |
343 dbus::MessageReader reader(response); | |
344 bool result = false; | |
345 if (!reader.PopBool(&result)) { | |
346 callback.Run(DBUS_METHOD_CALL_FAILURE, false); | |
347 return; | |
348 } | |
349 callback.Run(DBUS_METHOD_CALL_SUCCESS, result); | |
350 } | |
351 | |
352 // Handles responses for Pkcs11GetTpmtTokenInfo. | |
353 void OnPkcs11GetTpmTokenInfo(Pkcs11GetTpmTokenInfoCallback callback, | |
354 dbus::Response* response) { | |
355 if (!response) { | |
356 callback.Run(DBUS_METHOD_CALL_FAILURE, std::string(), std::string()); | |
357 return; | |
358 } | |
359 dbus::MessageReader reader(response); | |
360 std::string label; | |
361 std::string user_pin; | |
362 if (!reader.PopString(&label) || !reader.PopString(&user_pin)) { | |
363 callback.Run(DBUS_METHOD_CALL_FAILURE, std::string(), std::string()); | |
364 return; | |
365 } | |
366 callback.Run(DBUS_METHOD_CALL_SUCCESS, label, user_pin); | |
367 } | |
368 | |
369 // Handles AsyncCallStatus signal. | |
370 void OnAsyncCallStatus(dbus::Signal* signal) { | |
371 dbus::MessageReader reader(signal); | |
372 int async_id = 0; | |
373 bool return_status = false; | |
374 int return_code = 0; | |
375 if (!reader.PopInt32(&async_id) || | |
376 !reader.PopBool(&return_status) || | |
377 !reader.PopInt32(&return_code)) { | |
378 LOG(ERROR) << "Invalid signal: " << signal->ToString(); | |
379 return; | |
380 } | |
381 if (!async_call_status_handler_.is_null()) | |
382 async_call_status_handler_.Run(async_id, return_status, return_code); | |
383 } | |
384 | |
385 // Handles the result of signal connection setup. | |
386 void OnSignalConnected(const std::string& interface, | |
387 const std::string& signal, | |
388 bool successed) { | |
389 LOG_IF(ERROR, !successed) << "Connect to " << interface << " " << | |
390 signal << " failed."; | |
391 } | |
392 | |
393 dbus::ObjectProxy* proxy_; | |
394 base::WeakPtrFactory<CryptohomeClientImpl> weak_ptr_factory_; | |
395 BlockingMethodCaller blocking_method_caller_; | |
396 AsyncCallStatusHandler async_call_status_handler_; | |
397 | |
398 DISALLOW_COPY_AND_ASSIGN(CryptohomeClientImpl); | |
399 }; | |
400 | |
401 // A stub implementaion of CryptohomeClient. | |
402 class CryptohomeClientStubImpl : public CryptohomeClient { | |
403 public: | |
404 CryptohomeClientStubImpl() | |
405 : weak_ptr_factory_(this), | |
406 async_call_id_(1), | |
407 tpm_is_ready_counter_(0), | |
408 locked_(false) { | |
409 } | |
410 | |
411 virtual ~CryptohomeClientStubImpl() {} | |
412 | |
413 // CryptohomeClient override. | |
414 virtual void SetAsyncCallStatusHandler(AsyncCallStatusHandler handler) | |
415 OVERRIDE { | |
416 async_call_status_handler_ = handler; | |
417 } | |
418 | |
419 // CryptohomeClient override. | |
420 virtual void ResetAsyncCallStatusHandler() OVERRIDE { | |
421 async_call_status_handler_.Reset(); | |
422 } | |
423 | |
424 // CryptohomeClient override. | |
425 virtual bool IsMounted(bool* is_mounted) OVERRIDE { | |
426 *is_mounted = true; | |
427 return true; | |
428 } | |
429 | |
430 // CryptohomeClient override. | |
431 virtual bool Unmount(bool* success) OVERRIDE { | |
432 *success = true; | |
433 return true; | |
434 } | |
435 | |
436 // CryptohomeClient override. | |
437 virtual void AsyncCheckKey(const std::string& username, | |
438 const std::string& key, | |
439 AsyncMethodCallback callback) OVERRIDE { | |
440 ReturnAsyncMethodResult(callback); | |
441 } | |
442 | |
443 // CryptohomeClient override. | |
444 virtual void AsyncMigrateKey(const std::string& username, | |
445 const std::string& from_key, | |
446 const std::string& to_key, | |
447 AsyncMethodCallback callback) OVERRIDE { | |
448 ReturnAsyncMethodResult(callback); | |
449 } | |
450 | |
451 // CryptohomeClient override. | |
452 virtual void AsyncRemove(const std::string& username, | |
453 AsyncMethodCallback callback) OVERRIDE { | |
454 ReturnAsyncMethodResult(callback); | |
455 } | |
456 | |
457 // CryptohomeClient override. | |
458 virtual bool GetSystemSalt(std::vector<uint8>* salt) OVERRIDE { | |
459 const char kStubSystemSalt[] = "stub_system_salt"; | |
460 salt->assign(kStubSystemSalt, | |
461 kStubSystemSalt + arraysize(kStubSystemSalt)); | |
462 return true; | |
463 } | |
464 | |
465 // CryptohomeClient override. | |
466 virtual void AsyncMount(const std::string& username, | |
467 const std::string& key, | |
468 const bool create_if_missing, | |
469 AsyncMethodCallback callback) OVERRIDE { | |
470 ReturnAsyncMethodResult(callback); | |
471 } | |
472 | |
473 // CryptohomeClient override. | |
474 virtual void AsyncMountGuest(AsyncMethodCallback callback) OVERRIDE { | |
475 ReturnAsyncMethodResult(callback); | |
476 } | |
477 | |
478 // CryptohomeClient override. | |
479 virtual bool TpmIsReady(bool* ready) OVERRIDE { | |
480 *ready = (tpm_is_ready_counter_++ > 20); | |
481 return true; | |
482 } | |
483 | |
484 // CryptohomeClient override. | |
485 virtual void TpmIsEnabled(BoolMethodCallback callback) OVERRIDE { | |
486 MessageLoop::current()->PostTask( | |
487 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true)); | |
488 } | |
489 | |
490 // CryptohomeClient override. | |
491 virtual bool CallTpmIsEnabledAndBlock(bool* enabled) OVERRIDE { | |
492 *enabled = true; | |
493 return true; | |
494 } | |
495 | |
496 // CryptohomeClient override. | |
497 virtual bool TpmGetPassword(std::string* password) OVERRIDE { | |
498 const char kStubTpmPassword[] = "Stub-TPM-password"; | |
499 *password = kStubTpmPassword; | |
500 return true; | |
501 } | |
502 | |
503 // CryptohomeClient override. | |
504 virtual bool TpmIsOwned(bool* owned) OVERRIDE { | |
505 *owned = true; | |
506 return true; | |
507 } | |
508 | |
509 // CryptohomeClient override. | |
510 virtual bool TpmIsBeingOwned(bool* owning) OVERRIDE { | |
511 *owning = true; | |
512 return true; | |
513 } | |
514 | |
515 // CryptohomeClient override. | |
516 virtual bool TpmCanAttemptOwnership() OVERRIDE { return true; } | |
517 | |
518 // CryptohomeClient override. | |
519 virtual bool TpmClearStoredPassword() OVERRIDE { return true; } | |
520 | |
521 // CryptohomeClient override. | |
522 virtual void Pkcs11IsTpmTokenReady(BoolMethodCallback callback) OVERRIDE { | |
523 MessageLoop::current()->PostTask( | |
524 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true)); | |
525 } | |
526 | |
527 // CryptohomeClient override. | |
528 virtual void Pkcs11GetTpmTokenInfo( | |
529 Pkcs11GetTpmTokenInfoCallback callback) OVERRIDE { | |
530 const char kStubLabel[] = "Stub TPM Token"; | |
531 const char kStubUserPin[] = "012345"; | |
532 MessageLoop::current()->PostTask( | |
533 FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, kStubLabel, | |
534 kStubUserPin)); | |
535 } | |
536 | |
537 // CryptohomeClient override. | |
538 virtual bool InstallAttributesGet(const std::string& name, | |
539 std::vector<uint8>* value, | |
540 bool* successful) OVERRIDE { | |
541 if (install_attrs_.find(name) != install_attrs_.end()) { | |
542 *value = install_attrs_[name]; | |
543 *successful = true; | |
544 } else { | |
545 value->clear(); | |
546 *successful = false; | |
547 } | |
548 return true; | |
549 } | |
550 | |
551 // CryptohomeClient override. | |
552 virtual bool InstallAttributesSet(const std::string& name, | |
553 const std::vector<uint8>& value, | |
554 bool* successful) OVERRIDE { | |
555 install_attrs_[name] = value; | |
556 *successful = true; | |
557 return true; | |
558 } | |
559 | |
560 // CryptohomeClient override. | |
561 virtual bool InstallAttributesFinalize(bool* successful) OVERRIDE { | |
562 locked_ = true; | |
563 *successful = true; | |
564 return true; | |
565 } | |
566 | |
567 // CryptohomeClient override. | |
568 virtual bool InstallAttributesIsReady(bool* is_ready) OVERRIDE { | |
569 *is_ready = true; | |
570 return true; | |
571 } | |
572 | |
573 // CryptohomeClient override. | |
574 virtual bool InstallAttributesIsInvalid(bool* is_invalid) OVERRIDE { | |
575 *is_invalid = false; | |
576 return true; | |
577 } | |
578 | |
579 // CryptohomeClient override. | |
580 virtual bool InstallAttributesIsFirstInstall(bool* is_first_install) OVERRIDE | |
581 { | |
582 *is_first_install = !locked_; | |
583 return true; | |
584 } | |
585 | |
586 private: | |
587 // Posts tasks which return fake results to the UI thread. | |
588 void ReturnAsyncMethodResult(AsyncMethodCallback callback) { | |
589 MessageLoop::current()->PostTask( | |
590 FROM_HERE, | |
591 base::Bind(&CryptohomeClientStubImpl::ReturnAsyncMethodResultInternal, | |
592 weak_ptr_factory_.GetWeakPtr(), | |
593 callback)); | |
594 } | |
595 | |
596 // This method is used to implement ReturnAsyncMethodResult. | |
597 void ReturnAsyncMethodResultInternal(AsyncMethodCallback callback) { | |
598 callback.Run(async_call_id_); | |
599 if (!async_call_status_handler_.is_null()) { | |
600 MessageLoop::current()->PostTask( | |
601 FROM_HERE, | |
602 base::Bind(async_call_status_handler_, | |
603 async_call_id_, | |
604 true, | |
605 cryptohome::MOUNT_ERROR_NONE)); | |
606 } | |
607 ++async_call_id_; | |
608 } | |
609 | |
610 base::WeakPtrFactory<CryptohomeClientStubImpl> weak_ptr_factory_; | |
611 int async_call_id_; | |
612 AsyncCallStatusHandler async_call_status_handler_; | |
613 int tpm_is_ready_counter_; | |
614 std::map<std::string, std::vector<uint8> > install_attrs_; | |
615 bool locked_; | |
616 | |
617 DISALLOW_COPY_AND_ASSIGN(CryptohomeClientStubImpl); | |
618 }; | |
619 | |
620 } // namespace | |
621 | |
622 //////////////////////////////////////////////////////////////////////////////// | |
623 // CryptohomeClient | |
624 | |
625 CryptohomeClient::CryptohomeClient() {} | |
626 | |
627 CryptohomeClient::~CryptohomeClient() {} | |
628 | |
629 // static | |
630 CryptohomeClient* CryptohomeClient::Create(DBusClientImplementationType type, | |
631 dbus::Bus* bus) { | |
632 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | |
633 return new CryptohomeClientImpl(bus); | |
634 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
635 return new CryptohomeClientStubImpl(); | |
636 } | |
637 | |
638 } // namespace chromeos | |
OLD | NEW |