2018-03-06 15:40:44 +08:00
|
|
|
/**
|
|
|
|
|
* SslContext.h
|
|
|
|
|
*
|
|
|
|
|
* Class to create and maintain a tcp ssl context
|
|
|
|
|
*
|
|
|
|
|
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
|
|
|
|
|
* @copyright 2018 Copernica BV
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Include guard
|
|
|
|
|
*/
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Begin of namespace
|
|
|
|
|
*/
|
|
|
|
|
namespace AMQP {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class definition
|
|
|
|
|
*/
|
|
|
|
|
class SslContext
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
/**
|
|
|
|
|
* The wrapped context
|
|
|
|
|
* @var SSL_CTX
|
|
|
|
|
*/
|
|
|
|
|
SSL_CTX *_ctx;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Constructor
|
|
|
|
|
* @param method
|
|
|
|
|
* @throws std::runtime_error
|
|
|
|
|
*/
|
2018-03-07 05:03:53 +08:00
|
|
|
SslContext(const SSL_METHOD *method) : _ctx(OpenSSL::SSL_CTX_new(method))
|
2018-03-06 15:40:44 +08:00
|
|
|
{
|
|
|
|
|
// report error
|
|
|
|
|
if (_ctx == nullptr) throw std::runtime_error("failed to construct ssl context");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-03-08 18:04:39 +08:00
|
|
|
* Copy constructor is delete because the object is refcounted,
|
|
|
|
|
* and we do not have a decent way to update the refcount in openssl 1.0
|
2018-03-06 15:40:44 +08:00
|
|
|
* @param that
|
|
|
|
|
*/
|
2018-03-08 18:04:39 +08:00
|
|
|
SslContext(SslContext &that) = delete;
|
2018-03-06 15:40:44 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destructor
|
|
|
|
|
*/
|
|
|
|
|
virtual ~SslContext()
|
|
|
|
|
{
|
|
|
|
|
// free resource (this updates the refcount -1, and may destruct it)
|
2018-03-07 05:03:53 +08:00
|
|
|
OpenSSL::SSL_CTX_free(_ctx);
|
2018-03-06 15:40:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cast to the actual context
|
|
|
|
|
* @return SSL_CTX *
|
|
|
|
|
*/
|
|
|
|
|
operator SSL_CTX * () { return _ctx; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* End of namespace
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|