]> wagner.pp.ru Git - oss/ctypescrypto.git/blobdiff - ctypescrypto/oid.py
more style cleanup
[oss/ctypescrypto.git] / ctypescrypto / oid.py
index 9852043e0b936cf8e5b9dc5b0c25cbbed2011734..8caa57a68ad766c94cb79b4d40ad99042939e113 100644 (file)
@@ -1,5 +1,6 @@
 """    
  Interface to OpenSSL object identifier database.
 """    
  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.
 
  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
 """
 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"),
        """
                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"
 
        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))
                        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))
                        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 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
                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):
        """
 
 def create(dotted,shortname,longname):
        """