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