commit 6abfb03715e33163c42fb001512986c834ec7462
Author: Stanislav Král <stanislav.kral3@gmail.com>
Date:   Tue Apr 13 10:13:15 2021 +0200

    Re #8573 - Added some CertificateService tests and improved CryptographyService tests of methods that extract public keys

diff --git a/src/services/key_service.py b/src/services/key_service.py
index 820e44e..e103891 100644
--- a/src/services/key_service.py
+++ b/src/services/key_service.py
@@ -68,4 +68,5 @@ class KeyService:
         :param private_key: private key from which a public key should be extracted
         :return: a string containing the extracted public key in PEM format
         """
-        return self.cryptography_service.extract_public_key_from_private_key(private_key.private_key, private_key.password)
+        return self.cryptography_service.extract_public_key_from_private_key(private_key.private_key,
+                                                                             private_key.password)
diff --git a/tests/integration_tests/services/certificate_service_test.py b/tests/integration_tests/services/certificate_service_test.py
index aa812fb..aaa0ad3 100644
--- a/tests/integration_tests/services/certificate_service_test.py
+++ b/tests/integration_tests/services/certificate_service_test.py
@@ -243,3 +243,30 @@ def test_get_subject_from_certificate(private_key_service, certificate_service):
     assert subject.locality is None
     assert subject.state is None
     assert subject.country == "CZ"
+
+
+def test_extract_public_key_from_certificate(private_key_service, certificate_service, cryptography_service):
+    private_key = private_key_service.create_new_key()
+
+    certificate = certificate_service.create_root_ca(private_key, Subject(common_name="CZ"))
+
+    pub_key_extracted_from_cert = certificate_service.get_public_key_from_certificate(certificate)
+
+    expected_pub_key_from_cert = cryptography_service.extract_public_key_from_certificate(certificate.pem_data)
+    expected_pub_key_from_key = cryptography_service.extract_public_key_from_private_key(private_key.private_key)
+
+    assert expected_pub_key_from_cert == expected_pub_key_from_key == pub_key_extracted_from_cert
+
+
+def test_extract_public_key_from_certificate_encrypted(private_key_service, certificate_service, cryptography_service):
+    private_key = private_key_service.create_new_key(passphrase="foobar")
+
+    certificate = certificate_service.create_root_ca(private_key, Subject(common_name="CZ"))
+
+    pub_key_extracted_from_cert = certificate_service.get_public_key_from_certificate(certificate)
+
+    expected_pub_key_from_cert = cryptography_service.extract_public_key_from_certificate(certificate.pem_data)
+    expected_pub_key_from_key = cryptography_service.extract_public_key_from_private_key(private_key.private_key,
+                                                                                         passphrase="foobar")
+
+    assert expected_pub_key_from_cert == expected_pub_key_from_key == pub_key_extracted_from_cert
diff --git a/tests/unit_tests/services/cryptography/conftest.py b/tests/unit_tests/services/cryptography/conftest.py
index 490f2f4..f2374e9 100644
--- a/tests/unit_tests/services/cryptography/conftest.py
+++ b/tests/unit_tests/services/cryptography/conftest.py
@@ -79,7 +79,7 @@ qRz7WctkmA9/SOxoF6EPaEhIyXNjjFOQbjsXVc+i0Bnw2DjY3s5SYJg6yycwdaHz
 
 
 @pytest.fixture
-def certificate_pem():
+def certificate_pk():
     return """
 -----BEGIN CERTIFICATE-----
 MIIDazCCAlOgAwIBAgIUTQwIafNfZdJmqZ+TvhtWT3dKLbMwDQYJKoZIhvcNAQEL
@@ -102,12 +102,40 @@ Fe1UXz7MF0HipLprIwxkH8DsGT5djSQ4jCLGnLL2h5DHW/9HY23OgAMSkpJbIo6k
 NyJXVOPghVAqaFbgaeBsXkL3QQYVR5wQl7delo0dn36crlckUPi+13INDhnLYgq7
 GO9Jbty2Qeomxyf+i+SB
 -----END CERTIFICATE-----
