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