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