blob: 908eeab19be755af20b95e243856dd3f5b9d3c09 [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
Selene Huang31ab4042020-04-29 04:22:39 -070057class KeyMintAidlTestBase : public ::testing::TestWithParam<string> {
58 public:
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +000059 struct KeyData {
60 vector<uint8_t> blob;
61 vector<KeyCharacteristics> characteristics;
62 };
63
Shawn Willden7c130392020-12-21 09:58:22 -070064 static bool arm_deleteAllKeys;
65 static bool dump_Attestations;
66
David Drysdale9f5c0c52022-11-03 15:10:16 +000067 // Directory to store/retrieve keyblobs, using subdirectories named for the
68 // KeyMint instance in question (e.g. "./default/", "./strongbox/").
69 static std::string keyblob_dir;
70
Selene Huang31ab4042020-04-29 04:22:39 -070071 void SetUp() override;
72 void TearDown() override {
73 if (key_blob_.size()) {
74 CheckedDeleteKey();
75 }
76 AbortIfNeeded();
77 }
78
Janis Danisevskis24c04702020-12-16 18:28:39 -080079 void InitializeKeyMint(std::shared_ptr<IKeyMintDevice> keyMint);
Selene Huang31ab4042020-04-29 04:22:39 -070080 IKeyMintDevice& keyMint() { return *keymint_; }
David Drysdale7dff4fc2021-12-10 10:10:52 +000081 int32_t AidlVersion();
Selene Huang31ab4042020-04-29 04:22:39 -070082 uint32_t os_version() { return os_version_; }
83 uint32_t os_patch_level() { return os_patch_level_; }
David Drysdalebb3d85e2021-04-13 11:15:51 +010084 uint32_t vendor_patch_level() { return vendor_patch_level_; }
David Drysdale37af4b32021-05-14 16:46:59 +010085 uint32_t boot_patch_level(const vector<KeyCharacteristics>& key_characteristics);
86 uint32_t boot_patch_level();
Prashant Patil88ad1892022-03-15 16:31:02 +000087 bool isDeviceIdAttestationRequired();
Selene Huang31ab4042020-04-29 04:22:39 -070088
David Drysdale42fe1892021-10-14 14:43:46 +010089 bool Curve25519Supported();
90
Janis Danisevskis24c04702020-12-16 18:28:39 -080091 ErrorCode GetReturnErrorCode(const Status& result);
Selene Huang31ab4042020-04-29 04:22:39 -070092
Shawn Willden7c130392020-12-21 09:58:22 -070093 ErrorCode GenerateKey(const AuthorizationSet& key_desc, vector<uint8_t>* key_blob,
94 vector<KeyCharacteristics>* key_characteristics) {
95 return GenerateKey(key_desc, std::nullopt /* attest_key */, key_blob, key_characteristics,
96 &cert_chain_);
97 }
98 ErrorCode GenerateKey(const AuthorizationSet& key_desc,
99 const optional<AttestationKey>& attest_key, vector<uint8_t>* key_blob,
100 vector<KeyCharacteristics>* key_characteristics,
101 vector<Certificate>* cert_chain);
102 ErrorCode GenerateKey(const AuthorizationSet& key_desc,
103 const optional<AttestationKey>& attest_key = std::nullopt);
104
subrahmanyaman7d9bc462022-03-16 01:40:39 +0000105 // Generate key for implementations which do not support factory attestation.
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 vector<Certificate>* cert_chain);
111
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 return GenerateKeyWithSelfSignedAttestKey(attest_key_desc, key_desc, key_blob,
117 key_characteristics, &cert_chain_);
118 }
119
Selene Huang31ab4042020-04-29 04:22:39 -0700120 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
121 const string& key_material, vector<uint8_t>* key_blob,
Shawn Willden7f424372021-01-10 18:06:50 -0700122 vector<KeyCharacteristics>* key_characteristics);
Selene Huang31ab4042020-04-29 04:22:39 -0700123 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
124 const string& key_material);
125
126 ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
127 const AuthorizationSet& wrapping_key_desc, string masking_key,
David Drysdaled2cc8c22021-04-15 13:29:45 +0100128 const AuthorizationSet& unwrapping_params, int64_t password_sid,
129 int64_t biometric_sid);
130 ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
131 const AuthorizationSet& wrapping_key_desc, string masking_key,
132 const AuthorizationSet& unwrapping_params) {
133 return ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, masking_key,
134 unwrapping_params, 0 /* password_sid */, 0 /* biometric_sid */);
135 }
Selene Huang31ab4042020-04-29 04:22:39 -0700136
David Drysdale300b5552021-05-20 12:05:26 +0100137 ErrorCode GetCharacteristics(const vector<uint8_t>& key_blob, const vector<uint8_t>& app_id,
138 const vector<uint8_t>& app_data,
139 vector<KeyCharacteristics>* key_characteristics);
140 ErrorCode GetCharacteristics(const vector<uint8_t>& key_blob,
141 vector<KeyCharacteristics>* key_characteristics);
142
143 void CheckCharacteristics(const vector<uint8_t>& key_blob,
144 const vector<KeyCharacteristics>& generate_characteristics);
145 void CheckAppIdCharacteristics(const vector<uint8_t>& key_blob, std::string_view app_id_string,
146 std::string_view app_data_string,
147 const vector<KeyCharacteristics>& generate_characteristics);
148
Selene Huang31ab4042020-04-29 04:22:39 -0700149 ErrorCode DeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false);
150 ErrorCode DeleteKey(bool keep_key_blob = false);
151
152 ErrorCode DeleteAllKeys();
153
David Drysdaled2cc8c22021-04-15 13:29:45 +0100154 ErrorCode DestroyAttestationIds();
155
Selene Huang31ab4042020-04-29 04:22:39 -0700156 void CheckedDeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false);
157 void CheckedDeleteKey();
158
159 ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob,
160 const AuthorizationSet& in_params, AuthorizationSet* out_params,
Janis Danisevskis24c04702020-12-16 18:28:39 -0800161 std::shared_ptr<IKeyMintOperation>& op);
Selene Huang31ab4042020-04-29 04:22:39 -0700162 ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob,
163 const AuthorizationSet& in_params, AuthorizationSet* out_params);
164 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
165 AuthorizationSet* out_params);
166 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params);
167
Shawn Willden92d79c02021-02-19 07:31:55 -0700168 ErrorCode UpdateAad(const string& input);
169 ErrorCode Update(const string& input, string* output);
Selene Huang31ab4042020-04-29 04:22:39 -0700170
Selene Huang31ab4042020-04-29 04:22:39 -0700171 ErrorCode Finish(const string& message, const string& signature, string* output);
Shawn Willden92d79c02021-02-19 07:31:55 -0700172 ErrorCode Finish(const string& message, string* output) {
173 return Finish(message, {} /* signature */, output);
174 }
175 ErrorCode Finish(string* output) { return Finish({} /* message */, output); }
Selene Huang31ab4042020-04-29 04:22:39 -0700176
177 ErrorCode Abort();
Janis Danisevskis24c04702020-12-16 18:28:39 -0800178 ErrorCode Abort(const shared_ptr<IKeyMintOperation>& op);
Selene Huang31ab4042020-04-29 04:22:39 -0700179 void AbortIfNeeded();
180
181 string ProcessMessage(const vector<uint8_t>& key_blob, KeyPurpose operation,
182 const string& message, const AuthorizationSet& in_params,
183 AuthorizationSet* out_params);
Shawn Willden92d79c02021-02-19 07:31:55 -0700184 std::tuple<ErrorCode, std::string /* processedMessage */> ProcessMessage(
185 const vector<uint8_t>& key_blob, KeyPurpose operation, const std::string& message,
186 const AuthorizationSet& in_params);
Selene Huang31ab4042020-04-29 04:22:39 -0700187 string SignMessage(const vector<uint8_t>& key_blob, const string& message,
188 const AuthorizationSet& params);
189 string SignMessage(const string& message, const AuthorizationSet& params);
190
191 string MacMessage(const string& message, Digest digest, size_t mac_length);
192
anil.hiranniah19a4ca12022-03-03 17:39:30 +0530193 void CheckAesIncrementalEncryptOperation(BlockMode block_mode, int message_size);
194
Prashant Patildd5f7f02022-07-06 18:58:07 +0000195 void AesCheckEncryptOneByteAtATime(const string& key, BlockMode block_mode,
196 PaddingMode padding_mode, const string& iv,
197 const string& plaintext, const string& exp_cipher_text);
198
Selene Huang31ab4042020-04-29 04:22:39 -0700199 void CheckHmacTestVector(const string& key, const string& message, Digest digest,
200 const string& expected_mac);
201
202 void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message,
203 const string& expected_ciphertext);
204
205 void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
206 PaddingMode padding_mode, const string& key, const string& iv,
207 const string& input, const string& expected_output);
208
209 void VerifyMessage(const vector<uint8_t>& key_blob, const string& message,
210 const string& signature, const AuthorizationSet& params);
211 void VerifyMessage(const string& message, const string& signature,
212 const AuthorizationSet& params);
David Drysdale9f5c0c52022-11-03 15:10:16 +0000213 void LocalVerifyMessage(const vector<uint8_t>& der_cert, const string& message,
214 const string& signature, const AuthorizationSet& params);
David Drysdaledf8f52e2021-05-06 08:10:58 +0100215 void LocalVerifyMessage(const string& message, const string& signature,
216 const AuthorizationSet& params);
Selene Huang31ab4042020-04-29 04:22:39 -0700217
David Drysdale59cae642021-05-12 13:52:03 +0100218 string LocalRsaEncryptMessage(const string& message, const AuthorizationSet& params);
Selene Huang31ab4042020-04-29 04:22:39 -0700219 string EncryptMessage(const vector<uint8_t>& key_blob, const string& message,
220 const AuthorizationSet& in_params, AuthorizationSet* out_params);
221 string EncryptMessage(const string& message, const AuthorizationSet& params,
222 AuthorizationSet* out_params);
223 string EncryptMessage(const string& message, const AuthorizationSet& params);
224 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding);
225 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
226 vector<uint8_t>* iv_out);
227 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
228 const vector<uint8_t>& iv_in);
229 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
230 uint8_t mac_length_bits, const vector<uint8_t>& iv_in);
David Drysdaled2cc8c22021-04-15 13:29:45 +0100231 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
232 uint8_t mac_length_bits);
Selene Huang31ab4042020-04-29 04:22:39 -0700233
234 string DecryptMessage(const vector<uint8_t>& key_blob, const string& ciphertext,
235 const AuthorizationSet& params);
236 string DecryptMessage(const string& ciphertext, const AuthorizationSet& params);
237 string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode,
238 const vector<uint8_t>& iv);
239
240 std::pair<ErrorCode, vector<uint8_t>> UpgradeKey(const vector<uint8_t>& key_blob);
241
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000242 template <typename TagType>
243 std::tuple<KeyData /* aesKey */, KeyData /* hmacKey */, KeyData /* rsaKey */,
244 KeyData /* ecdsaKey */>
David Drysdaleadfe6112021-05-27 12:00:53 +0100245 CreateTestKeys(
246 TagType tagToTest, ErrorCode expectedReturn,
247 std::function<void(AuthorizationSetBuilder*)> tagModifier =
248 [](AuthorizationSetBuilder*) {}) {
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000249 /* AES */
250 KeyData aesKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100251 AuthorizationSetBuilder aesBuilder = AuthorizationSetBuilder()
252 .AesEncryptionKey(128)
253 .Authorization(tagToTest)
254 .BlockMode(BlockMode::ECB)
255 .Padding(PaddingMode::NONE)
256 .Authorization(TAG_NO_AUTH_REQUIRED);
257 tagModifier(&aesBuilder);
258 ErrorCode errorCode =
259 GenerateKey(aesBuilder, &aesKeyData.blob, &aesKeyData.characteristics);
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000260 EXPECT_EQ(expectedReturn, errorCode);
261
262 /* HMAC */
263 KeyData hmacKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100264 AuthorizationSetBuilder hmacBuilder = AuthorizationSetBuilder()
265 .HmacKey(128)
266 .Authorization(tagToTest)
267 .Digest(Digest::SHA_2_256)
268 .Authorization(TAG_MIN_MAC_LENGTH, 128)
269 .Authorization(TAG_NO_AUTH_REQUIRED);
270 tagModifier(&hmacBuilder);
271 errorCode = GenerateKey(hmacBuilder, &hmacKeyData.blob, &hmacKeyData.characteristics);
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000272 EXPECT_EQ(expectedReturn, errorCode);
273
274 /* RSA */
275 KeyData rsaKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100276 AuthorizationSetBuilder rsaBuilder = AuthorizationSetBuilder()
277 .RsaSigningKey(2048, 65537)
278 .Authorization(tagToTest)
279 .Digest(Digest::NONE)
280 .Padding(PaddingMode::NONE)
281 .Authorization(TAG_NO_AUTH_REQUIRED)
282 .SetDefaultValidity();
283 tagModifier(&rsaBuilder);
284 errorCode = GenerateKey(rsaBuilder, &rsaKeyData.blob, &rsaKeyData.characteristics);
subrahmanyaman05642492022-02-05 07:10:56 +0000285 if (!(SecLevel() == SecurityLevel::STRONGBOX &&
286 ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED == errorCode)) {
287 EXPECT_EQ(expectedReturn, errorCode);
288 }
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000289
290 /* ECDSA */
291 KeyData ecdsaKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100292 AuthorizationSetBuilder ecdsaBuilder = AuthorizationSetBuilder()
David Drysdaledf09e542021-06-08 15:46:11 +0100293 .EcdsaSigningKey(EcCurve::P_256)
David Drysdaleadfe6112021-05-27 12:00:53 +0100294 .Authorization(tagToTest)
295 .Digest(Digest::SHA_2_256)
296 .Authorization(TAG_NO_AUTH_REQUIRED)
297 .SetDefaultValidity();
298 tagModifier(&ecdsaBuilder);
299 errorCode = GenerateKey(ecdsaBuilder, &ecdsaKeyData.blob, &ecdsaKeyData.characteristics);
subrahmanyaman05642492022-02-05 07:10:56 +0000300 if (!(SecLevel() == SecurityLevel::STRONGBOX &&
301 ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED == errorCode)) {
302 EXPECT_EQ(expectedReturn, errorCode);
303 }
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000304 return {aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData};
305 }
Shawn Willden7f424372021-01-10 18:06:50 -0700306 bool IsSecure() const { return securityLevel_ != SecurityLevel::SOFTWARE; }
307 SecurityLevel SecLevel() const { return securityLevel_; }
Selene Huang31ab4042020-04-29 04:22:39 -0700308
309 vector<uint32_t> ValidKeySizes(Algorithm algorithm);
310 vector<uint32_t> InvalidKeySizes(Algorithm algorithm);
311
David Drysdale7de9feb2021-03-05 14:56:19 +0000312 vector<BlockMode> ValidBlockModes(Algorithm algorithm);
313 vector<PaddingMode> ValidPaddingModes(Algorithm algorithm, BlockMode blockMode);
314 vector<PaddingMode> InvalidPaddingModes(Algorithm algorithm, BlockMode blockMode);
315
Selene Huang31ab4042020-04-29 04:22:39 -0700316 vector<EcCurve> ValidCurves();
317 vector<EcCurve> InvalidCurves();
318
319 vector<Digest> ValidDigests(bool withNone, bool withMD5);
subrahmanyaman05642492022-02-05 07:10:56 +0000320 vector<uint64_t> ValidExponents();
Selene Huang31ab4042020-04-29 04:22:39 -0700321
322 static vector<string> build_params() {
Janis Danisevskis24c04702020-12-16 18:28:39 -0800323 auto params = ::android::getAidlHalInstanceNames(IKeyMintDevice::descriptor);
Selene Huang31ab4042020-04-29 04:22:39 -0700324 return params;
325 }
326
Janis Danisevskis24c04702020-12-16 18:28:39 -0800327 std::shared_ptr<IKeyMintOperation> op_;
Shawn Willden7f424372021-01-10 18:06:50 -0700328 vector<Certificate> cert_chain_;
Selene Huang31ab4042020-04-29 04:22:39 -0700329 vector<uint8_t> key_blob_;
Shawn Willden7f424372021-01-10 18:06:50 -0700330 vector<KeyCharacteristics> key_characteristics_;
331
332 const vector<KeyParameter>& SecLevelAuthorizations(
333 const vector<KeyCharacteristics>& key_characteristics);
334 inline const vector<KeyParameter>& SecLevelAuthorizations() {
335 return SecLevelAuthorizations(key_characteristics_);
336 }
Qi Wubeefae42021-01-28 23:16:37 +0800337 const vector<KeyParameter>& SecLevelAuthorizations(
338 const vector<KeyCharacteristics>& key_characteristics, SecurityLevel securityLevel);
339
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000340 ErrorCode UseAesKey(const vector<uint8_t>& aesKeyBlob);
341 ErrorCode UseHmacKey(const vector<uint8_t>& hmacKeyBlob);
342 ErrorCode UseRsaKey(const vector<uint8_t>& rsaKeyBlob);
343 ErrorCode UseEcdsaKey(const vector<uint8_t>& ecdsaKeyBlob);
Selene Huang31ab4042020-04-29 04:22:39 -0700344
Shawn Willdend659c7c2021-02-19 14:51:51 -0700345 protected:
Janis Danisevskis24c04702020-12-16 18:28:39 -0800346 std::shared_ptr<IKeyMintDevice> keymint_;
Selene Huang31ab4042020-04-29 04:22:39 -0700347 uint32_t os_version_;
348 uint32_t os_patch_level_;
David Drysdalebb3d85e2021-04-13 11:15:51 +0100349 uint32_t vendor_patch_level_;
David Drysdaled2cc8c22021-04-15 13:29:45 +0100350 bool timestamp_token_required_;
Selene Huang31ab4042020-04-29 04:22:39 -0700351
352 SecurityLevel securityLevel_;
353 string name_;
354 string author_;
355 long challenge_;
Prashant Patildd5f7f02022-07-06 18:58:07 +0000356
357 private:
358 void CheckEncryptOneByteAtATime(BlockMode block_mode, const int block_size,
359 PaddingMode padding_mode, const string& iv,
360 const string& plaintext, const string& exp_cipher_text);
Selene Huang31ab4042020-04-29 04:22:39 -0700361};
362
David Drysdalea676c3b2021-06-14 14:46:02 +0100363// If the given property is available, add it to the tag set under the given tag ID.
364template <Tag tag>
365void add_tag_from_prop(AuthorizationSetBuilder* tags, TypedTag<TagType::BYTES, tag> ttag,
366 const char* prop) {
367 std::string prop_value = ::android::base::GetProperty(prop, /* default= */ "");
368 if (!prop_value.empty()) {
369 tags->Authorization(ttag, prop_value.data(), prop_value.size());
370 }
371}
372
Shawn Willden22fb9c12022-06-02 14:04:33 -0600373// Return the VSR API level for this device.
374int get_vsr_api_level();
375
David Drysdale555ba002022-05-03 18:48:57 +0100376// Indicate whether the test is running on a GSI image.
377bool is_gsi_image();
378
Selene Huang6e46f142021-04-20 19:20:11 -0700379vector<uint8_t> build_serial_blob(const uint64_t serial_int);
380void verify_subject(const X509* cert, const string& subject, bool self_signed);
381void verify_serial(X509* cert, const uint64_t expected_serial);
382void verify_subject_and_serial(const Certificate& certificate, //
383 const uint64_t expected_serial, //
384 const string& subject, bool self_signed);
Shawn Willden4315e132022-03-20 12:49:46 -0600385void verify_root_of_trust(const vector<uint8_t>& verified_boot_key, //
386 bool device_locked, //
387 VerifiedBoot verified_boot_state, //
388 const vector<uint8_t>& verified_boot_hash);
David Drysdale7dff4fc2021-12-10 10:10:52 +0000389bool verify_attestation_record(int aidl_version, //
390 const string& challenge, //
Shawn Willden7c130392020-12-21 09:58:22 -0700391 const string& app_id, //
392 AuthorizationSet expected_sw_enforced, //
393 AuthorizationSet expected_hw_enforced, //
394 SecurityLevel security_level,
David Drysdale565ccc72021-10-11 12:49:50 +0100395 const vector<uint8_t>& attestation_cert,
396 vector<uint8_t>* unique_id = nullptr);
Selene Huang6e46f142021-04-20 19:20:11 -0700397
Shawn Willden7c130392020-12-21 09:58:22 -0700398string bin2hex(const vector<uint8_t>& data);
399X509_Ptr parse_cert_blob(const vector<uint8_t>& blob);
David Drysdalef0d516d2021-03-22 07:51:43 +0000400vector<uint8_t> make_name_from_str(const string& name);
David Drysdale4dc01072021-04-01 12:17:35 +0100401void check_maced_pubkey(const MacedPublicKey& macedPubKey, bool testMode,
402 vector<uint8_t>* payload_value);
403void p256_pub_key(const vector<uint8_t>& coseKeyData, EVP_PKEY_Ptr* signingKey);
Max Biresa97ec692022-11-21 23:37:54 -0800404void device_id_attestation_vsr_check(const ErrorCode& result);
David Drysdale4dc01072021-04-01 12:17:35 +0100405
David Drysdalef0d516d2021-03-22 07:51:43 +0000406AuthorizationSet HwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics);
407AuthorizationSet SwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics);
Eran Messeri03d7a1a2021-07-06 12:07:57 +0100408::testing::AssertionResult ChainSignaturesAreValid(const vector<Certificate>& chain,
409 bool strict_issuer_check = true);
Shawn Willden7c130392020-12-21 09:58:22 -0700410
Selene Huang31ab4042020-04-29 04:22:39 -0700411#define INSTANTIATE_KEYMINT_AIDL_TEST(name) \
412 INSTANTIATE_TEST_SUITE_P(PerInstance, name, \
413 testing::ValuesIn(KeyMintAidlTestBase::build_params()), \
Shawn Willden7e71f1e2021-04-01 16:44:22 -0600414 ::android::PrintInstanceNameToString); \
415 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(name);
Selene Huang31ab4042020-04-29 04:22:39 -0700416
Shawn Willden0e80b5d2020-12-17 09:07:27 -0700417} // namespace test
418
419} // namespace aidl::android::hardware::security::keymint