GeneralName.cpp

Go to the documentation of this file.
00001 
00009 #include "GeneralName.h"
00010 #include "Buffer.h"
00011 #include "OID.h"
00012 #include "Name.h"
00013 #include "ToolkitUtils.h"
00014 
00015 #include "ASN1Helper.h"
00016 #include "PKIX1Implicit88.h"
00017 #include "PKIX1Explicit88.h"
00018 
00019 #include "components.h"
00020 #include "PKIFCommonErrors.h"
00021 
00022 #include <iostream>
00023 #include <boost/scoped_array.hpp>
00024 
00025 using std::endl;
00026 
00028 
00029 struct CPKIFGeneralNameImpl
00030 {
00031   CPKIFGeneralName::GENNAMETYPE m_type;
00032 
00033   void ProcessNameStruct(const CACX509V3GeneralName& name);
00034   void ProcessNameStruct(const CPKIFBufferPtr& name);
00035 
00036   CPKIFNamePtr m_dirName;
00037   CPKIFStringPtr m_strName; //rfc822, dNSName, uniformResourceIdentifier
00038   CPKIFBufferPtr m_bufName; 
00039   CPKIFOIDPtr m_oid; //registeredID
00040   CPKIFBufferPtr m_encoded; 
00041 };
00042 
00051 void CPKIFGeneralNameImpl::ProcessNameStruct(
00053     const CACX509V3GeneralName& name)
00054 {
00055     switch(name.t)
00056     {
00057     case 1: //otherName
00058         {
00059         m_type = CPKIFGeneralName::OTHERNAME;
00060         CPKIFBufferPtr tmpBuf (new CPKIFBuffer(name.u.otherName->data, name.u.otherName->numocts));
00061         m_bufName = tmpBuf;
00062         }
00063         break;
00064     case 2: //rfc822
00065         {
00066         m_type = CPKIFGeneralName::RFC822;
00067         CPKIFStringPtr tmpStr(new std::string(name.u.rfc822Name));
00068         m_strName = tmpStr;
00069         }
00070         break;
00071     case 3: //dNSName
00072         {
00073         m_type = CPKIFGeneralName::DNSNAME;
00074         CPKIFStringPtr tmpStr(new std::string(name.u.dNSName));
00075         m_strName = tmpStr;
00076         }
00077         break;
00078     case 4: //x400Address
00079         {
00080         m_type = CPKIFGeneralName::X400ADDRESS;
00081         CPKIFBufferPtr tmpBuf (new CPKIFBuffer(name.u.x400Address->data, name.u.x400Address->numocts));
00082         m_bufName = tmpBuf;
00083         }
00084         break;
00085     case 5: //directoryName
00086         {
00087         m_type = CPKIFGeneralName::DIRECTORYNAME;
00088         CACASNWRAPPER_CREATE(CACX509V3Name, objPDU2);
00089         ASN1OpenType* data1 = objPDU2.Encode(name.u.directoryName);
00090         CPKIFBufferPtr tmpBuf;
00091         if (data1 != NULL)
00092         {
00093              tmpBuf = CPKIFBufferPtr(new CPKIFBuffer(data1->data, data1->numocts));
00094             delete data1;
00095         }
00096         //CPKIFNamePtr tmpName(new CPKIFName(*name.u.directoryName));
00097         CPKIFNamePtr tmpName(new CPKIFName(tmpBuf));
00098         m_dirName = tmpName;
00099         }
00100         break;
00101     case 6: //ediPartyName
00102         {
00103         //XXX-DEFER implement me
00104         }
00105         break;
00106     case 7: //uniformResourceIdentifier
00107         {
00108         m_type = CPKIFGeneralName::URI;
00109         CPKIFStringPtr tmpStr(new std::string(name.u.uniformResourceIdentifier));
00110         m_strName = tmpStr;
00111         }
00112         break;
00113     case 8: //iPAddress
00114         {
00115         m_type = CPKIFGeneralName::IPADDRESS;
00116         CPKIFBufferPtr tmpBuf (new CPKIFBuffer(name.u.iPAddress->data, name.u.iPAddress->numocts));
00117         m_bufName = tmpBuf;
00118         }
00119         break;
00120     case 9: //registeredID
00121         {
00122         m_type = CPKIFGeneralName::OID;
00123         CPKIFOIDPtr tmpOID(new CPKIFOID(name.u.registeredID->subid, name.u.registeredID->numids));
00124         m_oid = tmpOID;
00125         }
00126         break;
00127     }
00128 
00129     CACASNWRAPPER_CREATE(CACX509V3GeneralName, objPDU);
00130     ASN1OpenType* data1 = objPDU.Encode(&name);
00131     CPKIFBufferPtr tmpBuf;
00132     if(data1 != NULL)
00133     {
00134         tmpBuf = CPKIFBufferPtr(new CPKIFBuffer(data1->data, data1->numocts));
00135         m_encoded = tmpBuf;
00136         delete data1;
00137     }
00138 }
00139 
00148 void CPKIFGeneralNameImpl::ProcessNameStruct(
00150     const CPKIFBufferPtr& name)
00151 {
00152     CACASNWRAPPER_CREATE(CACX509V3GeneralName, objPDU);
00153     objPDU.Decode(name->GetBuffer(), name->GetLength());
00154 
00155     switch(objPDU->t)
00156     {
00157     case 1: //otherName
00158         {
00159         m_type = CPKIFGeneralName::OTHERNAME;
00160         CPKIFBufferPtr tmpBuf (new CPKIFBuffer(objPDU->u.otherName->data, objPDU->u.otherName->numocts));
00161         m_bufName = tmpBuf;
00162         }
00163         break;
00164     case 2: //rfc822
00165         {
00166         m_type = CPKIFGeneralName::RFC822;
00167         CPKIFStringPtr tmpStr(new std::string(objPDU->u.rfc822Name));
00168         m_strName = tmpStr;
00169         }
00170         break;
00171     case 3: //dNSName
00172         {
00173         m_type = CPKIFGeneralName::DNSNAME;
00174         CPKIFStringPtr tmpStr(new std::string(objPDU->u.dNSName));
00175         m_strName = tmpStr;
00176         }
00177         break;
00178     case 4: //x400Address
00179         {
00180         m_type = CPKIFGeneralName::X400ADDRESS;
00181         CPKIFBufferPtr tmpBuf (new CPKIFBuffer(objPDU->u.x400Address->data, objPDU->u.x400Address->numocts));
00182         m_bufName = tmpBuf;
00183         }
00184         break;
00185     case 5: //directoryName
00186         {
00187         m_type = CPKIFGeneralName::DIRECTORYNAME;
00188 
00189         CACASNWRAPPER_CREATE(CACX509V3Name, objPDU2);
00190         ASN1OpenType* data1 = objPDU2.Encode(objPDU->u.directoryName);
00191         CPKIFBufferPtr tmpBuf;
00192         if (data1 != NULL)
00193         {
00194              tmpBuf = CPKIFBufferPtr(new CPKIFBuffer(data1->data, data1->numocts));
00195             delete data1;
00196         }
00197         CPKIFNamePtr tmpName(new CPKIFName(tmpBuf));
00198         //CPKIFNamePtr tmpName(new CPKIFName(*objPDU->u.directoryName));
00199         m_dirName = tmpName;
00200         }
00201         break;
00202     case 6: //ediPartyName
00203         {
00204         //XXX-DEFER implement me
00205         }
00206         break;
00207     case 7: //uniformResourceIdentifier
00208         {
00209         m_type = CPKIFGeneralName::URI;
00210         CPKIFStringPtr tmpStr(new std::string(objPDU->u.uniformResourceIdentifier));
00211         m_strName = tmpStr;
00212         }
00213         break;
00214     case 8: //iPAddress
00215         {
00216         m_type = CPKIFGeneralName::IPADDRESS;
00217         CPKIFBufferPtr tmpBuf (new CPKIFBuffer(objPDU->u.iPAddress->data, objPDU->u.iPAddress->numocts));
00218         m_bufName = tmpBuf;
00219         }
00220         break;
00221     case 9: //registeredID
00222         {
00223         m_type = CPKIFGeneralName::OID;
00224         CPKIFOIDPtr tmpOID(new CPKIFOID(objPDU->u.registeredID->subid, objPDU->u.registeredID->numids));
00225         m_oid = tmpOID;
00226         }
00227         break;
00228     }
00229 }
00230 
00232 
00240 CPKIFGeneralName::CPKIFGeneralName()
00241   :m_impl (new CPKIFGeneralNameImpl)
00242 {
00243     m_impl->m_type = NOTSET; //added 8/13/2004
00244 }
00255 //CPKIFGeneralName::CPKIFGeneralName(
00256 //  //![in] A reference to CACX509V3GeneralName structure
00257 //  const CACX509V3GeneralName& name)
00258 //  :m_impl (new CPKIFGeneralNameImpl)
00259 //{
00260 //  m_impl->m_type = NOTSET;//added 8/13/2004
00261 //  m_impl->ProcessNameStruct(name);
00262 //}
00263 
00274 CPKIFGeneralName::CPKIFGeneralName(
00276     const CPKIFBufferPtr& name)
00277   :m_impl (new CPKIFGeneralNameImpl)
00278 {
00279     m_impl->m_type = NOTSET;//added 8/13/2004
00280     m_impl->ProcessNameStruct(name);
00281 
00282     m_impl->m_encoded = name;
00283 }
00284 
00285 //commented out 8/26/2004
00286 //CPKIFGeneralName::CPKIFGeneralName(const CPKIFGeneralName& name)
00287 //{
00288 //  m_type = NOTSET;//added 8/13/2004
00289 //}
00290 
00298 CPKIFGeneralName::~CPKIFGeneralName()
00299 {
00300     if(m_impl)
00301     {
00302         delete m_impl;
00303         m_impl = 0;
00304     }
00305 }
00313 CPKIFGeneralName::CPKIFGeneralName(const CPKIFNamePtr& name) 
00314 :m_impl(new CPKIFGeneralNameImpl)
00315 {
00316     m_impl->m_type = DIRECTORYNAME; 
00317     m_impl->m_dirName = name;
00318 
00319     m_impl->m_encoded = EncodeCPKIFNameAsGenName(name);
00320 }
00347 CPKIFGeneralName::GENNAMETYPE CPKIFGeneralName::GetType() const 
00348 {
00349     return m_impl->m_type;
00350 }
00351 
00359 void CPKIFGeneralName::Decode(
00361                               CPKIFBufferPtr& bp)
00362 {
00363     CPKIFASNWrapper<CACX509V3GeneralName> gn( BEREncCACX509V3GeneralName, BERDecCACX509V3GeneralName );
00364     gn.Decode(bp->GetBuffer(), bp->GetLength());
00365     m_impl->ProcessNameStruct(*gn.data());
00366 }
00377 CPKIFBufferPtr CPKIFGeneralName::otherName() const
00378 {
00379     if(OTHERNAME == m_impl->m_type)
00380         return m_impl->m_bufName;
00381     else
00382     {
00383         CPKIFBufferPtr tmp;
00384         return tmp;
00385     }
00386 }
00397 const char* CPKIFGeneralName::rfc822Name() const
00398 {
00399     if(RFC822 == m_impl->m_type)
00400         return m_impl->m_strName->c_str();
00401     else
00402         return NULL;
00403 }
00415 const char* CPKIFGeneralName::dnsName() const
00416 {
00417     if(DNSNAME == m_impl->m_type)
00418         return m_impl->m_strName->c_str();
00419     else
00420         return NULL;
00421 }
00432 CPKIFBufferPtr CPKIFGeneralName::x400Address() const
00433 {
00434     if(X400ADDRESS == m_impl->m_type)
00435         return m_impl->m_bufName;
00436     else
00437     {
00438         CPKIFBufferPtr tmp;
00439         return tmp;
00440     }
00441 }
00452 CPKIFNamePtr CPKIFGeneralName::directoryName() const
00453 {
00454     if(DIRECTORYNAME == m_impl->m_type)
00455         return m_impl->m_dirName;
00456     else
00457     {
00458         CPKIFNamePtr tmp;
00459         return tmp;
00460     }
00461 }
00471 const char* CPKIFGeneralName::uri() const
00472 {
00473     if(URI == m_impl->m_type)
00474         return m_impl->m_strName->c_str();
00475     else
00476         return NULL;
00477 }
00488 CPKIFBufferPtr CPKIFGeneralName::ipAddress() const
00489 {
00490     if(IPADDRESS == m_impl->m_type)
00491         return m_impl->m_bufName;
00492     else
00493     {
00494         CPKIFBufferPtr tmp;
00495         return tmp;
00496     }
00497 }
00508 CPKIFOIDPtr CPKIFGeneralName::oid() const
00509 {
00510     if(OID == m_impl->m_type)
00511         return m_impl->m_oid;
00512     else
00513     {
00514         CPKIFOIDPtr tmp;
00515         return tmp;
00516     }
00517 }
00529 bool CPKIFGeneralName::operator==(
00531                                   const CPKIFGeneralName& rhs)
00532 {
00533     if(m_impl->m_type != rhs.m_impl->m_type)
00534         return false;
00535 
00536     switch(m_impl->m_type)
00537     {
00538     case OTHERNAME: //otherName
00539     case X400ADDRESS: //x400Address
00540         return *m_impl->m_bufName == *rhs.m_impl->m_bufName;
00541     case RFC822: //rfc822
00542     case DNSNAME: //dNSName
00543     case URI: //uniformResourceIdentifier
00544     case IPADDRESS: //iPAddress
00545         return *m_impl->m_strName == *rhs.m_impl->m_strName;
00546     case DIRECTORYNAME: //directoryName
00547         return *m_impl->m_dirName == *rhs.m_impl->m_dirName;
00548     case EDIPARTYNAME: //ediPartyName
00549         //XXX-DEFER implement me
00550         throw CPKIFException(TOOLKIT_X509_ASN, COMMON_NOT_IMPLEMENTED, "Support for the ediPartyName form has not been implemented.");
00551     case OID: //registeredID
00552         return *m_impl->m_oid == *rhs.m_impl->m_oid;
00553     default:
00554         {
00555             //changed error code to COMMON_UNSUPPORTED_CHOICE 3/2/2004
00556         throw CPKIFException(TOOLKIT_X509_ASN, COMMON_UNSUPPORTED_CHOICE, "Encountered unknown name form.");
00557         }
00558     }
00559 }
00560 
00568 CPKIFBufferPtr CPKIFGeneralName::Encoded()
00569 {
00570     return m_impl->m_encoded;
00571 }
00572 
00573 
00574 CAC_API std::ostream& operator<<(std::ostream & os, const CPKIFGeneralNamePtr & gn)
00575 {
00576     return operator<<(os,*gn);
00577 }
00578 
00579 CAC_API std::ostream& operator<<(std::ostream & os, const CPKIFGeneralName & gn)
00580 {
00581     CPKIFGeneralName::GENNAMETYPE gnt = gn.GetType();
00582     if(CPKIFGeneralName::DIRECTORYNAME == gnt)
00583     {
00584         os << "Directory Name:" << endl;
00585         os << "\t" << gn.directoryName()->ToString();
00586     }
00587     else if(CPKIFGeneralName::DNSNAME == gnt)
00588     {
00589         os << "DNS Name: " << gn.dnsName() << endl;
00590     }
00591     else if(CPKIFGeneralName::IPADDRESS == gnt)
00592     {
00593         CPKIFBufferPtr ip = gn.ipAddress();
00594         try {
00595             os << "IP Address/Mask: " << DecodeIPAndMask(ip) ;
00596         }catch(std::exception &){
00597             os << "IP Address/Mask: (unsupported on this OS)";
00598         }
00599     }
00600     else if(CPKIFGeneralName::OID == gnt)
00601     {
00602         os << "OID: " <<gn.oid()->ToString();
00603     }
00604     else if(CPKIFGeneralName::OTHERNAME == gnt)
00605     {
00606         CPKIFBufferPtr on = gn.otherName();
00607         boost::scoped_array<char> buf(new char[2*on->GetLength()+1]);
00608         btoa((char*)on->GetBuffer(), buf.get(), on->GetLength());
00609         os << "Other Name: " << buf.get();
00610     }   
00611     else if(CPKIFGeneralName::RFC822 == gnt)
00612     {
00613         os << "RFC822 Name: " << gn.rfc822Name() << endl;
00614     }
00615     else if(CPKIFGeneralName::URI == gnt)
00616     {
00617         os << "URI: " << gn.uri() << endl;
00618     }
00619     // x400address is unsupported right now.
00620     return os;
00621 }
00622 
00623 CAC_API std::ostream& operator<<(std::ostream & os, const CPKIFGeneralNamesPtr & gns)
00624 {
00625     return operator<<(os,*gns);
00626 }
00627 
00628 CAC_API std::ostream& operator<<(std::ostream & os, const CPKIFGeneralNames & gns)
00629 {
00630     CPKIFGeneralNames::const_iterator first = gns.begin();
00631     CPKIFGeneralNames::const_iterator end = gns.end();
00632     for(CPKIFGeneralNames::const_iterator i = first; i != end; ++i)
00633     {
00634         if(i != first) os << endl;
00635         os << (*i);
00636     }
00637     return os;
00638 }
00639 

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