]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blob - ctypescrypto/oid.py
cleaned up documentation
[oss/ctypescrypto.git] / ctypescrypto / oid.py
1 """     
2  Interface to OpenSSL object identifier database.
3
4  It is primarily intended to deal with OIDs which are compiled into the
5  database or defined in the openssl configuration files.
6
7  But see create() function
8
9 """
10 from ctypescrypto import libcrypto
11 from ctypes import c_char_p, c_void_p, c_int, create_string_buffer
12 class Oid:
13         """
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
20                 with ValueError
21
22                 attribute nid - contains object nid.
23
24
25         """
26
27         def __init__(self,value):
28                 " Object constuctor. Accepts string or integer"
29                 if type(value) == type(""):
30                         self.nid=libcrypto.OBJ_txt2nid(value)
31                         if self.nid==0:
32                                 raise ValueError("Cannot find object %s in the database"%(value))
33                 elif type(value) == type(0):
34                         cn=libcrypto.OBJ_nid2sn(value)
35                         if cn is None:
36                                 raise ValueError("No such nid %d in the database"%(value))
37                         self.nid=value
38                 else:
39                         raise TypeError("Cannot convert this type to object identifier")
40         def __hash__(self):
41                 " Returns NID "
42                 return self.nid
43         def __cmp__(self,other):
44                 " Compares NIDs of two objects "
45                 return self.nid-other.nid
46         def __str__(self):
47                 " Default string representation of Oid is dotted-decimal"
48                 return self.dotted()
49         def __repr__(self):
50                 return "Oid('%s')"%(self.dotted())
51         def shortname(self):
52                 " Returns short name if any "
53                 return libcrypto.OBJ_nid2sn(self.nid)
54         def longname(self):
55                 " Returns logn name if any "
56                 return  libcrypto.OBJ_nid2ln(self.nid)
57         def dotted(self):
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)
62                 return buf.value
63
64 def create(dotted,shortname,longname):
65         """
66                 Creates new OID in the database
67
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
71
72                 @returns Oid object corresponding to new OID
73                 
74                 This function should be used with exreme care. Whenever
75                 possible, it is better to add new OIDs via OpenSSL configuration
76                 file
77
78                 Results of calling this function twice for same OIDor for
79                 Oid alredy in database are undefined
80         """
81         nid=libcrypto.OBJ_create(dotted,shortname,longname)
82         if nid == 0:
83                 raise LibCryptoError("Problem adding new OID to the  database")
84         return Oid(nid)
85
86 def cleanup():
87         """
88                 Removes all the objects, dynamically added by current
89                 application from database.
90         """
91         libcrypto.OBJ_cleanup()
92
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)