blob: 29369d0571fc0d637b4c50dcfe0f84b476125fd9 [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 Danisevskisff3d7f42018-10-08 07:15:09 -070030#include <keystore/ExportResult.h>
31#include <keystore/KeyCharacteristics.h>
32#include <keystore/KeymasterArguments.h>
33#include <keystore/KeymasterBlob.h>
34#include <keystore/KeymasterCertificateChain.h>
35#include <keystore/OperationResult.h>
36#include <keystore/keystore_return_types.h>
37
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010038namespace keystore {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070039
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070040// Class provides implementation for generated BnKeystoreService.h based on
41// gen/aidl/android/security/BnKeystoreService.h generated from
42// java/android/security/IKeystoreService.aidl Note that all generated methods return binder::Status
43// and use last arguments to send actual result to the caller. Private methods don't need to handle
44// binder::Status. Input parameters cannot be null unless annotated with @nullable in .aidl file.
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070045class KeyStoreService : public android::security::BnKeystoreService {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070046 public:
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070047 explicit KeyStoreService(sp<KeyStore> keyStore) : mKeyStore(keyStore) {}
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070048 virtual ~KeyStoreService() = default;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070049
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010050 void binderDied(const android::wp<android::IBinder>& who);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070051
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070052 ::android::binder::Status getState(int32_t userId, int32_t* _aidl_return) override;
53 ::android::binder::Status get(const ::android::String16& name, int32_t uid,
54 ::std::vector<uint8_t>* _aidl_return) override;
55 ::android::binder::Status insert(const ::android::String16& name,
56 const ::std::vector<uint8_t>& item, int32_t uid, int32_t flags,
57 int32_t* _aidl_return) override;
58 ::android::binder::Status del(const ::android::String16& name, int32_t uid,
59 int32_t* _aidl_return) override;
60 ::android::binder::Status exist(const ::android::String16& name, int32_t uid,
61 int32_t* _aidl_return) override;
62 ::android::binder::Status list(const ::android::String16& namePrefix, int32_t uid,
63 ::std::vector<::android::String16>* _aidl_return) override;
64 ::android::binder::Status reset(int32_t* _aidl_return) override;
65 ::android::binder::Status onUserPasswordChanged(int32_t userId,
66 const ::android::String16& newPassword,
67 int32_t* _aidl_return) override;
68 ::android::binder::Status lock(int32_t userId, int32_t* _aidl_return) override;
69 ::android::binder::Status unlock(int32_t userId, const ::android::String16& userPassword,
70 int32_t* _aidl_return) override;
71 ::android::binder::Status isEmpty(int32_t userId, int32_t* _aidl_return) override;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070072 ::android::binder::Status grant(const ::android::String16& name, int32_t granteeUid,
73 ::android::String16* _aidl_return) override;
74 ::android::binder::Status ungrant(const ::android::String16& name, int32_t granteeUid,
75 int32_t* _aidl_return) override;
76 ::android::binder::Status getmtime(const ::android::String16& name, int32_t uid,
77 int64_t* _aidl_return) override;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070078 ::android::binder::Status is_hardware_backed(const ::android::String16& string,
79 int32_t* _aidl_return) override;
80 ::android::binder::Status clear_uid(int64_t uid, int32_t* _aidl_return) override;
Janis Danisevskisc1460142017-12-18 16:48:46 -080081 ::android::binder::Status addRngEntropy(const ::std::vector<uint8_t>& data, int32_t flags,
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070082 int32_t* _aidl_return) override;
83 ::android::binder::Status
84 generateKey(const ::android::String16& alias,
85 const ::android::security::keymaster::KeymasterArguments& arguments,
86 const ::std::vector<uint8_t>& entropy, int32_t uid, int32_t flags,
87 ::android::security::keymaster::KeyCharacteristics* characteristics,
88 int32_t* _aidl_return) override;
89 ::android::binder::Status
90 getKeyCharacteristics(const ::android::String16& alias,
91 const ::android::security::keymaster::KeymasterBlob& clientId,
92 const ::android::security::keymaster::KeymasterBlob& appId, int32_t uid,
93 ::android::security::keymaster::KeyCharacteristics* characteristics,
94 int32_t* _aidl_return) override;
95 ::android::binder::Status
96 importKey(const ::android::String16& alias,
97 const ::android::security::keymaster::KeymasterArguments& arguments, int32_t format,
98 const ::std::vector<uint8_t>& keyData, int32_t uid, int32_t flags,
99 ::android::security::keymaster::KeyCharacteristics* characteristics,
100 int32_t* _aidl_return) override;
101 ::android::binder::Status
102 exportKey(const ::android::String16& alias, int32_t format,
103 const ::android::security::keymaster::KeymasterBlob& clientId,
104 const ::android::security::keymaster::KeymasterBlob& appId, int32_t uid,
105 ::android::security::keymaster::ExportResult* _aidl_return) override;
106 ::android::binder::Status
107 begin(const ::android::sp<::android::IBinder>& appToken, const ::android::String16& alias,
108 int32_t purpose, bool pruneable,
109 const ::android::security::keymaster::KeymasterArguments& params,
110 const ::std::vector<uint8_t>& entropy, int32_t uid,
111 ::android::security::keymaster::OperationResult* _aidl_return) override;
112 ::android::binder::Status
113 update(const ::android::sp<::android::IBinder>& token,
114 const ::android::security::keymaster::KeymasterArguments& params,
115 const ::std::vector<uint8_t>& input,
116 ::android::security::keymaster::OperationResult* _aidl_return) override;
117 ::android::binder::Status
118 finish(const ::android::sp<::android::IBinder>& token,
119 const ::android::security::keymaster::KeymasterArguments& params,
120 const ::std::vector<uint8_t>& signature, const ::std::vector<uint8_t>& entropy,
121 ::android::security::keymaster::OperationResult* _aidl_return) override;
122 ::android::binder::Status abort(const ::android::sp<::android::IBinder>& handle,
123 int32_t* _aidl_return) override;
Brian Youngccb492d2018-02-22 23:36:01 +0000124 ::android::binder::Status addAuthToken(const ::std::vector<uint8_t>& authToken,
Brian Young1b759292018-01-29 23:57:29 +0000125 int32_t* _aidl_return) override;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700126 ::android::binder::Status onUserAdded(int32_t userId, int32_t parentId,
127 int32_t* _aidl_return) override;
128 ::android::binder::Status onUserRemoved(int32_t userId, int32_t* _aidl_return) override;
129 ::android::binder::Status
130 attestKey(const ::android::String16& alias,
131 const ::android::security::keymaster::KeymasterArguments& params,
132 ::android::security::keymaster::KeymasterCertificateChain* chain,
133 int32_t* _aidl_return) override;
134 ::android::binder::Status
135 attestDeviceIds(const ::android::security::keymaster::KeymasterArguments& params,
136 ::android::security::keymaster::KeymasterCertificateChain* chain,
137 int32_t* _aidl_return) override;
138 ::android::binder::Status onDeviceOffBody(int32_t* _aidl_return) override;
David Zeuthenc6eb7cd2017-11-27 11:33:55 -0500139
Janis Danisevskiscb9267d2017-12-19 16:27:52 -0800140 ::android::binder::Status importWrappedKey(
141 const ::android::String16& wrappedKeyAlias, const ::std::vector<uint8_t>& wrappedKey,
142 const ::android::String16& wrappingKeyAlias, const ::std::vector<uint8_t>& maskingKey,
143 const ::android::security::keymaster::KeymasterArguments& params, int64_t rootSid,
144 int64_t fingerprintSid, ::android::security::keymaster::KeyCharacteristics* characteristics,
145 int32_t* _aidl_return) override;
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400146
David Zeuthenc6eb7cd2017-11-27 11:33:55 -0500147 ::android::binder::Status presentConfirmationPrompt(
148 const ::android::sp<::android::IBinder>& listener, const ::android::String16& promptText,
149 const ::std::vector<uint8_t>& extraData, const ::android::String16& locale,
150 int32_t uiOptionsAsFlags, int32_t* _aidl_return) override;
151 ::android::binder::Status
152 cancelConfirmationPrompt(const ::android::sp<::android::IBinder>& listener,
153 int32_t* _aidl_return) override;
David Zeuthen1a492312018-02-26 11:00:30 -0500154 ::android::binder::Status isConfirmationPromptSupported(bool* _aidl_return) override;
David Zeuthenc6eb7cd2017-11-27 11:33:55 -0500155
Brian Young9371e952018-02-23 18:03:14 +0000156 ::android::binder::Status onKeyguardVisibilityChanged(bool isShowing, int32_t userId,
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700157 int32_t* _aidl_return) override;
Brian Young9371e952018-02-23 18:03:14 +0000158
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700159 private:
160 static const int32_t UID_SELF = -1;
161
162 /**
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700163 * Get the effective target uid for a binder operation that takes an
164 * optional uid as the target.
165 */
166 uid_t getEffectiveUid(int32_t targetUid);
167
168 /**
169 * Check if the caller of the current binder method has the required
170 * permission and if acting on other uids the grants to do so.
171 */
172 bool checkBinderPermission(perm_t permission, int32_t targetUid = UID_SELF);
173
174 /**
175 * Check if the caller of the current binder method has the required
176 * permission and the target uid is the caller or the caller is system.
177 */
178 bool checkBinderPermissionSelfOrSystem(perm_t permission, int32_t targetUid);
179
180 /**
181 * Check if the caller of the current binder method has the required
182 * permission or the target of the operation is the caller's uid. This is
183 * for operation where the permission is only for cross-uid activity and all
184 * uids are allowed to act on their own (ie: clearing all entries for a
185 * given uid).
186 */
187 bool checkBinderPermissionOrSelfTarget(perm_t permission, int32_t targetUid);
188
189 /**
190 * Helper method to check that the caller has the required permission as
191 * well as the keystore is in the unlocked state if checkUnlocked is true.
192 *
193 * Returns NO_ERROR on success, PERMISSION_DENIED on a permission error and
194 * otherwise the state of keystore when not unlocked and checkUnlocked is
195 * true.
196 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100197 KeyStoreServiceReturnCode checkBinderPermissionAndKeystoreState(perm_t permission,
198 int32_t targetUid = -1,
199 bool checkUnlocked = true);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700200
201 bool isKeystoreUnlocked(State state);
202
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700203 /**
204 * Check that all keymaster_key_param_t's provided by the application are
205 * allowed. Any parameter that keystore adds itself should be disallowed here.
206 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100207 bool checkAllowedOperationParams(const hidl_vec<KeyParameter>& params);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700208
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100209 void addLegacyBeginParams(const android::String16& name, AuthorizationSet* params);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700210
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100211 KeyStoreServiceReturnCode doLegacySignVerify(const android::String16& name,
212 const hidl_vec<uint8_t>& data,
213 hidl_vec<uint8_t>* out,
214 const hidl_vec<uint8_t>& signature,
215 KeyPurpose purpose);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700216
Shawn Willden98c59162016-03-20 09:10:18 -0600217 /**
David Zeuthenc6eb7cd2017-11-27 11:33:55 -0500218 * Adds a Confirmation Token to the key parameters if needed.
219 */
220 void appendConfirmationTokenIfNeeded(const KeyCharacteristics& keyCharacteristics,
221 std::vector<KeyParameter>* params);
222
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700223 sp<KeyStore> mKeyStore;
224
225 std::mutex operationDeviceMapMutex_;
226 std::map<sp<IBinder>, std::shared_ptr<KeymasterWorker>> operationDeviceMap_;
227
228 void addOperationDevice(sp<IBinder> token, std::shared_ptr<KeymasterWorker> dev) {
229 std::lock_guard<std::mutex> lock(operationDeviceMapMutex_);
230 operationDeviceMap_.emplace(std::move(token), std::move(dev));
231 }
232 std::shared_ptr<KeymasterWorker> getOperationDevice(const sp<IBinder>& token) {
233 std::lock_guard<std::mutex> lock(operationDeviceMapMutex_);
234 auto it = operationDeviceMap_.find(token);
235 if (it != operationDeviceMap_.end()) {
236 return it->second;
237 }
238 return {};
239 }
240 void removeOperationDevice(const sp<IBinder>& token) {
241 std::lock_guard<std::mutex> lock(operationDeviceMapMutex_);
242 operationDeviceMap_.erase(token);
243 }
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700244};
245
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100246}; // namespace keystore
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700247
248#endif // KEYSTORE_KEYSTORE_SERVICE_H_