blob: 5b09ca5c3efd84e415e9f5d089368627371ada3a [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
Selene Huang31ab4042020-04-29 04:22:39 -070067 void SetUp() override;
68 void TearDown() override {
69 if (key_blob_.size()) {
70 CheckedDeleteKey();
71 }
72 AbortIfNeeded();
73 }
74
Janis Danisevskis24c04702020-12-16 18:28:39 -080075 void InitializeKeyMint(std::shared_ptr<IKeyMintDevice> keyMint);
Selene Huang31ab4042020-04-29 04:22:39 -070076 IKeyMintDevice& keyMint() { return *keymint_; }
David Drysdale7dff4fc2021-12-10 10:10:52 +000077 int32_t AidlVersion();
Selene Huang31ab4042020-04-29 04:22:39 -070078 uint32_t os_version() { return os_version_; }
79 uint32_t os_patch_level() { return os_patch_level_; }
David Drysdalebb3d85e2021-04-13 11:15:51 +010080 uint32_t vendor_patch_level() { return vendor_patch_level_; }
David Drysdale37af4b32021-05-14 16:46:59 +010081 uint32_t boot_patch_level(const vector<KeyCharacteristics>& key_characteristics);
82 uint32_t boot_patch_level();
Prashant Patil88ad1892022-03-15 16:31:02 +000083 bool isDeviceIdAttestationRequired();
Selene Huang31ab4042020-04-29 04:22:39 -070084
David Drysdale42fe1892021-10-14 14:43:46 +010085 bool Curve25519Supported();
86
Janis Danisevskis24c04702020-12-16 18:28:39 -080087 ErrorCode GetReturnErrorCode(const Status& result);
Selene Huang31ab4042020-04-29 04:22:39 -070088
Shawn Willden7c130392020-12-21 09:58:22 -070089 ErrorCode GenerateKey(const AuthorizationSet& key_desc, vector<uint8_t>* key_blob,
90 vector<KeyCharacteristics>* key_characteristics) {
91 return GenerateKey(key_desc, std::nullopt /* attest_key */, key_blob, key_characteristics,
92 &cert_chain_);
93 }
94 ErrorCode GenerateKey(const AuthorizationSet& key_desc,
95 const optional<AttestationKey>& attest_key, vector<uint8_t>* key_blob,
96 vector<KeyCharacteristics>* key_characteristics,
97 vector<Certificate>* cert_chain);
98 ErrorCode GenerateKey(const AuthorizationSet& key_desc,
99 const optional<AttestationKey>& attest_key = std::nullopt);
100
subrahmanyaman7d9bc462022-03-16 01:40:39 +0000101 // Generate key for implementations which do not support factory attestation.
102 ErrorCode GenerateKeyWithSelfSignedAttestKey(const AuthorizationSet& attest_key_desc,
103 const AuthorizationSet& key_desc,
104 vector<uint8_t>* key_blob,
105 vector<KeyCharacteristics>* key_characteristics,
106 vector<Certificate>* cert_chain);
107
108 ErrorCode GenerateKeyWithSelfSignedAttestKey(const AuthorizationSet& attest_key_desc,
109 const AuthorizationSet& key_desc,
110 vector<uint8_t>* key_blob,
111 vector<KeyCharacteristics>* key_characteristics) {
112 return GenerateKeyWithSelfSignedAttestKey(attest_key_desc, key_desc, key_blob,
113 key_characteristics, &cert_chain_);
114 }
115
Selene Huang31ab4042020-04-29 04:22:39 -0700116 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
117 const string& key_material, vector<uint8_t>* key_blob,
Shawn Willden7f424372021-01-10 18:06:50 -0700118 vector<KeyCharacteristics>* key_characteristics);
Selene Huang31ab4042020-04-29 04:22:39 -0700119 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
120 const string& key_material);
121
122 ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
123 const AuthorizationSet& wrapping_key_desc, string masking_key,
David Drysdaled2cc8c22021-04-15 13:29:45 +0100124 const AuthorizationSet& unwrapping_params, int64_t password_sid,
125 int64_t biometric_sid);
126 ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
127 const AuthorizationSet& wrapping_key_desc, string masking_key,
128 const AuthorizationSet& unwrapping_params) {
129 return ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, masking_key,
130 unwrapping_params, 0 /* password_sid */, 0 /* biometric_sid */);
131 }
Selene Huang31ab4042020-04-29 04:22:39 -0700132
David Drysdale300b5552021-05-20 12:05:26 +0100133 ErrorCode GetCharacteristics(const vector<uint8_t>& key_blob, const vector<uint8_t>& app_id,
134 const vector<uint8_t>& app_data,
135 vector<KeyCharacteristics>* key_characteristics);
136 ErrorCode GetCharacteristics(const vector<uint8_t>& key_blob,
137 vector<KeyCharacteristics>* key_characteristics);
138
139 void CheckCharacteristics(const vector<uint8_t>& key_blob,
140 const vector<KeyCharacteristics>& generate_characteristics);
141 void CheckAppIdCharacteristics(const vector<uint8_t>& key_blob, std::string_view app_id_string,
142 std::string_view app_data_string,
143 const vector<KeyCharacteristics>& generate_characteristics);
144
Selene Huang31ab4042020-04-29 04:22:39 -0700145 ErrorCode DeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false);
146 ErrorCode DeleteKey(bool keep_key_blob = false);
147
148 ErrorCode DeleteAllKeys();
149
David Drysdaled2cc8c22021-04-15 13:29:45 +0100150 ErrorCode DestroyAttestationIds();
151
Selene Huang31ab4042020-04-29 04:22:39 -0700152 void CheckedDeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false);
153 void CheckedDeleteKey();
154
155 ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob,
156 const AuthorizationSet& in_params, AuthorizationSet* out_params,
Janis Danisevskis24c04702020-12-16 18:28:39 -0800157 std::shared_ptr<IKeyMintOperation>& op);
Selene Huang31ab4042020-04-29 04:22:39 -0700158 ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob,
159 const AuthorizationSet& in_params, AuthorizationSet* out_params);
160 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
161 AuthorizationSet* out_params);
162 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params);
163
Shawn Willden92d79c02021-02-19 07:31:55 -0700164 ErrorCode UpdateAad(const string& input);
165 ErrorCode Update(const string& input, string* output);
Selene Huang31ab4042020-04-29 04:22:39 -0700166
Selene Huang31ab4042020-04-29 04:22:39 -0700167 ErrorCode Finish(const string& message, const string& signature, string* output);
Shawn Willden92d79c02021-02-19 07:31:55 -0700168 ErrorCode Finish(const string& message, string* output) {
169 return Finish(message, {} /* signature */, output);
170 }
171 ErrorCode Finish(string* output) { return Finish({} /* message */, output); }
Selene Huang31ab4042020-04-29 04:22:39 -0700172
173 ErrorCode Abort();
Janis Danisevskis24c04702020-12-16 18:28:39 -0800174 ErrorCode Abort(const shared_ptr<IKeyMintOperation>& op);
Selene Huang31ab4042020-04-29 04:22:39 -0700175 void AbortIfNeeded();
176
177 string ProcessMessage(const vector<uint8_t>& key_blob, KeyPurpose operation,
178 const string& message, const AuthorizationSet& in_params,
179 AuthorizationSet* out_params);
Shawn Willden92d79c02021-02-19 07:31:55 -0700180 std::tuple<ErrorCode, std::string /* processedMessage */> ProcessMessage(
181 const vector<uint8_t>& key_blob, KeyPurpose operation, const std::string& message,
182 const AuthorizationSet& in_params);
Selene Huang31ab4042020-04-29 04:22:39 -0700183 string SignMessage(const vector<uint8_t>& key_blob, const string& message,
184 const AuthorizationSet& params);
185 string SignMessage(const string& message, const AuthorizationSet& params);
186
187 string MacMessage(const string& message, Digest digest, size_t mac_length);
188
anil.hiranniah19a4ca12022-03-03 17:39:30 +0530189 void CheckAesIncrementalEncryptOperation(BlockMode block_mode, int message_size);
190
Prashant Patildd5f7f02022-07-06 18:58:07 +0000191 void AesCheckEncryptOneByteAtATime(const string& key, BlockMode block_mode,
192 PaddingMode padding_mode, const string& iv,
193 const string& plaintext, const string& exp_cipher_text);
194
Selene Huang31ab4042020-04-29 04:22:39 -0700195 void CheckHmacTestVector(const string& key, const string& message, Digest digest,
196 const string& expected_mac);
197
198 void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message,
199 const string& expected_ciphertext);
200
201 void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
202 PaddingMode padding_mode, const string& key, const string& iv,
203 const string& input, const string& expected_output);
204
205 void VerifyMessage(const vector<uint8_t>& key_blob, const string& message,
206 const string& signature, const AuthorizationSet& params);
207 void VerifyMessage(const string& message, const string& signature,
208 const AuthorizationSet& params);
David Drysdaledf8f52e2021-05-06 08:10:58 +0100209 void LocalVerifyMessage(const string& message, const string& signature,
210 const AuthorizationSet& params);
Selene Huang31ab4042020-04-29 04:22:39 -0700211
David Drysdale59cae642021-05-12 13:52:03 +0100212 string LocalRsaEncryptMessage(const string& message, const AuthorizationSet& params);
Selene Huang31ab4042020-04-29 04:22:39 -0700213 string EncryptMessage(const vector<uint8_t>& key_blob, const string& message,
214 const AuthorizationSet& in_params, AuthorizationSet* out_params);
215 string EncryptMessage(const string& message, const AuthorizationSet& params,
216 AuthorizationSet* out_params);
217 string EncryptMessage(const string& message, const AuthorizationSet& params);
218 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding);
219 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
220 vector<uint8_t>* iv_out);
221 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
222 const vector<uint8_t>& iv_in);
223 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
224 uint8_t mac_length_bits, const vector<uint8_t>& iv_in);
David Drysdaled2cc8c22021-04-15 13:29:45 +0100225 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
226 uint8_t mac_length_bits);
Selene Huang31ab4042020-04-29 04:22:39 -0700227
228 string DecryptMessage(const vector<uint8_t>& key_blob, const string& ciphertext,
229 const AuthorizationSet& params);
230 string DecryptMessage(const string& ciphertext, const AuthorizationSet& params);
231 string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode,
232 const vector<uint8_t>& iv);
233
234 std::pair<ErrorCode, vector<uint8_t>> UpgradeKey(const vector<uint8_t>& key_blob);
235
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000236 template <typename TagType>
237 std::tuple<KeyData /* aesKey */, KeyData /* hmacKey */, KeyData /* rsaKey */,
238 KeyData /* ecdsaKey */>
David Drysdaleadfe6112021-05-27 12:00:53 +0100239 CreateTestKeys(
240 TagType tagToTest, ErrorCode expectedReturn,
241 std::function<void(AuthorizationSetBuilder*)> tagModifier =
242 [](AuthorizationSetBuilder*) {}) {
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000243 /* AES */
244 KeyData aesKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100245 AuthorizationSetBuilder aesBuilder = AuthorizationSetBuilder()
246 .AesEncryptionKey(128)
247 .Authorization(tagToTest)
248 .BlockMode(BlockMode::ECB)
249 .Padding(PaddingMode::NONE)
250 .Authorization(TAG_NO_AUTH_REQUIRED);
251 tagModifier(&aesBuilder);
252 ErrorCode errorCode =
253 GenerateKey(aesBuilder, &aesKeyData.blob, &aesKeyData.characteristics);
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000254 EXPECT_EQ(expectedReturn, errorCode);
255
256 /* HMAC */
257 KeyData hmacKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100258 AuthorizationSetBuilder hmacBuilder = AuthorizationSetBuilder()
259 .HmacKey(128)
260 .Authorization(tagToTest)
261 .Digest(Digest::SHA_2_256)
262 .Authorization(TAG_MIN_MAC_LENGTH, 128)
263 .Authorization(TAG_NO_AUTH_REQUIRED);
264 tagModifier(&hmacBuilder);
265 errorCode = GenerateKey(hmacBuilder, &hmacKeyData.blob, &hmacKeyData.characteristics);
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000266 EXPECT_EQ(expectedReturn, errorCode);
267
268 /* RSA */
269 KeyData rsaKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100270 AuthorizationSetBuilder rsaBuilder = AuthorizationSetBuilder()
271 .RsaSigningKey(2048, 65537)
272 .Authorization(tagToTest)
273 .Digest(Digest::NONE)
274 .Padding(PaddingMode::NONE)
275 .Authorization(TAG_NO_AUTH_REQUIRED)
276 .SetDefaultValidity();
277 tagModifier(&rsaBuilder);
278 errorCode = GenerateKey(rsaBuilder, &rsaKeyData.blob, &rsaKeyData.characteristics);
subrahmanyaman05642492022-02-05 07:10:56 +0000279 if (!(SecLevel() == SecurityLevel::STRONGBOX &&
280 ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED == errorCode)) {
281 EXPECT_EQ(expectedReturn, errorCode);
282 }
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000283
284 /* ECDSA */
285 KeyData ecdsaKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100286 AuthorizationSetBuilder ecdsaBuilder = AuthorizationSetBuilder()
David Drysdaledf09e542021-06-08 15:46:11 +0100287 .EcdsaSigningKey(EcCurve::P_256)
David Drysdaleadfe6112021-05-27 12:00:53 +0100288 .Authorization(tagToTest)
289 .Digest(Digest::SHA_2_256)
290 .Authorization(TAG_NO_AUTH_REQUIRED)
291 .SetDefaultValidity();
292 tagModifier(&ecdsaBuilder);
293 errorCode = GenerateKey(ecdsaBuilder, &ecdsaKeyData.blob, &ecdsaKeyData.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 return {aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData};
299 }
Shawn Willden7f424372021-01-10 18:06:50 -0700300 bool IsSecure() const { return securityLevel_ != SecurityLevel::SOFTWARE; }
301 SecurityLevel SecLevel() const { return securityLevel_; }
Selene Huang31ab4042020-04-29 04:22:39 -0700302
303 vector<uint32_t> ValidKeySizes(Algorithm algorithm);
304 vector<uint32_t> InvalidKeySizes(Algorithm algorithm);
305
David Drysdale7de9feb2021-03-05 14:56:19 +0000306 vector<BlockMode> ValidBlockModes(Algorithm algorithm);
307 vector<PaddingMode> ValidPaddingModes(Algorithm algorithm, BlockMode blockMode);
308 vector<PaddingMode> InvalidPaddingModes(Algorithm algorithm, BlockMode blockMode);
309
Selene Huang31ab4042020-04-29 04:22:39 -0700310 vector<EcCurve> ValidCurves();
311 vector<EcCurve> InvalidCurves();
312
313 vector<Digest> ValidDigests(bool withNone, bool withMD5);
subrahmanyaman05642492022-02-05 07:10:56 +0000314 vector<uint64_t> ValidExponents();
Selene Huang31ab4042020-04-29 04:22:39 -0700315
316 static vector<string> build_params() {
Janis Danisevskis24c04702020-12-16 18:28:39 -0800317 auto params = ::android::getAidlHalInstanceNames(IKeyMintDevice::descriptor);
Selene Huang31ab4042020-04-29 04:22:39 -0700318 return params;
319 }
320
Janis Danisevskis24c04702020-12-16 18:28:39 -0800321 std::shared_ptr<IKeyMintOperation> op_;
Shawn Willden7f424372021-01-10 18:06:50 -0700322 vector<Certificate> cert_chain_;
Selene Huang31ab4042020-04-29 04:22:39 -0700323 vector<uint8_t> key_blob_;
Shawn Willden7f424372021-01-10 18:06:50 -0700324 vector<KeyCharacteristics> key_characteristics_;
325
326 const vector<KeyParameter>& SecLevelAuthorizations(
327 const vector<KeyCharacteristics>& key_characteristics);
328 inline const vector<KeyParameter>& SecLevelAuthorizations() {
329 return SecLevelAuthorizations(key_characteristics_);
330 }
Qi Wubeefae42021-01-28 23:16:37 +0800331 const vector<KeyParameter>& SecLevelAuthorizations(
332 const vector<KeyCharacteristics>& key_characteristics, SecurityLevel securityLevel);
333
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000334 ErrorCode UseAesKey(const vector<uint8_t>& aesKeyBlob);
335 ErrorCode UseHmacKey(const vector<uint8_t>& hmacKeyBlob);
336 ErrorCode UseRsaKey(const vector<uint8_t>& rsaKeyBlob);
337 ErrorCode UseEcdsaKey(const vector<uint8_t>& ecdsaKeyBlob);
Selene Huang31ab4042020-04-29 04:22:39 -0700338
Shawn Willdend659c7c2021-02-19 14:51:51 -0700339 protected:
Janis Danisevskis24c04702020-12-16 18:28:39 -0800340 std::shared_ptr<IKeyMintDevice> keymint_;
Selene Huang31ab4042020-04-29 04:22:39 -0700341 uint32_t os_version_;
342 uint32_t os_patch_level_;
David Drysdalebb3d85e2021-04-13 11:15:51 +0100343 uint32_t vendor_patch_level_;
David Drysdaled2cc8c22021-04-15 13:29:45 +0100344 bool timestamp_token_required_;
Selene Huang31ab4042020-04-29 04:22:39 -0700345
346 SecurityLevel securityLevel_;
347 string name_;
348 string author_;
349 long challenge_;
Prashant Patildd5f7f02022-07-06 18:58:07 +0000350
351 private:
352 void CheckEncryptOneByteAtATime(BlockMode block_mode, const int block_size,
353 PaddingMode padding_mode, const string& iv,
354 const string& plaintext, const string& exp_cipher_text);
Selene Huang31ab4042020-04-29 04:22:39 -0700355};
356
David Drysdalea676c3b2021-06-14 14:46:02 +0100357// If the given property is available, add it to the tag set under the given tag ID.
358template <Tag tag>
359void add_tag_from_prop(AuthorizationSetBuilder* tags, TypedTag<TagType::BYTES, tag> ttag,
360 const char* prop) {
361 std::string prop_value = ::android::base::GetProperty(prop, /* default= */ "");
362 if (!prop_value.empty()) {
363 tags->Authorization(ttag, prop_value.data(), prop_value.size());
364 }
365}
366
Shawn Willden22fb9c12022-06-02 14:04:33 -0600367// Return the VSR API level for this device.
368int get_vsr_api_level();
369
David Drysdale555ba002022-05-03 18:48:57 +0100370// Indicate whether the test is running on a GSI image.
371bool is_gsi_image();
372
Selene Huang6e46f142021-04-20 19:20:11 -0700373vector<uint8_t> build_serial_blob(const uint64_t serial_int);
374void verify_subject(const X509* cert, const string& subject, bool self_signed);
375void verify_serial(X509* cert, const uint64_t expected_serial);
376void verify_subject_and_serial(const Certificate& certificate, //
377 const uint64_t expected_serial, //
378 const string& subject, bool self_signed);
Shawn Willden4315e132022-03-20 12:49:46 -0600379void verify_root_of_trust(const vector<uint8_t>& verified_boot_key, //
380 bool device_locked, //
381 VerifiedBoot verified_boot_state, //
382 const vector<uint8_t>& verified_boot_hash);
David Drysdale7dff4fc2021-12-10 10:10:52 +0000383bool verify_attestation_record(int aidl_version, //
384 const string& challenge, //
Shawn Willden7c130392020-12-21 09:58:22 -0700385 const string& app_id, //
386 AuthorizationSet expected_sw_enforced, //
387 AuthorizationSet expected_hw_enforced, //
388 SecurityLevel security_level,
David Drysdale565ccc72021-10-11 12:49:50 +0100389 const vector<uint8_t>& attestation_cert,
390 vector<uint8_t>* unique_id = nullptr);
Selene Huang6e46f142021-04-20 19:20:11 -0700391
Shawn Willden7c130392020-12-21 09:58:22 -0700392string bin2hex(const vector<uint8_t>& data);
393X509_Ptr parse_cert_blob(const vector<uint8_t>& blob);
David Drysdalef0d516d2021-03-22 07:51:43 +0000394vector<uint8_t> make_name_from_str(const string& name);
David Drysdale4dc01072021-04-01 12:17:35 +0100395void check_maced_pubkey(const MacedPublicKey& macedPubKey, bool testMode,
396 vector<uint8_t>* payload_value);
397void p256_pub_key(const vector<uint8_t>& coseKeyData, EVP_PKEY_Ptr* signingKey);
Max Biresa97ec692022-11-21 23:37:54 -0800398void device_id_attestation_vsr_check(const ErrorCode& result);
David Drysdale4dc01072021-04-01 12:17:35 +0100399
David Drysdalef0d516d2021-03-22 07:51:43 +0000400AuthorizationSet HwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics);
401AuthorizationSet SwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics);
Eran Messeri03d7a1a2021-07-06 12:07:57 +0100402::testing::AssertionResult ChainSignaturesAreValid(const vector<Certificate>& chain,
403 bool strict_issuer_check = true);
Shawn Willden7c130392020-12-21 09:58:22 -0700404
Selene Huang31ab4042020-04-29 04:22:39 -0700405#define INSTANTIATE_KEYMINT_AIDL_TEST(name) \
406 INSTANTIATE_TEST_SUITE_P(PerInstance, name, \
407 testing::ValuesIn(KeyMintAidlTestBase::build_params()), \
Shawn Willden7e71f1e2021-04-01 16:44:22 -0600408 ::android::PrintInstanceNameToString); \
409 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(name);
Selene Huang31ab4042020-04-29 04:22:39 -0700410
Shawn Willden0e80b5d2020-12-17 09:07:27 -0700411} // namespace test
412
413} // namespace aidl::android::hardware::security::keymint