blob: cb389386842a0de25f8dbf8c7984f8913411df81 [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);
165
166 string EncryptMessage(const vector<uint8_t>& key_blob, const string& message,
167 const AuthorizationSet& in_params, AuthorizationSet* out_params);
168 string EncryptMessage(const string& message, const AuthorizationSet& params,
169 AuthorizationSet* out_params);
170 string EncryptMessage(const string& message, const AuthorizationSet& params);
171 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding);
172 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
173 vector<uint8_t>* iv_out);
174 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
175 const vector<uint8_t>& iv_in);
176 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
177 uint8_t mac_length_bits, const vector<uint8_t>& iv_in);
David Drysdaled2cc8c22021-04-15 13:29:45 +0100178 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
179 uint8_t mac_length_bits);
Selene Huang31ab4042020-04-29 04:22:39 -0700180
181 string DecryptMessage(const vector<uint8_t>& key_blob, const string& ciphertext,
182 const AuthorizationSet& params);
183 string DecryptMessage(const string& ciphertext, const AuthorizationSet& params);
184 string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode,
185 const vector<uint8_t>& iv);
186
187 std::pair<ErrorCode, vector<uint8_t>> UpgradeKey(const vector<uint8_t>& key_blob);
188
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000189 template <typename TagType>
190 std::tuple<KeyData /* aesKey */, KeyData /* hmacKey */, KeyData /* rsaKey */,
191 KeyData /* ecdsaKey */>
192 CreateTestKeys(TagType tagToTest, ErrorCode expectedReturn) {
193 /* AES */
194 KeyData aesKeyData;
195 ErrorCode errorCode = GenerateKey(AuthorizationSetBuilder()
196 .AesEncryptionKey(128)
197 .Authorization(tagToTest)
198 .BlockMode(BlockMode::ECB)
199 .Padding(PaddingMode::NONE)
200 .Authorization(TAG_NO_AUTH_REQUIRED),
201 &aesKeyData.blob, &aesKeyData.characteristics);
202 EXPECT_EQ(expectedReturn, errorCode);
203
204 /* HMAC */
205 KeyData hmacKeyData;
206 errorCode = GenerateKey(AuthorizationSetBuilder()
207 .HmacKey(128)
208 .Authorization(tagToTest)
209 .Digest(Digest::SHA_2_256)
210 .Authorization(TAG_MIN_MAC_LENGTH, 128)
211 .Authorization(TAG_NO_AUTH_REQUIRED),
212 &hmacKeyData.blob, &hmacKeyData.characteristics);
213 EXPECT_EQ(expectedReturn, errorCode);
214
215 /* RSA */
216 KeyData rsaKeyData;
217 errorCode = GenerateKey(AuthorizationSetBuilder()
218 .RsaSigningKey(2048, 65537)
219 .Authorization(tagToTest)
220 .Digest(Digest::NONE)
221 .Padding(PaddingMode::NONE)
222 .Authorization(TAG_NO_AUTH_REQUIRED)
223 .SetDefaultValidity(),
224 &rsaKeyData.blob, &rsaKeyData.characteristics);
225 EXPECT_EQ(expectedReturn, errorCode);
226
227 /* ECDSA */
228 KeyData ecdsaKeyData;
229 errorCode = GenerateKey(AuthorizationSetBuilder()
230 .EcdsaSigningKey(256)
231 .Authorization(tagToTest)
232 .Digest(Digest::SHA_2_256)
233 .Authorization(TAG_NO_AUTH_REQUIRED)
234 .SetDefaultValidity(),
235 &ecdsaKeyData.blob, &ecdsaKeyData.characteristics);
236 EXPECT_EQ(expectedReturn, errorCode);
237 return {aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData};
238 }
Shawn Willden7f424372021-01-10 18:06:50 -0700239 bool IsSecure() const { return securityLevel_ != SecurityLevel::SOFTWARE; }
240 SecurityLevel SecLevel() const { return securityLevel_; }
Selene Huang31ab4042020-04-29 04:22:39 -0700241
242 vector<uint32_t> ValidKeySizes(Algorithm algorithm);
243 vector<uint32_t> InvalidKeySizes(Algorithm algorithm);
244
David Drysdale7de9feb2021-03-05 14:56:19 +0000245 vector<BlockMode> ValidBlockModes(Algorithm algorithm);
246 vector<PaddingMode> ValidPaddingModes(Algorithm algorithm, BlockMode blockMode);
247 vector<PaddingMode> InvalidPaddingModes(Algorithm algorithm, BlockMode blockMode);
248
Selene Huang31ab4042020-04-29 04:22:39 -0700249 vector<EcCurve> ValidCurves();
250 vector<EcCurve> InvalidCurves();
251
252 vector<Digest> ValidDigests(bool withNone, bool withMD5);
253
254 static vector<string> build_params() {
Janis Danisevskis24c04702020-12-16 18:28:39 -0800255 auto params = ::android::getAidlHalInstanceNames(IKeyMintDevice::descriptor);
Selene Huang31ab4042020-04-29 04:22:39 -0700256 return params;
257 }
258
Janis Danisevskis24c04702020-12-16 18:28:39 -0800259 std::shared_ptr<IKeyMintOperation> op_;
Shawn Willden7f424372021-01-10 18:06:50 -0700260 vector<Certificate> cert_chain_;
Selene Huang31ab4042020-04-29 04:22:39 -0700261 vector<uint8_t> key_blob_;
Shawn Willden7f424372021-01-10 18:06:50 -0700262 vector<KeyCharacteristics> key_characteristics_;
263
264 const vector<KeyParameter>& SecLevelAuthorizations(
265 const vector<KeyCharacteristics>& key_characteristics);
266 inline const vector<KeyParameter>& SecLevelAuthorizations() {
267 return SecLevelAuthorizations(key_characteristics_);
268 }
Qi Wubeefae42021-01-28 23:16:37 +0800269 const vector<KeyParameter>& SecLevelAuthorizations(
270 const vector<KeyCharacteristics>& key_characteristics, SecurityLevel securityLevel);
271
Chirag Pathak9ea6a0a2021-02-01 23:54:27 +0000272 ErrorCode UseAesKey(const vector<uint8_t>& aesKeyBlob);
273 ErrorCode UseHmacKey(const vector<uint8_t>& hmacKeyBlob);
274 ErrorCode UseRsaKey(const vector<uint8_t>& rsaKeyBlob);
275 ErrorCode UseEcdsaKey(const vector<uint8_t>& ecdsaKeyBlob);
Selene Huang31ab4042020-04-29 04:22:39 -0700276
Shawn Willdend659c7c2021-02-19 14:51:51 -0700277 protected:
Janis Danisevskis24c04702020-12-16 18:28:39 -0800278 std::shared_ptr<IKeyMintDevice> keymint_;
Selene Huang31ab4042020-04-29 04:22:39 -0700279 uint32_t os_version_;
280 uint32_t os_patch_level_;
David Drysdalebb3d85e2021-04-13 11:15:51 +0100281 uint32_t vendor_patch_level_;
David Drysdaled2cc8c22021-04-15 13:29:45 +0100282 bool timestamp_token_required_;
Selene Huang31ab4042020-04-29 04:22:39 -0700283
284 SecurityLevel securityLevel_;
285 string name_;
286 string author_;
287 long challenge_;
288};
289
Selene Huang6e46f142021-04-20 19:20:11 -0700290vector<uint8_t> build_serial_blob(const uint64_t serial_int);
291void verify_subject(const X509* cert, const string& subject, bool self_signed);
292void verify_serial(X509* cert, const uint64_t expected_serial);
293void verify_subject_and_serial(const Certificate& certificate, //
294 const uint64_t expected_serial, //
295 const string& subject, bool self_signed);
296
Shawn Willden7c130392020-12-21 09:58:22 -0700297bool verify_attestation_record(const string& challenge, //
298 const string& app_id, //
299 AuthorizationSet expected_sw_enforced, //
300 AuthorizationSet expected_hw_enforced, //
301 SecurityLevel security_level,
302 const vector<uint8_t>& attestation_cert);
Selene Huang6e46f142021-04-20 19:20:11 -0700303
Shawn Willden7c130392020-12-21 09:58:22 -0700304string bin2hex(const vector<uint8_t>& data);
305X509_Ptr parse_cert_blob(const vector<uint8_t>& blob);
David Drysdalef0d516d2021-03-22 07:51:43 +0000306vector<uint8_t> make_name_from_str(const string& name);
David Drysdale4dc01072021-04-01 12:17:35 +0100307void check_maced_pubkey(const MacedPublicKey& macedPubKey, bool testMode,
308 vector<uint8_t>* payload_value);
309void p256_pub_key(const vector<uint8_t>& coseKeyData, EVP_PKEY_Ptr* signingKey);
310
David Drysdalef0d516d2021-03-22 07:51:43 +0000311AuthorizationSet HwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics);
312AuthorizationSet SwEnforcedAuthorizations(const vector<KeyCharacteristics>& key_characteristics);
Shawn Willden7c130392020-12-21 09:58:22 -0700313::testing::AssertionResult ChainSignaturesAreValid(const vector<Certificate>& chain);
314
Selene Huang31ab4042020-04-29 04:22:39 -0700315#define INSTANTIATE_KEYMINT_AIDL_TEST(name) \
316 INSTANTIATE_TEST_SUITE_P(PerInstance, name, \
317 testing::ValuesIn(KeyMintAidlTestBase::build_params()), \
Shawn Willden7e71f1e2021-04-01 16:44:22 -0600318 ::android::PrintInstanceNameToString); \
319 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(name);
Selene Huang31ab4042020-04-29 04:22:39 -0700320
Shawn Willden0e80b5d2020-12-17 09:07:27 -0700321} // namespace test
322
323} // namespace aidl::android::hardware::security::keymint