|
| 1 | +/** |
| 2 | + * @file tests/unit/test_crypto.cpp |
| 3 | + * @brief Test src/crypto.*. |
| 4 | + */ |
| 5 | +// test imports |
| 6 | +#include "../tests_common.h" |
| 7 | + |
| 8 | +// lib imports |
| 9 | +#include <openssl/x509.h> |
| 10 | + |
| 11 | +// local imports |
| 12 | +#include <src/crypto.h> |
| 13 | + |
| 14 | +TEST(CryptoTest, GeneratedCredentialsExposeSubjectAndVerifySignatures) { |
| 15 | + constexpr std::string_view common_name = "Sunshine Test Host"; |
| 16 | + constexpr std::string_view payload = "payload"; |
| 17 | + |
| 18 | + auto creds = crypto::gen_creds(common_name, 2048); |
| 19 | + ASSERT_FALSE(creds.x509.empty()); |
| 20 | + ASSERT_FALSE(creds.pkey.empty()); |
| 21 | + |
| 22 | + auto cert = crypto::x509(creds.x509); |
| 23 | + auto pkey = crypto::pkey(creds.pkey); |
| 24 | + ASSERT_NE(cert.get(), nullptr); |
| 25 | + ASSERT_NE(pkey.get(), nullptr); |
| 26 | + |
| 27 | + const auto subject = X509_get_subject_name(cert.get()); |
| 28 | + ASSERT_NE(subject, nullptr); |
| 29 | + |
| 30 | + const auto common_name_index = X509_NAME_get_index_by_NID(subject, NID_commonName, -1); |
| 31 | + ASSERT_GE(common_name_index, 0); |
| 32 | + |
| 33 | + const auto common_name_entry = X509_NAME_get_entry(subject, common_name_index); |
| 34 | + ASSERT_NE(common_name_entry, nullptr); |
| 35 | + |
| 36 | + const auto common_name_data = X509_NAME_ENTRY_get_data(common_name_entry); |
| 37 | + ASSERT_NE(common_name_data, nullptr); |
| 38 | + |
| 39 | + const std::string_view parsed_common_name { |
| 40 | + reinterpret_cast<const char *>(ASN1_STRING_get0_data(common_name_data)), |
| 41 | + static_cast<std::size_t>(ASN1_STRING_length(common_name_data)) |
| 42 | + }; |
| 43 | + ASSERT_EQ(parsed_common_name, common_name); |
| 44 | + |
| 45 | + ASSERT_FALSE(crypto::signature(cert).empty()); |
| 46 | + |
| 47 | + const auto signature = crypto::sign256(pkey, payload); |
| 48 | + ASSERT_FALSE(signature.empty()); |
| 49 | + ASSERT_TRUE(crypto::verify256(cert, payload, {reinterpret_cast<const char *>(signature.data()), signature.size()})); |
| 50 | +} |
0 commit comments