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