Vanetza
Loading...
Searching...
No Matches
secured_message.hpp
1#ifndef SECURED_MESSAGE_HPP_DCBC74AC
2#define SECURED_MESSAGE_HPP_DCBC74AC
3
4#include <vanetza/asn1/asn1c_wrapper.hpp>
5#include <vanetza/asn1/security_profile.hpp>
6#include VANETZA_ASN1_SECURITY_HEADER(EtsiTs103097Data.h)
7#include <vanetza/common/archives.hpp>
8#include <vanetza/common/its_aid.hpp>
9#include <vanetza/net/packet_variant.hpp>
10#include <vanetza/security/hash_algorithm.hpp>
11#include <vanetza/security/hashed_id.hpp>
12#include <vanetza/security/public_key.hpp>
13#include <vanetza/security/signature.hpp>
14#include <vanetza/security/v3/asn1_types.hpp>
15#include <vanetza/security/v3/certificate.hpp>
16
17#include <boost/optional/optional_fwd.hpp>
18#include <boost/variant/variant_fwd.hpp>
19#include <array>
20#include <cstdint>
21#include <list>
22
23namespace vanetza
24{
25namespace security
26{
27namespace v3
28{
29
31{
32 using Time64 = std::uint64_t;
33 using SignerIdentifier = boost::variant<const asn1::HashedId8*, const asn1::Certificate*>;
34
35 SecuredMessage();
36 static SecuredMessage with_signed_data();
37 static SecuredMessage with_signed_data_hash();
38 static SecuredMessage with_encrypted_data();
39
40 uint8_t protocol_version() const;
41 ItsAid its_aid() const;
42 PacketVariant payload() const;
43 boost::optional<HashedId8> certificate_id() const;
44 bool is_signed() const;
45 bool is_encrypted() const;
46 boost::optional<Time64> generation_time() const;
47 boost::optional<Signature> signature() const;
48 SignerIdentifier signer_identifier() const;
49 ByteBuffer signing_payload() const;
50 HashAlgorithm hash_id() const;
51
52 void set_its_aid(ItsAid its_aid);
53 void set_generation_time(Time64 time);
54 void set_generation_location(const asn1::ThreeDLocation& location);
55 void set_payload(const ByteBuffer& payload);
56 void set_external_payload_hash(const Sha256Digest& hash);
57 void set_hash_id(HashAlgorithm);
58 void set_signature(const Signature& signature);
59 std::list<HashedId3> get_inline_p2pcd_request() const;
60 void set_inline_p2pcd_request(std::list<HashedId3> requests);
61 void add_inline_p2pcd_request(HashedId3 unkown_certificate_digest);
62 void set_signature(const SomeEcdsaSignature& signature);
63 void set_dummy_signature();
64 void set_signer_identifier_self();
65 void set_signer_identifier(const HashedId8&);
66 void set_signer_identifier(const Certificate&);
67 void set_requested_certificate(const Certificate&);
68
69 void get_aes_ccm_ciphertext(ByteBuffer& ccm_ciphertext, std::array<uint8_t, 12>& nonce) const;
70 void set_aes_ccm_ciphertext(const ByteBuffer& ccm_ciphertext, const std::array<uint8_t, 12>& nonce);
71 void set_cert_recip_info(const HashedId8& recipient_id,
72 const std::array<uint8_t, 16>& ecies_ciphertext,
73 const std::array<uint8_t, 16>& ecies_tag,
74 const PublicKey& ecies_pub_key);
75 bool check_psk_match(const std::array<uint8_t, 16>& psk) const;
76};
77
78/**
79 * \brief Calculate size of encoded secured message
80 * \param msg secured message
81 * \return number of octets needed to serialize this message
82 */
83size_t get_size(const SecuredMessage& msg);
84
85/**
86 * \brief Serialize a secured message
87 *
88 * @param ar output archive
89 * @param msg message to be serialized
90 */
91void serialize(OutputArchive& ar, const SecuredMessage& msg);
92
93/**
94 * \brief Deserialize a secured message
95 *
96 * \param ar input archive
97 * \param msg destination message object
98 * \return size of deserialized message
99 */
100size_t deserialize(InputArchive& ar, SecuredMessage& msg);
101
102ByteBuffer get_payload(const asn1::Opaque*);
103ByteBuffer get_payload(const asn1::SignedData*);
104void set_payload(asn1::Opaque* unsecured, const ByteBuffer& buffer);
105ByteBuffer convert_to_payload(vanetza::ChunkPacket packet);
106
107boost::optional<HashedId8> get_certificate_id(const SecuredMessage::SignerIdentifier&);
108
109/**
110 * Check if signer identifier contains a full certificate
111 * \param signer_identifier to check
112 * \param true if signer identifier contains a full certificate
113 */
114bool contains_certificate(const SecuredMessage::SignerIdentifier& signer_identifier);
115
116/**
117 * Fetch certificate from identifier if full certificate is included.
118 * \return certificate or nullptr
119 */
120const asn1::Certificate* get_certificate(const SecuredMessage::SignerIdentifier&);
121
122} // namespace v3
123} // namespace security
124} // namespace vanetza
125
126#endif /* SECURED_MESSAGE_HPP_DCBC74AC */
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(const std::string &name, Args... args) const
Definition factory.hpp:31
bool configure_default(const std::string &name)
Definition factory.hpp:70
bool add(const std::string &name, Function f)
Definition factory.hpp:60
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 ecdsa256::KeyPair generate_key_pair()=0
generate a private key and the corresponding public key
virtual boost::optional< Uncompressed > decompress_point(const EccPoint &ecc_point)=0
decompress a possibly compressed elliptic curve point
virtual Signature sign_digest(const PrivateKey &, const ByteBuffer &digest)=0
calculate signature for given digest and private key
virtual EcdsaSignature sign_data(const ecdsa256::PrivateKey &private_key, const ByteBuffer &data)=0
calculate signature for given data and private key
virtual bool verify_data(const ecdsa256::PublicKey &public_key, const ByteBuffer &data, const EcdsaSignature &sig)=0
try to verify data using public key and signature
virtual bool verify_digest(const PublicKey &public_key, const ByteBuffer &digest, const Signature &sig)=0
try to verify digest using public key and signature
virtual ByteBuffer calculate_hash(HashAlgorithm algo, const ByteBuffer &data)=0
calculate hash value of data
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,...
std::chrono::seconds to_seconds() const
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, section 4.2.10
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)
CircularRegion specified in TS 103 097 v1.2.1, section 4.2.22.
Definition region.hpp:74
EciesEncryptedKey specified in TS 103 097 v1.2.1, section 5.9.
EncryptionKey specified in TS 103 097 v1.2.1, section 6.4.
IdentifiedRegion specified in TS 103 097 v1.2.1, section 4.2.25.
Definition region.hpp:110
ItsAidSsp specified in TS 103 097 v1.2.1, section 6.9.
Specified in TS 103 097 v1.2.1, section 4.2.20.
Definition region.hpp:65
OpaqueKey specified in TS 103 097 v1.2.1, section 5.8.
Payload specified in TS 103 097 v1.2.1, section 5.2.
Definition payload.hpp:28
RecipientInfo specified in TS 103 097 v1.2.1, section 5.8.
RectangularRegion specified in TS 103 097 v1.2.1, section 4.2.23.
Definition region.hpp:90
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)
SubjectAssurance specified in TS 103 097 v1.2.1 in section 6.6 and 7.4.1.
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.
TwoDLocation specified in TS 103 097 v1.2.1, section 4.2.18.
Definition region.hpp:47
VerificationKey specified in TS 103 097 v1.2.1, section 6.4.
ecdsa_nistp256_with_sha256 specified in TS 103 097 v1.2.1, section 4.2.4
ecies_nistp256 specified in TS 103 097 v1.2.1, section 4.2.4
resolve type for matching HeaderFieldType
resolve type for matching TrailerFieldType