Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 1 | /* |
| 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 Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 18 | #include <android-base/logging.h> |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 19 | |
| 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 | |
| 31 | namespace aidl::android::hardware::security::keymint { |
| 32 | |
Shawn Willden | 9a3792e | 2021-04-08 09:38:14 -0600 | [diff] [blame] | 33 | using namespace keymaster; // NOLINT(google-build-using-namespace) |
| 34 | |
| 35 | using km_utils::authToken2AidlVec; |
| 36 | using km_utils::kmBlob2vector; |
| 37 | using km_utils::kmError2ScopedAStatus; |
| 38 | using km_utils::kmParam2Aidl; |
| 39 | using km_utils::KmParamSet; |
| 40 | using km_utils::kmParamSet2Aidl; |
| 41 | using km_utils::legacy_enum_conversion; |
Chirag Pathak | b292e9a | 2021-02-02 07:28:09 +0000 | [diff] [blame] | 42 | using secureclock::TimeStampToken; |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 43 | |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 44 | namespace { |
| 45 | |
| 46 | vector<KeyCharacteristics> convertKeyCharacteristics(SecurityLevel keyMintSecurityLevel, |
David Drysdale | b90937d | 2021-03-08 13:52:17 +0000 | [diff] [blame^] | 47 | const AuthorizationSet& requestParams, |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 48 | const AuthorizationSet& sw_enforced, |
| 49 | const AuthorizationSet& hw_enforced) { |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 50 | KeyCharacteristics keyMintEnforced{keyMintSecurityLevel, {}}; |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 51 | |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 52 | if (keyMintSecurityLevel != SecurityLevel::SOFTWARE) { |
Qi Wu | f7c8e9d | 2021-02-11 03:14:52 +0800 | [diff] [blame] | 53 | // We're pretending to be TRUSTED_ENVIRONMENT or STRONGBOX. |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 54 | keyMintEnforced.authorizations = kmParamSet2Aidl(hw_enforced); |
Qi Wu | f7c8e9d | 2021-02-11 03:14:52 +0800 | [diff] [blame] | 55 | // Put all the software authorizations in the keystore list. |
| 56 | KeyCharacteristics keystoreEnforced{SecurityLevel::KEYSTORE, kmParamSet2Aidl(sw_enforced)}; |
| 57 | return {std::move(keyMintEnforced), std::move(keystoreEnforced)}; |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 60 | KeyCharacteristics keystoreEnforced{SecurityLevel::KEYSTORE, {}}; |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 61 | CHECK(hw_enforced.empty()) << "Hardware-enforced list is non-empty for pure SW KeyMint"; |
| 62 | |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 63 | // This is a pure software implementation, so all tags are in sw_enforced. |
| 64 | // We need to walk through the SW-enforced list and figure out which tags to |
| 65 | // return in the software list and which in the keystore list. |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 66 | |
| 67 | for (auto& entry : sw_enforced) { |
| 68 | switch (entry.tag) { |
| 69 | /* Invalid and unused */ |
| 70 | case KM_TAG_ECIES_SINGLE_HASH_MODE: |
| 71 | case KM_TAG_INVALID: |
| 72 | case KM_TAG_KDF: |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 73 | case KM_TAG_ROLLBACK_RESISTANCE: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 74 | CHECK(false) << "We shouldn't see tag " << entry.tag; |
| 75 | break; |
| 76 | |
| 77 | /* Unimplemented */ |
| 78 | case KM_TAG_ALLOW_WHILE_ON_BODY: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 79 | case KM_TAG_BOOTLOADER_ONLY: |
| 80 | case KM_TAG_EARLY_BOOT_ONLY: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 81 | case KM_TAG_ROLLBACK_RESISTANT: |
| 82 | case KM_TAG_STORAGE_KEY: |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 83 | case KM_TAG_TRUSTED_CONFIRMATION_REQUIRED: |
| 84 | case KM_TAG_TRUSTED_USER_PRESENCE_REQUIRED: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 85 | break; |
| 86 | |
David Drysdale | b90937d | 2021-03-08 13:52:17 +0000 | [diff] [blame^] | 87 | /* Keystore-enforced if not locally generated. */ |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 88 | case KM_TAG_CREATION_DATETIME: |
David Drysdale | b90937d | 2021-03-08 13:52:17 +0000 | [diff] [blame^] | 89 | // A KeyMaster implementation is required to add this tag to generated/imported keys. |
| 90 | // A KeyMint implementation is not required to create this tag, only to echo it back if |
| 91 | // it was included in the key generation/import request. |
| 92 | if (requestParams.Contains(KM_TAG_CREATION_DATETIME)) { |
| 93 | keystoreEnforced.authorizations.push_back(kmParam2Aidl(entry)); |
| 94 | } |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 95 | break; |
| 96 | |
| 97 | /* Disallowed in KeyCharacteristics */ |
| 98 | case KM_TAG_APPLICATION_DATA: |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 99 | case KM_TAG_ATTESTATION_APPLICATION_ID: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 100 | break; |
| 101 | |
| 102 | /* Not key characteristics */ |
| 103 | case KM_TAG_ASSOCIATED_DATA: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 104 | case KM_TAG_ATTESTATION_CHALLENGE: |
| 105 | case KM_TAG_ATTESTATION_ID_BRAND: |
| 106 | case KM_TAG_ATTESTATION_ID_DEVICE: |
| 107 | case KM_TAG_ATTESTATION_ID_IMEI: |
| 108 | case KM_TAG_ATTESTATION_ID_MANUFACTURER: |
| 109 | case KM_TAG_ATTESTATION_ID_MEID: |
| 110 | case KM_TAG_ATTESTATION_ID_MODEL: |
| 111 | case KM_TAG_ATTESTATION_ID_PRODUCT: |
| 112 | case KM_TAG_ATTESTATION_ID_SERIAL: |
| 113 | case KM_TAG_AUTH_TOKEN: |
| 114 | case KM_TAG_CERTIFICATE_SERIAL: |
| 115 | case KM_TAG_CERTIFICATE_SUBJECT: |
Janis Danisevskis | a5780e2 | 2021-01-31 22:05:22 -0800 | [diff] [blame] | 116 | case KM_TAG_CERTIFICATE_NOT_AFTER: |
| 117 | case KM_TAG_CERTIFICATE_NOT_BEFORE: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 118 | case KM_TAG_CONFIRMATION_TOKEN: |
| 119 | case KM_TAG_DEVICE_UNIQUE_ATTESTATION: |
| 120 | case KM_TAG_IDENTITY_CREDENTIAL_KEY: |
| 121 | case KM_TAG_MAC_LENGTH: |
| 122 | case KM_TAG_NONCE: |
| 123 | case KM_TAG_RESET_SINCE_ID_ROTATION: |
| 124 | case KM_TAG_ROOT_OF_TRUST: |
| 125 | case KM_TAG_UNIQUE_ID: |
| 126 | break; |
| 127 | |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 128 | /* KeyMint-enforced */ |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 129 | case KM_TAG_ALGORITHM: |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 130 | case KM_TAG_APPLICATION_ID: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 131 | case KM_TAG_AUTH_TIMEOUT: |
| 132 | case KM_TAG_BLOB_USAGE_REQUIREMENTS: |
| 133 | case KM_TAG_BLOCK_MODE: |
| 134 | case KM_TAG_BOOT_PATCHLEVEL: |
| 135 | case KM_TAG_CALLER_NONCE: |
| 136 | case KM_TAG_DIGEST: |
| 137 | case KM_TAG_EC_CURVE: |
| 138 | case KM_TAG_EXPORTABLE: |
| 139 | case KM_TAG_INCLUDE_UNIQUE_ID: |
| 140 | case KM_TAG_KEY_SIZE: |
| 141 | case KM_TAG_MAX_USES_PER_BOOT: |
| 142 | case KM_TAG_MIN_MAC_LENGTH: |
| 143 | case KM_TAG_MIN_SECONDS_BETWEEN_OPS: |
| 144 | case KM_TAG_NO_AUTH_REQUIRED: |
| 145 | case KM_TAG_ORIGIN: |
| 146 | case KM_TAG_OS_PATCHLEVEL: |
| 147 | case KM_TAG_OS_VERSION: |
| 148 | case KM_TAG_PADDING: |
| 149 | case KM_TAG_PURPOSE: |
| 150 | case KM_TAG_RSA_OAEP_MGF_DIGEST: |
| 151 | case KM_TAG_RSA_PUBLIC_EXPONENT: |
| 152 | case KM_TAG_UNLOCKED_DEVICE_REQUIRED: |
| 153 | case KM_TAG_USER_AUTH_TYPE: |
| 154 | case KM_TAG_USER_SECURE_ID: |
| 155 | case KM_TAG_VENDOR_PATCHLEVEL: |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 156 | keyMintEnforced.authorizations.push_back(kmParam2Aidl(entry)); |
| 157 | break; |
| 158 | |
| 159 | /* Keystore-enforced */ |
| 160 | case KM_TAG_ACTIVE_DATETIME: |
| 161 | case KM_TAG_ALL_APPLICATIONS: |
| 162 | case KM_TAG_ALL_USERS: |
Paul Crowley | 8384653 | 2021-02-10 11:24:50 -0800 | [diff] [blame] | 163 | case KM_TAG_MAX_BOOT_LEVEL: |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 164 | case KM_TAG_ORIGINATION_EXPIRE_DATETIME: |
| 165 | case KM_TAG_USAGE_EXPIRE_DATETIME: |
| 166 | case KM_TAG_USER_ID: |
| 167 | case KM_TAG_USAGE_COUNT_LIMIT: |
| 168 | keystoreEnforced.authorizations.push_back(kmParam2Aidl(entry)); |
| 169 | break; |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 173 | vector<KeyCharacteristics> retval; |
| 174 | retval.reserve(2); |
| 175 | if (!keyMintEnforced.authorizations.empty()) retval.push_back(std::move(keyMintEnforced)); |
| 176 | if (!keystoreEnforced.authorizations.empty()) retval.push_back(std::move(keystoreEnforced)); |
| 177 | |
| 178 | return retval; |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Shawn Willden | d7d4f31 | 2020-12-21 08:31:01 -0700 | [diff] [blame] | 181 | Certificate convertCertificate(const keymaster_blob_t& cert) { |
| 182 | return {std::vector<uint8_t>(cert.data, cert.data + cert.data_length)}; |
| 183 | } |
| 184 | |
| 185 | vector<Certificate> convertCertificateChain(const CertificateChain& chain) { |
| 186 | vector<Certificate> retval; |
| 187 | retval.reserve(chain.entry_count); |
| 188 | std::transform(chain.begin(), chain.end(), std::back_inserter(retval), convertCertificate); |
| 189 | return retval; |
| 190 | } |
| 191 | |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 192 | } // namespace |
| 193 | |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 194 | constexpr size_t kOperationTableSize = 16; |
| 195 | |
| 196 | AndroidKeyMintDevice::AndroidKeyMintDevice(SecurityLevel securityLevel) |
| 197 | : impl_(new ::keymaster::AndroidKeymaster( |
| 198 | [&]() -> auto { |
| 199 | auto context = new PureSoftKeymasterContext( |
| 200 | KmVersion::KEYMINT_1, static_cast<keymaster_security_level_t>(securityLevel)); |
| 201 | context->SetSystemVersion(::keymaster::GetOsVersion(), |
| 202 | ::keymaster::GetOsPatchlevel()); |
| 203 | return context; |
| 204 | }(), |
| 205 | kOperationTableSize)), |
| 206 | securityLevel_(securityLevel) {} |
| 207 | |
| 208 | AndroidKeyMintDevice::~AndroidKeyMintDevice() {} |
| 209 | |
| 210 | ScopedAStatus AndroidKeyMintDevice::getHardwareInfo(KeyMintHardwareInfo* info) { |
| 211 | info->versionNumber = 1; |
| 212 | info->securityLevel = securityLevel_; |
| 213 | info->keyMintName = "FakeKeyMintDevice"; |
| 214 | info->keyMintAuthorName = "Google"; |
Chirag Pathak | b292e9a | 2021-02-02 07:28:09 +0000 | [diff] [blame] | 215 | info->timestampTokenRequired = false; |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 216 | return ScopedAStatus::ok(); |
| 217 | } |
| 218 | |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 219 | ScopedAStatus AndroidKeyMintDevice::addRngEntropy(const vector<uint8_t>& data) { |
| 220 | if (data.size() == 0) { |
| 221 | return ScopedAStatus::ok(); |
| 222 | } |
| 223 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 224 | AddEntropyRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 225 | request.random_data.Reinitialize(data.data(), data.size()); |
| 226 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 227 | AddEntropyResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 228 | impl_->AddRngEntropy(request, &response); |
| 229 | |
| 230 | return kmError2ScopedAStatus(response.error); |
| 231 | } |
| 232 | |
| 233 | ScopedAStatus AndroidKeyMintDevice::generateKey(const vector<KeyParameter>& keyParams, |
Shawn Willden | 44c3888 | 2020-12-21 18:35:13 -0700 | [diff] [blame] | 234 | const optional<AttestationKey>& attestationKey, |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 235 | KeyCreationResult* creationResult) { |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 236 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 237 | GenerateKeyRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 238 | request.key_description.Reinitialize(KmParamSet(keyParams)); |
Shawn Willden | 44c3888 | 2020-12-21 18:35:13 -0700 | [diff] [blame] | 239 | if (attestationKey) { |
| 240 | request.attestation_signing_key_blob = |
| 241 | KeymasterKeyBlob(attestationKey->keyBlob.data(), attestationKey->keyBlob.size()); |
| 242 | request.attest_key_params.Reinitialize(KmParamSet(attestationKey->attestKeyParams)); |
| 243 | request.issuer_subject = KeymasterBlob(attestationKey->issuerSubjectName.data(), |
| 244 | attestationKey->issuerSubjectName.size()); |
| 245 | } |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 246 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 247 | GenerateKeyResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 248 | impl_->GenerateKey(request, &response); |
| 249 | |
| 250 | if (response.error != KM_ERROR_OK) { |
| 251 | // Note a key difference between this current aidl and previous hal, is |
| 252 | // that hal returns void where as aidl returns the error status. If |
| 253 | // aidl returns error, then aidl will not return any change you may make |
| 254 | // to the out parameters. This is quite different from hal where all |
| 255 | // output variable can be modified due to hal returning void. |
| 256 | // |
| 257 | // So the caller need to be aware not to expect aidl functions to clear |
| 258 | // the output variables for you in case of error. If you left some |
| 259 | // wrong data set in the out parameters, they will stay there. |
| 260 | return kmError2ScopedAStatus(response.error); |
| 261 | } |
| 262 | |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 263 | creationResult->keyBlob = kmBlob2vector(response.key_blob); |
David Drysdale | b90937d | 2021-03-08 13:52:17 +0000 | [diff] [blame^] | 264 | creationResult->keyCharacteristics = convertKeyCharacteristics( |
| 265 | securityLevel_, request.key_description, response.unenforced, response.enforced); |
Shawn Willden | d7d4f31 | 2020-12-21 08:31:01 -0700 | [diff] [blame] | 266 | creationResult->certificateChain = convertCertificateChain(response.certificate_chain); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 267 | return ScopedAStatus::ok(); |
| 268 | } |
| 269 | |
| 270 | ScopedAStatus AndroidKeyMintDevice::importKey(const vector<KeyParameter>& keyParams, |
| 271 | KeyFormat keyFormat, const vector<uint8_t>& keyData, |
Shawn Willden | 44c3888 | 2020-12-21 18:35:13 -0700 | [diff] [blame] | 272 | const optional<AttestationKey>& attestationKey, |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 273 | KeyCreationResult* creationResult) { |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 274 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 275 | ImportKeyRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 276 | request.key_description.Reinitialize(KmParamSet(keyParams)); |
| 277 | request.key_format = legacy_enum_conversion(keyFormat); |
Shawn Willden | 6e20ea4 | 2020-12-21 18:52:34 -0700 | [diff] [blame] | 278 | request.key_data = KeymasterKeyBlob(keyData.data(), keyData.size()); |
Shawn Willden | 44c3888 | 2020-12-21 18:35:13 -0700 | [diff] [blame] | 279 | if (attestationKey) { |
| 280 | request.attestation_signing_key_blob = |
| 281 | KeymasterKeyBlob(attestationKey->keyBlob.data(), attestationKey->keyBlob.size()); |
| 282 | request.attest_key_params.Reinitialize(KmParamSet(attestationKey->attestKeyParams)); |
| 283 | request.issuer_subject = KeymasterBlob(attestationKey->issuerSubjectName.data(), |
| 284 | attestationKey->issuerSubjectName.size()); |
| 285 | } |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 286 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 287 | ImportKeyResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 288 | impl_->ImportKey(request, &response); |
| 289 | |
| 290 | if (response.error != KM_ERROR_OK) { |
| 291 | return kmError2ScopedAStatus(response.error); |
| 292 | } |
| 293 | |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 294 | creationResult->keyBlob = kmBlob2vector(response.key_blob); |
David Drysdale | b90937d | 2021-03-08 13:52:17 +0000 | [diff] [blame^] | 295 | creationResult->keyCharacteristics = convertKeyCharacteristics( |
| 296 | securityLevel_, request.key_description, response.unenforced, response.enforced); |
Shawn Willden | d7d4f31 | 2020-12-21 08:31:01 -0700 | [diff] [blame] | 297 | creationResult->certificateChain = convertCertificateChain(response.certificate_chain); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 298 | |
| 299 | return ScopedAStatus::ok(); |
| 300 | } |
| 301 | |
Shawn Willden | 44c3888 | 2020-12-21 18:35:13 -0700 | [diff] [blame] | 302 | ScopedAStatus |
| 303 | AndroidKeyMintDevice::importWrappedKey(const vector<uint8_t>& wrappedKeyData, // |
| 304 | const vector<uint8_t>& wrappingKeyBlob, // |
| 305 | const vector<uint8_t>& maskingKey, // |
| 306 | const vector<KeyParameter>& unwrappingParams, // |
| 307 | int64_t passwordSid, int64_t biometricSid, // |
| 308 | KeyCreationResult* creationResult) { |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 309 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 310 | ImportWrappedKeyRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 311 | request.SetWrappedMaterial(wrappedKeyData.data(), wrappedKeyData.size()); |
| 312 | request.SetWrappingMaterial(wrappingKeyBlob.data(), wrappingKeyBlob.size()); |
| 313 | request.SetMaskingKeyMaterial(maskingKey.data(), maskingKey.size()); |
| 314 | request.additional_params.Reinitialize(KmParamSet(unwrappingParams)); |
| 315 | request.password_sid = static_cast<uint64_t>(passwordSid); |
| 316 | request.biometric_sid = static_cast<uint64_t>(biometricSid); |
| 317 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 318 | ImportWrappedKeyResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 319 | impl_->ImportWrappedKey(request, &response); |
| 320 | |
| 321 | if (response.error != KM_ERROR_OK) { |
| 322 | return kmError2ScopedAStatus(response.error); |
| 323 | } |
| 324 | |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 325 | creationResult->keyBlob = kmBlob2vector(response.key_blob); |
David Drysdale | b90937d | 2021-03-08 13:52:17 +0000 | [diff] [blame^] | 326 | creationResult->keyCharacteristics = convertKeyCharacteristics( |
| 327 | securityLevel_, request.additional_params, response.unenforced, response.enforced); |
Shawn Willden | d7d4f31 | 2020-12-21 08:31:01 -0700 | [diff] [blame] | 328 | creationResult->certificateChain = convertCertificateChain(response.certificate_chain); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 329 | |
| 330 | return ScopedAStatus::ok(); |
| 331 | } |
| 332 | |
| 333 | ScopedAStatus AndroidKeyMintDevice::upgradeKey(const vector<uint8_t>& keyBlobToUpgrade, |
| 334 | const vector<KeyParameter>& upgradeParams, |
| 335 | vector<uint8_t>* keyBlob) { |
| 336 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 337 | UpgradeKeyRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 338 | request.SetKeyMaterial(keyBlobToUpgrade.data(), keyBlobToUpgrade.size()); |
| 339 | request.upgrade_params.Reinitialize(KmParamSet(upgradeParams)); |
| 340 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 341 | UpgradeKeyResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 342 | impl_->UpgradeKey(request, &response); |
| 343 | |
| 344 | if (response.error != KM_ERROR_OK) { |
| 345 | return kmError2ScopedAStatus(response.error); |
| 346 | } |
| 347 | |
| 348 | *keyBlob = kmBlob2vector(response.upgraded_key); |
| 349 | return ScopedAStatus::ok(); |
| 350 | } |
| 351 | |
| 352 | ScopedAStatus AndroidKeyMintDevice::deleteKey(const vector<uint8_t>& keyBlob) { |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 353 | DeleteKeyRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 354 | request.SetKeyMaterial(keyBlob.data(), keyBlob.size()); |
| 355 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 356 | DeleteKeyResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 357 | impl_->DeleteKey(request, &response); |
| 358 | |
| 359 | return kmError2ScopedAStatus(response.error); |
| 360 | } |
| 361 | |
| 362 | ScopedAStatus AndroidKeyMintDevice::deleteAllKeys() { |
| 363 | // There's nothing to be done to delete software key blobs. |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 364 | DeleteAllKeysRequest request(impl_->message_version()); |
| 365 | DeleteAllKeysResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 366 | impl_->DeleteAllKeys(request, &response); |
| 367 | |
| 368 | return kmError2ScopedAStatus(response.error); |
| 369 | } |
| 370 | |
| 371 | ScopedAStatus AndroidKeyMintDevice::destroyAttestationIds() { |
| 372 | return kmError2ScopedAStatus(KM_ERROR_UNIMPLEMENTED); |
| 373 | } |
| 374 | |
| 375 | ScopedAStatus AndroidKeyMintDevice::begin(KeyPurpose purpose, const vector<uint8_t>& keyBlob, |
| 376 | const vector<KeyParameter>& params, |
David Drysdale | 8181591 | 2021-04-19 19:11:41 +0100 | [diff] [blame] | 377 | const optional<HardwareAuthToken>& authToken, |
| 378 | BeginResult* result) { |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 379 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 380 | BeginOperationRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 381 | request.purpose = legacy_enum_conversion(purpose); |
| 382 | request.SetKeyMaterial(keyBlob.data(), keyBlob.size()); |
| 383 | request.additional_params.Reinitialize(KmParamSet(params)); |
| 384 | |
| 385 | vector<uint8_t> vector_token = authToken2AidlVec(authToken); |
| 386 | request.additional_params.push_back( |
| 387 | TAG_AUTH_TOKEN, reinterpret_cast<uint8_t*>(vector_token.data()), vector_token.size()); |
| 388 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 389 | BeginOperationResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 390 | impl_->BeginOperation(request, &response); |
| 391 | |
| 392 | if (response.error != KM_ERROR_OK) { |
| 393 | return kmError2ScopedAStatus(response.error); |
| 394 | } |
| 395 | |
| 396 | result->params = kmParamSet2Aidl(response.output_params); |
| 397 | result->challenge = response.op_handle; |
| 398 | result->operation = |
| 399 | ndk::SharedRefBase::make<AndroidKeyMintOperation>(impl_, response.op_handle); |
| 400 | return ScopedAStatus::ok(); |
| 401 | } |
| 402 | |
Chirag Pathak | b292e9a | 2021-02-02 07:28:09 +0000 | [diff] [blame] | 403 | ScopedAStatus AndroidKeyMintDevice::deviceLocked( |
Shawn Willden | 5592969 | 2021-02-19 14:53:02 -0700 | [diff] [blame] | 404 | bool passwordOnly, const std::optional<secureclock::TimeStampToken>& timestampToken) { |
Chirag Pathak | b292e9a | 2021-02-02 07:28:09 +0000 | [diff] [blame] | 405 | DeviceLockedRequest request(impl_->message_version()); |
Shawn Willden | 5592969 | 2021-02-19 14:53:02 -0700 | [diff] [blame] | 406 | request.passwordOnly = passwordOnly; |
| 407 | if (timestampToken.has_value()) { |
| 408 | request.token.challenge = timestampToken->challenge; |
| 409 | request.token.mac = {timestampToken->mac.data(), timestampToken->mac.size()}; |
| 410 | request.token.timestamp = timestampToken->timestamp.milliSeconds; |
Chirag Pathak | b292e9a | 2021-02-02 07:28:09 +0000 | [diff] [blame] | 411 | } |
| 412 | DeviceLockedResponse response = impl_->DeviceLocked(request); |
| 413 | return kmError2ScopedAStatus(response.error); |
| 414 | } |
| 415 | |
| 416 | ScopedAStatus AndroidKeyMintDevice::earlyBootEnded() { |
| 417 | EarlyBootEndedResponse response = impl_->EarlyBootEnded(); |
| 418 | return kmError2ScopedAStatus(response.error); |
| 419 | } |
| 420 | |
Satya Tangirala | 86d2540 | 2021-03-05 16:33:50 -0800 | [diff] [blame] | 421 | ScopedAStatus |
| 422 | AndroidKeyMintDevice::convertStorageKeyToEphemeral(const std::vector<uint8_t>& /* storageKeyBlob */, |
| 423 | std::vector<uint8_t>* /* ephemeralKeyBlob */) { |
| 424 | return kmError2ScopedAStatus(KM_ERROR_UNIMPLEMENTED); |
| 425 | } |
| 426 | |
Paul Crowley | 08641bf | 2021-04-29 12:46:49 -0700 | [diff] [blame] | 427 | ScopedAStatus AndroidKeyMintDevice::getKeyCharacteristics( |
| 428 | const std::vector<uint8_t>& /* storageKeyBlob */, const std::vector<uint8_t>& /* appId */, |
| 429 | const std::vector<uint8_t>& /* appData */, |
| 430 | std::vector<KeyCharacteristics>* /* keyCharacteristics */) { |
| 431 | return kmError2ScopedAStatus(KM_ERROR_UNIMPLEMENTED); |
| 432 | } |
| 433 | |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 434 | IKeyMintDevice* CreateKeyMintDevice(SecurityLevel securityLevel) { |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 435 | return ::new AndroidKeyMintDevice(securityLevel); |
| 436 | } |
| 437 | |
| 438 | } // namespace aidl::android::hardware::security::keymint |