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; |
nagendra modadugu | 36a9f4f | 2018-09-24 13:40:23 -0700 | [diff] [blame] | 1377 | if (!result->resultCode.isOk()) { |
| 1378 | LOG(ERROR) << "Failed to verify authorization " << rc << " from begin()"; |
| 1379 | rc = KS_HANDLE_HIDL_ERROR(dev->abort(result->handle)); |
| 1380 | if (!rc.isOk()) { |
| 1381 | LOG(ERROR) << "Failed to abort operation " << rc << " from begin()"; |
| 1382 | } |
| 1383 | return Status::ok(); |
| 1384 | } |
Shawn Willden | 2eef135 | 2018-05-22 16:13:24 -0600 | [diff] [blame] | 1385 | } |
| 1386 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1387 | // Note: The operation map takes possession of the contents of "characteristics". |
| 1388 | // 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] | 1389 | sp<IBinder> operationToken = |
| 1390 | mOperationMap.addOperation(result->handle, keyid, keyPurpose, dev, appToken, |
| 1391 | std::move(characteristics), params.getParameters(), pruneable); |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1392 | assert(characteristics.hardwareEnforced.size() == 0); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1393 | assert(characteristics.softwareEnforced.size() == 0); |
Shawn Willden | c5e8f36 | 2017-08-31 09:23:06 -0600 | [diff] [blame] | 1394 | result->token = operationToken; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1395 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1396 | mOperationMap.setOperationAuthToken(operationToken, std::move(authToken)); |
Shawn Willden | 2eef135 | 2018-05-22 16:13:24 -0600 | [diff] [blame] | 1397 | mOperationMap.setOperationVerificationToken(operationToken, std::move(verificationToken)); |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1398 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1399 | // Return the authentication lookup result. If this is a per operation |
| 1400 | // auth'd key then the resultCode will be ::OP_AUTH_NEEDED and the |
| 1401 | // application should get an auth token using the handle before the |
| 1402 | // first call to update, which will fail if keystore hasn't received the |
| 1403 | // auth token. |
Shawn Willden | 2f96c79 | 2017-09-07 23:59:08 -0600 | [diff] [blame] | 1404 | if (result->resultCode == ErrorCode::OK) { |
| 1405 | result->resultCode = authResult; |
| 1406 | } |
Shawn Willden | c5e8f36 | 2017-08-31 09:23:06 -0600 | [diff] [blame] | 1407 | |
| 1408 | // Other result fields were set in the begin operation's callback. |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1409 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1410 | } |
| 1411 | |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1412 | void KeyStoreService::appendConfirmationTokenIfNeeded(const KeyCharacteristics& keyCharacteristics, |
| 1413 | std::vector<KeyParameter>* params) { |
| 1414 | if (!(containsTag(keyCharacteristics.softwareEnforced, Tag::TRUSTED_CONFIRMATION_REQUIRED) || |
| 1415 | containsTag(keyCharacteristics.hardwareEnforced, Tag::TRUSTED_CONFIRMATION_REQUIRED))) { |
| 1416 | return; |
| 1417 | } |
| 1418 | |
| 1419 | hidl_vec<uint8_t> confirmationToken = mConfirmationManager->getLatestConfirmationToken(); |
| 1420 | if (confirmationToken.size() == 0) { |
| 1421 | return; |
| 1422 | } |
| 1423 | |
| 1424 | params->push_back( |
| 1425 | Authorization(keymaster::TAG_CONFIRMATION_TOKEN, std::move(confirmationToken))); |
| 1426 | ALOGD("Appending confirmation token\n"); |
| 1427 | } |
| 1428 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1429 | Status KeyStoreService::update(const sp<IBinder>& token, const KeymasterArguments& params, |
| 1430 | const ::std::vector<uint8_t>& data, OperationResult* result) { |
| 1431 | if (!checkAllowedOperationParams(params.getParameters())) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1432 | result->resultCode = ErrorCode::INVALID_ARGUMENT; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1433 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1434 | } |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1435 | |
| 1436 | auto getOpResult = mOperationMap.getOperation(token); |
| 1437 | if (!getOpResult.isOk()) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1438 | result->resultCode = ErrorCode::INVALID_OPERATION_HANDLE; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1439 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1440 | } |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1441 | const auto& op = getOpResult.value(); |
| 1442 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1443 | HardwareAuthToken authToken; |
| 1444 | std::tie(result->resultCode, authToken) = getOperationAuthTokenIfNeeded(token); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1445 | if (!result->resultCode.isOk()) return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1446 | |
| 1447 | // Check that all key authorization policy requirements are met. |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1448 | AuthorizationSet key_auths(op.characteristics.hardwareEnforced); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1449 | key_auths.append(op.characteristics.softwareEnforced.begin(), |
| 1450 | op.characteristics.softwareEnforced.end()); |
| 1451 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1452 | result->resultCode = enforcement_policy.AuthorizeOperation( |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1453 | op.purpose, op.keyid, key_auths, params.getParameters(), authToken, op.handle, |
| 1454 | false /* is_begin_operation */); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1455 | if (!result->resultCode.isOk()) return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1456 | |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1457 | std::vector<KeyParameter> inParams = params.getParameters(); |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1458 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1459 | auto hidlCb = [&](ErrorCode ret, uint32_t inputConsumed, |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1460 | const hidl_vec<KeyParameter>& outParams, |
| 1461 | const ::std::vector<uint8_t>& output) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1462 | result->resultCode = ret; |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1463 | if (result->resultCode.isOk()) { |
| 1464 | result->inputConsumed = inputConsumed; |
| 1465 | result->outParams = outParams; |
| 1466 | result->data = output; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1467 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1468 | }; |
| 1469 | |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1470 | KeyStoreServiceReturnCode rc = KS_HANDLE_HIDL_ERROR( |
Shawn Willden | 2eef135 | 2018-05-22 16:13:24 -0600 | [diff] [blame] | 1471 | op.device->update(op.handle, inParams, data, authToken, op.verificationToken, hidlCb)); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1472 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1473 | // just a reminder: on success result->resultCode was set in the callback. So we only overwrite |
| 1474 | // it if there was a communication error indicated by the ErrorCode. |
nagendra modadugu | 36a9f4f | 2018-09-24 13:40:23 -0700 | [diff] [blame] | 1475 | if (!rc.isOk()) { |
| 1476 | result->resultCode = rc; |
| 1477 | // removeOperation() will free the memory 'op' used, so the order is important |
| 1478 | mAuthTokenTable.MarkCompleted(op.handle); |
| 1479 | mOperationMap.removeOperation(token, /* wasOpSuccessful */ false); |
| 1480 | } |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1481 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1482 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1483 | } |
| 1484 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1485 | Status KeyStoreService::finish(const sp<IBinder>& token, const KeymasterArguments& params, |
| 1486 | const ::std::vector<uint8_t>& signature, |
| 1487 | const ::std::vector<uint8_t>& entropy, OperationResult* result) { |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1488 | auto getOpResult = mOperationMap.getOperation(token); |
| 1489 | if (!getOpResult.isOk()) { |
| 1490 | result->resultCode = ErrorCode::INVALID_OPERATION_HANDLE; |
| 1491 | return Status::ok(); |
| 1492 | } |
| 1493 | const auto& op = std::move(getOpResult.value()); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1494 | if (!checkAllowedOperationParams(params.getParameters())) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1495 | result->resultCode = ErrorCode::INVALID_ARGUMENT; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1496 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1497 | } |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1498 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1499 | HardwareAuthToken authToken; |
| 1500 | std::tie(result->resultCode, authToken) = getOperationAuthTokenIfNeeded(token); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1501 | if (!result->resultCode.isOk()) return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1502 | |
| 1503 | if (entropy.size()) { |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1504 | result->resultCode = KS_HANDLE_HIDL_ERROR(op.device->addRngEntropy(entropy)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1505 | if (!result->resultCode.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1506 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1507 | } |
| 1508 | } |
| 1509 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1510 | // Check that all key authorization policy requirements are met. |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1511 | AuthorizationSet key_auths(op.characteristics.hardwareEnforced); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1512 | key_auths.append(op.characteristics.softwareEnforced.begin(), |
| 1513 | op.characteristics.softwareEnforced.end()); |
| 1514 | |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1515 | std::vector<KeyParameter> inParams = params.getParameters(); |
| 1516 | appendConfirmationTokenIfNeeded(op.characteristics, &inParams); |
| 1517 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1518 | result->resultCode = enforcement_policy.AuthorizeOperation( |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1519 | op.purpose, op.keyid, key_auths, params.getParameters(), authToken, op.handle, |
| 1520 | false /* is_begin_operation */); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1521 | if (!result->resultCode.isOk()) return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1522 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1523 | auto hidlCb = [&](ErrorCode ret, const hidl_vec<KeyParameter>& outParams, |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1524 | const ::std::vector<uint8_t>& output) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1525 | result->resultCode = ret; |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1526 | if (result->resultCode.isOk()) { |
| 1527 | result->outParams = outParams; |
| 1528 | result->data = output; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1529 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1530 | }; |
| 1531 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1532 | KeyStoreServiceReturnCode rc = KS_HANDLE_HIDL_ERROR( |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1533 | op.device->finish(op.handle, inParams, |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1534 | ::std::vector<uint8_t>() /* TODO(swillden): wire up input to finish() */, |
Shawn Willden | 2eef135 | 2018-05-22 16:13:24 -0600 | [diff] [blame] | 1535 | signature, authToken, op.verificationToken, hidlCb)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1536 | |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 1537 | bool wasOpSuccessful = true; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1538 | // just a reminder: on success result->resultCode was set in the callback. So we only overwrite |
| 1539 | // it if there was a communication error indicated by the ErrorCode. |
| 1540 | if (!rc.isOk()) { |
| 1541 | result->resultCode = rc; |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 1542 | wasOpSuccessful = false; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1543 | } |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 1544 | |
| 1545 | // removeOperation() will free the memory 'op' used, so the order is important |
| 1546 | mAuthTokenTable.MarkCompleted(op.handle); |
| 1547 | mOperationMap.removeOperation(token, wasOpSuccessful); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1548 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1549 | } |
| 1550 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1551 | Status KeyStoreService::abort(const sp<IBinder>& token, int32_t* aidl_return) { |
Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [diff] [blame] | 1552 | auto getOpResult = mOperationMap.removeOperation(token, false /* wasOpSuccessful */); |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1553 | if (!getOpResult.isOk()) { |
| 1554 | *aidl_return = static_cast<int32_t>(ErrorCode::INVALID_OPERATION_HANDLE); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1555 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1556 | } |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1557 | auto op = std::move(getOpResult.value()); |
| 1558 | mAuthTokenTable.MarkCompleted(op.handle); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1559 | |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 1560 | ErrorCode error_code = KS_HANDLE_HIDL_ERROR(op.device->abort(op.handle)); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1561 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(error_code)); |
| 1562 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1563 | } |
| 1564 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1565 | Status KeyStoreService::isOperationAuthorized(const sp<IBinder>& token, bool* aidl_return) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1566 | AuthorizationSet ignored; |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1567 | KeyStoreServiceReturnCode rc; |
| 1568 | std::tie(rc, std::ignore) = getOperationAuthTokenIfNeeded(token); |
| 1569 | *aidl_return = rc.isOk(); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1570 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1573 | Status KeyStoreService::addAuthToken(const ::std::vector<uint8_t>& authTokenAsVector, |
Brian Young | ccb492d | 2018-02-22 23:36:01 +0000 | [diff] [blame] | 1574 | int32_t* aidl_return) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1575 | |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1576 | // TODO(swillden): When gatekeeper and fingerprint are ready, this should be updated to |
| 1577 | // receive a HardwareAuthToken, rather than an opaque byte array. |
| 1578 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1579 | if (!checkBinderPermission(P_ADD_AUTH)) { |
| 1580 | ALOGW("addAuthToken: permission denied for %d", IPCThreadState::self()->getCallingUid()); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1581 | *aidl_return = static_cast<int32_t>(ResponseCode::PERMISSION_DENIED); |
| 1582 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1583 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1584 | if (authTokenAsVector.size() != sizeof(hw_auth_token_t)) { |
| 1585 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT)); |
| 1586 | return Status::ok(); |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1587 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1588 | |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1589 | hw_auth_token_t authToken; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1590 | memcpy(reinterpret_cast<void*>(&authToken), authTokenAsVector.data(), sizeof(hw_auth_token_t)); |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1591 | if (authToken.version != 0) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1592 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT)); |
| 1593 | return Status::ok(); |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1594 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1595 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 1596 | mAuthTokenTable.AddAuthenticationToken(hidlVec2AuthToken(hidl_vec<uint8_t>(authTokenAsVector))); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1597 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 1598 | return Status::ok(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1599 | } |
| 1600 | |
Rubin Xu | 1a203e3 | 2018-05-08 14:25:21 +0100 | [diff] [blame] | 1601 | int isDeviceIdAttestationRequested(const KeymasterArguments& params) { |
Chih-Hung Hsieh | b81e68b | 2018-07-13 11:37:45 -0700 | [diff] [blame^] | 1602 | const hardware::hidl_vec<KeyParameter>& paramsVec = params.getParameters(); |
Rubin Xu | 1a203e3 | 2018-05-08 14:25:21 +0100 | [diff] [blame] | 1603 | int result = 0; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1604 | for (size_t i = 0; i < paramsVec.size(); ++i) { |
| 1605 | switch (paramsVec[i].tag) { |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1606 | case Tag::ATTESTATION_ID_BRAND: |
| 1607 | case Tag::ATTESTATION_ID_DEVICE: |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1608 | case Tag::ATTESTATION_ID_MANUFACTURER: |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1609 | case Tag::ATTESTATION_ID_MODEL: |
| 1610 | case Tag::ATTESTATION_ID_PRODUCT: |
Rubin Xu | 1a203e3 | 2018-05-08 14:25:21 +0100 | [diff] [blame] | 1611 | result |= ID_ATTESTATION_REQUEST_GENERIC_INFO; |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1612 | break; |
Rubin Xu | 1a203e3 | 2018-05-08 14:25:21 +0100 | [diff] [blame] | 1613 | case Tag::ATTESTATION_ID_IMEI: |
| 1614 | case Tag::ATTESTATION_ID_MEID: |
| 1615 | case Tag::ATTESTATION_ID_SERIAL: |
| 1616 | result |= ID_ATTESTATION_REQUEST_UNIQUE_DEVICE_ID; |
| 1617 | break; |
| 1618 | default: |
| 1619 | continue; |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1620 | } |
| 1621 | } |
Rubin Xu | 1a203e3 | 2018-05-08 14:25:21 +0100 | [diff] [blame] | 1622 | return result; |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1623 | } |
| 1624 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1625 | Status KeyStoreService::attestKey(const String16& name, const KeymasterArguments& params, |
| 1626 | ::android::security::keymaster::KeymasterCertificateChain* chain, |
| 1627 | int32_t* aidl_return) { |
| 1628 | // check null output if method signature is updated and return ErrorCode::OUTPUT_PARAMETER_NULL |
| 1629 | if (!checkAllowedOperationParams(params.getParameters())) { |
| 1630 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT)); |
| 1631 | return Status::ok(); |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
Eran Messeri | e2c3415 | 2017-12-21 21:01:22 +0000 | [diff] [blame] | 1634 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1635 | |
Rubin Xu | 1a203e3 | 2018-05-08 14:25:21 +0100 | [diff] [blame] | 1636 | int needsIdAttestation = isDeviceIdAttestationRequested(params); |
| 1637 | bool needsUniqueIdAttestation = needsIdAttestation & ID_ATTESTATION_REQUEST_UNIQUE_DEVICE_ID; |
| 1638 | bool isPrimaryUserSystemUid = (callingUid == AID_SYSTEM); |
| 1639 | bool isSomeUserSystemUid = (get_app_id(callingUid) == AID_SYSTEM); |
| 1640 | // Allow system context from any user to request attestation with basic device information, |
| 1641 | // while only allow system context from user 0 (device owner) to request attestation with |
| 1642 | // unique device ID. |
| 1643 | if ((needsIdAttestation && !isSomeUserSystemUid) || |
| 1644 | (needsUniqueIdAttestation && !isPrimaryUserSystemUid)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1645 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT)); |
| 1646 | return Status::ok(); |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1647 | } |
| 1648 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1649 | AuthorizationSet mutableParams = params.getParameters(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1650 | KeyStoreServiceReturnCode rc = updateParamsForAttestation(callingUid, &mutableParams); |
| 1651 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1652 | *aidl_return = static_cast<int32_t>(rc); |
| 1653 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1654 | } |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1655 | |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1656 | Blob keyBlob; |
| 1657 | String8 name8(name); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1658 | rc = mKeyStore->getKeyForName(&keyBlob, name8, callingUid, TYPE_KEYMASTER_10); |
| 1659 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1660 | *aidl_return = static_cast<int32_t>(rc); |
| 1661 | return Status::ok(); |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1664 | KeyStoreServiceReturnCode error; |
| 1665 | auto hidlCb = [&](ErrorCode ret, const hidl_vec<hidl_vec<uint8_t>>& certChain) { |
| 1666 | error = ret; |
| 1667 | if (!error.isOk()) { |
| 1668 | return; |
| 1669 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1670 | if (chain) { |
| 1671 | *chain = KeymasterCertificateChain(certChain); |
| 1672 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1673 | }; |
| 1674 | |
| 1675 | auto hidlKey = blob2hidlVec(keyBlob); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1676 | auto dev = mKeyStore->getDevice(keyBlob); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1677 | rc = KS_HANDLE_HIDL_ERROR(dev->attestKey(hidlKey, mutableParams.hidl_data(), hidlCb)); |
| 1678 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1679 | *aidl_return = static_cast<int32_t>(rc); |
| 1680 | return Status::ok(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1681 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1682 | *aidl_return = static_cast<int32_t>(error); |
| 1683 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1684 | } |
| 1685 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1686 | Status |
| 1687 | KeyStoreService::attestDeviceIds(const KeymasterArguments& params, |
| 1688 | ::android::security::keymaster::KeymasterCertificateChain* chain, |
| 1689 | int32_t* aidl_return) { |
| 1690 | // 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] | 1691 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1692 | if (!checkAllowedOperationParams(params.getParameters())) { |
| 1693 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT)); |
| 1694 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | if (!isDeviceIdAttestationRequested(params)) { |
| 1698 | // There is an attestKey() method for attesting keys without device ID attestation. |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1699 | *aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::INVALID_ARGUMENT)); |
| 1700 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1704 | sp<IBinder> binder = defaultServiceManager()->getService(String16("permission")); |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 1705 | if (binder == nullptr) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1706 | *aidl_return = |
| 1707 | static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::CANNOT_ATTEST_IDS)); |
| 1708 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1709 | } |
| 1710 | if (!interface_cast<IPermissionController>(binder)->checkPermission( |
| 1711 | String16("android.permission.READ_PRIVILEGED_PHONE_STATE"), |
| 1712 | IPCThreadState::self()->getCallingPid(), callingUid)) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1713 | *aidl_return = |
| 1714 | static_cast<int32_t>(KeyStoreServiceReturnCode(ErrorCode::CANNOT_ATTEST_IDS)); |
| 1715 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1716 | } |
| 1717 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1718 | AuthorizationSet mutableParams = params.getParameters(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1719 | KeyStoreServiceReturnCode rc = updateParamsForAttestation(callingUid, &mutableParams); |
| 1720 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1721 | *aidl_return = static_cast<int32_t>(rc); |
| 1722 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | // Generate temporary key. |
Shawn Willden | 70c1a78 | 2018-07-11 15:13:20 -0600 | [diff] [blame] | 1726 | sp<Keymaster> dev = mKeyStore->getDevice(SecurityLevel::TRUSTED_ENVIRONMENT); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1727 | |
Shawn Willden | 70c1a78 | 2018-07-11 15:13:20 -0600 | [diff] [blame] | 1728 | if (!dev) { |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 1729 | *aidl_return = static_cast<int32_t>(ResponseCode::SYSTEM_ERROR); |
| 1730 | return Status::ok(); |
| 1731 | } |
| 1732 | |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1733 | KeyStoreServiceReturnCode error; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1734 | ::std::vector<uint8_t> hidlKey; |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1735 | |
| 1736 | AuthorizationSet keyCharacteristics; |
| 1737 | keyCharacteristics.push_back(TAG_PURPOSE, KeyPurpose::VERIFY); |
| 1738 | keyCharacteristics.push_back(TAG_ALGORITHM, Algorithm::EC); |
| 1739 | keyCharacteristics.push_back(TAG_DIGEST, Digest::SHA_2_256); |
| 1740 | keyCharacteristics.push_back(TAG_NO_AUTH_REQUIRED); |
| 1741 | keyCharacteristics.push_back(TAG_EC_CURVE, EcCurve::P_256); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1742 | auto generateHidlCb = [&](ErrorCode ret, const ::std::vector<uint8_t>& hidlKeyBlob, |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1743 | const KeyCharacteristics&) { |
| 1744 | error = ret; |
| 1745 | if (!error.isOk()) { |
| 1746 | return; |
| 1747 | } |
| 1748 | hidlKey = hidlKeyBlob; |
| 1749 | }; |
| 1750 | |
| 1751 | rc = KS_HANDLE_HIDL_ERROR(dev->generateKey(keyCharacteristics.hidl_data(), generateHidlCb)); |
| 1752 | if (!rc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1753 | *aidl_return = static_cast<int32_t>(rc); |
| 1754 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1755 | } |
| 1756 | if (!error.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1757 | *aidl_return = static_cast<int32_t>(error); |
| 1758 | return Status::ok(); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | // Attest key and device IDs. |
| 1762 | auto attestHidlCb = [&](ErrorCode ret, const hidl_vec<hidl_vec<uint8_t>>& certChain) { |
| 1763 | error = ret; |
| 1764 | if (!error.isOk()) { |
| 1765 | return; |
| 1766 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1767 | *chain = ::android::security::keymaster::KeymasterCertificateChain(certChain); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1768 | }; |
| 1769 | KeyStoreServiceReturnCode attestationRc = |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1770 | KS_HANDLE_HIDL_ERROR(dev->attestKey(hidlKey, mutableParams.hidl_data(), attestHidlCb)); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame] | 1771 | |
| 1772 | // Delete temporary key. |
| 1773 | KeyStoreServiceReturnCode deletionRc = KS_HANDLE_HIDL_ERROR(dev->deleteKey(hidlKey)); |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1774 | |
| 1775 | if (!attestationRc.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1776 | *aidl_return = static_cast<int32_t>(attestationRc); |
| 1777 | return Status::ok(); |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1778 | } |
| 1779 | if (!error.isOk()) { |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1780 | *aidl_return = static_cast<int32_t>(error); |
| 1781 | return Status::ok(); |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1782 | } |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1783 | *aidl_return = static_cast<int32_t>(deletionRc); |
| 1784 | return Status::ok(); |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1785 | } |
| 1786 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1787 | Status KeyStoreService::onDeviceOffBody(int32_t* aidl_return) { |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1788 | // TODO(tuckeris): add permission check. This should be callable from ClockworkHome only. |
| 1789 | mAuthTokenTable.onDeviceOffBody(); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1790 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
| 1791 | return Status::ok(); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1792 | } |
| 1793 | |
Janis Danisevskis | cb9267d | 2017-12-19 16:27:52 -0800 | [diff] [blame] | 1794 | #define AIDL_RETURN(rc) \ |
| 1795 | (*_aidl_return = static_cast<int32_t>(KeyStoreServiceReturnCode(rc)), Status::ok()) |
| 1796 | |
| 1797 | Status KeyStoreService::importWrappedKey( |
| 1798 | const ::android::String16& wrappedKeyAlias, const ::std::vector<uint8_t>& wrappedKey, |
| 1799 | const ::android::String16& wrappingKeyAlias, const ::std::vector<uint8_t>& maskingKey, |
| 1800 | const KeymasterArguments& params, int64_t rootSid, int64_t fingerprintSid, |
| 1801 | ::android::security::keymaster::KeyCharacteristics* outCharacteristics, int32_t* _aidl_return) { |
| 1802 | |
| 1803 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1804 | |
| 1805 | if (!checkBinderPermission(P_INSERT, callingUid)) { |
| 1806 | return AIDL_RETURN(ResponseCode::PERMISSION_DENIED); |
| 1807 | } |
| 1808 | |
| 1809 | Blob wrappingKeyBlob; |
| 1810 | String8 wrappingKeyName8(wrappingKeyAlias); |
| 1811 | KeyStoreServiceReturnCode rc = |
| 1812 | mKeyStore->getKeyForName(&wrappingKeyBlob, wrappingKeyName8, callingUid, TYPE_KEYMASTER_10); |
| 1813 | if (!rc.isOk()) { |
| 1814 | return AIDL_RETURN(rc); |
| 1815 | } |
| 1816 | |
| 1817 | SecurityLevel securityLevel = wrappingKeyBlob.getSecurityLevel(); |
| 1818 | auto dev = mKeyStore->getDevice(securityLevel); |
| 1819 | if (!dev) { |
| 1820 | return AIDL_RETURN(ErrorCode::HARDWARE_TYPE_UNAVAILABLE); |
| 1821 | } |
| 1822 | |
| 1823 | auto hidlWrappingKey = blob2hidlVec(wrappingKeyBlob); |
| 1824 | String8 wrappedKeyAlias8(wrappedKeyAlias); |
| 1825 | |
| 1826 | KeyStoreServiceReturnCode error; |
| 1827 | |
| 1828 | auto hidlCb = [&](ErrorCode ret, const ::std::vector<uint8_t>& keyBlob, |
| 1829 | const KeyCharacteristics& keyCharacteristics) { |
| 1830 | error = ret; |
| 1831 | if (!error.isOk()) { |
| 1832 | return; |
| 1833 | } |
| 1834 | if (outCharacteristics) { |
| 1835 | *outCharacteristics = |
| 1836 | ::android::security::keymaster::KeyCharacteristics(keyCharacteristics); |
| 1837 | } |
| 1838 | |
| 1839 | // Write the key: |
| 1840 | String8 filename( |
| 1841 | mKeyStore->getKeyNameForUidWithDir(wrappedKeyAlias8, callingUid, ::TYPE_KEYMASTER_10)); |
| 1842 | |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 1843 | Blob ksBlob(&keyBlob[0], keyBlob.size(), nullptr, 0, ::TYPE_KEYMASTER_10); |
Janis Danisevskis | cb9267d | 2017-12-19 16:27:52 -0800 | [diff] [blame] | 1844 | ksBlob.setSecurityLevel(securityLevel); |
| 1845 | |
| 1846 | if (containsTag(keyCharacteristics.hardwareEnforced, Tag::USER_SECURE_ID)) { |
| 1847 | ksBlob.setSuperEncrypted(true); |
| 1848 | } |
| 1849 | |
| 1850 | error = mKeyStore->put(filename.string(), &ksBlob, get_user_id(callingUid)); |
| 1851 | }; |
| 1852 | |
Shawn Willden | 0a198a0 | 2018-01-19 13:36:31 -0700 | [diff] [blame] | 1853 | rc = KS_HANDLE_HIDL_ERROR(dev->importWrappedKey(wrappedKey, hidlWrappingKey, maskingKey, |
| 1854 | params.getParameters(), rootSid, fingerprintSid, |
| 1855 | hidlCb)); |
| 1856 | |
Janis Danisevskis | cb9267d | 2017-12-19 16:27:52 -0800 | [diff] [blame] | 1857 | // possible hidl error |
| 1858 | if (!rc.isOk()) { |
| 1859 | return AIDL_RETURN(rc); |
| 1860 | } |
| 1861 | // now check error from callback |
| 1862 | if (!error.isOk()) { |
| 1863 | return AIDL_RETURN(error); |
| 1864 | } |
| 1865 | |
| 1866 | // Write the characteristics: |
| 1867 | String8 cFilename(mKeyStore->getKeyNameForUidWithDir(wrappedKeyAlias8, callingUid, |
| 1868 | ::TYPE_KEY_CHARACTERISTICS)); |
| 1869 | |
| 1870 | AuthorizationSet opParams = params.getParameters(); |
| 1871 | std::stringstream kcStream; |
| 1872 | opParams.Serialize(&kcStream); |
| 1873 | if (kcStream.bad()) { |
| 1874 | return AIDL_RETURN(ResponseCode::SYSTEM_ERROR); |
| 1875 | } |
| 1876 | auto kcBuf = kcStream.str(); |
| 1877 | |
Yi Kong | e353f25 | 2018-07-30 01:38:39 -0700 | [diff] [blame] | 1878 | 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] | 1879 | ::TYPE_KEY_CHARACTERISTICS); |
| 1880 | charBlob.setSecurityLevel(securityLevel); |
| 1881 | |
| 1882 | return AIDL_RETURN(mKeyStore->put(cFilename.string(), &charBlob, get_user_id(callingUid))); |
| 1883 | } |
| 1884 | |
David Zeuthen | c6eb7cd | 2017-11-27 11:33:55 -0500 | [diff] [blame] | 1885 | Status KeyStoreService::presentConfirmationPrompt(const sp<IBinder>& listener, |
| 1886 | const String16& promptText, |
| 1887 | const ::std::vector<uint8_t>& extraData, |
| 1888 | const String16& locale, int32_t uiOptionsAsFlags, |
| 1889 | int32_t* aidl_return) { |
| 1890 | return mConfirmationManager->presentConfirmationPrompt(listener, promptText, extraData, locale, |
| 1891 | uiOptionsAsFlags, aidl_return); |
| 1892 | } |
| 1893 | |
| 1894 | Status KeyStoreService::cancelConfirmationPrompt(const sp<IBinder>& listener, |
| 1895 | int32_t* aidl_return) { |
| 1896 | return mConfirmationManager->cancelConfirmationPrompt(listener, aidl_return); |
| 1897 | } |
| 1898 | |
David Zeuthen | 1a49231 | 2018-02-26 11:00:30 -0500 | [diff] [blame] | 1899 | Status KeyStoreService::isConfirmationPromptSupported(bool* aidl_return) { |
| 1900 | return mConfirmationManager->isConfirmationPromptSupported(aidl_return); |
| 1901 | } |
| 1902 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1903 | /** |
| 1904 | * Prune the oldest pruneable operation. |
| 1905 | */ |
| 1906 | bool KeyStoreService::pruneOperation() { |
| 1907 | sp<IBinder> oldest = mOperationMap.getOldestPruneableOperation(); |
| 1908 | ALOGD("Trying to prune operation %p", oldest.get()); |
| 1909 | size_t op_count_before_abort = mOperationMap.getOperationCount(); |
| 1910 | // We mostly ignore errors from abort() because all we care about is whether at least |
| 1911 | // one operation has been removed. |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 1912 | int32_t abort_error; |
| 1913 | abort(oldest, &abort_error); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1914 | if (mOperationMap.getOperationCount() >= op_count_before_abort) { |
| 1915 | ALOGE("Failed to abort pruneable operation %p, error: %d", oldest.get(), abort_error); |
| 1916 | return false; |
| 1917 | } |
| 1918 | return true; |
| 1919 | } |
| 1920 | |
| 1921 | /** |
| 1922 | * Get the effective target uid for a binder operation that takes an |
| 1923 | * optional uid as the target. |
| 1924 | */ |
| 1925 | uid_t KeyStoreService::getEffectiveUid(int32_t targetUid) { |
| 1926 | if (targetUid == UID_SELF) { |
| 1927 | return IPCThreadState::self()->getCallingUid(); |
| 1928 | } |
| 1929 | return static_cast<uid_t>(targetUid); |
| 1930 | } |
| 1931 | |
| 1932 | /** |
| 1933 | * Check if the caller of the current binder method has the required |
| 1934 | * permission and if acting on other uids the grants to do so. |
| 1935 | */ |
| 1936 | bool KeyStoreService::checkBinderPermission(perm_t permission, int32_t targetUid) { |
| 1937 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1938 | pid_t spid = IPCThreadState::self()->getCallingPid(); |
| 1939 | if (!has_permission(callingUid, permission, spid)) { |
| 1940 | ALOGW("permission %s denied for %d", get_perm_label(permission), callingUid); |
| 1941 | return false; |
| 1942 | } |
| 1943 | if (!is_granted_to(callingUid, getEffectiveUid(targetUid))) { |
| 1944 | ALOGW("uid %d not granted to act for %d", callingUid, targetUid); |
| 1945 | return false; |
| 1946 | } |
| 1947 | return true; |
| 1948 | } |
| 1949 | |
| 1950 | /** |
| 1951 | * Check if the caller of the current binder method has the required |
| 1952 | * permission and the target uid is the caller or the caller is system. |
| 1953 | */ |
| 1954 | bool KeyStoreService::checkBinderPermissionSelfOrSystem(perm_t permission, int32_t targetUid) { |
| 1955 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1956 | pid_t spid = IPCThreadState::self()->getCallingPid(); |
| 1957 | if (!has_permission(callingUid, permission, spid)) { |
| 1958 | ALOGW("permission %s denied for %d", get_perm_label(permission), callingUid); |
| 1959 | return false; |
| 1960 | } |
| 1961 | return getEffectiveUid(targetUid) == callingUid || callingUid == AID_SYSTEM; |
| 1962 | } |
| 1963 | |
| 1964 | /** |
| 1965 | * Check if the caller of the current binder method has the required |
| 1966 | * permission or the target of the operation is the caller's uid. This is |
| 1967 | * for operation where the permission is only for cross-uid activity and all |
| 1968 | * uids are allowed to act on their own (ie: clearing all entries for a |
| 1969 | * given uid). |
| 1970 | */ |
| 1971 | bool KeyStoreService::checkBinderPermissionOrSelfTarget(perm_t permission, int32_t targetUid) { |
| 1972 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1973 | if (getEffectiveUid(targetUid) == callingUid) { |
| 1974 | return true; |
| 1975 | } else { |
| 1976 | return checkBinderPermission(permission, targetUid); |
| 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | /** |
| 1981 | * Helper method to check that the caller has the required permission as |
| 1982 | * well as the keystore is in the unlocked state if checkUnlocked is true. |
| 1983 | * |
| 1984 | * Returns NO_ERROR on success, PERMISSION_DENIED on a permission error and |
| 1985 | * otherwise the state of keystore when not unlocked and checkUnlocked is |
| 1986 | * true. |
| 1987 | */ |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1988 | KeyStoreServiceReturnCode |
| 1989 | KeyStoreService::checkBinderPermissionAndKeystoreState(perm_t permission, int32_t targetUid, |
| 1990 | bool checkUnlocked) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1991 | if (!checkBinderPermission(permission, targetUid)) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1992 | return ResponseCode::PERMISSION_DENIED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1993 | } |
| 1994 | State state = mKeyStore->getState(get_user_id(getEffectiveUid(targetUid))); |
| 1995 | if (checkUnlocked && !isKeystoreUnlocked(state)) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1996 | // All State values coincide with ResponseCodes |
| 1997 | return static_cast<ResponseCode>(state); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1998 | } |
| 1999 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2000 | return ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | bool KeyStoreService::isKeystoreUnlocked(State state) { |
| 2004 | switch (state) { |
| 2005 | case ::STATE_NO_ERROR: |
| 2006 | return true; |
| 2007 | case ::STATE_UNINITIALIZED: |
| 2008 | case ::STATE_LOCKED: |
| 2009 | return false; |
| 2010 | } |
| 2011 | return false; |
| 2012 | } |
| 2013 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2014 | /** |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2015 | * Check that all KeyParameters provided by the application are allowed. Any parameter that keystore |
| 2016 | * adds itself should be disallowed here. |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2017 | */ |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2018 | bool KeyStoreService::checkAllowedOperationParams(const hidl_vec<KeyParameter>& params) { |
| 2019 | for (size_t i = 0; i < params.size(); ++i) { |
| 2020 | switch (params[i].tag) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2021 | case Tag::ATTESTATION_APPLICATION_ID: |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 2022 | case Tag::RESET_SINCE_ID_ROTATION: |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2023 | return false; |
| 2024 | default: |
| 2025 | break; |
| 2026 | } |
| 2027 | } |
| 2028 | return true; |
| 2029 | } |
| 2030 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2031 | ErrorCode KeyStoreService::getOperationCharacteristics(const hidl_vec<uint8_t>& key, |
Shawn Willden | c67a8aa | 2017-12-03 17:51:29 -0700 | [diff] [blame] | 2032 | sp<Keymaster>* dev, |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2033 | const AuthorizationSet& params, |
| 2034 | KeyCharacteristics* out) { |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 2035 | ::std::vector<uint8_t> clientId; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2036 | ::std::vector<uint8_t> appData; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2037 | for (auto param : params) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2038 | if (param.tag == Tag::APPLICATION_ID) { |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 2039 | clientId = authorizationValue(TAG_APPLICATION_ID, param).value(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2040 | } else if (param.tag == Tag::APPLICATION_DATA) { |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 2041 | appData = authorizationValue(TAG_APPLICATION_DATA, param).value(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2042 | } |
| 2043 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2044 | ErrorCode error = ErrorCode::OK; |
| 2045 | |
| 2046 | auto hidlCb = [&](ErrorCode ret, const KeyCharacteristics& keyCharacteristics) { |
| 2047 | error = ret; |
| 2048 | if (error != ErrorCode::OK) { |
| 2049 | return; |
| 2050 | } |
| 2051 | if (out) *out = keyCharacteristics; |
| 2052 | }; |
| 2053 | |
Janis Danisevskis | 64ec1fe | 2018-02-26 16:47:21 -0800 | [diff] [blame] | 2054 | ErrorCode rc = |
| 2055 | KS_HANDLE_HIDL_ERROR((*dev)->getKeyCharacteristics(key, clientId, appData, hidlCb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2056 | if (rc != ErrorCode::OK) { |
| 2057 | return rc; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2058 | } |
| 2059 | return error; |
| 2060 | } |
| 2061 | |
| 2062 | /** |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 2063 | * Get the auth token for this operation from the auth token table. |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2064 | * |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 2065 | * Returns ResponseCode::NO_ERROR if the auth token was set or none was required. |
| 2066 | * ::OP_AUTH_NEEDED if it is a per op authorization, no |
| 2067 | * authorization token exists for that operation and |
| 2068 | * failOnTokenMissing is false. |
| 2069 | * KM_ERROR_KEY_USER_NOT_AUTHENTICATED if there is no valid auth |
| 2070 | * token for the operation |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2071 | */ |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2072 | std::pair<KeyStoreServiceReturnCode, HardwareAuthToken> |
| 2073 | KeyStoreService::getAuthToken(const KeyCharacteristics& characteristics, uint64_t handle, |
| 2074 | KeyPurpose purpose, bool failOnTokenMissing) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2075 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2076 | AuthorizationSet allCharacteristics(characteristics.softwareEnforced); |
| 2077 | allCharacteristics.append(characteristics.hardwareEnforced.begin(), |
| 2078 | characteristics.hardwareEnforced.end()); |
| 2079 | |
| 2080 | const HardwareAuthToken* authToken = nullptr; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2081 | AuthTokenTable::Error err = mAuthTokenTable.FindAuthorization( |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2082 | allCharacteristics, static_cast<KeyPurpose>(purpose), handle, &authToken); |
| 2083 | |
| 2084 | KeyStoreServiceReturnCode rc; |
| 2085 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2086 | switch (err) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2087 | case AuthTokenTable::OK: |
| 2088 | case AuthTokenTable::AUTH_NOT_REQUIRED: |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2089 | rc = ResponseCode::NO_ERROR; |
| 2090 | break; |
| 2091 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2092 | case AuthTokenTable::AUTH_TOKEN_NOT_FOUND: |
| 2093 | case AuthTokenTable::AUTH_TOKEN_EXPIRED: |
| 2094 | case AuthTokenTable::AUTH_TOKEN_WRONG_SID: |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2095 | ALOGE("getAuthToken failed: %d", err); // STOPSHIP: debug only, to be removed |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2096 | rc = ErrorCode::KEY_USER_NOT_AUTHENTICATED; |
| 2097 | break; |
| 2098 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2099 | case AuthTokenTable::OP_HANDLE_REQUIRED: |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2100 | rc = failOnTokenMissing ? KeyStoreServiceReturnCode(ErrorCode::KEY_USER_NOT_AUTHENTICATED) |
| 2101 | : KeyStoreServiceReturnCode(ResponseCode::OP_AUTH_NEEDED); |
| 2102 | break; |
| 2103 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2104 | default: |
| 2105 | ALOGE("Unexpected FindAuthorization return value %d", err); |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2106 | rc = ErrorCode::INVALID_ARGUMENT; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2107 | } |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2108 | |
| 2109 | return {rc, authToken ? std::move(*authToken) : HardwareAuthToken()}; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | /** |
| 2113 | * Add the auth token for the operation to the param list if the operation |
| 2114 | * requires authorization. Uses the cached result in the OperationMap if available |
| 2115 | * otherwise gets the token from the AuthTokenTable and caches the result. |
| 2116 | * |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2117 | * 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] | 2118 | * KM_ERROR_KEY_USER_NOT_AUTHENTICATED if the operation is not |
| 2119 | * authenticated. |
| 2120 | * KM_ERROR_INVALID_OPERATION_HANDLE if token is not a valid |
| 2121 | * operation token. |
| 2122 | */ |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2123 | std::pair<KeyStoreServiceReturnCode, const HardwareAuthToken&> |
| 2124 | KeyStoreService::getOperationAuthTokenIfNeeded(const sp<IBinder>& token) { |
| 2125 | static HardwareAuthToken emptyToken = {}; |
| 2126 | |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 2127 | auto getOpResult = mOperationMap.getOperation(token); |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2128 | if (!getOpResult.isOk()) return {ErrorCode::INVALID_OPERATION_HANDLE, emptyToken}; |
Shawn Willden | da6dcc3 | 2017-12-03 14:56:05 -0700 | [diff] [blame] | 2129 | const auto& op = getOpResult.value(); |
| 2130 | |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2131 | if (!op.hasAuthToken()) { |
| 2132 | KeyStoreServiceReturnCode rc; |
| 2133 | HardwareAuthToken found; |
| 2134 | std::tie(rc, found) = getAuthToken(op.characteristics, op.handle, op.purpose); |
| 2135 | if (!rc.isOk()) return {rc, emptyToken}; |
| 2136 | mOperationMap.setOperationAuthToken(token, std::move(found)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2137 | } |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2138 | |
| 2139 | return {ResponseCode::NO_ERROR, op.authToken}; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | /** |
| 2143 | * Translate a result value to a legacy return value. All keystore errors are |
| 2144 | * preserved and keymaster errors become SYSTEM_ERRORs |
| 2145 | */ |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2146 | KeyStoreServiceReturnCode KeyStoreService::translateResultToLegacyResult(int32_t result) { |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2147 | if (result > 0) return static_cast<ResponseCode>(result); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2148 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2149 | } |
| 2150 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2151 | static NullOr<const Algorithm&> getKeyAlgoritmFromKeyCharacteristics( |
| 2152 | const ::android::security::keymaster::KeyCharacteristics& characteristics) { |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2153 | for (const auto& param : characteristics.hardwareEnforced.getParameters()) { |
| 2154 | auto algo = authorizationValue(TAG_ALGORITHM, param); |
| 2155 | if (algo.isOk()) return algo; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2156 | } |
Shawn Willden | 0329a82 | 2017-12-04 13:55:14 -0700 | [diff] [blame] | 2157 | for (const auto& param : characteristics.softwareEnforced.getParameters()) { |
| 2158 | auto algo = authorizationValue(TAG_ALGORITHM, param); |
| 2159 | if (algo.isOk()) return algo; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2160 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2161 | return {}; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2162 | } |
| 2163 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2164 | void KeyStoreService::addLegacyBeginParams(const String16& name, AuthorizationSet* params) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2165 | // All legacy keys are DIGEST_NONE/PAD_NONE. |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2166 | params->push_back(TAG_DIGEST, Digest::NONE); |
| 2167 | params->push_back(TAG_PADDING, PaddingMode::NONE); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2168 | |
| 2169 | // Look up the algorithm of the key. |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2170 | ::android::security::keymaster::KeyCharacteristics characteristics; |
| 2171 | int32_t result; |
| 2172 | auto rc = getKeyCharacteristics(name, ::android::security::keymaster::KeymasterBlob(), |
| 2173 | ::android::security::keymaster::KeymasterBlob(), UID_SELF, |
| 2174 | &characteristics, &result); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2175 | if (!rc.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2176 | ALOGE("Failed to get key characteristics"); |
| 2177 | return; |
| 2178 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2179 | auto algorithm = getKeyAlgoritmFromKeyCharacteristics(characteristics); |
| 2180 | if (!algorithm.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2181 | ALOGE("getKeyCharacteristics did not include KM_TAG_ALGORITHM"); |
| 2182 | return; |
| 2183 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2184 | params->push_back(TAG_ALGORITHM, algorithm.value()); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2185 | } |
| 2186 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2187 | KeyStoreServiceReturnCode KeyStoreService::doLegacySignVerify(const String16& name, |
| 2188 | const hidl_vec<uint8_t>& data, |
| 2189 | hidl_vec<uint8_t>* out, |
| 2190 | const hidl_vec<uint8_t>& signature, |
| 2191 | KeyPurpose purpose) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2192 | |
| 2193 | std::basic_stringstream<uint8_t> outBuffer; |
| 2194 | OperationResult result; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2195 | AuthorizationSet inArgs; |
| 2196 | addLegacyBeginParams(name, &inArgs); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2197 | sp<IBinder> appToken(new BBinder); |
| 2198 | sp<IBinder> token; |
| 2199 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2200 | begin(appToken, name, static_cast<int32_t>(purpose), true, |
| 2201 | KeymasterArguments(inArgs.hidl_data()), ::std::vector<uint8_t>(), UID_SELF, &result); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2202 | if (!result.resultCode.isOk()) { |
| 2203 | if (result.resultCode == ResponseCode::KEY_NOT_FOUND) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2204 | ALOGW("Key not found"); |
| 2205 | } else { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2206 | ALOGW("Error in begin: %d", int32_t(result.resultCode)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2207 | } |
| 2208 | return translateResultToLegacyResult(result.resultCode); |
| 2209 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2210 | inArgs.Clear(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2211 | token = result.token; |
| 2212 | size_t consumed = 0; |
| 2213 | size_t lastConsumed = 0; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2214 | hidl_vec<uint8_t> data_view; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2215 | do { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2216 | data_view.setToExternal(const_cast<uint8_t*>(&data[consumed]), data.size() - consumed); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2217 | update(token, KeymasterArguments(inArgs.hidl_data()), data_view, &result); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2218 | if (result.resultCode != ResponseCode::NO_ERROR) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2219 | ALOGW("Error in update: %d", int32_t(result.resultCode)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2220 | return translateResultToLegacyResult(result.resultCode); |
| 2221 | } |
| 2222 | if (out) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2223 | outBuffer.write(&result.data[0], result.data.size()); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2224 | } |
| 2225 | lastConsumed = result.inputConsumed; |
| 2226 | consumed += lastConsumed; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2227 | } while (consumed < data.size() && lastConsumed > 0); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2228 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2229 | if (consumed != data.size()) { |
| 2230 | ALOGW("Not all data consumed. Consumed %zu of %zu", consumed, data.size()); |
| 2231 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2232 | } |
| 2233 | |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2234 | finish(token, KeymasterArguments(inArgs.hidl_data()), signature, ::std::vector<uint8_t>(), |
| 2235 | &result); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2236 | if (result.resultCode != ResponseCode::NO_ERROR) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2237 | ALOGW("Error in finish: %d", int32_t(result.resultCode)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2238 | return translateResultToLegacyResult(result.resultCode); |
| 2239 | } |
| 2240 | if (out) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2241 | outBuffer.write(&result.data[0], result.data.size()); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | if (out) { |
| 2245 | auto buf = outBuffer.str(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2246 | out->resize(buf.size()); |
| 2247 | memcpy(&(*out)[0], buf.data(), out->size()); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2248 | } |
| 2249 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2250 | return ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 2251 | } |
| 2252 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2253 | KeyStoreServiceReturnCode KeyStoreService::upgradeKeyBlob(const String16& name, uid_t uid, |
| 2254 | const AuthorizationSet& params, |
| 2255 | Blob* blob) { |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 2256 | // Read the blob rather than assuming the caller provided the right name/uid/blob triplet. |
| 2257 | String8 name8(name); |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2258 | KeyStoreServiceReturnCode responseCode = |
| 2259 | mKeyStore->getKeyForName(blob, name8, uid, TYPE_KEYMASTER_10); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2260 | if (responseCode != ResponseCode::NO_ERROR) { |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 2261 | return responseCode; |
| 2262 | } |
Rubin Xu | 7675c9f | 2017-03-15 19:26:52 +0000 | [diff] [blame] | 2263 | ALOGI("upgradeKeyBlob %s %d", name8.string(), uid); |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 2264 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2265 | auto hidlKey = blob2hidlVec(*blob); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 2266 | auto dev = mKeyStore->getDevice(*blob); |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 2267 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2268 | KeyStoreServiceReturnCode error; |
Dmitry Dementyev | a447b3c | 2017-10-27 23:09:53 -0700 | [diff] [blame] | 2269 | auto hidlCb = [&](ErrorCode ret, const ::std::vector<uint8_t>& upgradedKeyBlob) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2270 | error = ret; |
| 2271 | if (!error.isOk()) { |
Pavel Grafov | cef3947 | 2018-02-12 18:45:02 +0000 | [diff] [blame] | 2272 | if (error == ErrorCode::INVALID_KEY_BLOB) { |
| 2273 | log_key_integrity_violation(name8, uid); |
| 2274 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2275 | return; |
| 2276 | } |
| 2277 | |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 2278 | auto filename = mKeyStore->getBlobFileNameIfExists(name8, uid, ::TYPE_KEYMASTER_10); |
| 2279 | if (!filename.isOk()) { |
| 2280 | ALOGI("trying to upgrade a non existing blob"); |
| 2281 | return; |
| 2282 | } |
| 2283 | error = mKeyStore->del(filename.value().string(), ::TYPE_ANY, get_user_id(uid)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2284 | if (!error.isOk()) { |
Rubin Xu | 7675c9f | 2017-03-15 19:26:52 +0000 | [diff] [blame] | 2285 | ALOGI("upgradeKeyBlob keystore->del failed %d", (int)error); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2286 | return; |
| 2287 | } |
| 2288 | |
| 2289 | Blob newBlob(&upgradedKeyBlob[0], upgradedKeyBlob.size(), nullptr /* info */, |
| 2290 | 0 /* infoLength */, ::TYPE_KEYMASTER_10); |
Janis Danisevskis | c146014 | 2017-12-18 16:48:46 -0800 | [diff] [blame] | 2291 | newBlob.setSecurityLevel(blob->getSecurityLevel()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2292 | newBlob.setEncrypted(blob->isEncrypted()); |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 2293 | newBlob.setSuperEncrypted(blob->isSuperEncrypted()); |
| 2294 | newBlob.setCriticalToDeviceEncryption(blob->isCriticalToDeviceEncryption()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2295 | |
Janis Danisevskis | af7783f | 2017-09-21 11:29:47 -0700 | [diff] [blame] | 2296 | error = mKeyStore->put(filename.value().string(), &newBlob, get_user_id(uid)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2297 | if (!error.isOk()) { |
Rubin Xu | 7675c9f | 2017-03-15 19:26:52 +0000 | [diff] [blame] | 2298 | ALOGI("upgradeKeyBlob keystore->put failed %d", (int)error); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2299 | return; |
| 2300 | } |
| 2301 | |
| 2302 | // Re-read blob for caller. We can't use newBlob because writing it modified it. |
| 2303 | error = mKeyStore->getKeyForName(blob, name8, uid, TYPE_KEYMASTER_10); |
| 2304 | }; |
| 2305 | |
| 2306 | KeyStoreServiceReturnCode rc = |
| 2307 | KS_HANDLE_HIDL_ERROR(dev->upgradeKey(hidlKey, params.hidl_data(), hidlCb)); |
| 2308 | if (!rc.isOk()) { |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 2309 | return rc; |
| 2310 | } |
| 2311 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 2312 | return error; |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 2313 | } |
| 2314 | |
Brian Young | 9a947d5 | 2018-02-23 18:03:14 +0000 | [diff] [blame] | 2315 | Status KeyStoreService::onKeyguardVisibilityChanged(bool isShowing, int32_t userId, |
| 2316 | int32_t* aidl_return) { |
| 2317 | enforcement_policy.set_device_locked(isShowing, userId); |
| 2318 | *aidl_return = static_cast<int32_t>(ResponseCode::NO_ERROR); |
Brian Young | 9371e95 | 2018-02-23 18:03:14 +0000 | [diff] [blame] | 2319 | |
| 2320 | return Status::ok(); |
| 2321 | } |
| 2322 | |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 2323 | } // namespace keystore |