blob: d8f1bb3dbe11b3336b460948a14e52b2731ded49 [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
19#include <aidl/Gtest.h>
20#include <aidl/Vintf.h>
Selene Huang31ab4042020-04-29 04:22:39 -070021#include <binder/IServiceManager.h>
22#include <binder/ProcessState.h>
23#include <gtest/gtest.h>
Shawn Willden7c130392020-12-21 09:58:22 -070024#include <openssl/x509.h>
Selene Huang31ab4042020-04-29 04:22:39 -070025
Janis Danisevskis24c04702020-12-16 18:28:39 -080026#include <aidl/android/hardware/security/keymint/ErrorCode.h>
27#include <aidl/android/hardware/security/keymint/IKeyMintDevice.h>
David Drysdale4dc01072021-04-01 12:17:35 +010028#include <aidl/android/hardware/security/keymint/MacedPublicKey.h>
Shawn Willden1d3f85e2020-12-09 14:18:44 -070029
Shawn Willden08a7e432020-12-11 13:05:27 +000030#include <keymint_support/authorization_set.h>
Shawn Willden7c130392020-12-21 09:58:22 -070031#include <keymint_support/openssl_utils.h>
Selene Huang31ab4042020-04-29 04:22:39 -070032
Shawn Willden0e80b5d2020-12-17 09:07:27 -070033namespace aidl::android::hardware::security::keymint {
34
35::std::ostream& operator<<(::std::ostream& os, const AuthorizationSet& set);
36
Shawn Willden7c130392020-12-21 09:58:22 -070037inline bool operator==(const keymint::AuthorizationSet& a, const keymint::AuthorizationSet& b) {
38 return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin());
39}
40
Shawn Willden0e80b5d2020-12-17 09:07:27 -070041namespace test {
Selene Huang31ab4042020-04-29 04:22:39 -070042
43using ::android::sp;
Janis Danisevskis24c04702020-12-16 18:28:39 -080044using Status = ::ndk::ScopedAStatus;
Shawn Willden7c130392020-12-21 09:58:22 -070045using ::std::optional;
Selene Huang31ab4042020-04-29 04:22:39 -070046using ::std::shared_ptr;
47using ::std::string;
48using ::std::vector;
49
50constexpr uint64_t kOpHandleSentinel = 0xFFFFFFFFFFFFFFFF;
51
Selene Huang31ab4042020-04-29 04:22:39 -070052class KeyMintAidlTestBase : public ::testing::TestWithParam<string> {
53 public:
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +000054 struct KeyData {
55 vector<uint8_t> blob;
56 vector<KeyCharacteristics> characteristics;
57 };
58
Shawn Willden7c130392020-12-21 09:58:22 -070059 static bool arm_deleteAllKeys;
60 static bool dump_Attestations;
61
Selene Huang31ab4042020-04-29 04:22:39 -070062 void SetUp() override;
63 void TearDown() override {
64 if (key_blob_.size()) {
65 CheckedDeleteKey();
66 }
67 AbortIfNeeded();
68 }
69
Janis Danisevskis24c04702020-12-16 18:28:39 -080070 void InitializeKeyMint(std::shared_ptr<IKeyMintDevice> keyMint);
Selene Huang31ab4042020-04-29 04:22:39 -070071 IKeyMintDevice& keyMint() { return *keymint_; }
72 uint32_t os_version() { return os_version_; }
73 uint32_t os_patch_level() { return os_patch_level_; }
David Drysdalebb3d85e2021-04-13 11:15:51 +010074 uint32_t vendor_patch_level() { return vendor_patch_level_; }
Selene Huang31ab4042020-04-29 04:22:39 -070075
Janis Danisevskis24c04702020-12-16 18:28:39 -080076 ErrorCode GetReturnErrorCode(const Status& result);
Selene Huang31ab4042020-04-29 04:22:39 -070077
Shawn Willden7c130392020-12-21 09:58:22 -070078 ErrorCode GenerateKey(const AuthorizationSet& key_desc, vector<uint8_t>* key_blob,
79 vector<KeyCharacteristics>* key_characteristics) {
80 return GenerateKey(key_desc, std::nullopt /* attest_key */, key_blob, key_characteristics,
81 &cert_chain_);
82 }
83 ErrorCode GenerateKey(const AuthorizationSet& key_desc,
84 const optional<AttestationKey>& attest_key, vector<uint8_t>* key_blob,
85 vector<KeyCharacteristics>* key_characteristics,
86 vector<Certificate>* cert_chain);
87 ErrorCode GenerateKey(const AuthorizationSet& key_desc,
88 const optional<AttestationKey>& attest_key = std::nullopt);
89
Selene Huang31ab4042020-04-29 04:22:39 -070090 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
91 const string& key_material, vector<uint8_t>* key_blob,
Shawn Willden7f424372021-01-10 18:06:50 -070092 vector<KeyCharacteristics>* key_characteristics);
Selene Huang31ab4042020-04-29 04:22:39 -070093 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
94 const string& key_material);
95
96 ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
97 const AuthorizationSet& wrapping_key_desc, string masking_key,
David Drysdaled2cc8c22021-04-15 13:29:45 +010098 const AuthorizationSet& unwrapping_params, int64_t password_sid,
99 int64_t biometric_sid);
100 ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
101 const AuthorizationSet& wrapping_key_desc, string masking_key,
102 const AuthorizationSet& unwrapping_params) {
103 return ImportWrappedKey(wrapped_key, wrapping_key, wrapping_key_desc, masking_key,
104 unwrapping_params, 0 /* password_sid */, 0 /* biometric_sid */);
105 }
Selene Huang31ab4042020-04-29 04:22:39 -0700106
107 ErrorCode DeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false);
108 ErrorCode DeleteKey(bool keep_key_blob = false);
109
110 ErrorCode DeleteAllKeys();
111
David Drysdaled2cc8c22021-04-15 13:29:45 +0100112 ErrorCode DestroyAttestationIds();
113
Selene Huang31ab4042020-04-29 04:22:39 -0700114 void CheckedDeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false);
115 void CheckedDeleteKey();
116
117 ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob,
118 const AuthorizationSet& in_params, AuthorizationSet* out_params,
Janis Danisevskis24c04702020-12-16 18:28:39 -0800119 std::shared_ptr<IKeyMintOperation>& op);
Selene Huang31ab4042020-04-29 04:22:39 -0700120 ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob,
121 const AuthorizationSet& in_params, AuthorizationSet* out_params);
122 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
123 AuthorizationSet* out_params);
124 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params);
125
Shawn Willden92d79c02021-02-19 07:31:55 -0700126 ErrorCode UpdateAad(const string& input);
127 ErrorCode Update(const string& input, string* output);
Selene Huang31ab4042020-04-29 04:22:39 -0700128
Selene Huang31ab4042020-04-29 04:22:39 -0700129 ErrorCode Finish(const string& message, const string& signature, string* output);
Shawn Willden92d79c02021-02-19 07:31:55 -0700130 ErrorCode Finish(const string& message, string* output) {
131 return Finish(message, {} /* signature */, output);
132 }
133 ErrorCode Finish(string* output) { return Finish({} /* message */, output); }
Selene Huang31ab4042020-04-29 04:22:39 -0700134
135 ErrorCode Abort();
Janis Danisevskis24c04702020-12-16 18:28:39 -0800136 ErrorCode Abort(const shared_ptr<IKeyMintOperation>& op);
Selene Huang31ab4042020-04-29 04:22:39 -0700137 void AbortIfNeeded();
138
139 string ProcessMessage(const vector<uint8_t>& key_blob, KeyPurpose operation,
140 const string& message, const AuthorizationSet& in_params,
141 AuthorizationSet* out_params);
Shawn Willden92d79c02021-02-19 07:31:55 -0700142 std::tuple<ErrorCode, std::string /* processedMessage */> ProcessMessage(
143 const vector<uint8_t>& key_blob, KeyPurpose operation, const std::string& message,
144 const AuthorizationSet& in_params);
Selene Huang31ab4042020-04-29 04:22:39 -0700145 string SignMessage(const vector<uint8_t>& key_blob, const string& message,
146 const AuthorizationSet& params);
147 string SignMessage(const string& message, const AuthorizationSet& params);
148
149 string MacMessage(const string& message, Digest digest, size_t mac_length);
150
151 void CheckHmacTestVector(const string& key, const string& message, Digest digest,
152 const string& expected_mac);
153
154 void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message,
155 const string& expected_ciphertext);
156
157 void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
158 PaddingMode padding_mode, const string& key, const string& iv,
159 const string& input, const string& expected_output);
160
161 void VerifyMessage(const vector<uint8_t>& key_blob, const string& message,
162 const string& signature, const AuthorizationSet& params);
163 void VerifyMessage(const string& message, const string& signature,
164 const AuthorizationSet& params);
David Drysdaledf8f52e2021-05-06 08:10:58 +0100165 void LocalVerifyMessage(const string& message, const string& signature,
166 const AuthorizationSet& params);
Selene Huang31ab4042020-04-29 04:22:39 -0700167
David Drysdale59cae642021-05-12 13:52:03 +0100168 string LocalRsaEncryptMessage(const string& message, const AuthorizationSet& params);
Selene Huang31ab4042020-04-29 04:22:39 -0700169 string EncryptMessage(const vector<uint8_t>& key_blob, const string& message,
170 const AuthorizationSet& in_params, AuthorizationSet* out_params);
171 string EncryptMessage(const string& message, const AuthorizationSet& params,
172 AuthorizationSet* out_params);
173 string EncryptMessage(const string& message, const AuthorizationSet& params);
174 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding);
175 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
176 vector<uint8_t>* iv_out);
177 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
178 const vector<uint8_t>& iv_in);
179 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
180 uint8_t mac_length_bits, const vector<uint8_t>& iv_in);
David Drysdaled2cc8c22021-04-15 13:29:45 +0100181 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
182 uint8_t mac_length_bits);
Selene Huang31ab4042020-04-29 04:22:39 -0700183
184 string DecryptMessage(const vector<uint8_t>& key_blob, const string& ciphertext,
185 const AuthorizationSet& params);
186 string DecryptMessage(const string& ciphertext, const AuthorizationSet& params);
187 string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode,
188 const vector<uint8_t>& iv);
189
190 std::pair<ErrorCode, vector<uint8_t>> UpgradeKey(const vector<uint8_t>& key_blob);
191
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000192 template <typename TagType>
193 std::tuple<KeyData /* aesKey */, KeyData /* hmacKey */, KeyData /* rsaKey */,
194 KeyData /* ecdsaKey */>
195 CreateTestKeys(TagType tagToTest, ErrorCode expectedReturn) {
196 /* AES */
197 KeyData aesKeyData;
198 ErrorCode errorCode = GenerateKey(AuthorizationSetBuilder()
199 .AesEncryptionKey(128)
200 .Authorization(tagToTest)
201 .BlockMode(BlockMode::ECB)
202 .Padding(PaddingMode::NONE)
203 .Authorization(TAG_NO_AUTH_REQUIRED),
204 &aesKeyData.blob, &aesKeyData.characteristics);
205 EXPECT_EQ(expectedReturn, errorCode);
206
207 /* HMAC */
208 KeyData hmacKeyData;
209 errorCode = GenerateKey(AuthorizationSetBuilder()
210 .HmacKey(128)
211 .Authorization(tagToTest)
212 .Digest(Digest::SHA_2_256)
213 .Authorization(TAG_MIN_MAC_LENGTH, 128)
214 .Authorization(TAG_NO_AUTH_REQUIRED),
215 &hmacKeyData.blob, &hmacKeyData.characteristics);
216 EXPECT_EQ(expectedReturn, errorCode);
217
218 /* RSA */
219 KeyData rsaKeyData;
220 errorCode = GenerateKey(AuthorizationSetBuilder()
221 .RsaSigningKey(2048, 65537)
222 .Authorization(tagToTest)
223 .Digest(Digest::NONE)
224 .Padding(PaddingMode::NONE)
225 .Authorization(TAG_NO_AUTH_REQUIRED)
226 .SetDefaultValidity(),
227 &rsaKeyData.blob, &rsaKeyData.characteristics);
228 EXPECT_EQ(expectedReturn, errorCode);
229
230 /* ECDSA */
231 KeyData ecdsaKeyData;
232 errorCode = GenerateKey(AuthorizationSetBuilder()
233 .EcdsaSigningKey(256)
234 .Authorization(tagToTest)
235 .Digest(Digest::SHA_2_256)
236 .Authorization(TAG_NO_AUTH_REQUIRED)
237 .SetDefaultValidity(),
238 &ecdsaKeyData.blob, &ecdsaKeyData.characteristics);
239 EXPECT_EQ(expectedReturn, errorCode);
240 return {aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData};
241 }
Shawn Willden7f424372021-01-10 18:06:50 -0700242 bool IsSecure() const { return securityLevel_ != SecurityLevel::SOFTWARE; }
243 SecurityLevel SecLevel() const { return securityLevel_; }
Selene Huang31ab4042020-04-29 04:22:39 -0700244
245 vector<uint32_t> ValidKeySizes(Algorithm algorithm);
246 vector<uint32_t> InvalidKeySizes(Algorithm algorithm);
247
David Drysdale7de9feb2021-03-05 14:56:19 +0000248 vector<BlockMode> ValidBlockModes(Algorithm algorithm);
249 vector<PaddingMode> ValidPaddingModes(Algorithm algorithm, BlockMode blockMode);
250 vector<PaddingMode> InvalidPaddingModes(Algorithm algorithm, BlockMode blockMode);
251
Selene Huang31ab4042020-04-29 04:22:39 -0700252 vector<EcCurve> ValidCurves();
253 vector<EcCurve> InvalidCurves();
254
255 vector<Digest> ValidDigests(bool withNone, bool withMD5);
256
257 static vector<string> build_params() {
Janis Danisevskis24c04702020-12-16 18:28:39 -0800258 auto params = ::android::getAidlHalInstanceNames(IKeyMintDevice::descriptor);
Selene Huang31ab4042020-04-29 04:22:39 -0700259 return params;
260 }
261
Janis Danisevskis24c04702020-12-16 18:28:39 -0800262 std::shared_ptr<IKeyMintOperation> op_;
Shawn Willden7f424372021-01-10 18:06:50 -0700263 vector<Certificate> cert_chain_;
Selene Huang31ab4042020-04-29 04:22:39 -0700264 vector<uint8_t> key_blob_;
Shawn Willden7f424372021-01-10 18:06:50 -0700265 vector<KeyCharacteristics> key_characteristics_;
266
267 const vector<KeyParameter>& SecLevelAuthorizations(
268 const vector<KeyCharacteristics>& key_characteristics);
269 inline const vector<KeyParameter>& SecLevelAuthorizations() {
270 return SecLevelAuthorizations(key_characteristics_);
271 }
Qi Wubeefae42021-01-28 23:16:37 +0800272 const vector<KeyParameter>& SecLevelAuthorizations(
273 const vector<KeyCharacteristics>& key_characteristics, SecurityLevel securityLevel);
274
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000275 ErrorCode UseAesKey(const vector<uint8_t>& aesKeyBlob);
276 ErrorCode UseHmacKey(const vector<uint8_t>& hmacKeyBlob);
277 ErrorCode UseRsaKey(const vector<uint8_t>& rsaKeyBlob);
278 ErrorCode UseEcdsaKey(const vector<uint8_t>& ecdsaKeyBlob);
Selene Huang31ab4042020-04-29 04:22:39 -0700279
Shawn Willdend659c7c2021-02-19 14:51:51 -0700280 protected:
Janis Danisevskis24c04702020-12-16 18:28:39 -0800281 std::shared_ptr<IKeyMintDevice> keymint_;
Selene Huang31ab4042020-04-29 04:22:39 -0700282 uint32_t os_version_;
283 uint32_t os_patch_level_;
David Drysdalebb3d85e2021-04-13 11:15:51 +0100284 uint32_t vendor_patch_level_;
David Drysdaled2cc8c22021-04-15 13:29:45 +0100285 bool timestamp_token_required_;
Selene Huang31ab4042020-04-29 04:22:39 -0700286
287 SecurityLevel securityLevel_;
288 string name_;
289 string author_;
290 long challenge_;
291};
292
Selene Huang6e46f142021-04-20 19:20:11 -0700293vector<uint8_t> build_serial_blob(const uint64_t serial_int);
294void verify_subject(const X509* cert, const string& subject, bool self_signed);
295void verify_serial(X509* cert, const uint64_t expected_serial);
296void verify_subject_and_serial(const Certificate& certificate, //
297 const uint64_t expected_serial, //
298 const string& subject, bool self_signed);
299
Shawn Willden7c130392020-12-21 09:58:22 -0700300bool verify_attestation_record(const string& challenge, //
301 const string& app_id, //
302 AuthorizationSet expected_sw_enforced, //
303 AuthorizationSet expected_hw_enforced, //
304 SecurityLevel security_level,
305 const vector<uint8_t>& attestation_cert);
Selene Huang6e46f142021-04-20 19:20:11 -0700306
Shawn Willden7c130392020-12-21 09:58:22 -0700307string bin2hex(const vector<uint8_t>& data);
308X509_Ptr parse_cert_blob(const vector<uint8_t>& blob);
David Drysdalef0d516d2021-03-22 07:51:43 +0000309vector<uint8_t> make_name_from_str(const string& name);
David Drysdale4dc01072021-04-01 12:17:35 +0100310void check_maced_pubkey(const MacedPublicKey& macedPubKey, bool testMode,
311 vector<uint8_t>* payload_value);
312void p256_pub_key(const vector<uint8_t>& coseKeyData, EVP_PKEY_Ptr* signingKey);
313
David Drysdalef0d516d2021-03-22 07:51:43 +0000314AuthorizationSet HwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics);
315AuthorizationSet SwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics);
Shawn Willden7c130392020-12-21 09:58:22 -0700316::testing::AssertionResult ChainSignaturesAreValid(const vector<Certificate>& chain);
317
Selene Huang31ab4042020-04-29 04:22:39 -0700318#define INSTANTIATE_KEYMINT_AIDL_TEST(name) \
319 INSTANTIATE_TEST_SUITE_P(PerInstance, name, \
320 testing::ValuesIn(KeyMintAidlTestBase::build_params()), \
Shawn Willden7e71f1e2021-04-01 16:44:22 -0600321 ::android::PrintInstanceNameToString); \
322 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(name);
Selene Huang31ab4042020-04-29 04:22:39 -0700323
Shawn Willden0e80b5d2020-12-17 09:07:27 -0700324} // namespace test
325
326} // namespace aidl::android::hardware::security::keymint