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