commit 2f5101f1015f44b33a67e0abfadcb976ca8d0c50
Author: Stanislav Král <stanislav.kral3@gmail.com>
Date:   Tue Apr 6 00:52:54 2021 +0200

    Re #8472 - Added a parameter to the craete_sscrt method in CryptographyService in order to be able to specify tha number of days for which the generated certificate will be valid.

diff --git a/src/services/certificate_service.py b/src/services/certificate_service.py
index 9559ffe..c88f12a 100644
--- a/src/services/certificate_service.py
+++ b/src/services/certificate_service.py
@@ -21,7 +21,7 @@ class CertificateService:
 
     # TODO usages present in method parameters but not in class diagram
     def create_root_ca(self, key: PrivateKey, subject: Subject, extensions: str = "", config: str = "",
-                       usages=None):
+                       usages=None, days=30):
         """
         Creates a root CA certificate based on the given parameters.
         :param key: Private key to be used when generating the certificate
@@ -29,6 +29,7 @@ class CertificateService:
         :param config: String containing the configuration to be used
         :param extensions: Name of the section in the configuration representing extensions
         :param usages: A dictionary containing usages of the certificate to be generated (see constants.py)
+        :param days: Number of days for which the generated cert. will be considered valid
         :return: An instance of Certificate class representing the generated root CA cert
         """
         if usages is None:
@@ -36,7 +37,7 @@ class CertificateService:
 
         # create a new self signed  certificate
         cert_pem = self.cryptography_service.create_sscrt(subject, key.private_key, key_pass=key.password,
-                                                          extensions=extensions, config=config)
+                                                          extensions=extensions, config=config, days=days)
         # specify CA usage
         usages[CA_ID] = True
 
diff --git a/src/services/cryptography.py b/src/services/cryptography.py
index 635c277..dad7e5c 100644
--- a/src/services/cryptography.py
+++ b/src/services/cryptography.py
@@ -92,7 +92,7 @@ class CryptographyService:
             return self.__run_for_output(
                 ["genrsa", PRIVATE_KEY_ENCRYPTION_METHOD, "-passout", f"pass:{passphrase}", "2048"]).decode()
 
-    def create_sscrt(self, subject, key, config="", extensions="", key_pass=None):
+    def create_sscrt(self, subject, key, config="", extensions="", key_pass=None, days=30):
         """
         Creates a root CA
 
@@ -101,6 +101,7 @@ class CryptographyService:
         :param config: string containing the configuration to be used
         :param extensions: name of the section in the configuration representing extensions
         :param key_pass: passphrase of the private key
+        :param days: number of days for which the certificate will be valid
 
         :return: string containing the generated certificate in PEM format
         """
@@ -110,7 +111,7 @@ class CryptographyService:
         subj = self.__subject_to_param_format(subject)
 
         with TemporaryFile("openssl.conf", config) as conf_path:
-            args = ["req", "-x509", "-new", "-subj", subj,
+            args = ["req", "-x509", "-new", "-subj", subj, "-days", f"{days}",
                     "-key", "-"]
             if len(config) > 0:
                 args.extend(["-config", conf_path])
diff --git a/tests/unit_tests/services/cryptography/self_signed_cert_test.py b/tests/unit_tests/services/cryptography/self_signed_cert_test.py
index 83a7b46..67585ca 100644
--- a/tests/unit_tests/services/cryptography/self_signed_cert_test.py
+++ b/tests/unit_tests/services/cryptography/self_signed_cert_test.py
@@ -110,3 +110,33 @@ def test_create_sscrt_incorrect_passphrase(service):
     with pytest.raises(CryptographyException) as e:
         service.create_sscrt(Subject(common_name="Topnax", country="CZ"), private_key)
     assert "bad decrypt" in e.value.message
+
+
+def test_create_sscrt_days(service):
+    # create a self signed certificate using configuration and extensions
+    private_key = service.create_private_key(passphrase="foobar")
+
+    cert = service.create_sscrt(Subject(common_name="Topnax"), private_key, key_pass="foobar",
+                                days=30)
+
+    cert_2 = service.create_sscrt(Subject(common_name="Topnax"), private_key,
+                                  key_pass="foobar",
+                                  days=31)
+
+    cert_3 = service.create_sscrt(Subject(common_name="Topnax"), private_key,
+                                  key_pass="foobar",
+                                  days=32)
+
+    args = ["openssl", "x509", "-noout", "-enddate", "-in", "-"]
+    cert_printed = subprocess.check_output(args,
+                                           input=bytes(cert, encoding="utf-8"), stderr=subprocess.STDOUT).decode()
+    cert_printed_2 = subprocess.check_output(args,
+                                             input=bytes(cert_2, encoding="utf-8"), stderr=subprocess.STDOUT).decode()
+
+    cert_printed_3 = subprocess.check_output(args,
+                                             input=bytes(cert_3, encoding="utf-8"), stderr=subprocess.STDOUT).decode()
+
+    # TODO improve this test by parsing the date
+    assert cert_printed != cert_printed_2
+    assert cert_printed_2 != cert_printed_3
+    assert cert_printed != cert_printed_3
