blob: 690abf2212e773e1ae5010419e24a999fca905eb [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) {
44 // We're pretending to be TRUSTED_ENVIRONMENT or STRONGBOX. Only the entries in hw_enforced
45 // should be returned.
Shawn Willden09f47102021-01-15 15:21:56 -070046 keyMintEnforced.authorizations = kmParamSet2Aidl(hw_enforced);
47 return {std::move(keyMintEnforced)};
Shawn Willden763166c2021-01-10 19:45:01 -070048 }
49
Shawn Willden09f47102021-01-15 15:21:56 -070050 KeyCharacteristics keystoreEnforced{SecurityLevel::KEYSTORE, {}};
Shawn Willden763166c2021-01-10 19:45:01 -070051 CHECK(hw_enforced.empty()) << "Hardware-enforced list is non-empty for pure SW KeyMint";
52
Shawn Willden09f47102021-01-15 15:21:56 -070053 // This is a pure software implementation, so all tags are in sw_enforced.
54 // We need to walk through the SW-enforced list and figure out which tags to
55 // return in the software list and which in the keystore list.
Shawn Willden763166c2021-01-10 19:45:01 -070056
57 for (auto& entry : sw_enforced) {
58 switch (entry.tag) {
59 /* Invalid and unused */
60 case KM_TAG_ECIES_SINGLE_HASH_MODE:
61 case KM_TAG_INVALID:
62 case KM_TAG_KDF:
Shawn Willden09f47102021-01-15 15:21:56 -070063 case KM_TAG_ROLLBACK_RESISTANCE:
Shawn Willden763166c2021-01-10 19:45:01 -070064 CHECK(false) << "We shouldn't see tag " << entry.tag;
65 break;
66
67 /* Unimplemented */
68 case KM_TAG_ALLOW_WHILE_ON_BODY:
Shawn Willden763166c2021-01-10 19:45:01 -070069 case KM_TAG_BOOTLOADER_ONLY:
70 case KM_TAG_EARLY_BOOT_ONLY:
Shawn Willden763166c2021-01-10 19:45:01 -070071 case KM_TAG_ROLLBACK_RESISTANT:
72 case KM_TAG_STORAGE_KEY:
Shawn Willden09f47102021-01-15 15:21:56 -070073 case KM_TAG_TRUSTED_CONFIRMATION_REQUIRED:
74 case KM_TAG_TRUSTED_USER_PRESENCE_REQUIRED:
Shawn Willden763166c2021-01-10 19:45:01 -070075 break;
76
77 /* Unenforceable */
Shawn Willden763166c2021-01-10 19:45:01 -070078 case KM_TAG_CREATION_DATETIME:
Shawn Willden763166c2021-01-10 19:45:01 -070079 break;
80
81 /* Disallowed in KeyCharacteristics */
82 case KM_TAG_APPLICATION_DATA:
Shawn Willden09f47102021-01-15 15:21:56 -070083 case KM_TAG_ATTESTATION_APPLICATION_ID:
Shawn Willden763166c2021-01-10 19:45:01 -070084 break;
85
86 /* Not key characteristics */
87 case KM_TAG_ASSOCIATED_DATA:
Shawn Willden763166c2021-01-10 19:45:01 -070088 case KM_TAG_ATTESTATION_CHALLENGE:
89 case KM_TAG_ATTESTATION_ID_BRAND:
90 case KM_TAG_ATTESTATION_ID_DEVICE:
91 case KM_TAG_ATTESTATION_ID_IMEI:
92 case KM_TAG_ATTESTATION_ID_MANUFACTURER:
93 case KM_TAG_ATTESTATION_ID_MEID:
94 case KM_TAG_ATTESTATION_ID_MODEL:
95 case KM_TAG_ATTESTATION_ID_PRODUCT:
96 case KM_TAG_ATTESTATION_ID_SERIAL:
97 case KM_TAG_AUTH_TOKEN:
98 case KM_TAG_CERTIFICATE_SERIAL:
99 case KM_TAG_CERTIFICATE_SUBJECT:
Janis Danisevskisa5780e22021-01-31 22:05:22 -0800100 case KM_TAG_CERTIFICATE_NOT_AFTER:
101 case KM_TAG_CERTIFICATE_NOT_BEFORE:
Shawn Willden763166c2021-01-10 19:45:01 -0700102 case KM_TAG_CONFIRMATION_TOKEN:
103 case KM_TAG_DEVICE_UNIQUE_ATTESTATION:
104 case KM_TAG_IDENTITY_CREDENTIAL_KEY:
105 case KM_TAG_MAC_LENGTH:
106 case KM_TAG_NONCE:
107 case KM_TAG_RESET_SINCE_ID_ROTATION:
108 case KM_TAG_ROOT_OF_TRUST:
109 case KM_TAG_UNIQUE_ID:
110 break;
111
Shawn Willden09f47102021-01-15 15:21:56 -0700112 /* KeyMint-enforced */
Shawn Willden763166c2021-01-10 19:45:01 -0700113 case KM_TAG_ALGORITHM:
Shawn Willden09f47102021-01-15 15:21:56 -0700114 case KM_TAG_APPLICATION_ID:
Shawn Willden763166c2021-01-10 19:45:01 -0700115 case KM_TAG_AUTH_TIMEOUT:
116 case KM_TAG_BLOB_USAGE_REQUIREMENTS:
117 case KM_TAG_BLOCK_MODE:
118 case KM_TAG_BOOT_PATCHLEVEL:
119 case KM_TAG_CALLER_NONCE:
120 case KM_TAG_DIGEST:
121 case KM_TAG_EC_CURVE:
122 case KM_TAG_EXPORTABLE:
123 case KM_TAG_INCLUDE_UNIQUE_ID:
124 case KM_TAG_KEY_SIZE:
125 case KM_TAG_MAX_USES_PER_BOOT:
126 case KM_TAG_MIN_MAC_LENGTH:
127 case KM_TAG_MIN_SECONDS_BETWEEN_OPS:
128 case KM_TAG_NO_AUTH_REQUIRED:
129 case KM_TAG_ORIGIN:
130 case KM_TAG_OS_PATCHLEVEL:
131 case KM_TAG_OS_VERSION:
132 case KM_TAG_PADDING:
133 case KM_TAG_PURPOSE:
134 case KM_TAG_RSA_OAEP_MGF_DIGEST:
135 case KM_TAG_RSA_PUBLIC_EXPONENT:
136 case KM_TAG_UNLOCKED_DEVICE_REQUIRED:
137 case KM_TAG_USER_AUTH_TYPE:
138 case KM_TAG_USER_SECURE_ID:
139 case KM_TAG_VENDOR_PATCHLEVEL:
Shawn Willden09f47102021-01-15 15:21:56 -0700140 keyMintEnforced.authorizations.push_back(kmParam2Aidl(entry));
141 break;
142
143 /* Keystore-enforced */
144 case KM_TAG_ACTIVE_DATETIME:
145 case KM_TAG_ALL_APPLICATIONS:
146 case KM_TAG_ALL_USERS:
147 case KM_TAG_ORIGINATION_EXPIRE_DATETIME:
148 case KM_TAG_USAGE_EXPIRE_DATETIME:
149 case KM_TAG_USER_ID:
150 case KM_TAG_USAGE_COUNT_LIMIT:
151 keystoreEnforced.authorizations.push_back(kmParam2Aidl(entry));
152 break;
Shawn Willden763166c2021-01-10 19:45:01 -0700153 }
154 }
155
Shawn Willden09f47102021-01-15 15:21:56 -0700156 vector<KeyCharacteristics> retval;
157 retval.reserve(2);
158 if (!keyMintEnforced.authorizations.empty()) retval.push_back(std::move(keyMintEnforced));
159 if (!keystoreEnforced.authorizations.empty()) retval.push_back(std::move(keystoreEnforced));
160
161 return retval;
Shawn Willden763166c2021-01-10 19:45:01 -0700162}
163
Shawn Willdend7d4f312020-12-21 08:31:01 -0700164Certificate convertCertificate(const keymaster_blob_t& cert) {
165 return {std::vector<uint8_t>(cert.data, cert.data + cert.data_length)};
166}
167
168vector<Certificate> convertCertificateChain(const CertificateChain& chain) {
169 vector<Certificate> retval;
170 retval.reserve(chain.entry_count);
171 std::transform(chain.begin(), chain.end(), std::back_inserter(retval), convertCertificate);
172 return retval;
173}
174
Shawn Willden763166c2021-01-10 19:45:01 -0700175} // namespace
176
Shawn Willden815e8962020-12-11 13:05:27 +0000177constexpr size_t kOperationTableSize = 16;
178
179AndroidKeyMintDevice::AndroidKeyMintDevice(SecurityLevel securityLevel)
180 : impl_(new ::keymaster::AndroidKeymaster(
181 [&]() -> auto {
182 auto context = new PureSoftKeymasterContext(
183 KmVersion::KEYMINT_1, static_cast<keymaster_security_level_t>(securityLevel));
184 context->SetSystemVersion(::keymaster::GetOsVersion(),
185 ::keymaster::GetOsPatchlevel());
186 return context;
187 }(),
188 kOperationTableSize)),
189 securityLevel_(securityLevel) {}
190
191AndroidKeyMintDevice::~AndroidKeyMintDevice() {}
192
193ScopedAStatus AndroidKeyMintDevice::getHardwareInfo(KeyMintHardwareInfo* info) {
194 info->versionNumber = 1;
195 info->securityLevel = securityLevel_;
196 info->keyMintName = "FakeKeyMintDevice";
197 info->keyMintAuthorName = "Google";
198
199 return ScopedAStatus::ok();
200}
201
Shawn Willden815e8962020-12-11 13:05:27 +0000202ScopedAStatus AndroidKeyMintDevice::addRngEntropy(const vector<uint8_t>& data) {
203 if (data.size() == 0) {
204 return ScopedAStatus::ok();
205 }
206
Shawn Willden950eb0b2021-01-06 19:15:29 +0000207 AddEntropyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000208 request.random_data.Reinitialize(data.data(), data.size());
209
Shawn Willden950eb0b2021-01-06 19:15:29 +0000210 AddEntropyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000211 impl_->AddRngEntropy(request, &response);
212
213 return kmError2ScopedAStatus(response.error);
214}
215
216ScopedAStatus AndroidKeyMintDevice::generateKey(const vector<KeyParameter>& keyParams,
Shawn Willden763166c2021-01-10 19:45:01 -0700217 KeyCreationResult* creationResult) {
Shawn Willden815e8962020-12-11 13:05:27 +0000218
Shawn Willden950eb0b2021-01-06 19:15:29 +0000219 GenerateKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000220 request.key_description.Reinitialize(KmParamSet(keyParams));
221
Shawn Willden950eb0b2021-01-06 19:15:29 +0000222 GenerateKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000223 impl_->GenerateKey(request, &response);
224
225 if (response.error != KM_ERROR_OK) {
226 // Note a key difference between this current aidl and previous hal, is
227 // that hal returns void where as aidl returns the error status. If
228 // aidl returns error, then aidl will not return any change you may make
229 // to the out parameters. This is quite different from hal where all
230 // output variable can be modified due to hal returning void.
231 //
232 // So the caller need to be aware not to expect aidl functions to clear
233 // the output variables for you in case of error. If you left some
234 // wrong data set in the out parameters, they will stay there.
235 return kmError2ScopedAStatus(response.error);
236 }
237
Shawn Willden763166c2021-01-10 19:45:01 -0700238 creationResult->keyBlob = kmBlob2vector(response.key_blob);
239 creationResult->keyCharacteristics =
240 convertKeyCharacteristics(securityLevel_, response.unenforced, response.enforced);
Shawn Willdend7d4f312020-12-21 08:31:01 -0700241 creationResult->certificateChain = convertCertificateChain(response.certificate_chain);
Shawn Willden815e8962020-12-11 13:05:27 +0000242 return ScopedAStatus::ok();
243}
244
245ScopedAStatus AndroidKeyMintDevice::importKey(const vector<KeyParameter>& keyParams,
246 KeyFormat keyFormat, const vector<uint8_t>& keyData,
Shawn Willden763166c2021-01-10 19:45:01 -0700247 KeyCreationResult* creationResult) {
Shawn Willden815e8962020-12-11 13:05:27 +0000248
Shawn Willden950eb0b2021-01-06 19:15:29 +0000249 ImportKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000250 request.key_description.Reinitialize(KmParamSet(keyParams));
251 request.key_format = legacy_enum_conversion(keyFormat);
Shawn Willden6e20ea42020-12-21 18:52:34 -0700252 request.key_data = KeymasterKeyBlob(keyData.data(), keyData.size());
Shawn Willden815e8962020-12-11 13:05:27 +0000253
Shawn Willden950eb0b2021-01-06 19:15:29 +0000254 ImportKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000255 impl_->ImportKey(request, &response);
256
257 if (response.error != KM_ERROR_OK) {
258 return kmError2ScopedAStatus(response.error);
259 }
260
Shawn Willden763166c2021-01-10 19:45:01 -0700261 creationResult->keyBlob = kmBlob2vector(response.key_blob);
262 creationResult->keyCharacteristics =
263 convertKeyCharacteristics(securityLevel_, response.unenforced, response.enforced);
Shawn Willdend7d4f312020-12-21 08:31:01 -0700264 creationResult->certificateChain = convertCertificateChain(response.certificate_chain);
Shawn Willden815e8962020-12-11 13:05:27 +0000265
266 return ScopedAStatus::ok();
267}
268
Shawn Willden763166c2021-01-10 19:45:01 -0700269ScopedAStatus AndroidKeyMintDevice::importWrappedKey(const vector<uint8_t>& wrappedKeyData,
270 const vector<uint8_t>& wrappingKeyBlob,
271 const vector<uint8_t>& maskingKey,
272 const vector<KeyParameter>& unwrappingParams,
273 int64_t passwordSid, int64_t biometricSid,
274 KeyCreationResult* creationResult) {
Shawn Willden815e8962020-12-11 13:05:27 +0000275
Shawn Willden950eb0b2021-01-06 19:15:29 +0000276 ImportWrappedKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000277 request.SetWrappedMaterial(wrappedKeyData.data(), wrappedKeyData.size());
278 request.SetWrappingMaterial(wrappingKeyBlob.data(), wrappingKeyBlob.size());
279 request.SetMaskingKeyMaterial(maskingKey.data(), maskingKey.size());
280 request.additional_params.Reinitialize(KmParamSet(unwrappingParams));
281 request.password_sid = static_cast<uint64_t>(passwordSid);
282 request.biometric_sid = static_cast<uint64_t>(biometricSid);
283
Shawn Willden950eb0b2021-01-06 19:15:29 +0000284 ImportWrappedKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000285 impl_->ImportWrappedKey(request, &response);
286
287 if (response.error != KM_ERROR_OK) {
288 return kmError2ScopedAStatus(response.error);
289 }
290
Shawn Willden763166c2021-01-10 19:45:01 -0700291 creationResult->keyBlob = kmBlob2vector(response.key_blob);
292 creationResult->keyCharacteristics =
293 convertKeyCharacteristics(securityLevel_, response.unenforced, response.enforced);
Shawn Willdend7d4f312020-12-21 08:31:01 -0700294 creationResult->certificateChain = convertCertificateChain(response.certificate_chain);
Shawn Willden815e8962020-12-11 13:05:27 +0000295
296 return ScopedAStatus::ok();
297}
298
299ScopedAStatus AndroidKeyMintDevice::upgradeKey(const vector<uint8_t>& keyBlobToUpgrade,
300 const vector<KeyParameter>& upgradeParams,
301 vector<uint8_t>* keyBlob) {
302
Shawn Willden950eb0b2021-01-06 19:15:29 +0000303 UpgradeKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000304 request.SetKeyMaterial(keyBlobToUpgrade.data(), keyBlobToUpgrade.size());
305 request.upgrade_params.Reinitialize(KmParamSet(upgradeParams));
306
Shawn Willden950eb0b2021-01-06 19:15:29 +0000307 UpgradeKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000308 impl_->UpgradeKey(request, &response);
309
310 if (response.error != KM_ERROR_OK) {
311 return kmError2ScopedAStatus(response.error);
312 }
313
314 *keyBlob = kmBlob2vector(response.upgraded_key);
315 return ScopedAStatus::ok();
316}
317
318ScopedAStatus AndroidKeyMintDevice::deleteKey(const vector<uint8_t>& keyBlob) {
Shawn Willden950eb0b2021-01-06 19:15:29 +0000319 DeleteKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000320 request.SetKeyMaterial(keyBlob.data(), keyBlob.size());
321
Shawn Willden950eb0b2021-01-06 19:15:29 +0000322 DeleteKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000323 impl_->DeleteKey(request, &response);
324
325 return kmError2ScopedAStatus(response.error);
326}
327
328ScopedAStatus AndroidKeyMintDevice::deleteAllKeys() {
329 // There's nothing to be done to delete software key blobs.
Shawn Willden950eb0b2021-01-06 19:15:29 +0000330 DeleteAllKeysRequest request(impl_->message_version());
331 DeleteAllKeysResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000332 impl_->DeleteAllKeys(request, &response);
333
334 return kmError2ScopedAStatus(response.error);
335}
336
337ScopedAStatus AndroidKeyMintDevice::destroyAttestationIds() {
338 return kmError2ScopedAStatus(KM_ERROR_UNIMPLEMENTED);
339}
340
341ScopedAStatus AndroidKeyMintDevice::begin(KeyPurpose purpose, const vector<uint8_t>& keyBlob,
342 const vector<KeyParameter>& params,
343 const HardwareAuthToken& authToken, BeginResult* result) {
344
Shawn Willden950eb0b2021-01-06 19:15:29 +0000345 BeginOperationRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000346 request.purpose = legacy_enum_conversion(purpose);
347 request.SetKeyMaterial(keyBlob.data(), keyBlob.size());
348 request.additional_params.Reinitialize(KmParamSet(params));
349
350 vector<uint8_t> vector_token = authToken2AidlVec(authToken);
351 request.additional_params.push_back(
352 TAG_AUTH_TOKEN, reinterpret_cast<uint8_t*>(vector_token.data()), vector_token.size());
353
Shawn Willden950eb0b2021-01-06 19:15:29 +0000354 BeginOperationResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000355 impl_->BeginOperation(request, &response);
356
357 if (response.error != KM_ERROR_OK) {
358 return kmError2ScopedAStatus(response.error);
359 }
360
361 result->params = kmParamSet2Aidl(response.output_params);
362 result->challenge = response.op_handle;
363 result->operation =
364 ndk::SharedRefBase::make<AndroidKeyMintOperation>(impl_, response.op_handle);
365 return ScopedAStatus::ok();
366}
367
368IKeyMintDevice* CreateKeyMintDevice(SecurityLevel securityLevel) {
369
370 return ::new AndroidKeyMintDevice(securityLevel);
371}
372
373} // namespace aidl::android::hardware::security::keymint