]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blob - ctypescrypto/oid.py
Merge branch 'master' of https://github.com/vbwagner/ctypescrypto
[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
13 __all__ = ['Oid','create','cleanup']
14
15 class Oid:
16         """
17                 Represents an OID. It can be consturucted by textual
18                 representation like Oid("commonName") or Oid("CN"),
19                 dotted-decimal Oid("1.2.3.4") or using OpenSSL numeric
20                 identifer (NID), which is typically returned or required by 
21                 OpenSSL API functions. If object is consturcted from textual
22                 representation which is not present in the database, it fails
23                 with ValueError
24
25                 attribute nid - contains object nid.
26
27
28         """
29
30         def __init__(self,value):
31                 " Object constuctor. Accepts string or integer"
32                 if type(value) == type(""):
33                         self.nid=libcrypto.OBJ_txt2nid(value)
34                         if self.nid==0:
35                                 raise ValueError("Cannot find object %s in the database"%(value))
36                 elif type(value) == type(0):
37                         cn=libcrypto.OBJ_nid2sn(value)
38                         if cn is None:
39                                 raise ValueError("No such nid %d in the database"%(value))
40                         self.nid=value
41                 else:
42                         raise TypeError("Cannot convert this type to object identifier")
43         def __hash__(self):
44                 " Returns NID "
45                 return self.nid
46         def __cmp__(self,other):
47                 " Compares NIDs of two objects "
48                 return self.nid-other.nid
49         def __str__(self):
50                 " Default string representation of Oid is dotted-decimal"
51                 return self.dotted()
52         def __repr__(self):
53                 return "Oid('%s')"%(self.dotted())
54         def shortname(self):
55                 " Returns short name if any "
56                 return libcrypto.OBJ_nid2sn(self.nid)
57         def longname(self):
58                 " Returns logn name if any "
59                 return  libcrypto.OBJ_nid2ln(self.nid)
60         def dotted(self):
61                 " Returns dotted-decimal reperesntation "
62                 obj=libcrypto.OBJ_nid2obj(self.nid)
63                 buf=create_string_buffer(256)
64                 libcrypto.OBJ_obj2txt(buf,256,obj,1)
65                 return buf.value
66
67 def create(dotted,shortname,longname):
68         """
69                 Creates new OID in the database
70
71                 @param dotted - dotted-decimal representation of new OID
72                 @param shortname - short name for new OID
73                 @param longname - long name for new OID
74
75                 @returns Oid object corresponding to new OID
76                 
77                 This function should be used with exreme care. Whenever
78                 possible, it is better to add new OIDs via OpenSSL configuration
79                 file
80
81                 Results of calling this function twice for same OIDor for
82                 Oid alredy in database are undefined
83         """
84         nid=libcrypto.OBJ_create(dotted,shortname,longname)
85         if nid == 0:
86                 raise LibCryptoError("Problem adding new OID to the  database")
87         return Oid(nid)
88
89 def cleanup():
90         """
91                 Removes all the objects, dynamically added by current
92                 application from database.
93         """
94         libcrypto.OBJ_cleanup()
95
96 libcrypto.OBJ_nid2sn.restype=c_char_p
97 libcrypto.OBJ_nid2ln.restype=c_char_p
98 libcrypto.OBJ_nid2obj.restype=c_void_p
99 libcrypto.OBJ_obj2txt.argtypes=(c_char_p,c_int,c_void_p,c_int)
100 libcrypto.OBJ_txt2nid.argtupes=(c_char_p,)
101 libcrypto.OBJ_create.argtypes=(c_char_p,c_char_p,c_char_p)