1
|
from flask import Response
|
2
|
from injector import inject
|
3
|
import base64
|
4
|
|
5
|
from src.exceptions.certificate_not_found_exception import CertificateNotFoundException
|
6
|
from src.exceptions.private_key_not_found_exception import PrivateKeyNotFoundException
|
7
|
from src.services.crl_ocsp.crl_ocsp_service import CrlOcspService
|
8
|
from src.controllers.return_codes import *
|
9
|
|
10
|
E_WRONG_PARAMETERS = {"success": False, "data": "Invalid request, wrong parameters."}
|
11
|
E_NO_CERTIFICATES_FOUND = {"success": False, "data": "No such certificate found."}
|
12
|
|
13
|
|
14
|
class CrlOcspController:
|
15
|
|
16
|
@inject
|
17
|
def __init__(self, crl_service: CrlOcspService):
|
18
|
self.crl_service = crl_service
|
19
|
|
20
|
def get_crl(self, ca_id: str):
|
21
|
"""
|
22
|
Generate and download a CRL for a selected certificate authority
|
23
|
:param ca_id: certificate authority whose CRL is requested
|
24
|
:return: download response / error
|
25
|
"""
|
26
|
# convert id from string to int
|
27
|
try:
|
28
|
identifier = int(ca_id)
|
29
|
except ValueError:
|
30
|
return E_WRONG_PARAMETERS, C_BAD_REQUEST
|
31
|
|
32
|
# generate the CRL
|
33
|
try:
|
34
|
crl = self.crl_service.generate_crl_response(identifier)
|
35
|
except CertificateNotFoundException:
|
36
|
return E_NO_CERTIFICATES_FOUND, C_NOT_FOUND
|
37
|
|
38
|
return Response(crl, mimetype="application/x-x509-ca-cert",
|
39
|
headers={"Content-Disposition": f"attachment;filename={ca_id}_crl.pem"})
|
40
|
|
41
|
def get_ocsp_from_base64(self, ca_id, base64_der_ocsp_request):
|
42
|
"""
|
43
|
Generate an OCSP Response for a base-64 encoded DER encoded OCSP Request.
|
44
|
:param ca_id: certificate authority ID
|
45
|
:param base64_der_ocsp_request: encoded request
|
46
|
:return: DER OCSP response
|
47
|
"""
|
48
|
return self.get_ocsp_from_der(ca_id, base64.b64decode(base64_der_ocsp_request))
|
49
|
|
50
|
def get_ocsp_from_der(self, ca_id, der_ocsp_request):
|
51
|
"""
|
52
|
Generate an OCSP Response for a DER encoded OCSP Request.
|
53
|
:param ca_id: certificate authority ID
|
54
|
:param der_ocsp_request: encoded request
|
55
|
:return: DER OCSP response
|
56
|
"""
|
57
|
if len(der_ocsp_request) == 0:
|
58
|
return E_WRONG_PARAMETERS, C_BAD_REQUEST
|
59
|
|
60
|
try:
|
61
|
ca_id = int(ca_id)
|
62
|
except ValueError:
|
63
|
return E_WRONG_PARAMETERS, C_BAD_REQUEST
|
64
|
|
65
|
try:
|
66
|
ocsp_response = self.crl_service.generate_ocsp_response(ca_id, der_ocsp_request)
|
67
|
except CertificateNotFoundException:
|
68
|
return E_NO_CERTIFICATES_FOUND, C_NOT_FOUND
|
69
|
|
70
|
return Response(ocsp_response, mimetype="application/ocsp-response")
|