Projekt

Obecné

Profil

« Předchozí | Další » 

Revize e8face67

Přidáno uživatelem Stanislav Král před více než 3 roky(ů)

Re #8573 - Changed extract_public_key method in such way that it now extracts a public key from a private key instead of a certificate

Zobrazit rozdíly:

src/services/cryptography.py
231 231
            # the process failed because of some other reason (incorrect cert format)
232 232
            raise CryptographyException(OPENSSL_EXECUTABLE, args, err.decode())
233 233

  
234
    def extract_public_key(self, cert_pem: str) -> str:
234
    def extract_public_key(self, private_key_pem: str, passphrase=None) -> str:
235 235
        """
236
        Extracts a public key from the given certificate passed in PEM format
237
        :param cert_pem: PEM data representing a certificate from which a public key should be extracted
236
        Extracts a public key from the given private key passed in PEM format
237
        :param private_key_pem: PEM data representing the private key from which a public key should be extracted
238
        :param passphrase: passphrase to be provided when the supplied private key is encrypted
238 239
        :return: a string containing the extracted public key in PEM format
239 240
        """
240
        args = ["x509", "-in", "-", "-noout", "-pubkey"]
241
        return self.__run_for_output(args, proc_input=bytes(cert_pem, encoding="utf-8")).decode()
241
        args = ["rsa", "-in", "-", "-pubout"]
242
        if passphrase is not None:
243
            args.extend(["-passin", f"pass:{passphrase}"])
244
        return self.__run_for_output(args, proc_input=bytes(private_key_pem, encoding="utf-8")).decode()
242 245

  
243 246
    def parse_cert_pem(self, cert_pem):
244 247
        """
tests/unit_tests/services/cryptography/conftest.py
7 7
def service():
8 8
    # provide a CryptographyService fixture
9 9
    return CryptographyService()
10

  
11

  
12
@pytest.fixture
13
def private_key_pem():
14
    return """
15
-----BEGIN RSA PRIVATE KEY-----
16
MIIEpgIBAAKCAQEA8Ue4iYHc7ECrdHij9JHLRPhw9YXsK09ojPZaMW+GnhJrh//V
17
uOlUsA3Qb2xrdpwcevpTy8+96FU+w/laDA0KGsRPDfZnRYFGfonzuhJKDNkU7p2H
18
sqKp+ttxNEFm2ZTPo9KvrvQrg286RUMu+b0e/PlSG9X3L/+OMx3GsblBbXKNQmSq
19
T5qdWmoFMRSR4Z7wufAGebJXBTn8Z5r1KkhLLViRYdWC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4b41FKeIBbYh7T
36
X7wZUmoPhT6cpDsPgU864QIy2qypV8rHZ7ovpeDrkdjPrjcANFqeT4hFHwOAU459
37
GjJtQC8EdzsVT0Po3oE5TzmbmimPonGqJXf3SRo+O8ask69285ws6yIsh7OYNcaY
38
cVIS7vM5AoGBAPK2UieUMGTfK/SJ7tizApFkvonaInY8rKmiEeF9zuOxJBJ+mjXC
39
2dGH5h6itrdbQw7ykrnf4KgmS1ZDamrCYlhMRewbk1gzEvBV1I6W67DaMgIHxzrc
40
kXs8LBdrpkP93JM50KBFORHJvr9zmPjQ5VJdjipmqmwAI75qIBcdmlYG
41
-----END RSA PRIVATE KEY-----
42
    """
43

  
44

  
45
@pytest.fixture
46
def private_key_encrypted_pem():
47
    return """
48
-----BEGIN RSA PRIVATE KEY-----
49
Proc-Type: 4,ENCRYPTED
50
DEK-Info: AES-256-CBC,3D2BFBB1BD0EB9CECF062FB4E3DFBB58
51

  
52
U1YtKQOBkvoqM/9ChVBtDgz7fUazZG3GK58F/p1B+vHsfk3W025Enw0bXs0k3nUL
53
gbV/6x8Xx2it2FCo31eupHTXi3UhdzrCY4sNu3fz9y4twUouMQpcbSvz8CB6xNvb
54
y74BbIDdkpHHNPCkYQ6uzuS1nvZsdgulU9ukKLr7iyiJN4bPsuOox+4LKPJkXpuB
55
zNh5gKI5qvHucq5fiMWQvQUUJSkGKkF89psGwgNlTfJLmKfJmBSZytHpcJJVReI6
56
IQNT+Lge5zfYW4cDVfX1aiYS7L19a69mPIGoRTepS1IAecD6pxTMNastaRvpvEfc
57
k1u42eYczBKQ/EMhW5DoWlRkgr0hAfpuy9akl/oCCA82JDyTyAHsooHSiPqKJ5Qh
58
4NcKEBxxJzrBl1G27BrOlhkdOpz0IQv8WBRav6+KLCgu8g6wj2qNkPmxE0G8awrw
59
h+d77hdosewXHUon9rp2rNwn+xBYnLomV/wWd1h4BJD9bjdxoKE2aWdSjMg2+Tk7
60
n4PSgusayKLjA9dgEoompdJw0WKHpEBuX6owxia/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jsXVc+i0Bnw2DjY3s5SYJg6yycwdaHz
77
-----END RSA PRIVATE KEY-----
78
    """, "pass"
tests/unit_tests/services/cryptography/extract_public_key_test.py
1
def test_extract_public_key(service):
2
    cert_pem = """
