]> WPIA git - cassiopeia.git/blob - src/bios.h
add: Initial code to implement revocation
[cassiopeia.git] / src / bios.h
1 #pragma once
2
3 #include <openssl/bio.h>
4
5 #define BIO_TYPE_CUSTOM 0xff
6
7 class OpensslBIO {
8 public:
9     static const int typeID = BIO_TYPE_CUSTOM;
10     virtual ~OpensslBIO();
11
12     virtual int write( const char* buf, int num ) = 0;
13     virtual int read( char* buf, int size ) = 0;
14     virtual int puts( const char* str );
15     virtual int gets( char* str, int size );
16     virtual long ctrl( int cmod, long arg1, void* arg2 ) = 0;
17 };
18
19 namespace BIOWrapper {
20
21     int write( BIO* b, const char* buf, int num );
22
23     int read( BIO* b, char* buf, int size );
24
25     int puts( BIO* b, const char* str );
26
27     int gets( BIO* b, char* str, int size );
28
29     long ctrl( BIO* b, int cmod, long arg1, void* arg2 );
30
31     template <typename T>
32     int bio_new( BIO* b ) {
33         b->init = 1;
34         b->num = 0;
35         b->ptr = new T();
36         return 1;
37     }
38
39     int free( BIO* b );
40
41 }
42
43 template <typename T>
44 BIO_METHOD* toBio() {
45     return toBio<T>( BIOWrapper::bio_new<T> );
46 }
47
48 template <typename T>
49 BIO_METHOD* toBio( int ( *newfunc )( BIO* ) ) {
50     static BIO_METHOD new_method = {
51         T::typeID,
52         T::getName(),
53         BIOWrapper::write,
54         BIOWrapper::read,
55         BIOWrapper::puts,
56         BIOWrapper::gets,
57         BIOWrapper::ctrl,
58         newfunc,
59         BIOWrapper::free,
60         NULL,
61     };
62
63     return &new_method;
64 }