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/bn.h>
7#include <openssl/ecdsa.h>
8#include <openssl/err.h>
9#include <array>
10#include <cstdint>
11#include <stdexcept>
12
13namespace vanetza
14{
15namespace security
16{
17
18// forward declaration
19struct EcdsaSignature;
20struct Signature;
21
22namespace openssl
23{
24
25/**
26 * Check valid condition
27 * \param valid If false stored OpenSSL error will be thrown
28 */
29void check(bool valid);
30
31
32class Exception : public std::runtime_error
33{
34public:
35 using code_type = decltype(ERR_get_error());
36
37 Exception();
38 explicit Exception(code_type err);
39};
40
41
42class BigNumber : private boost::noncopyable
43{
44public:
45 BigNumber();
46 ~BigNumber();
47
48 BigNumber(const ByteBuffer& buf) :
49 BigNumber(buf.data(), buf.size())
50 {
51 }
52
53 template<std::size_t N>
54 BigNumber(const std::array<uint8_t, N>& bin) :
55 BigNumber(bin.data(), bin.size())
56 {
57 }
58
59 operator BIGNUM*() { return bignum; }
60 BIGNUM* move();
61
62private:
63 BigNumber(const uint8_t*, std::size_t);
64
65 BIGNUM* bignum;
66};
67
68
69class BigNumberContext : private boost::noncopyable
70{
71public:
74
75 operator BN_CTX*() { return ctx; }
76
77private:
78 BN_CTX* ctx;
79};
80
81
82class Group : private boost::noncopyable
83{
84public:
85 explicit Group(int nid);
86 ~Group();
87
88 operator EC_GROUP*() { return group; }
89
90private:
91 EC_GROUP* group;
92};
93
94
95class Point : private boost::noncopyable
96{
97public:
98 explicit Point(const EC_GROUP* group);
99 Point(Point&&);
100 Point& operator=(Point&&);
101 ~Point();
102
103 operator EC_POINT*() { return point; }
104
105private:
106 EC_POINT* point;
107};
108
109
110class Signature : private boost::noncopyable
111{
112public:
113 explicit Signature(ECDSA_SIG* sig);
114 explicit Signature(const EcdsaSignature& sig);
115 explicit Signature(const security::Signature& sig);
117 Signature& operator=(Signature&&);
118 ~Signature();
119
120 operator const ECDSA_SIG*() { return signature; }
121 const ECDSA_SIG* operator->() const { return signature; }
122 ECDSA_SIG* operator->() { return signature; }
123 operator bool() { return signature != nullptr; }
124
125private:
126 Signature(const ByteBuffer& r, const ByteBuffer& s);
127
128 ECDSA_SIG* signature;
129};
130
131
132class Key
133{
134public:
135 Key();
136 explicit Key(int nid);
137 // non-copyable
138 Key(const Key&) = delete;
139 Key& operator=(const Key&) = delete;
140 // but movable
141 Key(Key&&);
142 Key& operator=(Key&&);
143 ~Key();
144
145 operator EC_KEY*() { return eckey; }
146
147private:
148 EC_KEY* eckey;
149};
150
151} // namespace openssl
152} // namespace security
153} // namespace vanetza
154
155#endif /* OPENSSL_WRAPPER_HPP_GUFJGICT */
156
EcdsaSignature specified in TS 103 097 v1.2.1, section 4.2.9.
Definition: signature.hpp:17