commit a2965e9ab4e1d0113c467e624a8db1bd90701214
Author: Captain_Trojan <sejakm@students.zcu.cz>
Date:   Sun May 2 16:25:49 2021 +0200

    Re #8704 - Added the `extension` parameter to the create endpoint.

diff --git a/src/controllers/certificates_controller.py b/src/controllers/certificates_controller.py
index da031f9..6d1d900 100644
--- a/src/controllers/certificates_controller.py
+++ b/src/controllers/certificates_controller.py
@@ -17,10 +17,13 @@ from src.services.certificate_service import CertificateService, RevocationReaso
     CertificateStatusInvalidException, CertificateNotFoundException, CertificateAlreadyRevokedException, \
     CertificateCannotBeSetToValid
 #  responsibility.
+from src.services.cryptography import CryptographyException
 from src.services.key_service import KeyService
 from src.utils.logger import Logger
 from src.utils.util import dict_to_string
 
+EXTENSIONS = "extensions"
+
 TREE_NODE_TYPE_COUNT = 3
 
 FILTERING = "filtering"
@@ -108,42 +111,51 @@ class CertController:
                     return E_WRONG_PARAMETERS, C_BAD_REQUEST                        # and throw if it is not
                 usages_dict[CertController.KEY_MAP[k]] = v                          # otherwise translate key and set
 
-            key = self.key_service.create_new_key()                                      # TODO pass key
-
-            if CA not in body or body[CA] is None:                                  # if issuer omitted (legal) or none
-                cert = self.certificate_service.create_root_ca(                          # create a root CA
-                    key,
-                    subject,
-                    usages=usages_dict,                                             # TODO ignoring usages -> discussion
-                    days=body[VALIDITY_DAYS]
-                )
-            else:
-                issuer = self.certificate_service.get_certificate(body[CA])              # get base issuer info
-
-                if issuer is None:                                                  # if such issuer does not exist
-                    Logger.error(f"No certificate authority with such unique ID exists 'ID = {key.private_key_id}'.")
-                    self.key_service.delete_key(key.private_key_id)                      # free
-                    return E_NO_ISSUER_FOUND, C_BAD_REQUEST                         # and throw
+            key = self.key_service.create_new_key()                                 # TODO pass key
 
-                issuer_key = self.key_service.get_key(issuer.private_key_id)             # get issuer's key, which must exist
+            extensions = None
+            if EXTENSIONS in body:
+                extensions = body[EXTENSIONS]
 
-                if issuer_key is None:                                              # if it does not
-                    Logger.error(f"Internal server error (corrupted database).")
-                    self.key_service.delete_key(key.private_key_id)                      # free
-                    return E_CORRUPTED_DATABASE, C_INTERNAL_SERVER_ERROR            # and throw
-
-                f = self.certificate_service.create_ca if CA_ID in usages_dict and usages_dict[CA_ID] else \
-                    self.certificate_service.create_end_cert
-
-                # noinspection PyTypeChecker
-                cert = f(                                                           # create inter CA or end cert
-                    key,                                                            # according to whether 'CA' is among
-                    subject,                                                        # the usages' fields
-                    issuer,
-                    issuer_key,
-                    usages=usages_dict,
-                    days=body[VALIDITY_DAYS]
-                )
+            try:
+                if CA not in body or body[CA] is None:                              # if issuer omitted (legal) or none
+                    cert = self.certificate_service.create_root_ca(                 # create a root CA
+                        key,
+                        subject,
+                        usages=usages_dict,                                         # TODO ignoring usages -> discussion
+                        days=body[VALIDITY_DAYS],
+                        extensions=extensions
+                    )
+                else:
+                    issuer = self.certificate_service.get_certificate(body[CA])     # get base issuer info
+
+                    if issuer is None:                                              # if such issuer does not exist
+                        Logger.error(f"No certificate authority with such unique ID exists 'ID = {key.private_key_id}'.")
+                        self.key_service.delete_key(key.private_key_id)             # free
+                        return E_NO_ISSUER_FOUND, C_BAD_REQUEST                     # and throw
+
+                    issuer_key = self.key_service.get_key(issuer.private_key_id)    # get issuer's key, which must exist
+
+                    if issuer_key is None:                                          # if it does not
+                        Logger.error(f"Internal server error (corrupted database).")
+                        self.key_service.delete_key(key.private_key_id)             # free
+                        return E_CORRUPTED_DATABASE, C_INTERNAL_SERVER_ERROR        # and throw
+
+                    f = self.certificate_service.create_ca if CA_ID in usages_dict and usages_dict[CA_ID] else \
+                        self.certificate_service.create_end_cert
+
+                    # noinspection PyArgumentList
+                    cert = f(                                                       # create inter CA or end cert
+                        key,                                                        # according to whether 'CA' is among
+                        subject,                                                    # the usages' fields
+                        issuer,
+                        issuer_key,
+                        usages=usages_dict,
+                        days=body[VALIDITY_DAYS],
+                        extensions=extensions
+                    )
+            except CryptographyException as e:
+                return {"success": False, "data": e.message}, C_INTERNAL_SERVER_ERROR
 
             if cert is not None:
                 return {"success": True,
