Vanetza
Loading...
Searching...
No Matches
serialization.hpp
1#ifndef SERIALIZATION_HPP_IENSIAL4
2#define SERIALIZATION_HPP_IENSIAL4
3
4#include <vanetza/common/serialization.hpp>
5#include <vanetza/security/v2/length_coding.hpp>
6#include <cassert>
7#include <list>
8
9namespace vanetza
10{
11namespace security
12{
13namespace v2
14{
15
16using vanetza::serialize;
17using vanetza::deserialize;
18
19/**
20 * \brief Serialize given length
21 * \param ar to serialize in
22 * \param size to encode
23 */
24void serialize_length(OutputArchive&, std::uintmax_t);
25
26/**
27 * \brief Deserialize length from a given archive
28 * \param ar shall start with encoded length
29 * \return length deserialized from archive
30 */
31std::uintmax_t deserialize_length(InputArchive&);
32
33/**
34 * \brief Calculate size of a list
35 *
36 * Sums up sizes of all list elements only, length itself is not included.
37 * Therefore, the returned length is suitable as argument for serialize_length.
38 *
39 * \tparam T list element type
40 * \param list
41 * \return accumulated elements' size
42 */
43template<class T>
44size_t get_size(const std::list<T>& list)
45{
46 using vanetza::security::v2::get_size;
47 size_t size = 0;
48 for (auto& elem : list) {
49 size += get_size(elem);
50 }
51 return size;
52}
53
54/**
55 * \brief Trim (possibly) wider size type safely
56 *
57 * This function throws an exception if size would be truncated.
58 *
59 * \param in wide size type
60 * \return same size using narrow type
61 */
62std::size_t trim_size(std::uintmax_t in);
63
64/** \brief Serialize from any given list into given binary archive
65 * \tparam T the type of the list
66 * \tparam ARGS all additional arguments for the underlying functions
67 * \param ar to serialize in
68 * \param list
69 * \param args the additional arguments
70 */
71template<class T, typename... ARGS>
72void serialize(OutputArchive& ar, const std::list<T>& list, ARGS&&... args)
73{
74 using vanetza::security::v2::get_size;
75 using vanetza::security::v2::serialize;
76 size_t size = get_size(list);
77 serialize_length(ar, size);
78 for (auto& elem : list) {
79 serialize(ar, elem, std::forward<ARGS>(args)...);
80 }
81}
82
83/** \brief Deserialize a list from given archive
84 * \tparam T the type of the list
85 * \tparam ARGS all additional arguments for the underlying functions
86 * \param ar, shall start with the list
87 * \param args the additional arguments
88 * \return size of the deserialized list in bytes
89 */
90template<class T, typename... ARGS>
91std::size_t deserialize(InputArchive& ar, std::list<T>& list, ARGS&&... args)
92{
93 using vanetza::security::v2::deserialize;
94 static const std::size_t length_limit = 4096;
95
96 const auto length = trim_size(deserialize_length(ar));
97 if (length <= length_limit) {
98 std::size_t remainder = length;
99 while (remainder > 0) {
100 T t;
101 std::size_t size = deserialize(ar, t, std::forward<ARGS>(args)...);
102 if (size <= remainder && ar.is_good()) {
103 list.push_back(std::move(t));
104 remainder -= size;
105 } else {
106 ar.fail(InputArchive::ErrorCode::ConstraintViolation);
107 break;
108 }
109 }
110 } else {
111 ar.fail(InputArchive::ErrorCode::ExcessiveLength);
112 }
113
114 return length;
115}
116
117} // namespace v2
118} // namespace security
119} // namespace vanetza
120
121#endif /* SERIALIZATION_HPP_IENSIAL4 */
ChunckPacket is a packet consisting of several memory chunks.
ByteBufferConvertible & operator[](OsiLayer ol)
ChunkPacket & merge(ChunkPacket &packet, OsiLayer from, OsiLayer to)
std::size_t size() const
const ByteBufferConvertible & layer(OsiLayer ol) const
const ByteBufferConvertible & operator[](OsiLayer ol) const
ByteBufferConvertible & layer(OsiLayer ol)
std::size_t size(OsiLayer from, OsiLayer to) const
ChunkPacket extract(OsiLayer from, OsiLayer to)
std::size_t size(OsiLayer from, OsiLayer to) const
buffer_const_range operator[](OsiLayer layer) const
std::size_t size() const
void set_boundary(OsiLayer, unsigned bytes)
const ByteBuffer & buffer() const
CohesivePacket(const ByteBuffer &buffer, OsiLayer layer)
void trim(OsiLayer from, unsigned bytes)
std::size_t size(OsiLayer single_layer) const
Result create(Args... args) const
Definition factory.hpp:45
value_type operator[](size_type) const
Definition byte_view.cpp:48
byte_view_range(ByteBuffer &&)
Definition byte_view.cpp:37
byte_view_range(const ByteBuffer::const_iterator &, const ByteBuffer::const_iterator &)
Definition byte_view.cpp:27
ByteBuffer::const_pointer data() const
Definition byte_view.cpp:42
void encode(units::Duration)
Definition lifetime.cpp:45
units::Duration decode() const
Definition lifetime.cpp:59
bool after(const Timestamp &other) const
Definition timestamp.cpp:76
bool before(const Timestamp &other) const
Definition timestamp.cpp:71
virtual boost::optional< Uncompressed > decompress_point(const EccPoint &ecc_point)=0
decompress a possibly compressed elliptic curve point
static CertificateValidity valid()
Create CertificateValidity signalling a valid certificate This method is equivalent to default constr...
CertificateValidity(CertificateInvalidReason reason)
CertificateInvalidReason reason() const
Get reason for certificate invalidity This call is only safe if reason is available,...
IntX specified in TS 103 097 v1.2.1, section 4.2.1.
Definition int_x.hpp:21
Compressed_Lsb_Y_0 specified in TS 103 097 v1.2.1 in section 4.2.5.
Definition ecc_point.hpp:24
Compressed_Lsb_Y_1 specified in TS 103 097 v1.2.1 in section 4.2.5.
Definition ecc_point.hpp:30
static DecapConfirm from(VerifyConfirm &&verify_confirm, const SecuredMessageView &msg_view)
Input data for decapsulating a secured message.
EcdsaSignature specified in TS 103 097 v1.2.1, section 4.2.9.
Definition signature.hpp:17
Uncompressed specified in TS 103 097 v1.2.1 in section 4.2.5.
Definition ecc_point.hpp:36
X_Coordinate_Only specified in TS 103 097 v1.2.1 in section 4.2.5.
Definition ecc_point.hpp:18
described in TS 103 097 v1.2.1 (2015-06), section 6.1
void add_permission(ItsAid aid, const ByteBuffer &ssp)
const ValidityRestriction * get_restriction(ValidityRestrictionType type) const
const SubjectAttribute * get_attribute(SubjectAttributeType type) const
void remove_attribute(SubjectAttributeType type)
const validity_restriction_type< T > * get_restriction() const
const subject_attribute_type< T > * get_attribute() const
void remove_restriction(ValidityRestrictionType type)
Payload specified in TS 103 097 v1.2.1, section 5.2.
Definition payload.hpp:28
SecuredMessage as specified in TS 103 097 v1.2.1, section 5.1.
HeaderField * header_field(HeaderFieldType)
const TrailerField * trailer_field(TrailerFieldType type) const
const HeaderField * header_field(HeaderFieldType type) const
TrailerField * trailer_field(TrailerFieldType)
described in TS 103 097 v1.2.1, section 6.2
ThreeDLocation specified in TS 103 097 v1.2.1, section 4.2.19.
Definition region.hpp:21
Time64WithStandardDeviation specified in TS 103 097 v1.2.1, section 4.2.16.
resolve type for matching HeaderFieldType
resolve type for matching TrailerFieldType