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