]> wagner.pp.ru Git - oss/ctypescrypto.git/blobdiff - ctypescrypto/oid.py
Python 3 support for modules pbkdf2 pkey ec x509
[oss/ctypescrypto.git] / ctypescrypto / oid.py
index 569739e6b7360c8c411580957847afd4117143e2..5f22f358f3539f40fbab544e39d7edd0f6aa91b8 100644 (file)
@@ -18,7 +18,7 @@ library.
 This module provides Oid object which represents entry to OpenSSL
 OID database.
 """
 This module provides Oid object which represents entry to OpenSSL
 OID database.
 """
-from ctypescrypto import libcrypto
+from ctypescrypto import libcrypto, pyver,bintype,chartype,inttype
 from ctypes import c_char_p, c_void_p, c_int, create_string_buffer
 from ctypescrypto.exception import LibCryptoError
 
 from ctypes import c_char_p, c_void_p, c_int, create_string_buffer
 from ctypescrypto.exception import LibCryptoError
 
@@ -42,21 +42,21 @@ class Oid(object):
 
     def __init__(self, value):
         """
 
     def __init__(self, value):
         """
-        Object constuctor. Accepts string, integer, or another Oid
+        Object constructor. Accepts string, integer, or another Oid
         object.
 
         Integer should be OpenSSL numeric identifier (nid) as returned
         by some libcrypto function or extracted from some libcrypto
         structure
         """
         object.
 
         Integer should be OpenSSL numeric identifier (nid) as returned
         by some libcrypto function or extracted from some libcrypto
         structure
         """
-        if isinstance(value, unicode):
+        if isinstance(value, chartype):
             value = value.encode('ascii')
             value = value.encode('ascii')
-        if isinstance(value, str):
+        if isinstance(value, bintype):
             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 isinstance(value, (int, long)):
+        elif isinstance(value, inttype):
             short = libcrypto.OBJ_nid2sn(value)
             if short is None:
                 raise ValueError("No such nid %d in the database" % value)
             short = libcrypto.OBJ_nid2sn(value)
             if short is None:
                 raise ValueError("No such nid %d in the database" % value)
@@ -68,27 +68,43 @@ class Oid(object):
     def __hash__(self):
         " Hash of object is equal to nid because Oids with same nid are same"
         return self.nid
     def __hash__(self):
         " Hash of object is equal to nid because Oids with same nid are same"
         return self.nid
-    def __cmp__(self, other):
-        " Compares NIDs of two objects "
-        return self.nid - other.nid
+    def __eq__ (self, other):
+        return self.nid == other.nid
+    def __hash__(self):
+        """ Returns NID of object as hash value. Should make Oids with 
+          identical NID compare equal and also let use Oids as
+          dictionary keys"""
+        return self.nid
     def __str__(self):
         " Default string representation of Oid is dotted-decimal "
         return self.dotted()
     def __repr__(self):
         " Returns constructor call of Oid with dotted representation "
         return "Oid('%s')" % (self.dotted())
     def __str__(self):
         " Default string representation of Oid is dotted-decimal "
         return self.dotted()
     def __repr__(self):
         " Returns constructor call of Oid with dotted representation "
         return "Oid('%s')" % (self.dotted())
-    def shortname(self):
-        " Returns short name if any "
-        return libcrypto.OBJ_nid2sn(self.nid)
-    def longname(self):
-        " Returns logn name if any "
-        return  libcrypto.OBJ_nid2ln(self.nid)
+    if pyver == 2:
+        def shortname(self):
+            " Returns short name if any "
+            return libcrypto.OBJ_nid2sn(self.nid)
+        def longname(self):
+            " Returns long name if any "
+            return  libcrypto.OBJ_nid2ln(self.nid)
+    else:
+        def shortname(self):
+            " Returns short name if any "
+            return libcrypto.OBJ_nid2sn(self.nid).decode('utf-8')
+        def longname(self):
+            " Returns long name if any "
+            return  libcrypto.OBJ_nid2ln(self.nid).decode('utf-8')
+            
     def dotted(self):
         " Returns dotted-decimal reperesentation "
         obj = libcrypto.OBJ_nid2obj(self.nid)
         buf = create_string_buffer(256)
         libcrypto.OBJ_obj2txt(buf, 256, obj, 1)
     def dotted(self):
         " 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
+        if pyver == 2:
+            return buf.value
+        else:
+            return buf.value.decode('ascii')
     @staticmethod
     def fromobj(obj):
         """
     @staticmethod
     def fromobj(obj):
         """
@@ -124,6 +140,10 @@ def create(dotted, shortname, longname):
     Oid alredy in database are undefined
 
     """
     Oid alredy in database are undefined
 
     """
+    if pyver  > 2:
+        dotted = dotted.encode('ascii')
+        shortname = shortname.encode('utf-8')
+        longname = longname.encode('utf-8')
     nid = libcrypto.OBJ_create(dotted, shortname, longname)
     if nid == 0:
         raise LibCryptoError("Problem adding new OID to the  database")
     nid = libcrypto.OBJ_create(dotted, shortname, longname)
     if nid == 0:
         raise LibCryptoError("Problem adding new OID to the  database")
@@ -133,8 +153,11 @@ def cleanup():
     """
     Removes all the objects, dynamically added by current
     application from database.
     """
     Removes all the objects, dynamically added by current
     application from database.
+
+    Note that in OpenSSL 1.1.0 and above OBJ_cleanup really does nothing
     """
     """
-    libcrypto.OBJ_cleanup()
+    if hasattr(libcrypto,"OBJ_cleanup"):
+        libcrypto.OBJ_cleanup()
 
 libcrypto.OBJ_nid2sn.restype = c_char_p
 libcrypto.OBJ_nid2ln.restype = c_char_p
 
 libcrypto.OBJ_nid2sn.restype = c_char_p
 libcrypto.OBJ_nid2ln.restype = c_char_p