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")
65 self.cert=libcrypto.PEM_read_bio_X509(b.bio,None,None,None)
67 self.cert=libcrypto.d2i_X509_bio(b.bio,None)
69 raise X509Error("error reading certificate")
71 libcrypto.X509_free(self.cert)
73 """ Returns der string of the certificate """
75 if libcrypto.i2d_X509_bio(b.bio,self.cert)==0:
76 raise X509Error("error serializing certificate")
79 """EVP PKEy object of certificate public key"""
80 return PKey(ptr=libcrypto.X509_get_pubkey(self.cert,False))
82 """ Verify self on given issuer key """
85 """ X509Name for certificate subject name """
86 return X509Name(libcrypto.X509_get_subject_name(self.cert))
89 """ X509Name for certificate issuer name """
90 return X509Name(libcrypto.X509_get_issuer_name(self.cert))
93 """ Serial number of certificate as integer """
97 """ Certificate validity period start date """
101 """ Certificate validity period end date """
103 def extensions(self):