]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blob - ctypescrypto/oid.py
Fixes some style. Improved tests coverage for bio,oid,digest and cipher. Prepare...
[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(object):
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 isinstance(value,unicode):
33                         value=value.encode('ascii')
34                 if isinstance(value,str):
35                         self.nid=libcrypto.OBJ_txt2nid(value)
36                         if self.nid==0:
37                                 raise ValueError("Cannot find object %s in the database"%(value))
38                 elif isinstance(value,(int,long)):
39                         cn=libcrypto.OBJ_nid2sn(value)
40                         if cn is None:
41                                 raise ValueError("No such nid %d in the database"%(value))
42                         self.nid=value
43                 else:
44                         raise TypeError("Cannot convert this type to object identifier")
45         def __hash__(self):
46                 " Returns NID "
47                 return self.nid
48         def __cmp__(self,other):
49                 " Compares NIDs of two objects "
50                 return self.nid-other.nid
51         def __str__(self):
52                 " Default string representation of Oid is dotted-decimal"
53                 return self.dotted()
54         def __repr__(self):
55                 return "Oid('%s')"%(self.dotted())
56         def shortname(self):
57                 " Returns short name if any "
58                 return libcrypto.OBJ_nid2sn(self.nid)
59         def longname(self):
60                 " Returns logn name if any "
61                 return  libcrypto.OBJ_nid2ln(self.nid)
62         def dotted(self):
63                 " Returns dotted-decimal reperesentation "
64                 obj=libcrypto.OBJ_nid2obj(self.nid)
65                 buf=create_string_buffer(256)
66                 libcrypto.OBJ_obj2txt(buf,256,obj,1)
67                 return buf.value
68         @staticmethod
69         def fromobj(obj):
70                 """
71                 Creates an OID object from the pointer to ASN1_OBJECT c structure.
72                 Strictly for internal use
73                 """
74                 nid=libcrypto.OBJ_obj2nid(obj)
75                 if nid==0:
76                         buf=create_string_buffer(80)
77                         l=libcrypto.OBJ_obj2txt(buf,80,obj,1)
78                         oid=create(buf[0:l],buf[0:l],buf[0:l])
79                 else:
80                         oid=Oid(nid)
81                 return oid
82
83 def create(dotted,shortname,longname):
84         """
85                 Creates new OID in the database
86
87                 @param dotted - dotted-decimal representation of new OID
88                 @param shortname - short name for new OID
89                 @param longname - long name for new OID
90
91                 @returns Oid object corresponding to new OID
92                 
93                 This function should be used with exreme care. Whenever
94                 possible, it is better to add new OIDs via OpenSSL configuration
95                 file
96
97                 Results of calling this function twice for same OIDor for
98                 Oid alredy in database are undefined
99         """
100         nid=libcrypto.OBJ_create(dotted,shortname,longname)
101         if nid == 0:
102                 raise LibCryptoError("Problem adding new OID to the  database")
103         return Oid(nid)
104
105 def cleanup():
106         """
107                 Removes all the objects, dynamically added by current
108                 application from database.
109         """
110         libcrypto.OBJ_cleanup()
111
112 libcrypto.OBJ_nid2sn.restype=c_char_p
113 libcrypto.OBJ_nid2ln.restype=c_char_p
114 libcrypto.OBJ_nid2obj.restype=c_void_p
115 libcrypto.OBJ_obj2txt.argtypes=(c_char_p,c_int,c_void_p,c_int)
116 libcrypto.OBJ_txt2nid.argtupes=(c_char_p,)
117 libcrypto.OBJ_create.argtypes=(c_char_p,c_char_p,c_char_p)