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