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