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 | |
| 33 | using namespace ::keymaster; |
Shawn Willden | 96d4e8c | 2020-12-07 17:03:54 -0700 | [diff] [blame] | 34 | using namespace km_utils; |
Chirag Pathak | b292e9a | 2021-02-02 07:28:09 +0000 | [diff] [blame] | 35 | using secureclock::TimeStampToken; |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 36 | |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 37 | namespace { |
| 38 | |
| 39 | vector<KeyCharacteristics> convertKeyCharacteristics(SecurityLevel keyMintSecurityLevel, |
| 40 | const AuthorizationSet& sw_enforced, |
| 41 | const AuthorizationSet& hw_enforced) { |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 42 | KeyCharacteristics keyMintEnforced{keyMintSecurityLevel, {}}; |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 43 | |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 44 | if (keyMintSecurityLevel != SecurityLevel::SOFTWARE) { |
| 45 | // We're pretending to be TRUSTED_ENVIRONMENT or STRONGBOX. Only the entries in hw_enforced |
| 46 | // should be returned. |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 47 | keyMintEnforced.authorizations = kmParamSet2Aidl(hw_enforced); |
| 48 | return {std::move(keyMintEnforced)}; |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 51 | KeyCharacteristics keystoreEnforced{SecurityLevel::KEYSTORE, {}}; |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 52 | CHECK(hw_enforced.empty()) << "Hardware-enforced list is non-empty for pure SW KeyMint"; |
| 53 | |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 54 | // 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 Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 57 | |
| 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 Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 64 | case KM_TAG_ROLLBACK_RESISTANCE: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 65 | CHECK(false) << "We shouldn't see tag " << entry.tag; |
| 66 | break; |
| 67 | |
| 68 | /* Unimplemented */ |
| 69 | case KM_TAG_ALLOW_WHILE_ON_BODY: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 70 | case KM_TAG_BOOTLOADER_ONLY: |
| 71 | case KM_TAG_EARLY_BOOT_ONLY: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 72 | case KM_TAG_ROLLBACK_RESISTANT: |
| 73 | case KM_TAG_STORAGE_KEY: |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 74 | case KM_TAG_TRUSTED_CONFIRMATION_REQUIRED: |
| 75 | case KM_TAG_TRUSTED_USER_PRESENCE_REQUIRED: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 76 | break; |
| 77 | |
| 78 | /* Unenforceable */ |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 79 | case KM_TAG_CREATION_DATETIME: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 80 | break; |
| 81 | |
| 82 | /* Disallowed in KeyCharacteristics */ |
| 83 | case KM_TAG_APPLICATION_DATA: |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 84 | case KM_TAG_ATTESTATION_APPLICATION_ID: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 85 | break; |
| 86 | |
| 87 | /* Not key characteristics */ |
| 88 | case KM_TAG_ASSOCIATED_DATA: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 89 | 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 Danisevskis | a5780e2 | 2021-01-31 22:05:22 -0800 | [diff] [blame] | 101 | case KM_TAG_CERTIFICATE_NOT_AFTER: |
| 102 | case KM_TAG_CERTIFICATE_NOT_BEFORE: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 103 | 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 Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 113 | /* KeyMint-enforced */ |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 114 | case KM_TAG_ALGORITHM: |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 115 | case KM_TAG_APPLICATION_ID: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 116 | 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 Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 141 | 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: |
Paul Crowley | 8384653 | 2021-02-10 11:24:50 -0800 | [diff] [blame^] | 148 | case KM_TAG_MAX_BOOT_LEVEL: |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 149 | case KM_TAG_ORIGINATION_EXPIRE_DATETIME: |
| 150 | case KM_TAG_USAGE_EXPIRE_DATETIME: |
| 151 | case KM_TAG_USER_ID: |
| 152 | case KM_TAG_USAGE_COUNT_LIMIT: |
| 153 | keystoreEnforced.authorizations.push_back(kmParam2Aidl(entry)); |
| 154 | break; |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 158 | vector<KeyCharacteristics> retval; |
| 159 | retval.reserve(2); |
| 160 | if (!keyMintEnforced.authorizations.empty()) retval.push_back(std::move(keyMintEnforced)); |
| 161 | if (!keystoreEnforced.authorizations.empty()) retval.push_back(std::move(keystoreEnforced)); |
| 162 | |
| 163 | return retval; |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Shawn Willden | d7d4f31 | 2020-12-21 08:31:01 -0700 | [diff] [blame] | 166 | Certificate convertCertificate(const keymaster_blob_t& cert) { |
| 167 | return {std::vector<uint8_t>(cert.data, cert.data + cert.data_length)}; |
| 168 | } |
| 169 | |
| 170 | vector<Certificate> convertCertificateChain(const CertificateChain& chain) { |
| 171 | vector<Certificate> retval; |
| 172 | retval.reserve(chain.entry_count); |
| 173 | std::transform(chain.begin(), chain.end(), std::back_inserter(retval), convertCertificate); |
| 174 | return retval; |
| 175 | } |
| 176 | |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 177 | } // namespace |
| 178 | |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 179 | constexpr size_t kOperationTableSize = 16; |
| 180 | |
| 181 | AndroidKeyMintDevice::AndroidKeyMintDevice(SecurityLevel securityLevel) |
| 182 | : impl_(new ::keymaster::AndroidKeymaster( |
| 183 | [&]() -> auto { |
| 184 | auto context = new PureSoftKeymasterContext( |
| 185 | KmVersion::KEYMINT_1, static_cast<keymaster_security_level_t>(securityLevel)); |
| 186 | context->SetSystemVersion(::keymaster::GetOsVersion(), |
| 187 | ::keymaster::GetOsPatchlevel()); |
| 188 | return context; |
| 189 | }(), |
| 190 | kOperationTableSize)), |
| 191 | securityLevel_(securityLevel) {} |
| 192 | |
| 193 | AndroidKeyMintDevice::~AndroidKeyMintDevice() {} |
| 194 | |
| 195 | ScopedAStatus AndroidKeyMintDevice::getHardwareInfo(KeyMintHardwareInfo* info) { |
| 196 | info->versionNumber = 1; |
| 197 | info->securityLevel = securityLevel_; |
| 198 | info->keyMintName = "FakeKeyMintDevice"; |
| 199 | info->keyMintAuthorName = "Google"; |
Chirag Pathak | b292e9a | 2021-02-02 07:28:09 +0000 | [diff] [blame] | 200 | info->timestampTokenRequired = false; |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 201 | return ScopedAStatus::ok(); |
| 202 | } |
| 203 | |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 204 | ScopedAStatus AndroidKeyMintDevice::addRngEntropy(const vector<uint8_t>& data) { |
| 205 | if (data.size() == 0) { |
| 206 | return ScopedAStatus::ok(); |
| 207 | } |
| 208 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 209 | AddEntropyRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 210 | request.random_data.Reinitialize(data.data(), data.size()); |
| 211 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 212 | AddEntropyResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 213 | impl_->AddRngEntropy(request, &response); |
| 214 | |
| 215 | return kmError2ScopedAStatus(response.error); |
| 216 | } |
| 217 | |
| 218 | ScopedAStatus AndroidKeyMintDevice::generateKey(const vector<KeyParameter>& keyParams, |
Shawn Willden | 44c3888 | 2020-12-21 18:35:13 -0700 | [diff] [blame] | 219 | const optional<AttestationKey>& attestationKey, |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 220 | KeyCreationResult* creationResult) { |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 221 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 222 | GenerateKeyRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 223 | request.key_description.Reinitialize(KmParamSet(keyParams)); |
Shawn Willden | 44c3888 | 2020-12-21 18:35:13 -0700 | [diff] [blame] | 224 | if (attestationKey) { |
| 225 | request.attestation_signing_key_blob = |
| 226 | KeymasterKeyBlob(attestationKey->keyBlob.data(), attestationKey->keyBlob.size()); |
| 227 | request.attest_key_params.Reinitialize(KmParamSet(attestationKey->attestKeyParams)); |
| 228 | request.issuer_subject = KeymasterBlob(attestationKey->issuerSubjectName.data(), |
| 229 | attestationKey->issuerSubjectName.size()); |
| 230 | } |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 231 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 232 | GenerateKeyResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 233 | impl_->GenerateKey(request, &response); |
| 234 | |
| 235 | if (response.error != KM_ERROR_OK) { |
| 236 | // Note a key difference between this current aidl and previous hal, is |
| 237 | // that hal returns void where as aidl returns the error status. If |
| 238 | // aidl returns error, then aidl will not return any change you may make |
| 239 | // to the out parameters. This is quite different from hal where all |
| 240 | // output variable can be modified due to hal returning void. |
| 241 | // |
| 242 | // So the caller need to be aware not to expect aidl functions to clear |
| 243 | // the output variables for you in case of error. If you left some |
| 244 | // wrong data set in the out parameters, they will stay there. |
| 245 | return kmError2ScopedAStatus(response.error); |
| 246 | } |
| 247 | |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 248 | creationResult->keyBlob = kmBlob2vector(response.key_blob); |
| 249 | creationResult->keyCharacteristics = |
| 250 | convertKeyCharacteristics(securityLevel_, response.unenforced, response.enforced); |
Shawn Willden | d7d4f31 | 2020-12-21 08:31:01 -0700 | [diff] [blame] | 251 | creationResult->certificateChain = convertCertificateChain(response.certificate_chain); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 252 | return ScopedAStatus::ok(); |
| 253 | } |
| 254 | |
| 255 | ScopedAStatus AndroidKeyMintDevice::importKey(const vector<KeyParameter>& keyParams, |
| 256 | KeyFormat keyFormat, const vector<uint8_t>& keyData, |
Shawn Willden | 44c3888 | 2020-12-21 18:35:13 -0700 | [diff] [blame] | 257 | const optional<AttestationKey>& attestationKey, |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 258 | KeyCreationResult* creationResult) { |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 259 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 260 | ImportKeyRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 261 | request.key_description.Reinitialize(KmParamSet(keyParams)); |
| 262 | request.key_format = legacy_enum_conversion(keyFormat); |
Shawn Willden | 6e20ea4 | 2020-12-21 18:52:34 -0700 | [diff] [blame] | 263 | request.key_data = KeymasterKeyBlob(keyData.data(), keyData.size()); |
Shawn Willden | 44c3888 | 2020-12-21 18:35:13 -0700 | [diff] [blame] | 264 | if (attestationKey) { |
| 265 | request.attestation_signing_key_blob = |
| 266 | KeymasterKeyBlob(attestationKey->keyBlob.data(), attestationKey->keyBlob.size()); |
| 267 | request.attest_key_params.Reinitialize(KmParamSet(attestationKey->attestKeyParams)); |
| 268 | request.issuer_subject = KeymasterBlob(attestationKey->issuerSubjectName.data(), |
| 269 | attestationKey->issuerSubjectName.size()); |
| 270 | } |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 271 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 272 | ImportKeyResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 273 | impl_->ImportKey(request, &response); |
| 274 | |
| 275 | if (response.error != KM_ERROR_OK) { |
| 276 | return kmError2ScopedAStatus(response.error); |
| 277 | } |
| 278 | |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 279 | creationResult->keyBlob = kmBlob2vector(response.key_blob); |
| 280 | creationResult->keyCharacteristics = |
| 281 | convertKeyCharacteristics(securityLevel_, response.unenforced, response.enforced); |
Shawn Willden | d7d4f31 | 2020-12-21 08:31:01 -0700 | [diff] [blame] | 282 | creationResult->certificateChain = convertCertificateChain(response.certificate_chain); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 283 | |
| 284 | return ScopedAStatus::ok(); |
| 285 | } |
| 286 | |
Shawn Willden | 44c3888 | 2020-12-21 18:35:13 -0700 | [diff] [blame] | 287 | ScopedAStatus |
| 288 | AndroidKeyMintDevice::importWrappedKey(const vector<uint8_t>& wrappedKeyData, // |
| 289 | const vector<uint8_t>& wrappingKeyBlob, // |
| 290 | const vector<uint8_t>& maskingKey, // |
| 291 | const vector<KeyParameter>& unwrappingParams, // |
| 292 | int64_t passwordSid, int64_t biometricSid, // |
| 293 | KeyCreationResult* creationResult) { |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 294 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 295 | ImportWrappedKeyRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 296 | request.SetWrappedMaterial(wrappedKeyData.data(), wrappedKeyData.size()); |
| 297 | request.SetWrappingMaterial(wrappingKeyBlob.data(), wrappingKeyBlob.size()); |
| 298 | request.SetMaskingKeyMaterial(maskingKey.data(), maskingKey.size()); |
| 299 | request.additional_params.Reinitialize(KmParamSet(unwrappingParams)); |
| 300 | request.password_sid = static_cast<uint64_t>(passwordSid); |
| 301 | request.biometric_sid = static_cast<uint64_t>(biometricSid); |
| 302 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 303 | ImportWrappedKeyResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 304 | impl_->ImportWrappedKey(request, &response); |
| 305 | |
| 306 | if (response.error != KM_ERROR_OK) { |
| 307 | return kmError2ScopedAStatus(response.error); |
| 308 | } |
| 309 | |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 310 | creationResult->keyBlob = kmBlob2vector(response.key_blob); |
| 311 | creationResult->keyCharacteristics = |
| 312 | convertKeyCharacteristics(securityLevel_, response.unenforced, response.enforced); |
Shawn Willden | d7d4f31 | 2020-12-21 08:31:01 -0700 | [diff] [blame] | 313 | creationResult->certificateChain = convertCertificateChain(response.certificate_chain); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 314 | |
| 315 | return ScopedAStatus::ok(); |
| 316 | } |
| 317 | |
| 318 | ScopedAStatus AndroidKeyMintDevice::upgradeKey(const vector<uint8_t>& keyBlobToUpgrade, |
| 319 | const vector<KeyParameter>& upgradeParams, |
| 320 | vector<uint8_t>* keyBlob) { |
| 321 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 322 | UpgradeKeyRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 323 | request.SetKeyMaterial(keyBlobToUpgrade.data(), keyBlobToUpgrade.size()); |
| 324 | request.upgrade_params.Reinitialize(KmParamSet(upgradeParams)); |
| 325 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 326 | UpgradeKeyResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 327 | impl_->UpgradeKey(request, &response); |
| 328 | |
| 329 | if (response.error != KM_ERROR_OK) { |
| 330 | return kmError2ScopedAStatus(response.error); |
| 331 | } |
| 332 | |
| 333 | *keyBlob = kmBlob2vector(response.upgraded_key); |
| 334 | return ScopedAStatus::ok(); |
| 335 | } |
| 336 | |
| 337 | ScopedAStatus AndroidKeyMintDevice::deleteKey(const vector<uint8_t>& keyBlob) { |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 338 | DeleteKeyRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 339 | request.SetKeyMaterial(keyBlob.data(), keyBlob.size()); |
| 340 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 341 | DeleteKeyResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 342 | impl_->DeleteKey(request, &response); |
| 343 | |
| 344 | return kmError2ScopedAStatus(response.error); |
| 345 | } |
| 346 | |
| 347 | ScopedAStatus AndroidKeyMintDevice::deleteAllKeys() { |
| 348 | // There's nothing to be done to delete software key blobs. |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 349 | DeleteAllKeysRequest request(impl_->message_version()); |
| 350 | DeleteAllKeysResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 351 | impl_->DeleteAllKeys(request, &response); |
| 352 | |
| 353 | return kmError2ScopedAStatus(response.error); |
| 354 | } |
| 355 | |
| 356 | ScopedAStatus AndroidKeyMintDevice::destroyAttestationIds() { |
| 357 | return kmError2ScopedAStatus(KM_ERROR_UNIMPLEMENTED); |
| 358 | } |
| 359 | |
| 360 | ScopedAStatus AndroidKeyMintDevice::begin(KeyPurpose purpose, const vector<uint8_t>& keyBlob, |
| 361 | const vector<KeyParameter>& params, |
| 362 | const HardwareAuthToken& authToken, BeginResult* result) { |
| 363 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 364 | BeginOperationRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 365 | request.purpose = legacy_enum_conversion(purpose); |
| 366 | request.SetKeyMaterial(keyBlob.data(), keyBlob.size()); |
| 367 | request.additional_params.Reinitialize(KmParamSet(params)); |
| 368 | |
| 369 | vector<uint8_t> vector_token = authToken2AidlVec(authToken); |
| 370 | request.additional_params.push_back( |
| 371 | TAG_AUTH_TOKEN, reinterpret_cast<uint8_t*>(vector_token.data()), vector_token.size()); |
| 372 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 373 | BeginOperationResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 374 | impl_->BeginOperation(request, &response); |
| 375 | |
| 376 | if (response.error != KM_ERROR_OK) { |
| 377 | return kmError2ScopedAStatus(response.error); |
| 378 | } |
| 379 | |
| 380 | result->params = kmParamSet2Aidl(response.output_params); |
| 381 | result->challenge = response.op_handle; |
| 382 | result->operation = |
| 383 | ndk::SharedRefBase::make<AndroidKeyMintOperation>(impl_, response.op_handle); |
| 384 | return ScopedAStatus::ok(); |
| 385 | } |
| 386 | |
Chirag Pathak | b292e9a | 2021-02-02 07:28:09 +0000 | [diff] [blame] | 387 | ScopedAStatus AndroidKeyMintDevice::deviceLocked( |
| 388 | bool in_passwordOnly, |
| 389 | const std::optional<::aidl::android::hardware::security::secureclock::TimeStampToken>& |
| 390 | in_timestampToken) { |
| 391 | DeviceLockedRequest request(impl_->message_version()); |
| 392 | request.passwordOnly = in_passwordOnly; |
| 393 | if (in_timestampToken.has_value()) { |
| 394 | request.token.challenge = in_timestampToken->challenge; |
| 395 | request.token.mac = {in_timestampToken->mac.data(), in_timestampToken->mac.size()}; |
| 396 | request.token.timestamp = in_timestampToken->timestamp.milliSeconds; |
| 397 | } |
| 398 | DeviceLockedResponse response = impl_->DeviceLocked(request); |
| 399 | return kmError2ScopedAStatus(response.error); |
| 400 | } |
| 401 | |
| 402 | ScopedAStatus AndroidKeyMintDevice::earlyBootEnded() { |
| 403 | EarlyBootEndedResponse response = impl_->EarlyBootEnded(); |
| 404 | return kmError2ScopedAStatus(response.error); |
| 405 | } |
| 406 | |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 407 | IKeyMintDevice* CreateKeyMintDevice(SecurityLevel securityLevel) { |
| 408 | |
| 409 | return ::new AndroidKeyMintDevice(securityLevel); |
| 410 | } |
| 411 | |
| 412 | } // namespace aidl::android::hardware::security::keymint |