Skip to content

Commit 6a2b609

Browse files
committed
Try to verify metadata by CA signed certificate
1 parent b6892a5 commit 6a2b609

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/xmlsec/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import copy
1313
import traceback
1414
from lxml.builder import ElementMaker
15+
from cryptography.x509 import ExtensionNotFound, BasicConstraints, load_pem_x509_certificate
1516
from xmlsec.exceptions import XMLSigException
1617
from xmlsec import constants
1718
from xmlsec.utils import parse_xml, pem2b64, unescape_xml_entities, delete_elt, root_elt, b64d, b64e, etree_to_string
@@ -313,6 +314,27 @@ def _verify(t, keyspec, sig_path=".//{%s}Signature" % NS['ds'], drop_signature=F
313314
this_cert = xmlsec.crypto.from_keyspec(keyspec, signature_element=sig)
314315
log.debug("key size: {!s} bits".format(this_cert.keysize))
315316

317+
# Try verification by CA signed signing certificate
318+
bc = None
319+
try:
320+
bc = this_cert.key.extensions.get_extension_for_class(BasicConstraints)
321+
except ExtensionNotFound:
322+
pass
323+
else:
324+
# If this_cert a CA cert it is probably not the signing cert
325+
if bc.value.ca is True:
326+
# Find X509Certificate in signature that is child of the root element
327+
cert = t.find("/ds:Signature/ds:KeyInfo/ds:X509Data/ds:X509Certificate", namespaces=NS)
328+
if cert is not None:
329+
certspec = "-----BEGIN CERTIFICATE-----\n" + cert.text.strip() + "\n-----END CERTIFICATE-----"
330+
embedded_cert = load_pem_x509_certificate(certspec.encode())
331+
try:
332+
embedded_cert.verify_directly_issued_by(this_cert.key)
333+
except Exception:
334+
raise XMLSigException("Metadata certificate not signed by CA")
335+
else:
336+
this_cert.key = embedded_cert
337+
316338
si = sig.find(".//{%s}SignedInfo" % NS['ds'])
317339
log.debug("Found signedinfo {!s}".format(etree.tostring(si)))
318340
cm_alg = _cm_alg(si)

0 commit comments

Comments
 (0)