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> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 26 | #include <sstream> |
| 27 | |
Pavel Grafov | ff311b4 | 2018-01-24 20:34:37 +0000 | [diff] [blame] | 28 | #include <android-base/scopeguard.h> |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 29 | #include <binder/IInterface.h> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 30 | #include <binder/IPCThreadState.h> |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 31 | #include <binder/IPermissionController.h> |
| 32 | #include <binder/IServiceManager.h> |
Brian Claire Young | 3133c45 | 2018-08-31 13:56:49 -0700 | [diff] [blame] | 33 | #include <cutils/multiuser.h> |
Pavel Grafov | ff311b4 | 2018-01-24 20:34:37 +0000 | [diff] [blame] | 34 | #include <log/log_event_list.h> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 35 | |
| 36 | #include <private/android_filesystem_config.h> |
Pavel Grafov | ff311b4 | 2018-01-24 20:34:37 +0000 | [diff] [blame] | 37 | #include <private/android_logger.h> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 38 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 39 | #include <android/hardware/keymaster/3.0/IHwKeymasterDevice.h> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 40 | |
| 41 | #include "defaults.h" |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 42 | #include "key_proto_handler.h" |
Janis Danisevskis | 18f27ad | 2016-06-01 13:57:40 -0700 | [diff] [blame] | 43 | #include "keystore_attestation_id.h" |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 44 | #include "keystore_keymaster_enforcement.h" |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 45 | #include "keystore_utils.h" |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 46 | #include <keystore/keystore_hidl_support.h> |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 47 | |
Janis Danisevskis | 8f737ad | 2017-11-21 12:30:15 -0800 | [diff] [blame] | 48 | #include <hardware/hw_auth_token.h> |
| 49 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 50 | namespace keystore { |
Shawn Willden | d5a24e6 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 51 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 52 | using namespace android; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 53 | |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 54 | namespace { |
| 55 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 56 | using ::android::binder::Status; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 57 | using android::security::KeystoreArg; |
| 58 | using android::security::keymaster::ExportResult; |
| 59 | using android::security::keymaster::KeymasterArguments; |
| 60 | using android::security::keymaster::KeymasterBlob; |
| 61 | using android::security::keymaster::KeymasterCertificateChain; |
| 62 | using android::security::keymaster::OperationResult; |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 63 | using ConfirmationResponseCode = android::hardware::confirmationui::V1_0::ResponseCode; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 64 | |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 65 | constexpr size_t kMaxOperations = 15; |
| 66 | constexpr double kIdRotationPeriod = 30 * 24 * 60 * 60; /* Thirty days, in seconds */ |
| 67 | const char* kTimestampFilePath = "timestamp"; |
Rubin Xu | 1a203e3 | 2018-05-08 14:25:21 +0100 | [diff] [blame] | 68 | const int ID_ATTESTATION_REQUEST_GENERIC_INFO = 1 << 0; |
| 69 | const int ID_ATTESTATION_REQUEST_UNIQUE_DEVICE_ID = 1 << 1; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 70 | |
| 71 | struct BIGNUM_Delete { |
| 72 | void operator()(BIGNUM* p) const { BN_free(p); } |
| 73 | }; |
Janis Danisevskis | ccfff10 | 2017-05-01 11:02:51 -0700 | [diff] [blame] | 74 | typedef std::unique_ptr<BIGNUM, BIGNUM_Delete> Unique_BIGNUM; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 75 | |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 76 | bool containsTag(const hidl_vec<KeyParameter>& params, Tag tag) { |
| 77 | return params.end() != std::find_if(params.begin(), params.end(), |
| 78 | [&](auto& param) { return param.tag == tag; }); |
| 79 | } |
| 80 | |
Shawn Willden | d5a24e6 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 81 | bool isAuthenticationBound(const hidl_vec<KeyParameter>& params) { |
| 82 | return !containsTag(params, Tag::NO_AUTH_REQUIRED); |
| 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 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 146 | void KeyStoreService::binderDied(const wp<IBinder>& who) { |
| 147 | auto operations = mOperationMap.getOperationsForToken(who.unsafe_get()); |
Chih-Hung Hsieh | 24b2a39 | 2016-07-28 10:35:24 -0700 | [diff] [blame] | 148 | for (const auto& token : operations) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 149 | int32_t unused_result; |
| 150 | abort(token, &unused_result); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 151 | } |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 152 | mConfirmationManager->binderDied(who); |
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::getState(int32_t userId, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 156 | if (!checkBinderPermission(P_GET_STATE)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 157 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 158 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 159 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 160 | *aidl_return = mKeyStore->getState(userId); |
| 161 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 164 | 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] | 165 | uid_t targetUid = getEffectiveUid(uid); |
| 166 | if (!checkBinderPermission(P_GET, targetUid)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 167 | // see keystore/keystore.h |
| 168 | return Status::fromServiceSpecificError( |
| 169 | static_cast<int32_t>(ResponseCode::PERMISSION_DENIED)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | String8 name8(name); |
| 173 | Blob keyBlob; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 174 | KeyStoreServiceReturnCode rc = |
| 175 | mKeyStore->getKeyForName(&keyBlob, name8, targetUid, TYPE_GENERIC); |
| 176 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 177 | *item = ::std::vector<uint8_t>(); |
| 178 | // Return empty array if key is not found |
| 179 | // TODO: consider having returned value nullable or parse exception on the client. |
| 180 | return Status::fromServiceSpecificError(static_cast<int32_t>(rc)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 181 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 182 | auto resultBlob = blob2hidlVec(keyBlob); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 183 | // The static_cast here is needed to prevent a move, forcing a deep copy. |
| 184 | if (item) *item = static_cast<const hidl_vec<uint8_t>&>(blob2hidlVec(keyBlob)); |
| 185 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 188 | Status KeyStoreService::insert(const String16& name, const ::std::vector<uint8_t>& item, |
| 189 | int targetUid, int32_t flags, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 190 | targetUid = getEffectiveUid(targetUid); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 191 | KeyStoreServiceReturnCode result = |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 192 | checkBinderPermissionAndKeystoreState(P_INSERT, targetUid, flags & KEYSTORE_FLAG_ENCRYPTED); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 193 | if (!result.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 194 | *aidl_return = static_cast<int32_t>(result); |
| 195 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | String8 name8(name); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 199 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, targetUid, ::TYPE_GENERIC)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 200 | |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 201 | Blob keyBlob(&item[0], item.size(), nullptr, 0, ::TYPE_GENERIC); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 202 | keyBlob.setEncrypted(flags & KEYSTORE_FLAG_ENCRYPTED); |
| 203 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 204 | *aidl_return = |
| 205 | static_cast<int32_t>(mKeyStore->put(filename.string(), &keyBlob, get_user_id(targetUid))); |
| 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 | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 217 | auto filename = mKeyStore->getBlobFileNameIfExists(name8, targetUid, ::TYPE_ANY); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 218 | if (!filename.isOk()) { |
| 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 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 223 | ResponseCode result = |
| 224 | mKeyStore->del(filename.value().string(), ::TYPE_ANY, get_user_id(targetUid)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 225 | if (result != ResponseCode::NO_ERROR) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 226 | *aidl_return = static_cast<int32_t>(result); |
| 227 | return Status::ok(); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 228 | } |
| 229 | |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 230 | filename = mKeyStore->getBlobFileNameIfExists(name8, targetUid, ::TYPE_KEY_CHARACTERISTICS); |
| 231 | if (filename.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 232 | *aidl_return = static_cast<int32_t>(mKeyStore->del( |
| 233 | filename.value().string(), ::TYPE_KEY_CHARACTERISTICS, get_user_id(targetUid))); |
| 234 | return Status::ok(); |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 235 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 236 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 237 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 240 | Status KeyStoreService::exist(const String16& name, int targetUid, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 241 | targetUid = getEffectiveUid(targetUid); |
| 242 | if (!checkBinderPermission(P_EXIST, targetUid)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 243 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 244 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 247 | auto filename = mKeyStore->getBlobFileNameIfExists(String8(name), targetUid, ::TYPE_ANY); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 248 | *aidl_return = static_cast<int32_t>(filename.isOk() ? ResponseCode::NO_ERROR |
| 249 | : ResponseCode::KEY_NOT_FOUND); |
| 250 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 253 | Status KeyStoreService::list(const String16& prefix, int targetUid, |
| 254 | ::std::vector<::android::String16>* matches) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 255 | targetUid = getEffectiveUid(targetUid); |
| 256 | if (!checkBinderPermission(P_LIST, targetUid)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 257 | return Status::fromServiceSpecificError( |
| 258 | static_cast<int32_t>(ResponseCode::PERMISSION_DENIED)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 259 | } |
| 260 | const String8 prefix8(prefix); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 261 | String8 filename(mKeyStore->getKeyNameForUid(prefix8, targetUid, TYPE_ANY)); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 262 | android::Vector<android::String16> matches_internal; |
| 263 | if (mKeyStore->list(filename, &matches_internal, get_user_id(targetUid)) != |
| 264 | ResponseCode::NO_ERROR) { |
| 265 | return Status::fromServiceSpecificError(static_cast<int32_t>(ResponseCode::SYSTEM_ERROR)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 266 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 267 | matches->clear(); |
| 268 | for (size_t i = 0; i < matches_internal.size(); ++i) { |
| 269 | matches->push_back(matches_internal[i]); |
| 270 | } |
| 271 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 274 | Status KeyStoreService::reset(int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 275 | if (!checkBinderPermission(P_RESET)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 276 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 277 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 281 | mKeyStore->resetUser(get_user_id(callingUid), false); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 282 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 283 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 286 | Status KeyStoreService::onUserPasswordChanged(int32_t userId, const String16& password, |
| 287 | int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 288 | if (!checkBinderPermission(P_PASSWORD)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 289 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 290 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | const String8 password8(password); |
| 294 | // Flush the auth token table to prevent stale tokens from sticking |
| 295 | // around. |
| 296 | mAuthTokenTable.Clear(); |
| 297 | |
| 298 | if (password.size() == 0) { |
| 299 | ALOGI("Secure lockscreen for user %d removed, deleting encrypted entries", userId); |
| 300 | mKeyStore->resetUser(userId, true); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 301 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 302 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 303 | } else { |
| 304 | switch (mKeyStore->getState(userId)) { |
| 305 | case ::STATE_UNINITIALIZED: { |
| 306 | // generate master key, encrypt with password, write to file, |
| 307 | // initialize mMasterKey*. |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 308 | *aidl_return = static_cast<int32_t>(mKeyStore->initializeUser(password8, userId)); |
| 309 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 310 | } |
| 311 | case ::STATE_NO_ERROR: { |
| 312 | // rewrite master key with new password. |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 313 | *aidl_return = static_cast<int32_t>(mKeyStore->writeMasterKey(password8, userId)); |
| 314 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 315 | } |
| 316 | case ::STATE_LOCKED: { |
| 317 | ALOGE("Changing user %d's password while locked, clearing old encryption", userId); |
| 318 | mKeyStore->resetUser(userId, true); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 319 | *aidl_return = static_cast<int32_t>(mKeyStore->initializeUser(password8, userId)); |
| 320 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 323 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 324 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 328 | 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] | 329 | if (!checkBinderPermission(P_USER_CHANGED)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 330 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 331 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | // Sanity check that the new user has an empty keystore. |
| 335 | if (!mKeyStore->isEmpty(userId)) { |
| 336 | ALOGW("New user %d's keystore not empty. Clearing old entries.", userId); |
| 337 | } |
| 338 | // Unconditionally clear the keystore, just to be safe. |
| 339 | mKeyStore->resetUser(userId, false); |
| 340 | if (parentId != -1) { |
| 341 | // This profile must share the same master key password as the parent profile. Because the |
| 342 | // password of the parent profile is not known here, the best we can do is copy the parent's |
| 343 | // master key and master key file. This makes this profile use the same master key as the |
| 344 | // parent profile, forever. |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 345 | *aidl_return = static_cast<int32_t>(mKeyStore->copyMasterKey(parentId, userId)); |
| 346 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 347 | } else { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 348 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 349 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 353 | Status KeyStoreService::onUserRemoved(int32_t userId, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 354 | if (!checkBinderPermission(P_USER_CHANGED)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 355 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 356 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | mKeyStore->resetUser(userId, false); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 360 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 361 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 364 | Status KeyStoreService::lock(int32_t userId, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 365 | if (!checkBinderPermission(P_LOCK)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 366 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 367 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | State state = mKeyStore->getState(userId); |
| 371 | if (state != ::STATE_NO_ERROR) { |
| 372 | ALOGD("calling lock in state: %d", state); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 373 | *aidl_return = static_cast<int32_t>(ResponseCode(state)); |
| 374 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Brian Young | 9a947d5 | 2018-02-23 18:03:14 +0000 | [diff] [blame] | 377 | enforcement_policy.set_device_locked(true, userId); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 378 | mKeyStore->lock(userId); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 379 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 380 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 383 | 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] | 384 | if (!checkBinderPermission(P_UNLOCK)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 385 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 386 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | State state = mKeyStore->getState(userId); |
| 390 | if (state != ::STATE_LOCKED) { |
| 391 | switch (state) { |
| 392 | case ::STATE_NO_ERROR: |
| 393 | ALOGI("calling unlock when already unlocked, ignoring."); |
| 394 | break; |
| 395 | case ::STATE_UNINITIALIZED: |
| 396 | ALOGE("unlock called on uninitialized keystore."); |
| 397 | break; |
| 398 | default: |
| 399 | ALOGE("unlock called on keystore in unknown state: %d", state); |
| 400 | break; |
| 401 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 402 | *aidl_return = static_cast<int32_t>(ResponseCode(state)); |
| 403 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Brian Young | 9a947d5 | 2018-02-23 18:03:14 +0000 | [diff] [blame] | 406 | enforcement_policy.set_device_locked(false, userId); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 407 | const String8 password8(pw); |
| 408 | // read master key, decrypt with password, initialize mMasterKey*. |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 409 | *aidl_return = static_cast<int32_t>(mKeyStore->readMasterKey(password8, userId)); |
| 410 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 413 | Status KeyStoreService::isEmpty(int32_t userId, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 414 | if (!checkBinderPermission(P_IS_EMPTY)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 415 | *aidl_return = static_cast<int32_t>(false); |
| 416 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 419 | *aidl_return = static_cast<int32_t>(mKeyStore->isEmpty(userId)); |
| 420 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 423 | Status KeyStoreService::grant(const String16& name, int32_t granteeUid, |
| 424 | ::android::String16* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 425 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
Bo Zhu | 91de6db | 2018-03-20 12:52:13 -0700 | [diff] [blame] | 426 | auto result = |
| 427 | checkBinderPermissionAndKeystoreState(P_GRANT, /*targetUid=*/-1, /*checkUnlocked=*/false); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 428 | if (!result.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 429 | *aidl_return = String16(); |
| 430 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | String8 name8(name); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 434 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, callingUid, ::TYPE_ANY)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 435 | |
| 436 | if (access(filename.string(), R_OK) == -1) { |
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 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 441 | *aidl_return = |
| 442 | String16(mKeyStore->addGrant(String8(name).string(), callingUid, granteeUid).c_str()); |
| 443 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 446 | 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] | 447 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
Bo Zhu | 91de6db | 2018-03-20 12:52:13 -0700 | [diff] [blame] | 448 | KeyStoreServiceReturnCode result = |
| 449 | checkBinderPermissionAndKeystoreState(P_GRANT, /*targetUid=*/-1, /*checkUnlocked=*/false); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 450 | if (!result.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 451 | *aidl_return = static_cast<int32_t>(result); |
| 452 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | String8 name8(name); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 456 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, callingUid, ::TYPE_ANY)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 457 | |
| 458 | if (access(filename.string(), R_OK) == -1) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 459 | *aidl_return = static_cast<int32_t>((errno != ENOENT) ? ResponseCode::SYSTEM_ERROR |
| 460 | : ResponseCode::KEY_NOT_FOUND); |
| 461 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 464 | *aidl_return = static_cast<int32_t>(mKeyStore->removeGrant(name8, callingUid, granteeUid) |
| 465 | ? ResponseCode::NO_ERROR |
| 466 | : ResponseCode::KEY_NOT_FOUND); |
| 467 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 470 | Status KeyStoreService::getmtime(const String16& name, int32_t uid, int64_t* time) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 471 | uid_t targetUid = getEffectiveUid(uid); |
| 472 | if (!checkBinderPermission(P_GET, targetUid)) { |
| 473 | ALOGW("permission denied for %d: getmtime", targetUid); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 474 | *time = -1L; |
| 475 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 478 | auto filename = mKeyStore->getBlobFileNameIfExists(String8(name), targetUid, ::TYPE_ANY); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 479 | |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 480 | if (!filename.isOk()) { |
| 481 | ALOGW("could not access %s for getmtime", filename.value().string()); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 482 | *time = -1L; |
| 483 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 486 | int fd = TEMP_FAILURE_RETRY(open(filename.value().string(), O_NOFOLLOW, O_RDONLY)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 487 | if (fd < 0) { |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 488 | ALOGW("could not open %s for getmtime", filename.value().string()); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 489 | *time = -1L; |
| 490 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | struct stat s; |
| 494 | int ret = fstat(fd, &s); |
| 495 | close(fd); |
| 496 | if (ret == -1) { |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 497 | ALOGW("could not stat %s for getmtime", filename.value().string()); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 498 | *time = -1L; |
| 499 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 502 | *time = static_cast<int64_t>(s.st_mtime); |
| 503 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 506 | Status KeyStoreService::is_hardware_backed(const String16& keyType, int32_t* aidl_return) { |
| 507 | *aidl_return = static_cast<int32_t>(mKeyStore->isHardwareBacked(keyType) ? 1 : 0); |
| 508 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 511 | Status KeyStoreService::clear_uid(int64_t targetUid64, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 512 | uid_t targetUid = getEffectiveUid(targetUid64); |
| 513 | if (!checkBinderPermissionSelfOrSystem(P_CLEAR_UID, targetUid)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 514 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 515 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 516 | } |
Rubin Xu | 7675c9f | 2017-03-15 19:26:52 +0000 | [diff] [blame] | 517 | ALOGI("clear_uid %" PRId64, targetUid64); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 518 | |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 519 | mKeyStore->removeAllGrantsToUid(targetUid); |
| 520 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 521 | String8 prefix = String8::format("%u_", targetUid); |
| 522 | Vector<String16> aliases; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 523 | if (mKeyStore->list(prefix, &aliases, get_user_id(targetUid)) != ResponseCode::NO_ERROR) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 524 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 525 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | for (uint32_t i = 0; i < aliases.size(); i++) { |
| 529 | String8 name8(aliases[i]); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 530 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, targetUid, ::TYPE_ANY)); |
Rubin Xu | 85c85e9 | 2017-04-26 20:07:30 +0100 | [diff] [blame] | 531 | |
| 532 | if (get_app_id(targetUid) == AID_SYSTEM) { |
| 533 | Blob keyBlob; |
| 534 | ResponseCode responseCode = |
| 535 | mKeyStore->get(filename.string(), &keyBlob, ::TYPE_ANY, get_user_id(targetUid)); |
| 536 | if (responseCode == ResponseCode::NO_ERROR && keyBlob.isCriticalToDeviceEncryption()) { |
| 537 | // Do not clear keys critical to device encryption under system uid. |
| 538 | continue; |
| 539 | } |
| 540 | } |
| 541 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 542 | mKeyStore->del(filename.string(), ::TYPE_ANY, get_user_id(targetUid)); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 543 | |
| 544 | // del() will fail silently if no cached characteristics are present for this alias. |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 545 | String8 chr_filename( |
| 546 | mKeyStore->getKeyNameForUidWithDir(name8, targetUid, ::TYPE_KEY_CHARACTERISTICS)); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 547 | mKeyStore->del(chr_filename.string(), ::TYPE_KEY_CHARACTERISTICS, get_user_id(targetUid)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 548 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 549 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 550 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 553 | Status KeyStoreService::addRngEntropy(const ::std::vector<uint8_t>& entropy, int32_t flags, |
| 554 | int32_t* aidl_return) { |
| 555 | auto device = mKeyStore->getDevice(flagsToSecurityLevel(flags)); |
| 556 | if (!device) { |
| 557 | *aidl_return = static_cast<int32_t>(ErrorCode::HARDWARE_TYPE_UNAVAILABLE); |
| 558 | } else { |
| 559 | *aidl_return = static_cast<int32_t>( |
| 560 | KeyStoreServiceReturnCode(KS_HANDLE_HIDL_ERROR(device->addRngEntropy(entropy)))); |
| 561 | } |
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 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 607 | AuthorizationSet keyCharacteristics = params.getParameters(); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 608 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 609 | // TODO: Seed from Linux RNG before this. |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 610 | rc = KS_HANDLE_HIDL_ERROR(dev->addRngEntropy(entropy)); |
| 611 | if (!rc.isOk()) { |
| 612 | *aidl_return = static_cast<int32_t>(rc); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 613 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 616 | KeyStoreServiceReturnCode error; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 617 | auto hidl_cb = [&](ErrorCode ret, const ::std::vector<uint8_t>& hidlKeyBlob, |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 618 | const KeyCharacteristics& keyCharacteristics) { |
| 619 | error = ret; |
| 620 | if (!error.isOk()) { |
| 621 | return; |
| 622 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 623 | if (outCharacteristics) |
| 624 | *outCharacteristics = |
| 625 | ::android::security::keymaster::KeyCharacteristics(keyCharacteristics); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 626 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 627 | // Write the key |
| 628 | String8 name8(name); |
| 629 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, uid, ::TYPE_KEYMASTER_10)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 630 | |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 631 | Blob keyBlob(&hidlKeyBlob[0], hidlKeyBlob.size(), nullptr, 0, ::TYPE_KEYMASTER_10); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 632 | keyBlob.setSecurityLevel(securityLevel); |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 633 | keyBlob.setCriticalToDeviceEncryption(flags & KEYSTORE_FLAG_CRITICAL_TO_DEVICE_ENCRYPTION); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 634 | if (isAuthenticationBound(params.getParameters()) && |
| 635 | !keyBlob.isCriticalToDeviceEncryption()) { |
Shawn Willden | d5a24e6 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 636 | keyBlob.setSuperEncrypted(true); |
| 637 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 638 | keyBlob.setEncrypted(flags & KEYSTORE_FLAG_ENCRYPTED); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 639 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 640 | error = mKeyStore->put(filename.string(), &keyBlob, get_user_id(uid)); |
| 641 | }; |
| 642 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 643 | rc = KS_HANDLE_HIDL_ERROR(dev->generateKey(params.getParameters(), hidl_cb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 644 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 645 | *aidl_return = static_cast<int32_t>(rc); |
| 646 | return Status::ok(); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 647 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 648 | if (!error.isOk()) { |
| 649 | ALOGE("Failed to generate key -> falling back to software keymaster"); |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 650 | uploadKeyCharacteristicsAsProto(params.getParameters(), false /* wasCreationSuccessful */); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 651 | securityLevel = SecurityLevel::SOFTWARE; |
Janis Danisevskis | 4a1da2f | 2018-03-26 15:02:38 -0700 | [diff] [blame] | 652 | |
| 653 | // No fall back for 3DES |
| 654 | for (auto& param : params.getParameters()) { |
| 655 | auto algorithm = authorizationValue(TAG_ALGORITHM, param); |
| 656 | if (algorithm.isOk() && algorithm.value() == Algorithm::TRIPLE_DES) { |
| 657 | *aidl_return = static_cast<int32_t>(ErrorCode::UNSUPPORTED_ALGORITHM); |
| 658 | return Status::ok(); |
| 659 | } |
| 660 | } |
| 661 | |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 662 | auto fallback = mKeyStore->getFallbackDevice(); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 663 | if (!fallback) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 664 | *aidl_return = static_cast<int32_t>(error); |
| 665 | return Status::ok(); |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 666 | } |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 667 | rc = KS_HANDLE_HIDL_ERROR(fallback->generateKey(params.getParameters(), hidl_cb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 668 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 669 | *aidl_return = static_cast<int32_t>(rc); |
| 670 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 671 | } |
| 672 | if (!error.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 673 | *aidl_return = static_cast<int32_t>(error); |
| 674 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 675 | } |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 676 | } else { |
| 677 | uploadKeyCharacteristicsAsProto(params.getParameters(), true /* wasCreationSuccessful */); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 678 | } |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 679 | |
Brian C. Young | 78daac2 | 2018-03-31 15:02:34 -0700 | [diff] [blame] | 680 | if (!containsTag(params.getParameters(), Tag::USER_ID)) { |
| 681 | // Most Java processes don't have access to this tag |
| 682 | KeyParameter user_id; |
| 683 | user_id.tag = Tag::USER_ID; |
Brian Claire Young | 3133c45 | 2018-08-31 13:56:49 -0700 | [diff] [blame] | 684 | user_id.f.integer = multiuser_get_user_id(uid); |
Brian C. Young | 78daac2 | 2018-03-31 15:02:34 -0700 | [diff] [blame] | 685 | keyCharacteristics.push_back(user_id); |
| 686 | } |
| 687 | |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 688 | // Write the characteristics: |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 689 | String8 name8(name); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 690 | String8 cFilename(mKeyStore->getKeyNameForUidWithDir(name8, uid, ::TYPE_KEY_CHARACTERISTICS)); |
| 691 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 692 | std::stringstream kc_stream; |
| 693 | keyCharacteristics.Serialize(&kc_stream); |
| 694 | if (kc_stream.bad()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 695 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 696 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 697 | } |
| 698 | auto kc_buf = kc_stream.str(); |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 699 | Blob charBlob(reinterpret_cast<const uint8_t*>(kc_buf.data()), kc_buf.size(), nullptr, 0, |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 700 | ::TYPE_KEY_CHARACTERISTICS); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 701 | charBlob.setSecurityLevel(securityLevel); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 702 | charBlob.setEncrypted(flags & KEYSTORE_FLAG_ENCRYPTED); |
| 703 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 704 | *aidl_return = |
| 705 | static_cast<int32_t>(mKeyStore->put(cFilename.string(), &charBlob, get_user_id(uid))); |
| 706 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 709 | Status KeyStoreService::getKeyCharacteristics( |
| 710 | const String16& name, const ::android::security::keymaster::KeymasterBlob& clientId, |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 711 | const ::android::security::keymaster::KeymasterBlob& appData, int32_t uid, |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 712 | ::android::security::keymaster::KeyCharacteristics* outCharacteristics, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 713 | if (!outCharacteristics) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 714 | *aidl_return = |
| 715 | static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::UNEXPECTED_NULL_POINTER)); |
| 716 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | uid_t targetUid = getEffectiveUid(uid); |
| 720 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 721 | if (!is_granted_to(callingUid, targetUid)) { |
| 722 | ALOGW("uid %d not permitted to act for uid %d in getKeyCharacteristics", callingUid, |
| 723 | targetUid); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 724 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 725 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | Blob keyBlob; |
| 729 | String8 name8(name); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 730 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 731 | KeyStoreServiceReturnCode rc = |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 732 | mKeyStore->getKeyForName(&keyBlob, name8, targetUid, TYPE_KEYMASTER_10); |
Janis Danisevskis | d714a67 | 2017-09-01 14:31:36 -0700 | [diff] [blame] | 733 | if (rc == ResponseCode::UNINITIALIZED) { |
| 734 | /* |
| 735 | * If we fail reading the blob because the master key is missing we try to retrieve the |
| 736 | * key characteristics from the characteristics file. This happens when auth-bound |
| 737 | * keys are used after a screen lock has been removed by the user. |
| 738 | */ |
| 739 | rc = mKeyStore->getKeyForName(&keyBlob, name8, targetUid, TYPE_KEY_CHARACTERISTICS); |
| 740 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 741 | *aidl_return = static_cast<int32_t>(rc); |
| 742 | return Status::ok(); |
Janis Danisevskis | d714a67 | 2017-09-01 14:31:36 -0700 | [diff] [blame] | 743 | } |
| 744 | AuthorizationSet keyCharacteristics; |
| 745 | // TODO write one shot stream buffer to avoid copying (twice here) |
| 746 | std::string charBuffer(reinterpret_cast<const char*>(keyBlob.getValue()), |
| 747 | keyBlob.getLength()); |
| 748 | std::stringstream charStream(charBuffer); |
| 749 | keyCharacteristics.Deserialize(&charStream); |
| 750 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 751 | outCharacteristics->softwareEnforced = KeymasterArguments(keyCharacteristics.hidl_data()); |
| 752 | *aidl_return = static_cast<int32_t>(rc); |
| 753 | return Status::ok(); |
Janis Danisevskis | d714a67 | 2017-09-01 14:31:36 -0700 | [diff] [blame] | 754 | } else if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 755 | *aidl_return = static_cast<int32_t>(rc); |
| 756 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 757 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 758 | |
| 759 | auto hidlKeyBlob = blob2hidlVec(keyBlob); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 760 | auto dev = mKeyStore->getDevice(keyBlob); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 761 | |
| 762 | KeyStoreServiceReturnCode error; |
| 763 | |
| 764 | auto hidlCb = [&](ErrorCode ret, const KeyCharacteristics& keyCharacteristics) { |
| 765 | error = ret; |
| 766 | if (!error.isOk()) { |
Pavel Grafov | cef3947 | 2018-02-12 18:45:02 +0000 | [diff] [blame] | 767 | if (error == ErrorCode::INVALID_KEY_BLOB) { |
| 768 | log_key_integrity_violation(name8, targetUid); |
| 769 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 770 | return; |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 771 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 772 | *outCharacteristics = |
| 773 | ::android::security::keymaster::KeyCharacteristics(keyCharacteristics); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 774 | }; |
| 775 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 776 | rc = KS_HANDLE_HIDL_ERROR( |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 777 | dev->getKeyCharacteristics(hidlKeyBlob, clientId.getData(), appData.getData(), hidlCb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 778 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 779 | *aidl_return = static_cast<int32_t>(rc); |
| 780 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | if (error == ErrorCode::KEY_REQUIRES_UPGRADE) { |
| 784 | AuthorizationSet upgradeParams; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 785 | if (clientId.getData().size()) { |
| 786 | upgradeParams.push_back(TAG_APPLICATION_ID, clientId.getData()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 787 | } |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 788 | if (appData.getData().size()) { |
| 789 | upgradeParams.push_back(TAG_APPLICATION_DATA, appData.getData()); |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 790 | } |
| 791 | rc = upgradeKeyBlob(name, targetUid, upgradeParams, &keyBlob); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 792 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 793 | *aidl_return = static_cast<int32_t>(rc); |
| 794 | return Status::ok(); |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 795 | } |
Shawn Willden | 715d023 | 2016-01-21 00:45:13 -0700 | [diff] [blame] | 796 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 797 | auto upgradedHidlKeyBlob = blob2hidlVec(keyBlob); |
| 798 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 799 | rc = KS_HANDLE_HIDL_ERROR(dev->getKeyCharacteristics( |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 800 | upgradedHidlKeyBlob, clientId.getData(), appData.getData(), hidlCb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 801 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 802 | *aidl_return = static_cast<int32_t>(rc); |
| 803 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 804 | } |
| 805 | // Note that, on success, "error" will have been updated by the hidlCB callback. |
| 806 | // So it is fine to return "error" below. |
| 807 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 808 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(error)); |
| 809 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 810 | } |
| 811 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 812 | Status |
| 813 | KeyStoreService::importKey(const String16& name, const KeymasterArguments& params, int32_t format, |
| 814 | const ::std::vector<uint8_t>& keyData, int uid, int flags, |
| 815 | ::android::security::keymaster::KeyCharacteristics* outCharacteristics, |
| 816 | int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 817 | uid = getEffectiveUid(uid); |
Pavel Grafov | ff311b4 | 2018-01-24 20:34:37 +0000 | [diff] [blame] | 818 | auto logOnScopeExit = android::base::make_scope_guard([&] { |
| 819 | if (__android_log_security()) { |
| 820 | android_log_event_list(SEC_TAG_KEY_IMPORTED) |
| 821 | << int32_t(*aidl_return == static_cast<int32_t>(ResponseCode::NO_ERROR)) |
| 822 | << String8(name) << int32_t(uid) << LOG_ID_SECURITY; |
| 823 | } |
| 824 | }); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 825 | KeyStoreServiceReturnCode rc = |
| 826 | checkBinderPermissionAndKeystoreState(P_INSERT, uid, flags & KEYSTORE_FLAG_ENCRYPTED); |
| 827 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 828 | *aidl_return = static_cast<int32_t>(rc); |
| 829 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 830 | } |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 831 | if ((flags & KEYSTORE_FLAG_CRITICAL_TO_DEVICE_ENCRYPTION) && get_app_id(uid) != AID_SYSTEM) { |
| 832 | 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] | 833 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 834 | return Status::ok(); |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 835 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 836 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 837 | SecurityLevel securityLevel = flagsToSecurityLevel(flags); |
| 838 | auto dev = mKeyStore->getDevice(securityLevel); |
| 839 | if (!dev) { |
| 840 | *aidl_return = static_cast<int32_t>(ErrorCode::HARDWARE_TYPE_UNAVAILABLE); |
| 841 | return Status::ok(); |
| 842 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 843 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 844 | String8 name8(name); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 845 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 846 | KeyStoreServiceReturnCode error; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 847 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 848 | auto hidlCb = [&](ErrorCode ret, const ::std::vector<uint8_t>& keyBlob, |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 849 | const KeyCharacteristics& keyCharacteristics) { |
| 850 | error = ret; |
| 851 | if (!error.isOk()) { |
| 852 | return; |
| 853 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 854 | if (outCharacteristics) |
| 855 | *outCharacteristics = |
| 856 | ::android::security::keymaster::KeyCharacteristics(keyCharacteristics); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 857 | |
| 858 | // Write the key: |
| 859 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, uid, ::TYPE_KEYMASTER_10)); |
| 860 | |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 861 | Blob ksBlob(&keyBlob[0], keyBlob.size(), nullptr, 0, ::TYPE_KEYMASTER_10); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 862 | ksBlob.setSecurityLevel(securityLevel); |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 863 | ksBlob.setCriticalToDeviceEncryption(flags & KEYSTORE_FLAG_CRITICAL_TO_DEVICE_ENCRYPTION); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 864 | if (isAuthenticationBound(params.getParameters()) && |
| 865 | !ksBlob.isCriticalToDeviceEncryption()) { |
Shawn Willden | d5a24e6 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 866 | ksBlob.setSuperEncrypted(true); |
| 867 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 868 | ksBlob.setEncrypted(flags & KEYSTORE_FLAG_ENCRYPTED); |
| 869 | |
| 870 | error = mKeyStore->put(filename.string(), &ksBlob, get_user_id(uid)); |
| 871 | }; |
| 872 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 873 | rc = KS_HANDLE_HIDL_ERROR( |
| 874 | dev->importKey(params.getParameters(), KeyFormat(format), keyData, hidlCb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 875 | // possible hidl error |
| 876 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 877 | *aidl_return = static_cast<int32_t>(rc); |
| 878 | return Status::ok(); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 879 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 880 | // now check error from callback |
| 881 | if (!error.isOk()) { |
| 882 | ALOGE("Failed to import key -> falling back to software keymaster"); |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 883 | uploadKeyCharacteristicsAsProto(params.getParameters(), false /* wasCreationSuccessful */); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 884 | securityLevel = SecurityLevel::SOFTWARE; |
Janis Danisevskis | 4a1da2f | 2018-03-26 15:02:38 -0700 | [diff] [blame] | 885 | |
| 886 | // No fall back for 3DES |
| 887 | for (auto& param : params.getParameters()) { |
| 888 | auto algorithm = authorizationValue(TAG_ALGORITHM, param); |
| 889 | if (algorithm.isOk() && algorithm.value() == Algorithm::TRIPLE_DES) { |
| 890 | *aidl_return = static_cast<int32_t>(ErrorCode::UNSUPPORTED_ALGORITHM); |
| 891 | return Status::ok(); |
| 892 | } |
| 893 | } |
| 894 | |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 895 | auto fallback = mKeyStore->getFallbackDevice(); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 896 | if (!fallback) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 897 | *aidl_return = static_cast<int32_t>(error); |
| 898 | return Status::ok(); |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 899 | } |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 900 | rc = KS_HANDLE_HIDL_ERROR( |
| 901 | fallback->importKey(params.getParameters(), KeyFormat(format), keyData, hidlCb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 902 | // possible hidl error |
| 903 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 904 | *aidl_return = static_cast<int32_t>(rc); |
| 905 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 906 | } |
| 907 | // now check error from callback |
| 908 | if (!error.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 909 | *aidl_return = static_cast<int32_t>(error); |
| 910 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 911 | } |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 912 | } else { |
| 913 | uploadKeyCharacteristicsAsProto(params.getParameters(), true /* wasCreationSuccessful */); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 914 | } |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 915 | |
| 916 | // Write the characteristics: |
| 917 | String8 cFilename(mKeyStore->getKeyNameForUidWithDir(name8, uid, ::TYPE_KEY_CHARACTERISTICS)); |
| 918 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 919 | AuthorizationSet opParams = params.getParameters(); |
Brian C. Young | 78daac2 | 2018-03-31 15:02:34 -0700 | [diff] [blame] | 920 | if (!containsTag(params.getParameters(), Tag::USER_ID)) { |
| 921 | // Most Java processes don't have access to this tag |
| 922 | KeyParameter user_id; |
| 923 | user_id.tag = Tag::USER_ID; |
Brian Claire Young | 3133c45 | 2018-08-31 13:56:49 -0700 | [diff] [blame] | 924 | user_id.f.integer = multiuser_get_user_id(uid); |
Brian C. Young | 78daac2 | 2018-03-31 15:02:34 -0700 | [diff] [blame] | 925 | opParams.push_back(user_id); |
| 926 | } |
| 927 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 928 | std::stringstream kcStream; |
| 929 | opParams.Serialize(&kcStream); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 930 | if (kcStream.bad()) { |
| 931 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 932 | return Status::ok(); |
| 933 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 934 | auto kcBuf = kcStream.str(); |
| 935 | |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 936 | Blob charBlob(reinterpret_cast<const uint8_t*>(kcBuf.data()), kcBuf.size(), nullptr, 0, |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 937 | ::TYPE_KEY_CHARACTERISTICS); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 938 | charBlob.setSecurityLevel(securityLevel); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 939 | charBlob.setEncrypted(flags & KEYSTORE_FLAG_ENCRYPTED); |
| 940 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 941 | *aidl_return = |
| 942 | static_cast<int32_t>(mKeyStore->put(cFilename.string(), &charBlob, get_user_id(uid))); |
| 943 | |
| 944 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 947 | Status KeyStoreService::exportKey(const String16& name, int32_t format, |
| 948 | const ::android::security::keymaster::KeymasterBlob& clientId, |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 949 | const ::android::security::keymaster::KeymasterBlob& appData, |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 950 | int32_t uid, ExportResult* result) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 951 | |
| 952 | uid_t targetUid = getEffectiveUid(uid); |
| 953 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 954 | if (!is_granted_to(callingUid, targetUid)) { |
| 955 | 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] | 956 | result->resultCode = ResponseCode::PERMISSION_DENIED; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 957 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | Blob keyBlob; |
| 961 | String8 name8(name); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 962 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 963 | result->resultCode = mKeyStore->getKeyForName(&keyBlob, name8, targetUid, TYPE_KEYMASTER_10); |
| 964 | if (!result->resultCode.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 965 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 966 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 967 | |
| 968 | auto key = blob2hidlVec(keyBlob); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 969 | auto dev = mKeyStore->getDevice(keyBlob); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 970 | |
| 971 | auto hidlCb = [&](ErrorCode ret, const ::android::hardware::hidl_vec<uint8_t>& keyMaterial) { |
| 972 | result->resultCode = ret; |
| 973 | if (!result->resultCode.isOk()) { |
Pavel Grafov | cef3947 | 2018-02-12 18:45:02 +0000 | [diff] [blame] | 974 | if (result->resultCode == ErrorCode::INVALID_KEY_BLOB) { |
| 975 | log_key_integrity_violation(name8, targetUid); |
| 976 | } |
Ji Wang | 2c14231 | 2016-10-14 17:21:10 +0800 | [diff] [blame] | 977 | return; |
| 978 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 979 | result->exportData = keyMaterial; |
| 980 | }; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 981 | KeyStoreServiceReturnCode rc = KS_HANDLE_HIDL_ERROR( |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 982 | dev->exportKey(KeyFormat(format), key, clientId.getData(), appData.getData(), hidlCb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 983 | // Overwrite result->resultCode only on HIDL error. Otherwise we want the result set in the |
| 984 | // callback hidlCb. |
| 985 | if (!rc.isOk()) { |
| 986 | result->resultCode = rc; |
Ji Wang | 2c14231 | 2016-10-14 17:21:10 +0800 | [diff] [blame] | 987 | } |
| 988 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 989 | if (result->resultCode == ErrorCode::KEY_REQUIRES_UPGRADE) { |
| 990 | AuthorizationSet upgradeParams; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 991 | if (clientId.getData().size()) { |
| 992 | upgradeParams.push_back(TAG_APPLICATION_ID, clientId.getData()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 993 | } |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 994 | if (appData.getData().size()) { |
| 995 | upgradeParams.push_back(TAG_APPLICATION_DATA, appData.getData()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 996 | } |
| 997 | result->resultCode = upgradeKeyBlob(name, targetUid, upgradeParams, &keyBlob); |
| 998 | if (!result->resultCode.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 999 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | auto upgradedHidlKeyBlob = blob2hidlVec(keyBlob); |
| 1003 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1004 | result->resultCode = KS_HANDLE_HIDL_ERROR(dev->exportKey( |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 1005 | KeyFormat(format), upgradedHidlKeyBlob, clientId.getData(), appData.getData(), hidlCb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1006 | if (!result->resultCode.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1007 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1008 | } |
| 1009 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1010 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1013 | Status KeyStoreService::begin(const sp<IBinder>& appToken, const String16& name, int32_t purpose, |
| 1014 | bool pruneable, const KeymasterArguments& params, |
| 1015 | const ::std::vector<uint8_t>& entropy, int32_t uid, |
| 1016 | OperationResult* result) { |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1017 | auto keyPurpose = static_cast<KeyPurpose>(purpose); |
| 1018 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1019 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1020 | uid_t targetUid = getEffectiveUid(uid); |
| 1021 | if (!is_granted_to(callingUid, targetUid)) { |
| 1022 | 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] | 1023 | result->resultCode = ResponseCode::PERMISSION_DENIED; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1024 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1025 | } |
| 1026 | if (!pruneable && get_app_id(callingUid) != AID_SYSTEM) { |
| 1027 | ALOGE("Non-system uid %d trying to start non-pruneable operation", callingUid); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1028 | result->resultCode = ResponseCode::PERMISSION_DENIED; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1029 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1030 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1031 | if (!checkAllowedOperationParams(params.getParameters())) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1032 | result->resultCode = ErrorCode::INVALID_ARGUMENT; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1033 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1034 | } |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1035 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1036 | Blob keyBlob; |
| 1037 | String8 name8(name); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1038 | result->resultCode = mKeyStore->getKeyForName(&keyBlob, name8, targetUid, TYPE_KEYMASTER_10); |
Shawn Willden | d5a24e6 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 1039 | if (result->resultCode == ResponseCode::LOCKED && keyBlob.isSuperEncrypted()) { |
| 1040 | result->resultCode = ErrorCode::KEY_USER_NOT_AUTHENTICATED; |
| 1041 | } |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1042 | if (!result->resultCode.isOk()) return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1043 | |
| 1044 | auto key = blob2hidlVec(keyBlob); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1045 | auto dev = mKeyStore->getDevice(keyBlob); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1046 | AuthorizationSet opParams = params.getParameters(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1047 | KeyCharacteristics characteristics; |
| 1048 | result->resultCode = getOperationCharacteristics(key, &dev, opParams, &characteristics); |
| 1049 | |
| 1050 | if (result->resultCode == ErrorCode::KEY_REQUIRES_UPGRADE) { |
| 1051 | result->resultCode = upgradeKeyBlob(name, targetUid, opParams, &keyBlob); |
| 1052 | if (!result->resultCode.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1053 | return Status::ok(); |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 1054 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1055 | key = blob2hidlVec(keyBlob); |
| 1056 | result->resultCode = getOperationCharacteristics(key, &dev, opParams, &characteristics); |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 1057 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1058 | if (!result->resultCode.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1059 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1060 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1061 | |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1062 | // Merge these characteristics with the ones cached when the key was generated or imported |
| 1063 | Blob charBlob; |
| 1064 | AuthorizationSet persistedCharacteristics; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1065 | result->resultCode = |
| 1066 | mKeyStore->getKeyForName(&charBlob, name8, targetUid, TYPE_KEY_CHARACTERISTICS); |
| 1067 | if (result->resultCode.isOk()) { |
| 1068 | // TODO write one shot stream buffer to avoid copying (twice here) |
| 1069 | std::string charBuffer(reinterpret_cast<const char*>(charBlob.getValue()), |
| 1070 | charBlob.getLength()); |
| 1071 | std::stringstream charStream(charBuffer); |
| 1072 | persistedCharacteristics.Deserialize(&charStream); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1073 | } else { |
| 1074 | ALOGD("Unable to read cached characteristics for key"); |
| 1075 | } |
| 1076 | |
| 1077 | // Replace the sw_enforced set with those persisted to disk, minus hw_enforced |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1078 | AuthorizationSet softwareEnforced = characteristics.softwareEnforced; |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1079 | AuthorizationSet hardwareEnforced = characteristics.hardwareEnforced; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1080 | persistedCharacteristics.Union(softwareEnforced); |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1081 | persistedCharacteristics.Subtract(hardwareEnforced); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1082 | characteristics.softwareEnforced = persistedCharacteristics.hidl_data(); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1083 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1084 | KeyStoreServiceReturnCode authResult; |
| 1085 | HardwareAuthToken authToken; |
| 1086 | std::tie(authResult, authToken) = |
| 1087 | getAuthToken(characteristics, 0 /* no challenge */, keyPurpose, |
| 1088 | /*failOnTokenMissing*/ false); |
| 1089 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1090 | // If per-operation auth is needed we need to begin the operation and |
| 1091 | // the client will need to authorize that operation before calling |
| 1092 | // update. Any other auth issues stop here. |
Shawn Willden | 827243a | 2017-09-12 05:41:33 -0600 | [diff] [blame] | 1093 | if (!authResult.isOk() && authResult != ResponseCode::OP_AUTH_NEEDED) { |
| 1094 | result->resultCode = authResult; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1095 | return Status::ok(); |
Shawn Willden | 827243a | 2017-09-12 05:41:33 -0600 | [diff] [blame] | 1096 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1097 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1098 | // Add entropy to the device first. |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1099 | if (entropy.size()) { |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1100 | result->resultCode = KS_HANDLE_HIDL_ERROR(dev->addRngEntropy(entropy)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1101 | if (!result->resultCode.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1102 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1103 | } |
| 1104 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1105 | |
| 1106 | // Create a keyid for this key. |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1107 | km_id_t keyid; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1108 | if (!enforcement_policy.CreateKeyId(key, &keyid)) { |
| 1109 | ALOGE("Failed to create a key ID for authorization checking."); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1110 | result->resultCode = ErrorCode::UNKNOWN_ERROR; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1111 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | // Check that all key authorization policy requirements are met. |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1115 | AuthorizationSet key_auths = characteristics.hardwareEnforced; |
| 1116 | key_auths.append(characteristics.softwareEnforced.begin(), |
| 1117 | characteristics.softwareEnforced.end()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1118 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1119 | result->resultCode = |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1120 | enforcement_policy.AuthorizeOperation(keyPurpose, keyid, key_auths, opParams, authToken, |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1121 | 0 /* op_handle */, true /* is_begin_operation */); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1122 | if (!result->resultCode.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1123 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1126 | // If there are more than kMaxOperations, abort the oldest operation that was started as |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1127 | // pruneable. |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1128 | while (mOperationMap.getOperationCount() >= kMaxOperations) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1129 | ALOGD("Reached or exceeded concurrent operations limit"); |
| 1130 | if (!pruneOperation()) { |
| 1131 | break; |
| 1132 | } |
| 1133 | } |
| 1134 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1135 | auto hidlCb = [&](ErrorCode ret, const hidl_vec<KeyParameter>& outParams, |
| 1136 | uint64_t operationHandle) { |
| 1137 | result->resultCode = ret; |
| 1138 | if (!result->resultCode.isOk()) { |
Pavel Grafov | cef3947 | 2018-02-12 18:45:02 +0000 | [diff] [blame] | 1139 | if (result->resultCode == ErrorCode::INVALID_KEY_BLOB) { |
| 1140 | log_key_integrity_violation(name8, targetUid); |
| 1141 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1142 | return; |
| 1143 | } |
| 1144 | result->handle = operationHandle; |
| 1145 | result->outParams = outParams; |
| 1146 | }; |
| 1147 | |
Janis Danisevskis | 9c5ea7b | 2018-06-21 17:53:15 -0700 | [diff] [blame] | 1148 | KeyStoreServiceReturnCode rc = |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1149 | KS_HANDLE_HIDL_ERROR(dev->begin(keyPurpose, key, opParams.hidl_data(), authToken, hidlCb)); |
Janis Danisevskis | 9c5ea7b | 2018-06-21 17:53:15 -0700 | [diff] [blame] | 1150 | if (!rc.isOk()) { |
| 1151 | LOG(ERROR) << "Got error " << rc << " from begin()"; |
| 1152 | result->resultCode = ResponseCode::SYSTEM_ERROR; |
| 1153 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
Janis Danisevskis | 9c5ea7b | 2018-06-21 17:53:15 -0700 | [diff] [blame] | 1156 | rc = result->resultCode; |
| 1157 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1158 | // If there are too many operations abort the oldest operation that was |
| 1159 | // started as pruneable and try again. |
Janis Danisevskis | 9c5ea7b | 2018-06-21 17:53:15 -0700 | [diff] [blame] | 1160 | LOG(INFO) << rc << " " << mOperationMap.hasPruneableOperation(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1161 | while (rc == ErrorCode::TOO_MANY_OPERATIONS && mOperationMap.hasPruneableOperation()) { |
Janis Danisevskis | 9c5ea7b | 2018-06-21 17:53:15 -0700 | [diff] [blame] | 1162 | LOG(INFO) << "Ran out of operation handles"; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1163 | if (!pruneOperation()) { |
| 1164 | break; |
| 1165 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1166 | rc = KS_HANDLE_HIDL_ERROR( |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1167 | dev->begin(keyPurpose, key, opParams.hidl_data(), authToken, hidlCb)); |
Janis Danisevskis | 9c5ea7b | 2018-06-21 17:53:15 -0700 | [diff] [blame] | 1168 | if (!rc.isOk()) { |
| 1169 | LOG(ERROR) << "Got error " << rc << " from begin()"; |
| 1170 | result->resultCode = ResponseCode::SYSTEM_ERROR; |
| 1171 | return Status::ok(); |
| 1172 | } |
| 1173 | rc = result->resultCode; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1174 | } |
Janis Danisevskis | 9c5ea7b | 2018-06-21 17:53:15 -0700 | [diff] [blame] | 1175 | if (!rc.isOk()) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1176 | result->resultCode = rc; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1177 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
Shawn Willden | 2eef135 | 2018-05-22 16:13:24 -0600 | [diff] [blame] | 1180 | VerificationToken verificationToken; |
| 1181 | if (authResult.isOk() && authToken.mac.size() && |
| 1182 | dev->halVersion().securityLevel == SecurityLevel::STRONGBOX) { |
| 1183 | // This operation needs an auth token, but the device is a STRONGBOX, so it can't check the |
| 1184 | // timestamp in the auth token. Get a VerificationToken from the TEE, which will be passed |
| 1185 | // to update() and begin(). |
| 1186 | rc = KS_HANDLE_HIDL_ERROR(mKeyStore->getDevice(SecurityLevel::TRUSTED_ENVIRONMENT) |
| 1187 | ->verifyAuthorization(result->handle, |
| 1188 | {} /* parametersToVerify */, authToken, |
| 1189 | [&](auto error, const auto& token) { |
| 1190 | result->resultCode = error; |
| 1191 | verificationToken = token; |
| 1192 | })); |
| 1193 | |
Janis Danisevskis | 9c5ea7b | 2018-06-21 17:53:15 -0700 | [diff] [blame] | 1194 | if (!rc.isOk()) result->resultCode = rc; |
nagendra modadugu | 36a9f4f | 2018-09-24 13:40:23 -0700 | [diff] [blame] | 1195 | if (!result->resultCode.isOk()) { |
| 1196 | LOG(ERROR) << "Failed to verify authorization " << rc << " from begin()"; |
| 1197 | rc = KS_HANDLE_HIDL_ERROR(dev->abort(result->handle)); |
| 1198 | if (!rc.isOk()) { |
| 1199 | LOG(ERROR) << "Failed to abort operation " << rc << " from begin()"; |
| 1200 | } |
| 1201 | return Status::ok(); |
| 1202 | } |
Shawn Willden | 2eef135 | 2018-05-22 16:13:24 -0600 | [diff] [blame] | 1203 | } |
| 1204 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1205 | // Note: The operation map takes possession of the contents of "characteristics". |
| 1206 | // It is safe to use characteristics after the following line but it will be empty. |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 1207 | sp<IBinder> operationToken = |
| 1208 | mOperationMap.addOperation(result->handle, keyid, keyPurpose, dev, appToken, |
| 1209 | std::move(characteristics), params.getParameters(), pruneable); |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1210 | assert(characteristics.hardwareEnforced.size() == 0); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1211 | assert(characteristics.softwareEnforced.size() == 0); |
Shawn Willden | c5e8f36 | 2017-08-31 09:23:06 -0600 | [diff] [blame] | 1212 | result->token = operationToken; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1213 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1214 | mOperationMap.setOperationAuthToken(operationToken, std::move(authToken)); |
Shawn Willden | 2eef135 | 2018-05-22 16:13:24 -0600 | [diff] [blame] | 1215 | mOperationMap.setOperationVerificationToken(operationToken, std::move(verificationToken)); |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1216 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1217 | // Return the authentication lookup result. If this is a per operation |
| 1218 | // auth'd key then the resultCode will be ::OP_AUTH_NEEDED and the |
| 1219 | // application should get an auth token using the handle before the |
| 1220 | // first call to update, which will fail if keystore hasn't received the |
| 1221 | // auth token. |
Shawn Willden | 2f96c79 | 2017-09-07 23:59:08 -0600 | [diff] [blame] | 1222 | if (result->resultCode == ErrorCode::OK) { |
| 1223 | result->resultCode = authResult; |
| 1224 | } |
Shawn Willden | c5e8f36 | 2017-08-31 09:23:06 -0600 | [diff] [blame] | 1225 | |
| 1226 | // Other result fields were set in the begin operation's callback. |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1227 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1230 | void KeyStoreService::appendConfirmationTokenIfNeeded(const KeyCharacteristics& keyCharacteristics, |
| 1231 | std::vector<KeyParameter>* params) { |
| 1232 | if (!(containsTag(keyCharacteristics.softwareEnforced, Tag::TRUSTED_CONFIRMATION_REQUIRED) || |
| 1233 | containsTag(keyCharacteristics.hardwareEnforced, Tag::TRUSTED_CONFIRMATION_REQUIRED))) { |
| 1234 | return; |
| 1235 | } |
| 1236 | |
| 1237 | hidl_vec<uint8_t> confirmationToken = mConfirmationManager->getLatestConfirmationToken(); |
| 1238 | if (confirmationToken.size() == 0) { |
| 1239 | return; |
| 1240 | } |
| 1241 | |
| 1242 | params->push_back( |
| 1243 | Authorization(keymaster::TAG_CONFIRMATION_TOKEN, std::move(confirmationToken))); |
| 1244 | ALOGD("Appending confirmation token\n"); |
| 1245 | } |
| 1246 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1247 | Status KeyStoreService::update(const sp<IBinder>& token, const KeymasterArguments& params, |
| 1248 | const ::std::vector<uint8_t>& data, OperationResult* result) { |
| 1249 | if (!checkAllowedOperationParams(params.getParameters())) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1250 | result->resultCode = ErrorCode::INVALID_ARGUMENT; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1251 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1252 | } |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1253 | |
| 1254 | auto getOpResult = mOperationMap.getOperation(token); |
| 1255 | if (!getOpResult.isOk()) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1256 | result->resultCode = ErrorCode::INVALID_OPERATION_HANDLE; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1257 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1258 | } |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1259 | const auto& op = getOpResult.value(); |
| 1260 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1261 | HardwareAuthToken authToken; |
| 1262 | std::tie(result->resultCode, authToken) = getOperationAuthTokenIfNeeded(token); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1263 | if (!result->resultCode.isOk()) return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1264 | |
| 1265 | // Check that all key authorization policy requirements are met. |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1266 | AuthorizationSet key_auths(op.characteristics.hardwareEnforced); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1267 | key_auths.append(op.characteristics.softwareEnforced.begin(), |
| 1268 | op.characteristics.softwareEnforced.end()); |
| 1269 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1270 | result->resultCode = enforcement_policy.AuthorizeOperation( |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1271 | op.purpose, op.keyid, key_auths, params.getParameters(), authToken, op.handle, |
| 1272 | false /* is_begin_operation */); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1273 | if (!result->resultCode.isOk()) return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1274 | |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1275 | std::vector<KeyParameter> inParams = params.getParameters(); |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1276 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1277 | auto hidlCb = [&](ErrorCode ret, uint32_t inputConsumed, |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1278 | const hidl_vec<KeyParameter>& outParams, |
| 1279 | const ::std::vector<uint8_t>& output) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1280 | result->resultCode = ret; |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1281 | if (result->resultCode.isOk()) { |
| 1282 | result->inputConsumed = inputConsumed; |
| 1283 | result->outParams = outParams; |
| 1284 | result->data = output; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1285 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1286 | }; |
| 1287 | |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1288 | KeyStoreServiceReturnCode rc = KS_HANDLE_HIDL_ERROR( |
Shawn Willden | 2eef135 | 2018-05-22 16:13:24 -0600 | [diff] [blame] | 1289 | op.device->update(op.handle, inParams, data, authToken, op.verificationToken, hidlCb)); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1290 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1291 | // just a reminder: on success result->resultCode was set in the callback. So we only overwrite |
| 1292 | // it if there was a communication error indicated by the ErrorCode. |
nagendra modadugu | 36a9f4f | 2018-09-24 13:40:23 -0700 | [diff] [blame] | 1293 | if (!rc.isOk()) { |
| 1294 | result->resultCode = rc; |
| 1295 | // removeOperation() will free the memory 'op' used, so the order is important |
| 1296 | mAuthTokenTable.MarkCompleted(op.handle); |
| 1297 | mOperationMap.removeOperation(token, /* wasOpSuccessful */ false); |
| 1298 | } |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1299 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1300 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1303 | Status KeyStoreService::finish(const sp<IBinder>& token, const KeymasterArguments& params, |
| 1304 | const ::std::vector<uint8_t>& signature, |
| 1305 | const ::std::vector<uint8_t>& entropy, OperationResult* result) { |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1306 | auto getOpResult = mOperationMap.getOperation(token); |
| 1307 | if (!getOpResult.isOk()) { |
| 1308 | result->resultCode = ErrorCode::INVALID_OPERATION_HANDLE; |
| 1309 | return Status::ok(); |
| 1310 | } |
| 1311 | const auto& op = std::move(getOpResult.value()); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1312 | if (!checkAllowedOperationParams(params.getParameters())) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1313 | result->resultCode = ErrorCode::INVALID_ARGUMENT; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1314 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1315 | } |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1316 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1317 | HardwareAuthToken authToken; |
| 1318 | std::tie(result->resultCode, authToken) = getOperationAuthTokenIfNeeded(token); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1319 | if (!result->resultCode.isOk()) return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1320 | |
| 1321 | if (entropy.size()) { |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1322 | result->resultCode = KS_HANDLE_HIDL_ERROR(op.device->addRngEntropy(entropy)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1323 | if (!result->resultCode.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1324 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1325 | } |
| 1326 | } |
| 1327 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1328 | // Check that all key authorization policy requirements are met. |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1329 | AuthorizationSet key_auths(op.characteristics.hardwareEnforced); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1330 | key_auths.append(op.characteristics.softwareEnforced.begin(), |
| 1331 | op.characteristics.softwareEnforced.end()); |
| 1332 | |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1333 | std::vector<KeyParameter> inParams = params.getParameters(); |
| 1334 | appendConfirmationTokenIfNeeded(op.characteristics, &inParams); |
| 1335 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1336 | result->resultCode = enforcement_policy.AuthorizeOperation( |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1337 | op.purpose, op.keyid, key_auths, params.getParameters(), authToken, op.handle, |
| 1338 | false /* is_begin_operation */); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1339 | if (!result->resultCode.isOk()) return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1340 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1341 | auto hidlCb = [&](ErrorCode ret, const hidl_vec<KeyParameter>& outParams, |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1342 | const ::std::vector<uint8_t>& output) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1343 | result->resultCode = ret; |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1344 | if (result->resultCode.isOk()) { |
| 1345 | result->outParams = outParams; |
| 1346 | result->data = output; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1347 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1348 | }; |
| 1349 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1350 | KeyStoreServiceReturnCode rc = KS_HANDLE_HIDL_ERROR( |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1351 | op.device->finish(op.handle, inParams, |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1352 | ::std::vector<uint8_t>() /* TODO(swillden): wire up input to finish() */, |
Shawn Willden | 2eef135 | 2018-05-22 16:13:24 -0600 | [diff] [blame] | 1353 | signature, authToken, op.verificationToken, hidlCb)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1354 | |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 1355 | bool wasOpSuccessful = true; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1356 | // just a reminder: on success result->resultCode was set in the callback. So we only overwrite |
| 1357 | // it if there was a communication error indicated by the ErrorCode. |
| 1358 | if (!rc.isOk()) { |
| 1359 | result->resultCode = rc; |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 1360 | wasOpSuccessful = false; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1361 | } |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 1362 | |
| 1363 | // removeOperation() will free the memory 'op' used, so the order is important |
| 1364 | mAuthTokenTable.MarkCompleted(op.handle); |
| 1365 | mOperationMap.removeOperation(token, wasOpSuccessful); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1366 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1369 | Status KeyStoreService::abort(const sp<IBinder>& token, int32_t* aidl_return) { |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 1370 | auto getOpResult = mOperationMap.removeOperation(token, false /* wasOpSuccessful */); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1371 | if (!getOpResult.isOk()) { |
| 1372 | *aidl_return = static_cast<int32_t>(ErrorCode::INVALID_OPERATION_HANDLE); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1373 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1374 | } |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1375 | auto op = std::move(getOpResult.value()); |
| 1376 | mAuthTokenTable.MarkCompleted(op.handle); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1377 | |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1378 | ErrorCode error_code = KS_HANDLE_HIDL_ERROR(op.device->abort(op.handle)); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1379 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(error_code)); |
| 1380 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1383 | Status KeyStoreService::addAuthToken(const ::std::vector<uint8_t>& authTokenAsVector, |
Brian Young | ccb492d | 2018-02-22 23:36:01 +0000 | [diff] [blame] | 1384 | int32_t* aidl_return) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1385 | |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1386 | // TODO(swillden): When gatekeeper and fingerprint are ready, this should be updated to |
| 1387 | // receive a HardwareAuthToken, rather than an opaque byte array. |
| 1388 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1389 | if (!checkBinderPermission(P_ADD_AUTH)) { |
| 1390 | ALOGW("addAuthToken: permission denied for %d", IPCThreadState::self()->getCallingUid()); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1391 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 1392 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1393 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1394 | if (authTokenAsVector.size() != sizeof(hw_auth_token_t)) { |
| 1395 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT)); |
| 1396 | return Status::ok(); |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1397 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1398 | |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1399 | hw_auth_token_t authToken; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1400 | memcpy(reinterpret_cast<void*>(&authToken), authTokenAsVector.data(), sizeof(hw_auth_token_t)); |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1401 | if (authToken.version != 0) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1402 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT)); |
| 1403 | return Status::ok(); |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1404 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1405 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1406 | mAuthTokenTable.AddAuthenticationToken(hidlVec2AuthToken(hidl_vec<uint8_t>(authTokenAsVector))); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1407 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 1408 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1409 | } |
| 1410 | |
Rubin Xu | 1a203e3 | 2018-05-08 14:25:21 +0100 | [diff] [blame] | 1411 | int isDeviceIdAttestationRequested(const KeymasterArguments& params) { |
Chih-Hung Hsieh | b81e68b | 2018-07-13 11:37:45 -0700 | [diff] [blame] | 1412 | const hardware::hidl_vec<KeyParameter>& paramsVec = params.getParameters(); |
Rubin Xu | 1a203e3 | 2018-05-08 14:25:21 +0100 | [diff] [blame] | 1413 | int result = 0; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1414 | for (size_t i = 0; i < paramsVec.size(); ++i) { |
| 1415 | switch (paramsVec[i].tag) { |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1416 | case Tag::ATTESTATION_ID_BRAND: |
| 1417 | case Tag::ATTESTATION_ID_DEVICE: |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1418 | case Tag::ATTESTATION_ID_MANUFACTURER: |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1419 | case Tag::ATTESTATION_ID_MODEL: |
| 1420 | case Tag::ATTESTATION_ID_PRODUCT: |
Rubin Xu | 1a203e3 | 2018-05-08 14:25:21 +0100 | [diff] [blame] | 1421 | result |= ID_ATTESTATION_REQUEST_GENERIC_INFO; |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1422 | break; |
Rubin Xu | 1a203e3 | 2018-05-08 14:25:21 +0100 | [diff] [blame] | 1423 | case Tag::ATTESTATION_ID_IMEI: |
| 1424 | case Tag::ATTESTATION_ID_MEID: |
| 1425 | case Tag::ATTESTATION_ID_SERIAL: |
| 1426 | result |= ID_ATTESTATION_REQUEST_UNIQUE_DEVICE_ID; |
| 1427 | break; |
| 1428 | default: |
| 1429 | continue; |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1430 | } |
| 1431 | } |
Rubin Xu | 1a203e3 | 2018-05-08 14:25:21 +0100 | [diff] [blame] | 1432 | return result; |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1433 | } |
| 1434 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1435 | Status KeyStoreService::attestKey(const String16& name, const KeymasterArguments& params, |
| 1436 | ::android::security::keymaster::KeymasterCertificateChain* chain, |
| 1437 | int32_t* aidl_return) { |
| 1438 | // check null output if method signature is updated and return ErrorCode::OUTPUT_PARAMETER_NULL |
| 1439 | if (!checkAllowedOperationParams(params.getParameters())) { |
| 1440 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT)); |
| 1441 | return Status::ok(); |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
Eran Messeri | e2c3415 | 2017-12-21 21:01:22 +0000 | [diff] [blame] | 1444 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1445 | |
Rubin Xu | 1a203e3 | 2018-05-08 14:25:21 +0100 | [diff] [blame] | 1446 | int needsIdAttestation = isDeviceIdAttestationRequested(params); |
| 1447 | bool needsUniqueIdAttestation = needsIdAttestation & ID_ATTESTATION_REQUEST_UNIQUE_DEVICE_ID; |
| 1448 | bool isPrimaryUserSystemUid = (callingUid == AID_SYSTEM); |
| 1449 | bool isSomeUserSystemUid = (get_app_id(callingUid) == AID_SYSTEM); |
| 1450 | // Allow system context from any user to request attestation with basic device information, |
| 1451 | // while only allow system context from user 0 (device owner) to request attestation with |
| 1452 | // unique device ID. |
| 1453 | if ((needsIdAttestation && !isSomeUserSystemUid) || |
| 1454 | (needsUniqueIdAttestation && !isPrimaryUserSystemUid)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1455 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT)); |
| 1456 | return Status::ok(); |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1457 | } |
| 1458 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1459 | AuthorizationSet mutableParams = params.getParameters(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1460 | KeyStoreServiceReturnCode rc = updateParamsForAttestation(callingUid, &mutableParams); |
| 1461 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1462 | *aidl_return = static_cast<int32_t>(rc); |
| 1463 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1464 | } |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1465 | |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1466 | Blob keyBlob; |
| 1467 | String8 name8(name); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1468 | rc = mKeyStore->getKeyForName(&keyBlob, name8, callingUid, TYPE_KEYMASTER_10); |
| 1469 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1470 | *aidl_return = static_cast<int32_t>(rc); |
| 1471 | return Status::ok(); |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1474 | KeyStoreServiceReturnCode error; |
| 1475 | auto hidlCb = [&](ErrorCode ret, const hidl_vec<hidl_vec<uint8_t>>& certChain) { |
| 1476 | error = ret; |
| 1477 | if (!error.isOk()) { |
| 1478 | return; |
| 1479 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1480 | if (chain) { |
| 1481 | *chain = KeymasterCertificateChain(certChain); |
| 1482 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1483 | }; |
| 1484 | |
| 1485 | auto hidlKey = blob2hidlVec(keyBlob); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1486 | auto dev = mKeyStore->getDevice(keyBlob); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1487 | rc = KS_HANDLE_HIDL_ERROR(dev->attestKey(hidlKey, mutableParams.hidl_data(), hidlCb)); |
| 1488 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1489 | *aidl_return = static_cast<int32_t>(rc); |
| 1490 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1491 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1492 | *aidl_return = static_cast<int32_t>(error); |
| 1493 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1494 | } |
| 1495 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1496 | Status |
| 1497 | KeyStoreService::attestDeviceIds(const KeymasterArguments& params, |
| 1498 | ::android::security::keymaster::KeymasterCertificateChain* chain, |
| 1499 | int32_t* aidl_return) { |
| 1500 | // 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] | 1501 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1502 | if (!checkAllowedOperationParams(params.getParameters())) { |
| 1503 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT)); |
| 1504 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | if (!isDeviceIdAttestationRequested(params)) { |
| 1508 | // There is an attestKey() method for attesting keys without device ID attestation. |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1509 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT)); |
| 1510 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1514 | sp<IBinder> binder = defaultServiceManager()->getService(String16("permission")); |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 1515 | if (binder == nullptr) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1516 | *aidl_return = |
| 1517 | static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::CANNOT_ATTEST_IDS)); |
| 1518 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1519 | } |
| 1520 | if (!interface_cast<IPermissionController>(binder)->checkPermission( |
| 1521 | String16("android.permission.READ_PRIVILEGED_PHONE_STATE"), |
| 1522 | IPCThreadState::self()->getCallingPid(), callingUid)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1523 | *aidl_return = |
| 1524 | static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::CANNOT_ATTEST_IDS)); |
| 1525 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1526 | } |
| 1527 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1528 | AuthorizationSet mutableParams = params.getParameters(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1529 | KeyStoreServiceReturnCode rc = updateParamsForAttestation(callingUid, &mutableParams); |
| 1530 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1531 | *aidl_return = static_cast<int32_t>(rc); |
| 1532 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | // Generate temporary key. |
Shawn Willden | 70c1a78 | 2018-07-11 15:13:20 -0600 | [diff] [blame] | 1536 | sp<Keymaster> dev = mKeyStore->getDevice(SecurityLevel::TRUSTED_ENVIRONMENT); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1537 | |
Shawn Willden | 70c1a78 | 2018-07-11 15:13:20 -0600 | [diff] [blame] | 1538 | if (!dev) { |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1539 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 1540 | return Status::ok(); |
| 1541 | } |
| 1542 | |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1543 | KeyStoreServiceReturnCode error; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1544 | ::std::vector<uint8_t> hidlKey; |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1545 | |
| 1546 | AuthorizationSet keyCharacteristics; |
| 1547 | keyCharacteristics.push_back(TAG_PURPOSE, KeyPurpose::VERIFY); |
| 1548 | keyCharacteristics.push_back(TAG_ALGORITHM, Algorithm::EC); |
| 1549 | keyCharacteristics.push_back(TAG_DIGEST, Digest::SHA_2_256); |
| 1550 | keyCharacteristics.push_back(TAG_NO_AUTH_REQUIRED); |
| 1551 | keyCharacteristics.push_back(TAG_EC_CURVE, EcCurve::P_256); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1552 | auto generateHidlCb = [&](ErrorCode ret, const ::std::vector<uint8_t>& hidlKeyBlob, |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1553 | const KeyCharacteristics&) { |
| 1554 | error = ret; |
| 1555 | if (!error.isOk()) { |
| 1556 | return; |
| 1557 | } |
| 1558 | hidlKey = hidlKeyBlob; |
| 1559 | }; |
| 1560 | |
| 1561 | rc = KS_HANDLE_HIDL_ERROR(dev->generateKey(keyCharacteristics.hidl_data(), generateHidlCb)); |
| 1562 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1563 | *aidl_return = static_cast<int32_t>(rc); |
| 1564 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1565 | } |
| 1566 | if (!error.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1567 | *aidl_return = static_cast<int32_t>(error); |
| 1568 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | // Attest key and device IDs. |
| 1572 | auto attestHidlCb = [&](ErrorCode ret, const hidl_vec<hidl_vec<uint8_t>>& certChain) { |
| 1573 | error = ret; |
| 1574 | if (!error.isOk()) { |
| 1575 | return; |
| 1576 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1577 | *chain = ::android::security::keymaster::KeymasterCertificateChain(certChain); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1578 | }; |
| 1579 | KeyStoreServiceReturnCode attestationRc = |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1580 | KS_HANDLE_HIDL_ERROR(dev->attestKey(hidlKey, mutableParams.hidl_data(), attestHidlCb)); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1581 | |
| 1582 | // Delete temporary key. |
| 1583 | KeyStoreServiceReturnCode deletionRc = KS_HANDLE_HIDL_ERROR(dev->deleteKey(hidlKey)); |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1584 | |
| 1585 | if (!attestationRc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1586 | *aidl_return = static_cast<int32_t>(attestationRc); |
| 1587 | return Status::ok(); |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1588 | } |
| 1589 | if (!error.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1590 | *aidl_return = static_cast<int32_t>(error); |
| 1591 | return Status::ok(); |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1592 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1593 | *aidl_return = static_cast<int32_t>(deletionRc); |
| 1594 | return Status::ok(); |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1595 | } |
| 1596 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1597 | Status KeyStoreService::onDeviceOffBody(int32_t* aidl_return) { |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1598 | // TODO(tuckeris): add permission check. This should be callable from ClockworkHome only. |
| 1599 | mAuthTokenTable.onDeviceOffBody(); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1600 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 1601 | return Status::ok(); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1602 | } |
| 1603 | |
Janis Danisevskis | cb9267d | 2017-12-19 16:27:52 -0800 | [diff] [blame] | 1604 | #define AIDL_RETURN(rc) \ |
| 1605 | (*_aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(rc)), Status::ok()) |
| 1606 | |
| 1607 | Status KeyStoreService::importWrappedKey( |
| 1608 | const ::android::String16& wrappedKeyAlias, const ::std::vector<uint8_t>& wrappedKey, |
| 1609 | const ::android::String16& wrappingKeyAlias, const ::std::vector<uint8_t>& maskingKey, |
| 1610 | const KeymasterArguments& params, int64_t rootSid, int64_t fingerprintSid, |
| 1611 | ::android::security::keymaster::KeyCharacteristics* outCharacteristics, int32_t* _aidl_return) { |
| 1612 | |
| 1613 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1614 | |
| 1615 | if (!checkBinderPermission(P_INSERT, callingUid)) { |
| 1616 | return AIDL_RETURN(ResponseCode::PERMISSION_DENIED); |
| 1617 | } |
| 1618 | |
| 1619 | Blob wrappingKeyBlob; |
| 1620 | String8 wrappingKeyName8(wrappingKeyAlias); |
| 1621 | KeyStoreServiceReturnCode rc = |
| 1622 | mKeyStore->getKeyForName(&wrappingKeyBlob, wrappingKeyName8, callingUid, TYPE_KEYMASTER_10); |
| 1623 | if (!rc.isOk()) { |
| 1624 | return AIDL_RETURN(rc); |
| 1625 | } |
| 1626 | |
| 1627 | SecurityLevel securityLevel = wrappingKeyBlob.getSecurityLevel(); |
| 1628 | auto dev = mKeyStore->getDevice(securityLevel); |
| 1629 | if (!dev) { |
| 1630 | return AIDL_RETURN(ErrorCode::HARDWARE_TYPE_UNAVAILABLE); |
| 1631 | } |
| 1632 | |
| 1633 | auto hidlWrappingKey = blob2hidlVec(wrappingKeyBlob); |
| 1634 | String8 wrappedKeyAlias8(wrappedKeyAlias); |
| 1635 | |
| 1636 | KeyStoreServiceReturnCode error; |
| 1637 | |
| 1638 | auto hidlCb = [&](ErrorCode ret, const ::std::vector<uint8_t>& keyBlob, |
| 1639 | const KeyCharacteristics& keyCharacteristics) { |
| 1640 | error = ret; |
| 1641 | if (!error.isOk()) { |
| 1642 | return; |
| 1643 | } |
| 1644 | if (outCharacteristics) { |
| 1645 | *outCharacteristics = |
| 1646 | ::android::security::keymaster::KeyCharacteristics(keyCharacteristics); |
| 1647 | } |
| 1648 | |
| 1649 | // Write the key: |
| 1650 | String8 filename( |
| 1651 | mKeyStore->getKeyNameForUidWithDir(wrappedKeyAlias8, callingUid, ::TYPE_KEYMASTER_10)); |
| 1652 | |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 1653 | Blob ksBlob(&keyBlob[0], keyBlob.size(), nullptr, 0, ::TYPE_KEYMASTER_10); |
Janis Danisevskis | cb9267d | 2017-12-19 16:27:52 -0800 | [diff] [blame] | 1654 | ksBlob.setSecurityLevel(securityLevel); |
| 1655 | |
| 1656 | if (containsTag(keyCharacteristics.hardwareEnforced, Tag::USER_SECURE_ID)) { |
| 1657 | ksBlob.setSuperEncrypted(true); |
| 1658 | } |
| 1659 | |
| 1660 | error = mKeyStore->put(filename.string(), &ksBlob, get_user_id(callingUid)); |
| 1661 | }; |
| 1662 | |
Shawn Willden | 0a198a0 | 2018-01-19 13:36:31 -0700 | [diff] [blame] | 1663 | rc = KS_HANDLE_HIDL_ERROR(dev->importWrappedKey(wrappedKey, hidlWrappingKey, maskingKey, |
| 1664 | params.getParameters(), rootSid, fingerprintSid, |
| 1665 | hidlCb)); |
| 1666 | |
Janis Danisevskis | cb9267d | 2017-12-19 16:27:52 -0800 | [diff] [blame] | 1667 | // possible hidl error |
| 1668 | if (!rc.isOk()) { |
| 1669 | return AIDL_RETURN(rc); |
| 1670 | } |
| 1671 | // now check error from callback |
| 1672 | if (!error.isOk()) { |
| 1673 | return AIDL_RETURN(error); |
| 1674 | } |
| 1675 | |
| 1676 | // Write the characteristics: |
| 1677 | String8 cFilename(mKeyStore->getKeyNameForUidWithDir(wrappedKeyAlias8, callingUid, |
| 1678 | ::TYPE_KEY_CHARACTERISTICS)); |
| 1679 | |
| 1680 | AuthorizationSet opParams = params.getParameters(); |
| 1681 | std::stringstream kcStream; |
| 1682 | opParams.Serialize(&kcStream); |
| 1683 | if (kcStream.bad()) { |
| 1684 | return AIDL_RETURN(ResponseCode::SYSTEM_ERROR); |
| 1685 | } |
| 1686 | auto kcBuf = kcStream.str(); |
| 1687 | |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 1688 | Blob charBlob(reinterpret_cast<const uint8_t*>(kcBuf.data()), kcBuf.size(), nullptr, 0, |
Janis Danisevskis | cb9267d | 2017-12-19 16:27:52 -0800 | [diff] [blame] | 1689 | ::TYPE_KEY_CHARACTERISTICS); |
| 1690 | charBlob.setSecurityLevel(securityLevel); |
| 1691 | |
| 1692 | return AIDL_RETURN(mKeyStore->put(cFilename.string(), &charBlob, get_user_id(callingUid))); |
| 1693 | } |
| 1694 | |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1695 | Status KeyStoreService::presentConfirmationPrompt(const sp<IBinder>& listener, |
| 1696 | const String16& promptText, |
| 1697 | const ::std::vector<uint8_t>& extraData, |
| 1698 | const String16& locale, int32_t uiOptionsAsFlags, |
| 1699 | int32_t* aidl_return) { |
| 1700 | return mConfirmationManager->presentConfirmationPrompt(listener, promptText, extraData, locale, |
| 1701 | uiOptionsAsFlags, aidl_return); |
| 1702 | } |
| 1703 | |
| 1704 | Status KeyStoreService::cancelConfirmationPrompt(const sp<IBinder>& listener, |
| 1705 | int32_t* aidl_return) { |
| 1706 | return mConfirmationManager->cancelConfirmationPrompt(listener, aidl_return); |
| 1707 | } |
| 1708 | |
David Zeuthen | 1a49231 | 2018-02-26 11:00:30 -0500 | [diff] [blame] | 1709 | Status KeyStoreService::isConfirmationPromptSupported(bool* aidl_return) { |
| 1710 | return mConfirmationManager->isConfirmationPromptSupported(aidl_return); |
| 1711 | } |
| 1712 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1713 | /** |
| 1714 | * Prune the oldest pruneable operation. |
| 1715 | */ |
| 1716 | bool KeyStoreService::pruneOperation() { |
| 1717 | sp<IBinder> oldest = mOperationMap.getOldestPruneableOperation(); |
| 1718 | ALOGD("Trying to prune operation %p", oldest.get()); |
| 1719 | size_t op_count_before_abort = mOperationMap.getOperationCount(); |
| 1720 | // We mostly ignore errors from abort() because all we care about is whether at least |
| 1721 | // one operation has been removed. |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1722 | int32_t abort_error; |
| 1723 | abort(oldest, &abort_error); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1724 | if (mOperationMap.getOperationCount() >= op_count_before_abort) { |
| 1725 | ALOGE("Failed to abort pruneable operation %p, error: %d", oldest.get(), abort_error); |
| 1726 | return false; |
| 1727 | } |
| 1728 | return true; |
| 1729 | } |
| 1730 | |
| 1731 | /** |
| 1732 | * Get the effective target uid for a binder operation that takes an |
| 1733 | * optional uid as the target. |
| 1734 | */ |
| 1735 | uid_t KeyStoreService::getEffectiveUid(int32_t targetUid) { |
| 1736 | if (targetUid == UID_SELF) { |
| 1737 | return IPCThreadState::self()->getCallingUid(); |
| 1738 | } |
| 1739 | return static_cast<uid_t>(targetUid); |
| 1740 | } |
| 1741 | |
| 1742 | /** |
| 1743 | * Check if the caller of the current binder method has the required |
| 1744 | * permission and if acting on other uids the grants to do so. |
| 1745 | */ |
| 1746 | bool KeyStoreService::checkBinderPermission(perm_t permission, int32_t targetUid) { |
| 1747 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1748 | pid_t spid = IPCThreadState::self()->getCallingPid(); |
| 1749 | if (!has_permission(callingUid, permission, spid)) { |
| 1750 | ALOGW("permission %s denied for %d", get_perm_label(permission), callingUid); |
| 1751 | return false; |
| 1752 | } |
| 1753 | if (!is_granted_to(callingUid, getEffectiveUid(targetUid))) { |
| 1754 | ALOGW("uid %d not granted to act for %d", callingUid, targetUid); |
| 1755 | return false; |
| 1756 | } |
| 1757 | return true; |
| 1758 | } |
| 1759 | |
| 1760 | /** |
| 1761 | * Check if the caller of the current binder method has the required |
| 1762 | * permission and the target uid is the caller or the caller is system. |
| 1763 | */ |
| 1764 | bool KeyStoreService::checkBinderPermissionSelfOrSystem(perm_t permission, int32_t targetUid) { |
| 1765 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1766 | pid_t spid = IPCThreadState::self()->getCallingPid(); |
| 1767 | if (!has_permission(callingUid, permission, spid)) { |
| 1768 | ALOGW("permission %s denied for %d", get_perm_label(permission), callingUid); |
| 1769 | return false; |
| 1770 | } |
| 1771 | return getEffectiveUid(targetUid) == callingUid || callingUid == AID_SYSTEM; |
| 1772 | } |
| 1773 | |
| 1774 | /** |
| 1775 | * Check if the caller of the current binder method has the required |
| 1776 | * permission or the target of the operation is the caller's uid. This is |
| 1777 | * for operation where the permission is only for cross-uid activity and all |
| 1778 | * uids are allowed to act on their own (ie: clearing all entries for a |
| 1779 | * given uid). |
| 1780 | */ |
| 1781 | bool KeyStoreService::checkBinderPermissionOrSelfTarget(perm_t permission, int32_t targetUid) { |
| 1782 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1783 | if (getEffectiveUid(targetUid) == callingUid) { |
| 1784 | return true; |
| 1785 | } else { |
| 1786 | return checkBinderPermission(permission, targetUid); |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | /** |
| 1791 | * Helper method to check that the caller has the required permission as |
| 1792 | * well as the keystore is in the unlocked state if checkUnlocked is true. |
| 1793 | * |
| 1794 | * Returns NO_ERROR on success, PERMISSION_DENIED on a permission error and |
| 1795 | * otherwise the state of keystore when not unlocked and checkUnlocked is |
| 1796 | * true. |
| 1797 | */ |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1798 | KeyStoreServiceReturnCode |
| 1799 | KeyStoreService::checkBinderPermissionAndKeystoreState(perm_t permission, int32_t targetUid, |
| 1800 | bool checkUnlocked) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1801 | if (!checkBinderPermission(permission, targetUid)) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1802 | return ResponseCode::PERMISSION_DENIED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1803 | } |
| 1804 | State state = mKeyStore->getState(get_user_id(getEffectiveUid(targetUid))); |
| 1805 | if (checkUnlocked && !isKeystoreUnlocked(state)) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1806 | // All State values coincide with ResponseCodes |
| 1807 | return static_cast<ResponseCode>(state); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1810 | return ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | bool KeyStoreService::isKeystoreUnlocked(State state) { |
| 1814 | switch (state) { |
| 1815 | case ::STATE_NO_ERROR: |
| 1816 | return true; |
| 1817 | case ::STATE_UNINITIALIZED: |
| 1818 | case ::STATE_LOCKED: |
| 1819 | return false; |
| 1820 | } |
| 1821 | return false; |
| 1822 | } |
| 1823 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1824 | /** |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1825 | * Check that all KeyParameters provided by the application are allowed. Any parameter that keystore |
| 1826 | * adds itself should be disallowed here. |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1827 | */ |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1828 | bool KeyStoreService::checkAllowedOperationParams(const hidl_vec<KeyParameter>& params) { |
| 1829 | for (size_t i = 0; i < params.size(); ++i) { |
| 1830 | switch (params[i].tag) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1831 | case Tag::ATTESTATION_APPLICATION_ID: |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1832 | case Tag::RESET_SINCE_ID_ROTATION: |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1833 | return false; |
| 1834 | default: |
| 1835 | break; |
| 1836 | } |
| 1837 | } |
| 1838 | return true; |
| 1839 | } |
| 1840 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1841 | ErrorCode KeyStoreService::getOperationCharacteristics(const hidl_vec<uint8_t>& key, |
Shawn Willden | c67a8aa | 2017-12-03 17:51:29 -0700 | [diff] [blame] | 1842 | sp<Keymaster>* dev, |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1843 | const AuthorizationSet& params, |
| 1844 | KeyCharacteristics* out) { |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 1845 | ::std::vector<uint8_t> clientId; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1846 | ::std::vector<uint8_t> appData; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1847 | for (auto param : params) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1848 | if (param.tag == Tag::APPLICATION_ID) { |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 1849 | clientId = authorizationValue(TAG_APPLICATION_ID, param).value(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1850 | } else if (param.tag == Tag::APPLICATION_DATA) { |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 1851 | appData = authorizationValue(TAG_APPLICATION_DATA, param).value(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1852 | } |
| 1853 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1854 | ErrorCode error = ErrorCode::OK; |
| 1855 | |
| 1856 | auto hidlCb = [&](ErrorCode ret, const KeyCharacteristics& keyCharacteristics) { |
| 1857 | error = ret; |
| 1858 | if (error != ErrorCode::OK) { |
| 1859 | return; |
| 1860 | } |
| 1861 | if (out) *out = keyCharacteristics; |
| 1862 | }; |
| 1863 | |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 1864 | ErrorCode rc = |
| 1865 | KS_HANDLE_HIDL_ERROR((*dev)->getKeyCharacteristics(key, clientId, appData, hidlCb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1866 | if (rc != ErrorCode::OK) { |
| 1867 | return rc; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1868 | } |
| 1869 | return error; |
| 1870 | } |
| 1871 | |
| 1872 | /** |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1873 | * Get the auth token for this operation from the auth token table. |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1874 | * |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1875 | * Returns ResponseCode::NO_ERROR if the auth token was set or none was required. |
| 1876 | * ::OP_AUTH_NEEDED if it is a per op authorization, no |
| 1877 | * authorization token exists for that operation and |
| 1878 | * failOnTokenMissing is false. |
| 1879 | * KM_ERROR_KEY_USER_NOT_AUTHENTICATED if there is no valid auth |
| 1880 | * token for the operation |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1881 | */ |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1882 | std::pair<KeyStoreServiceReturnCode, HardwareAuthToken> |
| 1883 | KeyStoreService::getAuthToken(const KeyCharacteristics& characteristics, uint64_t handle, |
| 1884 | KeyPurpose purpose, bool failOnTokenMissing) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1885 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1886 | AuthorizationSet allCharacteristics(characteristics.softwareEnforced); |
| 1887 | allCharacteristics.append(characteristics.hardwareEnforced.begin(), |
| 1888 | characteristics.hardwareEnforced.end()); |
| 1889 | |
| 1890 | const HardwareAuthToken* authToken = nullptr; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1891 | AuthTokenTable::Error err = mAuthTokenTable.FindAuthorization( |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1892 | allCharacteristics, static_cast<KeyPurpose>(purpose), handle, &authToken); |
| 1893 | |
| 1894 | KeyStoreServiceReturnCode rc; |
| 1895 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1896 | switch (err) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1897 | case AuthTokenTable::OK: |
| 1898 | case AuthTokenTable::AUTH_NOT_REQUIRED: |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1899 | rc = ResponseCode::NO_ERROR; |
| 1900 | break; |
| 1901 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1902 | case AuthTokenTable::AUTH_TOKEN_NOT_FOUND: |
| 1903 | case AuthTokenTable::AUTH_TOKEN_EXPIRED: |
| 1904 | case AuthTokenTable::AUTH_TOKEN_WRONG_SID: |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1905 | ALOGE("getAuthToken failed: %d", err); // STOPSHIP: debug only, to be removed |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1906 | rc = ErrorCode::KEY_USER_NOT_AUTHENTICATED; |
| 1907 | break; |
| 1908 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1909 | case AuthTokenTable::OP_HANDLE_REQUIRED: |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1910 | rc = failOnTokenMissing ? KeyStoreServiceReturnCode(ErrorCode::KEY_USER_NOT_AUTHENTICATED) |
| 1911 | : KeyStoreServiceReturnCode(ResponseCode::OP_AUTH_NEEDED); |
| 1912 | break; |
| 1913 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1914 | default: |
| 1915 | ALOGE("Unexpected FindAuthorization return value %d", err); |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1916 | rc = ErrorCode::INVALID_ARGUMENT; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1917 | } |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1918 | |
| 1919 | return {rc, authToken ? std::move(*authToken) : HardwareAuthToken()}; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | /** |
| 1923 | * Add the auth token for the operation to the param list if the operation |
| 1924 | * requires authorization. Uses the cached result in the OperationMap if available |
| 1925 | * otherwise gets the token from the AuthTokenTable and caches the result. |
| 1926 | * |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1927 | * Returns ResponseCode::NO_ERROR if the auth token was added or not needed. |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1928 | * KM_ERROR_KEY_USER_NOT_AUTHENTICATED if the operation is not |
| 1929 | * authenticated. |
| 1930 | * KM_ERROR_INVALID_OPERATION_HANDLE if token is not a valid |
| 1931 | * operation token. |
| 1932 | */ |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1933 | std::pair<KeyStoreServiceReturnCode, const HardwareAuthToken&> |
| 1934 | KeyStoreService::getOperationAuthTokenIfNeeded(const sp<IBinder>& token) { |
| 1935 | static HardwareAuthToken emptyToken = {}; |
| 1936 | |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1937 | auto getOpResult = mOperationMap.getOperation(token); |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1938 | if (!getOpResult.isOk()) return {ErrorCode::INVALID_OPERATION_HANDLE, emptyToken}; |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1939 | const auto& op = getOpResult.value(); |
| 1940 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1941 | if (!op.hasAuthToken()) { |
| 1942 | KeyStoreServiceReturnCode rc; |
| 1943 | HardwareAuthToken found; |
| 1944 | std::tie(rc, found) = getAuthToken(op.characteristics, op.handle, op.purpose); |
| 1945 | if (!rc.isOk()) return {rc, emptyToken}; |
| 1946 | mOperationMap.setOperationAuthToken(token, std::move(found)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1947 | } |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1948 | |
| 1949 | return {ResponseCode::NO_ERROR, op.authToken}; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1950 | } |
| 1951 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1952 | KeyStoreServiceReturnCode KeyStoreService::upgradeKeyBlob(const String16& name, uid_t uid, |
| 1953 | const AuthorizationSet& params, |
| 1954 | Blob* blob) { |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 1955 | // Read the blob rather than assuming the caller provided the right name/uid/blob triplet. |
| 1956 | String8 name8(name); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1957 | KeyStoreServiceReturnCode responseCode = |
| 1958 | mKeyStore->getKeyForName(blob, name8, uid, TYPE_KEYMASTER_10); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1959 | if (responseCode != ResponseCode::NO_ERROR) { |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 1960 | return responseCode; |
| 1961 | } |
Rubin Xu | 7675c9f | 2017-03-15 19:26:52 +0000 | [diff] [blame] | 1962 | ALOGI("upgradeKeyBlob %s %d", name8.string(), uid); |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 1963 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1964 | auto hidlKey = blob2hidlVec(*blob); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1965 | auto dev = mKeyStore->getDevice(*blob); |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 1966 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1967 | KeyStoreServiceReturnCode error; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1968 | auto hidlCb = [&](ErrorCode ret, const ::std::vector<uint8_t>& upgradedKeyBlob) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1969 | error = ret; |
| 1970 | if (!error.isOk()) { |
Pavel Grafov | cef3947 | 2018-02-12 18:45:02 +0000 | [diff] [blame] | 1971 | if (error == ErrorCode::INVALID_KEY_BLOB) { |
| 1972 | log_key_integrity_violation(name8, uid); |
| 1973 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1974 | return; |
| 1975 | } |
| 1976 | |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 1977 | auto filename = mKeyStore->getBlobFileNameIfExists(name8, uid, ::TYPE_KEYMASTER_10); |
| 1978 | if (!filename.isOk()) { |
| 1979 | ALOGI("trying to upgrade a non existing blob"); |
| 1980 | return; |
| 1981 | } |
| 1982 | error = mKeyStore->del(filename.value().string(), ::TYPE_ANY, get_user_id(uid)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1983 | if (!error.isOk()) { |
Rubin Xu | 7675c9f | 2017-03-15 19:26:52 +0000 | [diff] [blame] | 1984 | ALOGI("upgradeKeyBlob keystore->del failed %d", (int)error); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1985 | return; |
| 1986 | } |
| 1987 | |
| 1988 | Blob newBlob(&upgradedKeyBlob[0], upgradedKeyBlob.size(), nullptr /* info */, |
| 1989 | 0 /* infoLength */, ::TYPE_KEYMASTER_10); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1990 | newBlob.setSecurityLevel(blob->getSecurityLevel()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1991 | newBlob.setEncrypted(blob->isEncrypted()); |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 1992 | newBlob.setSuperEncrypted(blob->isSuperEncrypted()); |
| 1993 | newBlob.setCriticalToDeviceEncryption(blob->isCriticalToDeviceEncryption()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1994 | |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 1995 | error = mKeyStore->put(filename.value().string(), &newBlob, get_user_id(uid)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1996 | if (!error.isOk()) { |
Rubin Xu | 7675c9f | 2017-03-15 19:26:52 +0000 | [diff] [blame] | 1997 | ALOGI("upgradeKeyBlob keystore->put failed %d", (int)error); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1998 | return; |
| 1999 | } |
| 2000 | |
| 2001 | // Re-read blob for caller. We can't use newBlob because writing it modified it. |
| 2002 | error = mKeyStore->getKeyForName(blob, name8, uid, TYPE_KEYMASTER_10); |
| 2003 | }; |
| 2004 | |
| 2005 | KeyStoreServiceReturnCode rc = |
| 2006 | KS_HANDLE_HIDL_ERROR(dev->upgradeKey(hidlKey, params.hidl_data(), hidlCb)); |
| 2007 | if (!rc.isOk()) { |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 2008 | return rc; |
| 2009 | } |
| 2010 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2011 | return error; |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 2012 | } |
| 2013 | |
Brian Young | 9a947d5 | 2018-02-23 18:03:14 +0000 | [diff] [blame] | 2014 | Status KeyStoreService::onKeyguardVisibilityChanged(bool isShowing, int32_t userId, |
| 2015 | int32_t* aidl_return) { |
| 2016 | enforcement_policy.set_device_locked(isShowing, userId); |
| 2017 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
Brian Young | 9371e95 | 2018-02-23 18:03:14 +0000 | [diff] [blame] | 2018 | |
| 2019 | return Status::ok(); |
| 2020 | } |
| 2021 | |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 2022 | } // namespace keystore |