-class _validity(Structure):
- """ ctypes representation of X509_VAL structure
- needed to access certificate validity period, because openssl
- doesn't provide fuctions for it - only macros
- """
- _fields_ = [('notBefore', c_void_p), ('notAfter', c_void_p)]
-
-class _cinf(Structure):
- """ ctypes representtion of X509_CINF structure
- neede to access certificate data, which are accessable only
- via macros
- """
- _fields_ = [('version', c_void_p),
- ('serialNumber', c_void_p),
- ('sign_alg', c_void_p),
- ('issuer', c_void_p),
- ('validity', POINTER(_validity)),
- ('subject', c_void_p),
- ('pubkey', c_void_p),
- ('issuerUID', c_void_p),
- ('subjectUID', c_void_p),
- ('extensions', c_void_p),
- ]
-
-class _x509(Structure):
- """
- ctypes represntation of X509 structure needed
- to access certificate data which are accesable only via
- macros, not functions
- """
- _fields_ = [('cert_info', POINTER(_cinf)),
- ('sig_alg', c_void_p),
- ('signature', c_void_p),
- # There are a lot of parsed extension fields there
- ]
-_px509 = POINTER(_x509)
+if hasattr(libcrypto,"X509_get_version"):
+
+# If it is OpenSSL 1.1 or above, use accessor functions
+ _X509_get_version = libcrypto.X509_get_version
+ _X509_get_version.restype = c_long
+ _X509_get_version.argtypes = (c_void_p,)
+
+ _X509_get_notBefore=libcrypto.X509_get_notBefore
+ _X509_get_notBefore.restype = c_void_p
+ _X509_get_notBefore.argtypes = (c_void_p,)
+
+ _X509_get_notAfter=libcrypto.X509_get_notAfter
+ _X509_get_notAfter.restype = c_void_p
+ _X509_get_notAfter.argtypes = (c_void_p,)
+else:
+ # Otherwise declare X509 structure internals and define deep poke
+ # functions
+ class _validity(Structure):
+ """ ctypes representation of X509_VAL structure
+ needed to access certificate validity period, because openssl
+ doesn't provide fuctions for it - only macros
+ """
+ _fields_ = [('notBefore', c_void_p), ('notAfter', c_void_p)]
+
+ class _cinf(Structure):
+ """ ctypes representtion of X509_CINF structure
+ neede to access certificate data, which are accessable only
+ via macros
+ """
+ _fields_ = [('version', c_void_p),
+ ('serialNumber', c_void_p),
+ ('sign_alg', c_void_p),
+ ('issuer', c_void_p),
+ ('validity', POINTER(_validity)),
+ ('subject', c_void_p),
+ ('pubkey', c_void_p),
+ ('issuerUID', c_void_p),
+ ('subjectUID', c_void_p),
+ ('extensions', c_void_p),
+ ]
+
+ class _x509(Structure):
+ """
+ ctypes represntation of X509 structure needed
+ to access certificate data which are accesable only via
+ macros, not functions
+ """
+ _fields_ = [('cert_info', POINTER(_cinf)),
+ ('sig_alg', c_void_p),
+ ('signature', c_void_p),
+ # There are a lot of parsed extension fields there
+ ]
+ _px509 = POINTER(_x509)
+ def _X509_get_version(ptr):
+ asn1int = cast(ptr, _px509)[0].cert_info[0].version
+ return libcrypto.ASN1_INTEGER_get(asn1int)
+
+ def _X509_get_notBefore(ptr):
+ # (x)->cert_info->validity->notBefore
+ return cast(ptr, _px509)[0].cert_info[0].validity[0].notBefore
+ def _X509_get_notAfter(ptr):
+ return cast(ptr, _px509)[0].cert_info[0].validity[0].notAfter