00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <openssl/pem.h>
00019 #include "Certificate.h"
00020
00021 using namespace CODEX_ASN1;
00022
00023 Certificate::Certificate() :
00024 Base( false ),
00025 m_value( 0 )
00026 {
00027 }
00028
00029 Certificate::Certificate( X509* value ) :
00030 Base( true ),
00031 m_value( value )
00032 {
00033 }
00034
00035 Certificate::Certificate( const Certificate& aCert ) :
00036 Base( aCert.m_initialized ),
00037 m_value( 0 )
00038 {
00039 if ( 0 != aCert.m_value )
00040 {
00041 m_value = X509_dup( aCert.m_value );
00042 }
00043 }
00044
00045 Certificate::~Certificate()
00046 {
00047 X509_free( m_value );
00048 }
00049
00050 void
00051 Certificate::operator=( const Certificate& aCert )
00052 {
00053 m_initialized = aCert.m_initialized;
00054 if ( 0 != m_value )
00055 {
00056 X509_free( m_value );
00057 m_value = 0;
00058 }
00059 if ( 0 != aCert.m_value )
00060 {
00061 m_value = X509_dup( aCert.m_value );
00062 }
00063 }
00064
00065 int
00066 Certificate::marshal( unsigned char ** pp ) const
00067 {
00068 return i2d_X509( m_value, pp );
00069 }
00070
00071 void*
00072 Certificate::unmarshal( void* bogus, unsigned char** pp, long length )
00073 {
00074 if ( m_initialized )
00075 {
00076 return NULL;
00077 }
00078 if ( 0 == d2i_X509( &m_value, (unsigned char**)pp, length ) )
00079 {
00080 return NULL;
00081 }
00082 m_initialized = true;
00083 return this;
00084 }
00085
00086 void*
00087 Certificate::fromPEMFile(const char* fname)
00088 {
00089 FILE* fp = fopen( fname, "r" );
00090 if ( NULL == fp )
00091 {
00092 return NULL;
00093 }
00094 if ( NULL != m_value )
00095 {
00096 X509_free( m_value );
00097 m_value = NULL;
00098 }
00099 if ( ! PEM_read_X509( fp, &m_value, NULL, NULL ) )
00100 {
00101 fclose( fp );
00102 return NULL;
00103 }
00104 fclose( fp );
00105 m_initialized = true;
00106 return this;
00107 }