Projekt

Obecné

Profil

« Předchozí | Další » 

Revize e40c571e

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

Re #8575 - Added unit tests verifying that serial number is correctly set when creating a certificate

Renamed some create_crt_test.py tests.

Zobrazit rozdíly:

tests/unit_tests/services/cryptography/create_crt_test.py
11 11
                                   input=bytes(crt, encoding="utf-8"), stderr=subprocess.STDOUT).decode()
12 12

  
13 13

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

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

  
22 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)
23
    cert = service.create_crt(Subject(common_name="bar", country="CZ"), cert_key, root_ca, root_key)
24 24

  
25
    inter_ca_printed = export_crt(inter_ca)
25
    cert_printed = export_crt(cert)
26 26

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

  
31 31

  
32
def test_sign_crt_passphrase(service):
32
def test_create_crt_passphrase(service):
33 33
    # create root CA and encrypt the private key of the root CA
34 34
    root_key_passphrase = "barbaz"
35 35
    root_key = service.create_private_key(passphrase=root_key_passphrase)
......
39 39
    inter_key_passphrase = "foobazbar"
40 40
    inter_key = service.create_private_key(passphrase=inter_key_passphrase)
41 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
                                  subject_key_pass=inter_key_passphrase, issuer_key_pass=root_key_passphrase)
42
    # create a certificate using the root CA
43
    cert = service.create_crt(Subject(common_name="bar", country="CZ"), inter_key, root_ca, root_key,
44
                              subject_key_pass=inter_key_passphrase, issuer_key_pass=root_key_passphrase)
45 45

  
46
    inter_ca_printed = export_crt(inter_ca)
46
    cert_printed = export_crt(cert)
47 47

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

  
52 52
    # some basic incorrect passphrase combinations
53 53
    passphrases = [
......
62 62
    for (key_pass, issuer_key_pass) in passphrases:
63 63
        # try to create it using a wrong issuer passphrase
64 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
                                          subject_key_pass=key_pass, issuer_key_pass=issuer_key_pass)
65
            service.create_crt(Subject(common_name="bar", country="CZ"), inter_key, root_ca, root_key,
66
                               subject_key_pass=key_pass, issuer_key_pass=issuer_key_pass)
67 67
        assert "bad decrypt" in e.value.message
68 68

  
69 69

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

  
76 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()
77
    cert_key_passphrase = "foofoo"
78
    cert_key = service.create_private_key()
79 79

  
80 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
                                  subject_key_pass=inter_key_passphrase, issuer_key_pass=root_key_passphrase,
83
                                  extensions="authorityInfoAccess=caIssuers;URI:bar.cz/baz/cert\n"
84
                                             "basicConstraints=critical,CA:TRUE\n"
85
                                             "crlDistributionPoints=URI:http://localhost/api/crl/0\n"
86
                                             "authorityInfoAccess=OCSP;URI:http://localhost/api/ocsp/0\n")
81
    cert = service.create_crt(Subject(common_name="bar", country="CZ"), cert_key, root_ca, root_key,
82
                              subject_key_pass=cert_key_passphrase, issuer_key_pass=root_key_passphrase,
83
                              extensions="authorityInfoAccess=caIssuers;URI:bar.cz/baz/cert\n"
84
                                         "basicConstraints=critical,CA:TRUE\n"
85
                                         "crlDistributionPoints=URI:http://localhost/api/crl/0\n"
86
                                         "authorityInfoAccess=OCSP;URI:http://localhost/api/ocsp/0\n")
87 87

  
88
    inter_ca_printed = export_crt(inter_ca)
88
    cert_printed = export_crt(cert)
89 89

  
90 90
    # assert fields
91
    assert "Issuer: CN = foo" in inter_ca_printed
92
    assert "Subject: CN = bar, C = CZ" in inter_ca_printed
93
    assert "X509v3 CRL Distribution Points:" in inter_ca_printed
94
    assert "URI:http://localhost/api/crl/0" in inter_ca_printed
95
    assert "Authority Information Access:" in inter_ca_printed
96
    assert "OCSP - URI:http://localhost/api/ocsp/0" in inter_ca_printed
91
    assert "Issuer: CN = foo" in cert_printed
92
    assert "Subject: CN = bar, C = CZ" in cert_printed
93
    assert "X509v3 CRL Distribution Points:" in cert_printed
94
    assert "URI:http://localhost/api/crl/0" in cert_printed
95
    assert "Authority Information Access:" in cert_printed
96
    assert "OCSP - URI:http://localhost/api/ocsp/0" in cert_printed
97 97

  
98 98
    # assert extensions
99 99
    expected_extensions = """        X509v3 extensions:
......
107 107
            Authority Information Access: 
108 108
                OCSP - URI:http://localhost/api/ocsp/0"""
109 109
    expected_extensions = expected_extensions.replace("\n", "").replace("\r", "")
110
    inter_ca_printed = inter_ca_printed.replace("\n", "").replace("\r", "")
111
    assert expected_extensions in inter_ca_printed
110
    cert_printed = cert_printed.replace("\n", "").replace("\r", "")
111
    assert expected_extensions in cert_printed
112

  
113

  
114
def test_create_crt_serial_number(service):
115
    # create root CA
116
    root_key = service.create_private_key()
117
    root_ca = service.create_sscrt(Subject(common_name="foo"), root_key)
118

  
119
    # create a private key to be used to make a new certificate
120
    cert_key = service.create_private_key()
121

  
122
    # define a serial number to be used
123
    serial_number = 1
124
    serial_number_hex = hex(serial_number).replace("x", "")
125

  
126
    # create a certificate using the root CA
127
    cert = service.create_crt(Subject(common_name="bar", country="CZ"), cert_key, root_ca, root_key,
128
                              sn=serial_number)
129

  
130
    cert_printed = subprocess.check_output(["openssl", "x509", "-noout", "-in", "-", "-serial"],
131
                                           input=bytes(cert, encoding="utf-8"), stderr=subprocess.STDOUT).decode()
132

  
133
    assert f"serial={serial_number_hex}" in cert_printed
134

  
135

  
136
def test_create_crt_negative_serial_number(service):
137
    # create root CA
138
    root_key = service.create_private_key()
139
    root_ca = service.create_sscrt(Subject(common_name="foo"), root_key)
140

  
141
    # create a private key to be used to make a new certificate
142
    cert_key = service.create_private_key()
143

  
144
    # define a serial number to be used
145
    serial_number = -1
146
    serial_number_hex = hex(serial_number).replace("x", "")
147

  
148
    # create a certificate using the root CA
149
    cert = service.create_crt(Subject(common_name="bar", country="CZ"), cert_key, root_ca, root_key,
150
                              sn=serial_number)
151

  
152
    cert_printed = subprocess.check_output(["openssl", "x509", "-noout", "-in", "-", "-serial"],
153
                                           input=bytes(cert, encoding="utf-8"), stderr=subprocess.STDOUT).decode()
154

  
155
    assert f"serial={serial_number_hex}" in cert_printed

Také k dispozici: Unified diff