blob: b120dc9d19ecee833ef5bbb9aac5de62eaa987ba [file] [log] [blame]
Selene Huang92b61d62020-03-04 02:24:16 -08001/*
2 * Copyright 2019, 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
17#ifndef VTS_IDENTITY_TEST_UTILS_H
18#define VTS_IDENTITY_TEST_UTILS_H
19
20#include <android/hardware/identity/IIdentityCredentialStore.h>
21#include <android/hardware/identity/support/IdentityCredentialSupport.h>
Seth Moore1bf823c2022-01-25 23:04:37 +000022#include <android/hardware/security/keymint/MacedPublicKey.h>
Selene Huang92b61d62020-03-04 02:24:16 -080023#include <cppbor.h>
24#include <cppbor_parse.h>
David Zeuthen49f2d252020-10-16 11:27:24 -040025#include <gtest/gtest.h>
Selene Huang92b61d62020-03-04 02:24:16 -080026
27namespace android::hardware::identity::test_utils {
28
29using ::std::map;
30using ::std::optional;
31using ::std::string;
32using ::std::vector;
33
34using ::android::sp;
35using ::android::binder::Status;
36
37struct AttestationData {
38 AttestationData(sp<IWritableIdentityCredential>& writableCredential, string challenge,
David Zeuthen34abaae2020-10-26 20:26:36 -040039 vector<uint8_t> attestationAppId)
40 : attestationApplicationId(attestationAppId) {
Selene Huang92b61d62020-03-04 02:24:16 -080041 // ASSERT_NE(writableCredential, nullptr);
42
43 if (!challenge.empty()) {
44 attestationChallenge.assign(challenge.begin(), challenge.end());
45 }
46
47 result = writableCredential->getAttestationCertificate(
48 attestationApplicationId, attestationChallenge, &attestationCertificate);
49 }
50
51 AttestationData() {}
52
53 vector<uint8_t> attestationChallenge;
54 vector<uint8_t> attestationApplicationId;
55 vector<Certificate> attestationCertificate;
56 Status result;
57};
58
59struct TestEntryData {
60 TestEntryData(string nameSpace, string name, vector<int32_t> profileIds)
61 : nameSpace(nameSpace), name(name), profileIds(profileIds) {}
62
63 TestEntryData(string nameSpace, string name, const string& value, vector<int32_t> profileIds)
64 : TestEntryData(nameSpace, name, profileIds) {
65 valueCbor = cppbor::Tstr(((const char*)value.data())).encode();
66 }
67 TestEntryData(string nameSpace, string name, const vector<uint8_t>& value,
68 vector<int32_t> profileIds)
69 : TestEntryData(nameSpace, name, profileIds) {
70 valueCbor = cppbor::Bstr(value).encode();
71 }
72 TestEntryData(string nameSpace, string name, bool value, vector<int32_t> profileIds)
73 : TestEntryData(nameSpace, name, profileIds) {
74 valueCbor = cppbor::Bool(value).encode();
75 }
76 TestEntryData(string nameSpace, string name, int64_t value, vector<int32_t> profileIds)
77 : TestEntryData(nameSpace, name, profileIds) {
78 if (value >= 0) {
79 valueCbor = cppbor::Uint(value).encode();
80 } else {
81 valueCbor = cppbor::Nint(-value).encode();
82 }
83 }
84
85 string nameSpace;
86 string name;
87 vector<uint8_t> valueCbor;
88 vector<int32_t> profileIds;
89};
90
91struct TestProfile {
92 uint16_t id;
93 vector<uint8_t> readerCertificate;
94 bool userAuthenticationRequired;
95 uint64_t timeoutMillis;
96};
97
Selene Huangcab019a2020-03-11 04:37:48 -070098bool setupWritableCredential(sp<IWritableIdentityCredential>& writableCredential,
David Zeuthen34abaae2020-10-26 20:26:36 -040099 sp<IIdentityCredentialStore>& credentialStore, bool testCredential);
Selene Huang92b61d62020-03-04 02:24:16 -0800100
Seth Moore1bf823c2022-01-25 23:04:37 +0000101optional<vector<vector<uint8_t>>> createFakeRemotelyProvisionedCertificateChain(
102 const ::android::hardware::security::keymint::MacedPublicKey& macedPublicKey);
103
Selene Huangcab019a2020-03-11 04:37:48 -0700104optional<vector<uint8_t>> generateReaderCertificate(string serialDecimal);
Selene Huang92b61d62020-03-04 02:24:16 -0800105
Selene Huangcab019a2020-03-11 04:37:48 -0700106optional<vector<uint8_t>> generateReaderCertificate(string serialDecimal,
107 vector<uint8_t>* outReaderPrivateKey);
Selene Huang92b61d62020-03-04 02:24:16 -0800108
Selene Huangcab019a2020-03-11 04:37:48 -0700109optional<vector<SecureAccessControlProfile>> addAccessControlProfiles(
Selene Huang92b61d62020-03-04 02:24:16 -0800110 sp<IWritableIdentityCredential>& writableCredential,
111 const vector<TestProfile>& testProfiles);
112
Selene Huangcab019a2020-03-11 04:37:48 -0700113bool addEntry(sp<IWritableIdentityCredential>& writableCredential, const TestEntryData& entry,
Selene Huang92b61d62020-03-04 02:24:16 -0800114 int dataChunkSize, map<const TestEntryData*, vector<vector<uint8_t>>>& encryptedBlobs,
115 bool expectSuccess);
116
Selene Huangcab019a2020-03-11 04:37:48 -0700117void setImageData(vector<uint8_t>& image);
Selene Huang92b61d62020-03-04 02:24:16 -0800118
David Zeuthen34abaae2020-10-26 20:26:36 -0400119void validateAttestationCertificate(const vector<Certificate>& credentialKeyCertChain,
Selene Huangcab019a2020-03-11 04:37:48 -0700120 const vector<uint8_t>& expectedChallenge,
David Zeuthen34abaae2020-10-26 20:26:36 -0400121 const vector<uint8_t>& expectedAppId, bool isTestCredential);
Selene Huang92b61d62020-03-04 02:24:16 -0800122
David Zeuthen28edb102020-04-28 18:54:55 -0400123vector<RequestNamespace> buildRequestNamespaces(const vector<TestEntryData> entries);
124
David Zeuthen34abaae2020-10-26 20:26:36 -0400125// Verifies that the X.509 certificate for a just created authentication key
126// is valid.
127//
128void verifyAuthKeyCertificate(const vector<uint8_t>& authKeyCertChain);
129
Selene Huang92b61d62020-03-04 02:24:16 -0800130} // namespace android::hardware::identity::test_utils
131
132#endif // VTS_IDENTITY_TEST_UTILS_H