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