blob: 958b0dcb2ded96377af58ba78bb85455a5339295 [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
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070020#include <android/security/BnKeystoreService.h>
Shawn Willden98c59162016-03-20 09:10:18 -060021
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070022#include "auth_token_table.h"
David Zeuthenc6eb7cd2017-11-27 11:33:55 -050023#include "confirmation_manager.h"
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070024
Shawn Willdenfa5702f2017-12-03 15:14:58 -070025#include "KeyStore.h"
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070026#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
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070032// Class provides implementation for generated BnKeystoreService.h based on
33// gen/aidl/android/security/BnKeystoreService.h generated from
34// java/android/security/IKeystoreService.aidl Note that all generated methods return binder::Status
35// and use last arguments to send actual result to the caller. Private methods don't need to handle
36// binder::Status. Input parameters cannot be null unless annotated with @nullable in .aidl file.
37class KeyStoreService : public android::security::BnKeystoreService,
38 android::IBinder::DeathRecipient {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070039 public:
David Zeuthenc6eb7cd2017-11-27 11:33:55 -050040 explicit KeyStoreService(KeyStore* keyStore)
41 : mKeyStore(keyStore), mOperationMap(this),
42 mConfirmationManager(new ConfirmationManager(this)) {}
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070043 virtual ~KeyStoreService() = default;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070044
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010045 void binderDied(const android::wp<android::IBinder>& who);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070046
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070047 ::android::binder::Status getState(int32_t userId, int32_t* _aidl_return) override;
48 ::android::binder::Status get(const ::android::String16& name, int32_t uid,
49 ::std::vector<uint8_t>* _aidl_return) override;
50 ::android::binder::Status insert(const ::android::String16& name,
51 const ::std::vector<uint8_t>& item, int32_t uid, int32_t flags,
52 int32_t* _aidl_return) override;
53 ::android::binder::Status del(const ::android::String16& name, int32_t uid,
54 int32_t* _aidl_return) override;
55 ::android::binder::Status exist(const ::android::String16& name, int32_t uid,
56 int32_t* _aidl_return) override;
57 ::android::binder::Status list(const ::android::String16& namePrefix, int32_t uid,
58 ::std::vector<::android::String16>* _aidl_return) override;
59 ::android::binder::Status reset(int32_t* _aidl_return) override;
60 ::android::binder::Status onUserPasswordChanged(int32_t userId,
61 const ::android::String16& newPassword,
62 int32_t* _aidl_return) override;
63 ::android::binder::Status lock(int32_t userId, int32_t* _aidl_return) override;
64 ::android::binder::Status unlock(int32_t userId, const ::android::String16& userPassword,
65 int32_t* _aidl_return) override;
66 ::android::binder::Status isEmpty(int32_t userId, int32_t* _aidl_return) override;
67 ::android::binder::Status generate(const ::android::String16& name, int32_t uid,
68 int32_t keyType, int32_t keySize, int32_t flags,
69 const ::android::security::KeystoreArguments& args,
70 int32_t* _aidl_return) override;
71 ::android::binder::Status import_key(const ::android::String16& name,
72 const ::std::vector<uint8_t>& data, int32_t uid,
73 int32_t flags, int32_t* _aidl_return) override;
74 ::android::binder::Status sign(const ::android::String16& name,
75 const ::std::vector<uint8_t>& data,
76 ::std::vector<uint8_t>* _aidl_return) override;
77 ::android::binder::Status verify(const ::android::String16& name,
78 const ::std::vector<uint8_t>& data,
79 const ::std::vector<uint8_t>& signature,
80 int32_t* _aidl_return) override;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070081 /*
82 * TODO: The abstraction between things stored in hardware and regular blobs
83 * of data stored on the filesystem should be moved down to keystore itself.
84 * Unfortunately the Java code that calls this has naming conventions that it
85 * knows about. Ideally keystore shouldn't be used to store random blobs of
86 * data.
87 *
88 * Until that happens, it's necessary to have a separate "get_pubkey" and
89 * "del_key" since the Java code doesn't really communicate what it's
90 * intentions are.
91 */
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070092 ::android::binder::Status get_pubkey(const ::android::String16& name,
93 ::std::vector<uint8_t>* _aidl_return) override;
94 ::android::binder::Status grant(const ::android::String16& name, int32_t granteeUid,
95 ::android::String16* _aidl_return) override;
96 ::android::binder::Status ungrant(const ::android::String16& name, int32_t granteeUid,
97 int32_t* _aidl_return) override;
98 ::android::binder::Status getmtime(const ::android::String16& name, int32_t uid,
99 int64_t* _aidl_return) override;
100 ::android::binder::Status duplicate(const ::android::String16& srcKey, int32_t srcUid,
101 const ::android::String16& destKey, int32_t destUid,
102 int32_t* _aidl_return) override;
103 ::android::binder::Status is_hardware_backed(const ::android::String16& string,
104 int32_t* _aidl_return) override;
105 ::android::binder::Status clear_uid(int64_t uid, int32_t* _aidl_return) override;
Janis Danisevskisc1460142017-12-18 16:48:46 -0800106 ::android::binder::Status addRngEntropy(const ::std::vector<uint8_t>& data, int32_t flags,
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700107 int32_t* _aidl_return) override;
108 ::android::binder::Status
109 generateKey(const ::android::String16& alias,
110 const ::android::security::keymaster::KeymasterArguments& arguments,
111 const ::std::vector<uint8_t>& entropy, int32_t uid, int32_t flags,
112 ::android::security::keymaster::KeyCharacteristics* characteristics,
113 int32_t* _aidl_return) override;
114 ::android::binder::Status
115 getKeyCharacteristics(const ::android::String16& alias,
116 const ::android::security::keymaster::KeymasterBlob& clientId,
117 const ::android::security::keymaster::KeymasterBlob& appId, int32_t uid,
118 ::android::security::keymaster::KeyCharacteristics* characteristics,
119 int32_t* _aidl_return) override;
120 ::android::binder::Status
121 importKey(const ::android::String16& alias,
122 const ::android::security::keymaster::KeymasterArguments& arguments, int32_t format,
123 const ::std::vector<uint8_t>& keyData, int32_t uid, int32_t flags,
124 ::android::security::keymaster::KeyCharacteristics* characteristics,
125 int32_t* _aidl_return) override;
126 ::android::binder::Status
127 exportKey(const ::android::String16& alias, int32_t format,
128 const ::android::security::keymaster::KeymasterBlob& clientId,
129 const ::android::security::keymaster::KeymasterBlob& appId, int32_t uid,
130 ::android::security::keymaster::ExportResult* _aidl_return) override;
131 ::android::binder::Status
132 begin(const ::android::sp<::android::IBinder>& appToken, const ::android::String16& alias,
133 int32_t purpose, bool pruneable,
134 const ::android::security::keymaster::KeymasterArguments& params,
135 const ::std::vector<uint8_t>& entropy, int32_t uid,
136 ::android::security::keymaster::OperationResult* _aidl_return) override;
137 ::android::binder::Status
138 update(const ::android::sp<::android::IBinder>& token,
139 const ::android::security::keymaster::KeymasterArguments& params,
140 const ::std::vector<uint8_t>& input,
141 ::android::security::keymaster::OperationResult* _aidl_return) override;
142 ::android::binder::Status
143 finish(const ::android::sp<::android::IBinder>& token,
144 const ::android::security::keymaster::KeymasterArguments& params,
145 const ::std::vector<uint8_t>& signature, const ::std::vector<uint8_t>& entropy,
146 ::android::security::keymaster::OperationResult* _aidl_return) override;
147 ::android::binder::Status abort(const ::android::sp<::android::IBinder>& handle,
148 int32_t* _aidl_return) override;
149 ::android::binder::Status isOperationAuthorized(const ::android::sp<::android::IBinder>& token,
150 bool* _aidl_return) override;
151 ::android::binder::Status addAuthToken(const ::std::vector<uint8_t>& authToken,
152 int32_t* _aidl_return) override;
153 ::android::binder::Status onUserAdded(int32_t userId, int32_t parentId,
154 int32_t* _aidl_return) override;
155 ::android::binder::Status onUserRemoved(int32_t userId, int32_t* _aidl_return) override;
156 ::android::binder::Status
157 attestKey(const ::android::String16& alias,
158 const ::android::security::keymaster::KeymasterArguments& params,
159 ::android::security::keymaster::KeymasterCertificateChain* chain,
160 int32_t* _aidl_return) override;
161 ::android::binder::Status
162 attestDeviceIds(const ::android::security::keymaster::KeymasterArguments& params,
163 ::android::security::keymaster::KeymasterCertificateChain* chain,
164 int32_t* _aidl_return) override;
165 ::android::binder::Status onDeviceOffBody(int32_t* _aidl_return) override;
David Zeuthenc6eb7cd2017-11-27 11:33:55 -0500166
Janis Danisevskiscb9267d2017-12-19 16:27:52 -0800167 ::android::binder::Status importWrappedKey(
168 const ::android::String16& wrappedKeyAlias, const ::std::vector<uint8_t>& wrappedKey,
169 const ::android::String16& wrappingKeyAlias, const ::std::vector<uint8_t>& maskingKey,
170 const ::android::security::keymaster::KeymasterArguments& params, int64_t rootSid,
171 int64_t fingerprintSid, ::android::security::keymaster::KeyCharacteristics* characteristics,
172 int32_t* _aidl_return) override;
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400173
David Zeuthenc6eb7cd2017-11-27 11:33:55 -0500174 ::android::binder::Status presentConfirmationPrompt(
175 const ::android::sp<::android::IBinder>& listener, const ::android::String16& promptText,
176 const ::std::vector<uint8_t>& extraData, const ::android::String16& locale,
177 int32_t uiOptionsAsFlags, int32_t* _aidl_return) override;
178 ::android::binder::Status
179 cancelConfirmationPrompt(const ::android::sp<::android::IBinder>& listener,
180 int32_t* _aidl_return) override;
181
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700182 private:
183 static const int32_t UID_SELF = -1;
184
185 /**
186 * Prune the oldest pruneable operation.
187 */
188 bool pruneOperation();
189
190 /**
191 * Get the effective target uid for a binder operation that takes an
192 * optional uid as the target.
193 */
194 uid_t getEffectiveUid(int32_t targetUid);
195
196 /**
197 * Check if the caller of the current binder method has the required
198 * permission and if acting on other uids the grants to do so.
199 */
200 bool checkBinderPermission(perm_t permission, int32_t targetUid = UID_SELF);
201
202 /**
203 * Check if the caller of the current binder method has the required
204 * permission and the target uid is the caller or the caller is system.
205 */
206 bool checkBinderPermissionSelfOrSystem(perm_t permission, int32_t targetUid);
207
208 /**
209 * Check if the caller of the current binder method has the required
210 * permission or the target of the operation is the caller's uid. This is
211 * for operation where the permission is only for cross-uid activity and all
212 * uids are allowed to act on their own (ie: clearing all entries for a
213 * given uid).
214 */
215 bool checkBinderPermissionOrSelfTarget(perm_t permission, int32_t targetUid);
216
217 /**
218 * Helper method to check that the caller has the required permission as
219 * well as the keystore is in the unlocked state if checkUnlocked is true.
220 *
221 * Returns NO_ERROR on success, PERMISSION_DENIED on a permission error and
222 * otherwise the state of keystore when not unlocked and checkUnlocked is
223 * true.
224 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100225 KeyStoreServiceReturnCode checkBinderPermissionAndKeystoreState(perm_t permission,
226 int32_t targetUid = -1,
227 bool checkUnlocked = true);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700228
229 bool isKeystoreUnlocked(State state);
230
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700231 /**
232 * Check that all keymaster_key_param_t's provided by the application are
233 * allowed. Any parameter that keystore adds itself should be disallowed here.
234 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100235 bool checkAllowedOperationParams(const hidl_vec<KeyParameter>& params);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700236
Shawn Willdenc67a8aa2017-12-03 17:51:29 -0700237 ErrorCode getOperationCharacteristics(const hidl_vec<uint8_t>& key, sp<Keymaster>* dev,
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100238 const AuthorizationSet& params, KeyCharacteristics* out);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700239
240 /**
241 * Get the auth token for this operation from the auth token table.
242 *
Shawn Willden0329a822017-12-04 13:55:14 -0700243 * Returns NO_ERROR if the auth token was found or none was required. If not needed, the
244 * token will be empty (which keymaster interprets as no auth token).
245 * OP_AUTH_NEEDED if it is a per op authorization, no authorization token exists for
246 * that operation and failOnTokenMissing is false.
247 * KM_ERROR_KEY_USER_NOT_AUTHENTICATED if there is no valid auth token for the operation
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700248 */
Shawn Willden0329a822017-12-04 13:55:14 -0700249 std::pair<KeyStoreServiceReturnCode, HardwareAuthToken>
250 getAuthToken(const KeyCharacteristics& characteristics, uint64_t handle, KeyPurpose purpose,
251 bool failOnTokenMissing = true);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700252
253 /**
Shawn Willden0329a822017-12-04 13:55:14 -0700254 * Get the auth token for the operation if the operation requires authorization. Uses the cached
255 * result in the OperationMap if available otherwise gets the token from the AuthTokenTable and
256 * caches the result.
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700257 *
Shawn Willden0329a822017-12-04 13:55:14 -0700258 * Returns NO_ERROR if the auth token was found or not needed. If not needed, the token will
259 * be empty (which keymaster interprets as no auth token).
260 * KM_ERROR_KEY_USER_NOT_AUTHENTICATED if the operation is not authenticated.
261 * KM_ERROR_INVALID_OPERATION_HANDLE if token is not a valid operation token.
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700262 */
Shawn Willden0329a822017-12-04 13:55:14 -0700263 std::pair<KeyStoreServiceReturnCode, const HardwareAuthToken&>
264 getOperationAuthTokenIfNeeded(const sp<android::IBinder>& token);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700265
266 /**
267 * Translate a result value to a legacy return value. All keystore errors are
268 * preserved and keymaster errors become SYSTEM_ERRORs
269 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100270 KeyStoreServiceReturnCode translateResultToLegacyResult(int32_t result);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700271
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100272 void addLegacyBeginParams(const android::String16& name, AuthorizationSet* params);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700273
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100274 KeyStoreServiceReturnCode doLegacySignVerify(const android::String16& name,
275 const hidl_vec<uint8_t>& data,
276 hidl_vec<uint8_t>* out,
277 const hidl_vec<uint8_t>& signature,
278 KeyPurpose purpose);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700279
Shawn Willden98c59162016-03-20 09:10:18 -0600280 /**
281 * Upgrade a key blob under alias "name", returning the new blob in "blob". If "blob"
282 * previously contained data, it will be overwritten.
283 *
284 * Returns ::NO_ERROR if the key was upgraded successfully.
285 * KM_ERROR_VERSION_MISMATCH if called on a key whose patch level is greater than or
286 * equal to the current system patch level.
287 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100288 KeyStoreServiceReturnCode upgradeKeyBlob(const android::String16& name, uid_t targetUid,
289 const AuthorizationSet& params, Blob* blob);
Shawn Willden98c59162016-03-20 09:10:18 -0600290
David Zeuthenc6eb7cd2017-11-27 11:33:55 -0500291 /**
292 * Adds a Confirmation Token to the key parameters if needed.
293 */
294 void appendConfirmationTokenIfNeeded(const KeyCharacteristics& keyCharacteristics,
295 std::vector<KeyParameter>* params);
296
Shawn Willdenc67a8aa2017-12-03 17:51:29 -0700297 KeyStore* mKeyStore;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700298 OperationMap mOperationMap;
David Zeuthenc6eb7cd2017-11-27 11:33:55 -0500299 android::sp<ConfirmationManager> mConfirmationManager;
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100300 keystore::AuthTokenTable mAuthTokenTable;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700301 KeystoreKeymasterEnforcement enforcement_policy;
302};
303
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100304}; // namespace keystore
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700305
306#endif // KEYSTORE_KEYSTORE_SERVICE_H_