xref: /aosp_15_r20/frameworks/native/libs/binder/tests/RpcTlsUtilsTest.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2021 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #include <binder/RpcTlsTestUtils.h>
18*38e8c45fSAndroid Build Coastguard Worker #include <binder/RpcTlsUtils.h>
19*38e8c45fSAndroid Build Coastguard Worker #include <gtest/gtest.h>
20*38e8c45fSAndroid Build Coastguard Worker 
21*38e8c45fSAndroid Build Coastguard Worker namespace android {
22*38e8c45fSAndroid Build Coastguard Worker 
toDebugString(EVP_PKEY * pkey)23*38e8c45fSAndroid Build Coastguard Worker std::string toDebugString(EVP_PKEY* pkey) {
24*38e8c45fSAndroid Build Coastguard Worker     bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
25*38e8c45fSAndroid Build Coastguard Worker     int res = EVP_PKEY_print_public(bio.get(), pkey, 2, nullptr);
26*38e8c45fSAndroid Build Coastguard Worker     std::string buf = "\nEVP_PKEY_print_public -> " + std::to_string(res) + "\n";
27*38e8c45fSAndroid Build Coastguard Worker     if (BIO_write(bio.get(), buf.data(), buf.length()) <= 0) return {};
28*38e8c45fSAndroid Build Coastguard Worker     res = EVP_PKEY_print_private(bio.get(), pkey, 2, nullptr);
29*38e8c45fSAndroid Build Coastguard Worker     buf = "\nEVP_PKEY_print_private -> " + std::to_string(res);
30*38e8c45fSAndroid Build Coastguard Worker     if (BIO_write(bio.get(), buf.data(), buf.length()) <= 0) return {};
31*38e8c45fSAndroid Build Coastguard Worker     const uint8_t* data;
32*38e8c45fSAndroid Build Coastguard Worker     size_t len;
33*38e8c45fSAndroid Build Coastguard Worker     if (!BIO_mem_contents(bio.get(), &data, &len)) return {};
34*38e8c45fSAndroid Build Coastguard Worker     return std::string(reinterpret_cast<const char*>(data), len);
35*38e8c45fSAndroid Build Coastguard Worker }
36*38e8c45fSAndroid Build Coastguard Worker 
37*38e8c45fSAndroid Build Coastguard Worker class RpcTlsUtilsKeyTest : public testing::TestWithParam<RpcKeyFormat> {
38*38e8c45fSAndroid Build Coastguard Worker public:
PrintParamInfo(const testing::TestParamInfo<ParamType> & info)39*38e8c45fSAndroid Build Coastguard Worker     static inline std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) {
40*38e8c45fSAndroid Build Coastguard Worker         return PrintToString(info.param);
41*38e8c45fSAndroid Build Coastguard Worker     }
42*38e8c45fSAndroid Build Coastguard Worker };
43*38e8c45fSAndroid Build Coastguard Worker 
TEST_P(RpcTlsUtilsKeyTest,Test)44*38e8c45fSAndroid Build Coastguard Worker TEST_P(RpcTlsUtilsKeyTest, Test) {
45*38e8c45fSAndroid Build Coastguard Worker     auto pkey = makeKeyPairForSelfSignedCert();
46*38e8c45fSAndroid Build Coastguard Worker     ASSERT_NE(nullptr, pkey);
47*38e8c45fSAndroid Build Coastguard Worker     auto pkeyData = serializeUnencryptedPrivatekey(pkey.get(), GetParam());
48*38e8c45fSAndroid Build Coastguard Worker     auto deserializedPkey = deserializeUnencryptedPrivatekey(pkeyData, GetParam());
49*38e8c45fSAndroid Build Coastguard Worker     ASSERT_NE(nullptr, deserializedPkey);
50*38e8c45fSAndroid Build Coastguard Worker     EXPECT_EQ(1, EVP_PKEY_cmp(pkey.get(), deserializedPkey.get()))
51*38e8c45fSAndroid Build Coastguard Worker             << "expected: " << toDebugString(pkey.get())
52*38e8c45fSAndroid Build Coastguard Worker             << "\nactual: " << toDebugString(deserializedPkey.get());
53*38e8c45fSAndroid Build Coastguard Worker }
54*38e8c45fSAndroid Build Coastguard Worker 
55*38e8c45fSAndroid Build Coastguard Worker INSTANTIATE_TEST_SUITE_P(RpcTlsUtilsTest, RpcTlsUtilsKeyTest,
56*38e8c45fSAndroid Build Coastguard Worker                          testing::Values(RpcKeyFormat::PEM, RpcKeyFormat::DER),
57*38e8c45fSAndroid Build Coastguard Worker                          RpcTlsUtilsKeyTest::PrintParamInfo);
58*38e8c45fSAndroid Build Coastguard Worker 
59*38e8c45fSAndroid Build Coastguard Worker class RpcTlsUtilsCertTest : public testing::TestWithParam<RpcCertificateFormat> {
60*38e8c45fSAndroid Build Coastguard Worker public:
PrintParamInfo(const testing::TestParamInfo<ParamType> & info)61*38e8c45fSAndroid Build Coastguard Worker     static inline std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) {
62*38e8c45fSAndroid Build Coastguard Worker         return PrintToString(info.param);
63*38e8c45fSAndroid Build Coastguard Worker     }
64*38e8c45fSAndroid Build Coastguard Worker };
65*38e8c45fSAndroid Build Coastguard Worker 
TEST_P(RpcTlsUtilsCertTest,Test)66*38e8c45fSAndroid Build Coastguard Worker TEST_P(RpcTlsUtilsCertTest, Test) {
67*38e8c45fSAndroid Build Coastguard Worker     auto pkey = makeKeyPairForSelfSignedCert();
68*38e8c45fSAndroid Build Coastguard Worker     ASSERT_NE(nullptr, pkey);
69*38e8c45fSAndroid Build Coastguard Worker     // Make certificate from the original key in memory
70*38e8c45fSAndroid Build Coastguard Worker     auto cert = makeSelfSignedCert(pkey.get(), kCertValidSeconds);
71*38e8c45fSAndroid Build Coastguard Worker     ASSERT_NE(nullptr, cert);
72*38e8c45fSAndroid Build Coastguard Worker     auto certData = serializeCertificate(cert.get(), GetParam());
73*38e8c45fSAndroid Build Coastguard Worker     auto deserializedCert = deserializeCertificate(certData, GetParam());
74*38e8c45fSAndroid Build Coastguard Worker     ASSERT_NE(nullptr, deserializedCert);
75*38e8c45fSAndroid Build Coastguard Worker     EXPECT_EQ(0, X509_cmp(cert.get(), deserializedCert.get()));
76*38e8c45fSAndroid Build Coastguard Worker }
77*38e8c45fSAndroid Build Coastguard Worker 
78*38e8c45fSAndroid Build Coastguard Worker INSTANTIATE_TEST_SUITE_P(RpcTlsUtilsTest, RpcTlsUtilsCertTest,
79*38e8c45fSAndroid Build Coastguard Worker                          testing::Values(RpcCertificateFormat::PEM, RpcCertificateFormat::DER),
80*38e8c45fSAndroid Build Coastguard Worker                          RpcTlsUtilsCertTest::PrintParamInfo);
81*38e8c45fSAndroid Build Coastguard Worker 
82*38e8c45fSAndroid Build Coastguard Worker class RpcTlsUtilsKeyAndCertTest
83*38e8c45fSAndroid Build Coastguard Worker       : public testing::TestWithParam<std::tuple<RpcKeyFormat, RpcCertificateFormat>> {
84*38e8c45fSAndroid Build Coastguard Worker public:
PrintParamInfo(const testing::TestParamInfo<ParamType> & info)85*38e8c45fSAndroid Build Coastguard Worker     static inline std::string PrintParamInfo(const testing::TestParamInfo<ParamType>& info) {
86*38e8c45fSAndroid Build Coastguard Worker         auto [keyFormat, certificateFormat] = info.param;
87*38e8c45fSAndroid Build Coastguard Worker         return "key_" + PrintToString(keyFormat) + "_cert_" + PrintToString(certificateFormat);
88*38e8c45fSAndroid Build Coastguard Worker     }
89*38e8c45fSAndroid Build Coastguard Worker };
90*38e8c45fSAndroid Build Coastguard Worker 
TEST_P(RpcTlsUtilsKeyAndCertTest,TestCertFromDeserializedKey)91*38e8c45fSAndroid Build Coastguard Worker TEST_P(RpcTlsUtilsKeyAndCertTest, TestCertFromDeserializedKey) {
92*38e8c45fSAndroid Build Coastguard Worker     auto [keyFormat, certificateFormat] = GetParam();
93*38e8c45fSAndroid Build Coastguard Worker     auto pkey = makeKeyPairForSelfSignedCert();
94*38e8c45fSAndroid Build Coastguard Worker     ASSERT_NE(nullptr, pkey);
95*38e8c45fSAndroid Build Coastguard Worker     auto pkeyData = serializeUnencryptedPrivatekey(pkey.get(), keyFormat);
96*38e8c45fSAndroid Build Coastguard Worker     auto deserializedPkey = deserializeUnencryptedPrivatekey(pkeyData, keyFormat);
97*38e8c45fSAndroid Build Coastguard Worker     ASSERT_NE(nullptr, deserializedPkey);
98*38e8c45fSAndroid Build Coastguard Worker 
99*38e8c45fSAndroid Build Coastguard Worker     // Make certificate from deserialized key loaded from bytes
100*38e8c45fSAndroid Build Coastguard Worker     auto cert = makeSelfSignedCert(deserializedPkey.get(), kCertValidSeconds);
101*38e8c45fSAndroid Build Coastguard Worker     ASSERT_NE(nullptr, cert);
102*38e8c45fSAndroid Build Coastguard Worker     auto certData = serializeCertificate(cert.get(), certificateFormat);
103*38e8c45fSAndroid Build Coastguard Worker     auto deserializedCert = deserializeCertificate(certData, certificateFormat);
104*38e8c45fSAndroid Build Coastguard Worker     ASSERT_NE(nullptr, deserializedCert);
105*38e8c45fSAndroid Build Coastguard Worker     EXPECT_EQ(0, X509_cmp(cert.get(), deserializedCert.get()));
106*38e8c45fSAndroid Build Coastguard Worker }
107*38e8c45fSAndroid Build Coastguard Worker 
108*38e8c45fSAndroid Build Coastguard Worker INSTANTIATE_TEST_SUITE_P(RpcTlsUtilsTest, RpcTlsUtilsKeyAndCertTest,
109*38e8c45fSAndroid Build Coastguard Worker                          testing::Combine(testing::Values(RpcKeyFormat::PEM, RpcKeyFormat::DER),
110*38e8c45fSAndroid Build Coastguard Worker                                           testing::Values(RpcCertificateFormat::PEM,
111*38e8c45fSAndroid Build Coastguard Worker                                                           RpcCertificateFormat::DER)),
112*38e8c45fSAndroid Build Coastguard Worker                          RpcTlsUtilsKeyAndCertTest::PrintParamInfo);
113*38e8c45fSAndroid Build Coastguard Worker 
114*38e8c45fSAndroid Build Coastguard Worker } // namespace android
115