blob: cd4339143ee2cb5546d313abf43841643a73b972 [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
Shawn Willden98c59162016-03-20 09:10:18 -060022#include <keymaster/authorization_set.h>
23
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
30namespace android {
31
32class KeyStoreService : public BnKeystoreService, public IBinder::DeathRecipient {
33 public:
34 KeyStoreService(KeyStore* keyStore) : mKeyStore(keyStore), mOperationMap(this) {}
35
36 void binderDied(const wp<IBinder>& who);
37
38 int32_t getState(int32_t userId);
39
40 int32_t get(const String16& name, int32_t uid, uint8_t** item, size_t* itemLength);
41 int32_t insert(const String16& name, const uint8_t* item, size_t itemLength, int targetUid,
42 int32_t flags);
43 int32_t del(const String16& name, int targetUid);
44 int32_t exist(const String16& name, int targetUid);
45 int32_t list(const String16& prefix, int targetUid, Vector<String16>* matches);
46
47 int32_t reset();
48
49 int32_t onUserPasswordChanged(int32_t userId, const String16& password);
50 int32_t onUserAdded(int32_t userId, int32_t parentId);
51 int32_t onUserRemoved(int32_t userId);
52
53 int32_t lock(int32_t userId);
54 int32_t unlock(int32_t userId, const String16& pw);
55
56 bool isEmpty(int32_t userId);
57
58 int32_t generate(const String16& name, int32_t targetUid, int32_t keyType, int32_t keySize,
59 int32_t flags, Vector<sp<KeystoreArg>>* args);
60 int32_t import(const String16& name, const uint8_t* data, size_t length, int targetUid,
61 int32_t flags);
62 int32_t sign(const String16& name, const uint8_t* data, size_t length, uint8_t** out,
63 size_t* outLength);
64 int32_t verify(const String16& name, const uint8_t* data, size_t dataLength,
65 const uint8_t* signature, size_t signatureLength);
66
67 /*
68 * TODO: The abstraction between things stored in hardware and regular blobs
69 * of data stored on the filesystem should be moved down to keystore itself.
70 * Unfortunately the Java code that calls this has naming conventions that it
71 * knows about. Ideally keystore shouldn't be used to store random blobs of
72 * data.
73 *
74 * Until that happens, it's necessary to have a separate "get_pubkey" and
75 * "del_key" since the Java code doesn't really communicate what it's
76 * intentions are.
77 */
78 int32_t get_pubkey(const String16& name, uint8_t** pubkey, size_t* pubkeyLength);
79
80 int32_t grant(const String16& name, int32_t granteeUid);
81 int32_t ungrant(const String16& name, int32_t granteeUid);
82
83 int64_t getmtime(const String16& name, int32_t uid);
84
85 int32_t duplicate(const String16& srcKey, int32_t srcUid, const String16& destKey,
86 int32_t destUid);
87
88 int32_t is_hardware_backed(const String16& keyType);
89
90 int32_t clear_uid(int64_t targetUid64);
91
92 int32_t addRngEntropy(const uint8_t* data, size_t dataLength);
93 int32_t generateKey(const String16& name, const KeymasterArguments& params,
94 const uint8_t* entropy, size_t entropyLength, int uid, int flags,
95 KeyCharacteristics* outCharacteristics);
96 int32_t getKeyCharacteristics(const String16& name, const keymaster_blob_t* clientId,
97 const keymaster_blob_t* appData, int32_t uid,
98 KeyCharacteristics* outCharacteristics);
99 int32_t importKey(const String16& name, const KeymasterArguments& params,
100 keymaster_key_format_t format, const uint8_t* keyData, size_t keyLength,
101 int uid, int flags, KeyCharacteristics* outCharacteristics);
102 void exportKey(const String16& name, keymaster_key_format_t format,
103 const keymaster_blob_t* clientId, const keymaster_blob_t* appData, int32_t uid,
104 ExportResult* result);
105 void begin(const sp<IBinder>& appToken, const String16& name, keymaster_purpose_t purpose,
106 bool pruneable, const KeymasterArguments& params, const uint8_t* entropy,
107 size_t entropyLength, int32_t uid, OperationResult* result);
108 void update(const sp<IBinder>& token, const KeymasterArguments& params, const uint8_t* data,
109 size_t dataLength, OperationResult* result);
110 void finish(const sp<IBinder>& token, const KeymasterArguments& params,
111 const uint8_t* signature, size_t signatureLength, const uint8_t* entropy,
112 size_t entropyLength, OperationResult* result);
113 int32_t abort(const sp<IBinder>& token);
114
115 bool isOperationAuthorized(const sp<IBinder>& token);
116
117 int32_t addAuthToken(const uint8_t* token, size_t length);
118
Shawn Willden50eb1b22016-01-21 12:41:23 -0700119 int32_t attestKey(const String16& name, const KeymasterArguments& params,
120 KeymasterCertificateChain* outChain) override;
121
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400122 int32_t onDeviceOffBody();
123
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700124 private:
125 static const int32_t UID_SELF = -1;
126
127 /**
128 * Prune the oldest pruneable operation.
129 */
130 bool pruneOperation();
131
132 /**
133 * Get the effective target uid for a binder operation that takes an
134 * optional uid as the target.
135 */
136 uid_t getEffectiveUid(int32_t targetUid);
137
138 /**
139 * Check if the caller of the current binder method has the required
140 * permission and if acting on other uids the grants to do so.
141 */
142 bool checkBinderPermission(perm_t permission, int32_t targetUid = UID_SELF);
143
144 /**
145 * Check if the caller of the current binder method has the required
146 * permission and the target uid is the caller or the caller is system.
147 */
148 bool checkBinderPermissionSelfOrSystem(perm_t permission, int32_t targetUid);
149
150 /**
151 * Check if the caller of the current binder method has the required
152 * permission or the target of the operation is the caller's uid. This is
153 * for operation where the permission is only for cross-uid activity and all
154 * uids are allowed to act on their own (ie: clearing all entries for a
155 * given uid).
156 */
157 bool checkBinderPermissionOrSelfTarget(perm_t permission, int32_t targetUid);
158
159 /**
160 * Helper method to check that the caller has the required permission as
161 * well as the keystore is in the unlocked state if checkUnlocked is true.
162 *
163 * Returns NO_ERROR on success, PERMISSION_DENIED on a permission error and
164 * otherwise the state of keystore when not unlocked and checkUnlocked is
165 * true.
166 */
167 int32_t checkBinderPermissionAndKeystoreState(perm_t permission, int32_t targetUid = -1,
168 bool checkUnlocked = true);
169
170 bool isKeystoreUnlocked(State state);
171
Shawn Willden715d0232016-01-21 00:45:13 -0700172 bool isKeyTypeSupported(const keymaster2_device_t* device, keymaster_keypair_t keyType);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700173
174 /**
175 * Check that all keymaster_key_param_t's provided by the application are
176 * allowed. Any parameter that keystore adds itself should be disallowed here.
177 */
178 bool checkAllowedOperationParams(const std::vector<keymaster_key_param_t>& params);
179
180 keymaster_error_t getOperationCharacteristics(const keymaster_key_blob_t& key,
Shawn Willden715d0232016-01-21 00:45:13 -0700181 const keymaster2_device_t* dev,
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700182 const std::vector<keymaster_key_param_t>& params,
183 keymaster_key_characteristics_t* out);
184
185 /**
186 * Get the auth token for this operation from the auth token table.
187 *
188 * Returns ::NO_ERROR if the auth token was set or none was required.
189 * ::OP_AUTH_NEEDED if it is a per op authorization, no
190 * authorization token exists for that operation and
191 * failOnTokenMissing is false.
192 * KM_ERROR_KEY_USER_NOT_AUTHENTICATED if there is no valid auth
193 * token for the operation
194 */
195 int32_t getAuthToken(const keymaster_key_characteristics_t* characteristics,
196 keymaster_operation_handle_t handle, keymaster_purpose_t purpose,
197 const hw_auth_token_t** authToken, bool failOnTokenMissing = true);
198
199 void addAuthToParams(std::vector<keymaster_key_param_t>* params, const hw_auth_token_t* token);
200
201 /**
202 * Add the auth token for the operation to the param list if the operation
203 * requires authorization. Uses the cached result in the OperationMap if available
204 * otherwise gets the token from the AuthTokenTable and caches the result.
205 *
206 * Returns ::NO_ERROR if the auth token was added or not needed.
207 * KM_ERROR_KEY_USER_NOT_AUTHENTICATED if the operation is not
208 * authenticated.
209 * KM_ERROR_INVALID_OPERATION_HANDLE if token is not a valid
210 * operation token.
211 */
212 int32_t addOperationAuthTokenIfNeeded(sp<IBinder> token,
213 std::vector<keymaster_key_param_t>* params);
214
215 /**
216 * Translate a result value to a legacy return value. All keystore errors are
217 * preserved and keymaster errors become SYSTEM_ERRORs
218 */
219 int32_t translateResultToLegacyResult(int32_t result);
220
221 keymaster_key_param_t* getKeyAlgorithm(keymaster_key_characteristics_t* characteristics);
222
223 void addLegacyBeginParams(const String16& name, std::vector<keymaster_key_param_t>& params);
224
225 int32_t doLegacySignVerify(const String16& name, const uint8_t* data, size_t length,
226 uint8_t** out, size_t* outLength, const uint8_t* signature,
227 size_t signatureLength, keymaster_purpose_t purpose);
228
Shawn Willden98c59162016-03-20 09:10:18 -0600229 /**
230 * Upgrade a key blob under alias "name", returning the new blob in "blob". If "blob"
231 * previously contained data, it will be overwritten.
232 *
233 * Returns ::NO_ERROR if the key was upgraded successfully.
234 * KM_ERROR_VERSION_MISMATCH if called on a key whose patch level is greater than or
235 * equal to the current system patch level.
236 */
237 int32_t upgradeKeyBlob(const String16& name, uid_t targetUid,
238 const keymaster::AuthorizationSet& params, Blob* blob);
239
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700240 ::KeyStore* mKeyStore;
241 OperationMap mOperationMap;
242 keymaster::AuthTokenTable mAuthTokenTable;
243 KeystoreKeymasterEnforcement enforcement_policy;
244};
245
246}; // namespace android
247
248#endif // KEYSTORE_KEYSTORE_SERVICE_H_