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
8 class X509Error(LibCryptoError):
13 def __init__(self,ptr):
16 libcrypto.X509_NAME_free(self.ptr)
19 libcrypto.X509_NAME_print_ex(b.bio,self.ptr,0,PRING_FLAG)
20 return str(b).decode("utf-8")
23 return libcrypto.X509_NAME_entry_count(self.ptr)
25 def __getattr__(self,key):
26 if isinstance(key,Oid):
27 # Return list of strings
29 elif isinstance(key,int):
30 # Return OID, sting tuple
33 raise TypeError("X509 name can be indexed with oids and numbers only")
35 def __setattr__(self,key,val):
38 def __init__(self,ptr):
41 libcrypto.X509_NAME_free(self.ptr)
45 return libcrypto.X509_NAME_entry_count(self.ptr)
47 def __getattr__(self,key):
49 def __setattr__(self,key,val):
56 def __init__(self,data=None,ptr=None,format="PEM"):
59 raise TypeError("Cannot use data and ptr simultaneously")
62 raise TypeError("data argument is required")
66 self.cert=libcrypto.PEM_read_bio_X509(b.bio,None,None,None)
68 self.cert=libcrypto.d2i_X509_bio(b.bio,None)
70 raise X509Error("error reading certificate")
72 libcrypto.X509_free(self.cert)
74 """ Returns der string of the certificate """
76 if libcrypto.i2d_X509_bio(b.bio,self.cert)==0:
77 raise X509Error("error serializing certificate")
80 """EVP PKEy object of certificate public key"""
81 return PKey(ptr=libcrypto.X509_get_pubkey(self.cert,False))
83 """ Verify self on given issuer key """
86 """ X509Name for certificate subject name """
87 return X509Name(libcrypto.X509_get_subject_name(self.cert))
90 """ X509Name for certificate issuer name """
91 return X509Name(libcrypto.X509_get_issuer_name(self.cert))
94 """ Serial number of certificate as integer """
98 """ Certificate validity period start date """
102 """ Certificate validity period end date """
104 def extensions(self):