Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Use cryptography primivites for x509 certificate processing #55

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions digid_eherkenning/saml2/eherkenning.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
from django.urls import reverse
from django.utils import timezone

from cryptography.hazmat.primitives import serialization
from cryptography.x509 import load_pem_x509_certificate
from furl.furl import furl
from lxml.builder import ElementMaker
from lxml.etree import Element, tostring
from OpenSSL import crypto

from ..choices import AssuranceLevels
from ..models import EherkenningConfiguration
Expand Down Expand Up @@ -268,19 +269,17 @@ def create_classifiers_element(classifiers: list) -> ElementMaker:
return ESC("Classifiers", *classifiers_elements)


def create_key_descriptor(x509_certificate_content):
x509_certificate = crypto.load_certificate(
crypto.FILETYPE_PEM, x509_certificate_content
)
key_descriptor_cert = b64encode(
crypto.dump_certificate(crypto.FILETYPE_ASN1, x509_certificate)
).decode("ascii")

certificate = x509_certificate.to_cryptography()
def create_key_descriptor(x509_certificate_content: bytes):
certificate = load_pem_x509_certificate(x509_certificate_content)
key_name = binascii.hexlify(
certificate.fingerprint(certificate.signature_hash_algorithm)
).decode("ascii")

# grab the actual base64 data describding the certificate, but without the
# BEGIN/END CERTIFICATE headers and footers and stripped of line breaks.
Comment on lines +278 to +279
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this new behaviour or is it the same as before?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as before, I had to step through the code with a debugger to figure out what the original code was even doing 😬

certificate_content = certificate.public_bytes(serialization.Encoding.DER)
key_descriptor_cert = b64encode(certificate_content).decode("ascii")

args = [
DS(
"KeyInfo",
Expand All @@ -297,7 +296,7 @@ def create_service_catalogus(conf, validate=True):
https://afsprakenstelsel.etoegang.nl/display/as/Service+catalog
"""
with conf["cert_file"].open("rb") as cert_file:
x509_certificate_content = cert_file.read()
x509_certificate_content: bytes = cert_file.read()

sc_id = str(uuid4())
service_provider_id = conf["oin"]
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ zip_safe = False
include_package_data = True
packages = find:
install_requires =
cryptography >= 40.0.0
django >= 3.2.0
django-sessionprofile
django-simple-certmanager
Expand Down
Loading