blob: 415a83e4e842bb96e568487c6788c70219335adb [file] [log] [blame]
Selene Huang31ab4042020-04-29 04:22:39 -07001/*
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 Huang31ab4042020-04-29 04:22:39 -070017#pragma once
18
David Drysdaleadfe6112021-05-27 12:00:53 +010019#include <functional>
David Drysdale300b5552021-05-20 12:05:26 +010020#include <string_view>
21
Selene Huang31ab4042020-04-29 04:22:39 -070022#include <aidl/Gtest.h>
23#include <aidl/Vintf.h>
David Drysdalea676c3b2021-06-14 14:46:02 +010024#include <android-base/properties.h>
Selene Huang31ab4042020-04-29 04:22:39 -070025#include <binder/IServiceManager.h>
26#include <binder/ProcessState.h>
27#include <gtest/gtest.h>
Shawn Willden7c130392020-12-21 09:58:22 -070028#include <openssl/x509.h>
Selene Huang31ab4042020-04-29 04:22:39 -070029
Janis Danisevskis24c04702020-12-16 18:28:39 -080030#include <aidl/android/hardware/security/keymint/ErrorCode.h>
31#include <aidl/android/hardware/security/keymint/IKeyMintDevice.h>
David Drysdale4dc01072021-04-01 12:17:35 +010032#include <aidl/android/hardware/security/keymint/MacedPublicKey.h>
Shawn Willden1d3f85e2020-12-09 14:18:44 -070033
Shawn Willden4315e132022-03-20 12:49:46 -060034#include <keymint_support/attestation_record.h>
Shawn Willden08a7e432020-12-11 13:05:27 +000035#include <keymint_support/authorization_set.h>
Shawn Willden7c130392020-12-21 09:58:22 -070036#include <keymint_support/openssl_utils.h>
Selene Huang31ab4042020-04-29 04:22:39 -070037
Shawn Willden0e80b5d2020-12-17 09:07:27 -070038namespace aidl::android::hardware::security::keymint {
39
40::std::ostream& operator<<(::std::ostream& os, const AuthorizationSet& set);
41
Shawn Willden7c130392020-12-21 09:58:22 -070042inline 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 Willden0e80b5d2020-12-17 09:07:27 -070046namespace test {
Selene Huang31ab4042020-04-29 04:22:39 -070047
48using ::android::sp;
Janis Danisevskis24c04702020-12-16 18:28:39 -080049using Status = ::ndk::ScopedAStatus;
Shawn Willden7c130392020-12-21 09:58:22 -070050using ::std::optional;
Selene Huang31ab4042020-04-29 04:22:39 -070051using ::std::shared_ptr;
52using ::std::string;
53using ::std::vector;
54
55constexpr uint64_t kOpHandleSentinel = 0xFFFFFFFFFFFFFFFF;
56
Subrahmanyaman50fcf7d2023-04-20 22:48:39 +000057const string FEATURE_KEYSTORE_APP_ATTEST_KEY = "android.hardware.keystore.app_attest_key";
58const string FEATURE_STRONGBOX_KEYSTORE = "android.hardware.strongbox_keystore";
59
Selene Huang31ab4042020-04-29 04:22:39 -070060class KeyMintAidlTestBase : public ::testing::TestWithParam<string> {
61 public:
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +000062 struct KeyData {
63 vector<uint8_t> blob;
64 vector<KeyCharacteristics> characteristics;
65 };
66
Shawn Willden7c130392020-12-21 09:58:22 -070067 static bool arm_deleteAllKeys;
68 static bool dump_Attestations;
69
David Drysdale9f5c0c52022-11-03 15:10:16 +000070 // Directory to store/retrieve keyblobs, using subdirectories named for the
71 // KeyMint instance in question (e.g. "./default/", "./strongbox/").
72 static std::string keyblob_dir;
Tommy Chiu025f3c52023-05-15 06:23:44 +000073 // To specify if users expect an upgrade on the keyBlobs.
74 static std::optional<bool> expect_upgrade;
David Drysdale9f5c0c52022-11-03 15:10:16 +000075
Selene Huang31ab4042020-04-29 04:22:39 -070076 void SetUp() override;
77 void TearDown() override {
78 if (key_blob_.size()) {
79 CheckedDeleteKey();
80 }
81 AbortIfNeeded();
82 }
83
Janis Danisevskis24c04702020-12-16 18:28:39 -080084 void InitializeKeyMint(std::shared_ptr<IKeyMintDevice> keyMint);
Selene Huang31ab4042020-04-29 04:22:39 -070085 IKeyMintDevice& keyMint() { return *keymint_; }
David Drysdale7dff4fc2021-12-10 10:10:52 +000086 int32_t AidlVersion();
Selene Huang31ab4042020-04-29 04:22:39 -070087 uint32_t os_version() { return os_version_; }
88 uint32_t os_patch_level() { return os_patch_level_; }
David Drysdalebb3d85e2021-04-13 11:15:51 +010089 uint32_t vendor_patch_level() { return vendor_patch_level_; }
David Drysdale37af4b32021-05-14 16:46:59 +010090 uint32_t boot_patch_level(const vector<KeyCharacteristics>& key_characteristics);
91 uint32_t boot_patch_level();
Prashant Patil88ad1892022-03-15 16:31:02 +000092 bool isDeviceIdAttestationRequired();
Rajesh Nyamagoud5283f812023-01-06 00:27:56 +000093 bool isSecondImeiIdAttestationRequired();
Selene Huang31ab4042020-04-29 04:22:39 -070094
David Drysdale42fe1892021-10-14 14:43:46 +010095 bool Curve25519Supported();
96
Janis Danisevskis24c04702020-12-16 18:28:39 -080097 ErrorCode GetReturnErrorCode(const Status& result);
Selene Huang31ab4042020-04-29 04:22:39 -070098
Shawn Willden7c130392020-12-21 09:58:22 -070099 ErrorCode GenerateKey(const AuthorizationSet& key_desc, vector<uint8_t>* key_blob,
100 vector<KeyCharacteristics>* key_characteristics) {
101 return GenerateKey(key_desc, std::nullopt /* attest_key */, key_blob, key_characteristics,
102 &cert_chain_);
103 }
104 ErrorCode GenerateKey(const AuthorizationSet& key_desc,
105 const optional<AttestationKey>& attest_key, vector<uint8_t>* key_blob,
106 vector<KeyCharacteristics>* key_characteristics,
107 vector<Certificate>* cert_chain);
108 ErrorCode GenerateKey(const AuthorizationSet& key_desc,
109 const optional<AttestationKey>& attest_key = std::nullopt);
110
subrahmanyaman7d9bc462022-03-16 01:40:39 +0000111 // Generate key for implementations which do not support factory attestation.
112 ErrorCode GenerateKeyWithSelfSignedAttestKey(const AuthorizationSet& attest_key_desc,
113 const AuthorizationSet& key_desc,
114 vector<uint8_t>* key_blob,
115 vector<KeyCharacteristics>* key_characteristics,
116 vector<Certificate>* cert_chain);
117
118 ErrorCode GenerateKeyWithSelfSignedAttestKey(const AuthorizationSet& attest_key_desc,
119 const AuthorizationSet& key_desc,
120 vector<uint8_t>* key_blob,
121 vector<KeyCharacteristics>* key_characteristics) {
122 return GenerateKeyWithSelfSignedAttestKey(attest_key_desc, key_desc, key_blob,
123 key_characteristics, &cert_chain_);
124 }
125
Selene Huang31ab4042020-04-29 04:22:39 -0700126 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
127 const string& key_material, vector<uint8_t>* key_blob,
Shawn Willden7f424372021-01-10 18:06:50 -0700128 vector<KeyCharacteristics>* key_characteristics);
Selene Huang31ab4042020-04-29 04:22:39 -0700129 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
130 const string& key_material);
131
132 ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
133 const AuthorizationSet& wrapping_key_desc, string masking_key,
David Drysdaled2cc8c22021-04-15 13:29:45 +0100134 const AuthorizationSet& unwrapping_params, int64_t password_sid,
135 int64_t biometric_sid);
136 ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
137 const AuthorizationSet& wrapping_key_desc, string masking_key,
138 const AuthorizationSet& unwrapping_params) {
139 return ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, masking_key,
140 unwrapping_params, 0 /* password_sid */, 0 /* biometric_sid */);
141 }
Selene Huang31ab4042020-04-29 04:22:39 -0700142
David Drysdale300b5552021-05-20 12:05:26 +0100143 ErrorCode GetCharacteristics(const vector<uint8_t>& key_blob, const vector<uint8_t>& app_id,
144 const vector<uint8_t>& app_data,
145 vector<KeyCharacteristics>* key_characteristics);
146 ErrorCode GetCharacteristics(const vector<uint8_t>& key_blob,
147 vector<KeyCharacteristics>* key_characteristics);
148
149 void CheckCharacteristics(const vector<uint8_t>& key_blob,
150 const vector<KeyCharacteristics>& generate_characteristics);
151 void CheckAppIdCharacteristics(const vector<uint8_t>& key_blob, std::string_view app_id_string,
152 std::string_view app_data_string,
153 const vector<KeyCharacteristics>& generate_characteristics);
154
Selene Huang31ab4042020-04-29 04:22:39 -0700155 ErrorCode DeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false);
156 ErrorCode DeleteKey(bool keep_key_blob = false);
157
158 ErrorCode DeleteAllKeys();
159
David Drysdaled2cc8c22021-04-15 13:29:45 +0100160 ErrorCode DestroyAttestationIds();
161
Selene Huang31ab4042020-04-29 04:22:39 -0700162 void CheckedDeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false);
163 void CheckedDeleteKey();
164
165 ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob,
166 const AuthorizationSet& in_params, AuthorizationSet* out_params,
Janis Danisevskis24c04702020-12-16 18:28:39 -0800167 std::shared_ptr<IKeyMintOperation>& op);
Selene Huang31ab4042020-04-29 04:22:39 -0700168 ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob,
David Drysdale28fa9312023-02-01 14:53:01 +0000169 const AuthorizationSet& in_params, AuthorizationSet* out_params,
170 std::optional<HardwareAuthToken> hat = std::nullopt);
Selene Huang31ab4042020-04-29 04:22:39 -0700171 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
172 AuthorizationSet* out_params);
173 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params);
174
Shawn Willden92d79c02021-02-19 07:31:55 -0700175 ErrorCode UpdateAad(const string& input);
176 ErrorCode Update(const string& input, string* output);
Selene Huang31ab4042020-04-29 04:22:39 -0700177
David Drysdale28fa9312023-02-01 14:53:01 +0000178 ErrorCode Finish(const string& message, const string& signature, string* output,
179 std::optional<HardwareAuthToken> hat = std::nullopt,
180 std::optional<secureclock::TimeStampToken> time_token = std::nullopt);
Shawn Willden92d79c02021-02-19 07:31:55 -0700181 ErrorCode Finish(const string& message, string* output) {
182 return Finish(message, {} /* signature */, output);
183 }
184 ErrorCode Finish(string* output) { return Finish({} /* message */, output); }
Selene Huang31ab4042020-04-29 04:22:39 -0700185
186 ErrorCode Abort();
Janis Danisevskis24c04702020-12-16 18:28:39 -0800187 ErrorCode Abort(const shared_ptr<IKeyMintOperation>& op);
Selene Huang31ab4042020-04-29 04:22:39 -0700188 void AbortIfNeeded();
189
190 string ProcessMessage(const vector<uint8_t>& key_blob, KeyPurpose operation,
191 const string& message, const AuthorizationSet& in_params,
192 AuthorizationSet* out_params);
Shawn Willden92d79c02021-02-19 07:31:55 -0700193 std::tuple<ErrorCode, std::string /* processedMessage */> ProcessMessage(
194 const vector<uint8_t>& key_blob, KeyPurpose operation, const std::string& message,
195 const AuthorizationSet& in_params);
Selene Huang31ab4042020-04-29 04:22:39 -0700196 string SignMessage(const vector<uint8_t>& key_blob, const string& message,
197 const AuthorizationSet& params);
198 string SignMessage(const string& message, const AuthorizationSet& params);
199
200 string MacMessage(const string& message, Digest digest, size_t mac_length);
201
anil.hiranniah19a4ca12022-03-03 17:39:30 +0530202 void CheckAesIncrementalEncryptOperation(BlockMode block_mode, int message_size);
203
Prashant Patildd5f7f02022-07-06 18:58:07 +0000204 void AesCheckEncryptOneByteAtATime(const string& key, BlockMode block_mode,
205 PaddingMode padding_mode, const string& iv,
206 const string& plaintext, const string& exp_cipher_text);
207
Selene Huang31ab4042020-04-29 04:22:39 -0700208 void CheckHmacTestVector(const string& key, const string& message, Digest digest,
209 const string& expected_mac);
210
211 void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message,
212 const string& expected_ciphertext);
213
214 void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
215 PaddingMode padding_mode, const string& key, const string& iv,
216 const string& input, const string& expected_output);
217
218 void VerifyMessage(const vector<uint8_t>& key_blob, const string& message,
219 const string& signature, const AuthorizationSet& params);
220 void VerifyMessage(const string& message, const string& signature,
221 const AuthorizationSet& params);
David Drysdale9f5c0c52022-11-03 15:10:16 +0000222 void LocalVerifyMessage(const vector<uint8_t>& der_cert, const string& message,
223 const string& signature, const AuthorizationSet& params);
David Drysdaledf8f52e2021-05-06 08:10:58 +0100224 void LocalVerifyMessage(const string& message, const string& signature,
225 const AuthorizationSet& params);
Selene Huang31ab4042020-04-29 04:22:39 -0700226
David Drysdale59cae642021-05-12 13:52:03 +0100227 string LocalRsaEncryptMessage(const string& message, const AuthorizationSet& params);
Selene Huang31ab4042020-04-29 04:22:39 -0700228 string EncryptMessage(const vector<uint8_t>& key_blob, const string& message,
229 const AuthorizationSet& in_params, AuthorizationSet* out_params);
230 string EncryptMessage(const string& message, const AuthorizationSet& params,
231 AuthorizationSet* out_params);
232 string EncryptMessage(const string& message, const AuthorizationSet& params);
233 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding);
234 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
235 vector<uint8_t>* iv_out);
236 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
237 const vector<uint8_t>& iv_in);
238 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
239 uint8_t mac_length_bits, const vector<uint8_t>& iv_in);
David Drysdaled2cc8c22021-04-15 13:29:45 +0100240 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
241 uint8_t mac_length_bits);
Selene Huang31ab4042020-04-29 04:22:39 -0700242
243 string DecryptMessage(const vector<uint8_t>& key_blob, const string& ciphertext,
244 const AuthorizationSet& params);
245 string DecryptMessage(const string& ciphertext, const AuthorizationSet& params);
246 string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode,
247 const vector<uint8_t>& iv);
248
249 std::pair<ErrorCode, vector<uint8_t>> UpgradeKey(const vector<uint8_t>& key_blob);
250
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000251 template <typename TagType>
252 std::tuple<KeyData /* aesKey */, KeyData /* hmacKey */, KeyData /* rsaKey */,
253 KeyData /* ecdsaKey */>
David Drysdaleadfe6112021-05-27 12:00:53 +0100254 CreateTestKeys(
255 TagType tagToTest, ErrorCode expectedReturn,
256 std::function<void(AuthorizationSetBuilder*)> tagModifier =
257 [](AuthorizationSetBuilder*) {}) {
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000258 /* AES */
259 KeyData aesKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100260 AuthorizationSetBuilder aesBuilder = AuthorizationSetBuilder()
261 .AesEncryptionKey(128)
262 .Authorization(tagToTest)
263 .BlockMode(BlockMode::ECB)
264 .Padding(PaddingMode::NONE)
265 .Authorization(TAG_NO_AUTH_REQUIRED);
266 tagModifier(&aesBuilder);
267 ErrorCode errorCode =
268 GenerateKey(aesBuilder, &aesKeyData.blob, &aesKeyData.characteristics);
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000269 EXPECT_EQ(expectedReturn, errorCode);
270
271 /* HMAC */
272 KeyData hmacKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100273 AuthorizationSetBuilder hmacBuilder = AuthorizationSetBuilder()
274 .HmacKey(128)
275 .Authorization(tagToTest)
276 .Digest(Digest::SHA_2_256)
277 .Authorization(TAG_MIN_MAC_LENGTH, 128)
278 .Authorization(TAG_NO_AUTH_REQUIRED);
279 tagModifier(&hmacBuilder);
280 errorCode = GenerateKey(hmacBuilder, &hmacKeyData.blob, &hmacKeyData.characteristics);
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000281 EXPECT_EQ(expectedReturn, errorCode);
282
283 /* RSA */
284 KeyData rsaKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100285 AuthorizationSetBuilder rsaBuilder = AuthorizationSetBuilder()
286 .RsaSigningKey(2048, 65537)
287 .Authorization(tagToTest)
288 .Digest(Digest::NONE)
289 .Padding(PaddingMode::NONE)
290 .Authorization(TAG_NO_AUTH_REQUIRED)
291 .SetDefaultValidity();
292 tagModifier(&rsaBuilder);
293 errorCode = GenerateKey(rsaBuilder, &rsaKeyData.blob, &rsaKeyData.characteristics);
subrahmanyaman05642492022-02-05 07:10:56 +0000294 if (!(SecLevel() == SecurityLevel::STRONGBOX &&
295 ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED == errorCode)) {
296 EXPECT_EQ(expectedReturn, errorCode);
297 }
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000298
299 /* ECDSA */
300 KeyData ecdsaKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100301 AuthorizationSetBuilder ecdsaBuilder = AuthorizationSetBuilder()
David Drysdaledf09e542021-06-08 15:46:11 +0100302 .EcdsaSigningKey(EcCurve::P_256)
David Drysdaleadfe6112021-05-27 12:00:53 +0100303 .Authorization(tagToTest)
304 .Digest(Digest::SHA_2_256)
305 .Authorization(TAG_NO_AUTH_REQUIRED)
306 .SetDefaultValidity();
307 tagModifier(&ecdsaBuilder);
308 errorCode = GenerateKey(ecdsaBuilder, &ecdsaKeyData.blob, &ecdsaKeyData.characteristics);
subrahmanyaman05642492022-02-05 07:10:56 +0000309 if (!(SecLevel() == SecurityLevel::STRONGBOX &&
310 ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED == errorCode)) {
311 EXPECT_EQ(expectedReturn, errorCode);
312 }
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000313 return {aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData};
314 }
Shawn Willden7f424372021-01-10 18:06:50 -0700315 bool IsSecure() const { return securityLevel_ != SecurityLevel::SOFTWARE; }
316 SecurityLevel SecLevel() const { return securityLevel_; }
Seth Moorea12ac742023-03-03 13:40:30 -0800317 bool IsRkpSupportRequired() const;
Selene Huang31ab4042020-04-29 04:22:39 -0700318
319 vector<uint32_t> ValidKeySizes(Algorithm algorithm);
320 vector<uint32_t> InvalidKeySizes(Algorithm algorithm);
321
David Drysdale7de9feb2021-03-05 14:56:19 +0000322 vector<BlockMode> ValidBlockModes(Algorithm algorithm);
323 vector<PaddingMode> ValidPaddingModes(Algorithm algorithm, BlockMode blockMode);
324 vector<PaddingMode> InvalidPaddingModes(Algorithm algorithm, BlockMode blockMode);
325
Selene Huang31ab4042020-04-29 04:22:39 -0700326 vector<EcCurve> ValidCurves();
327 vector<EcCurve> InvalidCurves();
328
329 vector<Digest> ValidDigests(bool withNone, bool withMD5);
subrahmanyaman05642492022-02-05 07:10:56 +0000330 vector<uint64_t> ValidExponents();
Selene Huang31ab4042020-04-29 04:22:39 -0700331
332 static vector<string> build_params() {
Janis Danisevskis24c04702020-12-16 18:28:39 -0800333 auto params = ::android::getAidlHalInstanceNames(IKeyMintDevice::descriptor);
Selene Huang31ab4042020-04-29 04:22:39 -0700334 return params;
335 }
336
Janis Danisevskis24c04702020-12-16 18:28:39 -0800337 std::shared_ptr<IKeyMintOperation> op_;
Shawn Willden7f424372021-01-10 18:06:50 -0700338 vector<Certificate> cert_chain_;
Selene Huang31ab4042020-04-29 04:22:39 -0700339 vector<uint8_t> key_blob_;
Shawn Willden7f424372021-01-10 18:06:50 -0700340 vector<KeyCharacteristics> key_characteristics_;
341
342 const vector<KeyParameter>& SecLevelAuthorizations(
343 const vector<KeyCharacteristics>& key_characteristics);
344 inline const vector<KeyParameter>& SecLevelAuthorizations() {
345 return SecLevelAuthorizations(key_characteristics_);
346 }
Qi Wubeefae42021-01-28 23:16:37 +0800347 const vector<KeyParameter>& SecLevelAuthorizations(
348 const vector<KeyCharacteristics>& key_characteristics, SecurityLevel securityLevel);
349
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000350 ErrorCode UseAesKey(const vector<uint8_t>& aesKeyBlob);
351 ErrorCode UseHmacKey(const vector<uint8_t>& hmacKeyBlob);
352 ErrorCode UseRsaKey(const vector<uint8_t>& rsaKeyBlob);
353 ErrorCode UseEcdsaKey(const vector<uint8_t>& ecdsaKeyBlob);
Selene Huang31ab4042020-04-29 04:22:39 -0700354
Subrahmanyaman50fcf7d2023-04-20 22:48:39 +0000355 ErrorCode GenerateAttestKey(const AuthorizationSet& key_desc,
356 const optional<AttestationKey>& attest_key,
357 vector<uint8_t>* key_blob,
358 vector<KeyCharacteristics>* key_characteristics,
359 vector<Certificate>* cert_chain);
360
361 bool is_attest_key_feature_disabled(void) const;
362 bool is_strongbox_enabled(void) const;
363 bool is_chipset_allowed_km4_strongbox(void) const;
364 void skipAttestKeyTest(void) const;
365
Shawn Willdend659c7c2021-02-19 14:51:51 -0700366 protected:
Janis Danisevskis24c04702020-12-16 18:28:39 -0800367 std::shared_ptr<IKeyMintDevice> keymint_;
Selene Huang31ab4042020-04-29 04:22:39 -0700368 uint32_t os_version_;
369 uint32_t os_patch_level_;
David Drysdalebb3d85e2021-04-13 11:15:51 +0100370 uint32_t vendor_patch_level_;
David Drysdaled2cc8c22021-04-15 13:29:45 +0100371 bool timestamp_token_required_;
Selene Huang31ab4042020-04-29 04:22:39 -0700372
373 SecurityLevel securityLevel_;
374 string name_;
375 string author_;
David Drysdale4cbe2152023-03-07 14:44:38 +0000376 int64_t challenge_;
Prashant Patildd5f7f02022-07-06 18:58:07 +0000377
378 private:
379 void CheckEncryptOneByteAtATime(BlockMode block_mode, const int block_size,
380 PaddingMode padding_mode, const string& iv,
381 const string& plaintext, const string& exp_cipher_text);
Selene Huang31ab4042020-04-29 04:22:39 -0700382};
383
David Drysdalea676c3b2021-06-14 14:46:02 +0100384// If the given property is available, add it to the tag set under the given tag ID.
385template <Tag tag>
386void add_tag_from_prop(AuthorizationSetBuilder* tags, TypedTag<TagType::BYTES, tag> ttag,
387 const char* prop) {
388 std::string prop_value = ::android::base::GetProperty(prop, /* default= */ "");
389 if (!prop_value.empty()) {
390 tags->Authorization(ttag, prop_value.data(), prop_value.size());
391 }
392}
393
Shawn Willden22fb9c12022-06-02 14:04:33 -0600394// Return the VSR API level for this device.
395int get_vsr_api_level();
396
David Drysdale555ba002022-05-03 18:48:57 +0100397// Indicate whether the test is running on a GSI image.
398bool is_gsi_image();
399
Selene Huang6e46f142021-04-20 19:20:11 -0700400vector<uint8_t> build_serial_blob(const uint64_t serial_int);
401void verify_subject(const X509* cert, const string& subject, bool self_signed);
402void verify_serial(X509* cert, const uint64_t expected_serial);
403void verify_subject_and_serial(const Certificate& certificate, //
404 const uint64_t expected_serial, //
405 const string& subject, bool self_signed);
Shawn Willden4315e132022-03-20 12:49:46 -0600406void verify_root_of_trust(const vector<uint8_t>& verified_boot_key, //
407 bool device_locked, //
408 VerifiedBoot verified_boot_state, //
409 const vector<uint8_t>& verified_boot_hash);
David Drysdale7dff4fc2021-12-10 10:10:52 +0000410bool verify_attestation_record(int aidl_version, //
411 const string& challenge, //
Shawn Willden7c130392020-12-21 09:58:22 -0700412 const string& app_id, //
413 AuthorizationSet expected_sw_enforced, //
414 AuthorizationSet expected_hw_enforced, //
415 SecurityLevel security_level,
David Drysdale565ccc72021-10-11 12:49:50 +0100416 const vector<uint8_t>& attestation_cert,
417 vector<uint8_t>* unique_id = nullptr);
Selene Huang6e46f142021-04-20 19:20:11 -0700418
Shawn Willden7c130392020-12-21 09:58:22 -0700419string bin2hex(const vector<uint8_t>& data);
420X509_Ptr parse_cert_blob(const vector<uint8_t>& blob);
Tri Voec50ee12023-02-14 16:29:53 -0800421ASN1_OCTET_STRING* get_attestation_record(X509* certificate);
David Drysdalef0d516d2021-03-22 07:51:43 +0000422vector<uint8_t> make_name_from_str(const string& name);
David Drysdale4dc01072021-04-01 12:17:35 +0100423void check_maced_pubkey(const MacedPublicKey& macedPubKey, bool testMode,
424 vector<uint8_t>* payload_value);
425void p256_pub_key(const vector<uint8_t>& coseKeyData, EVP_PKEY_Ptr* signingKey);
Max Biresa97ec692022-11-21 23:37:54 -0800426void device_id_attestation_vsr_check(const ErrorCode& result);
David Drysdale3d2ba0a2023-01-11 13:27:26 +0000427bool check_feature(const std::string& name);
David Drysdale4dc01072021-04-01 12:17:35 +0100428
David Drysdalef0d516d2021-03-22 07:51:43 +0000429AuthorizationSet HwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics);
430AuthorizationSet SwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics);
Eran Messeri03d7a1a2021-07-06 12:07:57 +0100431::testing::AssertionResult ChainSignaturesAreValid(const vector<Certificate>& chain,
432 bool strict_issuer_check = true);
Shawn Willden7c130392020-12-21 09:58:22 -0700433
Selene Huang31ab4042020-04-29 04:22:39 -0700434#define INSTANTIATE_KEYMINT_AIDL_TEST(name) \
435 INSTANTIATE_TEST_SUITE_P(PerInstance, name, \
436 testing::ValuesIn(KeyMintAidlTestBase::build_params()), \
Shawn Willden7e71f1e2021-04-01 16:44:22 -0600437 ::android::PrintInstanceNameToString); \
438 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(name);
Selene Huang31ab4042020-04-29 04:22:39 -0700439
Shawn Willden0e80b5d2020-12-17 09:07:27 -0700440} // namespace test
441
442} // namespace aidl::android::hardware::security::keymint