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
6namespace vanetza
7{
8namespace security
9{
10namespace openssl
11{
12
13void check(bool valid)
14{
15 if (!valid) {
16 throw Exception();
17 }
18}
19
20Exception::Exception() : Exception(ERR_get_error())
21{
22}
23
24Exception::Exception(code_type err) :
25 std::runtime_error(ERR_reason_error_string(err))
26{
27}
28
29BigNumber::BigNumber() : bignum(BN_new())
30{
31 check(bignum != nullptr);
32}
33
34BigNumber::BigNumber(const uint8_t* arr, std::size_t len) : BigNumber()
35{
36 BN_bin2bn(arr, len, bignum);
37}
38
39BIGNUM* BigNumber::move()
40{
41 BIGNUM* ptr = nullptr;
42 std::swap(ptr, bignum);
43 return ptr;
44}
45
46BigNumber::~BigNumber()
47{
48 if (bignum) {
49 BN_clear_free(bignum);
50 }
51}
52
53BigNumberContext::BigNumberContext() : ctx(BN_CTX_new())
54{
55 check(ctx != nullptr);
56 BN_CTX_start(ctx);
57}
58
59BigNumberContext::~BigNumberContext()
60{
61 BN_CTX_end(ctx);
62 BN_CTX_free(ctx);
63}
64
65Point::Point(const EC_GROUP* group) : point(EC_POINT_new(group))
66{
67 check(point != nullptr);
68}
69
70Point::Point(Point&& other) : point(nullptr)
71{
72 std::swap(point, other.point);
73}
74
75Point& Point::operator=(Point&& other)
76{
77 std::swap(point, other.point);
78 return *this;
79}
80
81Point::~Point()
82{
83 EC_POINT_free(point);
84}
85
86Group::Group(int nid) : group(EC_GROUP_new_by_curve_name(nid))
87{
88 check(group != nullptr);
89}
90
91Group::~Group()
92{
93 EC_GROUP_clear_free(group);
94}
95
96Signature::Signature(ECDSA_SIG* sig) : signature(sig)
97{
98 check(signature);
99}
100
101Signature::Signature(const EcdsaSignature& ecdsa) :
102 Signature(convert_for_signing(ecdsa.R), ecdsa.s)
103{
104}
105
106Signature::Signature(const security::Signature& sig) :
107 Signature(sig.r, sig.s)
108{
109}
110
111Signature::Signature(const ByteBuffer& r, const ByteBuffer& s) :
112 signature(ECDSA_SIG_new())
113{
114 check(signature);
115#if OPENSSL_API_COMPAT < 0x10100000L
116 BN_bin2bn(r.data(), r.size(), signature->r);
117 BN_bin2bn(s.data(), s.size(), signature->s);
118#else
119 BigNumber bn_r { r };
120 BigNumber bn_s { s };
121 // ownership of big numbers is transfered by calling ECDSA_SIG_set0!
122 ECDSA_SIG_set0(signature, bn_r.move(), bn_s.move());
123#endif
124}
125
126Signature::Signature(Signature&& other) : signature(nullptr)
127{
128 std::swap(signature, other.signature);
129}
130
131Signature& Signature::operator=(Signature&& other)
132{
133 std::swap(signature, other.signature);
134 return *this;
135}
136
137Signature::~Signature()
138{
139 ECDSA_SIG_free(signature);
140}
141
142Key::Key() : eckey(EC_KEY_new())
143{
144 check(eckey);
145}
146
147Key::Key(int nid) : eckey(EC_KEY_new_by_curve_name(nid))
148{
149 check(eckey);
150}
151
152Key::Key(Key&& other) : eckey(nullptr)
153{
154 std::swap(eckey, other.eckey);
155}
156
157Key& Key::operator=(Key&& other)
158{
159 std::swap(eckey, other.eckey);
160 return *this;
161}
162
163Key::~Key()
164{
165 EC_KEY_free(eckey);
166}
167
168} // namespace openssl
169} // namespace security
170} // namespace vanetza
STL namespace.