certidude/certidude/api/revoked.py

45 lines
2.1 KiB
Python
Raw Normal View History

import click
2016-03-29 10:28:58 +00:00
import falcon
2016-03-30 19:00:18 +00:00
import json
import logging
2017-01-30 06:29:01 +00:00
from certidude import const, config
2016-03-30 19:00:18 +00:00
from certidude.authority import export_crl, list_revoked
2016-03-29 10:28:58 +00:00
from cryptography import x509
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.serialization import Encoding
2017-04-04 05:02:08 +00:00
logger = logging.getLogger(__name__)
class RevocationListResource(object):
def on_get(self, req, resp):
2016-03-29 10:28:58 +00:00
# Primarily offer DER encoded CRL as per RFC5280
# This is also what StrongSwan expects
if req.client_accepts("application/x-pkcs7-crl"):
resp.set_header("Content-Type", "application/x-pkcs7-crl")
resp.append_header(
"Content-Disposition",
("attachment; filename=%s.crl" % const.HOSTNAME).encode("ascii"))
2016-03-29 10:28:58 +00:00
# Convert PEM to DER
logger.debug(u"Serving revocation list to %s in DER format", req.context.get("remote_addr"))
2016-03-30 19:00:18 +00:00
resp.body = x509.load_pem_x509_crl(export_crl(),
default_backend()).public_bytes(Encoding.DER)
2016-03-29 10:28:58 +00:00
elif req.client_accepts("application/x-pem-file"):
2017-01-30 06:29:01 +00:00
if req.get_param_as_bool("wait"):
url = config.LONG_POLL_SUBSCRIBE % "crl"
2017-01-30 06:29:01 +00:00
resp.status = falcon.HTTP_SEE_OTHER
resp.set_header("Location", url.encode("ascii"))
logger.debug(u"Redirecting to CRL request to %s", url)
resp.body = "Redirecting to %s" % url
2017-01-30 06:29:01 +00:00
else:
resp.set_header("Content-Type", "application/x-pem-file")
resp.append_header(
"Content-Disposition",
("attachment; filename=%s-crl.pem" % const.HOSTNAME).encode("ascii"))
logger.debug(u"Serving revocation list to %s in PEM format", req.context.get("remote_addr"))
resp.body = export_crl()
2016-03-29 10:28:58 +00:00
else:
logger.debug(u"Client %s asked revocation list in unsupported format" % req.context.get("remote_addr"))
2016-03-29 10:28:58 +00:00
raise falcon.HTTPUnsupportedMediaType(
"Client did not accept application/x-pkcs7-crl or application/x-pem-file")