1 /*
2  * Copyright 2021 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 #include <android-base/properties.h>
18 #include <cppbor_parse.h>
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include <keymaster/android_keymaster_utils.h>
22 #include <keymaster/logger.h>
23 #include <keymaster/remote_provisioning_utils.h>
24 #include <openssl/curve25519.h>
25 #include <remote_prov/remote_prov_utils.h>
26 #include <cstdint>
27 #include "cppbor.h"
28 #include "keymaster/cppcose/cppcose.h"
29 
30 namespace aidl::android::hardware::security::keymint::remote_prov {
31 namespace {
32 
33 using ::keymaster::KeymasterBlob;
34 using ::keymaster::validateAndExtractEekPubAndId;
35 using ::testing::ElementsAreArray;
36 
TEST(RemoteProvUtilsTest,GenerateEekChainInvalidLength)37 TEST(RemoteProvUtilsTest, GenerateEekChainInvalidLength) {
38     ASSERT_FALSE(generateEekChain(1, /*eekId=*/{}));
39 }
40 
TEST(RemoteProvUtilsTest,GenerateEekChain)41 TEST(RemoteProvUtilsTest, GenerateEekChain) {
42     bytevec kTestEekId = {'t', 'e', 's', 't', 'I', 'd', 0};
43     for (size_t length : {2, 3, 31}) {
44         auto get_eek_result = generateEekChain(length, kTestEekId);
45         ASSERT_TRUE(get_eek_result) << get_eek_result.message();
46 
47         auto& [chain, pubkey, privkey] = *get_eek_result;
48 
49         auto validation_result = validateAndExtractEekPubAndId(
50                 /*testMode=*/true, KeymasterBlob(chain.data(), chain.size()));
51         ASSERT_TRUE(validation_result.isOk());
52 
53         auto& [eekPub, eekId] = *validation_result;
54         EXPECT_THAT(eekId, ElementsAreArray(kTestEekId));
55         EXPECT_THAT(eekPub, ElementsAreArray(pubkey));
56     }
57 }
58 
TEST(RemoteProvUtilsTest,GetProdEekChain)59 TEST(RemoteProvUtilsTest, GetProdEekChain) {
60     auto chain = getProdEekChain();
61 
62     auto validation_result = validateAndExtractEekPubAndId(
63             /*testMode=*/false, KeymasterBlob(chain.data(), chain.size()));
64     ASSERT_TRUE(validation_result.isOk()) << "Error: " << validation_result.moveError();
65 
66     auto& [eekPub, eekId] = *validation_result;
67 
68     auto [geekCert, ignoredNewPos, error] =
69             cppbor::parse(kCoseEncodedGeekCert, sizeof(kCoseEncodedGeekCert));
70     ASSERT_NE(geekCert, nullptr) << "Error: " << error;
71     ASSERT_NE(geekCert->asArray(), nullptr);
72 
73     auto& encodedGeekCoseKey = geekCert->asArray()->get(kCoseSign1Payload);
74     ASSERT_NE(encodedGeekCoseKey, nullptr);
75     ASSERT_NE(encodedGeekCoseKey->asBstr(), nullptr);
76 
77     auto geek = CoseKey::parse(encodedGeekCoseKey->asBstr()->value());
78     ASSERT_TRUE(geek) << "Error: " << geek.message();
79 
80     const std::vector<uint8_t> empty;
81     EXPECT_THAT(eekId, ElementsAreArray(geek->getBstrValue(CoseKey::KEY_ID).value_or(empty)));
82     EXPECT_THAT(eekPub, ElementsAreArray(geek->getBstrValue(CoseKey::PUBKEY_X).value_or(empty)));
83 }
84 
TEST(RemoteProvUtilsTest,JsonEncodeCsr)85 TEST(RemoteProvUtilsTest, JsonEncodeCsr) {
86     cppbor::Array array;
87     array.add(1);
88 
89     auto [json, error] = jsonEncodeCsrWithBuild(array);
90 
91     ASSERT_TRUE(error.empty()) << error;
92 
93     std::string expected = R"({"build_fingerprint":")" +
94                            ::android::base::GetProperty("ro.build.fingerprint", /*default=*/"") +
95                            R"(","csr":"gQE="})";
96 
97     ASSERT_EQ(json, expected);
98 }
99 
100 }  // namespace
101 }  // namespace aidl::android::hardware::security::keymint::remote_prov
102