Skip to content

Commit d348f6e

Browse files
authored
Merge pull request #356 from IdentityPython/jocar-264
Fix broken signature validation in XDRs
2 parents aadde30 + 3bf14df commit d348f6e

4 files changed

Lines changed: 132 additions & 15 deletions

File tree

‎docs/usage/examples.rst‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,23 +198,28 @@ contains the following:
198198
</XRDS>
199199
200200
The structure of the file should be fairly self-evident. Only links with @rel="urn:oasis:names:tc:SAML:2.0:metadata"
201-
will be parsed. If a KeyInfo with a X509Certificate element (usual base64-encoded certificate format) then this
202-
certificate is used to validate the signature on the downloaded SAML metadata. Note that while 'load' supports validation
203-
based on certificate fingerprint the XRD format does not and you will have to include Base64-encoded certificates if
204-
you want validation to work.
201+
will be parsed. The X509Certificate elements (base64-encoded certificates) in a link's KeyInfo are the trusted signers
202+
of the metadata it points to: the downloaded metadata is only loaded if it is signed by one of them, so during a key
203+
rollover a link can list both the old and the new certificate. The certificate is used as such, not via its
204+
fingerprint, so this also works for metadata whose signature does not carry the signing certificate, as is common for
205+
MDQ responses.
206+
207+
.. warning::
208+
209+
A link without a certificate is loaded **without** signature verification.
205210

206211
Example 4 - Sign using a PKCS#11 module
207212
---------------------------------------
208213

209-
Fetch SWAMID metadata (and validate the signature using a certificate matching the given SHA256 fingerprint), select
214+
Fetch SWAMID metadata (and validate the signature using the SWAMID signing certificate in swamid-2.0.crt), select
210215
the Identity Providers, tidy it up a bit and sign with the key with the label 'signer' in the PKCS#11 module
211216
/usr/lib/libsofthsm.so. If a certificate is found in the same PKCS#11 object, that certificate is included in
212217
the Signature object.
213218

214219
.. code-block:: yaml
215220
216221
- load:
217-
- http://mds.swamid.se/md/swamid-2.0.xml A6:78:5A:37:C9:C9:0C:25:AD:5F:1F:69:22:EF:76:7B:C9:78:67:67:3A:AF:4F:8B:EA:A1:A7:6D:A3:A8:E5:85
222+
- http://mds.swamid.se/md/swamid-2.0.xml verify swamid-2.0.crt
218223
- select: "!//md:EntityDescriptor[md:IDPSSODescriptor]"
219224
- xslt:
220225
stylesheet: tidy.xsl

