commit 4a40b0d2750690823678610df895a2e66996f33b
Author: Stanislav Král <stanislav.kral3@gmail.com>
Date:   Sun Apr 4 00:18:46 2021 +0200

    Re #8472 - WIP commit of adding a method capable of parsing a Subject from a cert PEM

diff --git a/src/services/certificate_service.py b/src/services/certificate_service.py
new file mode 100644
index 0000000..a5e3cb2
--- /dev/null
+++ b/src/services/certificate_service.py
@@ -0,0 +1,20 @@
+from src.dao.certificate_repository import CertificateRepository
+from src.dao.private_key_repository import PrivateKeyRepository
+from src.model.certificate import Certificate
+from src.model.subject import Subject
+from src.services.cryptography import CryptographyService
+
+
+class CertificateService:
+
+    def __init__(self, cryptography_service: CryptographyService, certificate_repository: CertificateRepository):
+        self.cryptography_service = cryptography_service
+        self.certificate_repository = certificate_repository
+
+    # TODO key passphrase is not present in class diagram
+    def create_root_ca(self, key: PrivateKeyRepository, subject: Subject, extensions: str, config: str,
+                       key_passphrase: str):
+        cert_pem = self.cryptography_service.create_sscrt(subject, key, key_pass=key_passphrase, extensions=extensions,
+                                                      config=config)
+
+        certificate = Certificate(-1, subject.common_name, subject)
diff --git a/src/services/cryptography.py b/src/services/cryptography.py
index 27dcf50..450cc31 100644
--- a/src/services/cryptography.py
+++ b/src/services/cryptography.py
@@ -1,4 +1,5 @@
 import subprocess
+import re
 
 # encryption method to be used when generating private keys
 from src.utils.temporary_file import TemporaryFile
@@ -225,6 +226,22 @@ class CryptographyService:
             # the process failed because of some other reason (incorrect cert format)
             raise CryptographyException(OPENSSL_EXECUTABLE, args, err.decode())
 
+    def parse_cert_pem(self, cert_pem):
+        args = ["x509", "-noout", "-text", "-in", "-"]
+
+        result = self.__run_for_output(args, proc_input=bytes(cert_pem, encoding="utf-8")).decode()
+        match = re.search(r"Subject:\s(.*)", result)
+        pass
+        # TODO use logger
+        if match is None:
+            print(f"Could not find subject to parse: {result}")
+        else:
+            found = re.findall(r"\s?([^=\s]+)\s?=\s?([^,\n]+)", match)
+            print(found)
+            for pair in found:
+                print(pair)
+
+
 
 class CryptographyException(Exception):
 
diff --git a/tests/services/cryptography/parse_cert_pem_test.py b/tests/services/cryptography/parse_cert_pem_test.py
new file mode 100644
index 0000000..e13af7e
--- /dev/null
+++ b/tests/services/cryptography/parse_cert_pem_test.py
@@ -0,0 +1,41 @@
+def test_parse_cert_pem(service):
+    cert_pem = """
+-----BEGIN CERTIFICATE-----
+
+MIIGITCCBAmgAwIBAgIUb7xAdXd6AkevhmeQqy2BASDqv/IwDQYJKoZIhvcNAQEL
+BQAwgZ8xCzAJBgNVBAYTAkNaMRYwFAYDVQQIDA1QaWxzZW4gUmVnaW9uMQ8wDQYD
+VQQHDAZQaWxzZW4xFjAUBgNVBAoMDVJvb3RpbmcgUm9vdHMxHDAaBgNVBAsME0Rl
+cGFydG1lbnQgb2YgUk9vdHMxFDASBgNVBAMMC01haW4gUm9vdGVyMRswGQYJKoZI
+hvcNAQkBFgxyb290QHJvb3QuY3owHhcNMjEwMzIxMTAwMTUyWhcNMjYwMzIxMTAw
+MTUyWjCBnzELMAkGA1UEBhMCQ1oxFjAUBgNVBAgMDVBpbHNlbiBSZWdpb24xDzAN
+BgNVBAcMBlBpbHNlbjEWMBQGA1UECgwNUm9vdGluZyBSb290czEcMBoGA1UECwwT
+RGVwYXJ0bWVudCBvZiBST290czEUMBIGA1UEAwwLTWFpbiBSb290ZXIxGzAZBgkq
+hkiG9w0BCQEWDHJvb3RAcm9vdC5jejCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCC
+AgoCggIBAMKozynv+ja1VkNWpldsrl6tEGYrkNuG9umyqF0ZOZmzWzR7PiszV8DW
+o+OQ3SY7MQ7o3qoE/pSiaApmNFxgarWvGxnVgouncrai1AKB92tFY1VnVfQYICD3
+gdjSzo4Lbfc8+67DHTPc0N70oBZuMueQ6ifUQhrjuVaONwAOsZBdal+VWvctJcrf
+fd+s6Jkgb/qWuld21Bzea36PLmgwoe8/RNyS9yzspC8jwdU68BemAPy9NBf9Q8Is
+0R7aZ0YwKPsdln3lR5GixrNy+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