fixed some compile issues

This commit is contained in:
Emiel Bruijntjes 2018-03-06 17:52:44 +01:00
parent 963d06c1e8
commit 25d5410b13
4 changed files with 220 additions and 134 deletions

View File

@ -29,6 +29,7 @@
*/ */
#include <cstddef> #include <cstddef>
#include <functional> #include <functional>
#include <dlfcn.h>
/** /**
* Namespace * Namespace

64
src/linux_tcp/library.h Normal file
View File

@ -0,0 +1,64 @@
/**
* Library.h
*
* The Library class is a wrapper around dlopen()
*
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
* @copyright 2018 Copernica BV
*/
/**
* Include guard
*/
#pragma once
/**
* Begin of namespace
*/
namespace AMQP {
/**
* Class definition
*/
class Library
{
private:
/**
* The library handle
* @var void *
*/
void *_handle;
public:
/**
* Constructor
*/
Library() : _handle(dlopen(nullptr, RTLD_NOW)) {}
/**
* No copying
* @param that
*/
Library(const Library &that) = delete;
/**
* Destructor
*/
virtual ~Library()
{
// close library
if (_handle) dlclose(_handle);
}
/**
* Cast to the handle
* @return void *
*/
operator void * () { return _handle; }
};
/**
* End of namespace
*/
}

View File

