From 7a55bb5cf8efa7570cd672dad08cff6a78fb8f88 Mon Sep 17 00:00:00 2001 From: Seth Moore Date: Tue, 22 Jun 2021 16:47:48 -0700 Subject: [PATCH] Add a unit test for remote_prov_utils This functionality will be used for the factory tooling, so we should test it. Additionally, some new functionality will soon be added, and it also needs to be tested. Ignore-AOSP-First: No merge path to aosp, will manually merge Test: libkeymint_remote_prov_support_test Bug: 191301285 Change-Id: I6a8798fc4b09fff1e829185a4b9e471921e5d2a9 --- security/keymint/support/Android.bp | 16 ++++++ .../keymint/support/remote_prov_utils.cpp | 4 ++ .../support/remote_prov_utils_test.cpp | 55 +++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 security/keymint/support/remote_prov_utils_test.cpp diff --git a/security/keymint/support/Android.bp b/security/keymint/support/Android.bp index 718133aa41..c2dba044bf 100644 --- a/security/keymint/support/Android.bp +++ b/security/keymint/support/Android.bp @@ -62,3 +62,19 @@ cc_library { "libcrypto", ], } + +cc_test { + name: "libkeymint_remote_prov_support_test", + srcs: ["remote_prov_utils_test.cpp"], + static_libs: [ + "libgmock", + "libgtest_main", + ], + shared_libs: [ + "libcppbor_external", + "libcppcose_rkp", + "libcrypto", + "libkeymaster_portable", + "libkeymint_remote_prov_support", + ], +} diff --git a/security/keymint/support/remote_prov_utils.cpp b/security/keymint/support/remote_prov_utils.cpp index 33f1ed3353..ac7cb6219f 100644 --- a/security/keymint/support/remote_prov_utils.cpp +++ b/security/keymint/support/remote_prov_utils.cpp @@ -31,6 +31,10 @@ bytevec randomBytes(size_t numBytes) { } ErrMsgOr generateEekChain(size_t length, const bytevec& eekId) { + if (length < 2) { + return "EEK chain must contain at least 2 certs."; + } + auto eekChain = cppbor::Array(); bytevec prev_priv_key; diff --git a/security/keymint/support/remote_prov_utils_test.cpp b/security/keymint/support/remote_prov_utils_test.cpp new file mode 100644 index 0000000000..fbf5b95897 --- /dev/null +++ b/security/keymint/support/remote_prov_utils_test.cpp @@ -0,0 +1,55 @@ +/* + * Copyright 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include + +namespace aidl::android::hardware::security::keymint::remote_prov { +namespace { + +using ::keymaster::KeymasterBlob; +using ::keymaster::validateAndExtractEekPubAndId; +using ::testing::ElementsAreArray; + +TEST(RemoteProvUtilsTest, GenerateEekChainInvalidLength) { + ASSERT_FALSE(generateEekChain(1, /*eekId=*/{})); +} + +TEST(RemoteProvUtilsTest, GenerateEekChain) { + bytevec kTestEekId = {'t', 'e', 's', 't', 'I', 'd', 0}; + for (size_t length : {2, 3, 31}) { + auto get_eek_result = generateEekChain(length, kTestEekId); + ASSERT_TRUE(get_eek_result) << get_eek_result.message(); + + auto& [chain, pubkey, privkey] = *get_eek_result; + + auto validation_result = validateAndExtractEekPubAndId( + /*testMode=*/true, KeymasterBlob(chain.data(), chain.size())); + ASSERT_TRUE(validation_result.isOk()); + + auto& [eekPub, eekId] = *validation_result; + EXPECT_THAT(eekId, ElementsAreArray(kTestEekId)); + EXPECT_THAT(eekPub, ElementsAreArray(pubkey)); + } +} + +} // namespace +} // namespace aidl::android::hardware::security::keymint::remote_prov