Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 1 | /* |
| 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 Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 24 | #include <gtest/gtest.h> |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 25 | |
| 26 | namespace android::hardware::identity::test_utils { |
| 27 | |
| 28 | using ::std::map; |
| 29 | using ::std::optional; |
| 30 | using ::std::string; |
| 31 | using ::std::vector; |
| 32 | |
| 33 | using ::android::sp; |
| 34 | using ::android::binder::Status; |
| 35 | |
| 36 | struct AttestationData { |
| 37 | AttestationData(sp<IWritableIdentityCredential>& writableCredential, string challenge, |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 38 | vector<uint8_t> attestationAppId) |
| 39 | : attestationApplicationId(attestationAppId) { |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 40 | // 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 | |
| 58 | struct 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 | |
| 90 | struct TestProfile { |
| 91 | uint16_t id; |
| 92 | vector<uint8_t> readerCertificate; |
| 93 | bool userAuthenticationRequired; |
| 94 | uint64_t timeoutMillis; |
| 95 | }; |
| 96 | |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 97 | bool setupWritableCredential(sp<IWritableIdentityCredential>& writableCredential, |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 98 | sp<IIdentityCredentialStore>& credentialStore, bool testCredential); |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 99 | |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 100 | optional<vector<uint8_t>> generateReaderCertificate(string serialDecimal); |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 101 | |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 102 | optional<vector<uint8_t>> generateReaderCertificate(string serialDecimal, |
| 103 | vector<uint8_t>* outReaderPrivateKey); |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 104 | |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 105 | optional<vector<SecureAccessControlProfile>> addAccessControlProfiles( |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 106 | sp<IWritableIdentityCredential>& writableCredential, |
| 107 | const vector<TestProfile>& testProfiles); |
| 108 | |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 109 | bool addEntry(sp<IWritableIdentityCredential>& writableCredential, const TestEntryData& entry, |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 110 | int dataChunkSize, map<const TestEntryData*, vector<vector<uint8_t>>>& encryptedBlobs, |
| 111 | bool expectSuccess); |
| 112 | |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 113 | void setImageData(vector<uint8_t>& image); |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 114 | |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 115 | void validateAttestationCertificate(const vector<Certificate>& credentialKeyCertChain, |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 116 | const vector<uint8_t>& expectedChallenge, |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 117 | const vector<uint8_t>& expectedAppId, bool isTestCredential); |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 118 | |
David Zeuthen | 28edb10 | 2020-04-28 18:54:55 -0400 | [diff] [blame] | 119 | vector<RequestNamespace> buildRequestNamespaces(const vector<TestEntryData> entries); |
| 120 | |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 121 | // Verifies that the X.509 certificate for a just created authentication key |
| 122 | // is valid. |
| 123 | // |
| 124 | void verifyAuthKeyCertificate(const vector<uint8_t>& authKeyCertChain); |
| 125 | |
Selene Huang | 92b61d6 | 2020-03-04 02:24:16 -0800 | [diff] [blame] | 126 | } // namespace android::hardware::identity::test_utils |
| 127 | |
| 128 | #endif // VTS_IDENTITY_TEST_UTILS_H |