Files
MicrOBU/microbu-esp32c5/external/vanetza-idf/tools/pki/tests/signed_builder.cpp
T
Ashin Walpola 0e9525162d Keep the colleague's microbu-esp32c5 tree in this repository
obu-firmware builds against vanetza-idf from microbu-esp32c5/external, but
that tree was gitignored, so a clone of this repository could not build the
firmware it ships. It is now committed here as ordinary files in its own
folder, microbu-esp32c5/: the colleague's commit cf4b99f plus the V2X2MAP
bridge's signature verification (--trust) used on the bench. Nothing is
fetched from or pushed to the colleague's repository; this repository and
its remotes carry everything. The folder's own .gitignore keeps build output,
downloaded components and private key material out, as it did there; the
committed file set is identical to that repository's tracked files.

The ESP32-C5 is still flashed from obu-firmware/, which only takes
vanetza-idf from microbu-esp32c5/, so the two stay separate folders.
FLASHING.md says how to take a newer version of the colleague's tree (copy
it over the folder, rebuild, test, commit).
2026-09-23 17:46:40 +02:00

73 lines
2.8 KiB
C++

#include "signed_builder.hpp"
#include "asn1.hpp"
#include "certificate.hpp"
#include "hashed_id8.hpp"
#include "mock_credential_storage.hpp"
#include "openssl_security_module.hpp"
#include "stub_certificate.hpp"
#include "validation.hpp"
#include <vanetza/asn1/security/SignedData.h>
#include <vanetza/common/byte_buffer.hpp>
#include <gtest/gtest.h>
#include <cstring>
#include <memory>
namespace vanetza
{
namespace pki
{
class SignedBuilderTest : public ::testing::Test
{
protected:
SignedBuilderTest() : m_credentials(std::make_shared<MockCredentialStorage>()), m_security(m_credentials)
{
}
std::shared_ptr<MockCredentialStorage> m_credentials;
OpenSslSecurityModule m_security;
};
// create_external_signed produces an EtsiTs103097Data-SignedExternalPayload
// envelope whose SignedData:
// - tbsData.payload.extDataHash is set (not data)
// - extDataHash carries SHA-256(external_payload) when hash_algo is SHA256
// - signer = digest(cert) when a cert is provided
// - signature verifies against the cert's verification key
TEST_F(SignedBuilderTest, external_signed_carries_payload_hash_and_verifies)
{
PublicKey ec_key = m_security.create_key(KeyType::NistP256);
Certificate ec_cert = build_stub_certificate(ec_key);
ByteBuffer payload { 0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE };
SignedData envelope = create_external_signed(payload, m_security, ec_key, HashAlgorithm::SHA256, &ec_cert);
ASSERT_EQ(Vanetza_Security_Ieee1609Dot2Content_PR_signedData, envelope->content->present);
Vanetza_Security_SignedData_t* sd = envelope->content->choice.signedData;
ASSERT_NE(nullptr, sd);
// payload.extDataHash present, payload.data absent
ASSERT_NE(nullptr, sd->tbsData);
ASSERT_NE(nullptr, sd->tbsData->payload);
ASSERT_NE(nullptr, sd->tbsData->payload->extDataHash);
EXPECT_EQ(nullptr, sd->tbsData->payload->data);
ASSERT_EQ(Vanetza_Security_HashedData_PR_sha256HashedData, sd->tbsData->payload->extDataHash->present);
// hash equals SHA-256(payload)
Sha256Hash expected = m_security.calculate_sha256_hash(payload.data(), payload.size());
const auto& hash_oct = sd->tbsData->payload->extDataHash->choice.sha256HashedData;
ASSERT_EQ(32, hash_oct.size);
EXPECT_EQ(0, std::memcmp(hash_oct.buf, expected.octets.data(), 32));
// signer = digest, matches HashedId8(ec_cert)
ASSERT_EQ(Vanetza_Security_SignerIdentifier_PR_digest, sd->signer.present);
HashedId8 ec_hid8 = ec_cert.calculate_hashed_id8(m_security);
EXPECT_TRUE(sd->signer.choice.digest == ec_hid8.octets);
// signature verifies against ec_cert's verification key
Sha256Hash digest = calculate_digest<Sha256Hash>(m_security, *sd->tbsData, &ec_cert.raw());
EXPECT_TRUE(m_security.verify(digest, make_signature(sd->signature), ec_key));
}
} // namespace pki
} // namespace vanetza