blob: 9cc32d46ef7f90b74bd988debb4483a2eaaad438 [file] [log] [blame]
Shawn Willden815e8962020-12-11 13:05:27 +00001/*
2 * Copyright 2020, 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#define LOG_TAG "android.hardware.security.keymint-impl"
Shawn Willden763166c2021-01-10 19:45:01 -070018#include <android-base/logging.h>
Shawn Willden815e8962020-12-11 13:05:27 +000019
20#include "AndroidKeyMintDevice.h"
21
22#include <aidl/android/hardware/security/keymint/ErrorCode.h>
23
24#include <keymaster/android_keymaster.h>
25#include <keymaster/contexts/pure_soft_keymaster_context.h>
26#include <keymaster/keymaster_configuration.h>
27
28#include "AndroidKeyMintOperation.h"
29#include "KeyMintUtils.h"
30
31namespace aidl::android::hardware::security::keymint {
32
33using namespace ::keymaster;
Shawn Willden96d4e8c2020-12-07 17:03:54 -070034using namespace km_utils;
Shawn Willden815e8962020-12-11 13:05:27 +000035
Shawn Willden763166c2021-01-10 19:45:01 -070036namespace {
37
38vector<KeyCharacteristics> convertKeyCharacteristics(SecurityLevel keyMintSecurityLevel,
39 const AuthorizationSet& sw_enforced,
40 const AuthorizationSet& hw_enforced) {
Shawn Willden09f47102021-01-15 15:21:56 -070041 KeyCharacteristics keyMintEnforced{keyMintSecurityLevel, {}};
Shawn Willden763166c2021-01-10 19:45:01 -070042
Shawn Willden763166c2021-01-10 19:45:01 -070043 if (keyMintSecurityLevel != SecurityLevel::SOFTWARE) {
Qi Wuf7c8e9d2021-02-11 03:14:52 +080044 // We're pretending to be TRUSTED_ENVIRONMENT or STRONGBOX.
Shawn Willden09f47102021-01-15 15:21:56 -070045 keyMintEnforced.authorizations = kmParamSet2Aidl(hw_enforced);
Qi Wuf7c8e9d2021-02-11 03:14:52 +080046 // Put all the software authorizations in the keystore list.
47 KeyCharacteristics keystoreEnforced{SecurityLevel::KEYSTORE, kmParamSet2Aidl(sw_enforced)};
48 return {std::move(keyMintEnforced), std::move(keystoreEnforced)};
Shawn Willden763166c2021-01-10 19:45:01 -070049 }
50
Shawn Willden09f47102021-01-15 15:21:56 -070051 KeyCharacteristics keystoreEnforced{SecurityLevel::KEYSTORE, {}};
Shawn Willden763166c2021-01-10 19:45:01 -070052 CHECK(hw_enforced.empty()) << "Hardware-enforced list is non-empty for pure SW KeyMint";
53
Shawn Willden09f47102021-01-15 15:21:56 -070054 // This is a pure software implementation, so all tags are in sw_enforced.
55 // We need to walk through the SW-enforced list and figure out which tags to
56 // return in the software list and which in the keystore list.
Shawn Willden763166c2021-01-10 19:45:01 -070057
58 for (auto& entry : sw_enforced) {
59 switch (entry.tag) {
60 /* Invalid and unused */
61 case KM_TAG_ECIES_SINGLE_HASH_MODE:
62 case KM_TAG_INVALID:
63 case KM_TAG_KDF:
Shawn Willden09f47102021-01-15 15:21:56 -070064 case KM_TAG_ROLLBACK_RESISTANCE:
Shawn Willden763166c2021-01-10 19:45:01 -070065 CHECK(false) << "We shouldn't see tag " << entry.tag;
66 break;
67
68 /* Unimplemented */
69 case KM_TAG_ALLOW_WHILE_ON_BODY:
Shawn Willden763166c2021-01-10 19:45:01 -070070 case KM_TAG_BOOTLOADER_ONLY:
71 case KM_TAG_EARLY_BOOT_ONLY:
Shawn Willden763166c2021-01-10 19:45:01 -070072 case KM_TAG_ROLLBACK_RESISTANT:
73 case KM_TAG_STORAGE_KEY:
Shawn Willden09f47102021-01-15 15:21:56 -070074 case KM_TAG_TRUSTED_CONFIRMATION_REQUIRED:
75 case KM_TAG_TRUSTED_USER_PRESENCE_REQUIRED:
Shawn Willden763166c2021-01-10 19:45:01 -070076 break;
77
78 /* Unenforceable */
Shawn Willden763166c2021-01-10 19:45:01 -070079 case KM_TAG_CREATION_DATETIME:
Shawn Willden763166c2021-01-10 19:45:01 -070080 break;
81
82 /* Disallowed in KeyCharacteristics */
83 case KM_TAG_APPLICATION_DATA:
Shawn Willden09f47102021-01-15 15:21:56 -070084 case KM_TAG_ATTESTATION_APPLICATION_ID:
Shawn Willden763166c2021-01-10 19:45:01 -070085 break;
86
87 /* Not key characteristics */
88 case KM_TAG_ASSOCIATED_DATA:
Shawn Willden763166c2021-01-10 19:45:01 -070089 case KM_TAG_ATTESTATION_CHALLENGE:
90 case KM_TAG_ATTESTATION_ID_BRAND:
91 case KM_TAG_ATTESTATION_ID_DEVICE:
92 case KM_TAG_ATTESTATION_ID_IMEI:
93 case KM_TAG_ATTESTATION_ID_MANUFACTURER:
94 case KM_TAG_ATTESTATION_ID_MEID:
95 case KM_TAG_ATTESTATION_ID_MODEL:
96 case KM_TAG_ATTESTATION_ID_PRODUCT:
97 case KM_TAG_ATTESTATION_ID_SERIAL:
98 case KM_TAG_AUTH_TOKEN:
99 case KM_TAG_CERTIFICATE_SERIAL:
100 case KM_TAG_CERTIFICATE_SUBJECT:
Janis Danisevskisa5780e22021-01-31 22:05:22 -0800101 case KM_TAG_CERTIFICATE_NOT_AFTER:
102 case KM_TAG_CERTIFICATE_NOT_BEFORE:
Shawn Willden763166c2021-01-10 19:45:01 -0700103 case KM_TAG_CONFIRMATION_TOKEN:
104 case KM_TAG_DEVICE_UNIQUE_ATTESTATION:
105 case KM_TAG_IDENTITY_CREDENTIAL_KEY:
106 case KM_TAG_MAC_LENGTH:
107 case KM_TAG_NONCE:
108 case KM_TAG_RESET_SINCE_ID_ROTATION:
109 case KM_TAG_ROOT_OF_TRUST:
110 case KM_TAG_UNIQUE_ID:
111 break;
112
Shawn Willden09f47102021-01-15 15:21:56 -0700113 /* KeyMint-enforced */
Shawn Willden763166c2021-01-10 19:45:01 -0700114 case KM_TAG_ALGORITHM:
Shawn Willden09f47102021-01-15 15:21:56 -0700115 case KM_TAG_APPLICATION_ID:
Shawn Willden763166c2021-01-10 19:45:01 -0700116 case KM_TAG_AUTH_TIMEOUT:
117 case KM_TAG_BLOB_USAGE_REQUIREMENTS:
118 case KM_TAG_BLOCK_MODE:
119 case KM_TAG_BOOT_PATCHLEVEL:
120 case KM_TAG_CALLER_NONCE:
121 case KM_TAG_DIGEST:
122 case KM_TAG_EC_CURVE:
123 case KM_TAG_EXPORTABLE:
124 case KM_TAG_INCLUDE_UNIQUE_ID:
125 case KM_TAG_KEY_SIZE:
126 case KM_TAG_MAX_USES_PER_BOOT:
127 case KM_TAG_MIN_MAC_LENGTH:
128 case KM_TAG_MIN_SECONDS_BETWEEN_OPS:
129 case KM_TAG_NO_AUTH_REQUIRED:
130 case KM_TAG_ORIGIN:
131 case KM_TAG_OS_PATCHLEVEL:
132 case KM_TAG_OS_VERSION:
133 case KM_TAG_PADDING:
134 case KM_TAG_PURPOSE:
135 case KM_TAG_RSA_OAEP_MGF_DIGEST:
136 case KM_TAG_RSA_PUBLIC_EXPONENT:
137 case KM_TAG_UNLOCKED_DEVICE_REQUIRED:
138 case KM_TAG_USER_AUTH_TYPE:
139 case KM_TAG_USER_SECURE_ID:
140 case KM_TAG_VENDOR_PATCHLEVEL:
Shawn Willden09f47102021-01-15 15:21:56 -0700141 keyMintEnforced.authorizations.push_back(kmParam2Aidl(entry));
142 break;
143
144 /* Keystore-enforced */
145 case KM_TAG_ACTIVE_DATETIME:
146 case KM_TAG_ALL_APPLICATIONS:
147 case KM_TAG_ALL_USERS:
148 case KM_TAG_ORIGINATION_EXPIRE_DATETIME:
149 case KM_TAG_USAGE_EXPIRE_DATETIME:
150 case KM_TAG_USER_ID:
151 case KM_TAG_USAGE_COUNT_LIMIT:
152 keystoreEnforced.authorizations.push_back(kmParam2Aidl(entry));
153 break;
Shawn Willden763166c2021-01-10 19:45:01 -0700154 }
155 }
156
Shawn Willden09f47102021-01-15 15:21:56 -0700157 vector<KeyCharacteristics> retval;
158 retval.reserve(2);
159 if (!keyMintEnforced.authorizations.empty()) retval.push_back(std::move(keyMintEnforced));
160 if (!keystoreEnforced.authorizations.empty()) retval.push_back(std::move(keystoreEnforced));
161
162 return retval;
Shawn Willden763166c2021-01-10 19:45:01 -0700163}
164
Shawn Willdend7d4f312020-12-21 08:31:01 -0700165Certificate convertCertificate(const keymaster_blob_t& cert) {
166 return {std::vector<uint8_t>(cert.data, cert.data + cert.data_length)};
167}
168
169vector<Certificate> convertCertificateChain(const CertificateChain& chain) {
170 vector<Certificate> retval;
171 retval.reserve(chain.entry_count);
172 std::transform(chain.begin(), chain.end(), std::back_inserter(retval), convertCertificate);
173 return retval;
174}
175
Shawn Willden763166c2021-01-10 19:45:01 -0700176} // namespace
177
Shawn Willden815e8962020-12-11 13:05:27 +0000178constexpr size_t kOperationTableSize = 16;
179
180AndroidKeyMintDevice::AndroidKeyMintDevice(SecurityLevel securityLevel)
181 : impl_(new ::keymaster::AndroidKeymaster(
182 [&]() -> auto {
183 auto context = new PureSoftKeymasterContext(
184 KmVersion::KEYMINT_1, static_cast<keymaster_security_level_t>(securityLevel));
185 context->SetSystemVersion(::keymaster::GetOsVersion(),
186 ::keymaster::GetOsPatchlevel());
187 return context;
188 }(),
189 kOperationTableSize)),
190 securityLevel_(securityLevel) {}
191
192AndroidKeyMintDevice::~AndroidKeyMintDevice() {}
193
194ScopedAStatus AndroidKeyMintDevice::getHardwareInfo(KeyMintHardwareInfo* info) {
195 info->versionNumber = 1;
196 info->securityLevel = securityLevel_;
197 info->keyMintName = "FakeKeyMintDevice";
198 info->keyMintAuthorName = "Google";
199
200 return ScopedAStatus::ok();
201}
202
Shawn Willden815e8962020-12-11 13:05:27 +0000203ScopedAStatus AndroidKeyMintDevice::addRngEntropy(const vector<uint8_t>& data) {
204 if (data.size() == 0) {
205 return ScopedAStatus::ok();
206 }
207
Shawn Willden950eb0b2021-01-06 19:15:29 +0000208 AddEntropyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000209 request.random_data.Reinitialize(data.data(), data.size());
210
Shawn Willden950eb0b2021-01-06 19:15:29 +0000211 AddEntropyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000212 impl_->AddRngEntropy(request, &response);
213
214 return kmError2ScopedAStatus(response.error);
215}
216
217ScopedAStatus AndroidKeyMintDevice::generateKey(const vector<KeyParameter>& keyParams,
Shawn Willden763166c2021-01-10 19:45:01 -0700218 KeyCreationResult* creationResult) {
Shawn Willden815e8962020-12-11 13:05:27 +0000219
Shawn Willden950eb0b2021-01-06 19:15:29 +0000220 GenerateKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000221 request.key_description.Reinitialize(KmParamSet(keyParams));
222
Shawn Willden950eb0b2021-01-06 19:15:29 +0000223 GenerateKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000224 impl_->GenerateKey(request, &response);
225
226 if (response.error != KM_ERROR_OK) {
227 // Note a key difference between this current aidl and previous hal, is
228 // that hal returns void where as aidl returns the error status. If
229 // aidl returns error, then aidl will not return any change you may make
230 // to the out parameters. This is quite different from hal where all
231 // output variable can be modified due to hal returning void.
232 //
233 // So the caller need to be aware not to expect aidl functions to clear
234 // the output variables for you in case of error. If you left some
235 // wrong data set in the out parameters, they will stay there.
236 return kmError2ScopedAStatus(response.error);
237 }
238
Shawn Willden763166c2021-01-10 19:45:01 -0700239 creationResult->keyBlob = kmBlob2vector(response.key_blob);
240 creationResult->keyCharacteristics =
241 convertKeyCharacteristics(securityLevel_, response.unenforced, response.enforced);
Shawn Willdend7d4f312020-12-21 08:31:01 -0700242 creationResult->certificateChain = convertCertificateChain(response.certificate_chain);
Shawn Willden815e8962020-12-11 13:05:27 +0000243 return ScopedAStatus::ok();
244}
245
246ScopedAStatus AndroidKeyMintDevice::importKey(const vector<KeyParameter>& keyParams,
247 KeyFormat keyFormat, const vector<uint8_t>& keyData,
Shawn Willden763166c2021-01-10 19:45:01 -0700248 KeyCreationResult* creationResult) {
Shawn Willden815e8962020-12-11 13:05:27 +0000249
Shawn Willden950eb0b2021-01-06 19:15:29 +0000250 ImportKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000251 request.key_description.Reinitialize(KmParamSet(keyParams));
252 request.key_format = legacy_enum_conversion(keyFormat);
Shawn Willden6e20ea42020-12-21 18:52:34 -0700253 request.key_data = KeymasterKeyBlob(keyData.data(), keyData.size());
Shawn Willden815e8962020-12-11 13:05:27 +0000254
Shawn Willden950eb0b2021-01-06 19:15:29 +0000255 ImportKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000256 impl_->ImportKey(request, &response);
257
258 if (response.error != KM_ERROR_OK) {
259 return kmError2ScopedAStatus(response.error);
260 }
261
Shawn Willden763166c2021-01-10 19:45:01 -0700262 creationResult->keyBlob = kmBlob2vector(response.key_blob);
263 creationResult->keyCharacteristics =
264 convertKeyCharacteristics(securityLevel_, response.unenforced, response.enforced);
Shawn Willdend7d4f312020-12-21 08:31:01 -0700265 creationResult->certificateChain = convertCertificateChain(response.certificate_chain);
Shawn Willden815e8962020-12-11 13:05:27 +0000266
267 return ScopedAStatus::ok();
268}
269
Shawn Willden763166c2021-01-10 19:45:01 -0700270ScopedAStatus AndroidKeyMintDevice::importWrappedKey(const vector<uint8_t>& wrappedKeyData,
271 const vector<uint8_t>& wrappingKeyBlob,
272 const vector<uint8_t>& maskingKey,
273 const vector<KeyParameter>& unwrappingParams,
274 int64_t passwordSid, int64_t biometricSid,
275 KeyCreationResult* creationResult) {
Shawn Willden815e8962020-12-11 13:05:27 +0000276
Shawn Willden950eb0b2021-01-06 19:15:29 +0000277 ImportWrappedKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000278 request.SetWrappedMaterial(wrappedKeyData.data(), wrappedKeyData.size());
279 request.SetWrappingMaterial(wrappingKeyBlob.data(), wrappingKeyBlob.size());
280 request.SetMaskingKeyMaterial(maskingKey.data(), maskingKey.size());
281 request.additional_params.Reinitialize(KmParamSet(unwrappingParams));
282 request.password_sid = static_cast<uint64_t>(passwordSid);
283 request.biometric_sid = static_cast<uint64_t>(biometricSid);
284
Shawn Willden950eb0b2021-01-06 19:15:29 +0000285 ImportWrappedKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000286 impl_->ImportWrappedKey(request, &response);
287
288 if (response.error != KM_ERROR_OK) {
289 return kmError2ScopedAStatus(response.error);
290 }
291
Shawn Willden763166c2021-01-10 19:45:01 -0700292 creationResult->keyBlob = kmBlob2vector(response.key_blob);
293 creationResult->keyCharacteristics =
294 convertKeyCharacteristics(securityLevel_, response.unenforced, response.enforced);
Shawn Willdend7d4f312020-12-21 08:31:01 -0700295 creationResult->certificateChain = convertCertificateChain(response.certificate_chain);
Shawn Willden815e8962020-12-11 13:05:27 +0000296
297 return ScopedAStatus::ok();
298}
299
300ScopedAStatus AndroidKeyMintDevice::upgradeKey(const vector<uint8_t>& keyBlobToUpgrade,
301 const vector<KeyParameter>& upgradeParams,
302 vector<uint8_t>* keyBlob) {
303
Shawn Willden950eb0b2021-01-06 19:15:29 +0000304 UpgradeKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000305 request.SetKeyMaterial(keyBlobToUpgrade.data(), keyBlobToUpgrade.size());
306 request.upgrade_params.Reinitialize(KmParamSet(upgradeParams));
307
Shawn Willden950eb0b2021-01-06 19:15:29 +0000308 UpgradeKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000309 impl_->UpgradeKey(request, &response);
310
311 if (response.error != KM_ERROR_OK) {
312 return kmError2ScopedAStatus(response.error);
313 }
314
315 *keyBlob = kmBlob2vector(response.upgraded_key);
316 return ScopedAStatus::ok();
317}
318
319ScopedAStatus AndroidKeyMintDevice::deleteKey(const vector<uint8_t>& keyBlob) {
Shawn Willden950eb0b2021-01-06 19:15:29 +0000320 DeleteKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000321 request.SetKeyMaterial(keyBlob.data(), keyBlob.size());
322
Shawn Willden950eb0b2021-01-06 19:15:29 +0000323 DeleteKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000324 impl_->DeleteKey(request, &response);
325
326 return kmError2ScopedAStatus(response.error);
327}
328
329ScopedAStatus AndroidKeyMintDevice::deleteAllKeys() {
330 // There's nothing to be done to delete software key blobs.
Shawn Willden950eb0b2021-01-06 19:15:29 +0000331 DeleteAllKeysRequest request(impl_->message_version());
332 DeleteAllKeysResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000333 impl_->DeleteAllKeys(request, &response);
334
335 return kmError2ScopedAStatus(response.error);
336}
337
338ScopedAStatus AndroidKeyMintDevice::destroyAttestationIds() {
339 return kmError2ScopedAStatus(KM_ERROR_UNIMPLEMENTED);
340}
341
342ScopedAStatus AndroidKeyMintDevice::begin(KeyPurpose purpose, const vector<uint8_t>& keyBlob,
343 const vector<KeyParameter>& params,
344 const HardwareAuthToken& authToken, BeginResult* result) {
345
Shawn Willden950eb0b2021-01-06 19:15:29 +0000346 BeginOperationRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000347 request.purpose = legacy_enum_conversion(purpose);
348 request.SetKeyMaterial(keyBlob.data(), keyBlob.size());
349 request.additional_params.Reinitialize(KmParamSet(params));
350
351 vector<uint8_t> vector_token = authToken2AidlVec(authToken);
352 request.additional_params.push_back(
353 TAG_AUTH_TOKEN, reinterpret_cast<uint8_t*>(vector_token.data()), vector_token.size());
354
Shawn Willden950eb0b2021-01-06 19:15:29 +0000355 BeginOperationResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000356 impl_->BeginOperation(request, &response);
357
358 if (response.error != KM_ERROR_OK) {
359 return kmError2ScopedAStatus(response.error);
360 }
361
362 result->params = kmParamSet2Aidl(response.output_params);
363 result->challenge = response.op_handle;
364 result->operation =
365 ndk::SharedRefBase::make<AndroidKeyMintOperation>(impl_, response.op_handle);
366 return ScopedAStatus::ok();
367}
368
369IKeyMintDevice* CreateKeyMintDevice(SecurityLevel securityLevel) {
370
371 return ::new AndroidKeyMintDevice(securityLevel);
372}
373
374} // namespace aidl::android::hardware::security::keymint