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;
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
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
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;
00093 m_impl->m_subjectPublicKey->data = new unsigned char[key->GetLength()];
00094 memcpy((void*)m_impl->m_subjectPublicKey->data, key->GetBuffer(), key->GetLength());
00095 }
00096
00097
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 }