From 1dc78c2bf6f449a89d48387ceea5e6de98a29c3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20G=C3=B6cke?= Date: Sat, 21 Jan 2023 17:46:57 +0100 Subject: [PATCH] added get_certificate_expiry_date and certificate_is_expired --- .vscode/spellright.dict | 1 + License.txt | 1 + .../ScriptCollection/GeneralUtilities.py | 17 +++++++++++++++++ ScriptCollection/requirements.txt | 1 + ScriptCollection/setup.cfg | 1 + 5 files changed, 21 insertions(+) diff --git a/.vscode/spellright.dict b/.vscode/spellright.dict index 519d1523..487a7fc2 100644 --- a/.vscode/spellright.dict +++ b/.vscode/spellright.dict @@ -35,3 +35,4 @@ docfx cyclonedx gitversion dockerfile +pyOpenSSL diff --git a/License.txt b/License.txt index af9d4d07..b30359e3 100644 --- a/License.txt +++ b/License.txt @@ -6,6 +6,7 @@ ScriptCollection uses dependencies which are released under their own license: -keyboard is licensed under the terms of the MIT-license (see appendix 1) -lxml is licensed under the terms of the BSD-license (see https://pypi.org/project/lxml ) -ntplib is licensed under the terms of the MIT-license (see appendix 1) +-pyOpenSSL is licensed under the terms of the Apache License, Version 2.0-license (see https://raw.githubusercontent.com/pyca/pyopenssl/main/LICENSE ) -PyPDF2 is licensed under the terms of its own license (see appendix 2) -pycdlib is licensed under the terms of the LGPL-v2.1-license (see appendix 3) -qrcode is licensed under the terms of the BSD-license (see https://pypi.org/project/qrcode ) diff --git a/ScriptCollection/ScriptCollection/GeneralUtilities.py b/ScriptCollection/ScriptCollection/GeneralUtilities.py index 5c8280f3..8f771b0f 100644 --- a/ScriptCollection/ScriptCollection/GeneralUtilities.py +++ b/ScriptCollection/ScriptCollection/GeneralUtilities.py @@ -17,6 +17,7 @@ from shutil import copyfile import typing from defusedxml.minidom import parse +from OpenSSL import crypto class GeneralUtilities: @@ -791,3 +792,19 @@ def generate_password(length: int = 16, alphabet: str = None) -> None: def assert_condition(condition: bool, information: str) -> None: if (not condition): raise ValueError("Condition failed. "+information) + + @staticmethod + @check_arguments + def get_certificate_expiry_date(certificate_file: str) -> datetime: + with open(certificate_file, encoding="utf-8") as certificate_file_content: + cert = crypto.load_certificate(crypto.FILETYPE_PEM, certificate_file_content.read()) + date_as_bytes = cert.get_notAfter() + date_as_string = date_as_bytes.decode("utf-8") + result = datetime.strptime(date_as_string, '%Y%m%d%H%M%SZ') + return result + + + @staticmethod + @check_arguments + def certificate_is_expired(certificate_file: str) -> bool: + return GeneralUtilities.get_certificate_expiry_date(certificate_file) < datetime.now() diff --git a/ScriptCollection/requirements.txt b/ScriptCollection/requirements.txt index f3a46d64..1c0488c9 100644 --- a/ScriptCollection/requirements.txt +++ b/ScriptCollection/requirements.txt @@ -7,6 +7,7 @@ lxml ntplib>=0.4.0 pycdlib>=1.13.0 pylint>=2.15.10 +pyOpenSSL>=23.0.0 PyPDF2>=3.0.1 pytest>=7.2.1 qrcode>=7.3.1 diff --git a/ScriptCollection/setup.cfg b/ScriptCollection/setup.cfg index c341ec3b..0238da65 100644 --- a/ScriptCollection/setup.cfg +++ b/ScriptCollection/setup.cfg @@ -40,6 +40,7 @@ install_requires = ntplib>=0.4.0 pycdlib>=1.13.0 pylint>=2.15.10 + pyOpenSSL>=23.0.0 PyPDF2>=3.0.1 pytest>=7.2.1 qrcode>=7.3.1