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 #ifndef CHROMEOS_DBUS_CRYPTOHOME_CLIENT_H_
6 #define CHROMEOS_DBUS_CRYPTOHOME_CLIENT_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "chromeos/attestation/attestation_constants.h"
14 #include "chromeos/chromeos_export.h"
15 #include "chromeos/dbus/dbus_client_implementation_type.h"
16 #include "chromeos/dbus/dbus_method_call_status.h"
24 // CryptohomeClient is used to communicate with the Cryptohome service.
25 // All method should be called from the origin thread (UI thread) which
26 // initializes the DBusThreadManager instance.
27 class CHROMEOS_EXPORT CryptohomeClient
{
29 // A callback to handle AsyncCallStatus signals.
30 typedef base::Callback
<void(int async_id
,
33 AsyncCallStatusHandler
;
34 // A callback to handle AsyncCallStatusWithData signals.
35 typedef base::Callback
<void(int async_id
,
37 const std::string
& data
)>
38 AsyncCallStatusWithDataHandler
;
39 // A callback to handle responses of AsyncXXX methods.
40 typedef base::Callback
<void(int async_id
)> AsyncMethodCallback
;
41 // A callback to handle responses of Pkcs11GetTpmTokenInfo method.
42 typedef base::Callback
<void(
43 DBusMethodCallStatus call_status
,
44 const std::string
& label
,
45 const std::string
& user_pin
)> Pkcs11GetTpmTokenInfoCallback
;
46 // A callback for methods which return both a bool result and data.
47 typedef base::Callback
<void(DBusMethodCallStatus call_status
,
49 const std::string
& data
)> DataMethodCallback
;
51 virtual ~CryptohomeClient();
53 // Factory function, creates a new instance and returns ownership.
54 // For normal usage, access the singleton via DBusThreadManager::Get().
55 static CryptohomeClient
* Create(DBusClientImplementationType type
,
58 // Returns the sanitized |username| that the stub implementation would return.
59 static std::string
GetStubSanitizedUsername(const std::string
& username
);
61 // Sets AsyncCallStatus signal handlers.
62 // |handler| is called when results for AsyncXXX methods are returned.
63 // Cryptohome service will process the calls in a first-in-first-out manner
64 // when they are made in parallel.
65 virtual void SetAsyncCallStatusHandlers(
66 const AsyncCallStatusHandler
& handler
,
67 const AsyncCallStatusWithDataHandler
& data_handler
) = 0;
69 // Resets AsyncCallStatus signal handlers.
70 virtual void ResetAsyncCallStatusHandlers() = 0;
72 // Calls IsMounted method and returns true when the call succeeds.
73 virtual void IsMounted(const BoolDBusMethodCallback
& callback
) = 0;
75 // Calls Unmount method and returns true when the call succeeds.
76 // This method blocks until the call returns.
77 virtual bool Unmount(bool* success
) = 0;
79 // Calls AsyncCheckKey method. |callback| is called after the method call
81 virtual void AsyncCheckKey(const std::string
& username
,
82 const std::string
& key
,
83 const AsyncMethodCallback
& callback
) = 0;
85 // Calls AsyncMigrateKey method. |callback| is called after the method call
87 virtual void AsyncMigrateKey(const std::string
& username
,
88 const std::string
& from_key
,
89 const std::string
& to_key
,
90 const AsyncMethodCallback
& callback
) = 0;
92 // Calls AsyncRemove method. |callback| is called after the method call
94 virtual void AsyncRemove(const std::string
& username
,
95 const AsyncMethodCallback
& callback
) = 0;
97 // Calls GetSystemSalt method. This method blocks until the call returns.
98 // The original content of |salt| is lost.
99 virtual bool GetSystemSalt(std::vector
<uint8
>* salt
) = 0;
101 // Calls GetSanitizedUsername method. |callback| is called after the method
103 virtual void GetSanitizedUsername(
104 const std::string
& username
,
105 const StringDBusMethodCallback
& callback
) = 0;
107 // Calls the AsyncMount method to asynchronously mount the cryptohome for
108 // |username|, using |key| to unlock it. For supported |flags|, see the
109 // documentation of AsyncMethodCaller::AsyncMount().
110 // |callback| is called after the method call succeeds.
111 virtual void AsyncMount(const std::string
& username
,
112 const std::string
& key
,
114 const AsyncMethodCallback
& callback
) = 0;
116 // Calls AsyncMountGuest method. |callback| is called after the method call
118 virtual void AsyncMountGuest(const AsyncMethodCallback
& callback
) = 0;
120 // Calls TpmIsReady method.
121 virtual void TpmIsReady(const BoolDBusMethodCallback
& callback
) = 0;
123 // Calls TpmIsEnabled method.
124 virtual void TpmIsEnabled(const BoolDBusMethodCallback
& callback
) = 0;
126 // Calls TpmIsEnabled method and returns true when the call succeeds.
127 // This method blocks until the call returns.
128 // TODO(hashimoto): Remove this method. crbug.com/141006
129 virtual bool CallTpmIsEnabledAndBlock(bool* enabled
) = 0;
131 // Calls TpmGetPassword method.
132 virtual void TpmGetPassword(const StringDBusMethodCallback
& callback
) = 0;
134 // Calls TpmIsOwned method.
135 virtual void TpmIsOwned(const BoolDBusMethodCallback
& callback
) = 0;
137 // Calls TpmIsOwned method and returns true when the call succeeds.
138 // This method blocks until the call returns.
139 // TODO(hashimoto): Remove this method. crbug.com/141012
140 virtual bool CallTpmIsOwnedAndBlock(bool* owned
) = 0;
142 // Calls TpmIsBeingOwned method.
143 virtual void TpmIsBeingOwned(const BoolDBusMethodCallback
& callback
) = 0;
145 // Calls TpmIsBeingOwned method and returns true when the call succeeds.
146 // This method blocks until the call returns.
147 // TODO(hashimoto): Remove this method. crbug.com/141011
148 virtual bool CallTpmIsBeingOwnedAndBlock(bool* owning
) = 0;
150 // Calls TpmCanAttemptOwnership method.
151 // This method tells the service that it is OK to attempt ownership.
152 virtual void TpmCanAttemptOwnership(
153 const VoidDBusMethodCallback
& callback
) = 0;
155 // Calls TpmClearStoredPasswordMethod.
156 virtual void TpmClearStoredPassword(
157 const VoidDBusMethodCallback
& callback
) = 0;
159 // Calls TpmClearStoredPassword method and returns true when the call
160 // succeeds. This method blocks until the call returns.
161 // TODO(hashimoto): Remove this method. crbug.com/141010
162 virtual bool CallTpmClearStoredPasswordAndBlock() = 0;
164 // Calls Pkcs11IsTpmTokenReady method.
165 virtual void Pkcs11IsTpmTokenReady(
166 const BoolDBusMethodCallback
& callback
) = 0;
168 // Calls Pkcs11GetTpmTokenInfo method.
169 virtual void Pkcs11GetTpmTokenInfo(
170 const Pkcs11GetTpmTokenInfoCallback
& callback
) = 0;
172 // Calls InstallAttributesGet method and returns true when the call succeeds.
173 // This method blocks until the call returns.
174 // The original content of |value| is lost.
175 virtual bool InstallAttributesGet(const std::string
& name
,
176 std::vector
<uint8
>* value
,
177 bool* successful
) = 0;
179 // Calls InstallAttributesSet method and returns true when the call succeeds.
180 // This method blocks until the call returns.
181 virtual bool InstallAttributesSet(const std::string
& name
,
182 const std::vector
<uint8
>& value
,
183 bool* successful
) = 0;
185 // Calls InstallAttributesFinalize method and returns true when the call
186 // succeeds. This method blocks until the call returns.
187 virtual bool InstallAttributesFinalize(bool* successful
) = 0;
189 // Calls InstallAttributesIsReady method.
190 virtual void InstallAttributesIsReady(
191 const BoolDBusMethodCallback
& callback
) = 0;
193 // Calls InstallAttributesIsInvalid method and returns true when the call
194 // succeeds. This method blocks until the call returns.
195 virtual bool InstallAttributesIsInvalid(bool* is_invalid
) = 0;
197 // Calls InstallAttributesIsFirstInstall method and returns true when the call
198 // succeeds. This method blocks until the call returns.
199 virtual bool InstallAttributesIsFirstInstall(bool* is_first_install
) = 0;
201 // Calls the TpmAttestationIsPrepared dbus method. The callback is called
202 // when the operation completes.
203 virtual void TpmAttestationIsPrepared(
204 const BoolDBusMethodCallback
& callback
) = 0;
206 // Calls the TpmAttestationIsEnrolled dbus method. The callback is called
207 // when the operation completes.
208 virtual void TpmAttestationIsEnrolled(
209 const BoolDBusMethodCallback
& callback
) = 0;
211 // Asynchronously creates an attestation enrollment request. The callback
212 // will be called when the dbus call completes. When the operation completes,
213 // the AsyncCallStatusWithDataHandler signal handler is called. The data that
214 // is sent with the signal is an enrollment request to be sent to the Privacy
215 // CA. The enrollment is completed by calling AsyncTpmAttestationEnroll.
216 virtual void AsyncTpmAttestationCreateEnrollRequest(
217 const AsyncMethodCallback
& callback
) = 0;
219 // Asynchronously finishes an attestation enrollment operation. The callback
220 // will be called when the dbus call completes. When the operation completes,
221 // the AsyncCallStatusHandler signal handler is called. |pca_response| is the
222 // response to the enrollment request emitted by the Privacy CA.
223 virtual void AsyncTpmAttestationEnroll(
224 const std::string
& pca_response
,
225 const AsyncMethodCallback
& callback
) = 0;
227 // Asynchronously creates an attestation certificate request according to
228 // |options|, which is a combination of AttestationCertificateOptions.
229 // |callback| will be called when the dbus call completes. When the operation
230 // completes, the AsyncCallStatusWithDataHandler signal handler is called.
231 // The data that is sent with the signal is a certificate request to be sent
232 // to the Privacy CA. The certificate request is completed by calling
233 // AsyncTpmAttestationFinishCertRequest.
234 virtual void AsyncTpmAttestationCreateCertRequest(
236 const AsyncMethodCallback
& callback
) = 0;
238 // Asynchronously finishes a certificate request operation. The callback will
239 // be called when the dbus call completes. When the operation completes, the
240 // AsyncCallStatusWithDataHandler signal handler is called. The data that is
241 // sent with the signal is a certificate chain in PEM format. |pca_response|
242 // is the response to the certificate request emitted by the Privacy CA.
243 // |key_type| determines whether the certified key is to be associated with
244 // the current user. |key_name| is a name for the key.
245 virtual void AsyncTpmAttestationFinishCertRequest(
246 const std::string
& pca_response
,
247 attestation::AttestationKeyType key_type
,
248 const std::string
& key_name
,
249 const AsyncMethodCallback
& callback
) = 0;
251 // Checks if an attestation key already exists. If the key specified by
252 // |key_type| and |key_name| exists, then the result sent to the callback will
254 virtual void TpmAttestationDoesKeyExist(
255 attestation::AttestationKeyType key_type
,
256 const std::string
& key_name
,
257 const BoolDBusMethodCallback
& callback
) = 0;
259 // Gets the attestation certificate for the key specified by |key_type| and
260 // |key_name|. |callback| will be called when the operation completes. If
261 // the key does not exist the callback |result| parameter will be false.
262 virtual void TpmAttestationGetCertificate(
263 attestation::AttestationKeyType key_type
,
264 const std::string
& key_name
,
265 const DataMethodCallback
& callback
) = 0;
267 // Gets the public key for the key specified by |key_type| and |key_name|.
268 // |callback| will be called when the operation completes. If the key does
269 // not exist the callback |result| parameter will be false.
270 virtual void TpmAttestationGetPublicKey(
271 attestation::AttestationKeyType key_type
,
272 const std::string
& key_name
,
273 const DataMethodCallback
& callback
) = 0;
275 // Asynchronously registers an attestation key with the current user's
276 // PKCS #11 token. The |callback| will be called when the dbus call
277 // completes. When the operation completes, the AsyncCallStatusHandler signal
278 // handler is called. |key_type| and |key_name| specify the key to register.
279 virtual void TpmAttestationRegisterKey(
280 attestation::AttestationKeyType key_type
,
281 const std::string
& key_name
,
282 const AsyncMethodCallback
& callback
) = 0;
284 // Asynchronously signs an enterprise challenge with the key specified by
285 // |key_type| and |key_name|. |domain| and |device_id| will be included in
286 // the challenge response. |options| control how the challenge response is
287 // generated. |challenge| must be a valid enterprise attestation challenge.
288 // The |callback| will be called when the dbus call completes. When the
289 // operation completes, the AsyncCallStatusWithDataHandler signal handler is
291 virtual void TpmAttestationSignEnterpriseChallenge(
292 attestation::AttestationKeyType key_type
,
293 const std::string
& key_name
,
294 const std::string
& domain
,
295 const std::string
& device_id
,
296 attestation::AttestationChallengeOptions options
,
297 const std::string
& challenge
,
298 const AsyncMethodCallback
& callback
) = 0;
300 // Asynchronously signs a simple challenge with the key specified by
301 // |key_type| and |key_name|. |challenge| can be any set of arbitrary bytes.
302 // A nonce will be appended to the challenge before signing; this method
303 // cannot be used to sign arbitrary data. The |callback| will be called when
304 // the dbus call completes. When the operation completes, the
305 // AsyncCallStatusWithDataHandler signal handler is called.
306 virtual void TpmAttestationSignSimpleChallenge(
307 attestation::AttestationKeyType key_type
,
308 const std::string
& key_name
,
309 const std::string
& challenge
,
310 const AsyncMethodCallback
& callback
) = 0;
312 // Gets the payload associated with the key specified by |key_type| and
313 // |key_name|. The |callback| will be called when the operation completes.
314 // If the key does not exist the callback |result| parameter will be false.
315 // If no payload has been set for the key the callback |result| parameter will
316 // be true and the |data| parameter will be empty.
317 virtual void TpmAttestationGetKeyPayload(
318 attestation::AttestationKeyType key_type
,
319 const std::string
& key_name
,
320 const DataMethodCallback
& callback
) = 0;
322 // Sets the |payload| associated with the key specified by |key_type| and
323 // |key_name|. The |callback| will be called when the operation completes.
324 // If the operation succeeds, the callback |result| parameter will be true.
325 virtual void TpmAttestationSetKeyPayload(
326 attestation::AttestationKeyType key_type
,
327 const std::string
& key_name
,
328 const std::string
& payload
,
329 const BoolDBusMethodCallback
& callback
) = 0;
332 // Create() should be used instead.
336 DISALLOW_COPY_AND_ASSIGN(CryptohomeClient
);
339 } // namespace chromeos
341 #endif // CHROMEOS_DBUS_CRYPTOHOME_CLIENT_H_