blob: 7ce58ca9b25428d2f44e84b76db3780c323fa390 [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
Janis Danisevskis011675d2016-09-01 11:41:29 +010017#define LOG_TAG "keystore"
18
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070019#include "key_store_service.h"
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070020#include "include/keystore/KeystoreArg.h"
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070021
22#include <fcntl.h>
23#include <sys/stat.h>
24
Janis Danisevskis7612fd42016-09-01 11:50:02 +010025#include <algorithm>
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070026#include <atomic>
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070027#include <sstream>
28
Pavel Grafovff311b42018-01-24 20:34:37 +000029#include <android-base/scopeguard.h>
Bartosz Fabianowskia9452d92017-01-23 22:21:11 +010030#include <binder/IInterface.h>
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070031#include <binder/IPCThreadState.h>
Bartosz Fabianowskia9452d92017-01-23 22:21:11 +010032#include <binder/IPermissionController.h>
33#include <binder/IServiceManager.h>
Brian Claire Young3133c452018-08-31 13:56:49 -070034#include <cutils/multiuser.h>
Pavel Grafovff311b42018-01-24 20:34:37 +000035#include <log/log_event_list.h>
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070036
37#include <private/android_filesystem_config.h>
Pavel Grafovff311b42018-01-24 20:34:37 +000038#include <private/android_logger.h>
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070039
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070040#include <android/hardware/confirmationui/1.0/IConfirmationUI.h>
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010041#include <android/hardware/keymaster/3.0/IHwKeymasterDevice.h>
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070042
43#include "defaults.h"
Max Bires33aac2d2018-02-23 10:53:10 -080044#include "key_proto_handler.h"
Janis Danisevskis18f27ad2016-06-01 13:57:40 -070045#include "keystore_attestation_id.h"
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010046#include "keystore_keymaster_enforcement.h"
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070047#include "keystore_utils.h"
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010048#include <keystore/keystore_hidl_support.h>
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070049#include <keystore/keystore_return_types.h>
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070050
Janis Danisevskis8f737ad2017-11-21 12:30:15 -080051#include <hardware/hw_auth_token.h>
52
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010053namespace keystore {
Shawn Willdend5a24e62017-02-28 13:53:24 -070054
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010055using namespace android;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070056
Shawn Willdene2a7b522017-04-11 09:27:40 -060057namespace {
58
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070059using ::android::binder::Status;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070060using android::security::KeystoreArg;
61using android::security::keymaster::ExportResult;
62using android::security::keymaster::KeymasterArguments;
63using android::security::keymaster::KeymasterBlob;
64using android::security::keymaster::KeymasterCertificateChain;
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070065using android::security::keymaster::operationFailed;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070066using android::security::keymaster::OperationResult;
David Zeuthenc6eb7cd2017-11-27 11:33:55 -050067using ConfirmationResponseCode = android::hardware::confirmationui::V1_0::ResponseCode;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -070068
Shawn Willdene2a7b522017-04-11 09:27:40 -060069constexpr double kIdRotationPeriod = 30 * 24 * 60 * 60; /* Thirty days, in seconds */
70const char* kTimestampFilePath = "timestamp";
Rubin Xu1a203e32018-05-08 14:25:21 +010071const int ID_ATTESTATION_REQUEST_GENERIC_INFO = 1 << 0;
72const int ID_ATTESTATION_REQUEST_UNIQUE_DEVICE_ID = 1 << 1;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070073
74struct BIGNUM_Delete {
75 void operator()(BIGNUM* p) const { BN_free(p); }
76};
Janis Danisevskisccfff102017-05-01 11:02:51 -070077typedef std::unique_ptr<BIGNUM, BIGNUM_Delete> Unique_BIGNUM;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -070078
Shawn Willdene2a7b522017-04-11 09:27:40 -060079bool containsTag(const hidl_vec<KeyParameter>& params, Tag tag) {
Janis Danisevskisff3d7f42018-10-08 07:15:09 -070080 return params.end() !=
81 std::find_if(params.begin(), params.end(),
82 [&](const KeyParameter& param) { return param.tag == tag; });
Shawn Willdend5a24e62017-02-28 13:53:24 -070083}
84
Shawn Willdene2a7b522017-04-11 09:27:40 -060085std::pair<KeyStoreServiceReturnCode, bool> hadFactoryResetSinceIdRotation() {
86 struct stat sbuf;
87 if (stat(kTimestampFilePath, &sbuf) == 0) {
Yi Konge353f252018-07-30 01:38:39 -070088 double diff_secs = difftime(time(nullptr), sbuf.st_ctime);
Shawn Willdene2a7b522017-04-11 09:27:40 -060089 return {ResponseCode::NO_ERROR, diff_secs < kIdRotationPeriod};
90 }
91
92 if (errno != ENOENT) {
93 ALOGE("Failed to stat \"timestamp\" file, with error %d", errno);
94 return {ResponseCode::SYSTEM_ERROR, false /* don't care */};
95 }
96
97 int fd = creat(kTimestampFilePath, 0600);
98 if (fd < 0) {
99 ALOGE("Couldn't create \"timestamp\" file, with error %d", errno);
100 return {ResponseCode::SYSTEM_ERROR, false /* don't care */};
101 }
102
103 if (close(fd)) {
104 ALOGE("Couldn't close \"timestamp\" file, with error %d", errno);
105 return {ResponseCode::SYSTEM_ERROR, false /* don't care */};
106 }
107
108 return {ResponseCode::NO_ERROR, true};
109}
110
Eran Messeri03fc4c82018-08-16 18:53:15 +0100111using ::android::security::KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE;
Bartosz Fabianowski5aa93e02017-04-24 13:54:49 +0200112
113KeyStoreServiceReturnCode updateParamsForAttestation(uid_t callingUid, AuthorizationSet* params) {
114 KeyStoreServiceReturnCode responseCode;
115 bool factoryResetSinceIdRotation;
116 std::tie(responseCode, factoryResetSinceIdRotation) = hadFactoryResetSinceIdRotation();
117
118 if (!responseCode.isOk()) return responseCode;
119 if (factoryResetSinceIdRotation) params->push_back(TAG_RESET_SINCE_ID_ROTATION);
120
121 auto asn1_attestation_id_result = security::gather_attestation_application_id(callingUid);
122 if (!asn1_attestation_id_result.isOk()) {
123 ALOGE("failed to gather attestation_id");
124 return ErrorCode::ATTESTATION_APPLICATION_ID_MISSING;
125 }
126 std::vector<uint8_t>& asn1_attestation_id = asn1_attestation_id_result;
127
128 /*
Eran Messeri03fc4c82018-08-16 18:53:15 +0100129 * The attestation application ID must not be longer than
130 * KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE, error out if gather_attestation_application_id
131 * returned such an invalid vector.
Bartosz Fabianowski5aa93e02017-04-24 13:54:49 +0200132 */
133 if (asn1_attestation_id.size() > KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE) {
Eran Messeri03fc4c82018-08-16 18:53:15 +0100134 ALOGE("BUG: Gathered Attestation Application ID is too big (%d)",
135 static_cast<int32_t>(asn1_attestation_id.size()));
136 return ErrorCode::CANNOT_ATTEST_IDS;
Bartosz Fabianowski5aa93e02017-04-24 13:54:49 +0200137 }
138
139 params->push_back(TAG_ATTESTATION_APPLICATION_ID, asn1_attestation_id);
140
141 return ResponseCode::NO_ERROR;
142}
143
Shawn Willdene2a7b522017-04-11 09:27:40 -0600144} // anonymous namespace
145
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700146Status KeyStoreService::getState(int32_t userId, int32_t* aidl_return) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700147 if (!checkBinderPermission(P_GET_STATE)) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700148 *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED);
149 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700150 }
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700151 *aidl_return = mKeyStore->getState(userId);
152 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700153}
154
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700155Status KeyStoreService::get(const String16& name, int32_t uid, ::std::vector<uint8_t>* item) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700156 uid_t targetUid = getEffectiveUid(uid);
157 if (!checkBinderPermission(P_GET, targetUid)) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700158 // see keystore/keystore.h
159 return Status::fromServiceSpecificError(
160 static_cast<int32_t>(ResponseCode::PERMISSION_DENIED));
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700161 }
162
163 String8 name8(name);
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700164 ResponseCode rc;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700165 Blob keyBlob;
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700166 Blob charBlob;
167 LockedKeyBlobEntry lockedEntry;
168
169 std::tie(rc, keyBlob, charBlob, lockedEntry) =
170 mKeyStore->getKeyForName(name8, targetUid, TYPE_GENERIC);
171 if (rc != ResponseCode::NO_ERROR) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700172 *item = ::std::vector<uint8_t>();
173 // Return empty array if key is not found
174 // TODO: consider having returned value nullable or parse exception on the client.
175 return Status::fromServiceSpecificError(static_cast<int32_t>(rc));
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700176 }
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100177 auto resultBlob = blob2hidlVec(keyBlob);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700178 // The static_cast here is needed to prevent a move, forcing a deep copy.
179 if (item) *item = static_cast<const hidl_vec<uint8_t>&>(blob2hidlVec(keyBlob));
180 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700181}
182
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700183Status KeyStoreService::insert(const String16& name, const ::std::vector<uint8_t>& item,
184 int targetUid, int32_t flags, int32_t* aidl_return) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700185 targetUid = getEffectiveUid(targetUid);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700186 KeyStoreServiceReturnCode result =
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700187 checkBinderPermissionAndKeystoreState(P_INSERT, targetUid, flags & KEYSTORE_FLAG_ENCRYPTED);
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100188 if (!result.isOk()) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700189 *aidl_return = static_cast<int32_t>(result);
190 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700191 }
192
193 String8 name8(name);
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700194 auto lockedEntry = mKeyStore->getLockedBlobEntryIfNotExists(name8.string(), targetUid);
195
196 if (!lockedEntry) {
197 ALOGE("failed to grab lock on blob entry %u_%s", targetUid, name8.string());
198 *aidl_return = static_cast<int32_t>(ResponseCode::KEY_ALREADY_EXISTS);
199 return Status::ok();
200 }
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700201
Yi Konge353f252018-07-30 01:38:39 -0700202 Blob keyBlob(&item[0], item.size(), nullptr, 0, ::TYPE_GENERIC);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700203 keyBlob.setEncrypted(flags & KEYSTORE_FLAG_ENCRYPTED);
204
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700205 *aidl_return = static_cast<int32_t>(mKeyStore->put(lockedEntry, keyBlob, {}));
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700206 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700207}
208
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700209Status KeyStoreService::del(const String16& name, int targetUid, int32_t* aidl_return) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700210 targetUid = getEffectiveUid(targetUid);
211 if (!checkBinderPermission(P_DELETE, targetUid)) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700212 *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED);
213 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700214 }
215 String8 name8(name);
Rubin Xu7675c9f2017-03-15 19:26:52 +0000216 ALOGI("del %s %d", name8.string(), targetUid);
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700217 auto lockedEntry = mKeyStore->getLockedBlobEntryIfExists(name8.string(), targetUid);
218 if (!lockedEntry) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700219 *aidl_return = static_cast<int32_t>(ResponseCode::KEY_NOT_FOUND);
220 return Status::ok();
221 }
Janis Danisevskisaf7783f2017-09-21 11:29:47 -0700222
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700223 ResponseCode result = mKeyStore->del(lockedEntry);
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400224
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700225 *aidl_return = static_cast<int32_t>(result);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700226 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700227}
228
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700229Status KeyStoreService::exist(const String16& name, int targetUid, int32_t* aidl_return) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700230 targetUid = getEffectiveUid(targetUid);
231 if (!checkBinderPermission(P_EXIST, targetUid)) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700232 *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED);
233 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700234 }
235
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700236 LockedKeyBlobEntry lockedEntry =
237 mKeyStore->getLockedBlobEntryIfExists(String8(name).string(), targetUid);
238 *aidl_return =
239 static_cast<int32_t>(lockedEntry ? ResponseCode::NO_ERROR : ResponseCode::KEY_NOT_FOUND);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700240 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700241}
242
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700243Status KeyStoreService::list(const String16& prefix, int32_t targetUid,
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700244 ::std::vector<::android::String16>* matches) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700245 targetUid = getEffectiveUid(targetUid);
246 if (!checkBinderPermission(P_LIST, targetUid)) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700247 return Status::fromServiceSpecificError(
248 static_cast<int32_t>(ResponseCode::PERMISSION_DENIED));
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700249 }
250 const String8 prefix8(prefix);
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700251 const std::string stdPrefix(prefix8.string());
252
253 ResponseCode rc;
254 std::list<LockedKeyBlobEntry> internal_matches;
255
256 std::tie(rc, internal_matches) = LockedKeyBlobEntry::list(
257 mKeyStore->getUserStateDB().getUserStateByUid(targetUid)->getUserDirName(),
258 [&](uid_t uid, const std::string& alias) {
259 std::mismatch(stdPrefix.begin(), stdPrefix.end(), alias.begin(), alias.end());
260 return uid == static_cast<uid_t>(targetUid) &&
261 std::mismatch(stdPrefix.begin(), stdPrefix.end(), alias.begin(), alias.end())
262 .first == stdPrefix.end();
263 });
264
265 if (rc != ResponseCode::NO_ERROR) {
266 return Status::fromServiceSpecificError(static_cast<int32_t>(rc));
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700267 }
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700268
269 for (LockedKeyBlobEntry& entry : internal_matches) {
270 matches->push_back(String16(entry->alias().substr(prefix8.size()).c_str()));
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700271 }
272 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700273}
274
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700275Status KeyStoreService::reset(int32_t* aidl_return) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700276 if (!checkBinderPermission(P_RESET)) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700277 *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED);
278 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700279 }
280
281 uid_t callingUid = IPCThreadState::self()->getCallingUid();
282 mKeyStore->resetUser(get_user_id(callingUid), false);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700283 *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR);
284 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700285}
286
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700287Status KeyStoreService::onUserPasswordChanged(int32_t userId, const String16& password,
288 int32_t* aidl_return) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700289 if (!checkBinderPermission(P_PASSWORD)) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700290 *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED);
291 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700292 }
293
294 const String8 password8(password);
295 // Flush the auth token table to prevent stale tokens from sticking
296 // around.
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700297 mKeyStore->getAuthTokenTable().Clear();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700298
299 if (password.size() == 0) {
300 ALOGI("Secure lockscreen for user %d removed, deleting encrypted entries", userId);
301 mKeyStore->resetUser(userId, true);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700302 *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR);
303 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700304 } else {
305 switch (mKeyStore->getState(userId)) {
306 case ::STATE_UNINITIALIZED: {
307 // generate master key, encrypt with password, write to file,
308 // initialize mMasterKey*.
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700309 *aidl_return = static_cast<int32_t>(mKeyStore->initializeUser(password8, userId));
310 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700311 }
312 case ::STATE_NO_ERROR: {
313 // rewrite master key with new password.
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700314 *aidl_return = static_cast<int32_t>(mKeyStore->writeMasterKey(password8, userId));
315 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700316 }
317 case ::STATE_LOCKED: {
318 ALOGE("Changing user %d's password while locked, clearing old encryption", userId);
319 mKeyStore->resetUser(userId, true);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700320 *aidl_return = static_cast<int32_t>(mKeyStore->initializeUser(password8, userId));
321 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700322 }
323 }
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700324 *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR);
325 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700326 }
327}
328
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700329Status KeyStoreService::onUserAdded(int32_t userId, int32_t parentId, int32_t* aidl_return) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700330 if (!checkBinderPermission(P_USER_CHANGED)) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700331 *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED);
332 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700333 }
334
335 // Sanity check that the new user has an empty keystore.
336 if (!mKeyStore->isEmpty(userId)) {
337 ALOGW("New user %d's keystore not empty. Clearing old entries.", userId);
338 }
339 // Unconditionally clear the keystore, just to be safe.
340 mKeyStore->resetUser(userId, false);
341 if (parentId != -1) {
342 // This profile must share the same master key password as the parent profile. Because the
343 // password of the parent profile is not known here, the best we can do is copy the parent's
344 // master key and master key file. This makes this profile use the same master key as the
345 // parent profile, forever.
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700346 *aidl_return = static_cast<int32_t>(mKeyStore->copyMasterKey(parentId, userId));
347 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700348 } else {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700349 *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR);
350 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700351 }
352}
353
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700354Status KeyStoreService::onUserRemoved(int32_t userId, int32_t* aidl_return) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700355 if (!checkBinderPermission(P_USER_CHANGED)) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700356 *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED);
357 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700358 }
359
360 mKeyStore->resetUser(userId, false);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700361 *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR);
362 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700363}
364
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700365Status KeyStoreService::lock(int32_t userId, int32_t* aidl_return) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700366 if (!checkBinderPermission(P_LOCK)) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700367 *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED);
368 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700369 }
370
371 State state = mKeyStore->getState(userId);
372 if (state != ::STATE_NO_ERROR) {
373 ALOGD("calling lock in state: %d", state);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700374 *aidl_return = static_cast<int32_t>(ResponseCode(state));
375 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700376 }
377
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700378 mKeyStore->getEnforcementPolicy().set_device_locked(true, userId);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700379 mKeyStore->lock(userId);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700380 *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR);
381 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700382}
383
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700384Status KeyStoreService::unlock(int32_t userId, const String16& pw, int32_t* aidl_return) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700385 if (!checkBinderPermission(P_UNLOCK)) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700386 *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED);
387 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700388 }
389
390 State state = mKeyStore->getState(userId);
391 if (state != ::STATE_LOCKED) {
392 switch (state) {
393 case ::STATE_NO_ERROR:
394 ALOGI("calling unlock when already unlocked, ignoring.");
395 break;
396 case ::STATE_UNINITIALIZED:
397 ALOGE("unlock called on uninitialized keystore.");
398 break;
399 default:
400 ALOGE("unlock called on keystore in unknown state: %d", state);
401 break;
402 }
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700403 *aidl_return = static_cast<int32_t>(ResponseCode(state));
404 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700405 }
406
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700407 mKeyStore->getEnforcementPolicy().set_device_locked(false, userId);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700408 const String8 password8(pw);
409 // read master key, decrypt with password, initialize mMasterKey*.
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700410 *aidl_return = static_cast<int32_t>(mKeyStore->readMasterKey(password8, userId));
411 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700412}
413
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700414Status KeyStoreService::isEmpty(int32_t userId, int32_t* aidl_return) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700415 if (!checkBinderPermission(P_IS_EMPTY)) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700416 *aidl_return = static_cast<int32_t>(false);
417 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700418 }
419
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700420 *aidl_return = static_cast<int32_t>(mKeyStore->isEmpty(userId));
421 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700422}
423
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700424Status KeyStoreService::grant(const String16& name, int32_t granteeUid,
425 ::android::String16* aidl_return) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700426 uid_t callingUid = IPCThreadState::self()->getCallingUid();
Bo Zhu91de6db2018-03-20 12:52:13 -0700427 auto result =
428 checkBinderPermissionAndKeystoreState(P_GRANT, /*targetUid=*/-1, /*checkUnlocked=*/false);
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100429 if (!result.isOk()) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700430 *aidl_return = String16();
431 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700432 }
433
434 String8 name8(name);
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700435 auto lockedEntry = mKeyStore->getLockedBlobEntryIfExists(name8.string(), callingUid);
436 if (!lockedEntry) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700437 *aidl_return = String16();
438 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700439 }
440
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700441 *aidl_return = String16(mKeyStore->addGrant(lockedEntry, granteeUid).c_str());
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700442 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700443}
444
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700445Status KeyStoreService::ungrant(const String16& name, int32_t granteeUid, int32_t* aidl_return) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700446 uid_t callingUid = IPCThreadState::self()->getCallingUid();
Bo Zhu91de6db2018-03-20 12:52:13 -0700447 KeyStoreServiceReturnCode result =
448 checkBinderPermissionAndKeystoreState(P_GRANT, /*targetUid=*/-1, /*checkUnlocked=*/false);
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100449 if (!result.isOk()) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700450 *aidl_return = static_cast<int32_t>(result);
451 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700452 }
453
454 String8 name8(name);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700455
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700456 auto lockedEntry = mKeyStore->getLockedBlobEntryIfExists(name8.string(), callingUid);
457 if (!lockedEntry) {
458 *aidl_return = static_cast<int32_t>(ResponseCode::KEY_NOT_FOUND);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700459 }
460
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700461 *aidl_return = mKeyStore->removeGrant(lockedEntry, granteeUid);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700462 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700463}
464
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700465Status KeyStoreService::getmtime(const String16& name, int32_t uid, int64_t* time) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700466 uid_t targetUid = getEffectiveUid(uid);
467 if (!checkBinderPermission(P_GET, targetUid)) {
468 ALOGW("permission denied for %d: getmtime", targetUid);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700469 *time = -1L;
470 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700471 }
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700472 String8 name8(name);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700473
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700474 auto lockedEntry = mKeyStore->getLockedBlobEntryIfExists(name8.string(), targetUid);
475 if (!lockedEntry) {
476 ALOGW("could not access key with alias %s for getmtime", name8.string());
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700477 *time = -1L;
478 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700479 }
480
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700481 std::string filename = lockedEntry->getKeyBlobPath();
482
483 int fd = TEMP_FAILURE_RETRY(open(filename.c_str(), O_NOFOLLOW, O_RDONLY));
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700484 if (fd < 0) {
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700485 ALOGW("could not open %s for getmtime", filename.c_str());
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700486 *time = -1L;
487 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700488 }
489
490 struct stat s;
491 int ret = fstat(fd, &s);
492 close(fd);
493 if (ret == -1) {
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700494 ALOGW("could not stat %s for getmtime", filename.c_str());
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700495 *time = -1L;
496 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700497 }
498
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700499 *time = static_cast<int64_t>(s.st_mtime);
500 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700501}
502
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700503Status KeyStoreService::is_hardware_backed(const String16& keyType, int32_t* aidl_return) {
504 *aidl_return = static_cast<int32_t>(mKeyStore->isHardwareBacked(keyType) ? 1 : 0);
505 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700506}
507
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700508Status KeyStoreService::clear_uid(int64_t targetUid64, int32_t* aidl_return) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700509 uid_t targetUid = getEffectiveUid(targetUid64);
510 if (!checkBinderPermissionSelfOrSystem(P_CLEAR_UID, targetUid)) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700511 *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED);
512 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700513 }
Rubin Xu7675c9f2017-03-15 19:26:52 +0000514 ALOGI("clear_uid %" PRId64, targetUid64);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700515
Janis Danisevskisaf7783f2017-09-21 11:29:47 -0700516 mKeyStore->removeAllGrantsToUid(targetUid);
517
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700518 ResponseCode rc;
519 std::list<LockedKeyBlobEntry> entries;
520
521 // list has a fence making sure no workers are modifying blob files before iterating the
522 // data base. All returned entries are locked.
523 std::tie(rc, entries) = LockedKeyBlobEntry::list(
524 mKeyStore->getUserStateDB().getUserStateByUid(targetUid)->getUserDirName(),
525 [&](uid_t uid, const std::string&) -> bool { return uid == targetUid; });
526
527 if (rc != ResponseCode::NO_ERROR) {
528 *aidl_return = static_cast<int32_t>(rc);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700529 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700530 }
531
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700532 for (LockedKeyBlobEntry& lockedEntry : entries) {
Rubin Xu85c85e92017-04-26 20:07:30 +0100533 if (get_app_id(targetUid) == AID_SYSTEM) {
534 Blob keyBlob;
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700535 Blob charBlob;
536 std::tie(rc, keyBlob, charBlob) = mKeyStore->get(lockedEntry);
537 if (rc == ResponseCode::NO_ERROR && keyBlob.isCriticalToDeviceEncryption()) {
Rubin Xu85c85e92017-04-26 20:07:30 +0100538 // Do not clear keys critical to device encryption under system uid.
539 continue;
540 }
541 }
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700542 mKeyStore->del(lockedEntry);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700543 }
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700544 *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR);
545 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700546}
547
Janis Danisevskisc1460142017-12-18 16:48:46 -0800548Status KeyStoreService::addRngEntropy(const ::std::vector<uint8_t>& entropy, int32_t flags,
549 int32_t* aidl_return) {
550 auto device = mKeyStore->getDevice(flagsToSecurityLevel(flags));
551 if (!device) {
552 *aidl_return = static_cast<int32_t>(ErrorCode::HARDWARE_TYPE_UNAVAILABLE);
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700553 return Status::ok();
Janis Danisevskisc1460142017-12-18 16:48:46 -0800554 }
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700555 std::promise<KeyStoreServiceReturnCode> resultPromise;
556 auto resultFuture = resultPromise.get_future();
557
558 device->addRngEntropy(
559 entropy, [&](Return<ErrorCode> rc) { resultPromise.set_value(KS_HANDLE_HIDL_ERROR(rc)); });
560 resultFuture.wait();
561 *aidl_return = int32_t(resultFuture.get());
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700562 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700563}
564
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700565Status
566KeyStoreService::generateKey(const String16& name, const KeymasterArguments& params,
567 const ::std::vector<uint8_t>& entropy, int uid, int flags,
568 android::security::keymaster::KeyCharacteristics* outCharacteristics,
569 int32_t* aidl_return) {
Max Biresef4f0672017-11-29 14:38:48 -0800570 // TODO(jbires): remove this getCallingUid call upon implementation of b/25646100
571 uid_t originalUid = IPCThreadState::self()->getCallingUid();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700572 uid = getEffectiveUid(uid);
Pavel Grafovff311b42018-01-24 20:34:37 +0000573 auto logOnScopeExit = android::base::make_scope_guard([&] {
574 if (__android_log_security()) {
575 android_log_event_list(SEC_TAG_AUTH_KEY_GENERATED)
576 << int32_t(*aidl_return == static_cast<int32_t>(ResponseCode::NO_ERROR))
577 << String8(name) << int32_t(uid) << LOG_ID_SECURITY;
578 }
579 });
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100580 KeyStoreServiceReturnCode rc =
581 checkBinderPermissionAndKeystoreState(P_INSERT, uid, flags & KEYSTORE_FLAG_ENCRYPTED);
582 if (!rc.isOk()) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700583 *aidl_return = static_cast<int32_t>(rc);
584 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700585 }
Rubin Xu67899de2017-04-21 19:15:13 +0100586 if ((flags & KEYSTORE_FLAG_CRITICAL_TO_DEVICE_ENCRYPTION) && get_app_id(uid) != AID_SYSTEM) {
587 ALOGE("Non-system uid %d cannot set FLAG_CRITICAL_TO_DEVICE_ENCRYPTION", uid);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700588 *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED);
589 return Status::ok();
Rubin Xu67899de2017-04-21 19:15:13 +0100590 }
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700591
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700592 if (containsTag(params.getParameters(), Tag::INCLUDE_UNIQUE_ID)) {
Shawn Willdenda6dcc32017-12-03 14:56:05 -0700593 // TODO(jbires): remove uid checking upon implementation of b/25646100
Max Bires670467d2017-12-12 11:16:43 -0800594 if (!checkBinderPermission(P_GEN_UNIQUE_ID) ||
Max Biresef4f0672017-11-29 14:38:48 -0800595 originalUid != IPCThreadState::self()->getCallingUid()) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700596 *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED);
597 return Status::ok();
598 }
Shawn Willdene2a7b522017-04-11 09:27:40 -0600599 }
600
Janis Danisevskisc1460142017-12-18 16:48:46 -0800601 SecurityLevel securityLevel = flagsToSecurityLevel(flags);
602 auto dev = mKeyStore->getDevice(securityLevel);
603 if (!dev) {
604 *aidl_return = static_cast<int32_t>(ErrorCode::HARDWARE_TYPE_UNAVAILABLE);
605 return Status::ok();
606 }
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400607
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100608 String8 name8(name);
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700609 auto lockedEntry = mKeyStore->getLockedBlobEntryIfNotExists(name8.string(), uid);
610 if (!lockedEntry) {
611 *aidl_return = static_cast<int32_t>(ResponseCode::KEY_ALREADY_EXISTS);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700612 return Status::ok();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100613 }
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400614
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700615 logOnScopeExit.Disable();
616
617 std::promise<KeyStoreServiceReturnCode> resultPromise;
618 auto resultFuture = resultPromise.get_future();
619
620 dev->generateKey(std::move(lockedEntry), params.getParameters(), entropy, flags,
621 [&, uid](KeyStoreServiceReturnCode rc, KeyCharacteristics keyCharacteristics) {
622 if (outCharacteristics && rc.isOk()) {
623 *outCharacteristics = android::security::keymaster::KeyCharacteristics(
624 keyCharacteristics);
625 }
626 if (__android_log_security()) {
627 android_log_event_list(SEC_TAG_AUTH_KEY_GENERATED)
628 << rc.isOk() << String8(name) << int32_t(uid) << LOG_ID_SECURITY;
629 }
630 resultPromise.set_value(rc);
631 });
632
633 resultFuture.wait();
634 *aidl_return = int32_t(resultFuture.get());
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700635 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700636}
637
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700638Status KeyStoreService::getKeyCharacteristics(
639 const String16& name, const ::android::security::keymaster::KeymasterBlob& clientId,
Janis Danisevskis64ec1fe2018-02-26 16:47:21 -0800640 const ::android::security::keymaster::KeymasterBlob& appData, int32_t uid,
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700641 ::android::security::keymaster::KeyCharacteristics* outCharacteristics, int32_t* aidl_return) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700642 if (!outCharacteristics) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700643 *aidl_return =
644 static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::UNEXPECTED_NULL_POINTER));
645 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700646 }
647
648 uid_t targetUid = getEffectiveUid(uid);
649 uid_t callingUid = IPCThreadState::self()->getCallingUid();
650 if (!is_granted_to(callingUid, targetUid)) {
651 ALOGW("uid %d not permitted to act for uid %d in getKeyCharacteristics", callingUid,
652 targetUid);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700653 *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED);
654 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700655 }
656
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700657 String8 name8(name);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700658
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700659 ResponseCode rc;
660 Blob keyBlob;
661 Blob charBlob;
662 LockedKeyBlobEntry lockedEntry;
Janis Danisevskisd714a672017-09-01 14:31:36 -0700663
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700664 std::tie(rc, keyBlob, charBlob, lockedEntry) =
665 mKeyStore->getKeyForName(name8, targetUid, TYPE_KEYMASTER_10);
666
667 if (rc != ResponseCode::NO_ERROR) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700668 *aidl_return = static_cast<int32_t>(rc);
669 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700670 }
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100671
Janis Danisevskisc1460142017-12-18 16:48:46 -0800672 auto dev = mKeyStore->getDevice(keyBlob);
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700673 if (!dev) {
674 *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700675 return Status::ok();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100676 }
677
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700678 // If the charBlob is up to date, it simply moves the argument blobs to the returned blobs
679 // and extracts the characteristics on the way. Otherwise it updates the cache file with data
680 // from keymaster. It may also upgrade the key blob.
681 std::promise<KeyStoreServiceReturnCode> resultPromise;
682 auto resultFuture = resultPromise.get_future();
Shawn Willden715d0232016-01-21 00:45:13 -0700683
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700684 dev->getKeyCharacteristics(
685 std::move(lockedEntry), clientId.getData(), appData.getData(), std::move(keyBlob),
686 std::move(charBlob),
687 [&](KeyStoreServiceReturnCode rc, KeyCharacteristics keyCharacteristics) {
688 if (outCharacteristics && rc.isOk()) {
689 *outCharacteristics = std::move(keyCharacteristics);
690 }
691 resultPromise.set_value(rc);
692 });
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100693
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700694 resultFuture.wait();
695 *aidl_return = int32_t(resultFuture.get());
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700696 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700697}
698
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700699Status
700KeyStoreService::importKey(const String16& name, const KeymasterArguments& params, int32_t format,
701 const ::std::vector<uint8_t>& keyData, int uid, int flags,
702 ::android::security::keymaster::KeyCharacteristics* outCharacteristics,
703 int32_t* aidl_return) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700704 uid = getEffectiveUid(uid);
Pavel Grafovff311b42018-01-24 20:34:37 +0000705 auto logOnScopeExit = android::base::make_scope_guard([&] {
706 if (__android_log_security()) {
707 android_log_event_list(SEC_TAG_KEY_IMPORTED)
708 << int32_t(*aidl_return == static_cast<int32_t>(ResponseCode::NO_ERROR))
709 << String8(name) << int32_t(uid) << LOG_ID_SECURITY;
710 }
711 });
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100712 KeyStoreServiceReturnCode rc =
713 checkBinderPermissionAndKeystoreState(P_INSERT, uid, flags & KEYSTORE_FLAG_ENCRYPTED);
714 if (!rc.isOk()) {
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700715 LOG(ERROR) << "permissission denied";
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700716 *aidl_return = static_cast<int32_t>(rc);
717 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700718 }
Rubin Xu67899de2017-04-21 19:15:13 +0100719 if ((flags & KEYSTORE_FLAG_CRITICAL_TO_DEVICE_ENCRYPTION) && get_app_id(uid) != AID_SYSTEM) {
720 ALOGE("Non-system uid %d cannot set FLAG_CRITICAL_TO_DEVICE_ENCRYPTION", uid);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700721 *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED);
722 return Status::ok();
Rubin Xu67899de2017-04-21 19:15:13 +0100723 }
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700724
Janis Danisevskisc1460142017-12-18 16:48:46 -0800725 SecurityLevel securityLevel = flagsToSecurityLevel(flags);
726 auto dev = mKeyStore->getDevice(securityLevel);
727 if (!dev) {
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700728 LOG(ERROR) << "importKey - cound not get keymaster device";
Janis Danisevskisc1460142017-12-18 16:48:46 -0800729 *aidl_return = static_cast<int32_t>(ErrorCode::HARDWARE_TYPE_UNAVAILABLE);
730 return Status::ok();
731 }
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700732
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700733 String8 name8(name);
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700734 auto lockedEntry = mKeyStore->getLockedBlobEntryIfNotExists(name8.string(), uid);
735 if (!lockedEntry) {
736 LOG(ERROR) << "importKey - key: " << name8.string() << " " << int(uid)
737 << " already exists.";
738 *aidl_return = static_cast<int32_t>(ResponseCode::KEY_ALREADY_EXISTS);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700739 return Status::ok();
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400740 }
Janis Danisevskis4a1da2f2018-03-26 15:02:38 -0700741
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700742 logOnScopeExit.Disable();
Janis Danisevskis4a1da2f2018-03-26 15:02:38 -0700743
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700744 std::promise<KeyStoreServiceReturnCode> resultPromise;
745 auto resultFuture = resultPromise.get_future();
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400746
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700747 dev->importKey(std::move(lockedEntry), params.getParameters(), KeyFormat(format), keyData,
748 flags,
749 [&, uid](KeyStoreServiceReturnCode rc, KeyCharacteristics keyCharacteristics) {
750 if (outCharacteristics && rc.isOk()) {
751 *outCharacteristics = std::move(keyCharacteristics);
752 }
753 if (__android_log_security()) {
754 android_log_event_list(SEC_TAG_KEY_IMPORTED)
755 << rc.isOk() << String8(name) << int32_t(uid) << LOG_ID_SECURITY;
756 }
757 resultPromise.set_value(rc);
758 });
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400759
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700760 resultFuture.wait();
761 *aidl_return = int32_t(resultFuture.get());
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700762 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700763}
764
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700765Status KeyStoreService::exportKey(const String16& name, int32_t format,
766 const ::android::security::keymaster::KeymasterBlob& clientId,
Janis Danisevskis64ec1fe2018-02-26 16:47:21 -0800767 const ::android::security::keymaster::KeymasterBlob& appData,
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700768 int32_t uid, ExportResult* result) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700769
770 uid_t targetUid = getEffectiveUid(uid);
771 uid_t callingUid = IPCThreadState::self()->getCallingUid();
772 if (!is_granted_to(callingUid, targetUid)) {
773 ALOGW("uid %d not permitted to act for uid %d in exportKey", callingUid, targetUid);
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100774 result->resultCode = ResponseCode::PERMISSION_DENIED;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700775 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700776 }
777
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700778 String8 name8(name);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700779
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700780 KeyStoreServiceReturnCode rc;
781 Blob keyBlob;
782 Blob charBlob;
783 LockedKeyBlobEntry lockedEntry;
784
785 std::tie(rc, keyBlob, charBlob, lockedEntry) =
786 mKeyStore->getKeyForName(name8, targetUid, TYPE_KEYMASTER_10);
787 if (!rc) {
788 result->resultCode = rc;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700789 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700790 }
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100791
Janis Danisevskisc1460142017-12-18 16:48:46 -0800792 auto dev = mKeyStore->getDevice(keyBlob);
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700793 std::promise<void> resultPromise;
794 auto resultFuture = resultPromise.get_future();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100795
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700796 dev->exportKey(std::move(lockedEntry), KeyFormat(format), clientId.getData(), appData.getData(),
797 std::move(keyBlob), std::move(charBlob), [&](ExportResult exportResult) {
798 *result = std::move(exportResult);
799 resultPromise.set_value();
800 });
Ji Wang2c142312016-10-14 17:21:10 +0800801
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700802 resultFuture.wait();
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700803 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700804}
805
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700806Status KeyStoreService::begin(const sp<IBinder>& appToken, const String16& name, int32_t purpose,
807 bool pruneable, const KeymasterArguments& params,
808 const ::std::vector<uint8_t>& entropy, int32_t uid,
809 OperationResult* result) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700810 uid_t callingUid = IPCThreadState::self()->getCallingUid();
811 uid_t targetUid = getEffectiveUid(uid);
812 if (!is_granted_to(callingUid, targetUid)) {
813 ALOGW("uid %d not permitted to act for uid %d in begin", callingUid, targetUid);
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100814 result->resultCode = ResponseCode::PERMISSION_DENIED;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700815 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700816 }
817 if (!pruneable && get_app_id(callingUid) != AID_SYSTEM) {
818 ALOGE("Non-system uid %d trying to start non-pruneable operation", callingUid);
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100819 result->resultCode = ResponseCode::PERMISSION_DENIED;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700820 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700821 }
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700822 if (!checkAllowedOperationParams(params.getParameters())) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100823 result->resultCode = ErrorCode::INVALID_ARGUMENT;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700824 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700825 }
Shawn Willden0329a822017-12-04 13:55:14 -0700826
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700827 String8 name8(name);
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700828 Blob keyBlob;
829 Blob charBlob;
830 LockedKeyBlobEntry lockedEntry;
831 ResponseCode rc;
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100832
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700833 std::tie(rc, keyBlob, charBlob, lockedEntry) =
834 mKeyStore->getKeyForName(name8, targetUid, TYPE_KEYMASTER_10);
835
836 if (rc == ResponseCode::LOCKED && keyBlob.isSuperEncrypted()) {
837 return result->resultCode = ErrorCode::KEY_USER_NOT_AUTHENTICATED, Status::ok();
838 }
839 if (rc != ResponseCode::NO_ERROR) return result->resultCode = rc, Status::ok();
840
Janis Danisevskisc1460142017-12-18 16:48:46 -0800841 auto dev = mKeyStore->getDevice(keyBlob);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700842 AuthorizationSet opParams = params.getParameters();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100843 KeyCharacteristics characteristics;
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100844
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700845 std::promise<void> resultPromise;
846 auto resultFuture = resultPromise.get_future();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100847
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700848 dev->begin(std::move(lockedEntry), appToken, std::move(keyBlob), std::move(charBlob), pruneable,
849 static_cast<KeyPurpose>(purpose), std::move(opParams), entropy,
850 [&, this](OperationResult result_) {
851 if (result_.resultCode.isOk() ||
852 result_.resultCode == ResponseCode::OP_AUTH_NEEDED) {
853 addOperationDevice(result_.token, dev);
854 }
855 if (result) *result = std::move(result_);
856 resultPromise.set_value();
857 });
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -0400858
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700859 resultFuture.wait();
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700860 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700861}
862
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700863Status KeyStoreService::update(const sp<IBinder>& token, const KeymasterArguments& params,
864 const ::std::vector<uint8_t>& data, OperationResult* result) {
865 if (!checkAllowedOperationParams(params.getParameters())) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100866 result->resultCode = ErrorCode::INVALID_ARGUMENT;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700867 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700868 }
Shawn Willdenda6dcc32017-12-03 14:56:05 -0700869
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700870 std::promise<void> resultPromise;
871 auto resultFuture = resultPromise.get_future();
872
873 auto dev = getOperationDevice(token);
874 if (!dev) {
875 *result = operationFailed(ErrorCode::INVALID_OPERATION_HANDLE);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700876 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700877 }
Shawn Willdenda6dcc32017-12-03 14:56:05 -0700878
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700879 dev->update(token, params.getParameters(), data, [&](OperationResult result_) {
880 if (!result_.resultCode.isOk()) {
881 removeOperationDevice(token);
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100882 }
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700883 if (result) *result = std::move(result_);
884 resultPromise.set_value();
885 });
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100886
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700887 resultFuture.wait();
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700888 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700889}
890
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700891Status KeyStoreService::finish(const sp<IBinder>& token, const KeymasterArguments& params,
892 const ::std::vector<uint8_t>& signature,
893 const ::std::vector<uint8_t>& entropy, OperationResult* result) {
894 if (!checkAllowedOperationParams(params.getParameters())) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100895 result->resultCode = ErrorCode::INVALID_ARGUMENT;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700896 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700897 }
Shawn Willdenda6dcc32017-12-03 14:56:05 -0700898
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700899 std::promise<void> resultPromise;
900 auto resultFuture = resultPromise.get_future();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100901
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700902 auto dev = getOperationDevice(token);
903 if (!dev) {
904 *result = operationFailed(ErrorCode::INVALID_OPERATION_HANDLE);
905 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700906 }
907
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700908 dev->finish(token, params.getParameters(), {}, signature, entropy,
909 [&](OperationResult result_) {
910 if (!result_.resultCode.isOk()) {
911 removeOperationDevice(token);
912 }
913 if (result) *result = std::move(result_);
914 resultPromise.set_value();
915 });
Shawn Willdenda6dcc32017-12-03 14:56:05 -0700916
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700917 resultFuture.wait();
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700918 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700919}
920
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700921Status KeyStoreService::abort(const sp<IBinder>& token, int32_t* aidl_return) {
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700922 auto dev = getOperationDevice(token);
923 if (!dev) {
Shawn Willdenda6dcc32017-12-03 14:56:05 -0700924 *aidl_return = static_cast<int32_t>(ErrorCode::INVALID_OPERATION_HANDLE);
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700925 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700926 }
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700927 std::promise<KeyStoreServiceReturnCode> resultPromise;
928 auto resultFuture = resultPromise.get_future();
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100929
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700930 dev->abort(token, [&](KeyStoreServiceReturnCode rc) { resultPromise.set_value(rc); });
931
932 resultFuture.wait();
933 *aidl_return = int32_t(resultFuture.get());
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700934 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700935}
936
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700937Status KeyStoreService::addAuthToken(const ::std::vector<uint8_t>& authTokenAsVector,
Brian Youngccb492d2018-02-22 23:36:01 +0000938 int32_t* aidl_return) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700939
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000940 // TODO(swillden): When gatekeeper and fingerprint are ready, this should be updated to
941 // receive a HardwareAuthToken, rather than an opaque byte array.
942
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700943 if (!checkBinderPermission(P_ADD_AUTH)) {
944 ALOGW("addAuthToken: permission denied for %d", IPCThreadState::self()->getCallingUid());
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700945 *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED);
946 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700947 }
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700948 if (authTokenAsVector.size() != sizeof(hw_auth_token_t)) {
949 *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT));
950 return Status::ok();
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000951 }
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100952
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000953 hw_auth_token_t authToken;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700954 memcpy(reinterpret_cast<void*>(&authToken), authTokenAsVector.data(), sizeof(hw_auth_token_t));
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000955 if (authToken.version != 0) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700956 *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT));
957 return Status::ok();
Shawn Willdend3ed3a22017-03-28 00:39:16 +0000958 }
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100959
Janis Danisevskisff3d7f42018-10-08 07:15:09 -0700960 mKeyStore->getAuthTokenTable().AddAuthenticationToken(
961 hidlVec2AuthToken(hidl_vec<uint8_t>(authTokenAsVector)));
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700962 *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR);
963 return Status::ok();
Shawn Willdenc1d1fee2016-01-26 22:44:56 -0700964}
965
Rubin Xu1a203e32018-05-08 14:25:21 +0100966int isDeviceIdAttestationRequested(const KeymasterArguments& params) {
Chih-Hung Hsiehb81e68b2018-07-13 11:37:45 -0700967 const hardware::hidl_vec<KeyParameter>& paramsVec = params.getParameters();
Rubin Xu1a203e32018-05-08 14:25:21 +0100968 int result = 0;
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700969 for (size_t i = 0; i < paramsVec.size(); ++i) {
970 switch (paramsVec[i].tag) {
Shawn Willdene2a7b522017-04-11 09:27:40 -0600971 case Tag::ATTESTATION_ID_BRAND:
972 case Tag::ATTESTATION_ID_DEVICE:
Shawn Willdene2a7b522017-04-11 09:27:40 -0600973 case Tag::ATTESTATION_ID_MANUFACTURER:
Shawn Willdene2a7b522017-04-11 09:27:40 -0600974 case Tag::ATTESTATION_ID_MODEL:
975 case Tag::ATTESTATION_ID_PRODUCT:
Rubin Xu1a203e32018-05-08 14:25:21 +0100976 result |= ID_ATTESTATION_REQUEST_GENERIC_INFO;
Shawn Willdene2a7b522017-04-11 09:27:40 -0600977 break;
Rubin Xu1a203e32018-05-08 14:25:21 +0100978 case Tag::ATTESTATION_ID_IMEI:
979 case Tag::ATTESTATION_ID_MEID:
980 case Tag::ATTESTATION_ID_SERIAL:
981 result |= ID_ATTESTATION_REQUEST_UNIQUE_DEVICE_ID;
982 break;
983 default:
984 continue;
Bartosz Fabianowskia9452d92017-01-23 22:21:11 +0100985 }
986 }
Rubin Xu1a203e32018-05-08 14:25:21 +0100987 return result;
Bartosz Fabianowskia9452d92017-01-23 22:21:11 +0100988}
989
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -0700990Status KeyStoreService::attestKey(const String16& name, const KeymasterArguments& params,
991 ::android::security::keymaster::KeymasterCertificateChain* chain,
992 int32_t* aidl_return) {
993 // check null output if method signature is updated and return ErrorCode::OUTPUT_PARAMETER_NULL
994 if (!checkAllowedOperationParams(params.getParameters())) {
995 *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT));
996 return Status::ok();
Shawn Willden50eb1b22016-01-21 12:41:23 -0700997 }
998
Eran Messerie2c34152017-12-21 21:01:22 +0000999 uid_t callingUid = IPCThreadState::self()->getCallingUid();
1000
Rubin Xu1a203e32018-05-08 14:25:21 +01001001 int needsIdAttestation = isDeviceIdAttestationRequested(params);
1002 bool needsUniqueIdAttestation = needsIdAttestation & ID_ATTESTATION_REQUEST_UNIQUE_DEVICE_ID;
1003 bool isPrimaryUserSystemUid = (callingUid == AID_SYSTEM);
1004 bool isSomeUserSystemUid = (get_app_id(callingUid) == AID_SYSTEM);
1005 // Allow system context from any user to request attestation with basic device information,
1006 // while only allow system context from user 0 (device owner) to request attestation with
1007 // unique device ID.
1008 if ((needsIdAttestation && !isSomeUserSystemUid) ||
1009 (needsUniqueIdAttestation && !isPrimaryUserSystemUid)) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -07001010 *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT));
1011 return Status::ok();
Bartosz Fabianowskia9452d92017-01-23 22:21:11 +01001012 }
1013
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -07001014 AuthorizationSet mutableParams = params.getParameters();
Bartosz Fabianowski5aa93e02017-04-24 13:54:49 +02001015 KeyStoreServiceReturnCode rc = updateParamsForAttestation(callingUid, &mutableParams);
1016 if (!rc.isOk()) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -07001017 *aidl_return = static_cast<int32_t>(rc);
1018 return Status::ok();
Bartosz Fabianowski5aa93e02017-04-24 13:54:49 +02001019 }
Shawn Willdene2a7b522017-04-11 09:27:40 -06001020
Shawn Willden50eb1b22016-01-21 12:41:23 -07001021 String8 name8(name);
Janis Danisevskisff3d7f42018-10-08 07:15:09 -07001022 Blob keyBlob;
1023 Blob charBlob;
1024 LockedKeyBlobEntry lockedEntry;
Shawn Willden50eb1b22016-01-21 12:41:23 -07001025
Janis Danisevskisff3d7f42018-10-08 07:15:09 -07001026 std::tie(rc, keyBlob, charBlob, lockedEntry) =
1027 mKeyStore->getKeyForName(name8, callingUid, TYPE_KEYMASTER_10);
1028
1029 std::promise<KeyStoreServiceReturnCode> resultPromise;
1030 auto resultFuture = resultPromise.get_future();
1031
1032 auto worker_cb = [&](Return<void> rc,
1033 std::tuple<ErrorCode, hidl_vec<hidl_vec<uint8_t>>>&& hidlResult) {
1034 auto& [ret, certChain] = hidlResult;
1035 if (!rc.isOk()) {
1036 resultPromise.set_value(ResponseCode::SYSTEM_ERROR);
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +01001037 return;
1038 }
Janis Danisevskisff3d7f42018-10-08 07:15:09 -07001039 if (ret == ErrorCode::OK && chain) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -07001040 *chain = KeymasterCertificateChain(certChain);
1041 }
Janis Danisevskisff3d7f42018-10-08 07:15:09 -07001042 resultPromise.set_value(ret);
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +01001043 };
Janis Danisevskisc1460142017-12-18 16:48:46 -08001044 auto dev = mKeyStore->getDevice(keyBlob);
Janis Danisevskisff3d7f42018-10-08 07:15:09 -07001045 auto hidlKey = blob2hidlVec(keyBlob);
1046 dev->attestKey(std::move(hidlKey), mutableParams.hidl_data(), worker_cb);
1047
1048 resultFuture.wait();
1049 *aidl_return = static_cast<int32_t>(resultFuture.get());
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -07001050 return Status::ok();
Bartosz Fabianowski5aa93e02017-04-24 13:54:49 +02001051}
1052
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -07001053Status
1054KeyStoreService::attestDeviceIds(const KeymasterArguments& params,
1055 ::android::security::keymaster::KeymasterCertificateChain* chain,
1056 int32_t* aidl_return) {
1057 // check null output if method signature is updated and return ErrorCode::OUTPUT_PARAMETER_NULL
Bartosz Fabianowski5aa93e02017-04-24 13:54:49 +02001058
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -07001059 if (!checkAllowedOperationParams(params.getParameters())) {
1060 *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT));
1061 return Status::ok();
Bartosz Fabianowski5aa93e02017-04-24 13:54:49 +02001062 }
1063
1064 if (!isDeviceIdAttestationRequested(params)) {
1065 // There is an attestKey() method for attesting keys without device ID attestation.
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -07001066 *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT));
1067 return Status::ok();
Bartosz Fabianowski5aa93e02017-04-24 13:54:49 +02001068 }
1069
1070 uid_t callingUid = IPCThreadState::self()->getCallingUid();
1071 sp<IBinder> binder = defaultServiceManager()->getService(String16("permission"));
Yi Konge353f252018-07-30 01:38:39 -07001072 if (binder == nullptr) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -07001073 *aidl_return =
1074 static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::CANNOT_ATTEST_IDS));
1075 return Status::ok();
Bartosz Fabianowski5aa93e02017-04-24 13:54:49 +02001076 }
1077 if (!interface_cast<IPermissionController>(binder)->checkPermission(
1078 String16("android.permission.READ_PRIVILEGED_PHONE_STATE"),
1079 IPCThreadState::self()->getCallingPid(), callingUid)) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -07001080 *aidl_return =
1081 static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::CANNOT_ATTEST_IDS));
1082 return Status::ok();
Bartosz Fabianowski5aa93e02017-04-24 13:54:49 +02001083 }
1084
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -07001085 AuthorizationSet mutableParams = params.getParameters();
Bartosz Fabianowski5aa93e02017-04-24 13:54:49 +02001086 KeyStoreServiceReturnCode rc = updateParamsForAttestation(callingUid, &mutableParams);
1087 if (!rc.isOk()) {
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -07001088 *aidl_return = static_cast<int32_t>(rc);
1089 return Status::ok();
Bartosz Fabianowski5aa93e02017-04-24 13:54:49 +02001090 }
1091
1092 // Generate temporary key.
Janis Danisevskisff3d7f42018-10-08 07:15:09 -07001093 auto dev = mKeyStore->getDevice(SecurityLevel::TRUSTED_ENVIRONMENT);
Janis Danisevskisc1460142017-12-18 16:48:46 -08001094
Shawn Willden70c1a782018-07-11 15:13:20 -06001095 if (!dev) {
Janis Danisevskisc1460142017-12-18 16:48:46 -08001096 *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR);
1097 return Status::ok();
1098 }
1099
Bartosz Fabianowski5aa93e02017-04-24 13:54:49 +02001100
1101 AuthorizationSet keyCharacteristics;
1102 keyCharacteristics.push_back(TAG_PURPOSE, KeyPurpose::VERIFY);
1103 keyCharacteristics.push_back(TAG_ALGORITHM, Algorithm::EC);
1104 keyCharacteristics.push_back(TAG_DIGEST, Digest::SHA_2_256);
1105 keyCharacteristics.push_back(TAG_NO_AUTH_REQUIRED);
1106 keyCharacteristics.push_back(TAG_EC_CURVE, EcCurve::P_256);
Bartosz Fabianowski5aa93e02017-04-24 13:54:49 +02001107
Janis Danisevskisff3d7f42018-10-08 07:15:09 -07001108 std::promise<KeyStoreServiceReturnCode> resultPromise;
1109 auto resultFuture = resultPromise.get_future();
Bartosz Fabianowski5aa93e02017-04-24 13:54:49 +02001110
Janis Danisevskisff3d7f42018-10-08 07:15:09 -07001111 dev->generateKey(
1112 keyCharacteristics.hidl_data(),
1113 [&, dev](Return<void> rc,
1114 std::tuple<ErrorCode, ::std::vector<uint8_t>, KeyCharacteristics>&& hidlResult) {
1115 auto& [ret, hidlKeyBlob_, dummyCharacteristics] = hidlResult;
1116 auto hidlKeyBlob = std::move(hidlKeyBlob_);
1117 if (!rc.isOk()) {
1118 resultPromise.set_value(ResponseCode::SYSTEM_ERROR);
1119 return;
1120 }
1121 if (ret != ErrorCode::OK) {
1122 resultPromise.set_value(ret);
1123 return;
1124 }
1125 dev->attestKey(
1126 hidlKeyBlob, mutableParams.hidl_data(),
1127 [&, dev,
1128 hidlKeyBlob](Return<void> rc,
1129 std::tuple<ErrorCode, hidl_vec<hidl_vec<uint8_t>>>&& hidlResult) {
1130 auto& [ret, certChain] = hidlResult;
1131 // shedule temp key for deletion
1132 dev->deleteKey(std::move(hidlKeyBlob), [](Return<ErrorCode> rc) {
1133 // log error but don't return an error
1134 KS_HANDLE_HIDL_ERROR(rc);
1135 });
1136 if (!rc.isOk()) {
1137 resultPromise.set_value(ResponseCode::SYSTEM_ERROR);
1138 return;
1139 }
1140 if (ret == ErrorCode::OK && chain) {
1141 *chain =
1142 ::android::security::keymaster::KeymasterCertificateChain(certChain);
1143 }
1144 resultPromise.set_value(ret);
1145 });
1146 });
Bartosz Fabianowski5aa93e02017-04-24 13:54:49 +02001147
Janis Danisevskisff3d7f42018-10-08 07:15:09 -07001148 resultFuture.wait();
1149 *aidl_return = static_cast<int32_t>(resultFuture.get());
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -07001150 return Status::ok();
Shawn Willden50eb1b22016-01-21 12:41:23 -07001151}
1152
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -07001153Status KeyStoreService::onDeviceOffBody(int32_t* aidl_return) {
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -04001154 // TODO(tuckeris): add permission check. This should be callable from ClockworkHome only.
Janis Danisevskisff3d7f42018-10-08 07:15:09 -07001155 mKeyStore->getAuthTokenTable().onDeviceOffBody();
Dmitry Dementyeva447b3c2017-10-27 23:09:53 -07001156 *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR);
1157 return Status::ok();
Tucker Sylvestro0ab28b72016-08-05 18:02:47 -04001158}
1159
Janis Danisevskiscb9267d2017-12-19 16:27:52 -08001160#define AIDL_RETURN(rc) \
1161 (*_aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(rc)), Status::ok())
1162
1163Status KeyStoreService::importWrappedKey(
1164 const ::android::String16& wrappedKeyAlias, const ::std::vector<uint8_t>& wrappedKey,
1165 const ::android::String16& wrappingKeyAlias, const ::std::vector<uint8_t>& maskingKey,
1166 const KeymasterArguments& params, int64_t rootSid, int64_t fingerprintSid,
1167 ::android::security::keymaster::KeyCharacteristics* outCharacteristics, int32_t* _aidl_return) {
1168
1169 uid_t callingUid = IPCThreadState::self()->getCallingUid();
1170
1171 if (!checkBinderPermission(P_INSERT, callingUid)) {
1172 return AIDL_RETURN(ResponseCode::PERMISSION_DENIED);
1173 }
1174
Janis Danisevskiscb9267d2017-12-19 16:27:52 -08001175 String8 wrappingKeyName8(wrappingKeyAlias);
Janis Danisevskisff3d7f42018-10-08 07:15:09 -07001176
1177 KeyStoreServiceReturnCode rc;
1178 Blob wrappingKeyBlob;
1179 Blob wrappingCharBlob;
1180 LockedKeyBlobEntry wrappingLockedEntry;
1181
1182 std::tie(rc, wrappingKeyBlob, wrappingCharBlob, wrappingLockedEntry) =
1183 mKeyStore->getKeyForName(wrappingKeyName8, callingUid, TYPE_KEYMASTER_10);
1184 if (!rc) {
Janis Danisevskiscb9267d2017-12-19 16:27:52 -08001185 return AIDL_RETURN(rc);
1186 }
1187
Janis Danisevskisff3d7f42018-10-08 07:15:09 -07001188 String8 wrappedKeyName8(wrappedKeyAlias);
1189 auto wrappedLockedEntry =
1190 mKeyStore->getLockedBlobEntryIfNotExists(wrappedKeyName8.string(), callingUid);
1191 if (!wrappedLockedEntry) {
1192 return AIDL_RETURN(ResponseCode::KEY_ALREADY_EXISTS);
1193 }
1194
Janis Danisevskiscb9267d2017-12-19 16:27:52 -08001195 SecurityLevel securityLevel = wrappingKeyBlob.getSecurityLevel();
1196 auto dev = mKeyStore->getDevice(securityLevel);
1197 if (!dev) {
1198 return AIDL_RETURN(ErrorCode::HARDWARE_TYPE_UNAVAILABLE);
1199 }
1200
Janis Danisevskisff3d7f42018-10-08 07:15:09 -07001201 std::promise<KeyStoreServiceReturnCode> resultPromise;
1202 auto resultFuture = resultPromise.get_future();
Janis Danisevskiscb9267d2017-12-19 16:27:52 -08001203
Janis Danisevskisff3d7f42018-10-08 07:15:09 -07001204 dev->importWrappedKey(
1205 std::move(wrappingLockedEntry), std::move(wrappedLockedEntry), wrappedKey, maskingKey,
1206 params.getParameters(), std::move(wrappingKeyBlob), std::move(wrappingCharBlob), rootSid,
1207 fingerprintSid, [&](KeyStoreServiceReturnCode rc, KeyCharacteristics keyCharacteristics) {
1208 if (rc.isOk() && outCharacteristics) {
1209 *outCharacteristics =
1210 ::android::security::keymaster::KeyCharacteristics(keyCharacteristics);
1211 }
1212 resultPromise.set_value(rc);
1213 });
Janis Danisevskiscb9267d2017-12-19 16:27:52 -08001214
Janis Danisevskisff3d7f42018-10-08 07:15:09 -07001215 resultFuture.wait();
1216 return AIDL_RETURN(resultFuture.get());
Janis Danisevskiscb9267d2017-12-19 16:27:52 -08001217}
1218
David Zeuthenc6eb7cd2017-11-27 11:33:55 -05001219Status KeyStoreService::presentConfirmationPrompt(const sp<IBinder>& listener,
1220 const String16& promptText,
1221 const ::std::vector<uint8_t>& extraData,
1222 const String16& locale, int32_t uiOptionsAsFlags,
1223 int32_t* aidl_return) {
Janis Danisevskisff3d7f42018-10-08 07:15:09 -07001224 return mKeyStore->getConfirmationManager().presentConfirmationPrompt(
1225 listener, promptText, extraData, locale, uiOptionsAsFlags, aidl_return);
David Zeuthenc6eb7cd2017-11-27 11:33:55 -05001226}
1227
1228Status KeyStoreService::cancelConfirmationPrompt(const sp<IBinder>& listener,
1229 int32_t* aidl_return) {
Janis Danisevskisff3d7f42018-10-08 07:15:09 -07001230 return mKeyStore->getConfirmationManager().cancelConfirmationPrompt(listener, aidl_return);
David Zeuthenc6eb7cd2017-11-27 11:33:55 -05001231}
1232
David Zeuthen1a492312018-02-26 11:00:30 -05001233Status KeyStoreService::isConfirmationPromptSupported(bool* aidl_return) {
Janis Danisevskisff3d7f42018-10-08 07:15:09 -07001234 return mKeyStore->getConfirmationManager().isConfirmationPromptSupported(aidl_return);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -07001235}
1236
1237/**
1238 * Get the effective target uid for a binder operation that takes an
1239 * optional uid as the target.
1240 */
1241uid_t KeyStoreService::getEffectiveUid(int32_t targetUid) {
1242 if (targetUid == UID_SELF) {
1243 return IPCThreadState::self()->getCallingUid();
1244 }
1245 return static_cast<uid_t>(targetUid);
1246}
1247
1248/**
1249 * Check if the caller of the current binder method has the required
1250 * permission and if acting on other uids the grants to do so.
1251 */
1252bool KeyStoreService::checkBinderPermission(perm_t permission, int32_t targetUid) {
1253 uid_t callingUid = IPCThreadState::self()->getCallingUid();
1254 pid_t spid = IPCThreadState::self()->getCallingPid();
1255 if (!has_permission(callingUid, permission, spid)) {
1256 ALOGW("permission %s denied for %d", get_perm_label(permission), callingUid);
1257 return false;
1258 }
1259 if (!is_granted_to(callingUid, getEffectiveUid(targetUid))) {
1260 ALOGW("uid %d not granted to act for %d", callingUid, targetUid);
1261 return false;
1262 }
1263 return true;
1264}
1265
1266/**
1267 * Check if the caller of the current binder method has the required
1268 * permission and the target uid is the caller or the caller is system.
1269 */
1270bool KeyStoreService::checkBinderPermissionSelfOrSystem(perm_t permission, int32_t targetUid) {
1271 uid_t callingUid = IPCThreadState::self()->getCallingUid();
1272 pid_t spid = IPCThreadState::self()->getCallingPid();
1273 if (!has_permission(callingUid, permission, spid)) {
1274 ALOGW("permission %s denied for %d", get_perm_label(permission), callingUid);
1275 return false;
1276 }
1277 return getEffectiveUid(targetUid) == callingUid || callingUid == AID_SYSTEM;
1278}
1279
1280/**
1281 * Check if the caller of the current binder method has the required
1282 * permission or the target of the operation is the caller's uid. This is
1283 * for operation where the permission is only for cross-uid activity and all
1284 * uids are allowed to act on their own (ie: clearing all entries for a
1285 * given uid).
1286 */
1287bool KeyStoreService::checkBinderPermissionOrSelfTarget(perm_t permission, int32_t targetUid) {
1288 uid_t callingUid = IPCThreadState::self()->getCallingUid();
1289 if (getEffectiveUid(targetUid) == callingUid) {
1290 return true;
1291 } else {
1292 return checkBinderPermission(permission, targetUid);
1293 }
1294}
1295
1296/**
1297 * Helper method to check that the caller has the required permission as
1298 * well as the keystore is in the unlocked state if checkUnlocked is true.
1299 *
1300 * Returns NO_ERROR on success, PERMISSION_DENIED on a permission error and
1301 * otherwise the state of keystore when not unlocked and checkUnlocked is
1302 * true.
1303 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +01001304KeyStoreServiceReturnCode
1305KeyStoreService::checkBinderPermissionAndKeystoreState(perm_t permission, int32_t targetUid,
1306 bool checkUnlocked) {
Shawn Willdenc1d1fee2016-01-26 22:44:56 -07001307 if (!checkBinderPermission(permission, targetUid)) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +01001308 return ResponseCode::PERMISSION_DENIED;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -07001309 }
1310 State state = mKeyStore->getState(get_user_id(getEffectiveUid(targetUid)));
1311 if (checkUnlocked && !isKeystoreUnlocked(state)) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +01001312 // All State values coincide with ResponseCodes
1313 return static_cast<ResponseCode>(state);
Shawn Willdenc1d1fee2016-01-26 22:44:56 -07001314 }
1315
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +01001316 return ResponseCode::NO_ERROR;
Shawn Willdenc1d1fee2016-01-26 22:44:56 -07001317}
1318
1319bool KeyStoreService::isKeystoreUnlocked(State state) {
1320 switch (state) {
1321 case ::STATE_NO_ERROR:
1322 return true;
1323 case ::STATE_UNINITIALIZED:
1324 case ::STATE_LOCKED:
1325 return false;
1326 }
1327 return false;
1328}
1329
Shawn Willdenc1d1fee2016-01-26 22:44:56 -07001330/**
Shawn Willden0329a822017-12-04 13:55:14 -07001331 * Check that all KeyParameters provided by the application are allowed. Any parameter that keystore
1332 * adds itself should be disallowed here.
Shawn Willdenc1d1fee2016-01-26 22:44:56 -07001333 */
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +01001334bool KeyStoreService::checkAllowedOperationParams(const hidl_vec<KeyParameter>& params) {
1335 for (size_t i = 0; i < params.size(); ++i) {
1336 switch (params[i].tag) {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +01001337 case Tag::ATTESTATION_APPLICATION_ID:
Shawn Willdene2a7b522017-04-11 09:27:40 -06001338 case Tag::RESET_SINCE_ID_ROTATION:
Shawn Willdenc1d1fee2016-01-26 22:44:56 -07001339 return false;
1340 default:
1341 break;
1342 }
1343 }
1344 return true;
1345}
1346
Brian Young9a947d52018-02-23 18:03:14 +00001347Status KeyStoreService::onKeyguardVisibilityChanged(bool isShowing, int32_t userId,
1348 int32_t* aidl_return) {
Janis Danisevskisff3d7f42018-10-08 07:15:09 -07001349 mKeyStore->getEnforcementPolicy().set_device_locked(isShowing, userId);
Brian Young9a947d52018-02-23 18:03:14 +00001350 *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR);
Brian Young9371e952018-02-23 18:03:14 +00001351
1352 return Status::ok();
1353}
1354
Shawn Willdene2a7b522017-04-11 09:27:40 -06001355} // namespace keystore