Vanetza
Loading...
Searching...
No Matches
certificate.hpp
1#ifndef CERTIFICATE_HPP_LWBWIAVL
2#define CERTIFICATE_HPP_LWBWIAVL
3
4#include <vanetza/common/byte_buffer.hpp>
5#include <vanetza/common/its_aid.hpp>
6#include <vanetza/security/backend.hpp>
7#include <vanetza/security/ecdsa256.hpp>
8#include <vanetza/security/v2/basic_elements.hpp>
9#include <vanetza/security/v2/ecc_point.hpp>
10#include <vanetza/security/v2/serialization.hpp>
11#include <vanetza/security/v2/signature.hpp>
12#include <vanetza/security/v2/signer_info.hpp>
13#include <vanetza/security/v2/subject_attribute.hpp>
14#include <vanetza/security/v2/subject_info.hpp>
15#include <vanetza/security/v2/validity_restriction.hpp>
16#include <boost/optional/optional.hpp>
17#include <boost/variant/get.hpp>
18
19namespace vanetza
20{
21namespace security
22{
23namespace v2
24{
25
26/// described in TS 103 097 v1.2.1 (2015-06), section 6.1
28{
29 SignerInfo signer_info;
30 SubjectInfo subject_info;
31 std::list<SubjectAttribute> subject_attributes;
32 std::list<ValidityRestriction> validity_restriction;
33 Signature signature;
34 // certificate version is two, for conformance with the present standard
35 uint8_t version() const { return 2; }
36
37 /**
38 * Get subject attribute of a certain type (if present)
39 * \param type of subject attribute
40 */
41 const SubjectAttribute* get_attribute(SubjectAttributeType type) const;
42
43 /**
44 * Get validity restriction of a certain type (if present)
45 * \param type of validity restriction
46 */
47 const ValidityRestriction* get_restriction(ValidityRestrictionType type) const;
48
49 /**
50 * Remove subject attribute of a certain type (if present)
51 * \param type of subject attribute
52 */
53 void remove_attribute(SubjectAttributeType type);
54
55 /**
56 * Remove validity restriction of a certain type (if present)
57 * \param type of validity restriction
58 */
59 void remove_restriction(ValidityRestrictionType type);
60
61 /**
62 * Add ITS-AID to certificate's subject attributes
63 * \param aid ITS-AID
64 */
65 void add_permission(ItsAid aid);
66
67 /**
68 * Add ITS-AID along with SSP to certificate's subject attributes
69 * \param aid ITS-AID
70 * \param ssp Service Specific Permissions
71 */
72 void add_permission(ItsAid aid, const ByteBuffer& ssp);
73
74 /**
75 * Get subject attribute by type
76 * \tparam T subject attribute type
77 * \return subject attribute, nullptr if not found
78 */
79 template<SubjectAttributeType T>
81 {
82 using type = subject_attribute_type<T>;
83 const SubjectAttribute* field = get_attribute(T);
84 return boost::get<type>(field);
85 }
86
87 /**
88 * Get validity restriction by type
89 * \tparam T validity restriction type
90 * \return validity restriction, nullptr if not found
91 */
92 template<ValidityRestrictionType T>
94 {
95 using type = validity_restriction_type<T>;
96 const ValidityRestriction* field = get_restriction(T);
97 return boost::get<type>(field);
98 }
99};
100
101/**
102 * \brief Calculates size of an certificate object
103 *
104 * \param cert
105 * \return number of octets needed to serialize the object
106 */
107size_t get_size(const Certificate&);
108
109/**
110 * \brief Serializes an object into a binary archive
111 *
112 * \param ar archive to serialize in
113 * \param cert to serialize
114 */
115void serialize(OutputArchive&, const Certificate&);
116
117/**
118 * \brief Deserializes an object from a binary archive
119 *
120 * \param ar archive with a serialized object at the beginning
121 * \param cert to deserialize
122 * \return size of the deserialized object
123 */
124size_t deserialize(InputArchive&, Certificate&);
125
126/**
127* \brief Serialize parts of a Certificate for signature calculation
128*
129* Uses version, signer_field, subject_info, subject_attributes (+ length),
130* validity_restriction (+ length).
131*
132* \param cert certificate to be converted
133* \return binary representation
134*/
135ByteBuffer convert_for_signing(const Certificate&);
136
137/**
138 * \brief Sort lists in the certificate to be in the correct order for serialization
139 *
140 * \param cert certificate to sort
141 */
142void sort(Certificate& certificate);
143
144/**
145 * \brief Extract public key from certificate
146 * \param cert Certificate
147 * \param backend Backend
148 * \return Uncompressed public key (if available)
149 */
150boost::optional<Uncompressed> get_uncompressed_public_key(const Certificate&, Backend& backend);
151
152/**
153 * \brief Extract public ECDSA256 key from certificate
154 * \param cert Certificate
155 * \param backend Backend
156 * \return public key (if available)
157 */
158boost::optional<ecdsa256::PublicKey> get_public_key(const Certificate&, Backend& backend);
159
160/**
161 * Calculate hash id of certificate
162 * \param cert Certificate
163 * \return hash
164 */
165HashedId8 calculate_hash(const Certificate&);
166
167} // namespace v2
168} // namespace security
169} // namespace vanetza
170
171#endif /* CERTIFICATE_HPP_LWBWIAVL */
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
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
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
static DecapConfirm from(VerifyConfirm &&verify_confirm, const SecuredMessageView &msg_view)
Input data for decapsulating a secured message.
Uncompressed specified in TS 103 097 v1.2.1 in section 4.2.5.
Definition ecc_point.hpp:36
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