blob: 8934a57e7b579307b3c4e6c9745c75f7dc9a28c3 [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
David Drysdale1b9febc2023-06-07 13:43:24 +010060// RAII class to ensure that a keyblob is deleted regardless of how a test exits.
61class KeyBlobDeleter {
62 public:
63 KeyBlobDeleter(const shared_ptr<IKeyMintDevice>& keymint, const vector<uint8_t>& key_blob)
64 : keymint_(keymint), key_blob_(key_blob) {}
65 ~KeyBlobDeleter();
66
67 private:
68 shared_ptr<IKeyMintDevice> keymint_;
69 vector<uint8_t> key_blob_;
70};
71
Selene Huang31ab4042020-04-29 04:22:39 -070072class KeyMintAidlTestBase : public ::testing::TestWithParam<string> {
73 public:
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +000074 struct KeyData {
75 vector<uint8_t> blob;
76 vector<KeyCharacteristics> characteristics;
77 };
78
Shawn Willden7c130392020-12-21 09:58:22 -070079 static bool arm_deleteAllKeys;
80 static bool dump_Attestations;
81
David Drysdale9f5c0c52022-11-03 15:10:16 +000082 // Directory to store/retrieve keyblobs, using subdirectories named for the
83 // KeyMint instance in question (e.g. "./default/", "./strongbox/").
84 static std::string keyblob_dir;
Tommy Chiu025f3c52023-05-15 06:23:44 +000085 // To specify if users expect an upgrade on the keyBlobs.
86 static std::optional<bool> expect_upgrade;
David Drysdale9f5c0c52022-11-03 15:10:16 +000087
Selene Huang31ab4042020-04-29 04:22:39 -070088 void SetUp() override;
89 void TearDown() override {
90 if (key_blob_.size()) {
91 CheckedDeleteKey();
92 }
93 AbortIfNeeded();
94 }
95
Janis Danisevskis24c04702020-12-16 18:28:39 -080096 void InitializeKeyMint(std::shared_ptr<IKeyMintDevice> keyMint);
Selene Huang31ab4042020-04-29 04:22:39 -070097 IKeyMintDevice& keyMint() { return *keymint_; }
David Drysdale7dff4fc2021-12-10 10:10:52 +000098 int32_t AidlVersion();
Selene Huang31ab4042020-04-29 04:22:39 -070099 uint32_t os_version() { return os_version_; }
100 uint32_t os_patch_level() { return os_patch_level_; }
David Drysdalebb3d85e2021-04-13 11:15:51 +0100101 uint32_t vendor_patch_level() { return vendor_patch_level_; }
David Drysdale37af4b32021-05-14 16:46:59 +0100102 uint32_t boot_patch_level(const vector<KeyCharacteristics>& key_characteristics);
103 uint32_t boot_patch_level();
Prashant Patil88ad1892022-03-15 16:31:02 +0000104 bool isDeviceIdAttestationRequired();
Rajesh Nyamagoud5283f812023-01-06 00:27:56 +0000105 bool isSecondImeiIdAttestationRequired();
Selene Huang31ab4042020-04-29 04:22:39 -0700106
David Drysdale42fe1892021-10-14 14:43:46 +0100107 bool Curve25519Supported();
108
Shawn Willden7c130392020-12-21 09:58:22 -0700109 ErrorCode GenerateKey(const AuthorizationSet& key_desc, vector<uint8_t>* key_blob,
110 vector<KeyCharacteristics>* key_characteristics) {
111 return GenerateKey(key_desc, std::nullopt /* attest_key */, key_blob, key_characteristics,
112 &cert_chain_);
113 }
114 ErrorCode GenerateKey(const AuthorizationSet& key_desc,
115 const optional<AttestationKey>& attest_key, vector<uint8_t>* key_blob,
116 vector<KeyCharacteristics>* key_characteristics,
117 vector<Certificate>* cert_chain);
118 ErrorCode GenerateKey(const AuthorizationSet& key_desc,
119 const optional<AttestationKey>& attest_key = std::nullopt);
120
subrahmanyaman7d9bc462022-03-16 01:40:39 +0000121 // Generate key for implementations which do not support factory attestation.
122 ErrorCode GenerateKeyWithSelfSignedAttestKey(const AuthorizationSet& attest_key_desc,
123 const AuthorizationSet& key_desc,
124 vector<uint8_t>* key_blob,
125 vector<KeyCharacteristics>* key_characteristics,
126 vector<Certificate>* cert_chain);
127
128 ErrorCode GenerateKeyWithSelfSignedAttestKey(const AuthorizationSet& attest_key_desc,
129 const AuthorizationSet& key_desc,
130 vector<uint8_t>* key_blob,
131 vector<KeyCharacteristics>* key_characteristics) {
132 return GenerateKeyWithSelfSignedAttestKey(attest_key_desc, key_desc, key_blob,
133 key_characteristics, &cert_chain_);
134 }
135
Selene Huang31ab4042020-04-29 04:22:39 -0700136 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
137 const string& key_material, vector<uint8_t>* key_blob,
Shawn Willden7f424372021-01-10 18:06:50 -0700138 vector<KeyCharacteristics>* key_characteristics);
Selene Huang31ab4042020-04-29 04:22:39 -0700139 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
140 const string& key_material);
141
142 ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
143 const AuthorizationSet& wrapping_key_desc, string masking_key,
David Drysdaled2cc8c22021-04-15 13:29:45 +0100144 const AuthorizationSet& unwrapping_params, int64_t password_sid,
145 int64_t biometric_sid);
146 ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
147 const AuthorizationSet& wrapping_key_desc, string masking_key,
148 const AuthorizationSet& unwrapping_params) {
149 return ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, masking_key,
150 unwrapping_params, 0 /* password_sid */, 0 /* biometric_sid */);
151 }
Selene Huang31ab4042020-04-29 04:22:39 -0700152
David Drysdale300b5552021-05-20 12:05:26 +0100153 ErrorCode GetCharacteristics(const vector<uint8_t>& key_blob, const vector<uint8_t>& app_id,
154 const vector<uint8_t>& app_data,
155 vector<KeyCharacteristics>* key_characteristics);
156 ErrorCode GetCharacteristics(const vector<uint8_t>& key_blob,
157 vector<KeyCharacteristics>* key_characteristics);
158
159 void CheckCharacteristics(const vector<uint8_t>& key_blob,
160 const vector<KeyCharacteristics>& generate_characteristics);
161 void CheckAppIdCharacteristics(const vector<uint8_t>& key_blob, std::string_view app_id_string,
162 std::string_view app_data_string,
163 const vector<KeyCharacteristics>& generate_characteristics);
164
Selene Huang31ab4042020-04-29 04:22:39 -0700165 ErrorCode DeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false);
166 ErrorCode DeleteKey(bool keep_key_blob = false);
167
168 ErrorCode DeleteAllKeys();
169
David Drysdaled2cc8c22021-04-15 13:29:45 +0100170 ErrorCode DestroyAttestationIds();
171
Selene Huang31ab4042020-04-29 04:22:39 -0700172 void CheckedDeleteKey();
173
174 ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob,
175 const AuthorizationSet& in_params, AuthorizationSet* out_params,
Janis Danisevskis24c04702020-12-16 18:28:39 -0800176 std::shared_ptr<IKeyMintOperation>& op);
Selene Huang31ab4042020-04-29 04:22:39 -0700177 ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob,
David Drysdale28fa9312023-02-01 14:53:01 +0000178 const AuthorizationSet& in_params, AuthorizationSet* out_params,
179 std::optional<HardwareAuthToken> hat = std::nullopt);
Selene Huang31ab4042020-04-29 04:22:39 -0700180 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
181 AuthorizationSet* out_params);
182 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params);
183
Shawn Willden92d79c02021-02-19 07:31:55 -0700184 ErrorCode UpdateAad(const string& input);
185 ErrorCode Update(const string& input, string* output);
Selene Huang31ab4042020-04-29 04:22:39 -0700186
David Drysdale28fa9312023-02-01 14:53:01 +0000187 ErrorCode Finish(const string& message, const string& signature, string* output,
188 std::optional<HardwareAuthToken> hat = std::nullopt,
189 std::optional<secureclock::TimeStampToken> time_token = std::nullopt);
Shawn Willden92d79c02021-02-19 07:31:55 -0700190 ErrorCode Finish(const string& message, string* output) {
191 return Finish(message, {} /* signature */, output);
192 }
193 ErrorCode Finish(string* output) { return Finish({} /* message */, output); }
Selene Huang31ab4042020-04-29 04:22:39 -0700194
195 ErrorCode Abort();
Janis Danisevskis24c04702020-12-16 18:28:39 -0800196 ErrorCode Abort(const shared_ptr<IKeyMintOperation>& op);
Selene Huang31ab4042020-04-29 04:22:39 -0700197 void AbortIfNeeded();
198
199 string ProcessMessage(const vector<uint8_t>& key_blob, KeyPurpose operation,
200 const string& message, const AuthorizationSet& in_params,
201 AuthorizationSet* out_params);
Shawn Willden92d79c02021-02-19 07:31:55 -0700202 std::tuple<ErrorCode, std::string /* processedMessage */> ProcessMessage(
203 const vector<uint8_t>& key_blob, KeyPurpose operation, const std::string& message,
204 const AuthorizationSet& in_params);
Selene Huang31ab4042020-04-29 04:22:39 -0700205 string SignMessage(const vector<uint8_t>& key_blob, const string& message,
206 const AuthorizationSet& params);
207 string SignMessage(const string& message, const AuthorizationSet& params);
208
209 string MacMessage(const string& message, Digest digest, size_t mac_length);
210
anil.hiranniah19a4ca12022-03-03 17:39:30 +0530211 void CheckAesIncrementalEncryptOperation(BlockMode block_mode, int message_size);
212
Prashant Patildd5f7f02022-07-06 18:58:07 +0000213 void AesCheckEncryptOneByteAtATime(const string& key, BlockMode block_mode,
214 PaddingMode padding_mode, const string& iv,
215 const string& plaintext, const string& exp_cipher_text);
216
Selene Huang31ab4042020-04-29 04:22:39 -0700217 void CheckHmacTestVector(const string& key, const string& message, Digest digest,
218 const string& expected_mac);
219
220 void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message,
221 const string& expected_ciphertext);
222
223 void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
224 PaddingMode padding_mode, const string& key, const string& iv,
225 const string& input, const string& expected_output);
226
227 void VerifyMessage(const vector<uint8_t>& key_blob, const string& message,
228 const string& signature, const AuthorizationSet& params);
229 void VerifyMessage(const string& message, const string& signature,
230 const AuthorizationSet& params);
David Drysdale9f5c0c52022-11-03 15:10:16 +0000231 void LocalVerifyMessage(const vector<uint8_t>& der_cert, const string& message,
232 const string& signature, const AuthorizationSet& params);
David Drysdaledf8f52e2021-05-06 08:10:58 +0100233 void LocalVerifyMessage(const string& message, const string& signature,
234 const AuthorizationSet& params);
Selene Huang31ab4042020-04-29 04:22:39 -0700235
David Drysdale59cae642021-05-12 13:52:03 +0100236 string LocalRsaEncryptMessage(const string& message, const AuthorizationSet& params);
Selene Huang31ab4042020-04-29 04:22:39 -0700237 string EncryptMessage(const vector<uint8_t>& key_blob, const string& message,
238 const AuthorizationSet& in_params, AuthorizationSet* out_params);
239 string EncryptMessage(const string& message, const AuthorizationSet& params,
240 AuthorizationSet* out_params);
241 string EncryptMessage(const string& message, const AuthorizationSet& params);
242 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding);
243 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
244 vector<uint8_t>* iv_out);
245 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
246 const vector<uint8_t>& iv_in);
247 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
248 uint8_t mac_length_bits, const vector<uint8_t>& iv_in);
David Drysdaled2cc8c22021-04-15 13:29:45 +0100249 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
250 uint8_t mac_length_bits);
Selene Huang31ab4042020-04-29 04:22:39 -0700251
252 string DecryptMessage(const vector<uint8_t>& key_blob, const string& ciphertext,
253 const AuthorizationSet& params);
254 string DecryptMessage(const string& ciphertext, const AuthorizationSet& params);
255 string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode,
256 const vector<uint8_t>& iv);
257
258 std::pair<ErrorCode, vector<uint8_t>> UpgradeKey(const vector<uint8_t>& key_blob);
259
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000260 template <typename TagType>
261 std::tuple<KeyData /* aesKey */, KeyData /* hmacKey */, KeyData /* rsaKey */,
262 KeyData /* ecdsaKey */>
David Drysdaleadfe6112021-05-27 12:00:53 +0100263 CreateTestKeys(
264 TagType tagToTest, ErrorCode expectedReturn,
265 std::function<void(AuthorizationSetBuilder*)> tagModifier =
266 [](AuthorizationSetBuilder*) {}) {
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000267 /* AES */
268 KeyData aesKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100269 AuthorizationSetBuilder aesBuilder = AuthorizationSetBuilder()
270 .AesEncryptionKey(128)
271 .Authorization(tagToTest)
272 .BlockMode(BlockMode::ECB)
273 .Padding(PaddingMode::NONE)
274 .Authorization(TAG_NO_AUTH_REQUIRED);
275 tagModifier(&aesBuilder);
276 ErrorCode errorCode =
277 GenerateKey(aesBuilder, &aesKeyData.blob, &aesKeyData.characteristics);
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000278 EXPECT_EQ(expectedReturn, errorCode);
279
280 /* HMAC */
281 KeyData hmacKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100282 AuthorizationSetBuilder hmacBuilder = AuthorizationSetBuilder()
283 .HmacKey(128)
284 .Authorization(tagToTest)
285 .Digest(Digest::SHA_2_256)
286 .Authorization(TAG_MIN_MAC_LENGTH, 128)
287 .Authorization(TAG_NO_AUTH_REQUIRED);
288 tagModifier(&hmacBuilder);
289 errorCode = GenerateKey(hmacBuilder, &hmacKeyData.blob, &hmacKeyData.characteristics);
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000290 EXPECT_EQ(expectedReturn, errorCode);
291
292 /* RSA */
293 KeyData rsaKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100294 AuthorizationSetBuilder rsaBuilder = AuthorizationSetBuilder()
295 .RsaSigningKey(2048, 65537)
296 .Authorization(tagToTest)
297 .Digest(Digest::NONE)
298 .Padding(PaddingMode::NONE)
299 .Authorization(TAG_NO_AUTH_REQUIRED)
300 .SetDefaultValidity();
301 tagModifier(&rsaBuilder);
302 errorCode = GenerateKey(rsaBuilder, &rsaKeyData.blob, &rsaKeyData.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
308 /* ECDSA */
309 KeyData ecdsaKeyData;
David Drysdaleadfe6112021-05-27 12:00:53 +0100310 AuthorizationSetBuilder ecdsaBuilder = AuthorizationSetBuilder()
David Drysdaledf09e542021-06-08 15:46:11 +0100311 .EcdsaSigningKey(EcCurve::P_256)
David Drysdaleadfe6112021-05-27 12:00:53 +0100312 .Authorization(tagToTest)
313 .Digest(Digest::SHA_2_256)
314 .Authorization(TAG_NO_AUTH_REQUIRED)
315 .SetDefaultValidity();
316 tagModifier(&ecdsaBuilder);
317 errorCode = GenerateKey(ecdsaBuilder, &ecdsaKeyData.blob, &ecdsaKeyData.characteristics);
subrahmanyaman05642492022-02-05 07:10:56 +0000318 if (!(SecLevel() == SecurityLevel::STRONGBOX &&
319 ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED == errorCode)) {
320 EXPECT_EQ(expectedReturn, errorCode);
321 }
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000322 return {aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData};
323 }
Shawn Willden7f424372021-01-10 18:06:50 -0700324 bool IsSecure() const { return securityLevel_ != SecurityLevel::SOFTWARE; }
325 SecurityLevel SecLevel() const { return securityLevel_; }
Seth Moorea12ac742023-03-03 13:40:30 -0800326 bool IsRkpSupportRequired() const;
Selene Huang31ab4042020-04-29 04:22:39 -0700327
328 vector<uint32_t> ValidKeySizes(Algorithm algorithm);
329 vector<uint32_t> InvalidKeySizes(Algorithm algorithm);
330
David Drysdale7de9feb2021-03-05 14:56:19 +0000331 vector<BlockMode> ValidBlockModes(Algorithm algorithm);
332 vector<PaddingMode> ValidPaddingModes(Algorithm algorithm, BlockMode blockMode);
333 vector<PaddingMode> InvalidPaddingModes(Algorithm algorithm, BlockMode blockMode);
334
Selene Huang31ab4042020-04-29 04:22:39 -0700335 vector<EcCurve> ValidCurves();
336 vector<EcCurve> InvalidCurves();
337
338 vector<Digest> ValidDigests(bool withNone, bool withMD5);
subrahmanyaman05642492022-02-05 07:10:56 +0000339 vector<uint64_t> ValidExponents();
Selene Huang31ab4042020-04-29 04:22:39 -0700340
341 static vector<string> build_params() {
Janis Danisevskis24c04702020-12-16 18:28:39 -0800342 auto params = ::android::getAidlHalInstanceNames(IKeyMintDevice::descriptor);
Selene Huang31ab4042020-04-29 04:22:39 -0700343 return params;
344 }
345
Janis Danisevskis24c04702020-12-16 18:28:39 -0800346 std::shared_ptr<IKeyMintOperation> op_;
Shawn Willden7f424372021-01-10 18:06:50 -0700347 vector<Certificate> cert_chain_;
Selene Huang31ab4042020-04-29 04:22:39 -0700348 vector<uint8_t> key_blob_;
Shawn Willden7f424372021-01-10 18:06:50 -0700349 vector<KeyCharacteristics> key_characteristics_;
350
351 const vector<KeyParameter>& SecLevelAuthorizations(
352 const vector<KeyCharacteristics>& key_characteristics);
353 inline const vector<KeyParameter>& SecLevelAuthorizations() {
354 return SecLevelAuthorizations(key_characteristics_);
355 }
Qi Wubeefae42021-01-28 23:16:37 +0800356 const vector<KeyParameter>& SecLevelAuthorizations(
357 const vector<KeyCharacteristics>& key_characteristics, SecurityLevel securityLevel);
358
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000359 ErrorCode UseAesKey(const vector<uint8_t>& aesKeyBlob);
360 ErrorCode UseHmacKey(const vector<uint8_t>& hmacKeyBlob);
361 ErrorCode UseRsaKey(const vector<uint8_t>& rsaKeyBlob);
362 ErrorCode UseEcdsaKey(const vector<uint8_t>& ecdsaKeyBlob);
Selene Huang31ab4042020-04-29 04:22:39 -0700363
Subrahmanyaman50fcf7d2023-04-20 22:48:39 +0000364 ErrorCode GenerateAttestKey(const AuthorizationSet& key_desc,
365 const optional<AttestationKey>& attest_key,
366 vector<uint8_t>* key_blob,
367 vector<KeyCharacteristics>* key_characteristics,
368 vector<Certificate>* cert_chain);
369
370 bool is_attest_key_feature_disabled(void) const;
371 bool is_strongbox_enabled(void) const;
372 bool is_chipset_allowed_km4_strongbox(void) const;
David Drysdalec3de1ca2023-05-09 08:10:36 +0100373 bool shouldSkipAttestKeyTest(void) const;
Subrahmanyaman50fcf7d2023-04-20 22:48:39 +0000374 void skipAttestKeyTest(void) const;
375
Shawn Willdend659c7c2021-02-19 14:51:51 -0700376 protected:
Janis Danisevskis24c04702020-12-16 18:28:39 -0800377 std::shared_ptr<IKeyMintDevice> keymint_;
Selene Huang31ab4042020-04-29 04:22:39 -0700378 uint32_t os_version_;
379 uint32_t os_patch_level_;
David Drysdalebb3d85e2021-04-13 11:15:51 +0100380 uint32_t vendor_patch_level_;
David Drysdaled2cc8c22021-04-15 13:29:45 +0100381 bool timestamp_token_required_;
Selene Huang31ab4042020-04-29 04:22:39 -0700382
383 SecurityLevel securityLevel_;
384 string name_;
385 string author_;
David Drysdale4cbe2152023-03-07 14:44:38 +0000386 int64_t challenge_;
Prashant Patildd5f7f02022-07-06 18:58:07 +0000387
388 private:
389 void CheckEncryptOneByteAtATime(BlockMode block_mode, const int block_size,
390 PaddingMode padding_mode, const string& iv,
391 const string& plaintext, const string& exp_cipher_text);
Selene Huang31ab4042020-04-29 04:22:39 -0700392};
393
David Drysdalea676c3b2021-06-14 14:46:02 +0100394// If the given property is available, add it to the tag set under the given tag ID.
395template <Tag tag>
396void add_tag_from_prop(AuthorizationSetBuilder* tags, TypedTag<TagType::BYTES, tag> ttag,
397 const char* prop) {
398 std::string prop_value = ::android::base::GetProperty(prop, /* default= */ "");
399 if (!prop_value.empty()) {
400 tags->Authorization(ttag, prop_value.data(), prop_value.size());
401 }
402}
403
Shawn Willden22fb9c12022-06-02 14:04:33 -0600404// Return the VSR API level for this device.
405int get_vsr_api_level();
406
David Drysdale555ba002022-05-03 18:48:57 +0100407// Indicate whether the test is running on a GSI image.
408bool is_gsi_image();
409
Selene Huang6e46f142021-04-20 19:20:11 -0700410vector<uint8_t> build_serial_blob(const uint64_t serial_int);
411void verify_subject(const X509* cert, const string& subject, bool self_signed);
412void verify_serial(X509* cert, const uint64_t expected_serial);
413void verify_subject_and_serial(const Certificate& certificate, //
414 const uint64_t expected_serial, //
415 const string& subject, bool self_signed);
Shawn Willden4315e132022-03-20 12:49:46 -0600416void verify_root_of_trust(const vector<uint8_t>& verified_boot_key, //
417 bool device_locked, //
418 VerifiedBoot verified_boot_state, //
419 const vector<uint8_t>& verified_boot_hash);
David Drysdale7dff4fc2021-12-10 10:10:52 +0000420bool verify_attestation_record(int aidl_version, //
421 const string& challenge, //
Shawn Willden7c130392020-12-21 09:58:22 -0700422 const string& app_id, //
423 AuthorizationSet expected_sw_enforced, //
424 AuthorizationSet expected_hw_enforced, //
425 SecurityLevel security_level,
David Drysdale565ccc72021-10-11 12:49:50 +0100426 const vector<uint8_t>& attestation_cert,
427 vector<uint8_t>* unique_id = nullptr);
Selene Huang6e46f142021-04-20 19:20:11 -0700428
Shawn Willden7c130392020-12-21 09:58:22 -0700429string bin2hex(const vector<uint8_t>& data);
430X509_Ptr parse_cert_blob(const vector<uint8_t>& blob);
Tri Voec50ee12023-02-14 16:29:53 -0800431ASN1_OCTET_STRING* get_attestation_record(X509* certificate);
David Drysdalef0d516d2021-03-22 07:51:43 +0000432vector<uint8_t> make_name_from_str(const string& name);
Rajesh Nyamagoud7b9ae3c2023-04-27 00:43:16 +0000433void assert_mgf_digests_present_in_key_characteristics(
434 const vector<KeyCharacteristics>& key_characteristics,
435 std::vector<android::hardware::security::keymint::Digest>& expected_mgf_digests);
436bool is_mgf_digest_present(const vector<KeyCharacteristics>& key_characteristics,
437 android::hardware::security::keymint::Digest expected_mgf_digest);
David Drysdale4dc01072021-04-01 12:17:35 +0100438void check_maced_pubkey(const MacedPublicKey& macedPubKey, bool testMode,
439 vector<uint8_t>* payload_value);
440void p256_pub_key(const vector<uint8_t>& coseKeyData, EVP_PKEY_Ptr* signingKey);
David Drysdalef42238c2023-06-15 09:41:05 +0100441void device_id_attestation_check_acceptable_error(Tag tag, const ErrorCode& result);
David Drysdale3d2ba0a2023-01-11 13:27:26 +0000442bool check_feature(const std::string& name);
David Drysdale4dc01072021-04-01 12:17:35 +0100443
David Drysdalef0d516d2021-03-22 07:51:43 +0000444AuthorizationSet HwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics);
445AuthorizationSet SwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics);
Eran Messeri03d7a1a2021-07-06 12:07:57 +0100446::testing::AssertionResult ChainSignaturesAreValid(const vector<Certificate>& chain,
447 bool strict_issuer_check = true);
Shawn Willden7c130392020-12-21 09:58:22 -0700448
David Drysdale1b9febc2023-06-07 13:43:24 +0100449ErrorCode GetReturnErrorCode(const Status& result);
450
Selene Huang31ab4042020-04-29 04:22:39 -0700451#define INSTANTIATE_KEYMINT_AIDL_TEST(name) \
452 INSTANTIATE_TEST_SUITE_P(PerInstance, name, \
453 testing::ValuesIn(KeyMintAidlTestBase::build_params()), \
Shawn Willden7e71f1e2021-04-01 16:44:22 -0600454 ::android::PrintInstanceNameToString); \
455 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(name);
Selene Huang31ab4042020-04-29 04:22:39 -0700456
Shawn Willden0e80b5d2020-12-17 09:07:27 -0700457} // namespace test
458
459} // namespace aidl::android::hardware::security::keymint