@ -1,19 +1,17 @@
/** /**
* OpenSSL.cpp * OpenSSL.cpp
* *
* Implementation file for the openssl.h header file * Implementation file for the openssl.h header file
* *
* @copyright 2018 Copernica BV * @copyright 2018 Copernica BV
*/ */
/** /**
* Dependencies * Dependencies
*/ */
// #include "includes.h" #include "openssl.h"
#include "amqpcpp/linux_tcp/openssl.h"
#include "function.h" #include "function.h"
#include <iostream> #include "library.h"
/** /**
* Begin of namespace * Begin of namespace
@ -22,203 +20,180 @@ namespace AMQP { namespace OpenSSL {
/** /**
* Get the library handle * Get the library handle
* @return void * * @return void *
*/ */
static void *library() static void *library()
{ {
// stored pointer // create on instance
static void *ptr = nullptr; static Library instance;
// is it already opened? // return the instance (it has a cast-to-void-ptr operator)
if (ptr != nullptr) return ptr; return instance;
// open ourselves
ptr = dlopen("libssl.so", RTLD_NOW);
if (!ptr)
{
std::cout << "Cannot load library: " << dlerror() << std::endl;
// reset errors
dlerror();
return nullptr;
}
return ptr;
} }
/** /**
* Initialize SSL library by registering algorithnms * Create new SSL context
*/ * @param method SSL_METHOD can be of the following types: TLS_method(), TLS_server_method(), TLS_client_method()
//~int SSL_library_init() * @return pointer to object
//~{
//~// create function
//~static Function<decltype(::SSL_library_init)> func(library(), "SSL_libarary_init");
//~// call the openssl function
//~func();
//~}
/**
* Create new SSL context
* @param SSL_METHOD can be of the following types: TLS_method(), TLS_server_method(), TLS_client_method()
* @return nullptr if failed pointer to object otherwise
*/ */
SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) SSL_CTX *SSL_CTX_new(const SSL_METHOD *method)
{ {
// create a function // create a function
static Function<decltype(::SSL_CTX_new)> func(library(), "SSL_CTX_new"); static Function<decltype(::SSL_CTX_new)> func(library(), "SSL_CTX_new");
// call the openssl function // call the openssl function
return func(method); return func(method);
} }
/** /**
* Read data from an ssl socket * Read data from an ssl socket
* @param ssl * @param ssl ssl structure
* @param buf * @param buf buffer to read into
* @param num * @param num size of buffer
* @return int number of bytes read * @return int number of bytes read
*/ */
int SSL_read(SSL *ssl, void *buf, int num) int SSL_read(SSL *ssl, void *buf, int num)
{ {
// create a function // create a function
static Function<decltype(::SSL_read)> func(library(), "SSL_read"); static Function<decltype(::SSL_read)> func(library(), "SSL_read");
// call the openssl function // call the openssl function
return func(ssl, buf, num); return func(ssl, buf, num);
} }
/** /**
* Read data from an ssl socket * Read data from an ssl socket
* @param ssl * @param ssl ssl structure
* @param buf * @param buf buffer to write
* @param num * @param num size of buffer
* @return int number of bytes written * @return int number of bytes written
*/ */
int SSL_write(SSL *ssl, const void *buf, int num) int SSL_write(SSL *ssl, const void *buf, int num)
{ {
// create a function // create a function
static Function<decltype(::SSL_write)> func(library(), "SSL_write"); static Function<decltype(::SSL_write)> func(library(), "SSL_write");
// call the openssl function // call the openssl function
return func(ssl, buf, num); return func(ssl, buf, num);
} }
/** /**
* Connect the SSL object with a file descriptor * Connect the SSL object with a file descriptor
* @param ssl SSL object * @param ssl SSL object
* @param fd file descriptor * @param fd file descriptor
* @return int wether the operation succeeded or not * @return int wether the operation succeeded or not
*/ */
int SSL_set_fd(SSL *ssl, int fd) int SSL_set_fd(SSL *ssl, int fd)
{ {
// create a function // create a function
static Function<decltype(::SSL_set_fd)> func(library(), "SSL_set_fd"); static Function<decltype(::SSL_set_fd)> func(library(), "SSL_set_fd");
// call the openssl function // call the openssl function
return func(ssl, fd); return func(ssl, fd);
} }
/** /**
* Free an allocated SSL structure * Free an allocated SSL structure
* @param ssl SSL object to be freed * @param ssl SSL object to be freed
* @return int wether the operation succeeded or not * @return int wether the operation succeeded or not
*/ */
void SSL_free(SSL *ssl) void SSL_free(SSL *ssl)
{ {
// create a function // create a function
static Function<decltype(::SSL_free)> func(library(), "SSL_free"); static Function<decltype(::SSL_free)> func(library(), "SSL_free");
// call the openssl function
return func(ssl);
// call the openssl function
return func(ssl);
} }
/** /**
* Create a new SSL structure for a connection * Create a new SSL structure for a connection
* @param ctx SSL context object * @param ctx SSL context object
* @return SSL the created SSL oject based on th context * @return SSL the created SSL oject based on th context
*/ */
SSL *SSL_new(SSL_CTX *ctx) SSL *SSL_new(SSL_CTX *ctx)
{ {
// create a function // create a function
static Function<decltype(::SSL_new)> func(library(), "SSL_new"); static Function<decltype(::SSL_new)> func(library(), "SSL_new");
// call the openssl function // call the openssl function
return func(ctx); return func(ctx);
} }
/** /**
* Create a new SSL structure for a connection * Increment refcount for a ssl structure
* @param ctx SSL context object * @param ctx SSL structure
* @return SSL the created SSL oject based on th context * @return int 1 for success, 0 for failure
*/ */
int SSL_up_ref(SSL *ssl) int SSL_up_ref(SSL *ssl)
{ {
// create a function // create a function
static Function<decltype(::SSL_up_ref)> func(library(), "SSL_up_ref"); static Function<decltype(SSL_up_ref)> func(library(), "SSL_up_ref");
// call the openssl function // call the openssl function if it exists
return func(ssl); if (func) return func(ssl);
// @todo use our own implementation
return 0;
} }
/** /**
* Shut down a TLS/SSL shut down * Shut down a TLS/SSL shut down
* @param ssl SSL object to terminate * @param ssl SSL object to terminate
* @return int returns diagnostic values * @return int returns diagnostic values
*/ */
int SSL_shutdown(SSL *ssl) int SSL_shutdown(SSL *ssl)
{ {
// create a function // create a function
static Function<decltype(::SSL_shutdown)> func(library(), "SSL_shutdown"); static Function<decltype(::SSL_shutdown)> func(library(), "SSL_shutdown");
// call the openssl function // call the openssl function
return func(ssl); return func(ssl);
} }
/** /**
* Prepare SSL object to work in client or server mode * Prepare SSL object to work in client or server mode
* @param ssl SSL object to set connect state on * @param ssl SSL object to set connect state on
*/ */
void SSL_set_connect_state(SSL *ssl) void SSL_set_connect_state(SSL *ssl)
{ {
// create a function // create a function
static Function<decltype(::SSL_set_connect_state)> func(library(), "SSL_set_connect_state"); static Function<decltype(::SSL_set_connect_state)> func(library(), "SSL_set_connect_state");
// call the openssl function // call the openssl function
func(ssl); func(ssl);
} }
/** /**
* Perform a TLS/SSL handshake * Perform a TLS/SSL handshake
* @param ssl SSL object * @param ssl SSL object
* @return int returns diagnostic values * @return int returns diagnostic values
*/ */
int SSL_do_handshake(SSL *ssl) int SSL_do_handshake(SSL *ssl)
{ {
// create a function // create a function
static Function<decltype(::SSL_do_handshake)> func(library(), "SSL_do_handshake"); static Function<decltype(::SSL_do_handshake)> func(library(), "SSL_do_handshake");
// call the openssl function // call the openssl function
return func(ssl); return func(ssl);
} }
/** /**
* Obtain result code for TLS/SSL I/O operation * Obtain result code for TLS/SSL I/O operation
* @param ssl SSL object * @param ssl SSL object
* @param ret the returned diagnostic value of SSL calls * @param ret the returned diagnostic value of SSL calls
* @return int returns error values * @return int returns error values
*/ */
int SSL_get_error(const SSL *ssl, int ret) int SSL_get_error(const SSL *ssl, int ret)
{ {
// create a function // create a function
static Function<decltype(::SSL_get_error)> func(library(), "SSL_get_error"); static Function<decltype(::SSL_get_error)> func(library(), "SSL_get_error");
// call the openssl function // call the openssl function
return func(ssl, ret); return func(ssl, ret);
} }
/** /**

46
src/linux_tcp/openssl.h Normal file
View File

@ -0,0 +1,46 @@
/**
* OpenSSL.h
*
* Header file in which we list all openssl functions in our own namespace
* that we call instead of the actual openssl functions. This allows us to
* intercept the calls and forward them to a dynamically loaded namespace
*
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
* @copyright 2018 Copernica BV
*/
/**
* Include guard
*/
#pragma once
/**
* Dependencies
*/
#include <openssl/ssl.h>
/**
* Begin of namespace
*/
namespace Copernica { namespace OpenSSL {
/**
* List of all methods that we need
*/
SSL_CTX *SSL_CTX_new(const SSL_METHOD *method);
SSL *SSL_new(SSL_CTX *ctx);
int SSL_do_handshake(SSL *ssl);
int SSL_read(SSL *ssl, void *buf, int num);
int SSL_write(SSL *ssl, const void *buf, int num);
int SSL_shutdown(SSL *ssl);
int SSL_set_fd(SSL *ssl, int fd);
int SSL_get_error(const SSL *ssl, int ret);
int SSL_up_ref(SSL *ssl);
void SSL_set_connect_state(SSL *ssl);
void SSL_free(SSL *ssl);
/**
* End of namespace
*/
}}