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