blob: 463ed8ec4805ddba47ddfabc53e8bc18712d4b19 [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
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070035class KeyStore {
36 public:
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070037 KeyStore(Entropy* entropy, const sp<Keymaster>& device, const sp<Keymaster>& fallback,
Janis Danisevskise8ba1802017-01-30 10:49:51 +000038 bool allowNewFallback);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070039 ~KeyStore();
40
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070041 sp<Keymaster>& getDevice() { return mDevice; }
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070042
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070043 NullOr<sp<Keymaster>&> getFallbackDevice() {
Janis Danisevskise8ba1802017-01-30 10:49:51 +000044 // we only return the fallback device if the creation of new fallback key blobs is
45 // allowed. (also see getDevice below)
46 if (mAllowNewFallback) {
47 return mFallbackDevice;
48 } else {
49 return {};
50 }
51 }
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070052
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070053 sp<Keymaster>& getDevice(const Blob& blob) {
Janis Danisevskise8ba1802017-01-30 10:49:51 +000054 // We return a device, based on the nature of the blob to provide backward
55 // compatibility with old key blobs generated using the fallback device.
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070056 return blob.isFallback() ? mFallbackDevice : mDevice;
57 }
58
59 ResponseCode initialize();
60
61 State getState(uid_t userId) { return getUserState(userId)->getState(); }
62
63 ResponseCode initializeUser(const android::String8& pw, uid_t userId);
64
65 ResponseCode copyMasterKey(uid_t srcUser, uid_t dstUser);
66 ResponseCode writeMasterKey(const android::String8& pw, uid_t userId);
67 ResponseCode readMasterKey(const android::String8& pw, uid_t userId);
68
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -040069 android::String8 getKeyName(const android::String8& keyName, const BlobType type);
70 android::String8 getKeyNameForUid(const android::String8& keyName, uid_t uid,
71 const BlobType type);
72 android::String8 getKeyNameForUidWithDir(const android::String8& keyName, uid_t uid,
73 const BlobType type);
Janis Danisevskis31b44f22017-09-21 11:29:47 -070074 NullOr<android::String8> getBlobFileNameIfExists(const android::String8& alias, uid_t uid,
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070075 const BlobType type);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070076
77 /*
78 * Delete entries owned by userId. If keepUnencryptedEntries is true
79 * then only encrypted entries will be removed, otherwise all entries will
80 * be removed.
81 */
82 void resetUser(uid_t userId, bool keepUnenryptedEntries);
83 bool isEmpty(uid_t userId) const;
84
85 void lock(uid_t userId);
86
87 ResponseCode get(const char* filename, Blob* keyBlob, const BlobType type, uid_t userId);
88 ResponseCode put(const char* filename, Blob* keyBlob, uid_t userId);
89 ResponseCode del(const char* filename, const BlobType type, uid_t userId);
90 ResponseCode list(const android::String8& prefix, android::Vector<android::String16>* matches,
91 uid_t userId);
92
Janis Danisevskis6905c332017-09-01 13:24:23 -070093 std::string addGrant(const char* alias, uid_t granterUid, uid_t granteeUid);
Janis Danisevskis31b44f22017-09-21 11:29:47 -070094 bool removeGrant(const char* alias, const uid_t granterUid, const uid_t granteeUid);
95 void removeAllGrantsToUid(const uid_t granteeUid);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070096
97 ResponseCode importKey(const uint8_t* key, size_t keyLen, const char* filename, uid_t userId,
98 int32_t flags);
99
100 bool isHardwareBacked(const android::String16& keyType) const;
101
102 ResponseCode getKeyForName(Blob* keyBlob, const android::String8& keyName, const uid_t uid,
103 const BlobType type);
104
105 /**
106 * Returns any existing UserState or creates it if it doesn't exist.
107 */
108 UserState* getUserState(uid_t userId);
109
110 /**
111 * Returns any existing UserState or creates it if it doesn't exist.
112 */
113 UserState* getUserStateByUid(uid_t uid);
114
115 /**
116 * Returns NULL if the UserState doesn't already exist.
117 */
118 const UserState* getUserState(uid_t userId) const;
119
120 /**
121 * Returns NULL if the UserState doesn't already exist.
122 */
123 const UserState* getUserStateByUid(uid_t uid) const;
124
125 private:
Shawn Willden0329a822017-12-04 13:55:14 -0700126 static const char* kOldMasterKey;
127 static const char* kMetaDataFile;
128 static const android::String16 kRsaKeyType;
129 static const android::String16 kEcKeyType;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700130 Entropy* mEntropy;
131
Shawn Willdenc67a8aa2017-12-03 17:51:29 -0700132 sp<Keymaster> mDevice;
133 sp<Keymaster> mFallbackDevice;
Janis Danisevskise8ba1802017-01-30 10:49:51 +0000134 bool mAllowNewFallback;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700135
136 android::Vector<UserState*> mMasterKeys;
137
Janis Danisevskis6d449e82017-06-07 18:03:31 -0700138 ::keystore::GrantStore mGrants;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700139
Shawn Willdenfa5702f2017-12-03 15:14:58 -0700140 typedef struct { uint32_t version; } keystore_metadata_t;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700141
142 keystore_metadata_t mMetaData;
143
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700144 /**
145 * Upgrade the key from the current version to whatever is newest.
146 */
147 bool upgradeBlob(const char* filename, Blob* blob, const uint8_t oldVersion,
148 const BlobType type, uid_t uid);
149
150 /**
151 * Takes a blob that is an PEM-encoded RSA key as a byte array and converts it to a DER-encoded
152 * PKCS#8 for import into a keymaster. Then it overwrites the original blob with the new blob
153 * format that is returned from the keymaster.
154 */
155 ResponseCode importBlobAsKey(Blob* blob, const char* filename, uid_t uid);
156
157 void readMetaData();
158 void writeMetaData();
159
160 bool upgradeKeystore();
161};
162
Shawn Willdenc67a8aa2017-12-03 17:51:29 -0700163} // namespace keystore
164
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700165#endif // KEYSTORE_KEYSTORE_H_