Home | History | Annotate | Download | only in dbus
      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 #ifndef CHROMEOS_DBUS_FAKE_CRYPTOHOME_CLIENT_H_
      6 #define CHROMEOS_DBUS_FAKE_CRYPTOHOME_CLIENT_H_
      7 
      8 #include <map>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/memory/weak_ptr.h"
     12 #include "chromeos/dbus/cryptohome_client.h"
     13 
     14 namespace chromeos {
     15 
     16 class CHROMEOS_EXPORT FakeCryptohomeClient : public CryptohomeClient {
     17  public:
     18   FakeCryptohomeClient();
     19   virtual ~FakeCryptohomeClient();
     20 
     21   virtual void Init(dbus::Bus* bus) OVERRIDE;
     22   virtual void SetAsyncCallStatusHandlers(
     23       const AsyncCallStatusHandler& handler,
     24       const AsyncCallStatusWithDataHandler& data_handler) OVERRIDE;
     25   virtual void ResetAsyncCallStatusHandlers() OVERRIDE;
     26   virtual void WaitForServiceToBeAvailable(
     27       const WaitForServiceToBeAvailableCallback& callback) OVERRIDE;
     28   virtual void IsMounted(const BoolDBusMethodCallback& callback) OVERRIDE;
     29   virtual bool Unmount(bool* success) OVERRIDE;
     30   virtual void AsyncCheckKey(const std::string& username,
     31                              const std::string& key,
     32                              const AsyncMethodCallback& callback) OVERRIDE;
     33   virtual void AsyncMigrateKey(const std::string& username,
     34                                const std::string& from_key,
     35                                const std::string& to_key,
     36                                const AsyncMethodCallback& callback) OVERRIDE;
     37   virtual void AsyncRemove(const std::string& username,
     38                            const AsyncMethodCallback& callback) OVERRIDE;
     39   virtual void GetSystemSalt(const GetSystemSaltCallback& callback) OVERRIDE;
     40   virtual void GetSanitizedUsername(
     41       const std::string& username,
     42       const StringDBusMethodCallback& callback) OVERRIDE;
     43   virtual std::string BlockingGetSanitizedUsername(
     44       const std::string& username) OVERRIDE;
     45   virtual void AsyncMount(const std::string& username,
     46                           const std::string& key,
     47                           int flags,
     48                           const AsyncMethodCallback& callback) OVERRIDE;
     49   virtual void AsyncAddKey(const std::string& username,
     50                            const std::string& key,
     51                            const std::string& new_key,
     52                            const AsyncMethodCallback& callback) OVERRIDE;
     53   virtual void AsyncMountGuest(const AsyncMethodCallback& callback) OVERRIDE;
     54   virtual void AsyncMountPublic(const std::string& public_mount_id,
     55                                 int flags,
     56                                 const AsyncMethodCallback& callback) OVERRIDE;
     57   virtual void TpmIsReady(const BoolDBusMethodCallback& callback) OVERRIDE;
     58   virtual void TpmIsEnabled(const BoolDBusMethodCallback& callback) OVERRIDE;
     59   virtual bool CallTpmIsEnabledAndBlock(bool* enabled) OVERRIDE;
     60   virtual void TpmGetPassword(
     61       const StringDBusMethodCallback& callback) OVERRIDE;
     62   virtual void TpmIsOwned(const BoolDBusMethodCallback& callback) OVERRIDE;
     63   virtual bool CallTpmIsOwnedAndBlock(bool* owned) OVERRIDE;
     64   virtual void TpmIsBeingOwned(const BoolDBusMethodCallback& callback) OVERRIDE;
     65   virtual bool CallTpmIsBeingOwnedAndBlock(bool* owning) OVERRIDE;
     66   virtual void TpmCanAttemptOwnership(
     67       const VoidDBusMethodCallback& callback) OVERRIDE;
     68   virtual void TpmClearStoredPassword(
     69       const VoidDBusMethodCallback& callback) OVERRIDE;
     70   virtual bool CallTpmClearStoredPasswordAndBlock() OVERRIDE;
     71   virtual void Pkcs11IsTpmTokenReady(
     72       const BoolDBusMethodCallback& callback) OVERRIDE;
     73   virtual void Pkcs11GetTpmTokenInfo(
     74       const Pkcs11GetTpmTokenInfoCallback& callback) OVERRIDE;
     75   virtual void Pkcs11GetTpmTokenInfoForUser(
     76       const std::string& username,
     77       const Pkcs11GetTpmTokenInfoCallback& callback) OVERRIDE;
     78   virtual bool InstallAttributesGet(const std::string& name,
     79                                     std::vector<uint8>* value,
     80                                     bool* successful) OVERRIDE;
     81   virtual bool InstallAttributesSet(const std::string& name,
     82                                     const std::vector<uint8>& value,
     83                                     bool* successful) OVERRIDE;
     84   virtual bool InstallAttributesFinalize(bool* successful) OVERRIDE;
     85   virtual void InstallAttributesIsReady(
     86       const BoolDBusMethodCallback& callback) OVERRIDE;
     87   virtual bool InstallAttributesIsInvalid(bool* is_invalid) OVERRIDE;
     88   virtual bool InstallAttributesIsFirstInstall(bool* is_first_install) OVERRIDE;
     89   virtual void TpmAttestationIsPrepared(
     90       const BoolDBusMethodCallback& callback) OVERRIDE;
     91   virtual void TpmAttestationIsEnrolled(
     92       const BoolDBusMethodCallback& callback) OVERRIDE;
     93   virtual void AsyncTpmAttestationCreateEnrollRequest(
     94       chromeos::attestation::PrivacyCAType pca_type,
     95       const AsyncMethodCallback& callback) OVERRIDE;
     96   virtual void AsyncTpmAttestationEnroll(
     97       chromeos::attestation::PrivacyCAType pca_type,
     98       const std::string& pca_response,
     99       const AsyncMethodCallback& callback) OVERRIDE;
    100   virtual void AsyncTpmAttestationCreateCertRequest(
    101       chromeos::attestation::PrivacyCAType pca_type,
    102       attestation::AttestationCertificateProfile certificate_profile,
    103       const std::string& user_id,
    104       const std::string& request_origin,
    105       const AsyncMethodCallback& callback) OVERRIDE;
    106   virtual void AsyncTpmAttestationFinishCertRequest(
    107       const std::string& pca_response,
    108       attestation::AttestationKeyType key_type,
    109       const std::string& user_id,
    110       const std::string& key_name,
    111       const AsyncMethodCallback& callback) OVERRIDE;
    112   virtual void TpmAttestationDoesKeyExist(
    113       attestation::AttestationKeyType key_type,
    114       const std::string& user_id,
    115       const std::string& key_name,
    116       const BoolDBusMethodCallback& callback) OVERRIDE;
    117   virtual void TpmAttestationGetCertificate(
    118       attestation::AttestationKeyType key_type,
    119       const std::string& user_id,
    120       const std::string& key_name,
    121       const DataMethodCallback& callback) OVERRIDE;
    122   virtual void TpmAttestationGetPublicKey(
    123       attestation::AttestationKeyType key_type,
    124       const std::string& user_id,
    125       const std::string& key_name,
    126       const DataMethodCallback& callback) OVERRIDE;
    127   virtual void TpmAttestationRegisterKey(
    128       attestation::AttestationKeyType key_type,
    129       const std::string& user_id,
    130       const std::string& key_name,
    131       const AsyncMethodCallback& callback) OVERRIDE;
    132   virtual void TpmAttestationSignEnterpriseChallenge(
    133       attestation::AttestationKeyType key_type,
    134       const std::string& user_id,
    135       const std::string& key_name,
    136       const std::string& domain,
    137       const std::string& device_id,
    138       attestation::AttestationChallengeOptions options,
    139       const std::string& challenge,
    140       const AsyncMethodCallback& callback) OVERRIDE;
    141   virtual void TpmAttestationSignSimpleChallenge(
    142       attestation::AttestationKeyType key_type,
    143       const std::string& user_id,
    144       const std::string& key_name,
    145       const std::string& challenge,
    146       const AsyncMethodCallback& callback) OVERRIDE;
    147   virtual void TpmAttestationGetKeyPayload(
    148       attestation::AttestationKeyType key_type,
    149       const std::string& user_id,
    150       const std::string& key_name,
    151       const DataMethodCallback& callback) OVERRIDE;
    152   virtual void TpmAttestationSetKeyPayload(
    153       attestation::AttestationKeyType key_type,
    154       const std::string& user_id,
    155       const std::string& key_name,
    156       const std::string& payload,
    157       const BoolDBusMethodCallback& callback) OVERRIDE;
    158   virtual void TpmAttestationDeleteKeys(
    159       attestation::AttestationKeyType key_type,
    160       const std::string& user_id,
    161       const std::string& key_prefix,
    162       const BoolDBusMethodCallback& callback) OVERRIDE;
    163   virtual void GetKeyDataEx(
    164       const cryptohome::AccountIdentifier& id,
    165       const cryptohome::AuthorizationRequest& auth,
    166       const cryptohome::GetKeyDataRequest& request,
    167       const ProtobufMethodCallback& callback) OVERRIDE;
    168   virtual void CheckKeyEx(
    169       const cryptohome::AccountIdentifier& id,
    170       const cryptohome::AuthorizationRequest& auth,
    171       const cryptohome::CheckKeyRequest& request,
    172       const ProtobufMethodCallback& callback) OVERRIDE;
    173   virtual void MountEx(
    174       const cryptohome::AccountIdentifier& id,
    175       const cryptohome::AuthorizationRequest& auth,
    176       const cryptohome::MountRequest& request,
    177       const ProtobufMethodCallback& callback) OVERRIDE;
    178   virtual void AddKeyEx(
    179       const cryptohome::AccountIdentifier& id,
    180       const cryptohome::AuthorizationRequest& auth,
    181       const cryptohome::AddKeyRequest& request,
    182       const ProtobufMethodCallback& callback) OVERRIDE;
    183   virtual void UpdateKeyEx(
    184       const cryptohome::AccountIdentifier& id,
    185       const cryptohome::AuthorizationRequest& auth,
    186       const cryptohome::UpdateKeyRequest& request,
    187       const ProtobufMethodCallback& callback) OVERRIDE;
    188   virtual void RemoveKeyEx(const cryptohome::AccountIdentifier& id,
    189                            const cryptohome::AuthorizationRequest& auth,
    190                            const cryptohome::RemoveKeyRequest& request,
    191                            const ProtobufMethodCallback& callback) OVERRIDE;
    192   virtual void GetBootAttribute(
    193       const cryptohome::GetBootAttributeRequest& request,
    194       const ProtobufMethodCallback& callback) OVERRIDE;
    195   virtual void SetBootAttribute(
    196       const cryptohome::SetBootAttributeRequest& request,
    197       const ProtobufMethodCallback& callback) OVERRIDE;
    198   virtual void FlushAndSignBootAttributes(
    199       const cryptohome::FlushAndSignBootAttributesRequest& request,
    200       const ProtobufMethodCallback& callback) OVERRIDE;
    201 
    202   // Changes the behavior of WaitForServiceToBeAvailable(). This method runs
    203   // pending callbacks if is_available is true.
    204   void SetServiceIsAvailable(bool is_available);
    205 
    206   // Sets the unmount result of Unmount() call.
    207   void set_unmount_result(bool result) {
    208     unmount_result_= result;
    209   }
    210 
    211   // Sets the system salt which will be returned from GetSystemSalt(). By
    212   // default, GetSystemSalt() returns the value generated by
    213   // GetStubSystemSalt().
    214   void set_system_salt(const std::vector<uint8>& system_salt) {
    215     system_salt_ = system_salt;
    216   }
    217 
    218   // Returns the stub system salt as raw bytes. (not as a string encoded in the
    219   // format used by SystemSaltGetter::ConvertRawSaltToHexString()).
    220   static std::vector<uint8> GetStubSystemSalt();
    221 
    222  private:
    223   void ReturnProtobufMethodCallback(
    224       const cryptohome::BaseReply& reply,
    225       const ProtobufMethodCallback& callback);
    226 
    227   // Posts tasks which return fake results to the UI thread.
    228   void ReturnAsyncMethodResult(const AsyncMethodCallback& callback,
    229                                bool returns_data);
    230 
    231   // This method is used to implement ReturnAsyncMethodResult.
    232   void ReturnAsyncMethodResultInternal(const AsyncMethodCallback& callback,
    233                                        bool returns_data);
    234 
    235   bool service_is_available_;
    236   int async_call_id_;
    237   AsyncCallStatusHandler async_call_status_handler_;
    238   AsyncCallStatusWithDataHandler async_call_status_data_handler_;
    239   int tpm_is_ready_counter_;
    240   bool unmount_result_;
    241   std::vector<uint8> system_salt_;
    242 
    243   std::vector<WaitForServiceToBeAvailableCallback>
    244       pending_wait_for_service_to_be_available_callbacks_;
    245 
    246   // A stub store for InstallAttributes, mapping an attribute name to the
    247   // associated data blob. Used to implement InstallAttributesSet and -Get.
    248   std::map<std::string, std::vector<uint8> > install_attrs_;
    249   bool locked_;
    250   base::WeakPtrFactory<FakeCryptohomeClient> weak_ptr_factory_;
    251 
    252   DISALLOW_COPY_AND_ASSIGN(FakeCryptohomeClient);
    253 };
    254 
    255 }  // namespace chromeos
    256 
    257 #endif  // CHROMEOS_DBUS_FAKE_CRYPTOHOME_CLIENT_H_
    258