‎src/pyff/builtins.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,11 +694,18 @@ def load(req: Plumbing.Request, *opts):
694694
- ``as``: name the resource, e.g. to refer to it in later pipeline steps.
695695
- ``verify``: check the XML signature of the resource, using either a PEM certificate file or a SHA-1
696696
fingerprint of the signing certificate. With a fingerprint, the certificate embedded in the signature is
697-
used and the fingerprint pins it. ``verify`` may be repeated to support key rollovers; a signature by any
698-
one of the given certificates (or fingerprints) is accepted. Certificates and fingerprints cannot be mixed.
697+
used and the fingerprint pins it, so a document whose signature carries no certificate - common for MDQ
698+
responses - can only be verified with the certificate file. Other fingerprints, eg SHA-256, are not
699+
supported. ``verify`` may be repeated to support key rollovers; a signature by any one of the given
700+
certificates (or fingerprints) is accepted. Certificates and fingerprints cannot be mixed.
699701
- ``via``: name of a ``when`` clause in the same pipeline that is applied to the loaded metadata, for
700702
example to remove entities with ``fork merge remove``.
701703
- ``cleanup``: name of a ``when`` clause used to pre-process the parsed metadata before it is validated.
704+
705+
A resource may also be an XRD or eIDAS MDSL file listing metadata URLs. Each listed URL is loaded and verified
706+
with the certificates given for it in the file, as if with ``verify``; any one of them is accepted, so a key
707+
rollover can list both. An XRD link without a certificate is loaded unverified; an MDSL entry without one is
708+
skipped.
702709
"""
703710
_opts = dict(list(zip(opts[::2], opts[1::2])))
704711
_opts.setdefault('timeout', 120)

‎src/pyff/parse.py‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from pyff.resource import Resource, ResourceInfo
1212

1313
# cert_dict replaces xmlsec.crypto.CertDict (fingerprint -> cert mapping).
14-
from pyff.utils import cert_dict, find_matching_files, parse_xml, root, unicode_stream, utc_now
14+
from pyff.utils import b64_to_pem, cert_dict, find_matching_files, parse_xml, root, unicode_stream, utc_now
1515

1616
__author__ = 'leifj'
1717

@@ -120,12 +120,16 @@ def parse(self, resource: Resource, content: str) -> ParserInfo:
120120
for link in xrd.findall(".//{{{}}}Link[@rel='{}']".format(NS['xrd'], NS['md'])):
121121
link_href = link.get("href")
122122
certs = cert_dict(link)
123-
fingerprints = list(certs.keys())
124-
fp = None
125-
if len(fingerprints) > 0:
126-
fp = fingerprints[0]
127-
log.debug(f"XRD: {link_href} verified by {fp}")
128-
child_opts = resource.opts.model_copy(update={'alias': None})
123+
if certs:
124+
log.debug(f"XRD: {link_href} verified by {', '.join(certs.keys())}")
125+
else:
126+
log.debug(f"XRD: {link_href} has no certificate, not verified")
127+
# The Link's certificates are the trusted signers; all of them are accepted so that a key rollover
128+
# can list both the old and the new certificate. Without one the metadata is not verified, rather
129+
# than with the verify option of the XRD itself.
130+
child_opts = resource.opts.model_copy(
131+
update={'alias': None, 'verify': [b64_to_pem(cert_b64) for cert_b64 in certs.values()] or None}
132+
)
129133
resource.add_child(link_href, child_opts)
130134
resource.last_seen = utc_now().replace(microsecond=0)
131135
resource.expire_time = None

‎src/pyff/test/test_pipeline.py‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,107 @@ def test_no_fail_on_error_invalid_dir(self):
280280
assert md.lookup('https://idp.example.com/saml2/idp/metadata.php')
281281

282282

283+
class XRDTest(PipeLineTest):
284+
"""
285+
Each XRD Link's certificates are the trusted signers of the metadata it points to: cf. issue #264.
286+
"""
287+
288+
def setUp(self):
289+
super().setUp()
290+
self.tmpdir = tempfile.mkdtemp()
291+
self.signed = os.path.join(self.tmpdir, 'signed.xml')
292+
self.exec_pipeline(
293+
f"""
294+
- load:
295+
- {self.datadir}/simple-pipeline
296+
- select
297+
- finalize:
298+
cacheDuration: PT5H
299+
validUntil: P10D
300+
- sign:
301+
key: {self.private_keyspec}
302+
cert: {self.public_keyspec}
303+
- publish:
304+
output: {self.signed}
305+
"""
306+
)
307+
with open(self.public_keyspec) as fd:
308+
self.signer_cert = fd.read()
309+
310+
def tearDown(self):
311+
shutil.rmtree(self.tmpdir)
312+
313+
@staticmethod
314+
def _other_cert():
315+
"""A self-signed certificate for a key that did not sign the metadata"""
316+
import datetime
317+
318+
from cryptography import x509
319+
from cryptography.hazmat.primitives import hashes, serialization
320+
from cryptography.hazmat.primitives.asymmetric import rsa
321+
from cryptography.x509.oid import NameOID
322+
323+
key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
324+
name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, 'other')])
325+
now = datetime.datetime.now(datetime.timezone.utc)
326+
cert = (
327+
x509.CertificateBuilder()
328+
.subject_name(name)
329+
.issuer_name(name)
330+
.public_key(key.public_key())
331+
.serial_number(x509.random_serial_number())
332+
.not_valid_before(now)
333+
.not_valid_after(now + datetime.timedelta(days=2))
334+
.sign(key, hashes.SHA256())
335+
)
336+
return cert.public_bytes(serialization.Encoding.PEM).decode()
337+
338+
def _load_via_xrd(self, href, certs):
339+
"""Load ``href`` through an XRD Link carrying ``certs`` (PEM) and return the loaded entityIDs"""
340+
key_infos = "".join(
341+
'<ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><ds:X509Data><ds:X509Certificate>'
342+
+ "".join(pem.strip().splitlines()[1:-1])
343+
+ '</ds:X509Certificate></ds:X509Data></ds:KeyInfo>'
344+
for pem in certs
345+
)
346+
xrd = os.path.join(self.tmpdir, 'links.xrd')
347+
with open(xrd, 'w') as fd:
348+
fd.write(
349+
f'<XRDS xmlns="{NS["xrd"]}"><XRD><Subject>{href}</Subject>'
350+
f'<Link rel="{NS["md"]}" href="{href}"><Title>test</Title>{key_infos}</Link></XRD></XRDS>'
351+
)
352+
_res, md = self.exec_pipeline(f"- load:\n - {xrd}\n")
353+
return {e.get('entityID') for e in md.store}
354+
355+
def _signed_url(self):
356+
return f'file://{self.signed}'
357+
358+
def _tampered_url(self):
359+
"""The signed metadata with one entityID changed after signing"""
360+
tampered = os.path.join(self.tmpdir, 'tampered.xml')
361+
with open(self.signed) as src, open(tampered, 'w') as dst:
362+
dst.write(src.read().replace('https://idp.aco.net/idp/shibboleth', 'https://evil.example.org/idp'))
363+
return f'file://{tampered}'
364+
365+
def test_link_certificate_verifies(self):
366+
assert 'https://idp.aco.net/idp/shibboleth' in self._load_via_xrd(self._signed_url(), [self.signer_cert])
367+
368+
def test_wrong_link_certificate_is_rejected(self):
369+
assert self._load_via_xrd(self._signed_url(), [self._other_cert()]) == set()
370+
371+
def test_tampered_metadata_is_rejected(self):
372+
assert self._load_via_xrd(self._tampered_url(), [self.signer_cert]) == set()
373+
374+
def test_any_link_certificate_verifies(self):
375+
"""A key rollover lists both the old and the new certificate"""
376+
entity_ids = self._load_via_xrd(self._signed_url(), [self._other_cert(), self.signer_cert])
377+
assert 'https://idp.aco.net/idp/shibboleth' in entity_ids
378+
379+
def test_link_without_certificate_is_loaded_unverified(self):
380+
"""As in pyFF 2.0.0: without a certificate there is nothing to verify against"""
381+
assert 'https://evil.example.org/idp' in self._load_via_xrd(self._tampered_url(), [])
382+
383+
283384
class SortTest(PipeLineTest):
284385
EID1 = "https://idp.aco.net/idp/shibboleth"
285386
EID2 = "https://idp.example.com/saml2/idp/metadata.php"

0 commit comments

Comments
 (0)