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