Vanetza
Loading...
Searching...
No Matches
openssl_wrapper.hpp
1#ifndef OPENSSL_WRAPPER_HPP_GUFJGICT
2#define OPENSSL_WRAPPER_HPP_GUFJGICT
3
4#include <vanetza/common/byte_buffer.hpp>
5#include <boost/core/noncopyable.hpp>
6#include <openssl/ec.h>
7#include <openssl/err.h>
8#include <array>
9#include <cstdint>
10#include <stdexcept>
11
12namespace vanetza
13{
14namespace security
15{
16
17// forward declaration
18struct EcdsaSignature;
19struct Signature;
20
21namespace openssl
22{
23
24/**
25 * Check valid condition
26 * \param valid If false stored OpenSSL error will be thrown
27 */
28void check(bool valid);
29
30
31class Exception : public std::runtime_error
32{
33public:
34 using code_type = decltype(ERR_get_error());
35
36 Exception();
37 explicit Exception(code_type err);
38 explicit Exception(const std::string& msg);
39 explicit Exception(code_type, const std::string& msg);
40 ~Exception() override = default;
41
42 code_type error_code() const { return m_error; }
43 const char* reason() const;
44 const char* library() const;
45
46private:
47 code_type m_error = 0;
48};
49
50
51class BigNumber : private boost::noncopyable
52{
53public:
54 BigNumber();
55 ~BigNumber();
56
57 BigNumber(const ByteBuffer& buf) :
58 BigNumber(buf.data(), buf.size())
59 {
60 }
61
62 template<std::size_t N>
63 BigNumber(const std::array<uint8_t, N>& bin) :
64 BigNumber(bin.data(), bin.size())
65 {
66 }
67
68 operator BIGNUM*() { return bignum; }
69
70private:
71 BigNumber(const uint8_t*, std::size_t);
72
73 BIGNUM* bignum;
74};
75
76
77class BigNumberContext : private boost::noncopyable
78{
79public:
80 BigNumberContext();
81 ~BigNumberContext();
82
83 operator BN_CTX*() { return ctx; }
84
85private:
86 BN_CTX* ctx;
87};
88
89
90class Group : private boost::noncopyable
91{
92public:
93 explicit Group(int nid);
94 ~Group();
95
96 operator EC_GROUP*() { return group; }
97
98private:
99 EC_GROUP* group;
100};
101
102
103class Point : private boost::noncopyable
104{
105public:
106 explicit Point(const EC_GROUP* group);
107 Point(Point&&);
108 Point& operator=(Point&&);
109 ~Point();
110
111 operator EC_POINT*() { return point; }
112
113private:
114 EC_POINT* point;
115};
116
117
118class Signature : private boost::noncopyable
119{
120public:
121 explicit Signature(ECDSA_SIG* sig);
122 explicit Signature(const EcdsaSignature& sig);
123 explicit Signature(const security::Signature& sig);
124 Signature(Signature&&);
125 Signature& operator=(Signature&&);
126 ~Signature();
127
128 operator const ECDSA_SIG*() { return signature; }
129 const ECDSA_SIG* operator->() const { return signature; }
130 ECDSA_SIG* operator->() { return signature; }
131 operator bool() { return signature != nullptr; }
132
133private:
134 Signature(const ByteBuffer& r, const ByteBuffer& s);
135
136 ECDSA_SIG* signature;
137};
138
139
140class Key
141{
142public:
143 Key();
144 explicit Key(int nid);
145 explicit Key(EC_KEY* key);
146 // non-copyable
147 Key(const Key&) = delete;
148 Key& operator=(const Key&) = delete;
149 // but movable
150 Key(Key&&);
151 Key& operator=(Key&&);
152 ~Key();
153
154 operator EC_KEY*() { return eckey; }
155 operator bool() const { return eckey != nullptr; }
156
157private:
158 EC_KEY* eckey;
159};
160
161
162class Bio : private boost::noncopyable
163{
164public:
165 explicit Bio(BIO* bio);
166 Bio(Bio&&);
167 Bio& operator=(Bio&&);
168 ~Bio();
169
170 operator BIO*() { return bio; }
171 operator bool() const { return bio != nullptr; }
172
173private:
174 BIO* bio;
175};
176
177
178class EvpKey : private boost::noncopyable
179{
180public:
181 explicit EvpKey(EVP_PKEY* pkey);
182 EvpKey(EvpKey&&);
183 EvpKey& operator=(EvpKey&&);
184 ~EvpKey();
185
186 operator EVP_PKEY*() { return pkey; }
187 operator bool() const { return pkey != nullptr; }
188
189private:
190 EVP_PKEY* pkey;
191};
192
193} // namespace openssl
194} // namespace security
195} // namespace vanetza
196
197#endif /* OPENSSL_WRAPPER_HPP_GUFJGICT */
Backend implementation based on OpenSSL.
std::array< uint8_t, 32 > calculate_sha256_digest(const ByteBuffer &data) const
calculate SHA256 digest of data buffer
bool verify_data(const ecdsa256::PublicKey &public_key, const ByteBuffer &data, const EcdsaSignature &sig) override
bool verify_digest(const PublicKey &, const ByteBuffer &digest, const Signature &) override
std::array< uint8_t, 48 > calculate_sha384_digest(const ByteBuffer &data) const
calculate SHA384 digest of data buffer
openssl::Key internal_public_key(const ecdsa256::PublicKey &) const
convert to internal format of public key
openssl::Point internal_ec_point(const PublicKey &) const
convert to internal format of an EC point
ecdsa256::KeyPair generate_key_pair() override
Signature sign_digest(const PrivateKey &, const ByteBuffer &digest) override
EcdsaSignature sign_data(const ecdsa256::PrivateKey &private_key, const ByteBuffer &data_buffer) override
boost::optional< Uncompressed > decompress_point(const EccPoint &ecc_point) override
openssl::Key internal_private_key(const ecdsa256::PrivateKey &) const
convert to internal format of private key
ByteBuffer calculate_hash(HashAlgorithm, const ByteBuffer &) override
calculate hash value of data
Compressed_Lsb_Y_0 specified in TS 103 097 v1.2.1 in section 4.2.5.
Definition ecc_point.hpp:24
Compressed_Lsb_Y_1 specified in TS 103 097 v1.2.1 in section 4.2.5.
Definition ecc_point.hpp:30
EcdsaSignature specified in TS 103 097 v1.2.1, section 4.2.9.
Definition signature.hpp:17
Uncompressed specified in TS 103 097 v1.2.1 in section 4.2.5.
Definition ecc_point.hpp:36
X_Coordinate_Only specified in TS 103 097 v1.2.1 in section 4.2.5.
Definition ecc_point.hpp:18