blob: 0a1b8f1829b3e813645efd3f343b47d70eb00939 [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"
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070023
Shawn Willdenfa5702f2017-12-03 15:14:58 -070024#include "KeyStore.h"
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070025#include "keystore_keymaster_enforcement.h"
26#include "operation.h"
27#include "permissions.h"
28
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010029namespace keystore {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070030
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070031// Class provides implementation for generated BnKeystoreService.h based on
32// gen/aidl/android/security/BnKeystoreService.h generated from
33// java/android/security/IKeystoreService.aidl Note that all generated methods return binder::Status
34// and use last arguments to send actual result to the caller. Private methods don't need to handle
35// binder::Status. Input parameters cannot be null unless annotated with @nullable in .aidl file.
36class KeyStoreService : public android::security::BnKeystoreService,
37 android::IBinder::DeathRecipient {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010038 typedef ::android::sp<::android::hardware::keymaster::V3_0::IKeymasterDevice> km_device_t;
39
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070040 public:
Chih-Hung Hsiehd7791be2016-07-12 11:58:02 -070041 explicit KeyStoreService(KeyStore* keyStore) : mKeyStore(keyStore), mOperationMap(this) {}
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070042
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010043 void binderDied(const android::wp<android::IBinder>& who);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070044
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070045 ::android::binder::Status getState(int32_t userId, int32_t* _aidl_return) override;
46 ::android::binder::Status get(const ::android::String16& name, int32_t uid,
47 ::std::vector<uint8_t>* _aidl_return) override;
48 ::android::binder::Status insert(const ::android::String16& name,
49 const ::std::vector<uint8_t>& item, int32_t uid, int32_t flags,
50 int32_t* _aidl_return) override;
51 ::android::binder::Status del(const ::android::String16& name, int32_t uid,
52 int32_t* _aidl_return) override;
53 ::android::binder::Status exist(const ::android::String16& name, int32_t uid,
54 int32_t* _aidl_return) override;
55 ::android::binder::Status list(const ::android::String16& namePrefix, int32_t uid,
56 ::std::vector<::android::String16>* _aidl_return) override;
57 ::android::binder::Status reset(int32_t* _aidl_return) override;
58 ::android::binder::Status onUserPasswordChanged(int32_t userId,
59 const ::android::String16& newPassword,
60 int32_t* _aidl_return) override;
61 ::android::binder::Status lock(int32_t userId, int32_t* _aidl_return) override;
62 ::android::binder::Status unlock(int32_t userId, const ::android::String16& userPassword,
63 int32_t* _aidl_return) override;
64 ::android::binder::Status isEmpty(int32_t userId, int32_t* _aidl_return) override;
65 ::android::binder::Status generate(const ::android::String16& name, int32_t uid,
66 int32_t keyType, int32_t keySize, int32_t flags,
67 const ::android::security::KeystoreArguments& args,
68 int32_t* _aidl_return) override;
69 ::android::binder::Status import_key(const ::android::String16& name,
70 const ::std::vector<uint8_t>& data, int32_t uid,
71 int32_t flags, int32_t* _aidl_return) override;
72 ::android::binder::Status sign(const ::android::String16& name,
73 const ::std::vector<uint8_t>& data,
74 ::std::vector<uint8_t>* _aidl_return) override;
75 ::android::binder::Status verify(const ::android::String16& name,
76 const ::std::vector<uint8_t>& data,
77 const ::std::vector<uint8_t>& signature,
78 int32_t* _aidl_return) override;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070079 /*
80 * TODO: The abstraction between things stored in hardware and regular blobs
81 * of data stored on the filesystem should be moved down to keystore itself.
82 * Unfortunately the Java code that calls this has naming conventions that it
83 * knows about. Ideally keystore shouldn't be used to store random blobs of
84 * data.
85 *
86 * Until that happens, it's necessary to have a separate "get_pubkey" and
87 * "del_key" since the Java code doesn't really communicate what it's
88 * intentions are.
89 */
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070090 ::android::binder::Status get_pubkey(const ::android::String16& name,
91 ::std::vector<uint8_t>* _aidl_return) override;
92 ::android::binder::Status grant(const ::android::String16& name, int32_t granteeUid,
93 ::android::String16* _aidl_return) override;
94 ::android::binder::Status ungrant(const ::android::String16& name, int32_t granteeUid,
95 int32_t* _aidl_return) override;
96 ::android::binder::Status getmtime(const ::android::String16& name, int32_t uid,
97 int64_t* _aidl_return) override;
98 ::android::binder::Status duplicate(const ::android::String16& srcKey, int32_t srcUid,
99 const ::android::String16& destKey, int32_t destUid,
100 int32_t* _aidl_return) override;
101 ::android::binder::Status is_hardware_backed(const ::android::String16& string,
102 int32_t* _aidl_return) override;
103 ::android::binder::Status clear_uid(int64_t uid, int32_t* _aidl_return) override;
104 ::android::binder::Status addRngEntropy(const ::std::vector<uint8_t>& data,
105 int32_t* _aidl_return) override;
106 ::android::binder::Status
107 generateKey(const ::android::String16& alias,
108 const ::android::security::keymaster::KeymasterArguments& arguments,
109 const ::std::vector<uint8_t>& entropy, int32_t uid, int32_t flags,
110 ::android::security::keymaster::KeyCharacteristics* characteristics,
111 int32_t* _aidl_return) override;
112 ::android::binder::Status
113 getKeyCharacteristics(const ::android::String16& alias,
114 const ::android::security::keymaster::KeymasterBlob& clientId,
115 const ::android::security::keymaster::KeymasterBlob& appId, int32_t uid,
116 ::android::security::keymaster::KeyCharacteristics* characteristics,
117 int32_t* _aidl_return) override;
118 ::android::binder::Status
119 importKey(const ::android::String16& alias,
120 const ::android::security::keymaster::KeymasterArguments& arguments, int32_t format,
121 const ::std::vector<uint8_t>& keyData, int32_t uid, int32_t flags,
122 ::android::security::keymaster::KeyCharacteristics* characteristics,
123 int32_t* _aidl_return) override;
124 ::android::binder::Status
125 exportKey(const ::android::String16& alias, int32_t format,
126 const ::android::security::keymaster::KeymasterBlob& clientId,
127 const ::android::security::keymaster::KeymasterBlob& appId, int32_t uid,
128 ::android::security::keymaster::ExportResult* _aidl_return) override;
129 ::android::binder::Status
130 begin(const ::android::sp<::android::IBinder>& appToken, const ::android::String16& alias,
131 int32_t purpose, bool pruneable,
132 const ::android::security::keymaster::KeymasterArguments& params,
133 const ::std::vector<uint8_t>& entropy, int32_t uid,
134 ::android::security::keymaster::OperationResult* _aidl_return) override;
135 ::android::binder::Status
136 update(const ::android::sp<::android::IBinder>& token,
137 const ::android::security::keymaster::KeymasterArguments& params,
138 const ::std::vector<uint8_t>& input,
139 ::android::security::keymaster::OperationResult* _aidl_return) override;
140 ::android::binder::Status
141 finish(const ::android::sp<::android::IBinder>& token,
142 const ::android::security::keymaster::KeymasterArguments& params,
143 const ::std::vector<uint8_t>& signature, const ::std::vector<uint8_t>& entropy,
144 ::android::security::keymaster::OperationResult* _aidl_return) override;
145 ::android::binder::Status abort(const ::android::sp<::android::IBinder>& handle,
146 int32_t* _aidl_return) override;
147 ::android::binder::Status isOperationAuthorized(const ::android::sp<::android::IBinder>& token,
148 bool* _aidl_return) override;
149 ::android::binder::Status addAuthToken(const ::std::vector<uint8_t>& authToken,
150 int32_t* _aidl_return) override;
151 ::android::binder::Status onUserAdded(int32_t userId, int32_t parentId,
152 int32_t* _aidl_return) override;
153 ::android::binder::Status onUserRemoved(int32_t userId, int32_t* _aidl_return) override;
154 ::android::binder::Status
155 attestKey(const ::android::String16& alias,
156 const ::android::security::keymaster::KeymasterArguments& params,
157 ::android::security::keymaster::KeymasterCertificateChain* chain,
158 int32_t* _aidl_return) override;
159 ::android::binder::Status
160 attestDeviceIds(const ::android::security::keymaster::KeymasterArguments& params,
161 ::android::security::keymaster::KeymasterCertificateChain* chain,
162 int32_t* _aidl_return) override;
163 ::android::binder::Status onDeviceOffBody(int32_t* _aidl_return) override;
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400164
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700165 private:
166 static const int32_t UID_SELF = -1;
167
168 /**
169 * Prune the oldest pruneable operation.
170 */
171 bool pruneOperation();
172
173 /**
174 * Get the effective target uid for a binder operation that takes an
175 * optional uid as the target.
176 */
177 uid_t getEffectiveUid(int32_t targetUid);
178
179 /**
180 * Check if the caller of the current binder method has the required
181 * permission and if acting on other uids the grants to do so.
182 */
183 bool checkBinderPermission(perm_t permission, int32_t targetUid = UID_SELF);
184
185 /**
186 * Check if the caller of the current binder method has the required
187 * permission and the target uid is the caller or the caller is system.
188 */
189 bool checkBinderPermissionSelfOrSystem(perm_t permission, int32_t targetUid);
190
191 /**
192 * Check if the caller of the current binder method has the required
193 * permission or the target of the operation is the caller's uid. This is
194 * for operation where the permission is only for cross-uid activity and all
195 * uids are allowed to act on their own (ie: clearing all entries for a
196 * given uid).
197 */
198 bool checkBinderPermissionOrSelfTarget(perm_t permission, int32_t targetUid);
199
200 /**
201 * Helper method to check that the caller has the required permission as
202 * well as the keystore is in the unlocked state if checkUnlocked is true.
203 *
204 * Returns NO_ERROR on success, PERMISSION_DENIED on a permission error and
205 * otherwise the state of keystore when not unlocked and checkUnlocked is
206 * true.
207 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100208 KeyStoreServiceReturnCode checkBinderPermissionAndKeystoreState(perm_t permission,
209 int32_t targetUid = -1,
210 bool checkUnlocked = true);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700211
212 bool isKeystoreUnlocked(State state);
213
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700214 /**
215 * Check that all keymaster_key_param_t's provided by the application are
216 * allowed. Any parameter that keystore adds itself should be disallowed here.
217 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100218 bool checkAllowedOperationParams(const hidl_vec<KeyParameter>& params);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700219
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100220 ErrorCode getOperationCharacteristics(const hidl_vec<uint8_t>& key, km_device_t* dev,
221 const AuthorizationSet& params, KeyCharacteristics* out);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700222
223 /**
224 * Get the auth token for this operation from the auth token table.
225 *
226 * Returns ::NO_ERROR if the auth token was set or none was required.
227 * ::OP_AUTH_NEEDED if it is a per op authorization, no
228 * authorization token exists for that operation and
229 * failOnTokenMissing is false.
230 * KM_ERROR_KEY_USER_NOT_AUTHENTICATED if there is no valid auth
231 * token for the operation
232 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100233 KeyStoreServiceReturnCode getAuthToken(const KeyCharacteristics& characteristics,
234 uint64_t handle, KeyPurpose purpose,
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000235 const HardwareAuthToken** authToken,
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100236 bool failOnTokenMissing = true);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700237
238 /**
239 * Add the auth token for the operation to the param list if the operation
240 * requires authorization. Uses the cached result in the OperationMap if available
241 * otherwise gets the token from the AuthTokenTable and caches the result.
242 *
243 * Returns ::NO_ERROR if the auth token was added or not needed.
244 * KM_ERROR_KEY_USER_NOT_AUTHENTICATED if the operation is not
245 * authenticated.
246 * KM_ERROR_INVALID_OPERATION_HANDLE if token is not a valid
247 * operation token.
248 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100249 KeyStoreServiceReturnCode addOperationAuthTokenIfNeeded(const sp<android::IBinder>& token,
250 AuthorizationSet* params);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700251
252 /**
253 * Translate a result value to a legacy return value. All keystore errors are
254 * preserved and keymaster errors become SYSTEM_ERRORs
255 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100256 KeyStoreServiceReturnCode translateResultToLegacyResult(int32_t result);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700257
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100258 void addLegacyBeginParams(const android::String16& name, AuthorizationSet* params);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700259
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100260 KeyStoreServiceReturnCode doLegacySignVerify(const android::String16& name,
261 const hidl_vec<uint8_t>& data,
262 hidl_vec<uint8_t>* out,
263 const hidl_vec<uint8_t>& signature,
264 KeyPurpose purpose);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700265
Shawn Willden98c59162016-03-20 09:10:18 -0600266 /**
267 * Upgrade a key blob under alias "name", returning the new blob in "blob". If "blob"
268 * previously contained data, it will be overwritten.
269 *
270 * Returns ::NO_ERROR if the key was upgraded successfully.
271 * KM_ERROR_VERSION_MISMATCH if called on a key whose patch level is greater than or
272 * equal to the current system patch level.
273 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100274 KeyStoreServiceReturnCode upgradeKeyBlob(const android::String16& name, uid_t targetUid,
275 const AuthorizationSet& params, Blob* blob);
Shawn Willden98c59162016-03-20 09:10:18 -0600276
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700277 ::KeyStore* mKeyStore;
278 OperationMap mOperationMap;
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100279 keystore::AuthTokenTable mAuthTokenTable;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700280 KeystoreKeymasterEnforcement enforcement_policy;
281};
282
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100283}; // namespace keystore
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700284
285#endif // KEYSTORE_KEYSTORE_SERVICE_H_