MessageDigestAttribute.cpp
Go to the documentation of this file.00001
00009 #include "MessageDigestAttribute.h"
00010
00011 #include "ASN1Helper.h"
00012 #include "CryptographicMessageSyntax2004.h"
00013 #include "Buffer.h"
00014 #include "ToolkitUtils.h"
00015 #include "PKIFMessageException.h"
00016
00018 struct CPKIFMessageDigestAttributeImpl
00019 {
00020 CPKIFMessageDigestAttribute *m_parent;
00021
00022 CPKIFBufferPtr m_messageDigest;
00023 void ParseAttributes (const unsigned char *buf, const unsigned int length);
00024 };
00032 void CPKIFMessageDigestAttributeImpl::ParseAttributes (
00034 const unsigned char *buf,
00036 const unsigned int length)
00037 {
00038 CACASNWRAPPER_CREATE(CACCMSAttributeValues, attrsVals);
00039 CACCMSAttributeValues* attributesList = attrsVals.Decode(buf, length);
00040
00041 OOCTXT ctxt;
00042 initContext (&ctxt);
00043 ASN1OpenType* ot = (ASN1OpenType*)((DListNode*)attributesList->head)->data;
00044 setBERDecBufPtr(&ctxt, ot->data, ot->numocts, NULL, NULL);
00045
00046 unsigned char* iv = NULL;
00047 unsigned int ivLen = ot->numocts;
00048
00049 int stat = decodeDynOctStr (&ctxt, (const OSOCTET* *)&iv, (OSUINT32 *)&ivLen, ASN1EXPL, ot->numocts);
00050 if(0 != stat)
00051 {
00052
00053
00054
00055 freeContext(&ctxt);
00056
00057 throw CPKIFException(TOOLKIT_MESSAGE, ASN1_DECODE_ERROR);
00058 }
00059 else
00060 {
00061 try
00062 {
00063 CPKIFBufferPtr tmp(new CPKIFBuffer(iv, ivLen));
00064 m_parent->SetMessageDigest(tmp);
00065 }
00066 catch(std::bad_alloc& ba)
00067 {
00068
00069
00070 freeContext(&ctxt);
00071
00072 throw ba;
00073 }
00074
00075
00076
00077
00078 freeContext(&ctxt);
00079 }
00080 }
00081
00082
00084
00085 char CPKIFMessageDigestAttribute::extOID[] = "1.2.840.113549.1.9.4";
00093 CPKIFMessageDigestAttribute::CPKIFMessageDigestAttribute()
00094 :m_impl (new CPKIFMessageDigestAttributeImpl)
00095 {
00096 LOG_STRING_DEBUG("CPKIFMessageDigestAttribute::CPKIFMessageDigestAttribute()", TOOLKIT_MESSAGE_ASN, 0, this);
00097
00098 m_impl->m_parent = this;
00099 }
00109
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00130 CPKIFMessageDigestAttribute::CPKIFMessageDigestAttribute(
00132 const CPKIFBufferPtr& buf)
00133 :m_impl (new CPKIFMessageDigestAttributeImpl)
00134 {
00135 LOG_STRING_DEBUG("CPKIFMessageDigestAttribute::CPKIFMessageDigestAttribute(const CACCMSAttribute& ext)", TOOLKIT_MESSAGE_ASN, 0, this);
00136
00137 m_impl->m_parent = this;
00138 m_impl->ParseAttributes (buf->GetBuffer(), buf->GetLength());
00139 }
00140
00148 CPKIFMessageDigestAttribute::~CPKIFMessageDigestAttribute()
00149 {
00150 LOG_STRING_DEBUG("CPKIFMessageDigestAttribute::~CPKIFMessageDigestAttribute()", TOOLKIT_MESSAGE_ASN, 0, this);
00151
00152 delete m_impl;
00153 m_impl = NULL;
00154 }
00155
00156
00164 CPKIFOIDPtr CPKIFMessageDigestAttribute::GetOID() const
00165 {
00166 return g_messageDigestAttribute;
00167 }
00177 void CPKIFMessageDigestAttribute::GetValues(
00179 CPKIFBufferList& values) const
00180 {
00181 LOG_STRING_DEBUG("CPKIFMessageDigestAttribute::GetValues(CPKIFBufferList& values)", TOOLKIT_MESSAGE_ASN, 0, this);
00182
00183 values.clear();
00184
00185 OOCTXT ctxt;
00186 initContext (&ctxt);
00187 setBEREncBufPtr(&ctxt, NULL, 0);
00188 OOCTXT* pctxt = &ctxt;
00189
00190 if(m_impl->m_messageDigest != (CPKIFBuffer*)NULL)
00191 {
00192 int len = encodeOctStr (pctxt, m_impl->m_messageDigest->GetBuffer(), m_impl->m_messageDigest->GetLength(), ASN1EXPL);
00193 OSOCTET* buf = getBEREncBufPtr(pctxt);
00194 if(0 < len && NULL != buf)
00195 {
00196
00197 try
00198 {
00199 CPKIFBufferPtr tmp(new CPKIFBuffer(buf, len));
00200 values.push_back(tmp);
00201 }
00202 catch(std::bad_alloc& ba)
00203 {
00204
00205
00206 freeContext(&ctxt);
00207
00208 throw ba;
00209 }
00210
00211
00212
00213 freeContext(&ctxt);
00214 }
00215 else
00216 {
00217
00218
00219
00220 freeContext(&ctxt);
00221 throw CPKIFException(TOOLKIT_MESSAGE, ASN1_ENCODE_ERROR);
00222 }
00223 }
00224 }
00234 void CPKIFMessageDigestAttribute::SetMessageDigest(
00236 CPKIFBufferPtr& md)
00237 {
00238 LOG_STRING_DEBUG("CPKIFMessageDigestAttribute::SetMessageDigest(CPKIFBufferPtr& md)", TOOLKIT_MESSAGE_ASN, 0, this);
00239
00240
00241 if(md == (CPKIFBuffer*)NULL)
00242 throw CPKIFMessageException(TOOLKIT_MESSAGE, COMMON_INVALID_INPUT);
00243
00244 m_impl->m_messageDigest = md;
00245 }
00253 CPKIFBufferPtr CPKIFMessageDigestAttribute::GetMessageDigest()
00254 {
00255 return m_impl->m_messageDigest;
00256 }