SCVPCertID.cpp

Go to the documentation of this file.
00001 
00010 #include "Certificate.h"
00011 #include "PKIFMediators.h"
00012 #include "SCVPCertID.h"
00013 #include "PKIFSCVPErrors.h"
00014 #include "SCVPException.h"
00015 #include "ToolkitUtils.h"
00016 #include "CPKIFCryptoMediator2.h"
00017 #include "IPKIFHashContext.h"
00018 #include "SCVP.h"
00019 #include "ASN1Helper.h"
00020 #include "AlgorithmIdentifier.h"
00021 
00022 using namespace PKIFCRYPTO;
00024 
00025 struct CPKIFSCVPCertIDImpl 
00026 {
00027     CPKIFCertificatePtr m_issuer;
00028     IPKIFMediatorPtr m_mediator;
00029     CPKIFBufferPtr m_certHash;
00030     const char * m_issuerSerialNumber;
00031     CPKIFGeneralNameListPtr m_issuerName;
00032     CPKIFAlgorithmIdentifierPtr m_HashAlgID;
00033 };
00034 
00036 
00044 CPKIFSCVPCertID::CPKIFSCVPCertID() :m_impl(new CPKIFSCVPCertIDImpl)
00045 {
00046     LOG_STRING_DEBUG("CPKIFSCVPCertID::CPKIFSCVPCertID()", TOOLKIT_SCVP_ASN, 0, this);
00047 
00048 }
00059 CPKIFSCVPCertID::CPKIFSCVPCertID(
00061     const CPKIFBufferPtr& pkcRef)
00062   :m_impl (new CPKIFSCVPCertIDImpl)
00063 {
00064     if(pkcRef == (CPKIFBuffer*)NULL || 0 == pkcRef->GetLength())
00065     {
00066         throw CPKIFSCVPException(TOOLKIT_SCVP_ASN, COMMON_INVALID_INPUT);
00067     }
00068 
00069     CACASNWRAPPER_CREATE(SCVPCertID, objPDU);
00070     objPDU.Decode(pkcRef->GetBuffer(), pkcRef->GetLength());
00071 
00072 
00073     CPKIFBufferPtr tmpCertHash(new CPKIFBuffer(objPDU->certHash.data, objPDU->certHash.numocts));
00074     m_impl->m_certHash = tmpCertHash;
00075 
00076     CPKIFOIDPtr algOID(new CPKIFOID(objPDU->hashAlgorithm.algorithm.subid, 
00077                 objPDU->hashAlgorithm.algorithm.numids));
00078 
00079     CPKIFBufferPtr algParams; 
00080     if(objPDU->hashAlgorithm.m.parametersPresent == 1)
00081     {
00082         CPKIFBufferPtr tmpAlgParams(new CPKIFBuffer(objPDU->hashAlgorithm.parameters.data,
00083             objPDU->hashAlgorithm.parameters.numocts));
00084 
00085         algParams = tmpAlgParams;
00086     }
00087     CPKIFAlgorithmIdentifierPtr hashAlg(new CPKIFAlgorithmIdentifier(algOID, algParams));
00088 
00089     m_impl->m_HashAlgID = hashAlg;
00090 
00091 
00092     CPKIFGeneralNameListPtr tmpNameList(new CPKIFGeneralNameList());
00093     DListNode* cur = objPDU->issuerSerial.issuer.head;
00094     while(NULL != cur)
00095     {
00096         CACX509V3GeneralName* tmp = (CACX509V3GeneralName*)cur->data;
00097         CACASNWRAPPER_CREATE(CACX509V3GeneralName, objPDU);
00098         ASN1OpenType* data1 = objPDU.Encode(tmp);
00099         CPKIFBufferPtr tmpBuf(new CPKIFBuffer(data1->data, data1->numocts));
00100         CPKIFGeneralNamePtr tmpName(new CPKIFGeneralName(tmpBuf));
00101 
00102         if(NULL != data1)
00103             delete data1;
00104 
00105         m_impl->m_issuerName->push_back(tmpName);
00106 
00107         cur = cur->next;
00108     }
00109 
00110     m_impl->m_issuerSerialNumber = strdup(objPDU->issuerSerial.serialNumber);
00111 }
00119 CPKIFSCVPCertID::~CPKIFSCVPCertID()
00120 {
00121     LOG_STRING_DEBUG("CPKIFSCVPCertID::~CPKIFSCVPCertID()", TOOLKIT_SCVP_ASN, 0, this);
00122 
00123     if (m_impl) {
00124       delete m_impl;
00125     }   
00126 }
00134 void CPKIFSCVPCertID::SetIssuerName(
00136     CPKIFGeneralNameListPtr& issuerName)
00137 {
00138     m_impl->m_issuerName = issuerName;
00139 }
00147 void CPKIFSCVPCertID::GetIssuerName(
00149     CPKIFGeneralNameListPtr& issuerName) const
00150 {
00151     issuerName = m_impl->m_issuerName;
00152 }
00153 
00161  void CPKIFSCVPCertID::SetSerialNumber(
00163      const char * issuerSerialNumber)
00164 {
00165     m_impl->m_issuerSerialNumber = issuerSerialNumber;
00166 }
00167 
00175 const char * CPKIFSCVPCertID::GetSerialNumber() const
00176 {
00177     return m_impl->m_issuerSerialNumber;
00178 }
00186 const CPKIFBufferPtr CPKIFSCVPCertID::GetCertHash() const
00187 {
00188     return m_impl->m_certHash;
00189 }
00197 void CPKIFSCVPCertID::CalculateCertHash(
00199     CPKIFBufferPtr& cert)
00200 {
00201     if(m_impl->m_mediator == (IPKIFMediator*)NULL)
00202     {
00203         throw CPKIFSCVPException(TOOLKIT_SCVP, SCVP_MISSING_MEDIATOR, "mediator not specified.");
00204     }
00205     CPKIFCryptoMediator2 *cm = m_impl->m_mediator->GetMediator<CPKIFCryptoMediator2>();
00206 
00207     char hashResult[MAXHASH];
00208     int nResultLen = MAXHASH;
00209 
00210     int length = cert->GetLength();
00211 
00212     IPKIFHashContext* hash = NULL;
00213     try
00214     {
00215         //create a hash object, pass the buffer and length we calculated and obtain the result
00216         HASH_ALG hashAlg = SHA1;
00217         if(m_impl->m_HashAlgID != (CPKIFAlgorithmIdentifier*)NULL)
00218         {
00219             if(*(m_impl->m_HashAlgID->oid()) == *g_md5)
00220             {
00221                 hashAlg = MD5;
00222             }
00223             else if(*(m_impl->m_HashAlgID->oid()) == *g_sha256)
00224             {
00225                 hashAlg = SHA256;
00226             }
00227             else if(*(m_impl->m_HashAlgID->oid()) == *g_sha384)
00228             {
00229                 hashAlg = SHA384;
00230             }
00231             else if(*(m_impl->m_HashAlgID->oid()) == *g_sha512)
00232             {
00233                 hashAlg = SHA512;
00234             }
00235 
00236         }
00237         hash = cm->HashInit(hashAlg);
00238         cm->HashUpdate(hash, (unsigned char*)cert->GetBuffer(), length);
00239         cm->HashFinal(hash, (unsigned char*)hashResult, &nResultLen);
00240     }
00241     catch(CPKIFException& e)
00242     {
00243         if(NULL != hash)
00244             delete hash;
00245 
00246         throw e;
00247     }
00248 
00249     CPKIFBufferPtr tmp(new CPKIFBuffer((unsigned char*)hashResult, nResultLen));
00250     m_impl->m_certHash = tmp;
00251     //clean up the hash
00252     if(NULL != hash)
00253         delete hash; 
00254 }
00262 void CPKIFSCVPCertID::SetHashAlgorithm(CPKIFAlgorithmIdentifierPtr& hashAlg)
00263 {
00264     m_impl->m_HashAlgID = hashAlg;
00265 }
00273 const CPKIFAlgorithmIdentifierPtr CPKIFSCVPCertID::GetHashAlgorithm() const
00274 {
00275     return m_impl->m_HashAlgID;
00276 }
00277 
00285 void CPKIFSCVPCertID::SetMediator(IPKIFMediatorPtr &med)
00286 {
00287     m_impl->m_mediator = med;
00288 }

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