blob: cfc5c31a5180b71907999e006bb636eef426c8d1 [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 Willdenc1d1fee2016-01-26 22:44:56 -070021
22#include <utils/Vector.h>
23
Shawn Willdenbb22a6c2017-12-06 19:35:28 -070024#include <keystore/keymaster_types.h>
25
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070026#include "Keymaster.h"
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070027#include "blob.h"
Janis Danisevskis6d449e82017-06-07 18:03:31 -070028#include "grant_store.h"
Shawn Willdenfa5702f2017-12-03 15:14:58 -070029#include "user_state.h"
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070030
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070031namespace keystore {
32
33using ::android::sp;
Janis Danisevskise8ba1802017-01-30 10:49:51 +000034
Janis Danisevskisc1460142017-12-18 16:48:46 -080035class KeymasterDevices : public std::array<sp<Keymaster>, 3> {
36 public:
37 sp<Keymaster>& operator[](SecurityLevel secLevel);
38 sp<Keymaster> operator[](SecurityLevel secLevel) const;
39};
40
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070041class KeyStore {
42 public:
Janis Danisevskisc1460142017-12-18 16:48:46 -080043 KeyStore(Entropy* entropy, const KeymasterDevices& kmDevices,
44 SecurityLevel minimalAllowedSecurityLevelForNewKeys);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070045 ~KeyStore();
46
Janis Danisevskisc1460142017-12-18 16:48:46 -080047 sp<Keymaster> getDevice(SecurityLevel securityLevel) const { return mKmDevices[securityLevel]; }
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070048
Janis Danisevskisc1460142017-12-18 16:48:46 -080049 std::pair<sp<Keymaster>, SecurityLevel> getMostSecureDevice() const {
50 SecurityLevel level = SecurityLevel::STRONGBOX;
51 do {
52 if (mKmDevices[level].get()) {
53 return {mKmDevices[level], level};
54 }
55 level = static_cast<SecurityLevel>(static_cast<uint32_t>(level) - 1);
56 } while (level != SecurityLevel::SOFTWARE);
57 return {nullptr, SecurityLevel::SOFTWARE};
58 }
59
60 sp<Keymaster> getFallbackDevice() const {
Janis Danisevskise8ba1802017-01-30 10:49:51 +000061 // we only return the fallback device if the creation of new fallback key blobs is
62 // allowed. (also see getDevice below)
63 if (mAllowNewFallback) {
Janis Danisevskisc1460142017-12-18 16:48:46 -080064 return mKmDevices[SecurityLevel::SOFTWARE];
Janis Danisevskise8ba1802017-01-30 10:49:51 +000065 } else {
Janis Danisevskisc1460142017-12-18 16:48:46 -080066 return nullptr;
Janis Danisevskise8ba1802017-01-30 10:49:51 +000067 }
68 }
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070069
Janis Danisevskisc1460142017-12-18 16:48:46 -080070 sp<Keymaster> getDevice(const Blob& blob) { return mKmDevices[blob.getSecurityLevel()]; }
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070071
72 ResponseCode initialize();
73
74 State getState(uid_t userId) { return getUserState(userId)->getState(); }
75
76 ResponseCode initializeUser(const android::String8& pw, uid_t userId);
77
78 ResponseCode copyMasterKey(uid_t srcUser, uid_t dstUser);
79 ResponseCode writeMasterKey(const android::String8& pw, uid_t userId);
80 ResponseCode readMasterKey(const android::String8& pw, uid_t userId);
81
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -040082 android::String8 getKeyName(const android::String8& keyName, const BlobType type);
83 android::String8 getKeyNameForUid(const android::String8& keyName, uid_t uid,
84 const BlobType type);
85 android::String8 getKeyNameForUidWithDir(const android::String8& keyName, uid_t uid,
86 const BlobType type);
Janis Danisevskis31b44f22017-09-21 11:29:47 -070087 NullOr<android::String8> getBlobFileNameIfExists(const android::String8& alias, uid_t uid,
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070088 const BlobType type);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070089
90 /*
91 * Delete entries owned by userId. If keepUnencryptedEntries is true
92 * then only encrypted entries will be removed, otherwise all entries will
93 * be removed.
94 */
95 void resetUser(uid_t userId, bool keepUnenryptedEntries);
96 bool isEmpty(uid_t userId) const;
97
98 void lock(uid_t userId);
99
100 ResponseCode get(const char* filename, Blob* keyBlob, const BlobType type, uid_t userId);
101 ResponseCode put(const char* filename, Blob* keyBlob, uid_t userId);
102 ResponseCode del(const char* filename, const BlobType type, uid_t userId);
103 ResponseCode list(const android::String8& prefix, android::Vector<android::String16>* matches,
104 uid_t userId);
105
Janis Danisevskis6905c332017-09-01 13:24:23 -0700106 std::string addGrant(const char* alias, uid_t granterUid, uid_t granteeUid);
Janis Danisevskis31b44f22017-09-21 11:29:47 -0700107 bool removeGrant(const char* alias, const uid_t granterUid, const uid_t granteeUid);
108 void removeAllGrantsToUid(const uid_t granteeUid);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700109
110 ResponseCode importKey(const uint8_t* key, size_t keyLen, const char* filename, uid_t userId,
111 int32_t flags);
112
113 bool isHardwareBacked(const android::String16& keyType) const;
114
115 ResponseCode getKeyForName(Blob* keyBlob, const android::String8& keyName, const uid_t uid,
116 const BlobType type);
117
118 /**
119 * Returns any existing UserState or creates it if it doesn't exist.
120 */
121 UserState* getUserState(uid_t userId);
122
123 /**
124 * Returns any existing UserState or creates it if it doesn't exist.
125 */
126 UserState* getUserStateByUid(uid_t uid);
127
128 /**
129 * Returns NULL if the UserState doesn't already exist.
130 */
131 const UserState* getUserState(uid_t userId) const;
132
133 /**
134 * Returns NULL if the UserState doesn't already exist.
135 */
136 const UserState* getUserStateByUid(uid_t uid) const;
137
138 private:
Shawn Willden0329a822017-12-04 13:55:14 -0700139 static const char* kOldMasterKey;
140 static const char* kMetaDataFile;
141 static const android::String16 kRsaKeyType;
142 static const android::String16 kEcKeyType;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700143 Entropy* mEntropy;
144
Janis Danisevskisc1460142017-12-18 16:48:46 -0800145 KeymasterDevices mKmDevices;
Janis Danisevskise8ba1802017-01-30 10:49:51 +0000146 bool mAllowNewFallback;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700147
148 android::Vector<UserState*> mMasterKeys;
149
Janis Danisevskis6d449e82017-06-07 18:03:31 -0700150 ::keystore::GrantStore mGrants;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700151
Shawn Willdenfa5702f2017-12-03 15:14:58 -0700152 typedef struct { uint32_t version; } keystore_metadata_t;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700153
154 keystore_metadata_t mMetaData;
155
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700156 /**
157 * Upgrade the key from the current version to whatever is newest.
158 */
159 bool upgradeBlob(const char* filename, Blob* blob, const uint8_t oldVersion,
160 const BlobType type, uid_t uid);
161
162 /**
163 * Takes a blob that is an PEM-encoded RSA key as a byte array and converts it to a DER-encoded
164 * PKCS#8 for import into a keymaster. Then it overwrites the original blob with the new blob
165 * format that is returned from the keymaster.
166 */
167 ResponseCode importBlobAsKey(Blob* blob, const char* filename, uid_t uid);
168
169 void readMetaData();
170 void writeMetaData();
171
172 bool upgradeKeystore();
173};
174
Shawn Willdenc67a8aa2017-12-03 17:51:29 -0700175} // namespace keystore
176
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700177#endif // KEYSTORE_KEYSTORE_H_