TimeStampResponse.cpp

Go to the documentation of this file.
00001 
00009 #include "TimeStampResponse.h"
00010 #include "PKIFTSP.h"
00011 #include "Buffer.h"
00012 #include "PKIStatusInfo.h"
00013 #include "ContentInfo.h"
00014 #include "ASN1Helper.h"
00015 #include "ToolkitUtils.h"
00016 #include "TSPException.h"
00017 #include "private/PKIFTSPMemoryHelper.h"
00018 #include "pkiftsperrors.h"
00019 #include "OID.h"
00020 
00021 #include "PKIXTSP.h"
00022 
00024 
00025 struct CPKIFTimeStampResponseImpl
00026 {
00027     CPKIFTimeStampResponse *m_parent;
00028 
00029     CPKIFPKIStatusInfoPtr m_status;
00030     CPKIFContentInfoPtr m_timeStampToken;
00031 
00032     CPKIFASNWrapper<PKIFTSPTimeStampResp>* m_tsr;
00033 
00034     void ClearAllMembers();
00035     void MakeTSR();
00036     void FreeTSR();
00037 
00038     //member to maintain state when calling Set functions
00039     void CallingAllGets();
00040 };
00041 
00050 void CPKIFTimeStampResponseImpl::ClearAllMembers()
00051 {
00052     LOG_STRING_DEBUG("CPKIFTimeStampResponse::ClearAllMembers()", TOOLKIT_TSP_ASN, 0, this);
00053 
00054     FreeTSR();
00055 }
00063 void CPKIFTimeStampResponseImpl::CallingAllGets()
00064 {
00065     LOG_STRING_DEBUG("CPKIFTimeStampResponse::CallingAllGets()", TOOLKIT_TSP_ASN, 0, this);
00066 
00067     m_parent->GetStatus();
00068     m_parent->GetTimeStampToken();
00069     FreeTSR();
00070 }
00078 void CPKIFTimeStampResponseImpl::MakeTSR()
00079 {
00080     LOG_STRING_DEBUG("CPKIFTimeStampResponse::MakeTSR()", TOOLKIT_TSP_ASN, 0, this);
00081 
00082     FreeTSR();
00083     m_tsr = new CPKIFASNWrapper<PKIFTSPTimeStampResp>( BEREncPKIFTSPTimeStampResp, BERDecPKIFTSPTimeStampResp );
00084 }
00092 void CPKIFTimeStampResponseImpl::FreeTSR()
00093 {
00094     LOG_STRING_DEBUG("CPKIFTimeStampResponse::FreeTSR()", TOOLKIT_TSP_ASN, 0, this);
00095 
00096     if(NULL != m_tsr)
00097         delete m_tsr;
00098     m_tsr = NULL;
00099 }
00100 
00102 
00111 CPKIFTimeStampResponse::CPKIFTimeStampResponse()
00112   :m_impl(new CPKIFTimeStampResponseImpl)
00113 {
00114     LOG_STRING_DEBUG("CPKIFTimeStampResponse::CPKIFTimeStampResponse()", TOOLKIT_TSP_ASN, 0, this);
00115 
00116     m_impl->m_parent = this;
00117     m_impl->m_tsr = NULL;
00118 }
00126 CPKIFTimeStampResponse::~CPKIFTimeStampResponse()
00127 {
00128     LOG_STRING_DEBUG("CPKIFTimeStampResponse::~CPKIFTimeStampResponse()", TOOLKIT_TSP_ASN, 0, this);
00129 
00130     m_impl->FreeTSR();
00131 
00132     if (m_impl)
00133     {
00134       delete m_impl;
00135     }
00136 }
00144 void CPKIFTimeStampResponse::SetStatus(
00147     CPKIFPKIStatusInfoPtr& status)
00148 {
00149     LOG_STRING_DEBUG("CPKIFTimeStampResponse::SetStatus(CPKIFPKIStatusInfoPtr& status)", TOOLKIT_TSP_ASN, 0, this);
00150 
00151     m_impl->CallingAllGets();
00152     m_impl->m_status = status;
00153 }
00162 CPKIFPKIStatusInfoPtr CPKIFTimeStampResponse::GetStatus() const
00163 {
00164     if(m_impl->m_status == (CPKIFPKIStatusInfo*)NULL && NULL != m_impl->m_tsr && NULL != (*m_impl->m_tsr).data())
00165     {
00166         LOG_STRING_DEBUG("CPKIFTimeStampResponse::GetStatus()", TOOLKIT_TSP_ASN, 0, this);
00167 
00168         CACASNWRAPPER_CREATE(PKIFTSPPKIStatusInfo, siWrapper);
00169         ASN1OpenType *data  = siWrapper.Encode(&(*m_impl->m_tsr)->status);
00170         CPKIFBufferPtr siBuf(new CPKIFBuffer(data->data, data->numocts));
00171 
00172         CPKIFPKIStatusInfoPtr tmpRef(new CPKIFPKIStatusInfo(siBuf)); 
00173         //CPKIFPKIStatusInfoPtr tmpRef(new CPKIFPKIStatusInfo((*m_impl->m_tsr)->status)); 
00174 
00175         CPKIFTimeStampResponse* nonConst = const_cast<CPKIFTimeStampResponse*>(this);
00176         nonConst->m_impl->m_status = tmpRef;
00177     }
00178 
00179     return m_impl->m_status;
00180 }
00188 void CPKIFTimeStampResponse::SetTimeStampToken(
00190     CPKIFContentInfoPtr& timeStampToken)
00191 {
00192     LOG_STRING_DEBUG("CPKIFTimeStampResponse::SetTimeStampToken(CPKIFContentInfoPtr& timeStampToken)", TOOLKIT_TSP_ASN, 0, this);
00193 
00194     m_impl->CallingAllGets();
00195     m_impl->m_timeStampToken = timeStampToken;
00196 }
00205 CPKIFContentInfoPtr CPKIFTimeStampResponse::GetTimeStampToken() const
00206 {
00207     if(m_impl->m_timeStampToken == (CPKIFContentInfo*)NULL && NULL != m_impl->m_tsr && (*m_impl->m_tsr)->m.timeStampTokenPresent)
00208     {
00209         LOG_STRING_DEBUG("CPKIFTimeStampResponse::GetTimeStampToken()", TOOLKIT_TSP_ASN, 0, this);
00210 
00211         CPKIFOIDPtr oid(new CPKIFOID((*m_impl->m_tsr)->timeStampToken.contentType.subid, (*m_impl->m_tsr)->timeStampToken.contentType.numids));
00212         CPKIFBufferPtr buffer(new CPKIFBuffer((*m_impl->m_tsr)->timeStampToken.content.data, 
00213                                               (*m_impl->m_tsr)->timeStampToken.content.numocts));
00214 
00215         CPKIFContentInfoPtr tmpRef(new CPKIFContentInfo(oid, buffer));
00216         //CPKIFContentInfoPtr tmpRef(new CPKIFContentInfo((*m_impl->m_tsr)->timeStampToken));
00217 
00218         CPKIFTimeStampResponse* nonConst = const_cast<CPKIFTimeStampResponse*>(this);
00219         nonConst->m_impl->m_timeStampToken = tmpRef;
00220     }
00221 
00222     return m_impl->m_timeStampToken;
00223 }
00234 CPKIFBufferPtr CPKIFTimeStampResponse::Encode() const
00235 {
00236     LOG_STRING_DEBUG("CPKIFTimeStampResponse::Encode()", TOOLKIT_TSP_ASN, 0, this);
00237 
00238     PKIFTSPMemoryHelper mhTSPResponse;
00239     mhTSPResponse.pResponse = new PKIFTSPTimeStampResp;
00240     memset(mhTSPResponse.pResponse, 0, sizeof(PKIFTSPTimeStampResp));
00241 
00242     CPKIFPKIStatusInfoPtr si = GetStatus();
00243     if(si == (CPKIFPKIStatusInfo*)NULL)
00244     {
00245         throw CPKIFTSPException(TOOLKIT_TSP_ASN, TSP_MISSING_REQUIRED_FIELD, "Status field not present.");
00246     }
00247     else
00248     {
00249         PKIStatus status = si->GetStatus();
00250         mhTSPResponse.pResponse->status.status = (PKIFTSPPKIStatus_Root)status;
00251 
00252         //get fail info
00253         std::bitset<8> failInfo;
00254         si->GetFailInfo(failInfo);
00255 
00256         CPKIFStringArray strs;
00257         si->GetStatusStrings(strs);
00258 
00259         if(!strs.empty())
00260             mhTSPResponse.pResponse->status.m.statusStringPresent = 1;
00261 
00262         DListNode* cur = NULL;
00263         CPKIFStringArray::iterator pos;
00264         CPKIFStringArray::iterator end = strs.end();
00265         for(pos = strs.begin(); pos != end; ++pos)
00266         {
00267             if(NULL == cur)
00268             {
00269                 NEW_NODE(cur)
00270             }
00271             else
00272             {
00273                 NEW_NEXT_AND_ADVANCE(cur)
00274             }
00275 
00276             const char* pStr = (*pos)->c_str();
00277             cur->data = (void*)&pStr;
00278 
00279             SET_HEAD_TAIL_INCREMENT(mhTSPResponse.pResponse->status.statusString, cur)
00280         }
00281     }
00282 
00283     CPKIFContentInfoPtr tst = GetTimeStampToken();
00284     if(tst != (CPKIFContentInfo*)NULL)
00285     {
00286         mhTSPResponse.pResponse->m.timeStampTokenPresent = 1;
00287         CPKIFStringPtr str(new std::string(tst->GetContentType()->ToString())); 
00288         ASN1OBJID* tmpOid = ConvertStringToASN1OBJID(str);
00289         CopyOID(&mhTSPResponse.pResponse->timeStampToken.contentType, tmpOid);
00290 
00291         if( NULL != tmpOid)
00292             delete tmpOid;
00293         //mhTSPResponse.pResponse->timeStampToken.contentType = *tst->GetContentType()->raw();
00294         mhTSPResponse.pResponse->timeStampToken.content.data = tst->GetContent()->GetBuffer();
00295         mhTSPResponse.pResponse->timeStampToken.content.numocts = tst->GetContent()->GetLength();
00296     }
00297 
00298     CACASNWRAPPER_CREATE(PKIFTSPTimeStampResp, objPDU);
00299     ASN1OpenType* data1 = objPDU.Encode(mhTSPResponse.pResponse);
00300     CPKIFBufferPtr tmp(new CPKIFBuffer(data1->data, data1->numocts));
00301     delete data1;
00302     return tmp;
00303 }
00311 void CPKIFTimeStampResponse::Decode(
00313     CPKIFBufferPtr& buf)
00314 {
00315     LOG_STRING_DEBUG("CPKIFTimeStampResponse::Decode(CPKIFBufferPtr& buf)", TOOLKIT_TSP_ASN, 0, this);
00316     m_impl->ClearAllMembers();
00317 
00318     //if the input is empty - fail now
00319     if(buf == (CPKIFBuffer*)NULL || 0 == buf->GetLength())
00320     {
00321         throw CPKIFTSPException(TOOLKIT_TSP_ASN, COMMON_INVALID_INPUT);
00322     }
00323 
00324     m_impl->MakeTSR();
00325 
00326     try
00327     {
00328         //otherwise try to parse it into the m_asr member
00329         (*m_impl->m_tsr).Decode(buf->GetBuffer(), buf->GetLength());
00330     }
00331     catch(CPKIFException&)
00332     {
00333         //delete e;
00334         //changed error code 3/2/2004
00335         throw CPKIFTSPException(TOOLKIT_TSP_ASN, ASN1_DECODE_ERROR);
00336     }
00337 }

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