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.
5 #include "chromeos/dbus/cryptohome_client.h"
8 #include "base/message_loop.h"
9 #include "chromeos/cryptohome/async_method_caller.h"
10 #include "chromeos/dbus/blocking_method_caller.h"
12 #include "dbus/message.h"
13 #include "dbus/object_path.h"
14 #include "dbus/object_proxy.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
21 // The CryptohomeClient implementation.
22 class CryptohomeClientImpl
: public CryptohomeClient
{
24 explicit CryptohomeClientImpl(dbus::Bus
* bus
)
25 : proxy_(bus
->GetObjectProxy(
26 cryptohome::kCryptohomeServiceName
,
27 dbus::ObjectPath(cryptohome::kCryptohomeServicePath
))),
28 blocking_method_caller_(bus
, proxy_
),
29 weak_ptr_factory_(this) {
30 proxy_
->ConnectToSignal(
31 cryptohome::kCryptohomeInterface
,
32 cryptohome::kSignalAsyncCallStatus
,
33 base::Bind(&CryptohomeClientImpl::OnAsyncCallStatus
,
34 weak_ptr_factory_
.GetWeakPtr()),
35 base::Bind(&CryptohomeClientImpl::OnSignalConnected
,
36 weak_ptr_factory_
.GetWeakPtr()));
37 proxy_
->ConnectToSignal(
38 cryptohome::kCryptohomeInterface
,
39 cryptohome::kSignalAsyncCallStatusWithData
,
40 base::Bind(&CryptohomeClientImpl::OnAsyncCallStatusWithData
,
41 weak_ptr_factory_
.GetWeakPtr()),
42 base::Bind(&CryptohomeClientImpl::OnSignalConnected
,
43 weak_ptr_factory_
.GetWeakPtr()));
46 // CryptohomeClient override.
47 virtual void SetAsyncCallStatusHandlers(
48 const AsyncCallStatusHandler
& handler
,
49 const AsyncCallStatusWithDataHandler
& data_handler
) OVERRIDE
{
50 async_call_status_handler_
= handler
;
51 async_call_status_data_handler_
= data_handler
;
54 // CryptohomeClient override.
55 virtual void ResetAsyncCallStatusHandlers() OVERRIDE
{
56 async_call_status_handler_
.Reset();
57 async_call_status_data_handler_
.Reset();
60 // CryptohomeClient override.
61 virtual void IsMounted(const BoolDBusMethodCallback
& callback
) OVERRIDE
{
62 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
63 cryptohome::kCryptohomeIsMounted
);
64 CallBoolMethod(&method_call
, callback
);
67 // CryptohomeClient override.
68 virtual bool Unmount(bool *success
) OVERRIDE
{
69 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
70 cryptohome::kCryptohomeUnmount
);
71 return CallBoolMethodAndBlock(&method_call
, success
);
74 // CryptohomeClient override.
75 virtual void AsyncCheckKey(const std::string
& username
,
76 const std::string
& key
,
77 const AsyncMethodCallback
& callback
) OVERRIDE
{
78 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
79 cryptohome::kCryptohomeAsyncCheckKey
);
80 dbus::MessageWriter
writer(&method_call
);
81 writer
.AppendString(username
);
82 writer
.AppendString(key
);
83 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
84 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall
,
85 weak_ptr_factory_
.GetWeakPtr(),
89 // CryptohomeClient override.
90 virtual void AsyncMigrateKey(const std::string
& username
,
91 const std::string
& from_key
,
92 const std::string
& to_key
,
93 const AsyncMethodCallback
& callback
) OVERRIDE
{
94 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
95 cryptohome::kCryptohomeAsyncMigrateKey
);
96 dbus::MessageWriter
writer(&method_call
);
97 writer
.AppendString(username
);
98 writer
.AppendString(from_key
);
99 writer
.AppendString(to_key
);
100 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
101 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall
,
102 weak_ptr_factory_
.GetWeakPtr(),
106 // CryptohomeClient override.
107 virtual void AsyncRemove(const std::string
& username
,
108 const AsyncMethodCallback
& callback
) OVERRIDE
{
109 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
110 cryptohome::kCryptohomeAsyncRemove
);
111 dbus::MessageWriter
writer(&method_call
);
112 writer
.AppendString(username
);
113 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
114 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall
,
115 weak_ptr_factory_
.GetWeakPtr(),
119 // CryptohomeClient override.
120 virtual bool GetSystemSalt(std::vector
<uint8
>* salt
) OVERRIDE
{
121 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
122 cryptohome::kCryptohomeGetSystemSalt
);
123 scoped_ptr
<dbus::Response
> response(
124 blocking_method_caller_
.CallMethodAndBlock(&method_call
));
127 dbus::MessageReader
reader(response
.get());
130 if (!reader
.PopArrayOfBytes(&bytes
, &length
))
132 salt
->assign(bytes
, bytes
+ length
);
136 // CryptohomeClient override,
137 virtual void GetSanitizedUsername(
138 const std::string
& username
,
139 const StringDBusMethodCallback
& callback
) OVERRIDE
{
140 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
141 cryptohome::kCryptohomeGetSanitizedUsername
);
142 dbus::MessageWriter
writer(&method_call
);
143 writer
.AppendString(username
);
144 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
145 base::Bind(&CryptohomeClientImpl::OnStringMethod
,
146 weak_ptr_factory_
.GetWeakPtr(),
150 // CryptohomeClient override.
151 virtual void AsyncMount(const std::string
& username
,
152 const std::string
& key
,
154 const AsyncMethodCallback
& callback
) OVERRIDE
{
155 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
156 cryptohome::kCryptohomeAsyncMount
);
157 dbus::MessageWriter
writer(&method_call
);
158 writer
.AppendString(username
);
159 writer
.AppendString(key
);
160 writer
.AppendBool(flags
& cryptohome::CREATE_IF_MISSING
);
161 writer
.AppendBool(flags
& cryptohome::ENSURE_EPHEMERAL
);
162 // deprecated_tracked_subdirectories
163 writer
.AppendArrayOfStrings(std::vector
<std::string
>());
164 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
165 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall
,
166 weak_ptr_factory_
.GetWeakPtr(),
170 // CryptohomeClient override.
171 virtual void AsyncMountGuest(const AsyncMethodCallback
& callback
) OVERRIDE
{
172 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
173 cryptohome::kCryptohomeAsyncMountGuest
);
174 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
175 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall
,
176 weak_ptr_factory_
.GetWeakPtr(),
180 // CryptohomeClient override.
181 virtual void TpmIsReady(const BoolDBusMethodCallback
& callback
) OVERRIDE
{
182 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
183 cryptohome::kCryptohomeTpmIsReady
);
184 CallBoolMethod(&method_call
, callback
);
187 // CryptohomeClient override.
188 virtual void TpmIsEnabled(const BoolDBusMethodCallback
& callback
) OVERRIDE
{
189 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
190 cryptohome::kCryptohomeTpmIsEnabled
);
191 CallBoolMethod(&method_call
, callback
);
194 // CryptohomeClient override.
195 // TODO(hashimoto): Remove this method. crbug.com/141006
196 virtual bool CallTpmIsEnabledAndBlock(bool* enabled
) OVERRIDE
{
197 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
198 cryptohome::kCryptohomeTpmIsEnabled
);
199 return CallBoolMethodAndBlock(&method_call
, enabled
);
202 // CryptohomeClient override.
203 virtual void TpmGetPassword(
204 const StringDBusMethodCallback
& callback
) OVERRIDE
{
205 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
206 cryptohome::kCryptohomeTpmGetPassword
);
208 &method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
209 base::Bind(&CryptohomeClientImpl::OnStringMethod
,
210 weak_ptr_factory_
.GetWeakPtr(),
214 // CryptohomeClient override.
215 virtual void TpmIsOwned(const BoolDBusMethodCallback
& callback
) OVERRIDE
{
216 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
217 cryptohome::kCryptohomeTpmIsOwned
);
218 CallBoolMethod(&method_call
, callback
);
221 // CryptohomeClient override.
222 // TODO(hashimoto): Remove this method. crbug.com/141012
223 virtual bool CallTpmIsOwnedAndBlock(bool* owned
) OVERRIDE
{
224 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
225 cryptohome::kCryptohomeTpmIsOwned
);
226 return CallBoolMethodAndBlock(&method_call
, owned
);
229 // CryptohomeClient override.
230 virtual void TpmIsBeingOwned(const BoolDBusMethodCallback
& callback
)
232 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
233 cryptohome::kCryptohomeTpmIsBeingOwned
);
234 CallBoolMethod(&method_call
, callback
);
237 // CryptohomeClient override.
238 // TODO(hashimoto): Remove this method. crbug.com/141011
239 virtual bool CallTpmIsBeingOwnedAndBlock(bool* owning
) OVERRIDE
{
240 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
241 cryptohome::kCryptohomeTpmIsBeingOwned
);
242 return CallBoolMethodAndBlock(&method_call
, owning
);
245 // CryptohomeClient override.
246 virtual void TpmCanAttemptOwnership(
247 const VoidDBusMethodCallback
& callback
) OVERRIDE
{
248 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
249 cryptohome::kCryptohomeTpmCanAttemptOwnership
);
250 CallVoidMethod(&method_call
, callback
);
253 // CryptohomeClient overrides.
254 virtual void TpmClearStoredPassword(const VoidDBusMethodCallback
& callback
)
256 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
257 cryptohome::kCryptohomeTpmClearStoredPassword
);
258 CallVoidMethod(&method_call
, callback
);
261 // CryptohomeClient override.
262 // TODO(hashimoto): Remove this method. crbug.com/141010
263 virtual bool CallTpmClearStoredPasswordAndBlock() OVERRIDE
{
264 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
265 cryptohome::kCryptohomeTpmClearStoredPassword
);
266 scoped_ptr
<dbus::Response
> response(
267 blocking_method_caller_
.CallMethodAndBlock(&method_call
));
268 return response
.get() != NULL
;
271 // CryptohomeClient override.
272 virtual void Pkcs11IsTpmTokenReady(const BoolDBusMethodCallback
& callback
)
274 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
275 cryptohome::kCryptohomePkcs11IsTpmTokenReady
);
276 CallBoolMethod(&method_call
, callback
);
279 // CryptohomeClient override.
280 virtual void Pkcs11GetTpmTokenInfo(
281 const Pkcs11GetTpmTokenInfoCallback
& callback
) OVERRIDE
{
282 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
283 cryptohome::kCryptohomePkcs11GetTpmTokenInfo
);
285 &method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
287 &CryptohomeClientImpl::OnPkcs11GetTpmTokenInfo
,
288 weak_ptr_factory_
.GetWeakPtr(),
292 // CryptohomeClient override.
293 virtual bool InstallAttributesGet(const std::string
& name
,
294 std::vector
<uint8
>* value
,
295 bool* successful
) OVERRIDE
{
296 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
297 cryptohome::kCryptohomeInstallAttributesGet
);
298 dbus::MessageWriter
writer(&method_call
);
299 writer
.AppendString(name
);
300 scoped_ptr
<dbus::Response
> response(
301 blocking_method_caller_
.CallMethodAndBlock(&method_call
));
304 dbus::MessageReader
reader(response
.get());
307 if (!reader
.PopArrayOfBytes(&bytes
, &length
) ||
308 !reader
.PopBool(successful
))
310 value
->assign(bytes
, bytes
+ length
);
314 // CryptohomeClient override.
315 virtual bool InstallAttributesSet(const std::string
& name
,
316 const std::vector
<uint8
>& value
,
317 bool* successful
) OVERRIDE
{
318 dbus::MethodCall
method_call(cryptohome::kCryptohomeInterface
,
319 cryptohome::kCryptohomeInstallAttributesSet
);
320 dbus::MessageWriter
writer(&method_call
);
321 writer
.AppendString(name
);
322 writer
.AppendArrayOfBytes(value
.data(), value
.size());
323 return CallBoolMethodAndBlock(&method_call
, successful
);
326 // CryptohomeClient override.
327 virtual bool InstallAttributesFinalize(bool* successful
) OVERRIDE
{
328 dbus::MethodCall
method_call(
329 cryptohome::kCryptohomeInterface
,
330 cryptohome::kCryptohomeInstallAttributesFinalize
);
331 return CallBoolMethodAndBlock(&method_call
, successful
);
334 // CryptohomeClient override.
335 virtual void InstallAttributesIsReady(const BoolDBusMethodCallback
& callback
)
337 dbus::MethodCall
method_call(
338 cryptohome::kCryptohomeInterface
,
339 cryptohome::kCryptohomeInstallAttributesIsReady
);
340 return CallBoolMethod(&method_call
, callback
);
343 // CryptohomeClient override.
344 virtual bool InstallAttributesIsInvalid(bool* is_invalid
) OVERRIDE
{
345 dbus::MethodCall
method_call(
346 cryptohome::kCryptohomeInterface
,
347 cryptohome::kCryptohomeInstallAttributesIsInvalid
);
348 return CallBoolMethodAndBlock(&method_call
, is_invalid
);
351 // CryptohomeClient override.
352 virtual bool InstallAttributesIsFirstInstall(
353 bool* is_first_install
) OVERRIDE
{
354 dbus::MethodCall
method_call(
355 cryptohome::kCryptohomeInterface
,
356 cryptohome::kCryptohomeInstallAttributesIsFirstInstall
);
357 return CallBoolMethodAndBlock(&method_call
, is_first_install
);
360 // CryptohomeClient override.
361 virtual void TpmAttestationIsPrepared(
362 const BoolDBusMethodCallback
& callback
) OVERRIDE
{
363 dbus::MethodCall
method_call(
364 cryptohome::kCryptohomeInterface
,
365 cryptohome::kCryptohomeTpmIsAttestationPrepared
);
366 return CallBoolMethod(&method_call
, callback
);
369 // CryptohomeClient override.
370 virtual void TpmAttestationIsEnrolled(
371 const BoolDBusMethodCallback
& callback
) OVERRIDE
{
372 dbus::MethodCall
method_call(
373 cryptohome::kCryptohomeInterface
,
374 cryptohome::kCryptohomeTpmIsAttestationEnrolled
);
375 return CallBoolMethod(&method_call
, callback
);
378 // CryptohomeClient override.
379 virtual void AsyncTpmAttestationCreateEnrollRequest(
380 const AsyncMethodCallback
& callback
) OVERRIDE
{
381 dbus::MethodCall
method_call(
382 cryptohome::kCryptohomeInterface
,
383 cryptohome::kCryptohomeAsyncTpmAttestationCreateEnrollRequest
);
384 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
385 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall
,
386 weak_ptr_factory_
.GetWeakPtr(),
390 // CryptohomeClient override.
391 virtual void AsyncTpmAttestationEnroll(
392 const std::string
& pca_response
,
393 const AsyncMethodCallback
& callback
) OVERRIDE
{
394 dbus::MethodCall
method_call(
395 cryptohome::kCryptohomeInterface
,
396 cryptohome::kCryptohomeAsyncTpmAttestationEnroll
);
397 dbus::MessageWriter
writer(&method_call
);
398 writer
.AppendArrayOfBytes(
399 reinterpret_cast<const uint8
*>(pca_response
.data()),
400 pca_response
.size());
401 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
402 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall
,
403 weak_ptr_factory_
.GetWeakPtr(),
407 // CryptohomeClient override.
408 virtual void AsyncTpmAttestationCreateCertRequest(
410 const AsyncMethodCallback
& callback
) OVERRIDE
{
411 dbus::MethodCall
method_call(
412 cryptohome::kCryptohomeInterface
,
413 cryptohome::kCryptohomeAsyncTpmAttestationCreateCertRequest
);
414 dbus::MessageWriter
writer(&method_call
);
415 bool include_stable_id
= (options
& INCLUDE_STABLE_ID
);
416 writer
.AppendBool(include_stable_id
);
417 bool include_device_state
= (options
& INCLUDE_DEVICE_STATE
);
418 writer
.AppendBool(include_device_state
);
419 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
420 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall
,
421 weak_ptr_factory_
.GetWeakPtr(),
425 // CryptohomeClient override.
426 virtual void AsyncTpmAttestationFinishCertRequest(
427 const std::string
& pca_response
,
428 AttestationKeyType key_type
,
429 const std::string
& key_name
,
430 const AsyncMethodCallback
& callback
) OVERRIDE
{
431 dbus::MethodCall
method_call(
432 cryptohome::kCryptohomeInterface
,
433 cryptohome::kCryptohomeAsyncTpmAttestationFinishCertRequest
);
434 dbus::MessageWriter
writer(&method_call
);
435 writer
.AppendArrayOfBytes(
436 reinterpret_cast<const uint8
*>(pca_response
.data()),
437 pca_response
.size());
438 bool is_user_specific
= (key_type
== USER_KEY
);
439 writer
.AppendBool(is_user_specific
);
440 writer
.AppendString(key_name
);
441 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
442 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall
,
443 weak_ptr_factory_
.GetWeakPtr(),
447 // CryptohomeClient override.
448 virtual void TpmAttestationDoesKeyExist(
449 AttestationKeyType key_type
,
450 const std::string
& key_name
,
451 const BoolDBusMethodCallback
& callback
) OVERRIDE
{
452 dbus::MethodCall
method_call(
453 cryptohome::kCryptohomeInterface
,
454 cryptohome::kCryptohomeTpmAttestationDoesKeyExist
);
455 dbus::MessageWriter
writer(&method_call
);
456 bool is_user_specific
= (key_type
== USER_KEY
);
457 writer
.AppendBool(is_user_specific
);
458 writer
.AppendString(key_name
);
459 CallBoolMethod(&method_call
, callback
);
462 // CryptohomeClient override.
463 virtual void TpmAttestationGetCertificate(
464 AttestationKeyType key_type
,
465 const std::string
& key_name
,
466 const DataMethodCallback
& callback
) OVERRIDE
{
467 dbus::MethodCall
method_call(
468 cryptohome::kCryptohomeInterface
,
469 cryptohome::kCryptohomeTpmAttestationGetCertificate
);
470 dbus::MessageWriter
writer(&method_call
);
471 bool is_user_specific
= (key_type
== USER_KEY
);
472 writer
.AppendBool(is_user_specific
);
473 writer
.AppendString(key_name
);
474 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
475 base::Bind(&CryptohomeClientImpl::OnDataMethod
,
476 weak_ptr_factory_
.GetWeakPtr(),
480 // CryptohomeClient override.
481 virtual void TpmAttestationGetPublicKey(
482 AttestationKeyType key_type
,
483 const std::string
& key_name
,
484 const DataMethodCallback
& callback
) OVERRIDE
{
485 dbus::MethodCall
method_call(
486 cryptohome::kCryptohomeInterface
,
487 cryptohome::kCryptohomeTpmAttestationGetPublicKey
);
488 dbus::MessageWriter
writer(&method_call
);
489 bool is_user_specific
= (key_type
== USER_KEY
);
490 writer
.AppendBool(is_user_specific
);
491 writer
.AppendString(key_name
);
492 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
493 base::Bind(&CryptohomeClientImpl::OnDataMethod
,
494 weak_ptr_factory_
.GetWeakPtr(),
498 // CryptohomeClient override.
499 virtual void TpmAttestationRegisterKey(
500 AttestationKeyType key_type
,
501 const std::string
& key_name
,
502 const AsyncMethodCallback
& callback
) OVERRIDE
{
503 dbus::MethodCall
method_call(
504 cryptohome::kCryptohomeInterface
,
505 cryptohome::kCryptohomeTpmAttestationRegisterKey
);
506 dbus::MessageWriter
writer(&method_call
);
507 bool is_user_specific
= (key_type
== USER_KEY
);
508 writer
.AppendBool(is_user_specific
);
509 writer
.AppendString(key_name
);
510 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
511 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall
,
512 weak_ptr_factory_
.GetWeakPtr(),
516 // CryptohomeClient override.
517 virtual void TpmAttestationSignEnterpriseChallenge(
518 AttestationKeyType key_type
,
519 const std::string
& key_name
,
520 const std::string
& domain
,
521 const std::string
& device_id
,
522 AttestationChallengeOptions options
,
523 const std::string
& challenge
,
524 const AsyncMethodCallback
& callback
) OVERRIDE
{
525 dbus::MethodCall
method_call(
526 cryptohome::kCryptohomeInterface
,
527 cryptohome::kCryptohomeTpmAttestationSignEnterpriseChallenge
);
528 dbus::MessageWriter
writer(&method_call
);
529 bool is_user_specific
= (key_type
== USER_KEY
);
530 writer
.AppendBool(is_user_specific
);
531 writer
.AppendString(key_name
);
532 writer
.AppendString(domain
);
533 writer
.AppendString(device_id
);
534 bool include_signed_public_key
= (options
& INCLUDE_SIGNED_PUBLIC_KEY
);
535 writer
.AppendBool(include_signed_public_key
);
536 writer
.AppendString(challenge
);
537 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
538 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall
,
539 weak_ptr_factory_
.GetWeakPtr(),
543 // CryptohomeClient override.
544 virtual void TpmAttestationSignSimpleChallenge(
545 AttestationKeyType key_type
,
546 const std::string
& key_name
,
547 const std::string
& challenge
,
548 const AsyncMethodCallback
& callback
) OVERRIDE
{
549 dbus::MethodCall
method_call(
550 cryptohome::kCryptohomeInterface
,
551 cryptohome::kCryptohomeTpmAttestationSignEnterpriseChallenge
);
552 dbus::MessageWriter
writer(&method_call
);
553 bool is_user_specific
= (key_type
== USER_KEY
);
554 writer
.AppendBool(is_user_specific
);
555 writer
.AppendString(key_name
);
556 writer
.AppendString(challenge
);
557 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
558 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall
,
559 weak_ptr_factory_
.GetWeakPtr(),
564 // Handles the result of AsyncXXX methods.
565 void OnAsyncMethodCall(const AsyncMethodCallback
& callback
,
566 dbus::Response
* response
) {
569 dbus::MessageReader
reader(response
);
571 if (!reader
.PopInt32(&async_id
)) {
572 LOG(ERROR
) << "Invalid response: " << response
->ToString();
575 callback
.Run(async_id
);
578 // Calls a method without result values.
579 void CallVoidMethod(dbus::MethodCall
* method_call
,
580 const VoidDBusMethodCallback
& callback
) {
581 proxy_
->CallMethod(method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
582 base::Bind(&CryptohomeClientImpl::OnVoidMethod
,
583 weak_ptr_factory_
.GetWeakPtr(),
587 void OnVoidMethod(const VoidDBusMethodCallback
& callback
,
588 dbus::Response
* response
) {
590 callback
.Run(DBUS_METHOD_CALL_FAILURE
);
593 callback
.Run(DBUS_METHOD_CALL_SUCCESS
);
596 // Calls a method with a bool value reult and block.
597 bool CallBoolMethodAndBlock(dbus::MethodCall
* method_call
,
599 scoped_ptr
<dbus::Response
> response(
600 blocking_method_caller_
.CallMethodAndBlock(method_call
));
603 dbus::MessageReader
reader(response
.get());
604 return reader
.PopBool(result
);
607 // Calls a method with a bool value result.
608 void CallBoolMethod(dbus::MethodCall
* method_call
,
609 const BoolDBusMethodCallback
& callback
) {
610 proxy_
->CallMethod(method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
612 &CryptohomeClientImpl::OnBoolMethod
,
613 weak_ptr_factory_
.GetWeakPtr(),
617 // Handles responses for methods with a bool value result.
618 void OnBoolMethod(const BoolDBusMethodCallback
& callback
,
619 dbus::Response
* response
) {
621 callback
.Run(DBUS_METHOD_CALL_FAILURE
, false);
624 dbus::MessageReader
reader(response
);
626 if (!reader
.PopBool(&result
)) {
627 callback
.Run(DBUS_METHOD_CALL_FAILURE
, false);
630 callback
.Run(DBUS_METHOD_CALL_SUCCESS
, result
);
633 // Handles responses for methods with a string value result.
634 void OnStringMethod(const StringDBusMethodCallback
& callback
,
635 dbus::Response
* response
) {
637 callback
.Run(DBUS_METHOD_CALL_FAILURE
, std::string());
640 dbus::MessageReader
reader(response
);
642 if (!reader
.PopString(&result
)) {
643 callback
.Run(DBUS_METHOD_CALL_FAILURE
, std::string());
646 callback
.Run(DBUS_METHOD_CALL_SUCCESS
, result
);
649 // Handles responses for methods with a bool result and data.
650 void OnDataMethod(const DataMethodCallback
& callback
,
651 dbus::Response
* response
) {
653 callback
.Run(DBUS_METHOD_CALL_FAILURE
, false, std::string());
656 dbus::MessageReader
reader(response
);
658 if (!reader
.PopBool(&result
)) {
659 callback
.Run(DBUS_METHOD_CALL_FAILURE
, false, std::string());
663 if (!reader
.PopString(&data
)) {
664 callback
.Run(DBUS_METHOD_CALL_FAILURE
, false, std::string());
667 callback
.Run(DBUS_METHOD_CALL_SUCCESS
, result
, data
);
670 // Handles responses for Pkcs11GetTpmtTokenInfo.
671 void OnPkcs11GetTpmTokenInfo(const Pkcs11GetTpmTokenInfoCallback
& callback
,
672 dbus::Response
* response
) {
674 callback
.Run(DBUS_METHOD_CALL_FAILURE
, std::string(), std::string());
677 dbus::MessageReader
reader(response
);
679 std::string user_pin
;
680 if (!reader
.PopString(&label
) || !reader
.PopString(&user_pin
)) {
681 callback
.Run(DBUS_METHOD_CALL_FAILURE
, std::string(), std::string());
684 callback
.Run(DBUS_METHOD_CALL_SUCCESS
, label
, user_pin
);
687 // Handles AsyncCallStatus signal.
688 void OnAsyncCallStatus(dbus::Signal
* signal
) {
689 dbus::MessageReader
reader(signal
);
691 bool return_status
= false;
693 if (!reader
.PopInt32(&async_id
) ||
694 !reader
.PopBool(&return_status
) ||
695 !reader
.PopInt32(&return_code
)) {
696 LOG(ERROR
) << "Invalid signal: " << signal
->ToString();
699 if (!async_call_status_handler_
.is_null())
700 async_call_status_handler_
.Run(async_id
, return_status
, return_code
);
703 // Handles AsyncCallStatusWithData signal.
704 void OnAsyncCallStatusWithData(dbus::Signal
* signal
) {
705 dbus::MessageReader
reader(signal
);
707 bool return_status
= false;
708 uint8
* return_data_buffer
= NULL
;
709 size_t return_data_length
= 0;
710 if (!reader
.PopInt32(&async_id
) ||
711 !reader
.PopBool(&return_status
) ||
712 !reader
.PopArrayOfBytes(&return_data_buffer
, &return_data_length
)) {
713 LOG(ERROR
) << "Invalid signal: " << signal
->ToString();
716 if (!async_call_status_data_handler_
.is_null()) {
717 std::string
return_data(reinterpret_cast<char*>(return_data_buffer
),
719 async_call_status_data_handler_
.Run(async_id
, return_status
, return_data
);
723 // Handles the result of signal connection setup.
724 void OnSignalConnected(const std::string
& interface
,
725 const std::string
& signal
,
727 LOG_IF(ERROR
, !succeeded
) << "Connect to " << interface
<< " " <<
728 signal
<< " failed.";
731 dbus::ObjectProxy
* proxy_
;
732 BlockingMethodCaller blocking_method_caller_
;
733 AsyncCallStatusHandler async_call_status_handler_
;
734 AsyncCallStatusWithDataHandler async_call_status_data_handler_
;
736 // Note: This should remain the last member so it'll be destroyed and
737 // invalidate its weak pointers before any other members are destroyed.
738 base::WeakPtrFactory
<CryptohomeClientImpl
> weak_ptr_factory_
;
740 DISALLOW_COPY_AND_ASSIGN(CryptohomeClientImpl
);
743 // A stub implementaion of CryptohomeClient.
744 class CryptohomeClientStubImpl
: public CryptohomeClient
{
746 CryptohomeClientStubImpl()
748 tpm_is_ready_counter_(0),
750 weak_ptr_factory_(this) {
753 virtual ~CryptohomeClientStubImpl() {}
755 // CryptohomeClient override.
756 virtual void SetAsyncCallStatusHandlers(
757 const AsyncCallStatusHandler
& handler
,
758 const AsyncCallStatusWithDataHandler
& data_handler
) OVERRIDE
{
759 async_call_status_handler_
= handler
;
760 async_call_status_data_handler_
= data_handler
;
763 // CryptohomeClient override.
764 virtual void ResetAsyncCallStatusHandlers() OVERRIDE
{
765 async_call_status_handler_
.Reset();
766 async_call_status_data_handler_
.Reset();
769 // CryptohomeClient override.
770 virtual void IsMounted(const BoolDBusMethodCallback
& callback
) OVERRIDE
{
771 MessageLoop::current()->PostTask(
772 FROM_HERE
, base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
, true));
775 // CryptohomeClient override.
776 virtual bool Unmount(bool* success
) OVERRIDE
{
781 // CryptohomeClient override.
782 virtual void AsyncCheckKey(const std::string
& username
,
783 const std::string
& key
,
784 const AsyncMethodCallback
& callback
) OVERRIDE
{
785 ReturnAsyncMethodResult(callback
, false);
788 // CryptohomeClient override.
789 virtual void AsyncMigrateKey(const std::string
& username
,
790 const std::string
& from_key
,
791 const std::string
& to_key
,
792 const AsyncMethodCallback
& callback
) OVERRIDE
{
793 ReturnAsyncMethodResult(callback
, false);
796 // CryptohomeClient override.
797 virtual void AsyncRemove(const std::string
& username
,
798 const AsyncMethodCallback
& callback
) OVERRIDE
{
799 ReturnAsyncMethodResult(callback
, false);
802 // CryptohomeClient override.
803 virtual bool GetSystemSalt(std::vector
<uint8
>* salt
) OVERRIDE
{
804 const char kStubSystemSalt
[] = "stub_system_salt";
805 salt
->assign(kStubSystemSalt
,
806 kStubSystemSalt
+ arraysize(kStubSystemSalt
) - 1);
810 // CryptohomeClient override.
811 virtual void GetSanitizedUsername(
812 const std::string
& username
,
813 const StringDBusMethodCallback
& callback
) OVERRIDE
{
814 // Even for stub implementation we have to return different values
815 // so that multi-profiles would work.
816 std::string sanitized_username
= username
+ "-profile";
817 MessageLoop::current()->PostTask(
819 base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
, sanitized_username
));
822 // CryptohomeClient override.
823 virtual void AsyncMount(const std::string
& username
,
824 const std::string
& key
,
826 const AsyncMethodCallback
& callback
) OVERRIDE
{
827 ReturnAsyncMethodResult(callback
, false);
830 // CryptohomeClient override.
831 virtual void AsyncMountGuest(const AsyncMethodCallback
& callback
) OVERRIDE
{
832 ReturnAsyncMethodResult(callback
, false);
835 // CryptohomeClient override.
836 virtual void TpmIsReady(const BoolDBusMethodCallback
& callback
) OVERRIDE
{
837 MessageLoop::current()->PostTask(
838 FROM_HERE
, base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
, true));
841 // CryptohomeClient override.
842 virtual void TpmIsEnabled(const BoolDBusMethodCallback
& callback
) OVERRIDE
{
843 MessageLoop::current()->PostTask(
844 FROM_HERE
, base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
, true));
847 // CryptohomeClient override.
848 virtual bool CallTpmIsEnabledAndBlock(bool* enabled
) OVERRIDE
{
853 // CryptohomeClient override.
854 virtual void TpmGetPassword(
855 const StringDBusMethodCallback
& callback
) OVERRIDE
{
856 const char kStubTpmPassword
[] = "Stub-TPM-password";
857 MessageLoop::current()->PostTask(
859 base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
, kStubTpmPassword
));
862 // CryptohomeClient override.
863 virtual void TpmIsOwned(const BoolDBusMethodCallback
& callback
) OVERRIDE
{
864 MessageLoop::current()->PostTask(
865 FROM_HERE
, base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
, true));
868 // CryptohomeClient override.
869 virtual bool CallTpmIsOwnedAndBlock(bool* owned
) OVERRIDE
{
874 // CryptohomeClient override.
875 virtual void TpmIsBeingOwned(const BoolDBusMethodCallback
& callback
)
877 MessageLoop::current()->PostTask(
878 FROM_HERE
, base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
, true));
881 // CryptohomeClient override.
882 virtual bool CallTpmIsBeingOwnedAndBlock(bool* owning
) OVERRIDE
{
887 // CryptohomeClient override.
888 virtual void TpmCanAttemptOwnership(
889 const VoidDBusMethodCallback
& callback
) OVERRIDE
{
890 MessageLoop::current()->PostTask(
891 FROM_HERE
, base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
));
894 // CryptohomeClient override.
895 virtual void TpmClearStoredPassword(const VoidDBusMethodCallback
& callback
)
897 MessageLoop::current()->PostTask(
898 FROM_HERE
, base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
));
901 // CryptohomeClient override.
902 virtual bool CallTpmClearStoredPasswordAndBlock() OVERRIDE
{ return true; }
904 // CryptohomeClient override.
905 virtual void Pkcs11IsTpmTokenReady(
906 const BoolDBusMethodCallback
& callback
) OVERRIDE
{
907 MessageLoop::current()->PostTask(
908 FROM_HERE
, base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
, true));
911 // CryptohomeClient override.
912 virtual void Pkcs11GetTpmTokenInfo(
913 const Pkcs11GetTpmTokenInfoCallback
& callback
) OVERRIDE
{
914 const char kStubLabel
[] = "Stub TPM Token";
915 const char kStubUserPin
[] = "012345";
916 MessageLoop::current()->PostTask(
917 FROM_HERE
, base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
, kStubLabel
,
921 // CryptohomeClient override.
922 virtual bool InstallAttributesGet(const std::string
& name
,
923 std::vector
<uint8
>* value
,
924 bool* successful
) OVERRIDE
{
925 if (install_attrs_
.find(name
) != install_attrs_
.end()) {
926 *value
= install_attrs_
[name
];
935 // CryptohomeClient override.
936 virtual bool InstallAttributesSet(const std::string
& name
,
937 const std::vector
<uint8
>& value
,
938 bool* successful
) OVERRIDE
{
939 install_attrs_
[name
] = value
;
944 // CryptohomeClient override.
945 virtual bool InstallAttributesFinalize(bool* successful
) OVERRIDE
{
951 // CryptohomeClient override.
952 virtual void InstallAttributesIsReady(const BoolDBusMethodCallback
& callback
)
954 MessageLoop::current()->PostTask(
955 FROM_HERE
, base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
, true));
958 // CryptohomeClient override.
959 virtual bool InstallAttributesIsInvalid(bool* is_invalid
) OVERRIDE
{
964 // CryptohomeClient override.
965 virtual bool InstallAttributesIsFirstInstall(
966 bool* is_first_install
) OVERRIDE
{
967 *is_first_install
= !locked_
;
971 // CryptohomeClient override.
972 virtual void TpmAttestationIsPrepared(
973 const BoolDBusMethodCallback
& callback
) OVERRIDE
{
974 MessageLoop::current()->PostTask(
975 FROM_HERE
, base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
, true));
978 // CryptohomeClient override.
979 virtual void TpmAttestationIsEnrolled(
980 const BoolDBusMethodCallback
& callback
) OVERRIDE
{
981 MessageLoop::current()->PostTask(
982 FROM_HERE
, base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
, true));
985 // CryptohomeClient override.
986 virtual void AsyncTpmAttestationCreateEnrollRequest(
987 const AsyncMethodCallback
& callback
) OVERRIDE
{
988 ReturnAsyncMethodResult(callback
, true);
991 // CryptohomeClient override.
992 virtual void AsyncTpmAttestationEnroll(
993 const std::string
& pca_response
,
994 const AsyncMethodCallback
& callback
) OVERRIDE
{
995 ReturnAsyncMethodResult(callback
, false);
998 // CryptohomeClient override.
999 virtual void AsyncTpmAttestationCreateCertRequest(
1001 const AsyncMethodCallback
& callback
) OVERRIDE
{
1002 ReturnAsyncMethodResult(callback
, true);
1005 // CryptohomeClient override.
1006 virtual void AsyncTpmAttestationFinishCertRequest(
1007 const std::string
& pca_response
,
1008 AttestationKeyType key_type
,
1009 const std::string
& key_name
,
1010 const AsyncMethodCallback
& callback
) OVERRIDE
{
1011 ReturnAsyncMethodResult(callback
, true);
1014 // CryptohomeClient override.
1015 virtual void TpmAttestationDoesKeyExist(
1016 AttestationKeyType key_type
,
1017 const std::string
& key_name
,
1018 const BoolDBusMethodCallback
& callback
) OVERRIDE
{
1019 MessageLoop::current()->PostTask(
1021 base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
, false));
1024 // CryptohomeClient override.
1025 virtual void TpmAttestationGetCertificate(
1026 AttestationKeyType key_type
,
1027 const std::string
& key_name
,
1028 const DataMethodCallback
& callback
) OVERRIDE
{
1029 MessageLoop::current()->PostTask(
1031 base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
, false, std::string()));
1034 // CryptohomeClient override.
1035 virtual void TpmAttestationGetPublicKey(
1036 AttestationKeyType key_type
,
1037 const std::string
& key_name
,
1038 const DataMethodCallback
& callback
) OVERRIDE
{
1039 MessageLoop::current()->PostTask(
1041 base::Bind(callback
, DBUS_METHOD_CALL_SUCCESS
, false, std::string()));
1044 // CryptohomeClient override.
1045 virtual void TpmAttestationRegisterKey(
1046 AttestationKeyType key_type
,
1047 const std::string
& key_name
,
1048 const AsyncMethodCallback
& callback
) OVERRIDE
{
1049 ReturnAsyncMethodResult(callback
, true);
1052 // CryptohomeClient override.
1053 virtual void TpmAttestationSignEnterpriseChallenge(
1054 AttestationKeyType key_type
,
1055 const std::string
& key_name
,
1056 const std::string
& domain
,
1057 const std::string
& device_id
,
1058 AttestationChallengeOptions options
,
1059 const std::string
& challenge
,
1060 const AsyncMethodCallback
& callback
) OVERRIDE
{
1061 ReturnAsyncMethodResult(callback
, true);
1064 // CryptohomeClient override.
1065 virtual void TpmAttestationSignSimpleChallenge(
1066 AttestationKeyType key_type
,
1067 const std::string
& key_name
,
1068 const std::string
& challenge
,
1069 const AsyncMethodCallback
& callback
) OVERRIDE
{
1070 ReturnAsyncMethodResult(callback
, true);
1074 // Posts tasks which return fake results to the UI thread.
1075 void ReturnAsyncMethodResult(const AsyncMethodCallback
& callback
,
1076 bool returns_data
) {
1077 MessageLoop::current()->PostTask(
1079 base::Bind(&CryptohomeClientStubImpl::ReturnAsyncMethodResultInternal
,
1080 weak_ptr_factory_
.GetWeakPtr(),
1085 // This method is used to implement ReturnAsyncMethodResult.
1086 void ReturnAsyncMethodResultInternal(const AsyncMethodCallback
& callback
,
1087 bool returns_data
) {
1088 callback
.Run(async_call_id_
);
1089 if (!returns_data
&& !async_call_status_handler_
.is_null()) {
1090 MessageLoop::current()->PostTask(
1092 base::Bind(async_call_status_handler_
,
1095 cryptohome::MOUNT_ERROR_NONE
));
1096 } else if (returns_data
&& !async_call_status_data_handler_
.is_null()) {
1097 MessageLoop::current()->PostTask(
1099 base::Bind(async_call_status_data_handler_
,
1108 AsyncCallStatusHandler async_call_status_handler_
;
1109 AsyncCallStatusWithDataHandler async_call_status_data_handler_
;
1110 int tpm_is_ready_counter_
;
1111 std::map
<std::string
, std::vector
<uint8
> > install_attrs_
;
1113 base::WeakPtrFactory
<CryptohomeClientStubImpl
> weak_ptr_factory_
;
1115 DISALLOW_COPY_AND_ASSIGN(CryptohomeClientStubImpl
);
1120 ////////////////////////////////////////////////////////////////////////////////
1123 CryptohomeClient::CryptohomeClient() {}
1125 CryptohomeClient::~CryptohomeClient() {}
1128 CryptohomeClient
* CryptohomeClient::Create(DBusClientImplementationType type
,
1130 if (type
== REAL_DBUS_CLIENT_IMPLEMENTATION
)
1131 return new CryptohomeClientImpl(bus
);
1132 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION
, type
);
1133 return new CryptohomeClientStubImpl();
1136 } // namespace chromeos