commit 7ff4be59be5353d296feb9cc326c1021bf078a58
Author: Stanislav Král <stanislav.kral3@gmail.com>
Date:   Tue Apr 13 01:29:38 2021 +0200

    Re #8573 - Improved testing of extract_public_key_from_certificate and extract_public_key_from_private_key methods

diff --git a/tests/unit_tests/services/cryptography/conftest.py b/tests/unit_tests/services/cryptography/conftest.py
index f574832..490f2f4 100644
--- a/tests/unit_tests/services/cryptography/conftest.py
+++ b/tests/unit_tests/services/cryptography/conftest.py
@@ -105,6 +105,7 @@ GO9Jbty2Qeomxyf+i+SB
     """
 
 
+# contains certificate, private key as well as PK passphrase
 @pytest.fixture
 def certificate_with_encrypted_pk_pem():
     return """
@@ -129,4 +130,35 @@ IeHGOq+4gEYzF0YsGu2QaAPR3hRMcRtc7LDtuy+eRz4BqvRm3jF3W26Ha8cSojs7
 c2xJmcA3t2cFCHYKXDmlBJJ5Kvkuak9U+TZLl7+Y7+SmyJIv9nRN2THwDKpaRtff
 94wl65n2IoG1YP4XyjyX
 -----END CERTIFICATE-----
-    """, "pass"
\ No newline at end of file
+    """, """
+-----BEGIN RSA PRIVATE KEY-----
+Proc-Type: 4,ENCRYPTED
+DEK-Info: AES-256-CBC,CC2F8ACBF28E244F2DCD4239D38EE3A5
+
+75uLZWsoCKfzOlp//IbWVkISUsrG2MqrJi8JIRKWeR63jGCuskju87U+LoPP7EZ+
+xBm9uP0voayUIXPnGOJS/1veUfV8477RGhqK0/pM/MLWcYnggeoBu7EIzUVi80tQ
+chC1DJxB+mi1DG3lytyzvuDq8AsQ7iK7VWmST9pJHDgw9K19zBLRtPjZLYs09A0e
+PJE74IgZySOctZxwidmdwYDysGaMK35nxP6tPUngQjuA2TdDQxOKBTG17g4drqcJ
+ddJFpw3HKiSRyHtl8DQoQwHXvgSFNz/dAnXRRAA+8ogyCyzcroekBGqaMJWpJMNp
+sbJgochbFZ+ayAQuQQGxDQHkzpTlYP4HCeDW7hR79HUWLziPXCjoE5PcEz/z/Be1
+dxYzK3zArFSHthvhKWNhwsF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ao0tZCmjJEXaik9L/
+H1WudWhZjp6RflnLKTcLvYslutwUb4q23mPUim03H/7YZ5tPLDWgSHiOk4phHVPG
+pIeQ6WxVbfVqyCBrTAsKUvkfs+rGvyM0iXgPw5As7M2nRE3emwpw8PskqLUnTMyK
+-----END RSA PRIVATE KEY-----
+    """, "pass"
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 5a476da..0f11919 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,34 @@
+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])
 
-    assert "-----BEGIN PUBLIC KEY-----" in public_key
+    # 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"),
+        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
 
 
 def test_extract_public_key(service, certificate_pem):
     public_key = service.extract_public_key_from_certificate(certificate_pem)
 
-    # TODO test whether public key matches a private key
-    assert "-----BEGIN PUBLIC KEY-----" in public_key
+    # get public key from the certificate
+    expected_public_key = subprocess.check_output(
+        ["openssl", "x509", "-in", "-", "-noout", "-pubkey"],
+        input=bytes(certificate_pem, encoding="utf-8"),
+        stderr=subprocess.STDOUT).decode()
+
+    assert expected_public_key == public_key
 
diff --git a/tests/unit_tests/services/cryptography/extract_public_key_test.py b/tests/unit_tests/services/cryptography/extract_public_key_test.py
index a37a103..abcfbab 100644
--- a/tests/unit_tests/services/cryptography/extract_public_key_test.py
+++ b/tests/unit_tests/services/cryptography/extract_public_key_test.py
@@ -1,3 +1,5 @@
+import subprocess
+
 import pytest
 
 from src.services.cryptography import CryptographyException
@@ -7,14 +9,41 @@ def test_extract_public_key_encrypted(service, private_key_encrypted_pem):
     public_key = service.extract_public_key_from_private_key(private_key_encrypted_pem[0],
                                                              passphrase=private_key_encrypted_pem[1])
 
-    assert "-----BEGIN PUBLIC KEY-----" in public_key
+    # get modulus from the private key
+    modulus_pk = subprocess.check_output(
+        ["openssl", "rsa", "-noout", "-in", "-", "-modulus", "-passin", f"pass:{private_key_encrypted_pem[1]}"],
+        input=bytes(private_key_encrypted_pem[0], 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
 
 
 def test_extract_public_key(service, private_key_pem):
     public_key = service.extract_public_key_from_private_key(private_key_pem)
 
-    # TODO test whether public key matches a private key
-    assert "-----BEGIN PUBLIC KEY-----" in public_key
+    # get modulus from the private key
+    modulus_pk = subprocess.check_output(
+        ["openssl", "rsa", "-noout", "-in", "-", "-modulus"],
+        input=bytes(private_key_pem, 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
 
 
 def test_extract_public_key_encrypted_fails(service, private_key_encrypted_pem):
