]> WPIA git - cassiopeia.git/commitdiff
add: wrapping of openssl-bios into cpp-classes (both directions)
authorFelix Dörre <felix@dogcraft.de>
Tue, 25 Nov 2014 08:07:34 +0000 (09:07 +0100)
committerBenny Baumann <BenBE@geshi.org>
Sat, 24 Jan 2015 16:39:31 +0000 (17:39 +0100)
src/bios.cpp [new file with mode: 0644]
src/bios.h [new file with mode: 0644]
src/opensslBIO.cpp [new file with mode: 0644]
src/opensslBIO.h [new file with mode: 0644]
test/src/bioWrapper.cpp [new file with mode: 0644]

diff --git a/src/bios.cpp b/src/bios.cpp
new file mode 100644 (file)
index 0000000..15d318c
--- /dev/null
@@ -0,0 +1,45 @@
+#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;
+}
diff --git a/src/bios.h b/src/bios.h
new file mode 100644 (file)
index 0000000..4ea874c
--- /dev/null
@@ -0,0 +1,55 @@
+#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;
+}
diff --git a/src/opensslBIO.cpp b/src/opensslBIO.cpp
new file mode 100644 (file)
index 0000000..57a07d1
--- /dev/null
@@ -0,0 +1,33 @@
+#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";
+}
diff --git a/src/opensslBIO.h b/src/opensslBIO.h
new file mode 100644 (file)
index 0000000..9c7e12e
--- /dev/null
@@ -0,0 +1,20 @@
+#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();
+};
diff --git a/test/src/bioWrapper.cpp b/test/src/bioWrapper.cpp
new file mode 100644 (file)
index 0000000..73d7c36
--- /dev/null
@@ -0,0 +1,59 @@
+#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()