ESSCertId.cpp

Go to the documentation of this file.
00001 
00009 #include "ESSCertId.h"
00010 
00011 #include "Buffer.h"
00012 #include "Certificate.h"
00013 
00014 #include "ASN1Helper.h"
00015 #include "ToolkitUtils.h"
00016 #include "PKIFMessageException.h"
00017 
00018 #include "PKIFCryptUtils.h"
00019 #include "IPKIFCryptoMisc.h"
00020 #include "IPKIFHashContext.h"
00021 #include "PKIFAlgorithm.h"
00022 
00023 #include "ExtendedSecurityServices.h"
00024 
00026 struct CPKIFESSCertIdImpl
00027 {
00028     CPKIFBufferPtr m_certHash;
00029     char * m_issuerSerialNumber;
00030     CPKIFGeneralNameListPtr m_issuerName;
00031 };
00032 
00033 
00035 
00043 CPKIFESSCertId::CPKIFESSCertId()
00044     :m_impl (new CPKIFESSCertIdImpl)
00045 {
00046     LOG_STRING_DEBUG("CPKIFESSCertId::CPKIFESSCertId()", TOOLKIT_MESSAGE_ASN, 0, this);
00047     
00048     m_impl->m_issuerSerialNumber = NULL;
00049 }
00050 
00058 CPKIFESSCertId::CPKIFESSCertId( 
00060                                const CPKIFCertificatePtr & cert
00061                                )
00062     :m_impl (new CPKIFESSCertIdImpl)
00063 {
00064     LOG_STRING_DEBUG("CPKIFESSCertId::CPKIFESSCertId( const CPKIFCertificatePtr & cert )", TOOLKIT_MESSAGE_ASN, 0, this);
00065     m_impl->m_issuerSerialNumber = 0;
00066     SetCertificate(cert);
00067 }
00068 
00076 CPKIFESSCertId::CPKIFESSCertId(
00078     const CPKIFBufferPtr& buf)
00079     :m_impl (new CPKIFESSCertIdImpl)
00080 {
00081     LOG_STRING_DEBUG("CPKIFESSCertId::CPKIFESSCertId(const CACCMSAttribute& ext)", TOOLKIT_MESSAGE_ASN, 0, this);
00082     m_impl->m_issuerSerialNumber = 0;
00083     if(buf == (CPKIFBuffer*)NULL || 0 == buf->GetLength())
00084     {
00085         throw CPKIFMessageException(TOOLKIT_MESSAGE_ASN, COMMON_INVALID_INPUT, "NULL parameter passed to CPKIFESSCertId::CPKIFESSCertId");
00086     }
00087 
00088     CACASNWRAPPER_CREATE(CACCMSESSCertID, objPDU);
00089     objPDU.Decode(buf->GetBuffer(), buf->GetLength());
00090 
00091 
00092     CPKIFBufferPtr tmpCertHash(new CPKIFBuffer(objPDU->certHash.data, objPDU->certHash.numocts));
00093     m_impl->m_certHash = tmpCertHash;
00094 
00095 
00096     if(objPDU->m.issuerSerialPresent) {
00097         CPKIFGeneralNameListPtr tmpNameList(new CPKIFGeneralNameList());
00098         DListNode* cur = objPDU->issuerSerial.issuer.head;
00099         while(NULL != cur)
00100         {
00101             CACX509V3GeneralName* tmp = (CACX509V3GeneralName*)cur->data;
00102             CACASNWRAPPER_CREATE(CACX509V3GeneralName, objPDU);
00103             ASN1OpenType* data1 = objPDU.Encode(tmp);
00104             CPKIFBufferPtr tmpBuf(new CPKIFBuffer(data1->data, data1->numocts));
00105             CPKIFGeneralNamePtr tmpName(new CPKIFGeneralName(tmpBuf));
00106 
00107             if(NULL != data1)
00108                 delete data1;
00109 
00110             tmpNameList->push_back(tmpName);
00111             
00112             cur = cur->next;
00113         }
00114         m_impl->m_issuerName = tmpNameList;
00115 
00116         m_impl->m_issuerSerialNumber = strdup(objPDU->issuerSerial.serialNumber);
00117     }
00118 }
00119 
00127 CPKIFESSCertId::~CPKIFESSCertId()
00128 {
00129     LOG_STRING_DEBUG("CPKIFESSCertId::~CPKIFESSCertId()", TOOLKIT_MESSAGE_ASN, 0, this);
00130 
00131     // since it's always coppied in using strdup, it should be freed with free() instead of delete
00132     if(0 != m_impl->m_issuerSerialNumber)
00133         free(m_impl->m_issuerSerialNumber);
00134 
00135     delete m_impl;
00136     m_impl = NULL;
00137 }
00138 
00146 void CPKIFESSCertId::GetIssuerName(
00148     CPKIFGeneralNameListPtr& issuerName) const
00149 {
00150     issuerName = m_impl->m_issuerName;
00151 }
00152 
00160 const char * CPKIFESSCertId::GetSerialNumber() const
00161 {
00162     return m_impl->m_issuerSerialNumber;
00163 }
00164 
00172 const CPKIFBufferPtr CPKIFESSCertId::GetCertHash() const
00173 {
00174     return m_impl->m_certHash;
00175 }
00176 
00184 void CPKIFESSCertId::SetCertificate(
00186     const CPKIFCertificatePtr & cert)
00187 {
00188 
00189     // extract issuer
00190     CPKIFGeneralNameListPtr ignList(new CPKIFGeneralNameList());
00191     CPKIFGeneralNamePtr ign(new CPKIFGeneralName(cert->GetIssuerName()));
00192     ignList->push_back(ign);
00193     m_impl->m_issuerName = ignList;
00194 
00195     const char * sn = cert->SerialNumber();
00196     if(m_impl->m_issuerSerialNumber) {
00197         free(m_impl->m_issuerSerialNumber);
00198         m_impl->m_issuerSerialNumber = 0;
00199     }
00200     if(sn) m_impl->m_issuerSerialNumber = strdup(sn);
00201 
00202     IPKIFCryptoMisc * cm = GetPlatformCryptoMisc();
00203     // SHA1 is the only choice here because the structure does not accomodate a choice
00204     // of digests until ESSCertIdv2, which is incompatible
00205     CPKIFAlgorithm * alg = CPKIFAlgorithm::GetAlg(PKIFCRYPTO::SHA1);
00206     int hbOutLen = alg->DigestSize();
00207     IPKIFHashContext * hctx = cm->HashInit(alg->HashAlg());
00208     CPKIFBufferPtr certBuf = cert->Encoded();
00209     unsigned char * hbOut = new unsigned char[hbOutLen];
00210     cm->HashUpdate(hctx,const_cast<unsigned char *>(certBuf->GetBuffer()),certBuf->GetLength());
00211     cm->HashFinal(hctx,hbOut,&hbOutLen);
00212     CPKIFBufferPtr hbp(new CPKIFBuffer(true,hbOut,hbOutLen));
00213     m_impl->m_certHash = hbp;
00214 }

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