blob: be282dd71a51bda3bcbf258d97b34b7f80bd7f4d [file] [log] [blame]
Paul Crowley1ef25582016-01-21 20:26:12 +00001/*
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 Tangiralae8de4ff2021-02-28 22:32:07 -080020
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 Danisevskis3915b082021-04-20 12:50:58 -070024#include <aidl/android/system/keystore2/EphemeralStorageKeyResponse.h>
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080025#include <aidl/android/system/keystore2/KeyDescriptor.h>
26
27// Keep these in sync with system/security/keystore2/src/keystore2_main.rs
28static constexpr const char keystore2_service_name[] =
29 "android.system.keystore2.IKeystoreService/default";
30static 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 */
36static 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
39static constexpr const int VOLD_NAMESPACE = 100;
Paul Crowley1ef25582016-01-21 20:26:12 +000040
41namespace android {
42namespace vold {
43
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080044namespace ks2_maint = ::aidl::android::security::maintenance;
Shawn Willden35351812018-01-22 09:08:32 -070045
Paul Crowley0323afd2016-03-15 17:04:39 -070046KeymasterOperation::~KeymasterOperation() {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080047 if (ks2Operation) ks2Operation->abort();
48}
49
50static void zeroize_vector(std::vector<uint8_t>& vec) {
51 memset_s(vec.data(), 0, vec.size());
52}
53
54static 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 Crowley0323afd2016-03-15 17:04:39 -070066}
67
Pavel Grafove2e2d302017-08-01 17:15:53 +010068bool KeymasterOperation::updateCompletely(const char* input, size_t inputLen,
Shawn Willden785365b2018-01-20 09:37:36 -070069 const std::function<void(const char*, size_t)> consumer) {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080070 if (!ks2Operation) return false;
Janis Danisevskis8e537b82016-10-26 14:27:10 +010071
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080072 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 Danisevskis8e537b82016-10-26 14:27:10 +010077
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080078 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 Crowley1ef25582016-01-21 20:26:12 +000083 return false;
84 }
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080085
86 if (!output) {
87 LOG(ERROR) << "Keystore2 operation update didn't return output.";
88 ks2Operation = nullptr;
Janis Danisevskis8e537b82016-10-26 14:27:10 +010089 return false;
90 }
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080091
92 consumer((const char*)output->data(), output->size());
Paul Crowley1ef25582016-01-21 20:26:12 +000093 }
94 return true;
95}
96
Paul Crowleydff8c722016-05-16 08:14:56 -070097bool KeymasterOperation::finish(std::string* output) {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080098 std::optional<std::vector<uint8_t>> out_vec;
99
100 if (!ks2Operation) return false;
101
102 auto rc = ks2Operation->finish(std::nullopt, std::nullopt, &out_vec);
103 if (logKeystore2ExceptionIfPresent(rc, "finish")) {
104 ks2Operation = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +0000105 return false;
106 }
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800107
108 if (output) *output = std::string(out_vec->begin(), out_vec->end());
109
Paul Crowley1ef25582016-01-21 20:26:12 +0000110 return true;
111}
112
113Keymaster::Keymaster() {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800114 ::ndk::SpAIBinder binder(AServiceManager_getService(keystore2_service_name));
115 auto keystore2Service = ks2::IKeystoreService::fromBinder(binder);
116
117 if (!keystore2Service) {
118 LOG(ERROR) << "Vold unable to connect to keystore2.";
119 return;
Shawn Willden28075362018-05-09 08:12:10 -0600120 }
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800121
122 /*
123 * There are only two options available to vold for the SecurityLevel: TRUSTED_ENVIRONMENT (TEE)
124 * and STRONGBOX. We don't use STRONGBOX because if a TEE is present it will have Weaver, which
125 * already strengthens CE, so there's no additional benefit from using StrongBox.
126 *
127 * The picture is slightly more complicated because Keystore2 reports a SOFTWARE instance as
128 * a TEE instance when there isn't a TEE instance available, but in that case, a STRONGBOX
129 * instance won't be available either, so we'll still be doing the best we can.
130 */
131 auto rc = keystore2Service->getSecurityLevel(km::SecurityLevel::TRUSTED_ENVIRONMENT,
132 &securityLevel);
133 if (logKeystore2ExceptionIfPresent(rc, "getSecurityLevel"))
134 LOG(ERROR) << "Vold unable to get security level from keystore2.";
Paul Crowley1ef25582016-01-21 20:26:12 +0000135}
136
Shawn Willden35351812018-01-22 09:08:32 -0700137bool Keymaster::generateKey(const km::AuthorizationSet& inParams, std::string* key) {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800138 ks2::KeyDescriptor in_key = {
139 .domain = ks2::Domain::BLOB,
140 .alias = std::nullopt,
141 .nspace = VOLD_NAMESPACE,
142 .blob = std::nullopt,
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100143 };
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800144 ks2::KeyMetadata keyMetadata;
145 auto rc = securityLevel->generateKey(in_key, std::nullopt, inParams.vector_data(), 0, {},
146 &keyMetadata);
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100147
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800148 if (logKeystore2ExceptionIfPresent(rc, "generateKey")) return false;
149
150 if (keyMetadata.key.blob == std::nullopt) {
151 LOG(ERROR) << "keystore2 generated key blob was null";
Paul Crowley1ef25582016-01-21 20:26:12 +0000152 return false;
153 }
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800154 if (key) *key = std::string(keyMetadata.key.blob->begin(), keyMetadata.key.blob->end());
155
156 zeroize_vector(keyMetadata.key.blob.value());
Paul Crowley1ef25582016-01-21 20:26:12 +0000157 return true;
158}
159
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800160bool Keymaster::exportKey(const KeyBuffer& kmKey, std::string* key) {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800161 bool ret = false;
162 ks2::KeyDescriptor storageKey = {
163 .domain = ks2::Domain::BLOB,
164 .alias = std::nullopt,
165 .nspace = VOLD_NAMESPACE,
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800166 };
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800167 storageKey.blob = std::make_optional<std::vector<uint8_t>>(kmKey.begin(), kmKey.end());
Janis Danisevskis3915b082021-04-20 12:50:58 -0700168 ks2::EphemeralStorageKeyResponse ephemeral_key_response;
169 auto rc = securityLevel->convertStorageKeyToEphemeral(storageKey, &ephemeral_key_response);
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800170
171 if (logKeystore2ExceptionIfPresent(rc, "exportKey")) goto out;
Janis Danisevskis3915b082021-04-20 12:50:58 -0700172 if (key)
173 *key = std::string(ephemeral_key_response.ephemeralKey.begin(),
174 ephemeral_key_response.ephemeralKey.end());
175
176 // TODO b/185811713 store the upgraded key blob if provided and delete the old key blob.
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800177
178 ret = true;
179out:
Janis Danisevskis3915b082021-04-20 12:50:58 -0700180 zeroize_vector(ephemeral_key_response.ephemeralKey);
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800181 zeroize_vector(storageKey.blob.value());
182 return ret;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800183}
184
Paul Crowleydf528a72016-03-09 09:31:37 -0800185bool Keymaster::deleteKey(const std::string& key) {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800186 ks2::KeyDescriptor keyDesc = {
187 .domain = ks2::Domain::BLOB,
188 .alias = std::nullopt,
189 .nspace = VOLD_NAMESPACE,
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100190 };
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800191 keyDesc.blob =
192 std::optional<std::vector<uint8_t>>(std::vector<uint8_t>(key.begin(), key.end()));
193
194 auto rc = securityLevel->deleteKey(keyDesc);
195 return !logKeystore2ExceptionIfPresent(rc, "deleteKey");
Paul Crowleydff8c722016-05-16 08:14:56 -0700196}
197
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800198KeymasterOperation Keymaster::begin(const std::string& key, const km::AuthorizationSet& inParams,
Shawn Willden35351812018-01-22 09:08:32 -0700199 km::AuthorizationSet* outParams) {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800200 ks2::KeyDescriptor keyDesc = {
201 .domain = ks2::Domain::BLOB,
202 .alias = std::nullopt,
203 .nspace = VOLD_NAMESPACE,
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100204 };
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800205 keyDesc.blob =
206 std::optional<std::vector<uint8_t>>(std::vector<uint8_t>(key.begin(), key.end()));
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100207
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800208 ks2::CreateOperationResponse cor;
209 auto rc = securityLevel->createOperation(keyDesc, inParams.vector_data(), true, &cor);
210 if (logKeystore2ExceptionIfPresent(rc, "createOperation")) {
211 if (rc.getExceptionCode() == EX_SERVICE_SPECIFIC)
212 return KeymasterOperation((km::ErrorCode)rc.getServiceSpecificError());
213 else
214 return KeymasterOperation();
Paul Crowley1ef25582016-01-21 20:26:12 +0000215 }
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800216
217 if (!cor.iOperation) {
218 LOG(ERROR) << "keystore2 createOperation didn't return an operation";
219 return KeymasterOperation();
Paul Crowley1ef25582016-01-21 20:26:12 +0000220 }
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800221
222 if (outParams && cor.parameters) *outParams = cor.parameters->keyParameter;
223
224 return KeymasterOperation(cor.iOperation, cor.upgradedBlob);
Paul Crowley1ef25582016-01-21 20:26:12 +0000225}
Shawn Willden35351812018-01-22 09:08:32 -0700226
Shawn Willden2b1ff5a2020-01-16 14:08:36 -0700227void Keymaster::earlyBootEnded() {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800228 ::ndk::SpAIBinder binder(AServiceManager_getService(maintenance_service_name));
229 auto maint_service = ks2_maint::IKeystoreMaintenance::fromBinder(binder);
230
231 if (!maint_service) {
232 LOG(ERROR) << "Unable to connect to keystore2 maintenance service for earlyBootEnded";
233 return;
Shawn Willden2b1ff5a2020-01-16 14:08:36 -0700234 }
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800235
236 auto rc = maint_service->earlyBootEnded();
237 logKeystore2ExceptionIfPresent(rc, "earlyBootEnded");
Shawn Willden2b1ff5a2020-01-16 14:08:36 -0700238}
239
Paul Crowley1ef25582016-01-21 20:26:12 +0000240} // namespace vold
241} // namespace android