2 Interface to OpenSSL object identifier database.
3 It is primarily intended to deal with OIDs which are compiled into the
4 database or defined in the openssl configuration files.
6 But see create() function
9 from ctypescrypto import libcrypto
10 from ctypes import c_char_p, c_void_p, c_int, create_string_buffer
13 Represents an OID. It can be consturucted by textual
14 representation like Oid("commonName") or Oid("CN"),
15 dotted-decimal Oid("1.2.3.4") or using OpenSSL numeric
16 identifer (NID), which is typically returned or required by
17 OpenSSL API functions. If object is consturcted from textual
18 representation which is not present in the database, it fails
21 attribute nid - contains object nid.
26 def __init__(self,value):
27 " Object constuctor. Accepts string or integer"
28 if type(value) == type(""):
29 self.nid=libcrypto.OBJ_txt2nid(value)
31 raise ValueError("Cannot find object %s in the database"%(value))
32 elif type(value) == type(0):
33 cn=libcrypto.OBJ_nid2sn(value)
35 raise ValueError("No such nid %d in the database"%(value))
38 raise TypeError("Cannot convert this type to object identifier")
42 def __cmp__(self,other):
43 " Compares NIDs of two objects "
44 return self.nid-other.nid
46 " Default string representation of Oid is dotted-decimal"
49 return "Oid('%s')"%(self.dotted())
51 " Returns short name if any "
52 return libcrypto.OBJ_nid2sn(self.nid)
54 " Returns logn name if any "
55 return libcrypto.OBJ_nid2ln(self.nid)
57 " Returns dotted-decimal reperesntation "
58 obj=libcrypto.OBJ_nid2obj(self.nid)
59 buf=create_string_buffer(256)
60 libcrypto.OBJ_obj2txt(buf,256,obj,1)
63 def create(dotted,shortname,longname):
65 Creates new OID in the database
67 @param dotted - dotted-decimal representation of new OID
68 @param shortname - short name for new OID
69 @param longname - long name for new OID
71 @returns Oid object corresponding to new OID
73 This function should be used with exreme care. Whenever
74 possible, it is better to add new OIDs via OpenSSL configuration
77 Results of calling this function twice for same OIDor for
78 Oid alredy in database are undefined
80 nid=libcrypto.OBJ_create(dotted,shortname,longname)
82 raise LibCryptoError("Problem adding new OID to the database")
87 Removes all the objects, dynamically added by current
88 application from database.
90 libcrypto.OBJ_cleanup()
92 libcrypto.OBJ_nid2sn.restype=c_char_p
93 libcrypto.OBJ_nid2ln.restype=c_char_p
94 libcrypto.OBJ_nid2obj.restype=c_void_p
95 libcrypto.OBJ_obj2txt.argtypes=(c_char_p,c_int,c_void_p,c_int)
96 libcrypto.OBJ_txt2nid.argtupes=(c_char_p,)
97 libcrypto.OBJ_create.argtypes=(c_char_p,c_char_p,c_char_p)