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 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 17 | #pragma once |
| 18 | |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 19 | #include <functional> |
David Drysdale | 7b05efd | 2024-10-10 18:11:25 +0100 | [diff] [blame] | 20 | #include <optional> |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 21 | #include <string_view> |
| 22 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 23 | #include <aidl/Gtest.h> |
| 24 | #include <aidl/Vintf.h> |
David Drysdale | a676c3b | 2021-06-14 14:46:02 +0100 | [diff] [blame] | 25 | #include <android-base/properties.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 26 | #include <binder/IServiceManager.h> |
| 27 | #include <binder/ProcessState.h> |
| 28 | #include <gtest/gtest.h> |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 29 | #include <openssl/x509.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 30 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 31 | #include <aidl/android/hardware/security/keymint/ErrorCode.h> |
| 32 | #include <aidl/android/hardware/security/keymint/IKeyMintDevice.h> |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 33 | #include <aidl/android/hardware/security/keymint/MacedPublicKey.h> |
Shawn Willden | 1d3f85e | 2020-12-09 14:18:44 -0700 | [diff] [blame] | 34 | |
Shawn Willden | 4315e13 | 2022-03-20 12:49:46 -0600 | [diff] [blame] | 35 | #include <keymint_support/attestation_record.h> |
Shawn Willden | 08a7e43 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 36 | #include <keymint_support/authorization_set.h> |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 37 | #include <keymint_support/openssl_utils.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 38 | |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 39 | namespace aidl::android::hardware::security::keymint { |
| 40 | |
| 41 | ::std::ostream& operator<<(::std::ostream& os, const AuthorizationSet& set); |
| 42 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 43 | inline bool operator==(const keymint::AuthorizationSet& a, const keymint::AuthorizationSet& b) { |
| 44 | return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin()); |
| 45 | } |
| 46 | |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 47 | namespace test { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 48 | |
| 49 | using ::android::sp; |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 50 | using Status = ::ndk::ScopedAStatus; |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 51 | using ::std::optional; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 52 | using ::std::shared_ptr; |
| 53 | using ::std::string; |
| 54 | using ::std::vector; |
| 55 | |
| 56 | constexpr uint64_t kOpHandleSentinel = 0xFFFFFFFFFFFFFFFF; |
| 57 | |
Subrahmanyaman | 50fcf7d | 2023-04-20 22:48:39 +0000 | [diff] [blame] | 58 | const string FEATURE_KEYSTORE_APP_ATTEST_KEY = "android.hardware.keystore.app_attest_key"; |
| 59 | const string FEATURE_STRONGBOX_KEYSTORE = "android.hardware.strongbox_keystore"; |
David Drysdale | 6c9bdb8 | 2024-01-23 09:32:04 +0000 | [diff] [blame] | 60 | const string FEATURE_HARDWARE_KEYSTORE = "android.hardware.hardware_keystore"; |
Subrahmanyaman | 50fcf7d | 2023-04-20 22:48:39 +0000 | [diff] [blame] | 61 | |
David Drysdale | 1b9febc | 2023-06-07 13:43:24 +0100 | [diff] [blame] | 62 | // RAII class to ensure that a keyblob is deleted regardless of how a test exits. |
| 63 | class KeyBlobDeleter { |
| 64 | public: |
| 65 | KeyBlobDeleter(const shared_ptr<IKeyMintDevice>& keymint, const vector<uint8_t>& key_blob) |
| 66 | : keymint_(keymint), key_blob_(key_blob) {} |
| 67 | ~KeyBlobDeleter(); |
| 68 | |
| 69 | private: |
| 70 | shared_ptr<IKeyMintDevice> keymint_; |
| 71 | vector<uint8_t> key_blob_; |
| 72 | }; |
| 73 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 74 | class KeyMintAidlTestBase : public ::testing::TestWithParam<string> { |
| 75 | public: |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 76 | struct KeyData { |
| 77 | vector<uint8_t> blob; |
| 78 | vector<KeyCharacteristics> characteristics; |
| 79 | }; |
| 80 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 81 | static bool arm_deleteAllKeys; |
| 82 | static bool dump_Attestations; |
| 83 | |
David Drysdale | 9f5c0c5 | 2022-11-03 15:10:16 +0000 | [diff] [blame] | 84 | // Directory to store/retrieve keyblobs, using subdirectories named for the |
| 85 | // KeyMint instance in question (e.g. "./default/", "./strongbox/"). |
| 86 | static std::string keyblob_dir; |
Tommy Chiu | 025f3c5 | 2023-05-15 06:23:44 +0000 | [diff] [blame] | 87 | // To specify if users expect an upgrade on the keyBlobs. |
| 88 | static std::optional<bool> expect_upgrade; |
David Drysdale | 9f5c0c5 | 2022-11-03 15:10:16 +0000 | [diff] [blame] | 89 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 90 | void SetUp() override; |
| 91 | void TearDown() override { |
| 92 | if (key_blob_.size()) { |
| 93 | CheckedDeleteKey(); |
| 94 | } |
| 95 | AbortIfNeeded(); |
| 96 | } |
| 97 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 98 | void InitializeKeyMint(std::shared_ptr<IKeyMintDevice> keyMint); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 99 | IKeyMintDevice& keyMint() { return *keymint_; } |
Prashant Patil | 2114dca | 2023-09-21 14:57:10 +0000 | [diff] [blame] | 100 | int32_t AidlVersion() const; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 101 | uint32_t os_version() { return os_version_; } |
| 102 | uint32_t os_patch_level() { return os_patch_level_; } |
David Drysdale | bb3d85e | 2021-04-13 11:15:51 +0100 | [diff] [blame] | 103 | uint32_t vendor_patch_level() { return vendor_patch_level_; } |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 104 | uint32_t boot_patch_level(const vector<KeyCharacteristics>& key_characteristics); |
| 105 | uint32_t boot_patch_level(); |
Prashant Patil | 88ad189 | 2022-03-15 16:31:02 +0000 | [diff] [blame] | 106 | bool isDeviceIdAttestationRequired(); |
Rajesh Nyamagoud | 5283f81 | 2023-01-06 00:27:56 +0000 | [diff] [blame] | 107 | bool isSecondImeiIdAttestationRequired(); |
Karuna Wadhera | 0c5b650 | 2024-07-09 20:21:44 +0000 | [diff] [blame] | 108 | std::optional<bool> isRkpOnly(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 109 | |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 110 | bool Curve25519Supported(); |
| 111 | |
Seth Moore | c5c52ce | 2024-04-07 15:26:33 -0700 | [diff] [blame] | 112 | ErrorCode GenerateKey(const AuthorizationSet& key_desc); |
| 113 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 114 | ErrorCode GenerateKey(const AuthorizationSet& key_desc, vector<uint8_t>* key_blob, |
Seth Moore | c5c52ce | 2024-04-07 15:26:33 -0700 | [diff] [blame] | 115 | vector<KeyCharacteristics>* key_characteristics); |
| 116 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 117 | ErrorCode GenerateKey(const AuthorizationSet& key_desc, |
| 118 | const optional<AttestationKey>& attest_key, vector<uint8_t>* key_blob, |
| 119 | vector<KeyCharacteristics>* key_characteristics, |
| 120 | vector<Certificate>* cert_chain); |
subrahmanyaman | 7d9bc46 | 2022-03-16 01:40:39 +0000 | [diff] [blame] | 121 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 122 | ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format, |
| 123 | const string& key_material, vector<uint8_t>* key_blob, |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 124 | vector<KeyCharacteristics>* key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 125 | ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format, |
| 126 | const string& key_material); |
| 127 | |
| 128 | ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key, |
| 129 | const AuthorizationSet& wrapping_key_desc, string masking_key, |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 130 | const AuthorizationSet& unwrapping_params, int64_t password_sid, |
| 131 | int64_t biometric_sid); |
| 132 | ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key, |
| 133 | const AuthorizationSet& wrapping_key_desc, string masking_key, |
| 134 | const AuthorizationSet& unwrapping_params) { |
| 135 | return ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, masking_key, |
| 136 | unwrapping_params, 0 /* password_sid */, 0 /* biometric_sid */); |
| 137 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 138 | |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 139 | ErrorCode GetCharacteristics(const vector<uint8_t>& key_blob, const vector<uint8_t>& app_id, |
| 140 | const vector<uint8_t>& app_data, |
| 141 | vector<KeyCharacteristics>* key_characteristics); |
| 142 | ErrorCode GetCharacteristics(const vector<uint8_t>& key_blob, |
| 143 | vector<KeyCharacteristics>* key_characteristics); |
| 144 | |
| 145 | void CheckCharacteristics(const vector<uint8_t>& key_blob, |
| 146 | const vector<KeyCharacteristics>& generate_characteristics); |
| 147 | void CheckAppIdCharacteristics(const vector<uint8_t>& key_blob, std::string_view app_id_string, |
| 148 | std::string_view app_data_string, |
| 149 | const vector<KeyCharacteristics>& generate_characteristics); |
| 150 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 151 | ErrorCode DeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false); |
| 152 | ErrorCode DeleteKey(bool keep_key_blob = false); |
| 153 | |
| 154 | ErrorCode DeleteAllKeys(); |
| 155 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 156 | ErrorCode DestroyAttestationIds(); |
| 157 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 158 | void CheckedDeleteKey(); |
| 159 | |
| 160 | ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob, |
| 161 | const AuthorizationSet& in_params, AuthorizationSet* out_params, |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 162 | std::shared_ptr<IKeyMintOperation>& op); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 163 | ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob, |
David Drysdale | 28fa931 | 2023-02-01 14:53:01 +0000 | [diff] [blame] | 164 | const AuthorizationSet& in_params, AuthorizationSet* out_params, |
| 165 | std::optional<HardwareAuthToken> hat = std::nullopt); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 166 | ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params, |
| 167 | AuthorizationSet* out_params); |
| 168 | ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params); |
| 169 | |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 170 | ErrorCode UpdateAad(const string& input); |
| 171 | ErrorCode Update(const string& input, string* output); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 172 | |
David Drysdale | 28fa931 | 2023-02-01 14:53:01 +0000 | [diff] [blame] | 173 | ErrorCode Finish(const string& message, const string& signature, string* output, |
| 174 | std::optional<HardwareAuthToken> hat = std::nullopt, |
| 175 | std::optional<secureclock::TimeStampToken> time_token = std::nullopt); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 176 | ErrorCode Finish(const string& message, string* output) { |
| 177 | return Finish(message, {} /* signature */, output); |
| 178 | } |
| 179 | ErrorCode Finish(string* output) { return Finish({} /* message */, output); } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 180 | |
| 181 | ErrorCode Abort(); |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 182 | ErrorCode Abort(const shared_ptr<IKeyMintOperation>& op); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 183 | void AbortIfNeeded(); |
| 184 | |
| 185 | string ProcessMessage(const vector<uint8_t>& key_blob, KeyPurpose operation, |
| 186 | const string& message, const AuthorizationSet& in_params, |
| 187 | AuthorizationSet* out_params); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 188 | std::tuple<ErrorCode, std::string /* processedMessage */> ProcessMessage( |
| 189 | const vector<uint8_t>& key_blob, KeyPurpose operation, const std::string& message, |
| 190 | const AuthorizationSet& in_params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 191 | string SignMessage(const vector<uint8_t>& key_blob, const string& message, |
| 192 | const AuthorizationSet& params); |
| 193 | string SignMessage(const string& message, const AuthorizationSet& params); |
| 194 | |
| 195 | string MacMessage(const string& message, Digest digest, size_t mac_length); |
| 196 | |
anil.hiranniah | 19a4ca1 | 2022-03-03 17:39:30 +0530 | [diff] [blame] | 197 | void CheckAesIncrementalEncryptOperation(BlockMode block_mode, int message_size); |
| 198 | |
Prashant Patil | dd5f7f0 | 2022-07-06 18:58:07 +0000 | [diff] [blame] | 199 | void AesCheckEncryptOneByteAtATime(const string& key, BlockMode block_mode, |
| 200 | PaddingMode padding_mode, const string& iv, |
| 201 | const string& plaintext, const string& exp_cipher_text); |
| 202 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 203 | void CheckHmacTestVector(const string& key, const string& message, Digest digest, |
| 204 | const string& expected_mac); |
| 205 | |
| 206 | void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message, |
| 207 | const string& expected_ciphertext); |
| 208 | |
| 209 | void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode, |
| 210 | PaddingMode padding_mode, const string& key, const string& iv, |
| 211 | const string& input, const string& expected_output); |
| 212 | |
| 213 | void VerifyMessage(const vector<uint8_t>& key_blob, const string& message, |
| 214 | const string& signature, const AuthorizationSet& params); |
| 215 | void VerifyMessage(const string& message, const string& signature, |
| 216 | const AuthorizationSet& params); |
David Drysdale | 9f5c0c5 | 2022-11-03 15:10:16 +0000 | [diff] [blame] | 217 | void LocalVerifyMessage(const vector<uint8_t>& der_cert, const string& message, |
| 218 | const string& signature, const AuthorizationSet& params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 219 | void LocalVerifyMessage(const string& message, const string& signature, |
| 220 | const AuthorizationSet& params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 221 | |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 222 | string LocalRsaEncryptMessage(const string& message, const AuthorizationSet& params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 223 | string EncryptMessage(const vector<uint8_t>& key_blob, const string& message, |
| 224 | const AuthorizationSet& in_params, AuthorizationSet* out_params); |
| 225 | string EncryptMessage(const string& message, const AuthorizationSet& params, |
| 226 | AuthorizationSet* out_params); |
| 227 | string EncryptMessage(const string& message, const AuthorizationSet& params); |
| 228 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding); |
| 229 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 230 | vector<uint8_t>* iv_out); |
| 231 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 232 | const vector<uint8_t>& iv_in); |
| 233 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 234 | uint8_t mac_length_bits, const vector<uint8_t>& iv_in); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 235 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 236 | uint8_t mac_length_bits); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 237 | |
| 238 | string DecryptMessage(const vector<uint8_t>& key_blob, const string& ciphertext, |
| 239 | const AuthorizationSet& params); |
| 240 | string DecryptMessage(const string& ciphertext, const AuthorizationSet& params); |
| 241 | string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode, |
| 242 | const vector<uint8_t>& iv); |
| 243 | |
| 244 | std::pair<ErrorCode, vector<uint8_t>> UpgradeKey(const vector<uint8_t>& key_blob); |
| 245 | |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 246 | template <typename TagType> |
| 247 | std::tuple<KeyData /* aesKey */, KeyData /* hmacKey */, KeyData /* rsaKey */, |
| 248 | KeyData /* ecdsaKey */> |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 249 | CreateTestKeys( |
| 250 | TagType tagToTest, ErrorCode expectedReturn, |
| 251 | std::function<void(AuthorizationSetBuilder*)> tagModifier = |
| 252 | [](AuthorizationSetBuilder*) {}) { |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 253 | /* AES */ |
| 254 | KeyData aesKeyData; |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 255 | AuthorizationSetBuilder aesBuilder = AuthorizationSetBuilder() |
| 256 | .AesEncryptionKey(128) |
| 257 | .Authorization(tagToTest) |
| 258 | .BlockMode(BlockMode::ECB) |
| 259 | .Padding(PaddingMode::NONE) |
| 260 | .Authorization(TAG_NO_AUTH_REQUIRED); |
| 261 | tagModifier(&aesBuilder); |
| 262 | ErrorCode errorCode = |
| 263 | GenerateKey(aesBuilder, &aesKeyData.blob, &aesKeyData.characteristics); |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 264 | EXPECT_EQ(expectedReturn, errorCode); |
| 265 | |
| 266 | /* HMAC */ |
| 267 | KeyData hmacKeyData; |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 268 | AuthorizationSetBuilder hmacBuilder = AuthorizationSetBuilder() |
| 269 | .HmacKey(128) |
| 270 | .Authorization(tagToTest) |
| 271 | .Digest(Digest::SHA_2_256) |
| 272 | .Authorization(TAG_MIN_MAC_LENGTH, 128) |
| 273 | .Authorization(TAG_NO_AUTH_REQUIRED); |
| 274 | tagModifier(&hmacBuilder); |
| 275 | errorCode = GenerateKey(hmacBuilder, &hmacKeyData.blob, &hmacKeyData.characteristics); |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 276 | EXPECT_EQ(expectedReturn, errorCode); |
| 277 | |
| 278 | /* RSA */ |
| 279 | KeyData rsaKeyData; |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 280 | AuthorizationSetBuilder rsaBuilder = AuthorizationSetBuilder() |
| 281 | .RsaSigningKey(2048, 65537) |
| 282 | .Authorization(tagToTest) |
| 283 | .Digest(Digest::NONE) |
| 284 | .Padding(PaddingMode::NONE) |
| 285 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 286 | .SetDefaultValidity(); |
| 287 | tagModifier(&rsaBuilder); |
| 288 | errorCode = GenerateKey(rsaBuilder, &rsaKeyData.blob, &rsaKeyData.characteristics); |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 289 | if (!(SecLevel() == SecurityLevel::STRONGBOX && |
| 290 | ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED == errorCode)) { |
| 291 | EXPECT_EQ(expectedReturn, errorCode); |
| 292 | } |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 293 | |
| 294 | /* ECDSA */ |
| 295 | KeyData ecdsaKeyData; |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 296 | AuthorizationSetBuilder ecdsaBuilder = AuthorizationSetBuilder() |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 297 | .EcdsaSigningKey(EcCurve::P_256) |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 298 | .Authorization(tagToTest) |
| 299 | .Digest(Digest::SHA_2_256) |
| 300 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 301 | .SetDefaultValidity(); |
| 302 | tagModifier(&ecdsaBuilder); |
| 303 | errorCode = GenerateKey(ecdsaBuilder, &ecdsaKeyData.blob, &ecdsaKeyData.characteristics); |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 304 | if (!(SecLevel() == SecurityLevel::STRONGBOX && |
| 305 | ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED == errorCode)) { |
| 306 | EXPECT_EQ(expectedReturn, errorCode); |
| 307 | } |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 308 | return {aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData}; |
| 309 | } |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 310 | bool IsSecure() const { return securityLevel_ != SecurityLevel::SOFTWARE; } |
| 311 | SecurityLevel SecLevel() const { return securityLevel_; } |
Seth Moore | a12ac74 | 2023-03-03 13:40:30 -0800 | [diff] [blame] | 312 | bool IsRkpSupportRequired() const; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 313 | |
| 314 | vector<uint32_t> ValidKeySizes(Algorithm algorithm); |
| 315 | vector<uint32_t> InvalidKeySizes(Algorithm algorithm); |
| 316 | |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 317 | vector<BlockMode> ValidBlockModes(Algorithm algorithm); |
| 318 | vector<PaddingMode> ValidPaddingModes(Algorithm algorithm, BlockMode blockMode); |
| 319 | vector<PaddingMode> InvalidPaddingModes(Algorithm algorithm, BlockMode blockMode); |
| 320 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 321 | vector<EcCurve> ValidCurves(); |
| 322 | vector<EcCurve> InvalidCurves(); |
| 323 | |
| 324 | vector<Digest> ValidDigests(bool withNone, bool withMD5); |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 325 | vector<uint64_t> ValidExponents(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 326 | |
| 327 | static vector<string> build_params() { |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 328 | auto params = ::android::getAidlHalInstanceNames(IKeyMintDevice::descriptor); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 329 | return params; |
| 330 | } |
| 331 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 332 | std::shared_ptr<IKeyMintOperation> op_; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 333 | vector<Certificate> cert_chain_; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 334 | vector<uint8_t> key_blob_; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 335 | vector<KeyCharacteristics> key_characteristics_; |
| 336 | |
| 337 | const vector<KeyParameter>& SecLevelAuthorizations( |
| 338 | const vector<KeyCharacteristics>& key_characteristics); |
| 339 | inline const vector<KeyParameter>& SecLevelAuthorizations() { |
| 340 | return SecLevelAuthorizations(key_characteristics_); |
| 341 | } |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 342 | const vector<KeyParameter>& SecLevelAuthorizations( |
| 343 | const vector<KeyCharacteristics>& key_characteristics, SecurityLevel securityLevel); |
| 344 | |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 345 | ErrorCode UseAesKey(const vector<uint8_t>& aesKeyBlob); |
| 346 | ErrorCode UseHmacKey(const vector<uint8_t>& hmacKeyBlob); |
| 347 | ErrorCode UseRsaKey(const vector<uint8_t>& rsaKeyBlob); |
| 348 | ErrorCode UseEcdsaKey(const vector<uint8_t>& ecdsaKeyBlob); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 349 | |
Subrahmanyaman | 50fcf7d | 2023-04-20 22:48:39 +0000 | [diff] [blame] | 350 | ErrorCode GenerateAttestKey(const AuthorizationSet& key_desc, |
| 351 | const optional<AttestationKey>& attest_key, |
| 352 | vector<uint8_t>* key_blob, |
| 353 | vector<KeyCharacteristics>* key_characteristics, |
| 354 | vector<Certificate>* cert_chain); |
| 355 | |
| 356 | bool is_attest_key_feature_disabled(void) const; |
| 357 | bool is_strongbox_enabled(void) const; |
| 358 | bool is_chipset_allowed_km4_strongbox(void) const; |
David Drysdale | c3de1ca | 2023-05-09 08:10:36 +0100 | [diff] [blame] | 359 | bool shouldSkipAttestKeyTest(void) const; |
Subrahmanyaman | 50fcf7d | 2023-04-20 22:48:39 +0000 | [diff] [blame] | 360 | |
Prashant Patil | 2114dca | 2023-09-21 14:57:10 +0000 | [diff] [blame] | 361 | void assert_mgf_digests_present_or_not_in_key_characteristics( |
| 362 | const vector<KeyCharacteristics>& key_characteristics, |
| 363 | std::vector<android::hardware::security::keymint::Digest>& expected_mgf_digests, |
| 364 | bool is_mgf_digest_expected) const; |
| 365 | |
| 366 | void assert_mgf_digests_present_or_not_in_key_characteristics( |
| 367 | std::vector<android::hardware::security::keymint::Digest>& expected_mgf_digests, |
| 368 | bool is_mgf_digest_expected) const; |
| 369 | |
Shawn Willden | d659c7c | 2021-02-19 14:51:51 -0700 | [diff] [blame] | 370 | protected: |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 371 | std::shared_ptr<IKeyMintDevice> keymint_; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 372 | uint32_t os_version_; |
| 373 | uint32_t os_patch_level_; |
David Drysdale | bb3d85e | 2021-04-13 11:15:51 +0100 | [diff] [blame] | 374 | uint32_t vendor_patch_level_; |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 375 | bool timestamp_token_required_; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 376 | |
| 377 | SecurityLevel securityLevel_; |
| 378 | string name_; |
| 379 | string author_; |
David Drysdale | 4cbe215 | 2023-03-07 14:44:38 +0000 | [diff] [blame] | 380 | int64_t challenge_; |
Prashant Patil | dd5f7f0 | 2022-07-06 18:58:07 +0000 | [diff] [blame] | 381 | |
| 382 | private: |
| 383 | void CheckEncryptOneByteAtATime(BlockMode block_mode, const int block_size, |
| 384 | PaddingMode padding_mode, const string& iv, |
| 385 | const string& plaintext, const string& exp_cipher_text); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 386 | }; |
| 387 | |
David Drysdale | 7b05efd | 2024-10-10 18:11:25 +0100 | [diff] [blame] | 388 | // If the given string is non-empty, add it to the tag set under the given tag ID. |
| 389 | template <Tag tag> |
| 390 | void add_tag(AuthorizationSetBuilder* tags, TypedTag<TagType::BYTES, tag> ttag, |
| 391 | const std::string& prop_value) { |
| 392 | if (!prop_value.empty()) { |
| 393 | tags->Authorization(ttag, prop_value.data(), prop_value.size()); |
| 394 | } |
| 395 | } |
| 396 | |
David Drysdale | a676c3b | 2021-06-14 14:46:02 +0100 | [diff] [blame] | 397 | // If the given property is available, add it to the tag set under the given tag ID. |
| 398 | template <Tag tag> |
| 399 | void add_tag_from_prop(AuthorizationSetBuilder* tags, TypedTag<TagType::BYTES, tag> ttag, |
| 400 | const char* prop) { |
David Drysdale | 7b05efd | 2024-10-10 18:11:25 +0100 | [diff] [blame] | 401 | add_tag(tags, ttag, ::android::base::GetProperty(prop, /* default= */ "")); |
David Drysdale | a676c3b | 2021-06-14 14:46:02 +0100 | [diff] [blame] | 402 | } |
| 403 | |
Shawn Willden | 22fb9c1 | 2022-06-02 14:04:33 -0600 | [diff] [blame] | 404 | // Return the VSR API level for this device. |
| 405 | int get_vsr_api_level(); |
| 406 | |
David Drysdale | 555ba00 | 2022-05-03 18:48:57 +0100 | [diff] [blame] | 407 | // Indicate whether the test is running on a GSI image. |
| 408 | bool is_gsi_image(); |
| 409 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 410 | vector<uint8_t> build_serial_blob(const uint64_t serial_int); |
| 411 | void verify_subject(const X509* cert, const string& subject, bool self_signed); |
| 412 | void verify_serial(X509* cert, const uint64_t expected_serial); |
| 413 | void verify_subject_and_serial(const Certificate& certificate, // |
| 414 | const uint64_t expected_serial, // |
| 415 | const string& subject, bool self_signed); |
Shawn Willden | 4315e13 | 2022-03-20 12:49:46 -0600 | [diff] [blame] | 416 | void verify_root_of_trust(const vector<uint8_t>& verified_boot_key, // |
| 417 | bool device_locked, // |
| 418 | VerifiedBoot verified_boot_state, // |
| 419 | const vector<uint8_t>& verified_boot_hash); |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 420 | bool verify_attestation_record(int aidl_version, // |
| 421 | const string& challenge, // |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 422 | const string& app_id, // |
| 423 | AuthorizationSet expected_sw_enforced, // |
| 424 | AuthorizationSet expected_hw_enforced, // |
| 425 | SecurityLevel security_level, |
David Drysdale | 565ccc7 | 2021-10-11 12:49:50 +0100 | [diff] [blame] | 426 | const vector<uint8_t>& attestation_cert, |
| 427 | vector<uint8_t>* unique_id = nullptr); |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 428 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 429 | string bin2hex(const vector<uint8_t>& data); |
| 430 | X509_Ptr parse_cert_blob(const vector<uint8_t>& blob); |
Tri Vo | ec50ee1 | 2023-02-14 16:29:53 -0800 | [diff] [blame] | 431 | ASN1_OCTET_STRING* get_attestation_record(X509* certificate); |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 432 | vector<uint8_t> make_name_from_str(const string& name); |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 433 | void check_maced_pubkey(const MacedPublicKey& macedPubKey, bool testMode, |
| 434 | vector<uint8_t>* payload_value); |
| 435 | void p256_pub_key(const vector<uint8_t>& coseKeyData, EVP_PKEY_Ptr* signingKey); |
David Drysdale | f42238c | 2023-06-15 09:41:05 +0100 | [diff] [blame] | 436 | void device_id_attestation_check_acceptable_error(Tag tag, const ErrorCode& result); |
David Drysdale | 3d2ba0a | 2023-01-11 13:27:26 +0000 | [diff] [blame] | 437 | bool check_feature(const std::string& name); |
David Drysdale | 6c9bdb8 | 2024-01-23 09:32:04 +0000 | [diff] [blame] | 438 | std::optional<int32_t> keymint_feature_value(bool strongbox); |
David Drysdale | ef1123b | 2024-05-28 15:23:08 +0100 | [diff] [blame] | 439 | std::string get_imei(int slot); |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 440 | |
David Drysdale | 7b05efd | 2024-10-10 18:11:25 +0100 | [diff] [blame] | 441 | // Retrieve a device ID property value, to match what is expected in attestations. |
| 442 | std::optional<std::string> get_attestation_id(const char* prop); |
| 443 | |
| 444 | // Add the appropriate attestation device ID tag value to the provided `AuthorizationSetBuilder`, |
| 445 | // if found. |
| 446 | template <Tag tag> |
| 447 | void add_attestation_id(AuthorizationSetBuilder* attestation_id_tags, |
| 448 | TypedTag<TagType::BYTES, tag> tag_type, const char* prop) { |
| 449 | auto prop_value = get_attestation_id(prop); |
| 450 | if (prop_value.has_value()) { |
| 451 | add_tag(attestation_id_tags, tag_type, prop_value.value()); |
| 452 | } |
| 453 | } |
| 454 | |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 455 | AuthorizationSet HwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics); |
| 456 | AuthorizationSet SwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics); |
Eran Messeri | 03d7a1a | 2021-07-06 12:07:57 +0100 | [diff] [blame] | 457 | ::testing::AssertionResult ChainSignaturesAreValid(const vector<Certificate>& chain, |
| 458 | bool strict_issuer_check = true); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 459 | |
David Drysdale | 1b9febc | 2023-06-07 13:43:24 +0100 | [diff] [blame] | 460 | ErrorCode GetReturnErrorCode(const Status& result); |
| 461 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 462 | #define INSTANTIATE_KEYMINT_AIDL_TEST(name) \ |
| 463 | INSTANTIATE_TEST_SUITE_P(PerInstance, name, \ |
| 464 | testing::ValuesIn(KeyMintAidlTestBase::build_params()), \ |
Shawn Willden | 7e71f1e | 2021-04-01 16:44:22 -0600 | [diff] [blame] | 465 | ::android::PrintInstanceNameToString); \ |
| 466 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(name); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 467 | |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 468 | } // namespace test |
| 469 | |
| 470 | } // namespace aidl::android::hardware::security::keymint |