--- /dev/null
+#include "bios.h"
+
+#include <string.h>
+
+namespace BIOWrapper {
+
+ int write( BIO* b, const char* buf, int num ) {
+ return ( ( OpensslBIO* )b->ptr )->write( buf, num );
+ }
+
+ int read( BIO* b, char* buf, int size ) {
+ return ( ( OpensslBIO* )b->ptr )->read( buf, size );
+ }
+
+ int puts( BIO* b, const char* str ) {
+ return ( ( OpensslBIO* )b->ptr )->puts( str );
+ }
+
+ int gets( BIO* b, char* str, int size ) {
+ return ( ( OpensslBIO* )b->ptr )->gets( str, size );
+ }
+
+ long ctrl( BIO* b, int cmod, long arg1, void* arg2 ) {
+ return ( ( OpensslBIO* )b->ptr )->ctrl( cmod, arg1, arg2 );
+ }
+
+ int free( BIO* b ) {
+ delete( ( OpensslBIO* ) b->ptr );
+ b->ptr = 0;
+ return 0;
+ }
+
+}
+
+OpensslBIO::~OpensslBIO() {}
+
+int OpensslBIO::puts( const char* str ) {
+ ( void ) str;
+ return -1;
+}
+int OpensslBIO::gets( char* str, int size ) {
+ ( void ) str;
+ ( void ) size;
+ return -1;
+}
--- /dev/null
+#pragma once
+
+#include <openssl/bio.h>
+
+#define BIO_TYPE_CUSTOM 0xff
+
+class OpensslBIO {
+public:
+ static const int typeID = BIO_TYPE_CUSTOM;
+ virtual ~OpensslBIO();
+
+ virtual int write( const char* buf, int num ) = 0;
+ virtual int read( char* buf, int size ) = 0;
+ virtual int puts( const char* str );
+ virtual int gets( char* str, int size );
+ virtual long ctrl( int cmod, long arg1, void* arg2 ) = 0;
+};
+
+namespace BIOWrapper {
+
+ int write( BIO* b, const char* buf, int num );
+ int read( BIO* b, char* buf, int size );
+ int puts( BIO* b, const char* str );
+ int gets( BIO* b, char* str, int size );
+ long ctrl( BIO* b, int cmod, long arg1, void* arg2 );
+
+ template <typename T>
+ int bio_new( BIO* b ) {
+ b->init = 1;
+ b->num = 0;
+ b->ptr = new T();
+ return 1;
+ }
+
+ int free( BIO* b );
+
+}
+
+template <typename T>
+BIO_METHOD* toBio() {
+ static BIO_METHOD new_method = {
+ T::typeID,
+ T::getName(),
+ BIOWrapper::write,
+ BIOWrapper::read,
+ BIOWrapper::puts,
+ BIOWrapper::gets,
+ BIOWrapper::ctrl,
+ BIOWrapper::bio_new<T>,
+ BIOWrapper::free,
+ NULL,
+ };
+
+ return &new_method;
+}
--- /dev/null
+#include "opensslBIO.h"
+
+OpensslBIOWrapper::OpensslBIOWrapper( BIO* b ) {
+ this->b = b;
+}
+
+OpensslBIOWrapper::~OpensslBIOWrapper() {
+ BIO_free( b );
+}
+
+int OpensslBIOWrapper::write( const char* buf, int num ) {
+ return BIO_write( b, buf, num );
+}
+
+int OpensslBIOWrapper::read( char* buf, int size ) {
+ return BIO_read( b, buf, size );
+}
+
+long OpensslBIOWrapper::ctrl( int cmod, long arg1, void* arg2 ) {
+ return BIO_ctrl( b, cmod, arg1, arg2 );
+}
+
+int OpensslBIOWrapper::puts( const char* str ) {
+ return BIO_puts( b, str );
+}
+
+int OpensslBIOWrapper::gets( char* str, int size ) {
+ return BIO_gets( b, str, size );
+}
+
+const char* OpensslBIOWrapper::getName() {
+ return "OpenSSLWrapper";
+}
--- /dev/null
+#pragma once
+
+#include "bios.h"
+
+class OpensslBIOWrapper : public OpensslBIO {
+private:
+ BIO* b;
+public:
+ OpensslBIOWrapper( BIO* b );
+ virtual ~OpensslBIOWrapper();
+
+ int write( const char* buf, int num );
+ int read( char* buf, int size );
+ long ctrl( int cmod, long arg1, void* arg2 );
+
+ int puts( const char* str );
+ int gets( char* str, int size );
+
+ static const char* getName();
+};
--- /dev/null
+#include <iostream>
+
+#include <boost/test/unit_test.hpp>
+
+#include "bios.h"
+#include "opensslBIO.h"
+
+class OpensslBIO1 : public OpensslBIO {
+public:
+ int state;
+
+ int write( const char* buf, int num );
+ int read( char* buf, int size );
+ long ctrl( int cmod, long arg1, void* arg2 );
+
+ static const char* getName();
+};
+
+int OpensslBIO1::write( const char* buf, int num ) {
+ state = num * 2;
+ ( void ) buf;
+ return 0;
+}
+int OpensslBIO1::read( char* buf, int size ) {
+ state = size * 3;
+ ( void ) buf;
+ return 0;
+}
+long OpensslBIO1::ctrl( int cmod, long arg1, void* arg2 ) {
+ state = cmod * 7;
+ ( void ) arg1;
+ ( void ) arg2;
+ return 0;
+}
+const char* OpensslBIO1::getName() {
+ return "dummyBIO";
+}
+
+BOOST_AUTO_TEST_SUITE( TestBioWrapper )
+
+BOOST_AUTO_TEST_CASE( BasicCalls ) {
+ BIO* n = BIO_new( toBio<OpensslBIO1>() );
+ OpensslBIO* o = new OpensslBIOWrapper( n );
+ OpensslBIO1* data = ( OpensslBIO1* ) n->ptr;
+
+ o->write( "bla", 13 );
+ BOOST_CHECK( data->state == 13 * 2 );
+
+ char buf[17];
+ o->read( buf, 17 );
+ BOOST_CHECK( data->state == 17 * 3 );
+
+ o->ctrl( 19, 0, 0 );
+ BOOST_CHECK( data->state == 19 * 7 );
+
+ delete o;
+}
+
+BOOST_AUTO_TEST_SUITE_END()