MessageImprint.cpp
Go to the documentation of this file.00001
00010 #include "MessageImprint.h"
00011 #include "PKIFTSP.h"
00012
00013 #ifdef _WIN32
00014 #include "PKIFCAPIRaw.h"
00015 #endif
00016
00017 #include "ASN1Helper.h"
00018 #include "ToolkitUtils.h"
00019 #include "PKIXTSP.h"
00020 #include "Buffer.h"
00021 #include "AlgorithmIdentifier.h"
00022 #include "PKIFMediators.h"
00023 #include "IPKIFHashContext.h"
00024 #include "IPKIFCryptoMisc.h"
00025 #include "PKIFCryptUtils.h"
00026
00028
00029 struct CPKIFMessageImprintImpl
00030 {
00031 CPKIFAlgorithmIdentifierPtr m_hashAlgorithm;
00032 CPKIFBufferPtr m_hashedMessage;
00033 };
00034
00036
00044 CPKIFMessageImprint::CPKIFMessageImprint() :m_impl(new CPKIFMessageImprintImpl)
00045 {
00046 LOG_STRING_DEBUG("CPKIFMessageImprint::CPKIFMessageImprint()", TOOLKIT_TSP_ASN, 0, this);
00047
00048 m_impl->m_hashAlgorithm = g_sha1AI;
00049 }
00057 CPKIFMessageImprint::CPKIFMessageImprint(
00060
00061 const CPKIFBufferPtr& miBuf)
00062 :m_impl (new CPKIFMessageImprintImpl)
00063 {
00064 LOG_STRING_DEBUG("CPKIFMessageImprint::CPKIFMessageImprint(PKIFTSPMessageImprint& mi)", TOOLKIT_TSP_ASN, 0, this);
00065
00066 CACASNWRAPPER_CREATE(PKIFTSPMessageImprint, MsgWrapper);
00067 PKIFTSPMessageImprint* mi = MsgWrapper.Decode(miBuf->GetBuffer(), miBuf->GetLength());
00068
00069
00070 CPKIFOIDPtr algOID(new CPKIFOID(mi->hashAlgorithm.algorithm.subid, mi->hashAlgorithm.algorithm.numids));
00071
00072 CPKIFBufferPtr paramBuf;
00073 if(mi->hashAlgorithm.m.parametersPresent)
00074 {
00075 paramBuf = CPKIFBufferPtr(new CPKIFBuffer(mi->hashAlgorithm.parameters.data, mi->hashAlgorithm.parameters.numocts));
00076
00077
00078 }
00079
00080 CPKIFAlgorithmIdentifierPtr ai(new CPKIFAlgorithmIdentifier(algOID, paramBuf));
00081
00082 m_impl->m_hashAlgorithm = ai;
00083
00084 CPKIFBufferPtr bp(new CPKIFBuffer(mi->hashedMessage.data, mi->hashedMessage.numocts));
00085 m_impl->m_hashedMessage = bp;
00086 }
00094 CPKIFMessageImprint::~CPKIFMessageImprint()
00095 {
00096 LOG_STRING_DEBUG("CPKIFMessageImprint::~CPKIFMessageImprint()", TOOLKIT_TSP_ASN, 0, this);
00097
00098 if (m_impl) {
00099 delete m_impl;
00100 }
00101 }
00109 void CPKIFMessageImprint::SetHashAlgorithm(
00112 CPKIFAlgorithmIdentifierPtr& hashAlgorithm)
00113 {
00114 m_impl->m_hashAlgorithm = hashAlgorithm;
00115 }
00123 CPKIFAlgorithmIdentifierPtr CPKIFMessageImprint::GetHashAlgorithm() const
00124 {
00125 return m_impl->m_hashAlgorithm;
00126 }
00134 void CPKIFMessageImprint::SetHashedMessage(
00137 CPKIFBufferPtr& hashedMessage)
00138 {
00139 m_impl->m_hashedMessage = hashedMessage;
00140 }
00148 CPKIFBufferPtr CPKIFMessageImprint::GetHashedMessage() const
00149 {
00150 return m_impl->m_hashedMessage;
00151 }
00179 CPKIFBufferPtr CPKIFMessageImprint::HashAndSet(
00182 PKIFCRYPTO::HASH_ALG hashAlgorithm,
00184 CPKIFBufferPtr& dataToHash,
00187 IPKIFMediator* m)
00188 {
00189 LOG_STRING_DEBUG("CPKIFMessageImprint::HashAndSet(HASH_ALG hashAlgorithm, CPKIFBufferPtr& dataToHash, IPKIFMediator* m)", TOOLKIT_TSP_ASN, 0, this);
00190
00191 IPKIFCryptoMisc* iMisc = NULL;
00192 if(NULL != m)
00193 iMisc = m->GetMediator<IPKIFCryptoMisc>();
00194
00195 unsigned char pResult[MAXHASH];
00196 int nResultLen = MAXHASH;
00197
00198 if(NULL != iMisc)
00199 {
00200
00201
00202 IPKIFHashContext* hash = iMisc->HashInit(hashAlgorithm);
00203 iMisc->HashUpdate(hash, (unsigned char*)dataToHash->GetBuffer(), dataToHash->GetLength());
00204 iMisc->HashFinal(hash, pResult, &nResultLen);
00205 delete hash;
00206 }
00207 else
00208 {
00209
00210
00211 IPKIFCryptoMisc* raw = GetPlatformCryptoMisc();
00212 IPKIFHashContext* hash = raw->HashInit(hashAlgorithm);
00213 raw->HashUpdate(hash, (unsigned char*)dataToHash->GetBuffer(), dataToHash->GetLength());
00214 raw->HashFinal(hash, pResult, &nResultLen);
00215 delete hash;
00216
00217
00218
00219
00220
00221
00222
00223 }
00224
00225 CPKIFBufferPtr buf(new CPKIFBuffer(pResult, nResultLen));
00226 SetHashedMessage(buf);
00227
00228 CPKIFAlgorithmIdentifierPtr ai = GetHashAlgAI(hashAlgorithm);
00229 SetHashAlgorithm(ai);
00230
00231 return buf;
00232 }