Revize 79a61171
Přidáno uživatelem Michal Seják před téměř 4 roky(ů)
tests/integration_tests/rest_api/crl_ocsp_test.py | ||
---|---|---|
1 |
from src.services.cryptography import CryptographyService as cs |
|
2 |
from src.utils.temporary_file import TemporaryFile |
|
3 |
run = cs._CryptographyService__run_for_output |
|
4 |
import re |
|
5 |
import base64 |
|
6 |
|
|
1 | 7 |
|
2 | 8 |
def make_root_ca(server, title="Root CA s.r.o."): |
3 | 9 |
return server.post("/api/certificates", content_type="application/json", json={ |
... | ... | |
106 | 112 |
assert ret.status_code == 404 |
107 | 113 |
|
108 | 114 |
|
115 |
def test_ocsp_valid(server): |
|
116 |
ret = make_root_ca(server) |
|
117 |
root_idx = ret.json["data"] |
|
118 |
end_1 = make_end_cert(server, root_idx, "end1") |
|
119 |
end_2 = make_end_cert(server, root_idx, "end2") |
|
120 |
|
|
121 |
root_contents = server.get(f"/api/certificates/{root_idx}") .json["data"] |
|
122 |
end_1_index = end_1.json['data'] |
|
123 |
end_1_contents = server.get(f"/api/certificates/{end_1_index}") .json["data"] |
|
124 |
end_2_index = end_2.json['data'] |
|
125 |
end_2_contents = server.get(f"/api/certificates/{end_2_index}") .json["data"] |
|
126 |
|
|
127 |
def call_ocsp_service_post(ocsp_req): |
|
128 |
retval = server.post(f"/api/ocsp/{root_idx}", data=ocsp_req, content_type="application/ocsp-request") |
|
129 |
assert retval.status_code == 200 |
|
130 |
ocsp_response_decoded = str( |
|
131 |
run(["ocsp", "-respin", "-", "-text", "-CAfile", root_cert], proc_input=retval.data), |
|
132 |
encoding='utf-8') |
|
133 |
cert_status = re.findall("Cert Status: ([a-z]*)", ocsp_response_decoded)[0] |
|
134 |
return cert_status |
|
135 |
|
|
136 |
def call_ocsp_service_get(ocsp_req): |
|
137 |
retval = server.get(f"/api/ocsp/{root_idx}/{str(base64.b64encode(ocsp_req), encoding='utf-8')}", content_type="application/ocsp-request") |
|
138 |
assert retval.status_code == 200 |
|
139 |
ocsp_response_decoded = str( |
|
140 |
run(["ocsp", "-respin", "-", "-text", "-CAfile", root_cert], proc_input=retval.data), |
|
141 |
encoding='utf-8') |
|
142 |
cert_status = re.findall("Cert Status: ([a-z]*)", ocsp_response_decoded)[0] |
|
143 |
return cert_status |
|
144 |
|
|
145 |
# check both OCSP methods and assert that the certificate is valid |
|
146 |
def assert_good(ocsp_req): |
|
147 |
cert_status_1 = call_ocsp_service_post(ocsp_req) |
|
148 |
cert_status_2 = call_ocsp_service_get(ocsp_req) |
|
149 |
assert cert_status_1 == cert_status_2 == "good" |
|
150 |
|
|
151 |
# check both OCSP methods and assert that the certificate is revoked |
|
152 |
def assert_revoked(ocsp_req): |
|
153 |
cert_status_1 = call_ocsp_service_post(ocsp_req) |
|
154 |
cert_status_2 = call_ocsp_service_post(ocsp_req) |
|
155 |
assert cert_status_1 == cert_status_2 == "revoked" |
|
156 |
|
|
157 |
with TemporaryFile("end_1_cert.pem", end_1_contents) as end_1_cert_pem, \ |
|
158 |
TemporaryFile("end_2_cert.pem", end_2_contents) as end_2_cert_pem, \ |
|
159 |
TemporaryFile("issuer.pem", root_contents) as root_cert: |
|
160 |
|
|
161 |
end_cert_1_contents = str(run(["x509", "-in", end_1_cert_pem, "-text", "-noout"]), encoding='utf-8') |
|
162 |
serial_1 = re.findall("Serial Number: ([0-9]+) ", end_cert_1_contents)[0] |
|
163 |
|
|
164 |
end_cert_2_contents = str(run(["x509", "-in", end_2_cert_pem, "-text", "-noout"]), encoding='utf-8') |
|
165 |
serial_2 = re.findall("Serial Number: ([0-9]+) ", end_cert_2_contents)[0] |
|
166 |
|
|
167 |
# test serial (first) |
|
168 |
ocsp_request = run(["ocsp", "-issuer", root_cert, "-serial", serial_1, "-reqout", "-"]) |
|
169 |
assert_good(ocsp_request) |
|
170 |
|
|
171 |
# test contents directly (second) |
|
172 |
ocsp_request = run(["ocsp", "-issuer", root_cert, "-cert", end_2_cert_pem, "-reqout", "-"]) |
|
173 |
assert_good(ocsp_request) |
|
174 |
|
|
175 |
# revoke first |
|
176 |
ret = server.patch(f"/api/certificates/{end_1_index}", json={"status": "revoked"}) |
|
177 |
assert ret.status_code == 200 |
|
178 |
|
|
179 |
# test serial (first) |
|
180 |
ocsp_request = run(["ocsp", "-issuer", root_cert, "-serial", serial_1, "-reqout", "-"]) |
|
181 |
assert_revoked(ocsp_request) |
|
182 |
|
|
183 |
# test contents directly (first) |
|
184 |
ocsp_request = run(["ocsp", "-issuer", root_cert, "-cert", end_1_cert_pem, "-reqout", "-"]) |
|
185 |
assert_revoked(ocsp_request) |
|
186 |
|
|
187 |
# test serial (second) |
|
188 |
ocsp_request = run(["ocsp", "-issuer", root_cert, "-serial", serial_2, "-reqout", "-"]) |
|
189 |
assert_good(ocsp_request) |
|
190 |
|
|
191 |
# test contents directly (second) |
|
192 |
ocsp_request = run(["ocsp", "-issuer", root_cert, "-cert", end_2_cert_pem, "-reqout", "-"]) |
|
193 |
assert_good(ocsp_request) |
|
194 |
|
|
195 |
|
|
196 |
def test_ocsp_invalid_1(server): |
|
197 |
assert server.post(f"/api/ocsp/8008135").status_code == 400 |
|
198 |
|
|
199 |
|
|
200 |
def test_ocsp_invalid_2(server): |
|
201 |
assert server.get(f"/api/ocsp/8008135/look_at_me_im_binary").status_code == 404 |
|
202 |
|
|
203 |
|
|
204 |
def test_ocsp_invalid_3(server): |
|
205 |
assert server.get(f"/api/ocsp/8008135").status_code == 405 |
|
206 |
|
Také k dispozici: Unified diff
Re #8577 - Added OCSP integration tests.