Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1 | /* |
| 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 Danisevskis | 011675d | 2016-09-01 11:41:29 +0100 | [diff] [blame] | 17 | #define LOG_TAG "keystore" |
| 18 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 19 | #include "key_store_service.h" |
| 20 | |
| 21 | #include <fcntl.h> |
| 22 | #include <sys/stat.h> |
| 23 | |
Janis Danisevskis | 7612fd4 | 2016-09-01 11:50:02 +0100 | [diff] [blame] | 24 | #include <algorithm> |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 25 | #include <atomic> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 26 | #include <sstream> |
| 27 | |
Pavel Grafov | ff311b4 | 2018-01-24 20:34:37 +0000 | [diff] [blame] | 28 | #include <android-base/scopeguard.h> |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 29 | #include <binder/IInterface.h> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 30 | #include <binder/IPCThreadState.h> |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 31 | #include <binder/IPermissionController.h> |
| 32 | #include <binder/IServiceManager.h> |
Brian Claire Young | 3133c45 | 2018-08-31 13:56:49 -0700 | [diff] [blame] | 33 | #include <cutils/multiuser.h> |
Pavel Grafov | ff311b4 | 2018-01-24 20:34:37 +0000 | [diff] [blame] | 34 | #include <log/log_event_list.h> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 35 | |
| 36 | #include <private/android_filesystem_config.h> |
Pavel Grafov | ff311b4 | 2018-01-24 20:34:37 +0000 | [diff] [blame] | 37 | #include <private/android_logger.h> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 38 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 39 | #include <android/hardware/confirmationui/1.0/IConfirmationUI.h> |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 40 | #include <android/hardware/keymaster/3.0/IHwKeymasterDevice.h> |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 41 | #include <keymasterV4_0/keymaster_utils.h> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 42 | |
| 43 | #include "defaults.h" |
Hasini Gunasinghe | 8c8c2d4 | 2020-06-23 17:17:47 +0000 | [diff] [blame] | 44 | #include "key_attestation_log_handler.h" |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 45 | #include "keystore_keymaster_enforcement.h" |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 46 | #include "keystore_utils.h" |
David Zeuthen | f2a2867 | 2020-01-30 16:20:07 -0500 | [diff] [blame] | 47 | #include <keystore/keystore_attestation_id.h> |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 48 | #include <keystore/keystore_hidl_support.h> |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 49 | #include <keystore/keystore_return_types.h> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 50 | |
Janis Danisevskis | 8f737ad | 2017-11-21 12:30:15 -0800 | [diff] [blame] | 51 | #include <hardware/hw_auth_token.h> |
| 52 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 53 | namespace keystore { |
Shawn Willden | d5a24e6 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 54 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 55 | using namespace android; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 56 | |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 57 | namespace { |
| 58 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 59 | using ::android::binder::Status; |
David Zeuthen | 59102f3 | 2020-05-08 10:58:09 -0400 | [diff] [blame] | 60 | using android::hardware::keymaster::V4_0::support::authToken2HidlVec; |
| 61 | using android::hardware::keymaster::V4_0::support::serializeVerificationToken; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 62 | using android::security::keymaster::ExportResult; |
| 63 | using android::security::keymaster::KeymasterArguments; |
| 64 | using android::security::keymaster::KeymasterBlob; |
| 65 | using android::security::keymaster::KeymasterCertificateChain; |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 66 | using android::security::keymaster::operationFailed; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 67 | using android::security::keymaster::OperationResult; |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 68 | using ConfirmationResponseCode = android::hardware::confirmationui::V1_0::ResponseCode; |
David Zeuthen | 59102f3 | 2020-05-08 10:58:09 -0400 | [diff] [blame] | 69 | using ::android::security::keystore::ICredstoreTokenCallback; |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 70 | using ::android::security::keystore::IKeystoreOperationResultCallback; |
| 71 | using ::android::security::keystore::IKeystoreResponseCallback; |
| 72 | using ::android::security::keystore::KeystoreResponse; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 73 | |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 74 | constexpr double kIdRotationPeriod = 30 * 24 * 60 * 60; /* Thirty days, in seconds */ |
| 75 | const char* kTimestampFilePath = "timestamp"; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 76 | |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 77 | bool containsTag(const hidl_vec<KeyParameter>& params, Tag tag) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 78 | return params.end() != |
| 79 | std::find_if(params.begin(), params.end(), |
| 80 | [&](const KeyParameter& param) { return param.tag == tag; }); |
Shawn Willden | d5a24e6 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Branden Archer | 7008074 | 2018-11-20 11:04:11 -0800 | [diff] [blame] | 83 | #define AIDL_RETURN(rc) (*_aidl_return = KeyStoreServiceReturnCode(rc).getErrorCode(), Status::ok()) |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 84 | |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 85 | std::pair<KeyStoreServiceReturnCode, bool> hadFactoryResetSinceIdRotation() { |
| 86 | struct stat sbuf; |
| 87 | if (stat(kTimestampFilePath, &sbuf) == 0) { |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 88 | double diff_secs = difftime(time(nullptr), sbuf.st_ctime); |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 89 | 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 Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 111 | using ::android::security::KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE; |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 112 | |
| 113 | KeyStoreServiceReturnCode 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"); |
Shawn Willden | 6f7d27c | 2019-09-11 22:51:46 -0600 | [diff] [blame] | 124 | // Couldn't get attestation ID; just use an empty one rather than failing. |
| 125 | asn1_attestation_id_result = std::vector<uint8_t>(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 126 | } |
| 127 | std::vector<uint8_t>& asn1_attestation_id = asn1_attestation_id_result; |
| 128 | |
| 129 | /* |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 130 | * The attestation application ID must not be longer than |
| 131 | * KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE, error out if gather_attestation_application_id |
| 132 | * returned such an invalid vector. |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 133 | */ |
| 134 | if (asn1_attestation_id.size() > KEY_ATTESTATION_APPLICATION_ID_MAX_SIZE) { |
Eran Messeri | 03fc4c8 | 2018-08-16 18:53:15 +0100 | [diff] [blame] | 135 | ALOGE("BUG: Gathered Attestation Application ID is too big (%d)", |
| 136 | static_cast<int32_t>(asn1_attestation_id.size())); |
| 137 | return ErrorCode::CANNOT_ATTEST_IDS; |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | params->push_back(TAG_ATTESTATION_APPLICATION_ID, asn1_attestation_id); |
| 141 | |
| 142 | return ResponseCode::NO_ERROR; |
| 143 | } |
| 144 | |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 145 | } // anonymous namespace |
| 146 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 147 | Status KeyStoreService::getState(int32_t userId, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 148 | if (!checkBinderPermission(P_GET_STATE)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 149 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 150 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 151 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 152 | *aidl_return = mKeyStore->getState(userId); |
| 153 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 156 | Status KeyStoreService::get(const String16& name, int32_t uid, ::std::vector<uint8_t>* item) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 157 | uid_t targetUid = getEffectiveUid(uid); |
| 158 | if (!checkBinderPermission(P_GET, targetUid)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 159 | // see keystore/keystore.h |
| 160 | return Status::fromServiceSpecificError( |
| 161 | static_cast<int32_t>(ResponseCode::PERMISSION_DENIED)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | String8 name8(name); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 165 | ResponseCode rc; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 166 | Blob keyBlob; |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 167 | Blob charBlob; |
| 168 | LockedKeyBlobEntry lockedEntry; |
| 169 | |
| 170 | std::tie(rc, keyBlob, charBlob, lockedEntry) = |
| 171 | mKeyStore->getKeyForName(name8, targetUid, TYPE_GENERIC); |
| 172 | if (rc != ResponseCode::NO_ERROR) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 173 | *item = ::std::vector<uint8_t>(); |
| 174 | // Return empty array if key is not found |
| 175 | // TODO: consider having returned value nullable or parse exception on the client. |
| 176 | return Status::fromServiceSpecificError(static_cast<int32_t>(rc)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 177 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 178 | auto resultBlob = blob2hidlVec(keyBlob); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 179 | // The static_cast here is needed to prevent a move, forcing a deep copy. |
| 180 | if (item) *item = static_cast<const hidl_vec<uint8_t>&>(blob2hidlVec(keyBlob)); |
| 181 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 184 | Status KeyStoreService::insert(const String16& name, const ::std::vector<uint8_t>& item, |
| 185 | int targetUid, int32_t flags, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 186 | targetUid = getEffectiveUid(targetUid); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 187 | KeyStoreServiceReturnCode result = |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 188 | checkBinderPermissionAndKeystoreState(P_INSERT, targetUid, flags & KEYSTORE_FLAG_ENCRYPTED); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 189 | if (!result.isOk()) { |
Branden Archer | 7008074 | 2018-11-20 11:04:11 -0800 | [diff] [blame] | 190 | *aidl_return = result.getErrorCode(); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 191 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | String8 name8(name); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 195 | auto lockedEntry = mKeyStore->getLockedBlobEntryIfNotExists(name8.string(), targetUid); |
| 196 | |
| 197 | if (!lockedEntry) { |
| 198 | ALOGE("failed to grab lock on blob entry %u_%s", targetUid, name8.string()); |
| 199 | *aidl_return = static_cast<int32_t>(ResponseCode::KEY_ALREADY_EXISTS); |
| 200 | return Status::ok(); |
| 201 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 202 | |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 203 | Blob keyBlob(&item[0], item.size(), nullptr, 0, ::TYPE_GENERIC); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 204 | keyBlob.setEncrypted(flags & KEYSTORE_FLAG_ENCRYPTED); |
| 205 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 206 | *aidl_return = static_cast<int32_t>(mKeyStore->put(lockedEntry, keyBlob, {})); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 207 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 210 | Status KeyStoreService::del(const String16& name, int targetUid, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 211 | targetUid = getEffectiveUid(targetUid); |
| 212 | if (!checkBinderPermission(P_DELETE, targetUid)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 213 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 214 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 215 | } |
| 216 | String8 name8(name); |
Rubin Xu | 7675c9f | 2017-03-15 19:26:52 +0000 | [diff] [blame] | 217 | ALOGI("del %s %d", name8.string(), targetUid); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 218 | auto lockedEntry = mKeyStore->getLockedBlobEntryIfExists(name8.string(), targetUid); |
| 219 | if (!lockedEntry) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 220 | *aidl_return = static_cast<int32_t>(ResponseCode::KEY_NOT_FOUND); |
| 221 | return Status::ok(); |
| 222 | } |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 223 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 224 | ResponseCode result = mKeyStore->del(lockedEntry); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 225 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 226 | *aidl_return = static_cast<int32_t>(result); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 227 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 230 | Status KeyStoreService::exist(const String16& name, int targetUid, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 231 | targetUid = getEffectiveUid(targetUid); |
| 232 | if (!checkBinderPermission(P_EXIST, targetUid)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 233 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 234 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 237 | LockedKeyBlobEntry lockedEntry = |
| 238 | mKeyStore->getLockedBlobEntryIfExists(String8(name).string(), targetUid); |
| 239 | *aidl_return = |
| 240 | static_cast<int32_t>(lockedEntry ? ResponseCode::NO_ERROR : ResponseCode::KEY_NOT_FOUND); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 241 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 244 | Status KeyStoreService::list(const String16& prefix, int32_t targetUid, |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 245 | ::std::vector<::android::String16>* matches) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 246 | targetUid = getEffectiveUid(targetUid); |
| 247 | if (!checkBinderPermission(P_LIST, targetUid)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 248 | return Status::fromServiceSpecificError( |
| 249 | static_cast<int32_t>(ResponseCode::PERMISSION_DENIED)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 250 | } |
| 251 | const String8 prefix8(prefix); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 252 | const std::string stdPrefix(prefix8.string()); |
| 253 | |
| 254 | ResponseCode rc; |
| 255 | std::list<LockedKeyBlobEntry> internal_matches; |
Janis Danisevskis | 265435f | 2018-11-16 14:10:46 -0800 | [diff] [blame] | 256 | auto userDirName = mKeyStore->getUserStateDB().getUserStateByUid(targetUid)->getUserDirName(); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 257 | |
Janis Danisevskis | 265435f | 2018-11-16 14:10:46 -0800 | [diff] [blame] | 258 | std::tie(rc, internal_matches) = |
| 259 | LockedKeyBlobEntry::list(userDirName, [&](uid_t uid, const std::string& alias) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 260 | std::mismatch(stdPrefix.begin(), stdPrefix.end(), alias.begin(), alias.end()); |
| 261 | return uid == static_cast<uid_t>(targetUid) && |
| 262 | std::mismatch(stdPrefix.begin(), stdPrefix.end(), alias.begin(), alias.end()) |
| 263 | .first == stdPrefix.end(); |
| 264 | }); |
| 265 | |
| 266 | if (rc != ResponseCode::NO_ERROR) { |
| 267 | return Status::fromServiceSpecificError(static_cast<int32_t>(rc)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 268 | } |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 269 | |
| 270 | for (LockedKeyBlobEntry& entry : internal_matches) { |
| 271 | matches->push_back(String16(entry->alias().substr(prefix8.size()).c_str())); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 272 | } |
| 273 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Rob Barnes | eb7f79b | 2018-11-08 15:44:10 -0700 | [diff] [blame] | 276 | /* |
| 277 | * This method will return the uids of all auth bound keys for the calling user. |
| 278 | * This is intended to be used for alerting the user about which apps will be affected |
| 279 | * if the password/pin is removed. Only allowed to be called by system. |
| 280 | * The output is bound by the initial size of uidsOut to be compatible with Java. |
| 281 | */ |
Rob Barnes | 5d59e63 | 2018-12-07 16:09:02 -0700 | [diff] [blame] | 282 | Status KeyStoreService::listUidsOfAuthBoundKeys(std::vector<std::string>* uidsOut, |
Rob Barnes | eb7f79b | 2018-11-08 15:44:10 -0700 | [diff] [blame] | 283 | int32_t* aidl_return) { |
| 284 | const int32_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 285 | const int32_t userId = get_user_id(callingUid); |
| 286 | const int32_t appId = get_app_id(callingUid); |
| 287 | if (appId != AID_SYSTEM) { |
| 288 | ALOGE("Permission listUidsOfAuthBoundKeys denied for aid %d", appId); |
| 289 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 290 | return Status::ok(); |
| 291 | } |
| 292 | |
| 293 | const String8 prefix8(""); |
| 294 | auto userState = mKeyStore->getUserStateDB().getUserState(userId); |
| 295 | const std::string userDirName = userState->getUserDirName(); |
| 296 | auto encryptionKey = userState->getEncryptionKey(); |
| 297 | auto state = userState->getState(); |
| 298 | // unlock the user state |
| 299 | userState = {}; |
| 300 | |
| 301 | ResponseCode rc; |
| 302 | std::list<LockedKeyBlobEntry> internal_matches; |
| 303 | std::tie(rc, internal_matches) = |
| 304 | LockedKeyBlobEntry::list(userDirName, [&](uid_t, const std::string&) { |
| 305 | // Need to filter on auth bound state, so just return true. |
| 306 | return true; |
| 307 | }); |
| 308 | if (rc != ResponseCode::NO_ERROR) { |
| 309 | ALOGE("Error listing blob entries for user %d", userId); |
| 310 | return Status::fromServiceSpecificError(static_cast<int32_t>(rc)); |
| 311 | } |
| 312 | |
Rob Barnes | eb7f79b | 2018-11-08 15:44:10 -0700 | [diff] [blame] | 313 | for (LockedKeyBlobEntry& entry : internal_matches) { |
Rob Barnes | 5d59e63 | 2018-12-07 16:09:02 -0700 | [diff] [blame] | 314 | // Need to store uids as a list of strings because integer list output |
| 315 | // parameters is not supported in aidl-cpp. |
| 316 | std::string entryUid = std::to_string(entry->uid()); |
| 317 | if (std::find(uidsOut->begin(), uidsOut->end(), entryUid) != uidsOut->end()) { |
Rob Barnes | eb7f79b | 2018-11-08 15:44:10 -0700 | [diff] [blame] | 318 | // uid already in list, skip |
| 319 | continue; |
| 320 | } |
| 321 | |
| 322 | auto [rc, blob, charBlob] = entry.readBlobs(encryptionKey, state); |
| 323 | if (rc != ResponseCode::NO_ERROR && rc != ResponseCode::LOCKED) { |
| 324 | ALOGE("Error reading blob for key %s", entry->alias().c_str()); |
| 325 | continue; |
| 326 | } |
| 327 | |
| 328 | if (blob && blob.isEncrypted()) { |
Rob Barnes | 5d59e63 | 2018-12-07 16:09:02 -0700 | [diff] [blame] | 329 | uidsOut->push_back(entryUid); |
Rob Barnes | eb7f79b | 2018-11-08 15:44:10 -0700 | [diff] [blame] | 330 | } else if (charBlob) { |
| 331 | auto [success, hwEnforced, swEnforced] = charBlob.getKeyCharacteristics(); |
| 332 | if (!success) { |
| 333 | ALOGE("Error reading blob characteristics for key %s", entry->alias().c_str()); |
| 334 | continue; |
| 335 | } |
| 336 | if (hwEnforced.Contains(TAG_USER_SECURE_ID) || |
| 337 | swEnforced.Contains(TAG_USER_SECURE_ID)) { |
Rob Barnes | 5d59e63 | 2018-12-07 16:09:02 -0700 | [diff] [blame] | 338 | uidsOut->push_back(entryUid); |
Rob Barnes | eb7f79b | 2018-11-08 15:44:10 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | } |
| 342 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 343 | return Status::ok(); |
| 344 | } |
| 345 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 346 | Status KeyStoreService::onUserPasswordChanged(int32_t userId, const String16& password, |
| 347 | int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 348 | if (!checkBinderPermission(P_PASSWORD)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 349 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 350 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 353 | if (password.size() == 0) { |
| 354 | ALOGI("Secure lockscreen for user %d removed, deleting encrypted entries", userId); |
| 355 | mKeyStore->resetUser(userId, true); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 356 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 357 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 358 | } else { |
Pavel Grafov | f9b53eb | 2019-02-06 17:16:21 +0000 | [diff] [blame] | 359 | const String8 password8(password); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 360 | switch (mKeyStore->getState(userId)) { |
| 361 | case ::STATE_UNINITIALIZED: { |
| 362 | // generate master key, encrypt with password, write to file, |
| 363 | // initialize mMasterKey*. |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 364 | *aidl_return = static_cast<int32_t>(mKeyStore->initializeUser(password8, userId)); |
| 365 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 366 | } |
| 367 | case ::STATE_NO_ERROR: { |
| 368 | // rewrite master key with new password. |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 369 | *aidl_return = static_cast<int32_t>(mKeyStore->writeMasterKey(password8, userId)); |
| 370 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 371 | } |
| 372 | case ::STATE_LOCKED: { |
| 373 | ALOGE("Changing user %d's password while locked, clearing old encryption", userId); |
| 374 | mKeyStore->resetUser(userId, true); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 375 | *aidl_return = static_cast<int32_t>(mKeyStore->initializeUser(password8, userId)); |
| 376 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 379 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 380 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 384 | Status KeyStoreService::onUserAdded(int32_t userId, int32_t parentId, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 385 | if (!checkBinderPermission(P_USER_CHANGED)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 386 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 387 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // Sanity check that the new user has an empty keystore. |
| 391 | if (!mKeyStore->isEmpty(userId)) { |
| 392 | ALOGW("New user %d's keystore not empty. Clearing old entries.", userId); |
| 393 | } |
| 394 | // Unconditionally clear the keystore, just to be safe. |
| 395 | mKeyStore->resetUser(userId, false); |
| 396 | if (parentId != -1) { |
| 397 | // This profile must share the same master key password as the parent profile. Because the |
| 398 | // password of the parent profile is not known here, the best we can do is copy the parent's |
| 399 | // master key and master key file. This makes this profile use the same master key as the |
| 400 | // parent profile, forever. |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 401 | *aidl_return = static_cast<int32_t>(mKeyStore->copyMasterKey(parentId, userId)); |
| 402 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 403 | } else { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 404 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 405 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 409 | Status KeyStoreService::onUserRemoved(int32_t userId, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 410 | if (!checkBinderPermission(P_USER_CHANGED)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 411 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 412 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | mKeyStore->resetUser(userId, false); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 416 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 417 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 420 | Status KeyStoreService::lock(int32_t userId, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 421 | if (!checkBinderPermission(P_LOCK)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 422 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 423 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | State state = mKeyStore->getState(userId); |
| 427 | if (state != ::STATE_NO_ERROR) { |
| 428 | ALOGD("calling lock in state: %d", state); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 429 | *aidl_return = static_cast<int32_t>(ResponseCode(state)); |
| 430 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 433 | mKeyStore->getEnforcementPolicy().set_device_locked(true, userId); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 434 | mKeyStore->lock(userId); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 435 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 436 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 439 | Status KeyStoreService::unlock(int32_t userId, const String16& pw, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 440 | if (!checkBinderPermission(P_UNLOCK)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 441 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 442 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | State state = mKeyStore->getState(userId); |
| 446 | if (state != ::STATE_LOCKED) { |
| 447 | switch (state) { |
| 448 | case ::STATE_NO_ERROR: |
| 449 | ALOGI("calling unlock when already unlocked, ignoring."); |
| 450 | break; |
| 451 | case ::STATE_UNINITIALIZED: |
| 452 | ALOGE("unlock called on uninitialized keystore."); |
| 453 | break; |
| 454 | default: |
| 455 | ALOGE("unlock called on keystore in unknown state: %d", state); |
| 456 | break; |
| 457 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 458 | *aidl_return = static_cast<int32_t>(ResponseCode(state)); |
| 459 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 462 | mKeyStore->getEnforcementPolicy().set_device_locked(false, userId); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 463 | const String8 password8(pw); |
| 464 | // read master key, decrypt with password, initialize mMasterKey*. |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 465 | *aidl_return = static_cast<int32_t>(mKeyStore->readMasterKey(password8, userId)); |
| 466 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 469 | Status KeyStoreService::isEmpty(int32_t userId, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 470 | if (!checkBinderPermission(P_IS_EMPTY)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 471 | *aidl_return = static_cast<int32_t>(false); |
| 472 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 475 | *aidl_return = static_cast<int32_t>(mKeyStore->isEmpty(userId)); |
| 476 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 479 | Status KeyStoreService::grant(const String16& name, int32_t granteeUid, |
| 480 | ::android::String16* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 481 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
Bo Zhu | 91de6db | 2018-03-20 12:52:13 -0700 | [diff] [blame] | 482 | auto result = |
| 483 | checkBinderPermissionAndKeystoreState(P_GRANT, /*targetUid=*/-1, /*checkUnlocked=*/false); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 484 | if (!result.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 485 | *aidl_return = String16(); |
| 486 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | String8 name8(name); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 490 | auto lockedEntry = mKeyStore->getLockedBlobEntryIfExists(name8.string(), callingUid); |
| 491 | if (!lockedEntry) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 492 | *aidl_return = String16(); |
| 493 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 496 | *aidl_return = String16(mKeyStore->addGrant(lockedEntry, granteeUid).c_str()); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 497 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 500 | Status KeyStoreService::ungrant(const String16& name, int32_t granteeUid, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 501 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
Bo Zhu | 91de6db | 2018-03-20 12:52:13 -0700 | [diff] [blame] | 502 | KeyStoreServiceReturnCode result = |
| 503 | checkBinderPermissionAndKeystoreState(P_GRANT, /*targetUid=*/-1, /*checkUnlocked=*/false); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 504 | if (!result.isOk()) { |
Branden Archer | 7008074 | 2018-11-20 11:04:11 -0800 | [diff] [blame] | 505 | *aidl_return = result.getErrorCode(); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 506 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | String8 name8(name); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 510 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 511 | auto lockedEntry = mKeyStore->getLockedBlobEntryIfExists(name8.string(), callingUid); |
| 512 | if (!lockedEntry) { |
| 513 | *aidl_return = static_cast<int32_t>(ResponseCode::KEY_NOT_FOUND); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 516 | *aidl_return = mKeyStore->removeGrant(lockedEntry, granteeUid); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 517 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 520 | Status KeyStoreService::getmtime(const String16& name, int32_t uid, int64_t* time) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 521 | uid_t targetUid = getEffectiveUid(uid); |
| 522 | if (!checkBinderPermission(P_GET, targetUid)) { |
| 523 | ALOGW("permission denied for %d: getmtime", targetUid); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 524 | *time = -1L; |
| 525 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 526 | } |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 527 | String8 name8(name); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 528 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 529 | auto lockedEntry = mKeyStore->getLockedBlobEntryIfExists(name8.string(), targetUid); |
| 530 | if (!lockedEntry) { |
| 531 | ALOGW("could not access key with alias %s for getmtime", name8.string()); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 532 | *time = -1L; |
| 533 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 536 | std::string filename = lockedEntry->getKeyBlobPath(); |
| 537 | |
| 538 | int fd = TEMP_FAILURE_RETRY(open(filename.c_str(), O_NOFOLLOW, O_RDONLY)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 539 | if (fd < 0) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 540 | ALOGW("could not open %s for getmtime", filename.c_str()); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 541 | *time = -1L; |
| 542 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | struct stat s; |
| 546 | int ret = fstat(fd, &s); |
| 547 | close(fd); |
| 548 | if (ret == -1) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 549 | ALOGW("could not stat %s for getmtime", filename.c_str()); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 550 | *time = -1L; |
| 551 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 554 | *time = static_cast<int64_t>(s.st_mtime); |
| 555 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 558 | Status KeyStoreService::is_hardware_backed(const String16& keyType, int32_t* aidl_return) { |
| 559 | *aidl_return = static_cast<int32_t>(mKeyStore->isHardwareBacked(keyType) ? 1 : 0); |
| 560 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Janis Danisevskis | 265435f | 2018-11-16 14:10:46 -0800 | [diff] [blame] | 563 | Status KeyStoreService::clear_uid(int64_t targetUid64, int32_t* _aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 564 | uid_t targetUid = getEffectiveUid(targetUid64); |
| 565 | if (!checkBinderPermissionSelfOrSystem(P_CLEAR_UID, targetUid)) { |
Janis Danisevskis | 265435f | 2018-11-16 14:10:46 -0800 | [diff] [blame] | 566 | return AIDL_RETURN(ResponseCode::PERMISSION_DENIED); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 567 | } |
Rubin Xu | 7675c9f | 2017-03-15 19:26:52 +0000 | [diff] [blame] | 568 | ALOGI("clear_uid %" PRId64, targetUid64); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 569 | |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 570 | mKeyStore->removeAllGrantsToUid(targetUid); |
| 571 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 572 | ResponseCode rc; |
| 573 | std::list<LockedKeyBlobEntry> entries; |
Janis Danisevskis | 265435f | 2018-11-16 14:10:46 -0800 | [diff] [blame] | 574 | auto userDirName = mKeyStore->getUserStateDB().getUserStateByUid(targetUid)->getUserDirName(); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 575 | |
| 576 | // list has a fence making sure no workers are modifying blob files before iterating the |
| 577 | // data base. All returned entries are locked. |
| 578 | std::tie(rc, entries) = LockedKeyBlobEntry::list( |
Janis Danisevskis | 265435f | 2018-11-16 14:10:46 -0800 | [diff] [blame] | 579 | userDirName, [&](uid_t uid, const std::string&) -> bool { return uid == targetUid; }); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 580 | |
| 581 | if (rc != ResponseCode::NO_ERROR) { |
Janis Danisevskis | 265435f | 2018-11-16 14:10:46 -0800 | [diff] [blame] | 582 | return AIDL_RETURN(rc); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 585 | for (LockedKeyBlobEntry& lockedEntry : entries) { |
Rubin Xu | 85c85e9 | 2017-04-26 20:07:30 +0100 | [diff] [blame] | 586 | if (get_app_id(targetUid) == AID_SYSTEM) { |
| 587 | Blob keyBlob; |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 588 | Blob charBlob; |
| 589 | std::tie(rc, keyBlob, charBlob) = mKeyStore->get(lockedEntry); |
| 590 | if (rc == ResponseCode::NO_ERROR && keyBlob.isCriticalToDeviceEncryption()) { |
Rubin Xu | 85c85e9 | 2017-04-26 20:07:30 +0100 | [diff] [blame] | 591 | // Do not clear keys critical to device encryption under system uid. |
| 592 | continue; |
| 593 | } |
| 594 | } |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 595 | mKeyStore->del(lockedEntry); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 596 | } |
Janis Danisevskis | 265435f | 2018-11-16 14:10:46 -0800 | [diff] [blame] | 597 | return AIDL_RETURN(ResponseCode::NO_ERROR); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 600 | Status KeyStoreService::addRngEntropy( |
| 601 | const ::android::sp<::android::security::keystore::IKeystoreResponseCallback>& cb, |
| 602 | const ::std::vector<uint8_t>& entropy, int32_t flags, int32_t* _aidl_return) { |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 603 | auto device = mKeyStore->getDevice(flagsToSecurityLevel(flags)); |
| 604 | if (!device) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 605 | return AIDL_RETURN(ErrorCode::HARDWARE_TYPE_UNAVAILABLE); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 606 | } |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 607 | |
Janis Danisevskis | a359c67 | 2019-03-14 17:15:06 -0700 | [diff] [blame] | 608 | device->addRngEntropy(entropy, [device, cb](Return<ErrorCode> rc) { |
| 609 | cb->onFinished(KeyStoreServiceReturnCode(KS_HANDLE_HIDL_ERROR(device, rc))); |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 610 | }); |
| 611 | |
| 612 | return AIDL_RETURN(ResponseCode::NO_ERROR); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 615 | Status KeyStoreService::generateKey( |
| 616 | const ::android::sp<::android::security::keystore::IKeystoreKeyCharacteristicsCallback>& cb, |
| 617 | const String16& name, const KeymasterArguments& params, const ::std::vector<uint8_t>& entropy, |
| 618 | int uid, int flags, int32_t* _aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 619 | uid = getEffectiveUid(uid); |
Pavel Grafov | ff311b4 | 2018-01-24 20:34:37 +0000 | [diff] [blame] | 620 | auto logOnScopeExit = android::base::make_scope_guard([&] { |
| 621 | if (__android_log_security()) { |
| 622 | android_log_event_list(SEC_TAG_AUTH_KEY_GENERATED) |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 623 | << int32_t(*_aidl_return == static_cast<int32_t>(ResponseCode::NO_ERROR)) |
Pavel Grafov | ff311b4 | 2018-01-24 20:34:37 +0000 | [diff] [blame] | 624 | << String8(name) << int32_t(uid) << LOG_ID_SECURITY; |
| 625 | } |
| 626 | }); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 627 | KeyStoreServiceReturnCode rc = |
| 628 | checkBinderPermissionAndKeystoreState(P_INSERT, uid, flags & KEYSTORE_FLAG_ENCRYPTED); |
| 629 | if (!rc.isOk()) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 630 | return AIDL_RETURN(rc); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 631 | } |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 632 | if ((flags & KEYSTORE_FLAG_CRITICAL_TO_DEVICE_ENCRYPTION) && get_app_id(uid) != AID_SYSTEM) { |
| 633 | ALOGE("Non-system uid %d cannot set FLAG_CRITICAL_TO_DEVICE_ENCRYPTION", uid); |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 634 | return AIDL_RETURN(ResponseCode::PERMISSION_DENIED); |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 635 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 636 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 637 | if (containsTag(params.getParameters(), Tag::INCLUDE_UNIQUE_ID)) { |
Max Bires | 36ae17f | 2019-11-12 21:34:24 +0000 | [diff] [blame] | 638 | if (!checkBinderPermission(P_GEN_UNIQUE_ID)) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 639 | return AIDL_RETURN(ResponseCode::PERMISSION_DENIED); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 640 | } |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 641 | } |
| 642 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 643 | SecurityLevel securityLevel = flagsToSecurityLevel(flags); |
| 644 | auto dev = mKeyStore->getDevice(securityLevel); |
| 645 | if (!dev) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 646 | return AIDL_RETURN(ErrorCode::HARDWARE_TYPE_UNAVAILABLE); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 647 | } |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 648 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 649 | String8 name8(name); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 650 | auto lockedEntry = mKeyStore->getLockedBlobEntryIfNotExists(name8.string(), uid); |
| 651 | if (!lockedEntry) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 652 | return AIDL_RETURN(ResponseCode::KEY_ALREADY_EXISTS); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 653 | } |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 654 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 655 | logOnScopeExit.Disable(); |
| 656 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 657 | dev->generateKey( |
| 658 | std::move(lockedEntry), params.getParameters(), entropy, flags, |
| 659 | [cb, uid, name](KeyStoreServiceReturnCode rc, KeyCharacteristics keyCharacteristics) { |
| 660 | if (__android_log_security()) { |
| 661 | android_log_event_list(SEC_TAG_AUTH_KEY_GENERATED) |
| 662 | << rc.isOk() << String8(name) << int32_t(uid) << LOG_ID_SECURITY; |
| 663 | } |
| 664 | cb->onFinished(rc, |
| 665 | android::security::keymaster::KeyCharacteristics(keyCharacteristics)); |
| 666 | }); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 667 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 668 | return AIDL_RETURN(ResponseCode::NO_ERROR); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 671 | Status KeyStoreService::getKeyCharacteristics( |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 672 | const ::android::sp<::android::security::keystore::IKeystoreKeyCharacteristicsCallback>& cb, |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 673 | const String16& name, const ::android::security::keymaster::KeymasterBlob& clientId, |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 674 | const ::android::security::keymaster::KeymasterBlob& appData, int32_t uid, |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 675 | int32_t* _aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 676 | |
| 677 | uid_t targetUid = getEffectiveUid(uid); |
| 678 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 679 | if (!is_granted_to(callingUid, targetUid)) { |
| 680 | ALOGW("uid %d not permitted to act for uid %d in getKeyCharacteristics", callingUid, |
| 681 | targetUid); |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 682 | return AIDL_RETURN(ResponseCode::PERMISSION_DENIED); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 685 | String8 name8(name); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 686 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 687 | ResponseCode rc; |
| 688 | Blob keyBlob; |
| 689 | Blob charBlob; |
| 690 | LockedKeyBlobEntry lockedEntry; |
Janis Danisevskis | d714a67 | 2017-09-01 14:31:36 -0700 | [diff] [blame] | 691 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 692 | std::tie(rc, keyBlob, charBlob, lockedEntry) = |
| 693 | mKeyStore->getKeyForName(name8, targetUid, TYPE_KEYMASTER_10); |
| 694 | |
| 695 | if (rc != ResponseCode::NO_ERROR) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 696 | return AIDL_RETURN(rc); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 697 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 698 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 699 | auto dev = mKeyStore->getDevice(keyBlob); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 700 | if (!dev) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 701 | return AIDL_RETURN(ResponseCode::SYSTEM_ERROR); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 702 | } |
| 703 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 704 | // If the charBlob is up to date, it simply moves the argument blobs to the returned blobs |
| 705 | // and extracts the characteristics on the way. Otherwise it updates the cache file with data |
| 706 | // from keymaster. It may also upgrade the key blob. |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 707 | dev->getKeyCharacteristics( |
| 708 | std::move(lockedEntry), clientId.getData(), appData.getData(), std::move(keyBlob), |
| 709 | std::move(charBlob), |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 710 | [cb](KeyStoreServiceReturnCode rc, KeyCharacteristics keyCharacteristics) { |
| 711 | cb->onFinished(rc, |
| 712 | android::security::keymaster::KeyCharacteristics(keyCharacteristics)); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 713 | }); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 714 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 715 | return AIDL_RETURN(ResponseCode::NO_ERROR); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 716 | } |
| 717 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 718 | Status KeyStoreService::importKey( |
| 719 | const ::android::sp<::android::security::keystore::IKeystoreKeyCharacteristicsCallback>& cb, |
| 720 | const String16& name, const KeymasterArguments& params, int32_t format, |
| 721 | const ::std::vector<uint8_t>& keyData, int uid, int flags, int32_t* _aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 722 | uid = getEffectiveUid(uid); |
Pavel Grafov | ff311b4 | 2018-01-24 20:34:37 +0000 | [diff] [blame] | 723 | auto logOnScopeExit = android::base::make_scope_guard([&] { |
| 724 | if (__android_log_security()) { |
| 725 | android_log_event_list(SEC_TAG_KEY_IMPORTED) |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 726 | << int32_t(*_aidl_return == static_cast<int32_t>(ResponseCode::NO_ERROR)) |
Pavel Grafov | ff311b4 | 2018-01-24 20:34:37 +0000 | [diff] [blame] | 727 | << String8(name) << int32_t(uid) << LOG_ID_SECURITY; |
| 728 | } |
| 729 | }); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 730 | KeyStoreServiceReturnCode rc = |
| 731 | checkBinderPermissionAndKeystoreState(P_INSERT, uid, flags & KEYSTORE_FLAG_ENCRYPTED); |
| 732 | if (!rc.isOk()) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 733 | LOG(ERROR) << "permissission denied"; |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 734 | return AIDL_RETURN(rc); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 735 | } |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 736 | if ((flags & KEYSTORE_FLAG_CRITICAL_TO_DEVICE_ENCRYPTION) && get_app_id(uid) != AID_SYSTEM) { |
| 737 | ALOGE("Non-system uid %d cannot set FLAG_CRITICAL_TO_DEVICE_ENCRYPTION", uid); |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 738 | return AIDL_RETURN(ResponseCode::PERMISSION_DENIED); |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 739 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 740 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 741 | SecurityLevel securityLevel = flagsToSecurityLevel(flags); |
| 742 | auto dev = mKeyStore->getDevice(securityLevel); |
| 743 | if (!dev) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 744 | LOG(ERROR) << "importKey - cound not get keymaster device"; |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 745 | return AIDL_RETURN(ErrorCode::HARDWARE_TYPE_UNAVAILABLE); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 746 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 747 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 748 | String8 name8(name); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 749 | auto lockedEntry = mKeyStore->getLockedBlobEntryIfNotExists(name8.string(), uid); |
| 750 | if (!lockedEntry) { |
| 751 | LOG(ERROR) << "importKey - key: " << name8.string() << " " << int(uid) |
| 752 | << " already exists."; |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 753 | return AIDL_RETURN(ResponseCode::KEY_ALREADY_EXISTS); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 754 | } |
Janis Danisevskis | 4a1da2f | 2018-03-26 15:02:38 -0700 | [diff] [blame] | 755 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 756 | logOnScopeExit.Disable(); |
Janis Danisevskis | 4a1da2f | 2018-03-26 15:02:38 -0700 | [diff] [blame] | 757 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 758 | dev->importKey( |
| 759 | std::move(lockedEntry), params.getParameters(), KeyFormat(format), keyData, flags, |
| 760 | [cb, uid, name](KeyStoreServiceReturnCode rc, KeyCharacteristics keyCharacteristics) { |
| 761 | if (__android_log_security()) { |
| 762 | android_log_event_list(SEC_TAG_KEY_IMPORTED) |
| 763 | << rc.isOk() << String8(name) << int32_t(uid) << LOG_ID_SECURITY; |
| 764 | } |
| 765 | cb->onFinished(rc, |
| 766 | android::security::keymaster::KeyCharacteristics(keyCharacteristics)); |
| 767 | }); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 768 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 769 | return AIDL_RETURN(ResponseCode::NO_ERROR); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 772 | Status KeyStoreService::exportKey( |
| 773 | const ::android::sp<::android::security::keystore::IKeystoreExportKeyCallback>& cb, |
| 774 | const String16& name, int32_t format, |
| 775 | const ::android::security::keymaster::KeymasterBlob& clientId, |
| 776 | const ::android::security::keymaster::KeymasterBlob& appData, int32_t uid, |
| 777 | int32_t* _aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 778 | |
| 779 | uid_t targetUid = getEffectiveUid(uid); |
| 780 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 781 | if (!is_granted_to(callingUid, targetUid)) { |
| 782 | ALOGW("uid %d not permitted to act for uid %d in exportKey", callingUid, targetUid); |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 783 | return AIDL_RETURN(ResponseCode::PERMISSION_DENIED); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 786 | String8 name8(name); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 787 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 788 | KeyStoreServiceReturnCode rc; |
| 789 | Blob keyBlob; |
| 790 | Blob charBlob; |
| 791 | LockedKeyBlobEntry lockedEntry; |
| 792 | |
| 793 | std::tie(rc, keyBlob, charBlob, lockedEntry) = |
| 794 | mKeyStore->getKeyForName(name8, targetUid, TYPE_KEYMASTER_10); |
Janis Danisevskis | 3cf8532 | 2018-11-19 13:37:49 -0800 | [diff] [blame] | 795 | if (!rc.isOk()) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 796 | return AIDL_RETURN(rc); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 797 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 798 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 799 | auto dev = mKeyStore->getDevice(keyBlob); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 800 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 801 | dev->exportKey(std::move(lockedEntry), KeyFormat(format), clientId.getData(), appData.getData(), |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 802 | std::move(keyBlob), std::move(charBlob), |
| 803 | [cb](ExportResult exportResult) { cb->onFinished(exportResult); }); |
Ji Wang | 2c14231 | 2016-10-14 17:21:10 +0800 | [diff] [blame] | 804 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 805 | return AIDL_RETURN(ResponseCode::NO_ERROR); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 806 | } |
| 807 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 808 | Status KeyStoreService::begin(const sp<IKeystoreOperationResultCallback>& cb, |
| 809 | const sp<IBinder>& appToken, const String16& name, int32_t purpose, |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 810 | bool pruneable, const KeymasterArguments& params, |
| 811 | const ::std::vector<uint8_t>& entropy, int32_t uid, |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 812 | int32_t* _aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 813 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 814 | uid_t targetUid = getEffectiveUid(uid); |
| 815 | if (!is_granted_to(callingUid, targetUid)) { |
| 816 | ALOGW("uid %d not permitted to act for uid %d in begin", callingUid, targetUid); |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 817 | return AIDL_RETURN(ResponseCode::PERMISSION_DENIED); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 818 | } |
| 819 | if (!pruneable && get_app_id(callingUid) != AID_SYSTEM) { |
| 820 | ALOGE("Non-system uid %d trying to start non-pruneable operation", callingUid); |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 821 | return AIDL_RETURN(ResponseCode::PERMISSION_DENIED); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 822 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 823 | if (!checkAllowedOperationParams(params.getParameters())) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 824 | return AIDL_RETURN(ErrorCode::INVALID_ARGUMENT); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 825 | } |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 826 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 827 | String8 name8(name); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 828 | Blob keyBlob; |
| 829 | Blob charBlob; |
| 830 | LockedKeyBlobEntry lockedEntry; |
| 831 | ResponseCode rc; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 832 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 833 | std::tie(rc, keyBlob, charBlob, lockedEntry) = |
| 834 | mKeyStore->getKeyForName(name8, targetUid, TYPE_KEYMASTER_10); |
| 835 | |
| 836 | if (rc == ResponseCode::LOCKED && keyBlob.isSuperEncrypted()) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 837 | return AIDL_RETURN(ErrorCode::KEY_USER_NOT_AUTHENTICATED); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 838 | } |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 839 | if (rc != ResponseCode::NO_ERROR) return AIDL_RETURN(rc); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 840 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 841 | auto dev = mKeyStore->getDevice(keyBlob); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 842 | AuthorizationSet opParams = params.getParameters(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 843 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 844 | dev->begin(std::move(lockedEntry), appToken, std::move(keyBlob), std::move(charBlob), pruneable, |
| 845 | static_cast<KeyPurpose>(purpose), std::move(opParams), entropy, |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 846 | [this, cb, dev](OperationResult result_) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 847 | if (result_.resultCode.isOk() || |
| 848 | result_.resultCode == ResponseCode::OP_AUTH_NEEDED) { |
Janis Danisevskis | bec8999 | 2019-08-14 13:42:19 -0700 | [diff] [blame] | 849 | mKeyStore->addOperationDevice(result_.token, dev); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 850 | } |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 851 | cb->onFinished(result_); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 852 | }); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 853 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 854 | return AIDL_RETURN(ResponseCode::NO_ERROR); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 857 | Status KeyStoreService::update(const ::android::sp<IKeystoreOperationResultCallback>& cb, |
| 858 | const ::android::sp<::android::IBinder>& token, |
| 859 | const ::android::security::keymaster::KeymasterArguments& params, |
| 860 | const ::std::vector<uint8_t>& input, int32_t* _aidl_return) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 861 | if (!checkAllowedOperationParams(params.getParameters())) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 862 | return AIDL_RETURN(ErrorCode::INVALID_ARGUMENT); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 863 | } |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 864 | |
Janis Danisevskis | bec8999 | 2019-08-14 13:42:19 -0700 | [diff] [blame] | 865 | auto dev = mKeyStore->getOperationDevice(token); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 866 | if (!dev) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 867 | return AIDL_RETURN(ErrorCode::INVALID_OPERATION_HANDLE); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 868 | } |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 869 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 870 | dev->update(token, params.getParameters(), input, [this, cb, token](OperationResult result_) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 871 | if (!result_.resultCode.isOk()) { |
Janis Danisevskis | bec8999 | 2019-08-14 13:42:19 -0700 | [diff] [blame] | 872 | mKeyStore->removeOperationDevice(token); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 873 | } |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 874 | cb->onFinished(result_); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 875 | }); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 876 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 877 | return AIDL_RETURN(ResponseCode::NO_ERROR); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 880 | Status KeyStoreService::finish(const ::android::sp<IKeystoreOperationResultCallback>& cb, |
| 881 | const ::android::sp<::android::IBinder>& token, |
| 882 | const ::android::security::keymaster::KeymasterArguments& params, |
Rob Barnes | 3af223f | 2019-11-14 14:50:30 -0700 | [diff] [blame] | 883 | const ::std::vector<uint8_t>& input, |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 884 | const ::std::vector<uint8_t>& signature, |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 885 | const ::std::vector<uint8_t>& entropy, int32_t* _aidl_return) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 886 | if (!checkAllowedOperationParams(params.getParameters())) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 887 | return AIDL_RETURN(ErrorCode::INVALID_ARGUMENT); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 888 | } |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 889 | |
Janis Danisevskis | bec8999 | 2019-08-14 13:42:19 -0700 | [diff] [blame] | 890 | auto dev = mKeyStore->getOperationDevice(token); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 891 | if (!dev) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 892 | return AIDL_RETURN(ErrorCode::INVALID_OPERATION_HANDLE); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Rob Barnes | 3af223f | 2019-11-14 14:50:30 -0700 | [diff] [blame] | 895 | dev->finish(token, params.getParameters(), input, signature, entropy, |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 896 | [this, cb, token](OperationResult result_) { |
Janis Danisevskis | 8c4c1d6 | 2019-10-04 11:01:54 -0700 | [diff] [blame] | 897 | mKeyStore->removeOperationDevice(token); |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 898 | cb->onFinished(result_); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 899 | }); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 900 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 901 | return AIDL_RETURN(ResponseCode::NO_ERROR); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 904 | Status KeyStoreService::abort(const ::android::sp<IKeystoreResponseCallback>& cb, |
| 905 | const ::android::sp<::android::IBinder>& token, |
| 906 | int32_t* _aidl_return) { |
Janis Danisevskis | bec8999 | 2019-08-14 13:42:19 -0700 | [diff] [blame] | 907 | auto dev = mKeyStore->getOperationDevice(token); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 908 | if (!dev) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 909 | return AIDL_RETURN(ErrorCode::INVALID_OPERATION_HANDLE); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 910 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 911 | |
Janis Danisevskis | bec8999 | 2019-08-14 13:42:19 -0700 | [diff] [blame] | 912 | dev->abort(token, [this, cb, token](KeyStoreServiceReturnCode rc) { |
| 913 | mKeyStore->removeOperationDevice(token); |
| 914 | cb->onFinished(rc); |
| 915 | }); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 916 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 917 | return AIDL_RETURN(ResponseCode::NO_ERROR); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 918 | } |
| 919 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 920 | Status KeyStoreService::addAuthToken(const ::std::vector<uint8_t>& authTokenAsVector, |
Brian Young | ccb492d | 2018-02-22 23:36:01 +0000 | [diff] [blame] | 921 | int32_t* aidl_return) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 922 | |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 923 | // TODO(swillden): When gatekeeper and fingerprint are ready, this should be updated to |
| 924 | // receive a HardwareAuthToken, rather than an opaque byte array. |
| 925 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 926 | if (!checkBinderPermission(P_ADD_AUTH)) { |
| 927 | ALOGW("addAuthToken: permission denied for %d", IPCThreadState::self()->getCallingUid()); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 928 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 929 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 930 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 931 | if (authTokenAsVector.size() != sizeof(hw_auth_token_t)) { |
Branden Archer | 7008074 | 2018-11-20 11:04:11 -0800 | [diff] [blame] | 932 | *aidl_return = KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT).getErrorCode(); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 933 | return Status::ok(); |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 934 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 935 | |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 936 | hw_auth_token_t authToken; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 937 | memcpy(reinterpret_cast<void*>(&authToken), authTokenAsVector.data(), sizeof(hw_auth_token_t)); |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 938 | if (authToken.version != 0) { |
Branden Archer | 7008074 | 2018-11-20 11:04:11 -0800 | [diff] [blame] | 939 | *aidl_return = KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT).getErrorCode(); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 940 | return Status::ok(); |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 941 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 942 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 943 | mKeyStore->getAuthTokenTable().AddAuthenticationToken( |
| 944 | hidlVec2AuthToken(hidl_vec<uint8_t>(authTokenAsVector))); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 945 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 946 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 947 | } |
| 948 | |
David Zeuthen | 59102f3 | 2020-05-08 10:58:09 -0400 | [diff] [blame] | 949 | Status KeyStoreService::getTokensForCredstore(int64_t challenge, int64_t secureUserId, |
| 950 | int32_t authTokenMaxAgeMillis, |
| 951 | const ::android::sp<ICredstoreTokenCallback>& cb) { |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 952 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 953 | if (callingUid != AID_CREDSTORE) { |
| 954 | return Status::fromServiceSpecificError(static_cast<int32_t>(0)); |
| 955 | } |
| 956 | |
| 957 | auto [err, authToken] = mKeyStore->getAuthTokenTable().FindAuthorizationForCredstore( |
| 958 | challenge, secureUserId, authTokenMaxAgeMillis); |
David Zeuthen | 59102f3 | 2020-05-08 10:58:09 -0400 | [diff] [blame] | 959 | // It's entirely possible we couldn't find an authToken (e.g. no user auth |
| 960 | // happened within the requested deadline) and in that case, we just |
| 961 | // callback immediately signaling success but just not returning any tokens. |
| 962 | if (err != AuthTokenTable::OK) { |
| 963 | cb->onFinished(true, {} /* serializedAuthToken */, {} /* serializedVerificationToken */); |
| 964 | return Status::ok(); |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 965 | } |
David Zeuthen | 59102f3 | 2020-05-08 10:58:09 -0400 | [diff] [blame] | 966 | |
| 967 | // If we did find an authToken, get a verificationToken as well... |
| 968 | // |
| 969 | std::vector<uint8_t> serializedAuthToken = authToken2HidlVec(authToken); |
| 970 | std::vector<uint8_t> serializedVerificationToken; |
| 971 | std::shared_ptr<KeymasterWorker> dev = mKeyStore->getDevice(SecurityLevel::TRUSTED_ENVIRONMENT); |
| 972 | if (!dev) { |
| 973 | LOG(ERROR) << "Unable to get KM device for SecurityLevel::TRUSTED_ENVIRONMENT"; |
| 974 | dev = mKeyStore->getDevice(SecurityLevel::SOFTWARE); |
| 975 | if (!dev) { |
| 976 | LOG(ERROR) << "Unable to get KM device for SecurityLevel::SOFTWARE"; |
| 977 | cb->onFinished(false, {}, {}); |
| 978 | return Status::fromServiceSpecificError(static_cast<int32_t>(0)); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | dev->verifyAuthorization( |
| 983 | challenge, {} /* params */, authToken, |
| 984 | [serializedAuthToken, cb](KeyStoreServiceReturnCode rc, HardwareAuthToken, |
| 985 | VerificationToken verificationToken) { |
| 986 | if (rc != ErrorCode::OK) { |
| 987 | LOG(ERROR) << "verifyAuthorization failed, rc=" << rc; |
| 988 | cb->onFinished(false, {}, {}); |
| 989 | return; |
| 990 | } |
| 991 | std::optional<std::vector<uint8_t>> serializedVerificationToken = |
| 992 | serializeVerificationToken(verificationToken); |
| 993 | if (!serializedVerificationToken) { |
| 994 | LOG(ERROR) << "Error serializing verificationToken"; |
| 995 | cb->onFinished(false, {}, {}); |
| 996 | return; |
| 997 | } |
| 998 | cb->onFinished(true, serializedAuthToken, serializedVerificationToken.value()); |
| 999 | }); |
| 1000 | |
David Zeuthen | ab3e565 | 2019-10-28 13:32:48 -0400 | [diff] [blame] | 1001 | return Status::ok(); |
| 1002 | } |
| 1003 | |
Dorin Drimus | 4b7a7fb | 2020-04-20 17:32:13 +0200 | [diff] [blame] | 1004 | bool isDeviceIdAttestationTag(Tag tag) { |
| 1005 | switch (tag) { |
| 1006 | case Tag::ATTESTATION_ID_BRAND: |
| 1007 | case Tag::ATTESTATION_ID_DEVICE: |
| 1008 | case Tag::ATTESTATION_ID_MANUFACTURER: |
| 1009 | case Tag::ATTESTATION_ID_MODEL: |
| 1010 | case Tag::ATTESTATION_ID_PRODUCT: |
| 1011 | case Tag::ATTESTATION_ID_IMEI: |
| 1012 | case Tag::ATTESTATION_ID_MEID: |
| 1013 | case Tag::ATTESTATION_ID_SERIAL: |
| 1014 | return true; |
| 1015 | case Tag::INVALID: |
| 1016 | case Tag::PURPOSE: |
| 1017 | case Tag::ALGORITHM: |
| 1018 | case Tag::KEY_SIZE: |
| 1019 | case Tag::BLOCK_MODE: |
| 1020 | case Tag::DIGEST: |
| 1021 | case Tag::PADDING: |
| 1022 | case Tag::CALLER_NONCE: |
| 1023 | case Tag::MIN_MAC_LENGTH: |
| 1024 | case Tag::EC_CURVE: |
| 1025 | case Tag::RSA_PUBLIC_EXPONENT: |
| 1026 | case Tag::INCLUDE_UNIQUE_ID: |
| 1027 | case Tag::BLOB_USAGE_REQUIREMENTS: |
| 1028 | case Tag::BOOTLOADER_ONLY: |
| 1029 | case Tag::ROLLBACK_RESISTANCE: |
| 1030 | case Tag::HARDWARE_TYPE: |
| 1031 | case Tag::ACTIVE_DATETIME: |
| 1032 | case Tag::ORIGINATION_EXPIRE_DATETIME: |
| 1033 | case Tag::USAGE_EXPIRE_DATETIME: |
| 1034 | case Tag::MIN_SECONDS_BETWEEN_OPS: |
| 1035 | case Tag::MAX_USES_PER_BOOT: |
| 1036 | case Tag::USER_ID: |
| 1037 | case Tag::USER_SECURE_ID: |
| 1038 | case Tag::NO_AUTH_REQUIRED: |
| 1039 | case Tag::USER_AUTH_TYPE: |
| 1040 | case Tag::AUTH_TIMEOUT: |
| 1041 | case Tag::ALLOW_WHILE_ON_BODY: |
| 1042 | case Tag::TRUSTED_USER_PRESENCE_REQUIRED: |
| 1043 | case Tag::TRUSTED_CONFIRMATION_REQUIRED: |
| 1044 | case Tag::UNLOCKED_DEVICE_REQUIRED: |
| 1045 | case Tag::APPLICATION_ID: |
| 1046 | case Tag::APPLICATION_DATA: |
| 1047 | case Tag::CREATION_DATETIME: |
| 1048 | case Tag::ORIGIN: |
| 1049 | case Tag::ROOT_OF_TRUST: |
| 1050 | case Tag::OS_VERSION: |
| 1051 | case Tag::OS_PATCHLEVEL: |
| 1052 | case Tag::UNIQUE_ID: |
| 1053 | case Tag::ATTESTATION_CHALLENGE: |
| 1054 | case Tag::ATTESTATION_APPLICATION_ID: |
| 1055 | case Tag::VENDOR_PATCHLEVEL: |
| 1056 | case Tag::BOOT_PATCHLEVEL: |
| 1057 | case Tag::ASSOCIATED_DATA: |
| 1058 | case Tag::NONCE: |
| 1059 | case Tag::MAC_LENGTH: |
| 1060 | case Tag::RESET_SINCE_ID_ROTATION: |
| 1061 | case Tag::CONFIRMATION_TOKEN: |
| 1062 | return false; |
| 1063 | // no default, all values must be present in the switch, in this way the compiler ensures |
| 1064 | // that new values added in the Tag enum are also added here. |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | // These are attestation id tags that are not unique per device and don't require special permission |
| 1069 | // to be attested. Any addition to this list needs privacy review and approval (PWG). |
| 1070 | bool isDevicePropertyAttestationTag(Tag tag) { |
| 1071 | switch (tag) { |
| 1072 | case Tag::ATTESTATION_ID_BRAND: |
| 1073 | case Tag::ATTESTATION_ID_DEVICE: |
| 1074 | case Tag::ATTESTATION_ID_MANUFACTURER: |
| 1075 | case Tag::ATTESTATION_ID_MODEL: |
| 1076 | case Tag::ATTESTATION_ID_PRODUCT: |
| 1077 | return true; |
| 1078 | default: |
| 1079 | return false; |
| 1080 | } |
| 1081 | } |
| 1082 | |
Eran Messeri | d9f8ae5 | 2018-10-24 13:54:00 +0100 | [diff] [blame] | 1083 | bool isDeviceIdAttestationRequested(const KeymasterArguments& params) { |
Chih-Hung Hsieh | b81e68b | 2018-07-13 11:37:45 -0700 | [diff] [blame] | 1084 | const hardware::hidl_vec<KeyParameter>& paramsVec = params.getParameters(); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1085 | for (size_t i = 0; i < paramsVec.size(); ++i) { |
Dorin Drimus | 4b7a7fb | 2020-04-20 17:32:13 +0200 | [diff] [blame] | 1086 | if (isDeviceIdAttestationTag(paramsVec[i].tag)) { |
Eran Messeri | d9f8ae5 | 2018-10-24 13:54:00 +0100 | [diff] [blame] | 1087 | return true; |
Dorin Drimus | 4b7a7fb | 2020-04-20 17:32:13 +0200 | [diff] [blame] | 1088 | } |
| 1089 | } |
| 1090 | return false; |
| 1091 | } |
| 1092 | |
| 1093 | // Device properties can be attested safely without special permission |
| 1094 | bool needsPermissionToAttestDeviceIds(const KeymasterArguments& params) { |
| 1095 | const hardware::hidl_vec<KeyParameter>& paramsVec = params.getParameters(); |
| 1096 | for (size_t i = 0; i < paramsVec.size(); ++i) { |
| 1097 | if (isDeviceIdAttestationTag(paramsVec[i].tag) && |
| 1098 | !isDevicePropertyAttestationTag(paramsVec[i].tag)) { |
| 1099 | return true; |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1100 | } |
| 1101 | } |
Eran Messeri | d9f8ae5 | 2018-10-24 13:54:00 +0100 | [diff] [blame] | 1102 | return false; |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1103 | } |
| 1104 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1105 | Status KeyStoreService::attestKey( |
| 1106 | const ::android::sp<::android::security::keystore::IKeystoreCertificateChainCallback>& cb, |
| 1107 | const String16& name, const KeymasterArguments& params, int32_t* _aidl_return) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1108 | // check null output if method signature is updated and return ErrorCode::OUTPUT_PARAMETER_NULL |
| 1109 | if (!checkAllowedOperationParams(params.getParameters())) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1110 | return AIDL_RETURN(ErrorCode::INVALID_ARGUMENT); |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
Eran Messeri | e2c3415 | 2017-12-21 21:01:22 +0000 | [diff] [blame] | 1113 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1114 | |
Dorin Drimus | 2f43a6a | 2020-05-05 19:19:24 +0200 | [diff] [blame] | 1115 | if (needsPermissionToAttestDeviceIds(params) && (get_app_id(callingUid) != AID_SYSTEM)) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1116 | return AIDL_RETURN(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT)); |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1117 | } |
| 1118 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1119 | AuthorizationSet mutableParams = params.getParameters(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1120 | KeyStoreServiceReturnCode rc = updateParamsForAttestation(callingUid, &mutableParams); |
Hasini Gunasinghe | 8c8c2d4 | 2020-06-23 17:17:47 +0000 | [diff] [blame] | 1121 | |
| 1122 | auto logErrorOnReturn = android::base::make_scope_guard( |
| 1123 | [&] { logKeystoreKeyAttestationEvent(false /*wasSuccessful*/, rc.getErrorCode()); }); |
| 1124 | |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1125 | if (!rc.isOk()) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1126 | return AIDL_RETURN(rc); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1127 | } |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1128 | |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1129 | String8 name8(name); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1130 | Blob keyBlob; |
| 1131 | Blob charBlob; |
| 1132 | LockedKeyBlobEntry lockedEntry; |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1133 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1134 | std::tie(rc, keyBlob, charBlob, lockedEntry) = |
| 1135 | mKeyStore->getKeyForName(name8, callingUid, TYPE_KEYMASTER_10); |
| 1136 | |
Janis Danisevskis | 9dff56c | 2019-07-10 14:08:06 -0700 | [diff] [blame] | 1137 | if (!rc.isOk()) { |
| 1138 | return AIDL_RETURN(rc); |
| 1139 | } |
| 1140 | |
Hasini Gunasinghe | 8c8c2d4 | 2020-06-23 17:17:47 +0000 | [diff] [blame] | 1141 | logErrorOnReturn.Disable(); |
| 1142 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1143 | auto dev = mKeyStore->getDevice(keyBlob); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1144 | auto hidlKey = blob2hidlVec(keyBlob); |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1145 | dev->attestKey( |
| 1146 | std::move(hidlKey), mutableParams.hidl_data(), |
Janis Danisevskis | a359c67 | 2019-03-14 17:15:06 -0700 | [diff] [blame] | 1147 | [dev, cb](Return<void> rc, |
| 1148 | std::tuple<ErrorCode, hidl_vec<hidl_vec<uint8_t>>>&& hidlResult) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1149 | auto& [ret, certChain] = hidlResult; |
| 1150 | if (!rc.isOk()) { |
Hasini Gunasinghe | 8c8c2d4 | 2020-06-23 17:17:47 +0000 | [diff] [blame] | 1151 | logKeystoreKeyAttestationEvent(false /*wasSuccessful*/, |
| 1152 | static_cast<int32_t>(ResponseCode::SYSTEM_ERROR)); |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1153 | cb->onFinished(KeyStoreServiceReturnCode(ResponseCode::SYSTEM_ERROR), {}); |
| 1154 | } else if (ret != ErrorCode::OK) { |
Hasini Gunasinghe | 8c8c2d4 | 2020-06-23 17:17:47 +0000 | [diff] [blame] | 1155 | KeyStoreServiceReturnCode ksrc(ret); |
| 1156 | logKeystoreKeyAttestationEvent(false /*wasSuccessful*/, ksrc.getErrorCode()); |
Janis Danisevskis | a359c67 | 2019-03-14 17:15:06 -0700 | [diff] [blame] | 1157 | dev->logIfKeymasterVendorError(ret); |
Hasini Gunasinghe | 8c8c2d4 | 2020-06-23 17:17:47 +0000 | [diff] [blame] | 1158 | cb->onFinished(ksrc, {}); |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1159 | } else { |
Hasini Gunasinghe | 8c8c2d4 | 2020-06-23 17:17:47 +0000 | [diff] [blame] | 1160 | KeyStoreServiceReturnCode ksrc(ret); |
| 1161 | logKeystoreKeyAttestationEvent(true /*wasSuccessful*/, ksrc.getErrorCode()); |
| 1162 | cb->onFinished(ksrc, KeymasterCertificateChain(std::move(certChain))); |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1163 | } |
| 1164 | }); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1165 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1166 | return AIDL_RETURN(ResponseCode::NO_ERROR); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1167 | } |
| 1168 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1169 | // My IDE defines "CAPTURE_MOVE(x) x" because it does not understand generalized lambda captures. |
| 1170 | // It should never be redefined by a build system though. |
| 1171 | #ifndef CAPTURE_MOVE |
| 1172 | #define CAPTURE_MOVE(x) x = std::move(x) |
| 1173 | #endif |
| 1174 | |
| 1175 | Status KeyStoreService::attestDeviceIds( |
| 1176 | const ::android::sp<::android::security::keystore::IKeystoreCertificateChainCallback>& cb, |
| 1177 | const KeymasterArguments& params, int32_t* _aidl_return) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1178 | // check null output if method signature is updated and return ErrorCode::OUTPUT_PARAMETER_NULL |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1179 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1180 | if (!checkAllowedOperationParams(params.getParameters())) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1181 | return AIDL_RETURN(ErrorCode::INVALID_ARGUMENT); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | if (!isDeviceIdAttestationRequested(params)) { |
| 1185 | // There is an attestKey() method for attesting keys without device ID attestation. |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1186 | return AIDL_RETURN(ErrorCode::INVALID_ARGUMENT); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
Dorin Drimus | 4b7a7fb | 2020-04-20 17:32:13 +0200 | [diff] [blame] | 1190 | |
| 1191 | // Request special permission only for unique ids |
| 1192 | if (needsPermissionToAttestDeviceIds(params)) { |
| 1193 | sp<IBinder> binder = defaultServiceManager()->getService(String16("permission")); |
| 1194 | if (binder == nullptr) { |
| 1195 | return AIDL_RETURN(ErrorCode::CANNOT_ATTEST_IDS); |
| 1196 | } |
| 1197 | |
| 1198 | if (!interface_cast<IPermissionController>(binder)->checkPermission( |
| 1199 | String16("android.permission.READ_PRIVILEGED_PHONE_STATE"), |
| 1200 | IPCThreadState::self()->getCallingPid(), callingUid)) { |
| 1201 | return AIDL_RETURN(ErrorCode::CANNOT_ATTEST_IDS); |
| 1202 | } |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1203 | } |
| 1204 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1205 | AuthorizationSet mutableParams = params.getParameters(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1206 | KeyStoreServiceReturnCode rc = updateParamsForAttestation(callingUid, &mutableParams); |
| 1207 | if (!rc.isOk()) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1208 | return AIDL_RETURN(rc); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | // Generate temporary key. |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1212 | auto dev = mKeyStore->getDevice(SecurityLevel::TRUSTED_ENVIRONMENT); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1213 | |
Shawn Willden | 70c1a78 | 2018-07-11 15:13:20 -0600 | [diff] [blame] | 1214 | if (!dev) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1215 | return AIDL_RETURN(ResponseCode::SYSTEM_ERROR); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1216 | } |
| 1217 | |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1218 | |
| 1219 | AuthorizationSet keyCharacteristics; |
| 1220 | keyCharacteristics.push_back(TAG_PURPOSE, KeyPurpose::VERIFY); |
| 1221 | keyCharacteristics.push_back(TAG_ALGORITHM, Algorithm::EC); |
| 1222 | keyCharacteristics.push_back(TAG_DIGEST, Digest::SHA_2_256); |
| 1223 | keyCharacteristics.push_back(TAG_NO_AUTH_REQUIRED); |
| 1224 | keyCharacteristics.push_back(TAG_EC_CURVE, EcCurve::P_256); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1225 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1226 | std::promise<KeyStoreServiceReturnCode> resultPromise; |
| 1227 | auto resultFuture = resultPromise.get_future(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1228 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1229 | dev->generateKey( |
| 1230 | keyCharacteristics.hidl_data(), |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1231 | [cb, dev, CAPTURE_MOVE(mutableParams)]( |
| 1232 | Return<void> rc, |
| 1233 | std::tuple<ErrorCode, ::std::vector<uint8_t>, KeyCharacteristics>&& hidlResult) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1234 | auto& [ret, hidlKeyBlob_, dummyCharacteristics] = hidlResult; |
| 1235 | auto hidlKeyBlob = std::move(hidlKeyBlob_); |
| 1236 | if (!rc.isOk()) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1237 | cb->onFinished(KeyStoreServiceReturnCode(ResponseCode::SYSTEM_ERROR), {}); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1238 | return; |
| 1239 | } |
| 1240 | if (ret != ErrorCode::OK) { |
Janis Danisevskis | a359c67 | 2019-03-14 17:15:06 -0700 | [diff] [blame] | 1241 | dev->logIfKeymasterVendorError(ret); |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1242 | cb->onFinished(KeyStoreServiceReturnCode(ret), {}); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1243 | return; |
| 1244 | } |
| 1245 | dev->attestKey( |
| 1246 | hidlKeyBlob, mutableParams.hidl_data(), |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1247 | [cb, dev, |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1248 | hidlKeyBlob](Return<void> rc, |
| 1249 | std::tuple<ErrorCode, hidl_vec<hidl_vec<uint8_t>>>&& hidlResult) { |
| 1250 | auto& [ret, certChain] = hidlResult; |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1251 | // schedule temp key for deletion |
Janis Danisevskis | a359c67 | 2019-03-14 17:15:06 -0700 | [diff] [blame] | 1252 | dev->deleteKey(std::move(hidlKeyBlob), [dev](Return<ErrorCode> rc) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1253 | // log error but don't return an error |
Janis Danisevskis | a359c67 | 2019-03-14 17:15:06 -0700 | [diff] [blame] | 1254 | KS_HANDLE_HIDL_ERROR(dev, rc); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1255 | }); |
| 1256 | if (!rc.isOk()) { |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1257 | cb->onFinished(KeyStoreServiceReturnCode(ResponseCode::SYSTEM_ERROR), {}); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1258 | return; |
| 1259 | } |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1260 | if (ret == ErrorCode::OK) { |
| 1261 | cb->onFinished( |
| 1262 | KeyStoreServiceReturnCode(ret), |
| 1263 | ::android::security::keymaster::KeymasterCertificateChain(certChain)); |
| 1264 | } else { |
Janis Danisevskis | a359c67 | 2019-03-14 17:15:06 -0700 | [diff] [blame] | 1265 | dev->logIfKeymasterVendorError(ret); |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1266 | cb->onFinished(KeyStoreServiceReturnCode(ret), {}); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1267 | } |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1268 | }); |
| 1269 | }); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1270 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1271 | return AIDL_RETURN(ResponseCode::NO_ERROR); |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1274 | Status KeyStoreService::onDeviceOffBody(int32_t* aidl_return) { |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1275 | // TODO(tuckeris): add permission check. This should be callable from ClockworkHome only. |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1276 | mKeyStore->getAuthTokenTable().onDeviceOffBody(); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1277 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 1278 | return Status::ok(); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1279 | } |
| 1280 | |
Janis Danisevskis | cb9267d | 2017-12-19 16:27:52 -0800 | [diff] [blame] | 1281 | Status KeyStoreService::importWrappedKey( |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1282 | const ::android::sp<::android::security::keystore::IKeystoreKeyCharacteristicsCallback>& cb, |
Janis Danisevskis | cb9267d | 2017-12-19 16:27:52 -0800 | [diff] [blame] | 1283 | const ::android::String16& wrappedKeyAlias, const ::std::vector<uint8_t>& wrappedKey, |
| 1284 | const ::android::String16& wrappingKeyAlias, const ::std::vector<uint8_t>& maskingKey, |
| 1285 | const KeymasterArguments& params, int64_t rootSid, int64_t fingerprintSid, |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1286 | int32_t* _aidl_return) { |
Janis Danisevskis | cb9267d | 2017-12-19 16:27:52 -0800 | [diff] [blame] | 1287 | |
| 1288 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1289 | |
| 1290 | if (!checkBinderPermission(P_INSERT, callingUid)) { |
| 1291 | return AIDL_RETURN(ResponseCode::PERMISSION_DENIED); |
| 1292 | } |
| 1293 | |
Janis Danisevskis | cb9267d | 2017-12-19 16:27:52 -0800 | [diff] [blame] | 1294 | String8 wrappingKeyName8(wrappingKeyAlias); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1295 | |
| 1296 | KeyStoreServiceReturnCode rc; |
| 1297 | Blob wrappingKeyBlob; |
| 1298 | Blob wrappingCharBlob; |
| 1299 | LockedKeyBlobEntry wrappingLockedEntry; |
| 1300 | |
| 1301 | std::tie(rc, wrappingKeyBlob, wrappingCharBlob, wrappingLockedEntry) = |
| 1302 | mKeyStore->getKeyForName(wrappingKeyName8, callingUid, TYPE_KEYMASTER_10); |
Branden Archer | d166a88 | 2018-11-20 10:56:48 -0800 | [diff] [blame] | 1303 | if (!rc.isOk()) { |
Janis Danisevskis | cb9267d | 2017-12-19 16:27:52 -0800 | [diff] [blame] | 1304 | return AIDL_RETURN(rc); |
| 1305 | } |
| 1306 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1307 | String8 wrappedKeyName8(wrappedKeyAlias); |
| 1308 | auto wrappedLockedEntry = |
| 1309 | mKeyStore->getLockedBlobEntryIfNotExists(wrappedKeyName8.string(), callingUid); |
| 1310 | if (!wrappedLockedEntry) { |
| 1311 | return AIDL_RETURN(ResponseCode::KEY_ALREADY_EXISTS); |
| 1312 | } |
| 1313 | |
Janis Danisevskis | cb9267d | 2017-12-19 16:27:52 -0800 | [diff] [blame] | 1314 | SecurityLevel securityLevel = wrappingKeyBlob.getSecurityLevel(); |
| 1315 | auto dev = mKeyStore->getDevice(securityLevel); |
| 1316 | if (!dev) { |
| 1317 | return AIDL_RETURN(ErrorCode::HARDWARE_TYPE_UNAVAILABLE); |
| 1318 | } |
| 1319 | |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1320 | dev->importWrappedKey( |
| 1321 | std::move(wrappingLockedEntry), std::move(wrappedLockedEntry), wrappedKey, maskingKey, |
| 1322 | params.getParameters(), std::move(wrappingKeyBlob), std::move(wrappingCharBlob), rootSid, |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1323 | fingerprintSid, [cb](KeyStoreServiceReturnCode rc, KeyCharacteristics keyCharacteristics) { |
| 1324 | cb->onFinished(rc, |
| 1325 | ::android::security::keymaster::KeyCharacteristics(keyCharacteristics)); |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1326 | }); |
Janis Danisevskis | cb9267d | 2017-12-19 16:27:52 -0800 | [diff] [blame] | 1327 | |
Rob Barnes | bb6cabd | 2018-10-04 17:10:37 -0600 | [diff] [blame] | 1328 | return AIDL_RETURN(ResponseCode::NO_ERROR); |
Janis Danisevskis | cb9267d | 2017-12-19 16:27:52 -0800 | [diff] [blame] | 1329 | } |
| 1330 | |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1331 | Status KeyStoreService::presentConfirmationPrompt(const sp<IBinder>& listener, |
| 1332 | const String16& promptText, |
| 1333 | const ::std::vector<uint8_t>& extraData, |
| 1334 | const String16& locale, int32_t uiOptionsAsFlags, |
| 1335 | int32_t* aidl_return) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1336 | return mKeyStore->getConfirmationManager().presentConfirmationPrompt( |
| 1337 | listener, promptText, extraData, locale, uiOptionsAsFlags, aidl_return); |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | Status KeyStoreService::cancelConfirmationPrompt(const sp<IBinder>& listener, |
| 1341 | int32_t* aidl_return) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1342 | return mKeyStore->getConfirmationManager().cancelConfirmationPrompt(listener, aidl_return); |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1343 | } |
| 1344 | |
David Zeuthen | 1a49231 | 2018-02-26 11:00:30 -0500 | [diff] [blame] | 1345 | Status KeyStoreService::isConfirmationPromptSupported(bool* aidl_return) { |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1346 | return mKeyStore->getConfirmationManager().isConfirmationPromptSupported(aidl_return); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | /** |
| 1350 | * Get the effective target uid for a binder operation that takes an |
| 1351 | * optional uid as the target. |
| 1352 | */ |
| 1353 | uid_t KeyStoreService::getEffectiveUid(int32_t targetUid) { |
| 1354 | if (targetUid == UID_SELF) { |
| 1355 | return IPCThreadState::self()->getCallingUid(); |
| 1356 | } |
| 1357 | return static_cast<uid_t>(targetUid); |
| 1358 | } |
| 1359 | |
| 1360 | /** |
| 1361 | * Check if the caller of the current binder method has the required |
| 1362 | * permission and if acting on other uids the grants to do so. |
| 1363 | */ |
| 1364 | bool KeyStoreService::checkBinderPermission(perm_t permission, int32_t targetUid) { |
| 1365 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1366 | pid_t spid = IPCThreadState::self()->getCallingPid(); |
Steven Moreland | 23115b0 | 2019-01-10 16:20:20 -0800 | [diff] [blame] | 1367 | const char* ssid = IPCThreadState::self()->getCallingSid(); |
| 1368 | if (!has_permission(callingUid, permission, spid, ssid)) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1369 | ALOGW("permission %s denied for %d", get_perm_label(permission), callingUid); |
| 1370 | return false; |
| 1371 | } |
| 1372 | if (!is_granted_to(callingUid, getEffectiveUid(targetUid))) { |
| 1373 | ALOGW("uid %d not granted to act for %d", callingUid, targetUid); |
| 1374 | return false; |
| 1375 | } |
| 1376 | return true; |
| 1377 | } |
| 1378 | |
| 1379 | /** |
| 1380 | * Check if the caller of the current binder method has the required |
| 1381 | * permission and the target uid is the caller or the caller is system. |
| 1382 | */ |
| 1383 | bool KeyStoreService::checkBinderPermissionSelfOrSystem(perm_t permission, int32_t targetUid) { |
| 1384 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1385 | pid_t spid = IPCThreadState::self()->getCallingPid(); |
Steven Moreland | 23115b0 | 2019-01-10 16:20:20 -0800 | [diff] [blame] | 1386 | const char* ssid = IPCThreadState::self()->getCallingSid(); |
| 1387 | if (!has_permission(callingUid, permission, spid, ssid)) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1388 | ALOGW("permission %s denied for %d", get_perm_label(permission), callingUid); |
| 1389 | return false; |
| 1390 | } |
| 1391 | return getEffectiveUid(targetUid) == callingUid || callingUid == AID_SYSTEM; |
| 1392 | } |
| 1393 | |
| 1394 | /** |
| 1395 | * Check if the caller of the current binder method has the required |
| 1396 | * permission or the target of the operation is the caller's uid. This is |
| 1397 | * for operation where the permission is only for cross-uid activity and all |
| 1398 | * uids are allowed to act on their own (ie: clearing all entries for a |
| 1399 | * given uid). |
| 1400 | */ |
| 1401 | bool KeyStoreService::checkBinderPermissionOrSelfTarget(perm_t permission, int32_t targetUid) { |
| 1402 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1403 | if (getEffectiveUid(targetUid) == callingUid) { |
| 1404 | return true; |
| 1405 | } else { |
| 1406 | return checkBinderPermission(permission, targetUid); |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | /** |
| 1411 | * Helper method to check that the caller has the required permission as |
| 1412 | * well as the keystore is in the unlocked state if checkUnlocked is true. |
| 1413 | * |
| 1414 | * Returns NO_ERROR on success, PERMISSION_DENIED on a permission error and |
| 1415 | * otherwise the state of keystore when not unlocked and checkUnlocked is |
| 1416 | * true. |
| 1417 | */ |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1418 | KeyStoreServiceReturnCode |
| 1419 | KeyStoreService::checkBinderPermissionAndKeystoreState(perm_t permission, int32_t targetUid, |
| 1420 | bool checkUnlocked) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1421 | if (!checkBinderPermission(permission, targetUid)) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1422 | return ResponseCode::PERMISSION_DENIED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1423 | } |
| 1424 | State state = mKeyStore->getState(get_user_id(getEffectiveUid(targetUid))); |
| 1425 | if (checkUnlocked && !isKeystoreUnlocked(state)) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1426 | // All State values coincide with ResponseCodes |
| 1427 | return static_cast<ResponseCode>(state); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1428 | } |
| 1429 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1430 | return ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | bool KeyStoreService::isKeystoreUnlocked(State state) { |
| 1434 | switch (state) { |
| 1435 | case ::STATE_NO_ERROR: |
| 1436 | return true; |
| 1437 | case ::STATE_UNINITIALIZED: |
| 1438 | case ::STATE_LOCKED: |
| 1439 | return false; |
| 1440 | } |
| 1441 | return false; |
| 1442 | } |
| 1443 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1444 | /** |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1445 | * Check that all KeyParameters provided by the application are allowed. Any parameter that keystore |
| 1446 | * adds itself should be disallowed here. |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1447 | */ |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1448 | bool KeyStoreService::checkAllowedOperationParams(const hidl_vec<KeyParameter>& params) { |
| 1449 | for (size_t i = 0; i < params.size(); ++i) { |
| 1450 | switch (params[i].tag) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1451 | case Tag::ATTESTATION_APPLICATION_ID: |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1452 | case Tag::RESET_SINCE_ID_ROTATION: |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1453 | return false; |
| 1454 | default: |
| 1455 | break; |
| 1456 | } |
| 1457 | } |
| 1458 | return true; |
| 1459 | } |
| 1460 | |
Brian Young | 9a947d5 | 2018-02-23 18:03:14 +0000 | [diff] [blame] | 1461 | Status KeyStoreService::onKeyguardVisibilityChanged(bool isShowing, int32_t userId, |
Janis Danisevskis | 1642dc0 | 2020-01-21 14:33:30 -0800 | [diff] [blame] | 1462 | int32_t* _aidl_return) { |
Janis Danisevskis | 1642dc0 | 2020-01-21 14:33:30 -0800 | [diff] [blame] | 1463 | if (isShowing) { |
| 1464 | if (!checkBinderPermission(P_LOCK, UID_SELF)) { |
| 1465 | LOG(WARNING) << "onKeyguardVisibilityChanged called with isShowing == true but " |
| 1466 | "without LOCK permission"; |
| 1467 | return AIDL_RETURN(ResponseCode::PERMISSION_DENIED); |
| 1468 | } |
| 1469 | } else { |
| 1470 | if (!checkBinderPermission(P_UNLOCK, UID_SELF)) { |
| 1471 | LOG(WARNING) << "onKeyguardVisibilityChanged called with isShowing == false but " |
| 1472 | "without UNLOCK permission"; |
| 1473 | return AIDL_RETURN(ResponseCode::PERMISSION_DENIED); |
| 1474 | } |
| 1475 | } |
Janis Danisevskis | ff3d7f4 | 2018-10-08 07:15:09 -0700 | [diff] [blame] | 1476 | mKeyStore->getEnforcementPolicy().set_device_locked(isShowing, userId); |
Janis Danisevskis | 1642dc0 | 2020-01-21 14:33:30 -0800 | [diff] [blame] | 1477 | return AIDL_RETURN(ResponseCode::NO_ERROR); |
Brian Young | 9371e95 | 2018-02-23 18:03:14 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1480 | } // namespace keystore |