blob: 1bf2d9d59ecbff15d4a5f0b97da3d5afd3cad66b [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";
David Drysdale6c9bdb82024-01-23 09:32:04 +000059const string FEATURE_HARDWARE_KEYSTORE = "android.hardware.hardware_keystore";
Subrahmanyaman50fcf7d2023-04-20 22:48:39 +000060
David Drysdale1b9febc2023-06-07 13:43:24 +010061// RAII class to ensure that a keyblob is deleted regardless of how a test exits.
62class KeyBlobDeleter {
63 public:
64 KeyBlobDeleter(const shared_ptr<IKeyMintDevice>& keymint, const vector<uint8_t>& key_blob)
65 : keymint_(keymint), key_blob_(key_blob) {}
66 ~KeyBlobDeleter();
67
68 private:
69 shared_ptr<IKeyMintDevice> keymint_;
70 vector<uint8_t> key_blob_;
71};
72
Selene Huang31ab4042020-04-29 04:22:39 -070073class KeyMintAidlTestBase : public ::testing::TestWithParam<string> {
74 public:
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +000075 struct KeyData {
76 vector<uint8_t> blob;
77 vector<KeyCharacteristics> characteristics;
78 };
79
Shawn Willden7c130392020-12-21 09:58:22 -070080 static bool arm_deleteAllKeys;
81 static bool dump_Attestations;
82
David Drysdale9f5c0c52022-11-03 15:10:16 +000083 // Directory to store/retrieve keyblobs, using subdirectories named for the
84 // KeyMint instance in question (e.g. "./default/", "./strongbox/").
85 static std::string keyblob_dir;
Tommy Chiu025f3c52023-05-15 06:23:44 +000086 // To specify if users expect an upgrade on the keyBlobs.
87 static std::optional<bool> expect_upgrade;
David Drysdale9f5c0c52022-11-03 15:10:16 +000088
Selene Huang31ab4042020-04-29 04:22:39 -070089 void SetUp() override;
90 void TearDown() override {
91 if (key_blob_.size()) {
92 CheckedDeleteKey();
93 }
94 AbortIfNeeded();
95 }
96
Janis Danisevskis24c04702020-12-16 18:28:39 -080097 void InitializeKeyMint(std::shared_ptr<IKeyMintDevice> keyMint);
Selene Huang31ab4042020-04-29 04:22:39 -070098 IKeyMintDevice& keyMint() { return *keymint_; }
Prashant Patil2114dca2023-09-21 14:57:10 +000099 int32_t AidlVersion() const;
Selene Huang31ab4042020-04-29 04:22:39 -0700100 uint32_t os_version() { return os_version_; }
101 uint32_t os_patch_level() { return os_patch_level_; }
David Drysdalebb3d85e2021-04-13 11:15:51 +0100102 uint32_t vendor_patch_level() { return vendor_patch_level_; }
David Drysdale37af4b32021-05-14 16:46:59 +0100103 uint32_t boot_patch_level(const vector<KeyCharacteristics>& key_characteristics);
104 uint32_t boot_patch_level();
Prashant Patil88ad1892022-03-15 16:31:02 +0000105 bool isDeviceIdAttestationRequired();
Rajesh Nyamagoud5283f812023-01-06 00:27:56 +0000106 bool isSecondImeiIdAttestationRequired();
Seth Moorec5c52ce2024-04-07 15:26:33 -0700107 bool isRkpOnly();
Selene Huang31ab4042020-04-29 04:22:39 -0700108
David Drysdale42fe1892021-10-14 14:43:46 +0100109 bool Curve25519Supported();
110
Seth Moorec5c52ce2024-04-07 15:26:33 -0700111 ErrorCode GenerateKey(const AuthorizationSet& key_desc);
112
Shawn Willden7c130392020-12-21 09:58:22 -0700113 ErrorCode GenerateKey(const AuthorizationSet& key_desc, vector<uint8_t>* key_blob,
Seth Moorec5c52ce2024-04-07 15:26:33 -0700114 vector<KeyCharacteristics>* key_characteristics);
115
Shawn Willden7c130392020-12-21 09:58:22 -0700116 ErrorCode GenerateKey(const AuthorizationSet& key_desc,
117 const optional<AttestationKey>& attest_key, vector<uint8_t>* key_blob,
118 vector<KeyCharacteristics>* key_characteristics,
119 vector<Certificate>* cert_chain);
subrahmanyaman7d9bc462022-03-16 01:40:39 +0000120
Selene Huang31ab4042020-04-29 04:22:39 -0700121 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
122 const string& key_material, vector<uint8_t>* key_blob,
Shawn Willden7f424372021-01-10 18:06:50 -0700123 vector<KeyCharacteristics>* key_characteristics);
Selene Huang31ab4042020-04-29 04:22:39 -0700124 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
125 const string& key_material);
126
127 ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
128 const AuthorizationSet& wrapping_key_desc, string masking_key,
David Drysdaled2cc8c22021-04-15 13:29:45 +0100129 const AuthorizationSet& unwrapping_params, int64_t password_sid,
130 int64_t biometric_sid);
131 ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
132 const AuthorizationSet& wrapping_key_desc, string masking_key,
133 const AuthorizationSet& unwrapping_params) {
134 return ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, masking_key,
135 unwrapping_params, 0 /* password_sid */, 0 /* biometric_sid */);
136 }
Selene Huang31ab4042020-04-29 04:22:39 -0700137
David Drysdale300b5552021-05-20 12:05:26 +0100138 ErrorCode GetCharacteristics(const vector<uint8_t>& key_blob, const vector<uint8_t>& app_id,
139 const vector<uint8_t>& app_data,
140 vector<KeyCharacteristics>* key_characteristics);
141 ErrorCode GetCharacteristics(const vector<uint8_t>& key_blob,
142 vector<KeyCharacteristics>* key_characteristics);
143
144 void CheckCharacteristics(const vector<uint8_t>& key_blob,
145 const vector<KeyCharacteristics>& generate_characteristics);
146 void CheckAppIdCharacteristics(const vector<uint8_t>& key_blob, std::string_view app_id_string,
147 std::string_view app_data_string,
148 const vector<KeyCharacteristics>& generate_characteristics);
149
Selene Huang31ab4042020-04-29 04:22:39 -0700150 ErrorCode DeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false);
151 ErrorCode DeleteKey(bool keep_key_blob = false);
152
153 ErrorCode DeleteAllKeys();
154
David Drysdaled2cc8c22021-04-15 13:29:45 +0100155 ErrorCode DestroyAttestationIds();
156
Selene Huang31ab4042020-04-29 04:22:39 -0700157 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,
David Drysdale28fa9312023-02-01 14:53:01 +0000163 const AuthorizationSet& in_params, AuthorizationSet* out_params,
164 std::optional<HardwareAuthToken> hat = std::nullopt);
Selene Huang31ab4042020-04-29 04:22:39 -0700165 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
166 AuthorizationSet* out_params);
167 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params);
168
Shawn Willden92d79c02021-02-19 07:31:55 -0700169 ErrorCode UpdateAad(const string& input);
170 ErrorCode Update(const string& input, string* output);
Selene Huang31ab4042020-04-29 04:22:39 -0700171
David Drysdale28fa9312023-02-01 14:53:01 +0000172 ErrorCode Finish(const string& message, const string& signature, string* output,
173 std::optional<HardwareAuthToken> hat = std::nullopt,
174 std::optional<secureclock::TimeStampToken> time_token = std::nullopt);
Shawn Willden92d79c02021-02-19 07:31:55 -0700175 ErrorCode Finish(const string& message, string* output) {
176 return Finish(message, {} /* signature */, output);
177 }
178 ErrorCode Finish(string* output) { return Finish({} /* message */, output); }
Selene Huang31ab4042020-04-29 04:22:39 -0700179
180 ErrorCode Abort();
Janis Danisevskis24c04702020-12-16 18:28:39 -0800181 ErrorCode Abort(const shared_ptr<IKeyMintOperation>& op);
Selene Huang31ab4042020-04-29 04:22:39 -0700182 void AbortIfNeeded();
183
184 string ProcessMessage(const vector<uint8_t>& key_blob, KeyPurpose operation,
185 const string& message, const AuthorizationSet& in_params,
186 AuthorizationSet* out_params);
Shawn Willden92d79c02021-02-19 07:31:55 -0700187 std::tuple<ErrorCode, std::string /* processedMessage */> ProcessMessage(
188 const vector<uint8_t>& key_blob, KeyPurpose operation, const std::string& message,
189 const AuthorizationSet& in_params);
Selene Huang31ab4042020-04-29 04:22:39 -0700190 string SignMessage(const vector<uint8_t>& key_blob, const string& message,
191 const AuthorizationSet& params);
192 string SignMessage(const string& message, const AuthorizationSet& params);
193
194 string MacMessage(const string& message, Digest digest, size_t mac_length);
195
anil.hiranniah19a4ca12022-03-03 17:39:30 +0530196 void CheckAesIncrementalEncryptOperation(BlockMode block_mode, int message_size);
197
Prashant Patildd5f7f02022-07-06 18:58:07 +0000198 void AesCheckEncryptOneByteAtATime(const string& key, BlockMode block_mode,
199 PaddingMode padding_mode, const string& iv,
200 const string& plaintext, const string& exp_cipher_text);
201
Selene Huang31ab4042020-04-29 04:22:39 -0700202 void CheckHmacTestVector(const string& key, const string& message, Digest digest,
203 const string& expected_mac);
204
205 void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message,
206 const string& expected_ciphertext);
207
208 void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
209 PaddingMode padding_mode, const string& key, const string& iv,
210 const string& input, const string& expected_output);
211
212 void VerifyMessage(const vector<uint8_t>& key_blob, const string& message,
213 const string& signature, const AuthorizationSet& params);
214 void VerifyMessage(const string& message, const string& signature,
215 const AuthorizationSet& params);
David Drysdale9f5c0c52022-11-03 15:10:16 +0000216 void LocalVerifyMessage(const vector<uint8_t>& der_cert, const string& message,
217 const string& signature, const AuthorizationSet& params);
David Drysdaledf8f52e2021-05-06 08:10:58 +0100218 void LocalVerifyMessage(const string& message, const string& signature,
219 const AuthorizationSet& params);
Selene Huang31ab4042020-04-29 04:22:39 -0700220
David Drysdale59cae642021-05-12 13:52:03 +0100221 string LocalRsaEncryptMessage(const string& message, const AuthorizationSet& params);
Selene Huang31ab4042020-04-29 04:22:39 -0700222 string EncryptMessage(const vector<uint8_t>& key_blob, const string& message,
223 const AuthorizationSet& in_params, AuthorizationSet* out_params);
224 string EncryptMessage(const string& message, const AuthorizationSet& params,
225 AuthorizationSet* out_params);
226 string EncryptMessage(const string& message, const AuthorizationSet& params);
227 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding);
228 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
229 vector<uint8_t>* iv_out);
230 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
231 const vector<uint8_t>& iv_in);
232 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
233 uint8_t mac_length_bits, const vector<uint8_t>& iv_in);
David Drysdaled2cc8c22021-04-15 13:29:45 +0100234 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
235 uint8_t mac_length_bits);
Selene Huang31ab4042020-04-29 04:22:39 -0700236
237 string DecryptMessage(const vector<uint8_t>& key_blob, const string& ciphertext,
238 const AuthorizationSet& params);
239 string DecryptMessage(const string& ciphertext, const AuthorizationSet& params);
240 string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode,
241 const vector<uint8_t>& iv);
242
243 std::pair<ErrorCode, vector<uint8_t>> UpgradeKey(const vector<uint8_t>& key_blob);
244
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000245 template <typename TagType>
246 std::tuple<KeyData /* aesKey */, KeyData /* hmacKey */, KeyData /* rsaKey */,
247 KeyData /* ecdsaKey */>
David Drysdaleadfe6112021-05-27 12:00:53 +0100248 CreateTestKeys(
249 TagType tagToTest, ErrorCode expectedReturn,
250 std::function<void(AuthorizationSetBuilder*)> tagModifier =
251 [](AuthorizationSetBuilder*) {}) {
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000252 /* AES */
253 KeyData aesKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100254 AuthorizationSetBuilder aesBuilder = AuthorizationSetBuilder()
255 .AesEncryptionKey(128)
256 .Authorization(tagToTest)
257 .BlockMode(BlockMode::ECB)
258 .Padding(PaddingMode::NONE)
259 .Authorization(TAG_NO_AUTH_REQUIRED);
260 tagModifier(&aesBuilder);
261 ErrorCode errorCode =
262 GenerateKey(aesBuilder, &aesKeyData.blob, &aesKeyData.characteristics);
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000263 EXPECT_EQ(expectedReturn, errorCode);
264
265 /* HMAC */
266 KeyData hmacKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100267 AuthorizationSetBuilder hmacBuilder = AuthorizationSetBuilder()
268 .HmacKey(128)
269 .Authorization(tagToTest)
270 .Digest(Digest::SHA_2_256)
271 .Authorization(TAG_MIN_MAC_LENGTH, 128)
272 .Authorization(TAG_NO_AUTH_REQUIRED);
273 tagModifier(&hmacBuilder);
274 errorCode = GenerateKey(hmacBuilder, &hmacKeyData.blob, &hmacKeyData.characteristics);
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000275 EXPECT_EQ(expectedReturn, errorCode);
276
277 /* RSA */
278 KeyData rsaKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100279 AuthorizationSetBuilder rsaBuilder = AuthorizationSetBuilder()
280 .RsaSigningKey(2048, 65537)
281 .Authorization(tagToTest)
282 .Digest(Digest::NONE)
283 .Padding(PaddingMode::NONE)
284 .Authorization(TAG_NO_AUTH_REQUIRED)
285 .SetDefaultValidity();
286 tagModifier(&rsaBuilder);
287 errorCode = GenerateKey(rsaBuilder, &rsaKeyData.blob, &rsaKeyData.characteristics);
subrahmanyaman05642492022-02-05 07:10:56 +0000288 if (!(SecLevel() == SecurityLevel::STRONGBOX &&
289 ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED == errorCode)) {
290 EXPECT_EQ(expectedReturn, errorCode);
291 }
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000292
293 /* ECDSA */
294 KeyData ecdsaKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100295 AuthorizationSetBuilder ecdsaBuilder = AuthorizationSetBuilder()
David Drysdaledf09e542021-06-08 15:46:11 +0100296 .EcdsaSigningKey(EcCurve::P_256)
David Drysdaleadfe6112021-05-27 12:00:53 +0100297 .Authorization(tagToTest)
298 .Digest(Digest::SHA_2_256)
299 .Authorization(TAG_NO_AUTH_REQUIRED)
300 .SetDefaultValidity();
301 tagModifier(&ecdsaBuilder);
302 errorCode = GenerateKey(ecdsaBuilder, &ecdsaKeyData.blob, &ecdsaKeyData.characteristics);
subrahmanyaman05642492022-02-05 07:10:56 +0000303 if (!(SecLevel() == SecurityLevel::STRONGBOX &&
304 ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED == errorCode)) {
305 EXPECT_EQ(expectedReturn, errorCode);
306 }
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000307 return {aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData};
308 }
Shawn Willden7f424372021-01-10 18:06:50 -0700309 bool IsSecure() const { return securityLevel_ != SecurityLevel::SOFTWARE; }
310 SecurityLevel SecLevel() const { return securityLevel_; }
Seth Moorea12ac742023-03-03 13:40:30 -0800311 bool IsRkpSupportRequired() const;
Selene Huang31ab4042020-04-29 04:22:39 -0700312
313 vector<uint32_t> ValidKeySizes(Algorithm algorithm);
314 vector<uint32_t> InvalidKeySizes(Algorithm algorithm);
315
David Drysdale7de9feb2021-03-05 14:56:19 +0000316 vector<BlockMode> ValidBlockModes(Algorithm algorithm);
317 vector<PaddingMode> ValidPaddingModes(Algorithm algorithm, BlockMode blockMode);
318 vector<PaddingMode> InvalidPaddingModes(Algorithm algorithm, BlockMode blockMode);
319
Selene Huang31ab4042020-04-29 04:22:39 -0700320 vector<EcCurve> ValidCurves();
321 vector<EcCurve> InvalidCurves();
322
323 vector<Digest> ValidDigests(bool withNone, bool withMD5);
subrahmanyaman05642492022-02-05 07:10:56 +0000324 vector<uint64_t> ValidExponents();
Selene Huang31ab4042020-04-29 04:22:39 -0700325
326 static vector<string> build_params() {
Janis Danisevskis24c04702020-12-16 18:28:39 -0800327 auto params = ::android::getAidlHalInstanceNames(IKeyMintDevice::descriptor);
Selene Huang31ab4042020-04-29 04:22:39 -0700328 return params;
329 }
330
Janis Danisevskis24c04702020-12-16 18:28:39 -0800331 std::shared_ptr<IKeyMintOperation> op_;
Shawn Willden7f424372021-01-10 18:06:50 -0700332 vector<Certificate> cert_chain_;
Selene Huang31ab4042020-04-29 04:22:39 -0700333 vector<uint8_t> key_blob_;
Shawn Willden7f424372021-01-10 18:06:50 -0700334 vector<KeyCharacteristics> key_characteristics_;
335
336 const vector<KeyParameter>& SecLevelAuthorizations(
337 const vector<KeyCharacteristics>& key_characteristics);
338 inline const vector<KeyParameter>& SecLevelAuthorizations() {
339 return SecLevelAuthorizations(key_characteristics_);
340 }
Qi Wubeefae42021-01-28 23:16:37 +0800341 const vector<KeyParameter>& SecLevelAuthorizations(
342 const vector<KeyCharacteristics>& key_characteristics, SecurityLevel securityLevel);
343
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000344 ErrorCode UseAesKey(const vector<uint8_t>& aesKeyBlob);
345 ErrorCode UseHmacKey(const vector<uint8_t>& hmacKeyBlob);
346 ErrorCode UseRsaKey(const vector<uint8_t>& rsaKeyBlob);
347 ErrorCode UseEcdsaKey(const vector<uint8_t>& ecdsaKeyBlob);
Selene Huang31ab4042020-04-29 04:22:39 -0700348
Subrahmanyaman50fcf7d2023-04-20 22:48:39 +0000349 ErrorCode GenerateAttestKey(const AuthorizationSet& key_desc,
350 const optional<AttestationKey>& attest_key,
351 vector<uint8_t>* key_blob,
352 vector<KeyCharacteristics>* key_characteristics,
353 vector<Certificate>* cert_chain);
354
355 bool is_attest_key_feature_disabled(void) const;
356 bool is_strongbox_enabled(void) const;
357 bool is_chipset_allowed_km4_strongbox(void) const;
David Drysdalec3de1ca2023-05-09 08:10:36 +0100358 bool shouldSkipAttestKeyTest(void) const;
Seth Moorec5c52ce2024-04-07 15:26:33 -0700359 void skipAttestKeyTestIfNeeded() const;
Subrahmanyaman50fcf7d2023-04-20 22:48:39 +0000360
Prashant Patil2114dca2023-09-21 14:57:10 +0000361 void assert_mgf_digests_present_or_not_in_key_characteristics(
362 const vector<KeyCharacteristics>& key_characteristics,
363 std::vector<android::hardware::security::keymint::Digest>& expected_mgf_digests,
364 bool is_mgf_digest_expected) const;
365
366 void assert_mgf_digests_present_or_not_in_key_characteristics(
367 std::vector<android::hardware::security::keymint::Digest>& expected_mgf_digests,
368 bool is_mgf_digest_expected) const;
369
Shawn Willdend659c7c2021-02-19 14:51:51 -0700370 protected:
Janis Danisevskis24c04702020-12-16 18:28:39 -0800371 std::shared_ptr<IKeyMintDevice> keymint_;
Selene Huang31ab4042020-04-29 04:22:39 -0700372 uint32_t os_version_;
373 uint32_t os_patch_level_;
David Drysdalebb3d85e2021-04-13 11:15:51 +0100374 uint32_t vendor_patch_level_;
David Drysdaled2cc8c22021-04-15 13:29:45 +0100375 bool timestamp_token_required_;
Selene Huang31ab4042020-04-29 04:22:39 -0700376
377 SecurityLevel securityLevel_;
378 string name_;
379 string author_;
David Drysdale4cbe2152023-03-07 14:44:38 +0000380 int64_t challenge_;
Prashant Patildd5f7f02022-07-06 18:58:07 +0000381
382 private:
383 void CheckEncryptOneByteAtATime(BlockMode block_mode, const int block_size,
384 PaddingMode padding_mode, const string& iv,
385 const string& plaintext, const string& exp_cipher_text);
Selene Huang31ab4042020-04-29 04:22:39 -0700386};
387
David Drysdalea676c3b2021-06-14 14:46:02 +0100388// If the given property is available, add it to the tag set under the given tag ID.
389template <Tag tag>
390void add_tag_from_prop(AuthorizationSetBuilder* tags, TypedTag<TagType::BYTES, tag> ttag,
391 const char* prop) {
392 std::string prop_value = ::android::base::GetProperty(prop, /* default= */ "");
393 if (!prop_value.empty()) {
394 tags->Authorization(ttag, prop_value.data(), prop_value.size());
395 }
396}
397
Shawn Willden22fb9c12022-06-02 14:04:33 -0600398// Return the VSR API level for this device.
399int get_vsr_api_level();
400
David Drysdale555ba002022-05-03 18:48:57 +0100401// Indicate whether the test is running on a GSI image.
402bool is_gsi_image();
403
Selene Huang6e46f142021-04-20 19:20:11 -0700404vector<uint8_t> build_serial_blob(const uint64_t serial_int);
405void verify_subject(const X509* cert, const string& subject, bool self_signed);
406void verify_serial(X509* cert, const uint64_t expected_serial);
407void verify_subject_and_serial(const Certificate& certificate, //
408 const uint64_t expected_serial, //
409 const string& subject, bool self_signed);
Shawn Willden4315e132022-03-20 12:49:46 -0600410void verify_root_of_trust(const vector<uint8_t>& verified_boot_key, //
411 bool device_locked, //
412 VerifiedBoot verified_boot_state, //
413 const vector<uint8_t>& verified_boot_hash);
David Drysdale7dff4fc2021-12-10 10:10:52 +0000414bool verify_attestation_record(int aidl_version, //
415 const string& challenge, //
Shawn Willden7c130392020-12-21 09:58:22 -0700416 const string& app_id, //
417 AuthorizationSet expected_sw_enforced, //
418 AuthorizationSet expected_hw_enforced, //
419 SecurityLevel security_level,
David Drysdale565ccc72021-10-11 12:49:50 +0100420 const vector<uint8_t>& attestation_cert,
421 vector<uint8_t>* unique_id = nullptr);
Selene Huang6e46f142021-04-20 19:20:11 -0700422
Shawn Willden7c130392020-12-21 09:58:22 -0700423string bin2hex(const vector<uint8_t>& data);
424X509_Ptr parse_cert_blob(const vector<uint8_t>& blob);
Tri Voec50ee12023-02-14 16:29:53 -0800425ASN1_OCTET_STRING* get_attestation_record(X509* certificate);
David Drysdalef0d516d2021-03-22 07:51:43 +0000426vector<uint8_t> make_name_from_str(const string& name);
David Drysdale4dc01072021-04-01 12:17:35 +0100427void check_maced_pubkey(const MacedPublicKey& macedPubKey, bool testMode,
428 vector<uint8_t>* payload_value);
429void p256_pub_key(const vector<uint8_t>& coseKeyData, EVP_PKEY_Ptr* signingKey);
David Drysdalef42238c2023-06-15 09:41:05 +0100430void device_id_attestation_check_acceptable_error(Tag tag, const ErrorCode& result);
David Drysdale3d2ba0a2023-01-11 13:27:26 +0000431bool check_feature(const std::string& name);
David Drysdale6c9bdb82024-01-23 09:32:04 +0000432std::optional<int32_t> keymint_feature_value(bool strongbox);
David Drysdaleef1123b2024-05-28 15:23:08 +0100433std::string get_imei(int slot);
David Drysdale4dc01072021-04-01 12:17:35 +0100434
David Drysdalef0d516d2021-03-22 07:51:43 +0000435AuthorizationSet HwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics);
436AuthorizationSet SwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics);
Eran Messeri03d7a1a2021-07-06 12:07:57 +0100437::testing::AssertionResult ChainSignaturesAreValid(const vector<Certificate>& chain,
438 bool strict_issuer_check = true);
Shawn Willden7c130392020-12-21 09:58:22 -0700439
David Drysdale1b9febc2023-06-07 13:43:24 +0100440ErrorCode GetReturnErrorCode(const Status& result);
441
Selene Huang31ab4042020-04-29 04:22:39 -0700442#define INSTANTIATE_KEYMINT_AIDL_TEST(name) \
443 INSTANTIATE_TEST_SUITE_P(PerInstance, name, \
444 testing::ValuesIn(KeyMintAidlTestBase::build_params()), \
Shawn Willden7e71f1e2021-04-01 16:44:22 -0600445 ::android::PrintInstanceNameToString); \
446 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(name);
Selene Huang31ab4042020-04-29 04:22:39 -0700447
Prashant Patil24f75792023-09-07 15:25:14 +0000448// Use `ro.product.<property>_for_attestation` property for attestation if it is present else
449// fallback to use `ro.product.vendor.<property>` if it is present else fallback to
450// `ro.product.<property>`. Similar logic can be seen in Java method `getVendorDeviceIdProperty`
451// in frameworks/base/core/java/android/os/Build.java.
452template <Tag tag>
453void add_attestation_id(AuthorizationSetBuilder* attestation_id_tags,
454 TypedTag<TagType::BYTES, tag> tag_type, const char* prop) {
455 ::android::String8 prop_name =
456 ::android::String8::format("ro.product.%s_for_attestation", prop);
457 std::string prop_value = ::android::base::GetProperty(prop_name.c_str(), /* default= */ "");
458 if (!prop_value.empty()) {
459 add_tag_from_prop(attestation_id_tags, tag_type, prop_name.c_str());
460 } else {
461 prop_name = ::android::String8::format("ro.product.vendor.%s", prop);
462 prop_value = ::android::base::GetProperty(prop_name.c_str(), /* default= */ "");
463 if (!prop_value.empty()) {
464 add_tag_from_prop(attestation_id_tags, tag_type, prop_name.c_str());
465 } else {
466 prop_name = ::android::String8::format("ro.product.%s", prop);
467 add_tag_from_prop(attestation_id_tags, tag_type, prop_name.c_str());
468 }
469 }
470}
Shawn Willden0e80b5d2020-12-17 09:07:27 -0700471} // namespace test
472
473} // namespace aidl::android::hardware::security::keymint