obu-firmware builds against the vanetza-idf C-ITS library, which until now came from the colleague's microbu-esp32c5 tree beside the repository and was not tracked here, so a clone of this repository could not build the firmware it ships. The library alone is now part of obu-firmware, as obu-firmware/external/vanetza-idf: their external/vanetza-idf at commit cf4b99f, unchanged (9775 files; see its PROVENANCE.md). CMake takes it from there by default; -DVANETZA_IDF_DIR still points the build elsewhere. The rest of the colleague's tree (their own VAM firmware, PKI tooling, station-link Python tools, the V2X2MAP bridge) stays out of this repository and gitignored; nothing is pushed to their repository. NOTES.md, docs/06, TODO.md and the pcap verifier's usage line point at the new location.
138 lines
3.5 KiB
C++
138 lines
3.5 KiB
C++
#include <vanetza/security/exception.hpp>
|
|
#include <vanetza/security/v2/public_key.hpp>
|
|
#include <boost/variant/apply_visitor.hpp>
|
|
#include <boost/variant/static_visitor.hpp>
|
|
|
|
namespace vanetza
|
|
{
|
|
namespace security
|
|
{
|
|
namespace v2
|
|
{
|
|
|
|
PublicKeyAlgorithm get_type(const PublicKey& key)
|
|
{
|
|
struct public_key_visitor : public boost::static_visitor<PublicKeyAlgorithm>
|
|
{
|
|
PublicKeyAlgorithm operator()(const ecdsa_nistp256_with_sha256&)
|
|
{
|
|
return PublicKeyAlgorithm::ECDSA_NISTP256_With_SHA256;
|
|
}
|
|
|
|
PublicKeyAlgorithm operator()(const ecies_nistp256&)
|
|
{
|
|
return PublicKeyAlgorithm::ECIES_NISTP256;
|
|
}
|
|
};
|
|
|
|
public_key_visitor visit;
|
|
return boost::apply_visitor(visit, key);
|
|
}
|
|
|
|
void serialize(OutputArchive& ar, const PublicKey& key)
|
|
{
|
|
struct public_key_visitor : public boost::static_visitor<>
|
|
{
|
|
public_key_visitor(OutputArchive& ar, PublicKeyAlgorithm algo) :
|
|
m_archive(ar), m_algo(algo)
|
|
{
|
|
}
|
|
|
|
void operator()(const ecdsa_nistp256_with_sha256& ecdsa)
|
|
{
|
|
serialize(m_archive, ecdsa.public_key, m_algo);
|
|
}
|
|
|
|
void operator()(const ecies_nistp256& ecies)
|
|
{
|
|
serialize(m_archive, ecies.supported_symm_alg);
|
|
serialize(m_archive, ecies.public_key, m_algo);
|
|
}
|
|
|
|
OutputArchive& m_archive;
|
|
PublicKeyAlgorithm m_algo;
|
|
};
|
|
|
|
PublicKeyAlgorithm type = get_type(key);
|
|
serialize(ar, type);
|
|
public_key_visitor visit(ar, type);
|
|
boost::apply_visitor(visit, key);
|
|
}
|
|
|
|
std::size_t field_size(PublicKeyAlgorithm algo)
|
|
{
|
|
size_t size = 0;
|
|
switch (algo) {
|
|
case PublicKeyAlgorithm::ECDSA_NISTP256_With_SHA256:
|
|
size = 32;
|
|
break;
|
|
case PublicKeyAlgorithm::ECIES_NISTP256:
|
|
size = 32;
|
|
break;
|
|
}
|
|
return size;
|
|
}
|
|
|
|
std::size_t field_size(SymmetricAlgorithm algo)
|
|
{
|
|
size_t size = 0;
|
|
switch (algo) {
|
|
case SymmetricAlgorithm::AES128_CCM:
|
|
size = 16;
|
|
break;
|
|
default:
|
|
throw deserialization_error("Unknown SymmetricAlgorithm");
|
|
break;
|
|
}
|
|
return size;
|
|
}
|
|
|
|
size_t deserialize(InputArchive& ar, PublicKey& key)
|
|
{
|
|
PublicKeyAlgorithm type;
|
|
deserialize(ar, type);
|
|
switch (type) {
|
|
case PublicKeyAlgorithm::ECDSA_NISTP256_With_SHA256: {
|
|
ecdsa_nistp256_with_sha256 ecdsa;
|
|
deserialize(ar, ecdsa.public_key, PublicKeyAlgorithm::ECDSA_NISTP256_With_SHA256);
|
|
key = ecdsa;
|
|
break;
|
|
}
|
|
case PublicKeyAlgorithm::ECIES_NISTP256: {
|
|
ecies_nistp256 ecies;
|
|
deserialize(ar, ecies.supported_symm_alg);
|
|
deserialize(ar, ecies.public_key, PublicKeyAlgorithm::ECIES_NISTP256);
|
|
key = ecies;
|
|
break;
|
|
}
|
|
default:
|
|
throw deserialization_error("Unknown PublicKeyAlgorithm");
|
|
break;
|
|
}
|
|
return get_size(key);
|
|
}
|
|
|
|
size_t get_size(const PublicKey& key)
|
|
{
|
|
size_t size = sizeof(PublicKeyAlgorithm);
|
|
struct publicKey_visitor : public boost::static_visitor<size_t>
|
|
{
|
|
size_t operator()(ecdsa_nistp256_with_sha256 key)
|
|
{
|
|
return get_size(key.public_key);
|
|
}
|
|
|
|
size_t operator()(ecies_nistp256 key)
|
|
{
|
|
return get_size(key.public_key) + sizeof(key.supported_symm_alg);
|
|
}
|
|
};
|
|
publicKey_visitor visit;
|
|
size += boost::apply_visitor(visit, key);
|
|
return size;
|
|
}
|
|
|
|
} // namespace v2
|
|
} // namespace security
|
|
} // namespace vanetza
|