AuthorityKeyIdentifier.cpp

Go to the documentation of this file.
00001 
00009 #include "AuthorityKeyIdentifier.h"
00010 #include "OID.h"
00011 #include "Buffer.h"
00012 #include "GeneralName.h"
00013 
00014 #include "ASN1Helper.h"
00015 #include "PKIX1Implicit88.h"
00016 #include "PKIX1Explicit88.h"
00017 #include "ToolkitUtils.h"
00018 
00019 #include <boost/scoped_array.hpp>
00020 #include <boost/algorithm/string/replace.hpp>
00021 #include <boost/algorithm/string/find_iterator.hpp>
00022 #include <iostream>
00023 #include <sstream>
00024 
00025 using namespace boost::algorithm;
00026 using namespace std;
00027 
00029 
00030 struct CPKIFAuthorityKeyIdentifierImpl
00031 {
00032     CPKIFGeneralNameList m_issuer;
00033     CPKIFStringPtr m_serialNumber;
00034     CPKIFBufferPtr m_keyID;
00035     CPKIFBufferPtr m_value;
00036     bool m_extModified;
00037 };
00038 
00040 
00041 char CPKIFAuthorityKeyIdentifier::extOID[] =    "2.5.29.35";
00042 
00050 CPKIFAuthorityKeyIdentifier::CPKIFAuthorityKeyIdentifier()
00051   : m_impl (new CPKIFAuthorityKeyIdentifierImpl)
00052 {
00053 }
00054 
00065 //CPKIFAuthorityKeyIdentifier::CPKIFAuthorityKeyIdentifier(
00066 //  //! [in] Reference to an internal structure representation of the extension containing 
00067 //  //! information used to construct the object
00068 //  const CACX509V3Extension& ext)
00069 //  : CPKIFX509Extension(ext), m_impl (new CPKIFAuthorityKeyIdentifierImpl)
00070 //{
00071 //  CACASNWRAPPER_CREATE(CACX509V3AuthorityKeyIdentifier, objPDU);
00072 //  objPDU.Decode(ext.extnValue.data, ext.extnValue.numocts);
00073 //
00074 //  if(objPDU->m.authorityCertIssuerPresent)
00075 //  {
00076 //      DListNode* cur = objPDU->authorityCertIssuer.head;
00077 //      for(unsigned int ii = 0; ii < objPDU->authorityCertIssuer.count; ++ii)
00078 //      {
00079 //          CPKIFGeneralNamePtr tmpGN(new CPKIFGeneralName(*(CACX509V3GeneralName*)cur->data));
00080 //          m_impl->m_issuer.push_back(tmpGN);
00081 //
00082 //          cur = cur->next;
00083 //      }
00084 //  }
00085 //
00086 //  if(objPDU->m.authorityCertSerialNumberPresent)
00087 //  {
00088 //      CPKIFStringPtr tmpStr(new std::string(objPDU->authorityCertSerialNumber));
00089 //      m_impl->m_serialNumber = tmpStr;
00090 //  }
00091 //
00092 //  if(objPDU->m.keyIdentifierPresent)
00093 //  {
00094 //      CPKIFBufferPtr tmpBP(new CPKIFBuffer(objPDU->keyIdentifier.data, objPDU->keyIdentifier.numocts));
00095 //      m_impl->m_keyID = tmpBP;
00096 //  }
00097 //}
00098 
00109 CPKIFAuthorityKeyIdentifier::CPKIFAuthorityKeyIdentifier(
00110     const bool& criticality, const CPKIFBufferPtr& ext)
00111   : CPKIFX509Extension (criticality, ext), m_impl (new CPKIFAuthorityKeyIdentifierImpl)
00112 {
00113     CACASNWRAPPER_CREATE(CACX509V3AuthorityKeyIdentifier, objPDU);
00114     objPDU.Decode(ext->GetBuffer(), ext->GetLength());
00115 
00116     if(objPDU->m.authorityCertIssuerPresent)
00117     {
00118         DListNode* cur = objPDU->authorityCertIssuer.head;
00119         for(unsigned int ii = 0; ii < objPDU->authorityCertIssuer.count; ++ii)
00120         {
00121             //CPKIFBufferPtr tmpBuf(new CPKIFBuffer(((ASN1OpenType*)cur->data)->data, ((ASN1OpenType*)cur->data)->numocts));
00122             //CPKIFGeneralNamePtr tmpGN(new CPKIFGeneralName(tmpBuf));
00123             //CPKIFGeneralNamePtr tmpGN(new CPKIFGeneralName(*(CACX509V3GeneralName*)cur->data));
00124             CACASNWRAPPER_CREATE(CACX509V3GeneralName, objPDU);
00125             ASN1OpenType* data1 = objPDU.Encode((CACX509V3GeneralName*)cur->data);
00126             CPKIFBufferPtr tmpBuf;
00127             if (data1 != NULL)
00128             {
00129                  tmpBuf = CPKIFBufferPtr(new CPKIFBuffer(data1->data, data1->numocts));
00130                 delete data1;
00131             }
00132             CPKIFGeneralNamePtr tmpGN(new CPKIFGeneralName(tmpBuf));
00133             m_impl->m_issuer.push_back(tmpGN);
00134 
00135             cur = cur->next;
00136         }
00137     }
00138 
00139     if(objPDU->m.authorityCertSerialNumberPresent)
00140     {
00141         CPKIFStringPtr tmpStr(new std::string(objPDU->authorityCertSerialNumber));
00142         m_impl->m_serialNumber = tmpStr;
00143     }
00144 
00145     if(objPDU->m.keyIdentifierPresent)
00146     {
00147         CPKIFBufferPtr tmpBP(new CPKIFBuffer(objPDU->keyIdentifier.data, objPDU->keyIdentifier.numocts));
00148         m_impl->m_keyID = tmpBP;
00149     }
00150 
00151     m_impl->m_value = ext;
00152     m_impl->m_extModified = false;
00153 }
00154 
00155 
00163 CPKIFAuthorityKeyIdentifier::~CPKIFAuthorityKeyIdentifier()
00164 {
00165     if(m_impl)
00166     {
00167         delete m_impl;
00168         m_impl = 0;
00169     }
00170 }
00179 const CPKIFOIDPtr CPKIFAuthorityKeyIdentifier::oid() const
00180 {
00181     //added static variable for copying instead of string creation each call
00182     static CPKIFOID staticOID(extOID);
00183     //CPKIFOIDPtr tmp(new CPKIFOID(new std::string(extOID)));
00184     static CPKIFOIDPtr tmp(new CPKIFOID(staticOID));
00185     return tmp;
00186 }
00195 const char* CPKIFAuthorityKeyIdentifier::SerialNumber() const 
00196 {
00197     if(m_impl->m_serialNumber != NULL)
00198         return m_impl->m_serialNumber->c_str();
00199     else 
00200         return NULL;
00201 }
00209 CPKIFBufferPtr CPKIFAuthorityKeyIdentifier::KeyIdentifier() const 
00210 {
00211     return m_impl->m_keyID;
00212 }
00213 
00221 bool CPKIFAuthorityKeyIdentifier::IssDNAndSerialNumberPresent() const
00222 {
00223     if(!m_impl->m_issuer.empty() && m_impl->m_serialNumber != (std::string*)NULL)
00224         return true;
00225     else
00226         return false;
00227 }
00235 bool CPKIFAuthorityKeyIdentifier::KeyIDPresent() const
00236 {
00237     if(m_impl->m_keyID != (CPKIFBuffer*)NULL)
00238         return true;
00239     else
00240         return false;
00241 }
00250 void CPKIFAuthorityKeyIdentifier::Issuer(
00253     CPKIFGeneralNameList& genNames) const
00254 {
00255     genNames.clear();
00256 
00257     CPKIFGeneralNameList::const_iterator pos;
00258     CPKIFGeneralNameList::const_iterator end = m_impl->m_issuer.end();
00259     for(pos = m_impl->m_issuer.begin(); pos != end; ++pos)
00260     {
00261         genNames.push_back(*pos);
00262     }
00263 }
00271 CPKIFBufferPtr CPKIFAuthorityKeyIdentifier::value() const 
00272 {
00273     CPKIFBufferPtr rv = m_impl->m_value;
00274     if(m_impl->m_value == (CPKIFBuffer*)NULL || m_impl->m_extModified)
00275     {
00276         //XXX ENCODE HERE and set rv if necessary 
00277     }
00278 
00279     return rv;
00280 }
00281 
00283 CAC_API std::ostream& operator<<(std::ostream & os, const CPKIFAuthorityKeyIdentifierPtr & akid)
00284 {
00285     return operator<<(os,*akid);
00286 }
00287 
00289 CAC_API std::ostream& operator<<(std::ostream & os, const CPKIFAuthorityKeyIdentifier & akid)
00290 {
00291     bool output = false;
00292     if(akid.IssDNAndSerialNumberPresent()) {
00293         output = true;
00294         CPKIFGeneralNames inames;
00295         akid.Issuer(inames);
00296         const char * serial = akid.SerialNumber();
00297         ostringstream gnls;
00298         gnls << inames;
00299         os << "Issuer:" << endl;
00300         string names = gnls.str();
00301         for(split_iterator<string::iterator> line = make_split_iterator(names,first_finder("\n",is_equal()));
00302                 line != split_iterator<string::iterator>(); ++line)
00303         {
00304             os << "\t" << (*line) << endl;
00305         }
00306         os << "Serial: " << serial;
00307     }
00308     if(akid.KeyIDPresent())
00309     {
00310         if(output) os << endl;
00311         CPKIFBufferPtr kidBuf = akid.KeyIdentifier();
00312         if(!kidBuf) return os << "(null)";
00313         boost::scoped_array<char> buf(new char[2*kidBuf->GetLength()+1]);
00314         btoa((char*)kidBuf->GetBuffer(), buf.get(), kidBuf->GetLength());
00315         os << buf.get();
00316     }
00317     return os;
00318 }

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