blob: 4ffeaf2c4454b9dd1733e44fe27cbb8f0c4a7378 [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
24#include "blob.h"
Janis Danisevskis6d449e82017-06-07 18:03:31 -070025#include "grant_store.h"
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070026#include "include/keystore/keymaster_tags.h"
Shawn Willdenfa5702f2017-12-03 15:14:58 -070027#include "user_state.h"
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070028
Janis Danisevskise8ba1802017-01-30 10:49:51 +000029using ::keystore::NullOr;
30
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070031class KeyStore {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010032 typedef ::android::sp<::android::hardware::keymaster::V3_0::IKeymasterDevice> km_device_t;
33
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070034 public:
Janis Danisevskise8ba1802017-01-30 10:49:51 +000035 KeyStore(Entropy* entropy, const km_device_t& device, const km_device_t& fallback,
36 bool allowNewFallback);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070037 ~KeyStore();
38
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010039 km_device_t& getDevice() { return mDevice; }
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070040
Janis Danisevskise8ba1802017-01-30 10:49:51 +000041 NullOr<km_device_t&> getFallbackDevice() {
42 // we only return the fallback device if the creation of new fallback key blobs is
43 // allowed. (also see getDevice below)
44 if (mAllowNewFallback) {
45 return mFallbackDevice;
46 } else {
47 return {};
48 }
49 }
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070050
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010051 km_device_t& getDevice(const Blob& blob) {
Janis Danisevskise8ba1802017-01-30 10:49:51 +000052 // We return a device, based on the nature of the blob to provide backward
53 // compatibility with old key blobs generated using the fallback device.
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070054 return blob.isFallback() ? mFallbackDevice : mDevice;
55 }
56
57 ResponseCode initialize();
58
59 State getState(uid_t userId) { return getUserState(userId)->getState(); }
60
61 ResponseCode initializeUser(const android::String8& pw, uid_t userId);
62
63 ResponseCode copyMasterKey(uid_t srcUser, uid_t dstUser);
64 ResponseCode writeMasterKey(const android::String8& pw, uid_t userId);
65 ResponseCode readMasterKey(const android::String8& pw, uid_t userId);
66
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -040067 android::String8 getKeyName(const android::String8& keyName, const BlobType type);
68 android::String8 getKeyNameForUid(const android::String8& keyName, uid_t uid,
69 const BlobType type);
70 android::String8 getKeyNameForUidWithDir(const android::String8& keyName, uid_t uid,
71 const BlobType type);
Janis Danisevskis31b44f22017-09-21 11:29:47 -070072 NullOr<android::String8> getBlobFileNameIfExists(const android::String8& alias, uid_t uid,
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070073 const BlobType type);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070074
75 /*
76 * Delete entries owned by userId. If keepUnencryptedEntries is true
77 * then only encrypted entries will be removed, otherwise all entries will
78 * be removed.
79 */
80 void resetUser(uid_t userId, bool keepUnenryptedEntries);
81 bool isEmpty(uid_t userId) const;
82
83 void lock(uid_t userId);
84
85 ResponseCode get(const char* filename, Blob* keyBlob, const BlobType type, uid_t userId);
86 ResponseCode put(const char* filename, Blob* keyBlob, uid_t userId);
87 ResponseCode del(const char* filename, const BlobType type, uid_t userId);
88 ResponseCode list(const android::String8& prefix, android::Vector<android::String16>* matches,
89 uid_t userId);
90
Janis Danisevskis6905c332017-09-01 13:24:23 -070091 std::string addGrant(const char* alias, uid_t granterUid, uid_t granteeUid);
Janis Danisevskis31b44f22017-09-21 11:29:47 -070092 bool removeGrant(const char* alias, const uid_t granterUid, const uid_t granteeUid);
93 void removeAllGrantsToUid(const uid_t granteeUid);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070094
95 ResponseCode importKey(const uint8_t* key, size_t keyLen, const char* filename, uid_t userId,
96 int32_t flags);
97
98 bool isHardwareBacked(const android::String16& keyType) const;
99
100 ResponseCode getKeyForName(Blob* keyBlob, const android::String8& keyName, const uid_t uid,
101 const BlobType type);
102
103 /**
104 * Returns any existing UserState or creates it if it doesn't exist.
105 */
106 UserState* getUserState(uid_t userId);
107
108 /**
109 * Returns any existing UserState or creates it if it doesn't exist.
110 */
111 UserState* getUserStateByUid(uid_t uid);
112
113 /**
114 * Returns NULL if the UserState doesn't already exist.
115 */
116 const UserState* getUserState(uid_t userId) const;
117
118 /**
119 * Returns NULL if the UserState doesn't already exist.
120 */
121 const UserState* getUserStateByUid(uid_t uid) const;
122
123 private:
124 static const char* sOldMasterKey;
125 static const char* sMetaDataFile;
126 static const android::String16 sRSAKeyType;
127 Entropy* mEntropy;
128
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100129 km_device_t mDevice;
130 km_device_t mFallbackDevice;
Janis Danisevskise8ba1802017-01-30 10:49:51 +0000131 bool mAllowNewFallback;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700132
133 android::Vector<UserState*> mMasterKeys;
134
Janis Danisevskis6d449e82017-06-07 18:03:31 -0700135 ::keystore::GrantStore mGrants;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700136
Shawn Willdenfa5702f2017-12-03 15:14:58 -0700137 typedef struct { uint32_t version; } keystore_metadata_t;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700138
139 keystore_metadata_t mMetaData;
140
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700141 /**
142 * Upgrade the key from the current version to whatever is newest.
143 */
144 bool upgradeBlob(const char* filename, Blob* blob, const uint8_t oldVersion,
145 const BlobType type, uid_t uid);
146
147 /**
148 * Takes a blob that is an PEM-encoded RSA key as a byte array and converts it to a DER-encoded
149 * PKCS#8 for import into a keymaster. Then it overwrites the original blob with the new blob
150 * format that is returned from the keymaster.
151 */
152 ResponseCode importBlobAsKey(Blob* blob, const char* filename, uid_t uid);
153
154 void readMetaData();
155 void writeMetaData();
156
157 bool upgradeKeystore();
158};
159
160#endif // KEYSTORE_KEYSTORE_H_