fake_cryptohome_client.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
1// Copyright 2013 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 "chromeos/dbus/fake_cryptohome_client.h"
6
7#include "base/bind.h"
8#include "base/location.h"
9#include "base/message_loop/message_loop.h"
10#include "crypto/nss_util.h"
11#include "third_party/cros_system_api/dbus/service_constants.h"
12
13namespace chromeos {
14
15FakeCryptohomeClient::FakeCryptohomeClient()
16    : async_call_id_(1),
17      tpm_is_ready_counter_(0),
18      unmount_result_(true),
19      locked_(false),
20      weak_ptr_factory_(this) {}
21
22FakeCryptohomeClient::~FakeCryptohomeClient() {}
23
24void FakeCryptohomeClient::Init(dbus::Bus* bus) {
25}
26
27void FakeCryptohomeClient::SetAsyncCallStatusHandlers(
28    const AsyncCallStatusHandler& handler,
29    const AsyncCallStatusWithDataHandler& data_handler) {
30  async_call_status_handler_ = handler;
31  async_call_status_data_handler_ = data_handler;
32}
33
34void FakeCryptohomeClient::ResetAsyncCallStatusHandlers() {
35  async_call_status_handler_.Reset();
36  async_call_status_data_handler_.Reset();
37}
38
39void FakeCryptohomeClient::IsMounted(
40    const BoolDBusMethodCallback& callback) {
41  base::MessageLoop::current()->PostTask(
42      FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true));
43}
44
45bool FakeCryptohomeClient::Unmount(bool* success) {
46  *success = unmount_result_;
47  return true;
48}
49
50void FakeCryptohomeClient::AsyncCheckKey(
51    const std::string& username,
52    const std::string& key,
53    const AsyncMethodCallback& callback) {
54  ReturnAsyncMethodResult(callback, false);
55}
56
57void FakeCryptohomeClient::AsyncMigrateKey(
58    const std::string& username,
59    const std::string& from_key,
60    const std::string& to_key,
61    const AsyncMethodCallback& callback) {
62  ReturnAsyncMethodResult(callback, false);
63}
64
65void FakeCryptohomeClient::AsyncRemove(
66    const std::string& username,
67    const AsyncMethodCallback& callback) {
68  ReturnAsyncMethodResult(callback, false);
69}
70
71bool FakeCryptohomeClient::GetSystemSalt(std::vector<uint8>* salt) {
72  *salt = GetStubSystemSalt();
73  return true;
74}
75
76void FakeCryptohomeClient::GetSanitizedUsername(
77    const std::string& username,
78    const StringDBusMethodCallback& callback) {
79  // Even for stub implementation we have to return different values so that
80  // multi-profiles would work.
81  std::string sanitized_username = GetStubSanitizedUsername(username);
82  base::MessageLoop::current()->PostTask(
83      FROM_HERE,
84      base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, sanitized_username));
85}
86
87std::string FakeCryptohomeClient::BlockingGetSanitizedUsername(
88    const std::string& username) {
89  return GetStubSanitizedUsername(username);
90}
91
92void FakeCryptohomeClient::AsyncMount(const std::string& username,
93                                          const std::string& key,
94                                          int flags,
95                                          const AsyncMethodCallback& callback) {
96  ReturnAsyncMethodResult(callback, false);
97}
98
99void FakeCryptohomeClient::AsyncAddKey(
100    const std::string& username,
101    const std::string& key,
102    const std::string& new_key,
103    const AsyncMethodCallback& callback) {
104  ReturnAsyncMethodResult(callback, false);
105}
106
107void FakeCryptohomeClient::AsyncMountGuest(
108    const AsyncMethodCallback& callback) {
109  ReturnAsyncMethodResult(callback, false);
110}
111
112void FakeCryptohomeClient::AsyncMountPublic(
113    const std::string& public_mount_id,
114    int flags,
115    const AsyncMethodCallback& callback) {
116  ReturnAsyncMethodResult(callback, false);
117}
118
119void FakeCryptohomeClient::TpmIsReady(
120    const BoolDBusMethodCallback& callback) {
121  base::MessageLoop::current()->PostTask(
122      FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true));
123}
124
125void FakeCryptohomeClient::TpmIsEnabled(
126    const BoolDBusMethodCallback& callback) {
127  base::MessageLoop::current()->PostTask(
128      FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true));
129}
130
131bool FakeCryptohomeClient::CallTpmIsEnabledAndBlock(bool* enabled) {
132  *enabled = true;
133  return true;
134}
135
136void FakeCryptohomeClient::TpmGetPassword(
137    const StringDBusMethodCallback& callback) {
138  const char kStubTpmPassword[] = "Stub-TPM-password";
139  base::MessageLoop::current()->PostTask(
140      FROM_HERE,
141      base::Bind(callback, DBUS_METHOD_CALL_SUCCESS,
142                 std::string(kStubTpmPassword)));
143}
144
145void FakeCryptohomeClient::TpmIsOwned(
146    const BoolDBusMethodCallback& callback) {
147  base::MessageLoop::current()->PostTask(
148      FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true));
149}
150
151bool FakeCryptohomeClient::CallTpmIsOwnedAndBlock(bool* owned) {
152  *owned = true;
153  return true;
154}
155
156void FakeCryptohomeClient::TpmIsBeingOwned(
157    const BoolDBusMethodCallback& callback) {
158  base::MessageLoop::current()->PostTask(
159      FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true));
160}
161
162bool FakeCryptohomeClient::CallTpmIsBeingOwnedAndBlock(bool* owning) {
163  *owning = true;
164  return true;
165}
166
167void FakeCryptohomeClient::TpmCanAttemptOwnership(
168    const VoidDBusMethodCallback& callback) {
169  base::MessageLoop::current()->PostTask(
170      FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS));
171}
172
173void FakeCryptohomeClient::TpmClearStoredPassword(
174    const VoidDBusMethodCallback& callback) {
175  base::MessageLoop::current()->PostTask(
176      FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS));
177}
178
179bool FakeCryptohomeClient::CallTpmClearStoredPasswordAndBlock() {
180  return true;
181}
182
183void FakeCryptohomeClient::Pkcs11IsTpmTokenReady(
184    const BoolDBusMethodCallback& callback) {
185  base::MessageLoop::current()->PostTask(
186      FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true));
187}
188
189void FakeCryptohomeClient::Pkcs11GetTpmTokenInfo(
190    const Pkcs11GetTpmTokenInfoCallback& callback) {
191  const char kStubUserPin[] = "012345";
192  const int kStubSlot = 0;
193  base::MessageLoop::current()->PostTask(
194      FROM_HERE,
195      base::Bind(callback,
196                 DBUS_METHOD_CALL_SUCCESS,
197                 std::string(crypto::kTestTPMTokenName),
198                 std::string(kStubUserPin),
199                 kStubSlot));
200}
201
202void FakeCryptohomeClient::Pkcs11GetTpmTokenInfoForUser(
203    const std::string& username,
204    const Pkcs11GetTpmTokenInfoCallback& callback) {
205  Pkcs11GetTpmTokenInfo(callback);
206}
207
208bool FakeCryptohomeClient::InstallAttributesGet(const std::string& name,
209                                                    std::vector<uint8>* value,
210                                                    bool* successful) {
211  if (install_attrs_.find(name) != install_attrs_.end()) {
212    *value = install_attrs_[name];
213    *successful = true;
214  } else {
215    value->clear();
216    *successful = false;
217  }
218  return true;
219}
220
221bool FakeCryptohomeClient::InstallAttributesSet(
222    const std::string& name,
223    const std::vector<uint8>& value,
224    bool* successful) {
225  install_attrs_[name] = value;
226  *successful = true;
227  return true;
228}
229
230bool FakeCryptohomeClient::InstallAttributesFinalize(bool* successful) {
231  locked_ = true;
232  *successful = true;
233  return true;
234}
235
236void FakeCryptohomeClient::InstallAttributesIsReady(
237    const BoolDBusMethodCallback& callback) {
238  base::MessageLoop::current()->PostTask(
239      FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true));
240}
241
242bool FakeCryptohomeClient::InstallAttributesIsInvalid(bool* is_invalid) {
243  *is_invalid = false;
244  return true;
245}
246
247bool FakeCryptohomeClient::InstallAttributesIsFirstInstall(
248    bool* is_first_install) {
249  *is_first_install = !locked_;
250  return true;
251}
252
253void FakeCryptohomeClient::TpmAttestationIsPrepared(
254    const BoolDBusMethodCallback& callback) {
255  base::MessageLoop::current()->PostTask(
256      FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true));
257}
258
259void FakeCryptohomeClient::TpmAttestationIsEnrolled(
260    const BoolDBusMethodCallback& callback) {
261  base::MessageLoop::current()->PostTask(
262      FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, true));
263}
264
265void FakeCryptohomeClient::AsyncTpmAttestationCreateEnrollRequest(
266    const AsyncMethodCallback& callback) {
267  ReturnAsyncMethodResult(callback, true);
268}
269
270void FakeCryptohomeClient::AsyncTpmAttestationEnroll(
271    const std::string& pca_response,
272    const AsyncMethodCallback& callback) {
273  ReturnAsyncMethodResult(callback, false);
274}
275
276void FakeCryptohomeClient::AsyncTpmAttestationCreateCertRequest(
277    attestation::AttestationCertificateProfile certificate_profile,
278    const std::string& user_email,
279    const std::string& request_origin,
280    const AsyncMethodCallback& callback) {
281  ReturnAsyncMethodResult(callback, true);
282}
283
284void FakeCryptohomeClient::AsyncTpmAttestationFinishCertRequest(
285    const std::string& pca_response,
286    attestation::AttestationKeyType key_type,
287    const std::string& key_name,
288    const AsyncMethodCallback& callback) {
289  ReturnAsyncMethodResult(callback, true);
290}
291
292void FakeCryptohomeClient::TpmAttestationDoesKeyExist(
293    attestation::AttestationKeyType key_type,
294    const std::string& key_name,
295    const BoolDBusMethodCallback& callback) {
296  base::MessageLoop::current()->PostTask(
297      FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, false));
298}
299
300void FakeCryptohomeClient::TpmAttestationGetCertificate(
301    attestation::AttestationKeyType key_type,
302    const std::string& key_name,
303    const DataMethodCallback& callback) {
304  base::MessageLoop::current()->PostTask(
305      FROM_HERE,
306      base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, false, std::string()));
307}
308
309void FakeCryptohomeClient::TpmAttestationGetPublicKey(
310    attestation::AttestationKeyType key_type,
311    const std::string& key_name,
312    const DataMethodCallback& callback) {
313  base::MessageLoop::current()->PostTask(
314      FROM_HERE,
315      base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, false, std::string()));
316}
317
318void FakeCryptohomeClient::TpmAttestationRegisterKey(
319    attestation::AttestationKeyType key_type,
320    const std::string& key_name,
321    const AsyncMethodCallback& callback) {
322  ReturnAsyncMethodResult(callback, true);
323}
324
325void FakeCryptohomeClient::TpmAttestationSignEnterpriseChallenge(
326    attestation::AttestationKeyType key_type,
327    const std::string& key_name,
328    const std::string& domain,
329    const std::string& device_id,
330    attestation::AttestationChallengeOptions options,
331    const std::string& challenge,
332    const AsyncMethodCallback& callback) {
333  ReturnAsyncMethodResult(callback, true);
334}
335
336void FakeCryptohomeClient::TpmAttestationSignSimpleChallenge(
337    attestation::AttestationKeyType key_type,
338    const std::string& key_name,
339    const std::string& challenge,
340    const AsyncMethodCallback& callback) {
341  ReturnAsyncMethodResult(callback, true);
342}
343
344void FakeCryptohomeClient::TpmAttestationGetKeyPayload(
345    attestation::AttestationKeyType key_type,
346    const std::string& key_name,
347    const DataMethodCallback& callback) {
348  base::MessageLoop::current()->PostTask(
349      FROM_HERE,
350      base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, false, std::string()));
351}
352
353void FakeCryptohomeClient::TpmAttestationSetKeyPayload(
354    attestation::AttestationKeyType key_type,
355    const std::string& key_name,
356    const std::string& payload,
357    const BoolDBusMethodCallback& callback) {
358  base::MessageLoop::current()->PostTask(
359      FROM_HERE, base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, false));
360}
361
362// static
363std::vector<uint8> FakeCryptohomeClient::GetStubSystemSalt() {
364  const char kStubSystemSalt[] = "stub_system_salt";
365  return std::vector<uint8>(kStubSystemSalt,
366                            kStubSystemSalt + arraysize(kStubSystemSalt) - 1);
367}
368
369void FakeCryptohomeClient::ReturnAsyncMethodResult(
370    const AsyncMethodCallback& callback,
371    bool returns_data) {
372  base::MessageLoop::current()->PostTask(
373      FROM_HERE,
374      base::Bind(&FakeCryptohomeClient::ReturnAsyncMethodResultInternal,
375                 weak_ptr_factory_.GetWeakPtr(),
376                 callback,
377                 returns_data));
378}
379
380void FakeCryptohomeClient::ReturnAsyncMethodResultInternal(
381    const AsyncMethodCallback& callback,
382    bool returns_data) {
383  callback.Run(async_call_id_);
384  if (!returns_data && !async_call_status_handler_.is_null()) {
385    base::MessageLoop::current()->PostTask(
386        FROM_HERE,
387        base::Bind(async_call_status_handler_,
388                   async_call_id_,
389                   true,
390                   cryptohome::MOUNT_ERROR_NONE));
391  } else if (returns_data && !async_call_status_data_handler_.is_null()) {
392    base::MessageLoop::current()->PostTask(
393        FROM_HERE,
394        base::Bind(async_call_status_data_handler_,
395                   async_call_id_,
396                   true,
397                   std::string()));
398  }
399  ++async_call_id_;
400}
401
402}  // namespace chromeos
403