2 Interface to OpenSSL object identifier database.
4 It is primarily intended to deal with OIDs which are compiled into the
5 database or defined in the openssl configuration files.
7 But see create() function
10 from ctypescrypto import libcrypto
11 from ctypes import c_char_p, c_void_p, c_int, create_string_buffer
14 Represents an OID. It can be consturucted by textual
15 representation like Oid("commonName") or Oid("CN"),
16 dotted-decimal Oid("1.2.3.4") or using OpenSSL numeric
17 identifer (NID), which is typically returned or required by
18 OpenSSL API functions. If object is consturcted from textual
19 representation which is not present in the database, it fails
22 attribute nid - contains object nid.
27 def __init__(self,value):
28 " Object constuctor. Accepts string or integer"
29 if type(value) == type(""):
30 self.nid=libcrypto.OBJ_txt2nid(value)
32 raise ValueError("Cannot find object %s in the database"%(value))
33 elif type(value) == type(0):
34 cn=libcrypto.OBJ_nid2sn(value)
36 raise ValueError("No such nid %d in the database"%(value))
39 raise TypeError("Cannot convert this type to object identifier")
43 def __cmp__(self,other):
44 " Compares NIDs of two objects "
45 return self.nid-other.nid
47 " Default string representation of Oid is dotted-decimal"
50 return "Oid('%s')"%(self.dotted())
52 " Returns short name if any "
53 return libcrypto.OBJ_nid2sn(self.nid)
55 " Returns logn name if any "
56 return libcrypto.OBJ_nid2ln(self.nid)
58 " Returns dotted-decimal reperesntation "
59 obj=libcrypto.OBJ_nid2obj(self.nid)
60 buf=create_string_buffer(256)
61 libcrypto.OBJ_obj2txt(buf,256,obj,1)
64 def create(dotted,shortname,longname):
66 Creates new OID in the database
68 @param dotted - dotted-decimal representation of new OID
69 @param shortname - short name for new OID
70 @param longname - long name for new OID
72 @returns Oid object corresponding to new OID
74 This function should be used with exreme care. Whenever
75 possible, it is better to add new OIDs via OpenSSL configuration
78 Results of calling this function twice for same OIDor for
79 Oid alredy in database are undefined
81 nid=libcrypto.OBJ_create(dotted,shortname,longname)
83 raise LibCryptoError("Problem adding new OID to the database")
88 Removes all the objects, dynamically added by current
89 application from database.
91 libcrypto.OBJ_cleanup()
93 libcrypto.OBJ_nid2sn.restype=c_char_p
94 libcrypto.OBJ_nid2ln.restype=c_char_p
95 libcrypto.OBJ_nid2obj.restype=c_void_p
96 libcrypto.OBJ_obj2txt.argtypes=(c_char_p,c_int,c_void_p,c_int)
97 libcrypto.OBJ_txt2nid.argtupes=(c_char_p,)
98 libcrypto.OBJ_create.argtypes=(c_char_p,c_char_p,c_char_p)