ContentTypeAttribute.cpp
Go to the documentation of this file.00001
00009 #include "ContentTypeAttribute.h"
00010
00011 #include "OID.h"
00012 #include "Buffer.h"
00013
00014 #include "CryptographicMessageSyntax2004.h"
00015
00016 #include "ASN1Helper.h"
00017 #include "ToolkitUtils.h"
00018
00019 #include "PKIFMessageException.h"
00020
00021
00023 struct CPKIFContentTypeAttributeImpl
00024 {
00025 CPKIFContentTypeAttribute *m_parent;
00026
00027 CPKIFOIDPtr m_contentType;
00028 void ParseAttributes (const unsigned char *buf, const unsigned int length);
00029 };
00037 void CPKIFContentTypeAttributeImpl::ParseAttributes (
00039 const unsigned char *buf,
00041 const unsigned int length)
00042 {
00043 CACASNWRAPPER_CREATE(CACCMSAttributeValues, attrsVals);
00044 CACCMSAttributeValues* attributesList = attrsVals.Decode(buf, length);
00045
00046
00047 OOCTXT ctxt;
00048 initContext (&ctxt);
00049
00050 ASN1OpenType* ot = (ASN1OpenType*)((DListNode*)attributesList->head)->data;
00051 setBERDecBufPtr(&ctxt, ot->data, ot->numocts, NULL, NULL);
00052
00053 ASN1OBJID o;
00054
00055
00056 int stat = decodeOID (&ctxt, &o, ASN1EXPL, ot->numocts);
00057 if(0 != stat)
00058 {
00059
00060
00061
00062 freeContext(&ctxt);
00063
00064 throw CPKIFException(TOOLKIT_MESSAGE, ASN1_DECODE_ERROR);
00065 }
00066 else
00067 {
00068 try
00069 {
00070
00071 CPKIFOIDPtr tmp(new CPKIFOID(o.subid,o.numids));
00072 m_parent->SetContentType(tmp);
00073 }
00074 catch(std::bad_alloc& ba)
00075 {
00076
00077
00078 freeContext(&ctxt);
00079
00080 throw ba;
00081 }
00082
00083
00084
00085 freeContext(&ctxt);
00086 }
00087 }
00088
00090
00091 char CPKIFContentTypeAttribute::extOID[] = "1.2.840.113549.1.9.3";
00100 CPKIFContentTypeAttribute::CPKIFContentTypeAttribute()
00101 :m_impl (new CPKIFContentTypeAttributeImpl)
00102 {
00103 m_impl->m_parent = this;
00104 LOG_STRING_DEBUG("CPKIFContentTypeAttribute::CPKIFContentTypeAttribute()", TOOLKIT_MESSAGE_ASN, 0, this);
00105 }
00118
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 CPKIFContentTypeAttribute::CPKIFContentTypeAttribute(
00131 const CPKIFBufferPtr& buf)
00132 :m_impl (new CPKIFContentTypeAttributeImpl)
00133 {
00134 LOG_STRING_DEBUG("CPKIFContentTypeAttribute::CPKIFContentTypeAttribute(const CACCMSAttribute& ext)", TOOLKIT_MESSAGE_ASN, 0, this);
00135
00136 m_impl->m_parent = this;
00137 m_impl->ParseAttributes (buf->GetBuffer(), buf->GetLength());
00138 }
00139
00140
00148 CPKIFContentTypeAttribute::~CPKIFContentTypeAttribute()
00149 {
00150 LOG_STRING_DEBUG("CPKIFContentTypeAttribute::~CPKIFContentTypeAttribute()", TOOLKIT_MESSAGE_ASN, 0, this);
00151
00152 delete m_impl;
00153 m_impl = NULL;
00154 }
00155
00156
00164 CPKIFOIDPtr CPKIFContentTypeAttribute::GetOID() const
00165 {
00166 return g_contentTypeAttribute;
00167 }
00177 void CPKIFContentTypeAttribute::GetValues(
00179 CPKIFBufferList& values) const
00180 {
00181 LOG_STRING_DEBUG("CPKIFContentTypeAttribute::GetValues(CPKIFBufferList& values)", TOOLKIT_MESSAGE_ASN, 0, this);
00182
00183 values.clear();
00184
00185
00186 OOCTXT ctxt;
00187 initContext (&ctxt);
00188 setBEREncBufPtr(&ctxt, NULL, 0);
00189
00190 CPKIFStringPtr str(new std::string(m_impl->m_contentType->ToString()));
00191 ASN1OBJID* tmpOid = ConvertStringToASN1OBJID(str);
00192
00193 int len = encodeOID (&ctxt, tmpOid, ASN1EXPL);
00194 if(tmpOid)
00195 delete tmpOid;
00196 OSOCTET* buf = getBEREncBufPtr(&ctxt);
00197 if(0 < len && NULL != buf)
00198 {
00199
00200 try
00201 {
00202 CPKIFBufferPtr tmp(new CPKIFBuffer(buf, len));
00203 values.push_back(tmp);
00204 }
00205 catch(std::bad_alloc& ba)
00206 {
00207
00208
00209 freeContext(&ctxt);
00210
00211 throw ba;
00212 }
00213
00214
00215
00216 freeContext(&ctxt);
00217 }
00218 else
00219 {
00220
00221
00222
00223 freeContext(&ctxt);
00224 throw CPKIFException(TOOLKIT_MESSAGE, ASN1_ENCODE_ERROR);
00225 }
00226 }
00227
00228
00238 void CPKIFContentTypeAttribute::SetContentType(
00240 CPKIFOIDPtr& oid)
00241 {
00242 LOG_STRING_DEBUG("CPKIFContentTypeAttribute::SetContentType(CPKIFOIDPtr& oid)", TOOLKIT_MESSAGE_ASN, 0, this);
00243
00244
00245 if(oid == (CPKIFOID*)NULL)
00246 throw CPKIFMessageException(TOOLKIT_MESSAGE, COMMON_INVALID_INPUT);
00247
00248 m_impl->m_contentType = oid;
00249 }
00257 CPKIFOIDPtr CPKIFContentTypeAttribute::GetContentType()
00258 {
00259 return m_impl->m_contentType;
00260 }