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