BasicOCSPResponse.cpp

Go to the documentation of this file.
00001 
00010 #include "BasicOCSPResponse.h"
00011 
00012 #include "Buffer.h"
00013 #include "Certificate.h"
00014 #include "ResponseData.h"
00015 #include "AlgorithmIdentifier.h"
00016 #include "PKIFTime.h"
00017 #include "SingleResponse.h"
00018 #include "OCSPCertStatus.h"
00019 
00020 #include "OCSPException.h"
00021 #include "ToolkitUtils.h"
00022 #include "PKIFX509Extensions2.h"
00023 #include "ASN1Helper.h"
00024 #include "OCSP.h"
00025 #include "PKIFCommonErrors.h"
00026 #include "SubjectPublicKeyInfo.h"
00027 #include "ExtendedKeyUsage.h"
00028 #include "AccessDescription.h"
00029 #include "BasicOCSPResponse.h"
00030 
00031 #include <iterator>
00032 
00033 using namespace std;
00034 
00036 struct CPKIFBasicOCSPResponseImpl
00037 {
00038     CPKIFResponseDataPtr m_responseData;
00039     CPKIFAlgorithmIdentifierPtr m_sigAlg;
00040     CPKIFBufferPtr m_signature;
00041     CPKIFCertificateList m_certs;
00042 
00043     CPKIFASNWrapper<BasicOCSPResponse>* m_ocspResp;
00044 
00045     void ClearAllMembers();
00046     void MakeOCSPResponse();
00047     void FreeOCSPResponse();
00048 };
00050 
00058 CPKIFBasicOCSPResponse::CPKIFBasicOCSPResponse(void)
00059     :m_impl (new CPKIFBasicOCSPResponseImpl)
00060 {
00061     LOG_STRING_DEBUG("CPKIFBasicOCSPResponse::CPKIFBasicOCSPResponse(void)", TOOLKIT_OCSP_ASN, 0, this);
00062 
00063     m_impl->m_ocspResp = NULL;
00064 }
00072 CPKIFBasicOCSPResponse::~CPKIFBasicOCSPResponse(void)
00073 {
00074     LOG_STRING_DEBUG("CPKIFBasicOCSPResponse::~CPKIFBasicOCSPResponse(void)", TOOLKIT_OCSP_ASN, 0, this);
00075 
00076     if(NULL != m_impl->m_ocspResp)
00077         delete m_impl->m_ocspResp;
00078 
00079     delete m_impl;
00080     m_impl = NULL;
00081 }
00082 
00083 //tbsResponseData
00092 CPKIFResponseDataPtr CPKIFBasicOCSPResponse::GetResponseData() const
00093 {
00094     if(m_impl->m_responseData == (CPKIFResponseData*)NULL && NULL != m_impl->m_ocspResp && NULL != (*m_impl->m_ocspResp).data())
00095     {
00096         LOG_STRING_DEBUG("CPKIFBasicOCSPResponse::GetResponseData()", TOOLKIT_OCSP_ASN, 0, this);
00097 
00098         CPKIFBufferPtr buffPtr(new CPKIFBuffer((*m_impl->m_ocspResp)->tbsResponseData.data, (*m_impl->m_ocspResp)->tbsResponseData.numocts));
00099         CPKIFResponseData* tmp = new CPKIFResponseData(buffPtr);
00100         CPKIFResponseDataPtr tmpRef(tmp); 
00101 
00102         CPKIFBasicOCSPResponse* nonConst = const_cast<CPKIFBasicOCSPResponse*>(this);
00103         nonConst->m_impl->m_responseData = tmpRef;
00104     }
00105 
00106     return m_impl->m_responseData;
00107 }
00116 void CPKIFBasicOCSPResponse::SetResponseData(
00118     CPKIFResponseDataPtr& rd)
00119 {
00120     m_impl->m_responseData = rd;
00121 }
00122 
00123 //signatureAlg
00133 CPKIFAlgorithmIdentifierPtr CPKIFBasicOCSPResponse::GetSignatureAlg() const
00134 {
00135     if(m_impl->m_sigAlg == (CPKIFAlgorithmIdentifier*)NULL && NULL != m_impl->m_ocspResp && NULL != (*m_impl->m_ocspResp).data())
00136     {
00137         LOG_STRING_DEBUG("CPKIFBasicOCSPResponse::GetSignatureAlg()", TOOLKIT_OCSP_ASN, 0, this);
00138 
00139 
00140         CPKIFOIDPtr algOID(new CPKIFOID((*m_impl->m_ocspResp)->signatureAlgorithm.algorithm.subid,
00141                                     (*m_impl->m_ocspResp)->signatureAlgorithm.algorithm.numids));
00142         
00143         CPKIFBufferPtr paramBuf;
00144         if((*m_impl->m_ocspResp)->signatureAlgorithm.m.parametersPresent)
00145         {
00146             paramBuf = CPKIFBufferPtr(new CPKIFBuffer((*m_impl->m_ocspResp)->signatureAlgorithm.parameters.data,
00147                                 (*m_impl->m_ocspResp)->signatureAlgorithm.parameters.numocts));
00148         
00149         }
00150 
00151         CPKIFAlgorithmIdentifierPtr tmpRef(new CPKIFAlgorithmIdentifier(algOID, paramBuf)); 
00152 
00153         CPKIFBasicOCSPResponse* nonConst = const_cast<CPKIFBasicOCSPResponse*>(this);
00154         nonConst->m_impl->m_sigAlg = tmpRef;
00155     }
00156 
00157     return m_impl->m_sigAlg;
00158 }
00168 void CPKIFBasicOCSPResponse::SetSignatureAlg(
00170     CPKIFAlgorithmIdentifierPtr& sa)
00171 {
00172     m_impl->m_sigAlg = sa;
00173 }
00174 
00175 //signature
00184 CPKIFBufferPtr CPKIFBasicOCSPResponse::GetSignature() const
00185 {
00186     if(m_impl->m_signature == (CPKIFBuffer*)NULL && NULL != m_impl->m_ocspResp && NULL != (*m_impl->m_ocspResp).data())
00187     {
00188         LOG_STRING_DEBUG("CPKIFBasicOCSPResponse::GetSignature()", TOOLKIT_OCSP_ASN, 0, this);
00189 
00190         CPKIFBuffer* tmp = new CPKIFBuffer((*m_impl->m_ocspResp)->signature.data,(*m_impl->m_ocspResp)->signature.numbits/8); 
00191         CPKIFBufferPtr tmpRef(tmp); 
00192 
00193         CPKIFBasicOCSPResponse* nonConst = const_cast<CPKIFBasicOCSPResponse*>(this);
00194         nonConst->m_impl->m_signature = tmpRef;
00195     }
00196     return m_impl->m_signature;
00197 }
00206 void CPKIFBasicOCSPResponse::SetSignature(
00208     CPKIFBufferPtr& sig)
00209 {
00210     m_impl->m_signature = sig;
00211 }
00212 
00213 //certs
00221 void CPKIFBasicOCSPResponse::GetCerts(
00224     CPKIFCertificateList& certs)
00225 {
00226     copy(m_impl->m_certs.begin(), m_impl->m_certs.end(), back_inserter(certs));
00227 }
00238 void CPKIFBasicOCSPResponse::AddCert(
00240     CPKIFCertificatePtr& cert)
00241 {
00242     m_impl->m_certs.push_back(cert);
00243 }
00255 void CPKIFBasicOCSPResponse::Decode(
00257     const CPKIFBufferPtr& msg)
00258 {
00259     LOG_STRING_DEBUG("CPKIFBasicOCSPResponse::Decode(CPKIFBufferPtr& msg)", TOOLKIT_OCSP_ASN, 0, this);
00260 
00261     m_impl->ClearAllMembers();
00262 
00263     //if the input is empty - fail now
00264     if(msg == (CPKIFBuffer*)NULL || 0 == msg->GetLength())
00265     {
00266         throw CPKIFOCSPException(TOOLKIT_OCSP_ASN, COMMON_INVALID_INPUT);
00267     }
00268 
00269     m_impl->MakeOCSPResponse();
00270 
00271     try
00272     {
00273         //otherwise try to parse it into the m_tsr member
00274         (*m_impl->m_ocspResp).Decode(msg->GetBuffer(), msg->GetLength());
00275 
00276         if(0 < (*m_impl->m_ocspResp)->m.certsPresent)
00277         {
00278             DListNode* cur = (*m_impl->m_ocspResp)->certs.head;
00279             while(NULL != cur)
00280             {
00281                 ASN1OpenType* tmpNode = (ASN1OpenType*)cur->data;
00282                 CPKIFCertificatePtr tmpCert(new CPKIFCertificate());
00283                 tmpCert->Decode(tmpNode->data, tmpNode->numocts);
00284 
00285                 m_impl->m_certs.push_back(tmpCert);
00286         
00287                 cur = cur->next;
00288             }
00289         }
00290     }
00291     catch(CPKIFException&)
00292     {
00293         //delete e;
00294         //changed error code 3/2/2004
00295         throw CPKIFOCSPException(TOOLKIT_OCSP_ASN, ASN1_DECODE_ERROR);
00296     }
00297 }
00306 void CPKIFBasicOCSPResponseImpl::ClearAllMembers()
00307 {
00308     LOG_STRING_DEBUG("CPKIFBasicOCSPResponse::ClearAllMembers()", TOOLKIT_OCSP_ASN, 0, this);
00309 
00310     //added 8/23/2004
00311     CPKIFResponseDataPtr responseData;
00312     m_responseData = responseData;
00313     CPKIFAlgorithmIdentifierPtr sigAlg;
00314     m_sigAlg = sigAlg;
00315     CPKIFBufferPtr signature;
00316     m_signature = signature;
00317     m_certs.clear();
00318 
00319     FreeOCSPResponse();
00320 }
00331 void CPKIFBasicOCSPResponseImpl::MakeOCSPResponse()
00332 {
00333     LOG_STRING_DEBUG("CPKIFBasicOCSPResponse::MakeOCSPResponse()", TOOLKIT_OCSP_ASN, 0, this);
00334 
00335     FreeOCSPResponse();
00336     m_ocspResp = new CPKIFASNWrapper<BasicOCSPResponse>( BEREncBasicOCSPResponse, BERDecBasicOCSPResponse );
00337 }
00345 void CPKIFBasicOCSPResponseImpl::FreeOCSPResponse()
00346 {
00347     LOG_STRING_DEBUG("CPKIFBasicOCSPResponse::FreeOCSPResponse()", TOOLKIT_OCSP_ASN, 0, this);
00348 
00349     if(NULL != m_ocspResp)
00350         delete m_ocspResp;
00351     m_ocspResp = NULL;
00352 }

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