]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blob - ctypescrypto/x509.py
Fix variable keylength on 64 bit env
[oss/ctypescrypto.git] / ctypescrypto / x509.py
1 from ctypes import c_void_p
2 from ctypescrypto.bio import Membio
3 from ctypescrypto.pkey import Pkey
4 from ctypescrypto.oid import oid
5 from ctypescrypto.exception import LibCryptoError
6 from crypescrypto import libcrypto
7
8 class X509Error(LibCryptoError):
9         pass
10
11
12 class X509Name:
13         def __init__(self,ptr):
14                 self.ptr=ptr
15         def __del__(self):
16                 libcrypto.X509_NAME_free(self.ptr)
17         def __str__(self):
18                 b=Membio()
19                 libcrypto.X509_NAME_print_ex(b.bio,self.ptr,0,PRING_FLAG)
20                 return str(b).decode("utf-8")
21
22         def __len__(self):
23                 return libcrypto.X509_NAME_entry_count(self.ptr)
24
25         def __getattr__(self,key):
26                 if isinstatce(key,Oid):
27                 # Return list of strings
28                 
29                 elif isinstance(key,int):
30                 # Return OID, sting tuple
31                 else:
32                         raise TypeError("X509 name can be indexed with oids and numbers only")
33
34         def __setattr__(self,key,val):
35                 pass
36 class X509_extlist:
37         def __init__(self,ptr):
38                 self.ptr=ptr
39         def __del__(self):
40                 libcrypto.X509_NAME_free(self.ptr)
41         def __str__(self):
42
43         def __len__(self):
44                 return libcrypto.X509_NAME_entry_count(self.ptr)
45
46         def __getattr__(self,key):
47           
48         def __setattr__(self,key,val):
49
50
51         
52
53
54 class X509:
55         def __init__(self,data=None,ptr=None,format="PEM"):
56                 if ptr is not None:
57                         if data is not None: 
58                                 raise TypeError("Cannot use data and ptr simultaneously")
59                         self.cert = ptr
60                 elif data is None:
61                                 raise TypeError("data argument is required")
62                         b=Membio(data)
63                         if format == "PEM":
64                                 self.cert=libcrypto.PEM_read_bio_X509(b.bio,None,None,None)
65                         else:
66                                 self.cert=libcrypto.d2i_X509_bio(b.bio,None)
67                         if self.cert is None:
68                                 raise X509Error("error reading certificate")
69         def __del__(self):
70                 libcrypto.X509_free(self.cert)
71         def __str__(self):
72                 """ Returns der string of the certificate """
73                 b=Membio()
74                 if libcrypto.i2d_X509_bio(b.bio,self.cert)==0:
75                         raise X509Error("error serializing certificate")
76         @property
77         def pubkey(self):
78                 """EVP PKEy object of certificate public key"""
79                 return PKey(ptr=libcrypto.X509_get_pubkey(self.cert,False))
80         def verify(self,key):   
81                 """ Verify self on given issuer key """
82         @property
83         def subject(self):
84                 """ X509Name for certificate subject name """
85                 return X509Name(libcrypto.X509_get_subject_name(self.cert))
86         @property
87         def issuer(self):
88                 """ X509Name for certificate issuer name """
89                 return X509Name(libcrypto.X509_get_issuer_name(self.cert))
90         @property
91         def serial(self):
92                 """ Serial number of certificate as integer """
93                 return
94         @property
95         def startDate(self):
96                 """ Certificate validity period start date """
97         @property
98         def endDate(self);
99                 """ Certificate validity period end date """
100
101         def extensions(self):