]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blobdiff - ctypescrypto/oid.py
Fixes some style. Improved tests coverage for bio,oid,digest and cipher. Prepare...
[oss/ctypescrypto.git] / ctypescrypto / oid.py
index 976cd3f8e5b7cf07cc6ab86e93e1400d4acac52d..8caa57a68ad766c94cb79b4d40ad99042939e113 100644 (file)
@@ -9,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"),
@@ -26,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))
@@ -55,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):
        """