ASN1HelperLib.cpp

Go to the documentation of this file.
00001 
00010 #include "ASN1HelperLib.h"
00011 #include "PKIFCommonErrors.h"
00012 #include "PKIFException.h"
00013 #include "ASN1Helper.h"
00014 #include "Name.h"
00015 #include "Buffer.h"
00016 
00024 void stringtok( 
00026     std::string &input, 
00028     const std::string &delims, 
00030     std::string &result)
00031 {
00032     std::string::size_type split(input.find_first_of(delims));
00033     if(std::string::npos != split)
00034     {
00035         result = input.substr(0, split);
00036 
00037         std::string::size_type next(input.find_first_not_of(delims, split));
00038         if(std::string::npos != next)
00039             input.erase(0, next);
00040     }
00041     else
00042     {
00043         result = input;
00044         input.clear();
00045     }
00046     //return result;
00047 }
00048 
00077 void FreeASN1Context(
00079     OOCTXT* ctx)
00080 {
00081     /* GIB: freeContext will take care of both. This could lead
00082     to a double free
00083     freeEncodeBuffer(ctx);
00084     memFreeAll(ctx); */
00085     freeContext(ctx);
00086 }
00087 
00088 
00089 // GIB: move to ToolkitUtils since it's used outside CMS
00097 void CopyOID(
00099     ASN1OBJID* dest, 
00101     ASN1OBJID* src)
00102 {
00103     dest->numids = src->numids;
00104     for(unsigned int ii = 0; ii < src->numids; ++ii)
00105         dest->subid[ii] = src->subid[ii];
00106 }
00107 
00108 
00109 //converts a dot form string into an ASN1OBJ for use by the compiler
00117 ASN1OBJID* ConvertStringToASN1OBJID(
00119     const CPKIFStringPtr& str)
00120 {
00121     if(str == NULL || str->empty())
00122         return NULL;
00123 
00124     //get a copy of the string in a non-reference counted form (we will be modifying this copy)
00125     std::string oidStr = *str;
00126 
00127     //see if there;s any stuff in the str passed in that isn't valid for an OID
00128     if(std::string::npos != oidStr.find_first_not_of("0123456789."))
00129         return NULL;
00130 
00131     //create a new OID...
00132     ASN1OBJID* theOid = new ASN1OBJID;
00133     int numIds = 0;
00134     
00135     //and fill it
00136     std::string result;
00137     
00138     stringtok(oidStr, ".", result);
00139     while (!result.empty())
00140     {
00141         theOid->subid[numIds] = atoi(result.c_str());
00142         numIds++;
00143         stringtok(oidStr, ".", result);
00144     }
00145 
00146     theOid->numids = numIds;
00147     return theOid;
00148 }
00149 
00160 void CopyGeneralName(
00162     CACX509V3GeneralName& objName, 
00164     CPKIFGeneralNamePtr& gn)
00165 {
00166     //added NULL check 8/13/04
00167     if(gn == (CPKIFGeneralName*)NULL)
00168         throw CPKIFException(TOOLKIT_APP, COMMON_INVALID_INPUT, "NULL pointer passed to CopyGeneralName");
00169 
00170     //XXX***need to either implement support for other types or fail when different type is tried
00171     //      (matters little at the moment as it is only used internally and is only ever used with dir name)
00172 
00173     CACASNWRAPPER_CREATE(CACX509V3Name, objPDU);
00174     CACX509V3Name *tmpName = NULL;
00175     switch(gn->GetType())
00176     {
00177     case CPKIFGeneralName::OID:
00178         objName.t = 9;
00179         break;
00180     case CPKIFGeneralName::IPADDRESS:
00181         objName.t = 8;
00182         break;
00183     case CPKIFGeneralName::URI:
00184         objName.t = 7;
00185         break;
00186     case CPKIFGeneralName::EDIPARTYNAME:
00187         objName.t = 6;
00188         break;
00189     case CPKIFGeneralName::DIRECTORYNAME:
00190         {
00191         objName.t = 5;
00192         CPKIFBufferPtr tmpbuf = gn->directoryName()->rawName();
00193         tmpName = objPDU.Decode(tmpbuf->GetBuffer(), tmpbuf->GetLength());
00194         CopyName(&objName.u.directoryName, *tmpName);
00195         //CopyName(&objName.u.directoryName, *gn->directoryName()->rawName());
00196         break;
00197         }
00198     case CPKIFGeneralName::X400ADDRESS:
00199         objName.t = 4;
00200         break;
00201     case CPKIFGeneralName::DNSNAME:
00202         objName.t = 3;
00203         break;
00204     case CPKIFGeneralName::RFC822:
00205         {
00206             objName.t = 2;
00207             const char* tmpP = gn->rfc822Name();
00208             objName.u.rfc822Name = new char[strlen(tmpP) + 1];
00209 
00210             //reviewed 4/23/2006
00211             strcpy((char*)objName.u.rfc822Name, tmpP);
00212         }
00213         break;
00214     case CPKIFGeneralName::OTHERNAME:
00215         objName.t = 1;
00216         break;
00217     case CPKIFGeneralName::NOTSET:
00218         break;
00219     }
00220 }
00221 
00229 void CopyName(
00231     CACX509V3Name** dest,
00233     CACX509V3Name& name)
00234 {
00235     *dest = new CACX509V3Name;
00236     (*dest)->t = name.t;
00237 
00238     (*dest)->u.rdnSequence = new CACX509V3RelativeDistinguishedName;
00239     (*dest)->u.rdnSequence->count = name.u.rdnSequence->count;
00240     DListNode* cur = NULL, *cur2 = name.u.rdnSequence->head;
00241     ASN1OpenType* tmp = NULL, *tmp2 = NULL;
00242     for(unsigned int ii = 0; ii < name.u.rdnSequence->count; ++ii)
00243     {
00244         tmp = new ASN1OpenType;
00245         tmp2 = (ASN1OpenType*)cur2->data;
00246         tmp->numocts = tmp2->numocts;
00247         tmp->data = new unsigned char[tmp->numocts];
00248         memcpy((void*)tmp->data, tmp2->data, tmp->numocts);
00249 
00250         if(NULL == cur)
00251         {
00252             cur = new DListNode();
00253             (*dest)->u.rdnSequence->head = cur;
00254             cur->prev = NULL;
00255             cur->next = NULL;
00256         }
00257         else
00258         {
00259             cur->next = new DListNode();
00260             cur->next->prev = cur;
00261             cur = cur->next;
00262             cur->next = NULL;
00263         }
00264 
00265         cur->data = tmp;
00266         cur2 = cur2->next;
00267     }
00268     (*dest)->u.rdnSequence->tail = cur;
00269 }
00270 
00278 void CopyName(
00280     CACX509V3Name* dest,
00282     CACX509V3Name& name)
00283 {
00284     dest->t = name.t;
00285 
00286     dest->u.rdnSequence = new CACX509V3RelativeDistinguishedName;
00287     dest->u.rdnSequence->count = name.u.rdnSequence->count;
00288     DListNode* cur = NULL, *cur2 = name.u.rdnSequence->head;
00289     ASN1OpenType* tmp = NULL, *tmp2 = NULL;
00290     for(unsigned int ii = 0; ii < name.u.rdnSequence->count; ++ii)
00291     {
00292         tmp = new ASN1OpenType;
00293         tmp2 = (ASN1OpenType*)cur2->data;
00294         tmp->numocts = tmp2->numocts;
00295         tmp->data = new unsigned char[tmp->numocts];
00296         memcpy((void*)tmp->data, tmp2->data, tmp->numocts);
00297 
00298         if(NULL == cur)
00299         {
00300             cur = new DListNode();
00301             dest->u.rdnSequence->head = cur;
00302             cur->prev = NULL;
00303             cur->next = NULL;
00304         }
00305         else
00306         {
00307             cur->next = new DListNode();
00308             cur->next->prev = cur;
00309             cur = cur->next;
00310             cur->next = NULL;
00311         }
00312 
00313         cur->data = tmp;
00314         cur2 = cur2->next;
00315     }
00316     dest->u.rdnSequence->tail = cur;
00317 }
00318 

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