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 | |
| 19 | #include <aidl/Gtest.h> |
| 20 | #include <aidl/Vintf.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 21 | #include <binder/IServiceManager.h> |
| 22 | #include <binder/ProcessState.h> |
| 23 | #include <gtest/gtest.h> |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 24 | #include <openssl/x509.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 25 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 26 | #include <aidl/android/hardware/security/keymint/ErrorCode.h> |
| 27 | #include <aidl/android/hardware/security/keymint/IKeyMintDevice.h> |
Shawn Willden | 1d3f85e | 2020-12-09 14:18:44 -0700 | [diff] [blame] | 28 | |
Shawn Willden | 08a7e43 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 29 | #include <keymint_support/authorization_set.h> |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 30 | #include <keymint_support/openssl_utils.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 31 | |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 32 | namespace aidl::android::hardware::security::keymint { |
| 33 | |
| 34 | ::std::ostream& operator<<(::std::ostream& os, const AuthorizationSet& set); |
| 35 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 36 | inline bool operator==(const keymint::AuthorizationSet& a, const keymint::AuthorizationSet& b) { |
| 37 | return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin()); |
| 38 | } |
| 39 | |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 40 | namespace test { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 41 | |
| 42 | using ::android::sp; |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 43 | using Status = ::ndk::ScopedAStatus; |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 44 | using ::std::optional; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 45 | using ::std::shared_ptr; |
| 46 | using ::std::string; |
| 47 | using ::std::vector; |
| 48 | |
| 49 | constexpr uint64_t kOpHandleSentinel = 0xFFFFFFFFFFFFFFFF; |
| 50 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 51 | class KeyMintAidlTestBase : public ::testing::TestWithParam<string> { |
| 52 | public: |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 53 | struct KeyData { |
| 54 | vector<uint8_t> blob; |
| 55 | vector<KeyCharacteristics> characteristics; |
| 56 | }; |
| 57 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 58 | static bool arm_deleteAllKeys; |
| 59 | static bool dump_Attestations; |
| 60 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 61 | void SetUp() override; |
| 62 | void TearDown() override { |
| 63 | if (key_blob_.size()) { |
| 64 | CheckedDeleteKey(); |
| 65 | } |
| 66 | AbortIfNeeded(); |
| 67 | } |
| 68 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 69 | void InitializeKeyMint(std::shared_ptr<IKeyMintDevice> keyMint); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 70 | IKeyMintDevice& keyMint() { return *keymint_; } |
| 71 | uint32_t os_version() { return os_version_; } |
| 72 | uint32_t os_patch_level() { return os_patch_level_; } |
| 73 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 74 | ErrorCode GetReturnErrorCode(const Status& result); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 75 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 76 | ErrorCode GenerateKey(const AuthorizationSet& key_desc, vector<uint8_t>* key_blob, |
| 77 | vector<KeyCharacteristics>* key_characteristics) { |
| 78 | return GenerateKey(key_desc, std::nullopt /* attest_key */, key_blob, key_characteristics, |
| 79 | &cert_chain_); |
| 80 | } |
| 81 | ErrorCode GenerateKey(const AuthorizationSet& key_desc, |
| 82 | const optional<AttestationKey>& attest_key, vector<uint8_t>* key_blob, |
| 83 | vector<KeyCharacteristics>* key_characteristics, |
| 84 | vector<Certificate>* cert_chain); |
| 85 | ErrorCode GenerateKey(const AuthorizationSet& key_desc, |
| 86 | const optional<AttestationKey>& attest_key = std::nullopt); |
| 87 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 88 | ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format, |
| 89 | const string& key_material, vector<uint8_t>* key_blob, |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 90 | vector<KeyCharacteristics>* key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 91 | ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format, |
| 92 | const string& key_material); |
| 93 | |
| 94 | ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key, |
| 95 | const AuthorizationSet& wrapping_key_desc, string masking_key, |
| 96 | const AuthorizationSet& unwrapping_params); |
| 97 | |
| 98 | ErrorCode DeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false); |
| 99 | ErrorCode DeleteKey(bool keep_key_blob = false); |
| 100 | |
| 101 | ErrorCode DeleteAllKeys(); |
| 102 | |
| 103 | void CheckedDeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false); |
| 104 | void CheckedDeleteKey(); |
| 105 | |
| 106 | ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob, |
| 107 | const AuthorizationSet& in_params, AuthorizationSet* out_params, |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 108 | std::shared_ptr<IKeyMintOperation>& op); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 109 | ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob, |
| 110 | const AuthorizationSet& in_params, AuthorizationSet* out_params); |
| 111 | ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params, |
| 112 | AuthorizationSet* out_params); |
| 113 | ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params); |
| 114 | |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame^] | 115 | ErrorCode UpdateAad(const string& input); |
| 116 | ErrorCode Update(const string& input, string* output); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 117 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 118 | ErrorCode Finish(const string& message, const string& signature, string* output); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame^] | 119 | ErrorCode Finish(const string& message, string* output) { |
| 120 | return Finish(message, {} /* signature */, output); |
| 121 | } |
| 122 | ErrorCode Finish(string* output) { return Finish({} /* message */, output); } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 123 | |
| 124 | ErrorCode Abort(); |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 125 | ErrorCode Abort(const shared_ptr<IKeyMintOperation>& op); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 126 | void AbortIfNeeded(); |
| 127 | |
| 128 | string ProcessMessage(const vector<uint8_t>& key_blob, KeyPurpose operation, |
| 129 | const string& message, const AuthorizationSet& in_params, |
| 130 | AuthorizationSet* out_params); |
Shawn Willden | 92d79c0 | 2021-02-19 07:31:55 -0700 | [diff] [blame^] | 131 | std::tuple<ErrorCode, std::string /* processedMessage */> ProcessMessage( |
| 132 | const vector<uint8_t>& key_blob, KeyPurpose operation, const std::string& message, |
| 133 | const AuthorizationSet& in_params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 134 | string SignMessage(const vector<uint8_t>& key_blob, const string& message, |
| 135 | const AuthorizationSet& params); |
| 136 | string SignMessage(const string& message, const AuthorizationSet& params); |
| 137 | |
| 138 | string MacMessage(const string& message, Digest digest, size_t mac_length); |
| 139 | |
| 140 | void CheckHmacTestVector(const string& key, const string& message, Digest digest, |
| 141 | const string& expected_mac); |
| 142 | |
| 143 | void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message, |
| 144 | const string& expected_ciphertext); |
| 145 | |
| 146 | void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode, |
| 147 | PaddingMode padding_mode, const string& key, const string& iv, |
| 148 | const string& input, const string& expected_output); |
| 149 | |
| 150 | void VerifyMessage(const vector<uint8_t>& key_blob, const string& message, |
| 151 | const string& signature, const AuthorizationSet& params); |
| 152 | void VerifyMessage(const string& message, const string& signature, |
| 153 | const AuthorizationSet& params); |
| 154 | |
| 155 | string EncryptMessage(const vector<uint8_t>& key_blob, const string& message, |
| 156 | const AuthorizationSet& in_params, AuthorizationSet* out_params); |
| 157 | string EncryptMessage(const string& message, const AuthorizationSet& params, |
| 158 | AuthorizationSet* out_params); |
| 159 | string EncryptMessage(const string& message, const AuthorizationSet& params); |
| 160 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding); |
| 161 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 162 | vector<uint8_t>* iv_out); |
| 163 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 164 | const vector<uint8_t>& iv_in); |
| 165 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 166 | uint8_t mac_length_bits, const vector<uint8_t>& iv_in); |
| 167 | |
| 168 | string DecryptMessage(const vector<uint8_t>& key_blob, const string& ciphertext, |
| 169 | const AuthorizationSet& params); |
| 170 | string DecryptMessage(const string& ciphertext, const AuthorizationSet& params); |
| 171 | string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode, |
| 172 | const vector<uint8_t>& iv); |
| 173 | |
| 174 | std::pair<ErrorCode, vector<uint8_t>> UpgradeKey(const vector<uint8_t>& key_blob); |
| 175 | |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 176 | template <typename TagType> |
| 177 | std::tuple<KeyData /* aesKey */, KeyData /* hmacKey */, KeyData /* rsaKey */, |
| 178 | KeyData /* ecdsaKey */> |
| 179 | CreateTestKeys(TagType tagToTest, ErrorCode expectedReturn) { |
| 180 | /* AES */ |
| 181 | KeyData aesKeyData; |
| 182 | ErrorCode errorCode = GenerateKey(AuthorizationSetBuilder() |
| 183 | .AesEncryptionKey(128) |
| 184 | .Authorization(tagToTest) |
| 185 | .BlockMode(BlockMode::ECB) |
| 186 | .Padding(PaddingMode::NONE) |
| 187 | .Authorization(TAG_NO_AUTH_REQUIRED), |
| 188 | &aesKeyData.blob, &aesKeyData.characteristics); |
| 189 | EXPECT_EQ(expectedReturn, errorCode); |
| 190 | |
| 191 | /* HMAC */ |
| 192 | KeyData hmacKeyData; |
| 193 | errorCode = GenerateKey(AuthorizationSetBuilder() |
| 194 | .HmacKey(128) |
| 195 | .Authorization(tagToTest) |
| 196 | .Digest(Digest::SHA_2_256) |
| 197 | .Authorization(TAG_MIN_MAC_LENGTH, 128) |
| 198 | .Authorization(TAG_NO_AUTH_REQUIRED), |
| 199 | &hmacKeyData.blob, &hmacKeyData.characteristics); |
| 200 | EXPECT_EQ(expectedReturn, errorCode); |
| 201 | |
| 202 | /* RSA */ |
| 203 | KeyData rsaKeyData; |
| 204 | errorCode = GenerateKey(AuthorizationSetBuilder() |
| 205 | .RsaSigningKey(2048, 65537) |
| 206 | .Authorization(tagToTest) |
| 207 | .Digest(Digest::NONE) |
| 208 | .Padding(PaddingMode::NONE) |
| 209 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 210 | .SetDefaultValidity(), |
| 211 | &rsaKeyData.blob, &rsaKeyData.characteristics); |
| 212 | EXPECT_EQ(expectedReturn, errorCode); |
| 213 | |
| 214 | /* ECDSA */ |
| 215 | KeyData ecdsaKeyData; |
| 216 | errorCode = GenerateKey(AuthorizationSetBuilder() |
| 217 | .EcdsaSigningKey(256) |
| 218 | .Authorization(tagToTest) |
| 219 | .Digest(Digest::SHA_2_256) |
| 220 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 221 | .SetDefaultValidity(), |
| 222 | &ecdsaKeyData.blob, &ecdsaKeyData.characteristics); |
| 223 | EXPECT_EQ(expectedReturn, errorCode); |
| 224 | return {aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData}; |
| 225 | } |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 226 | bool IsSecure() const { return securityLevel_ != SecurityLevel::SOFTWARE; } |
| 227 | SecurityLevel SecLevel() const { return securityLevel_; } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 228 | |
| 229 | vector<uint32_t> ValidKeySizes(Algorithm algorithm); |
| 230 | vector<uint32_t> InvalidKeySizes(Algorithm algorithm); |
| 231 | |
| 232 | vector<EcCurve> ValidCurves(); |
| 233 | vector<EcCurve> InvalidCurves(); |
| 234 | |
| 235 | vector<Digest> ValidDigests(bool withNone, bool withMD5); |
| 236 | |
| 237 | static vector<string> build_params() { |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 238 | auto params = ::android::getAidlHalInstanceNames(IKeyMintDevice::descriptor); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 239 | return params; |
| 240 | } |
| 241 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 242 | std::shared_ptr<IKeyMintOperation> op_; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 243 | vector<Certificate> cert_chain_; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 244 | vector<uint8_t> key_blob_; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 245 | vector<KeyCharacteristics> key_characteristics_; |
| 246 | |
| 247 | const vector<KeyParameter>& SecLevelAuthorizations( |
| 248 | const vector<KeyCharacteristics>& key_characteristics); |
| 249 | inline const vector<KeyParameter>& SecLevelAuthorizations() { |
| 250 | return SecLevelAuthorizations(key_characteristics_); |
| 251 | } |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 252 | const vector<KeyParameter>& SecLevelAuthorizations( |
| 253 | const vector<KeyCharacteristics>& key_characteristics, SecurityLevel securityLevel); |
| 254 | |
| 255 | AuthorizationSet HwEnforcedAuthorizations( |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 256 | const vector<KeyCharacteristics>& key_characteristics); |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 257 | AuthorizationSet SwEnforcedAuthorizations( |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 258 | const vector<KeyCharacteristics>& key_characteristics); |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 259 | ErrorCode UseAesKey(const vector<uint8_t>& aesKeyBlob); |
| 260 | ErrorCode UseHmacKey(const vector<uint8_t>& hmacKeyBlob); |
| 261 | ErrorCode UseRsaKey(const vector<uint8_t>& rsaKeyBlob); |
| 262 | ErrorCode UseEcdsaKey(const vector<uint8_t>& ecdsaKeyBlob); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 263 | |
| 264 | private: |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 265 | std::shared_ptr<IKeyMintDevice> keymint_; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 266 | uint32_t os_version_; |
| 267 | uint32_t os_patch_level_; |
| 268 | |
| 269 | SecurityLevel securityLevel_; |
| 270 | string name_; |
| 271 | string author_; |
| 272 | long challenge_; |
| 273 | }; |
| 274 | |
Shawn Willden | 7c13039 | 2020-12-21 09:58:22 -0700 | [diff] [blame] | 275 | bool verify_attestation_record(const string& challenge, // |
| 276 | const string& app_id, // |
| 277 | AuthorizationSet expected_sw_enforced, // |
| 278 | AuthorizationSet expected_hw_enforced, // |
| 279 | SecurityLevel security_level, |
| 280 | const vector<uint8_t>& attestation_cert); |
| 281 | string bin2hex(const vector<uint8_t>& data); |
| 282 | X509_Ptr parse_cert_blob(const vector<uint8_t>& blob); |
| 283 | ::testing::AssertionResult ChainSignaturesAreValid(const vector<Certificate>& chain); |
| 284 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 285 | #define INSTANTIATE_KEYMINT_AIDL_TEST(name) \ |
| 286 | INSTANTIATE_TEST_SUITE_P(PerInstance, name, \ |
| 287 | testing::ValuesIn(KeyMintAidlTestBase::build_params()), \ |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 288 | ::android::PrintInstanceNameToString) |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 289 | |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 290 | } // namespace test |
| 291 | |
| 292 | } // namespace aidl::android::hardware::security::keymint |