blob: 210d99d999b8032ed1e38a838cc0a0ed008194b2 [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
20#include "user_state.h"
21
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010022#include <android/hardware/keymaster/3.0/IKeymasterDevice.h>
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070023
24#include <utils/Vector.h>
25
26#include "blob.h"
27
28typedef struct {
29 uint32_t uid;
30 const uint8_t* filename;
31} grant_t;
32
33class KeyStore {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010034 typedef ::android::sp<::android::hardware::keymaster::V3_0::IKeymasterDevice> km_device_t;
35
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070036 public:
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010037 KeyStore(Entropy* entropy, const km_device_t& device, const km_device_t& fallback);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070038 ~KeyStore();
39
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010040 km_device_t& getDevice() { return mDevice; }
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070041
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010042 km_device_t& getFallbackDevice() { return mFallbackDevice; }
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070043
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010044 km_device_t& getDevice(const Blob& blob) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070045 return blob.isFallback() ? mFallbackDevice : mDevice;
46 }
47
48 ResponseCode initialize();
49
50 State getState(uid_t userId) { return getUserState(userId)->getState(); }
51
52 ResponseCode initializeUser(const android::String8& pw, uid_t userId);
53
54 ResponseCode copyMasterKey(uid_t srcUser, uid_t dstUser);
55 ResponseCode writeMasterKey(const android::String8& pw, uid_t userId);
56 ResponseCode readMasterKey(const android::String8& pw, uid_t userId);
57
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -040058 android::String8 getKeyName(const android::String8& keyName, const BlobType type);
59 android::String8 getKeyNameForUid(const android::String8& keyName, uid_t uid,
60 const BlobType type);
61 android::String8 getKeyNameForUidWithDir(const android::String8& keyName, uid_t uid,
62 const BlobType type);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070063
64 /*
65 * Delete entries owned by userId. If keepUnencryptedEntries is true
66 * then only encrypted entries will be removed, otherwise all entries will
67 * be removed.
68 */
69 void resetUser(uid_t userId, bool keepUnenryptedEntries);
70 bool isEmpty(uid_t userId) const;
71
72 void lock(uid_t userId);
73
74 ResponseCode get(const char* filename, Blob* keyBlob, const BlobType type, uid_t userId);
75 ResponseCode put(const char* filename, Blob* keyBlob, uid_t userId);
76 ResponseCode del(const char* filename, const BlobType type, uid_t userId);
77 ResponseCode list(const android::String8& prefix, android::Vector<android::String16>* matches,
78 uid_t userId);
79
80 void addGrant(const char* filename, uid_t granteeUid);
81 bool removeGrant(const char* filename, uid_t granteeUid);
82 bool hasGrant(const char* filename, const uid_t uid) const {
83 return getGrant(filename, uid) != NULL;
84 }
85
86 ResponseCode importKey(const uint8_t* key, size_t keyLen, const char* filename, uid_t userId,
87 int32_t flags);
88
89 bool isHardwareBacked(const android::String16& keyType) const;
90
91 ResponseCode getKeyForName(Blob* keyBlob, const android::String8& keyName, const uid_t uid,
92 const BlobType type);
93
94 /**
95 * Returns any existing UserState or creates it if it doesn't exist.
96 */
97 UserState* getUserState(uid_t userId);
98
99 /**
100 * Returns any existing UserState or creates it if it doesn't exist.
101 */
102 UserState* getUserStateByUid(uid_t uid);
103
104 /**
105 * Returns NULL if the UserState doesn't already exist.
106 */
107 const UserState* getUserState(uid_t userId) const;
108
109 /**
110 * Returns NULL if the UserState doesn't already exist.
111 */
112 const UserState* getUserStateByUid(uid_t uid) const;
113
114 private:
115 static const char* sOldMasterKey;
116 static const char* sMetaDataFile;
117 static const android::String16 sRSAKeyType;
118 Entropy* mEntropy;
119
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100120 km_device_t mDevice;
121 km_device_t mFallbackDevice;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700122
123 android::Vector<UserState*> mMasterKeys;
124
125 android::Vector<grant_t*> mGrants;
126
127 typedef struct { uint32_t version; } keystore_metadata_t;
128
129 keystore_metadata_t mMetaData;
130
131 const grant_t* getGrant(const char* filename, uid_t uid) const;
132
133 /**
134 * Upgrade the key from the current version to whatever is newest.
135 */
136 bool upgradeBlob(const char* filename, Blob* blob, const uint8_t oldVersion,
137 const BlobType type, uid_t uid);
138
139 /**
140 * Takes a blob that is an PEM-encoded RSA key as a byte array and converts it to a DER-encoded
141 * PKCS#8 for import into a keymaster. Then it overwrites the original blob with the new blob
142 * format that is returned from the keymaster.
143 */
144 ResponseCode importBlobAsKey(Blob* blob, const char* filename, uid_t uid);
145
146 void readMetaData();
147 void writeMetaData();
148
149 bool upgradeKeystore();
150};
151
152#endif // KEYSTORE_KEYSTORE_H_