blob: 95f0c19c947a71075da77db7b902892d357ba9da [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_; }
74
Janis Danisevskis24c04702020-12-16 18:28:39 -080075 ErrorCode GetReturnErrorCode(const Status& result);
Selene Huang31ab4042020-04-29 04:22:39 -070076
Shawn Willden7c130392020-12-21 09:58:22 -070077 ErrorCode GenerateKey(const AuthorizationSet& key_desc, vector<uint8_t>* key_blob,
78 vector<KeyCharacteristics>* key_characteristics) {
79 return GenerateKey(key_desc, std::nullopt /* attest_key */, key_blob, key_characteristics,
80 &cert_chain_);
81 }
82 ErrorCode GenerateKey(const AuthorizationSet& key_desc,
83 const optional<AttestationKey>& attest_key, vector<uint8_t>* key_blob,
84 vector<KeyCharacteristics>* key_characteristics,
85 vector<Certificate>* cert_chain);
86 ErrorCode GenerateKey(const AuthorizationSet& key_desc,
87 const optional<AttestationKey>& attest_key = std::nullopt);
88
Selene Huang31ab4042020-04-29 04:22:39 -070089 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
90 const string& key_material, vector<uint8_t>* key_blob,
Shawn Willden7f424372021-01-10 18:06:50 -070091 vector<KeyCharacteristics>* key_characteristics);
Selene Huang31ab4042020-04-29 04:22:39 -070092 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
93 const string& key_material);
94
95 ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
96 const AuthorizationSet& wrapping_key_desc, string masking_key,
97 const AuthorizationSet& unwrapping_params);
98
99 ErrorCode DeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false);
100 ErrorCode DeleteKey(bool keep_key_blob = false);
101
102 ErrorCode DeleteAllKeys();
103
104 void CheckedDeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false);
105 void CheckedDeleteKey();
106
107 ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob,
108 const AuthorizationSet& in_params, AuthorizationSet* out_params,
Janis Danisevskis24c04702020-12-16 18:28:39 -0800109 std::shared_ptr<IKeyMintOperation>& op);
Selene Huang31ab4042020-04-29 04:22:39 -0700110 ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob,
111 const AuthorizationSet& in_params, AuthorizationSet* out_params);
112 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
113 AuthorizationSet* out_params);
114 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params);
115
Shawn Willden92d79c02021-02-19 07:31:55 -0700116 ErrorCode UpdateAad(const string& input);
117 ErrorCode Update(const string& input, string* output);
Selene Huang31ab4042020-04-29 04:22:39 -0700118
Selene Huang31ab4042020-04-29 04:22:39 -0700119 ErrorCode Finish(const string& message, const string& signature, string* output);
Shawn Willden92d79c02021-02-19 07:31:55 -0700120 ErrorCode Finish(const string& message, string* output) {
121 return Finish(message, {} /* signature */, output);
122 }
123 ErrorCode Finish(string* output) { return Finish({} /* message */, output); }
Selene Huang31ab4042020-04-29 04:22:39 -0700124
125 ErrorCode Abort();
Janis Danisevskis24c04702020-12-16 18:28:39 -0800126 ErrorCode Abort(const shared_ptr<IKeyMintOperation>& op);
Selene Huang31ab4042020-04-29 04:22:39 -0700127 void AbortIfNeeded();
128
129 string ProcessMessage(const vector<uint8_t>& key_blob, KeyPurpose operation,
130 const string& message, const AuthorizationSet& in_params,
131 AuthorizationSet* out_params);
Shawn Willden92d79c02021-02-19 07:31:55 -0700132 std::tuple<ErrorCode, std::string /* processedMessage */> ProcessMessage(
133 const vector<uint8_t>& key_blob, KeyPurpose operation, const std::string& message,
134 const AuthorizationSet& in_params);
Selene Huang31ab4042020-04-29 04:22:39 -0700135 string SignMessage(const vector<uint8_t>& key_blob, const string& message,
136 const AuthorizationSet& params);
137 string SignMessage(const string& message, const AuthorizationSet& params);
138
139 string MacMessage(const string& message, Digest digest, size_t mac_length);
140
141 void CheckHmacTestVector(const string& key, const string& message, Digest digest,
142 const string& expected_mac);
143
144 void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message,
145 const string& expected_ciphertext);
146
147 void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
148 PaddingMode padding_mode, const string& key, const string& iv,
149 const string& input, const string& expected_output);
150
151 void VerifyMessage(const vector<uint8_t>& key_blob, const string& message,
152 const string& signature, const AuthorizationSet& params);
153 void VerifyMessage(const string& message, const string& signature,
154 const AuthorizationSet& params);
155
156 string EncryptMessage(const vector<uint8_t>& key_blob, const string& message,
157 const AuthorizationSet& in_params, AuthorizationSet* out_params);
158 string EncryptMessage(const string& message, const AuthorizationSet& params,
159 AuthorizationSet* out_params);
160 string EncryptMessage(const string& message, const AuthorizationSet& params);
161 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding);
162 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
163 vector<uint8_t>* iv_out);
164 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
165 const vector<uint8_t>& iv_in);
166 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
167 uint8_t mac_length_bits, const vector<uint8_t>& iv_in);
168
169 string DecryptMessage(const vector<uint8_t>& key_blob, const string& ciphertext,
170 const AuthorizationSet& params);
171 string DecryptMessage(const string& ciphertext, const AuthorizationSet& params);
172 string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode,
173 const vector<uint8_t>& iv);
174
175 std::pair<ErrorCode, vector<uint8_t>> UpgradeKey(const vector<uint8_t>& key_blob);
176
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000177 template <typename TagType>
178 std::tuple<KeyData /* aesKey */, KeyData /* hmacKey */, KeyData /* rsaKey */,
179 KeyData /* ecdsaKey */>
180 CreateTestKeys(TagType tagToTest, ErrorCode expectedReturn) {
181 /* AES */
182 KeyData aesKeyData;
183 ErrorCode errorCode = GenerateKey(AuthorizationSetBuilder()
184 .AesEncryptionKey(128)
185 .Authorization(tagToTest)
186 .BlockMode(BlockMode::ECB)
187 .Padding(PaddingMode::NONE)
188 .Authorization(TAG_NO_AUTH_REQUIRED),
189 &aesKeyData.blob, &aesKeyData.characteristics);
190 EXPECT_EQ(expectedReturn, errorCode);
191
192 /* HMAC */
193 KeyData hmacKeyData;
194 errorCode = GenerateKey(AuthorizationSetBuilder()
195 .HmacKey(128)
196 .Authorization(tagToTest)
197 .Digest(Digest::SHA_2_256)
198 .Authorization(TAG_MIN_MAC_LENGTH, 128)
199 .Authorization(TAG_NO_AUTH_REQUIRED),
200 &hmacKeyData.blob, &hmacKeyData.characteristics);
201 EXPECT_EQ(expectedReturn, errorCode);
202
203 /* RSA */
204 KeyData rsaKeyData;
205 errorCode = GenerateKey(AuthorizationSetBuilder()
206 .RsaSigningKey(2048, 65537)
207 .Authorization(tagToTest)
208 .Digest(Digest::NONE)
209 .Padding(PaddingMode::NONE)
210 .Authorization(TAG_NO_AUTH_REQUIRED)
211 .SetDefaultValidity(),
212 &rsaKeyData.blob, &rsaKeyData.characteristics);
213 EXPECT_EQ(expectedReturn, errorCode);
214
215 /* ECDSA */
216 KeyData ecdsaKeyData;
217 errorCode = GenerateKey(AuthorizationSetBuilder()
218 .EcdsaSigningKey(256)
219 .Authorization(tagToTest)
220 .Digest(Digest::SHA_2_256)
221 .Authorization(TAG_NO_AUTH_REQUIRED)
222 .SetDefaultValidity(),
223 &ecdsaKeyData.blob, &ecdsaKeyData.characteristics);
224 EXPECT_EQ(expectedReturn, errorCode);
225 return {aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData};
226 }
Shawn Willden7f424372021-01-10 18:06:50 -0700227 bool IsSecure() const { return securityLevel_ != SecurityLevel::SOFTWARE; }
228 SecurityLevel SecLevel() const { return securityLevel_; }
Selene Huang31ab4042020-04-29 04:22:39 -0700229
230 vector<uint32_t> ValidKeySizes(Algorithm algorithm);
231 vector<uint32_t> InvalidKeySizes(Algorithm algorithm);
232
David Drysdale7de9feb2021-03-05 14:56:19 +0000233 vector<BlockMode> ValidBlockModes(Algorithm algorithm);
234 vector<PaddingMode> ValidPaddingModes(Algorithm algorithm, BlockMode blockMode);
235 vector<PaddingMode> InvalidPaddingModes(Algorithm algorithm, BlockMode blockMode);
236
Selene Huang31ab4042020-04-29 04:22:39 -0700237 vector<EcCurve> ValidCurves();
238 vector<EcCurve> InvalidCurves();
239
240 vector<Digest> ValidDigests(bool withNone, bool withMD5);
241
242 static vector<string> build_params() {
Janis Danisevskis24c04702020-12-16 18:28:39 -0800243 auto params = ::android::getAidlHalInstanceNames(IKeyMintDevice::descriptor);
Selene Huang31ab4042020-04-29 04:22:39 -0700244 return params;
245 }
246
Janis Danisevskis24c04702020-12-16 18:28:39 -0800247 std::shared_ptr<IKeyMintOperation> op_;
Shawn Willden7f424372021-01-10 18:06:50 -0700248 vector<Certificate> cert_chain_;
Selene Huang31ab4042020-04-29 04:22:39 -0700249 vector<uint8_t> key_blob_;
Shawn Willden7f424372021-01-10 18:06:50 -0700250 vector<KeyCharacteristics> key_characteristics_;
251
252 const vector<KeyParameter>& SecLevelAuthorizations(
253 const vector<KeyCharacteristics>& key_characteristics);
254 inline const vector<KeyParameter>& SecLevelAuthorizations() {
255 return SecLevelAuthorizations(key_characteristics_);
256 }
Qi Wubeefae42021-01-28 23:16:37 +0800257 const vector<KeyParameter>& SecLevelAuthorizations(
258 const vector<KeyCharacteristics>& key_characteristics, SecurityLevel securityLevel);
259
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000260 ErrorCode UseAesKey(const vector<uint8_t>& aesKeyBlob);
261 ErrorCode UseHmacKey(const vector<uint8_t>& hmacKeyBlob);
262 ErrorCode UseRsaKey(const vector<uint8_t>& rsaKeyBlob);
263 ErrorCode UseEcdsaKey(const vector<uint8_t>& ecdsaKeyBlob);
Selene Huang31ab4042020-04-29 04:22:39 -0700264
Shawn Willdend659c7c2021-02-19 14:51:51 -0700265 protected:
Janis Danisevskis24c04702020-12-16 18:28:39 -0800266 std::shared_ptr<IKeyMintDevice> keymint_;
Selene Huang31ab4042020-04-29 04:22:39 -0700267 uint32_t os_version_;
268 uint32_t os_patch_level_;
269
270 SecurityLevel securityLevel_;
271 string name_;
272 string author_;
273 long challenge_;
274};
275
Selene Huang6e46f142021-04-20 19:20:11 -0700276vector<uint8_t> build_serial_blob(const uint64_t serial_int);
277void verify_subject(const X509* cert, const string& subject, bool self_signed);
278void verify_serial(X509* cert, const uint64_t expected_serial);
279void verify_subject_and_serial(const Certificate& certificate, //
280 const uint64_t expected_serial, //
281 const string& subject, bool self_signed);
282
Shawn Willden7c130392020-12-21 09:58:22 -0700283bool verify_attestation_record(const string& challenge, //
284 const string& app_id, //
285 AuthorizationSet expected_sw_enforced, //
286 AuthorizationSet expected_hw_enforced, //
287 SecurityLevel security_level,
288 const vector<uint8_t>& attestation_cert);
Selene Huang6e46f142021-04-20 19:20:11 -0700289
Shawn Willden7c130392020-12-21 09:58:22 -0700290string bin2hex(const vector<uint8_t>& data);
291X509_Ptr parse_cert_blob(const vector<uint8_t>& blob);
David Drysdalef0d516d2021-03-22 07:51:43 +0000292vector<uint8_t> make_name_from_str(const string& name);
David Drysdale4dc01072021-04-01 12:17:35 +0100293void check_maced_pubkey(const MacedPublicKey& macedPubKey, bool testMode,
294 vector<uint8_t>* payload_value);
295void p256_pub_key(const vector<uint8_t>& coseKeyData, EVP_PKEY_Ptr* signingKey);
296
David Drysdalef0d516d2021-03-22 07:51:43 +0000297AuthorizationSet HwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics);
298AuthorizationSet SwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics);
Shawn Willden7c130392020-12-21 09:58:22 -0700299::testing::AssertionResult ChainSignaturesAreValid(const vector<Certificate>& chain);
300
Selene Huang31ab4042020-04-29 04:22:39 -0700301#define INSTANTIATE_KEYMINT_AIDL_TEST(name) \
302 INSTANTIATE_TEST_SUITE_P(PerInstance, name, \
303 testing::ValuesIn(KeyMintAidlTestBase::build_params()), \
Shawn Willden7e71f1e2021-04-01 16:44:22 -0600304 ::android::PrintInstanceNameToString); \
305 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(name);
Selene Huang31ab4042020-04-29 04:22:39 -0700306
Shawn Willden0e80b5d2020-12-17 09:07:27 -0700307} // namespace test
308
309} // namespace aidl::android::hardware::security::keymint