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 | 08a7e43 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 34 | #include <keymint_support/authorization_set.h> |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 35 | #include <keymint_support/openssl_utils.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 36 | |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 37 | namespace aidl::android::hardware::security::keymint { |
| 38 | |
| 39 | ::std::ostream& operator<<(::std::ostream& os, const AuthorizationSet& set); |
| 40 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 41 | inline bool operator==(const keymint::AuthorizationSet& a, const keymint::AuthorizationSet& b) { |
| 42 | return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin()); |
| 43 | } |
| 44 | |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 45 | namespace test { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 46 | |
| 47 | using ::android::sp; |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 48 | using Status = ::ndk::ScopedAStatus; |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 49 | using ::std::optional; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 50 | using ::std::shared_ptr; |
| 51 | using ::std::string; |
| 52 | using ::std::vector; |
| 53 | |
| 54 | constexpr uint64_t kOpHandleSentinel = 0xFFFFFFFFFFFFFFFF; |
| 55 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 56 | class KeyMintAidlTestBase : public ::testing::TestWithParam<string> { |
| 57 | public: |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 58 | struct KeyData { |
| 59 | vector<uint8_t> blob; |
| 60 | vector<KeyCharacteristics> characteristics; |
| 61 | }; |
| 62 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 63 | static bool arm_deleteAllKeys; |
| 64 | static bool dump_Attestations; |
| 65 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 66 | void SetUp() override; |
| 67 | void TearDown() override { |
| 68 | if (key_blob_.size()) { |
| 69 | CheckedDeleteKey(); |
| 70 | } |
| 71 | AbortIfNeeded(); |
| 72 | } |
| 73 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 74 | void InitializeKeyMint(std::shared_ptr<IKeyMintDevice> keyMint); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 75 | IKeyMintDevice& keyMint() { return *keymint_; } |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 76 | int32_t AidlVersion(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 77 | uint32_t os_version() { return os_version_; } |
| 78 | uint32_t os_patch_level() { return os_patch_level_; } |
David Drysdale | bb3d85e | 2021-04-13 11:15:51 +0100 | [diff] [blame] | 79 | uint32_t vendor_patch_level() { return vendor_patch_level_; } |
David Drysdale | 37af4b3 | 2021-05-14 16:46:59 +0100 | [diff] [blame] | 80 | uint32_t boot_patch_level(const vector<KeyCharacteristics>& key_characteristics); |
| 81 | uint32_t boot_patch_level(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 82 | |
David Drysdale | 42fe189 | 2021-10-14 14:43:46 +0100 | [diff] [blame] | 83 | bool Curve25519Supported(); |
| 84 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 85 | ErrorCode GetReturnErrorCode(const Status& result); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 86 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 87 | ErrorCode GenerateKey(const AuthorizationSet& key_desc, vector<uint8_t>* key_blob, |
| 88 | vector<KeyCharacteristics>* key_characteristics) { |
| 89 | return GenerateKey(key_desc, std::nullopt /* attest_key */, key_blob, key_characteristics, |
| 90 | &cert_chain_); |
| 91 | } |
| 92 | ErrorCode GenerateKey(const AuthorizationSet& key_desc, |
| 93 | const optional<AttestationKey>& attest_key, vector<uint8_t>* key_blob, |
| 94 | vector<KeyCharacteristics>* key_characteristics, |
| 95 | vector<Certificate>* cert_chain); |
| 96 | ErrorCode GenerateKey(const AuthorizationSet& key_desc, |
| 97 | const optional<AttestationKey>& attest_key = std::nullopt); |
| 98 | |
subrahmanyaman | 7d9bc46 | 2022-03-16 01:40:39 +0000 | [diff] [blame] | 99 | // Generate key for implementations which do not support factory attestation. |
| 100 | ErrorCode GenerateKeyWithSelfSignedAttestKey(const AuthorizationSet& attest_key_desc, |
| 101 | const AuthorizationSet& key_desc, |
| 102 | vector<uint8_t>* key_blob, |
| 103 | vector<KeyCharacteristics>* key_characteristics, |
| 104 | vector<Certificate>* cert_chain); |
| 105 | |
| 106 | ErrorCode GenerateKeyWithSelfSignedAttestKey(const AuthorizationSet& attest_key_desc, |
| 107 | const AuthorizationSet& key_desc, |
| 108 | vector<uint8_t>* key_blob, |
| 109 | vector<KeyCharacteristics>* key_characteristics) { |
| 110 | return GenerateKeyWithSelfSignedAttestKey(attest_key_desc, key_desc, key_blob, |
| 111 | key_characteristics, &cert_chain_); |
| 112 | } |
| 113 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 114 | ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format, |
| 115 | const string& key_material, vector<uint8_t>* key_blob, |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 116 | vector<KeyCharacteristics>* key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 117 | ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format, |
| 118 | const string& key_material); |
| 119 | |
| 120 | ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key, |
| 121 | const AuthorizationSet& wrapping_key_desc, string masking_key, |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 122 | const AuthorizationSet& unwrapping_params, int64_t password_sid, |
| 123 | int64_t biometric_sid); |
| 124 | ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key, |
| 125 | const AuthorizationSet& wrapping_key_desc, string masking_key, |
| 126 | const AuthorizationSet& unwrapping_params) { |
| 127 | return ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, masking_key, |
| 128 | unwrapping_params, 0 /* password_sid */, 0 /* biometric_sid */); |
| 129 | } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 130 | |
David Drysdale | 300b555 | 2021-05-20 12:05:26 +0100 | [diff] [blame] | 131 | ErrorCode GetCharacteristics(const vector<uint8_t>& key_blob, const vector<uint8_t>& app_id, |
| 132 | const vector<uint8_t>& app_data, |
| 133 | vector<KeyCharacteristics>* key_characteristics); |
| 134 | ErrorCode GetCharacteristics(const vector<uint8_t>& key_blob, |
| 135 | vector<KeyCharacteristics>* key_characteristics); |
| 136 | |
| 137 | void CheckCharacteristics(const vector<uint8_t>& key_blob, |
| 138 | const vector<KeyCharacteristics>& generate_characteristics); |
| 139 | void CheckAppIdCharacteristics(const vector<uint8_t>& key_blob, std::string_view app_id_string, |
| 140 | std::string_view app_data_string, |
| 141 | const vector<KeyCharacteristics>& generate_characteristics); |
| 142 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 143 | ErrorCode DeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false); |
| 144 | ErrorCode DeleteKey(bool keep_key_blob = false); |
| 145 | |
| 146 | ErrorCode DeleteAllKeys(); |
| 147 | |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 148 | ErrorCode DestroyAttestationIds(); |
| 149 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 150 | void CheckedDeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false); |
| 151 | void CheckedDeleteKey(); |
| 152 | |
| 153 | ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob, |
| 154 | const AuthorizationSet& in_params, AuthorizationSet* out_params, |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 155 | std::shared_ptr<IKeyMintOperation>& op); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 156 | ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob, |
| 157 | const AuthorizationSet& in_params, AuthorizationSet* out_params); |
| 158 | ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params, |
| 159 | AuthorizationSet* out_params); |
| 160 | ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params); |
| 161 | |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 162 | ErrorCode UpdateAad(const string& input); |
| 163 | ErrorCode Update(const string& input, string* output); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 164 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 165 | ErrorCode Finish(const string& message, const string& signature, string* output); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 166 | ErrorCode Finish(const string& message, string* output) { |
| 167 | return Finish(message, {} /* signature */, output); |
| 168 | } |
| 169 | ErrorCode Finish(string* output) { return Finish({} /* message */, output); } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 170 | |
| 171 | ErrorCode Abort(); |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 172 | ErrorCode Abort(const shared_ptr<IKeyMintOperation>& op); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 173 | void AbortIfNeeded(); |
| 174 | |
| 175 | string ProcessMessage(const vector<uint8_t>& key_blob, KeyPurpose operation, |
| 176 | const string& message, const AuthorizationSet& in_params, |
| 177 | AuthorizationSet* out_params); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame] | 178 | std::tuple<ErrorCode, std::string /* processedMessage */> ProcessMessage( |
| 179 | const vector<uint8_t>& key_blob, KeyPurpose operation, const std::string& message, |
| 180 | const AuthorizationSet& in_params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 181 | string SignMessage(const vector<uint8_t>& key_blob, const string& message, |
| 182 | const AuthorizationSet& params); |
| 183 | string SignMessage(const string& message, const AuthorizationSet& params); |
| 184 | |
| 185 | string MacMessage(const string& message, Digest digest, size_t mac_length); |
| 186 | |
anil.hiranniah | 19a4ca1 | 2022-03-03 17:39:30 +0530 | [diff] [blame] | 187 | void CheckAesIncrementalEncryptOperation(BlockMode block_mode, int message_size); |
| 188 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 189 | void CheckHmacTestVector(const string& key, const string& message, Digest digest, |
| 190 | const string& expected_mac); |
| 191 | |
| 192 | void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message, |
| 193 | const string& expected_ciphertext); |
| 194 | |
| 195 | void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode, |
| 196 | PaddingMode padding_mode, const string& key, const string& iv, |
| 197 | const string& input, const string& expected_output); |
| 198 | |
| 199 | void VerifyMessage(const vector<uint8_t>& key_blob, const string& message, |
| 200 | const string& signature, const AuthorizationSet& params); |
| 201 | void VerifyMessage(const string& message, const string& signature, |
| 202 | const AuthorizationSet& params); |
David Drysdale | df8f52e | 2021-05-06 08:10:58 +0100 | [diff] [blame] | 203 | void LocalVerifyMessage(const string& message, const string& signature, |
| 204 | const AuthorizationSet& params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 205 | |
David Drysdale | 59cae64 | 2021-05-12 13:52:03 +0100 | [diff] [blame] | 206 | string LocalRsaEncryptMessage(const string& message, const AuthorizationSet& params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 207 | string EncryptMessage(const vector<uint8_t>& key_blob, const string& message, |
| 208 | const AuthorizationSet& in_params, AuthorizationSet* out_params); |
| 209 | string EncryptMessage(const string& message, const AuthorizationSet& params, |
| 210 | AuthorizationSet* out_params); |
| 211 | string EncryptMessage(const string& message, const AuthorizationSet& params); |
| 212 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding); |
| 213 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 214 | vector<uint8_t>* iv_out); |
| 215 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 216 | const vector<uint8_t>& iv_in); |
| 217 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 218 | uint8_t mac_length_bits, const vector<uint8_t>& iv_in); |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 219 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 220 | uint8_t mac_length_bits); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 221 | |
| 222 | string DecryptMessage(const vector<uint8_t>& key_blob, const string& ciphertext, |
| 223 | const AuthorizationSet& params); |
| 224 | string DecryptMessage(const string& ciphertext, const AuthorizationSet& params); |
| 225 | string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode, |
| 226 | const vector<uint8_t>& iv); |
| 227 | |
| 228 | std::pair<ErrorCode, vector<uint8_t>> UpgradeKey(const vector<uint8_t>& key_blob); |
| 229 | |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 230 | template <typename TagType> |
| 231 | std::tuple<KeyData /* aesKey */, KeyData /* hmacKey */, KeyData /* rsaKey */, |
| 232 | KeyData /* ecdsaKey */> |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 233 | CreateTestKeys( |
| 234 | TagType tagToTest, ErrorCode expectedReturn, |
| 235 | std::function<void(AuthorizationSetBuilder*)> tagModifier = |
| 236 | [](AuthorizationSetBuilder*) {}) { |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 237 | /* AES */ |
| 238 | KeyData aesKeyData; |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 239 | AuthorizationSetBuilder aesBuilder = AuthorizationSetBuilder() |
| 240 | .AesEncryptionKey(128) |
| 241 | .Authorization(tagToTest) |
| 242 | .BlockMode(BlockMode::ECB) |
| 243 | .Padding(PaddingMode::NONE) |
| 244 | .Authorization(TAG_NO_AUTH_REQUIRED); |
| 245 | tagModifier(&aesBuilder); |
| 246 | ErrorCode errorCode = |
| 247 | GenerateKey(aesBuilder, &aesKeyData.blob, &aesKeyData.characteristics); |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 248 | EXPECT_EQ(expectedReturn, errorCode); |
| 249 | |
| 250 | /* HMAC */ |
| 251 | KeyData hmacKeyData; |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 252 | AuthorizationSetBuilder hmacBuilder = AuthorizationSetBuilder() |
| 253 | .HmacKey(128) |
| 254 | .Authorization(tagToTest) |
| 255 | .Digest(Digest::SHA_2_256) |
| 256 | .Authorization(TAG_MIN_MAC_LENGTH, 128) |
| 257 | .Authorization(TAG_NO_AUTH_REQUIRED); |
| 258 | tagModifier(&hmacBuilder); |
| 259 | errorCode = GenerateKey(hmacBuilder, &hmacKeyData.blob, &hmacKeyData.characteristics); |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 260 | EXPECT_EQ(expectedReturn, errorCode); |
| 261 | |
| 262 | /* RSA */ |
| 263 | KeyData rsaKeyData; |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 264 | AuthorizationSetBuilder rsaBuilder = AuthorizationSetBuilder() |
| 265 | .RsaSigningKey(2048, 65537) |
| 266 | .Authorization(tagToTest) |
| 267 | .Digest(Digest::NONE) |
| 268 | .Padding(PaddingMode::NONE) |
| 269 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 270 | .SetDefaultValidity(); |
| 271 | tagModifier(&rsaBuilder); |
| 272 | errorCode = GenerateKey(rsaBuilder, &rsaKeyData.blob, &rsaKeyData.characteristics); |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 273 | if (!(SecLevel() == SecurityLevel::STRONGBOX && |
| 274 | ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED == errorCode)) { |
| 275 | EXPECT_EQ(expectedReturn, errorCode); |
| 276 | } |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 277 | |
| 278 | /* ECDSA */ |
| 279 | KeyData ecdsaKeyData; |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 280 | AuthorizationSetBuilder ecdsaBuilder = AuthorizationSetBuilder() |
David Drysdale | df09e54 | 2021-06-08 15:46:11 +0100 | [diff] [blame] | 281 | .EcdsaSigningKey(EcCurve::P_256) |
David Drysdale | adfe611 | 2021-05-27 12:00:53 +0100 | [diff] [blame] | 282 | .Authorization(tagToTest) |
| 283 | .Digest(Digest::SHA_2_256) |
| 284 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 285 | .SetDefaultValidity(); |
| 286 | tagModifier(&ecdsaBuilder); |
| 287 | errorCode = GenerateKey(ecdsaBuilder, &ecdsaKeyData.blob, &ecdsaKeyData.characteristics); |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 288 | if (!(SecLevel() == SecurityLevel::STRONGBOX && |
| 289 | ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED == errorCode)) { |
| 290 | EXPECT_EQ(expectedReturn, errorCode); |
| 291 | } |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 292 | return {aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData}; |
| 293 | } |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 294 | bool IsSecure() const { return securityLevel_ != SecurityLevel::SOFTWARE; } |
| 295 | SecurityLevel SecLevel() const { return securityLevel_; } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 296 | |
| 297 | vector<uint32_t> ValidKeySizes(Algorithm algorithm); |
| 298 | vector<uint32_t> InvalidKeySizes(Algorithm algorithm); |
| 299 | |
David Drysdale | 7de9feb | 2021-03-05 14:56:19 +0000 | [diff] [blame] | 300 | vector<BlockMode> ValidBlockModes(Algorithm algorithm); |
| 301 | vector<PaddingMode> ValidPaddingModes(Algorithm algorithm, BlockMode blockMode); |
| 302 | vector<PaddingMode> InvalidPaddingModes(Algorithm algorithm, BlockMode blockMode); |
| 303 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 304 | vector<EcCurve> ValidCurves(); |
| 305 | vector<EcCurve> InvalidCurves(); |
| 306 | |
| 307 | vector<Digest> ValidDigests(bool withNone, bool withMD5); |
subrahmanyaman | 0564249 | 2022-02-05 07:10:56 +0000 | [diff] [blame] | 308 | vector<uint64_t> ValidExponents(); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 309 | |
| 310 | static vector<string> build_params() { |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 311 | auto params = ::android::getAidlHalInstanceNames(IKeyMintDevice::descriptor); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 312 | return params; |
| 313 | } |
| 314 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 315 | std::shared_ptr<IKeyMintOperation> op_; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 316 | vector<Certificate> cert_chain_; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 317 | vector<uint8_t> key_blob_; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 318 | vector<KeyCharacteristics> key_characteristics_; |
| 319 | |
| 320 | const vector<KeyParameter>& SecLevelAuthorizations( |
| 321 | const vector<KeyCharacteristics>& key_characteristics); |
| 322 | inline const vector<KeyParameter>& SecLevelAuthorizations() { |
| 323 | return SecLevelAuthorizations(key_characteristics_); |
| 324 | } |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 325 | const vector<KeyParameter>& SecLevelAuthorizations( |
| 326 | const vector<KeyCharacteristics>& key_characteristics, SecurityLevel securityLevel); |
| 327 | |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 328 | ErrorCode UseAesKey(const vector<uint8_t>& aesKeyBlob); |
| 329 | ErrorCode UseHmacKey(const vector<uint8_t>& hmacKeyBlob); |
| 330 | ErrorCode UseRsaKey(const vector<uint8_t>& rsaKeyBlob); |
| 331 | ErrorCode UseEcdsaKey(const vector<uint8_t>& ecdsaKeyBlob); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 332 | |
Shawn Willden | d659c7c | 2021-02-19 14:51:51 -0700 | [diff] [blame] | 333 | protected: |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 334 | std::shared_ptr<IKeyMintDevice> keymint_; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 335 | uint32_t os_version_; |
| 336 | uint32_t os_patch_level_; |
David Drysdale | bb3d85e | 2021-04-13 11:15:51 +0100 | [diff] [blame] | 337 | uint32_t vendor_patch_level_; |
David Drysdale | d2cc8c2 | 2021-04-15 13:29:45 +0100 | [diff] [blame] | 338 | bool timestamp_token_required_; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 339 | |
| 340 | SecurityLevel securityLevel_; |
| 341 | string name_; |
| 342 | string author_; |
| 343 | long challenge_; |
| 344 | }; |
| 345 | |
David Drysdale | a676c3b | 2021-06-14 14:46:02 +0100 | [diff] [blame] | 346 | // If the given property is available, add it to the tag set under the given tag ID. |
| 347 | template <Tag tag> |
| 348 | void add_tag_from_prop(AuthorizationSetBuilder* tags, TypedTag<TagType::BYTES, tag> ttag, |
| 349 | const char* prop) { |
| 350 | std::string prop_value = ::android::base::GetProperty(prop, /* default= */ ""); |
| 351 | if (!prop_value.empty()) { |
| 352 | tags->Authorization(ttag, prop_value.data(), prop_value.size()); |
| 353 | } |
| 354 | } |
| 355 | |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 356 | vector<uint8_t> build_serial_blob(const uint64_t serial_int); |
| 357 | void verify_subject(const X509* cert, const string& subject, bool self_signed); |
| 358 | void verify_serial(X509* cert, const uint64_t expected_serial); |
| 359 | void verify_subject_and_serial(const Certificate& certificate, // |
| 360 | const uint64_t expected_serial, // |
| 361 | const string& subject, bool self_signed); |
| 362 | |
David Drysdale | 7dff4fc | 2021-12-10 10:10:52 +0000 | [diff] [blame] | 363 | bool verify_attestation_record(int aidl_version, // |
| 364 | const string& challenge, // |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 365 | const string& app_id, // |
| 366 | AuthorizationSet expected_sw_enforced, // |
| 367 | AuthorizationSet expected_hw_enforced, // |
| 368 | SecurityLevel security_level, |
David Drysdale | 565ccc7 | 2021-10-11 12:49:50 +0100 | [diff] [blame] | 369 | const vector<uint8_t>& attestation_cert, |
| 370 | vector<uint8_t>* unique_id = nullptr); |
Selene Huang | 6e46f14 | 2021-04-20 19:20:11 -0700 | [diff] [blame] | 371 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 372 | string bin2hex(const vector<uint8_t>& data); |
| 373 | X509_Ptr parse_cert_blob(const vector<uint8_t>& blob); |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 374 | vector<uint8_t> make_name_from_str(const string& name); |
David Drysdale | 4dc0107 | 2021-04-01 12:17:35 +0100 | [diff] [blame] | 375 | void check_maced_pubkey(const MacedPublicKey& macedPubKey, bool testMode, |
| 376 | vector<uint8_t>* payload_value); |
| 377 | void p256_pub_key(const vector<uint8_t>& coseKeyData, EVP_PKEY_Ptr* signingKey); |
| 378 | |
David Drysdale | f0d516d | 2021-03-22 07:51:43 +0000 | [diff] [blame] | 379 | AuthorizationSet HwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics); |
| 380 | AuthorizationSet SwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics); |
Eran Messeri | 03d7a1a | 2021-07-06 12:07:57 +0100 | [diff] [blame] | 381 | ::testing::AssertionResult ChainSignaturesAreValid(const vector<Certificate>& chain, |
| 382 | bool strict_issuer_check = true); |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 383 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 384 | #define INSTANTIATE_KEYMINT_AIDL_TEST(name) \ |
| 385 | INSTANTIATE_TEST_SUITE_P(PerInstance, name, \ |
| 386 | testing::ValuesIn(KeyMintAidlTestBase::build_params()), \ |
Shawn Willden | 7e71f1e | 2021-04-01 16:44:22 -0600 | [diff] [blame] | 387 | ::android::PrintInstanceNameToString); \ |
| 388 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(name); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 389 | |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 390 | } // namespace test |
| 391 | |
| 392 | } // namespace aidl::android::hardware::security::keymint |