Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 9dbbcdae

Přidáno uživatelem Stanislav Král před asi 4 roky(ů)

Re #8472 - Added create_crt method that creates a certificate

Added 3 unit tests testing the added method.

Zobrazit rozdíly:

proj/services/cryptography.py
141 141
        :param csr: a string containing the CSR to be signed
142 142
        :param issuer_pem: string containing the certificate of the issuer that will sign this CSR in PEM format
143 143
        :param issuer_key: string containing the private key of the issuer's certificate in PEM format
144
        :param issuer_key_pass: string containing the private key of the issuer's certificate in PEM format
144
        :param issuer_key_pass: string containing the passphrase of the private key of the issuer's certificate in PEM
145
        format
145 146
        :param config: TODO NOT USED
146 147
        :param extensions: extensions to be applied when signing the CSR
147 148
        :return: string containing the generated and signed certificate in PEM format
......
166 167

  
167 168
            return self._run_for_output(params, proc_input=(bytes(proc_input, encoding="utf-8"))).decode()
168 169

  
170
    def create_crt(self, subject, key, issuer_pem, issuer_key, key_pass=None, issuer_key_pass=None, config="",
171
                   extensions=""):
172
        """
173
        Signs the given CSR by the given issuer CA
174
        :param subject: subject to be added to the created certificate
175
        :param key: string containing the private key to be used when creating the certificate in PEM format
176
        :param issuer_key: string containing the private key of the issuer's certificate in PEM format
177
        :param issuer_pem: string containing the certificate of the issuer that will sign this CSR in PEM format
178
        :param issuer_key: string containing the private key of the issuer's certificate in PEM format
179
        :param key_pass: string containing the passphrase of the private key used when creating the certificate in PEM
180
        format
181
        :param issuer_key_pass: string containing the passphrase of the private key of the issuer's certificate in PEM
182
        format
183
        :param config: TODO NOT USED
184
        :param extensions: extensions to be applied when creating the certificate
185
        :return: string containing the generated in PEM format
186
        """
187
        csr = self.make_csr(subject, key, subject_key_pass=key_pass)
188
        return self.sign_csr(csr, issuer_pem, issuer_key, issuer_key_pass=issuer_key_pass, extensions=extensions)
189

  
169 190

  
170 191
class CryptographyException(Exception):
171 192

  
proj/tests/services/cryptography/create_crt_test.py
1
import subprocess
2

  
3
import pytest
4

  
5
from proj.model.subject import Subject
6
from proj.services.cryptography import CryptographyException
7

  
8

  
9
def export_crt(crt):
10
    return subprocess.check_output(["openssl", "x509", "-noout", "-text", "-in", "-"],
11
                                   input=bytes(crt, encoding="utf-8"), stderr=subprocess.STDOUT).decode()
12

  
13

  
14
def test_sign_cst(service):
15
    # create root CA
16
    root_key = service.create_private_key()
17
    root_ca = service.create_sscrt(root_key, Subject(common_name="foo"))
18

  
19
    # create a private key to be used to make a CSR for the intermediate CA
20
    inter_key = service.create_private_key()
21

  
22
    # create a CA using the root CA
23
    inter_ca = service.create_crt(Subject(common_name="bar", country="CZ"), inter_key, root_ca, root_key)
24

  
25
    inter_ca_printed = export_crt(inter_ca)
26

  
27
    # assert fields
28
    assert "Issuer: CN = foo" in inter_ca_printed
29
    assert "Subject: CN = bar, C = CZ" in inter_ca_printed
30

  
31

  
32
def test_sign_crt_passphrase(service):
33
    # create root CA and encrypt the private key of the root CA
34
    root_key_passphrase = "barbaz"
35
    root_key = service.create_private_key(passphrase=root_key_passphrase)
36
    root_ca = service.create_sscrt(root_key, Subject(common_name="foo"), key_passphrase=root_key_passphrase)
37

  
38
    # create a private key to be used to make a CSR for the intermediate CA
39
    inter_key_passphrase = "foobazbar"
40
    inter_key = service.create_private_key(passphrase=inter_key_passphrase)
41

  
42
    # create a CA using the root CA
43
    inter_ca = service.create_crt(Subject(common_name="bar", country="CZ"), inter_key, root_ca, root_key,
44
                                  key_pass=inter_key_passphrase, issuer_key_pass=root_key_passphrase)
45

  
46
    inter_ca_printed = export_crt(inter_ca)
47

  
48
    # assert fields
49
    assert "Issuer: CN = foo" in inter_ca_printed
50
    assert "Subject: CN = bar, C = CZ" in inter_ca_printed
51

  
52
    # some basic incorrect passphrase combinations
53
    passphrases = [
54
        (inter_key, None),
55
        (inter_key, "foofoobarbar"),
56
        (None, root_key),
57
        ("foofoobarbar", root_key),
58
        ("foofoobarbar", "foofoobarbar"),
59
        (None, None)
60
    ]
61

  
62
    for (key_pass, issuer_key_pass) in passphrases:
63
        # try to create it using a wrong issuer passphrase
64
        with pytest.raises(CryptographyException) as e:
65
            inter_ca = service.create_crt(Subject(common_name="bar", country="CZ"), inter_key, root_ca, root_key,
66
                                          key_pass=key_pass, issuer_key_pass=issuer_key_pass)
67
        assert "bad decrypt" in e.value.message
68

  
69

  
70
def test_sign_crt_extensions(service):
71
    # create root CA and encrypt the private key of the root CA
72
    root_key_passphrase = "barbaz"
73
    root_key = service.create_private_key(passphrase=root_key_passphrase)
74
    root_ca = service.create_sscrt(root_key, Subject(common_name="foo"), key_passphrase=root_key_passphrase)
75

  
76
    # create a private key to be used to make a CSR for the intermediate CA
77
    inter_key_passphrase = "foofoo"
78
    inter_key = service.create_private_key()
79

  
80
    # create a CA using the root CA
81
    inter_ca = service.create_crt(Subject(common_name="bar", country="CZ"), inter_key, root_ca, root_key,
82
                                  key_pass=inter_key_passphrase, issuer_key_pass=root_key_passphrase,
83
                                  extensions="authorityInfoAccess = caIssuers;URI:bar.cz/baz/cert\nbasicConstraints=critical,CA:TRUE")
84

  
85
    inter_ca_printed = export_crt(inter_ca)
86

  
87
    # assert fields
88
    assert "Issuer: CN = foo" in inter_ca_printed
89
    assert "Subject: CN = bar, C = CZ" in inter_ca_printed
90

  
91
    # assert extensions
92
    expected_extensions = """        X509v3 extensions:
93
            Authority Information Access: 
94
                CA Issuers - URI:bar.cz/baz/cert
95

  
96
            X509v3 Basic Constraints: critical
97
                CA:TRUE"""
98
    assert expected_extensions in inter_ca_printed

Také k dispozici: Unified diff