blob: 80e52a21dada926424682490f08f06c3d3ba9583 [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>
David Zeuthen49f2d252020-10-16 11:27:24 -040024#include <gtest/gtest.h>
Selene Huang92b61d62020-03-04 02:24:16 -080025
26namespace android::hardware::identity::test_utils {
27
28using ::std::map;
29using ::std::optional;
30using ::std::string;
31using ::std::vector;
32
33using ::android::sp;
34using ::android::binder::Status;
35
36struct AttestationData {
37 AttestationData(sp<IWritableIdentityCredential>& writableCredential, string challenge,
David Zeuthen34abaae2020-10-26 20:26:36 -040038 vector<uint8_t> attestationAppId)
39 : attestationApplicationId(attestationAppId) {
Selene Huang92b61d62020-03-04 02:24:16 -080040 // ASSERT_NE(writableCredential, nullptr);
41
42 if (!challenge.empty()) {
43 attestationChallenge.assign(challenge.begin(), challenge.end());
44 }
45
46 result = writableCredential->getAttestationCertificate(
47 attestationApplicationId, attestationChallenge, &attestationCertificate);
48 }
49
50 AttestationData() {}
51
52 vector<uint8_t> attestationChallenge;
53 vector<uint8_t> attestationApplicationId;
54 vector<Certificate> attestationCertificate;
55 Status result;
56};
57
58struct TestEntryData {
59 TestEntryData(string nameSpace, string name, vector<int32_t> profileIds)
60 : nameSpace(nameSpace), name(name), profileIds(profileIds) {}
61
62 TestEntryData(string nameSpace, string name, const string& value, vector<int32_t> profileIds)
63 : TestEntryData(nameSpace, name, profileIds) {
64 valueCbor = cppbor::Tstr(((const char*)value.data())).encode();
65 }
66 TestEntryData(string nameSpace, string name, const vector<uint8_t>& value,
67 vector<int32_t> profileIds)
68 : TestEntryData(nameSpace, name, profileIds) {
69 valueCbor = cppbor::Bstr(value).encode();
70 }
71 TestEntryData(string nameSpace, string name, bool value, vector<int32_t> profileIds)
72 : TestEntryData(nameSpace, name, profileIds) {
73 valueCbor = cppbor::Bool(value).encode();
74 }
75 TestEntryData(string nameSpace, string name, int64_t value, vector<int32_t> profileIds)
76 : TestEntryData(nameSpace, name, profileIds) {
77 if (value >= 0) {
78 valueCbor = cppbor::Uint(value).encode();
79 } else {
80 valueCbor = cppbor::Nint(-value).encode();
81 }
82 }
83
84 string nameSpace;
85 string name;
86 vector<uint8_t> valueCbor;
87 vector<int32_t> profileIds;
88};
89
90struct TestProfile {
91 uint16_t id;
92 vector<uint8_t> readerCertificate;
93 bool userAuthenticationRequired;
94 uint64_t timeoutMillis;
95};
96
Selene Huangcab019a2020-03-11 04:37:48 -070097bool setupWritableCredential(sp<IWritableIdentityCredential>& writableCredential,
David Zeuthen34abaae2020-10-26 20:26:36 -040098 sp<IIdentityCredentialStore>& credentialStore, bool testCredential);
Selene Huang92b61d62020-03-04 02:24:16 -080099
Selene Huangcab019a2020-03-11 04:37:48 -0700100optional<vector<uint8_t>> generateReaderCertificate(string serialDecimal);
Selene Huang92b61d62020-03-04 02:24:16 -0800101
Selene Huangcab019a2020-03-11 04:37:48 -0700102optional<vector<uint8_t>> generateReaderCertificate(string serialDecimal,
103 vector<uint8_t>* outReaderPrivateKey);
Selene Huang92b61d62020-03-04 02:24:16 -0800104
Selene Huangcab019a2020-03-11 04:37:48 -0700105optional<vector<SecureAccessControlProfile>> addAccessControlProfiles(
Selene Huang92b61d62020-03-04 02:24:16 -0800106 sp<IWritableIdentityCredential>& writableCredential,
107 const vector<TestProfile>& testProfiles);
108
Selene Huangcab019a2020-03-11 04:37:48 -0700109bool addEntry(sp<IWritableIdentityCredential>& writableCredential, const TestEntryData& entry,
Selene Huang92b61d62020-03-04 02:24:16 -0800110 int dataChunkSize, map<const TestEntryData*, vector<vector<uint8_t>>>& encryptedBlobs,
111 bool expectSuccess);
112
Selene Huangcab019a2020-03-11 04:37:48 -0700113void setImageData(vector<uint8_t>& image);
Selene Huang92b61d62020-03-04 02:24:16 -0800114
David Zeuthen34abaae2020-10-26 20:26:36 -0400115void validateAttestationCertificate(const vector<Certificate>& credentialKeyCertChain,
Selene Huangcab019a2020-03-11 04:37:48 -0700116 const vector<uint8_t>& expectedChallenge,
David Zeuthen34abaae2020-10-26 20:26:36 -0400117 const vector<uint8_t>& expectedAppId, bool isTestCredential);
Selene Huang92b61d62020-03-04 02:24:16 -0800118
David Zeuthen28edb102020-04-28 18:54:55 -0400119vector<RequestNamespace> buildRequestNamespaces(const vector<TestEntryData> entries);
120
David Zeuthen34abaae2020-10-26 20:26:36 -0400121// Verifies that the X.509 certificate for a just created authentication key
122// is valid.
123//
124void verifyAuthKeyCertificate(const vector<uint8_t>& authKeyCertChain);
125
Selene Huang92b61d62020-03-04 02:24:16 -0800126} // namespace android::hardware::identity::test_utils
127
128#endif // VTS_IDENTITY_TEST_UTILS_H