CACContentInfo.cpp

Go to the documentation of this file.
00001 
00009 #include "ContentInfo.h"
00010 
00011 #include "PKIFMessageException.h"
00012 #include "ASN1Helper.h"
00013 #include "CryptographicMessageSyntax2004.h"
00014 #include "ToolkitUtils.h"
00015 #include "PKIFMessageErrors.h"
00016 
00017 #include "OID.h"
00018 #include "Buffer.h"
00019 #include "PKIFBase64.h"
00020 
00022 struct CPKIFContentInfoImpl
00023 {
00024     CPKIFOIDPtr m_contentType;
00025     CPKIFBufferPtr m_content;
00026 };
00028 
00036 CPKIFContentInfo::CPKIFContentInfo()
00037     :m_impl (new CPKIFContentInfoImpl)
00038 {
00039     LOG_STRING_DEBUG("CPKIFContentInfo::CPKIFContentInfo()", TOOLKIT_MESSAGE_CONTENT_INFO, 0, this);
00040     SetContentType(g_contentInfo);
00041 }
00049 CPKIFContentInfo::~CPKIFContentInfo()
00050 {
00051     LOG_STRING_DEBUG("CPKIFContentInfo::~CPKIFContentInfo()", TOOLKIT_MESSAGE_CONTENT_INFO, 0, this);
00052 
00053     delete m_impl;
00054     m_impl = NULL;
00055 }
00056 
00057 
00066 CPKIFContentInfo::CPKIFContentInfo(
00068     const CPKIFOIDPtr& oid,
00070     //CACCMSContentInfo& ci)
00071     const CPKIFBufferPtr& ciBuf)
00072     :m_impl (new CPKIFContentInfoImpl)
00073 {
00074     LOG_STRING_DEBUG("CPKIFContentInfo::CPKIFContentInfo(CACCMSContentInfo& ci)", TOOLKIT_MESSAGE_CONTENT_INFO, 0, this);
00075 
00076     m_impl->m_contentType = oid;
00077     m_impl->m_content = ciBuf;
00078 }
00079 
00088 /*CPKIFContentInfo::CPKIFContentInfo(
00090     CACCMSContentInfo& ci)
00091     :m_impl (new CPKIFContentInfoImpl)
00092 {
00093     LOG_STRING_DEBUG("CPKIFContentInfo::CPKIFContentInfo(CACCMSContentInfo& ci)", TOOLKIT_MESSAGE_CONTENT_INFO, 0, this);
00094 
00095     CPKIFOIDPtr tmpOID(new CPKIFOID(ci.contentType));
00096     m_impl->m_contentType = tmpOID;
00097 
00098     CPKIFBufferPtr tmpBuf(new CPKIFBuffer(ci.content.data, ci.content.numocts));
00099     m_impl->m_content = tmpBuf;
00100 }
00101 */
00112 void CPKIFContentInfo::SetContentType(
00114     CPKIFOIDPtr& contentType) 
00115 {
00116     m_impl->m_contentType = contentType;
00117 }
00128 CPKIFOIDPtr CPKIFContentInfo::GetContentType() const 
00129 {
00130     return m_impl->m_contentType;
00131 }
00140 void CPKIFContentInfo::SetContent(
00142     CPKIFBufferPtr& content) 
00143 {
00144     m_impl->m_content = content;
00145 }
00154 CPKIFBufferPtr CPKIFContentInfo::GetContent() const 
00155 {
00156     return m_impl->m_content;
00157 }
00171 void CPKIFContentInfo::Decode(
00173     const unsigned char* buf,
00175     int bufLen)
00176 {
00177     LOG_STRING_DEBUG("CPKIFContentInfo::Decode(const unsigned char* buf, int bufLen)", TOOLKIT_MESSAGE_CONTENT_INFO, 0, this);
00178 
00179     //if the input is empty - fail now
00180     if(NULL == buf || 0 == bufLen)
00181     {
00182         throw CPKIFMessageException(thisComponent, COMMON_INVALID_INPUT, "NULL parameter passed to CPKIFContentInfo::Decode");
00183     }
00184 
00185     try
00186     {
00187         //otherwise, try to parse (the decode function may throw)
00188         CPKIFASNWrapper<CACCMSContentInfo> cmsMsg(BEREncCACCMSContentInfo, BERDecCACCMSContentInfo);
00189         cmsMsg.Decode(buf, bufLen);
00190 
00191         //if we get here then all was well, save the pieces
00192         CPKIFOIDPtr tmpOID(new CPKIFOID(cmsMsg->contentType.subid, cmsMsg->contentType.numids));
00193 
00194         //added 3/26/2004
00195         //commented out 2/20/2006 - new ASN.1 compiler does not exhibit the same behavior
00196         //const unsigned char* bufEnd = buf + bufLen;
00197         //const unsigned char* parsedEnd = cmsMsg->content.data + cmsMsg->content.numocts;
00198         //if(bufEnd < parsedEnd)
00199         //  throw CPKIFMessageException(thisComponent, MSG_DECODE_FAILED);
00200 
00201         CPKIFBufferPtr tmp(new CPKIFBuffer(cmsMsg->content.data, cmsMsg->content.numocts));
00202 
00203         //then assign them to the members
00204         m_impl->m_contentType = tmpOID;
00205         m_impl->m_content = tmp;
00206     }
00207     catch(CPKIFException& e)
00208     {
00209         unsigned char * decoded = NULL;
00210         unsigned long decodedLen;
00211         bool b = PEMDecode((char *)buf, &decoded, &decodedLen);
00212         if(b)
00213         {
00214             try
00215             {
00216                 CPKIFASNWrapper<CACCMSContentInfo> cmsMsg(BEREncCACCMSContentInfo, BERDecCACCMSContentInfo);
00217                 cmsMsg.Decode(decoded, decodedLen);
00218 
00219                 //if we get here then all was well, save the pieces
00220                 CPKIFOIDPtr tmpOID(new CPKIFOID(cmsMsg->contentType.subid, cmsMsg->contentType.numids));
00221 
00222                 CPKIFBufferPtr tmp(new CPKIFBuffer(cmsMsg->content.data, cmsMsg->content.numocts));
00223 
00224                 //then assign them to the members
00225                 m_impl->m_contentType = tmpOID;
00226                 m_impl->m_content = tmp;
00227 
00228                 if(NULL != decoded)
00229                     delete decoded;
00230 
00231             }catch(CPKIFException& e)
00232             {
00233                 if(NULL != decoded)
00234                     delete decoded;
00235 
00236                 CPKIFMessageException me(thisComponent, MSG_DECODE_FAILED);
00237                 me.push_info(e);
00238                 //delete e;
00239                 throw me;
00240             }
00241         }
00242         else
00243         {
00244             if(NULL != decoded)
00245                 delete decoded;
00246 
00247             CPKIFMessageException me(thisComponent, MSG_DECODE_FAILED);
00248             me.push_info(e);
00249             //delete e;
00250             throw me;
00251         }
00252     }
00253 }
00261 void CPKIFContentInfo::Decode(
00263     CPKIFBufferPtr& buf)
00264 {
00265     if(buf == (CPKIFBuffer*)NULL)
00266     {
00267         throw CPKIFMessageException(thisComponent, COMMON_INVALID_INPUT, "NULL parameter passed to CPKIFContentInfo::Decode");
00268     }
00269 
00270     Decode(buf->GetBuffer(), buf->GetLength());
00271 }
00284 CPKIFBufferPtr CPKIFContentInfo::Encode()
00285 {
00286     LOG_STRING_DEBUG("CPKIFContentInfo::Encode()", TOOLKIT_MESSAGE_CONTENT_INFO, 0, this);
00287 
00288     //if we don't have a content OID and content then fail
00289     if(m_impl->m_contentType == (CPKIFOID*)NULL || m_impl->m_content == (CPKIFBuffer*)NULL)
00290     {
00291         throw CPKIFMessageException(thisComponent, MSG_INCOMPLETE);
00292     }
00293 
00294     //otherwise create a content info object
00295     CACCMSContentInfo cmsMsg;
00296 
00297     //set the object to point to existing stuff
00298     CPKIFStringPtr str(new std::string(m_impl->m_contentType->ToString())); 
00299     ASN1OBJID* tmpOid = ConvertStringToASN1OBJID(str);
00300     CopyOID(&cmsMsg.contentType, tmpOid);
00301 
00302     if(NULL != tmpOid)
00303         delete tmpOid;
00304     //cmsMsg.contentType = *m_impl->m_contentType->raw();
00305     //cmsMsg.contentType = *tmpOid;
00306     cmsMsg.content.data = m_impl->m_content->GetBuffer();
00307     cmsMsg.content.numocts = m_impl->m_content->GetLength();
00308 
00309     ASN1OpenType* data = NULL;
00310 
00311     try
00312     {
00313         //encode the message
00314         CACASNWRAPPER_CREATE(CACCMSContentInfo, objPDU);
00315         data = objPDU.Encode(&cmsMsg);
00316 
00317         //return a CPKIFBufferPtr containing the encoded message
00318         CPKIFBufferPtr tmp(new CPKIFBuffer(data->data, data->numocts));
00319         delete data;
00320         return tmp;
00321     }
00322     catch(CPKIFException& e)
00323     {
00324         CPKIFMessageException me(thisComponent, MSG_ENCODE_FAILED);
00325         me.push_info(e);
00326         //delete e;
00327         throw me;
00328     }
00329 }

Generated on Mon Nov 15 11:15:46 2010 for PublicKeyInfrastructureFramework(PKIF) by  doxygen 1.5.6