blob: 673b7368425187d416bb9f914c02ef082678ed38 [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>
22#include <cppbor.h>
23#include <cppbor_parse.h>
24
25namespace android::hardware::identity::test_utils {
26
27using ::std::map;
28using ::std::optional;
29using ::std::string;
30using ::std::vector;
31
32using ::android::sp;
33using ::android::binder::Status;
34
35struct AttestationData {
36 AttestationData(sp<IWritableIdentityCredential>& writableCredential, string challenge,
37 vector<uint8_t> applicationId)
38 : attestationApplicationId(applicationId) {
39 // ASSERT_NE(writableCredential, nullptr);
40
41 if (!challenge.empty()) {
42 attestationChallenge.assign(challenge.begin(), challenge.end());
43 }
44
45 result = writableCredential->getAttestationCertificate(
46 attestationApplicationId, attestationChallenge, &attestationCertificate);
47 }
48
49 AttestationData() {}
50
51 vector<uint8_t> attestationChallenge;
52 vector<uint8_t> attestationApplicationId;
53 vector<Certificate> attestationCertificate;
54 Status result;
55};
56
57struct TestEntryData {
58 TestEntryData(string nameSpace, string name, vector<int32_t> profileIds)
59 : nameSpace(nameSpace), name(name), profileIds(profileIds) {}
60
61 TestEntryData(string nameSpace, string name, const string& value, vector<int32_t> profileIds)
62 : TestEntryData(nameSpace, name, profileIds) {
63 valueCbor = cppbor::Tstr(((const char*)value.data())).encode();
64 }
65 TestEntryData(string nameSpace, string name, const vector<uint8_t>& value,
66 vector<int32_t> profileIds)
67 : TestEntryData(nameSpace, name, profileIds) {
68 valueCbor = cppbor::Bstr(value).encode();
69 }
70 TestEntryData(string nameSpace, string name, bool value, vector<int32_t> profileIds)
71 : TestEntryData(nameSpace, name, profileIds) {
72 valueCbor = cppbor::Bool(value).encode();
73 }
74 TestEntryData(string nameSpace, string name, int64_t value, vector<int32_t> profileIds)
75 : TestEntryData(nameSpace, name, profileIds) {
76 if (value >= 0) {
77 valueCbor = cppbor::Uint(value).encode();
78 } else {
79 valueCbor = cppbor::Nint(-value).encode();
80 }
81 }
82
83 string nameSpace;
84 string name;
85 vector<uint8_t> valueCbor;
86 vector<int32_t> profileIds;
87};
88
89struct TestProfile {
90 uint16_t id;
91 vector<uint8_t> readerCertificate;
92 bool userAuthenticationRequired;
93 uint64_t timeoutMillis;
94};
95
Selene Huangcab019a2020-03-11 04:37:48 -070096bool setupWritableCredential(sp<IWritableIdentityCredential>& writableCredential,
Selene Huang92b61d62020-03-04 02:24:16 -080097 sp<IIdentityCredentialStore>& credentialStore);
98
Selene Huangcab019a2020-03-11 04:37:48 -070099optional<vector<uint8_t>> generateReaderCertificate(string serialDecimal);
Selene Huang92b61d62020-03-04 02:24:16 -0800100
Selene Huangcab019a2020-03-11 04:37:48 -0700101optional<vector<uint8_t>> generateReaderCertificate(string serialDecimal,
102 vector<uint8_t>* outReaderPrivateKey);
Selene Huang92b61d62020-03-04 02:24:16 -0800103
Selene Huangcab019a2020-03-11 04:37:48 -0700104optional<vector<SecureAccessControlProfile>> addAccessControlProfiles(
Selene Huang92b61d62020-03-04 02:24:16 -0800105 sp<IWritableIdentityCredential>& writableCredential,
106 const vector<TestProfile>& testProfiles);
107
Selene Huangcab019a2020-03-11 04:37:48 -0700108bool addEntry(sp<IWritableIdentityCredential>& writableCredential, const TestEntryData& entry,
Selene Huang92b61d62020-03-04 02:24:16 -0800109 int dataChunkSize, map<const TestEntryData*, vector<vector<uint8_t>>>& encryptedBlobs,
110 bool expectSuccess);
111
Selene Huangcab019a2020-03-11 04:37:48 -0700112void setImageData(vector<uint8_t>& image);
Selene Huang92b61d62020-03-04 02:24:16 -0800113
Selene Huangcab019a2020-03-11 04:37:48 -0700114bool validateAttestationCertificate(const vector<Certificate>& inputCertificates,
115 const vector<uint8_t>& expectedChallenge,
116 const vector<uint8_t>& expectedAppId,
117 const HardwareInformation& hwInfo);
Selene Huang92b61d62020-03-04 02:24:16 -0800118
David Zeuthen28edb102020-04-28 18:54:55 -0400119vector<RequestNamespace> buildRequestNamespaces(const vector<TestEntryData> entries);
120
Selene Huang92b61d62020-03-04 02:24:16 -0800121} // namespace android::hardware::identity::test_utils
122
123#endif // VTS_IDENTITY_TEST_UTILS_H