Vanetza
Loading...
Searching...
No Matches
openssl_wrapper.cpp
1#include <vanetza/security/openssl_wrapper.hpp>
2#include <vanetza/security/public_key.hpp>
3#include <vanetza/security/signature.hpp>
4#include <cassert>
5#include <openssl/bio.h>
6#include <openssl/bn.h>
7#include <openssl/err.h>
8#include <openssl/evp.h>
9
10namespace vanetza
11{
12namespace security
13{
14namespace openssl
15{
16
17void check(bool valid)
18{
19 if (!valid) {
20 throw Exception();
21 }
22}
23
24static const char* reason_error_string(Exception::code_type err)
25{
26 const char* msg = ERR_reason_error_string(err);
27 return msg ? msg : "unknown reason";
28}
29
30static const char* library_error_string(Exception::code_type err)
31{
32 const char* msg = ERR_lib_error_string(err);
33 return msg ? msg : "unknown library";
34}
35
36Exception::Exception() : Exception(ERR_get_error())
37{
38}
39
40Exception::Exception(code_type err) :
41 Exception(err, reason_error_string(err))
42{
43}
44
45Exception::Exception(const std::string& msg) :
46 std::runtime_error(msg),
47 m_error(ERR_get_error())
48{
49}
50
51Exception::Exception(code_type err, const std::string& msg) :
52 std::runtime_error(msg),
53 m_error(err)
54{
55}
56
57const char* Exception::reason() const
58{
59 return reason_error_string(m_error);
60}
61
62const char* Exception::library() const
63{
64 return library_error_string(m_error);
65}
66
67BigNumber::BigNumber() : bignum(BN_new())
68{
69 check(bignum != nullptr);
70}
71
72BigNumber::BigNumber(const uint8_t* arr, std::size_t len) : bignum(BN_bin2bn(arr, len, nullptr))
73{
74 check(bignum != nullptr);
75}
76
77BigNumber::~BigNumber()
78{
79 BN_clear_free(bignum);
80}
81
82BigNumberContext::BigNumberContext() : ctx(BN_CTX_new())
83{
84 check(ctx != nullptr);
85 BN_CTX_start(ctx);
86}
87
88BigNumberContext::~BigNumberContext()
89{
90 BN_CTX_end(ctx);
91 BN_CTX_free(ctx);
92}
93
94Point::Point(const EC_GROUP* group) : point(EC_POINT_new(group))
95{
96 check(point != nullptr);
97}
98
99Point::Point(Point&& other) : point(nullptr)
100{
101 std::swap(point, other.point);
102}
103
104Point& Point::operator=(Point&& other)
105{
106 std::swap(point, other.point);
107 return *this;
108}
109
110Point::~Point()
111{
112 EC_POINT_free(point);
113}
114
115Group::Group(int nid) : group(EC_GROUP_new_by_curve_name(nid))
116{
117 check(group != nullptr);
118}
119
120Group::~Group()
121{
122 EC_GROUP_clear_free(group);
123}
124
125Signature::Signature(ECDSA_SIG* sig) : signature(sig)
126{
127 check(signature);
128}
129
130Signature::Signature(const EcdsaSignature& ecdsa) :
131 Signature(convert_for_signing(ecdsa.R), ecdsa.s)
132{
133}
134
135Signature::Signature(const security::Signature& sig) :
136 Signature(sig.r, sig.s)
137{
138}
139
140Signature::Signature(const ByteBuffer& r, const ByteBuffer& s) :
141 signature(ECDSA_SIG_new())
142{
143 check(signature);
144 // ownership of big numbers is transfered by calling ECDSA_SIG_set0!
145 BIGNUM* bn_r = BN_bin2bn(r.data(), r.size(), nullptr);
146 BIGNUM* bn_s = BN_bin2bn(s.data(), s.size(), nullptr);
147 if (!ECDSA_SIG_set0(signature, bn_r, bn_s)) {
148 BN_clear_free(bn_r);
149 BN_clear_free(bn_s);
150 throw Exception();
151 }
152}
153
154Signature::Signature(Signature&& other) : signature(nullptr)
155{
156 std::swap(signature, other.signature);
157}
158
159Signature& Signature::operator=(Signature&& other)
160{
161 std::swap(signature, other.signature);
162 return *this;
163}
164
165Signature::~Signature()
166{
167 ECDSA_SIG_free(signature);
168}
169
170Key::Key() : eckey(EC_KEY_new())
171{
172 check(eckey);
173}
174
175Key::Key(int nid) : eckey(EC_KEY_new_by_curve_name(nid))
176{
177 check(eckey);
178}
179
180Key::Key(EC_KEY* key) : eckey(key)
181{
182}
183
184Key::Key(Key&& other) : eckey(nullptr)
185{
186 std::swap(eckey, other.eckey);
187}
188
189Key& Key::operator=(Key&& other)
190{
191 std::swap(eckey, other.eckey);
192 return *this;
193}
194
195Key::~Key()
196{
197 EC_KEY_free(eckey);
198}
199
200Bio::Bio(BIO* bio) : bio(bio)
201{
202}
203
204Bio::Bio(Bio&& other) : bio(nullptr)
205{
206 std::swap(bio, other.bio);
207}
208
209Bio& Bio::operator=(Bio&& other)
210{
211 std::swap(bio, other.bio);
212 return *this;
213}
214
215Bio::~Bio()
216{
217 BIO_free(bio);
218}
219
220EvpKey::EvpKey(EVP_PKEY* pkey) : pkey(pkey)
221{
222}
223
224EvpKey::EvpKey(EvpKey&& other) : pkey(nullptr)
225{
226 std::swap(pkey, other.pkey);
227}
228
229EvpKey& EvpKey::operator=(EvpKey&& other)
230{
231 std::swap(pkey, other.pkey);
232 return *this;
233}
234
235EvpKey::~EvpKey()
236{
237 EVP_PKEY_free(pkey);
238}
239
240} // namespace openssl
241} // namespace security
242} // namespace vanetza
EcdsaSignature specified in TS 103 097 v1.2.1, section 4.2.9.
Definition signature.hpp:17