X-Git-Url: http://wagner.pp.ru/gitweb/?a=blobdiff_plain;f=ctypescrypto%2Foid.py;h=8caa57a68ad766c94cb79b4d40ad99042939e113;hb=ec92bf04b008b0401014cae90fa2dfca18efa02c;hp=9852043e0b936cf8e5b9dc5b0c25cbbed2011734;hpb=9a07daa396adaf452414e02fdaf3746351746224;p=oss%2Fctypescrypto.git diff --git a/ctypescrypto/oid.py b/ctypescrypto/oid.py index 9852043..8caa57a 100644 --- a/ctypescrypto/oid.py +++ b/ctypescrypto/oid.py @@ -1,5 +1,6 @@ """ Interface to OpenSSL object identifier database. + It is primarily intended to deal with OIDs which are compiled into the database or defined in the openssl configuration files. @@ -8,7 +9,10 @@ """ from ctypescrypto import libcrypto from ctypes import c_char_p, c_void_p, c_int, create_string_buffer -class Oid: + +__all__ = ['Oid','create','cleanup'] + +class Oid(object): """ Represents an OID. It can be consturucted by textual representation like Oid("commonName") or Oid("CN"), @@ -25,11 +29,13 @@ class Oid: def __init__(self,value): " Object constuctor. Accepts string or integer" - if type(value) == type(""): + if isinstance(value,unicode): + value=value.encode('ascii') + if isinstance(value,str): self.nid=libcrypto.OBJ_txt2nid(value) if self.nid==0: raise ValueError("Cannot find object %s in the database"%(value)) - elif type(value) == type(0): + elif isinstance(value,(int,long)): cn=libcrypto.OBJ_nid2sn(value) if cn is None: raise ValueError("No such nid %d in the database"%(value)) @@ -54,11 +60,25 @@ class Oid: " Returns logn name if any " return libcrypto.OBJ_nid2ln(self.nid) def dotted(self): - " Returns dotted-decimal reperesntation " + " Returns dotted-decimal reperesentation " obj=libcrypto.OBJ_nid2obj(self.nid) buf=create_string_buffer(256) libcrypto.OBJ_obj2txt(buf,256,obj,1) return buf.value + @staticmethod + def fromobj(obj): + """ + Creates an OID object from the pointer to ASN1_OBJECT c structure. + Strictly for internal use + """ + nid=libcrypto.OBJ_obj2nid(obj) + if nid==0: + buf=create_string_buffer(80) + l=libcrypto.OBJ_obj2txt(buf,80,obj,1) + oid=create(buf[0:l],buf[0:l],buf[0:l]) + else: + oid=Oid(nid) + return oid def create(dotted,shortname,longname): """