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
7 class X509Error(LibCryptoError):
12 def __init__(self,ptr):
15 libcrypto.X509_NAME_free(self.ptr)
18 libcrypto.X509_NAME_print_ex(b.bio,self.ptr,0,PRING_FLAG)
19 return str(b).decode("utf-8")
22 return libcrypto.X509_NAME_entry_count(self.ptr)
24 def __getattr__(self,key):
26 def __setattr__(self,key,val):
29 def __init__(self,ptr):
32 libcrypto.X509_NAME_free(self.ptr)
36 return libcrypto.X509_NAME_entry_count(self.ptr)
38 def __getattr__(self,key):
40 def __setattr__(self,key,val):
47 def __init__(self,data=None,ptr=None,format="PEM"):
50 raise TypeError("Cannot use data and ptr simultaneously")
53 raise TypeError("data argument is required")
56 self.cert=libcrypto.PEM_read_bio_X509(b.bio,None,None,None)
58 self.cert=libcrypto.d2i_X509_bio(b.bio,None)
60 raise X509Error("error reading certificate")
62 libcrypto.X509_free(self.cert)
64 """ Returns der string of the certificate """
66 if libcrypto.i2d_X509_bio(b.bio,self.cert)==0:
67 raise X509Error("error serializing certificate")
69 """ Returns EVP PKEy object of certificate public key"""
70 return PKey(ptr=libcrypto.X509_get_pubkey(self.cert,False))
72 """ Verify self on given issuer key """
75 return X509Name(libcrypto.X509_get_subject_name(self.cert))
77 return X509Name(libcrypto.X509_get_issuer_name(self.cert))