Vanetza
Loading...
Searching...
No Matches
recipient_info.cpp
1#include <vanetza/security/v2/recipient_info.hpp>
2#include <vanetza/security/v2/length_coding.hpp>
3#include <boost/variant/static_visitor.hpp>
4#include <boost/variant/apply_visitor.hpp>
5
6namespace vanetza
7{
8namespace security
9{
10namespace v2
11{
12
13PublicKeyAlgorithm get_type(const Key& key)
14{
15 struct key_visitor : public boost::static_visitor<PublicKeyAlgorithm>
16 {
17 PublicKeyAlgorithm operator()(const EciesEncryptedKey& key)
18 {
19 return PublicKeyAlgorithm::ECIES_NISTP256;
20 }
21
22 PublicKeyAlgorithm operator()(const OpaqueKey& key)
23 {
24 // TODO: could be anything except ECIES_NISTP256
25 return PublicKeyAlgorithm::ECDSA_NISTP256_With_SHA256;
26 }
27 };
28
29 key_visitor visitor;
30 return boost::apply_visitor(visitor, key);
31}
32
33PublicKeyAlgorithm RecipientInfo::pk_encryption() const
34{
35 return get_type(enc_key);
36}
37
38size_t get_size(const RecipientInfo& info)
39{
40 size_t size = info.cert_id.size();
41 size += sizeof(PublicKeyAlgorithm);
42 struct RecipientInfoKey_visitor : public boost::static_visitor<size_t>
43 {
44 size_t operator()(const EciesEncryptedKey& key)
45 {
46 return key.c.size() + key.t.size() + get_size(key.v);
47 }
48
49 size_t operator()(const OpaqueKey& key)
50 {
51 return length_coding_size(key.data.size()) + key.data.size();
52 }
53 };
54
55 RecipientInfoKey_visitor visit;
56 size += boost::apply_visitor(visit, info.enc_key);
57 return size;
58}
59
60void serialize(OutputArchive& ar, const RecipientInfo& info, SymmetricAlgorithm sym_algo)
61{
62 struct key_visitor : public boost::static_visitor<>
63 {
64 key_visitor(OutputArchive& ar, SymmetricAlgorithm sym_algo, PublicKeyAlgorithm pk_algo) :
65 m_archive(ar), m_sym_algo(sym_algo), m_pk_algo(pk_algo)
66 {
67 }
68 void operator()(const EciesEncryptedKey& key)
69 {
70 assert(key.c.size() == field_size(m_sym_algo));
71 serialize(m_archive, key.v, m_pk_algo);
72 for (auto& byte : key.c) {
73 m_archive << byte;
74 }
75 for (auto& byte : key.t) {
76 m_archive << byte;
77 }
78 }
79
80 void operator()(const OpaqueKey& key)
81 {
82 serialize_length(m_archive, key.data.size());
83 for (auto byte : key.data) {
84 m_archive << byte;
85 }
86 }
87
88 OutputArchive& m_archive;
89 SymmetricAlgorithm m_sym_algo;
90 PublicKeyAlgorithm m_pk_algo;
91 };
92
93 for (auto& byte : info.cert_id) {
94 ar << byte;
95 }
96 const PublicKeyAlgorithm pk_algo = info.pk_encryption();
97 serialize(ar, pk_algo);
98 key_visitor visitor(ar, sym_algo, pk_algo);
99 boost::apply_visitor(visitor, info.enc_key);
100}
101
102EciesEncryptedKey deserialize_ecies(InputArchive& ar, const SymmetricAlgorithm& symAlgo)
103{
104 EciesEncryptedKey ecies;
105 deserialize(ar, ecies.v, PublicKeyAlgorithm::ECIES_NISTP256);
106 const size_t fieldSize = field_size(symAlgo);
107 for (size_t c = 0; c < fieldSize; ++c) {
108 uint8_t tmp;
109 ar >> tmp;
110 ecies.c.push_back(tmp);
111 }
112 for (size_t c = 0; c < ecies.t.size(); ++c) {
113 uint8_t tmp;
114 ar >> tmp;
115 ecies.t[c] = tmp;
116 }
117 return ecies;
118}
119
120OpaqueKey deserialize_opaque(InputArchive& ar)
121{
122 static const std::uintmax_t length_limit = 512;
123 const std::uintmax_t length = deserialize_length(ar);
124
125 if (length <= length_limit) {
126 ByteBuffer opaque(length);
127 for (std::uintmax_t i = 0; i < length; ++i) {
128 ar >> opaque[i];
129 }
130 return OpaqueKey { std::move(opaque) };
131 } else {
132 return OpaqueKey {};
133 }
134}
135
136size_t deserialize(InputArchive& ar, RecipientInfo& info, const SymmetricAlgorithm& symAlgo)
137{
138 for (size_t c = 0; c < info.cert_id.size(); ++c) {
139 ar >> info.cert_id[c];
140 }
141 PublicKeyAlgorithm algo;
142 deserialize(ar, algo);
143 switch (algo) {
144 case PublicKeyAlgorithm::ECIES_NISTP256:
145 info.enc_key = deserialize_ecies(ar, symAlgo);
146 break;
147 default:
148 info.enc_key = deserialize_opaque(ar);
149 break;
150 }
151 return get_size(info);
152}
153
154} // namespace v2
155} // namespace security
156} // namespace vanetza
EciesEncryptedKey specified in TS 103 097 v1.2.1, section 5.9.
OpaqueKey specified in TS 103 097 v1.2.1, section 5.8.
RecipientInfo specified in TS 103 097 v1.2.1, section 5.8.