blob: 5a686305f5a5826f9c204f396c1c16a80e7880d2 [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>
24#include <aidl/android/system/keystore2/KeyDescriptor.h>
25
26// Keep these in sync with system/security/keystore2/src/keystore2_main.rs
27static constexpr const char keystore2_service_name[] =
28 "android.system.keystore2.IKeystoreService/default";
29static 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 */
35static 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
38static constexpr const int VOLD_NAMESPACE = 100;
Paul Crowley1ef25582016-01-21 20:26:12 +000039
40namespace android {
41namespace vold {
42
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080043namespace ks2_maint = ::aidl::android::security::maintenance;
Shawn Willden35351812018-01-22 09:08:32 -070044
Paul Crowley0323afd2016-03-15 17:04:39 -070045KeymasterOperation::~KeymasterOperation() {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080046 if (ks2Operation) ks2Operation->abort();
47}
48
49static void zeroize_vector(std::vector<uint8_t>& vec) {
50 memset_s(vec.data(), 0, vec.size());
51}
52
53static 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 Crowley0323afd2016-03-15 17:04:39 -070065}
66
Pavel Grafove2e2d302017-08-01 17:15:53 +010067bool KeymasterOperation::updateCompletely(const char* input, size_t inputLen,
Shawn Willden785365b2018-01-20 09:37:36 -070068 const std::function<void(const char*, size_t)> consumer) {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080069 if (!ks2Operation) return false;
Janis Danisevskis8e537b82016-10-26 14:27:10 +010070
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080071 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 Danisevskis8e537b82016-10-26 14:27:10 +010076
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080077 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 Crowley1ef25582016-01-21 20:26:12 +000082 return false;
83 }
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080084
85 if (!output) {
86 LOG(ERROR) << "Keystore2 operation update didn't return output.";
87 ks2Operation = nullptr;
Janis Danisevskis8e537b82016-10-26 14:27:10 +010088 return false;
89 }
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080090
91 consumer((const char*)output->data(), output->size());
Paul Crowley1ef25582016-01-21 20:26:12 +000092 }
93 return true;
94}
95
Paul Crowleydff8c722016-05-16 08:14:56 -070096bool KeymasterOperation::finish(std::string* output) {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -080097 std::optional<std::vector<uint8_t>> out_vec;
98
99 if (!ks2Operation) return false;
100
101 auto rc = ks2Operation->finish(std::nullopt, std::nullopt, &out_vec);
102 if (logKeystore2ExceptionIfPresent(rc, "finish")) {
103 ks2Operation = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +0000104 return false;
105 }
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800106
107 if (output) *output = std::string(out_vec->begin(), out_vec->end());
108
Paul Crowley1ef25582016-01-21 20:26:12 +0000109 return true;
110}
111
112Keymaster::Keymaster() {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800113 ::ndk::SpAIBinder binder(AServiceManager_getService(keystore2_service_name));
114 auto keystore2Service = ks2::IKeystoreService::fromBinder(binder);
115
116 if (!keystore2Service) {
117 LOG(ERROR) << "Vold unable to connect to keystore2.";
118 return;
Shawn Willden28075362018-05-09 08:12:10 -0600119 }
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800120
121 /*
122 * There are only two options available to vold for the SecurityLevel: TRUSTED_ENVIRONMENT (TEE)
123 * and STRONGBOX. We don't use STRONGBOX because if a TEE is present it will have Weaver, which
124 * already strengthens CE, so there's no additional benefit from using StrongBox.
125 *
126 * The picture is slightly more complicated because Keystore2 reports a SOFTWARE instance as
127 * a TEE instance when there isn't a TEE instance available, but in that case, a STRONGBOX
128 * instance won't be available either, so we'll still be doing the best we can.
129 */
130 auto rc = keystore2Service->getSecurityLevel(km::SecurityLevel::TRUSTED_ENVIRONMENT,
131 &securityLevel);
132 if (logKeystore2ExceptionIfPresent(rc, "getSecurityLevel"))
133 LOG(ERROR) << "Vold unable to get security level from keystore2.";
Paul Crowley1ef25582016-01-21 20:26:12 +0000134}
135
Shawn Willden35351812018-01-22 09:08:32 -0700136bool Keymaster::generateKey(const km::AuthorizationSet& inParams, std::string* key) {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800137 ks2::KeyDescriptor in_key = {
138 .domain = ks2::Domain::BLOB,
139 .alias = std::nullopt,
140 .nspace = VOLD_NAMESPACE,
141 .blob = std::nullopt,
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100142 };
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800143 ks2::KeyMetadata keyMetadata;
144 auto rc = securityLevel->generateKey(in_key, std::nullopt, inParams.vector_data(), 0, {},
145 &keyMetadata);
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100146
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800147 if (logKeystore2ExceptionIfPresent(rc, "generateKey")) return false;
148
149 if (keyMetadata.key.blob == std::nullopt) {
150 LOG(ERROR) << "keystore2 generated key blob was null";
Paul Crowley1ef25582016-01-21 20:26:12 +0000151 return false;
152 }
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800153 if (key) *key = std::string(keyMetadata.key.blob->begin(), keyMetadata.key.blob->end());
154
155 zeroize_vector(keyMetadata.key.blob.value());
Paul Crowley1ef25582016-01-21 20:26:12 +0000156 return true;
157}
158
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800159bool Keymaster::exportKey(const KeyBuffer& kmKey, std::string* key) {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800160 bool ret = false;
161 ks2::KeyDescriptor storageKey = {
162 .domain = ks2::Domain::BLOB,
163 .alias = std::nullopt,
164 .nspace = VOLD_NAMESPACE,
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800165 };
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800166 storageKey.blob = std::make_optional<std::vector<uint8_t>>(kmKey.begin(), kmKey.end());
167 std::vector<uint8_t> ephemeral_key;
168 auto rc = securityLevel->convertStorageKeyToEphemeral(storageKey, &ephemeral_key);
169
170 if (logKeystore2ExceptionIfPresent(rc, "exportKey")) goto out;
171 if (key) *key = std::string(ephemeral_key.begin(), ephemeral_key.end());
172
173 ret = true;
174out:
175 zeroize_vector(ephemeral_key);
176 zeroize_vector(storageKey.blob.value());
177 return ret;
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800178}
179
Paul Crowleydf528a72016-03-09 09:31:37 -0800180bool Keymaster::deleteKey(const std::string& key) {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800181 ks2::KeyDescriptor keyDesc = {
182 .domain = ks2::Domain::BLOB,
183 .alias = std::nullopt,
184 .nspace = VOLD_NAMESPACE,
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100185 };
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800186 keyDesc.blob =
187 std::optional<std::vector<uint8_t>>(std::vector<uint8_t>(key.begin(), key.end()));
188
189 auto rc = securityLevel->deleteKey(keyDesc);
190 return !logKeystore2ExceptionIfPresent(rc, "deleteKey");
Paul Crowleydff8c722016-05-16 08:14:56 -0700191}
192
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800193KeymasterOperation Keymaster::begin(const std::string& key, const km::AuthorizationSet& inParams,
Shawn Willden35351812018-01-22 09:08:32 -0700194 km::AuthorizationSet* outParams) {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800195 ks2::KeyDescriptor keyDesc = {
196 .domain = ks2::Domain::BLOB,
197 .alias = std::nullopt,
198 .nspace = VOLD_NAMESPACE,
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100199 };
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800200 keyDesc.blob =
201 std::optional<std::vector<uint8_t>>(std::vector<uint8_t>(key.begin(), key.end()));
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100202
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800203 ks2::CreateOperationResponse cor;
204 auto rc = securityLevel->createOperation(keyDesc, inParams.vector_data(), true, &cor);
205 if (logKeystore2ExceptionIfPresent(rc, "createOperation")) {
206 if (rc.getExceptionCode() == EX_SERVICE_SPECIFIC)
207 return KeymasterOperation((km::ErrorCode)rc.getServiceSpecificError());
208 else
209 return KeymasterOperation();
Paul Crowley1ef25582016-01-21 20:26:12 +0000210 }
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800211
212 if (!cor.iOperation) {
213 LOG(ERROR) << "keystore2 createOperation didn't return an operation";
214 return KeymasterOperation();
Paul Crowley1ef25582016-01-21 20:26:12 +0000215 }
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800216
217 if (outParams && cor.parameters) *outParams = cor.parameters->keyParameter;
218
219 return KeymasterOperation(cor.iOperation, cor.upgradedBlob);
Paul Crowley1ef25582016-01-21 20:26:12 +0000220}
Shawn Willden35351812018-01-22 09:08:32 -0700221
Janis Danisevskis015ec302017-01-31 11:31:08 +0000222bool Keymaster::isSecure() {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800223 return true;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000224}
Paul Crowley1ef25582016-01-21 20:26:12 +0000225
Shawn Willden2b1ff5a2020-01-16 14:08:36 -0700226void Keymaster::earlyBootEnded() {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800227 ::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 Willden2b1ff5a2020-01-16 14:08:36 -0700233 }
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800234
235 auto rc = maint_service->earlyBootEnded();
236 logKeystore2ExceptionIfPresent(rc, "earlyBootEnded");
Shawn Willden2b1ff5a2020-01-16 14:08:36 -0700237}
238
Paul Crowley1ef25582016-01-21 20:26:12 +0000239} // namespace vold
240} // namespace android
Janis Danisevskis015ec302017-01-31 11:31:08 +0000241
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800242// TODO: This always returns true right now since we hardcode the security level.
243// If it's alright to hardcode it, we should remove this function and simplify the callers.
Janis Danisevskis015ec302017-01-31 11:31:08 +0000244int keymaster_compatibility_cryptfs_scrypt() {
Satya Tangiralae8de4ff2021-02-28 22:32:07 -0800245 android::vold::Keymaster dev;
Janis Danisevskise7152c32017-03-08 11:02:30 -0800246 if (!dev) {
247 LOG(ERROR) << "Failed to initiate keymaster session";
248 return -1;
249 }
250 return dev.isSecure();
Janis Danisevskis015ec302017-01-31 11:31:08 +0000251}