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