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