]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blobdiff - ctypescrypto/x509.py
Fix variable keylength on 64 bit env
[oss/ctypescrypto.git] / ctypescrypto / x509.py
index 28c587c1673e879c6a5263cdc967695fe7dd5128..82c0ec749262216a45e8901957113ccc389e156b 100644 (file)
@@ -1,22 +1,38 @@
 from ctypes import c_void_p
 from ctypescrypto.bio import Membio
+from ctypescrypto.pkey import Pkey
+from ctypescrypto.oid import oid
 from ctypescrypto.exception import LibCryptoError
 from crypescrypto import libcrypto
 
+class X509Error(LibCryptoError):
+       pass
+
+
 class X509Name:
        def __init__(self,ptr):
                self.ptr=ptr
        def __del__(self):
                libcrypto.X509_NAME_free(self.ptr)
        def __str__(self):
+               b=Membio()
+               libcrypto.X509_NAME_print_ex(b.bio,self.ptr,0,PRING_FLAG)
+               return str(b).decode("utf-8")
 
        def __len__(self):
                return libcrypto.X509_NAME_entry_count(self.ptr)
 
        def __getattr__(self,key):
-         
-       def __setattr__(self,key,val):
+               if isinstatce(key,Oid):
+               # Return list of strings
+               
+               elif isinstance(key,int):
+               # Return OID, sting tuple
+               else:
+                       raise TypeError("X509 name can be indexed with oids and numbers only")
 
+       def __setattr__(self,key,val):
+               pass
 class X509_extlist:
        def __init__(self,ptr):
                self.ptr=ptr
@@ -36,30 +52,50 @@ class X509_extlist:
 
 
 class X509:
-       def __init__(self,ptr):
-               self.cert = ptr
+       def __init__(self,data=None,ptr=None,format="PEM"):
+               if ptr is not None:
+                       if data is not None: 
+                               raise TypeError("Cannot use data and ptr simultaneously")
+                       self.cert = ptr
+               elif data is None:
+                               raise TypeError("data argument is required")
+                       b=Membio(data)
+                       if format == "PEM":
+                               self.cert=libcrypto.PEM_read_bio_X509(b.bio,None,None,None)
+                       else:
+                               self.cert=libcrypto.d2i_X509_bio(b.bio,None)
+                       if self.cert is None:
+                               raise X509Error("error reading certificate")
        def __del__(self):
                libcrypto.X509_free(self.cert)
        def __str__(self):
                """ Returns der string of the certificate """
+               b=Membio()
+               if libcrypto.i2d_X509_bio(b.bio,self.cert)==0:
+                       raise X509Error("error serializing certificate")
+       @property
        def pubkey(self):
-               """ Returns EVP PKEy object of certificate public key"""
-               return PKey(libcrypto.X509_get_pubkey(self.cert,False)
+               """EVP PKEy object of certificate public key"""
+               return PKey(ptr=libcrypto.X509_get_pubkey(self.cert,False))
        def verify(self,key):   
                """ Verify self on given issuer key """
-       def frompem(s):
-               """ Create X509 object from pem string """
-       def fromder(s):
-               """ Create X509 object from der string """
+       @property
        def subject(self):
+               """ X509Name for certificate subject name """
                return X509Name(libcrypto.X509_get_subject_name(self.cert))
+       @property
        def issuer(self):
+               """ X509Name for certificate issuer name """
                return X509Name(libcrypto.X509_get_issuer_name(self.cert))
+       @property
        def serial(self):
+               """ Serial number of certificate as integer """
                return
-
+       @property
        def startDate(self):
-
+               """ Certificate validity period start date """
+       @property
        def endDate(self);
+               """ Certificate validity period end date """
 
        def extensions(self):