Vanetza
Loading...
Searching...
No Matches
certificate.hpp
1#pragma once
2#include <vanetza/asn1/asn1c_wrapper.hpp>
3#include <vanetza/asn1/security/EtsiTs103097Certificate.h>
4#include <vanetza/common/clock.hpp>
5#include <vanetza/common/its_aid.hpp>
6#include <vanetza/common/position_fix.hpp>
7#include <vanetza/net/packet_variant.hpp>
8#include <vanetza/security/hashed_id.hpp>
9#include <vanetza/security/key_type.hpp>
10#include <vanetza/security/public_key.hpp>
11#include <vanetza/security/signature.hpp>
12#include <vanetza/security/v3/asn1_types.hpp>
13#include <vanetza/security/v3/location_checker.hpp>
14#include <vanetza/security/v3/validity_restriction.hpp>
15#include <boost/optional/optional_fwd.hpp>
16#include <list>
17
18namespace vanetza
19{
20namespace security
21{
22namespace v3
23{
24
25// forward declaration
26class Certificate;
27
28/**
29 * Read-only view on a certificate
30 *
31 * In contrast to Certificate, a view does not own the certificate data.
32 * A view can be created with low overhead as no heavy copying is required.
33 */
34class CertificateView
35{
36public:
37 explicit CertificateView(const asn1::EtsiTs103097Certificate* cert);
38
39 /**
40 * Calculate digest of certificate
41 * \return digest if possible
42 */
43 boost::optional<HashedId8> calculate_digest() const;
44
45 /**
46 * Get start and end validity
47 * \return certificate start and end validity
48 */
50
51 /**
52 * Get verification key type
53 * \return verification key type if possible; otherwise unspecified
54 */
55 KeyType get_verification_key_type() const;
56
57 /**
58 * Get issuer digest (if any)
59 * \return issuer digest
60 */
61 boost::optional<HashedId8> issuer_digest() const;
62
63 /**
64 * Check if certificate is self-signed
65 * \return true if certificate is self-signed
66 */
67 bool issuer_is_self() const;
68
69 /**
70 * Check if certificate is a Certification Authority certificate
71 * \return true if certificate is a CA certificate
72 */
73 bool is_ca_certificate() const;
74
75 /**
76 * Check if certificate is an Authorization Ticket certificate
77 * \return true if certificate is an AT certificate
78 */
79 bool is_at_certificate() const;
80
81 /**
82 * Check if certificate has an region restriction
83 * \return true if certificate is only valid within a specific region
84 */
85 bool has_region_restriction() const;
86
87 /**
88 * Check if certificate is valid at given location
89 *
90 * \param location location to be checked
91 * \return true if certificate is valid at location
92 */
93 bool valid_at_location(const PositionFix& location, const LocationChecker* lc) const;
94
95 /**
96 * Check if certificate is valid at given time point
97 *
98 * \param time_point time point to be checked
99 * \return true if certificate is valid at time point
100 */
101 bool valid_at_timepoint(const Clock::time_point& time_point) const;
102
103 /**
104 * Check if certificate is valid for given application
105 *
106 * \param aid application to be checked
107 * \return true if certificate is valid for application
108 */
109 bool valid_for_application(ItsAid aid) const;
110
111 /**
112 * Check if certificate has a canonical format
113 * \return true if certificate is in canonical format
114 */
115 bool is_canonical() const;
116
117 /**
118 * Convert certificate into its canonical format if possible.
119 * \return canonical certificate (or none if conversion failed)
120 */
121 boost::optional<Certificate> canonicalize() const;
122
123 /**
124 * Encode certificate.
125 * \return encoded certificate
126 */
127 ByteBuffer encode() const;
128
129protected:
130 const asn1::EtsiTs103097Certificate* m_cert = nullptr;
131};
132
133struct Certificate : public asn1::asn1c_oer_wrapper<asn1::EtsiTs103097Certificate>, public CertificateView
134{
136
137 Certificate();
138 explicit Certificate(const asn1::EtsiTs103097Certificate&);
139
140 Certificate(const Certificate&);
141 Certificate& operator=(const Certificate&);
142
143 Certificate(Certificate&&);
144 Certificate& operator=(Certificate&&);
145
146 // resolve ambiguity
147 ByteBuffer encode() const;
148
149 void add_permission(ItsAid aid, const ByteBuffer& ssp);
150
151 void add_cert_permission(asn1::PsidGroupPermissions* group_permission);
152
153 void set_signature(const SomeEcdsaSignature& signature);
154};
155
156/**
157 * Calculate digest of v3 certificate
158 * \param cert certificate
159 * \return digest if possible
160 */
161boost::optional<HashedId8> calculate_digest(const asn1::EtsiTs103097Certificate& cert);
162
163/**
164 * Check if certificate is in canonical format suitable for digest calculation.
165 * \param cert certificate
166 * \return true if certificate is in canonical format
167 */
168bool is_canonical(const asn1::EtsiTs103097Certificate& cert);
169
170/**
171 * Convert certificate into its canonical format if possible.
172 * \param cert certificate
173 * \return canonical certificate (or none if conversion failed)
174 */
175boost::optional<Certificate> canonicalize(const asn1::EtsiTs103097Certificate& cert);
176
177/**
178 * Check if certificate is valid at given time point
179 *
180 * \param cert certificate to be checked
181 * \param time_point time point to be checked
182 * \return true if certificate is valid at time point
183 */
184bool valid_at_timepoint(const asn1::EtsiTs103097Certificate& cert, const Clock::time_point& time_point);
185
186/**
187 * Check if certificate is valid for given application
188 *
189 * \param cert certificate to be checked
190 * \param aid application to be checked
191 * \return true if certificate is valid for application
192 */
193bool valid_for_application(const asn1::EtsiTs103097Certificate& cert, ItsAid aid);
194
195/**
196 * Extract the public key out of a certificate
197 * \param cert certificate
198 * \return public key if possible
199 */
200boost::optional<PublicKey> get_public_key(const asn1::EtsiTs103097Certificate& cert);
201
202/**
203 * Get verification key type
204 * \param cert certificate
205 * \return verification key type (maybe unspecified)
206 */
207KeyType get_verification_key_type(const asn1::EtsiTs103097Certificate& cert);
208
209/**
210 * Extract the public key for encrypting out of a certificate
211 * \param cert certificate
212 * \return encryption key if possible
213 */
214boost::optional<PublicKey> get_public_encryption_key(const asn1::EtsiTs103097Certificate& cert);
215
216/**
217 * Extract the signature out of a certificate
218 * \param cert certificate
219 * \return signature if possible
220 */
221boost::optional<Signature> get_signature(const asn1::EtsiTs103097Certificate& cert);
222
223/**
224 * Get list of ITS AID permissions from certificate
225 * \param cert certificate
226 * \return list of ITS AIDs
227 */
228std::list<ItsAid> get_aids(const asn1::EtsiTs103097Certificate& cert);
229
230/**
231 * Get application permissions (SSP = service specific permissions)
232 * \param cert certificate containing application permissions
233 * \param aid look up permissions for this application identifier
234 * \return SSP bitmap or empty buffer
235 */
236ByteBuffer get_app_permissions(const asn1::EtsiTs103097Certificate& cert, ItsAid aid);
237
238void add_psid_group_permission(asn1::PsidGroupPermissions* group_permission, ItsAid aid, const ByteBuffer& ssp, const ByteBuffer& bitmask);
239
240void serialize(OutputArchive& ar, const Certificate& certificate);
241
242Certificate fake_certificate();
243
244} // namespace v3
245} // namespace security
246} // namespace vanetza
boost::optional< Certificate > canonicalize() const
bool valid_at_timepoint(const Clock::time_point &time_point) const
StartAndEndValidity get_start_and_end_validity() const
bool valid_for_application(ItsAid aid) const
boost::optional< HashedId8 > calculate_digest() const
bool valid_at_location(const PositionFix &location, const LocationChecker *lc) const
boost::optional< HashedId8 > issuer_digest() const