summaryrefslogtreecommitdiff
path: root/tests/test_81_certificates.py
blob: 772211ea668643646069ea98760f6f4709fbc21d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
from os import remove
import os
import time

__author__ = 'haho0032'
import unittest
from saml2.cert import OpenSSLWrapper


class TestGenerateCertificates(unittest.TestCase):
    def test_validate_with_root_cert(self):

        cert_info_ca = {
            "cn": "qwerty",
            "country_code": "qw",
            "state": "qwerty",
            "city": "qwerty",
            "organization": "qwerty",
            "organization_unit": "qwerty"
        }

        cert_info = {
            "cn": "asdfgh",
            "country_code": "as",
            "state": "asdfgh",
            "city": "asdfgh",
            "organization": "asdfgh",
            "organization_unit": "asdfg"
        }

        osw = OpenSSLWrapper()

        ca_cert, ca_key = osw.create_certificate(cert_info_ca, request=False,
                                                 write_to_file=True,
                                                 cert_dir=os.path.dirname(
                                                     os.path.abspath(
                                                         __file__)) + "/pki")

        req_cert_str, req_key_str = osw.create_certificate(cert_info,
                                                           request=True)

        ca_cert_str = osw.read_str_from_file(ca_cert)
        ca_key_str = osw.read_str_from_file(ca_key)

        cert_str = osw.create_cert_signed_certificate(ca_cert_str, ca_key_str,
                                                      req_cert_str)

        valid, mess = osw.verify(ca_cert_str, cert_str)
        self.assertTrue(valid)

        false_ca_cert, false_ca_key = osw.create_certificate(cert_info_ca,
                                                             request=False,
                                                             write_to_file=False)
        false_req_cert_str_1, false_req_key_str_1 = osw.create_certificate(
            cert_info_ca, request=True)
        false_cert_str_1 = osw.create_cert_signed_certificate(false_ca_cert,
                                                              false_ca_key,
                                                              false_req_cert_str_1)
        false_req_cert_str_2, false_req_key_str_2 = osw.create_certificate(
            cert_info, request=True)
        false_cert_str_2 = osw.create_cert_signed_certificate(false_ca_cert,
                                                              false_ca_key,
                                                              false_req_cert_str_2)

        valid, mess = osw.verify(false_ca_cert, cert_str)
        self.assertFalse(valid)
        valid, mess = osw.verify(false_ca_cert, false_cert_str_1)
        self.assertFalse(valid)
        valid, mess = osw.verify(ca_cert_str, false_cert_str_2)
        self.assertFalse(valid)

        if 'z' in cert_str:
            false_cert_str = cert_str.replace('z', 'x')
            valid, mess = osw.verify(ca_cert_str, false_cert_str)
            self.assertFalse(valid)

        remove(ca_cert)
        remove(ca_key)

    def test_validate_cert_chains(self):

        cert_info_ca = {
            "cn": "qwerty",
            "country_code": "qw",
            "state": "qwerty",
            "city": "qwerty",
            "organization": "qwerty",
            "organization_unit": "qwerty"
        }

        cert_intermediate_1_info = {
            "cn": "intermediate_1",
            "country_code": "as",
            "state": "asdfgh",
            "city": "asdfgh",
            "organization": "asdfgh",
            "organization_unit": "asdfg"
        }

        cert_intermediate_2_info = {
            "cn": "intermediate_2",
            "country_code": "as",
            "state": "asdfgh",
            "city": "asdfgh",
            "organization": "asdfgh",
            "organization_unit": "asdfg"
        }

        cert_client_cert_info = {
            "cn": "intermediate_1",
            "country_code": "as",
            "state": "asdfgh",
            "city": "asdfgh",
            "organization": "asdfgh",
            "organization_unit": "asdfg"
        }

        osw = OpenSSLWrapper()

        ca_cert_str, ca_key_str = osw.create_certificate(cert_info_ca,
                                                         request=False)

        req_cert_str, intermediate_1_key_str = osw.create_certificate(
            cert_intermediate_1_info, request=True)
        intermediate_cert_1_str = osw.create_cert_signed_certificate(
            ca_cert_str, ca_key_str, req_cert_str)

        req_cert_str, intermediate_2_key_str = osw.create_certificate(
            cert_intermediate_2_info, request=True)
        intermediate_cert_2_str = osw.create_cert_signed_certificate(
            intermediate_cert_1_str, intermediate_1_key_str,
            req_cert_str)

        req_cert_str, client_key_str = osw.create_certificate(
            cert_client_cert_info, request=True)
        client_cert_str = osw.create_cert_signed_certificate(
            intermediate_cert_2_str, intermediate_2_key_str,
            req_cert_str)

        cert_chain = [intermediate_cert_2_str, intermediate_cert_1_str,
                      ca_cert_str]

        valid, mess = osw.verify_chain(cert_chain, client_cert_str)
        self.assertTrue(valid)


    def test_validate_passphrase(self):

        cert_info_ca = {
            "cn": "qwerty",
            "country_code": "qw",
            "state": "qwerty",
            "city": "qwerty",
            "organization": "qwerty",
            "organization_unit": "qwerty"
        }

        cert_info = {
            "cn": "intermediate_1",
            "country_code": "as",
            "state": "asdfgh",
            "city": "asdfgh",
            "organization": "asdfgh",
            "organization_unit": "asdfg"
        }

        osw = OpenSSLWrapper()

        ca_cert_str, ca_key_str = osw.create_certificate(
            cert_info_ca, request=False,
            cipher_passphrase={"cipher": "blowfish", "passphrase": "qwerty"})

        req_cert_str, req_key_str = osw.create_certificate(cert_info,
                                                           request=True)
        cert_str = osw.create_cert_signed_certificate(ca_cert_str, ca_key_str,
                                                      req_cert_str,
                                                      passphrase=b"qwerty")

        valid = False
        try:
            cert_str = osw.create_cert_signed_certificate(
                ca_cert_str, ca_key_str, req_cert_str,
                passphrase="qwertyqwerty")
        except Exception:
            valid = True

        self.assertTrue(valid)

    def test_validate_expire(self):

        cert_info_ca = {
            "cn": "qwerty",
            "country_code": "qw",
            "state": "qwerty",
            "city": "qwerty",
            "organization": "qwerty",
            "organization_unit": "qwerty"
        }

        cert_info = {
            "cn": "intermediate_1",
            "country_code": "as",
            "state": "asdfgh",
            "city": "asdfgh",
            "organization": "asdfgh",
            "organization_unit": "asdfg"
        }

        osw = OpenSSLWrapper()

        ca_cert_str, ca_key_str = osw.create_certificate(cert_info_ca,
                                                         request=False)

        req_cert_str, req_key_str = osw.create_certificate(cert_info,
                                                           request=True)
        cert_str = osw.create_cert_signed_certificate(ca_cert_str, ca_key_str,
                                                      req_cert_str)

        valid, mess = osw.verify(ca_cert_str, cert_str)

        ca_cert_str, ca_key_str = osw.create_certificate(cert_info_ca,
                                                         request=False,
                                                         valid_from=1000,
                                                         valid_to=100000)
        req_cert_str, req_key_str = osw.create_certificate(cert_info,
                                                           request=True)
        cert_str = osw.create_cert_signed_certificate(ca_cert_str, ca_key_str,
                                                      req_cert_str)
        valid, mess = osw.verify(ca_cert_str, cert_str)
        self.assertFalse(valid)

        ca_cert_str, ca_key_str = osw.create_certificate(cert_info_ca,
                                                         request=False)
        req_cert_str, req_key_str = osw.create_certificate(cert_info,
                                                           request=True)
        cert_str = osw.create_cert_signed_certificate(ca_cert_str, ca_key_str,
                                                      req_cert_str,
                                                      valid_from=1000,
                                                      valid_to=100000)
        valid, mess = osw.verify(ca_cert_str, cert_str)
        self.assertFalse(valid)

        ca_cert_str, ca_key_str = osw.create_certificate(cert_info_ca,
                                                         request=False,
                                                         valid_from=0,
                                                         valid_to=1)
        req_cert_str, req_key_str = osw.create_certificate(cert_info,
                                                           request=True)
        cert_str = osw.create_cert_signed_certificate(ca_cert_str, ca_key_str,
                                                      req_cert_str)
        time.sleep(2)
        valid, mess = osw.verify(ca_cert_str, cert_str)
        self.assertFalse(valid)

        ca_cert_str, ca_key_str = osw.create_certificate(cert_info_ca,
                                                         request=False)
        req_cert_str, req_key_str = osw.create_certificate(cert_info,
                                                           request=True)
        cert_str = osw.create_cert_signed_certificate(ca_cert_str, ca_key_str,
                                                      req_cert_str,
                                                      valid_from=0, valid_to=1)
        time.sleep(2)
        valid, mess = osw.verify(ca_cert_str, cert_str)
        self.assertFalse(valid)