Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | #include "KeyMintAidlTestBase.h" |
| 18 | |
| 19 | #include <chrono> |
David Drysdale | 555ba00 | 2022-05-03 18:48:57 +0100 | [diff] [blame] | 20 | #include <fstream> |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 21 | #include <unordered_set> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
| 24 | #include <android-base/logging.h> |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 25 | #include <android/binder_manager.h> |
David Drysdale | 3d2ba0a | 2023-01-11 13:27:26 +0000 | [diff] [blame] | 26 | #include <android/content/pm/IPackageManagerNative.h> |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 27 | #include <cppbor_parse.h> |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 28 | #include <cutils/properties.h> |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 29 | #include <gmock/gmock.h> |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 30 | #include <openssl/evp.h> |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 31 | #include <openssl/mem.h> |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 32 | #include <remote_prov/remote_prov_utils.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 33 | |
Max Bires | 9704ff6 | 2021-04-07 11:12:01 -0700 | [diff] [blame] | 34 | #include <keymaster/cppcose/cppcose.h> |
Shawn Willden | 08a7e43 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 35 | #include <keymint_support/key_param_output.h> |
| 36 | #include <keymint_support/keymint_utils.h> |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 37 | #include <keymint_support/openssl_utils.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 38 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 39 | namespace aidl::android::hardware::security::keymint { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 40 | |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 41 | using namespace cppcose; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 42 | using namespace std::literals::chrono_literals; |
| 43 | using std::endl; |
| 44 | using std::optional; |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 45 | using std::unique_ptr; |
| 46 | using ::testing::AssertionFailure; |
| 47 | using ::testing::AssertionResult; |
| 48 | using ::testing::AssertionSuccess; |
Seth Moore | 026bb74 | 2021-04-30 11:41:18 -0700 | [diff] [blame] | 49 | using ::testing::ElementsAreArray; |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 50 | using ::testing::MatchesRegex; |
Seth Moore | 026bb74 | 2021-04-30 11:41:18 -0700 | [diff] [blame] | 51 | using ::testing::Not; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 52 | |
| 53 | ::std::ostream& operator<<(::std::ostream& os, const AuthorizationSet& set) { |
| 54 | if (set.size() == 0) |
| 55 | os << "(Empty)" << ::std::endl; |
| 56 | else { |
| 57 | os << "\n"; |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 58 | for (auto& entry : set) os << entry << ::std::endl; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 59 | } |
| 60 | return os; |
| 61 | } |
| 62 | |
| 63 | namespace test { |
| 64 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 65 | namespace { |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 66 | |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 67 | // Invalid value for a patchlevel (which is of form YYYYMMDD). |
| 68 | const uint32_t kInvalidPatchlevel = 99998877; |
| 69 | |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 70 | // Overhead for PKCS#1 v1.5 signature padding of undigested messages. Digested messages have |
| 71 | // additional overhead, for the digest algorithmIdentifier required by PKCS#1. |
| 72 | const size_t kPkcs1UndigestedSignaturePaddingOverhead = 11; |
| 73 | |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 74 | typedef KeyMintAidlTestBase::KeyData KeyData; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 75 | // Predicate for testing basic characteristics validity in generation or import. |
| 76 | bool KeyCharacteristicsBasicallyValid(SecurityLevel secLevel, |
| 77 | const vector<KeyCharacteristics>& key_characteristics) { |
| 78 | if (key_characteristics.empty()) return false; |
| 79 | |
| 80 | std::unordered_set<SecurityLevel> levels_seen; |
| 81 | for (auto& entry : key_characteristics) { |
Seth Moore | 2a9a00e | 2021-08-04 16:31:52 -0700 | [diff] [blame] | 82 | if (entry.authorizations.empty()) { |
| 83 | GTEST_LOG_(ERROR) << "empty authorizations for " << entry.securityLevel; |
| 84 | return false; |
| 85 | } |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 86 | |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 87 | // Just ignore the SecurityLevel::KEYSTORE as the KM won't do any enforcement on this. |
| 88 | if (entry.securityLevel == SecurityLevel::KEYSTORE) continue; |
| 89 | |
Seth Moore | 2a9a00e | 2021-08-04 16:31:52 -0700 | [diff] [blame] | 90 | if (levels_seen.find(entry.securityLevel) != levels_seen.end()) { |
| 91 | GTEST_LOG_(ERROR) << "duplicate authorizations for " << entry.securityLevel; |
| 92 | return false; |
| 93 | } |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 94 | levels_seen.insert(entry.securityLevel); |
| 95 | |
| 96 | // Generally, we should only have one entry, at the same security level as the KM |
| 97 | // instance. There is an exception: StrongBox KM can have some authorizations that are |
| 98 | // enforced by the TEE. |
| 99 | bool isExpectedSecurityLevel = secLevel == entry.securityLevel || |
| 100 | (secLevel == SecurityLevel::STRONGBOX && |
| 101 | entry.securityLevel == SecurityLevel::TRUSTED_ENVIRONMENT); |
| 102 | |
Seth Moore | 2a9a00e | 2021-08-04 16:31:52 -0700 | [diff] [blame] | 103 | if (!isExpectedSecurityLevel) { |
| 104 | GTEST_LOG_(ERROR) << "Unexpected security level " << entry.securityLevel; |
| 105 | return false; |
| 106 | } |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 107 | } |
| 108 | return true; |
| 109 | } |
| 110 | |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 111 | void check_attestation_version(uint32_t attestation_version, int32_t aidl_version) { |
| 112 | // Version numbers in attestation extensions should be a multiple of 100. |
| 113 | EXPECT_EQ(attestation_version % 100, 0); |
| 114 | |
| 115 | // The multiplier should never be higher than the AIDL version, but can be less |
| 116 | // (for example, if the implementation is from an earlier version but the HAL service |
| 117 | // uses the default libraries and so reports the current AIDL version). |
| 118 | EXPECT_TRUE((attestation_version / 100) <= aidl_version); |
| 119 | } |
| 120 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 121 | bool avb_verification_enabled() { |
| 122 | char value[PROPERTY_VALUE_MAX]; |
| 123 | return property_get("ro.boot.vbmeta.device_state", value, "") != 0; |
| 124 | } |
| 125 | |
| 126 | char nibble2hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', |
| 127 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; |
| 128 | |
| 129 | // Attestations don't contain everything in key authorization lists, so we need to filter the key |
| 130 | // lists to produce the lists that we expect to match the attestations. |
| 131 | auto kTagsToFilter = { |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 132 | Tag::CREATION_DATETIME, |
| 133 | Tag::HARDWARE_TYPE, |
| 134 | Tag::INCLUDE_UNIQUE_ID, |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | AuthorizationSet filtered_tags(const AuthorizationSet& set) { |
| 138 | AuthorizationSet filtered; |
| 139 | std::remove_copy_if( |
| 140 | set.begin(), set.end(), std::back_inserter(filtered), [](const auto& entry) -> bool { |
| 141 | return std::find(kTagsToFilter.begin(), kTagsToFilter.end(), entry.tag) != |
| 142 | kTagsToFilter.end(); |
| 143 | }); |
| 144 | return filtered; |
| 145 | } |
| 146 | |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 147 | // Remove any SecurityLevel::KEYSTORE entries from a list of key characteristics. |
| 148 | void strip_keystore_tags(vector<KeyCharacteristics>* characteristics) { |
| 149 | characteristics->erase(std::remove_if(characteristics->begin(), characteristics->end(), |
| 150 | [](const auto& entry) { |
| 151 | return entry.securityLevel == SecurityLevel::KEYSTORE; |
| 152 | }), |
| 153 | characteristics->end()); |
| 154 | } |
| 155 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 156 | string x509NameToStr(X509_NAME* name) { |
| 157 | char* s = X509_NAME_oneline(name, nullptr, 0); |
| 158 | string retval(s); |
| 159 | OPENSSL_free(s); |
| 160 | return retval; |
| 161 | } |
| 162 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 163 | } // namespace |
| 164 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 165 | bool KeyMintAidlTestBase::arm_deleteAllKeys = false; |
| 166 | bool KeyMintAidlTestBase::dump_Attestations = false; |
David Drysdale | 9f5c0c5 | 2022-11-03 15:10:16 +0000 | [diff] [blame] | 167 | std::string KeyMintAidlTestBase::keyblob_dir; |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 168 | |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 169 | uint32_t KeyMintAidlTestBase::boot_patch_level( |
| 170 | const vector<KeyCharacteristics>& key_characteristics) { |
| 171 | // The boot patchlevel is not available as a property, but should be present |
| 172 | // in the key characteristics of any created key. |
| 173 | AuthorizationSet allAuths; |
| 174 | for (auto& entry : key_characteristics) { |
| 175 | allAuths.push_back(AuthorizationSet(entry.authorizations)); |
| 176 | } |
| 177 | auto patchlevel = allAuths.GetTagValue(TAG_BOOT_PATCHLEVEL); |
| 178 | if (patchlevel.has_value()) { |
| 179 | return patchlevel.value(); |
| 180 | } else { |
| 181 | // No boot patchlevel is available. Return a value that won't match anything |
| 182 | // and so will trigger test failures. |
| 183 | return kInvalidPatchlevel; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | uint32_t KeyMintAidlTestBase::boot_patch_level() { |
| 188 | return boot_patch_level(key_characteristics_); |
| 189 | } |
| 190 | |
Prashant Patil | 88ad189 | 2022-03-15 16:31:02 +0000 | [diff] [blame] | 191 | /** |
| 192 | * An API to determine device IDs attestation is required or not, |
| 193 | * which is mandatory for KeyMint version 2 or first_api_level 33 or greater. |
| 194 | */ |
| 195 | bool KeyMintAidlTestBase::isDeviceIdAttestationRequired() { |
Shawn Willden | 1a545db | 2023-02-22 14:32:33 -0700 | [diff] [blame] | 196 | return AidlVersion() >= 2 || property_get_int32("ro.vendor.api_level", 0) >= __ANDROID_API_T__; |
Prashant Patil | 88ad189 | 2022-03-15 16:31:02 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Rajesh Nyamagoud | 5283f81 | 2023-01-06 00:27:56 +0000 | [diff] [blame] | 199 | /** |
| 200 | * An API to determine second IMEI ID attestation is required or not, |
| 201 | * which is supported for KeyMint version 3 or first_api_level greater than 33. |
| 202 | */ |
| 203 | bool KeyMintAidlTestBase::isSecondImeiIdAttestationRequired() { |
Shawn Willden | 1a545db | 2023-02-22 14:32:33 -0700 | [diff] [blame] | 204 | return AidlVersion() >= 3 && property_get_int32("ro.vendor.api_level", 0) > __ANDROID_API_T__; |
Rajesh Nyamagoud | 5283f81 | 2023-01-06 00:27:56 +0000 | [diff] [blame] | 205 | } |
| 206 | |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 207 | bool KeyMintAidlTestBase::Curve25519Supported() { |
| 208 | // Strongbox never supports curve 25519. |
| 209 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | // Curve 25519 was included in version 2 of the KeyMint interface. |
| 214 | int32_t version = 0; |
| 215 | auto status = keymint_->getInterfaceVersion(&version); |
| 216 | if (!status.isOk()) { |
| 217 | ADD_FAILURE() << "Failed to determine interface version"; |
| 218 | } |
| 219 | return version >= 2; |
| 220 | } |
| 221 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 222 | ErrorCode KeyMintAidlTestBase::GetReturnErrorCode(const Status& result) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 223 | if (result.isOk()) return ErrorCode::OK; |
| 224 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 225 | if (result.getExceptionCode() == EX_SERVICE_SPECIFIC) { |
| 226 | return static_cast<ErrorCode>(result.getServiceSpecificError()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | return ErrorCode::UNKNOWN_ERROR; |
| 230 | } |
| 231 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 232 | void KeyMintAidlTestBase::InitializeKeyMint(std::shared_ptr<IKeyMintDevice> keyMint) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 233 | ASSERT_NE(keyMint, nullptr); |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 234 | keymint_ = std::move(keyMint); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 235 | |
| 236 | KeyMintHardwareInfo info; |
| 237 | ASSERT_TRUE(keymint_->getHardwareInfo(&info).isOk()); |
| 238 | |
| 239 | securityLevel_ = info.securityLevel; |
| 240 | name_.assign(info.keyMintName.begin(), info.keyMintName.end()); |
| 241 | author_.assign(info.keyMintAuthorName.begin(), info.keyMintAuthorName.end()); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 242 | timestamp_token_required_ = info.timestampTokenRequired; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 243 | |
| 244 | os_version_ = getOsVersion(); |
| 245 | os_patch_level_ = getOsPatchlevel(); |
David Drysdale | bb3d85e | 2021-04-13 11:15:51 +0100 | [diff] [blame] | 246 | vendor_patch_level_ = getVendorPatchlevel(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 247 | } |
| 248 | |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 249 | int32_t KeyMintAidlTestBase::AidlVersion() { |
| 250 | int32_t version = 0; |
| 251 | auto status = keymint_->getInterfaceVersion(&version); |
| 252 | if (!status.isOk()) { |
| 253 | ADD_FAILURE() << "Failed to determine interface version"; |
| 254 | } |
| 255 | return version; |
| 256 | } |
| 257 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 258 | void KeyMintAidlTestBase::SetUp() { |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 259 | if (AServiceManager_isDeclared(GetParam().c_str())) { |
| 260 | ::ndk::SpAIBinder binder(AServiceManager_waitForService(GetParam().c_str())); |
| 261 | InitializeKeyMint(IKeyMintDevice::fromBinder(binder)); |
| 262 | } else { |
| 263 | InitializeKeyMint(nullptr); |
| 264 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | ErrorCode KeyMintAidlTestBase::GenerateKey(const AuthorizationSet& key_desc, |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 268 | const optional<AttestationKey>& attest_key, |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 269 | vector<uint8_t>* key_blob, |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 270 | vector<KeyCharacteristics>* key_characteristics, |
| 271 | vector<Certificate>* cert_chain) { |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 272 | EXPECT_NE(key_blob, nullptr) << "Key blob pointer must not be null. Test bug"; |
| 273 | EXPECT_NE(key_characteristics, nullptr) |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 274 | << "Previous characteristics not deleted before generating key. Test bug."; |
| 275 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 276 | KeyCreationResult creationResult; |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 277 | Status result = keymint_->generateKey(key_desc.vector_data(), attest_key, &creationResult); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 278 | if (result.isOk()) { |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 279 | EXPECT_PRED2(KeyCharacteristicsBasicallyValid, SecLevel(), |
| 280 | creationResult.keyCharacteristics); |
| 281 | EXPECT_GT(creationResult.keyBlob.size(), 0); |
| 282 | *key_blob = std::move(creationResult.keyBlob); |
| 283 | *key_characteristics = std::move(creationResult.keyCharacteristics); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 284 | *cert_chain = std::move(creationResult.certificateChain); |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 285 | |
| 286 | auto algorithm = key_desc.GetTagValue(TAG_ALGORITHM); |
| 287 | EXPECT_TRUE(algorithm); |
| 288 | if (algorithm && |
| 289 | (algorithm.value() == Algorithm::RSA || algorithm.value() == Algorithm::EC)) { |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 290 | EXPECT_GE(cert_chain->size(), 1); |
| 291 | if (key_desc.Contains(TAG_ATTESTATION_CHALLENGE)) { |
| 292 | if (attest_key) { |
| 293 | EXPECT_EQ(cert_chain->size(), 1); |
| 294 | } else { |
| 295 | EXPECT_GT(cert_chain->size(), 1); |
| 296 | } |
| 297 | } |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 298 | } else { |
| 299 | // For symmetric keys there should be no certificates. |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 300 | EXPECT_EQ(cert_chain->size(), 0); |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 301 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | return GetReturnErrorCode(result); |
| 305 | } |
| 306 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 307 | ErrorCode KeyMintAidlTestBase::GenerateKey(const AuthorizationSet& key_desc, |
| 308 | const optional<AttestationKey>& attest_key) { |
| 309 | return GenerateKey(key_desc, attest_key, &key_blob_, &key_characteristics_, &cert_chain_); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 310 | } |
| 311 | |
subrahmanyaman | 7d9bc46 | 2022-03-16 01:40:39 +0000 | [diff] [blame] | 312 | ErrorCode KeyMintAidlTestBase::GenerateKeyWithSelfSignedAttestKey( |
| 313 | const AuthorizationSet& attest_key_desc, const AuthorizationSet& key_desc, |
| 314 | vector<uint8_t>* key_blob, vector<KeyCharacteristics>* key_characteristics, |
| 315 | vector<Certificate>* cert_chain) { |
| 316 | AttestationKey attest_key; |
| 317 | vector<Certificate> attest_cert_chain; |
| 318 | vector<KeyCharacteristics> attest_key_characteristics; |
| 319 | // Generate a key with self signed attestation. |
| 320 | auto error = GenerateKey(attest_key_desc, std::nullopt, &attest_key.keyBlob, |
| 321 | &attest_key_characteristics, &attest_cert_chain); |
| 322 | if (error != ErrorCode::OK) { |
| 323 | return error; |
| 324 | } |
| 325 | |
| 326 | attest_key.issuerSubjectName = make_name_from_str("Android Keystore Key"); |
| 327 | // Generate a key, by passing the above self signed attestation key as attest key. |
| 328 | error = GenerateKey(key_desc, attest_key, key_blob, key_characteristics, cert_chain); |
| 329 | if (error == ErrorCode::OK) { |
| 330 | // Append the attest_cert_chain to the attested cert_chain to yield a valid cert chain. |
| 331 | cert_chain->push_back(attest_cert_chain[0]); |
| 332 | } |
| 333 | return error; |
| 334 | } |
| 335 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 336 | ErrorCode KeyMintAidlTestBase::ImportKey(const AuthorizationSet& key_desc, KeyFormat format, |
| 337 | const string& key_material, vector<uint8_t>* key_blob, |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 338 | vector<KeyCharacteristics>* key_characteristics) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 339 | Status result; |
| 340 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 341 | cert_chain_.clear(); |
| 342 | key_characteristics->clear(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 343 | key_blob->clear(); |
| 344 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 345 | KeyCreationResult creationResult; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 346 | result = keymint_->importKey(key_desc.vector_data(), format, |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 347 | vector<uint8_t>(key_material.begin(), key_material.end()), |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 348 | {} /* attestationSigningKeyBlob */, &creationResult); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 349 | |
| 350 | if (result.isOk()) { |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 351 | EXPECT_PRED2(KeyCharacteristicsBasicallyValid, SecLevel(), |
| 352 | creationResult.keyCharacteristics); |
| 353 | EXPECT_GT(creationResult.keyBlob.size(), 0); |
| 354 | |
| 355 | *key_blob = std::move(creationResult.keyBlob); |
| 356 | *key_characteristics = std::move(creationResult.keyCharacteristics); |
| 357 | cert_chain_ = std::move(creationResult.certificateChain); |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 358 | |
| 359 | auto algorithm = key_desc.GetTagValue(TAG_ALGORITHM); |
| 360 | EXPECT_TRUE(algorithm); |
| 361 | if (algorithm && |
| 362 | (algorithm.value() == Algorithm::RSA || algorithm.value() == Algorithm::EC)) { |
| 363 | EXPECT_GE(cert_chain_.size(), 1); |
| 364 | if (key_desc.Contains(TAG_ATTESTATION_CHALLENGE)) EXPECT_GT(cert_chain_.size(), 1); |
| 365 | } else { |
| 366 | // For symmetric keys there should be no certificates. |
| 367 | EXPECT_EQ(cert_chain_.size(), 0); |
| 368 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | return GetReturnErrorCode(result); |
| 372 | } |
| 373 | |
| 374 | ErrorCode KeyMintAidlTestBase::ImportKey(const AuthorizationSet& key_desc, KeyFormat format, |
| 375 | const string& key_material) { |
| 376 | return ImportKey(key_desc, format, key_material, &key_blob_, &key_characteristics_); |
| 377 | } |
| 378 | |
| 379 | ErrorCode KeyMintAidlTestBase::ImportWrappedKey(string wrapped_key, string wrapping_key, |
| 380 | const AuthorizationSet& wrapping_key_desc, |
| 381 | string masking_key, |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 382 | const AuthorizationSet& unwrapping_params, |
| 383 | int64_t password_sid, int64_t biometric_sid) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 384 | EXPECT_EQ(ErrorCode::OK, ImportKey(wrapping_key_desc, KeyFormat::PKCS8, wrapping_key)); |
| 385 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 386 | key_characteristics_.clear(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 387 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 388 | KeyCreationResult creationResult; |
| 389 | Status result = keymint_->importWrappedKey( |
| 390 | vector<uint8_t>(wrapped_key.begin(), wrapped_key.end()), key_blob_, |
| 391 | vector<uint8_t>(masking_key.begin(), masking_key.end()), |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 392 | unwrapping_params.vector_data(), password_sid, biometric_sid, &creationResult); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 393 | |
| 394 | if (result.isOk()) { |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 395 | EXPECT_PRED2(KeyCharacteristicsBasicallyValid, SecLevel(), |
| 396 | creationResult.keyCharacteristics); |
| 397 | EXPECT_GT(creationResult.keyBlob.size(), 0); |
| 398 | |
| 399 | key_blob_ = std::move(creationResult.keyBlob); |
| 400 | key_characteristics_ = std::move(creationResult.keyCharacteristics); |
| 401 | cert_chain_ = std::move(creationResult.certificateChain); |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 402 | |
| 403 | AuthorizationSet allAuths; |
| 404 | for (auto& entry : key_characteristics_) { |
| 405 | allAuths.push_back(AuthorizationSet(entry.authorizations)); |
| 406 | } |
| 407 | auto algorithm = allAuths.GetTagValue(TAG_ALGORITHM); |
| 408 | EXPECT_TRUE(algorithm); |
| 409 | if (algorithm && |
| 410 | (algorithm.value() == Algorithm::RSA || algorithm.value() == Algorithm::EC)) { |
| 411 | EXPECT_GE(cert_chain_.size(), 1); |
| 412 | } else { |
| 413 | // For symmetric keys there should be no certificates. |
| 414 | EXPECT_EQ(cert_chain_.size(), 0); |
| 415 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | return GetReturnErrorCode(result); |
| 419 | } |
| 420 | |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 421 | ErrorCode KeyMintAidlTestBase::GetCharacteristics(const vector<uint8_t>& key_blob, |
| 422 | const vector<uint8_t>& app_id, |
| 423 | const vector<uint8_t>& app_data, |
| 424 | vector<KeyCharacteristics>* key_characteristics) { |
| 425 | Status result = |
| 426 | keymint_->getKeyCharacteristics(key_blob, app_id, app_data, key_characteristics); |
| 427 | return GetReturnErrorCode(result); |
| 428 | } |
| 429 | |
| 430 | ErrorCode KeyMintAidlTestBase::GetCharacteristics(const vector<uint8_t>& key_blob, |
| 431 | vector<KeyCharacteristics>* key_characteristics) { |
| 432 | vector<uint8_t> empty_app_id, empty_app_data; |
| 433 | return GetCharacteristics(key_blob, empty_app_id, empty_app_data, key_characteristics); |
| 434 | } |
| 435 | |
| 436 | void KeyMintAidlTestBase::CheckCharacteristics( |
| 437 | const vector<uint8_t>& key_blob, |
| 438 | const vector<KeyCharacteristics>& generate_characteristics) { |
| 439 | // Any key characteristics that were in SecurityLevel::KEYSTORE when returned from |
| 440 | // generateKey() should be excluded, as KeyMint will have no record of them. |
| 441 | // This applies to CREATION_DATETIME in particular. |
| 442 | vector<KeyCharacteristics> expected_characteristics(generate_characteristics); |
| 443 | strip_keystore_tags(&expected_characteristics); |
| 444 | |
| 445 | vector<KeyCharacteristics> retrieved; |
| 446 | ASSERT_EQ(ErrorCode::OK, GetCharacteristics(key_blob, &retrieved)); |
| 447 | EXPECT_EQ(expected_characteristics, retrieved); |
| 448 | } |
| 449 | |
| 450 | void KeyMintAidlTestBase::CheckAppIdCharacteristics( |
| 451 | const vector<uint8_t>& key_blob, std::string_view app_id_string, |
| 452 | std::string_view app_data_string, |
| 453 | const vector<KeyCharacteristics>& generate_characteristics) { |
| 454 | // Exclude any SecurityLevel::KEYSTORE characteristics for comparisons. |
| 455 | vector<KeyCharacteristics> expected_characteristics(generate_characteristics); |
| 456 | strip_keystore_tags(&expected_characteristics); |
| 457 | |
| 458 | vector<uint8_t> app_id(app_id_string.begin(), app_id_string.end()); |
| 459 | vector<uint8_t> app_data(app_data_string.begin(), app_data_string.end()); |
| 460 | vector<KeyCharacteristics> retrieved; |
| 461 | ASSERT_EQ(ErrorCode::OK, GetCharacteristics(key_blob, app_id, app_data, &retrieved)); |
| 462 | EXPECT_EQ(expected_characteristics, retrieved); |
| 463 | |
| 464 | // Check that key characteristics can't be retrieved if the app ID or app data is missing. |
| 465 | vector<uint8_t> empty; |
| 466 | vector<KeyCharacteristics> not_retrieved; |
| 467 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 468 | GetCharacteristics(key_blob, empty, app_data, ¬_retrieved)); |
| 469 | EXPECT_EQ(not_retrieved.size(), 0); |
| 470 | |
| 471 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 472 | GetCharacteristics(key_blob, app_id, empty, ¬_retrieved)); |
| 473 | EXPECT_EQ(not_retrieved.size(), 0); |
| 474 | |
| 475 | EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB, |
| 476 | GetCharacteristics(key_blob, empty, empty, ¬_retrieved)); |
| 477 | EXPECT_EQ(not_retrieved.size(), 0); |
| 478 | } |
| 479 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 480 | ErrorCode KeyMintAidlTestBase::DeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob) { |
| 481 | Status result = keymint_->deleteKey(*key_blob); |
| 482 | if (!keep_key_blob) { |
| 483 | *key_blob = vector<uint8_t>(); |
| 484 | } |
| 485 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 486 | EXPECT_TRUE(result.isOk()) << result.getServiceSpecificError() << endl; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 487 | return GetReturnErrorCode(result); |
| 488 | } |
| 489 | |
| 490 | ErrorCode KeyMintAidlTestBase::DeleteKey(bool keep_key_blob) { |
| 491 | return DeleteKey(&key_blob_, keep_key_blob); |
| 492 | } |
| 493 | |
| 494 | ErrorCode KeyMintAidlTestBase::DeleteAllKeys() { |
| 495 | Status result = keymint_->deleteAllKeys(); |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 496 | EXPECT_TRUE(result.isOk()) << result.getServiceSpecificError() << endl; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 497 | return GetReturnErrorCode(result); |
| 498 | } |
| 499 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 500 | ErrorCode KeyMintAidlTestBase::DestroyAttestationIds() { |
| 501 | Status result = keymint_->destroyAttestationIds(); |
| 502 | return GetReturnErrorCode(result); |
| 503 | } |
| 504 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 505 | void KeyMintAidlTestBase::CheckedDeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob) { |
| 506 | ErrorCode result = DeleteKey(key_blob, keep_key_blob); |
| 507 | EXPECT_TRUE(result == ErrorCode::OK || result == ErrorCode::UNIMPLEMENTED) << result << endl; |
| 508 | } |
| 509 | |
| 510 | void KeyMintAidlTestBase::CheckedDeleteKey() { |
| 511 | CheckedDeleteKey(&key_blob_); |
| 512 | } |
| 513 | |
| 514 | ErrorCode KeyMintAidlTestBase::Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob, |
| 515 | const AuthorizationSet& in_params, |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 516 | AuthorizationSet* out_params, |
| 517 | std::shared_ptr<IKeyMintOperation>& op) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 518 | SCOPED_TRACE("Begin"); |
| 519 | Status result; |
| 520 | BeginResult out; |
David Drysdale | 56ba912 | 2021-04-19 19:10:47 +0100 | [diff] [blame] | 521 | result = keymint_->begin(purpose, key_blob, in_params.vector_data(), std::nullopt, &out); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 522 | |
| 523 | if (result.isOk()) { |
| 524 | *out_params = out.params; |
| 525 | challenge_ = out.challenge; |
| 526 | op = out.operation; |
| 527 | } |
| 528 | |
| 529 | return GetReturnErrorCode(result); |
| 530 | } |
| 531 | |
| 532 | ErrorCode KeyMintAidlTestBase::Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob, |
| 533 | const AuthorizationSet& in_params, |
David Drysdale | 28fa931 | 2023-02-01 14:53:01 +0000 | [diff] [blame] | 534 | AuthorizationSet* out_params, |
| 535 | std::optional<HardwareAuthToken> hat) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 536 | SCOPED_TRACE("Begin"); |
| 537 | Status result; |
| 538 | BeginResult out; |
| 539 | |
David Drysdale | 28fa931 | 2023-02-01 14:53:01 +0000 | [diff] [blame] | 540 | result = keymint_->begin(purpose, key_blob, in_params.vector_data(), hat, &out); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 541 | |
| 542 | if (result.isOk()) { |
| 543 | *out_params = out.params; |
| 544 | challenge_ = out.challenge; |
| 545 | op_ = out.operation; |
| 546 | } |
| 547 | |
| 548 | return GetReturnErrorCode(result); |
| 549 | } |
| 550 | |
| 551 | ErrorCode KeyMintAidlTestBase::Begin(KeyPurpose purpose, const AuthorizationSet& in_params, |
| 552 | AuthorizationSet* out_params) { |
| 553 | SCOPED_TRACE("Begin"); |
| 554 | EXPECT_EQ(nullptr, op_); |
| 555 | return Begin(purpose, key_blob_, in_params, out_params); |
| 556 | } |
| 557 | |
| 558 | ErrorCode KeyMintAidlTestBase::Begin(KeyPurpose purpose, const AuthorizationSet& in_params) { |
| 559 | SCOPED_TRACE("Begin"); |
| 560 | AuthorizationSet out_params; |
| 561 | ErrorCode result = Begin(purpose, in_params, &out_params); |
| 562 | EXPECT_TRUE(out_params.empty()); |
| 563 | return result; |
| 564 | } |
| 565 | |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 566 | ErrorCode KeyMintAidlTestBase::UpdateAad(const string& input) { |
| 567 | return GetReturnErrorCode(op_->updateAad(vector<uint8_t>(input.begin(), input.end()), |
| 568 | {} /* hardwareAuthToken */, |
| 569 | {} /* verificationToken */)); |
| 570 | } |
| 571 | |
| 572 | ErrorCode KeyMintAidlTestBase::Update(const string& input, string* output) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 573 | SCOPED_TRACE("Update"); |
| 574 | |
| 575 | Status result; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 576 | if (!output) return ErrorCode::UNEXPECTED_NULL_POINTER; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 577 | |
Brian J Murray | eabd9d6 | 2022-01-06 15:13:51 -0800 | [diff] [blame] | 578 | EXPECT_NE(op_, nullptr); |
| 579 | if (!op_) return ErrorCode::UNEXPECTED_NULL_POINTER; |
| 580 | |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 581 | std::vector<uint8_t> o_put; |
| 582 | result = op_->update(vector<uint8_t>(input.begin(), input.end()), {}, {}, &o_put); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 583 | |
David Drysdale | feab5d9 | 2022-01-06 15:46:23 +0000 | [diff] [blame] | 584 | if (result.isOk()) { |
| 585 | output->append(o_put.begin(), o_put.end()); |
| 586 | } else { |
| 587 | // Failure always terminates the operation. |
| 588 | op_ = {}; |
| 589 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 590 | |
| 591 | return GetReturnErrorCode(result); |
| 592 | } |
| 593 | |
David Drysdale | 28fa931 | 2023-02-01 14:53:01 +0000 | [diff] [blame] | 594 | ErrorCode KeyMintAidlTestBase::Finish(const string& input, const string& signature, string* output, |
| 595 | std::optional<HardwareAuthToken> hat, |
| 596 | std::optional<secureclock::TimeStampToken> time_token) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 597 | SCOPED_TRACE("Finish"); |
| 598 | Status result; |
| 599 | |
| 600 | EXPECT_NE(op_, nullptr); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 601 | if (!op_) return ErrorCode::UNEXPECTED_NULL_POINTER; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 602 | |
| 603 | vector<uint8_t> oPut; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 604 | result = op_->finish(vector<uint8_t>(input.begin(), input.end()), |
David Drysdale | 28fa931 | 2023-02-01 14:53:01 +0000 | [diff] [blame] | 605 | vector<uint8_t>(signature.begin(), signature.end()), hat, time_token, |
| 606 | {} /* confirmationToken */, &oPut); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 607 | |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 608 | if (result.isOk()) output->append(oPut.begin(), oPut.end()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 609 | |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 610 | op_ = {}; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 611 | return GetReturnErrorCode(result); |
| 612 | } |
| 613 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 614 | ErrorCode KeyMintAidlTestBase::Abort(const std::shared_ptr<IKeyMintOperation>& op) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 615 | SCOPED_TRACE("Abort"); |
| 616 | |
| 617 | EXPECT_NE(op, nullptr); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 618 | if (!op) return ErrorCode::UNEXPECTED_NULL_POINTER; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 619 | |
| 620 | Status retval = op->abort(); |
| 621 | EXPECT_TRUE(retval.isOk()); |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 622 | return static_cast<ErrorCode>(retval.getServiceSpecificError()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | ErrorCode KeyMintAidlTestBase::Abort() { |
| 626 | SCOPED_TRACE("Abort"); |
| 627 | |
| 628 | EXPECT_NE(op_, nullptr); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 629 | if (!op_) return ErrorCode::UNEXPECTED_NULL_POINTER; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 630 | |
| 631 | Status retval = op_->abort(); |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 632 | return static_cast<ErrorCode>(retval.getServiceSpecificError()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | void KeyMintAidlTestBase::AbortIfNeeded() { |
| 636 | SCOPED_TRACE("AbortIfNeeded"); |
| 637 | if (op_) { |
| 638 | EXPECT_EQ(ErrorCode::OK, Abort()); |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 639 | op_.reset(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 643 | auto KeyMintAidlTestBase::ProcessMessage(const vector<uint8_t>& key_blob, KeyPurpose operation, |
| 644 | const string& message, const AuthorizationSet& in_params) |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 645 | -> std::tuple<ErrorCode, string> { |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 646 | AuthorizationSet begin_out_params; |
| 647 | ErrorCode result = Begin(operation, key_blob, in_params, &begin_out_params); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 648 | if (result != ErrorCode::OK) return {result, {}}; |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 649 | |
| 650 | string output; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 651 | return {Finish(message, &output), output}; |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 654 | string KeyMintAidlTestBase::ProcessMessage(const vector<uint8_t>& key_blob, KeyPurpose operation, |
| 655 | const string& message, const AuthorizationSet& in_params, |
| 656 | AuthorizationSet* out_params) { |
| 657 | SCOPED_TRACE("ProcessMessage"); |
| 658 | AuthorizationSet begin_out_params; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 659 | ErrorCode result = Begin(operation, key_blob, in_params, out_params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 660 | EXPECT_EQ(ErrorCode::OK, result); |
| 661 | if (result != ErrorCode::OK) { |
| 662 | return ""; |
| 663 | } |
| 664 | |
| 665 | string output; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 666 | EXPECT_EQ(ErrorCode::OK, Finish(message, &output)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 667 | return output; |
| 668 | } |
| 669 | |
| 670 | string KeyMintAidlTestBase::SignMessage(const vector<uint8_t>& key_blob, const string& message, |
| 671 | const AuthorizationSet& params) { |
| 672 | SCOPED_TRACE("SignMessage"); |
| 673 | AuthorizationSet out_params; |
| 674 | string signature = ProcessMessage(key_blob, KeyPurpose::SIGN, message, params, &out_params); |
| 675 | EXPECT_TRUE(out_params.empty()); |
| 676 | return signature; |
| 677 | } |
| 678 | |
| 679 | string KeyMintAidlTestBase::SignMessage(const string& message, const AuthorizationSet& params) { |
| 680 | SCOPED_TRACE("SignMessage"); |
| 681 | return SignMessage(key_blob_, message, params); |
| 682 | } |
| 683 | |
| 684 | string KeyMintAidlTestBase::MacMessage(const string& message, Digest digest, size_t mac_length) { |
| 685 | SCOPED_TRACE("MacMessage"); |
| 686 | return SignMessage( |
| 687 | key_blob_, message, |
| 688 | AuthorizationSetBuilder().Digest(digest).Authorization(TAG_MAC_LENGTH, mac_length)); |
| 689 | } |
| 690 | |
anil.hiranniah | 19a4ca1 | 2022-03-03 17:39:30 +0530 | [diff] [blame] | 691 | void KeyMintAidlTestBase::CheckAesIncrementalEncryptOperation(BlockMode block_mode, |
| 692 | int message_size) { |
David Drysdale | 1a63719 | 2022-03-14 09:11:29 +0000 | [diff] [blame] | 693 | auto builder = AuthorizationSetBuilder() |
| 694 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 695 | .AesEncryptionKey(128) |
| 696 | .BlockMode(block_mode) |
| 697 | .Padding(PaddingMode::NONE); |
| 698 | if (block_mode == BlockMode::GCM) { |
| 699 | builder.Authorization(TAG_MIN_MAC_LENGTH, 128); |
| 700 | } |
| 701 | ASSERT_EQ(ErrorCode::OK, GenerateKey(builder)); |
anil.hiranniah | 19a4ca1 | 2022-03-03 17:39:30 +0530 | [diff] [blame] | 702 | |
| 703 | for (int increment = 1; increment <= message_size; ++increment) { |
| 704 | string message(message_size, 'a'); |
| 705 | auto params = AuthorizationSetBuilder().BlockMode(block_mode).Padding(PaddingMode::NONE); |
| 706 | if (block_mode == BlockMode::GCM) { |
| 707 | params.Authorization(TAG_MAC_LENGTH, 128) /* for GCM */; |
| 708 | } |
| 709 | |
| 710 | AuthorizationSet output_params; |
| 711 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params, &output_params)); |
| 712 | |
| 713 | string ciphertext; |
| 714 | string to_send; |
| 715 | for (size_t i = 0; i < message.size(); i += increment) { |
| 716 | EXPECT_EQ(ErrorCode::OK, Update(message.substr(i, increment), &ciphertext)); |
| 717 | } |
| 718 | EXPECT_EQ(ErrorCode::OK, Finish(to_send, &ciphertext)) |
| 719 | << "Error sending " << to_send << " with block mode " << block_mode; |
| 720 | |
| 721 | switch (block_mode) { |
| 722 | case BlockMode::GCM: |
| 723 | EXPECT_EQ(message.size() + 16, ciphertext.size()); |
| 724 | break; |
| 725 | case BlockMode::CTR: |
| 726 | EXPECT_EQ(message.size(), ciphertext.size()); |
| 727 | break; |
| 728 | case BlockMode::CBC: |
| 729 | case BlockMode::ECB: |
| 730 | EXPECT_EQ(message.size() + message.size() % 16, ciphertext.size()); |
| 731 | break; |
| 732 | } |
| 733 | |
| 734 | auto iv = output_params.GetTagValue(TAG_NONCE); |
| 735 | switch (block_mode) { |
| 736 | case BlockMode::CBC: |
| 737 | case BlockMode::GCM: |
| 738 | case BlockMode::CTR: |
| 739 | ASSERT_TRUE(iv) << "No IV for block mode " << block_mode; |
| 740 | EXPECT_EQ(block_mode == BlockMode::GCM ? 12U : 16U, iv->get().size()); |
| 741 | params.push_back(TAG_NONCE, iv->get()); |
| 742 | break; |
| 743 | |
| 744 | case BlockMode::ECB: |
| 745 | EXPECT_FALSE(iv) << "ECB mode should not generate IV"; |
| 746 | break; |
| 747 | } |
| 748 | |
| 749 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::DECRYPT, params)) |
| 750 | << "Decrypt begin() failed for block mode " << block_mode; |
| 751 | |
| 752 | string plaintext; |
| 753 | for (size_t i = 0; i < ciphertext.size(); i += increment) { |
| 754 | EXPECT_EQ(ErrorCode::OK, Update(ciphertext.substr(i, increment), &plaintext)); |
| 755 | } |
| 756 | ErrorCode error = Finish(to_send, &plaintext); |
| 757 | ASSERT_EQ(ErrorCode::OK, error) << "Decryption failed for block mode " << block_mode |
| 758 | << " and increment " << increment; |
| 759 | if (error == ErrorCode::OK) { |
| 760 | ASSERT_EQ(message, plaintext) << "Decryption didn't match for block mode " << block_mode |
| 761 | << " and increment " << increment; |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
Prashant Patil | dd5f7f0 | 2022-07-06 18:58:07 +0000 | [diff] [blame] | 766 | void KeyMintAidlTestBase::AesCheckEncryptOneByteAtATime(const string& key, BlockMode block_mode, |
| 767 | PaddingMode padding_mode, const string& iv, |
| 768 | const string& plaintext, |
| 769 | const string& exp_cipher_text) { |
| 770 | bool is_authenticated_cipher = (block_mode == BlockMode::GCM); |
| 771 | auto auth_set = AuthorizationSetBuilder() |
| 772 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 773 | .AesEncryptionKey(key.size() * 8) |
| 774 | .BlockMode(block_mode) |
| 775 | .Padding(padding_mode); |
| 776 | if (iv.size() > 0) auth_set.Authorization(TAG_CALLER_NONCE); |
| 777 | if (is_authenticated_cipher) auth_set.Authorization(TAG_MIN_MAC_LENGTH, 128); |
| 778 | ASSERT_EQ(ErrorCode::OK, ImportKey(auth_set, KeyFormat::RAW, key)); |
| 779 | |
| 780 | CheckEncryptOneByteAtATime(block_mode, 16 /*block_size*/, padding_mode, iv, plaintext, |
| 781 | exp_cipher_text); |
| 782 | } |
| 783 | |
| 784 | void KeyMintAidlTestBase::CheckEncryptOneByteAtATime(BlockMode block_mode, const int block_size, |
| 785 | PaddingMode padding_mode, const string& iv, |
| 786 | const string& plaintext, |
| 787 | const string& exp_cipher_text) { |
| 788 | bool is_stream_cipher = (block_mode == BlockMode::CTR || block_mode == BlockMode::GCM); |
| 789 | bool is_authenticated_cipher = (block_mode == BlockMode::GCM); |
| 790 | auto params = AuthorizationSetBuilder().BlockMode(block_mode).Padding(padding_mode); |
| 791 | if (iv.size() > 0) params.Authorization(TAG_NONCE, iv.data(), iv.size()); |
| 792 | if (is_authenticated_cipher) params.Authorization(TAG_MAC_LENGTH, 128); |
| 793 | |
| 794 | AuthorizationSet output_params; |
| 795 | EXPECT_EQ(ErrorCode::OK, Begin(KeyPurpose::ENCRYPT, params, &output_params)); |
| 796 | |
| 797 | string actual_ciphertext; |
| 798 | if (is_stream_cipher) { |
| 799 | // Assert that a 1 byte of output is produced for 1 byte of input. |
| 800 | // Every input byte produces an output byte. |
| 801 | for (int plaintext_index = 0; plaintext_index < plaintext.size(); plaintext_index++) { |
| 802 | string ciphertext; |
| 803 | EXPECT_EQ(ErrorCode::OK, Update(plaintext.substr(plaintext_index, 1), &ciphertext)); |
| 804 | // Some StrongBox implementations cannot support 1:1 input:output lengths, so |
| 805 | // we relax this API restriction for them. |
| 806 | if (SecLevel() != SecurityLevel::STRONGBOX) { |
| 807 | EXPECT_EQ(1, ciphertext.size()) << "plaintext index: " << plaintext_index; |
| 808 | } |
| 809 | actual_ciphertext.append(ciphertext); |
| 810 | } |
| 811 | string ciphertext; |
| 812 | EXPECT_EQ(ErrorCode::OK, Finish(&ciphertext)); |
| 813 | if (SecLevel() != SecurityLevel::STRONGBOX) { |
| 814 | string expected_final_output; |
| 815 | if (is_authenticated_cipher) { |
| 816 | expected_final_output = exp_cipher_text.substr(plaintext.size()); |
| 817 | } |
| 818 | EXPECT_EQ(expected_final_output, ciphertext); |
| 819 | } |
| 820 | actual_ciphertext.append(ciphertext); |
| 821 | } else { |
| 822 | // Assert that a block of output is produced once a full block of input is provided. |
| 823 | // Every input block produces an output block. |
| 824 | bool compare_output = true; |
| 825 | string additional_information; |
| 826 | int vendor_api_level = property_get_int32("ro.vendor.api_level", 0); |
| 827 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 828 | // This is known to be broken on older vendor implementations. |
Shawn Willden | 1a545db | 2023-02-22 14:32:33 -0700 | [diff] [blame] | 829 | if (vendor_api_level < __ANDROID_API_T__) { |
Prashant Patil | dd5f7f0 | 2022-07-06 18:58:07 +0000 | [diff] [blame] | 830 | compare_output = false; |
| 831 | } else { |
| 832 | additional_information = " (b/194134359) "; |
| 833 | } |
| 834 | } |
| 835 | for (int plaintext_index = 0; plaintext_index < plaintext.size(); plaintext_index++) { |
| 836 | string ciphertext; |
| 837 | EXPECT_EQ(ErrorCode::OK, Update(plaintext.substr(plaintext_index, 1), &ciphertext)); |
| 838 | if (compare_output) { |
| 839 | if ((plaintext_index % block_size) == block_size - 1) { |
| 840 | // Update is expected to have output a new block |
| 841 | EXPECT_EQ(block_size, ciphertext.size()) |
| 842 | << "plaintext index: " << plaintext_index << additional_information; |
| 843 | } else { |
| 844 | // Update is expected to have produced no output |
| 845 | EXPECT_EQ(0, ciphertext.size()) |
| 846 | << "plaintext index: " << plaintext_index << additional_information; |
| 847 | } |
| 848 | } |
| 849 | actual_ciphertext.append(ciphertext); |
| 850 | } |
| 851 | string ciphertext; |
| 852 | EXPECT_EQ(ErrorCode::OK, Finish(&ciphertext)); |
| 853 | actual_ciphertext.append(ciphertext); |
| 854 | } |
| 855 | // Regardless of how the completed ciphertext got accumulated, it should match the expected |
| 856 | // ciphertext. |
| 857 | EXPECT_EQ(exp_cipher_text, actual_ciphertext); |
| 858 | } |
| 859 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 860 | void KeyMintAidlTestBase::CheckHmacTestVector(const string& key, const string& message, |
| 861 | Digest digest, const string& expected_mac) { |
| 862 | SCOPED_TRACE("CheckHmacTestVector"); |
| 863 | ASSERT_EQ(ErrorCode::OK, |
| 864 | ImportKey(AuthorizationSetBuilder() |
| 865 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 866 | .HmacKey(key.size() * 8) |
| 867 | .Authorization(TAG_MIN_MAC_LENGTH, expected_mac.size() * 8) |
| 868 | .Digest(digest), |
| 869 | KeyFormat::RAW, key)); |
| 870 | string signature = MacMessage(message, digest, expected_mac.size() * 8); |
| 871 | EXPECT_EQ(expected_mac, signature) |
| 872 | << "Test vector didn't match for key of size " << key.size() << " message of size " |
| 873 | << message.size() << " and digest " << digest; |
| 874 | CheckedDeleteKey(); |
| 875 | } |
| 876 | |
| 877 | void KeyMintAidlTestBase::CheckAesCtrTestVector(const string& key, const string& nonce, |
| 878 | const string& message, |
| 879 | const string& expected_ciphertext) { |
| 880 | SCOPED_TRACE("CheckAesCtrTestVector"); |
| 881 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 882 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 883 | .AesEncryptionKey(key.size() * 8) |
| 884 | .BlockMode(BlockMode::CTR) |
| 885 | .Authorization(TAG_CALLER_NONCE) |
| 886 | .Padding(PaddingMode::NONE), |
| 887 | KeyFormat::RAW, key)); |
| 888 | |
| 889 | auto params = AuthorizationSetBuilder() |
| 890 | .Authorization(TAG_NONCE, nonce.data(), nonce.size()) |
| 891 | .BlockMode(BlockMode::CTR) |
| 892 | .Padding(PaddingMode::NONE); |
| 893 | AuthorizationSet out_params; |
| 894 | string ciphertext = EncryptMessage(key_blob_, message, params, &out_params); |
| 895 | EXPECT_EQ(expected_ciphertext, ciphertext); |
| 896 | } |
| 897 | |
| 898 | void KeyMintAidlTestBase::CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode, |
| 899 | PaddingMode padding_mode, const string& key, |
| 900 | const string& iv, const string& input, |
| 901 | const string& expected_output) { |
| 902 | auto authset = AuthorizationSetBuilder() |
| 903 | .TripleDesEncryptionKey(key.size() * 7) |
| 904 | .BlockMode(block_mode) |
| 905 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 906 | .Padding(padding_mode); |
| 907 | if (iv.size()) authset.Authorization(TAG_CALLER_NONCE); |
| 908 | ASSERT_EQ(ErrorCode::OK, ImportKey(authset, KeyFormat::RAW, key)); |
| 909 | ASSERT_GT(key_blob_.size(), 0U); |
| 910 | |
| 911 | auto begin_params = AuthorizationSetBuilder().BlockMode(block_mode).Padding(padding_mode); |
| 912 | if (iv.size()) begin_params.Authorization(TAG_NONCE, iv.data(), iv.size()); |
| 913 | AuthorizationSet output_params; |
| 914 | string output = ProcessMessage(key_blob_, purpose, input, begin_params, &output_params); |
| 915 | EXPECT_EQ(expected_output, output); |
| 916 | } |
| 917 | |
| 918 | void KeyMintAidlTestBase::VerifyMessage(const vector<uint8_t>& key_blob, const string& message, |
| 919 | const string& signature, const AuthorizationSet& params) { |
| 920 | SCOPED_TRACE("VerifyMessage"); |
| 921 | AuthorizationSet begin_out_params; |
| 922 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::VERIFY, key_blob, params, &begin_out_params)); |
| 923 | |
| 924 | string output; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 925 | EXPECT_EQ(ErrorCode::OK, Finish(message, signature, &output)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 926 | EXPECT_TRUE(output.empty()); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 927 | op_ = {}; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | void KeyMintAidlTestBase::VerifyMessage(const string& message, const string& signature, |
| 931 | const AuthorizationSet& params) { |
| 932 | SCOPED_TRACE("VerifyMessage"); |
| 933 | VerifyMessage(key_blob_, message, signature, params); |
| 934 | } |
| 935 | |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 936 | void KeyMintAidlTestBase::LocalVerifyMessage(const string& message, const string& signature, |
| 937 | const AuthorizationSet& params) { |
| 938 | SCOPED_TRACE("LocalVerifyMessage"); |
| 939 | |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 940 | ASSERT_GT(cert_chain_.size(), 0); |
David Drysdale | 9f5c0c5 | 2022-11-03 15:10:16 +0000 | [diff] [blame] | 941 | LocalVerifyMessage(cert_chain_[0].encodedCertificate, message, signature, params); |
| 942 | } |
| 943 | |
| 944 | void KeyMintAidlTestBase::LocalVerifyMessage(const vector<uint8_t>& der_cert, const string& message, |
| 945 | const string& signature, |
| 946 | const AuthorizationSet& params) { |
| 947 | // Retrieve the public key from the leaf certificate. |
| 948 | X509_Ptr key_cert(parse_cert_blob(der_cert)); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 949 | ASSERT_TRUE(key_cert.get()); |
| 950 | EVP_PKEY_Ptr pub_key(X509_get_pubkey(key_cert.get())); |
| 951 | ASSERT_TRUE(pub_key.get()); |
| 952 | |
| 953 | Digest digest = params.GetTagValue(TAG_DIGEST).value(); |
| 954 | PaddingMode padding = PaddingMode::NONE; |
| 955 | auto tag = params.GetTagValue(TAG_PADDING); |
| 956 | if (tag.has_value()) { |
| 957 | padding = tag.value(); |
| 958 | } |
| 959 | |
| 960 | if (digest == Digest::NONE) { |
| 961 | switch (EVP_PKEY_id(pub_key.get())) { |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 962 | case EVP_PKEY_ED25519: { |
| 963 | ASSERT_EQ(64, signature.size()); |
| 964 | uint8_t pub_keydata[32]; |
| 965 | size_t pub_len = sizeof(pub_keydata); |
| 966 | ASSERT_EQ(1, EVP_PKEY_get_raw_public_key(pub_key.get(), pub_keydata, &pub_len)); |
| 967 | ASSERT_EQ(sizeof(pub_keydata), pub_len); |
| 968 | ASSERT_EQ(1, ED25519_verify(reinterpret_cast<const uint8_t*>(message.data()), |
| 969 | message.size(), |
| 970 | reinterpret_cast<const uint8_t*>(signature.data()), |
| 971 | pub_keydata)); |
| 972 | break; |
| 973 | } |
| 974 | |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 975 | case EVP_PKEY_EC: { |
| 976 | vector<uint8_t> data((EVP_PKEY_bits(pub_key.get()) + 7) / 8); |
| 977 | size_t data_size = std::min(data.size(), message.size()); |
| 978 | memcpy(data.data(), message.data(), data_size); |
| 979 | EC_KEY_Ptr ecdsa(EVP_PKEY_get1_EC_KEY(pub_key.get())); |
| 980 | ASSERT_TRUE(ecdsa.get()); |
| 981 | ASSERT_EQ(1, |
| 982 | ECDSA_verify(0, reinterpret_cast<const uint8_t*>(data.data()), data_size, |
| 983 | reinterpret_cast<const uint8_t*>(signature.data()), |
| 984 | signature.size(), ecdsa.get())); |
| 985 | break; |
| 986 | } |
| 987 | case EVP_PKEY_RSA: { |
| 988 | vector<uint8_t> data(EVP_PKEY_size(pub_key.get())); |
| 989 | size_t data_size = std::min(data.size(), message.size()); |
| 990 | memcpy(data.data(), message.data(), data_size); |
| 991 | |
| 992 | RSA_Ptr rsa(EVP_PKEY_get1_RSA(const_cast<EVP_PKEY*>(pub_key.get()))); |
| 993 | ASSERT_TRUE(rsa.get()); |
| 994 | |
| 995 | size_t key_len = RSA_size(rsa.get()); |
| 996 | int openssl_padding = RSA_NO_PADDING; |
| 997 | switch (padding) { |
| 998 | case PaddingMode::NONE: |
| 999 | ASSERT_TRUE(data_size <= key_len); |
| 1000 | ASSERT_EQ(key_len, signature.size()); |
| 1001 | openssl_padding = RSA_NO_PADDING; |
| 1002 | break; |
| 1003 | case PaddingMode::RSA_PKCS1_1_5_SIGN: |
| 1004 | ASSERT_TRUE(data_size + kPkcs1UndigestedSignaturePaddingOverhead <= |
| 1005 | key_len); |
| 1006 | openssl_padding = RSA_PKCS1_PADDING; |
| 1007 | break; |
| 1008 | default: |
| 1009 | ADD_FAILURE() << "Unsupported RSA padding mode " << padding; |
| 1010 | } |
| 1011 | |
| 1012 | vector<uint8_t> decrypted_data(key_len); |
| 1013 | int bytes_decrypted = RSA_public_decrypt( |
| 1014 | signature.size(), reinterpret_cast<const uint8_t*>(signature.data()), |
| 1015 | decrypted_data.data(), rsa.get(), openssl_padding); |
| 1016 | ASSERT_GE(bytes_decrypted, 0); |
| 1017 | |
| 1018 | const uint8_t* compare_pos = decrypted_data.data(); |
| 1019 | size_t bytes_to_compare = bytes_decrypted; |
| 1020 | uint8_t zero_check_result = 0; |
| 1021 | if (padding == PaddingMode::NONE && data_size < bytes_to_compare) { |
| 1022 | // If the data is short, for "unpadded" signing we zero-pad to the left. So |
| 1023 | // during verification we should have zeros on the left of the decrypted data. |
| 1024 | // Do a constant-time check. |
| 1025 | const uint8_t* zero_end = compare_pos + bytes_to_compare - data_size; |
| 1026 | while (compare_pos < zero_end) zero_check_result |= *compare_pos++; |
| 1027 | ASSERT_EQ(0, zero_check_result); |
| 1028 | bytes_to_compare = data_size; |
| 1029 | } |
| 1030 | ASSERT_EQ(0, memcmp(compare_pos, data.data(), bytes_to_compare)); |
| 1031 | break; |
| 1032 | } |
| 1033 | default: |
| 1034 | ADD_FAILURE() << "Unknown public key type"; |
| 1035 | } |
| 1036 | } else { |
| 1037 | EVP_MD_CTX digest_ctx; |
| 1038 | EVP_MD_CTX_init(&digest_ctx); |
| 1039 | EVP_PKEY_CTX* pkey_ctx; |
| 1040 | const EVP_MD* md = openssl_digest(digest); |
| 1041 | ASSERT_NE(md, nullptr); |
| 1042 | ASSERT_EQ(1, EVP_DigestVerifyInit(&digest_ctx, &pkey_ctx, md, nullptr, pub_key.get())); |
| 1043 | |
| 1044 | if (padding == PaddingMode::RSA_PSS) { |
| 1045 | EXPECT_GT(EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING), 0); |
| 1046 | EXPECT_GT(EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, EVP_MD_size(md)), 0); |
David Drysdale | c6b8907 | 2021-12-14 14:32:51 +0000 | [diff] [blame] | 1047 | EXPECT_GT(EVP_PKEY_CTX_set_rsa_mgf1_md(pkey_ctx, md), 0); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | ASSERT_EQ(1, EVP_DigestVerifyUpdate(&digest_ctx, |
| 1051 | reinterpret_cast<const uint8_t*>(message.data()), |
| 1052 | message.size())); |
| 1053 | ASSERT_EQ(1, EVP_DigestVerifyFinal(&digest_ctx, |
| 1054 | reinterpret_cast<const uint8_t*>(signature.data()), |
| 1055 | signature.size())); |
| 1056 | EVP_MD_CTX_cleanup(&digest_ctx); |
| 1057 | } |
| 1058 | } |
| 1059 | |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 1060 | string KeyMintAidlTestBase::LocalRsaEncryptMessage(const string& message, |
| 1061 | const AuthorizationSet& params) { |
| 1062 | SCOPED_TRACE("LocalRsaEncryptMessage"); |
| 1063 | |
| 1064 | // Retrieve the public key from the leaf certificate. |
| 1065 | if (cert_chain_.empty()) { |
| 1066 | ADD_FAILURE() << "No public key available"; |
| 1067 | return "Failure"; |
| 1068 | } |
| 1069 | X509_Ptr key_cert(parse_cert_blob(cert_chain_[0].encodedCertificate)); |
David Drysdale | b97121d | 2022-08-12 11:54:08 +0100 | [diff] [blame] | 1070 | if (key_cert.get() == nullptr) { |
| 1071 | ADD_FAILURE() << "Failed to parse cert"; |
| 1072 | return "Failure"; |
| 1073 | } |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 1074 | EVP_PKEY_Ptr pub_key(X509_get_pubkey(key_cert.get())); |
David Drysdale | b97121d | 2022-08-12 11:54:08 +0100 | [diff] [blame] | 1075 | if (pub_key.get() == nullptr) { |
| 1076 | ADD_FAILURE() << "Failed to retrieve public key"; |
| 1077 | return "Failure"; |
| 1078 | } |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 1079 | RSA_Ptr rsa(EVP_PKEY_get1_RSA(const_cast<EVP_PKEY*>(pub_key.get()))); |
David Drysdale | b97121d | 2022-08-12 11:54:08 +0100 | [diff] [blame] | 1080 | if (rsa.get() == nullptr) { |
| 1081 | ADD_FAILURE() << "Failed to retrieve RSA public key"; |
| 1082 | return "Failure"; |
| 1083 | } |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 1084 | |
| 1085 | // Retrieve relevant tags. |
| 1086 | Digest digest = Digest::NONE; |
David Drysdale | ae3727b | 2021-11-11 09:00:14 +0000 | [diff] [blame] | 1087 | Digest mgf_digest = Digest::SHA1; |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 1088 | PaddingMode padding = PaddingMode::NONE; |
| 1089 | |
| 1090 | auto digest_tag = params.GetTagValue(TAG_DIGEST); |
| 1091 | if (digest_tag.has_value()) digest = digest_tag.value(); |
| 1092 | auto pad_tag = params.GetTagValue(TAG_PADDING); |
| 1093 | if (pad_tag.has_value()) padding = pad_tag.value(); |
| 1094 | auto mgf_tag = params.GetTagValue(TAG_RSA_OAEP_MGF_DIGEST); |
| 1095 | if (mgf_tag.has_value()) mgf_digest = mgf_tag.value(); |
| 1096 | |
| 1097 | const EVP_MD* md = openssl_digest(digest); |
| 1098 | const EVP_MD* mgf_md = openssl_digest(mgf_digest); |
| 1099 | |
| 1100 | // Set up encryption context. |
| 1101 | EVP_PKEY_CTX_Ptr ctx(EVP_PKEY_CTX_new(pub_key.get(), /* engine= */ nullptr)); |
| 1102 | if (EVP_PKEY_encrypt_init(ctx.get()) <= 0) { |
| 1103 | ADD_FAILURE() << "Encryption init failed: " << ERR_peek_last_error(); |
| 1104 | return "Failure"; |
| 1105 | } |
| 1106 | |
| 1107 | int rc = -1; |
| 1108 | switch (padding) { |
| 1109 | case PaddingMode::NONE: |
| 1110 | rc = EVP_PKEY_CTX_set_rsa_padding(ctx.get(), RSA_NO_PADDING); |
| 1111 | break; |
| 1112 | case PaddingMode::RSA_PKCS1_1_5_ENCRYPT: |
| 1113 | rc = EVP_PKEY_CTX_set_rsa_padding(ctx.get(), RSA_PKCS1_PADDING); |
| 1114 | break; |
| 1115 | case PaddingMode::RSA_OAEP: |
| 1116 | rc = EVP_PKEY_CTX_set_rsa_padding(ctx.get(), RSA_PKCS1_OAEP_PADDING); |
| 1117 | break; |
| 1118 | default: |
| 1119 | break; |
| 1120 | } |
| 1121 | if (rc <= 0) { |
| 1122 | ADD_FAILURE() << "Set padding failed: " << ERR_peek_last_error(); |
| 1123 | return "Failure"; |
| 1124 | } |
| 1125 | if (padding == PaddingMode::RSA_OAEP) { |
| 1126 | if (!EVP_PKEY_CTX_set_rsa_oaep_md(ctx.get(), md)) { |
| 1127 | ADD_FAILURE() << "Set digest failed: " << ERR_peek_last_error(); |
| 1128 | return "Failure"; |
| 1129 | } |
| 1130 | if (!EVP_PKEY_CTX_set_rsa_mgf1_md(ctx.get(), mgf_md)) { |
| 1131 | ADD_FAILURE() << "Set MGF digest failed: " << ERR_peek_last_error(); |
| 1132 | return "Failure"; |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | // Determine output size. |
| 1137 | size_t outlen; |
| 1138 | if (EVP_PKEY_encrypt(ctx.get(), nullptr /* out */, &outlen, |
| 1139 | reinterpret_cast<const uint8_t*>(message.data()), message.size()) <= 0) { |
| 1140 | ADD_FAILURE() << "Determine output size failed: " << ERR_peek_last_error(); |
| 1141 | return "Failure"; |
| 1142 | } |
| 1143 | |
| 1144 | // Left-zero-pad the input if necessary. |
| 1145 | const uint8_t* to_encrypt = reinterpret_cast<const uint8_t*>(message.data()); |
| 1146 | size_t to_encrypt_len = message.size(); |
| 1147 | |
| 1148 | std::unique_ptr<string> zero_padded_message; |
| 1149 | if (padding == PaddingMode::NONE && to_encrypt_len < outlen) { |
| 1150 | zero_padded_message.reset(new string(outlen, '\0')); |
| 1151 | memcpy(zero_padded_message->data() + (outlen - to_encrypt_len), message.data(), |
| 1152 | message.size()); |
| 1153 | to_encrypt = reinterpret_cast<const uint8_t*>(zero_padded_message->data()); |
| 1154 | to_encrypt_len = outlen; |
| 1155 | } |
| 1156 | |
| 1157 | // Do the encryption. |
| 1158 | string output(outlen, '\0'); |
| 1159 | if (EVP_PKEY_encrypt(ctx.get(), reinterpret_cast<uint8_t*>(output.data()), &outlen, to_encrypt, |
| 1160 | to_encrypt_len) <= 0) { |
| 1161 | ADD_FAILURE() << "Encryption failed: " << ERR_peek_last_error(); |
| 1162 | return "Failure"; |
| 1163 | } |
| 1164 | return output; |
| 1165 | } |
| 1166 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1167 | string KeyMintAidlTestBase::EncryptMessage(const vector<uint8_t>& key_blob, const string& message, |
| 1168 | const AuthorizationSet& in_params, |
| 1169 | AuthorizationSet* out_params) { |
| 1170 | SCOPED_TRACE("EncryptMessage"); |
| 1171 | return ProcessMessage(key_blob, KeyPurpose::ENCRYPT, message, in_params, out_params); |
| 1172 | } |
| 1173 | |
| 1174 | string KeyMintAidlTestBase::EncryptMessage(const string& message, const AuthorizationSet& params, |
| 1175 | AuthorizationSet* out_params) { |
| 1176 | SCOPED_TRACE("EncryptMessage"); |
| 1177 | return EncryptMessage(key_blob_, message, params, out_params); |
| 1178 | } |
| 1179 | |
| 1180 | string KeyMintAidlTestBase::EncryptMessage(const string& message, const AuthorizationSet& params) { |
| 1181 | SCOPED_TRACE("EncryptMessage"); |
| 1182 | AuthorizationSet out_params; |
| 1183 | string ciphertext = EncryptMessage(message, params, &out_params); |
| 1184 | EXPECT_TRUE(out_params.empty()) << "Output params should be empty. Contained: " << out_params; |
| 1185 | return ciphertext; |
| 1186 | } |
| 1187 | |
| 1188 | string KeyMintAidlTestBase::EncryptMessage(const string& message, BlockMode block_mode, |
| 1189 | PaddingMode padding) { |
| 1190 | SCOPED_TRACE("EncryptMessage"); |
| 1191 | auto params = AuthorizationSetBuilder().BlockMode(block_mode).Padding(padding); |
| 1192 | AuthorizationSet out_params; |
| 1193 | string ciphertext = EncryptMessage(message, params, &out_params); |
| 1194 | EXPECT_TRUE(out_params.empty()) << "Output params should be empty. Contained: " << out_params; |
| 1195 | return ciphertext; |
| 1196 | } |
| 1197 | |
| 1198 | string KeyMintAidlTestBase::EncryptMessage(const string& message, BlockMode block_mode, |
| 1199 | PaddingMode padding, vector<uint8_t>* iv_out) { |
| 1200 | SCOPED_TRACE("EncryptMessage"); |
| 1201 | auto params = AuthorizationSetBuilder().BlockMode(block_mode).Padding(padding); |
| 1202 | AuthorizationSet out_params; |
| 1203 | string ciphertext = EncryptMessage(message, params, &out_params); |
| 1204 | EXPECT_EQ(1U, out_params.size()); |
| 1205 | auto ivVal = out_params.GetTagValue(TAG_NONCE); |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 1206 | EXPECT_TRUE(ivVal); |
| 1207 | if (ivVal) *iv_out = *ivVal; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1208 | return ciphertext; |
| 1209 | } |
| 1210 | |
| 1211 | string KeyMintAidlTestBase::EncryptMessage(const string& message, BlockMode block_mode, |
| 1212 | PaddingMode padding, const vector<uint8_t>& iv_in) { |
| 1213 | SCOPED_TRACE("EncryptMessage"); |
| 1214 | auto params = AuthorizationSetBuilder() |
| 1215 | .BlockMode(block_mode) |
| 1216 | .Padding(padding) |
| 1217 | .Authorization(TAG_NONCE, iv_in); |
| 1218 | AuthorizationSet out_params; |
| 1219 | string ciphertext = EncryptMessage(message, params, &out_params); |
| 1220 | return ciphertext; |
| 1221 | } |
| 1222 | |
| 1223 | string KeyMintAidlTestBase::EncryptMessage(const string& message, BlockMode block_mode, |
| 1224 | PaddingMode padding, uint8_t mac_length_bits, |
| 1225 | const vector<uint8_t>& iv_in) { |
| 1226 | SCOPED_TRACE("EncryptMessage"); |
| 1227 | auto params = AuthorizationSetBuilder() |
| 1228 | .BlockMode(block_mode) |
| 1229 | .Padding(padding) |
| 1230 | .Authorization(TAG_MAC_LENGTH, mac_length_bits) |
| 1231 | .Authorization(TAG_NONCE, iv_in); |
| 1232 | AuthorizationSet out_params; |
| 1233 | string ciphertext = EncryptMessage(message, params, &out_params); |
| 1234 | return ciphertext; |
| 1235 | } |
| 1236 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 1237 | string KeyMintAidlTestBase::EncryptMessage(const string& message, BlockMode block_mode, |
| 1238 | PaddingMode padding, uint8_t mac_length_bits) { |
| 1239 | SCOPED_TRACE("EncryptMessage"); |
| 1240 | auto params = AuthorizationSetBuilder() |
| 1241 | .BlockMode(block_mode) |
| 1242 | .Padding(padding) |
| 1243 | .Authorization(TAG_MAC_LENGTH, mac_length_bits); |
| 1244 | AuthorizationSet out_params; |
| 1245 | string ciphertext = EncryptMessage(message, params, &out_params); |
| 1246 | return ciphertext; |
| 1247 | } |
| 1248 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1249 | string KeyMintAidlTestBase::DecryptMessage(const vector<uint8_t>& key_blob, |
| 1250 | const string& ciphertext, |
| 1251 | const AuthorizationSet& params) { |
| 1252 | SCOPED_TRACE("DecryptMessage"); |
| 1253 | AuthorizationSet out_params; |
| 1254 | string plaintext = |
| 1255 | ProcessMessage(key_blob, KeyPurpose::DECRYPT, ciphertext, params, &out_params); |
| 1256 | EXPECT_TRUE(out_params.empty()); |
| 1257 | return plaintext; |
| 1258 | } |
| 1259 | |
| 1260 | string KeyMintAidlTestBase::DecryptMessage(const string& ciphertext, |
| 1261 | const AuthorizationSet& params) { |
| 1262 | SCOPED_TRACE("DecryptMessage"); |
| 1263 | return DecryptMessage(key_blob_, ciphertext, params); |
| 1264 | } |
| 1265 | |
| 1266 | string KeyMintAidlTestBase::DecryptMessage(const string& ciphertext, BlockMode block_mode, |
| 1267 | PaddingMode padding_mode, const vector<uint8_t>& iv) { |
| 1268 | SCOPED_TRACE("DecryptMessage"); |
| 1269 | auto params = AuthorizationSetBuilder() |
| 1270 | .BlockMode(block_mode) |
| 1271 | .Padding(padding_mode) |
| 1272 | .Authorization(TAG_NONCE, iv); |
| 1273 | return DecryptMessage(key_blob_, ciphertext, params); |
| 1274 | } |
| 1275 | |
| 1276 | std::pair<ErrorCode, vector<uint8_t>> KeyMintAidlTestBase::UpgradeKey( |
| 1277 | const vector<uint8_t>& key_blob) { |
| 1278 | std::pair<ErrorCode, vector<uint8_t>> retval; |
| 1279 | vector<uint8_t> outKeyBlob; |
| 1280 | Status result = keymint_->upgradeKey(key_blob, vector<KeyParameter>(), &outKeyBlob); |
| 1281 | ErrorCode errorcode = GetReturnErrorCode(result); |
| 1282 | retval = std::tie(errorcode, outKeyBlob); |
| 1283 | |
| 1284 | return retval; |
| 1285 | } |
| 1286 | vector<uint32_t> KeyMintAidlTestBase::ValidKeySizes(Algorithm algorithm) { |
| 1287 | switch (algorithm) { |
| 1288 | case Algorithm::RSA: |
| 1289 | switch (SecLevel()) { |
| 1290 | case SecurityLevel::SOFTWARE: |
| 1291 | case SecurityLevel::TRUSTED_ENVIRONMENT: |
| 1292 | return {2048, 3072, 4096}; |
| 1293 | case SecurityLevel::STRONGBOX: |
| 1294 | return {2048}; |
| 1295 | default: |
| 1296 | ADD_FAILURE() << "Invalid security level " << uint32_t(SecLevel()); |
| 1297 | break; |
| 1298 | } |
| 1299 | break; |
| 1300 | case Algorithm::EC: |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1301 | ADD_FAILURE() << "EC keys must be specified by curve not size"; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1302 | break; |
| 1303 | case Algorithm::AES: |
| 1304 | return {128, 256}; |
| 1305 | case Algorithm::TRIPLE_DES: |
| 1306 | return {168}; |
| 1307 | case Algorithm::HMAC: { |
| 1308 | vector<uint32_t> retval((512 - 64) / 8 + 1); |
| 1309 | uint32_t size = 64 - 8; |
| 1310 | std::generate(retval.begin(), retval.end(), [&]() { return (size += 8); }); |
| 1311 | return retval; |
| 1312 | } |
| 1313 | default: |
| 1314 | ADD_FAILURE() << "Invalid Algorithm: " << algorithm; |
| 1315 | return {}; |
| 1316 | } |
| 1317 | ADD_FAILURE() << "Should be impossible to get here"; |
| 1318 | return {}; |
| 1319 | } |
| 1320 | |
| 1321 | vector<uint32_t> KeyMintAidlTestBase::InvalidKeySizes(Algorithm algorithm) { |
| 1322 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 1323 | switch (algorithm) { |
| 1324 | case Algorithm::RSA: |
| 1325 | return {3072, 4096}; |
| 1326 | case Algorithm::EC: |
| 1327 | return {224, 384, 521}; |
| 1328 | case Algorithm::AES: |
| 1329 | return {192}; |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 1330 | case Algorithm::TRIPLE_DES: |
| 1331 | return {56}; |
| 1332 | default: |
| 1333 | return {}; |
| 1334 | } |
| 1335 | } else { |
| 1336 | switch (algorithm) { |
Prashant Patil | d72b351 | 2021-11-16 08:19:19 +0000 | [diff] [blame] | 1337 | case Algorithm::AES: |
| 1338 | return {64, 96, 131, 512}; |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 1339 | case Algorithm::TRIPLE_DES: |
| 1340 | return {56}; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1341 | default: |
| 1342 | return {}; |
| 1343 | } |
| 1344 | } |
| 1345 | return {}; |
| 1346 | } |
| 1347 | |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 1348 | vector<BlockMode> KeyMintAidlTestBase::ValidBlockModes(Algorithm algorithm) { |
| 1349 | switch (algorithm) { |
| 1350 | case Algorithm::AES: |
| 1351 | return { |
| 1352 | BlockMode::CBC, |
| 1353 | BlockMode::CTR, |
| 1354 | BlockMode::ECB, |
| 1355 | BlockMode::GCM, |
| 1356 | }; |
| 1357 | case Algorithm::TRIPLE_DES: |
| 1358 | return { |
| 1359 | BlockMode::CBC, |
| 1360 | BlockMode::ECB, |
| 1361 | }; |
| 1362 | default: |
| 1363 | return {}; |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | vector<PaddingMode> KeyMintAidlTestBase::ValidPaddingModes(Algorithm algorithm, |
| 1368 | BlockMode blockMode) { |
| 1369 | switch (algorithm) { |
| 1370 | case Algorithm::AES: |
| 1371 | switch (blockMode) { |
| 1372 | case BlockMode::CBC: |
| 1373 | case BlockMode::ECB: |
| 1374 | return {PaddingMode::NONE, PaddingMode::PKCS7}; |
| 1375 | case BlockMode::CTR: |
| 1376 | case BlockMode::GCM: |
| 1377 | return {PaddingMode::NONE}; |
| 1378 | default: |
| 1379 | return {}; |
| 1380 | }; |
| 1381 | case Algorithm::TRIPLE_DES: |
| 1382 | switch (blockMode) { |
| 1383 | case BlockMode::CBC: |
| 1384 | case BlockMode::ECB: |
| 1385 | return {PaddingMode::NONE, PaddingMode::PKCS7}; |
| 1386 | default: |
| 1387 | return {}; |
| 1388 | }; |
| 1389 | default: |
| 1390 | return {}; |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | vector<PaddingMode> KeyMintAidlTestBase::InvalidPaddingModes(Algorithm algorithm, |
| 1395 | BlockMode blockMode) { |
| 1396 | switch (algorithm) { |
| 1397 | case Algorithm::AES: |
| 1398 | switch (blockMode) { |
| 1399 | case BlockMode::CTR: |
| 1400 | case BlockMode::GCM: |
| 1401 | return {PaddingMode::PKCS7}; |
| 1402 | default: |
| 1403 | return {}; |
| 1404 | }; |
| 1405 | default: |
| 1406 | return {}; |
| 1407 | } |
| 1408 | } |
| 1409 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1410 | vector<EcCurve> KeyMintAidlTestBase::ValidCurves() { |
| 1411 | if (securityLevel_ == SecurityLevel::STRONGBOX) { |
| 1412 | return {EcCurve::P_256}; |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 1413 | } else if (Curve25519Supported()) { |
| 1414 | return {EcCurve::P_224, EcCurve::P_256, EcCurve::P_384, EcCurve::P_521, |
| 1415 | EcCurve::CURVE_25519}; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1416 | } else { |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 1417 | return { |
| 1418 | EcCurve::P_224, |
| 1419 | EcCurve::P_256, |
| 1420 | EcCurve::P_384, |
| 1421 | EcCurve::P_521, |
| 1422 | }; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | vector<EcCurve> KeyMintAidlTestBase::InvalidCurves() { |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1427 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 1428 | // Curve 25519 is not supported, either because: |
| 1429 | // - KeyMint v1: it's an unknown enum value |
| 1430 | // - KeyMint v2+: it's not supported by StrongBox. |
| 1431 | return {EcCurve::P_224, EcCurve::P_384, EcCurve::P_521, EcCurve::CURVE_25519}; |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1432 | } else { |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 1433 | if (Curve25519Supported()) { |
| 1434 | return {}; |
| 1435 | } else { |
| 1436 | return {EcCurve::CURVE_25519}; |
| 1437 | } |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 1438 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1439 | } |
| 1440 | |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 1441 | vector<uint64_t> KeyMintAidlTestBase::ValidExponents() { |
| 1442 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 1443 | return {65537}; |
| 1444 | } else { |
| 1445 | return {3, 65537}; |
| 1446 | } |
| 1447 | } |
| 1448 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1449 | vector<Digest> KeyMintAidlTestBase::ValidDigests(bool withNone, bool withMD5) { |
| 1450 | switch (SecLevel()) { |
| 1451 | case SecurityLevel::SOFTWARE: |
| 1452 | case SecurityLevel::TRUSTED_ENVIRONMENT: |
| 1453 | if (withNone) { |
| 1454 | if (withMD5) |
| 1455 | return {Digest::NONE, Digest::MD5, Digest::SHA1, |
| 1456 | Digest::SHA_2_224, Digest::SHA_2_256, Digest::SHA_2_384, |
| 1457 | Digest::SHA_2_512}; |
| 1458 | else |
| 1459 | return {Digest::NONE, Digest::SHA1, Digest::SHA_2_224, |
| 1460 | Digest::SHA_2_256, Digest::SHA_2_384, Digest::SHA_2_512}; |
| 1461 | } else { |
| 1462 | if (withMD5) |
| 1463 | return {Digest::MD5, Digest::SHA1, Digest::SHA_2_224, |
| 1464 | Digest::SHA_2_256, Digest::SHA_2_384, Digest::SHA_2_512}; |
| 1465 | else |
| 1466 | return {Digest::SHA1, Digest::SHA_2_224, Digest::SHA_2_256, Digest::SHA_2_384, |
| 1467 | Digest::SHA_2_512}; |
| 1468 | } |
| 1469 | break; |
| 1470 | case SecurityLevel::STRONGBOX: |
| 1471 | if (withNone) |
| 1472 | return {Digest::NONE, Digest::SHA_2_256}; |
| 1473 | else |
| 1474 | return {Digest::SHA_2_256}; |
| 1475 | break; |
| 1476 | default: |
| 1477 | ADD_FAILURE() << "Invalid security level " << uint32_t(SecLevel()); |
| 1478 | break; |
| 1479 | } |
| 1480 | ADD_FAILURE() << "Should be impossible to get here"; |
| 1481 | return {}; |
| 1482 | } |
| 1483 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 1484 | static const vector<KeyParameter> kEmptyAuthList{}; |
| 1485 | |
| 1486 | const vector<KeyParameter>& KeyMintAidlTestBase::SecLevelAuthorizations( |
| 1487 | const vector<KeyCharacteristics>& key_characteristics) { |
| 1488 | auto found = std::find_if(key_characteristics.begin(), key_characteristics.end(), |
| 1489 | [this](auto& entry) { return entry.securityLevel == SecLevel(); }); |
| 1490 | return (found == key_characteristics.end()) ? kEmptyAuthList : found->authorizations; |
| 1491 | } |
| 1492 | |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 1493 | const vector<KeyParameter>& KeyMintAidlTestBase::SecLevelAuthorizations( |
| 1494 | const vector<KeyCharacteristics>& key_characteristics, SecurityLevel securityLevel) { |
| 1495 | auto found = std::find_if( |
| 1496 | key_characteristics.begin(), key_characteristics.end(), |
| 1497 | [securityLevel](auto& entry) { return entry.securityLevel == securityLevel; }); |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 1498 | return (found == key_characteristics.end()) ? kEmptyAuthList : found->authorizations; |
| 1499 | } |
| 1500 | |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 1501 | ErrorCode KeyMintAidlTestBase::UseAesKey(const vector<uint8_t>& aesKeyBlob) { |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 1502 | auto [result, ciphertext] = ProcessMessage( |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 1503 | aesKeyBlob, KeyPurpose::ENCRYPT, "1234567890123456", |
| 1504 | AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE)); |
| 1505 | return result; |
| 1506 | } |
| 1507 | |
| 1508 | ErrorCode KeyMintAidlTestBase::UseHmacKey(const vector<uint8_t>& hmacKeyBlob) { |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 1509 | auto [result, mac] = ProcessMessage( |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 1510 | hmacKeyBlob, KeyPurpose::SIGN, "1234567890123456", |
| 1511 | AuthorizationSetBuilder().Authorization(TAG_MAC_LENGTH, 128).Digest(Digest::SHA_2_256)); |
| 1512 | return result; |
| 1513 | } |
| 1514 | |
| 1515 | ErrorCode KeyMintAidlTestBase::UseRsaKey(const vector<uint8_t>& rsaKeyBlob) { |
| 1516 | std::string message(2048 / 8, 'a'); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 1517 | auto [result, signature] = ProcessMessage( |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 1518 | rsaKeyBlob, KeyPurpose::SIGN, message, |
| 1519 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE)); |
| 1520 | return result; |
| 1521 | } |
| 1522 | |
| 1523 | ErrorCode KeyMintAidlTestBase::UseEcdsaKey(const vector<uint8_t>& ecdsaKeyBlob) { |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 1524 | auto [result, signature] = ProcessMessage(ecdsaKeyBlob, KeyPurpose::SIGN, "a", |
| 1525 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256)); |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 1526 | return result; |
| 1527 | } |
| 1528 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1529 | void verify_serial(X509* cert, const uint64_t expected_serial) { |
| 1530 | BIGNUM_Ptr ser(BN_new()); |
| 1531 | EXPECT_TRUE(ASN1_INTEGER_to_BN(X509_get_serialNumber(cert), ser.get())); |
| 1532 | |
| 1533 | uint64_t serial; |
| 1534 | EXPECT_TRUE(BN_get_u64(ser.get(), &serial)); |
| 1535 | EXPECT_EQ(serial, expected_serial); |
| 1536 | } |
| 1537 | |
| 1538 | // Please set self_signed to true for fake certificates or self signed |
| 1539 | // certificates |
| 1540 | void verify_subject(const X509* cert, // |
| 1541 | const string& subject, // |
| 1542 | bool self_signed) { |
| 1543 | char* cert_issuer = // |
| 1544 | X509_NAME_oneline(X509_get_issuer_name(cert), nullptr, 0); |
| 1545 | |
| 1546 | char* cert_subj = X509_NAME_oneline(X509_get_subject_name(cert), nullptr, 0); |
| 1547 | |
| 1548 | string expected_subject("/CN="); |
| 1549 | if (subject.empty()) { |
| 1550 | expected_subject.append("Android Keystore Key"); |
| 1551 | } else { |
| 1552 | expected_subject.append(subject); |
| 1553 | } |
| 1554 | |
| 1555 | EXPECT_STREQ(expected_subject.c_str(), cert_subj) << "Cert has wrong subject." << cert_subj; |
| 1556 | |
| 1557 | if (self_signed) { |
| 1558 | EXPECT_STREQ(cert_issuer, cert_subj) |
| 1559 | << "Cert issuer and subject mismatch for self signed certificate."; |
| 1560 | } |
| 1561 | |
| 1562 | OPENSSL_free(cert_subj); |
| 1563 | OPENSSL_free(cert_issuer); |
| 1564 | } |
| 1565 | |
Shawn Willden | 22fb9c1 | 2022-06-02 14:04:33 -0600 | [diff] [blame] | 1566 | int get_vsr_api_level() { |
Shawn Willden | 35db349 | 2022-06-16 12:50:40 -0600 | [diff] [blame] | 1567 | int vendor_api_level = ::android::base::GetIntProperty("ro.vendor.api_level", -1); |
| 1568 | if (vendor_api_level != -1) { |
| 1569 | return vendor_api_level; |
Shawn Willden | 22fb9c1 | 2022-06-02 14:04:33 -0600 | [diff] [blame] | 1570 | } |
Shawn Willden | 35db349 | 2022-06-16 12:50:40 -0600 | [diff] [blame] | 1571 | |
| 1572 | // Android S and older devices do not define ro.vendor.api_level |
| 1573 | vendor_api_level = ::android::base::GetIntProperty("ro.board.api_level", -1); |
| 1574 | if (vendor_api_level == -1) { |
| 1575 | vendor_api_level = ::android::base::GetIntProperty("ro.board.first_api_level", -1); |
Shawn Willden | 22fb9c1 | 2022-06-02 14:04:33 -0600 | [diff] [blame] | 1576 | } |
Shawn Willden | 35db349 | 2022-06-16 12:50:40 -0600 | [diff] [blame] | 1577 | |
| 1578 | int product_api_level = ::android::base::GetIntProperty("ro.product.first_api_level", -1); |
| 1579 | if (product_api_level == -1) { |
| 1580 | product_api_level = ::android::base::GetIntProperty("ro.build.version.sdk", -1); |
| 1581 | EXPECT_NE(product_api_level, -1) << "Could not find ro.build.version.sdk"; |
Shawn Willden | 22fb9c1 | 2022-06-02 14:04:33 -0600 | [diff] [blame] | 1582 | } |
Shawn Willden | 35db349 | 2022-06-16 12:50:40 -0600 | [diff] [blame] | 1583 | |
| 1584 | // VSR API level is the minimum of vendor_api_level and product_api_level. |
| 1585 | if (vendor_api_level == -1 || vendor_api_level > product_api_level) { |
| 1586 | return product_api_level; |
Shawn Willden | 22fb9c1 | 2022-06-02 14:04:33 -0600 | [diff] [blame] | 1587 | } |
Shawn Willden | 35db349 | 2022-06-16 12:50:40 -0600 | [diff] [blame] | 1588 | return vendor_api_level; |
Shawn Willden | 22fb9c1 | 2022-06-02 14:04:33 -0600 | [diff] [blame] | 1589 | } |
| 1590 | |
David Drysdale | 555ba00 | 2022-05-03 18:48:57 +0100 | [diff] [blame] | 1591 | bool is_gsi_image() { |
| 1592 | std::ifstream ifs("/system/system_ext/etc/init/init.gsi.rc"); |
| 1593 | return ifs.good(); |
| 1594 | } |
| 1595 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1596 | vector<uint8_t> build_serial_blob(const uint64_t serial_int) { |
| 1597 | BIGNUM_Ptr serial(BN_new()); |
| 1598 | EXPECT_TRUE(BN_set_u64(serial.get(), serial_int)); |
| 1599 | |
| 1600 | int len = BN_num_bytes(serial.get()); |
| 1601 | vector<uint8_t> serial_blob(len); |
| 1602 | if (BN_bn2bin(serial.get(), serial_blob.data()) != len) { |
| 1603 | return {}; |
| 1604 | } |
| 1605 | |
David Drysdale | db0dcf5 | 2021-05-18 11:43:31 +0100 | [diff] [blame] | 1606 | if (serial_blob.empty() || serial_blob[0] & 0x80) { |
| 1607 | // An empty blob is OpenSSL's encoding of the zero value; we need single zero byte. |
| 1608 | // Top bit being set indicates a negative number in two's complement, but our input |
| 1609 | // was positive. |
| 1610 | // In either case, prepend a zero byte. |
| 1611 | serial_blob.insert(serial_blob.begin(), 0x00); |
| 1612 | } |
| 1613 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 1614 | return serial_blob; |
| 1615 | } |
| 1616 | |
| 1617 | void verify_subject_and_serial(const Certificate& certificate, // |
| 1618 | const uint64_t expected_serial, // |
| 1619 | const string& subject, bool self_signed) { |
| 1620 | X509_Ptr cert(parse_cert_blob(certificate.encodedCertificate)); |
| 1621 | ASSERT_TRUE(!!cert.get()); |
| 1622 | |
| 1623 | verify_serial(cert.get(), expected_serial); |
| 1624 | verify_subject(cert.get(), subject, self_signed); |
| 1625 | } |
| 1626 | |
Shawn Willden | 4315e13 | 2022-03-20 12:49:46 -0600 | [diff] [blame] | 1627 | void verify_root_of_trust(const vector<uint8_t>& verified_boot_key, bool device_locked, |
| 1628 | VerifiedBoot verified_boot_state, |
| 1629 | const vector<uint8_t>& verified_boot_hash) { |
| 1630 | char property_value[PROPERTY_VALUE_MAX] = {}; |
| 1631 | |
| 1632 | if (avb_verification_enabled()) { |
| 1633 | EXPECT_NE(property_get("ro.boot.vbmeta.digest", property_value, ""), 0); |
| 1634 | string prop_string(property_value); |
| 1635 | EXPECT_EQ(prop_string.size(), 64); |
| 1636 | EXPECT_EQ(prop_string, bin2hex(verified_boot_hash)); |
| 1637 | |
| 1638 | EXPECT_NE(property_get("ro.boot.vbmeta.device_state", property_value, ""), 0); |
| 1639 | if (!strcmp(property_value, "unlocked")) { |
| 1640 | EXPECT_FALSE(device_locked); |
| 1641 | } else { |
| 1642 | EXPECT_TRUE(device_locked); |
| 1643 | } |
| 1644 | |
| 1645 | // Check that the device is locked if not debuggable, e.g., user build |
| 1646 | // images in CTS. For VTS, debuggable images are used to allow adb root |
| 1647 | // and the device is unlocked. |
| 1648 | if (!property_get_bool("ro.debuggable", false)) { |
| 1649 | EXPECT_TRUE(device_locked); |
| 1650 | } else { |
| 1651 | EXPECT_FALSE(device_locked); |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | // Verified boot key should be all 0's if the boot state is not verified or self signed |
| 1656 | std::string empty_boot_key(32, '\0'); |
| 1657 | std::string verified_boot_key_str((const char*)verified_boot_key.data(), |
| 1658 | verified_boot_key.size()); |
| 1659 | EXPECT_NE(property_get("ro.boot.verifiedbootstate", property_value, ""), 0); |
| 1660 | if (!strcmp(property_value, "green")) { |
| 1661 | EXPECT_EQ(verified_boot_state, VerifiedBoot::VERIFIED); |
| 1662 | EXPECT_NE(0, memcmp(verified_boot_key.data(), empty_boot_key.data(), |
| 1663 | verified_boot_key.size())); |
| 1664 | } else if (!strcmp(property_value, "yellow")) { |
| 1665 | EXPECT_EQ(verified_boot_state, VerifiedBoot::SELF_SIGNED); |
| 1666 | EXPECT_NE(0, memcmp(verified_boot_key.data(), empty_boot_key.data(), |
| 1667 | verified_boot_key.size())); |
| 1668 | } else if (!strcmp(property_value, "orange")) { |
| 1669 | EXPECT_EQ(verified_boot_state, VerifiedBoot::UNVERIFIED); |
| 1670 | EXPECT_EQ(0, memcmp(verified_boot_key.data(), empty_boot_key.data(), |
| 1671 | verified_boot_key.size())); |
| 1672 | } else if (!strcmp(property_value, "red")) { |
| 1673 | EXPECT_EQ(verified_boot_state, VerifiedBoot::FAILED); |
| 1674 | } else { |
| 1675 | EXPECT_EQ(verified_boot_state, VerifiedBoot::UNVERIFIED); |
| 1676 | EXPECT_EQ(0, memcmp(verified_boot_key.data(), empty_boot_key.data(), |
| 1677 | verified_boot_key.size())); |
| 1678 | } |
| 1679 | } |
| 1680 | |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 1681 | bool verify_attestation_record(int32_t aidl_version, // |
| 1682 | const string& challenge, // |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1683 | const string& app_id, // |
| 1684 | AuthorizationSet expected_sw_enforced, // |
| 1685 | AuthorizationSet expected_hw_enforced, // |
| 1686 | SecurityLevel security_level, |
David Drysdale | 565ccc7 | 2021-10-11 12:49:50 +0100 | [diff] [blame] | 1687 | const vector<uint8_t>& attestation_cert, |
| 1688 | vector<uint8_t>* unique_id) { |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1689 | X509_Ptr cert(parse_cert_blob(attestation_cert)); |
| 1690 | EXPECT_TRUE(!!cert.get()); |
| 1691 | if (!cert.get()) return false; |
| 1692 | |
| 1693 | ASN1_OCTET_STRING* attest_rec = get_attestation_record(cert.get()); |
| 1694 | EXPECT_TRUE(!!attest_rec); |
| 1695 | if (!attest_rec) return false; |
| 1696 | |
| 1697 | AuthorizationSet att_sw_enforced; |
| 1698 | AuthorizationSet att_hw_enforced; |
| 1699 | uint32_t att_attestation_version; |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1700 | uint32_t att_keymint_version; |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1701 | SecurityLevel att_attestation_security_level; |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1702 | SecurityLevel att_keymint_security_level; |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1703 | vector<uint8_t> att_challenge; |
| 1704 | vector<uint8_t> att_unique_id; |
| 1705 | vector<uint8_t> att_app_id; |
| 1706 | |
| 1707 | auto error = parse_attestation_record(attest_rec->data, // |
| 1708 | attest_rec->length, // |
| 1709 | &att_attestation_version, // |
| 1710 | &att_attestation_security_level, // |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1711 | &att_keymint_version, // |
| 1712 | &att_keymint_security_level, // |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1713 | &att_challenge, // |
| 1714 | &att_sw_enforced, // |
| 1715 | &att_hw_enforced, // |
| 1716 | &att_unique_id); |
| 1717 | EXPECT_EQ(ErrorCode::OK, error); |
| 1718 | if (error != ErrorCode::OK) return false; |
| 1719 | |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 1720 | check_attestation_version(att_attestation_version, aidl_version); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1721 | vector<uint8_t> appId(app_id.begin(), app_id.end()); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1722 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1723 | // check challenge and app id only if we expects a non-fake certificate |
| 1724 | if (challenge.length() > 0) { |
| 1725 | EXPECT_EQ(challenge.length(), att_challenge.size()); |
| 1726 | EXPECT_EQ(0, memcmp(challenge.data(), att_challenge.data(), challenge.length())); |
| 1727 | |
| 1728 | expected_sw_enforced.push_back(TAG_ATTESTATION_APPLICATION_ID, appId); |
| 1729 | } |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1730 | |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 1731 | check_attestation_version(att_keymint_version, aidl_version); |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1732 | EXPECT_EQ(security_level, att_keymint_security_level); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1733 | EXPECT_EQ(security_level, att_attestation_security_level); |
| 1734 | |
Tri Vo | b21e6df | 2023-02-17 14:55:43 -0800 | [diff] [blame] | 1735 | for (int i = 0; i < att_hw_enforced.size(); i++) { |
| 1736 | if (att_hw_enforced[i].tag == TAG_BOOT_PATCHLEVEL || |
| 1737 | att_hw_enforced[i].tag == TAG_VENDOR_PATCHLEVEL) { |
| 1738 | std::string date = |
| 1739 | std::to_string(att_hw_enforced[i].value.get<KeyParameterValue::integer>()); |
David Drysdale | 168228a | 2021-10-05 08:43:52 +0100 | [diff] [blame] | 1740 | |
Tri Vo | b21e6df | 2023-02-17 14:55:43 -0800 | [diff] [blame] | 1741 | // strptime seems to require delimiters, but the tag value will |
| 1742 | // be YYYYMMDD |
| 1743 | if (date.size() != 8) { |
| 1744 | ADD_FAILURE() << "Tag " << att_hw_enforced[i].tag |
| 1745 | << " with invalid format (not YYYYMMDD): " << date; |
| 1746 | return false; |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1747 | } |
Tri Vo | b21e6df | 2023-02-17 14:55:43 -0800 | [diff] [blame] | 1748 | date.insert(6, "-"); |
| 1749 | date.insert(4, "-"); |
| 1750 | struct tm time; |
| 1751 | strptime(date.c_str(), "%Y-%m-%d", &time); |
| 1752 | |
| 1753 | // Day of the month (0-31) |
| 1754 | EXPECT_GE(time.tm_mday, 0); |
| 1755 | EXPECT_LT(time.tm_mday, 32); |
| 1756 | // Months since Jan (0-11) |
| 1757 | EXPECT_GE(time.tm_mon, 0); |
| 1758 | EXPECT_LT(time.tm_mon, 12); |
| 1759 | // Years since 1900 |
| 1760 | EXPECT_GT(time.tm_year, 110); |
| 1761 | EXPECT_LT(time.tm_year, 200); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1762 | } |
| 1763 | } |
| 1764 | |
| 1765 | // Check to make sure boolean values are properly encoded. Presence of a boolean tag |
| 1766 | // indicates true. A provided boolean tag that can be pulled back out of the certificate |
| 1767 | // indicates correct encoding. No need to check if it's in both lists, since the |
| 1768 | // AuthorizationSet compare below will handle mismatches of tags. |
| 1769 | if (security_level == SecurityLevel::SOFTWARE) { |
| 1770 | EXPECT_TRUE(expected_sw_enforced.Contains(TAG_NO_AUTH_REQUIRED)); |
| 1771 | } else { |
| 1772 | EXPECT_TRUE(expected_hw_enforced.Contains(TAG_NO_AUTH_REQUIRED)); |
| 1773 | } |
| 1774 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1775 | if (att_hw_enforced.Contains(TAG_ALGORITHM, Algorithm::EC)) { |
| 1776 | // For ECDSA keys, either an EC_CURVE or a KEY_SIZE can be specified, but one must be. |
| 1777 | EXPECT_TRUE(att_hw_enforced.Contains(TAG_EC_CURVE) || |
| 1778 | att_hw_enforced.Contains(TAG_KEY_SIZE)); |
| 1779 | } |
| 1780 | |
| 1781 | // Test root of trust elements |
| 1782 | vector<uint8_t> verified_boot_key; |
| 1783 | VerifiedBoot verified_boot_state; |
| 1784 | bool device_locked; |
| 1785 | vector<uint8_t> verified_boot_hash; |
| 1786 | error = parse_root_of_trust(attest_rec->data, attest_rec->length, &verified_boot_key, |
| 1787 | &verified_boot_state, &device_locked, &verified_boot_hash); |
| 1788 | EXPECT_EQ(ErrorCode::OK, error); |
Shawn Willden | 4315e13 | 2022-03-20 12:49:46 -0600 | [diff] [blame] | 1789 | verify_root_of_trust(verified_boot_key, device_locked, verified_boot_state, verified_boot_hash); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1790 | |
| 1791 | att_sw_enforced.Sort(); |
| 1792 | expected_sw_enforced.Sort(); |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 1793 | EXPECT_EQ(filtered_tags(expected_sw_enforced), filtered_tags(att_sw_enforced)); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1794 | |
| 1795 | att_hw_enforced.Sort(); |
| 1796 | expected_hw_enforced.Sort(); |
| 1797 | EXPECT_EQ(filtered_tags(expected_hw_enforced), filtered_tags(att_hw_enforced)); |
| 1798 | |
David Drysdale | 565ccc7 | 2021-10-11 12:49:50 +0100 | [diff] [blame] | 1799 | if (unique_id != nullptr) { |
| 1800 | *unique_id = att_unique_id; |
| 1801 | } |
| 1802 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1803 | return true; |
| 1804 | } |
| 1805 | |
| 1806 | string bin2hex(const vector<uint8_t>& data) { |
| 1807 | string retval; |
| 1808 | retval.reserve(data.size() * 2 + 1); |
| 1809 | for (uint8_t byte : data) { |
| 1810 | retval.push_back(nibble2hex[0x0F & (byte >> 4)]); |
| 1811 | retval.push_back(nibble2hex[0x0F & byte]); |
| 1812 | } |
| 1813 | return retval; |
| 1814 | } |
| 1815 | |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 1816 | AuthorizationSet HwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics) { |
| 1817 | AuthorizationSet authList; |
| 1818 | for (auto& entry : key_characteristics) { |
| 1819 | if (entry.securityLevel == SecurityLevel::STRONGBOX || |
| 1820 | entry.securityLevel == SecurityLevel::TRUSTED_ENVIRONMENT) { |
| 1821 | authList.push_back(AuthorizationSet(entry.authorizations)); |
| 1822 | } |
| 1823 | } |
| 1824 | return authList; |
| 1825 | } |
| 1826 | |
| 1827 | AuthorizationSet SwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics) { |
| 1828 | AuthorizationSet authList; |
| 1829 | for (auto& entry : key_characteristics) { |
| 1830 | if (entry.securityLevel == SecurityLevel::SOFTWARE || |
| 1831 | entry.securityLevel == SecurityLevel::KEYSTORE) { |
| 1832 | authList.push_back(AuthorizationSet(entry.authorizations)); |
| 1833 | } |
| 1834 | } |
| 1835 | return authList; |
| 1836 | } |
| 1837 | |
Eran Messeri | 03d7a1a | 2021-07-06 12:07:57 +0100 | [diff] [blame] | 1838 | AssertionResult ChainSignaturesAreValid(const vector<Certificate>& chain, |
| 1839 | bool strict_issuer_check) { |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1840 | std::stringstream cert_data; |
| 1841 | |
| 1842 | for (size_t i = 0; i < chain.size(); ++i) { |
| 1843 | cert_data << bin2hex(chain[i].encodedCertificate) << std::endl; |
| 1844 | |
| 1845 | X509_Ptr key_cert(parse_cert_blob(chain[i].encodedCertificate)); |
| 1846 | X509_Ptr signing_cert; |
| 1847 | if (i < chain.size() - 1) { |
| 1848 | signing_cert = parse_cert_blob(chain[i + 1].encodedCertificate); |
| 1849 | } else { |
| 1850 | signing_cert = parse_cert_blob(chain[i].encodedCertificate); |
| 1851 | } |
| 1852 | if (!key_cert.get() || !signing_cert.get()) return AssertionFailure() << cert_data.str(); |
| 1853 | |
| 1854 | EVP_PKEY_Ptr signing_pubkey(X509_get_pubkey(signing_cert.get())); |
| 1855 | if (!signing_pubkey.get()) return AssertionFailure() << cert_data.str(); |
| 1856 | |
| 1857 | if (!X509_verify(key_cert.get(), signing_pubkey.get())) { |
| 1858 | return AssertionFailure() |
| 1859 | << "Verification of certificate " << i << " failed " |
| 1860 | << "OpenSSL error string: " << ERR_error_string(ERR_get_error(), NULL) << '\n' |
| 1861 | << cert_data.str(); |
| 1862 | } |
| 1863 | |
| 1864 | string cert_issuer = x509NameToStr(X509_get_issuer_name(key_cert.get())); |
| 1865 | string signer_subj = x509NameToStr(X509_get_subject_name(signing_cert.get())); |
Eran Messeri | 03d7a1a | 2021-07-06 12:07:57 +0100 | [diff] [blame] | 1866 | if (cert_issuer != signer_subj && strict_issuer_check) { |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 1867 | return AssertionFailure() << "Cert " << i << " has wrong issuer.\n" |
| 1868 | << " Signer subject is " << signer_subj |
| 1869 | << " Issuer subject is " << cert_issuer << endl |
| 1870 | << cert_data.str(); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1871 | } |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | if (KeyMintAidlTestBase::dump_Attestations) std::cout << cert_data.str(); |
| 1875 | return AssertionSuccess(); |
| 1876 | } |
| 1877 | |
| 1878 | X509_Ptr parse_cert_blob(const vector<uint8_t>& blob) { |
| 1879 | const uint8_t* p = blob.data(); |
| 1880 | return X509_Ptr(d2i_X509(nullptr /* allocate new */, &p, blob.size())); |
| 1881 | } |
| 1882 | |
Tri Vo | ec50ee1 | 2023-02-14 16:29:53 -0800 | [diff] [blame] | 1883 | // Extract attestation record from cert. Returned object is still part of cert; don't free it |
| 1884 | // separately. |
| 1885 | ASN1_OCTET_STRING* get_attestation_record(X509* certificate) { |
| 1886 | ASN1_OBJECT_Ptr oid(OBJ_txt2obj(kAttestionRecordOid, 1 /* dotted string format */)); |
| 1887 | EXPECT_TRUE(!!oid.get()); |
| 1888 | if (!oid.get()) return nullptr; |
| 1889 | |
| 1890 | int location = X509_get_ext_by_OBJ(certificate, oid.get(), -1 /* search from beginning */); |
| 1891 | EXPECT_NE(-1, location) << "Attestation extension not found in certificate"; |
| 1892 | if (location == -1) return nullptr; |
| 1893 | |
| 1894 | X509_EXTENSION* attest_rec_ext = X509_get_ext(certificate, location); |
| 1895 | EXPECT_TRUE(!!attest_rec_ext) |
| 1896 | << "Found attestation extension but couldn't retrieve it? Probably a BoringSSL bug."; |
| 1897 | if (!attest_rec_ext) return nullptr; |
| 1898 | |
| 1899 | ASN1_OCTET_STRING* attest_rec = X509_EXTENSION_get_data(attest_rec_ext); |
| 1900 | EXPECT_TRUE(!!attest_rec) << "Attestation extension contained no data"; |
| 1901 | return attest_rec; |
| 1902 | } |
| 1903 | |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 1904 | vector<uint8_t> make_name_from_str(const string& name) { |
| 1905 | X509_NAME_Ptr x509_name(X509_NAME_new()); |
| 1906 | EXPECT_TRUE(x509_name.get() != nullptr); |
| 1907 | if (!x509_name) return {}; |
| 1908 | |
| 1909 | EXPECT_EQ(1, X509_NAME_add_entry_by_txt(x509_name.get(), // |
| 1910 | "CN", // |
| 1911 | MBSTRING_ASC, |
| 1912 | reinterpret_cast<const uint8_t*>(name.c_str()), |
| 1913 | -1, // len |
| 1914 | -1, // loc |
| 1915 | 0 /* set */)); |
| 1916 | |
| 1917 | int len = i2d_X509_NAME(x509_name.get(), nullptr /* only return length */); |
| 1918 | EXPECT_GT(len, 0); |
| 1919 | |
| 1920 | vector<uint8_t> retval(len); |
| 1921 | uint8_t* p = retval.data(); |
| 1922 | i2d_X509_NAME(x509_name.get(), &p); |
| 1923 | |
| 1924 | return retval; |
| 1925 | } |
| 1926 | |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 1927 | namespace { |
| 1928 | |
| 1929 | void check_cose_key(const vector<uint8_t>& data, bool testMode) { |
| 1930 | auto [parsedPayload, __, payloadParseErr] = cppbor::parse(data); |
| 1931 | ASSERT_TRUE(parsedPayload) << "Key parse failed: " << payloadParseErr; |
| 1932 | |
| 1933 | // The following check assumes that canonical CBOR encoding is used for the COSE_Key. |
| 1934 | if (testMode) { |
Elliott Hughes | be36da4 | 2022-11-09 21:35:07 +0000 | [diff] [blame] | 1935 | EXPECT_THAT( |
| 1936 | cppbor::prettyPrint(parsedPayload.get()), |
| 1937 | MatchesRegex("\\{\n" |
| 1938 | " 1 : 2,\n" // kty: EC2 |
| 1939 | " 3 : -7,\n" // alg: ES256 |
| 1940 | " -1 : 1,\n" // EC id: P256 |
| 1941 | // The regex {(0x[0-9a-f]{2}, ){31}0x[0-9a-f]{2}} matches a |
| 1942 | // sequence of 32 hexadecimal bytes, enclosed in braces and |
| 1943 | // separated by commas. In this case, some Ed25519 public key. |
| 1944 | " -2 : \\{(0x[0-9a-f]{2}, ){31}0x[0-9a-f]{2}\\},\n" // pub_x: data |
| 1945 | " -3 : \\{(0x[0-9a-f]{2}, ){31}0x[0-9a-f]{2}\\},\n" // pub_y: data |
| 1946 | " -70000 : null,\n" // test marker |
| 1947 | "\\}")); |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 1948 | } else { |
Elliott Hughes | be36da4 | 2022-11-09 21:35:07 +0000 | [diff] [blame] | 1949 | EXPECT_THAT( |
| 1950 | cppbor::prettyPrint(parsedPayload.get()), |
| 1951 | MatchesRegex("\\{\n" |
| 1952 | " 1 : 2,\n" // kty: EC2 |
| 1953 | " 3 : -7,\n" // alg: ES256 |
| 1954 | " -1 : 1,\n" // EC id: P256 |
| 1955 | // The regex {(0x[0-9a-f]{2}, ){31}0x[0-9a-f]{2}} matches a |
| 1956 | // sequence of 32 hexadecimal bytes, enclosed in braces and |
| 1957 | // separated by commas. In this case, some Ed25519 public key. |
| 1958 | " -2 : \\{(0x[0-9a-f]{2}, ){31}0x[0-9a-f]{2}\\},\n" // pub_x: data |
| 1959 | " -3 : \\{(0x[0-9a-f]{2}, ){31}0x[0-9a-f]{2}\\},\n" // pub_y: data |
| 1960 | "\\}")); |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | } // namespace |
| 1965 | |
| 1966 | void check_maced_pubkey(const MacedPublicKey& macedPubKey, bool testMode, |
| 1967 | vector<uint8_t>* payload_value) { |
| 1968 | auto [coseMac0, _, mac0ParseErr] = cppbor::parse(macedPubKey.macedKey); |
| 1969 | ASSERT_TRUE(coseMac0) << "COSE Mac0 parse failed " << mac0ParseErr; |
| 1970 | |
| 1971 | ASSERT_NE(coseMac0->asArray(), nullptr); |
| 1972 | ASSERT_EQ(coseMac0->asArray()->size(), kCoseMac0EntryCount); |
| 1973 | |
| 1974 | auto protParms = coseMac0->asArray()->get(kCoseMac0ProtectedParams)->asBstr(); |
| 1975 | ASSERT_NE(protParms, nullptr); |
| 1976 | |
| 1977 | // Header label:value of 'alg': HMAC-256 |
| 1978 | ASSERT_EQ(cppbor::prettyPrint(protParms->value()), "{\n 1 : 5,\n}"); |
| 1979 | |
| 1980 | auto unprotParms = coseMac0->asArray()->get(kCoseMac0UnprotectedParams)->asMap(); |
| 1981 | ASSERT_NE(unprotParms, nullptr); |
| 1982 | ASSERT_EQ(unprotParms->size(), 0); |
| 1983 | |
| 1984 | // The payload is a bstr holding an encoded COSE_Key |
| 1985 | auto payload = coseMac0->asArray()->get(kCoseMac0Payload)->asBstr(); |
| 1986 | ASSERT_NE(payload, nullptr); |
| 1987 | check_cose_key(payload->value(), testMode); |
| 1988 | |
| 1989 | auto coseMac0Tag = coseMac0->asArray()->get(kCoseMac0Tag)->asBstr(); |
| 1990 | ASSERT_TRUE(coseMac0Tag); |
| 1991 | auto extractedTag = coseMac0Tag->value(); |
| 1992 | EXPECT_EQ(extractedTag.size(), 32U); |
| 1993 | |
| 1994 | // Compare with tag generated with kTestMacKey. Should only match in test mode |
Seth Moore | 026bb74 | 2021-04-30 11:41:18 -0700 | [diff] [blame] | 1995 | auto macFunction = [](const cppcose::bytevec& input) { |
| 1996 | return cppcose::generateHmacSha256(remote_prov::kTestMacKey, input); |
| 1997 | }; |
| 1998 | auto testTag = |
| 1999 | cppcose::generateCoseMac0Mac(macFunction, {} /* external_aad */, payload->value()); |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 2000 | ASSERT_TRUE(testTag) << "Tag calculation failed: " << testTag.message(); |
| 2001 | |
| 2002 | if (testMode) { |
Seth Moore | 026bb74 | 2021-04-30 11:41:18 -0700 | [diff] [blame] | 2003 | EXPECT_THAT(*testTag, ElementsAreArray(extractedTag)); |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 2004 | } else { |
Seth Moore | 026bb74 | 2021-04-30 11:41:18 -0700 | [diff] [blame] | 2005 | EXPECT_THAT(*testTag, Not(ElementsAreArray(extractedTag))); |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 2006 | } |
| 2007 | if (payload_value != nullptr) { |
| 2008 | *payload_value = payload->value(); |
| 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | void p256_pub_key(const vector<uint8_t>& coseKeyData, EVP_PKEY_Ptr* signingKey) { |
| 2013 | // Extract x and y affine coordinates from the encoded Cose_Key. |
| 2014 | auto [parsedPayload, __, payloadParseErr] = cppbor::parse(coseKeyData); |
| 2015 | ASSERT_TRUE(parsedPayload) << "Key parse failed: " << payloadParseErr; |
| 2016 | auto coseKey = parsedPayload->asMap(); |
| 2017 | const std::unique_ptr<cppbor::Item>& xItem = coseKey->get(cppcose::CoseKey::PUBKEY_X); |
| 2018 | ASSERT_NE(xItem->asBstr(), nullptr); |
| 2019 | vector<uint8_t> x = xItem->asBstr()->value(); |
| 2020 | const std::unique_ptr<cppbor::Item>& yItem = coseKey->get(cppcose::CoseKey::PUBKEY_Y); |
| 2021 | ASSERT_NE(yItem->asBstr(), nullptr); |
| 2022 | vector<uint8_t> y = yItem->asBstr()->value(); |
| 2023 | |
| 2024 | // Concatenate: 0x04 (uncompressed form marker) | x | y |
| 2025 | vector<uint8_t> pubKeyData{0x04}; |
| 2026 | pubKeyData.insert(pubKeyData.end(), x.begin(), x.end()); |
| 2027 | pubKeyData.insert(pubKeyData.end(), y.begin(), y.end()); |
| 2028 | |
| 2029 | EC_KEY_Ptr ecKey = EC_KEY_Ptr(EC_KEY_new()); |
| 2030 | ASSERT_NE(ecKey, nullptr); |
| 2031 | EC_GROUP_Ptr group = EC_GROUP_Ptr(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1)); |
| 2032 | ASSERT_NE(group, nullptr); |
| 2033 | ASSERT_EQ(EC_KEY_set_group(ecKey.get(), group.get()), 1); |
| 2034 | EC_POINT_Ptr point = EC_POINT_Ptr(EC_POINT_new(group.get())); |
| 2035 | ASSERT_NE(point, nullptr); |
| 2036 | ASSERT_EQ(EC_POINT_oct2point(group.get(), point.get(), pubKeyData.data(), pubKeyData.size(), |
| 2037 | nullptr), |
| 2038 | 1); |
| 2039 | ASSERT_EQ(EC_KEY_set_public_key(ecKey.get(), point.get()), 1); |
| 2040 | |
| 2041 | EVP_PKEY_Ptr pubKey = EVP_PKEY_Ptr(EVP_PKEY_new()); |
| 2042 | ASSERT_NE(pubKey, nullptr); |
| 2043 | EVP_PKEY_assign_EC_KEY(pubKey.get(), ecKey.release()); |
| 2044 | *signingKey = std::move(pubKey); |
| 2045 | } |
| 2046 | |
Max Bires | a97ec69 | 2022-11-21 23:37:54 -0800 | [diff] [blame] | 2047 | void device_id_attestation_vsr_check(const ErrorCode& result) { |
Shawn Willden | 1a545db | 2023-02-22 14:32:33 -0700 | [diff] [blame] | 2048 | if (get_vsr_api_level() > __ANDROID_API_T__) { |
Max Bires | a97ec69 | 2022-11-21 23:37:54 -0800 | [diff] [blame] | 2049 | ASSERT_FALSE(result == ErrorCode::INVALID_TAG) |
| 2050 | << "It is a specification violation for INVALID_TAG to be returned due to ID " |
| 2051 | << "mismatch in a Device ID Attestation call. INVALID_TAG is only intended to " |
| 2052 | << "be used for a case where updateAad() is called after update(). As of " |
| 2053 | << "VSR-14, this is now enforced as an error."; |
| 2054 | } |
| 2055 | } |
| 2056 | |
David Drysdale | 3d2ba0a | 2023-01-11 13:27:26 +0000 | [diff] [blame] | 2057 | // Check whether the given named feature is available. |
| 2058 | bool check_feature(const std::string& name) { |
| 2059 | ::android::sp<::android::IServiceManager> sm(::android::defaultServiceManager()); |
Tommy Chiu | 6e5736b | 2023-02-08 10:16:03 +0800 | [diff] [blame] | 2060 | ::android::sp<::android::IBinder> binder( |
| 2061 | sm->waitForService(::android::String16("package_native"))); |
David Drysdale | 3d2ba0a | 2023-01-11 13:27:26 +0000 | [diff] [blame] | 2062 | if (binder == nullptr) { |
Tommy Chiu | 6e5736b | 2023-02-08 10:16:03 +0800 | [diff] [blame] | 2063 | GTEST_LOG_(ERROR) << "waitForService package_native failed"; |
David Drysdale | 3d2ba0a | 2023-01-11 13:27:26 +0000 | [diff] [blame] | 2064 | return false; |
| 2065 | } |
| 2066 | ::android::sp<::android::content::pm::IPackageManagerNative> packageMgr = |
| 2067 | ::android::interface_cast<::android::content::pm::IPackageManagerNative>(binder); |
| 2068 | if (packageMgr == nullptr) { |
| 2069 | GTEST_LOG_(ERROR) << "Cannot find package manager"; |
| 2070 | return false; |
| 2071 | } |
| 2072 | bool hasFeature = false; |
| 2073 | auto status = packageMgr->hasSystemFeature(::android::String16(name.c_str()), 0, &hasFeature); |
| 2074 | if (!status.isOk()) { |
| 2075 | GTEST_LOG_(ERROR) << "hasSystemFeature('" << name << "') failed: " << status; |
| 2076 | return false; |
| 2077 | } |
| 2078 | return hasFeature; |
| 2079 | } |
| 2080 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 2081 | } // namespace test |
Shawn Willden | 08a7e43 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 2082 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 2083 | } // namespace aidl::android::hardware::security::keymint |