X509Extension.cpp
Go to the documentation of this file.00001
00009 #include "X509Extension.h"
00010 #include "OID.h"
00011 #include "Buffer.h"
00012
00013 #include "ASN1Helper.h"
00014 #include "PKIX1Implicit88.h"
00015 #include "PKIX1Explicit88.h"
00016
00017 #include "ToolkitUtils.h"
00018
00019 #include <iostream>
00020
00021 #include <boost/scoped_array.hpp>
00022
00024
00025 struct CPKIFX509ExtensionImpl
00026 {
00027 CPKIFX509ExtensionImpl()
00028 {
00029 m_criticality = false;
00030 }
00031 bool m_criticality;
00032 CPKIFOIDPtr m_oid;
00033 CPKIFBufferPtr m_value;
00034 };
00035
00037
00045 CPKIFX509Extension::CPKIFX509Extension()
00046 :m_impl (new CPKIFX509ExtensionImpl)
00047 {
00048 }
00056 CPKIFX509Extension::CPKIFX509Extension(const CPKIFX509Extension& refExtension)
00057 :m_impl (new CPKIFX509ExtensionImpl)
00058 {
00059 }
00060
00067 CPKIFX509Extension::CPKIFX509Extension (const bool& criticality, const CPKIFBufferPtr& value)
00068 :m_impl (new CPKIFX509ExtensionImpl)
00069 {
00070 m_impl->m_criticality = criticality;
00071 m_impl->m_value = value;
00072 }
00073
00080 CPKIFX509Extension::CPKIFX509Extension (const CPKIFOIDPtr& oid, const bool& criticality, const CPKIFBufferPtr& value)
00081 :m_impl (new CPKIFX509ExtensionImpl)
00082 {
00083 m_impl->m_oid = oid;
00084 m_impl->m_value = value;
00085 m_impl->m_criticality = criticality;
00086 }
00087
00095 CPKIFX509Extension::~CPKIFX509Extension()
00096 {
00097 if(m_impl){
00098 delete m_impl;
00099 m_impl = 0;
00100 }
00101 }
00102
00111 bool CPKIFX509Extension::operator==(
00113 const CPKIFX509Extension& rhs)
00114 {
00115 CPKIFOIDPtr lhsOID = oid();
00116 CPKIFOIDPtr rhsOID = rhs.oid();
00117 return *lhsOID == *rhsOID;
00118 }
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00148 bool CPKIFX509Extension::isCritical() const
00149 {
00150 return m_impl->m_criticality;
00151 }
00152
00156 void CPKIFX509Extension::SetCriticality(bool b)
00157 {
00158 m_impl->m_criticality = b;
00159 }
00160
00169 const CPKIFOIDPtr CPKIFX509Extension::oid() const
00170 {
00171 return m_impl->m_oid;
00172 }
00173
00181 CPKIFBufferPtr CPKIFX509Extension::value() const
00182 {
00183 return m_impl->m_value;
00184 }
00192 void CPKIFX509Extension::value(
00194 const CPKIFBufferPtr &value)
00195 {
00196 m_impl->m_value = value;
00197 }
00205 CPKIFBufferPtr CPKIFX509Extension::Encode()
00206 {
00207 CACASNWRAPPER_CREATE(CACX509V3Extension, objPDU);
00208 CACX509V3Extension ext;
00209 ext.critical = isCritical();
00210 CPKIFStringPtr str(new std::string(oid()->ToString()));
00211 ASN1OBJID* tmpOid = ConvertStringToASN1OBJID(str);
00212 ext.extnID = *tmpOid;
00213
00214 CPKIFBufferPtr extValue = value();
00215 ext.extnValue.numocts = extValue->GetLength();
00216 ext.extnValue.data = new unsigned char[extValue->GetLength()];
00217 memcpy((void*)ext.extnValue.data, extValue->GetBuffer(), extValue->GetLength());
00218
00219 ASN1OpenType* extEncoded = NULL;
00220
00221 try
00222 {
00223 extEncoded = objPDU.Encode(&ext);
00224 }
00225 catch(CPKIFException& e)
00226 {
00227 if(tmpOid)
00228 delete tmpOid;
00229
00230 if(extEncoded)
00231 delete extEncoded;
00232
00233 if(ext.extnValue.data)
00234 delete[] ext.extnValue.data;
00235
00236 throw e;
00237 }
00238
00239
00240 CPKIFBufferPtr rv(new CPKIFBuffer(extEncoded->data, extEncoded->numocts));
00241
00242 if(tmpOid)
00243 delete tmpOid;
00244
00245 if(extEncoded)
00246 delete extEncoded;
00247
00248 if(ext.extnValue.data)
00249 delete[] ext.extnValue.data;
00250
00251 return rv;
00252 }
00253
00255 CAC_API std::ostream& operator<<(std::ostream & os, const CPKIFX509ExtensionPtr & extension)
00256 {
00257 return operator<<(os,*extension);
00258 }
00259
00261 CAC_API std::ostream& operator<<(std::ostream & os, const CPKIFX509Extension & extension)
00262 {
00263
00264 CPKIFBufferPtr encoded = extension.value();
00265 boost::scoped_array<char> buf(new char[2*encoded->GetLength()+1]);
00266 btoa((char*)encoded->GetBuffer(), buf.get(), encoded->GetLength());
00267 return os << buf.get();
00268 }