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