199 lines
3.6 KiB
C++
199 lines
3.6 KiB
C++
#ifndef OPENSSL_WRAPPER_HPP_GUFJGICT
|
|||
|
|
#define OPENSSL_WRAPPER_HPP_GUFJGICT
|
||
|
|
|
||
|
|
#include <vanetza/common/byte_buffer.hpp>
|
||
|
|
#include <boost/core/noncopyable.hpp>
|
||
|
|
#include <openssl/ec.h>
|
||
|
|
#include <openssl/err.h>
|
||
|
|
#include <array>
|
||
|
|
#include <cstdint>
|
||
|
|
#include <stdexcept>
|
||
|
|
|
||
|
|
namespace vanetza
|
||
|
|
{
|
||
|
|
namespace security
|
||
|
|
{
|
||
|
|
|
||
|
|
// forward declaration
|
||
|
|
struct EcdsaSignature;
|
||
|
|
struct Signature;
|
||
|
|
|
||
|
|
namespace openssl
|
||
|
|
{
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Check valid condition
|
||
|
|
* \param valid If false stored OpenSSL error will be thrown
|
||
|
|
*/
|
||
|
|
void check(bool valid);
|
||
|
|
|
||
|
|
|
||
|
|
class Exception : public std::runtime_error
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
using code_type = decltype(ERR_get_error());
|
||
|
|
|
||
|
|
Exception();
|
||
|
|
explicit Exception(code_type err);
|
||
|
|
explicit Exception(const std::string& msg);
|
||
|
|
explicit Exception(code_type, const std::string& msg);
|
||
|
|
~Exception() override = default;
|
||
|
|
|
||
|
|
code_type error_code() const { return m_error; }
|
||
|
|
const char* reason() const;
|
||
|
|
const char* library() const;
|
||
|
|
|
||
|
|
private:
|
||
|
|
code_type m_error = 0;
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
class BigNumber : private boost::noncopyable
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
BigNumber();
|
||
|
|
~BigNumber();
|
||
|
|
|
||
|
|
BigNumber(const ByteBuffer& buf) :
|
||
|
|
BigNumber(buf.data(), buf.size())
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
template<std::size_t N>
|
||
|
|
BigNumber(const std::array<uint8_t, N>& bin) :
|
||
|
|
BigNumber(bin.data(), bin.size())
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
operator BIGNUM*() { return bignum; }
|
||
|
|
|
||
|
|
private:
|
||
|
|
BigNumber(const uint8_t*, std::size_t);
|
||
|
|
|
||
|
|
BIGNUM* bignum;
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
class BigNumberContext : private boost::noncopyable
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
BigNumberContext();
|
||
|
|
~BigNumberContext();
|
||
|
|
|
||
|
|
operator BN_CTX*() { return ctx; }
|
||
|
|
|
||
|
|
private:
|
||
|
|
BN_CTX* ctx;
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
class Group : private boost::noncopyable
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
explicit Group(int nid);
|
||
|
|
~Group();
|
||
|
|
|
||
|
|
operator EC_GROUP*() { return group; }
|
||
|
|
|
||
|
|
private:
|
||
|
|
EC_GROUP* group;
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
class Point : private boost::noncopyable
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
explicit Point(const EC_GROUP* group);
|
||
|
|
Point(Point&&);
|
||
|
|
Point& operator=(Point&&);
|
||
|
|
~Point();
|
||
|
|
|
||
|
|
operator EC_POINT*() { return point; }
|
||
|
|
|
||
|
|
private:
|
||
|
|
EC_POINT* point;
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
class Signature : private boost::noncopyable
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
explicit Signature(ECDSA_SIG* sig);
|
||
|
|
explicit Signature(const EcdsaSignature& sig);
|
||
|
|
explicit Signature(const security::Signature& sig);
|
||
|
|
Signature(Signature&&);
|
||
|
|
Signature& operator=(Signature&&);
|
||
|
|
~Signature();
|
||
|
|
|
||
|
|
operator const ECDSA_SIG*() { return signature; }
|
||
|
|
const ECDSA_SIG* operator->() const { return signature; }
|
||
|
|
ECDSA_SIG* operator->() { return signature; }
|
||
|
|
operator bool() { return signature != nullptr; }
|
||
|
|
|
||
|
|
private:
|
||
|
|
Signature(const ByteBuffer& r, const ByteBuffer& s);
|
||
|
|
|
||
|
|
ECDSA_SIG* signature;
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
class Key
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
Key();
|
||
|
|
explicit Key(int nid);
|
||
|
|
explicit Key(EC_KEY* key);
|
||
|
|
// non-copyable
|
||
|
|
Key(const Key&) = delete;
|
||
|
|
Key& operator=(const Key&) = delete;
|
||
|
|
// but movable
|
||
|
|
Key(Key&&);
|
||
|
|
Key& operator=(Key&&);
|
||
|
|
~Key();
|
||
|
|
|
||
|
|
operator EC_KEY*() { return eckey; }
|
||
|
|
operator bool() const { return eckey != nullptr; }
|
||
|
|
|
||
|
|
private:
|
||
|
|
EC_KEY* eckey;
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
class Bio : private boost::noncopyable
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
explicit Bio(BIO* bio);
|
||
|
|
Bio(Bio&&);
|
||
|
|
Bio& operator=(Bio&&);
|
||
|
|
~Bio();
|
||
|
|
|
||
|
|
operator BIO*() { return bio; }
|
||
|
|
operator bool() const { return bio != nullptr; }
|
||
|
|
|
||
|
|
private:
|
||
|
|
BIO* bio;
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
class EvpKey : private boost::noncopyable
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
explicit EvpKey(EVP_PKEY* pkey);
|
||
|
|
EvpKey(EvpKey&&);
|
||
|
|
EvpKey& operator=(EvpKey&&);
|
||
|
|
~EvpKey();
|
||
|
|
|
||
|
|
operator EVP_PKEY*() { return pkey; }
|
||
|
|
operator bool() const { return pkey != nullptr; }
|
||
|
|
|
||
|
|
private:
|
||
|
|
EVP_PKEY* pkey;
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace openssl
|
||
|
|
} // namespace security
|
||
|
|
} // namespace vanetza
|
||
|
|
|
||
|
|
#endif /* OPENSSL_WRAPPER_HPP_GUFJGICT */
|
||
|
|
|