Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | #define LOG_TAG "VtsAttestationTests" |
| 18 | |
| 19 | #include <aidl/Gtest.h> |
| 20 | #include <aidl/Vintf.h> |
| 21 | #include <android-base/logging.h> |
| 22 | #include <android/hardware/identity/IIdentityCredentialStore.h> |
| 23 | #include <android/hardware/identity/support/IdentityCredentialSupport.h> |
| 24 | #include <binder/IServiceManager.h> |
| 25 | #include <binder/ProcessState.h> |
| 26 | #include <cppbor.h> |
| 27 | #include <cppbor_parse.h> |
| 28 | #include <gtest/gtest.h> |
| 29 | #include <future> |
| 30 | #include <map> |
| 31 | |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 32 | #include "Util.h" |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 33 | |
| 34 | namespace android::hardware::identity { |
| 35 | |
| 36 | using std::endl; |
| 37 | using std::map; |
| 38 | using std::optional; |
| 39 | using std::string; |
| 40 | using std::vector; |
| 41 | |
| 42 | using ::android::sp; |
| 43 | using ::android::String16; |
| 44 | using ::android::binder::Status; |
| 45 | |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 46 | using test_utils::setupWritableCredential; |
| 47 | using test_utils::validateAttestationCertificate; |
| 48 | |
| 49 | // This file verifies the Identity Credential VTS Attestation Certificate |
| 50 | // generated. |
| 51 | class VtsAttestationTests : public testing::TestWithParam<std::string> { |
| 52 | public: |
| 53 | virtual void SetUp() override { |
| 54 | credentialStore_ = android::waitForDeclaredService<IIdentityCredentialStore>( |
| 55 | String16(GetParam().c_str())); |
| 56 | ASSERT_NE(credentialStore_, nullptr); |
| 57 | } |
| 58 | |
| 59 | sp<IIdentityCredentialStore> credentialStore_; |
| 60 | }; |
| 61 | |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 62 | TEST_P(VtsAttestationTests, verifyAttestationWithNonemptyChallengeNonemptyId) { |
| 63 | Status result; |
| 64 | |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 65 | sp<IWritableIdentityCredential> writableCredential; |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 66 | ASSERT_TRUE(setupWritableCredential(writableCredential, credentialStore_, |
| 67 | false /* testCredential */)); |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 68 | |
| 69 | string challenge = "NotSoRandomChallenge1NotSoRandomChallenge1NotSoRandomChallenge1"; |
| 70 | vector<uint8_t> attestationChallenge(challenge.begin(), challenge.end()); |
| 71 | vector<Certificate> attestationCertificate; |
| 72 | string applicationId = "Attestation Verification"; |
| 73 | vector<uint8_t> attestationApplicationId = {applicationId.begin(), applicationId.end()}; |
| 74 | |
| 75 | result = writableCredential->getAttestationCertificate( |
| 76 | attestationApplicationId, attestationChallenge, &attestationCertificate); |
| 77 | |
| 78 | ASSERT_TRUE(result.isOk()) << result.exceptionCode() << "; " << result.exceptionMessage() |
| 79 | << endl; |
| 80 | |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 81 | validateAttestationCertificate(attestationCertificate, attestationChallenge, |
| 82 | attestationApplicationId, false); |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | TEST_P(VtsAttestationTests, verifyAttestationWithVeryShortChallengeAndId) { |
| 86 | Status result; |
| 87 | |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 88 | sp<IWritableIdentityCredential> writableCredential; |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 89 | ASSERT_TRUE(setupWritableCredential(writableCredential, credentialStore_, |
| 90 | false /* testCredential */)); |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 91 | |
| 92 | string challenge = "c"; |
| 93 | vector<uint8_t> attestationChallenge(challenge.begin(), challenge.end()); |
| 94 | vector<Certificate> attestationCertificate; |
| 95 | string applicationId = "i"; |
| 96 | vector<uint8_t> attestationApplicationId = {applicationId.begin(), applicationId.end()}; |
| 97 | |
| 98 | result = writableCredential->getAttestationCertificate( |
| 99 | attestationApplicationId, attestationChallenge, &attestationCertificate); |
| 100 | |
| 101 | ASSERT_TRUE(result.isOk()) << result.exceptionCode() << "; " << result.exceptionMessage() |
| 102 | << endl; |
| 103 | |
David Zeuthen | 34abaae | 2020-10-26 20:26:36 -0400 | [diff] [blame] | 104 | validateAttestationCertificate(attestationCertificate, attestationChallenge, |
| 105 | attestationApplicationId, false); |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Dan Shi | ba4d532 | 2020-07-28 13:09:30 -0700 | [diff] [blame] | 108 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VtsAttestationTests); |
Selene Huang | cab019a | 2020-03-11 04:37:48 -0700 | [diff] [blame] | 109 | INSTANTIATE_TEST_SUITE_P( |
| 110 | Identity, VtsAttestationTests, |
| 111 | testing::ValuesIn(android::getAidlHalInstanceNames(IIdentityCredentialStore::descriptor)), |
| 112 | android::PrintInstanceNameToString); |
| 113 | |
| 114 | } // namespace android::hardware::identity |