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