blob: 0027ec89922916ec2607a191d3762efbe9928ec8 [file] [log] [blame]
Shawn Willdenc1d1fee2016-01-26 22:44:56 -07001/*
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
17#ifndef KEYSTORE_KEYSTORE_H_
18#define KEYSTORE_KEYSTORE_H_
19
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010020#include <android/hardware/keymaster/3.0/IKeymasterDevice.h>
Shawn Willdena97aea42020-01-16 13:27:49 -070021#include <keymasterV4_1/Keymaster.h>
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070022#include <utils/Vector.h>
23
Shawn Willdenbb22a6c2017-12-06 19:35:28 -070024#include <keystore/keymaster_types.h>
25
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070026#include "auth_token_table.h"
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070027#include "blob.h"
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070028#include "confirmation_manager.h"
Janis Danisevskis6d449e82017-06-07 18:03:31 -070029#include "grant_store.h"
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070030#include "keymaster_worker.h"
31#include "keystore_keymaster_enforcement.h"
32#include "operation.h"
Shawn Willdenfa5702f2017-12-03 15:14:58 -070033#include "user_state.h"
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070034
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070035#include <array>
36#include <optional>
37#include <tuple>
38
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070039namespace keystore {
40
41using ::android::sp;
Shawn Willdeneedcfe92018-01-18 15:35:46 -070042using keymaster::support::Keymaster;
Janis Danisevskise8ba1802017-01-30 10:49:51 +000043
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070044template <typename T, size_t count> class Devices : public std::array<T, count> {
Janis Danisevskisc1460142017-12-18 16:48:46 -080045 public:
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070046 T& operator[](SecurityLevel secLevel) {
47 static_assert(uint32_t(SecurityLevel::SOFTWARE) == 0 &&
48 uint32_t(SecurityLevel::TRUSTED_ENVIRONMENT) == 1 &&
49 uint32_t(SecurityLevel::STRONGBOX) == 2,
50 "Numeric values of security levels have changed");
51 return std::array<T, count>::at(static_cast<uint32_t>(secLevel));
52 }
53 T operator[](SecurityLevel secLevel) const {
54 if (static_cast<uint32_t>(secLevel) > static_cast<uint32_t>(SecurityLevel::STRONGBOX)) {
55 LOG(ERROR) << "Invalid security level requested";
56 return {};
57 }
58 return (*const_cast<Devices*>(this))[secLevel];
59 }
Janis Danisevskisc1460142017-12-18 16:48:46 -080060};
61
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070062} // namespace keystore
63
64namespace std {
Dan Albertb1e79362019-01-11 15:09:54 -080065template <typename T, size_t count> struct tuple_size<keystore::Devices<T, count>> {
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070066 public:
67 static constexpr size_t value = std::tuple_size<std::array<T, count>>::value;
68};
69} // namespace std
70
71namespace keystore {
72
73using KeymasterWorkers = Devices<std::shared_ptr<KeymasterWorker>, 3>;
74using KeymasterDevices = Devices<sp<Keymaster>, 3>;
75
76class KeyStore : public ::android::IBinder::DeathRecipient {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070077 public:
Branden Archer44d1afa2018-12-28 09:10:49 -080078 KeyStore(const KeymasterDevices& kmDevices,
Janis Danisevskisc1460142017-12-18 16:48:46 -080079 SecurityLevel minimalAllowedSecurityLevelForNewKeys);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070080 ~KeyStore();
81
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070082 std::shared_ptr<KeymasterWorker> getDevice(SecurityLevel securityLevel) const {
83 return mKmDevices[securityLevel];
Janis Danisevskisc1460142017-12-18 16:48:46 -080084 }
85
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070086 std::shared_ptr<KeymasterWorker> getFallbackDevice() const {
Janis Danisevskise8ba1802017-01-30 10:49:51 +000087 // we only return the fallback device if the creation of new fallback key blobs is
88 // allowed. (also see getDevice below)
89 if (mAllowNewFallback) {
Janis Danisevskisc1460142017-12-18 16:48:46 -080090 return mKmDevices[SecurityLevel::SOFTWARE];
Janis Danisevskise8ba1802017-01-30 10:49:51 +000091 } else {
Janis Danisevskisc1460142017-12-18 16:48:46 -080092 return nullptr;
Janis Danisevskise8ba1802017-01-30 10:49:51 +000093 }
94 }
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070095
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070096 std::shared_ptr<KeymasterWorker> getDevice(const Blob& blob) {
97 return mKmDevices[blob.getSecurityLevel()];
98 }
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070099
100 ResponseCode initialize();
101
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700102 State getState(uid_t userId) { return mUserStateDB.getUserState(userId)->getState(); }
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700103
104 ResponseCode initializeUser(const android::String8& pw, uid_t userId);
105
106 ResponseCode copyMasterKey(uid_t srcUser, uid_t dstUser);
107 ResponseCode writeMasterKey(const android::String8& pw, uid_t userId);
108 ResponseCode readMasterKey(const android::String8& pw, uid_t userId);
109
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700110 LockedKeyBlobEntry getLockedBlobEntryIfNotExists(const std::string& alias, uid_t uid);
111 std::optional<KeyBlobEntry> getBlobEntryIfExists(const std::string& alias, uid_t uid);
112 LockedKeyBlobEntry getLockedBlobEntryIfExists(const std::string& alias, uid_t uid);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700113 /*
114 * Delete entries owned by userId. If keepUnencryptedEntries is true
115 * then only encrypted entries will be removed, otherwise all entries will
116 * be removed.
117 */
118 void resetUser(uid_t userId, bool keepUnenryptedEntries);
119 bool isEmpty(uid_t userId) const;
120
121 void lock(uid_t userId);
122
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700123 std::tuple<ResponseCode, Blob, Blob> get(const LockedKeyBlobEntry& blobfile);
124 ResponseCode put(const LockedKeyBlobEntry& blobfile, Blob keyBlob, Blob characteristicsBlob);
125 ResponseCode del(const LockedKeyBlobEntry& blobfile);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700126
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700127 std::string addGrant(const LockedKeyBlobEntry& blobfile, uid_t granteeUid);
128 bool removeGrant(const LockedKeyBlobEntry& blobfile, const uid_t granteeUid);
Janis Danisevskis31b44f22017-09-21 11:29:47 -0700129 void removeAllGrantsToUid(const uid_t granteeUid);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700130
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700131 ResponseCode importKey(const uint8_t* key, size_t keyLen, const LockedKeyBlobEntry& blobfile,
132 uid_t userId, int32_t flags);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700133
134 bool isHardwareBacked(const android::String16& keyType) const;
135
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700136 std::tuple<ResponseCode, Blob, Blob, LockedKeyBlobEntry>
137 getKeyForName(const android::String8& keyName, const uid_t uid, const BlobType type);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700138
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700139 void binderDied(const ::android::wp<IBinder>& who) override;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700140
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700141 UserStateDB& getUserStateDB() { return mUserStateDB; }
142 AuthTokenTable& getAuthTokenTable() { return mAuthTokenTable; }
143 KeystoreKeymasterEnforcement& getEnforcementPolicy() { return mEnforcementPolicy; }
144 ConfirmationManager& getConfirmationManager() { return *mConfirmationManager; }
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700145
Janis Danisevskisbec89992019-08-14 13:42:19 -0700146 void addOperationDevice(sp<IBinder> token, std::shared_ptr<KeymasterWorker> dev) {
147 std::lock_guard<std::mutex> lock(operationDeviceMapMutex_);
148 operationDeviceMap_.emplace(std::move(token), std::move(dev));
149 }
150 std::shared_ptr<KeymasterWorker> getOperationDevice(const sp<IBinder>& token) {
151 std::lock_guard<std::mutex> lock(operationDeviceMapMutex_);
152 auto it = operationDeviceMap_.find(token);
153 if (it != operationDeviceMap_.end()) {
154 return it->second;
155 }
156 return {};
157 }
158 void removeOperationDevice(const sp<IBinder>& token) {
159 std::lock_guard<std::mutex> lock(operationDeviceMapMutex_);
160 operationDeviceMap_.erase(token);
161 }
162
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700163 private:
Shawn Willden0329a822017-12-04 13:55:14 -0700164 static const char* kOldMasterKey;
165 static const char* kMetaDataFile;
166 static const android::String16 kRsaKeyType;
167 static const android::String16 kEcKeyType;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700168
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700169 KeymasterWorkers mKmDevices;
170
Janis Danisevskise8ba1802017-01-30 10:49:51 +0000171 bool mAllowNewFallback;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700172
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700173 UserStateDB mUserStateDB;
174 AuthTokenTable mAuthTokenTable;
175 KeystoreKeymasterEnforcement mEnforcementPolicy;
176 sp<ConfirmationManager> mConfirmationManager;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700177
Janis Danisevskis6d449e82017-06-07 18:03:31 -0700178 ::keystore::GrantStore mGrants;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700179
Shawn Willdenfa5702f2017-12-03 15:14:58 -0700180 typedef struct { uint32_t version; } keystore_metadata_t;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700181
182 keystore_metadata_t mMetaData;
183
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700184 /**
185 * Upgrade the key from the current version to whatever is newest.
186 */
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700187 bool upgradeBlob(Blob* blob, const uint8_t oldVersion);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700188
189 void readMetaData();
190 void writeMetaData();
191
192 bool upgradeKeystore();
Janis Danisevskisbec89992019-08-14 13:42:19 -0700193
194 std::mutex operationDeviceMapMutex_;
195 std::map<sp<IBinder>, std::shared_ptr<KeymasterWorker>> operationDeviceMap_;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700196};
197
Shawn Willdenc67a8aa2017-12-03 17:51:29 -0700198} // namespace keystore
199
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700200#endif // KEYSTORE_KEYSTORE_H_