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