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 | }; |
| 55 | typedef UniquePtr<BIGNUM, BIGNUM_Delete> Unique_BIGNUM; |
| 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); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 194 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, targetUid, ::TYPE_ANY)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 195 | ResponseCode result = mKeyStore->del(filename.string(), ::TYPE_ANY, get_user_id(targetUid)); |
| 196 | if (result != ResponseCode::NO_ERROR) { |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 197 | return result; |
| 198 | } |
| 199 | |
| 200 | // Also delete any characteristics files |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 201 | String8 chrFilename( |
| 202 | mKeyStore->getKeyNameForUidWithDir(name8, targetUid, ::TYPE_KEY_CHARACTERISTICS)); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 203 | return mKeyStore->del(chrFilename.string(), ::TYPE_KEY_CHARACTERISTICS, get_user_id(targetUid)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 206 | KeyStoreServiceReturnCode KeyStoreService::exist(const String16& name, int targetUid) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 207 | targetUid = getEffectiveUid(targetUid); |
| 208 | if (!checkBinderPermission(P_EXIST, targetUid)) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 209 | return ResponseCode::PERMISSION_DENIED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | String8 name8(name); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 213 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, targetUid, ::TYPE_ANY)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 214 | |
| 215 | if (access(filename.string(), R_OK) == -1) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 216 | return (errno != ENOENT) ? ResponseCode::SYSTEM_ERROR : ResponseCode::KEY_NOT_FOUND; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 217 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 218 | return ResponseCode::NO_ERROR; |
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 | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 515 | KeyStoreServiceReturnCode 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()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 519 | return result; |
| 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 | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 526 | return (errno != ENOENT) ? ResponseCode::SYSTEM_ERROR : ResponseCode::KEY_NOT_FOUND; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | mKeyStore->addGrant(filename.string(), granteeUid); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 530 | return ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 533 | KeyStoreServiceReturnCode KeyStoreService::ungrant(const String16& name, int32_t granteeUid) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 534 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 535 | auto result = checkBinderPermissionAndKeystoreState(P_GRANT); |
| 536 | if (!result.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 537 | return result; |
| 538 | } |
| 539 | |
| 540 | String8 name8(name); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 541 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, callingUid, ::TYPE_ANY)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 542 | |
| 543 | if (access(filename.string(), R_OK) == -1) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 544 | return (errno != ENOENT) ? ResponseCode::SYSTEM_ERROR : ResponseCode::KEY_NOT_FOUND; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 547 | return mKeyStore->removeGrant(filename.string(), granteeUid) ? ResponseCode::NO_ERROR |
| 548 | : ResponseCode::KEY_NOT_FOUND; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | int64_t KeyStoreService::getmtime(const String16& name, int32_t uid) { |
| 552 | uid_t targetUid = getEffectiveUid(uid); |
| 553 | if (!checkBinderPermission(P_GET, targetUid)) { |
| 554 | ALOGW("permission denied for %d: getmtime", targetUid); |
| 555 | return -1L; |
| 556 | } |
| 557 | |
| 558 | String8 name8(name); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 559 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, targetUid, ::TYPE_ANY)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 560 | |
| 561 | if (access(filename.string(), R_OK) == -1) { |
| 562 | ALOGW("could not access %s for getmtime", filename.string()); |
| 563 | return -1L; |
| 564 | } |
| 565 | |
| 566 | int fd = TEMP_FAILURE_RETRY(open(filename.string(), O_NOFOLLOW, O_RDONLY)); |
| 567 | if (fd < 0) { |
| 568 | ALOGW("could not open %s for getmtime", filename.string()); |
| 569 | return -1L; |
| 570 | } |
| 571 | |
| 572 | struct stat s; |
| 573 | int ret = fstat(fd, &s); |
| 574 | close(fd); |
| 575 | if (ret == -1) { |
| 576 | ALOGW("could not stat %s for getmtime", filename.string()); |
| 577 | return -1L; |
| 578 | } |
| 579 | |
| 580 | return static_cast<int64_t>(s.st_mtime); |
| 581 | } |
| 582 | |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 583 | // 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] | 584 | KeyStoreServiceReturnCode KeyStoreService::duplicate(const String16& srcKey, int32_t srcUid, |
| 585 | const String16& destKey, int32_t destUid) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 586 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 587 | pid_t spid = IPCThreadState::self()->getCallingPid(); |
| 588 | if (!has_permission(callingUid, P_DUPLICATE, spid)) { |
| 589 | ALOGW("permission denied for %d: duplicate", callingUid); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 590 | return ResponseCode::PERMISSION_DENIED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | State state = mKeyStore->getState(get_user_id(callingUid)); |
| 594 | if (!isKeystoreUnlocked(state)) { |
| 595 | ALOGD("calling duplicate in state: %d", state); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 596 | return ResponseCode(state); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | if (srcUid == -1 || static_cast<uid_t>(srcUid) == callingUid) { |
| 600 | srcUid = callingUid; |
| 601 | } else if (!is_granted_to(callingUid, srcUid)) { |
| 602 | ALOGD("migrate not granted from source: %d -> %d", callingUid, srcUid); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 603 | return ResponseCode::PERMISSION_DENIED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | if (destUid == -1) { |
| 607 | destUid = callingUid; |
| 608 | } |
| 609 | |
| 610 | if (srcUid != destUid) { |
| 611 | if (static_cast<uid_t>(srcUid) != callingUid) { |
| 612 | ALOGD("can only duplicate from caller to other or to same uid: " |
| 613 | "calling=%d, srcUid=%d, destUid=%d", |
| 614 | callingUid, srcUid, destUid); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 615 | return ResponseCode::PERMISSION_DENIED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | if (!is_granted_to(callingUid, destUid)) { |
| 619 | ALOGD("duplicate not granted to dest: %d -> %d", callingUid, destUid); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 620 | return ResponseCode::PERMISSION_DENIED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
| 624 | String8 source8(srcKey); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 625 | String8 sourceFile(mKeyStore->getKeyNameForUidWithDir(source8, srcUid, ::TYPE_ANY)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 626 | |
| 627 | String8 target8(destKey); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 628 | String8 targetFile(mKeyStore->getKeyNameForUidWithDir(target8, destUid, ::TYPE_ANY)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 629 | |
| 630 | if (access(targetFile.string(), W_OK) != -1 || errno != ENOENT) { |
| 631 | ALOGD("destination already exists: %s", targetFile.string()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 632 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | Blob keyBlob; |
| 636 | ResponseCode responseCode = |
| 637 | mKeyStore->get(sourceFile.string(), &keyBlob, TYPE_ANY, get_user_id(srcUid)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 638 | if (responseCode != ResponseCode::NO_ERROR) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 639 | return responseCode; |
| 640 | } |
| 641 | |
| 642 | return mKeyStore->put(targetFile.string(), &keyBlob, get_user_id(destUid)); |
| 643 | } |
| 644 | |
| 645 | int32_t KeyStoreService::is_hardware_backed(const String16& keyType) { |
| 646 | return mKeyStore->isHardwareBacked(keyType) ? 1 : 0; |
| 647 | } |
| 648 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 649 | KeyStoreServiceReturnCode KeyStoreService::clear_uid(int64_t targetUid64) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 650 | uid_t targetUid = getEffectiveUid(targetUid64); |
| 651 | if (!checkBinderPermissionSelfOrSystem(P_CLEAR_UID, targetUid)) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 652 | return ResponseCode::PERMISSION_DENIED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 653 | } |
Rubin Xu | 7675c9f | 2017-03-15 19:26:52 +0000 | [diff] [blame] | 654 | ALOGI("clear_uid %" PRId64, targetUid64); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 655 | |
| 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)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 665 | mKeyStore->del(filename.string(), ::TYPE_ANY, get_user_id(targetUid)); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 666 | |
| 667 | // 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] | 668 | String8 chr_filename( |
| 669 | mKeyStore->getKeyNameForUidWithDir(name8, targetUid, ::TYPE_KEY_CHARACTERISTICS)); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 670 | mKeyStore->del(chr_filename.string(), ::TYPE_KEY_CHARACTERISTICS, get_user_id(targetUid)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 671 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 672 | return ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 675 | KeyStoreServiceReturnCode KeyStoreService::addRngEntropy(const hidl_vec<uint8_t>& entropy) { |
| 676 | const auto& device = mKeyStore->getDevice(); |
| 677 | return KS_HANDLE_HIDL_ERROR(device->addRngEntropy(entropy)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 680 | KeyStoreServiceReturnCode KeyStoreService::generateKey(const String16& name, |
| 681 | const hidl_vec<KeyParameter>& params, |
| 682 | const hidl_vec<uint8_t>& entropy, int uid, |
| 683 | int flags, |
| 684 | KeyCharacteristics* outCharacteristics) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 685 | uid = getEffectiveUid(uid); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 686 | KeyStoreServiceReturnCode rc = |
| 687 | checkBinderPermissionAndKeystoreState(P_INSERT, uid, flags & KEYSTORE_FLAG_ENCRYPTED); |
| 688 | if (!rc.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 689 | return rc; |
| 690 | } |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 691 | if ((flags & KEYSTORE_FLAG_CRITICAL_TO_DEVICE_ENCRYPTION) && get_app_id(uid) != AID_SYSTEM) { |
| 692 | ALOGE("Non-system uid %d cannot set FLAG_CRITICAL_TO_DEVICE_ENCRYPTION", uid); |
| 693 | return ResponseCode::PERMISSION_DENIED; |
| 694 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 695 | |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 696 | if (containsTag(params, Tag::INCLUDE_UNIQUE_ID)) { |
| 697 | if (!checkBinderPermission(P_GEN_UNIQUE_ID)) return ResponseCode::PERMISSION_DENIED; |
| 698 | } |
| 699 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 700 | bool usingFallback = false; |
| 701 | auto& dev = mKeyStore->getDevice(); |
| 702 | AuthorizationSet keyCharacteristics = params; |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 703 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 704 | // TODO: Seed from Linux RNG before this. |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 705 | rc = addRngEntropy(entropy); |
| 706 | if (!rc.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 707 | return rc; |
| 708 | } |
| 709 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 710 | KeyStoreServiceReturnCode error; |
| 711 | auto hidl_cb = [&](ErrorCode ret, const hidl_vec<uint8_t>& hidlKeyBlob, |
| 712 | const KeyCharacteristics& keyCharacteristics) { |
| 713 | error = ret; |
| 714 | if (!error.isOk()) { |
| 715 | return; |
| 716 | } |
| 717 | if (outCharacteristics) *outCharacteristics = keyCharacteristics; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 718 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 719 | // Write the key |
| 720 | String8 name8(name); |
| 721 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, uid, ::TYPE_KEYMASTER_10)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 722 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 723 | Blob keyBlob(&hidlKeyBlob[0], hidlKeyBlob.size(), NULL, 0, ::TYPE_KEYMASTER_10); |
| 724 | keyBlob.setFallback(usingFallback); |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 725 | keyBlob.setCriticalToDeviceEncryption(flags & KEYSTORE_FLAG_CRITICAL_TO_DEVICE_ENCRYPTION); |
| 726 | if (isAuthenticationBound(params) && !keyBlob.isCriticalToDeviceEncryption()) { |
Shawn Willden | d5a24e6 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 727 | keyBlob.setSuperEncrypted(true); |
| 728 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 729 | keyBlob.setEncrypted(flags & KEYSTORE_FLAG_ENCRYPTED); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 730 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 731 | error = mKeyStore->put(filename.string(), &keyBlob, get_user_id(uid)); |
| 732 | }; |
| 733 | |
| 734 | rc = KS_HANDLE_HIDL_ERROR(dev->generateKey(params, hidl_cb)); |
| 735 | if (!rc.isOk()) { |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 736 | return rc; |
| 737 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 738 | if (!error.isOk()) { |
| 739 | ALOGE("Failed to generate key -> falling back to software keymaster"); |
| 740 | usingFallback = true; |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 741 | auto fallback = mKeyStore->getFallbackDevice(); |
| 742 | if (!fallback.isOk()) { |
| 743 | return error; |
| 744 | } |
| 745 | rc = KS_HANDLE_HIDL_ERROR(fallback.value()->generateKey(params, hidl_cb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 746 | if (!rc.isOk()) { |
| 747 | return rc; |
| 748 | } |
| 749 | if (!error.isOk()) { |
| 750 | return error; |
| 751 | } |
| 752 | } |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 753 | |
| 754 | // Write the characteristics: |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 755 | String8 name8(name); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 756 | String8 cFilename(mKeyStore->getKeyNameForUidWithDir(name8, uid, ::TYPE_KEY_CHARACTERISTICS)); |
| 757 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 758 | std::stringstream kc_stream; |
| 759 | keyCharacteristics.Serialize(&kc_stream); |
| 760 | if (kc_stream.bad()) { |
| 761 | return ResponseCode::SYSTEM_ERROR; |
| 762 | } |
| 763 | auto kc_buf = kc_stream.str(); |
| 764 | Blob charBlob(reinterpret_cast<const uint8_t*>(kc_buf.data()), kc_buf.size(), NULL, 0, |
| 765 | ::TYPE_KEY_CHARACTERISTICS); |
| 766 | charBlob.setFallback(usingFallback); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 767 | charBlob.setEncrypted(flags & KEYSTORE_FLAG_ENCRYPTED); |
| 768 | |
| 769 | return mKeyStore->put(cFilename.string(), &charBlob, get_user_id(uid)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 772 | KeyStoreServiceReturnCode |
| 773 | KeyStoreService::getKeyCharacteristics(const String16& name, const hidl_vec<uint8_t>& clientId, |
| 774 | const hidl_vec<uint8_t>& appData, int32_t uid, |
| 775 | KeyCharacteristics* outCharacteristics) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 776 | if (!outCharacteristics) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 777 | return ErrorCode::UNEXPECTED_NULL_POINTER; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | uid_t targetUid = getEffectiveUid(uid); |
| 781 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 782 | if (!is_granted_to(callingUid, targetUid)) { |
| 783 | ALOGW("uid %d not permitted to act for uid %d in getKeyCharacteristics", callingUid, |
| 784 | targetUid); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 785 | return ResponseCode::PERMISSION_DENIED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | Blob keyBlob; |
| 789 | String8 name8(name); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 790 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 791 | KeyStoreServiceReturnCode rc = |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 792 | mKeyStore->getKeyForName(&keyBlob, name8, targetUid, TYPE_KEYMASTER_10); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 793 | if (!rc.isOk()) { |
| 794 | return rc; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 795 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 796 | |
| 797 | auto hidlKeyBlob = blob2hidlVec(keyBlob); |
| 798 | auto& dev = mKeyStore->getDevice(keyBlob); |
| 799 | |
| 800 | KeyStoreServiceReturnCode error; |
| 801 | |
| 802 | auto hidlCb = [&](ErrorCode ret, const KeyCharacteristics& keyCharacteristics) { |
| 803 | error = ret; |
| 804 | if (!error.isOk()) { |
| 805 | return; |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 806 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 807 | *outCharacteristics = keyCharacteristics; |
| 808 | }; |
| 809 | |
| 810 | rc = KS_HANDLE_HIDL_ERROR(dev->getKeyCharacteristics(hidlKeyBlob, clientId, appData, hidlCb)); |
| 811 | if (!rc.isOk()) { |
| 812 | return rc; |
| 813 | } |
| 814 | |
| 815 | if (error == ErrorCode::KEY_REQUIRES_UPGRADE) { |
| 816 | AuthorizationSet upgradeParams; |
| 817 | if (clientId.size()) { |
| 818 | upgradeParams.push_back(TAG_APPLICATION_ID, clientId); |
| 819 | } |
| 820 | if (appData.size()) { |
| 821 | upgradeParams.push_back(TAG_APPLICATION_DATA, appData); |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 822 | } |
| 823 | rc = upgradeKeyBlob(name, targetUid, upgradeParams, &keyBlob); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 824 | if (!rc.isOk()) { |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 825 | return rc; |
| 826 | } |
Shawn Willden | 715d023 | 2016-01-21 00:45:13 -0700 | [diff] [blame] | 827 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 828 | auto upgradedHidlKeyBlob = blob2hidlVec(keyBlob); |
| 829 | |
| 830 | rc = KS_HANDLE_HIDL_ERROR( |
| 831 | dev->getKeyCharacteristics(upgradedHidlKeyBlob, clientId, appData, hidlCb)); |
| 832 | if (!rc.isOk()) { |
| 833 | return rc; |
| 834 | } |
| 835 | // Note that, on success, "error" will have been updated by the hidlCB callback. |
| 836 | // So it is fine to return "error" below. |
| 837 | } |
| 838 | return error; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 841 | KeyStoreServiceReturnCode |
| 842 | KeyStoreService::importKey(const String16& name, const hidl_vec<KeyParameter>& params, |
| 843 | KeyFormat format, const hidl_vec<uint8_t>& keyData, int uid, int flags, |
| 844 | KeyCharacteristics* outCharacteristics) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 845 | uid = getEffectiveUid(uid); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 846 | KeyStoreServiceReturnCode rc = |
| 847 | checkBinderPermissionAndKeystoreState(P_INSERT, uid, flags & KEYSTORE_FLAG_ENCRYPTED); |
| 848 | if (!rc.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 849 | return rc; |
| 850 | } |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 851 | if ((flags & KEYSTORE_FLAG_CRITICAL_TO_DEVICE_ENCRYPTION) && get_app_id(uid) != AID_SYSTEM) { |
| 852 | ALOGE("Non-system uid %d cannot set FLAG_CRITICAL_TO_DEVICE_ENCRYPTION", uid); |
| 853 | return ResponseCode::PERMISSION_DENIED; |
| 854 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 855 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 856 | bool usingFallback = false; |
| 857 | auto& dev = mKeyStore->getDevice(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 858 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 859 | String8 name8(name); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 860 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 861 | KeyStoreServiceReturnCode error; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 862 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 863 | auto hidlCb = [&](ErrorCode ret, const hidl_vec<uint8_t>& keyBlob, |
| 864 | const KeyCharacteristics& keyCharacteristics) { |
| 865 | error = ret; |
| 866 | if (!error.isOk()) { |
| 867 | return; |
| 868 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 869 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 870 | if (outCharacteristics) *outCharacteristics = keyCharacteristics; |
| 871 | |
| 872 | // Write the key: |
| 873 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, uid, ::TYPE_KEYMASTER_10)); |
| 874 | |
| 875 | Blob ksBlob(&keyBlob[0], keyBlob.size(), NULL, 0, ::TYPE_KEYMASTER_10); |
| 876 | ksBlob.setFallback(usingFallback); |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 877 | ksBlob.setCriticalToDeviceEncryption(flags & KEYSTORE_FLAG_CRITICAL_TO_DEVICE_ENCRYPTION); |
| 878 | if (isAuthenticationBound(params) && !ksBlob.isCriticalToDeviceEncryption()) { |
Shawn Willden | d5a24e6 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 879 | ksBlob.setSuperEncrypted(true); |
| 880 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 881 | ksBlob.setEncrypted(flags & KEYSTORE_FLAG_ENCRYPTED); |
| 882 | |
| 883 | error = mKeyStore->put(filename.string(), &ksBlob, get_user_id(uid)); |
| 884 | }; |
| 885 | |
| 886 | rc = KS_HANDLE_HIDL_ERROR(dev->importKey(params, format, keyData, hidlCb)); |
| 887 | // possible hidl error |
| 888 | if (!rc.isOk()) { |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 889 | return rc; |
| 890 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 891 | // now check error from callback |
| 892 | if (!error.isOk()) { |
| 893 | ALOGE("Failed to import key -> falling back to software keymaster"); |
| 894 | usingFallback = true; |
Janis Danisevskis | e8ba180 | 2017-01-30 10:49:51 +0000 | [diff] [blame] | 895 | auto fallback = mKeyStore->getFallbackDevice(); |
| 896 | if (!fallback.isOk()) { |
| 897 | return error; |
| 898 | } |
| 899 | rc = KS_HANDLE_HIDL_ERROR(fallback.value()->importKey(params, format, keyData, hidlCb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 900 | // possible hidl error |
| 901 | if (!rc.isOk()) { |
| 902 | return rc; |
| 903 | } |
| 904 | // now check error from callback |
| 905 | if (!error.isOk()) { |
| 906 | return error; |
| 907 | } |
| 908 | } |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 909 | |
| 910 | // Write the characteristics: |
| 911 | String8 cFilename(mKeyStore->getKeyNameForUidWithDir(name8, uid, ::TYPE_KEY_CHARACTERISTICS)); |
| 912 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 913 | AuthorizationSet opParams = params; |
| 914 | std::stringstream kcStream; |
| 915 | opParams.Serialize(&kcStream); |
| 916 | if (kcStream.bad()) return ResponseCode::SYSTEM_ERROR; |
| 917 | auto kcBuf = kcStream.str(); |
| 918 | |
| 919 | Blob charBlob(reinterpret_cast<const uint8_t*>(kcBuf.data()), kcBuf.size(), NULL, 0, |
| 920 | ::TYPE_KEY_CHARACTERISTICS); |
| 921 | charBlob.setFallback(usingFallback); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 922 | charBlob.setEncrypted(flags & KEYSTORE_FLAG_ENCRYPTED); |
| 923 | |
| 924 | return mKeyStore->put(cFilename.string(), &charBlob, get_user_id(uid)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 927 | void KeyStoreService::exportKey(const String16& name, KeyFormat format, |
| 928 | const hidl_vec<uint8_t>& clientId, const hidl_vec<uint8_t>& appData, |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 929 | int32_t uid, ExportResult* result) { |
| 930 | |
| 931 | uid_t targetUid = getEffectiveUid(uid); |
| 932 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 933 | if (!is_granted_to(callingUid, targetUid)) { |
| 934 | 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] | 935 | result->resultCode = ResponseCode::PERMISSION_DENIED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 936 | return; |
| 937 | } |
| 938 | |
| 939 | Blob keyBlob; |
| 940 | String8 name8(name); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 941 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 942 | result->resultCode = mKeyStore->getKeyForName(&keyBlob, name8, targetUid, TYPE_KEYMASTER_10); |
| 943 | if (!result->resultCode.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 944 | return; |
| 945 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 946 | |
| 947 | auto key = blob2hidlVec(keyBlob); |
| 948 | auto& dev = mKeyStore->getDevice(keyBlob); |
| 949 | |
| 950 | auto hidlCb = [&](ErrorCode ret, const ::android::hardware::hidl_vec<uint8_t>& keyMaterial) { |
| 951 | result->resultCode = ret; |
| 952 | if (!result->resultCode.isOk()) { |
Ji Wang | 2c14231 | 2016-10-14 17:21:10 +0800 | [diff] [blame] | 953 | return; |
| 954 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 955 | result->exportData = keyMaterial; |
| 956 | }; |
| 957 | KeyStoreServiceReturnCode rc = |
| 958 | KS_HANDLE_HIDL_ERROR(dev->exportKey(format, key, clientId, appData, hidlCb)); |
| 959 | // Overwrite result->resultCode only on HIDL error. Otherwise we want the result set in the |
| 960 | // callback hidlCb. |
| 961 | if (!rc.isOk()) { |
| 962 | result->resultCode = rc; |
Ji Wang | 2c14231 | 2016-10-14 17:21:10 +0800 | [diff] [blame] | 963 | } |
| 964 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 965 | if (result->resultCode == ErrorCode::KEY_REQUIRES_UPGRADE) { |
| 966 | AuthorizationSet upgradeParams; |
| 967 | if (clientId.size()) { |
| 968 | upgradeParams.push_back(TAG_APPLICATION_ID, clientId); |
| 969 | } |
| 970 | if (appData.size()) { |
| 971 | upgradeParams.push_back(TAG_APPLICATION_DATA, appData); |
| 972 | } |
| 973 | result->resultCode = upgradeKeyBlob(name, targetUid, upgradeParams, &keyBlob); |
| 974 | if (!result->resultCode.isOk()) { |
| 975 | return; |
| 976 | } |
| 977 | |
| 978 | auto upgradedHidlKeyBlob = blob2hidlVec(keyBlob); |
| 979 | |
| 980 | result->resultCode = KS_HANDLE_HIDL_ERROR( |
| 981 | dev->exportKey(format, upgradedHidlKeyBlob, clientId, appData, hidlCb)); |
| 982 | if (!result->resultCode.isOk()) { |
| 983 | return; |
| 984 | } |
| 985 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 986 | } |
| 987 | |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 988 | static inline void addAuthTokenToParams(AuthorizationSet* params, const HardwareAuthToken* token) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 989 | if (token) { |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 990 | params->push_back(TAG_AUTH_TOKEN, authToken2HidlVec(*token)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | |
| 994 | void KeyStoreService::begin(const sp<IBinder>& appToken, const String16& name, KeyPurpose purpose, |
| 995 | bool pruneable, const hidl_vec<KeyParameter>& params, |
| 996 | const hidl_vec<uint8_t>& entropy, int32_t uid, |
| 997 | OperationResult* result) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 998 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 999 | uid_t targetUid = getEffectiveUid(uid); |
| 1000 | if (!is_granted_to(callingUid, targetUid)) { |
| 1001 | 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] | 1002 | result->resultCode = ResponseCode::PERMISSION_DENIED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1003 | return; |
| 1004 | } |
| 1005 | if (!pruneable && get_app_id(callingUid) != AID_SYSTEM) { |
| 1006 | ALOGE("Non-system uid %d trying to start non-pruneable operation", callingUid); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1007 | result->resultCode = ResponseCode::PERMISSION_DENIED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1008 | return; |
| 1009 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1010 | if (!checkAllowedOperationParams(params)) { |
| 1011 | result->resultCode = ErrorCode::INVALID_ARGUMENT; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1012 | return; |
| 1013 | } |
| 1014 | Blob keyBlob; |
| 1015 | String8 name8(name); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1016 | result->resultCode = mKeyStore->getKeyForName(&keyBlob, name8, targetUid, TYPE_KEYMASTER_10); |
Shawn Willden | d5a24e6 | 2017-02-28 13:53:24 -0700 | [diff] [blame] | 1017 | if (result->resultCode == ResponseCode::LOCKED && keyBlob.isSuperEncrypted()) { |
| 1018 | result->resultCode = ErrorCode::KEY_USER_NOT_AUTHENTICATED; |
| 1019 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1020 | if (!result->resultCode.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1021 | return; |
| 1022 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1023 | |
| 1024 | auto key = blob2hidlVec(keyBlob); |
| 1025 | auto& dev = mKeyStore->getDevice(keyBlob); |
| 1026 | AuthorizationSet opParams = params; |
| 1027 | KeyCharacteristics characteristics; |
| 1028 | result->resultCode = getOperationCharacteristics(key, &dev, opParams, &characteristics); |
| 1029 | |
| 1030 | if (result->resultCode == ErrorCode::KEY_REQUIRES_UPGRADE) { |
| 1031 | result->resultCode = upgradeKeyBlob(name, targetUid, opParams, &keyBlob); |
| 1032 | if (!result->resultCode.isOk()) { |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 1033 | return; |
| 1034 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1035 | key = blob2hidlVec(keyBlob); |
| 1036 | result->resultCode = getOperationCharacteristics(key, &dev, opParams, &characteristics); |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 1037 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1038 | if (!result->resultCode.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1039 | return; |
| 1040 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1041 | |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1042 | const HardwareAuthToken* authToken = NULL; |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1043 | |
| 1044 | // Merge these characteristics with the ones cached when the key was generated or imported |
| 1045 | Blob charBlob; |
| 1046 | AuthorizationSet persistedCharacteristics; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1047 | result->resultCode = |
| 1048 | mKeyStore->getKeyForName(&charBlob, name8, targetUid, TYPE_KEY_CHARACTERISTICS); |
| 1049 | if (result->resultCode.isOk()) { |
| 1050 | // TODO write one shot stream buffer to avoid copying (twice here) |
| 1051 | std::string charBuffer(reinterpret_cast<const char*>(charBlob.getValue()), |
| 1052 | charBlob.getLength()); |
| 1053 | std::stringstream charStream(charBuffer); |
| 1054 | persistedCharacteristics.Deserialize(&charStream); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1055 | } else { |
| 1056 | ALOGD("Unable to read cached characteristics for key"); |
| 1057 | } |
| 1058 | |
| 1059 | // 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] | 1060 | AuthorizationSet softwareEnforced = characteristics.softwareEnforced; |
| 1061 | AuthorizationSet teeEnforced = characteristics.teeEnforced; |
| 1062 | persistedCharacteristics.Union(softwareEnforced); |
| 1063 | persistedCharacteristics.Subtract(teeEnforced); |
| 1064 | characteristics.softwareEnforced = persistedCharacteristics.hidl_data(); |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1065 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1066 | result->resultCode = getAuthToken(characteristics, 0, purpose, &authToken, |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1067 | /*failOnTokenMissing*/ false); |
| 1068 | // If per-operation auth is needed we need to begin the operation and |
| 1069 | // the client will need to authorize that operation before calling |
| 1070 | // update. Any other auth issues stop here. |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1071 | if (!result->resultCode.isOk() && result->resultCode != ResponseCode::OP_AUTH_NEEDED) return; |
| 1072 | |
| 1073 | addAuthTokenToParams(&opParams, authToken); |
| 1074 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1075 | // Add entropy to the device first. |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1076 | if (entropy.size()) { |
| 1077 | result->resultCode = addRngEntropy(entropy); |
| 1078 | if (!result->resultCode.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1079 | return; |
| 1080 | } |
| 1081 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1082 | |
| 1083 | // Create a keyid for this key. |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1084 | km_id_t keyid; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1085 | if (!enforcement_policy.CreateKeyId(key, &keyid)) { |
| 1086 | ALOGE("Failed to create a key ID for authorization checking."); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1087 | result->resultCode = ErrorCode::UNKNOWN_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1088 | return; |
| 1089 | } |
| 1090 | |
| 1091 | // Check that all key authorization policy requirements are met. |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1092 | AuthorizationSet key_auths = characteristics.teeEnforced; |
| 1093 | key_auths.append(&characteristics.softwareEnforced[0], |
| 1094 | &characteristics.softwareEnforced[characteristics.softwareEnforced.size()]); |
| 1095 | |
| 1096 | result->resultCode = enforcement_policy.AuthorizeOperation( |
| 1097 | purpose, keyid, key_auths, opParams, 0 /* op_handle */, true /* is_begin_operation */); |
| 1098 | if (!result->resultCode.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1099 | return; |
| 1100 | } |
| 1101 | |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1102 | // 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] | 1103 | // pruneable. |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1104 | while (mOperationMap.getOperationCount() >= kMaxOperations) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1105 | ALOGD("Reached or exceeded concurrent operations limit"); |
| 1106 | if (!pruneOperation()) { |
| 1107 | break; |
| 1108 | } |
| 1109 | } |
| 1110 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1111 | auto hidlCb = [&](ErrorCode ret, const hidl_vec<KeyParameter>& outParams, |
| 1112 | uint64_t operationHandle) { |
| 1113 | result->resultCode = ret; |
| 1114 | if (!result->resultCode.isOk()) { |
| 1115 | return; |
| 1116 | } |
| 1117 | result->handle = operationHandle; |
| 1118 | result->outParams = outParams; |
| 1119 | }; |
| 1120 | |
| 1121 | ErrorCode rc = KS_HANDLE_HIDL_ERROR(dev->begin(purpose, key, opParams.hidl_data(), hidlCb)); |
| 1122 | if (rc != ErrorCode::OK) { |
| 1123 | ALOGW("Got error %d from begin()", rc); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | // If there are too many operations abort the oldest operation that was |
| 1127 | // started as pruneable and try again. |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1128 | while (rc == ErrorCode::TOO_MANY_OPERATIONS && mOperationMap.hasPruneableOperation()) { |
| 1129 | ALOGW("Ran out of operation handles"); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1130 | if (!pruneOperation()) { |
| 1131 | break; |
| 1132 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1133 | 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] | 1134 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1135 | if (rc != ErrorCode::OK) { |
| 1136 | result->resultCode = rc; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1137 | return; |
| 1138 | } |
| 1139 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1140 | // Note: The operation map takes possession of the contents of "characteristics". |
| 1141 | // It is safe to use characteristics after the following line but it will be empty. |
| 1142 | sp<IBinder> operationToken = mOperationMap.addOperation( |
| 1143 | result->handle, keyid, purpose, dev, appToken, std::move(characteristics), pruneable); |
| 1144 | assert(characteristics.teeEnforced.size() == 0); |
| 1145 | assert(characteristics.softwareEnforced.size() == 0); |
| 1146 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1147 | if (authToken) { |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1148 | mOperationMap.setOperationAuthToken(operationToken, authToken); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1149 | } |
| 1150 | // Return the authentication lookup result. If this is a per operation |
| 1151 | // auth'd key then the resultCode will be ::OP_AUTH_NEEDED and the |
| 1152 | // application should get an auth token using the handle before the |
| 1153 | // first call to update, which will fail if keystore hasn't received the |
| 1154 | // auth token. |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1155 | // All fields but "token" were set in the begin operation's callback. |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1156 | result->token = operationToken; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1159 | void KeyStoreService::update(const sp<IBinder>& token, const hidl_vec<KeyParameter>& params, |
| 1160 | const hidl_vec<uint8_t>& data, OperationResult* result) { |
| 1161 | if (!checkAllowedOperationParams(params)) { |
| 1162 | result->resultCode = ErrorCode::INVALID_ARGUMENT; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1163 | return; |
| 1164 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1165 | km_device_t dev; |
| 1166 | uint64_t handle; |
| 1167 | KeyPurpose purpose; |
| 1168 | km_id_t keyid; |
| 1169 | const KeyCharacteristics* characteristics; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1170 | if (!mOperationMap.getOperation(token, &handle, &keyid, &purpose, &dev, &characteristics)) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1171 | result->resultCode = ErrorCode::INVALID_OPERATION_HANDLE; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1172 | return; |
| 1173 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1174 | AuthorizationSet opParams = params; |
| 1175 | result->resultCode = addOperationAuthTokenIfNeeded(token, &opParams); |
| 1176 | if (!result->resultCode.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1177 | return; |
| 1178 | } |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1179 | |
| 1180 | // Check that all key authorization policy requirements are met. |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1181 | AuthorizationSet key_auths(characteristics->teeEnforced); |
| 1182 | key_auths.append(&characteristics->softwareEnforced[0], |
| 1183 | &characteristics->softwareEnforced[characteristics->softwareEnforced.size()]); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1184 | result->resultCode = enforcement_policy.AuthorizeOperation( |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1185 | purpose, keyid, key_auths, opParams, handle, false /* is_begin_operation */); |
| 1186 | if (!result->resultCode.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1187 | return; |
| 1188 | } |
| 1189 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1190 | auto hidlCb = [&](ErrorCode ret, uint32_t inputConsumed, |
| 1191 | const hidl_vec<KeyParameter>& outParams, const hidl_vec<uint8_t>& output) { |
| 1192 | result->resultCode = ret; |
| 1193 | if (!result->resultCode.isOk()) { |
| 1194 | return; |
| 1195 | } |
| 1196 | result->inputConsumed = inputConsumed; |
| 1197 | result->outParams = outParams; |
| 1198 | result->data = output; |
| 1199 | }; |
| 1200 | |
Janis Danisevskis | b0245ee | 2017-01-25 15:43:01 +0000 | [diff] [blame] | 1201 | KeyStoreServiceReturnCode rc = KS_HANDLE_HIDL_ERROR(dev->update(handle, opParams.hidl_data(), |
| 1202 | data, hidlCb)); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1203 | // just a reminder: on success result->resultCode was set in the callback. So we only overwrite |
| 1204 | // it if there was a communication error indicated by the ErrorCode. |
| 1205 | if (!rc.isOk()) { |
| 1206 | result->resultCode = rc; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1207 | } |
| 1208 | } |
| 1209 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1210 | void KeyStoreService::finish(const sp<IBinder>& token, const hidl_vec<KeyParameter>& params, |
| 1211 | const hidl_vec<uint8_t>& signature, const hidl_vec<uint8_t>& entropy, |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1212 | OperationResult* result) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1213 | if (!checkAllowedOperationParams(params)) { |
| 1214 | result->resultCode = ErrorCode::INVALID_ARGUMENT; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1215 | return; |
| 1216 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1217 | km_device_t dev; |
| 1218 | uint64_t handle; |
| 1219 | KeyPurpose purpose; |
| 1220 | km_id_t keyid; |
| 1221 | const KeyCharacteristics* characteristics; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1222 | if (!mOperationMap.getOperation(token, &handle, &keyid, &purpose, &dev, &characteristics)) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1223 | result->resultCode = ErrorCode::INVALID_OPERATION_HANDLE; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1224 | return; |
| 1225 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1226 | AuthorizationSet opParams = params; |
| 1227 | result->resultCode = addOperationAuthTokenIfNeeded(token, &opParams); |
| 1228 | if (!result->resultCode.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1229 | return; |
| 1230 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1231 | |
| 1232 | if (entropy.size()) { |
| 1233 | result->resultCode = addRngEntropy(entropy); |
| 1234 | if (!result->resultCode.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1235 | return; |
| 1236 | } |
| 1237 | } |
| 1238 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1239 | // Check that all key authorization policy requirements are met. |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1240 | AuthorizationSet key_auths(characteristics->teeEnforced); |
| 1241 | key_auths.append(&characteristics->softwareEnforced[0], |
| 1242 | &characteristics->softwareEnforced[characteristics->softwareEnforced.size()]); |
| 1243 | result->resultCode = enforcement_policy.AuthorizeOperation( |
| 1244 | purpose, keyid, key_auths, opParams, handle, false /* is_begin_operation */); |
| 1245 | if (!result->resultCode.isOk()) return; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1246 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1247 | auto hidlCb = [&](ErrorCode ret, const hidl_vec<KeyParameter>& outParams, |
| 1248 | const hidl_vec<uint8_t>& output) { |
| 1249 | result->resultCode = ret; |
| 1250 | if (!result->resultCode.isOk()) { |
| 1251 | return; |
| 1252 | } |
| 1253 | result->outParams = outParams; |
| 1254 | result->data = output; |
| 1255 | }; |
| 1256 | |
| 1257 | KeyStoreServiceReturnCode rc = KS_HANDLE_HIDL_ERROR(dev->finish( |
| 1258 | handle, opParams.hidl_data(), |
| 1259 | 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] | 1260 | // Remove the operation regardless of the result |
| 1261 | mOperationMap.removeOperation(token); |
| 1262 | mAuthTokenTable.MarkCompleted(handle); |
| 1263 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1264 | // just a reminder: on success result->resultCode was set in the callback. So we only overwrite |
| 1265 | // it if there was a communication error indicated by the ErrorCode. |
| 1266 | if (!rc.isOk()) { |
| 1267 | result->resultCode = rc; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1268 | } |
| 1269 | } |
| 1270 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1271 | KeyStoreServiceReturnCode KeyStoreService::abort(const sp<IBinder>& token) { |
| 1272 | km_device_t dev; |
| 1273 | uint64_t handle; |
| 1274 | KeyPurpose purpose; |
| 1275 | km_id_t keyid; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1276 | if (!mOperationMap.getOperation(token, &handle, &keyid, &purpose, &dev, NULL)) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1277 | return ErrorCode::INVALID_OPERATION_HANDLE; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1278 | } |
| 1279 | mOperationMap.removeOperation(token); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1280 | |
| 1281 | ErrorCode rc = KS_HANDLE_HIDL_ERROR(dev->abort(handle)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1282 | mAuthTokenTable.MarkCompleted(handle); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1283 | return rc; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | bool KeyStoreService::isOperationAuthorized(const sp<IBinder>& token) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1287 | km_device_t dev; |
| 1288 | uint64_t handle; |
| 1289 | const KeyCharacteristics* characteristics; |
| 1290 | KeyPurpose purpose; |
| 1291 | km_id_t keyid; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1292 | if (!mOperationMap.getOperation(token, &handle, &keyid, &purpose, &dev, &characteristics)) { |
| 1293 | return false; |
| 1294 | } |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1295 | const HardwareAuthToken* authToken = NULL; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1296 | mOperationMap.getOperationAuthToken(token, &authToken); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1297 | AuthorizationSet ignored; |
| 1298 | auto authResult = addOperationAuthTokenIfNeeded(token, &ignored); |
| 1299 | return authResult.isOk(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1302 | KeyStoreServiceReturnCode KeyStoreService::addAuthToken(const uint8_t* token, size_t length) { |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1303 | // TODO(swillden): When gatekeeper and fingerprint are ready, this should be updated to |
| 1304 | // receive a HardwareAuthToken, rather than an opaque byte array. |
| 1305 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1306 | if (!checkBinderPermission(P_ADD_AUTH)) { |
| 1307 | ALOGW("addAuthToken: permission denied for %d", IPCThreadState::self()->getCallingUid()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1308 | return ResponseCode::PERMISSION_DENIED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1309 | } |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1310 | if (length != sizeof(hw_auth_token_t)) { |
| 1311 | return ErrorCode::INVALID_ARGUMENT; |
| 1312 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1313 | |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1314 | hw_auth_token_t authToken; |
| 1315 | memcpy(reinterpret_cast<void*>(&authToken), token, sizeof(hw_auth_token_t)); |
| 1316 | if (authToken.version != 0) { |
| 1317 | return ErrorCode::INVALID_ARGUMENT; |
| 1318 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1319 | |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1320 | std::unique_ptr<HardwareAuthToken> hidlAuthToken(new HardwareAuthToken); |
| 1321 | hidlAuthToken->challenge = authToken.challenge; |
| 1322 | hidlAuthToken->userId = authToken.user_id; |
| 1323 | hidlAuthToken->authenticatorId = authToken.authenticator_id; |
| 1324 | hidlAuthToken->authenticatorType = authToken.authenticator_type; |
| 1325 | hidlAuthToken->timestamp = authToken.timestamp; |
| 1326 | static_assert( |
| 1327 | std::is_same<decltype(hidlAuthToken->hmac), |
| 1328 | ::android::hardware::hidl_array<uint8_t, sizeof(authToken.hmac)>>::value, |
| 1329 | "This function assumes token HMAC is 32 bytes, but it might not be."); |
| 1330 | std::copy(authToken.hmac, authToken.hmac + sizeof(authToken.hmac), hidlAuthToken->hmac.data()); |
| 1331 | |
| 1332 | // The table takes ownership of authToken. |
| 1333 | mAuthTokenTable.AddAuthenticationToken(hidlAuthToken.release()); |
| 1334 | return ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1337 | bool isDeviceIdAttestationRequested(const hidl_vec<KeyParameter>& params) { |
| 1338 | for (size_t i = 0; i < params.size(); ++i) { |
| 1339 | switch (params[i].tag) { |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1340 | case Tag::ATTESTATION_ID_BRAND: |
| 1341 | case Tag::ATTESTATION_ID_DEVICE: |
| 1342 | case Tag::ATTESTATION_ID_IMEI: |
| 1343 | case Tag::ATTESTATION_ID_MANUFACTURER: |
| 1344 | case Tag::ATTESTATION_ID_MEID: |
| 1345 | case Tag::ATTESTATION_ID_MODEL: |
| 1346 | case Tag::ATTESTATION_ID_PRODUCT: |
| 1347 | case Tag::ATTESTATION_ID_SERIAL: |
| 1348 | return true; |
| 1349 | default: |
| 1350 | break; |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | return false; |
| 1354 | } |
| 1355 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1356 | KeyStoreServiceReturnCode KeyStoreService::attestKey(const String16& name, |
| 1357 | const hidl_vec<KeyParameter>& params, |
| 1358 | hidl_vec<hidl_vec<uint8_t>>* outChain) { |
| 1359 | if (!outChain) { |
| 1360 | return ErrorCode::OUTPUT_PARAMETER_NULL; |
| 1361 | } |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1362 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1363 | if (!checkAllowedOperationParams(params)) { |
| 1364 | return ErrorCode::INVALID_ARGUMENT; |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame^] | 1367 | if (isDeviceIdAttestationRequested(params)) { |
| 1368 | // There is a dedicated attestDeviceIds() method for device ID attestation. |
| 1369 | return ErrorCode::INVALID_ARGUMENT; |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1370 | } |
| 1371 | |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame^] | 1372 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1373 | |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1374 | AuthorizationSet mutableParams = params; |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame^] | 1375 | KeyStoreServiceReturnCode rc = updateParamsForAttestation(callingUid, &mutableParams); |
| 1376 | if (!rc.isOk()) { |
| 1377 | return rc; |
| 1378 | } |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1379 | |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1380 | Blob keyBlob; |
| 1381 | String8 name8(name); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame^] | 1382 | rc = mKeyStore->getKeyForName(&keyBlob, name8, callingUid, TYPE_KEYMASTER_10); |
| 1383 | if (!rc.isOk()) { |
| 1384 | return rc; |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1385 | } |
| 1386 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1387 | KeyStoreServiceReturnCode error; |
| 1388 | auto hidlCb = [&](ErrorCode ret, const hidl_vec<hidl_vec<uint8_t>>& certChain) { |
| 1389 | error = ret; |
| 1390 | if (!error.isOk()) { |
| 1391 | return; |
| 1392 | } |
| 1393 | if (outChain) *outChain = certChain; |
| 1394 | }; |
| 1395 | |
| 1396 | auto hidlKey = blob2hidlVec(keyBlob); |
| 1397 | auto& dev = mKeyStore->getDevice(keyBlob); |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame^] | 1398 | rc = KS_HANDLE_HIDL_ERROR(dev->attestKey(hidlKey, mutableParams.hidl_data(), hidlCb)); |
| 1399 | if (!rc.isOk()) { |
| 1400 | return rc; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1401 | } |
Bartosz Fabianowski | 5aa93e0 | 2017-04-24 13:54:49 +0200 | [diff] [blame^] | 1402 | return error; |
| 1403 | } |
| 1404 | |
| 1405 | KeyStoreServiceReturnCode KeyStoreService::attestDeviceIds(const hidl_vec<KeyParameter>& params, |
| 1406 | hidl_vec<hidl_vec<uint8_t>>* outChain) { |
| 1407 | if (!outChain) { |
| 1408 | return ErrorCode::OUTPUT_PARAMETER_NULL; |
| 1409 | } |
| 1410 | |
| 1411 | if (!checkAllowedOperationParams(params)) { |
| 1412 | return ErrorCode::INVALID_ARGUMENT; |
| 1413 | } |
| 1414 | |
| 1415 | if (!isDeviceIdAttestationRequested(params)) { |
| 1416 | // There is an attestKey() method for attesting keys without device ID attestation. |
| 1417 | return ErrorCode::INVALID_ARGUMENT; |
| 1418 | } |
| 1419 | |
| 1420 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1421 | sp<IBinder> binder = defaultServiceManager()->getService(String16("permission")); |
| 1422 | if (binder == 0) { |
| 1423 | return ErrorCode::CANNOT_ATTEST_IDS; |
| 1424 | } |
| 1425 | if (!interface_cast<IPermissionController>(binder)->checkPermission( |
| 1426 | String16("android.permission.READ_PRIVILEGED_PHONE_STATE"), |
| 1427 | IPCThreadState::self()->getCallingPid(), callingUid)) { |
| 1428 | return ErrorCode::CANNOT_ATTEST_IDS; |
| 1429 | } |
| 1430 | |
| 1431 | AuthorizationSet mutableParams = params; |
| 1432 | KeyStoreServiceReturnCode rc = updateParamsForAttestation(callingUid, &mutableParams); |
| 1433 | if (!rc.isOk()) { |
| 1434 | return rc; |
| 1435 | } |
| 1436 | |
| 1437 | // Generate temporary key. |
| 1438 | auto& dev = mKeyStore->getDevice(); |
| 1439 | KeyStoreServiceReturnCode error; |
| 1440 | hidl_vec<uint8_t> hidlKey; |
| 1441 | |
| 1442 | AuthorizationSet keyCharacteristics; |
| 1443 | keyCharacteristics.push_back(TAG_PURPOSE, KeyPurpose::VERIFY); |
| 1444 | keyCharacteristics.push_back(TAG_ALGORITHM, Algorithm::EC); |
| 1445 | keyCharacteristics.push_back(TAG_DIGEST, Digest::SHA_2_256); |
| 1446 | keyCharacteristics.push_back(TAG_NO_AUTH_REQUIRED); |
| 1447 | keyCharacteristics.push_back(TAG_EC_CURVE, EcCurve::P_256); |
| 1448 | auto generateHidlCb = [&](ErrorCode ret, const hidl_vec<uint8_t>& hidlKeyBlob, |
| 1449 | const KeyCharacteristics&) { |
| 1450 | error = ret; |
| 1451 | if (!error.isOk()) { |
| 1452 | return; |
| 1453 | } |
| 1454 | hidlKey = hidlKeyBlob; |
| 1455 | }; |
| 1456 | |
| 1457 | rc = KS_HANDLE_HIDL_ERROR(dev->generateKey(keyCharacteristics.hidl_data(), generateHidlCb)); |
| 1458 | if (!rc.isOk()) { |
| 1459 | return rc; |
| 1460 | } |
| 1461 | if (!error.isOk()) { |
| 1462 | return error; |
| 1463 | } |
| 1464 | |
| 1465 | // Attest key and device IDs. |
| 1466 | auto attestHidlCb = [&](ErrorCode ret, const hidl_vec<hidl_vec<uint8_t>>& certChain) { |
| 1467 | error = ret; |
| 1468 | if (!error.isOk()) { |
| 1469 | return; |
| 1470 | } |
| 1471 | *outChain = certChain; |
| 1472 | }; |
| 1473 | KeyStoreServiceReturnCode attestationRc = |
| 1474 | KS_HANDLE_HIDL_ERROR(dev->attestKey(hidlKey, mutableParams.hidl_data(), attestHidlCb)); |
| 1475 | |
| 1476 | // Delete temporary key. |
| 1477 | KeyStoreServiceReturnCode deletionRc = KS_HANDLE_HIDL_ERROR(dev->deleteKey(hidlKey)); |
Bartosz Fabianowski | a9452d9 | 2017-01-23 22:21:11 +0100 | [diff] [blame] | 1478 | |
| 1479 | if (!attestationRc.isOk()) { |
| 1480 | return attestationRc; |
| 1481 | } |
| 1482 | if (!error.isOk()) { |
| 1483 | return error; |
| 1484 | } |
| 1485 | return deletionRc; |
Shawn Willden | 50eb1b2 | 2016-01-21 12:41:23 -0700 | [diff] [blame] | 1486 | } |
| 1487 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1488 | KeyStoreServiceReturnCode KeyStoreService::onDeviceOffBody() { |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1489 | // TODO(tuckeris): add permission check. This should be callable from ClockworkHome only. |
| 1490 | mAuthTokenTable.onDeviceOffBody(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1491 | return ResponseCode::NO_ERROR; |
Tucker Sylvestro | 0ab28b7 | 2016-08-05 18:02:47 -0400 | [diff] [blame] | 1492 | } |
| 1493 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1494 | /** |
| 1495 | * Prune the oldest pruneable operation. |
| 1496 | */ |
| 1497 | bool KeyStoreService::pruneOperation() { |
| 1498 | sp<IBinder> oldest = mOperationMap.getOldestPruneableOperation(); |
| 1499 | ALOGD("Trying to prune operation %p", oldest.get()); |
| 1500 | size_t op_count_before_abort = mOperationMap.getOperationCount(); |
| 1501 | // We mostly ignore errors from abort() because all we care about is whether at least |
| 1502 | // one operation has been removed. |
| 1503 | int abort_error = abort(oldest); |
| 1504 | if (mOperationMap.getOperationCount() >= op_count_before_abort) { |
| 1505 | ALOGE("Failed to abort pruneable operation %p, error: %d", oldest.get(), abort_error); |
| 1506 | return false; |
| 1507 | } |
| 1508 | return true; |
| 1509 | } |
| 1510 | |
| 1511 | /** |
| 1512 | * Get the effective target uid for a binder operation that takes an |
| 1513 | * optional uid as the target. |
| 1514 | */ |
| 1515 | uid_t KeyStoreService::getEffectiveUid(int32_t targetUid) { |
| 1516 | if (targetUid == UID_SELF) { |
| 1517 | return IPCThreadState::self()->getCallingUid(); |
| 1518 | } |
| 1519 | return static_cast<uid_t>(targetUid); |
| 1520 | } |
| 1521 | |
| 1522 | /** |
| 1523 | * Check if the caller of the current binder method has the required |
| 1524 | * permission and if acting on other uids the grants to do so. |
| 1525 | */ |
| 1526 | bool KeyStoreService::checkBinderPermission(perm_t permission, int32_t targetUid) { |
| 1527 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1528 | pid_t spid = IPCThreadState::self()->getCallingPid(); |
| 1529 | if (!has_permission(callingUid, permission, spid)) { |
| 1530 | ALOGW("permission %s denied for %d", get_perm_label(permission), callingUid); |
| 1531 | return false; |
| 1532 | } |
| 1533 | if (!is_granted_to(callingUid, getEffectiveUid(targetUid))) { |
| 1534 | ALOGW("uid %d not granted to act for %d", callingUid, targetUid); |
| 1535 | return false; |
| 1536 | } |
| 1537 | return true; |
| 1538 | } |
| 1539 | |
| 1540 | /** |
| 1541 | * Check if the caller of the current binder method has the required |
| 1542 | * permission and the target uid is the caller or the caller is system. |
| 1543 | */ |
| 1544 | bool KeyStoreService::checkBinderPermissionSelfOrSystem(perm_t permission, int32_t targetUid) { |
| 1545 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1546 | pid_t spid = IPCThreadState::self()->getCallingPid(); |
| 1547 | if (!has_permission(callingUid, permission, spid)) { |
| 1548 | ALOGW("permission %s denied for %d", get_perm_label(permission), callingUid); |
| 1549 | return false; |
| 1550 | } |
| 1551 | return getEffectiveUid(targetUid) == callingUid || callingUid == AID_SYSTEM; |
| 1552 | } |
| 1553 | |
| 1554 | /** |
| 1555 | * Check if the caller of the current binder method has the required |
| 1556 | * permission or the target of the operation is the caller's uid. This is |
| 1557 | * for operation where the permission is only for cross-uid activity and all |
| 1558 | * uids are allowed to act on their own (ie: clearing all entries for a |
| 1559 | * given uid). |
| 1560 | */ |
| 1561 | bool KeyStoreService::checkBinderPermissionOrSelfTarget(perm_t permission, int32_t targetUid) { |
| 1562 | uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 1563 | if (getEffectiveUid(targetUid) == callingUid) { |
| 1564 | return true; |
| 1565 | } else { |
| 1566 | return checkBinderPermission(permission, targetUid); |
| 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | /** |
| 1571 | * Helper method to check that the caller has the required permission as |
| 1572 | * well as the keystore is in the unlocked state if checkUnlocked is true. |
| 1573 | * |
| 1574 | * Returns NO_ERROR on success, PERMISSION_DENIED on a permission error and |
| 1575 | * otherwise the state of keystore when not unlocked and checkUnlocked is |
| 1576 | * true. |
| 1577 | */ |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1578 | KeyStoreServiceReturnCode |
| 1579 | KeyStoreService::checkBinderPermissionAndKeystoreState(perm_t permission, int32_t targetUid, |
| 1580 | bool checkUnlocked) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1581 | if (!checkBinderPermission(permission, targetUid)) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1582 | return ResponseCode::PERMISSION_DENIED; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1583 | } |
| 1584 | State state = mKeyStore->getState(get_user_id(getEffectiveUid(targetUid))); |
| 1585 | if (checkUnlocked && !isKeystoreUnlocked(state)) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1586 | // All State values coincide with ResponseCodes |
| 1587 | return static_cast<ResponseCode>(state); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1588 | } |
| 1589 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1590 | return ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | bool KeyStoreService::isKeystoreUnlocked(State state) { |
| 1594 | switch (state) { |
| 1595 | case ::STATE_NO_ERROR: |
| 1596 | return true; |
| 1597 | case ::STATE_UNINITIALIZED: |
| 1598 | case ::STATE_LOCKED: |
| 1599 | return false; |
| 1600 | } |
| 1601 | return false; |
| 1602 | } |
| 1603 | |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1604 | /** |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1605 | * Check that all KeyParameter's provided by the application are |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1606 | * allowed. Any parameter that keystore adds itself should be disallowed here. |
| 1607 | */ |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1608 | bool KeyStoreService::checkAllowedOperationParams(const hidl_vec<KeyParameter>& params) { |
| 1609 | for (size_t i = 0; i < params.size(); ++i) { |
| 1610 | switch (params[i].tag) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1611 | case Tag::ATTESTATION_APPLICATION_ID: |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1612 | case Tag::AUTH_TOKEN: |
| 1613 | case Tag::RESET_SINCE_ID_ROTATION: |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1614 | return false; |
| 1615 | default: |
| 1616 | break; |
| 1617 | } |
| 1618 | } |
| 1619 | return true; |
| 1620 | } |
| 1621 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1622 | ErrorCode KeyStoreService::getOperationCharacteristics(const hidl_vec<uint8_t>& key, |
| 1623 | km_device_t* dev, |
| 1624 | const AuthorizationSet& params, |
| 1625 | KeyCharacteristics* out) { |
| 1626 | hidl_vec<uint8_t> appId; |
| 1627 | hidl_vec<uint8_t> appData; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1628 | for (auto param : params) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1629 | if (param.tag == Tag::APPLICATION_ID) { |
| 1630 | appId = authorizationValue(TAG_APPLICATION_ID, param).value(); |
| 1631 | } else if (param.tag == Tag::APPLICATION_DATA) { |
| 1632 | appData = authorizationValue(TAG_APPLICATION_DATA, param).value(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1633 | } |
| 1634 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1635 | ErrorCode error = ErrorCode::OK; |
| 1636 | |
| 1637 | auto hidlCb = [&](ErrorCode ret, const KeyCharacteristics& keyCharacteristics) { |
| 1638 | error = ret; |
| 1639 | if (error != ErrorCode::OK) { |
| 1640 | return; |
| 1641 | } |
| 1642 | if (out) *out = keyCharacteristics; |
| 1643 | }; |
| 1644 | |
| 1645 | ErrorCode rc = KS_HANDLE_HIDL_ERROR((*dev)->getKeyCharacteristics(key, appId, appData, hidlCb)); |
| 1646 | if (rc != ErrorCode::OK) { |
| 1647 | return rc; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1648 | } |
| 1649 | return error; |
| 1650 | } |
| 1651 | |
| 1652 | /** |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1653 | * Get the auth token for this operation from the auth token table. |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1654 | * |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1655 | * Returns ResponseCode::NO_ERROR if the auth token was set or none was required. |
| 1656 | * ::OP_AUTH_NEEDED if it is a per op authorization, no |
| 1657 | * authorization token exists for that operation and |
| 1658 | * failOnTokenMissing is false. |
| 1659 | * KM_ERROR_KEY_USER_NOT_AUTHENTICATED if there is no valid auth |
| 1660 | * token for the operation |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1661 | */ |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1662 | KeyStoreServiceReturnCode KeyStoreService::getAuthToken(const KeyCharacteristics& characteristics, |
| 1663 | uint64_t handle, KeyPurpose purpose, |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1664 | const HardwareAuthToken** authToken, |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1665 | bool failOnTokenMissing) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1666 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1667 | AuthorizationSet allCharacteristics; |
| 1668 | for (size_t i = 0; i < characteristics.softwareEnforced.size(); i++) { |
| 1669 | allCharacteristics.push_back(characteristics.softwareEnforced[i]); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1670 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1671 | for (size_t i = 0; i < characteristics.teeEnforced.size(); i++) { |
| 1672 | allCharacteristics.push_back(characteristics.teeEnforced[i]); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1673 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1674 | AuthTokenTable::Error err = |
| 1675 | mAuthTokenTable.FindAuthorization(allCharacteristics, purpose, handle, authToken); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1676 | switch (err) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1677 | case AuthTokenTable::OK: |
| 1678 | case AuthTokenTable::AUTH_NOT_REQUIRED: |
| 1679 | return ResponseCode::NO_ERROR; |
| 1680 | case AuthTokenTable::AUTH_TOKEN_NOT_FOUND: |
| 1681 | case AuthTokenTable::AUTH_TOKEN_EXPIRED: |
| 1682 | case AuthTokenTable::AUTH_TOKEN_WRONG_SID: |
| 1683 | return ErrorCode::KEY_USER_NOT_AUTHENTICATED; |
| 1684 | case AuthTokenTable::OP_HANDLE_REQUIRED: |
| 1685 | return failOnTokenMissing ? KeyStoreServiceReturnCode(ErrorCode::KEY_USER_NOT_AUTHENTICATED) |
| 1686 | : KeyStoreServiceReturnCode(ResponseCode::OP_AUTH_NEEDED); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1687 | default: |
| 1688 | ALOGE("Unexpected FindAuthorization return value %d", err); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1689 | return ErrorCode::INVALID_ARGUMENT; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | /** |
| 1694 | * Add the auth token for the operation to the param list if the operation |
| 1695 | * requires authorization. Uses the cached result in the OperationMap if available |
| 1696 | * otherwise gets the token from the AuthTokenTable and caches the result. |
| 1697 | * |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1698 | * 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] | 1699 | * KM_ERROR_KEY_USER_NOT_AUTHENTICATED if the operation is not |
| 1700 | * authenticated. |
| 1701 | * KM_ERROR_INVALID_OPERATION_HANDLE if token is not a valid |
| 1702 | * operation token. |
| 1703 | */ |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1704 | KeyStoreServiceReturnCode KeyStoreService::addOperationAuthTokenIfNeeded(const sp<IBinder>& token, |
| 1705 | AuthorizationSet* params) { |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1706 | const HardwareAuthToken* authToken = nullptr; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1707 | mOperationMap.getOperationAuthToken(token, &authToken); |
| 1708 | if (!authToken) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1709 | km_device_t dev; |
| 1710 | uint64_t handle; |
| 1711 | const KeyCharacteristics* characteristics = nullptr; |
| 1712 | KeyPurpose purpose; |
| 1713 | km_id_t keyid; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1714 | if (!mOperationMap.getOperation(token, &handle, &keyid, &purpose, &dev, &characteristics)) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1715 | return ErrorCode::INVALID_OPERATION_HANDLE; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1716 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1717 | auto result = getAuthToken(*characteristics, handle, purpose, &authToken); |
| 1718 | if (!result.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1719 | return result; |
| 1720 | } |
| 1721 | if (authToken) { |
Shawn Willden | d3ed3a2 | 2017-03-28 00:39:16 +0000 | [diff] [blame] | 1722 | mOperationMap.setOperationAuthToken(token, authToken); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1723 | } |
| 1724 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1725 | addAuthTokenToParams(params, authToken); |
| 1726 | return ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | /** |
| 1730 | * Translate a result value to a legacy return value. All keystore errors are |
| 1731 | * preserved and keymaster errors become SYSTEM_ERRORs |
| 1732 | */ |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1733 | KeyStoreServiceReturnCode KeyStoreService::translateResultToLegacyResult(int32_t result) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1734 | if (result > 0) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1735 | return static_cast<ResponseCode>(result); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1736 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1737 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1738 | } |
| 1739 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1740 | static NullOr<const Algorithm&> |
| 1741 | getKeyAlgoritmFromKeyCharacteristics(const KeyCharacteristics& characteristics) { |
| 1742 | for (size_t i = 0; i < characteristics.teeEnforced.size(); ++i) { |
| 1743 | auto algo = authorizationValue(TAG_ALGORITHM, characteristics.teeEnforced[i]); |
| 1744 | if (algo.isOk()) return algo.value(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1745 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1746 | for (size_t i = 0; i < characteristics.softwareEnforced.size(); ++i) { |
| 1747 | auto algo = authorizationValue(TAG_ALGORITHM, characteristics.softwareEnforced[i]); |
| 1748 | if (algo.isOk()) return algo.value(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1749 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1750 | return {}; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1751 | } |
| 1752 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1753 | void KeyStoreService::addLegacyBeginParams(const String16& name, AuthorizationSet* params) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1754 | // All legacy keys are DIGEST_NONE/PAD_NONE. |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1755 | params->push_back(TAG_DIGEST, Digest::NONE); |
| 1756 | params->push_back(TAG_PADDING, PaddingMode::NONE); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1757 | |
| 1758 | // Look up the algorithm of the key. |
| 1759 | KeyCharacteristics characteristics; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1760 | auto rc = getKeyCharacteristics(name, hidl_vec<uint8_t>(), hidl_vec<uint8_t>(), UID_SELF, |
| 1761 | &characteristics); |
| 1762 | if (!rc.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1763 | ALOGE("Failed to get key characteristics"); |
| 1764 | return; |
| 1765 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1766 | auto algorithm = getKeyAlgoritmFromKeyCharacteristics(characteristics); |
| 1767 | if (!algorithm.isOk()) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1768 | ALOGE("getKeyCharacteristics did not include KM_TAG_ALGORITHM"); |
| 1769 | return; |
| 1770 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1771 | params->push_back(TAG_ALGORITHM, algorithm.value()); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1772 | } |
| 1773 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1774 | KeyStoreServiceReturnCode KeyStoreService::doLegacySignVerify(const String16& name, |
| 1775 | const hidl_vec<uint8_t>& data, |
| 1776 | hidl_vec<uint8_t>* out, |
| 1777 | const hidl_vec<uint8_t>& signature, |
| 1778 | KeyPurpose purpose) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1779 | |
| 1780 | std::basic_stringstream<uint8_t> outBuffer; |
| 1781 | OperationResult result; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1782 | AuthorizationSet inArgs; |
| 1783 | addLegacyBeginParams(name, &inArgs); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1784 | sp<IBinder> appToken(new BBinder); |
| 1785 | sp<IBinder> token; |
| 1786 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1787 | begin(appToken, name, purpose, true, inArgs.hidl_data(), hidl_vec<uint8_t>(), UID_SELF, |
| 1788 | &result); |
| 1789 | if (!result.resultCode.isOk()) { |
| 1790 | if (result.resultCode == ResponseCode::KEY_NOT_FOUND) { |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1791 | ALOGW("Key not found"); |
| 1792 | } else { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1793 | ALOGW("Error in begin: %d", int32_t(result.resultCode)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1794 | } |
| 1795 | return translateResultToLegacyResult(result.resultCode); |
| 1796 | } |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1797 | inArgs.Clear(); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1798 | token = result.token; |
| 1799 | size_t consumed = 0; |
| 1800 | size_t lastConsumed = 0; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1801 | hidl_vec<uint8_t> data_view; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1802 | do { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1803 | data_view.setToExternal(const_cast<uint8_t*>(&data[consumed]), data.size() - consumed); |
| 1804 | update(token, inArgs.hidl_data(), data_view, &result); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1805 | if (result.resultCode != ResponseCode::NO_ERROR) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1806 | ALOGW("Error in update: %d", int32_t(result.resultCode)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1807 | return translateResultToLegacyResult(result.resultCode); |
| 1808 | } |
| 1809 | if (out) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1810 | outBuffer.write(&result.data[0], result.data.size()); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1811 | } |
| 1812 | lastConsumed = result.inputConsumed; |
| 1813 | consumed += lastConsumed; |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1814 | } while (consumed < data.size() && lastConsumed > 0); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1815 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1816 | if (consumed != data.size()) { |
| 1817 | ALOGW("Not all data consumed. Consumed %zu of %zu", consumed, data.size()); |
| 1818 | return ResponseCode::SYSTEM_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1821 | finish(token, inArgs.hidl_data(), signature, hidl_vec<uint8_t>(), &result); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1822 | if (result.resultCode != ResponseCode::NO_ERROR) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1823 | ALOGW("Error in finish: %d", int32_t(result.resultCode)); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1824 | return translateResultToLegacyResult(result.resultCode); |
| 1825 | } |
| 1826 | if (out) { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1827 | outBuffer.write(&result.data[0], result.data.size()); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | if (out) { |
| 1831 | auto buf = outBuffer.str(); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1832 | out->resize(buf.size()); |
| 1833 | memcpy(&(*out)[0], buf.data(), out->size()); |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1834 | } |
| 1835 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1836 | return ResponseCode::NO_ERROR; |
Shawn Willden | c1d1fee | 2016-01-26 22:44:56 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1839 | KeyStoreServiceReturnCode KeyStoreService::upgradeKeyBlob(const String16& name, uid_t uid, |
| 1840 | const AuthorizationSet& params, |
| 1841 | Blob* blob) { |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 1842 | // Read the blob rather than assuming the caller provided the right name/uid/blob triplet. |
| 1843 | String8 name8(name); |
| 1844 | ResponseCode responseCode = mKeyStore->getKeyForName(blob, name8, uid, TYPE_KEYMASTER_10); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1845 | if (responseCode != ResponseCode::NO_ERROR) { |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 1846 | return responseCode; |
| 1847 | } |
Rubin Xu | 7675c9f | 2017-03-15 19:26:52 +0000 | [diff] [blame] | 1848 | ALOGI("upgradeKeyBlob %s %d", name8.string(), uid); |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 1849 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1850 | auto hidlKey = blob2hidlVec(*blob); |
| 1851 | auto& dev = mKeyStore->getDevice(*blob); |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 1852 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1853 | KeyStoreServiceReturnCode error; |
| 1854 | auto hidlCb = [&](ErrorCode ret, const hidl_vec<uint8_t>& upgradedKeyBlob) { |
| 1855 | error = ret; |
| 1856 | if (!error.isOk()) { |
| 1857 | return; |
| 1858 | } |
| 1859 | |
| 1860 | String8 filename(mKeyStore->getKeyNameForUidWithDir(name8, uid, ::TYPE_KEYMASTER_10)); |
| 1861 | error = mKeyStore->del(filename.string(), ::TYPE_ANY, get_user_id(uid)); |
| 1862 | if (!error.isOk()) { |
Rubin Xu | 7675c9f | 2017-03-15 19:26:52 +0000 | [diff] [blame] | 1863 | ALOGI("upgradeKeyBlob keystore->del failed %d", (int)error); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1864 | return; |
| 1865 | } |
| 1866 | |
| 1867 | Blob newBlob(&upgradedKeyBlob[0], upgradedKeyBlob.size(), nullptr /* info */, |
| 1868 | 0 /* infoLength */, ::TYPE_KEYMASTER_10); |
| 1869 | newBlob.setFallback(blob->isFallback()); |
| 1870 | newBlob.setEncrypted(blob->isEncrypted()); |
Rubin Xu | 67899de | 2017-04-21 19:15:13 +0100 | [diff] [blame] | 1871 | newBlob.setSuperEncrypted(blob->isSuperEncrypted()); |
| 1872 | newBlob.setCriticalToDeviceEncryption(blob->isCriticalToDeviceEncryption()); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1873 | |
| 1874 | error = mKeyStore->put(filename.string(), &newBlob, get_user_id(uid)); |
| 1875 | if (!error.isOk()) { |
Rubin Xu | 7675c9f | 2017-03-15 19:26:52 +0000 | [diff] [blame] | 1876 | ALOGI("upgradeKeyBlob keystore->put failed %d", (int)error); |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1877 | return; |
| 1878 | } |
| 1879 | |
| 1880 | // Re-read blob for caller. We can't use newBlob because writing it modified it. |
| 1881 | error = mKeyStore->getKeyForName(blob, name8, uid, TYPE_KEYMASTER_10); |
| 1882 | }; |
| 1883 | |
| 1884 | KeyStoreServiceReturnCode rc = |
| 1885 | KS_HANDLE_HIDL_ERROR(dev->upgradeKey(hidlKey, params.hidl_data(), hidlCb)); |
| 1886 | if (!rc.isOk()) { |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 1887 | return rc; |
| 1888 | } |
| 1889 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 1890 | return error; |
Shawn Willden | 98c5916 | 2016-03-20 09:10:18 -0600 | [diff] [blame] | 1891 | } |
| 1892 | |
Shawn Willden | e2a7b52 | 2017-04-11 09:27:40 -0600 | [diff] [blame] | 1893 | } // namespace keystore |