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> |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 28 | #include <openssl/mem.h> |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 29 | #include <remote_prov/remote_prov_utils.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 30 | |
Max Bires | 9704ff6 | 2021-04-07 11:12:01 -0700 | [diff] [blame] | 31 | #include <keymaster/cppcose/cppcose.h> |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 32 | #include <keymint_support/attestation_record.h> |
Shawn Willden | 08a7e43 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 33 | #include <keymint_support/key_param_output.h> |
| 34 | #include <keymint_support/keymint_utils.h> |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 35 | #include <keymint_support/openssl_utils.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 36 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 37 | namespace aidl::android::hardware::security::keymint { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 38 | |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 39 | using namespace cppcose; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 40 | using namespace std::literals::chrono_literals; |
| 41 | using std::endl; |
| 42 | using std::optional; |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 43 | using std::unique_ptr; |
| 44 | using ::testing::AssertionFailure; |
| 45 | using ::testing::AssertionResult; |
| 46 | using ::testing::AssertionSuccess; |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 47 | using ::testing::MatchesRegex; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 48 | |
| 49 | ::std::ostream& operator<<(::std::ostream& os, const AuthorizationSet& set) { |
| 50 | if (set.size() == 0) |
| 51 | os << "(Empty)" << ::std::endl; |
| 52 | else { |
| 53 | os << "\n"; |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 54 | for (auto& entry : set) os << entry << ::std::endl; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 55 | } |
| 56 | return os; |
| 57 | } |
| 58 | |
| 59 | namespace test { |
| 60 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 61 | namespace { |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 62 | typedef KeyMintAidlTestBase::KeyData KeyData; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 63 | // Predicate for testing basic characteristics validity in generation or import. |
| 64 | bool KeyCharacteristicsBasicallyValid(SecurityLevel secLevel, |
| 65 | const vector<KeyCharacteristics>& key_characteristics) { |
| 66 | if (key_characteristics.empty()) return false; |
| 67 | |
| 68 | std::unordered_set<SecurityLevel> levels_seen; |
| 69 | for (auto& entry : key_characteristics) { |
| 70 | if (entry.authorizations.empty()) return false; |
| 71 | |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 72 | // Just ignore the SecurityLevel::KEYSTORE as the KM won't do any enforcement on this. |
| 73 | if (entry.securityLevel == SecurityLevel::KEYSTORE) continue; |
| 74 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 75 | if (levels_seen.find(entry.securityLevel) != levels_seen.end()) return false; |
| 76 | levels_seen.insert(entry.securityLevel); |
| 77 | |
| 78 | // Generally, we should only have one entry, at the same security level as the KM |
| 79 | // instance. There is an exception: StrongBox KM can have some authorizations that are |
| 80 | // enforced by the TEE. |
| 81 | bool isExpectedSecurityLevel = secLevel == entry.securityLevel || |
| 82 | (secLevel == SecurityLevel::STRONGBOX && |
| 83 | entry.securityLevel == SecurityLevel::TRUSTED_ENVIRONMENT); |
| 84 | |
| 85 | if (!isExpectedSecurityLevel) return false; |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 90 | // Extract attestation record from cert. Returned object is still part of cert; don't free it |
| 91 | // separately. |
| 92 | ASN1_OCTET_STRING* get_attestation_record(X509* certificate) { |
| 93 | ASN1_OBJECT_Ptr oid(OBJ_txt2obj(kAttestionRecordOid, 1 /* dotted string format */)); |
| 94 | EXPECT_TRUE(!!oid.get()); |
| 95 | if (!oid.get()) return nullptr; |
| 96 | |
| 97 | int location = X509_get_ext_by_OBJ(certificate, oid.get(), -1 /* search from beginning */); |
| 98 | EXPECT_NE(-1, location) << "Attestation extension not found in certificate"; |
| 99 | if (location == -1) return nullptr; |
| 100 | |
| 101 | X509_EXTENSION* attest_rec_ext = X509_get_ext(certificate, location); |
| 102 | EXPECT_TRUE(!!attest_rec_ext) |
| 103 | << "Found attestation extension but couldn't retrieve it? Probably a BoringSSL bug."; |
| 104 | if (!attest_rec_ext) return nullptr; |
| 105 | |
| 106 | ASN1_OCTET_STRING* attest_rec = X509_EXTENSION_get_data(attest_rec_ext); |
| 107 | EXPECT_TRUE(!!attest_rec) << "Attestation extension contained no data"; |
| 108 | return attest_rec; |
| 109 | } |
| 110 | |
| 111 | bool avb_verification_enabled() { |
| 112 | char value[PROPERTY_VALUE_MAX]; |
| 113 | return property_get("ro.boot.vbmeta.device_state", value, "") != 0; |
| 114 | } |
| 115 | |
| 116 | char nibble2hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', |
| 117 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; |
| 118 | |
| 119 | // Attestations don't contain everything in key authorization lists, so we need to filter the key |
| 120 | // lists to produce the lists that we expect to match the attestations. |
| 121 | auto kTagsToFilter = { |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 122 | Tag::CREATION_DATETIME, // |
| 123 | Tag::EC_CURVE, |
| 124 | Tag::HARDWARE_TYPE, |
| 125 | Tag::INCLUDE_UNIQUE_ID, |
| 126 | }; |
| 127 | |
| 128 | AuthorizationSet filtered_tags(const AuthorizationSet& set) { |
| 129 | AuthorizationSet filtered; |
| 130 | std::remove_copy_if( |
| 131 | set.begin(), set.end(), std::back_inserter(filtered), [](const auto& entry) -> bool { |
| 132 | return std::find(kTagsToFilter.begin(), kTagsToFilter.end(), entry.tag) != |
| 133 | kTagsToFilter.end(); |
| 134 | }); |
| 135 | return filtered; |
| 136 | } |
| 137 | |
| 138 | string x509NameToStr(X509_NAME* name) { |
| 139 | char* s = X509_NAME_oneline(name, nullptr, 0); |
| 140 | string retval(s); |
| 141 | OPENSSL_free(s); |
| 142 | return retval; |
| 143 | } |
| 144 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 145 | } // namespace |
| 146 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 147 | bool KeyMintAidlTestBase::arm_deleteAllKeys = false; |
| 148 | bool KeyMintAidlTestBase::dump_Attestations = false; |
| 149 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 150 | ErrorCode KeyMintAidlTestBase::GetReturnErrorCode(const Status& result) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 151 | if (result.isOk()) return ErrorCode::OK; |
| 152 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 153 | if (result.getExceptionCode() == EX_SERVICE_SPECIFIC) { |
| 154 | return static_cast<ErrorCode>(result.getServiceSpecificError()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | return ErrorCode::UNKNOWN_ERROR; |
| 158 | } |
| 159 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 160 | void KeyMintAidlTestBase::InitializeKeyMint(std::shared_ptr<IKeyMintDevice> keyMint) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 161 | ASSERT_NE(keyMint, nullptr); |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 162 | keymint_ = std::move(keyMint); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 163 | |
| 164 | KeyMintHardwareInfo info; |
| 165 | ASSERT_TRUE(keymint_->getHardwareInfo(&info).isOk()); |
| 166 | |
| 167 | securityLevel_ = info.securityLevel; |
| 168 | name_.assign(info.keyMintName.begin(), info.keyMintName.end()); |
| 169 | author_.assign(info.keyMintAuthorName.begin(), info.keyMintAuthorName.end()); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 170 | timestamp_token_required_ = info.timestampTokenRequired; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 171 | |
| 172 | os_version_ = getOsVersion(); |
| 173 | os_patch_level_ = getOsPatchlevel(); |
David Drysdale | bb3d85e | 2021-04-13 11:15:51 +0100 | [diff] [blame] | 174 | vendor_patch_level_ = getVendorPatchlevel(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void KeyMintAidlTestBase::SetUp() { |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 178 | if (AServiceManager_isDeclared(GetParam().c_str())) { |
| 179 | ::ndk::SpAIBinder binder(AServiceManager_waitForService(GetParam().c_str())); |
| 180 | InitializeKeyMint(IKeyMintDevice::fromBinder(binder)); |
| 181 | } else { |
| 182 | InitializeKeyMint(nullptr); |
| 183 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | ErrorCode KeyMintAidlTestBase::GenerateKey(const AuthorizationSet& key_desc, |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 187 | const optional<AttestationKey>& attest_key, |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 188 | vector<uint8_t>* key_blob, |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 189 | vector<KeyCharacteristics>* key_characteristics, |
| 190 | vector<Certificate>* cert_chain) { |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 191 | EXPECT_NE(key_blob, nullptr) << "Key blob pointer must not be null. Test bug"; |
| 192 | EXPECT_NE(key_characteristics, nullptr) |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 193 | << "Previous characteristics not deleted before generating key. Test bug."; |
| 194 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 195 | KeyCreationResult creationResult; |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 196 | Status result = keymint_->generateKey(key_desc.vector_data(), attest_key, &creationResult); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 197 | if (result.isOk()) { |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 198 | EXPECT_PRED2(KeyCharacteristicsBasicallyValid, SecLevel(), |
| 199 | creationResult.keyCharacteristics); |
| 200 | EXPECT_GT(creationResult.keyBlob.size(), 0); |
| 201 | *key_blob = std::move(creationResult.keyBlob); |
| 202 | *key_characteristics = std::move(creationResult.keyCharacteristics); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 203 | *cert_chain = std::move(creationResult.certificateChain); |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 204 | |
| 205 | auto algorithm = key_desc.GetTagValue(TAG_ALGORITHM); |
| 206 | EXPECT_TRUE(algorithm); |
| 207 | if (algorithm && |
| 208 | (algorithm.value() == Algorithm::RSA || algorithm.value() == Algorithm::EC)) { |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 209 | EXPECT_GE(cert_chain->size(), 1); |
| 210 | if (key_desc.Contains(TAG_ATTESTATION_CHALLENGE)) { |
| 211 | if (attest_key) { |
| 212 | EXPECT_EQ(cert_chain->size(), 1); |
| 213 | } else { |
| 214 | EXPECT_GT(cert_chain->size(), 1); |
| 215 | } |
| 216 | } |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 217 | } else { |
| 218 | // For symmetric keys there should be no certificates. |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 219 | EXPECT_EQ(cert_chain->size(), 0); |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 220 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | return GetReturnErrorCode(result); |
| 224 | } |
| 225 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 226 | ErrorCode KeyMintAidlTestBase::GenerateKey(const AuthorizationSet& key_desc, |
| 227 | const optional<AttestationKey>& attest_key) { |
| 228 | return GenerateKey(key_desc, attest_key, &key_blob_, &key_characteristics_, &cert_chain_); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | ErrorCode KeyMintAidlTestBase::ImportKey(const AuthorizationSet& key_desc, KeyFormat format, |
| 232 | const string& key_material, vector<uint8_t>* key_blob, |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 233 | vector<KeyCharacteristics>* key_characteristics) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 234 | Status result; |
| 235 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 236 | cert_chain_.clear(); |
| 237 | key_characteristics->clear(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 238 | key_blob->clear(); |
| 239 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 240 | KeyCreationResult creationResult; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 241 | result = keymint_->importKey(key_desc.vector_data(), format, |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 242 | vector<uint8_t>(key_material.begin(), key_material.end()), |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 243 | {} /* attestationSigningKeyBlob */, &creationResult); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 244 | |
| 245 | if (result.isOk()) { |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 246 | EXPECT_PRED2(KeyCharacteristicsBasicallyValid, SecLevel(), |
| 247 | creationResult.keyCharacteristics); |
| 248 | EXPECT_GT(creationResult.keyBlob.size(), 0); |
| 249 | |
| 250 | *key_blob = std::move(creationResult.keyBlob); |
| 251 | *key_characteristics = std::move(creationResult.keyCharacteristics); |
| 252 | cert_chain_ = std::move(creationResult.certificateChain); |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 253 | |
| 254 | auto algorithm = key_desc.GetTagValue(TAG_ALGORITHM); |
| 255 | EXPECT_TRUE(algorithm); |
| 256 | if (algorithm && |
| 257 | (algorithm.value() == Algorithm::RSA || algorithm.value() == Algorithm::EC)) { |
| 258 | EXPECT_GE(cert_chain_.size(), 1); |
| 259 | if (key_desc.Contains(TAG_ATTESTATION_CHALLENGE)) EXPECT_GT(cert_chain_.size(), 1); |
| 260 | } else { |
| 261 | // For symmetric keys there should be no certificates. |
| 262 | EXPECT_EQ(cert_chain_.size(), 0); |
| 263 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | return GetReturnErrorCode(result); |
| 267 | } |
| 268 | |
| 269 | ErrorCode KeyMintAidlTestBase::ImportKey(const AuthorizationSet& key_desc, KeyFormat format, |
| 270 | const string& key_material) { |
| 271 | return ImportKey(key_desc, format, key_material, &key_blob_, &key_characteristics_); |
| 272 | } |
| 273 | |
| 274 | ErrorCode KeyMintAidlTestBase::ImportWrappedKey(string wrapped_key, string wrapping_key, |
| 275 | const AuthorizationSet& wrapping_key_desc, |
| 276 | string masking_key, |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 277 | const AuthorizationSet& unwrapping_params, |
| 278 | int64_t password_sid, int64_t biometric_sid) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 279 | EXPECT_EQ(ErrorCode::OK, ImportKey(wrapping_key_desc, KeyFormat::PKCS8, wrapping_key)); |
| 280 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 281 | key_characteristics_.clear(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 282 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 283 | KeyCreationResult creationResult; |
| 284 | Status result = keymint_->importWrappedKey( |
| 285 | vector<uint8_t>(wrapped_key.begin(), wrapped_key.end()), key_blob_, |
| 286 | vector<uint8_t>(masking_key.begin(), masking_key.end()), |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 287 | unwrapping_params.vector_data(), password_sid, biometric_sid, &creationResult); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 288 | |
| 289 | if (result.isOk()) { |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 290 | EXPECT_PRED2(KeyCharacteristicsBasicallyValid, SecLevel(), |
| 291 | creationResult.keyCharacteristics); |
| 292 | EXPECT_GT(creationResult.keyBlob.size(), 0); |
| 293 | |
| 294 | key_blob_ = std::move(creationResult.keyBlob); |
| 295 | key_characteristics_ = std::move(creationResult.keyCharacteristics); |
| 296 | cert_chain_ = std::move(creationResult.certificateChain); |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 297 | |
| 298 | AuthorizationSet allAuths; |
| 299 | for (auto& entry : key_characteristics_) { |
| 300 | allAuths.push_back(AuthorizationSet(entry.authorizations)); |
| 301 | } |
| 302 | auto algorithm = allAuths.GetTagValue(TAG_ALGORITHM); |
| 303 | EXPECT_TRUE(algorithm); |
| 304 | if (algorithm && |
| 305 | (algorithm.value() == Algorithm::RSA || algorithm.value() == Algorithm::EC)) { |
| 306 | EXPECT_GE(cert_chain_.size(), 1); |
| 307 | } else { |
| 308 | // For symmetric keys there should be no certificates. |
| 309 | EXPECT_EQ(cert_chain_.size(), 0); |
| 310 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | return GetReturnErrorCode(result); |
| 314 | } |
| 315 | |
| 316 | ErrorCode KeyMintAidlTestBase::DeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob) { |
| 317 | Status result = keymint_->deleteKey(*key_blob); |
| 318 | if (!keep_key_blob) { |
| 319 | *key_blob = vector<uint8_t>(); |
| 320 | } |
| 321 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 322 | EXPECT_TRUE(result.isOk()) << result.getServiceSpecificError() << endl; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 323 | return GetReturnErrorCode(result); |
| 324 | } |
| 325 | |
| 326 | ErrorCode KeyMintAidlTestBase::DeleteKey(bool keep_key_blob) { |
| 327 | return DeleteKey(&key_blob_, keep_key_blob); |
| 328 | } |
| 329 | |
| 330 | ErrorCode KeyMintAidlTestBase::DeleteAllKeys() { |
| 331 | Status result = keymint_->deleteAllKeys(); |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 332 | EXPECT_TRUE(result.isOk()) << result.getServiceSpecificError() << endl; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 333 | return GetReturnErrorCode(result); |
| 334 | } |
| 335 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 336 | ErrorCode KeyMintAidlTestBase::DestroyAttestationIds() { |
| 337 | Status result = keymint_->destroyAttestationIds(); |
| 338 | return GetReturnErrorCode(result); |
| 339 | } |
| 340 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 341 | void KeyMintAidlTestBase::CheckedDeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob) { |
| 342 | ErrorCode result = DeleteKey(key_blob, keep_key_blob); |
| 343 | EXPECT_TRUE(result == ErrorCode::OK || result == ErrorCode::UNIMPLEMENTED) << result << endl; |
| 344 | } |
| 345 | |
| 346 | void KeyMintAidlTestBase::CheckedDeleteKey() { |
| 347 | CheckedDeleteKey(&key_blob_); |
| 348 | } |
| 349 | |
| 350 | ErrorCode KeyMintAidlTestBase::Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob, |
| 351 | const AuthorizationSet& in_params, |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 352 | AuthorizationSet* out_params, |
| 353 | std::shared_ptr<IKeyMintOperation>& op) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 354 | SCOPED_TRACE("Begin"); |
| 355 | Status result; |
| 356 | BeginResult out; |
David Drysdale | 56ba912 | 2021-04-19 19:10:47 +0100 | [diff] [blame] | 357 | 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] | 358 | |
| 359 | if (result.isOk()) { |
| 360 | *out_params = out.params; |
| 361 | challenge_ = out.challenge; |
| 362 | op = out.operation; |
| 363 | } |
| 364 | |
| 365 | return GetReturnErrorCode(result); |
| 366 | } |
| 367 | |
| 368 | ErrorCode KeyMintAidlTestBase::Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob, |
| 369 | const AuthorizationSet& in_params, |
| 370 | AuthorizationSet* out_params) { |
| 371 | SCOPED_TRACE("Begin"); |
| 372 | Status result; |
| 373 | BeginResult out; |
| 374 | |
David Drysdale | 56ba912 | 2021-04-19 19:10:47 +0100 | [diff] [blame] | 375 | 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] | 376 | |
| 377 | if (result.isOk()) { |
| 378 | *out_params = out.params; |
| 379 | challenge_ = out.challenge; |
| 380 | op_ = out.operation; |
| 381 | } |
| 382 | |
| 383 | return GetReturnErrorCode(result); |
| 384 | } |
| 385 | |
| 386 | ErrorCode KeyMintAidlTestBase::Begin(KeyPurpose purpose, const AuthorizationSet& in_params, |
| 387 | AuthorizationSet* out_params) { |
| 388 | SCOPED_TRACE("Begin"); |
| 389 | EXPECT_EQ(nullptr, op_); |
| 390 | return Begin(purpose, key_blob_, in_params, out_params); |
| 391 | } |
| 392 | |
| 393 | ErrorCode KeyMintAidlTestBase::Begin(KeyPurpose purpose, const AuthorizationSet& in_params) { |
| 394 | SCOPED_TRACE("Begin"); |
| 395 | AuthorizationSet out_params; |
| 396 | ErrorCode result = Begin(purpose, in_params, &out_params); |
| 397 | EXPECT_TRUE(out_params.empty()); |
| 398 | return result; |
| 399 | } |
| 400 | |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 401 | ErrorCode KeyMintAidlTestBase::UpdateAad(const string& input) { |
| 402 | return GetReturnErrorCode(op_->updateAad(vector<uint8_t>(input.begin(), input.end()), |
| 403 | {} /* hardwareAuthToken */, |
| 404 | {} /* verificationToken */)); |
| 405 | } |
| 406 | |
| 407 | ErrorCode KeyMintAidlTestBase::Update(const string& input, string* output) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 408 | SCOPED_TRACE("Update"); |
| 409 | |
| 410 | Status result; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 411 | if (!output) return ErrorCode::UNEXPECTED_NULL_POINTER; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 412 | |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 413 | std::vector<uint8_t> o_put; |
| 414 | result = op_->update(vector<uint8_t>(input.begin(), input.end()), {}, {}, &o_put); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 415 | |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 416 | if (result.isOk()) output->append(o_put.begin(), o_put.end()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 417 | |
| 418 | return GetReturnErrorCode(result); |
| 419 | } |
| 420 | |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 421 | ErrorCode KeyMintAidlTestBase::Finish(const string& input, const string& signature, |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 422 | string* output) { |
| 423 | SCOPED_TRACE("Finish"); |
| 424 | Status result; |
| 425 | |
| 426 | EXPECT_NE(op_, nullptr); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 427 | if (!op_) return ErrorCode::UNEXPECTED_NULL_POINTER; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 428 | |
| 429 | vector<uint8_t> oPut; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 430 | result = op_->finish(vector<uint8_t>(input.begin(), input.end()), |
| 431 | vector<uint8_t>(signature.begin(), signature.end()), {} /* authToken */, |
| 432 | {} /* timestampToken */, {} /* confirmationToken */, &oPut); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 433 | |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 434 | if (result.isOk()) output->append(oPut.begin(), oPut.end()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 435 | |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 436 | op_ = {}; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 437 | return GetReturnErrorCode(result); |
| 438 | } |
| 439 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 440 | ErrorCode KeyMintAidlTestBase::Abort(const std::shared_ptr<IKeyMintOperation>& op) { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 441 | SCOPED_TRACE("Abort"); |
| 442 | |
| 443 | EXPECT_NE(op, nullptr); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 444 | if (!op) return ErrorCode::UNEXPECTED_NULL_POINTER; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 445 | |
| 446 | Status retval = op->abort(); |
| 447 | EXPECT_TRUE(retval.isOk()); |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 448 | return static_cast<ErrorCode>(retval.getServiceSpecificError()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | ErrorCode KeyMintAidlTestBase::Abort() { |
| 452 | SCOPED_TRACE("Abort"); |
| 453 | |
| 454 | EXPECT_NE(op_, nullptr); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 455 | if (!op_) return ErrorCode::UNEXPECTED_NULL_POINTER; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 456 | |
| 457 | Status retval = op_->abort(); |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 458 | return static_cast<ErrorCode>(retval.getServiceSpecificError()); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | void KeyMintAidlTestBase::AbortIfNeeded() { |
| 462 | SCOPED_TRACE("AbortIfNeeded"); |
| 463 | if (op_) { |
| 464 | EXPECT_EQ(ErrorCode::OK, Abort()); |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 465 | op_.reset(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 469 | auto KeyMintAidlTestBase::ProcessMessage(const vector<uint8_t>& key_blob, KeyPurpose operation, |
| 470 | const string& message, const AuthorizationSet& in_params) |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 471 | -> std::tuple<ErrorCode, string> { |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 472 | AuthorizationSet begin_out_params; |
| 473 | ErrorCode result = Begin(operation, key_blob, in_params, &begin_out_params); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 474 | if (result != ErrorCode::OK) return {result, {}}; |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 475 | |
| 476 | string output; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 477 | return {Finish(message, &output), output}; |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 480 | string KeyMintAidlTestBase::ProcessMessage(const vector<uint8_t>& key_blob, KeyPurpose operation, |
| 481 | const string& message, const AuthorizationSet& in_params, |
| 482 | AuthorizationSet* out_params) { |
| 483 | SCOPED_TRACE("ProcessMessage"); |
| 484 | AuthorizationSet begin_out_params; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 485 | ErrorCode result = Begin(operation, key_blob, in_params, out_params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 486 | EXPECT_EQ(ErrorCode::OK, result); |
| 487 | if (result != ErrorCode::OK) { |
| 488 | return ""; |
| 489 | } |
| 490 | |
| 491 | string output; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 492 | EXPECT_EQ(ErrorCode::OK, Finish(message, &output)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 493 | return output; |
| 494 | } |
| 495 | |
| 496 | string KeyMintAidlTestBase::SignMessage(const vector<uint8_t>& key_blob, const string& message, |
| 497 | const AuthorizationSet& params) { |
| 498 | SCOPED_TRACE("SignMessage"); |
| 499 | AuthorizationSet out_params; |
| 500 | string signature = ProcessMessage(key_blob, KeyPurpose::SIGN, message, params, &out_params); |
| 501 | EXPECT_TRUE(out_params.empty()); |
| 502 | return signature; |
| 503 | } |
| 504 | |
| 505 | string KeyMintAidlTestBase::SignMessage(const string& message, const AuthorizationSet& params) { |
| 506 | SCOPED_TRACE("SignMessage"); |
| 507 | return SignMessage(key_blob_, message, params); |
| 508 | } |
| 509 | |
| 510 | string KeyMintAidlTestBase::MacMessage(const string& message, Digest digest, size_t mac_length) { |
| 511 | SCOPED_TRACE("MacMessage"); |
| 512 | return SignMessage( |
| 513 | key_blob_, message, |
| 514 | AuthorizationSetBuilder().Digest(digest).Authorization(TAG_MAC_LENGTH, mac_length)); |
| 515 | } |
| 516 | |
| 517 | void KeyMintAidlTestBase::CheckHmacTestVector(const string& key, const string& message, |
| 518 | Digest digest, const string& expected_mac) { |
| 519 | SCOPED_TRACE("CheckHmacTestVector"); |
| 520 | ASSERT_EQ(ErrorCode::OK, |
| 521 | ImportKey(AuthorizationSetBuilder() |
| 522 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 523 | .HmacKey(key.size() * 8) |
| 524 | .Authorization(TAG_MIN_MAC_LENGTH, expected_mac.size() * 8) |
| 525 | .Digest(digest), |
| 526 | KeyFormat::RAW, key)); |
| 527 | string signature = MacMessage(message, digest, expected_mac.size() * 8); |
| 528 | EXPECT_EQ(expected_mac, signature) |
| 529 | << "Test vector didn't match for key of size " << key.size() << " message of size " |
| 530 | << message.size() << " and digest " << digest; |
| 531 | CheckedDeleteKey(); |
| 532 | } |
| 533 | |
| 534 | void KeyMintAidlTestBase::CheckAesCtrTestVector(const string& key, const string& nonce, |
| 535 | const string& message, |
| 536 | const string& expected_ciphertext) { |
| 537 | SCOPED_TRACE("CheckAesCtrTestVector"); |
| 538 | ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder() |
| 539 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 540 | .AesEncryptionKey(key.size() * 8) |
| 541 | .BlockMode(BlockMode::CTR) |
| 542 | .Authorization(TAG_CALLER_NONCE) |
| 543 | .Padding(PaddingMode::NONE), |
| 544 | KeyFormat::RAW, key)); |
| 545 | |
| 546 | auto params = AuthorizationSetBuilder() |
| 547 | .Authorization(TAG_NONCE, nonce.data(), nonce.size()) |
| 548 | .BlockMode(BlockMode::CTR) |
| 549 | .Padding(PaddingMode::NONE); |
| 550 | AuthorizationSet out_params; |
| 551 | string ciphertext = EncryptMessage(key_blob_, message, params, &out_params); |
| 552 | EXPECT_EQ(expected_ciphertext, ciphertext); |
| 553 | } |
| 554 | |
| 555 | void KeyMintAidlTestBase::CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode, |
| 556 | PaddingMode padding_mode, const string& key, |
| 557 | const string& iv, const string& input, |
| 558 | const string& expected_output) { |
| 559 | auto authset = AuthorizationSetBuilder() |
| 560 | .TripleDesEncryptionKey(key.size() * 7) |
| 561 | .BlockMode(block_mode) |
| 562 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 563 | .Padding(padding_mode); |
| 564 | if (iv.size()) authset.Authorization(TAG_CALLER_NONCE); |
| 565 | ASSERT_EQ(ErrorCode::OK, ImportKey(authset, KeyFormat::RAW, key)); |
| 566 | ASSERT_GT(key_blob_.size(), 0U); |
| 567 | |
| 568 | auto begin_params = AuthorizationSetBuilder().BlockMode(block_mode).Padding(padding_mode); |
| 569 | if (iv.size()) begin_params.Authorization(TAG_NONCE, iv.data(), iv.size()); |
| 570 | AuthorizationSet output_params; |
| 571 | string output = ProcessMessage(key_blob_, purpose, input, begin_params, &output_params); |
| 572 | EXPECT_EQ(expected_output, output); |
| 573 | } |
| 574 | |
| 575 | void KeyMintAidlTestBase::VerifyMessage(const vector<uint8_t>& key_blob, const string& message, |
| 576 | const string& signature, const AuthorizationSet& params) { |
| 577 | SCOPED_TRACE("VerifyMessage"); |
| 578 | AuthorizationSet begin_out_params; |
| 579 | ASSERT_EQ(ErrorCode::OK, Begin(KeyPurpose::VERIFY, key_blob, params, &begin_out_params)); |
| 580 | |
| 581 | string output; |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 582 | EXPECT_EQ(ErrorCode::OK, Finish(message, signature, &output)); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 583 | EXPECT_TRUE(output.empty()); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 584 | op_ = {}; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | void KeyMintAidlTestBase::VerifyMessage(const string& message, const string& signature, |
| 588 | const AuthorizationSet& params) { |
| 589 | SCOPED_TRACE("VerifyMessage"); |
| 590 | VerifyMessage(key_blob_, message, signature, params); |
| 591 | } |
| 592 | |
| 593 | string KeyMintAidlTestBase::EncryptMessage(const vector<uint8_t>& key_blob, const string& message, |
| 594 | const AuthorizationSet& in_params, |
| 595 | AuthorizationSet* out_params) { |
| 596 | SCOPED_TRACE("EncryptMessage"); |
| 597 | return ProcessMessage(key_blob, KeyPurpose::ENCRYPT, message, in_params, out_params); |
| 598 | } |
| 599 | |
| 600 | string KeyMintAidlTestBase::EncryptMessage(const string& message, const AuthorizationSet& params, |
| 601 | AuthorizationSet* out_params) { |
| 602 | SCOPED_TRACE("EncryptMessage"); |
| 603 | return EncryptMessage(key_blob_, message, params, out_params); |
| 604 | } |
| 605 | |
| 606 | string KeyMintAidlTestBase::EncryptMessage(const string& message, const AuthorizationSet& params) { |
| 607 | SCOPED_TRACE("EncryptMessage"); |
| 608 | AuthorizationSet out_params; |
| 609 | string ciphertext = EncryptMessage(message, params, &out_params); |
| 610 | EXPECT_TRUE(out_params.empty()) << "Output params should be empty. Contained: " << out_params; |
| 611 | return ciphertext; |
| 612 | } |
| 613 | |
| 614 | string KeyMintAidlTestBase::EncryptMessage(const string& message, BlockMode block_mode, |
| 615 | PaddingMode padding) { |
| 616 | SCOPED_TRACE("EncryptMessage"); |
| 617 | auto params = AuthorizationSetBuilder().BlockMode(block_mode).Padding(padding); |
| 618 | AuthorizationSet out_params; |
| 619 | string ciphertext = EncryptMessage(message, params, &out_params); |
| 620 | EXPECT_TRUE(out_params.empty()) << "Output params should be empty. Contained: " << out_params; |
| 621 | return ciphertext; |
| 622 | } |
| 623 | |
| 624 | string KeyMintAidlTestBase::EncryptMessage(const string& message, BlockMode block_mode, |
| 625 | PaddingMode padding, vector<uint8_t>* iv_out) { |
| 626 | SCOPED_TRACE("EncryptMessage"); |
| 627 | auto params = AuthorizationSetBuilder().BlockMode(block_mode).Padding(padding); |
| 628 | AuthorizationSet out_params; |
| 629 | string ciphertext = EncryptMessage(message, params, &out_params); |
| 630 | EXPECT_EQ(1U, out_params.size()); |
| 631 | auto ivVal = out_params.GetTagValue(TAG_NONCE); |
Janis Danisevskis | 5ba0933 | 2020-12-17 10:05:15 -0800 | [diff] [blame] | 632 | EXPECT_TRUE(ivVal); |
| 633 | if (ivVal) *iv_out = *ivVal; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 634 | return ciphertext; |
| 635 | } |
| 636 | |
| 637 | string KeyMintAidlTestBase::EncryptMessage(const string& message, BlockMode block_mode, |
| 638 | PaddingMode padding, const vector<uint8_t>& iv_in) { |
| 639 | SCOPED_TRACE("EncryptMessage"); |
| 640 | auto params = AuthorizationSetBuilder() |
| 641 | .BlockMode(block_mode) |
| 642 | .Padding(padding) |
| 643 | .Authorization(TAG_NONCE, iv_in); |
| 644 | AuthorizationSet out_params; |
| 645 | string ciphertext = EncryptMessage(message, params, &out_params); |
| 646 | return ciphertext; |
| 647 | } |
| 648 | |
| 649 | string KeyMintAidlTestBase::EncryptMessage(const string& message, BlockMode block_mode, |
| 650 | PaddingMode padding, uint8_t mac_length_bits, |
| 651 | const vector<uint8_t>& iv_in) { |
| 652 | SCOPED_TRACE("EncryptMessage"); |
| 653 | auto params = AuthorizationSetBuilder() |
| 654 | .BlockMode(block_mode) |
| 655 | .Padding(padding) |
| 656 | .Authorization(TAG_MAC_LENGTH, mac_length_bits) |
| 657 | .Authorization(TAG_NONCE, iv_in); |
| 658 | AuthorizationSet out_params; |
| 659 | string ciphertext = EncryptMessage(message, params, &out_params); |
| 660 | return ciphertext; |
| 661 | } |
| 662 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 663 | string KeyMintAidlTestBase::EncryptMessage(const string& message, BlockMode block_mode, |
| 664 | PaddingMode padding, uint8_t mac_length_bits) { |
| 665 | SCOPED_TRACE("EncryptMessage"); |
| 666 | auto params = AuthorizationSetBuilder() |
| 667 | .BlockMode(block_mode) |
| 668 | .Padding(padding) |
| 669 | .Authorization(TAG_MAC_LENGTH, mac_length_bits); |
| 670 | AuthorizationSet out_params; |
| 671 | string ciphertext = EncryptMessage(message, params, &out_params); |
| 672 | return ciphertext; |
| 673 | } |
| 674 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 675 | string KeyMintAidlTestBase::DecryptMessage(const vector<uint8_t>& key_blob, |
| 676 | const string& ciphertext, |
| 677 | const AuthorizationSet& params) { |
| 678 | SCOPED_TRACE("DecryptMessage"); |
| 679 | AuthorizationSet out_params; |
| 680 | string plaintext = |
| 681 | ProcessMessage(key_blob, KeyPurpose::DECRYPT, ciphertext, params, &out_params); |
| 682 | EXPECT_TRUE(out_params.empty()); |
| 683 | return plaintext; |
| 684 | } |
| 685 | |
| 686 | string KeyMintAidlTestBase::DecryptMessage(const string& ciphertext, |
| 687 | const AuthorizationSet& params) { |
| 688 | SCOPED_TRACE("DecryptMessage"); |
| 689 | return DecryptMessage(key_blob_, ciphertext, params); |
| 690 | } |
| 691 | |
| 692 | string KeyMintAidlTestBase::DecryptMessage(const string& ciphertext, BlockMode block_mode, |
| 693 | PaddingMode padding_mode, const vector<uint8_t>& iv) { |
| 694 | SCOPED_TRACE("DecryptMessage"); |
| 695 | auto params = AuthorizationSetBuilder() |
| 696 | .BlockMode(block_mode) |
| 697 | .Padding(padding_mode) |
| 698 | .Authorization(TAG_NONCE, iv); |
| 699 | return DecryptMessage(key_blob_, ciphertext, params); |
| 700 | } |
| 701 | |
| 702 | std::pair<ErrorCode, vector<uint8_t>> KeyMintAidlTestBase::UpgradeKey( |
| 703 | const vector<uint8_t>& key_blob) { |
| 704 | std::pair<ErrorCode, vector<uint8_t>> retval; |
| 705 | vector<uint8_t> outKeyBlob; |
| 706 | Status result = keymint_->upgradeKey(key_blob, vector<KeyParameter>(), &outKeyBlob); |
| 707 | ErrorCode errorcode = GetReturnErrorCode(result); |
| 708 | retval = std::tie(errorcode, outKeyBlob); |
| 709 | |
| 710 | return retval; |
| 711 | } |
| 712 | vector<uint32_t> KeyMintAidlTestBase::ValidKeySizes(Algorithm algorithm) { |
| 713 | switch (algorithm) { |
| 714 | case Algorithm::RSA: |
| 715 | switch (SecLevel()) { |
| 716 | case SecurityLevel::SOFTWARE: |
| 717 | case SecurityLevel::TRUSTED_ENVIRONMENT: |
| 718 | return {2048, 3072, 4096}; |
| 719 | case SecurityLevel::STRONGBOX: |
| 720 | return {2048}; |
| 721 | default: |
| 722 | ADD_FAILURE() << "Invalid security level " << uint32_t(SecLevel()); |
| 723 | break; |
| 724 | } |
| 725 | break; |
| 726 | case Algorithm::EC: |
| 727 | switch (SecLevel()) { |
| 728 | case SecurityLevel::SOFTWARE: |
| 729 | case SecurityLevel::TRUSTED_ENVIRONMENT: |
| 730 | return {224, 256, 384, 521}; |
| 731 | case SecurityLevel::STRONGBOX: |
| 732 | return {256}; |
| 733 | default: |
| 734 | ADD_FAILURE() << "Invalid security level " << uint32_t(SecLevel()); |
| 735 | break; |
| 736 | } |
| 737 | break; |
| 738 | case Algorithm::AES: |
| 739 | return {128, 256}; |
| 740 | case Algorithm::TRIPLE_DES: |
| 741 | return {168}; |
| 742 | case Algorithm::HMAC: { |
| 743 | vector<uint32_t> retval((512 - 64) / 8 + 1); |
| 744 | uint32_t size = 64 - 8; |
| 745 | std::generate(retval.begin(), retval.end(), [&]() { return (size += 8); }); |
| 746 | return retval; |
| 747 | } |
| 748 | default: |
| 749 | ADD_FAILURE() << "Invalid Algorithm: " << algorithm; |
| 750 | return {}; |
| 751 | } |
| 752 | ADD_FAILURE() << "Should be impossible to get here"; |
| 753 | return {}; |
| 754 | } |
| 755 | |
| 756 | vector<uint32_t> KeyMintAidlTestBase::InvalidKeySizes(Algorithm algorithm) { |
| 757 | if (SecLevel() == SecurityLevel::STRONGBOX) { |
| 758 | switch (algorithm) { |
| 759 | case Algorithm::RSA: |
| 760 | return {3072, 4096}; |
| 761 | case Algorithm::EC: |
| 762 | return {224, 384, 521}; |
| 763 | case Algorithm::AES: |
| 764 | return {192}; |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 765 | case Algorithm::TRIPLE_DES: |
| 766 | return {56}; |
| 767 | default: |
| 768 | return {}; |
| 769 | } |
| 770 | } else { |
| 771 | switch (algorithm) { |
| 772 | case Algorithm::TRIPLE_DES: |
| 773 | return {56}; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 774 | default: |
| 775 | return {}; |
| 776 | } |
| 777 | } |
| 778 | return {}; |
| 779 | } |
| 780 | |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 781 | vector<BlockMode> KeyMintAidlTestBase::ValidBlockModes(Algorithm algorithm) { |
| 782 | switch (algorithm) { |
| 783 | case Algorithm::AES: |
| 784 | return { |
| 785 | BlockMode::CBC, |
| 786 | BlockMode::CTR, |
| 787 | BlockMode::ECB, |
| 788 | BlockMode::GCM, |
| 789 | }; |
| 790 | case Algorithm::TRIPLE_DES: |
| 791 | return { |
| 792 | BlockMode::CBC, |
| 793 | BlockMode::ECB, |
| 794 | }; |
| 795 | default: |
| 796 | return {}; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | vector<PaddingMode> KeyMintAidlTestBase::ValidPaddingModes(Algorithm algorithm, |
| 801 | BlockMode blockMode) { |
| 802 | switch (algorithm) { |
| 803 | case Algorithm::AES: |
| 804 | switch (blockMode) { |
| 805 | case BlockMode::CBC: |
| 806 | case BlockMode::ECB: |
| 807 | return {PaddingMode::NONE, PaddingMode::PKCS7}; |
| 808 | case BlockMode::CTR: |
| 809 | case BlockMode::GCM: |
| 810 | return {PaddingMode::NONE}; |
| 811 | default: |
| 812 | return {}; |
| 813 | }; |
| 814 | case Algorithm::TRIPLE_DES: |
| 815 | switch (blockMode) { |
| 816 | case BlockMode::CBC: |
| 817 | case BlockMode::ECB: |
| 818 | return {PaddingMode::NONE, PaddingMode::PKCS7}; |
| 819 | default: |
| 820 | return {}; |
| 821 | }; |
| 822 | default: |
| 823 | return {}; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | vector<PaddingMode> KeyMintAidlTestBase::InvalidPaddingModes(Algorithm algorithm, |
| 828 | BlockMode blockMode) { |
| 829 | switch (algorithm) { |
| 830 | case Algorithm::AES: |
| 831 | switch (blockMode) { |
| 832 | case BlockMode::CTR: |
| 833 | case BlockMode::GCM: |
| 834 | return {PaddingMode::PKCS7}; |
| 835 | default: |
| 836 | return {}; |
| 837 | }; |
| 838 | default: |
| 839 | return {}; |
| 840 | } |
| 841 | } |
| 842 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 843 | vector<EcCurve> KeyMintAidlTestBase::ValidCurves() { |
| 844 | if (securityLevel_ == SecurityLevel::STRONGBOX) { |
| 845 | return {EcCurve::P_256}; |
| 846 | } else { |
| 847 | return {EcCurve::P_224, EcCurve::P_256, EcCurve::P_384, EcCurve::P_521}; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | vector<EcCurve> KeyMintAidlTestBase::InvalidCurves() { |
| 852 | if (SecLevel() == SecurityLevel::TRUSTED_ENVIRONMENT) return {}; |
| 853 | CHECK(SecLevel() == SecurityLevel::STRONGBOX); |
| 854 | return {EcCurve::P_224, EcCurve::P_384, EcCurve::P_521}; |
| 855 | } |
| 856 | |
| 857 | vector<Digest> KeyMintAidlTestBase::ValidDigests(bool withNone, bool withMD5) { |
| 858 | switch (SecLevel()) { |
| 859 | case SecurityLevel::SOFTWARE: |
| 860 | case SecurityLevel::TRUSTED_ENVIRONMENT: |
| 861 | if (withNone) { |
| 862 | if (withMD5) |
| 863 | return {Digest::NONE, Digest::MD5, Digest::SHA1, |
| 864 | Digest::SHA_2_224, Digest::SHA_2_256, Digest::SHA_2_384, |
| 865 | Digest::SHA_2_512}; |
| 866 | else |
| 867 | return {Digest::NONE, Digest::SHA1, Digest::SHA_2_224, |
| 868 | Digest::SHA_2_256, Digest::SHA_2_384, Digest::SHA_2_512}; |
| 869 | } else { |
| 870 | if (withMD5) |
| 871 | return {Digest::MD5, Digest::SHA1, Digest::SHA_2_224, |
| 872 | Digest::SHA_2_256, Digest::SHA_2_384, Digest::SHA_2_512}; |
| 873 | else |
| 874 | return {Digest::SHA1, Digest::SHA_2_224, Digest::SHA_2_256, Digest::SHA_2_384, |
| 875 | Digest::SHA_2_512}; |
| 876 | } |
| 877 | break; |
| 878 | case SecurityLevel::STRONGBOX: |
| 879 | if (withNone) |
| 880 | return {Digest::NONE, Digest::SHA_2_256}; |
| 881 | else |
| 882 | return {Digest::SHA_2_256}; |
| 883 | break; |
| 884 | default: |
| 885 | ADD_FAILURE() << "Invalid security level " << uint32_t(SecLevel()); |
| 886 | break; |
| 887 | } |
| 888 | ADD_FAILURE() << "Should be impossible to get here"; |
| 889 | return {}; |
| 890 | } |
| 891 | |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 892 | static const vector<KeyParameter> kEmptyAuthList{}; |
| 893 | |
| 894 | const vector<KeyParameter>& KeyMintAidlTestBase::SecLevelAuthorizations( |
| 895 | const vector<KeyCharacteristics>& key_characteristics) { |
| 896 | auto found = std::find_if(key_characteristics.begin(), key_characteristics.end(), |
| 897 | [this](auto& entry) { return entry.securityLevel == SecLevel(); }); |
| 898 | return (found == key_characteristics.end()) ? kEmptyAuthList : found->authorizations; |
| 899 | } |
| 900 | |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 901 | const vector<KeyParameter>& KeyMintAidlTestBase::SecLevelAuthorizations( |
| 902 | const vector<KeyCharacteristics>& key_characteristics, SecurityLevel securityLevel) { |
| 903 | auto found = std::find_if( |
| 904 | key_characteristics.begin(), key_characteristics.end(), |
| 905 | [securityLevel](auto& entry) { return entry.securityLevel == securityLevel; }); |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 906 | return (found == key_characteristics.end()) ? kEmptyAuthList : found->authorizations; |
| 907 | } |
| 908 | |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 909 | ErrorCode KeyMintAidlTestBase::UseAesKey(const vector<uint8_t>& aesKeyBlob) { |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 910 | auto [result, ciphertext] = ProcessMessage( |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 911 | aesKeyBlob, KeyPurpose::ENCRYPT, "1234567890123456", |
| 912 | AuthorizationSetBuilder().BlockMode(BlockMode::ECB).Padding(PaddingMode::NONE)); |
| 913 | return result; |
| 914 | } |
| 915 | |
| 916 | ErrorCode KeyMintAidlTestBase::UseHmacKey(const vector<uint8_t>& hmacKeyBlob) { |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 917 | auto [result, mac] = ProcessMessage( |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 918 | hmacKeyBlob, KeyPurpose::SIGN, "1234567890123456", |
| 919 | AuthorizationSetBuilder().Authorization(TAG_MAC_LENGTH, 128).Digest(Digest::SHA_2_256)); |
| 920 | return result; |
| 921 | } |
| 922 | |
| 923 | ErrorCode KeyMintAidlTestBase::UseRsaKey(const vector<uint8_t>& rsaKeyBlob) { |
| 924 | std::string message(2048 / 8, 'a'); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 925 | auto [result, signature] = ProcessMessage( |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 926 | rsaKeyBlob, KeyPurpose::SIGN, message, |
| 927 | AuthorizationSetBuilder().Digest(Digest::NONE).Padding(PaddingMode::NONE)); |
| 928 | return result; |
| 929 | } |
| 930 | |
| 931 | ErrorCode KeyMintAidlTestBase::UseEcdsaKey(const vector<uint8_t>& ecdsaKeyBlob) { |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 932 | auto [result, signature] = ProcessMessage(ecdsaKeyBlob, KeyPurpose::SIGN, "a", |
| 933 | AuthorizationSetBuilder().Digest(Digest::SHA_2_256)); |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 934 | return result; |
| 935 | } |
| 936 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 937 | void verify_serial(X509* cert, const uint64_t expected_serial) { |
| 938 | BIGNUM_Ptr ser(BN_new()); |
| 939 | EXPECT_TRUE(ASN1_INTEGER_to_BN(X509_get_serialNumber(cert), ser.get())); |
| 940 | |
| 941 | uint64_t serial; |
| 942 | EXPECT_TRUE(BN_get_u64(ser.get(), &serial)); |
| 943 | EXPECT_EQ(serial, expected_serial); |
| 944 | } |
| 945 | |
| 946 | // Please set self_signed to true for fake certificates or self signed |
| 947 | // certificates |
| 948 | void verify_subject(const X509* cert, // |
| 949 | const string& subject, // |
| 950 | bool self_signed) { |
| 951 | char* cert_issuer = // |
| 952 | X509_NAME_oneline(X509_get_issuer_name(cert), nullptr, 0); |
| 953 | |
| 954 | char* cert_subj = X509_NAME_oneline(X509_get_subject_name(cert), nullptr, 0); |
| 955 | |
| 956 | string expected_subject("/CN="); |
| 957 | if (subject.empty()) { |
| 958 | expected_subject.append("Android Keystore Key"); |
| 959 | } else { |
| 960 | expected_subject.append(subject); |
| 961 | } |
| 962 | |
| 963 | EXPECT_STREQ(expected_subject.c_str(), cert_subj) << "Cert has wrong subject." << cert_subj; |
| 964 | |
| 965 | if (self_signed) { |
| 966 | EXPECT_STREQ(cert_issuer, cert_subj) |
| 967 | << "Cert issuer and subject mismatch for self signed certificate."; |
| 968 | } |
| 969 | |
| 970 | OPENSSL_free(cert_subj); |
| 971 | OPENSSL_free(cert_issuer); |
| 972 | } |
| 973 | |
| 974 | vector<uint8_t> build_serial_blob(const uint64_t serial_int) { |
| 975 | BIGNUM_Ptr serial(BN_new()); |
| 976 | EXPECT_TRUE(BN_set_u64(serial.get(), serial_int)); |
| 977 | |
| 978 | int len = BN_num_bytes(serial.get()); |
| 979 | vector<uint8_t> serial_blob(len); |
| 980 | if (BN_bn2bin(serial.get(), serial_blob.data()) != len) { |
| 981 | return {}; |
| 982 | } |
| 983 | |
| 984 | return serial_blob; |
| 985 | } |
| 986 | |
| 987 | void verify_subject_and_serial(const Certificate& certificate, // |
| 988 | const uint64_t expected_serial, // |
| 989 | const string& subject, bool self_signed) { |
| 990 | X509_Ptr cert(parse_cert_blob(certificate.encodedCertificate)); |
| 991 | ASSERT_TRUE(!!cert.get()); |
| 992 | |
| 993 | verify_serial(cert.get(), expected_serial); |
| 994 | verify_subject(cert.get(), subject, self_signed); |
| 995 | } |
| 996 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 997 | bool verify_attestation_record(const string& challenge, // |
| 998 | const string& app_id, // |
| 999 | AuthorizationSet expected_sw_enforced, // |
| 1000 | AuthorizationSet expected_hw_enforced, // |
| 1001 | SecurityLevel security_level, |
| 1002 | const vector<uint8_t>& attestation_cert) { |
| 1003 | X509_Ptr cert(parse_cert_blob(attestation_cert)); |
| 1004 | EXPECT_TRUE(!!cert.get()); |
| 1005 | if (!cert.get()) return false; |
| 1006 | |
| 1007 | ASN1_OCTET_STRING* attest_rec = get_attestation_record(cert.get()); |
| 1008 | EXPECT_TRUE(!!attest_rec); |
| 1009 | if (!attest_rec) return false; |
| 1010 | |
| 1011 | AuthorizationSet att_sw_enforced; |
| 1012 | AuthorizationSet att_hw_enforced; |
| 1013 | uint32_t att_attestation_version; |
| 1014 | uint32_t att_keymaster_version; |
| 1015 | SecurityLevel att_attestation_security_level; |
| 1016 | SecurityLevel att_keymaster_security_level; |
| 1017 | vector<uint8_t> att_challenge; |
| 1018 | vector<uint8_t> att_unique_id; |
| 1019 | vector<uint8_t> att_app_id; |
| 1020 | |
| 1021 | auto error = parse_attestation_record(attest_rec->data, // |
| 1022 | attest_rec->length, // |
| 1023 | &att_attestation_version, // |
| 1024 | &att_attestation_security_level, // |
| 1025 | &att_keymaster_version, // |
| 1026 | &att_keymaster_security_level, // |
| 1027 | &att_challenge, // |
| 1028 | &att_sw_enforced, // |
| 1029 | &att_hw_enforced, // |
| 1030 | &att_unique_id); |
| 1031 | EXPECT_EQ(ErrorCode::OK, error); |
| 1032 | if (error != ErrorCode::OK) return false; |
| 1033 | |
Shawn Willden | 3cb64a6 | 2021-04-05 14:39:05 -0600 | [diff] [blame] | 1034 | EXPECT_EQ(att_attestation_version, 100U); |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1035 | vector<uint8_t> appId(app_id.begin(), app_id.end()); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1036 | |
Selene Huang | 4f64c22 | 2021-04-13 19:54:36 -0700 | [diff] [blame] | 1037 | // check challenge and app id only if we expects a non-fake certificate |
| 1038 | if (challenge.length() > 0) { |
| 1039 | EXPECT_EQ(challenge.length(), att_challenge.size()); |
| 1040 | EXPECT_EQ(0, memcmp(challenge.data(), att_challenge.data(), challenge.length())); |
| 1041 | |
| 1042 | expected_sw_enforced.push_back(TAG_ATTESTATION_APPLICATION_ID, appId); |
| 1043 | } |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1044 | |
Shawn Willden | 3cb64a6 | 2021-04-05 14:39:05 -0600 | [diff] [blame] | 1045 | EXPECT_EQ(att_keymaster_version, 100U); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1046 | EXPECT_EQ(security_level, att_keymaster_security_level); |
| 1047 | EXPECT_EQ(security_level, att_attestation_security_level); |
| 1048 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1049 | |
| 1050 | char property_value[PROPERTY_VALUE_MAX] = {}; |
| 1051 | // TODO(b/136282179): When running under VTS-on-GSI the TEE-backed |
| 1052 | // keymaster implementation will report YYYYMM dates instead of YYYYMMDD |
| 1053 | // for the BOOT_PATCH_LEVEL. |
| 1054 | if (avb_verification_enabled()) { |
| 1055 | for (int i = 0; i < att_hw_enforced.size(); i++) { |
| 1056 | if (att_hw_enforced[i].tag == TAG_BOOT_PATCHLEVEL || |
| 1057 | att_hw_enforced[i].tag == TAG_VENDOR_PATCHLEVEL) { |
| 1058 | std::string date = |
Tommy Chiu | f00d8f1 | 2021-04-08 11:07:48 +0800 | [diff] [blame] | 1059 | std::to_string(att_hw_enforced[i].value.get<KeyParameterValue::integer>()); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1060 | // strptime seems to require delimiters, but the tag value will |
| 1061 | // be YYYYMMDD |
| 1062 | date.insert(6, "-"); |
| 1063 | date.insert(4, "-"); |
| 1064 | EXPECT_EQ(date.size(), 10); |
| 1065 | struct tm time; |
| 1066 | strptime(date.c_str(), "%Y-%m-%d", &time); |
| 1067 | |
| 1068 | // Day of the month (0-31) |
| 1069 | EXPECT_GE(time.tm_mday, 0); |
| 1070 | EXPECT_LT(time.tm_mday, 32); |
| 1071 | // Months since Jan (0-11) |
| 1072 | EXPECT_GE(time.tm_mon, 0); |
| 1073 | EXPECT_LT(time.tm_mon, 12); |
| 1074 | // Years since 1900 |
| 1075 | EXPECT_GT(time.tm_year, 110); |
| 1076 | EXPECT_LT(time.tm_year, 200); |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | // Check to make sure boolean values are properly encoded. Presence of a boolean tag |
| 1082 | // indicates true. A provided boolean tag that can be pulled back out of the certificate |
| 1083 | // indicates correct encoding. No need to check if it's in both lists, since the |
| 1084 | // AuthorizationSet compare below will handle mismatches of tags. |
| 1085 | if (security_level == SecurityLevel::SOFTWARE) { |
| 1086 | EXPECT_TRUE(expected_sw_enforced.Contains(TAG_NO_AUTH_REQUIRED)); |
| 1087 | } else { |
| 1088 | EXPECT_TRUE(expected_hw_enforced.Contains(TAG_NO_AUTH_REQUIRED)); |
| 1089 | } |
| 1090 | |
| 1091 | // Alternatively this checks the opposite - a false boolean tag (one that isn't provided in |
| 1092 | // the authorization list during key generation) isn't being attested to in the certificate. |
| 1093 | EXPECT_FALSE(expected_sw_enforced.Contains(TAG_TRUSTED_USER_PRESENCE_REQUIRED)); |
| 1094 | EXPECT_FALSE(att_sw_enforced.Contains(TAG_TRUSTED_USER_PRESENCE_REQUIRED)); |
| 1095 | EXPECT_FALSE(expected_hw_enforced.Contains(TAG_TRUSTED_USER_PRESENCE_REQUIRED)); |
| 1096 | EXPECT_FALSE(att_hw_enforced.Contains(TAG_TRUSTED_USER_PRESENCE_REQUIRED)); |
| 1097 | |
| 1098 | if (att_hw_enforced.Contains(TAG_ALGORITHM, Algorithm::EC)) { |
| 1099 | // For ECDSA keys, either an EC_CURVE or a KEY_SIZE can be specified, but one must be. |
| 1100 | EXPECT_TRUE(att_hw_enforced.Contains(TAG_EC_CURVE) || |
| 1101 | att_hw_enforced.Contains(TAG_KEY_SIZE)); |
| 1102 | } |
| 1103 | |
| 1104 | // Test root of trust elements |
| 1105 | vector<uint8_t> verified_boot_key; |
| 1106 | VerifiedBoot verified_boot_state; |
| 1107 | bool device_locked; |
| 1108 | vector<uint8_t> verified_boot_hash; |
| 1109 | error = parse_root_of_trust(attest_rec->data, attest_rec->length, &verified_boot_key, |
| 1110 | &verified_boot_state, &device_locked, &verified_boot_hash); |
| 1111 | EXPECT_EQ(ErrorCode::OK, error); |
| 1112 | |
| 1113 | if (avb_verification_enabled()) { |
| 1114 | EXPECT_NE(property_get("ro.boot.vbmeta.digest", property_value, ""), 0); |
| 1115 | string prop_string(property_value); |
| 1116 | EXPECT_EQ(prop_string.size(), 64); |
| 1117 | EXPECT_EQ(prop_string, bin2hex(verified_boot_hash)); |
| 1118 | |
| 1119 | EXPECT_NE(property_get("ro.boot.vbmeta.device_state", property_value, ""), 0); |
| 1120 | if (!strcmp(property_value, "unlocked")) { |
| 1121 | EXPECT_FALSE(device_locked); |
| 1122 | } else { |
| 1123 | EXPECT_TRUE(device_locked); |
| 1124 | } |
| 1125 | |
| 1126 | // Check that the device is locked if not debuggable, e.g., user build |
| 1127 | // images in CTS. For VTS, debuggable images are used to allow adb root |
| 1128 | // and the device is unlocked. |
| 1129 | if (!property_get_bool("ro.debuggable", false)) { |
| 1130 | EXPECT_TRUE(device_locked); |
| 1131 | } else { |
| 1132 | EXPECT_FALSE(device_locked); |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | // Verified boot key should be all 0's if the boot state is not verified or self signed |
| 1137 | std::string empty_boot_key(32, '\0'); |
| 1138 | std::string verified_boot_key_str((const char*)verified_boot_key.data(), |
| 1139 | verified_boot_key.size()); |
| 1140 | EXPECT_NE(property_get("ro.boot.verifiedbootstate", property_value, ""), 0); |
| 1141 | if (!strcmp(property_value, "green")) { |
| 1142 | EXPECT_EQ(verified_boot_state, VerifiedBoot::VERIFIED); |
| 1143 | EXPECT_NE(0, memcmp(verified_boot_key.data(), empty_boot_key.data(), |
| 1144 | verified_boot_key.size())); |
| 1145 | } else if (!strcmp(property_value, "yellow")) { |
| 1146 | EXPECT_EQ(verified_boot_state, VerifiedBoot::SELF_SIGNED); |
| 1147 | EXPECT_NE(0, memcmp(verified_boot_key.data(), empty_boot_key.data(), |
| 1148 | verified_boot_key.size())); |
| 1149 | } else if (!strcmp(property_value, "orange")) { |
| 1150 | EXPECT_EQ(verified_boot_state, VerifiedBoot::UNVERIFIED); |
| 1151 | EXPECT_EQ(0, memcmp(verified_boot_key.data(), empty_boot_key.data(), |
| 1152 | verified_boot_key.size())); |
| 1153 | } else if (!strcmp(property_value, "red")) { |
| 1154 | EXPECT_EQ(verified_boot_state, VerifiedBoot::FAILED); |
| 1155 | } else { |
| 1156 | EXPECT_EQ(verified_boot_state, VerifiedBoot::UNVERIFIED); |
| 1157 | EXPECT_NE(0, memcmp(verified_boot_key.data(), empty_boot_key.data(), |
| 1158 | verified_boot_key.size())); |
| 1159 | } |
| 1160 | |
| 1161 | att_sw_enforced.Sort(); |
| 1162 | expected_sw_enforced.Sort(); |
| 1163 | auto a = filtered_tags(expected_sw_enforced); |
| 1164 | auto b = filtered_tags(att_sw_enforced); |
| 1165 | EXPECT_EQ(a, b); |
| 1166 | |
| 1167 | att_hw_enforced.Sort(); |
| 1168 | expected_hw_enforced.Sort(); |
| 1169 | EXPECT_EQ(filtered_tags(expected_hw_enforced), filtered_tags(att_hw_enforced)); |
| 1170 | |
| 1171 | return true; |
| 1172 | } |
| 1173 | |
| 1174 | string bin2hex(const vector<uint8_t>& data) { |
| 1175 | string retval; |
| 1176 | retval.reserve(data.size() * 2 + 1); |
| 1177 | for (uint8_t byte : data) { |
| 1178 | retval.push_back(nibble2hex[0x0F & (byte >> 4)]); |
| 1179 | retval.push_back(nibble2hex[0x0F & byte]); |
| 1180 | } |
| 1181 | return retval; |
| 1182 | } |
| 1183 | |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 1184 | AuthorizationSet HwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics) { |
| 1185 | AuthorizationSet authList; |
| 1186 | for (auto& entry : key_characteristics) { |
| 1187 | if (entry.securityLevel == SecurityLevel::STRONGBOX || |
| 1188 | entry.securityLevel == SecurityLevel::TRUSTED_ENVIRONMENT) { |
| 1189 | authList.push_back(AuthorizationSet(entry.authorizations)); |
| 1190 | } |
| 1191 | } |
| 1192 | return authList; |
| 1193 | } |
| 1194 | |
| 1195 | AuthorizationSet SwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics) { |
| 1196 | AuthorizationSet authList; |
| 1197 | for (auto& entry : key_characteristics) { |
| 1198 | if (entry.securityLevel == SecurityLevel::SOFTWARE || |
| 1199 | entry.securityLevel == SecurityLevel::KEYSTORE) { |
| 1200 | authList.push_back(AuthorizationSet(entry.authorizations)); |
| 1201 | } |
| 1202 | } |
| 1203 | return authList; |
| 1204 | } |
| 1205 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1206 | AssertionResult ChainSignaturesAreValid(const vector<Certificate>& chain) { |
| 1207 | std::stringstream cert_data; |
| 1208 | |
| 1209 | for (size_t i = 0; i < chain.size(); ++i) { |
| 1210 | cert_data << bin2hex(chain[i].encodedCertificate) << std::endl; |
| 1211 | |
| 1212 | X509_Ptr key_cert(parse_cert_blob(chain[i].encodedCertificate)); |
| 1213 | X509_Ptr signing_cert; |
| 1214 | if (i < chain.size() - 1) { |
| 1215 | signing_cert = parse_cert_blob(chain[i + 1].encodedCertificate); |
| 1216 | } else { |
| 1217 | signing_cert = parse_cert_blob(chain[i].encodedCertificate); |
| 1218 | } |
| 1219 | if (!key_cert.get() || !signing_cert.get()) return AssertionFailure() << cert_data.str(); |
| 1220 | |
| 1221 | EVP_PKEY_Ptr signing_pubkey(X509_get_pubkey(signing_cert.get())); |
| 1222 | if (!signing_pubkey.get()) return AssertionFailure() << cert_data.str(); |
| 1223 | |
| 1224 | if (!X509_verify(key_cert.get(), signing_pubkey.get())) { |
| 1225 | return AssertionFailure() |
| 1226 | << "Verification of certificate " << i << " failed " |
| 1227 | << "OpenSSL error string: " << ERR_error_string(ERR_get_error(), NULL) << '\n' |
| 1228 | << cert_data.str(); |
| 1229 | } |
| 1230 | |
| 1231 | string cert_issuer = x509NameToStr(X509_get_issuer_name(key_cert.get())); |
| 1232 | string signer_subj = x509NameToStr(X509_get_subject_name(signing_cert.get())); |
| 1233 | if (cert_issuer != signer_subj) { |
Selene Huang | 8f9494c | 2021-04-21 15:10:36 -0700 | [diff] [blame] | 1234 | return AssertionFailure() << "Cert " << i << " has wrong issuer.\n" |
| 1235 | << " Signer subject is " << signer_subj |
| 1236 | << " Issuer subject is " << cert_issuer << endl |
| 1237 | << cert_data.str(); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1238 | } |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | if (KeyMintAidlTestBase::dump_Attestations) std::cout << cert_data.str(); |
| 1242 | return AssertionSuccess(); |
| 1243 | } |
| 1244 | |
| 1245 | X509_Ptr parse_cert_blob(const vector<uint8_t>& blob) { |
| 1246 | const uint8_t* p = blob.data(); |
| 1247 | return X509_Ptr(d2i_X509(nullptr /* allocate new */, &p, blob.size())); |
| 1248 | } |
| 1249 | |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 1250 | vector<uint8_t> make_name_from_str(const string& name) { |
| 1251 | X509_NAME_Ptr x509_name(X509_NAME_new()); |
| 1252 | EXPECT_TRUE(x509_name.get() != nullptr); |
| 1253 | if (!x509_name) return {}; |
| 1254 | |
| 1255 | EXPECT_EQ(1, X509_NAME_add_entry_by_txt(x509_name.get(), // |
| 1256 | "CN", // |
| 1257 | MBSTRING_ASC, |
| 1258 | reinterpret_cast<const uint8_t*>(name.c_str()), |
| 1259 | -1, // len |
| 1260 | -1, // loc |
| 1261 | 0 /* set */)); |
| 1262 | |
| 1263 | int len = i2d_X509_NAME(x509_name.get(), nullptr /* only return length */); |
| 1264 | EXPECT_GT(len, 0); |
| 1265 | |
| 1266 | vector<uint8_t> retval(len); |
| 1267 | uint8_t* p = retval.data(); |
| 1268 | i2d_X509_NAME(x509_name.get(), &p); |
| 1269 | |
| 1270 | return retval; |
| 1271 | } |
| 1272 | |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 1273 | namespace { |
| 1274 | |
| 1275 | void check_cose_key(const vector<uint8_t>& data, bool testMode) { |
| 1276 | auto [parsedPayload, __, payloadParseErr] = cppbor::parse(data); |
| 1277 | ASSERT_TRUE(parsedPayload) << "Key parse failed: " << payloadParseErr; |
| 1278 | |
| 1279 | // The following check assumes that canonical CBOR encoding is used for the COSE_Key. |
| 1280 | if (testMode) { |
| 1281 | EXPECT_THAT(cppbor::prettyPrint(parsedPayload.get()), |
| 1282 | MatchesRegex("{\n" |
| 1283 | " 1 : 2,\n" // kty: EC2 |
| 1284 | " 3 : -7,\n" // alg: ES256 |
| 1285 | " -1 : 1,\n" // EC id: P256 |
| 1286 | // The regex {(0x[0-9a-f]{2}, ){31}0x[0-9a-f]{2}} matches a |
| 1287 | // sequence of 32 hexadecimal bytes, enclosed in braces and |
| 1288 | // separated by commas. In this case, some Ed25519 public key. |
| 1289 | " -2 : {(0x[0-9a-f]{2}, ){31}0x[0-9a-f]{2}},\n" // pub_x: data |
| 1290 | " -3 : {(0x[0-9a-f]{2}, ){31}0x[0-9a-f]{2}},\n" // pub_y: data |
| 1291 | " -70000 : null,\n" // test marker |
| 1292 | "}")); |
| 1293 | } else { |
| 1294 | EXPECT_THAT(cppbor::prettyPrint(parsedPayload.get()), |
| 1295 | MatchesRegex("{\n" |
| 1296 | " 1 : 2,\n" // kty: EC2 |
| 1297 | " 3 : -7,\n" // alg: ES256 |
| 1298 | " -1 : 1,\n" // EC id: P256 |
| 1299 | // The regex {(0x[0-9a-f]{2}, ){31}0x[0-9a-f]{2}} matches a |
| 1300 | // sequence of 32 hexadecimal bytes, enclosed in braces and |
| 1301 | // separated by commas. In this case, some Ed25519 public key. |
| 1302 | " -2 : {(0x[0-9a-f]{2}, ){31}0x[0-9a-f]{2}},\n" // pub_x: data |
| 1303 | " -3 : {(0x[0-9a-f]{2}, ){31}0x[0-9a-f]{2}},\n" // pub_y: data |
| 1304 | "}")); |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | } // namespace |
| 1309 | |
| 1310 | void check_maced_pubkey(const MacedPublicKey& macedPubKey, bool testMode, |
| 1311 | vector<uint8_t>* payload_value) { |
| 1312 | auto [coseMac0, _, mac0ParseErr] = cppbor::parse(macedPubKey.macedKey); |
| 1313 | ASSERT_TRUE(coseMac0) << "COSE Mac0 parse failed " << mac0ParseErr; |
| 1314 | |
| 1315 | ASSERT_NE(coseMac0->asArray(), nullptr); |
| 1316 | ASSERT_EQ(coseMac0->asArray()->size(), kCoseMac0EntryCount); |
| 1317 | |
| 1318 | auto protParms = coseMac0->asArray()->get(kCoseMac0ProtectedParams)->asBstr(); |
| 1319 | ASSERT_NE(protParms, nullptr); |
| 1320 | |
| 1321 | // Header label:value of 'alg': HMAC-256 |
| 1322 | ASSERT_EQ(cppbor::prettyPrint(protParms->value()), "{\n 1 : 5,\n}"); |
| 1323 | |
| 1324 | auto unprotParms = coseMac0->asArray()->get(kCoseMac0UnprotectedParams)->asMap(); |
| 1325 | ASSERT_NE(unprotParms, nullptr); |
| 1326 | ASSERT_EQ(unprotParms->size(), 0); |
| 1327 | |
| 1328 | // The payload is a bstr holding an encoded COSE_Key |
| 1329 | auto payload = coseMac0->asArray()->get(kCoseMac0Payload)->asBstr(); |
| 1330 | ASSERT_NE(payload, nullptr); |
| 1331 | check_cose_key(payload->value(), testMode); |
| 1332 | |
| 1333 | auto coseMac0Tag = coseMac0->asArray()->get(kCoseMac0Tag)->asBstr(); |
| 1334 | ASSERT_TRUE(coseMac0Tag); |
| 1335 | auto extractedTag = coseMac0Tag->value(); |
| 1336 | EXPECT_EQ(extractedTag.size(), 32U); |
| 1337 | |
| 1338 | // Compare with tag generated with kTestMacKey. Should only match in test mode |
| 1339 | auto testTag = cppcose::generateCoseMac0Mac(remote_prov::kTestMacKey, {} /* external_aad */, |
| 1340 | payload->value()); |
| 1341 | ASSERT_TRUE(testTag) << "Tag calculation failed: " << testTag.message(); |
| 1342 | |
| 1343 | if (testMode) { |
| 1344 | EXPECT_EQ(*testTag, extractedTag); |
| 1345 | } else { |
| 1346 | EXPECT_NE(*testTag, extractedTag); |
| 1347 | } |
| 1348 | if (payload_value != nullptr) { |
| 1349 | *payload_value = payload->value(); |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | void p256_pub_key(const vector<uint8_t>& coseKeyData, EVP_PKEY_Ptr* signingKey) { |
| 1354 | // Extract x and y affine coordinates from the encoded Cose_Key. |
| 1355 | auto [parsedPayload, __, payloadParseErr] = cppbor::parse(coseKeyData); |
| 1356 | ASSERT_TRUE(parsedPayload) << "Key parse failed: " << payloadParseErr; |
| 1357 | auto coseKey = parsedPayload->asMap(); |
| 1358 | const std::unique_ptr<cppbor::Item>& xItem = coseKey->get(cppcose::CoseKey::PUBKEY_X); |
| 1359 | ASSERT_NE(xItem->asBstr(), nullptr); |
| 1360 | vector<uint8_t> x = xItem->asBstr()->value(); |
| 1361 | const std::unique_ptr<cppbor::Item>& yItem = coseKey->get(cppcose::CoseKey::PUBKEY_Y); |
| 1362 | ASSERT_NE(yItem->asBstr(), nullptr); |
| 1363 | vector<uint8_t> y = yItem->asBstr()->value(); |
| 1364 | |
| 1365 | // Concatenate: 0x04 (uncompressed form marker) | x | y |
| 1366 | vector<uint8_t> pubKeyData{0x04}; |
| 1367 | pubKeyData.insert(pubKeyData.end(), x.begin(), x.end()); |
| 1368 | pubKeyData.insert(pubKeyData.end(), y.begin(), y.end()); |
| 1369 | |
| 1370 | EC_KEY_Ptr ecKey = EC_KEY_Ptr(EC_KEY_new()); |
| 1371 | ASSERT_NE(ecKey, nullptr); |
| 1372 | EC_GROUP_Ptr group = EC_GROUP_Ptr(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1)); |
| 1373 | ASSERT_NE(group, nullptr); |
| 1374 | ASSERT_EQ(EC_KEY_set_group(ecKey.get(), group.get()), 1); |
| 1375 | EC_POINT_Ptr point = EC_POINT_Ptr(EC_POINT_new(group.get())); |
| 1376 | ASSERT_NE(point, nullptr); |
| 1377 | ASSERT_EQ(EC_POINT_oct2point(group.get(), point.get(), pubKeyData.data(), pubKeyData.size(), |
| 1378 | nullptr), |
| 1379 | 1); |
| 1380 | ASSERT_EQ(EC_KEY_set_public_key(ecKey.get(), point.get()), 1); |
| 1381 | |
| 1382 | EVP_PKEY_Ptr pubKey = EVP_PKEY_Ptr(EVP_PKEY_new()); |
| 1383 | ASSERT_NE(pubKey, nullptr); |
| 1384 | EVP_PKEY_assign_EC_KEY(pubKey.get(), ecKey.release()); |
| 1385 | *signingKey = std::move(pubKey); |
| 1386 | } |
| 1387 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1388 | } // namespace test |
Shawn Willden | 08a7e43 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 1389 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 1390 | } // namespace aidl::android::hardware::security::keymint |