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