3
-----BEGIN CERTIFICATE-----
4
MIIGITCCBAmgAwIBAgIUb7xAdXd6AkevhmeQqy2BASDqv/IwDQYJKoZIhvcNAQEL
5
BQAwgZ8xCzAJBgNVBAYTAkNaMRYwFAYDVQQIDA1QaWxzZW4gUmVnaW9uMQ8wDQYD
6
VQQHDAZQaWxzZW4xFjAUBgNVBAoMDVJvb3RpbmcgUm9vdHMxHDAaBgNVBAsME0Rl
7
cGFydG1lbnQgb2YgUk9vdHMxFDASBgNVBAMMC01haW4gUm9vdGVyMRswGQYJKoZI
8
hvcNAQkBFgxyb290QHJvb3QuY3owHhcNMjEwMzIxMTAwMTUyWhcNMjYwMzIxMTAw
9
MTUyWjCBnzELMAkGA1UEBhMCQ1oxFjAUBgNVBAgMDVBpbHNlbiBSZWdpb24xDzAN
10
BgNVBAcMBlBpbHNlbjEWMBQGA1UECgwNUm9vdGluZyBSb290czEcMBoGA1UECwwT
11
RGVwYXJ0bWVudCBvZiBST290czEUMBIGA1UEAwwLTWFpbiBSb290ZXIxGzAZBgkq
12
hkiG9w0BCQEWDHJvb3RAcm9vdC5jejCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCC
13
AgoCggIBAMKozynv+ja1VkNWpldsrl6tEGYrkNuG9umyqF0ZOZmzWzR7PiszV8DW
14
o+OQ3SY7MQ7o3qoE/pSiaApmNFxgarWvGxnVgouncrai1AKB92tFY1VnVfQYICD3
15
gdjSzo4Lbfc8+67DHTPc0N70oBZuMueQ6ifUQhrjuVaONwAOsZBdal+VWvctJcrf
16
fd+s6Jkgb/qWuld21Bzea36PLmgwoe8/RNyS9yzspC8jwdU68BemAPy9NBf9Q8Is
17
0R7aZ0YwKPsdln3lR5GixrNy+sQl0qwy0NgklWIbqpGbMAInJBbTBmBGIbS0zV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
Gh+KpX/I0JzuOc91ydNtvMEOjfIAp8mjLAqDCWRd0OzvE45rPbBAHJXPc4P76B1A
35
XXwUYr8GuSFQZb1Q4BpCayCYvTLj+7q3z72BCqAA+jMJYV/qU0EpsuFjPvzU8apg
36
7l9NhB7vf/qhW0XHDa4pv5+d+CXUiHPlW+UTIlni1AfgAel1Ww==
37
-----END CERTIFICATE-----
38
    """
39
    public_key = service.extract_public_key(cert_pem)
1
import pytest
2

  
3
from src.services.cryptography import CryptographyException
4

  
5

  
6
def test_extract_public_key_encrypted(service, private_key_encrypted_pem):
7
    public_key = service.extract_public_key(private_key_encrypted_pem[0], passphrase=private_key_encrypted_pem[1])
8

  
9
    assert "-----BEGIN PUBLIC KEY-----" in public_key
10

  
11

  
12
def test_extract_public_key_2(service, private_key_pem):
13
    public_key = service.extract_public_key(private_key_pem)
40 14

  
41 15
    # TODO test whether public key matches a private key
42 16
    assert "-----BEGIN PUBLIC KEY-----" in public_key
17

  
18

  
19
def test_extract_public_key_encrypted_fails(service, private_key_encrypted_pem):
20
    # try to extract it using no passphrase
21
    with pytest.raises(CryptographyException) as e:
22
        service.extract_public_key(private_key_encrypted_pem[0])
23

  
24
    assert "bad decrypt" in e.value.message
25

  
26
    # try to extract it using an empty passphrase
27
    with pytest.raises(CryptographyException) as e:
28
        service.extract_public_key(private_key_encrypted_pem[0], passphrase="")
29

  
30
    assert "bad decrypt" in e.value.message
31

  
32
    # try to extract it using a wrong passphrase
33
    with pytest.raises(CryptographyException) as e:
34
        service.extract_public_key(private_key_encrypted_pem[0], passphrase="foo")
35

  
36
    assert "bad decrypt" in e.value.message

Také k dispozici: Unified diff