KeyStore.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef KEYSTORE_KEYSTORE_H_
  17. #define KEYSTORE_KEYSTORE_H_
  18. #include <android/hardware/keymaster/3.0/IKeymasterDevice.h>
  19. #include <keymasterV4_0/Keymaster.h>
  20. #include <utils/Vector.h>
  21. #include <keystore/keymaster_types.h>
  22. #include "auth_token_table.h"
  23. #include "blob.h"
  24. #include "confirmation_manager.h"
  25. #include "grant_store.h"
  26. #include "keymaster_worker.h"
  27. #include "keystore_keymaster_enforcement.h"
  28. #include "operation.h"
  29. #include "user_state.h"
  30. #include <array>
  31. #include <optional>
  32. #include <tuple>
  33. namespace keystore {
  34. using ::android::sp;
  35. using keymaster::support::Keymaster;
  36. template <typename T, size_t count> class Devices : public std::array<T, count> {
  37. public:
  38. T& operator[](SecurityLevel secLevel) {
  39. static_assert(uint32_t(SecurityLevel::SOFTWARE) == 0 &&
  40. uint32_t(SecurityLevel::TRUSTED_ENVIRONMENT) == 1 &&
  41. uint32_t(SecurityLevel::STRONGBOX) == 2,
  42. "Numeric values of security levels have changed");
  43. return std::array<T, count>::at(static_cast<uint32_t>(secLevel));
  44. }
  45. T operator[](SecurityLevel secLevel) const {
  46. if (static_cast<uint32_t>(secLevel) > static_cast<uint32_t>(SecurityLevel::STRONGBOX)) {
  47. LOG(ERROR) << "Invalid security level requested";
  48. return {};
  49. }
  50. return (*const_cast<Devices*>(this))[secLevel];
  51. }
  52. };
  53. } // namespace keystore
  54. namespace std {
  55. template <typename T, size_t count> struct tuple_size<keystore::Devices<T, count>> {
  56. public:
  57. static constexpr size_t value = std::tuple_size<std::array<T, count>>::value;
  58. };
  59. } // namespace std
  60. namespace keystore {
  61. using KeymasterWorkers = Devices<std::shared_ptr<KeymasterWorker>, 3>;
  62. using KeymasterDevices = Devices<sp<Keymaster>, 3>;
  63. class KeyStore : public ::android::IBinder::DeathRecipient {
  64. public:
  65. KeyStore(const KeymasterDevices& kmDevices,
  66. SecurityLevel minimalAllowedSecurityLevelForNewKeys);
  67. ~KeyStore();
  68. std::shared_ptr<KeymasterWorker> getDevice(SecurityLevel securityLevel) const {
  69. return mKmDevices[securityLevel];
  70. }
  71. std::shared_ptr<KeymasterWorker> getFallbackDevice() const {
  72. // we only return the fallback device if the creation of new fallback key blobs is
  73. // allowed. (also see getDevice below)
  74. if (mAllowNewFallback) {
  75. return mKmDevices[SecurityLevel::SOFTWARE];
  76. } else {
  77. return nullptr;
  78. }
  79. }
  80. std::shared_ptr<KeymasterWorker> getDevice(const Blob& blob) {
  81. return mKmDevices[blob.getSecurityLevel()];
  82. }
  83. ResponseCode initialize();
  84. State getState(uid_t userId) { return mUserStateDB.getUserState(userId)->getState(); }
  85. ResponseCode initializeUser(const android::String8& pw, uid_t userId);
  86. ResponseCode copyMasterKey(uid_t srcUser, uid_t dstUser);
  87. ResponseCode writeMasterKey(const android::String8& pw, uid_t userId);
  88. ResponseCode readMasterKey(const android::String8& pw, uid_t userId);
  89. LockedKeyBlobEntry getLockedBlobEntryIfNotExists(const std::string& alias, uid_t uid);
  90. std::optional<KeyBlobEntry> getBlobEntryIfExists(const std::string& alias, uid_t uid);
  91. LockedKeyBlobEntry getLockedBlobEntryIfExists(const std::string& alias, uid_t uid);
  92. /*
  93. * Delete entries owned by userId. If keepUnencryptedEntries is true
  94. * then only encrypted entries will be removed, otherwise all entries will
  95. * be removed.
  96. */
  97. void resetUser(uid_t userId, bool keepUnenryptedEntries);
  98. bool isEmpty(uid_t userId) const;
  99. void lock(uid_t userId);
  100. std::tuple<ResponseCode, Blob, Blob> get(const LockedKeyBlobEntry& blobfile);
  101. ResponseCode put(const LockedKeyBlobEntry& blobfile, Blob keyBlob, Blob characteristicsBlob);
  102. ResponseCode del(const LockedKeyBlobEntry& blobfile);
  103. std::string addGrant(const LockedKeyBlobEntry& blobfile, uid_t granteeUid);
  104. bool removeGrant(const LockedKeyBlobEntry& blobfile, const uid_t granteeUid);
  105. void removeAllGrantsToUid(const uid_t granteeUid);
  106. ResponseCode importKey(const uint8_t* key, size_t keyLen, const LockedKeyBlobEntry& blobfile,
  107. uid_t userId, int32_t flags);
  108. bool isHardwareBacked(const android::String16& keyType) const;
  109. std::tuple<ResponseCode, Blob, Blob, LockedKeyBlobEntry>
  110. getKeyForName(const android::String8& keyName, const uid_t uid, const BlobType type);
  111. void binderDied(const ::android::wp<IBinder>& who) override;
  112. UserStateDB& getUserStateDB() { return mUserStateDB; }
  113. AuthTokenTable& getAuthTokenTable() { return mAuthTokenTable; }
  114. KeystoreKeymasterEnforcement& getEnforcementPolicy() { return mEnforcementPolicy; }
  115. ConfirmationManager& getConfirmationManager() { return *mConfirmationManager; }
  116. void addOperationDevice(sp<IBinder> token, std::shared_ptr<KeymasterWorker> dev) {
  117. std::lock_guard<std::mutex> lock(operationDeviceMapMutex_);
  118. operationDeviceMap_.emplace(std::move(token), std::move(dev));
  119. }
  120. std::shared_ptr<KeymasterWorker> getOperationDevice(const sp<IBinder>& token) {
  121. std::lock_guard<std::mutex> lock(operationDeviceMapMutex_);
  122. auto it = operationDeviceMap_.find(token);
  123. if (it != operationDeviceMap_.end()) {
  124. return it->second;
  125. }
  126. return {};
  127. }
  128. void removeOperationDevice(const sp<IBinder>& token) {
  129. std::lock_guard<std::mutex> lock(operationDeviceMapMutex_);
  130. operationDeviceMap_.erase(token);
  131. }
  132. private:
  133. static const char* kOldMasterKey;
  134. static const char* kMetaDataFile;
  135. static const android::String16 kRsaKeyType;
  136. static const android::String16 kEcKeyType;
  137. KeymasterWorkers mKmDevices;
  138. bool mAllowNewFallback;
  139. UserStateDB mUserStateDB;
  140. AuthTokenTable mAuthTokenTable;
  141. KeystoreKeymasterEnforcement mEnforcementPolicy;
  142. sp<ConfirmationManager> mConfirmationManager;
  143. ::keystore::GrantStore mGrants;
  144. typedef struct { uint32_t version; } keystore_metadata_t;
  145. keystore_metadata_t mMetaData;
  146. /**
  147. * Upgrade the key from the current version to whatever is newest.
  148. */
  149. bool upgradeBlob(Blob* blob, const uint8_t oldVersion);
  150. void readMetaData();
  151. void writeMetaData();
  152. bool upgradeKeystore();
  153. std::mutex operationDeviceMapMutex_;
  154. std::map<sp<IBinder>, std::shared_ptr<KeymasterWorker>> operationDeviceMap_;
  155. };
  156. } // namespace keystore
  157. #endif // KEYSTORE_KEYSTORE_H_