python-3.xopensslpyopenssl

Python3 - Issue with module pyOpenSSL


I have trouble with the module OpenSSL in python version 3.8.13.

Here is my code

from datetime import datetime
import OpenSSL
import ssl
arraySite=["osb.vn.prod","api-gateway.pdcvn1.vn.prod","ofs.vn.prod","bsl.vn.prod","lcs.vn.prod"]
for site in arraySite:
   cert=ssl.get_server_certificate((site, 443))
   x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
   expiredDate=datetime.strptime(x509.get_notAfter().decode('ascii'), '%Y%m%d%H%M%SZ').strftime("%Y-%m-%d %H:%M:%S")
   today = datetime.today()
   datetime_object = datetime.strptime(expiredDate, '%Y-%m-%d %H:%M:%S')
   rangeExpired = datetime_object - today
   days = rangeExpired.days
   if(days<80):
       print("ALLERT!! Cert domain "+ site +" will be expried on " + expiredDate + ". We have "+ str(rangeExpired) + " remaining")

I don't know why this code run success on my laptop, but it failed on my server. You can see this figure below. Note:

  1. Laptop

Env: python version 3.8.10 - openssl version OpenSSL 1.1.1f enter image description here 2. Server

Env: CentOS 7 - python version 3.8.13 - openssl version OpenSSL 1.0.2k-fips enter image description here

Thank you.


Solution

  • I think this error was caused by the OpenSSL version.

    I used this link to upgrade the OpenSSL version and it worked: https://gist.github.com/fernandoaleman/5459173e24d59b45ae2cfc618e20fe06 enter image description here