Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [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 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 17 | #include "Keystore.h" |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 18 | |
| 19 | #include <android-base/logging.h> |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 20 | |
| 21 | #include <aidl/android/hardware/security/keymint/SecurityLevel.h> |
| 22 | #include <aidl/android/security/maintenance/IKeystoreMaintenance.h> |
| 23 | #include <aidl/android/system/keystore2/Domain.h> |
Janis Danisevskis | 3915b08 | 2021-04-20 12:50:58 -0700 | [diff] [blame] | 24 | #include <aidl/android/system/keystore2/EphemeralStorageKeyResponse.h> |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 25 | #include <aidl/android/system/keystore2/KeyDescriptor.h> |
| 26 | |
| 27 | // Keep these in sync with system/security/keystore2/src/keystore2_main.rs |
| 28 | static constexpr const char keystore2_service_name[] = |
| 29 | "android.system.keystore2.IKeystoreService/default"; |
| 30 | static constexpr const char maintenance_service_name[] = "android.security.maintenance"; |
| 31 | |
| 32 | /* |
| 33 | * Keep this in sync with the description for update() in |
| 34 | * system/hardware/interfaces/keystore2/aidl/android/system/keystore2/IKeystoreOperation.aidl |
| 35 | */ |
| 36 | static constexpr const size_t UPDATE_INPUT_MAX_SIZE = 32 * 1024; // 32 KiB |
| 37 | |
| 38 | // Keep this in sync with system/sepolicy/private/keystore2_key_contexts |
| 39 | static constexpr const int VOLD_NAMESPACE = 100; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 40 | |
| 41 | namespace android { |
| 42 | namespace vold { |
| 43 | |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 44 | namespace ks2_maint = ::aidl::android::security::maintenance; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 45 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 46 | KeystoreOperation::~KeystoreOperation() { |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 47 | if (ks2Operation) ks2Operation->abort(); |
| 48 | } |
| 49 | |
| 50 | static void zeroize_vector(std::vector<uint8_t>& vec) { |
| 51 | memset_s(vec.data(), 0, vec.size()); |
| 52 | } |
| 53 | |
| 54 | static bool logKeystore2ExceptionIfPresent(::ndk::ScopedAStatus& rc, const std::string& func_name) { |
| 55 | if (rc.isOk()) return false; |
| 56 | |
| 57 | auto exception_code = rc.getExceptionCode(); |
| 58 | if (exception_code == EX_SERVICE_SPECIFIC) { |
| 59 | LOG(ERROR) << "keystore2 Keystore " << func_name |
| 60 | << " returned service specific error: " << rc.getServiceSpecificError(); |
| 61 | } else { |
| 62 | LOG(ERROR) << "keystore2 Communication with Keystore " << func_name |
| 63 | << " failed error: " << exception_code; |
| 64 | } |
| 65 | return true; |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 68 | bool KeystoreOperation::updateCompletely(const char* input, size_t inputLen, |
| 69 | const std::function<void(const char*, size_t)> consumer) { |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 70 | if (!ks2Operation) return false; |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 71 | |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 72 | while (inputLen != 0) { |
| 73 | size_t currLen = std::min(inputLen, UPDATE_INPUT_MAX_SIZE); |
| 74 | std::vector<uint8_t> input_vec(input, input + currLen); |
| 75 | inputLen -= currLen; |
| 76 | input += currLen; |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 77 | |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 78 | std::optional<std::vector<uint8_t>> output; |
| 79 | auto rc = ks2Operation->update(input_vec, &output); |
| 80 | zeroize_vector(input_vec); |
| 81 | if (logKeystore2ExceptionIfPresent(rc, "update")) { |
| 82 | ks2Operation = nullptr; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 83 | return false; |
| 84 | } |
Eric Biggers | 940c0e5 | 2021-04-22 16:36:58 -0700 | [diff] [blame] | 85 | if (output) consumer((const char*)output->data(), output->size()); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 90 | bool KeystoreOperation::finish(std::string* output) { |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 91 | std::optional<std::vector<uint8_t>> out_vec; |
| 92 | |
| 93 | if (!ks2Operation) return false; |
| 94 | |
| 95 | auto rc = ks2Operation->finish(std::nullopt, std::nullopt, &out_vec); |
| 96 | if (logKeystore2ExceptionIfPresent(rc, "finish")) { |
| 97 | ks2Operation = nullptr; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 98 | return false; |
| 99 | } |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 100 | |
| 101 | if (output) *output = std::string(out_vec->begin(), out_vec->end()); |
| 102 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 106 | Keystore::Keystore() { |
Satya Tangirala | 6ef4e37 | 2021-04-12 15:00:33 -0700 | [diff] [blame] | 107 | ::ndk::SpAIBinder binder(AServiceManager_waitForService(keystore2_service_name)); |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 108 | auto keystore2Service = ks2::IKeystoreService::fromBinder(binder); |
| 109 | |
| 110 | if (!keystore2Service) { |
| 111 | LOG(ERROR) << "Vold unable to connect to keystore2."; |
| 112 | return; |
Shawn Willden | 2807536 | 2018-05-09 08:12:10 -0600 | [diff] [blame] | 113 | } |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 114 | |
| 115 | /* |
| 116 | * There are only two options available to vold for the SecurityLevel: TRUSTED_ENVIRONMENT (TEE) |
| 117 | * and STRONGBOX. We don't use STRONGBOX because if a TEE is present it will have Weaver, which |
| 118 | * already strengthens CE, so there's no additional benefit from using StrongBox. |
| 119 | * |
| 120 | * The picture is slightly more complicated because Keystore2 reports a SOFTWARE instance as |
| 121 | * a TEE instance when there isn't a TEE instance available, but in that case, a STRONGBOX |
| 122 | * instance won't be available either, so we'll still be doing the best we can. |
| 123 | */ |
| 124 | auto rc = keystore2Service->getSecurityLevel(km::SecurityLevel::TRUSTED_ENVIRONMENT, |
| 125 | &securityLevel); |
| 126 | if (logKeystore2ExceptionIfPresent(rc, "getSecurityLevel")) |
| 127 | LOG(ERROR) << "Vold unable to get security level from keystore2."; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 130 | bool Keystore::generateKey(const km::AuthorizationSet& inParams, std::string* key) { |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 131 | ks2::KeyDescriptor in_key = { |
| 132 | .domain = ks2::Domain::BLOB, |
| 133 | .alias = std::nullopt, |
| 134 | .nspace = VOLD_NAMESPACE, |
| 135 | .blob = std::nullopt, |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 136 | }; |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 137 | ks2::KeyMetadata keyMetadata; |
| 138 | auto rc = securityLevel->generateKey(in_key, std::nullopt, inParams.vector_data(), 0, {}, |
| 139 | &keyMetadata); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 140 | |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 141 | if (logKeystore2ExceptionIfPresent(rc, "generateKey")) return false; |
| 142 | |
| 143 | if (keyMetadata.key.blob == std::nullopt) { |
| 144 | LOG(ERROR) << "keystore2 generated key blob was null"; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 145 | return false; |
| 146 | } |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 147 | if (key) *key = std::string(keyMetadata.key.blob->begin(), keyMetadata.key.blob->end()); |
| 148 | |
| 149 | zeroize_vector(keyMetadata.key.blob.value()); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 150 | return true; |
| 151 | } |
| 152 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 153 | bool Keystore::exportKey(const KeyBuffer& ksKey, std::string* key) { |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 154 | bool ret = false; |
| 155 | ks2::KeyDescriptor storageKey = { |
| 156 | .domain = ks2::Domain::BLOB, |
| 157 | .alias = std::nullopt, |
| 158 | .nspace = VOLD_NAMESPACE, |
Barani Muthukumaran | 3dfb094 | 2020-02-03 13:06:45 -0800 | [diff] [blame] | 159 | }; |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 160 | storageKey.blob = std::make_optional<std::vector<uint8_t>>(ksKey.begin(), ksKey.end()); |
Janis Danisevskis | 3915b08 | 2021-04-20 12:50:58 -0700 | [diff] [blame] | 161 | ks2::EphemeralStorageKeyResponse ephemeral_key_response; |
| 162 | auto rc = securityLevel->convertStorageKeyToEphemeral(storageKey, &ephemeral_key_response); |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 163 | |
| 164 | if (logKeystore2ExceptionIfPresent(rc, "exportKey")) goto out; |
Janis Danisevskis | 3915b08 | 2021-04-20 12:50:58 -0700 | [diff] [blame] | 165 | if (key) |
| 166 | *key = std::string(ephemeral_key_response.ephemeralKey.begin(), |
| 167 | ephemeral_key_response.ephemeralKey.end()); |
| 168 | |
Eric Biggers | 3aa35ee | 2022-03-17 22:12:14 +0000 | [diff] [blame] | 169 | // vold intentionally ignores ephemeral_key_response.upgradedBlob, since the |
| 170 | // concept of "upgrading" doesn't make sense for TAG_STORAGE_KEY keys |
| 171 | // (hardware-wrapped inline encryption keys). These keys are only meant as |
| 172 | // a substitute for raw keys; they still go through vold's usual layer of |
| 173 | // key wrapping, which already handles version binding. So, vold just keeps |
| 174 | // using the original blobs for TAG_STORAGE_KEY keys. If KeyMint "upgrades" |
| 175 | // them anyway, then they'll just get re-upgraded before each use. |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 176 | |
| 177 | ret = true; |
| 178 | out: |
Janis Danisevskis | 3915b08 | 2021-04-20 12:50:58 -0700 | [diff] [blame] | 179 | zeroize_vector(ephemeral_key_response.ephemeralKey); |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 180 | zeroize_vector(storageKey.blob.value()); |
| 181 | return ret; |
Barani Muthukumaran | 3dfb094 | 2020-02-03 13:06:45 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 184 | bool Keystore::deleteKey(const std::string& key) { |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 185 | ks2::KeyDescriptor keyDesc = { |
| 186 | .domain = ks2::Domain::BLOB, |
| 187 | .alias = std::nullopt, |
| 188 | .nspace = VOLD_NAMESPACE, |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 189 | }; |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 190 | keyDesc.blob = |
| 191 | std::optional<std::vector<uint8_t>>(std::vector<uint8_t>(key.begin(), key.end())); |
| 192 | |
| 193 | auto rc = securityLevel->deleteKey(keyDesc); |
| 194 | return !logKeystore2ExceptionIfPresent(rc, "deleteKey"); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 197 | KeystoreOperation Keystore::begin(const std::string& key, const km::AuthorizationSet& inParams, |
| 198 | km::AuthorizationSet* outParams) { |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 199 | ks2::KeyDescriptor keyDesc = { |
| 200 | .domain = ks2::Domain::BLOB, |
| 201 | .alias = std::nullopt, |
| 202 | .nspace = VOLD_NAMESPACE, |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 203 | }; |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 204 | keyDesc.blob = |
| 205 | std::optional<std::vector<uint8_t>>(std::vector<uint8_t>(key.begin(), key.end())); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 206 | |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 207 | ks2::CreateOperationResponse cor; |
| 208 | auto rc = securityLevel->createOperation(keyDesc, inParams.vector_data(), true, &cor); |
| 209 | if (logKeystore2ExceptionIfPresent(rc, "createOperation")) { |
| 210 | if (rc.getExceptionCode() == EX_SERVICE_SPECIFIC) |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 211 | return KeystoreOperation((km::ErrorCode)rc.getServiceSpecificError()); |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 212 | else |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 213 | return KeystoreOperation(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 214 | } |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 215 | |
| 216 | if (!cor.iOperation) { |
| 217 | LOG(ERROR) << "keystore2 createOperation didn't return an operation"; |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 218 | return KeystoreOperation(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 219 | } |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 220 | |
| 221 | if (outParams && cor.parameters) *outParams = cor.parameters->keyParameter; |
| 222 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 223 | return KeystoreOperation(cor.iOperation, cor.upgradedBlob); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 224 | } |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame] | 225 | |
Eric Biggers | d86a8ab | 2021-06-15 11:34:00 -0700 | [diff] [blame] | 226 | void Keystore::earlyBootEnded() { |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 227 | ::ndk::SpAIBinder binder(AServiceManager_getService(maintenance_service_name)); |
| 228 | auto maint_service = ks2_maint::IKeystoreMaintenance::fromBinder(binder); |
| 229 | |
| 230 | if (!maint_service) { |
| 231 | LOG(ERROR) << "Unable to connect to keystore2 maintenance service for earlyBootEnded"; |
| 232 | return; |
Shawn Willden | 2b1ff5a | 2020-01-16 14:08:36 -0700 | [diff] [blame] | 233 | } |
Satya Tangirala | e8de4ff | 2021-02-28 22:32:07 -0800 | [diff] [blame] | 234 | |
| 235 | auto rc = maint_service->earlyBootEnded(); |
| 236 | logKeystore2ExceptionIfPresent(rc, "earlyBootEnded"); |
Shawn Willden | 2b1ff5a | 2020-01-16 14:08:36 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Paul Crowley | 1e6a5f5 | 2021-08-06 15:16:10 -0700 | [diff] [blame] | 239 | void Keystore::deleteAllKeys() { |
| 240 | ::ndk::SpAIBinder binder(AServiceManager_getService(maintenance_service_name)); |
| 241 | auto maint_service = ks2_maint::IKeystoreMaintenance::fromBinder(binder); |
| 242 | |
| 243 | if (!maint_service) { |
| 244 | LOG(ERROR) << "Unable to connect to keystore2 maintenance service for deleteAllKeys"; |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | auto rc = maint_service->deleteAllKeys(); |
| 249 | logKeystore2ExceptionIfPresent(rc, "deleteAllKeys"); |
| 250 | } |
| 251 | |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 252 | } // namespace vold |
| 253 | } // namespace android |