Fix compilation error triggered with -Werror=sign-compare
[chromium-blink-merge.git] / chromeos / dbus / cryptohome_client.cc
blob4bf3c7885631ffd69bf7f57dc24f5581c7b0a49a
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"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "chromeos/cryptohome/async_method_caller.h"
12 #include "chromeos/dbus/blocking_method_caller.h"
13 #include "chromeos/dbus/cryptohome/key.pb.h"
14 #include "chromeos/dbus/cryptohome/rpc.pb.h"
15 #include "dbus/bus.h"
16 #include "dbus/message.h"
17 #include "dbus/object_path.h"
18 #include "dbus/object_proxy.h"
19 #include "third_party/cros_system_api/dbus/service_constants.h"
21 namespace chromeos {
23 namespace {
25 // This suffix is appended to user_id to get hash in stub implementation:
26 // stub_hash = "[user_id]-hash";
27 static const char kUserIdStubHashSuffix[] = "-hash";
29 // The CryptohomeClient implementation.
30 class CryptohomeClientImpl : public CryptohomeClient {
31 public:
32 CryptohomeClientImpl() : proxy_(NULL), weak_ptr_factory_(this) {}
34 // CryptohomeClient override.
35 virtual void SetAsyncCallStatusHandlers(
36 const AsyncCallStatusHandler& handler,
37 const AsyncCallStatusWithDataHandler& data_handler) OVERRIDE {
38 async_call_status_handler_ = handler;
39 async_call_status_data_handler_ = data_handler;
42 // CryptohomeClient override.
43 virtual void ResetAsyncCallStatusHandlers() OVERRIDE {
44 async_call_status_handler_.Reset();
45 async_call_status_data_handler_.Reset();
48 // CryptohomeClient override.
49 virtual void WaitForServiceToBeAvailable(
50 const WaitForServiceToBeAvailableCallback& callback) OVERRIDE {
51 proxy_->WaitForServiceToBeAvailable(callback);
54 // CryptohomeClient override.
55 virtual void IsMounted(const BoolDBusMethodCallback& callback) OVERRIDE {
56 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
57 cryptohome::kCryptohomeIsMounted);
58 CallBoolMethod(&method_call, callback);
61 // CryptohomeClient override.
62 virtual bool Unmount(bool *success) OVERRIDE {
63 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
64 cryptohome::kCryptohomeUnmount);
65 return CallBoolMethodAndBlock(&method_call, success);
68 // CryptohomeClient override.
69 virtual void AsyncCheckKey(const std::string& username,
70 const std::string& key,
71 const AsyncMethodCallback& callback) OVERRIDE {
72 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
73 cryptohome::kCryptohomeAsyncCheckKey);
74 dbus::MessageWriter writer(&method_call);
75 writer.AppendString(username);
76 writer.AppendString(key);
77 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
78 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall,
79 weak_ptr_factory_.GetWeakPtr(),
80 callback));
83 // CryptohomeClient override.
84 virtual void AsyncMigrateKey(const std::string& username,
85 const std::string& from_key,
86 const std::string& to_key,
87 const AsyncMethodCallback& callback) OVERRIDE {
88 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
89 cryptohome::kCryptohomeAsyncMigrateKey);
90 dbus::MessageWriter writer(&method_call);
91 writer.AppendString(username);
92 writer.AppendString(from_key);
93 writer.AppendString(to_key);
94 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
95 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall,
96 weak_ptr_factory_.GetWeakPtr(),
97 callback));
100 // CryptohomeClient override.
101 virtual void AsyncRemove(const std::string& username,
102 const AsyncMethodCallback& callback) OVERRIDE {
103 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
104 cryptohome::kCryptohomeAsyncRemove);
105 dbus::MessageWriter writer(&method_call);
106 writer.AppendString(username);
107 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
108 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall,
109 weak_ptr_factory_.GetWeakPtr(),
110 callback));
113 // CryptohomeClient override.
114 virtual void GetSystemSalt(const GetSystemSaltCallback& callback) OVERRIDE {
115 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
116 cryptohome::kCryptohomeGetSystemSalt);
117 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
118 base::Bind(&CryptohomeClientImpl::OnGetSystemSalt,
119 weak_ptr_factory_.GetWeakPtr(),
120 callback));
123 // CryptohomeClient override,
124 virtual void GetSanitizedUsername(
125 const std::string& username,
126 const StringDBusMethodCallback& callback) OVERRIDE {
127 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
128 cryptohome::kCryptohomeGetSanitizedUsername);
129 dbus::MessageWriter writer(&method_call);
130 writer.AppendString(username);
131 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
132 base::Bind(&CryptohomeClientImpl::OnStringMethod,
133 weak_ptr_factory_.GetWeakPtr(),
134 callback));
137 // CryptohomeClient override.
138 virtual std::string BlockingGetSanitizedUsername(
139 const std::string& username) OVERRIDE {
140 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
141 cryptohome::kCryptohomeGetSanitizedUsername);
142 dbus::MessageWriter writer(&method_call);
143 writer.AppendString(username);
145 scoped_ptr<dbus::Response> response =
146 blocking_method_caller_->CallMethodAndBlock(&method_call);
148 std::string sanitized_username;
149 if (response) {
150 dbus::MessageReader reader(response.get());
151 reader.PopString(&sanitized_username);
154 return sanitized_username;
157 // CryptohomeClient override.
158 virtual void AsyncMount(const std::string& username,
159 const std::string& key,
160 int flags,
161 const AsyncMethodCallback& callback) OVERRIDE {
162 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
163 cryptohome::kCryptohomeAsyncMount);
164 dbus::MessageWriter writer(&method_call);
165 writer.AppendString(username);
166 writer.AppendString(key);
167 writer.AppendBool(flags & cryptohome::CREATE_IF_MISSING);
168 writer.AppendBool(flags & cryptohome::ENSURE_EPHEMERAL);
169 // deprecated_tracked_subdirectories
170 writer.AppendArrayOfStrings(std::vector<std::string>());
171 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
172 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall,
173 weak_ptr_factory_.GetWeakPtr(),
174 callback));
177 // CryptohomeClient override.
178 virtual void AsyncAddKey(const std::string& username,
179 const std::string& key,
180 const std::string& new_key,
181 const AsyncMethodCallback& callback) OVERRIDE {
182 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
183 cryptohome::kCryptohomeAsyncAddKey);
184 dbus::MessageWriter writer(&method_call);
185 writer.AppendString(username);
186 writer.AppendString(key);
187 writer.AppendString(new_key);
188 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
189 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall,
190 weak_ptr_factory_.GetWeakPtr(),
191 callback));
194 // CryptohomeClient override.
195 virtual void AsyncMountGuest(const AsyncMethodCallback& callback) OVERRIDE {
196 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
197 cryptohome::kCryptohomeAsyncMountGuest);
198 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
199 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall,
200 weak_ptr_factory_.GetWeakPtr(),
201 callback));
204 // CryptohomeClient override.
205 virtual void AsyncMountPublic(const std::string& public_mount_id,
206 int flags,
207 const AsyncMethodCallback& callback) OVERRIDE {
208 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
209 cryptohome::kCryptohomeAsyncMountPublic);
210 dbus::MessageWriter writer(&method_call);
211 writer.AppendString(public_mount_id);
212 writer.AppendBool(flags & cryptohome::CREATE_IF_MISSING);
213 writer.AppendBool(flags & cryptohome::ENSURE_EPHEMERAL);
214 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
215 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall,
216 weak_ptr_factory_.GetWeakPtr(),
217 callback));
220 // CryptohomeClient override.
221 virtual void TpmIsReady(const BoolDBusMethodCallback& callback) OVERRIDE {
222 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
223 cryptohome::kCryptohomeTpmIsReady);
224 CallBoolMethod(&method_call, callback);
227 // CryptohomeClient override.
228 virtual void TpmIsEnabled(const BoolDBusMethodCallback& callback) OVERRIDE {
229 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
230 cryptohome::kCryptohomeTpmIsEnabled);
231 CallBoolMethod(&method_call, callback);
234 // CryptohomeClient override.
235 // TODO(hashimoto): Remove this method. crbug.com/141006
236 virtual bool CallTpmIsEnabledAndBlock(bool* enabled) OVERRIDE {
237 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
238 cryptohome::kCryptohomeTpmIsEnabled);
239 return CallBoolMethodAndBlock(&method_call, enabled);
242 // CryptohomeClient override.
243 virtual void TpmGetPassword(
244 const StringDBusMethodCallback& callback) OVERRIDE {
245 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
246 cryptohome::kCryptohomeTpmGetPassword);
247 proxy_->CallMethod(
248 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
249 base::Bind(&CryptohomeClientImpl::OnStringMethod,
250 weak_ptr_factory_.GetWeakPtr(),
251 callback));
254 // CryptohomeClient override.
255 virtual void TpmIsOwned(const BoolDBusMethodCallback& callback) OVERRIDE {
256 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
257 cryptohome::kCryptohomeTpmIsOwned);
258 CallBoolMethod(&method_call, callback);
261 // CryptohomeClient override.
262 // TODO(hashimoto): Remove this method. crbug.com/141012
263 virtual bool CallTpmIsOwnedAndBlock(bool* owned) OVERRIDE {
264 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
265 cryptohome::kCryptohomeTpmIsOwned);
266 return CallBoolMethodAndBlock(&method_call, owned);
269 // CryptohomeClient override.
270 virtual void TpmIsBeingOwned(const BoolDBusMethodCallback& callback)
271 OVERRIDE {
272 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
273 cryptohome::kCryptohomeTpmIsBeingOwned);
274 CallBoolMethod(&method_call, callback);
277 // CryptohomeClient override.
278 // TODO(hashimoto): Remove this method. crbug.com/141011
279 virtual bool CallTpmIsBeingOwnedAndBlock(bool* owning) OVERRIDE {
280 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
281 cryptohome::kCryptohomeTpmIsBeingOwned);
282 return CallBoolMethodAndBlock(&method_call, owning);
285 // CryptohomeClient override.
286 virtual void TpmCanAttemptOwnership(
287 const VoidDBusMethodCallback& callback) OVERRIDE {
288 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
289 cryptohome::kCryptohomeTpmCanAttemptOwnership);
290 CallVoidMethod(&method_call, callback);
293 // CryptohomeClient overrides.
294 virtual void TpmClearStoredPassword(const VoidDBusMethodCallback& callback)
295 OVERRIDE {
296 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
297 cryptohome::kCryptohomeTpmClearStoredPassword);
298 CallVoidMethod(&method_call, callback);
301 // CryptohomeClient override.
302 // TODO(hashimoto): Remove this method. crbug.com/141010
303 virtual bool CallTpmClearStoredPasswordAndBlock() OVERRIDE {
304 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
305 cryptohome::kCryptohomeTpmClearStoredPassword);
306 scoped_ptr<dbus::Response> response(
307 blocking_method_caller_->CallMethodAndBlock(&method_call));
308 return response.get() != NULL;
311 // CryptohomeClient override.
312 virtual void Pkcs11IsTpmTokenReady(const BoolDBusMethodCallback& callback)
313 OVERRIDE {
314 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
315 cryptohome::kCryptohomePkcs11IsTpmTokenReady);
316 CallBoolMethod(&method_call, callback);
319 // CryptohomeClient override.
320 virtual void Pkcs11GetTpmTokenInfo(
321 const Pkcs11GetTpmTokenInfoCallback& callback) OVERRIDE {
322 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
323 cryptohome::kCryptohomePkcs11GetTpmTokenInfo);
324 proxy_->CallMethod(
325 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
326 base::Bind(
327 &CryptohomeClientImpl::OnPkcs11GetTpmTokenInfo,
328 weak_ptr_factory_.GetWeakPtr(),
329 callback));
332 // CryptohomeClient override.
333 virtual void Pkcs11GetTpmTokenInfoForUser(
334 const std::string& user_email,
335 const Pkcs11GetTpmTokenInfoCallback& callback) OVERRIDE {
336 dbus::MethodCall method_call(
337 cryptohome::kCryptohomeInterface,
338 cryptohome::kCryptohomePkcs11GetTpmTokenInfoForUser);
339 dbus::MessageWriter writer(&method_call);
340 writer.AppendString(user_email);
341 proxy_->CallMethod(
342 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
343 base::Bind(
344 &CryptohomeClientImpl::OnPkcs11GetTpmTokenInfoForUser,
345 weak_ptr_factory_.GetWeakPtr(),
346 callback));
349 // CryptohomeClient override.
350 virtual bool InstallAttributesGet(const std::string& name,
351 std::vector<uint8>* value,
352 bool* successful) OVERRIDE {
353 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
354 cryptohome::kCryptohomeInstallAttributesGet);
355 dbus::MessageWriter writer(&method_call);
356 writer.AppendString(name);
357 scoped_ptr<dbus::Response> response(
358 blocking_method_caller_->CallMethodAndBlock(&method_call));
359 if (!response.get())
360 return false;
361 dbus::MessageReader reader(response.get());
362 const uint8* bytes = NULL;
363 size_t length = 0;
364 if (!reader.PopArrayOfBytes(&bytes, &length) ||
365 !reader.PopBool(successful))
366 return false;
367 value->assign(bytes, bytes + length);
368 return true;
371 // CryptohomeClient override.
372 virtual bool InstallAttributesSet(const std::string& name,
373 const std::vector<uint8>& value,
374 bool* successful) OVERRIDE {
375 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
376 cryptohome::kCryptohomeInstallAttributesSet);
377 dbus::MessageWriter writer(&method_call);
378 writer.AppendString(name);
379 writer.AppendArrayOfBytes(value.data(), value.size());
380 return CallBoolMethodAndBlock(&method_call, successful);
383 // CryptohomeClient override.
384 virtual bool InstallAttributesFinalize(bool* successful) OVERRIDE {
385 dbus::MethodCall method_call(
386 cryptohome::kCryptohomeInterface,
387 cryptohome::kCryptohomeInstallAttributesFinalize);
388 return CallBoolMethodAndBlock(&method_call, successful);
391 // CryptohomeClient override.
392 virtual void InstallAttributesIsReady(const BoolDBusMethodCallback& callback)
393 OVERRIDE {
394 dbus::MethodCall method_call(
395 cryptohome::kCryptohomeInterface,
396 cryptohome::kCryptohomeInstallAttributesIsReady);
397 return CallBoolMethod(&method_call, callback);
400 // CryptohomeClient override.
401 virtual bool InstallAttributesIsInvalid(bool* is_invalid) OVERRIDE {
402 dbus::MethodCall method_call(
403 cryptohome::kCryptohomeInterface,
404 cryptohome::kCryptohomeInstallAttributesIsInvalid);
405 return CallBoolMethodAndBlock(&method_call, is_invalid);
408 // CryptohomeClient override.
409 virtual bool InstallAttributesIsFirstInstall(
410 bool* is_first_install) OVERRIDE {
411 dbus::MethodCall method_call(
412 cryptohome::kCryptohomeInterface,
413 cryptohome::kCryptohomeInstallAttributesIsFirstInstall);
414 return CallBoolMethodAndBlock(&method_call, is_first_install);
417 // CryptohomeClient override.
418 virtual void TpmAttestationIsPrepared(
419 const BoolDBusMethodCallback& callback) OVERRIDE {
420 dbus::MethodCall method_call(
421 cryptohome::kCryptohomeInterface,
422 cryptohome::kCryptohomeTpmIsAttestationPrepared);
423 return CallBoolMethod(&method_call, callback);
426 // CryptohomeClient override.
427 virtual void TpmAttestationIsEnrolled(
428 const BoolDBusMethodCallback& callback) OVERRIDE {
429 dbus::MethodCall method_call(
430 cryptohome::kCryptohomeInterface,
431 cryptohome::kCryptohomeTpmIsAttestationEnrolled);
432 return CallBoolMethod(&method_call, callback);
435 // CryptohomeClient override.
436 virtual void AsyncTpmAttestationCreateEnrollRequest(
437 attestation::PrivacyCAType pca_type,
438 const AsyncMethodCallback& callback) OVERRIDE {
439 dbus::MethodCall method_call(
440 cryptohome::kCryptohomeInterface,
441 cryptohome::kCryptohomeAsyncTpmAttestationCreateEnrollRequest);
442 dbus::MessageWriter writer(&method_call);
443 writer.AppendInt32(pca_type);
444 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
445 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall,
446 weak_ptr_factory_.GetWeakPtr(),
447 callback));
450 // CryptohomeClient override.
451 virtual void AsyncTpmAttestationEnroll(
452 attestation::PrivacyCAType pca_type,
453 const std::string& pca_response,
454 const AsyncMethodCallback& callback) OVERRIDE {
455 dbus::MethodCall method_call(
456 cryptohome::kCryptohomeInterface,
457 cryptohome::kCryptohomeAsyncTpmAttestationEnroll);
458 dbus::MessageWriter writer(&method_call);
459 writer.AppendInt32(pca_type);
460 writer.AppendArrayOfBytes(
461 reinterpret_cast<const uint8*>(pca_response.data()),
462 pca_response.size());
463 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
464 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall,
465 weak_ptr_factory_.GetWeakPtr(),
466 callback));
469 // CryptohomeClient override.
470 virtual void AsyncTpmAttestationCreateCertRequest(
471 attestation::PrivacyCAType pca_type,
472 attestation::AttestationCertificateProfile certificate_profile,
473 const std::string& user_id,
474 const std::string& request_origin,
475 const AsyncMethodCallback& callback) OVERRIDE {
476 dbus::MethodCall method_call(
477 cryptohome::kCryptohomeInterface,
478 cryptohome::kCryptohomeAsyncTpmAttestationCreateCertRequest);
479 dbus::MessageWriter writer(&method_call);
480 writer.AppendInt32(pca_type);
481 writer.AppendInt32(certificate_profile);
482 writer.AppendString(user_id);
483 writer.AppendString(request_origin);
484 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
485 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall,
486 weak_ptr_factory_.GetWeakPtr(),
487 callback));
490 // CryptohomeClient override.
491 virtual void AsyncTpmAttestationFinishCertRequest(
492 const std::string& pca_response,
493 attestation::AttestationKeyType key_type,
494 const std::string& user_id,
495 const std::string& key_name,
496 const AsyncMethodCallback& callback) OVERRIDE {
497 dbus::MethodCall method_call(
498 cryptohome::kCryptohomeInterface,
499 cryptohome::kCryptohomeAsyncTpmAttestationFinishCertRequest);
500 dbus::MessageWriter writer(&method_call);
501 writer.AppendArrayOfBytes(
502 reinterpret_cast<const uint8*>(pca_response.data()),
503 pca_response.size());
504 bool is_user_specific = (key_type == attestation::KEY_USER);
505 writer.AppendBool(is_user_specific);
506 writer.AppendString(user_id);
507 writer.AppendString(key_name);
508 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
509 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall,
510 weak_ptr_factory_.GetWeakPtr(),
511 callback));
514 // CryptohomeClient override.
515 virtual void TpmAttestationDoesKeyExist(
516 attestation::AttestationKeyType key_type,
517 const std::string& user_id,
518 const std::string& key_name,
519 const BoolDBusMethodCallback& callback) OVERRIDE {
520 dbus::MethodCall method_call(
521 cryptohome::kCryptohomeInterface,
522 cryptohome::kCryptohomeTpmAttestationDoesKeyExist);
523 dbus::MessageWriter writer(&method_call);
524 bool is_user_specific = (key_type == attestation::KEY_USER);
525 writer.AppendBool(is_user_specific);
526 writer.AppendString(user_id);
527 writer.AppendString(key_name);
528 CallBoolMethod(&method_call, callback);
531 // CryptohomeClient override.
532 virtual void TpmAttestationGetCertificate(
533 attestation::AttestationKeyType key_type,
534 const std::string& user_id,
535 const std::string& key_name,
536 const DataMethodCallback& callback) OVERRIDE {
537 dbus::MethodCall method_call(
538 cryptohome::kCryptohomeInterface,
539 cryptohome::kCryptohomeTpmAttestationGetCertificate);
540 dbus::MessageWriter writer(&method_call);
541 bool is_user_specific = (key_type == attestation::KEY_USER);
542 writer.AppendBool(is_user_specific);
543 writer.AppendString(user_id);
544 writer.AppendString(key_name);
545 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
546 base::Bind(&CryptohomeClientImpl::OnDataMethod,
547 weak_ptr_factory_.GetWeakPtr(),
548 callback));
551 // CryptohomeClient override.
552 virtual void TpmAttestationGetPublicKey(
553 attestation::AttestationKeyType key_type,
554 const std::string& user_id,
555 const std::string& key_name,
556 const DataMethodCallback& callback) OVERRIDE {
557 dbus::MethodCall method_call(
558 cryptohome::kCryptohomeInterface,
559 cryptohome::kCryptohomeTpmAttestationGetPublicKey);
560 dbus::MessageWriter writer(&method_call);
561 bool is_user_specific = (key_type == attestation::KEY_USER);
562 writer.AppendBool(is_user_specific);
563 writer.AppendString(user_id);
564 writer.AppendString(key_name);
565 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
566 base::Bind(&CryptohomeClientImpl::OnDataMethod,
567 weak_ptr_factory_.GetWeakPtr(),
568 callback));
571 // CryptohomeClient override.
572 virtual void TpmAttestationRegisterKey(
573 attestation::AttestationKeyType key_type,
574 const std::string& user_id,
575 const std::string& key_name,
576 const AsyncMethodCallback& callback) OVERRIDE {
577 dbus::MethodCall method_call(
578 cryptohome::kCryptohomeInterface,
579 cryptohome::kCryptohomeTpmAttestationRegisterKey);
580 dbus::MessageWriter writer(&method_call);
581 bool is_user_specific = (key_type == attestation::KEY_USER);
582 writer.AppendBool(is_user_specific);
583 writer.AppendString(user_id);
584 writer.AppendString(key_name);
585 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
586 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall,
587 weak_ptr_factory_.GetWeakPtr(),
588 callback));
591 // CryptohomeClient override.
592 virtual void TpmAttestationSignEnterpriseChallenge(
593 attestation::AttestationKeyType key_type,
594 const std::string& user_id,
595 const std::string& key_name,
596 const std::string& domain,
597 const std::string& device_id,
598 attestation::AttestationChallengeOptions options,
599 const std::string& challenge,
600 const AsyncMethodCallback& callback) OVERRIDE {
601 dbus::MethodCall method_call(
602 cryptohome::kCryptohomeInterface,
603 cryptohome::kCryptohomeTpmAttestationSignEnterpriseChallenge);
604 dbus::MessageWriter writer(&method_call);
605 bool is_user_specific = (key_type == attestation::KEY_USER);
606 writer.AppendBool(is_user_specific);
607 writer.AppendString(user_id);
608 writer.AppendString(key_name);
609 writer.AppendString(domain);
610 writer.AppendArrayOfBytes(reinterpret_cast<const uint8*>(device_id.data()),
611 device_id.size());
612 bool include_signed_public_key =
613 (options & attestation::CHALLENGE_INCLUDE_SIGNED_PUBLIC_KEY);
614 writer.AppendBool(include_signed_public_key);
615 writer.AppendArrayOfBytes(reinterpret_cast<const uint8*>(challenge.data()),
616 challenge.size());
617 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
618 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall,
619 weak_ptr_factory_.GetWeakPtr(),
620 callback));
623 // CryptohomeClient override.
624 virtual void TpmAttestationSignSimpleChallenge(
625 attestation::AttestationKeyType key_type,
626 const std::string& user_id,
627 const std::string& key_name,
628 const std::string& challenge,
629 const AsyncMethodCallback& callback) OVERRIDE {
630 dbus::MethodCall method_call(
631 cryptohome::kCryptohomeInterface,
632 cryptohome::kCryptohomeTpmAttestationSignSimpleChallenge);
633 dbus::MessageWriter writer(&method_call);
634 bool is_user_specific = (key_type == attestation::KEY_USER);
635 writer.AppendBool(is_user_specific);
636 writer.AppendString(user_id);
637 writer.AppendString(key_name);
638 writer.AppendArrayOfBytes(reinterpret_cast<const uint8*>(challenge.data()),
639 challenge.size());
640 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
641 base::Bind(&CryptohomeClientImpl::OnAsyncMethodCall,
642 weak_ptr_factory_.GetWeakPtr(),
643 callback));
646 // CryptohomeClient override.
647 virtual void TpmAttestationGetKeyPayload(
648 attestation::AttestationKeyType key_type,
649 const std::string& user_id,
650 const std::string& key_name,
651 const DataMethodCallback& callback) OVERRIDE {
652 dbus::MethodCall method_call(
653 cryptohome::kCryptohomeInterface,
654 cryptohome::kCryptohomeTpmAttestationGetKeyPayload);
655 dbus::MessageWriter writer(&method_call);
656 bool is_user_specific = (key_type == attestation::KEY_USER);
657 writer.AppendBool(is_user_specific);
658 writer.AppendString(user_id);
659 writer.AppendString(key_name);
660 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
661 base::Bind(&CryptohomeClientImpl::OnDataMethod,
662 weak_ptr_factory_.GetWeakPtr(),
663 callback));
666 // CryptohomeClient override.
667 virtual void TpmAttestationSetKeyPayload(
668 attestation::AttestationKeyType key_type,
669 const std::string& user_id,
670 const std::string& key_name,
671 const std::string& payload,
672 const BoolDBusMethodCallback& callback) OVERRIDE {
673 dbus::MethodCall method_call(
674 cryptohome::kCryptohomeInterface,
675 cryptohome::kCryptohomeTpmAttestationSetKeyPayload);
676 dbus::MessageWriter writer(&method_call);
677 bool is_user_specific = (key_type == attestation::KEY_USER);
678 writer.AppendBool(is_user_specific);
679 writer.AppendString(user_id);
680 writer.AppendString(key_name);
681 writer.AppendArrayOfBytes(reinterpret_cast<const uint8*>(payload.data()),
682 payload.size());
683 CallBoolMethod(&method_call, callback);
686 // CryptohomeClient override.
687 virtual void TpmAttestationDeleteKeys(
688 attestation::AttestationKeyType key_type,
689 const std::string& user_id,
690 const std::string& key_prefix,
691 const BoolDBusMethodCallback& callback) OVERRIDE {
692 dbus::MethodCall method_call(
693 cryptohome::kCryptohomeInterface,
694 cryptohome::kCryptohomeTpmAttestationDeleteKeys);
695 dbus::MessageWriter writer(&method_call);
696 bool is_user_specific = (key_type == attestation::KEY_USER);
697 writer.AppendBool(is_user_specific);
698 writer.AppendString(user_id);
699 writer.AppendString(key_prefix);
700 CallBoolMethod(&method_call, callback);
703 virtual void CheckKeyEx(
704 const cryptohome::AccountIdentifier& id,
705 const cryptohome::AuthorizationRequest& auth,
706 const cryptohome::CheckKeyRequest& request,
707 const ProtobufMethodCallback& callback) OVERRIDE {
708 const char* method_name = cryptohome::kCryptohomeCheckKeyEx;
709 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
710 method_name);
712 dbus::MessageWriter writer(&method_call);
713 writer.AppendProtoAsArrayOfBytes(id);
714 writer.AppendProtoAsArrayOfBytes(auth);
715 writer.AppendProtoAsArrayOfBytes(request);
717 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
718 base::Bind(&CryptohomeClientImpl::OnBaseReplyMethod,
719 weak_ptr_factory_.GetWeakPtr(),
720 callback));
723 virtual void MountEx(
724 const cryptohome::AccountIdentifier& id,
725 const cryptohome::AuthorizationRequest& auth,
726 const cryptohome::MountRequest& request,
727 const ProtobufMethodCallback& callback) OVERRIDE {
728 const char* method_name = cryptohome::kCryptohomeMountEx;
729 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
730 method_name);
732 dbus::MessageWriter writer(&method_call);
733 writer.AppendProtoAsArrayOfBytes(id);
734 writer.AppendProtoAsArrayOfBytes(auth);
735 writer.AppendProtoAsArrayOfBytes(request);
737 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
738 base::Bind(&CryptohomeClientImpl::OnBaseReplyMethod,
739 weak_ptr_factory_.GetWeakPtr(),
740 callback));
743 virtual void AddKeyEx(
744 const cryptohome::AccountIdentifier& id,
745 const cryptohome::AuthorizationRequest& auth,
746 const cryptohome::AddKeyRequest& request,
747 const ProtobufMethodCallback& callback) OVERRIDE {
748 const char* method_name = cryptohome::kCryptohomeAddKeyEx;
749 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
750 method_name);
752 dbus::MessageWriter writer(&method_call);
753 writer.AppendProtoAsArrayOfBytes(id);
754 writer.AppendProtoAsArrayOfBytes(auth);
755 writer.AppendProtoAsArrayOfBytes(request);
757 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
758 base::Bind(&CryptohomeClientImpl::OnBaseReplyMethod,
759 weak_ptr_factory_.GetWeakPtr(),
760 callback));
763 virtual void UpdateKeyEx(
764 const cryptohome::AccountIdentifier& id,
765 const cryptohome::AuthorizationRequest& auth,
766 const cryptohome::UpdateKeyRequest& request,
767 const ProtobufMethodCallback& callback) OVERRIDE {
768 const char* method_name = cryptohome::kCryptohomeUpdateKeyEx;
769 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface,
770 method_name);
772 dbus::MessageWriter writer(&method_call);
773 writer.AppendProtoAsArrayOfBytes(id);
774 writer.AppendProtoAsArrayOfBytes(auth);
775 writer.AppendProtoAsArrayOfBytes(request);
777 proxy_->CallMethod(&method_call,
778 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
779 base::Bind(&CryptohomeClientImpl::OnBaseReplyMethod,
780 weak_ptr_factory_.GetWeakPtr(),
781 callback));
784 virtual void RemoveKeyEx(const cryptohome::AccountIdentifier& id,
785 const cryptohome::AuthorizationRequest& auth,
786 const cryptohome::RemoveKeyRequest& request,
787 const ProtobufMethodCallback& callback) OVERRIDE {
788 const char* method_name = cryptohome::kCryptohomeRemoveKeyEx;
789 dbus::MethodCall method_call(cryptohome::kCryptohomeInterface, method_name);
791 dbus::MessageWriter writer(&method_call);
792 writer.AppendProtoAsArrayOfBytes(id);
793 writer.AppendProtoAsArrayOfBytes(auth);
794 writer.AppendProtoAsArrayOfBytes(request);
796 proxy_->CallMethod(&method_call,
797 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
798 base::Bind(&CryptohomeClientImpl::OnBaseReplyMethod,
799 weak_ptr_factory_.GetWeakPtr(),
800 callback));
803 protected:
804 virtual void Init(dbus::Bus* bus) OVERRIDE {
805 proxy_ = bus->GetObjectProxy(
806 cryptohome::kCryptohomeServiceName,
807 dbus::ObjectPath(cryptohome::kCryptohomeServicePath));
809 blocking_method_caller_.reset(new BlockingMethodCaller(bus, proxy_));
811 proxy_->ConnectToSignal(cryptohome::kCryptohomeInterface,
812 cryptohome::kSignalAsyncCallStatus,
813 base::Bind(&CryptohomeClientImpl::OnAsyncCallStatus,
814 weak_ptr_factory_.GetWeakPtr()),
815 base::Bind(&CryptohomeClientImpl::OnSignalConnected,
816 weak_ptr_factory_.GetWeakPtr()));
817 proxy_->ConnectToSignal(
818 cryptohome::kCryptohomeInterface,
819 cryptohome::kSignalAsyncCallStatusWithData,
820 base::Bind(&CryptohomeClientImpl::OnAsyncCallStatusWithData,
821 weak_ptr_factory_.GetWeakPtr()),
822 base::Bind(&CryptohomeClientImpl::OnSignalConnected,
823 weak_ptr_factory_.GetWeakPtr()));
826 private:
827 // Handles the result of AsyncXXX methods.
828 void OnAsyncMethodCall(const AsyncMethodCallback& callback,
829 dbus::Response* response) {
830 if (!response)
831 return;
832 dbus::MessageReader reader(response);
833 int async_id = 0;
834 if (!reader.PopInt32(&async_id)) {
835 LOG(ERROR) << "Invalid response: " << response->ToString();
836 return;
838 callback.Run(async_id);
841 // Handles the result of GetSystemSalt().
842 void OnGetSystemSalt(const GetSystemSaltCallback& callback,
843 dbus::Response* response) {
844 if (!response) {
845 callback.Run(DBUS_METHOD_CALL_FAILURE, std::vector<uint8>());
846 return;
848 dbus::MessageReader reader(response);
849 const uint8* bytes = NULL;
850 size_t length = 0;
851 if (!reader.PopArrayOfBytes(&bytes, &length)) {
852 callback.Run(DBUS_METHOD_CALL_FAILURE, std::vector<uint8>());
853 return;
855 callback.Run(DBUS_METHOD_CALL_SUCCESS,
856 std::vector<uint8>(bytes, bytes + length));
859 // Calls a method without result values.
860 void CallVoidMethod(dbus::MethodCall* method_call,
861 const VoidDBusMethodCallback& callback) {
862 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
863 base::Bind(&CryptohomeClientImpl::OnVoidMethod,
864 weak_ptr_factory_.GetWeakPtr(),
865 callback));
868 void OnVoidMethod(const VoidDBusMethodCallback& callback,
869 dbus::Response* response) {
870 if (!response) {
871 callback.Run(DBUS_METHOD_CALL_FAILURE);
872 return;
874 callback.Run(DBUS_METHOD_CALL_SUCCESS);
877 // Calls a method with a bool value reult and block.
878 bool CallBoolMethodAndBlock(dbus::MethodCall* method_call,
879 bool* result) {
880 scoped_ptr<dbus::Response> response(
881 blocking_method_caller_->CallMethodAndBlock(method_call));
882 if (!response.get())
883 return false;
884 dbus::MessageReader reader(response.get());
885 return reader.PopBool(result);
888 // Calls a method with a bool value result.
889 void CallBoolMethod(dbus::MethodCall* method_call,
890 const BoolDBusMethodCallback& callback) {
891 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
892 base::Bind(
893 &CryptohomeClientImpl::OnBoolMethod,
894 weak_ptr_factory_.GetWeakPtr(),
895 callback));
898 // Handles responses for methods with a bool value result.
899 void OnBoolMethod(const BoolDBusMethodCallback& callback,
900 dbus::Response* response) {
901 if (!response) {
902 callback.Run(DBUS_METHOD_CALL_FAILURE, false);
903 return;
905 dbus::MessageReader reader(response);
906 bool result = false;
907 if (!reader.PopBool(&result)) {
908 callback.Run(DBUS_METHOD_CALL_FAILURE, false);
909 LOG(ERROR) << "Invalid response: " << response->ToString();
910 return;
912 callback.Run(DBUS_METHOD_CALL_SUCCESS, result);
915 // Handles responses for methods with a string value result.
916 void OnStringMethod(const StringDBusMethodCallback& callback,
917 dbus::Response* response) {
918 if (!response) {
919 callback.Run(DBUS_METHOD_CALL_FAILURE, std::string());
920 return;
922 dbus::MessageReader reader(response);
923 std::string result;
924 if (!reader.PopString(&result)) {
925 callback.Run(DBUS_METHOD_CALL_FAILURE, std::string());
926 return;
928 callback.Run(DBUS_METHOD_CALL_SUCCESS, result);
931 // Handles responses for methods with a bool result and data.
932 void OnDataMethod(const DataMethodCallback& callback,
933 dbus::Response* response) {
934 if (!response) {
935 callback.Run(DBUS_METHOD_CALL_FAILURE, false, std::string());
936 return;
938 dbus::MessageReader reader(response);
939 const uint8* data_buffer = NULL;
940 size_t data_length = 0;
941 bool result = false;
942 if (!reader.PopArrayOfBytes(&data_buffer, &data_length) ||
943 !reader.PopBool(&result)) {
944 callback.Run(DBUS_METHOD_CALL_FAILURE, false, std::string());
945 return;
947 std::string data(reinterpret_cast<const char*>(data_buffer), data_length);
948 callback.Run(DBUS_METHOD_CALL_SUCCESS, result, data);
951 // Handles responses for methods with a BaseReply protobuf method.
952 void OnBaseReplyMethod(const ProtobufMethodCallback& callback,
953 dbus::Response* response) {
954 cryptohome::BaseReply reply;
955 if (!response) {
956 callback.Run(DBUS_METHOD_CALL_FAILURE, false, reply);
957 return;
959 dbus::MessageReader reader(response);
960 if (!reader.PopArrayOfBytesAsProto(&reply)) {
961 callback.Run(DBUS_METHOD_CALL_FAILURE, false, reply);
962 return;
964 callback.Run(DBUS_METHOD_CALL_SUCCESS, true, reply);
967 // Handles responses for Pkcs11GetTpmTokenInfo.
968 void OnPkcs11GetTpmTokenInfo(const Pkcs11GetTpmTokenInfoCallback& callback,
969 dbus::Response* response) {
970 if (!response) {
971 callback.Run(DBUS_METHOD_CALL_FAILURE, std::string(), std::string(), -1);
972 return;
974 dbus::MessageReader reader(response);
975 std::string label;
976 std::string user_pin;
977 if (!reader.PopString(&label) || !reader.PopString(&user_pin)) {
978 callback.Run(DBUS_METHOD_CALL_FAILURE, std::string(), std::string(), -1);
979 LOG(ERROR) << "Invalid response: " << response->ToString();
980 return;
982 const int kDefaultSlot = 0;
983 callback.Run(DBUS_METHOD_CALL_SUCCESS, label, user_pin, kDefaultSlot);
986 // Handles responses for Pkcs11GetTpmTokenInfoForUser.
987 void OnPkcs11GetTpmTokenInfoForUser(
988 const Pkcs11GetTpmTokenInfoCallback& callback,
989 dbus::Response* response) {
990 if (!response) {
991 callback.Run(DBUS_METHOD_CALL_FAILURE, std::string(), std::string(), -1);
992 return;
994 dbus::MessageReader reader(response);
995 std::string label;
996 std::string user_pin;
997 int slot = 0;
998 if (!reader.PopString(&label) || !reader.PopString(&user_pin) ||
999 !reader.PopInt32(&slot)) {
1000 callback.Run(DBUS_METHOD_CALL_FAILURE, std::string(), std::string(), -1);
1001 LOG(ERROR) << "Invalid response: " << response->ToString();
1002 return;
1004 callback.Run(DBUS_METHOD_CALL_SUCCESS, label, user_pin, slot);
1007 // Handles AsyncCallStatus signal.
1008 void OnAsyncCallStatus(dbus::Signal* signal) {
1009 dbus::MessageReader reader(signal);
1010 int async_id = 0;
1011 bool return_status = false;
1012 int return_code = 0;
1013 if (!reader.PopInt32(&async_id) ||
1014 !reader.PopBool(&return_status) ||
1015 !reader.PopInt32(&return_code)) {
1016 LOG(ERROR) << "Invalid signal: " << signal->ToString();
1017 return;
1019 if (!async_call_status_handler_.is_null())
1020 async_call_status_handler_.Run(async_id, return_status, return_code);
1023 // Handles AsyncCallStatusWithData signal.
1024 void OnAsyncCallStatusWithData(dbus::Signal* signal) {
1025 dbus::MessageReader reader(signal);
1026 int async_id = 0;
1027 bool return_status = false;
1028 const uint8* return_data_buffer = NULL;
1029 size_t return_data_length = 0;
1030 if (!reader.PopInt32(&async_id) ||
1031 !reader.PopBool(&return_status) ||
1032 !reader.PopArrayOfBytes(&return_data_buffer, &return_data_length)) {
1033 LOG(ERROR) << "Invalid signal: " << signal->ToString();
1034 return;
1036 if (!async_call_status_data_handler_.is_null()) {
1037 std::string return_data(reinterpret_cast<const char*>(return_data_buffer),
1038 return_data_length);
1039 async_call_status_data_handler_.Run(async_id, return_status, return_data);
1043 // Handles the result of signal connection setup.
1044 void OnSignalConnected(const std::string& interface,
1045 const std::string& signal,
1046 bool succeeded) {
1047 LOG_IF(ERROR, !succeeded) << "Connect to " << interface << " " <<
1048 signal << " failed.";
1051 dbus::ObjectProxy* proxy_;
1052 scoped_ptr<BlockingMethodCaller> blocking_method_caller_;
1053 AsyncCallStatusHandler async_call_status_handler_;
1054 AsyncCallStatusWithDataHandler async_call_status_data_handler_;
1056 // Note: This should remain the last member so it'll be destroyed and
1057 // invalidate its weak pointers before any other members are destroyed.
1058 base::WeakPtrFactory<CryptohomeClientImpl> weak_ptr_factory_;
1060 DISALLOW_COPY_AND_ASSIGN(CryptohomeClientImpl);
1063 } // namespace
1065 ////////////////////////////////////////////////////////////////////////////////
1066 // CryptohomeClient
1068 CryptohomeClient::CryptohomeClient() {}
1070 CryptohomeClient::~CryptohomeClient() {}
1072 // static
1073 CryptohomeClient* CryptohomeClient::Create() {
1074 return new CryptohomeClientImpl();
1077 // static
1078 std::string CryptohomeClient::GetStubSanitizedUsername(
1079 const std::string& username) {
1080 return username + kUserIdStubHashSuffix;
1083 } // namespace chromeos