blob: 4060bd13253def1cae460f1b246e7d0be0b58a7e [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_SERVICE_H_
18#define KEYSTORE_KEYSTORE_SERVICE_H_
19
20#include <keystore/IKeystoreService.h>
21
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010022#include <keystore/authorization_set.h>
Shawn Willden98c59162016-03-20 09:10:18 -060023
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070024#include "auth_token_table.h"
25#include "keystore.h"
26#include "keystore_keymaster_enforcement.h"
27#include "operation.h"
28#include "permissions.h"
29
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010030namespace keystore {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070031
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010032class KeyStoreService : public android::BnKeystoreService, public android::IBinder::DeathRecipient {
33 typedef ::android::sp<::android::hardware::keymaster::V3_0::IKeymasterDevice> km_device_t;
34
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070035 public:
Chih-Hung Hsiehd7791be2016-07-12 11:58:02 -070036 explicit KeyStoreService(KeyStore* keyStore) : mKeyStore(keyStore), mOperationMap(this) {}
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070037
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010038 void binderDied(const android::wp<android::IBinder>& who);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070039
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010040 KeyStoreServiceReturnCode getState(int32_t userId) override;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070041
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010042 KeyStoreServiceReturnCode get(const android::String16& name, int32_t uid,
43 hidl_vec<uint8_t>* item) override;
44 KeyStoreServiceReturnCode insert(const android::String16& name, const hidl_vec<uint8_t>& item,
45 int targetUid, int32_t flags) override;
46 KeyStoreServiceReturnCode del(const android::String16& name, int targetUid) override;
47 KeyStoreServiceReturnCode exist(const android::String16& name, int targetUid) override;
48 KeyStoreServiceReturnCode list(const android::String16& prefix, int targetUid,
49 android::Vector<android::String16>* matches) override;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070050
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010051 KeyStoreServiceReturnCode reset() override;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070052
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010053 KeyStoreServiceReturnCode onUserPasswordChanged(int32_t userId,
54 const android::String16& password) override;
55 KeyStoreServiceReturnCode onUserAdded(int32_t userId, int32_t parentId) override;
56 KeyStoreServiceReturnCode onUserRemoved(int32_t userId) override;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070057
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010058 KeyStoreServiceReturnCode lock(int32_t userId) override;
59 KeyStoreServiceReturnCode unlock(int32_t userId, const android::String16& pw) override;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070060
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010061 bool isEmpty(int32_t userId) override;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070062
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010063 KeyStoreServiceReturnCode
64 generate(const android::String16& name, int32_t targetUid, int32_t keyType, int32_t keySize,
65 int32_t flags, android::Vector<android::sp<android::KeystoreArg>>* args) override;
66 KeyStoreServiceReturnCode import(const android::String16& name, const hidl_vec<uint8_t>& data,
67 int targetUid, int32_t flags) override;
68 KeyStoreServiceReturnCode sign(const android::String16& name, const hidl_vec<uint8_t>& data,
69 hidl_vec<uint8_t>* out) override;
70 KeyStoreServiceReturnCode verify(const android::String16& name, const hidl_vec<uint8_t>& data,
71 const hidl_vec<uint8_t>& signature) override;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070072
73 /*
74 * TODO: The abstraction between things stored in hardware and regular blobs
75 * of data stored on the filesystem should be moved down to keystore itself.
76 * Unfortunately the Java code that calls this has naming conventions that it
77 * knows about. Ideally keystore shouldn't be used to store random blobs of
78 * data.
79 *
80 * Until that happens, it's necessary to have a separate "get_pubkey" and
81 * "del_key" since the Java code doesn't really communicate what it's
82 * intentions are.
83 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010084 KeyStoreServiceReturnCode get_pubkey(const android::String16& name,
85 hidl_vec<uint8_t>* pubKey) override;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070086
Janis Danisevskis6d449e82017-06-07 18:03:31 -070087 android::String16 grant(const android::String16& name, int32_t granteeUid) override;
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010088 KeyStoreServiceReturnCode ungrant(const android::String16& name, int32_t granteeUid) override;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070089
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010090 int64_t getmtime(const android::String16& name, int32_t uid) override;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070091
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010092 KeyStoreServiceReturnCode duplicate(const android::String16& srcKey, int32_t srcUid,
93 const android::String16& destKey, int32_t destUid) override;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070094
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010095 int32_t is_hardware_backed(const android::String16& keyType) override;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070096
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010097 KeyStoreServiceReturnCode clear_uid(int64_t targetUid64) override;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070098
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010099 KeyStoreServiceReturnCode addRngEntropy(const hidl_vec<uint8_t>& entropy) override;
100 KeyStoreServiceReturnCode generateKey(const android::String16& name,
101 const hidl_vec<KeyParameter>& params,
102 const hidl_vec<uint8_t>& entropy, int uid, int flags,
103 KeyCharacteristics* outCharacteristics) override;
104 KeyStoreServiceReturnCode
105 getKeyCharacteristics(const android::String16& name, const hidl_vec<uint8_t>& clientId,
106 const hidl_vec<uint8_t>& appData, int32_t uid,
107 KeyCharacteristics* outCharacteristics) override;
108 KeyStoreServiceReturnCode importKey(const android::String16& name,
109 const hidl_vec<KeyParameter>& params, KeyFormat format,
110 const hidl_vec<uint8_t>& keyData, int uid, int flags,
111 KeyCharacteristics* outCharacteristics) override;
112 void exportKey(const android::String16& name, KeyFormat format,
113 const hidl_vec<uint8_t>& clientId, const hidl_vec<uint8_t>& appData, int32_t uid,
114 android::ExportResult* result) override;
115 void begin(const sp<android::IBinder>& appToken, const android::String16& name,
116 KeyPurpose purpose, bool pruneable, const hidl_vec<KeyParameter>& params,
117 const hidl_vec<uint8_t>& entropy, int32_t uid,
118 android::OperationResult* result) override;
119 void update(const sp<android::IBinder>& token, const hidl_vec<KeyParameter>& params,
120 const hidl_vec<uint8_t>& data, android::OperationResult* result) override;
121 void finish(const sp<android::IBinder>& token, const hidl_vec<KeyParameter>& params,
122 const hidl_vec<uint8_t>& signature, const hidl_vec<uint8_t>& entropy,
123 android::OperationResult* result) override;
124 KeyStoreServiceReturnCode abort(const sp<android::IBinder>& token) override;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700125
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100126 bool isOperationAuthorized(const sp<android::IBinder>& token) override;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700127
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100128 KeyStoreServiceReturnCode addAuthToken(const uint8_t* token, size_t length) override;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700129
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100130 KeyStoreServiceReturnCode attestKey(const android::String16& name,
131 const hidl_vec<KeyParameter>& params,
132 hidl_vec<hidl_vec<uint8_t>>* outChain) override;
Shawn Willden50eb1b22016-01-21 12:41:23 -0700133
Bartosz Fabianowski5aa93e02017-04-24 13:54:49 +0200134 KeyStoreServiceReturnCode attestDeviceIds(const hidl_vec<KeyParameter>& params,
135 hidl_vec<hidl_vec<uint8_t>>* outChain) override;
136
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100137 KeyStoreServiceReturnCode onDeviceOffBody() override;
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400138
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700139 private:
140 static const int32_t UID_SELF = -1;
141
142 /**
143 * Prune the oldest pruneable operation.
144 */
145 bool pruneOperation();
146
147 /**
148 * Get the effective target uid for a binder operation that takes an
149 * optional uid as the target.
150 */
151 uid_t getEffectiveUid(int32_t targetUid);
152
153 /**
154 * Check if the caller of the current binder method has the required
155 * permission and if acting on other uids the grants to do so.
156 */
157 bool checkBinderPermission(perm_t permission, int32_t targetUid = UID_SELF);
158
159 /**
160 * Check if the caller of the current binder method has the required
161 * permission and the target uid is the caller or the caller is system.
162 */
163 bool checkBinderPermissionSelfOrSystem(perm_t permission, int32_t targetUid);
164
165 /**
166 * Check if the caller of the current binder method has the required
167 * permission or the target of the operation is the caller's uid. This is
168 * for operation where the permission is only for cross-uid activity and all
169 * uids are allowed to act on their own (ie: clearing all entries for a
170 * given uid).
171 */
172 bool checkBinderPermissionOrSelfTarget(perm_t permission, int32_t targetUid);
173
174 /**
175 * Helper method to check that the caller has the required permission as
176 * well as the keystore is in the unlocked state if checkUnlocked is true.
177 *
178 * Returns NO_ERROR on success, PERMISSION_DENIED on a permission error and
179 * otherwise the state of keystore when not unlocked and checkUnlocked is
180 * true.
181 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100182 KeyStoreServiceReturnCode checkBinderPermissionAndKeystoreState(perm_t permission,
183 int32_t targetUid = -1,
184 bool checkUnlocked = true);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700185
186 bool isKeystoreUnlocked(State state);
187
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700188 /**
189 * Check that all keymaster_key_param_t's provided by the application are
190 * allowed. Any parameter that keystore adds itself should be disallowed here.
191 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100192 bool checkAllowedOperationParams(const hidl_vec<KeyParameter>& params);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700193
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100194 ErrorCode getOperationCharacteristics(const hidl_vec<uint8_t>& key, km_device_t* dev,
195 const AuthorizationSet& params, KeyCharacteristics* out);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700196
197 /**
198 * Get the auth token for this operation from the auth token table.
199 *
200 * Returns ::NO_ERROR if the auth token was set or none was required.
201 * ::OP_AUTH_NEEDED if it is a per op authorization, no
202 * authorization token exists for that operation and
203 * failOnTokenMissing is false.
204 * KM_ERROR_KEY_USER_NOT_AUTHENTICATED if there is no valid auth
205 * token for the operation
206 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100207 KeyStoreServiceReturnCode getAuthToken(const KeyCharacteristics& characteristics,
208 uint64_t handle, KeyPurpose purpose,
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000209 const HardwareAuthToken** authToken,
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100210 bool failOnTokenMissing = true);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700211
212 /**
213 * Add the auth token for the operation to the param list if the operation
214 * requires authorization. Uses the cached result in the OperationMap if available
215 * otherwise gets the token from the AuthTokenTable and caches the result.
216 *
217 * Returns ::NO_ERROR if the auth token was added or not needed.
218 * KM_ERROR_KEY_USER_NOT_AUTHENTICATED if the operation is not
219 * authenticated.
220 * KM_ERROR_INVALID_OPERATION_HANDLE if token is not a valid
221 * operation token.
222 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100223 KeyStoreServiceReturnCode addOperationAuthTokenIfNeeded(const sp<android::IBinder>& token,
224 AuthorizationSet* params);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700225
226 /**
227 * Translate a result value to a legacy return value. All keystore errors are
228 * preserved and keymaster errors become SYSTEM_ERRORs
229 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100230 KeyStoreServiceReturnCode translateResultToLegacyResult(int32_t result);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700231
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100232 void addLegacyBeginParams(const android::String16& name, AuthorizationSet* params);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700233
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100234 KeyStoreServiceReturnCode doLegacySignVerify(const android::String16& name,
235 const hidl_vec<uint8_t>& data,
236 hidl_vec<uint8_t>* out,
237 const hidl_vec<uint8_t>& signature,
238 KeyPurpose purpose);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700239
Shawn Willden98c59162016-03-20 09:10:18 -0600240 /**
241 * Upgrade a key blob under alias "name", returning the new blob in "blob". If "blob"
242 * previously contained data, it will be overwritten.
243 *
244 * Returns ::NO_ERROR if the key was upgraded successfully.
245 * KM_ERROR_VERSION_MISMATCH if called on a key whose patch level is greater than or
246 * equal to the current system patch level.
247 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100248 KeyStoreServiceReturnCode upgradeKeyBlob(const android::String16& name, uid_t targetUid,
249 const AuthorizationSet& params, Blob* blob);
Shawn Willden98c59162016-03-20 09:10:18 -0600250
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700251 ::KeyStore* mKeyStore;
252 OperationMap mOperationMap;
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100253 keystore::AuthTokenTable mAuthTokenTable;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700254 KeystoreKeymasterEnforcement enforcement_policy;
255};
256
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100257}; // namespace keystore
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700258
259#endif // KEYSTORE_KEYSTORE_SERVICE_H_