ASN1Helper.h
Go to the documentation of this file.00001
00010 #ifndef __ASN1HELPER_H__
00011 #define __ASN1HELPER_H__
00012
00013 #if _MSC_VER > 1000
00014 #pragma once
00015 #endif // _MSC_VER > 1000
00016 #include "PKIFdll.h"
00017 #include "components.h"
00018 #include "ASN1Errors.h"
00019 #include "PKIFCommonErrors.h"
00020 #include "PKIFException.h"
00021 #include "ASN1HelperLib.h"
00022
00023 extern "C"
00024 {
00025 #include "ooasn1.h"
00026 #include "asn1ber.h"
00027 }
00028
00029
00030 #define CACASNWRAPPER_CREATE(className, pduName) \
00031 CPKIFASNWrapper<className> pduName( BEREnc##className, BERDec##className );
00032
00033
00034
00035
00036 template<class T>
00043 class CPKIFASNWrapper
00044 {
00045 private:
00046 typedef CPKIFASNWrapper<T> this_type;
00047 public:
00055 CPKIFASNWrapper(int (*pEncode)(OOCTXT*, T*, ASN1TagType),
00056 int (*pDecode)(OOCTXT*, T*, ASN1TagType, int)) : mptrData( 0 ),
00057 m_pEncode(pEncode), m_pDecode(pDecode), m_didDecode(false), m_didEncode(false)
00058
00059 {
00060 initContext (&m_context);
00061
00062
00063 }
00071 ~CPKIFASNWrapper( )
00072 {
00073 clear( );
00074 }
00075
00083 T * data( ) const { return mptrData; }
00091 T * operator->( ) const { return mptrData; }
00092 typedef T* this_type::*unspecified_bool_type;
00093 operator unspecified_bool_type() const
00094 {
00095 return mptrData == 0 ? 0: &this_type::mptrData;
00096 }
00097
00105 bool operator!() const
00106 {
00107 return mptrData == 0;
00108 }
00116 T * Decode( const unsigned char* buf, int bufLen )
00117 {
00118 if(!buf || 0 >= bufLen)
00119 throw CPKIFException(TOOLKIT_ASN, COMMON_INVALID_INPUT);
00120
00121 ASN1OpenType temp;
00122 temp.data = buf;
00123 temp.numocts = bufLen;
00124
00125 return Decode( temp );
00126 }
00134 ASN1OpenType* Encode( const T * ptrData )
00135 {
00136 setBEREncBufPtr(&m_context, NULL, 0);
00137 T* nonConstPData = const_cast<T*>(ptrData);
00138 int msgLen = m_pEncode(&m_context, nonConstPData, ASN1EXPL);
00139 if (msgLen > 0)
00140 {
00141 m_didEncode = true;
00142
00143 ASN1OpenType* tmp = new ASN1OpenType();
00144 tmp->numocts = msgLen;
00145 tmp->data = getBEREncBufPtr(&m_context);
00146
00147 return tmp;
00148 }
00149 else
00150 {
00151 throw CPKIFException(TOOLKIT_ASN, ASN1_ENCODE_ERROR);
00152 }
00153 }
00161 void clear( )
00162 {
00163
00164
00165 FreeASN1Context(&m_context);
00166
00167 if( mptrData )
00168 {
00169 delete mptrData;
00170 mptrData = 0;
00171 }
00172 }
00180 T * Decode( const ASN1OpenType & encoded_ )
00181 {
00182 setBERDecBufPtr(&m_context, encoded_.data, encoded_.numocts, NULL, NULL);
00183
00184
00185 if( mptrData )
00186 {
00187 delete mptrData;
00188 mptrData = 0;
00189 }
00190
00191 mptrData = new T;
00192 int nRc = m_pDecode(&m_context, mptrData, ASN1EXPL, 0);
00193 if( nRc )
00194 {
00195
00196 FreeASN1Context(&m_context);
00197 if(mptrData)
00198 {
00199 delete mptrData; mptrData = NULL;
00200 }
00201 throw CPKIFException(TOOLKIT_ASN, ASN1_DECODE_ERROR, "Failed to decode ASN1 object");
00202 }
00203 else
00204 {
00205 m_didDecode = true;
00206 }
00207 return mptrData;
00208 }
00216 void InitContext()
00217 {
00218 initContext (&m_context);
00219
00220
00221 }
00222
00223 private:
00224
00225 CPKIFASNWrapper( const CPKIFASNWrapper<T> & );
00226 CPKIFASNWrapper<T> & operator=( const CPKIFASNWrapper<T> & );
00227
00228 private:
00229 OOCTXT m_context;
00230 T * mptrData;
00231 int (*m_pEncode)(OOCTXT*, T*, ASN1TagType);
00232 int (*m_pDecode)(OOCTXT*, T*, ASN1TagType, int);
00233 bool m_didEncode;
00234 bool m_didDecode;
00235 };
00236
00237 #endif
00238