1 /*
2 * Copyright (C) 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 <binder/RpcTlsTestUtils.h>
18 #include <binder/RpcTlsUtils.h>
19 #include <gtest/gtest.h>
20
21 namespace android {
22
toDebugString(EVP_PKEY * pkey)23 std::string toDebugString(EVP_PKEY* pkey) {
24 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
25 int res = EVP_PKEY_print_public(bio.get(), pkey, 2, nullptr);
26 std::string buf = "\nEVP_PKEY_print_public -> " + std::to_string(res) + "\n";
27 if (BIO_write(bio.get(), buf.data(), buf.length()) <= 0) return {};
28 res = EVP_PKEY_print_private(bio.get(), pkey, 2, nullptr);
29 buf = "\nEVP_PKEY_print_private -> " + std::to_string(res);
30 if (BIO_write(bio.get(), buf.data(), buf.length()) <= 0) return {};
31 const uint8_t* data;
32 size_t len;
33 if (!BIO_mem_contents(bio.get(), &data, &len)) return {};
34 return std::string(reinterpret_cast<const char*>(data), len);
35 }
36
37 class RpcTlsUtilsKeyTest : public testing::TestWithParam<RpcKeyFormat> {
38 public:
PrintParamInfo(const testing::TestParamInfo<ParamType> & info)39 static inline std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) {
40 return PrintToString(info.param);
41 }
42 };
43
TEST_P(RpcTlsUtilsKeyTest,Test)44 TEST_P(RpcTlsUtilsKeyTest, Test) {
45 auto pkey = makeKeyPairForSelfSignedCert();
46 ASSERT_NE(nullptr, pkey);
47 auto pkeyData = serializeUnencryptedPrivatekey(pkey.get(), GetParam());
48 auto deserializedPkey = deserializeUnencryptedPrivatekey(pkeyData, GetParam());
49 ASSERT_NE(nullptr, deserializedPkey);
50 EXPECT_EQ(1, EVP_PKEY_cmp(pkey.get(), deserializedPkey.get()))
51 << "expected: " << toDebugString(pkey.get())
52 << "\nactual: " << toDebugString(deserializedPkey.get());
53 }
54
55 INSTANTIATE_TEST_SUITE_P(RpcTlsUtilsTest, RpcTlsUtilsKeyTest,
56 testing::Values(RpcKeyFormat::PEM, RpcKeyFormat::DER),
57 RpcTlsUtilsKeyTest::PrintParamInfo);
58
59 class RpcTlsUtilsCertTest : public testing::TestWithParam<RpcCertificateFormat> {
60 public:
PrintParamInfo(const testing::TestParamInfo<ParamType> & info)61 static inline std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) {
62 return PrintToString(info.param);
63 }
64 };
65
TEST_P(RpcTlsUtilsCertTest,Test)66 TEST_P(RpcTlsUtilsCertTest, Test) {
67 auto pkey = makeKeyPairForSelfSignedCert();
68 ASSERT_NE(nullptr, pkey);
69 // Make certificate from the original key in memory
70 auto cert = makeSelfSignedCert(pkey.get(), kCertValidSeconds);
71 ASSERT_NE(nullptr, cert);
72 auto certData = serializeCertificate(cert.get(), GetParam());
73 auto deserializedCert = deserializeCertificate(certData, GetParam());
74 ASSERT_NE(nullptr, deserializedCert);
75 EXPECT_EQ(0, X509_cmp(cert.get(), deserializedCert.get()));
76 }
77
78 INSTANTIATE_TEST_SUITE_P(RpcTlsUtilsTest, RpcTlsUtilsCertTest,
79 testing::Values(RpcCertificateFormat::PEM, RpcCertificateFormat::DER),
80 RpcTlsUtilsCertTest::PrintParamInfo);
81
82 class RpcTlsUtilsKeyAndCertTest
83 : public testing::TestWithParam<std::tuple<RpcKeyFormat, RpcCertificateFormat>> {
84 public:
PrintParamInfo(const testing::TestParamInfo<ParamType> & info)85 static inline std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) {
86 auto [keyFormat, certificateFormat] = info.param;
87 return "key_" + PrintToString(keyFormat) + "_cert_" + PrintToString(certificateFormat);
88 }
89 };
90
TEST_P(RpcTlsUtilsKeyAndCertTest,TestCertFromDeserializedKey)91 TEST_P(RpcTlsUtilsKeyAndCertTest, TestCertFromDeserializedKey) {
92 auto [keyFormat, certificateFormat] = GetParam();
93 auto pkey = makeKeyPairForSelfSignedCert();
94 ASSERT_NE(nullptr, pkey);
95 auto pkeyData = serializeUnencryptedPrivatekey(pkey.get(), keyFormat);
96 auto deserializedPkey = deserializeUnencryptedPrivatekey(pkeyData, keyFormat);
97 ASSERT_NE(nullptr, deserializedPkey);
98
99 // Make certificate from deserialized key loaded from bytes
100 auto cert = makeSelfSignedCert(deserializedPkey.get(), kCertValidSeconds);
101 ASSERT_NE(nullptr, cert);
102 auto certData = serializeCertificate(cert.get(), certificateFormat);
103 auto deserializedCert = deserializeCertificate(certData, certificateFormat);
104 ASSERT_NE(nullptr, deserializedCert);
105 EXPECT_EQ(0, X509_cmp(cert.get(), deserializedCert.get()));
106 }
107
108 INSTANTIATE_TEST_SUITE_P(RpcTlsUtilsTest, RpcTlsUtilsKeyAndCertTest,
109 testing::Combine(testing::Values(RpcKeyFormat::PEM, RpcKeyFormat::DER),
110 testing::Values(RpcCertificateFormat::PEM,
111 RpcCertificateFormat::DER)),
112 RpcTlsUtilsKeyAndCertTest::PrintParamInfo);
113
114 } // namespace android
115