+    """, """
+-----BEGIN RSA PRIVATE KEY-----
+MIIEpAIBAAKCAQEA4fzrmp2RO1YNgsbyVEgGPlt5QxwjiVC6hGsXA0TX698Xd9si
+L+gZ8KjtapXIXsdinjDdj1ri0DRSNEkOwn2NEooYmz6qwClFtl8DU9pajAeDpzg9
+CRg3vlkQCRJlW2m5wH2LYT2dINgWwoqwuXF6S+TUw6+XM4+Xdh/9VyvLnszWx0ax
+Helvj9F9utoIShnm4w95quYvdnmTIxPF62p7kz7VZ/RvpRoy1vTMYDQtEDl+9azA
+5EyBFWo+q9JCFYoLvHUiC9wD1+a6nTojxDw2H/ZNQGfJod9dz9xH9f+P1hadqqiG
+in84wHRGRt0rs0LRN1yrk3rwJuD3/Iyl60nDdQIDAQABAoIBADj1TFSt/hrcd49M
+oWtIs/oniAq4HuTPKhOSAjzarylOE1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KP7Lt4cxg+3p
+T+QipcFdlQbEPC3crNu3MW42Zal2O6X7EzEmr1HXbBeZb0+jZ9ecmKS+LrDPMj7C
+IHZQtBxUbmbi8uNWqos/l0lhJdE9BBt5sLqfXAe+qz6g21alg1Vttg==
+-----END RSA PRIVATE KEY-----
     """
 
 
 # contains certificate, private key as well as PK passphrase
 @pytest.fixture
-def certificate_with_encrypted_pk_pem():
+def certificate_encrypted_pk():
     return """
 -----BEGIN CERTIFICATE-----
 MIIDazCCAlOgAwIBAgIUAWW0ebo18+AtvjzxLvIPEa4lIUkwDQYJKoZIhvcNAQEL
diff --git a/tests/unit_tests/services/cryptography/extract_public_key_from_certificate_test.py b/tests/unit_tests/services/cryptography/extract_public_key_from_certificate_test.py
index 0f11919..99ec99c 100644
--- a/tests/unit_tests/services/cryptography/extract_public_key_from_certificate_test.py
+++ b/tests/unit_tests/services/cryptography/extract_public_key_from_certificate_test.py
@@ -1,13 +1,13 @@
 import subprocess
 
 
-def test_extract_public_key_encrypted(service, certificate_with_encrypted_pk_pem):
-    public_key = service.extract_public_key_from_certificate(certificate_with_encrypted_pk_pem[0])
+def test_extract_public_key_encrypted(service, certificate_encrypted_pk):
+    public_key = service.extract_public_key_from_certificate(certificate_encrypted_pk[0])
 
     # get modulus from the private key
     modulus_pk = subprocess.check_output(
-        ["openssl", "rsa", "-noout", "-in", "-", "-modulus", "-passin", f"pass:{certificate_with_encrypted_pk_pem[2]}"],
-        input=bytes(certificate_with_encrypted_pk_pem[1], encoding="utf-8"),
+        ["openssl", "rsa", "-noout", "-in", "-", "-modulus", "-passin", f"pass:{certificate_encrypted_pk[2]}"],
+        input=bytes(certificate_encrypted_pk[1], encoding="utf-8"),
         stderr=subprocess.STDOUT).decode()
 
     # get the modulus from the extracted public key
@@ -21,14 +21,30 @@ def test_extract_public_key_encrypted(service, certificate_with_encrypted_pk_pem
     assert modulus_pk == modulus_from_extracted_public_key
 
 
-def test_extract_public_key(service, certificate_pem):
-    public_key = service.extract_public_key_from_certificate(certificate_pem)
+def test_extract_public_key(service, certificate_pk):
+    public_key = service.extract_public_key_from_certificate(certificate_pk[0])
 
     # get public key from the certificate
     expected_public_key = subprocess.check_output(
         ["openssl", "x509", "-in", "-", "-noout", "-pubkey"],
-        input=bytes(certificate_pem, encoding="utf-8"),
+        input=bytes(certificate_pk[0], encoding="utf-8"),
         stderr=subprocess.STDOUT).decode()
 
     assert expected_public_key == public_key
 
+    # get modulus from the private key
+    modulus_pk = subprocess.check_output(
+        ["openssl", "rsa", "-noout", "-in", "-", "-modulus"],
+        input=bytes(certificate_pk[1], encoding="utf-8"),
+        stderr=subprocess.STDOUT).decode()
+
+    # get the modulus from the extracted public key
+    modulus_from_extracted_public_key = subprocess.check_output(
+        ["openssl", "rsa", "-noout", "-pubin", "-in", "-", "-modulus"],
+        input=bytes(public_key,
+                    encoding="utf-8"),
+        stderr=subprocess.STDOUT).decode()
+
+    # moduli should match
+    assert modulus_pk == modulus_from_extracted_public_key
+
