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