Vanetza
 
Loading...
Searching...
No Matches
signer_info.cpp
1#include <vanetza/security/exception.hpp>
2#include <vanetza/security/v2/certificate.hpp>
3#include <vanetza/security/v2/length_coding.hpp>
4#include <vanetza/security/v2/signer_info.hpp>
5
6namespace vanetza
7{
8namespace security
9{
10namespace v2
11{
12
13SignerInfoType get_type(const SignerInfo& info)
14{
15 struct SignerInfo_visitor : public boost::static_visitor<SignerInfoType>
16 {
17 SignerInfoType operator()(const std::nullptr_t)
18 {
19 return SignerInfoType::Self;
20 }
21 SignerInfoType operator()(const HashedId8& id)
22 {
23 return SignerInfoType::Certificate_Digest_With_SHA256;
24 }
25 SignerInfoType operator()(const Certificate& cert)
26 {
27 return SignerInfoType::Certificate;
28 }
29 SignerInfoType operator()(const std::list<Certificate>& list)
30 {
31 return SignerInfoType::Certificate_Chain;
32 }
33 SignerInfoType operator()(const CertificateDigestWithOtherAlgorithm& cert)
34 {
35 return SignerInfoType::Certificate_Digest_With_Other_Algorithm;
36 }
37 };
38
39 SignerInfo_visitor visit;
40 return boost::apply_visitor(visit, info);
41}
42
43size_t get_size(const CertificateDigestWithOtherAlgorithm& cert)
44{
45 size_t size = cert.digest.size();
46 size += sizeof(cert.algorithm);
47 return size;
48}
49
50size_t get_size(const SignerInfo& info)
51{
52 size_t size = sizeof(SignerInfoType);
53 struct SignerInfo_visitor : public boost::static_visitor<size_t>
54 {
55 size_t operator()(const std::nullptr_t&)
56 {
57 return 0;
58 }
59 size_t operator()(const HashedId8& id)
60 {
61 return id.size();
62 }
63 size_t operator()(const Certificate& cert)
64 {
65 return get_size(cert);
66 }
67 size_t operator()(const std::list<Certificate>& list)
68 {
69 size_t size = get_size(list);
70 size += length_coding_size(size);
71 return size;
72 }
73 size_t operator()(const CertificateDigestWithOtherAlgorithm& cert)
74 {
75 return get_size(cert);
76 }
77 };
78
79 SignerInfo_visitor visit;
80 size += boost::apply_visitor(visit, info);
81 return size;
82}
83
84void serialize(OutputArchive& ar, const CertificateDigestWithOtherAlgorithm& cert)
85{
86 serialize(ar, cert.algorithm);
87 for (auto& byte : cert.digest) {
88 ar << byte;
89 }
90}
91
92void serialize(OutputArchive& ar, const SignerInfo& info)
93{
94 struct SignerInfo_visitor : public boost::static_visitor<>
95 {
96 SignerInfo_visitor(OutputArchive& ar) :
97 m_archive(ar)
98 {
99 }
100
101 void operator()(const std::nullptr_t)
102 {
103 // intentionally do nothing
104 }
105
106 void operator()(const HashedId8& id)
107 {
108 for (auto& byte : id) {
109 m_archive << byte;
110 }
111 }
112
113 void operator()(const Certificate& cert)
114 {
115 serialize(m_archive, cert);
116 }
117
118 void operator()(const std::list<Certificate>& list)
119 {
120 serialize(m_archive, list);
121 }
122
123 void operator()(const CertificateDigestWithOtherAlgorithm& cert)
124 {
125 serialize(m_archive, cert);
126 }
127
128 OutputArchive& m_archive;
129 };
130 SignerInfoType type = get_type(info);
131 serialize(ar, type);
132 SignerInfo_visitor visit(ar);
133 boost::apply_visitor(visit, info);
134}
135
136size_t deserialize(InputArchive& ar, CertificateDigestWithOtherAlgorithm& cert)
137{
138 deserialize(ar, cert.algorithm);
139 for (size_t c = 0; c < 8; c++) {
140 ar >> cert.digest[c];
141 }
142 size_t size = cert.digest.size();
143 size += sizeof(cert.algorithm);
144 return size;
145}
146
147size_t deserialize(InputArchive& ar, SignerInfo& info)
148{
149 SignerInfoType type;
150 size_t size = 0;
151 deserialize(ar, type);
152 size += sizeof(SignerInfoType);
153 switch (type) {
154 case SignerInfoType::Certificate: {
155 Certificate cert;
156 size += deserialize(ar, cert);
157 info = cert;
158 break;
159 }
160 case SignerInfoType::Certificate_Chain: {
161 std::list<Certificate> list;
162 size += deserialize(ar, list);
163 size += length_coding_size(size);
164 info = list;
165 break;
166 }
167 case SignerInfoType::Certificate_Digest_With_SHA256: {
168 HashedId8 cert;
169 for (size_t c = 0; c < 8; c++) {
170 ar >> cert[c];
171 }
172 info = cert;
173 size += sizeof(cert);
174 break;
175 }
176 case SignerInfoType::Certificate_Digest_With_Other_Algorithm: {
177 CertificateDigestWithOtherAlgorithm cert;
178 size += deserialize(ar, cert);
179 info = cert;
180 break;
181 }
182 case SignerInfoType::Self:
183 info = nullptr;
184 break;
185 default:
186 throw deserialization_error("Unknown SignerInfoType");
187 break;
188 }
189 return size;
190}
191
192} // ns v2
193} // ns security
194} // ns vanetza