blob: 601e63dc35b333a69351ea04f4d3a2f14da5ff87 [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
Rob Barnesbb6cabd2018-10-04 17:10:37 -060020#include <android/security/keystore/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 Danisevskisb50236a2019-03-25 10:26:30 -070038#include <mutex>
39
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010040namespace keystore {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070041
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070042// Class provides implementation for generated BnKeystoreService.h based on
43// gen/aidl/android/security/BnKeystoreService.h generated from
44// java/android/security/IKeystoreService.aidl Note that all generated methods return binder::Status
45// and use last arguments to send actual result to the caller. Private methods don't need to handle
46// binder::Status. Input parameters cannot be null unless annotated with @nullable in .aidl file.
Rob Barnesbb6cabd2018-10-04 17:10:37 -060047class KeyStoreService : public android::security::keystore::BnKeystoreService {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070048 public:
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070049 explicit KeyStoreService(sp<KeyStore> keyStore) : mKeyStore(keyStore) {}
Shawn Willdenc67a8aa2017-12-03 17:51:29 -070050 virtual ~KeyStoreService() = default;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070051
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010052 void binderDied(const android::wp<android::IBinder>& who);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070053
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070054 ::android::binder::Status getState(int32_t userId, int32_t* _aidl_return) override;
55 ::android::binder::Status get(const ::android::String16& name, int32_t uid,
56 ::std::vector<uint8_t>* _aidl_return) override;
57 ::android::binder::Status insert(const ::android::String16& name,
58 const ::std::vector<uint8_t>& item, int32_t uid, int32_t flags,
59 int32_t* _aidl_return) override;
60 ::android::binder::Status del(const ::android::String16& name, int32_t uid,
61 int32_t* _aidl_return) override;
62 ::android::binder::Status exist(const ::android::String16& name, int32_t uid,
63 int32_t* _aidl_return) override;
64 ::android::binder::Status list(const ::android::String16& namePrefix, int32_t uid,
65 ::std::vector<::android::String16>* _aidl_return) override;
Rob Barnes5d59e632018-12-07 16:09:02 -070066 ::android::binder::Status listUidsOfAuthBoundKeys(std::vector<::std::string>* uids,
Rob Barneseb7f79b2018-11-08 15:44:10 -070067 int32_t* _aidl_return) override;
68
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070069 ::android::binder::Status onUserPasswordChanged(int32_t userId,
70 const ::android::String16& newPassword,
71 int32_t* _aidl_return) override;
72 ::android::binder::Status lock(int32_t userId, int32_t* _aidl_return) override;
73 ::android::binder::Status unlock(int32_t userId, const ::android::String16& userPassword,
74 int32_t* _aidl_return) override;
75 ::android::binder::Status isEmpty(int32_t userId, int32_t* _aidl_return) override;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070076 ::android::binder::Status grant(const ::android::String16& name, int32_t granteeUid,
77 ::android::String16* _aidl_return) override;
78 ::android::binder::Status ungrant(const ::android::String16& name, int32_t granteeUid,
79 int32_t* _aidl_return) override;
80 ::android::binder::Status getmtime(const ::android::String16& name, int32_t uid,
81 int64_t* _aidl_return) override;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070082 ::android::binder::Status is_hardware_backed(const ::android::String16& string,
83 int32_t* _aidl_return) override;
84 ::android::binder::Status clear_uid(int64_t uid, int32_t* _aidl_return) override;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070085 ::android::binder::Status
Rob Barnesbb6cabd2018-10-04 17:10:37 -060086 addRngEntropy(const ::android::sp<::android::security::keystore::IKeystoreResponseCallback>& cb,
87 const ::std::vector<uint8_t>& data, int32_t flags,
88 int32_t* _aidl_return) override;
89 ::android::binder::Status generateKey(
90 const ::android::sp<::android::security::keystore::IKeystoreKeyCharacteristicsCallback>& cb,
91 const ::android::String16& alias,
92 const ::android::security::keymaster::KeymasterArguments& arguments,
93 const ::std::vector<uint8_t>& entropy, int32_t uid, int32_t flags,
94 int32_t* _aidl_return) override;
95 ::android::binder::Status getKeyCharacteristics(
96 const ::android::sp<::android::security::keystore::IKeystoreKeyCharacteristicsCallback>& cb,
97 const ::android::String16& alias,
98 const ::android::security::keymaster::KeymasterBlob& clientId,
99 const ::android::security::keymaster::KeymasterBlob& appId, int32_t uid,
100 int32_t* _aidl_return) override;
101 ::android::binder::Status importKey(
102 const ::android::sp<::android::security::keystore::IKeystoreKeyCharacteristicsCallback>& cb,
103 const ::android::String16& alias,
104 const ::android::security::keymaster::KeymasterArguments& arguments, int32_t format,
105 const ::std::vector<uint8_t>& keyData, int32_t uid, int32_t flags,
106 int32_t* _aidl_return) override;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700107 ::android::binder::Status
Rob Barnesbb6cabd2018-10-04 17:10:37 -0600108 exportKey(const ::android::sp<::android::security::keystore::IKeystoreExportKeyCallback>& cb,
109 const ::android::String16& alias, int32_t format,
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700110 const ::android::security::keymaster::KeymasterBlob& clientId,
111 const ::android::security::keymaster::KeymasterBlob& appId, int32_t uid,
Rob Barnesbb6cabd2018-10-04 17:10:37 -0600112 int32_t* _aidl_return) override;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700113 ::android::binder::Status
Rob Barnesbb6cabd2018-10-04 17:10:37 -0600114 begin(const ::android::sp<::android::security::keystore::IKeystoreOperationResultCallback>& cb,
115 const ::android::sp<::android::IBinder>& appToken, const ::android::String16& alias,
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700116 int32_t purpose, bool pruneable,
117 const ::android::security::keymaster::KeymasterArguments& params,
Rob Barnesbb6cabd2018-10-04 17:10:37 -0600118 const ::std::vector<uint8_t>& entropy, int32_t uid, int32_t* _aidl_return) override;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700119 ::android::binder::Status
Rob Barnesbb6cabd2018-10-04 17:10:37 -0600120 update(const ::android::sp<::android::security::keystore::IKeystoreOperationResultCallback>& cb,
121 const ::android::sp<::android::IBinder>& token,
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700122 const ::android::security::keymaster::KeymasterArguments& params,
Rob Barnesbb6cabd2018-10-04 17:10:37 -0600123 const ::std::vector<uint8_t>& input, int32_t* _aidl_return) override;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700124 ::android::binder::Status
Rob Barnesbb6cabd2018-10-04 17:10:37 -0600125 finish(const ::android::sp<::android::security::keystore::IKeystoreOperationResultCallback>& cb,
126 const ::android::sp<::android::IBinder>& token,
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700127 const ::android::security::keymaster::KeymasterArguments& params,
128 const ::std::vector<uint8_t>& signature, const ::std::vector<uint8_t>& entropy,
Rob Barnesbb6cabd2018-10-04 17:10:37 -0600129 int32_t* _aidl_return) override;
130 ::android::binder::Status
131 abort(const ::android::sp<::android::security::keystore::IKeystoreResponseCallback>& cb,
132 const ::android::sp<::android::IBinder>& token, int32_t* _aidl_return) override;
Brian Youngccb492d2018-02-22 23:36:01 +0000133 ::android::binder::Status addAuthToken(const ::std::vector<uint8_t>& authToken,
Brian Young1b759292018-01-29 23:57:29 +0000134 int32_t* _aidl_return) override;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700135 ::android::binder::Status onUserAdded(int32_t userId, int32_t parentId,
136 int32_t* _aidl_return) override;
137 ::android::binder::Status onUserRemoved(int32_t userId, int32_t* _aidl_return) override;
Rob Barnesbb6cabd2018-10-04 17:10:37 -0600138 ::android::binder::Status attestKey(
139 const ::android::sp<::android::security::keystore::IKeystoreCertificateChainCallback>& cb,
140 const ::android::String16& alias,
141 const ::android::security::keymaster::KeymasterArguments& params,
142 int32_t* _aidl_return) override;
143 ::android::binder::Status attestDeviceIds(
144 const ::android::sp<::android::security::keystore::IKeystoreCertificateChainCallback>& cb,
145 const ::android::security::keymaster::KeymasterArguments& params,
146 int32_t* _aidl_return) override;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700147 ::android::binder::Status onDeviceOffBody(int32_t* _aidl_return) override;
David Zeuthenc6eb7cd2017-11-27 11:33:55 -0500148
Janis Danisevskiscb9267d2017-12-19 16:27:52 -0800149 ::android::binder::Status importWrappedKey(
Rob Barnesbb6cabd2018-10-04 17:10:37 -0600150 const ::android::sp<::android::security::keystore::IKeystoreKeyCharacteristicsCallback>& cb,
Janis Danisevskiscb9267d2017-12-19 16:27:52 -0800151 const ::android::String16& wrappedKeyAlias, const ::std::vector<uint8_t>& wrappedKey,
152 const ::android::String16& wrappingKeyAlias, const ::std::vector<uint8_t>& maskingKey,
153 const ::android::security::keymaster::KeymasterArguments& params, int64_t rootSid,
Rob Barnesbb6cabd2018-10-04 17:10:37 -0600154 int64_t fingerprintSid, int32_t* _aidl_return) override;
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400155
David Zeuthenc6eb7cd2017-11-27 11:33:55 -0500156 ::android::binder::Status presentConfirmationPrompt(
157 const ::android::sp<::android::IBinder>& listener, const ::android::String16& promptText,
158 const ::std::vector<uint8_t>& extraData, const ::android::String16& locale,
159 int32_t uiOptionsAsFlags, int32_t* _aidl_return) override;
160 ::android::binder::Status
161 cancelConfirmationPrompt(const ::android::sp<::android::IBinder>& listener,
162 int32_t* _aidl_return) override;
David Zeuthen1a492312018-02-26 11:00:30 -0500163 ::android::binder::Status isConfirmationPromptSupported(bool* _aidl_return) override;
David Zeuthenc6eb7cd2017-11-27 11:33:55 -0500164
Brian Young9371e952018-02-23 18:03:14 +0000165 ::android::binder::Status onKeyguardVisibilityChanged(bool isShowing, int32_t userId,
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700166 int32_t* _aidl_return) override;
Brian Young9371e952018-02-23 18:03:14 +0000167
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700168 private:
169 static const int32_t UID_SELF = -1;
170
171 /**
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700172 * Get the effective target uid for a binder operation that takes an
173 * optional uid as the target.
174 */
175 uid_t getEffectiveUid(int32_t targetUid);
176
177 /**
178 * Check if the caller of the current binder method has the required
179 * permission and if acting on other uids the grants to do so.
180 */
181 bool checkBinderPermission(perm_t permission, int32_t targetUid = UID_SELF);
182
183 /**
184 * Check if the caller of the current binder method has the required
185 * permission and the target uid is the caller or the caller is system.
186 */
187 bool checkBinderPermissionSelfOrSystem(perm_t permission, int32_t targetUid);
188
189 /**
190 * Check if the caller of the current binder method has the required
191 * permission or the target of the operation is the caller's uid. This is
192 * for operation where the permission is only for cross-uid activity and all
193 * uids are allowed to act on their own (ie: clearing all entries for a
194 * given uid).
195 */
196 bool checkBinderPermissionOrSelfTarget(perm_t permission, int32_t targetUid);
197
198 /**
199 * Helper method to check that the caller has the required permission as
200 * well as the keystore is in the unlocked state if checkUnlocked is true.
201 *
202 * Returns NO_ERROR on success, PERMISSION_DENIED on a permission error and
203 * otherwise the state of keystore when not unlocked and checkUnlocked is
204 * true.
205 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100206 KeyStoreServiceReturnCode checkBinderPermissionAndKeystoreState(perm_t permission,
207 int32_t targetUid = -1,
208 bool checkUnlocked = true);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700209
210 bool isKeystoreUnlocked(State state);
211
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700212 /**
213 * Check that all keymaster_key_param_t's provided by the application are
214 * allowed. Any parameter that keystore adds itself should be disallowed here.
215 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100216 bool checkAllowedOperationParams(const hidl_vec<KeyParameter>& params);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700217
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100218 void addLegacyBeginParams(const android::String16& name, AuthorizationSet* params);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700219
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100220 KeyStoreServiceReturnCode doLegacySignVerify(const android::String16& name,
221 const hidl_vec<uint8_t>& data,
222 hidl_vec<uint8_t>* out,
223 const hidl_vec<uint8_t>& signature,
224 KeyPurpose purpose);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700225
Shawn Willden98c59162016-03-20 09:10:18 -0600226 /**
David Zeuthenc6eb7cd2017-11-27 11:33:55 -0500227 * Adds a Confirmation Token to the key parameters if needed.
228 */
229 void appendConfirmationTokenIfNeeded(const KeyCharacteristics& keyCharacteristics,
230 std::vector<KeyParameter>* params);
231
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700232 sp<KeyStore> mKeyStore;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700233};
234
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100235}; // namespace keystore
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700236
237#endif // KEYSTORE_KEYSTORE_SERVICE_H_