Skip to content

Commit

Permalink
geonet: handle incoming secured message v3
Browse files Browse the repository at this point in the history
  • Loading branch information
riebl committed Aug 5, 2023
1 parent af3edca commit 99a24c6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
15 changes: 15 additions & 0 deletions vanetza/common/archives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ void InputArchive::load_binary(char* data, std::size_t len)
}
}

char InputArchive::peek_byte()
{
auto got = m_stream_buffer->sgetc();
if (got == StreamBuffer::traits_type::eof()) {
throw Exception("impossible peek at end of stream");
} else {
return StreamBuffer::traits_type::to_char_type(got);
}
}

std::size_t InputArchive::remaining_bytes()
{
return m_stream_buffer->in_avail();
}

OutputArchive::OutputArchive(OutputStream& os) :
m_stream_buffer(os.rdbuf())
{
Expand Down
2 changes: 2 additions & 0 deletions vanetza/common/archives.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class InputArchive

void load_binary(unsigned char* data, std::size_t len);
void load_binary(char* data, std::size_t len);
char peek_byte();
std::size_t remaining_bytes();

private:
StreamBuffer* m_stream_buffer;
Expand Down
13 changes: 10 additions & 3 deletions vanetza/geonet/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,16 @@ std::size_t Parser::parse_secured(boost::optional<security::SecuredMessage>& sec
{
std::size_t bytes = 0;
try {
security::SecuredMessage tmp;
bytes = security::deserialize(m_archive, tmp);
secured = std::move(tmp);
std::uint8_t sec_first_byte = m_archive.peek_byte();
if (sec_first_byte < 3) {
security::v2::SecuredMessage msg;
bytes = security::v2::deserialize(m_archive, msg);
secured = std::move(msg);
} else if (sec_first_byte == 3) {
security::v3::SecuredMessage msg;
bytes = security::v3::deserialize(m_archive, msg);
secured = std::move(msg);
}
} catch (InputArchive::Exception&) {
} catch (security::deserialization_error&) {
}
Expand Down
25 changes: 25 additions & 0 deletions vanetza/security/secured_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ ItsAid get_its_aid(const SecuredMessage& msg)
{
return get_its_aid(msg);
}

ItsAid operator()(const v3::SecuredMessage& msg) const
{
return msg.its_aid();
}
};

return boost::apply_visitor(Visitor(), msg);
Expand All @@ -28,6 +33,11 @@ std::size_t get_size(const SecuredMessage& msg)
{
return get_size(msg);
}

std::size_t operator()(const v3::SecuredMessage& msg) const
{
return msg.size();
}
};

return boost::apply_visitor(Visitor(), msg);
Expand All @@ -44,6 +54,11 @@ void serialize(OutputArchive& ar, const SecuredMessage& msg)
{
serialize(m_archive, msg);
}

void operator()(const v3::SecuredMessage& msg)
{
serialize(m_archive, msg);
}
};

Visitor visitor(ar);
Expand All @@ -61,6 +76,11 @@ std::size_t deserialize(InputArchive& ar, SecuredMessage& msg)
{
return deserialize(m_archive, msg);
}

std::size_t operator()(v3::SecuredMessage& msg)
{
return deserialize(m_archive, msg);
}
};

Visitor visitor(ar);
Expand All @@ -75,6 +95,11 @@ PacketVariant get_payload_copy(const SecuredMessage& msg)
{
return msg.payload.data;
}

PacketVariant operator()(const v3::SecuredMessage& msg) const
{
return msg.payload();
}
};

return boost::apply_visitor(Visitor {}, msg);
Expand Down
3 changes: 2 additions & 1 deletion vanetza/security/secured_message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
#include <vanetza/common/serialization.hpp>
#include <vanetza/net/packet_variant.hpp>
#include <vanetza/security/v2/secured_message.hpp>
#include <vanetza/security/v3/secured_message.hpp>
#include <boost/variant/variant.hpp>

namespace vanetza
{
namespace security
{

using SecuredMessage = boost::variant<v2::SecuredMessage>;
using SecuredMessage = boost::variant<v2::SecuredMessage, v3::SecuredMessage>;

ItsAid get_its_aid(const SecuredMessage&);

Expand Down

0 comments on commit 99a24c6

Please sign in to comment.