]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blob - ctypescrypto/x509.py
Added pkey serialization (untested) and started to implement x509
[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.exception import LibCryptoError
5 from crypescrypto import libcrypto
6
7 class X509Error(LibCryptoError):
8         pass
9
10
11 class X509Name:
12         def __init__(self,ptr):
13                 self.ptr=ptr
14         def __del__(self):
15                 libcrypto.X509_NAME_free(self.ptr)
16         def __str__(self):
17                 b=Membio()
18                 libcrypto.X509_NAME_print_ex(b.bio,self.ptr,0,PRING_FLAG)
19                 return str(b).decode("utf-8")
20
21         def __len__(self):
22                 return libcrypto.X509_NAME_entry_count(self.ptr)
23
24         def __getattr__(self,key):
25                 
26         def __setattr__(self,key,val):
27
28 class X509_extlist:
29         def __init__(self,ptr):
30                 self.ptr=ptr
31         def __del__(self):
32                 libcrypto.X509_NAME_free(self.ptr)
33         def __str__(self):
34
35         def __len__(self):
36                 return libcrypto.X509_NAME_entry_count(self.ptr)
37
38         def __getattr__(self,key):
39           
40         def __setattr__(self,key,val):
41
42
43         
44
45
46 class X509:
47         def __init__(self,data=None,ptr=None,format="PEM"):
48                 if ptr is not None:
49                         if data is not None: 
50                                 raise TypeError("Cannot use data and ptr simultaneously")
51                         self.cert = ptr
52                 elif data is None:
53                                 raise TypeError("data argument is required")
54                         b=Membio(data)
55                         if format == "PEM":
56                                 self.cert=libcrypto.PEM_read_bio_X509(b.bio,None,None,None)
57                         else:
58                                 self.cert=libcrypto.d2i_X509_bio(b.bio,None)
59                         if self.cert is None:
60                                 raise X509Error("error reading certificate")
61         def __del__(self):
62                 libcrypto.X509_free(self.cert)
63         def __str__(self):
64                 """ Returns der string of the certificate """
65                 b=Membio()
66                 if libcrypto.i2d_X509_bio(b.bio,self.cert)==0:
67                         raise X509Error("error serializing certificate")
68         def pubkey(self):
69                 """ Returns EVP PKEy object of certificate public key"""
70                 return PKey(ptr=libcrypto.X509_get_pubkey(self.cert,False))
71         def verify(self,key):   
72                 """ Verify self on given issuer key """
73
74         def subject(self):
75                 return X509Name(libcrypto.X509_get_subject_name(self.cert))
76         def issuer(self):
77                 return X509Name(libcrypto.X509_get_issuer_name(self.cert))
78         def serial(self):
79                 return
80
81         def startDate(self):
82
83         def endDate(self);
84
85         def extensions(self):