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     //set up an ASN.1 context to parse the OID from the attribute
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     //parse the OID
00056     int stat = decodeOID (&ctxt, &o, ASN1EXPL, ot->numocts);
00057     if(0 != stat)
00058     {
00059         //upon failure - clean up and throw an exception
00060         //freeEncodeBuffer(&ctxt);
00061         //memFreeAll(&ctxt);
00062         freeContext(&ctxt);
00063 
00064         throw CPKIFException(TOOLKIT_MESSAGE, ASN1_DECODE_ERROR);
00065     }
00066     else
00067     {
00068         try
00069         {
00070             //otherwise set the value then clean up
00071             CPKIFOIDPtr tmp(new CPKIFOID(o.subid,o.numids));
00072             m_parent->SetContentType(tmp);
00073         }
00074         catch(std::bad_alloc& ba)
00075         {
00076             //freeEncodeBuffer(&ctxt);
00077             //memFreeAll(&ctxt);
00078             freeContext(&ctxt);
00079 
00080             throw ba;
00081         }
00082 
00083         //freeEncodeBuffer(&ctxt);
00084         //memFreeAll(&ctxt);
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 /*CPKIFContentTypeAttribute::CPKIFContentTypeAttribute(
00120     const CACCMSAttribute& ext)
00121     :m_impl (new CPKIFContentTypeAttributeImpl)
00122 {
00123     LOG_STRING_DEBUG("CPKIFContentTypeAttribute::CPKIFContentTypeAttribute(const CACCMSAttribute& ext)", TOOLKIT_MESSAGE_ASN, 0, this);
00124 
00125     m_impl->m_parent = this;
00126     m_impl->ParseAttributes (ext.attrValues.data, ext.attrValues.numocts);
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 //generic attribute functions
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     //set up an ASN.1 context to encode the OID value
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     //int len = encodeOID (&ctxt, m_impl->m_contentType->raw(), ASN1EXPL);
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         //success
00200         try
00201         {
00202             CPKIFBufferPtr tmp(new CPKIFBuffer(buf, len));
00203             values.push_back(tmp);
00204         }
00205         catch(std::bad_alloc& ba)
00206         {
00207             //freeEncodeBuffer(&ctxt);
00208             //memFreeAll(&ctxt);
00209             freeContext(&ctxt);
00210 
00211             throw ba;
00212         }
00213 
00214         //freeEncodeBuffer(&ctxt);
00215         //memFreeAll(&ctxt);
00216         freeContext(&ctxt);
00217     }
00218     else
00219     {
00220         //failure - clean up and throw an exception
00221         //freeEncodeBuffer(&ctxt);
00222         //memFreeAll(&ctxt);
00223         freeContext(&ctxt);
00224         throw CPKIFException(TOOLKIT_MESSAGE, ASN1_ENCODE_ERROR);
00225     }
00226 }
00227 
00228 //content type specific functions
00238 void CPKIFContentTypeAttribute::SetContentType(
00240     CPKIFOIDPtr& oid)
00241 {
00242     LOG_STRING_DEBUG("CPKIFContentTypeAttribute::SetContentType(CPKIFOIDPtr& oid)", TOOLKIT_MESSAGE_ASN, 0, this);
00243 
00244     //accept no NULLs
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 }

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