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::generate(const String16& name, int32_t targetUid, int32_t keyType, |
| 424 | int32_t keySize, int32_t flags, |
| 425 | const ::android::security::KeystoreArguments& keystoreArgs, |
| 426 | int32_t* aidl_return) { |
| 427 | const Vector<sp<KeystoreArg>>* args = &(keystoreArgs.getArguments()); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 428 | targetUid = getEffectiveUid(targetUid); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 429 | KeyStoreServiceReturnCode result = |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 430 | checkBinderPermissionAndKeystoreState(P_INSERT, targetUid, flags & KEYSTORE_FLAG_ENCRYPTED); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 431 | if (!result.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 432 | *aidl_return = static_cast<int32_t>(result); |
| 433 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 436 | keystore::AuthorizationSet params; |
| 437 | add_legacy_key_authorizations(keyType, ¶ms); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 438 | |
| 439 | switch (keyType) { |
| 440 | case EVP_PKEY_EC: { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 441 | params.push_back(TAG_ALGORITHM, Algorithm::EC); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 442 | if (keySize == -1) { |
| 443 | keySize = EC_DEFAULT_KEY_SIZE; |
| 444 | } else if (keySize < EC_MIN_KEY_SIZE || keySize > EC_MAX_KEY_SIZE) { |
| 445 | ALOGI("invalid key size %d", keySize); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 446 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 447 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 448 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 449 | params.push_back(TAG_KEY_SIZE, keySize); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 450 | break; |
| 451 | } |
| 452 | case EVP_PKEY_RSA: { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 453 | params.push_back(TAG_ALGORITHM, Algorithm::RSA); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 454 | if (keySize == -1) { |
| 455 | keySize = RSA_DEFAULT_KEY_SIZE; |
| 456 | } else if (keySize < RSA_MIN_KEY_SIZE || keySize > RSA_MAX_KEY_SIZE) { |
| 457 | ALOGI("invalid key size %d", keySize); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 458 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 459 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 460 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 461 | params.push_back(TAG_KEY_SIZE, keySize); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 462 | unsigned long exponent = RSA_DEFAULT_EXPONENT; |
| 463 | if (args->size() > 1) { |
| 464 | ALOGI("invalid number of arguments: %zu", args->size()); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 465 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 466 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 467 | } else if (args->size() == 1) { |
Chih-Hung Hsieh | 24b2a39 | 2016-07-28 10:35:24 -0700 | [diff] [blame] | 468 | const sp<KeystoreArg>& expArg = args->itemAt(0); |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 469 | if (expArg != nullptr) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 470 | Unique_BIGNUM pubExpBn(BN_bin2bn( |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 471 | reinterpret_cast<const unsigned char*>(expArg->data()), expArg->size(), nullptr)); |
| 472 | if (pubExpBn.get() == nullptr) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 473 | ALOGI("Could not convert public exponent to BN"); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 474 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 475 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 476 | } |
| 477 | exponent = BN_get_word(pubExpBn.get()); |
| 478 | if (exponent == 0xFFFFFFFFL) { |
| 479 | ALOGW("cannot represent public exponent as a long value"); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 480 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 481 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 482 | } |
| 483 | } else { |
| 484 | ALOGW("public exponent not read"); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 485 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 486 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 487 | } |
| 488 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 489 | params.push_back(TAG_RSA_PUBLIC_EXPONENT, exponent); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 490 | break; |
| 491 | } |
| 492 | default: { |
| 493 | ALOGW("Unsupported key type %d", keyType); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 494 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 495 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 499 | int32_t aidl_result; |
| 500 | android::security::keymaster::KeyCharacteristics unused_characteristics; |
| 501 | auto rc = generateKey(name, KeymasterArguments(params.hidl_data()), ::std::vector<uint8_t>(), |
| 502 | targetUid, flags, &unused_characteristics, &aidl_result); |
| 503 | if (!KeyStoreServiceReturnCode(aidl_result).isOk()) { |
| 504 | ALOGW("generate failed: %d", int32_t(aidl_result)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 505 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 506 | *aidl_return = aidl_result; |
| 507 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 510 | Status KeyStoreService::import_key(const String16& name, const ::std::vector<uint8_t>& data, |
| 511 | int targetUid, int32_t flags, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 512 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 513 | const uint8_t* ptr = &data[0]; |
| 514 | |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 515 | Unique_PKCS8_PRIV_KEY_INFO pkcs8(d2i_PKCS8_PRIV_KEY_INFO(nullptr, &ptr, data.size())); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 516 | if (!pkcs8.get()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 517 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 518 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 519 | } |
| 520 | Unique_EVP_PKEY pkey(EVP_PKCS82PKEY(pkcs8.get())); |
| 521 | if (!pkey.get()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 522 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 523 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 524 | } |
| 525 | int type = EVP_PKEY_type(pkey->type); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 526 | AuthorizationSet params; |
| 527 | add_legacy_key_authorizations(type, ¶ms); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 528 | switch (type) { |
| 529 | case EVP_PKEY_RSA: |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 530 | params.push_back(TAG_ALGORITHM, Algorithm::RSA); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 531 | break; |
| 532 | case EVP_PKEY_EC: |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 533 | params.push_back(TAG_ALGORITHM, Algorithm::EC); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 534 | break; |
| 535 | default: |
| 536 | ALOGW("Unsupported key type %d", type); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 537 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 538 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 539 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 540 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 541 | int import_result; |
| 542 | auto rc = importKey(name, KeymasterArguments(params.hidl_data()), |
| 543 | static_cast<int32_t>(KeyFormat::PKCS8), data, targetUid, flags, |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 544 | /*outCharacteristics*/ nullptr, &import_result); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 545 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 546 | if (!KeyStoreServiceReturnCode(import_result).isOk()) { |
| 547 | ALOGW("importKey failed: %d", int32_t(import_result)); |
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 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 553 | Status KeyStoreService::sign(const String16& name, const ::std::vector<uint8_t>& data, |
| 554 | ::std::vector<uint8_t>* out) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 555 | if (!checkBinderPermission(P_SIGN)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 556 | return Status::fromServiceSpecificError( |
| 557 | static_cast<int32_t>(ResponseCode::PERMISSION_DENIED)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 558 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 559 | hidl_vec<uint8_t> legacy_out; |
| 560 | KeyStoreServiceReturnCode res = |
| 561 | doLegacySignVerify(name, data, &legacy_out, hidl_vec<uint8_t>(), KeyPurpose::SIGN); |
Dmitry Dementyev | ec71e7b | 2018-03-29 14:22:52 -0700 | [diff] [blame] | 562 | if (!res.isOk()) { |
| 563 | return Status::fromServiceSpecificError((res)); |
| 564 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 565 | *out = legacy_out; |
Dmitry Dementyev | ec71e7b | 2018-03-29 14:22:52 -0700 | [diff] [blame] | 566 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 569 | Status KeyStoreService::verify(const String16& name, const ::std::vector<uint8_t>& data, |
| 570 | const ::std::vector<uint8_t>& signature, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 571 | if (!checkBinderPermission(P_VERIFY)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 572 | return Status::fromServiceSpecificError( |
| 573 | static_cast<int32_t>(ResponseCode::PERMISSION_DENIED)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 574 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 575 | *aidl_return = static_cast<int32_t>( |
| 576 | doLegacySignVerify(name, data, nullptr, signature, KeyPurpose::VERIFY)); |
| 577 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /* |
| 581 | * TODO: The abstraction between things stored in hardware and regular blobs |
| 582 | * of data stored on the filesystem should be moved down to keystore itself. |
| 583 | * Unfortunately the Java code that calls this has naming conventions that it |
| 584 | * knows about. Ideally keystore shouldn't be used to store random blobs of |
| 585 | * data. |
| 586 | * |
| 587 | * Until that happens, it's necessary to have a separate "get_pubkey" and |
| 588 | * "del_key" since the Java code doesn't really communicate what it's |
| 589 | * intentions are. |
| 590 | */ |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 591 | Status KeyStoreService::get_pubkey(const String16& name, ::std::vector<uint8_t>* pubKey) { |
| 592 | android::security::keymaster::ExportResult result; |
| 593 | KeymasterBlob clientId; |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 594 | KeymasterBlob appData; |
| 595 | exportKey(name, static_cast<int32_t>(KeyFormat::X509), clientId, appData, UID_SELF, &result); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 596 | if (!result.resultCode.isOk()) { |
| 597 | ALOGW("export failed: %d", int32_t(result.resultCode)); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 598 | return Status::fromServiceSpecificError(static_cast<int32_t>(result.resultCode)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 601 | if (pubKey) *pubKey = std::move(result.exportData); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 602 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 605 | Status KeyStoreService::grant(const String16& name, int32_t granteeUid, |
| 606 | ::android::String16* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 607 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
Bo Zhu | 91de6db | 2018-03-20 12:52:13 -0700 | [diff] [blame] | 608 | auto result = |
| 609 | checkBinderPermissionAndKeystoreState(P_GRANT, /*targetUid=*/-1, /*checkUnlocked=*/false); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 610 | if (!result.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 611 | *aidl_return = String16(); |
| 612 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | String8 name8(name); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 616 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, callingUid, ::TYPE_ANY)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 617 | |
| 618 | if (access(filename.string(), R_OK) == -1) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 619 | *aidl_return = String16(); |
| 620 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 623 | *aidl_return = |
| 624 | String16(mKeyStore->addGrant(String8(name).string(), callingUid, granteeUid).c_str()); |
| 625 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 628 | 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] | 629 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
Bo Zhu | 91de6db | 2018-03-20 12:52:13 -0700 | [diff] [blame] | 630 | KeyStoreServiceReturnCode result = |
| 631 | checkBinderPermissionAndKeystoreState(P_GRANT, /*targetUid=*/-1, /*checkUnlocked=*/false); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 632 | if (!result.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 633 | *aidl_return = static_cast<int32_t>(result); |
| 634 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | String8 name8(name); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 638 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, callingUid, ::TYPE_ANY)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 639 | |
| 640 | if (access(filename.string(), R_OK) == -1) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 641 | *aidl_return = static_cast<int32_t>((errno != ENOENT) ? ResponseCode::SYSTEM_ERROR |
| 642 | : ResponseCode::KEY_NOT_FOUND); |
| 643 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 646 | *aidl_return = static_cast<int32_t>(mKeyStore->removeGrant(name8, callingUid, granteeUid) |
| 647 | ? ResponseCode::NO_ERROR |
| 648 | : ResponseCode::KEY_NOT_FOUND); |
| 649 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 652 | Status KeyStoreService::getmtime(const String16& name, int32_t uid, int64_t* time) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 653 | uid_t targetUid = getEffectiveUid(uid); |
| 654 | if (!checkBinderPermission(P_GET, targetUid)) { |
| 655 | ALOGW("permission denied for %d: getmtime", targetUid); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 656 | *time = -1L; |
| 657 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 660 | auto filename = mKeyStore->getBlobFileNameIfExists(String8(name), targetUid, ::TYPE_ANY); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 661 | |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 662 | if (!filename.isOk()) { |
| 663 | ALOGW("could not access %s for getmtime", filename.value().string()); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 664 | *time = -1L; |
| 665 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 668 | 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] | 669 | if (fd < 0) { |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 670 | ALOGW("could not open %s for getmtime", filename.value().string()); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 671 | *time = -1L; |
| 672 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | struct stat s; |
| 676 | int ret = fstat(fd, &s); |
| 677 | close(fd); |
| 678 | if (ret == -1) { |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 679 | ALOGW("could not stat %s for getmtime", filename.value().string()); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 680 | *time = -1L; |
| 681 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 684 | *time = static_cast<int64_t>(s.st_mtime); |
| 685 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 688 | Status KeyStoreService::is_hardware_backed(const String16& keyType, int32_t* aidl_return) { |
| 689 | *aidl_return = static_cast<int32_t>(mKeyStore->isHardwareBacked(keyType) ? 1 : 0); |
| 690 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 693 | Status KeyStoreService::clear_uid(int64_t targetUid64, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 694 | uid_t targetUid = getEffectiveUid(targetUid64); |
| 695 | if (!checkBinderPermissionSelfOrSystem(P_CLEAR_UID, targetUid)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 696 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 697 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 698 | } |
Rubin Xu | 7675c9f | 2017-03-15 19:26:52 +0000 | [diff] [blame] | 699 | ALOGI("clear_uid %" PRId64, targetUid64); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 700 | |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 701 | mKeyStore->removeAllGrantsToUid(targetUid); |
| 702 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 703 | String8 prefix = String8::format("%u_", targetUid); |
| 704 | Vector<String16> aliases; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 705 | if (mKeyStore->list(prefix, &aliases, get_user_id(targetUid)) != ResponseCode::NO_ERROR) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 706 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 707 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | for (uint32_t i = 0; i < aliases.size(); i++) { |
| 711 | String8 name8(aliases[i]); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 712 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, targetUid, ::TYPE_ANY)); |
Rubin Xu | 85c85e9 | 2017-04-26 20:07:30 +0100 | [diff] [blame] | 713 | |
| 714 | if (get_app_id(targetUid) == AID_SYSTEM) { |
| 715 | Blob keyBlob; |
| 716 | ResponseCode responseCode = |
| 717 | mKeyStore->get(filename.string(), &keyBlob, ::TYPE_ANY, get_user_id(targetUid)); |
| 718 | if (responseCode == ResponseCode::NO_ERROR && keyBlob.isCriticalToDeviceEncryption()) { |
| 719 | // Do not clear keys critical to device encryption under system uid. |
| 720 | continue; |
| 721 | } |
| 722 | } |
| 723 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 724 | mKeyStore->del(filename.string(), ::TYPE_ANY, get_user_id(targetUid)); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 725 | |
| 726 | // 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] | 727 | String8 chr_filename( |
| 728 | mKeyStore->getKeyNameForUidWithDir(name8, targetUid, ::TYPE_KEY_CHARACTERISTICS)); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 729 | mKeyStore->del(chr_filename.string(), ::TYPE_KEY_CHARACTERISTICS, get_user_id(targetUid)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 730 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 731 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 732 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 735 | Status KeyStoreService::addRngEntropy(const ::std::vector<uint8_t>& entropy, int32_t flags, |
| 736 | int32_t* aidl_return) { |
| 737 | auto device = mKeyStore->getDevice(flagsToSecurityLevel(flags)); |
| 738 | if (!device) { |
| 739 | *aidl_return = static_cast<int32_t>(ErrorCode::HARDWARE_TYPE_UNAVAILABLE); |
| 740 | } else { |
| 741 | *aidl_return = static_cast<int32_t>( |
| 742 | KeyStoreServiceReturnCode(KS_HANDLE_HIDL_ERROR(device->addRngEntropy(entropy)))); |
| 743 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 744 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 747 | Status |
| 748 | KeyStoreService::generateKey(const String16& name, const KeymasterArguments& params, |
| 749 | const ::std::vector<uint8_t>& entropy, int uid, int flags, |
| 750 | android::security::keymaster::KeyCharacteristics* outCharacteristics, |
| 751 | int32_t* aidl_return) { |
Max Bires | ef4f067 | 2017-11-29 14:38:48 -0800 | [diff] [blame] | 752 | // TODO(jbires): remove this getCallingUid call upon implementation of b/25646100 |
| 753 | uid_t originalUid = IPCThreadState::self()->getCallingUid(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 754 | uid = getEffectiveUid(uid); |
Pavel Grafov | ff311b4 | 2018-01-24 20:34:37 +0000 | [diff] [blame] | 755 | auto logOnScopeExit = android::base::make_scope_guard([&] { |
| 756 | if (__android_log_security()) { |
| 757 | android_log_event_list(SEC_TAG_AUTH_KEY_GENERATED) |
| 758 | << int32_t(*aidl_return == static_cast<int32_t>(ResponseCode::NO_ERROR)) |
| 759 | << String8(name) << int32_t(uid) << LOG_ID_SECURITY; |
| 760 | } |
| 761 | }); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 762 | KeyStoreServiceReturnCode rc = |
| 763 | checkBinderPermissionAndKeystoreState(P_INSERT, uid, flags & KEYSTORE_FLAG_ENCRYPTED); |
| 764 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 765 | *aidl_return = static_cast<int32_t>(rc); |
| 766 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 767 | } |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 768 | if ((flags & KEYSTORE_FLAG_CRITICAL_TO_DEVICE_ENCRYPTION) && get_app_id(uid) != AID_SYSTEM) { |
| 769 | 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] | 770 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 771 | return Status::ok(); |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 772 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 773 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 774 | if (containsTag(params.getParameters(), Tag::INCLUDE_UNIQUE_ID)) { |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 775 | // TODO(jbires): remove uid checking upon implementation of b/25646100 |
Max Bires | 670467d | 2017-12-12 11:16:43 -0800 | [diff] [blame] | 776 | if (!checkBinderPermission(P_GEN_UNIQUE_ID) || |
Max Bires | ef4f067 | 2017-11-29 14:38:48 -0800 | [diff] [blame] | 777 | originalUid != IPCThreadState::self()->getCallingUid()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 778 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 779 | return Status::ok(); |
| 780 | } |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 781 | } |
| 782 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 783 | SecurityLevel securityLevel = flagsToSecurityLevel(flags); |
| 784 | auto dev = mKeyStore->getDevice(securityLevel); |
| 785 | if (!dev) { |
| 786 | *aidl_return = static_cast<int32_t>(ErrorCode::HARDWARE_TYPE_UNAVAILABLE); |
| 787 | return Status::ok(); |
| 788 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 789 | AuthorizationSet keyCharacteristics = params.getParameters(); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 790 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 791 | // TODO: Seed from Linux RNG before this. |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 792 | rc = KS_HANDLE_HIDL_ERROR(dev->addRngEntropy(entropy)); |
| 793 | if (!rc.isOk()) { |
| 794 | *aidl_return = static_cast<int32_t>(rc); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 795 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 798 | KeyStoreServiceReturnCode error; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 799 | auto hidl_cb = [&](ErrorCode ret, const ::std::vector<uint8_t>& hidlKeyBlob, |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 800 | const KeyCharacteristics& keyCharacteristics) { |
| 801 | error = ret; |
| 802 | if (!error.isOk()) { |
| 803 | return; |
| 804 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 805 | if (outCharacteristics) |
| 806 | *outCharacteristics = |
| 807 | ::android::security::keymaster::KeyCharacteristics(keyCharacteristics); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 808 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 809 | // Write the key |
| 810 | String8 name8(name); |
| 811 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, uid, ::TYPE_KEYMASTER_10)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 812 | |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 813 | Blob keyBlob(&hidlKeyBlob[0], hidlKeyBlob.size(), nullptr, 0, ::TYPE_KEYMASTER_10); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 814 | keyBlob.setSecurityLevel(securityLevel); |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 815 | keyBlob.setCriticalToDeviceEncryption(flags & KEYSTORE_FLAG_CRITICAL_TO_DEVICE_ENCRYPTION); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 816 | if (isAuthenticationBound(params.getParameters()) && |
| 817 | !keyBlob.isCriticalToDeviceEncryption()) { |
Shawn Willden | d5a24e6 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 818 | keyBlob.setSuperEncrypted(true); |
| 819 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 820 | keyBlob.setEncrypted(flags & KEYSTORE_FLAG_ENCRYPTED); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 821 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 822 | error = mKeyStore->put(filename.string(), &keyBlob, get_user_id(uid)); |
| 823 | }; |
| 824 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 825 | rc = KS_HANDLE_HIDL_ERROR(dev->generateKey(params.getParameters(), hidl_cb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 826 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 827 | *aidl_return = static_cast<int32_t>(rc); |
| 828 | return Status::ok(); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 829 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 830 | if (!error.isOk()) { |
| 831 | ALOGE("Failed to generate key -> falling back to software keymaster"); |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 832 | uploadKeyCharacteristicsAsProto(params.getParameters(), false /* wasCreationSuccessful */); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 833 | securityLevel = SecurityLevel::SOFTWARE; |
Janis Danisevskis | 4a1da2f | 2018-03-26 15:02:38 -0700 | [diff] [blame] | 834 | |
| 835 | // No fall back for 3DES |
| 836 | for (auto& param : params.getParameters()) { |
| 837 | auto algorithm = authorizationValue(TAG_ALGORITHM, param); |
| 838 | if (algorithm.isOk() && algorithm.value() == Algorithm::TRIPLE_DES) { |
| 839 | *aidl_return = static_cast<int32_t>(ErrorCode::UNSUPPORTED_ALGORITHM); |
| 840 | return Status::ok(); |
| 841 | } |
| 842 | } |
| 843 | |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 844 | auto fallback = mKeyStore->getFallbackDevice(); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 845 | if (!fallback) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 846 | *aidl_return = static_cast<int32_t>(error); |
| 847 | return Status::ok(); |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 848 | } |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 849 | rc = KS_HANDLE_HIDL_ERROR(fallback->generateKey(params.getParameters(), hidl_cb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 850 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 851 | *aidl_return = static_cast<int32_t>(rc); |
| 852 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 853 | } |
| 854 | if (!error.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 855 | *aidl_return = static_cast<int32_t>(error); |
| 856 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 857 | } |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 858 | } else { |
| 859 | uploadKeyCharacteristicsAsProto(params.getParameters(), true /* wasCreationSuccessful */); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 860 | } |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 861 | |
Brian C. Young | 78daac2 | 2018-03-31 15:02:34 -0700 | [diff] [blame] | 862 | if (!containsTag(params.getParameters(), Tag::USER_ID)) { |
| 863 | // Most Java processes don't have access to this tag |
| 864 | KeyParameter user_id; |
| 865 | user_id.tag = Tag::USER_ID; |
Brian Claire Young | 3133c45 | 2018-08-31 13:56:49 -0700 | [diff] [blame] | 866 | user_id.f.integer = multiuser_get_user_id(uid); |
Brian C. Young | 78daac2 | 2018-03-31 15:02:34 -0700 | [diff] [blame] | 867 | keyCharacteristics.push_back(user_id); |
| 868 | } |
| 869 | |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 870 | // Write the characteristics: |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 871 | String8 name8(name); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 872 | String8 cFilename(mKeyStore->getKeyNameForUidWithDir(name8, uid, ::TYPE_KEY_CHARACTERISTICS)); |
| 873 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 874 | std::stringstream kc_stream; |
| 875 | keyCharacteristics.Serialize(&kc_stream); |
| 876 | if (kc_stream.bad()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 877 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 878 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 879 | } |
| 880 | auto kc_buf = kc_stream.str(); |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 881 | 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] | 882 | ::TYPE_KEY_CHARACTERISTICS); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 883 | charBlob.setSecurityLevel(securityLevel); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 884 | charBlob.setEncrypted(flags & KEYSTORE_FLAG_ENCRYPTED); |
| 885 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 886 | *aidl_return = |
| 887 | static_cast<int32_t>(mKeyStore->put(cFilename.string(), &charBlob, get_user_id(uid))); |
| 888 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 891 | Status KeyStoreService::getKeyCharacteristics( |
| 892 | const String16& name, const ::android::security::keymaster::KeymasterBlob& clientId, |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 893 | const ::android::security::keymaster::KeymasterBlob& appData, int32_t uid, |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 894 | ::android::security::keymaster::KeyCharacteristics* outCharacteristics, int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 895 | if (!outCharacteristics) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 896 | *aidl_return = |
| 897 | static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::UNEXPECTED_NULL_POINTER)); |
| 898 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | uid_t targetUid = getEffectiveUid(uid); |
| 902 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 903 | if (!is_granted_to(callingUid, targetUid)) { |
| 904 | ALOGW("uid %d not permitted to act for uid %d in getKeyCharacteristics", callingUid, |
| 905 | targetUid); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 906 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 907 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | Blob keyBlob; |
| 911 | String8 name8(name); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 912 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 913 | KeyStoreServiceReturnCode rc = |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 914 | mKeyStore->getKeyForName(&keyBlob, name8, targetUid, TYPE_KEYMASTER_10); |
Janis Danisevskis | d714a67 | 2017-09-01 14:31:36 -0700 | [diff] [blame] | 915 | if (rc == ResponseCode::UNINITIALIZED) { |
| 916 | /* |
| 917 | * If we fail reading the blob because the master key is missing we try to retrieve the |
| 918 | * key characteristics from the characteristics file. This happens when auth-bound |
| 919 | * keys are used after a screen lock has been removed by the user. |
| 920 | */ |
| 921 | rc = mKeyStore->getKeyForName(&keyBlob, name8, targetUid, TYPE_KEY_CHARACTERISTICS); |
| 922 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 923 | *aidl_return = static_cast<int32_t>(rc); |
| 924 | return Status::ok(); |
Janis Danisevskis | d714a67 | 2017-09-01 14:31:36 -0700 | [diff] [blame] | 925 | } |
| 926 | AuthorizationSet keyCharacteristics; |
| 927 | // TODO write one shot stream buffer to avoid copying (twice here) |
| 928 | std::string charBuffer(reinterpret_cast<const char*>(keyBlob.getValue()), |
| 929 | keyBlob.getLength()); |
| 930 | std::stringstream charStream(charBuffer); |
| 931 | keyCharacteristics.Deserialize(&charStream); |
| 932 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 933 | outCharacteristics->softwareEnforced = KeymasterArguments(keyCharacteristics.hidl_data()); |
| 934 | *aidl_return = static_cast<int32_t>(rc); |
| 935 | return Status::ok(); |
Janis Danisevskis | d714a67 | 2017-09-01 14:31:36 -0700 | [diff] [blame] | 936 | } else if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 937 | *aidl_return = static_cast<int32_t>(rc); |
| 938 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 939 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 940 | |
| 941 | auto hidlKeyBlob = blob2hidlVec(keyBlob); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 942 | auto dev = mKeyStore->getDevice(keyBlob); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 943 | |
| 944 | KeyStoreServiceReturnCode error; |
| 945 | |
| 946 | auto hidlCb = [&](ErrorCode ret, const KeyCharacteristics& keyCharacteristics) { |
| 947 | error = ret; |
| 948 | if (!error.isOk()) { |
Pavel Grafov | cef3947 | 2018-02-12 18:45:02 +0000 | [diff] [blame] | 949 | if (error == ErrorCode::INVALID_KEY_BLOB) { |
| 950 | log_key_integrity_violation(name8, targetUid); |
| 951 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 952 | return; |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 953 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 954 | *outCharacteristics = |
| 955 | ::android::security::keymaster::KeyCharacteristics(keyCharacteristics); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 956 | }; |
| 957 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 958 | rc = KS_HANDLE_HIDL_ERROR( |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 959 | dev->getKeyCharacteristics(hidlKeyBlob, clientId.getData(), appData.getData(), hidlCb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 960 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 961 | *aidl_return = static_cast<int32_t>(rc); |
| 962 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | if (error == ErrorCode::KEY_REQUIRES_UPGRADE) { |
| 966 | AuthorizationSet upgradeParams; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 967 | if (clientId.getData().size()) { |
| 968 | upgradeParams.push_back(TAG_APPLICATION_ID, clientId.getData()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 969 | } |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 970 | if (appData.getData().size()) { |
| 971 | upgradeParams.push_back(TAG_APPLICATION_DATA, appData.getData()); |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 972 | } |
| 973 | rc = upgradeKeyBlob(name, targetUid, upgradeParams, &keyBlob); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 974 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 975 | *aidl_return = static_cast<int32_t>(rc); |
| 976 | return Status::ok(); |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 977 | } |
Shawn Willden | 715d023 | 2016-01-21 00:45:13 -0700 | [diff] [blame] | 978 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 979 | auto upgradedHidlKeyBlob = blob2hidlVec(keyBlob); |
| 980 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 981 | rc = KS_HANDLE_HIDL_ERROR(dev->getKeyCharacteristics( |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 982 | upgradedHidlKeyBlob, clientId.getData(), appData.getData(), hidlCb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 983 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 984 | *aidl_return = static_cast<int32_t>(rc); |
| 985 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 986 | } |
| 987 | // Note that, on success, "error" will have been updated by the hidlCB callback. |
| 988 | // So it is fine to return "error" below. |
| 989 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 990 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(error)); |
| 991 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 992 | } |
| 993 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 994 | Status |
| 995 | KeyStoreService::importKey(const String16& name, const KeymasterArguments& params, int32_t format, |
| 996 | const ::std::vector<uint8_t>& keyData, int uid, int flags, |
| 997 | ::android::security::keymaster::KeyCharacteristics* outCharacteristics, |
| 998 | int32_t* aidl_return) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 999 | uid = getEffectiveUid(uid); |
Pavel Grafov | ff311b4 | 2018-01-24 20:34:37 +0000 | [diff] [blame] | 1000 | auto logOnScopeExit = android::base::make_scope_guard([&] { |
| 1001 | if (__android_log_security()) { |
| 1002 | android_log_event_list(SEC_TAG_KEY_IMPORTED) |
| 1003 | << int32_t(*aidl_return == static_cast<int32_t>(ResponseCode::NO_ERROR)) |
| 1004 | << String8(name) << int32_t(uid) << LOG_ID_SECURITY; |
| 1005 | } |
| 1006 | }); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1007 | KeyStoreServiceReturnCode rc = |
| 1008 | checkBinderPermissionAndKeystoreState(P_INSERT, uid, flags & KEYSTORE_FLAG_ENCRYPTED); |
| 1009 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1010 | *aidl_return = static_cast<int32_t>(rc); |
| 1011 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1012 | } |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 1013 | if ((flags & KEYSTORE_FLAG_CRITICAL_TO_DEVICE_ENCRYPTION) && get_app_id(uid) != AID_SYSTEM) { |
| 1014 | 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] | 1015 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 1016 | return Status::ok(); |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 1017 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1018 | |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1019 | SecurityLevel securityLevel = flagsToSecurityLevel(flags); |
| 1020 | auto dev = mKeyStore->getDevice(securityLevel); |
| 1021 | if (!dev) { |
| 1022 | *aidl_return = static_cast<int32_t>(ErrorCode::HARDWARE_TYPE_UNAVAILABLE); |
| 1023 | return Status::ok(); |
| 1024 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1025 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1026 | String8 name8(name); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1027 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1028 | KeyStoreServiceReturnCode error; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1029 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1030 | auto hidlCb = [&](ErrorCode ret, const ::std::vector<uint8_t>& keyBlob, |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1031 | const KeyCharacteristics& keyCharacteristics) { |
| 1032 | error = ret; |
| 1033 | if (!error.isOk()) { |
| 1034 | return; |
| 1035 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1036 | if (outCharacteristics) |
| 1037 | *outCharacteristics = |
| 1038 | ::android::security::keymaster::KeyCharacteristics(keyCharacteristics); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1039 | |
| 1040 | // Write the key: |
| 1041 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, uid, ::TYPE_KEYMASTER_10)); |
| 1042 | |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 1043 | Blob ksBlob(&keyBlob[0], keyBlob.size(), nullptr, 0, ::TYPE_KEYMASTER_10); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1044 | ksBlob.setSecurityLevel(securityLevel); |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 1045 | ksBlob.setCriticalToDeviceEncryption(flags & KEYSTORE_FLAG_CRITICAL_TO_DEVICE_ENCRYPTION); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1046 | if (isAuthenticationBound(params.getParameters()) && |
| 1047 | !ksBlob.isCriticalToDeviceEncryption()) { |
Shawn Willden | d5a24e6 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 1048 | ksBlob.setSuperEncrypted(true); |
| 1049 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1050 | ksBlob.setEncrypted(flags & KEYSTORE_FLAG_ENCRYPTED); |
| 1051 | |
| 1052 | error = mKeyStore->put(filename.string(), &ksBlob, get_user_id(uid)); |
| 1053 | }; |
| 1054 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1055 | rc = KS_HANDLE_HIDL_ERROR( |
| 1056 | dev->importKey(params.getParameters(), KeyFormat(format), keyData, hidlCb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1057 | // possible hidl error |
| 1058 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1059 | *aidl_return = static_cast<int32_t>(rc); |
| 1060 | return Status::ok(); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1061 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1062 | // now check error from callback |
| 1063 | if (!error.isOk()) { |
| 1064 | ALOGE("Failed to import key -> falling back to software keymaster"); |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 1065 | uploadKeyCharacteristicsAsProto(params.getParameters(), false /* wasCreationSuccessful */); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1066 | securityLevel = SecurityLevel::SOFTWARE; |
Janis Danisevskis | 4a1da2f | 2018-03-26 15:02:38 -0700 | [diff] [blame] | 1067 | |
| 1068 | // No fall back for 3DES |
| 1069 | for (auto& param : params.getParameters()) { |
| 1070 | auto algorithm = authorizationValue(TAG_ALGORITHM, param); |
| 1071 | if (algorithm.isOk() && algorithm.value() == Algorithm::TRIPLE_DES) { |
| 1072 | *aidl_return = static_cast<int32_t>(ErrorCode::UNSUPPORTED_ALGORITHM); |
| 1073 | return Status::ok(); |
| 1074 | } |
| 1075 | } |
| 1076 | |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 1077 | auto fallback = mKeyStore->getFallbackDevice(); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1078 | if (!fallback) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1079 | *aidl_return = static_cast<int32_t>(error); |
| 1080 | return Status::ok(); |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 1081 | } |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1082 | rc = KS_HANDLE_HIDL_ERROR( |
| 1083 | fallback->importKey(params.getParameters(), KeyFormat(format), keyData, hidlCb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1084 | // possible hidl error |
| 1085 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1086 | *aidl_return = static_cast<int32_t>(rc); |
| 1087 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1088 | } |
| 1089 | // now check error from callback |
| 1090 | if (!error.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1091 | *aidl_return = static_cast<int32_t>(error); |
| 1092 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1093 | } |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 1094 | } else { |
| 1095 | uploadKeyCharacteristicsAsProto(params.getParameters(), true /* wasCreationSuccessful */); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1096 | } |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1097 | |
| 1098 | // Write the characteristics: |
| 1099 | String8 cFilename(mKeyStore->getKeyNameForUidWithDir(name8, uid, ::TYPE_KEY_CHARACTERISTICS)); |
| 1100 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1101 | AuthorizationSet opParams = params.getParameters(); |
Brian C. Young | 78daac2 | 2018-03-31 15:02:34 -0700 | [diff] [blame] | 1102 | if (!containsTag(params.getParameters(), Tag::USER_ID)) { |
| 1103 | // Most Java processes don't have access to this tag |
| 1104 | KeyParameter user_id; |
| 1105 | user_id.tag = Tag::USER_ID; |
Brian Claire Young | 3133c45 | 2018-08-31 13:56:49 -0700 | [diff] [blame] | 1106 | user_id.f.integer = multiuser_get_user_id(uid); |
Brian C. Young | 78daac2 | 2018-03-31 15:02:34 -0700 | [diff] [blame] | 1107 | opParams.push_back(user_id); |
| 1108 | } |
| 1109 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1110 | std::stringstream kcStream; |
| 1111 | opParams.Serialize(&kcStream); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1112 | if (kcStream.bad()) { |
| 1113 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 1114 | return Status::ok(); |
| 1115 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1116 | auto kcBuf = kcStream.str(); |
| 1117 | |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 1118 | 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] | 1119 | ::TYPE_KEY_CHARACTERISTICS); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1120 | charBlob.setSecurityLevel(securityLevel); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1121 | charBlob.setEncrypted(flags & KEYSTORE_FLAG_ENCRYPTED); |
| 1122 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1123 | *aidl_return = |
| 1124 | static_cast<int32_t>(mKeyStore->put(cFilename.string(), &charBlob, get_user_id(uid))); |
| 1125 | |
| 1126 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1129 | Status KeyStoreService::exportKey(const String16& name, int32_t format, |
| 1130 | const ::android::security::keymaster::KeymasterBlob& clientId, |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 1131 | const ::android::security::keymaster::KeymasterBlob& appData, |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1132 | int32_t uid, ExportResult* result) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1133 | |
| 1134 | uid_t targetUid = getEffectiveUid(uid); |
| 1135 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1136 | if (!is_granted_to(callingUid, targetUid)) { |
| 1137 | 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] | 1138 | result->resultCode = ResponseCode::PERMISSION_DENIED; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1139 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | Blob keyBlob; |
| 1143 | String8 name8(name); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1144 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1145 | result->resultCode = mKeyStore->getKeyForName(&keyBlob, name8, targetUid, TYPE_KEYMASTER_10); |
| 1146 | if (!result->resultCode.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1147 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1148 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1149 | |
| 1150 | auto key = blob2hidlVec(keyBlob); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1151 | auto dev = mKeyStore->getDevice(keyBlob); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1152 | |
| 1153 | auto hidlCb = [&](ErrorCode ret, const ::android::hardware::hidl_vec<uint8_t>& keyMaterial) { |
| 1154 | result->resultCode = ret; |
| 1155 | if (!result->resultCode.isOk()) { |
Pavel Grafov | cef3947 | 2018-02-12 18:45:02 +0000 | [diff] [blame] | 1156 | if (result->resultCode == ErrorCode::INVALID_KEY_BLOB) { |
| 1157 | log_key_integrity_violation(name8, targetUid); |
| 1158 | } |
Ji Wang | 2c14231 | 2016-10-14 17:21:10 +0800 | [diff] [blame] | 1159 | return; |
| 1160 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1161 | result->exportData = keyMaterial; |
| 1162 | }; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1163 | KeyStoreServiceReturnCode rc = KS_HANDLE_HIDL_ERROR( |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 1164 | dev->exportKey(KeyFormat(format), key, clientId.getData(), appData.getData(), hidlCb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1165 | // Overwrite result->resultCode only on HIDL error. Otherwise we want the result set in the |
| 1166 | // callback hidlCb. |
| 1167 | if (!rc.isOk()) { |
| 1168 | result->resultCode = rc; |
Ji Wang | 2c14231 | 2016-10-14 17:21:10 +0800 | [diff] [blame] | 1169 | } |
| 1170 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1171 | if (result->resultCode == ErrorCode::KEY_REQUIRES_UPGRADE) { |
| 1172 | AuthorizationSet upgradeParams; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1173 | if (clientId.getData().size()) { |
| 1174 | upgradeParams.push_back(TAG_APPLICATION_ID, clientId.getData()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1175 | } |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 1176 | if (appData.getData().size()) { |
| 1177 | upgradeParams.push_back(TAG_APPLICATION_DATA, appData.getData()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1178 | } |
| 1179 | result->resultCode = upgradeKeyBlob(name, targetUid, upgradeParams, &keyBlob); |
| 1180 | if (!result->resultCode.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1181 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | auto upgradedHidlKeyBlob = blob2hidlVec(keyBlob); |
| 1185 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1186 | result->resultCode = KS_HANDLE_HIDL_ERROR(dev->exportKey( |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 1187 | KeyFormat(format), upgradedHidlKeyBlob, clientId.getData(), appData.getData(), hidlCb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1188 | if (!result->resultCode.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1189 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1190 | } |
| 1191 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1192 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1195 | Status KeyStoreService::begin(const sp<IBinder>& appToken, const String16& name, int32_t purpose, |
| 1196 | bool pruneable, const KeymasterArguments& params, |
| 1197 | const ::std::vector<uint8_t>& entropy, int32_t uid, |
| 1198 | OperationResult* result) { |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1199 | auto keyPurpose = static_cast<KeyPurpose>(purpose); |
| 1200 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1201 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1202 | uid_t targetUid = getEffectiveUid(uid); |
| 1203 | if (!is_granted_to(callingUid, targetUid)) { |
| 1204 | 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] | 1205 | result->resultCode = ResponseCode::PERMISSION_DENIED; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1206 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1207 | } |
| 1208 | if (!pruneable && get_app_id(callingUid) != AID_SYSTEM) { |
| 1209 | ALOGE("Non-system uid %d trying to start non-pruneable operation", callingUid); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1210 | result->resultCode = ResponseCode::PERMISSION_DENIED; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1211 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1212 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1213 | if (!checkAllowedOperationParams(params.getParameters())) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1214 | result->resultCode = ErrorCode::INVALID_ARGUMENT; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1215 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1216 | } |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1217 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1218 | Blob keyBlob; |
| 1219 | String8 name8(name); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1220 | result->resultCode = mKeyStore->getKeyForName(&keyBlob, name8, targetUid, TYPE_KEYMASTER_10); |
Shawn Willden | d5a24e6 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 1221 | if (result->resultCode == ResponseCode::LOCKED && keyBlob.isSuperEncrypted()) { |
| 1222 | result->resultCode = ErrorCode::KEY_USER_NOT_AUTHENTICATED; |
| 1223 | } |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1224 | if (!result->resultCode.isOk()) return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1225 | |
| 1226 | auto key = blob2hidlVec(keyBlob); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1227 | auto dev = mKeyStore->getDevice(keyBlob); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1228 | AuthorizationSet opParams = params.getParameters(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1229 | KeyCharacteristics characteristics; |
| 1230 | result->resultCode = getOperationCharacteristics(key, &dev, opParams, &characteristics); |
| 1231 | |
| 1232 | if (result->resultCode == ErrorCode::KEY_REQUIRES_UPGRADE) { |
| 1233 | result->resultCode = upgradeKeyBlob(name, targetUid, opParams, &keyBlob); |
| 1234 | if (!result->resultCode.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1235 | return Status::ok(); |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 1236 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1237 | key = blob2hidlVec(keyBlob); |
| 1238 | result->resultCode = getOperationCharacteristics(key, &dev, opParams, &characteristics); |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 1239 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1240 | if (!result->resultCode.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1241 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1242 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1243 | |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1244 | // Merge these characteristics with the ones cached when the key was generated or imported |
| 1245 | Blob charBlob; |
| 1246 | AuthorizationSet persistedCharacteristics; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1247 | result->resultCode = |
| 1248 | mKeyStore->getKeyForName(&charBlob, name8, targetUid, TYPE_KEY_CHARACTERISTICS); |
| 1249 | if (result->resultCode.isOk()) { |
| 1250 | // TODO write one shot stream buffer to avoid copying (twice here) |
| 1251 | std::string charBuffer(reinterpret_cast<const char*>(charBlob.getValue()), |
| 1252 | charBlob.getLength()); |
| 1253 | std::stringstream charStream(charBuffer); |
| 1254 | persistedCharacteristics.Deserialize(&charStream); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1255 | } else { |
| 1256 | ALOGD("Unable to read cached characteristics for key"); |
| 1257 | } |
| 1258 | |
| 1259 | // 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] | 1260 | AuthorizationSet softwareEnforced = characteristics.softwareEnforced; |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1261 | AuthorizationSet hardwareEnforced = characteristics.hardwareEnforced; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1262 | persistedCharacteristics.Union(softwareEnforced); |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1263 | persistedCharacteristics.Subtract(hardwareEnforced); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1264 | characteristics.softwareEnforced = persistedCharacteristics.hidl_data(); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1265 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1266 | KeyStoreServiceReturnCode authResult; |
| 1267 | HardwareAuthToken authToken; |
| 1268 | std::tie(authResult, authToken) = |
| 1269 | getAuthToken(characteristics, 0 /* no challenge */, keyPurpose, |
| 1270 | /*failOnTokenMissing*/ false); |
| 1271 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1272 | // If per-operation auth is needed we need to begin the operation and |
| 1273 | // the client will need to authorize that operation before calling |
| 1274 | // update. Any other auth issues stop here. |
Shawn Willden | 827243a | 2017-09-12 05:41:33 -0600 | [diff] [blame] | 1275 | if (!authResult.isOk() && authResult != ResponseCode::OP_AUTH_NEEDED) { |
| 1276 | result->resultCode = authResult; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1277 | return Status::ok(); |
Shawn Willden | 827243a | 2017-09-12 05:41:33 -0600 | [diff] [blame] | 1278 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1279 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1280 | // Add entropy to the device first. |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1281 | if (entropy.size()) { |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1282 | result->resultCode = KS_HANDLE_HIDL_ERROR(dev->addRngEntropy(entropy)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1283 | if (!result->resultCode.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1284 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1285 | } |
| 1286 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1287 | |
| 1288 | // Create a keyid for this key. |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1289 | km_id_t keyid; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1290 | if (!enforcement_policy.CreateKeyId(key, &keyid)) { |
| 1291 | ALOGE("Failed to create a key ID for authorization checking."); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1292 | result->resultCode = ErrorCode::UNKNOWN_ERROR; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1293 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | // Check that all key authorization policy requirements are met. |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1297 | AuthorizationSet key_auths = characteristics.hardwareEnforced; |
| 1298 | key_auths.append(characteristics.softwareEnforced.begin(), |
| 1299 | characteristics.softwareEnforced.end()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1300 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1301 | result->resultCode = |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1302 | enforcement_policy.AuthorizeOperation(keyPurpose, keyid, key_auths, opParams, authToken, |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1303 | 0 /* op_handle */, true /* is_begin_operation */); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1304 | if (!result->resultCode.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1305 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1308 | // 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] | 1309 | // pruneable. |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1310 | while (mOperationMap.getOperationCount() >= kMaxOperations) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1311 | ALOGD("Reached or exceeded concurrent operations limit"); |
| 1312 | if (!pruneOperation()) { |
| 1313 | break; |
| 1314 | } |
| 1315 | } |
| 1316 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1317 | auto hidlCb = [&](ErrorCode ret, const hidl_vec<KeyParameter>& outParams, |
| 1318 | uint64_t operationHandle) { |
| 1319 | result->resultCode = ret; |
| 1320 | if (!result->resultCode.isOk()) { |
Pavel Grafov | cef3947 | 2018-02-12 18:45:02 +0000 | [diff] [blame] | 1321 | if (result->resultCode == ErrorCode::INVALID_KEY_BLOB) { |
| 1322 | log_key_integrity_violation(name8, targetUid); |
| 1323 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1324 | return; |
| 1325 | } |
| 1326 | result->handle = operationHandle; |
| 1327 | result->outParams = outParams; |
| 1328 | }; |
| 1329 | |
Janis Danisevskis | 9c5ea7b | 2018-06-21 17:53:15 -0700 | [diff] [blame^] | 1330 | KeyStoreServiceReturnCode rc = |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1331 | 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^] | 1332 | if (!rc.isOk()) { |
| 1333 | LOG(ERROR) << "Got error " << rc << " from begin()"; |
| 1334 | result->resultCode = ResponseCode::SYSTEM_ERROR; |
| 1335 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1336 | } |
| 1337 | |
Janis Danisevskis | 9c5ea7b | 2018-06-21 17:53:15 -0700 | [diff] [blame^] | 1338 | rc = result->resultCode; |
| 1339 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1340 | // If there are too many operations abort the oldest operation that was |
| 1341 | // started as pruneable and try again. |
Janis Danisevskis | 9c5ea7b | 2018-06-21 17:53:15 -0700 | [diff] [blame^] | 1342 | LOG(INFO) << rc << " " << mOperationMap.hasPruneableOperation(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1343 | while (rc == ErrorCode::TOO_MANY_OPERATIONS && mOperationMap.hasPruneableOperation()) { |
Janis Danisevskis | 9c5ea7b | 2018-06-21 17:53:15 -0700 | [diff] [blame^] | 1344 | LOG(INFO) << "Ran out of operation handles"; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1345 | if (!pruneOperation()) { |
| 1346 | break; |
| 1347 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1348 | rc = KS_HANDLE_HIDL_ERROR( |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1349 | dev->begin(keyPurpose, key, opParams.hidl_data(), authToken, hidlCb)); |
Janis Danisevskis | 9c5ea7b | 2018-06-21 17:53:15 -0700 | [diff] [blame^] | 1350 | if (!rc.isOk()) { |
| 1351 | LOG(ERROR) << "Got error " << rc << " from begin()"; |
| 1352 | result->resultCode = ResponseCode::SYSTEM_ERROR; |
| 1353 | return Status::ok(); |
| 1354 | } |
| 1355 | rc = result->resultCode; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1356 | } |
Janis Danisevskis | 9c5ea7b | 2018-06-21 17:53:15 -0700 | [diff] [blame^] | 1357 | if (!rc.isOk()) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1358 | result->resultCode = rc; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1359 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
Shawn Willden | 2eef135 | 2018-05-22 16:13:24 -0600 | [diff] [blame] | 1362 | VerificationToken verificationToken; |
| 1363 | if (authResult.isOk() && authToken.mac.size() && |
| 1364 | dev->halVersion().securityLevel == SecurityLevel::STRONGBOX) { |
| 1365 | // This operation needs an auth token, but the device is a STRONGBOX, so it can't check the |
| 1366 | // timestamp in the auth token. Get a VerificationToken from the TEE, which will be passed |
| 1367 | // to update() and begin(). |
| 1368 | rc = KS_HANDLE_HIDL_ERROR(mKeyStore->getDevice(SecurityLevel::TRUSTED_ENVIRONMENT) |
| 1369 | ->verifyAuthorization(result->handle, |
| 1370 | {} /* parametersToVerify */, authToken, |
| 1371 | [&](auto error, const auto& token) { |
| 1372 | result->resultCode = error; |
| 1373 | verificationToken = token; |
| 1374 | })); |
| 1375 | |
Janis Danisevskis | 9c5ea7b | 2018-06-21 17:53:15 -0700 | [diff] [blame^] | 1376 | if (!rc.isOk()) result->resultCode = rc; |
| 1377 | if (!result->resultCode.isOk()) return Status::ok(); |
Shawn Willden | 2eef135 | 2018-05-22 16:13:24 -0600 | [diff] [blame] | 1378 | } |
| 1379 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1380 | // Note: The operation map takes possession of the contents of "characteristics". |
| 1381 | // 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] | 1382 | sp<IBinder> operationToken = |
| 1383 | mOperationMap.addOperation(result->handle, keyid, keyPurpose, dev, appToken, |
| 1384 | std::move(characteristics), params.getParameters(), pruneable); |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1385 | assert(characteristics.hardwareEnforced.size() == 0); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1386 | assert(characteristics.softwareEnforced.size() == 0); |
Shawn Willden | c5e8f36 | 2017-08-31 09:23:06 -0600 | [diff] [blame] | 1387 | result->token = operationToken; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1388 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1389 | mOperationMap.setOperationAuthToken(operationToken, std::move(authToken)); |
Shawn Willden | 2eef135 | 2018-05-22 16:13:24 -0600 | [diff] [blame] | 1390 | mOperationMap.setOperationVerificationToken(operationToken, std::move(verificationToken)); |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1391 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1392 | // Return the authentication lookup result. If this is a per operation |
| 1393 | // auth'd key then the resultCode will be ::OP_AUTH_NEEDED and the |
| 1394 | // application should get an auth token using the handle before the |
| 1395 | // first call to update, which will fail if keystore hasn't received the |
| 1396 | // auth token. |
Shawn Willden | 2f96c79 | 2017-09-07 23:59:08 -0600 | [diff] [blame] | 1397 | if (result->resultCode == ErrorCode::OK) { |
| 1398 | result->resultCode = authResult; |
| 1399 | } |
Shawn Willden | c5e8f36 | 2017-08-31 09:23:06 -0600 | [diff] [blame] | 1400 | |
| 1401 | // Other result fields were set in the begin operation's callback. |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1402 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1405 | void KeyStoreService::appendConfirmationTokenIfNeeded(const KeyCharacteristics& keyCharacteristics, |
| 1406 | std::vector<KeyParameter>* params) { |
| 1407 | if (!(containsTag(keyCharacteristics.softwareEnforced, Tag::TRUSTED_CONFIRMATION_REQUIRED) || |
| 1408 | containsTag(keyCharacteristics.hardwareEnforced, Tag::TRUSTED_CONFIRMATION_REQUIRED))) { |
| 1409 | return; |
| 1410 | } |
| 1411 | |
| 1412 | hidl_vec<uint8_t> confirmationToken = mConfirmationManager->getLatestConfirmationToken(); |
| 1413 | if (confirmationToken.size() == 0) { |
| 1414 | return; |
| 1415 | } |
| 1416 | |
| 1417 | params->push_back( |
| 1418 | Authorization(keymaster::TAG_CONFIRMATION_TOKEN, std::move(confirmationToken))); |
| 1419 | ALOGD("Appending confirmation token\n"); |
| 1420 | } |
| 1421 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1422 | Status KeyStoreService::update(const sp<IBinder>& token, const KeymasterArguments& params, |
| 1423 | const ::std::vector<uint8_t>& data, OperationResult* result) { |
| 1424 | if (!checkAllowedOperationParams(params.getParameters())) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1425 | result->resultCode = ErrorCode::INVALID_ARGUMENT; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1426 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1427 | } |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1428 | |
| 1429 | auto getOpResult = mOperationMap.getOperation(token); |
| 1430 | if (!getOpResult.isOk()) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1431 | result->resultCode = ErrorCode::INVALID_OPERATION_HANDLE; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1432 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1433 | } |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1434 | const auto& op = getOpResult.value(); |
| 1435 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1436 | HardwareAuthToken authToken; |
| 1437 | std::tie(result->resultCode, authToken) = getOperationAuthTokenIfNeeded(token); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1438 | if (!result->resultCode.isOk()) return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1439 | |
| 1440 | // Check that all key authorization policy requirements are met. |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1441 | AuthorizationSet key_auths(op.characteristics.hardwareEnforced); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1442 | key_auths.append(op.characteristics.softwareEnforced.begin(), |
| 1443 | op.characteristics.softwareEnforced.end()); |
| 1444 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1445 | result->resultCode = enforcement_policy.AuthorizeOperation( |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1446 | op.purpose, op.keyid, key_auths, params.getParameters(), authToken, op.handle, |
| 1447 | false /* is_begin_operation */); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1448 | if (!result->resultCode.isOk()) return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1449 | |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1450 | std::vector<KeyParameter> inParams = params.getParameters(); |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1451 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1452 | auto hidlCb = [&](ErrorCode ret, uint32_t inputConsumed, |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1453 | const hidl_vec<KeyParameter>& outParams, |
| 1454 | const ::std::vector<uint8_t>& output) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1455 | result->resultCode = ret; |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1456 | if (result->resultCode.isOk()) { |
| 1457 | result->inputConsumed = inputConsumed; |
| 1458 | result->outParams = outParams; |
| 1459 | result->data = output; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1460 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1461 | }; |
| 1462 | |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1463 | KeyStoreServiceReturnCode rc = KS_HANDLE_HIDL_ERROR( |
Shawn Willden | 2eef135 | 2018-05-22 16:13:24 -0600 | [diff] [blame] | 1464 | op.device->update(op.handle, inParams, data, authToken, op.verificationToken, hidlCb)); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1465 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1466 | // just a reminder: on success result->resultCode was set in the callback. So we only overwrite |
| 1467 | // it if there was a communication error indicated by the ErrorCode. |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1468 | if (!rc.isOk()) result->resultCode = rc; |
| 1469 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1470 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1473 | Status KeyStoreService::finish(const sp<IBinder>& token, const KeymasterArguments& params, |
| 1474 | const ::std::vector<uint8_t>& signature, |
| 1475 | const ::std::vector<uint8_t>& entropy, OperationResult* result) { |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1476 | auto getOpResult = mOperationMap.getOperation(token); |
| 1477 | if (!getOpResult.isOk()) { |
| 1478 | result->resultCode = ErrorCode::INVALID_OPERATION_HANDLE; |
| 1479 | return Status::ok(); |
| 1480 | } |
| 1481 | const auto& op = std::move(getOpResult.value()); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1482 | if (!checkAllowedOperationParams(params.getParameters())) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1483 | result->resultCode = ErrorCode::INVALID_ARGUMENT; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1484 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1485 | } |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1486 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1487 | HardwareAuthToken authToken; |
| 1488 | std::tie(result->resultCode, authToken) = getOperationAuthTokenIfNeeded(token); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1489 | if (!result->resultCode.isOk()) return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1490 | |
| 1491 | if (entropy.size()) { |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1492 | result->resultCode = KS_HANDLE_HIDL_ERROR(op.device->addRngEntropy(entropy)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1493 | if (!result->resultCode.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1494 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1495 | } |
| 1496 | } |
| 1497 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1498 | // Check that all key authorization policy requirements are met. |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1499 | AuthorizationSet key_auths(op.characteristics.hardwareEnforced); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1500 | key_auths.append(op.characteristics.softwareEnforced.begin(), |
| 1501 | op.characteristics.softwareEnforced.end()); |
| 1502 | |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1503 | std::vector<KeyParameter> inParams = params.getParameters(); |
| 1504 | appendConfirmationTokenIfNeeded(op.characteristics, &inParams); |
| 1505 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1506 | result->resultCode = enforcement_policy.AuthorizeOperation( |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1507 | op.purpose, op.keyid, key_auths, params.getParameters(), authToken, op.handle, |
| 1508 | false /* is_begin_operation */); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1509 | if (!result->resultCode.isOk()) return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1510 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1511 | auto hidlCb = [&](ErrorCode ret, const hidl_vec<KeyParameter>& outParams, |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1512 | const ::std::vector<uint8_t>& output) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1513 | result->resultCode = ret; |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1514 | if (result->resultCode.isOk()) { |
| 1515 | result->outParams = outParams; |
| 1516 | result->data = output; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1517 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1518 | }; |
| 1519 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1520 | KeyStoreServiceReturnCode rc = KS_HANDLE_HIDL_ERROR( |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1521 | op.device->finish(op.handle, inParams, |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1522 | ::std::vector<uint8_t>() /* TODO(swillden): wire up input to finish() */, |
Shawn Willden | 2eef135 | 2018-05-22 16:13:24 -0600 | [diff] [blame] | 1523 | signature, authToken, op.verificationToken, hidlCb)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1524 | |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 1525 | bool wasOpSuccessful = true; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1526 | // just a reminder: on success result->resultCode was set in the callback. So we only overwrite |
| 1527 | // it if there was a communication error indicated by the ErrorCode. |
| 1528 | if (!rc.isOk()) { |
| 1529 | result->resultCode = rc; |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 1530 | wasOpSuccessful = false; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1531 | } |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 1532 | |
| 1533 | // removeOperation() will free the memory 'op' used, so the order is important |
| 1534 | mAuthTokenTable.MarkCompleted(op.handle); |
| 1535 | mOperationMap.removeOperation(token, wasOpSuccessful); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1536 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1537 | } |
| 1538 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1539 | Status KeyStoreService::abort(const sp<IBinder>& token, int32_t* aidl_return) { |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 1540 | auto getOpResult = mOperationMap.removeOperation(token, false /* wasOpSuccessful */); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1541 | if (!getOpResult.isOk()) { |
| 1542 | *aidl_return = static_cast<int32_t>(ErrorCode::INVALID_OPERATION_HANDLE); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1543 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1544 | } |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1545 | auto op = std::move(getOpResult.value()); |
| 1546 | mAuthTokenTable.MarkCompleted(op.handle); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1547 | |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1548 | ErrorCode error_code = KS_HANDLE_HIDL_ERROR(op.device->abort(op.handle)); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1549 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(error_code)); |
| 1550 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1551 | } |
| 1552 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1553 | Status KeyStoreService::isOperationAuthorized(const sp<IBinder>& token, bool* aidl_return) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1554 | AuthorizationSet ignored; |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1555 | KeyStoreServiceReturnCode rc; |
| 1556 | std::tie(rc, std::ignore) = getOperationAuthTokenIfNeeded(token); |
| 1557 | *aidl_return = rc.isOk(); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1558 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1561 | Status KeyStoreService::addAuthToken(const ::std::vector<uint8_t>& authTokenAsVector, |
Brian Young | ccb492d | 2018-02-22 23:36:01 +0000 | [diff] [blame] | 1562 | int32_t* aidl_return) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1563 | |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1564 | // TODO(swillden): When gatekeeper and fingerprint are ready, this should be updated to |
| 1565 | // receive a HardwareAuthToken, rather than an opaque byte array. |
| 1566 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1567 | if (!checkBinderPermission(P_ADD_AUTH)) { |
| 1568 | ALOGW("addAuthToken: permission denied for %d", IPCThreadState::self()->getCallingUid()); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1569 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 1570 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1571 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1572 | if (authTokenAsVector.size() != sizeof(hw_auth_token_t)) { |
| 1573 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT)); |
| 1574 | return Status::ok(); |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1575 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1576 | |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1577 | hw_auth_token_t authToken; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1578 | memcpy(reinterpret_cast<void*>(&authToken), authTokenAsVector.data(), sizeof(hw_auth_token_t)); |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1579 | if (authToken.version != 0) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1580 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT)); |
| 1581 | return Status::ok(); |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1582 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1583 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1584 | mAuthTokenTable.AddAuthenticationToken(hidlVec2AuthToken(hidl_vec<uint8_t>(authTokenAsVector))); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1585 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 1586 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1587 | } |
| 1588 | |
Rubin Xu | 1a203e3 | 2018-05-08 14:25:21 +0100 | [diff] [blame] | 1589 | int isDeviceIdAttestationRequested(const KeymasterArguments& params) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1590 | const hardware::hidl_vec<KeyParameter> paramsVec = params.getParameters(); |
Rubin Xu | 1a203e3 | 2018-05-08 14:25:21 +0100 | [diff] [blame] | 1591 | int result = 0; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1592 | for (size_t i = 0; i < paramsVec.size(); ++i) { |
| 1593 | switch (paramsVec[i].tag) { |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1594 | case Tag::ATTESTATION_ID_BRAND: |
| 1595 | case Tag::ATTESTATION_ID_DEVICE: |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1596 | case Tag::ATTESTATION_ID_MANUFACTURER: |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1597 | case Tag::ATTESTATION_ID_MODEL: |
| 1598 | case Tag::ATTESTATION_ID_PRODUCT: |
Rubin Xu | 1a203e3 | 2018-05-08 14:25:21 +0100 | [diff] [blame] | 1599 | result |= ID_ATTESTATION_REQUEST_GENERIC_INFO; |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1600 | break; |
Rubin Xu | 1a203e3 | 2018-05-08 14:25:21 +0100 | [diff] [blame] | 1601 | case Tag::ATTESTATION_ID_IMEI: |
| 1602 | case Tag::ATTESTATION_ID_MEID: |
| 1603 | case Tag::ATTESTATION_ID_SERIAL: |
| 1604 | result |= ID_ATTESTATION_REQUEST_UNIQUE_DEVICE_ID; |
| 1605 | break; |
| 1606 | default: |
| 1607 | continue; |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1608 | } |
| 1609 | } |
Rubin Xu | 1a203e3 | 2018-05-08 14:25:21 +0100 | [diff] [blame] | 1610 | return result; |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1611 | } |
| 1612 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1613 | Status KeyStoreService::attestKey(const String16& name, const KeymasterArguments& params, |
| 1614 | ::android::security::keymaster::KeymasterCertificateChain* chain, |
| 1615 | int32_t* aidl_return) { |
| 1616 | // check null output if method signature is updated and return ErrorCode::OUTPUT_PARAMETER_NULL |
| 1617 | if (!checkAllowedOperationParams(params.getParameters())) { |
| 1618 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT)); |
| 1619 | return Status::ok(); |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1620 | } |
| 1621 | |
Eran Messeri | e2c3415 | 2017-12-21 21:01:22 +0000 | [diff] [blame] | 1622 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1623 | |
Rubin Xu | 1a203e3 | 2018-05-08 14:25:21 +0100 | [diff] [blame] | 1624 | int needsIdAttestation = isDeviceIdAttestationRequested(params); |
| 1625 | bool needsUniqueIdAttestation = needsIdAttestation & ID_ATTESTATION_REQUEST_UNIQUE_DEVICE_ID; |
| 1626 | bool isPrimaryUserSystemUid = (callingUid == AID_SYSTEM); |
| 1627 | bool isSomeUserSystemUid = (get_app_id(callingUid) == AID_SYSTEM); |
| 1628 | // Allow system context from any user to request attestation with basic device information, |
| 1629 | // while only allow system context from user 0 (device owner) to request attestation with |
| 1630 | // unique device ID. |
| 1631 | if ((needsIdAttestation && !isSomeUserSystemUid) || |
| 1632 | (needsUniqueIdAttestation && !isPrimaryUserSystemUid)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1633 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT)); |
| 1634 | return Status::ok(); |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1635 | } |
| 1636 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1637 | AuthorizationSet mutableParams = params.getParameters(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1638 | KeyStoreServiceReturnCode rc = updateParamsForAttestation(callingUid, &mutableParams); |
| 1639 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1640 | *aidl_return = static_cast<int32_t>(rc); |
| 1641 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1642 | } |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1643 | |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1644 | Blob keyBlob; |
| 1645 | String8 name8(name); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1646 | rc = mKeyStore->getKeyForName(&keyBlob, name8, callingUid, TYPE_KEYMASTER_10); |
| 1647 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1648 | *aidl_return = static_cast<int32_t>(rc); |
| 1649 | return Status::ok(); |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1650 | } |
| 1651 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1652 | KeyStoreServiceReturnCode error; |
| 1653 | auto hidlCb = [&](ErrorCode ret, const hidl_vec<hidl_vec<uint8_t>>& certChain) { |
| 1654 | error = ret; |
| 1655 | if (!error.isOk()) { |
| 1656 | return; |
| 1657 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1658 | if (chain) { |
| 1659 | *chain = KeymasterCertificateChain(certChain); |
| 1660 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1661 | }; |
| 1662 | |
| 1663 | auto hidlKey = blob2hidlVec(keyBlob); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1664 | auto dev = mKeyStore->getDevice(keyBlob); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1665 | rc = KS_HANDLE_HIDL_ERROR(dev->attestKey(hidlKey, mutableParams.hidl_data(), hidlCb)); |
| 1666 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1667 | *aidl_return = static_cast<int32_t>(rc); |
| 1668 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1669 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1670 | *aidl_return = static_cast<int32_t>(error); |
| 1671 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1672 | } |
| 1673 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1674 | Status |
| 1675 | KeyStoreService::attestDeviceIds(const KeymasterArguments& params, |
| 1676 | ::android::security::keymaster::KeymasterCertificateChain* chain, |
| 1677 | int32_t* aidl_return) { |
| 1678 | // 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] | 1679 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1680 | if (!checkAllowedOperationParams(params.getParameters())) { |
| 1681 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT)); |
| 1682 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | if (!isDeviceIdAttestationRequested(params)) { |
| 1686 | // There is an attestKey() method for attesting keys without device ID attestation. |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1687 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT)); |
| 1688 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1692 | sp<IBinder> binder = defaultServiceManager()->getService(String16("permission")); |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 1693 | if (binder == nullptr) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1694 | *aidl_return = |
| 1695 | static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::CANNOT_ATTEST_IDS)); |
| 1696 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1697 | } |
| 1698 | if (!interface_cast<IPermissionController>(binder)->checkPermission( |
| 1699 | String16("android.permission.READ_PRIVILEGED_PHONE_STATE"), |
| 1700 | IPCThreadState::self()->getCallingPid(), callingUid)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1701 | *aidl_return = |
| 1702 | static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::CANNOT_ATTEST_IDS)); |
| 1703 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1704 | } |
| 1705 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1706 | AuthorizationSet mutableParams = params.getParameters(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1707 | KeyStoreServiceReturnCode rc = updateParamsForAttestation(callingUid, &mutableParams); |
| 1708 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1709 | *aidl_return = static_cast<int32_t>(rc); |
| 1710 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | // Generate temporary key. |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1714 | sp<Keymaster> dev; |
| 1715 | SecurityLevel securityLevel; |
| 1716 | std::tie(dev, securityLevel) = mKeyStore->getMostSecureDevice(); |
| 1717 | |
| 1718 | if (securityLevel == SecurityLevel::SOFTWARE) { |
| 1719 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 1720 | return Status::ok(); |
| 1721 | } |
| 1722 | |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1723 | KeyStoreServiceReturnCode error; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1724 | ::std::vector<uint8_t> hidlKey; |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1725 | |
| 1726 | AuthorizationSet keyCharacteristics; |
| 1727 | keyCharacteristics.push_back(TAG_PURPOSE, KeyPurpose::VERIFY); |
| 1728 | keyCharacteristics.push_back(TAG_ALGORITHM, Algorithm::EC); |
| 1729 | keyCharacteristics.push_back(TAG_DIGEST, Digest::SHA_2_256); |
| 1730 | keyCharacteristics.push_back(TAG_NO_AUTH_REQUIRED); |
| 1731 | keyCharacteristics.push_back(TAG_EC_CURVE, EcCurve::P_256); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1732 | auto generateHidlCb = [&](ErrorCode ret, const ::std::vector<uint8_t>& hidlKeyBlob, |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1733 | const KeyCharacteristics&) { |
| 1734 | error = ret; |
| 1735 | if (!error.isOk()) { |
| 1736 | return; |
| 1737 | } |
| 1738 | hidlKey = hidlKeyBlob; |
| 1739 | }; |
| 1740 | |
| 1741 | rc = KS_HANDLE_HIDL_ERROR(dev->generateKey(keyCharacteristics.hidl_data(), generateHidlCb)); |
| 1742 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1743 | *aidl_return = static_cast<int32_t>(rc); |
| 1744 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1745 | } |
| 1746 | if (!error.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1747 | *aidl_return = static_cast<int32_t>(error); |
| 1748 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | // Attest key and device IDs. |
| 1752 | auto attestHidlCb = [&](ErrorCode ret, const hidl_vec<hidl_vec<uint8_t>>& certChain) { |
| 1753 | error = ret; |
| 1754 | if (!error.isOk()) { |
| 1755 | return; |
| 1756 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1757 | *chain = ::android::security::keymaster::KeymasterCertificateChain(certChain); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1758 | }; |
| 1759 | KeyStoreServiceReturnCode attestationRc = |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1760 | KS_HANDLE_HIDL_ERROR(dev->attestKey(hidlKey, mutableParams.hidl_data(), attestHidlCb)); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1761 | |
| 1762 | // Delete temporary key. |
| 1763 | KeyStoreServiceReturnCode deletionRc = KS_HANDLE_HIDL_ERROR(dev->deleteKey(hidlKey)); |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1764 | |
| 1765 | if (!attestationRc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1766 | *aidl_return = static_cast<int32_t>(attestationRc); |
| 1767 | return Status::ok(); |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1768 | } |
| 1769 | if (!error.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1770 | *aidl_return = static_cast<int32_t>(error); |
| 1771 | return Status::ok(); |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1772 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1773 | *aidl_return = static_cast<int32_t>(deletionRc); |
| 1774 | return Status::ok(); |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1775 | } |
| 1776 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1777 | Status KeyStoreService::onDeviceOffBody(int32_t* aidl_return) { |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1778 | // TODO(tuckeris): add permission check. This should be callable from ClockworkHome only. |
| 1779 | mAuthTokenTable.onDeviceOffBody(); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1780 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 1781 | return Status::ok(); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1782 | } |
| 1783 | |
Janis Danisevskis | cb9267d | 2017-12-19 16:27:52 -0800 | [diff] [blame] | 1784 | #define AIDL_RETURN(rc) \ |
| 1785 | (*_aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(rc)), Status::ok()) |
| 1786 | |
| 1787 | Status KeyStoreService::importWrappedKey( |
| 1788 | const ::android::String16& wrappedKeyAlias, const ::std::vector<uint8_t>& wrappedKey, |
| 1789 | const ::android::String16& wrappingKeyAlias, const ::std::vector<uint8_t>& maskingKey, |
| 1790 | const KeymasterArguments& params, int64_t rootSid, int64_t fingerprintSid, |
| 1791 | ::android::security::keymaster::KeyCharacteristics* outCharacteristics, int32_t* _aidl_return) { |
| 1792 | |
| 1793 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1794 | |
| 1795 | if (!checkBinderPermission(P_INSERT, callingUid)) { |
| 1796 | return AIDL_RETURN(ResponseCode::PERMISSION_DENIED); |
| 1797 | } |
| 1798 | |
| 1799 | Blob wrappingKeyBlob; |
| 1800 | String8 wrappingKeyName8(wrappingKeyAlias); |
| 1801 | KeyStoreServiceReturnCode rc = |
| 1802 | mKeyStore->getKeyForName(&wrappingKeyBlob, wrappingKeyName8, callingUid, TYPE_KEYMASTER_10); |
| 1803 | if (!rc.isOk()) { |
| 1804 | return AIDL_RETURN(rc); |
| 1805 | } |
| 1806 | |
| 1807 | SecurityLevel securityLevel = wrappingKeyBlob.getSecurityLevel(); |
| 1808 | auto dev = mKeyStore->getDevice(securityLevel); |
| 1809 | if (!dev) { |
| 1810 | return AIDL_RETURN(ErrorCode::HARDWARE_TYPE_UNAVAILABLE); |
| 1811 | } |
| 1812 | |
| 1813 | auto hidlWrappingKey = blob2hidlVec(wrappingKeyBlob); |
| 1814 | String8 wrappedKeyAlias8(wrappedKeyAlias); |
| 1815 | |
| 1816 | KeyStoreServiceReturnCode error; |
| 1817 | |
| 1818 | auto hidlCb = [&](ErrorCode ret, const ::std::vector<uint8_t>& keyBlob, |
| 1819 | const KeyCharacteristics& keyCharacteristics) { |
| 1820 | error = ret; |
| 1821 | if (!error.isOk()) { |
| 1822 | return; |
| 1823 | } |
| 1824 | if (outCharacteristics) { |
| 1825 | *outCharacteristics = |
| 1826 | ::android::security::keymaster::KeyCharacteristics(keyCharacteristics); |
| 1827 | } |
| 1828 | |
| 1829 | // Write the key: |
| 1830 | String8 filename( |
| 1831 | mKeyStore->getKeyNameForUidWithDir(wrappedKeyAlias8, callingUid, ::TYPE_KEYMASTER_10)); |
| 1832 | |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 1833 | Blob ksBlob(&keyBlob[0], keyBlob.size(), nullptr, 0, ::TYPE_KEYMASTER_10); |
Janis Danisevskis | cb9267d | 2017-12-19 16:27:52 -0800 | [diff] [blame] | 1834 | ksBlob.setSecurityLevel(securityLevel); |
| 1835 | |
| 1836 | if (containsTag(keyCharacteristics.hardwareEnforced, Tag::USER_SECURE_ID)) { |
| 1837 | ksBlob.setSuperEncrypted(true); |
| 1838 | } |
| 1839 | |
| 1840 | error = mKeyStore->put(filename.string(), &ksBlob, get_user_id(callingUid)); |
| 1841 | }; |
| 1842 | |
Shawn Willden | 0a198a0 | 2018-01-19 13:36:31 -0700 | [diff] [blame] | 1843 | rc = KS_HANDLE_HIDL_ERROR(dev->importWrappedKey(wrappedKey, hidlWrappingKey, maskingKey, |
| 1844 | params.getParameters(), rootSid, fingerprintSid, |
| 1845 | hidlCb)); |
| 1846 | |
Janis Danisevskis | cb9267d | 2017-12-19 16:27:52 -0800 | [diff] [blame] | 1847 | // possible hidl error |
| 1848 | if (!rc.isOk()) { |
| 1849 | return AIDL_RETURN(rc); |
| 1850 | } |
| 1851 | // now check error from callback |
| 1852 | if (!error.isOk()) { |
| 1853 | return AIDL_RETURN(error); |
| 1854 | } |
| 1855 | |
| 1856 | // Write the characteristics: |
| 1857 | String8 cFilename(mKeyStore->getKeyNameForUidWithDir(wrappedKeyAlias8, callingUid, |
| 1858 | ::TYPE_KEY_CHARACTERISTICS)); |
| 1859 | |
| 1860 | AuthorizationSet opParams = params.getParameters(); |
| 1861 | std::stringstream kcStream; |
| 1862 | opParams.Serialize(&kcStream); |
| 1863 | if (kcStream.bad()) { |
| 1864 | return AIDL_RETURN(ResponseCode::SYSTEM_ERROR); |
| 1865 | } |
| 1866 | auto kcBuf = kcStream.str(); |
| 1867 | |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 1868 | 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] | 1869 | ::TYPE_KEY_CHARACTERISTICS); |
| 1870 | charBlob.setSecurityLevel(securityLevel); |
| 1871 | |
| 1872 | return AIDL_RETURN(mKeyStore->put(cFilename.string(), &charBlob, get_user_id(callingUid))); |
| 1873 | } |
| 1874 | |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1875 | Status KeyStoreService::presentConfirmationPrompt(const sp<IBinder>& listener, |
| 1876 | const String16& promptText, |
| 1877 | const ::std::vector<uint8_t>& extraData, |
| 1878 | const String16& locale, int32_t uiOptionsAsFlags, |
| 1879 | int32_t* aidl_return) { |
| 1880 | return mConfirmationManager->presentConfirmationPrompt(listener, promptText, extraData, locale, |
| 1881 | uiOptionsAsFlags, aidl_return); |
| 1882 | } |
| 1883 | |
| 1884 | Status KeyStoreService::cancelConfirmationPrompt(const sp<IBinder>& listener, |
| 1885 | int32_t* aidl_return) { |
| 1886 | return mConfirmationManager->cancelConfirmationPrompt(listener, aidl_return); |
| 1887 | } |
| 1888 | |
David Zeuthen | 1a49231 | 2018-02-26 11:00:30 -0500 | [diff] [blame] | 1889 | Status KeyStoreService::isConfirmationPromptSupported(bool* aidl_return) { |
| 1890 | return mConfirmationManager->isConfirmationPromptSupported(aidl_return); |
| 1891 | } |
| 1892 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1893 | /** |
| 1894 | * Prune the oldest pruneable operation. |
| 1895 | */ |
| 1896 | bool KeyStoreService::pruneOperation() { |
| 1897 | sp<IBinder> oldest = mOperationMap.getOldestPruneableOperation(); |
| 1898 | ALOGD("Trying to prune operation %p", oldest.get()); |
| 1899 | size_t op_count_before_abort = mOperationMap.getOperationCount(); |
| 1900 | // We mostly ignore errors from abort() because all we care about is whether at least |
| 1901 | // one operation has been removed. |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1902 | int32_t abort_error; |
| 1903 | abort(oldest, &abort_error); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1904 | if (mOperationMap.getOperationCount() >= op_count_before_abort) { |
| 1905 | ALOGE("Failed to abort pruneable operation %p, error: %d", oldest.get(), abort_error); |
| 1906 | return false; |
| 1907 | } |
| 1908 | return true; |
| 1909 | } |
| 1910 | |
| 1911 | /** |
| 1912 | * Get the effective target uid for a binder operation that takes an |
| 1913 | * optional uid as the target. |
| 1914 | */ |
| 1915 | uid_t KeyStoreService::getEffectiveUid(int32_t targetUid) { |
| 1916 | if (targetUid == UID_SELF) { |
| 1917 | return IPCThreadState::self()->getCallingUid(); |
| 1918 | } |
| 1919 | return static_cast<uid_t>(targetUid); |
| 1920 | } |
| 1921 | |
| 1922 | /** |
| 1923 | * Check if the caller of the current binder method has the required |
| 1924 | * permission and if acting on other uids the grants to do so. |
| 1925 | */ |
| 1926 | bool KeyStoreService::checkBinderPermission(perm_t permission, int32_t targetUid) { |
| 1927 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1928 | pid_t spid = IPCThreadState::self()->getCallingPid(); |
| 1929 | if (!has_permission(callingUid, permission, spid)) { |
| 1930 | ALOGW("permission %s denied for %d", get_perm_label(permission), callingUid); |
| 1931 | return false; |
| 1932 | } |
| 1933 | if (!is_granted_to(callingUid, getEffectiveUid(targetUid))) { |
| 1934 | ALOGW("uid %d not granted to act for %d", callingUid, targetUid); |
| 1935 | return false; |
| 1936 | } |
| 1937 | return true; |
| 1938 | } |
| 1939 | |
| 1940 | /** |
| 1941 | * Check if the caller of the current binder method has the required |
| 1942 | * permission and the target uid is the caller or the caller is system. |
| 1943 | */ |
| 1944 | bool KeyStoreService::checkBinderPermissionSelfOrSystem(perm_t permission, int32_t targetUid) { |
| 1945 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1946 | pid_t spid = IPCThreadState::self()->getCallingPid(); |
| 1947 | if (!has_permission(callingUid, permission, spid)) { |
| 1948 | ALOGW("permission %s denied for %d", get_perm_label(permission), callingUid); |
| 1949 | return false; |
| 1950 | } |
| 1951 | return getEffectiveUid(targetUid) == callingUid || callingUid == AID_SYSTEM; |
| 1952 | } |
| 1953 | |
| 1954 | /** |
| 1955 | * Check if the caller of the current binder method has the required |
| 1956 | * permission or the target of the operation is the caller's uid. This is |
| 1957 | * for operation where the permission is only for cross-uid activity and all |
| 1958 | * uids are allowed to act on their own (ie: clearing all entries for a |
| 1959 | * given uid). |
| 1960 | */ |
| 1961 | bool KeyStoreService::checkBinderPermissionOrSelfTarget(perm_t permission, int32_t targetUid) { |
| 1962 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1963 | if (getEffectiveUid(targetUid) == callingUid) { |
| 1964 | return true; |
| 1965 | } else { |
| 1966 | return checkBinderPermission(permission, targetUid); |
| 1967 | } |
| 1968 | } |
| 1969 | |
| 1970 | /** |
| 1971 | * Helper method to check that the caller has the required permission as |
| 1972 | * well as the keystore is in the unlocked state if checkUnlocked is true. |
| 1973 | * |
| 1974 | * Returns NO_ERROR on success, PERMISSION_DENIED on a permission error and |
| 1975 | * otherwise the state of keystore when not unlocked and checkUnlocked is |
| 1976 | * true. |
| 1977 | */ |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1978 | KeyStoreServiceReturnCode |
| 1979 | KeyStoreService::checkBinderPermissionAndKeystoreState(perm_t permission, int32_t targetUid, |
| 1980 | bool checkUnlocked) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1981 | if (!checkBinderPermission(permission, targetUid)) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1982 | return ResponseCode::PERMISSION_DENIED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1983 | } |
| 1984 | State state = mKeyStore->getState(get_user_id(getEffectiveUid(targetUid))); |
| 1985 | if (checkUnlocked && !isKeystoreUnlocked(state)) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1986 | // All State values coincide with ResponseCodes |
| 1987 | return static_cast<ResponseCode>(state); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1988 | } |
| 1989 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1990 | return ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | bool KeyStoreService::isKeystoreUnlocked(State state) { |
| 1994 | switch (state) { |
| 1995 | case ::STATE_NO_ERROR: |
| 1996 | return true; |
| 1997 | case ::STATE_UNINITIALIZED: |
| 1998 | case ::STATE_LOCKED: |
| 1999 | return false; |
| 2000 | } |
| 2001 | return false; |
| 2002 | } |
| 2003 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2004 | /** |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2005 | * Check that all KeyParameters provided by the application are allowed. Any parameter that keystore |
| 2006 | * adds itself should be disallowed here. |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2007 | */ |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2008 | bool KeyStoreService::checkAllowedOperationParams(const hidl_vec<KeyParameter>& params) { |
| 2009 | for (size_t i = 0; i < params.size(); ++i) { |
| 2010 | switch (params[i].tag) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2011 | case Tag::ATTESTATION_APPLICATION_ID: |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 2012 | case Tag::RESET_SINCE_ID_ROTATION: |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2013 | return false; |
| 2014 | default: |
| 2015 | break; |
| 2016 | } |
| 2017 | } |
| 2018 | return true; |
| 2019 | } |
| 2020 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2021 | ErrorCode KeyStoreService::getOperationCharacteristics(const hidl_vec<uint8_t>& key, |
Shawn Willden | c67a8aa | 2017-12-03 17:51:29 -0700 | [diff] [blame] | 2022 | sp<Keymaster>* dev, |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2023 | const AuthorizationSet& params, |
| 2024 | KeyCharacteristics* out) { |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 2025 | ::std::vector<uint8_t> clientId; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2026 | ::std::vector<uint8_t> appData; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2027 | for (auto param : params) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2028 | if (param.tag == Tag::APPLICATION_ID) { |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 2029 | clientId = authorizationValue(TAG_APPLICATION_ID, param).value(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2030 | } else if (param.tag == Tag::APPLICATION_DATA) { |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 2031 | appData = authorizationValue(TAG_APPLICATION_DATA, param).value(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2032 | } |
| 2033 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2034 | ErrorCode error = ErrorCode::OK; |
| 2035 | |
| 2036 | auto hidlCb = [&](ErrorCode ret, const KeyCharacteristics& keyCharacteristics) { |
| 2037 | error = ret; |
| 2038 | if (error != ErrorCode::OK) { |
| 2039 | return; |
| 2040 | } |
| 2041 | if (out) *out = keyCharacteristics; |
| 2042 | }; |
| 2043 | |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 2044 | ErrorCode rc = |
| 2045 | KS_HANDLE_HIDL_ERROR((*dev)->getKeyCharacteristics(key, clientId, appData, hidlCb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2046 | if (rc != ErrorCode::OK) { |
| 2047 | return rc; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2048 | } |
| 2049 | return error; |
| 2050 | } |
| 2051 | |
| 2052 | /** |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 2053 | * Get the auth token for this operation from the auth token table. |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2054 | * |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 2055 | * Returns ResponseCode::NO_ERROR if the auth token was set or none was required. |
| 2056 | * ::OP_AUTH_NEEDED if it is a per op authorization, no |
| 2057 | * authorization token exists for that operation and |
| 2058 | * failOnTokenMissing is false. |
| 2059 | * KM_ERROR_KEY_USER_NOT_AUTHENTICATED if there is no valid auth |
| 2060 | * token for the operation |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2061 | */ |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2062 | std::pair<KeyStoreServiceReturnCode, HardwareAuthToken> |
| 2063 | KeyStoreService::getAuthToken(const KeyCharacteristics& characteristics, uint64_t handle, |
| 2064 | KeyPurpose purpose, bool failOnTokenMissing) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2065 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2066 | AuthorizationSet allCharacteristics(characteristics.softwareEnforced); |
| 2067 | allCharacteristics.append(characteristics.hardwareEnforced.begin(), |
| 2068 | characteristics.hardwareEnforced.end()); |
| 2069 | |
| 2070 | const HardwareAuthToken* authToken = nullptr; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2071 | AuthTokenTable::Error err = mAuthTokenTable.FindAuthorization( |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2072 | allCharacteristics, static_cast<KeyPurpose>(purpose), handle, &authToken); |
| 2073 | |
| 2074 | KeyStoreServiceReturnCode rc; |
| 2075 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2076 | switch (err) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2077 | case AuthTokenTable::OK: |
| 2078 | case AuthTokenTable::AUTH_NOT_REQUIRED: |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2079 | rc = ResponseCode::NO_ERROR; |
| 2080 | break; |
| 2081 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2082 | case AuthTokenTable::AUTH_TOKEN_NOT_FOUND: |
| 2083 | case AuthTokenTable::AUTH_TOKEN_EXPIRED: |
| 2084 | case AuthTokenTable::AUTH_TOKEN_WRONG_SID: |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2085 | ALOGE("getAuthToken failed: %d", err); // STOPSHIP: debug only, to be removed |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2086 | rc = ErrorCode::KEY_USER_NOT_AUTHENTICATED; |
| 2087 | break; |
| 2088 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2089 | case AuthTokenTable::OP_HANDLE_REQUIRED: |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2090 | rc = failOnTokenMissing ? KeyStoreServiceReturnCode(ErrorCode::KEY_USER_NOT_AUTHENTICATED) |
| 2091 | : KeyStoreServiceReturnCode(ResponseCode::OP_AUTH_NEEDED); |
| 2092 | break; |
| 2093 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2094 | default: |
| 2095 | ALOGE("Unexpected FindAuthorization return value %d", err); |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2096 | rc = ErrorCode::INVALID_ARGUMENT; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2097 | } |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2098 | |
| 2099 | return {rc, authToken ? std::move(*authToken) : HardwareAuthToken()}; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | /** |
| 2103 | * Add the auth token for the operation to the param list if the operation |
| 2104 | * requires authorization. Uses the cached result in the OperationMap if available |
| 2105 | * otherwise gets the token from the AuthTokenTable and caches the result. |
| 2106 | * |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2107 | * 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] | 2108 | * KM_ERROR_KEY_USER_NOT_AUTHENTICATED if the operation is not |
| 2109 | * authenticated. |
| 2110 | * KM_ERROR_INVALID_OPERATION_HANDLE if token is not a valid |
| 2111 | * operation token. |
| 2112 | */ |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2113 | std::pair<KeyStoreServiceReturnCode, const HardwareAuthToken&> |
| 2114 | KeyStoreService::getOperationAuthTokenIfNeeded(const sp<IBinder>& token) { |
| 2115 | static HardwareAuthToken emptyToken = {}; |
| 2116 | |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 2117 | auto getOpResult = mOperationMap.getOperation(token); |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2118 | if (!getOpResult.isOk()) return {ErrorCode::INVALID_OPERATION_HANDLE, emptyToken}; |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 2119 | const auto& op = getOpResult.value(); |
| 2120 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2121 | if (!op.hasAuthToken()) { |
| 2122 | KeyStoreServiceReturnCode rc; |
| 2123 | HardwareAuthToken found; |
| 2124 | std::tie(rc, found) = getAuthToken(op.characteristics, op.handle, op.purpose); |
| 2125 | if (!rc.isOk()) return {rc, emptyToken}; |
| 2126 | mOperationMap.setOperationAuthToken(token, std::move(found)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2127 | } |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2128 | |
| 2129 | return {ResponseCode::NO_ERROR, op.authToken}; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | /** |
| 2133 | * Translate a result value to a legacy return value. All keystore errors are |
| 2134 | * preserved and keymaster errors become SYSTEM_ERRORs |
| 2135 | */ |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2136 | KeyStoreServiceReturnCode KeyStoreService::translateResultToLegacyResult(int32_t result) { |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2137 | if (result > 0) return static_cast<ResponseCode>(result); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2138 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2139 | } |
| 2140 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2141 | static NullOr<const Algorithm&> getKeyAlgoritmFromKeyCharacteristics( |
| 2142 | const ::android::security::keymaster::KeyCharacteristics& characteristics) { |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2143 | for (const auto& param : characteristics.hardwareEnforced.getParameters()) { |
| 2144 | auto algo = authorizationValue(TAG_ALGORITHM, param); |
| 2145 | if (algo.isOk()) return algo; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2146 | } |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2147 | for (const auto& param : characteristics.softwareEnforced.getParameters()) { |
| 2148 | auto algo = authorizationValue(TAG_ALGORITHM, param); |
| 2149 | if (algo.isOk()) return algo; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2150 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2151 | return {}; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2152 | } |
| 2153 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2154 | void KeyStoreService::addLegacyBeginParams(const String16& name, AuthorizationSet* params) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2155 | // All legacy keys are DIGEST_NONE/PAD_NONE. |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2156 | params->push_back(TAG_DIGEST, Digest::NONE); |
| 2157 | params->push_back(TAG_PADDING, PaddingMode::NONE); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2158 | |
| 2159 | // Look up the algorithm of the key. |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2160 | ::android::security::keymaster::KeyCharacteristics characteristics; |
| 2161 | int32_t result; |
| 2162 | auto rc = getKeyCharacteristics(name, ::android::security::keymaster::KeymasterBlob(), |
| 2163 | ::android::security::keymaster::KeymasterBlob(), UID_SELF, |
| 2164 | &characteristics, &result); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2165 | if (!rc.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2166 | ALOGE("Failed to get key characteristics"); |
| 2167 | return; |
| 2168 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2169 | auto algorithm = getKeyAlgoritmFromKeyCharacteristics(characteristics); |
| 2170 | if (!algorithm.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2171 | ALOGE("getKeyCharacteristics did not include KM_TAG_ALGORITHM"); |
| 2172 | return; |
| 2173 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2174 | params->push_back(TAG_ALGORITHM, algorithm.value()); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2175 | } |
| 2176 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2177 | KeyStoreServiceReturnCode KeyStoreService::doLegacySignVerify(const String16& name, |
| 2178 | const hidl_vec<uint8_t>& data, |
| 2179 | hidl_vec<uint8_t>* out, |
| 2180 | const hidl_vec<uint8_t>& signature, |
| 2181 | KeyPurpose purpose) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2182 | |
| 2183 | std::basic_stringstream<uint8_t> outBuffer; |
| 2184 | OperationResult result; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2185 | AuthorizationSet inArgs; |
| 2186 | addLegacyBeginParams(name, &inArgs); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2187 | sp<IBinder> appToken(new BBinder); |
| 2188 | sp<IBinder> token; |
| 2189 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2190 | begin(appToken, name, static_cast<int32_t>(purpose), true, |
| 2191 | KeymasterArguments(inArgs.hidl_data()), ::std::vector<uint8_t>(), UID_SELF, &result); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2192 | if (!result.resultCode.isOk()) { |
| 2193 | if (result.resultCode == ResponseCode::KEY_NOT_FOUND) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2194 | ALOGW("Key not found"); |
| 2195 | } else { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2196 | ALOGW("Error in begin: %d", int32_t(result.resultCode)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2197 | } |
| 2198 | return translateResultToLegacyResult(result.resultCode); |
| 2199 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2200 | inArgs.Clear(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2201 | token = result.token; |
| 2202 | size_t consumed = 0; |
| 2203 | size_t lastConsumed = 0; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2204 | hidl_vec<uint8_t> data_view; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2205 | do { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2206 | data_view.setToExternal(const_cast<uint8_t*>(&data[consumed]), data.size() - consumed); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2207 | update(token, KeymasterArguments(inArgs.hidl_data()), data_view, &result); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2208 | if (result.resultCode != ResponseCode::NO_ERROR) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2209 | ALOGW("Error in update: %d", int32_t(result.resultCode)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2210 | return translateResultToLegacyResult(result.resultCode); |
| 2211 | } |
| 2212 | if (out) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2213 | outBuffer.write(&result.data[0], result.data.size()); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2214 | } |
| 2215 | lastConsumed = result.inputConsumed; |
| 2216 | consumed += lastConsumed; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2217 | } while (consumed < data.size() && lastConsumed > 0); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2218 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2219 | if (consumed != data.size()) { |
| 2220 | ALOGW("Not all data consumed. Consumed %zu of %zu", consumed, data.size()); |
| 2221 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2222 | } |
| 2223 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2224 | finish(token, KeymasterArguments(inArgs.hidl_data()), signature, ::std::vector<uint8_t>(), |
| 2225 | &result); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2226 | if (result.resultCode != ResponseCode::NO_ERROR) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2227 | ALOGW("Error in finish: %d", int32_t(result.resultCode)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2228 | return translateResultToLegacyResult(result.resultCode); |
| 2229 | } |
| 2230 | if (out) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2231 | outBuffer.write(&result.data[0], result.data.size()); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | if (out) { |
| 2235 | auto buf = outBuffer.str(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2236 | out->resize(buf.size()); |
| 2237 | memcpy(&(*out)[0], buf.data(), out->size()); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2238 | } |
| 2239 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2240 | return ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2241 | } |
| 2242 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2243 | KeyStoreServiceReturnCode KeyStoreService::upgradeKeyBlob(const String16& name, uid_t uid, |
| 2244 | const AuthorizationSet& params, |
| 2245 | Blob* blob) { |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 2246 | // Read the blob rather than assuming the caller provided the right name/uid/blob triplet. |
| 2247 | String8 name8(name); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2248 | KeyStoreServiceReturnCode responseCode = |
| 2249 | mKeyStore->getKeyForName(blob, name8, uid, TYPE_KEYMASTER_10); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2250 | if (responseCode != ResponseCode::NO_ERROR) { |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 2251 | return responseCode; |
| 2252 | } |
Rubin Xu | 7675c9f | 2017-03-15 19:26:52 +0000 | [diff] [blame] | 2253 | ALOGI("upgradeKeyBlob %s %d", name8.string(), uid); |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 2254 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2255 | auto hidlKey = blob2hidlVec(*blob); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 2256 | auto dev = mKeyStore->getDevice(*blob); |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 2257 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2258 | KeyStoreServiceReturnCode error; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2259 | auto hidlCb = [&](ErrorCode ret, const ::std::vector<uint8_t>& upgradedKeyBlob) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2260 | error = ret; |
| 2261 | if (!error.isOk()) { |
Pavel Grafov | cef3947 | 2018-02-12 18:45:02 +0000 | [diff] [blame] | 2262 | if (error == ErrorCode::INVALID_KEY_BLOB) { |
| 2263 | log_key_integrity_violation(name8, uid); |
| 2264 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2265 | return; |
| 2266 | } |
| 2267 | |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 2268 | auto filename = mKeyStore->getBlobFileNameIfExists(name8, uid, ::TYPE_KEYMASTER_10); |
| 2269 | if (!filename.isOk()) { |
| 2270 | ALOGI("trying to upgrade a non existing blob"); |
| 2271 | return; |
| 2272 | } |
| 2273 | error = mKeyStore->del(filename.value().string(), ::TYPE_ANY, get_user_id(uid)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2274 | if (!error.isOk()) { |
Rubin Xu | 7675c9f | 2017-03-15 19:26:52 +0000 | [diff] [blame] | 2275 | ALOGI("upgradeKeyBlob keystore->del failed %d", (int)error); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2276 | return; |
| 2277 | } |
| 2278 | |
| 2279 | Blob newBlob(&upgradedKeyBlob[0], upgradedKeyBlob.size(), nullptr /* info */, |
| 2280 | 0 /* infoLength */, ::TYPE_KEYMASTER_10); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 2281 | newBlob.setSecurityLevel(blob->getSecurityLevel()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2282 | newBlob.setEncrypted(blob->isEncrypted()); |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 2283 | newBlob.setSuperEncrypted(blob->isSuperEncrypted()); |
| 2284 | newBlob.setCriticalToDeviceEncryption(blob->isCriticalToDeviceEncryption()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2285 | |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 2286 | error = mKeyStore->put(filename.value().string(), &newBlob, get_user_id(uid)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2287 | if (!error.isOk()) { |
Rubin Xu | 7675c9f | 2017-03-15 19:26:52 +0000 | [diff] [blame] | 2288 | ALOGI("upgradeKeyBlob keystore->put failed %d", (int)error); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2289 | return; |
| 2290 | } |
| 2291 | |
| 2292 | // Re-read blob for caller. We can't use newBlob because writing it modified it. |
| 2293 | error = mKeyStore->getKeyForName(blob, name8, uid, TYPE_KEYMASTER_10); |
| 2294 | }; |
| 2295 | |
| 2296 | KeyStoreServiceReturnCode rc = |
| 2297 | KS_HANDLE_HIDL_ERROR(dev->upgradeKey(hidlKey, params.hidl_data(), hidlCb)); |
| 2298 | if (!rc.isOk()) { |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 2299 | return rc; |
| 2300 | } |
| 2301 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2302 | return error; |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 2303 | } |
| 2304 | |
Brian Young | 9a947d5 | 2018-02-23 18:03:14 +0000 | [diff] [blame] | 2305 | Status KeyStoreService::onKeyguardVisibilityChanged(bool isShowing, int32_t userId, |
| 2306 | int32_t* aidl_return) { |
| 2307 | enforcement_policy.set_device_locked(isShowing, userId); |
| 2308 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
Brian Young | 9371e95 | 2018-02-23 18:03:14 +0000 | [diff] [blame] | 2309 | |
| 2310 | return Status::ok(); |
| 2311 | } |
| 2312 | |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 2313 | } // namespace keystore |