KeyUsage.cpp

Go to the documentation of this file.
00001 
00009 #include "KeyUsage.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 <iostream>
00018 
00019 using namespace std;
00020 
00022 
00023 struct CPKIFKeyUsageImpl
00024 {
00025     enum {DIGSIG, NONREP,KEYENC,DATAENC,KEYAGREE,CERT,CRL,ENCONLY,DECONLY};
00026     std::bitset<9> m_bits;
00027     CPKIFBufferPtr m_value;
00028     bool m_extModified;
00029 };
00030 
00032 
00033 char CPKIFKeyUsage::extOID[] =                  "2.5.29.15";
00034 
00042 CPKIFKeyUsage::CPKIFKeyUsage()
00043   :m_impl (new CPKIFKeyUsageImpl)
00044 {
00045 }
00046 
00047 
00058 CPKIFKeyUsage::CPKIFKeyUsage(
00060     const bool& criticality, 
00062     const CPKIFBufferPtr& ext) 
00063   : CPKIFX509Extension (criticality, ext), m_impl (new CPKIFKeyUsageImpl)
00064 {
00065     CACASNWRAPPER_CREATE(CACX509V3KeyUsage, objPDU);
00066     objPDU.Decode(ext->GetBuffer(), ext->GetLength());
00067 
00068     if( objPDU->data[BytXCACX509V3digitalSignature] & BitMCACX509V3digitalSignature ) 
00069     {
00070         m_impl->m_bits.set(CPKIFKeyUsageImpl::DIGSIG);
00071     }
00072     if( objPDU->data[BytXCACX509V3nonRepudiation] & BitMCACX509V3nonRepudiation ) 
00073     {
00074         m_impl->m_bits.set(CPKIFKeyUsageImpl::NONREP);
00075     }
00076     if( objPDU->data[BytXCACX509V3dataEncipherment] & BitMCACX509V3dataEncipherment ) 
00077     {
00078         m_impl->m_bits.set(CPKIFKeyUsageImpl::DATAENC);
00079     }
00080     if( objPDU->data[BytXCACX509V3keyEncipherment] & BitMCACX509V3keyEncipherment ) 
00081     {
00082         m_impl->m_bits.set(CPKIFKeyUsageImpl::KEYENC);
00083     }
00084     if( objPDU->data[BytXCACX509V3keyAgreement] & BitMCACX509V3keyAgreement ) 
00085     {
00086         m_impl->m_bits.set(CPKIFKeyUsageImpl::KEYAGREE);
00087     }
00088     if( objPDU->data[BytXCACX509V3keyCertSign] & BitMCACX509V3keyCertSign ) 
00089     {
00090         m_impl->m_bits.set(CPKIFKeyUsageImpl::CERT);
00091     }
00092     if( objPDU->data[BytXCACX509V3cRLSign] & BitMCACX509V3cRLSign ) 
00093     {
00094         m_impl->m_bits.set(CPKIFKeyUsageImpl::CRL);
00095     }
00096     if( objPDU->data[BytXCACX509V3encipherOnly] & BitMCACX509V3encipherOnly ) 
00097     {
00098         m_impl->m_bits.set(CPKIFKeyUsageImpl::ENCONLY);
00099     }
00100     if (objPDU->numbits > 8)    // only second byte is used if needed and uninitialized otherwise
00101     {
00102         if( objPDU->data[BytXCACX509V3decipherOnly] & BitMCACX509V3decipherOnly ) 
00103         {
00104             m_impl->m_bits.set(CPKIFKeyUsageImpl::DECONLY);
00105         }
00106     }
00107 
00108     m_impl->m_value = ext;
00109     m_impl->m_extModified = false;
00110 }
00111 
00119 CPKIFKeyUsage::~CPKIFKeyUsage()
00120 {
00121     if(m_impl)
00122     {
00123         delete m_impl;
00124         m_impl = 0;
00125     }
00126 }
00135 const CPKIFOIDPtr CPKIFKeyUsage::oid() const
00136 {
00137     //added static variable for copying instead of string creation each call
00138     static CPKIFOID staticOID(extOID);
00139     //CPKIFOIDPtr tmp(new CPKIFOID(new std::string(extOID)));
00140     static CPKIFOIDPtr tmp(new CPKIFOID(staticOID));
00141     return tmp;
00142 }
00151 bool CPKIFKeyUsage::DigitalSignature() const {return m_impl->m_bits[CPKIFKeyUsageImpl::DIGSIG];}
00160 bool CPKIFKeyUsage::NonRepudiation() const {return m_impl->m_bits[CPKIFKeyUsageImpl::NONREP];}
00169 bool CPKIFKeyUsage::KeyEncipherment() const {return m_impl->m_bits[CPKIFKeyUsageImpl::KEYENC];}
00170 
00179 bool CPKIFKeyUsage::DataEncipherment() const {return m_impl->m_bits[CPKIFKeyUsageImpl::DATAENC];}
00188 bool CPKIFKeyUsage::KeyAgreement() const {return m_impl->m_bits[CPKIFKeyUsageImpl::KEYAGREE];}
00197 bool CPKIFKeyUsage::KeyCertSign() const {return m_impl->m_bits[CPKIFKeyUsageImpl::CERT];}
00206 bool CPKIFKeyUsage::CRLSign() const {return m_impl->m_bits[CPKIFKeyUsageImpl::CRL];}
00215 bool CPKIFKeyUsage::EncipherOnly() const {return m_impl->m_bits[CPKIFKeyUsageImpl::ENCONLY];}
00224 bool CPKIFKeyUsage::DecipherOnly() const {return m_impl->m_bits[CPKIFKeyUsageImpl::DECONLY];}
00225 
00233 void CPKIFKeyUsage::SetDigitalSignature() {m_impl->m_bits.set(CPKIFKeyUsageImpl::DIGSIG);}
00241 void CPKIFKeyUsage::SetNonRepudiation() {m_impl->m_bits.set(CPKIFKeyUsageImpl::NONREP);}
00249 void CPKIFKeyUsage::SetKeyEncipherment() {m_impl->m_bits.set(CPKIFKeyUsageImpl::KEYENC);}
00250 
00258 void CPKIFKeyUsage::SetDataEncipherment() {m_impl->m_bits.set(CPKIFKeyUsageImpl::DATAENC);}
00266 void CPKIFKeyUsage::SetKeyAgreement() {m_impl->m_bits.set(CPKIFKeyUsageImpl::KEYAGREE);}
00275 void CPKIFKeyUsage::SetKeyCertSign() {m_impl->m_bits.set(CPKIFKeyUsageImpl::CERT);}
00283 void CPKIFKeyUsage::SetCRLSign() {m_impl->m_bits.set(CPKIFKeyUsageImpl::CRL);}
00291 void CPKIFKeyUsage::SetEncipherOnly() {m_impl->m_bits.set(CPKIFKeyUsageImpl::ENCONLY);}
00299 void CPKIFKeyUsage::SetDecipherOnly() {m_impl->m_bits.set(CPKIFKeyUsageImpl::DECONLY);}
00300 
00308 bitset<9> CPKIFKeyUsage::GetKeyUsage()
00309 {
00310     return m_impl->m_bits;
00311 }
00312 
00320 CPKIFBufferPtr CPKIFKeyUsage::value() const 
00321 {
00322     CPKIFBufferPtr rv = m_impl->m_value;
00323     if(m_impl->m_value == (CPKIFBuffer*)NULL || m_impl->m_extModified)
00324     {
00325         //XXX ENCODE HERE and set rv if necessary 
00326     }
00327 
00328     return rv;
00329 }
00330 
00331 CAC_API std::ostream& operator<<(std::ostream & os, const CPKIFKeyUsagePtr & extension)
00332 {
00333     return operator<<(os,*extension);
00334 }
00335 
00336 CAC_API std::ostream& operator<<(std::ostream & os, const CPKIFKeyUsage & extension)
00337 {
00338     bool output = false;
00339     if(extension.CRLSign()) {
00340         os << "CRL Signing";
00341         output = true;
00342     }
00343     if(extension.DataEncipherment()) {
00344         if(output) os << endl;
00345         output = true;
00346         os << "Data Encipherment ";
00347     }
00348     if(extension.DecipherOnly()){
00349         if(output) os << endl;
00350         output = true;
00351         os << "Decipher Only";
00352     }
00353     if(extension.DigitalSignature()) {
00354         if(output) os << endl;
00355         output = true;
00356         os << "Digital Signature";
00357     }
00358     if(extension.EncipherOnly()) {
00359         if(output) os << endl;
00360         output = true;
00361         os << "Encipher Only";
00362     }
00363     if(extension.KeyAgreement()) {
00364         if(output) os << endl;
00365         output = true;
00366         os << "Key Agreement";
00367     }
00368     if(extension.KeyCertSign()) {
00369         if(output) os << endl;
00370         output = true;
00371         os << "Certificate Signing";
00372     }
00373     if(extension.KeyEncipherment()) {
00374         if(output) os << endl;
00375         output = true;
00376         os << "Key Encipherment";
00377     }
00378     if(extension.NonRepudiation()) {
00379         if(output) os << endl;
00380         output = true;
00381         os << "Non Repudiation";
00382     }
00383     return os;
00384 }
00385 

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