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