SubjectPublicKeyInfo.cpp

Go to the documentation of this file.
00001 
00009 #include "SubjectPublicKeyInfo.h"
00010 #include "AlgorithmIdentifier.h"
00011 #include "ASN1Helper.h"
00012 #include "PKIX1Implicit88.h"
00013 #include "PKIX1Explicit88.h"
00014 #include "Buffer.h"
00015 #include "OID.h"
00016 #include "PKIX1Algorithms88.h"
00017 #include "PKIFException.h"
00018 #include "ASN1Errors.h"
00019 #include "boost/numeric/conversion/cast.hpp"
00020 
00021 using boost::numeric_cast;
00022 using boost::bad_numeric_cast;
00023 
00025 
00026 struct CPKIFSubjectPublicKeyInfoImpl
00027 {
00028   CPKIFAlgorithmIdentifierPtr m_algorithm;
00029   ASN1DynBitStr* m_subjectPublicKey;
00030   CPKIFBufferPtr m_rawKey;
00031   long m_numBits; // a negative value indicates that the key has not yet been decoded
00032 };
00033 
00035 
00043 CPKIFSubjectPublicKeyInfo::CPKIFSubjectPublicKeyInfo()
00044   :m_impl (new CPKIFSubjectPublicKeyInfoImpl)
00045 {
00046     m_impl->m_subjectPublicKey = NULL;
00047     m_impl->m_numBits = -1;
00048 }
00056 //CPKIFSubjectPublicKeyInfo::CPKIFSubjectPublicKeyInfo(
00057 //  //! [in] Reference to an internal structure representation of the extension containing information 
00058 //  //! used to construct the object
00059 //  const CACX509V3SubjectPublicKeyInfo spki)
00060 //  :m_impl (new CPKIFSubjectPublicKeyInfoImpl)
00061 //{
00062 //
00063 //
00064 //  CPKIFOIDPtr algOID(new CPKIFOID(spki.algorithm.algorithm.subid, spki.algorithm.algorithm.numids));
00065 //
00066 //  CPKIFBufferPtr paramBuf;
00067 //  if(spki.algorithm.m.parametersPresent)
00068 //  {
00069 //      paramBuf = CPKIFBufferPtr(new CPKIFBuffer(spki.algorithm.parameters.data, spki.algorithm.parameters.numocts));
00070 //  }
00071 //
00072 //  CPKIFAlgorithmIdentifierPtr tmpRef(new CPKIFAlgorithmIdentifier(algOID, paramBuf)); 
00073 //  m_impl->m_algorithm = tmpRef;
00074 //
00075 //  m_impl->m_subjectPublicKey = new ASN1DynBitStr;
00076 //  m_impl->m_subjectPublicKey->numbits = spki.subjectPublicKey.numbits;
00077 //  m_impl->m_subjectPublicKey->data = new unsigned char[m_impl->m_subjectPublicKey->numbits/8];
00078 //  memcpy((void*)m_impl->m_subjectPublicKey->data, spki.subjectPublicKey.data, m_impl->m_subjectPublicKey->numbits/8);
00079 //}
00080 CPKIFSubjectPublicKeyInfo::CPKIFSubjectPublicKeyInfo(
00082     const CPKIFAlgorithmIdentifierPtr& algID, 
00084     const CPKIFBufferPtr& key)
00085   :m_impl (new CPKIFSubjectPublicKeyInfoImpl)
00086 {
00087     m_impl->m_numBits = -1;
00088     CPKIFAlgorithmIdentifierPtr tmpRef(algID); 
00089     m_impl->m_algorithm = tmpRef;
00090 
00091     m_impl->m_subjectPublicKey = new ASN1DynBitStr;
00092     m_impl->m_subjectPublicKey->numbits = key->GetLength()*8;//spki.subjectPublicKey.numbits;
00093     m_impl->m_subjectPublicKey->data = new unsigned char[key->GetLength()];//m_impl->m_subjectPublicKey->numbits/8];
00094     memcpy((void*)m_impl->m_subjectPublicKey->data, key->GetBuffer(), key->GetLength());//spki.subjectPublicKey.data, m_impl->m_subjectPublicKey->numbits/8);
00095 }
00096 //commented out 8/26/2004
00097 //CPKIFSubjectPublicKeyInfo::CPKIFSubjectPublicKeyInfo(const CPKIFSubjectPublicKeyInfo& spki)
00098 //{
00099 //}
00100 
00108 CPKIFSubjectPublicKeyInfo::~CPKIFSubjectPublicKeyInfo()
00109 {
00110     if(m_impl)
00111     {
00112         if(NULL != m_impl->m_subjectPublicKey)
00113         {
00114             if(NULL != m_impl->m_subjectPublicKey->data)
00115             {
00116                 delete[] m_impl->m_subjectPublicKey->data; m_impl->m_subjectPublicKey->data = NULL;
00117             }
00118 
00119             delete m_impl->m_subjectPublicKey;
00120         }
00121 
00122         delete m_impl;
00123         m_impl = 0;
00124     }
00125 }
00134 CPKIFAlgorithmIdentifierPtr CPKIFSubjectPublicKeyInfo::alg() const 
00135 {
00136     return m_impl->m_algorithm;
00137 }
00138 
00147 CPKIFBufferPtr CPKIFSubjectPublicKeyInfo::rawKey() const
00148 {
00149     if(!m_impl->m_rawKey)
00150     {
00151         CPKIFBufferPtr tmpBuf(new CPKIFBuffer(m_impl->m_subjectPublicKey->data, m_impl->m_subjectPublicKey->numbits/8));
00152         m_impl->m_rawKey = tmpBuf;
00153     }
00154     return m_impl->m_rawKey;
00155 }
00156 
00164 unsigned long CPKIFSubjectPublicKeyInfo::numBits(void) const
00165 {
00166     if(!m_impl || !m_impl->m_subjectPublicKey)
00167         throw CPKIFException(TOOLKIT_ASN,COMMON_NOT_INITIALIZED,"numBits() called on uninitialized CPKIFSubjectPublicKeyInfo");
00168     
00169     return static_cast<unsigned long>(m_impl->m_subjectPublicKey->numbits);
00170 }
00171 
00176 bool CPKIFSubjectPublicKeyInfo::operator==(const CPKIFSubjectPublicKeyInfo& rhs) const
00177 {
00178     CPKIFBufferPtr lhsKey = this->rawKey();
00179     CPKIFBufferPtr rhsKey = rhs.rawKey();
00180     if(rhsKey && lhsKey && (*lhsKey == *rhsKey))
00181         return true;
00182     else
00183         return false;
00184 }

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