]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blob - ctypescrypto/x509.py
384bb271c57478577324bb082d38e2aeba134959
[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 ctypescrypto 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 isinstance(key,Oid):
27                 # Return list of strings
28                         raise NotImpemented     
29                 elif isinstance(key,int):
30                         # Return OID, sting tuple
31                         raise NotImplemented
32                 else:
33                         raise TypeError("X509 name can be indexed with oids and numbers only")
34
35         def __setattr__(self,key,val):
36                 pass
37 class X509_extlist:
38         def __init__(self,ptr):
39                 self.ptr=ptr
40         def __del__(self):
41                 libcrypto.X509_NAME_free(self.ptr)
42         def __str__(self):
43                 raise NotImplemented
44         def __len__(self):
45                 return libcrypto.X509_NAME_entry_count(self.ptr)
46
47         def __getattr__(self,key):
48                 raise NotImplemented
49         def __setattr__(self,key,val):
50                 raise NotImplemented
51
52         
53
54
55 class X509:
56         def __init__(self,data=None,ptr=None,format="PEM"):
57                 if ptr is not None:
58                         if data is not None: 
59                                 raise TypeError("Cannot use data and ptr simultaneously")
60                         self.cert = ptr
61                 elif data is None:
62                         raise TypeError("data argument is required")
63                         b=Membio(data)
64                         if format == "PEM":
65                                 self.cert=libcrypto.PEM_read_bio_X509(b.bio,None,None,None)
66                         else:
67                                 self.cert=libcrypto.d2i_X509_bio(b.bio,None)
68                         if self.cert is None:
69                                 raise X509Error("error reading certificate")
70         def __del__(self):
71                 libcrypto.X509_free(self.cert)
72         def __str__(self):
73                 """ Returns der string of the certificate """
74                 b=Membio()
75                 if libcrypto.i2d_X509_bio(b.bio,self.cert)==0:
76                         raise X509Error("error serializing certificate")
77         @property
78         def pubkey(self):
79                 """EVP PKEy object of certificate public key"""
80                 return PKey(ptr=libcrypto.X509_get_pubkey(self.cert,False))
81         def verify(self,key):   
82                 """ Verify self on given issuer key """
83         @property
84         def subject(self):
85                 """ X509Name for certificate subject name """
86                 return X509Name(libcrypto.X509_get_subject_name(self.cert))
87         @property
88         def issuer(self):
89                 """ X509Name for certificate issuer name """
90                 return X509Name(libcrypto.X509_get_issuer_name(self.cert))
91         @property
92         def serial(self):
93                 """ Serial number of certificate as integer """
94                 return
95         @property
96         def startDate(self):
97                 """ Certificate validity period start date """
98                 raise NotImplemented
99         @property
100         def endDate(self):
101                 """ Certificate validity period end date """
102                 raise NotImplemented
103         def extensions(self):
104                 raise NotImplemented