diff --git a/docs/usage/examples.rst b/docs/usage/examples.rst index 18fe65b5..60a6800b 100644 --- a/docs/usage/examples.rst +++ b/docs/usage/examples.rst @@ -198,15 +198,20 @@ contains the following: The structure of the file should be fairly self-evident. Only links with @rel="urn:oasis:names:tc:SAML:2.0:metadata" -will be parsed. If a KeyInfo with a X509Certificate element (usual base64-encoded certificate format) then this -certificate is used to validate the signature on the downloaded SAML metadata. Note that while 'load' supports validation -based on certificate fingerprint the XRD format does not and you will have to include Base64-encoded certificates if -you want validation to work. +will be parsed. The X509Certificate elements (base64-encoded certificates) in a link's KeyInfo are the trusted signers +of the metadata it points to: the downloaded metadata is only loaded if it is signed by one of them, so during a key +rollover a link can list both the old and the new certificate. The certificate is used as such, not via its +fingerprint, so this also works for metadata whose signature does not carry the signing certificate, as is common for +MDQ responses. + +.. warning:: + + A link without a certificate is loaded **without** signature verification. Example 4 - Sign using a PKCS#11 module --------------------------------------- -Fetch SWAMID metadata (and validate the signature using a certificate matching the given SHA256 fingerprint), select +Fetch SWAMID metadata (and validate the signature using the SWAMID signing certificate in swamid-2.0.crt), select the Identity Providers, tidy it up a bit and sign with the key with the label 'signer' in the PKCS#11 module /usr/lib/libsofthsm.so. If a certificate is found in the same PKCS#11 object, that certificate is included in the Signature object. @@ -214,7 +219,7 @@ the Signature object. .. code-block:: yaml - load: - - 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 + - http://mds.swamid.se/md/swamid-2.0.xml verify swamid-2.0.crt - select: "!//md:EntityDescriptor[md:IDPSSODescriptor]" - xslt: stylesheet: tidy.xsl diff --git a/src/pyff/builtins.py b/src/pyff/builtins.py index b5b2581c..5cb49de0 100644 --- a/src/pyff/builtins.py +++ b/src/pyff/builtins.py @@ -694,11 +694,18 @@ def load(req: Plumbing.Request, *opts): - ``as``: name the resource, e.g. to refer to it in later pipeline steps. - ``verify``: check the XML signature of the resource, using either a PEM certificate file or a SHA-1 fingerprint of the signing certificate. With a fingerprint, the certificate embedded in the signature is - used and the fingerprint pins it. ``verify`` may be repeated to support key rollovers; a signature by any - one of the given certificates (or fingerprints) is accepted. Certificates and fingerprints cannot be mixed. + used and the fingerprint pins it, so a document whose signature carries no certificate - common for MDQ + responses - can only be verified with the certificate file. Other fingerprints, eg SHA-256, are not + supported. ``verify`` may be repeated to support key rollovers; a signature by any one of the given + certificates (or fingerprints) is accepted. Certificates and fingerprints cannot be mixed. - ``via``: name of a ``when`` clause in the same pipeline that is applied to the loaded metadata, for example to remove entities with ``fork merge remove``. - ``cleanup``: name of a ``when`` clause used to pre-process the parsed metadata before it is validated. + + A resource may also be an XRD or eIDAS MDSL file listing metadata URLs. Each listed URL is loaded and verified + with the certificates given for it in the file, as if with ``verify``; any one of them is accepted, so a key + rollover can list both. An XRD link without a certificate is loaded unverified; an MDSL entry without one is + skipped. """ _opts = dict(list(zip(opts[::2], opts[1::2]))) _opts.setdefault('timeout', 120) diff --git a/src/pyff/parse.py b/src/pyff/parse.py index deb6d045..f06caaf3 100644 --- a/src/pyff/parse.py +++ b/src/pyff/parse.py @@ -11,7 +11,7 @@ from pyff.resource import Resource, ResourceInfo # cert_dict replaces xmlsec.crypto.CertDict (fingerprint -> cert mapping). -from pyff.utils import cert_dict, find_matching_files, parse_xml, root, unicode_stream, utc_now +from pyff.utils import b64_to_pem, cert_dict, find_matching_files, parse_xml, root, unicode_stream, utc_now __author__ = 'leifj' @@ -120,12 +120,16 @@ def parse(self, resource: Resource, content: str) -> ParserInfo: for link in xrd.findall(".//{{{}}}Link[@rel='{}']".format(NS['xrd'], NS['md'])): link_href = link.get("href") certs = cert_dict(link) - fingerprints = list(certs.keys()) - fp = None - if len(fingerprints) > 0: - fp = fingerprints[0] - log.debug(f"XRD: {link_href} verified by {fp}") - child_opts = resource.opts.model_copy(update={'alias': None}) + if certs: + log.debug(f"XRD: {link_href} verified by {', '.join(certs.keys())}") + else: + log.debug(f"XRD: {link_href} has no certificate, not verified") + # The Link's certificates are the trusted signers; all of them are accepted so that a key rollover + # can list both the old and the new certificate. Without one the metadata is not verified, rather + # than with the verify option of the XRD itself. + child_opts = resource.opts.model_copy( + update={'alias': None, 'verify': [b64_to_pem(cert_b64) for cert_b64 in certs.values()] or None} + ) resource.add_child(link_href, child_opts) resource.last_seen = utc_now().replace(microsecond=0) resource.expire_time = None diff --git a/src/pyff/test/test_pipeline.py b/src/pyff/test/test_pipeline.py index e6dfda73..299a773d 100644 --- a/src/pyff/test/test_pipeline.py +++ b/src/pyff/test/test_pipeline.py @@ -280,6 +280,107 @@ def test_no_fail_on_error_invalid_dir(self): assert md.lookup('https://idp.example.com/saml2/idp/metadata.php') +class XRDTest(PipeLineTest): + """ + Each XRD Link's certificates are the trusted signers of the metadata it points to: cf. issue #264. + """ + + def setUp(self): + super().setUp() + self.tmpdir = tempfile.mkdtemp() + self.signed = os.path.join(self.tmpdir, 'signed.xml') + self.exec_pipeline( + f""" +- load: + - {self.datadir}/simple-pipeline +- select +- finalize: + cacheDuration: PT5H + validUntil: P10D +- sign: + key: {self.private_keyspec} + cert: {self.public_keyspec} +- publish: + output: {self.signed} +""" + ) + with open(self.public_keyspec) as fd: + self.signer_cert = fd.read() + + def tearDown(self): + shutil.rmtree(self.tmpdir) + + @staticmethod + def _other_cert(): + """A self-signed certificate for a key that did not sign the metadata""" + import datetime + + from cryptography import x509 + from cryptography.hazmat.primitives import hashes, serialization + from cryptography.hazmat.primitives.asymmetric import rsa + from cryptography.x509.oid import NameOID + + key = rsa.generate_private_key(public_exponent=65537, key_size=2048) + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, 'other')]) + now = datetime.datetime.now(datetime.timezone.utc) + cert = ( + x509.CertificateBuilder() + .subject_name(name) + .issuer_name(name) + .public_key(key.public_key()) + .serial_number(x509.random_serial_number()) + .not_valid_before(now) + .not_valid_after(now + datetime.timedelta(days=2)) + .sign(key, hashes.SHA256()) + ) + return cert.public_bytes(serialization.Encoding.PEM).decode() + + def _load_via_xrd(self, href, certs): + """Load ``href`` through an XRD Link carrying ``certs`` (PEM) and return the loaded entityIDs""" + key_infos = "".join( + '' + + "".join(pem.strip().splitlines()[1:-1]) + + '' + for pem in certs + ) + xrd = os.path.join(self.tmpdir, 'links.xrd') + with open(xrd, 'w') as fd: + fd.write( + f'{href}' + f'test{key_infos}' + ) + _res, md = self.exec_pipeline(f"- load:\n - {xrd}\n") + return {e.get('entityID') for e in md.store} + + def _signed_url(self): + return f'file://{self.signed}' + + def _tampered_url(self): + """The signed metadata with one entityID changed after signing""" + tampered = os.path.join(self.tmpdir, 'tampered.xml') + with open(self.signed) as src, open(tampered, 'w') as dst: + dst.write(src.read().replace('https://idp.aco.net/idp/shibboleth', 'https://evil.example.org/idp')) + return f'file://{tampered}' + + def test_link_certificate_verifies(self): + assert 'https://idp.aco.net/idp/shibboleth' in self._load_via_xrd(self._signed_url(), [self.signer_cert]) + + def test_wrong_link_certificate_is_rejected(self): + assert self._load_via_xrd(self._signed_url(), [self._other_cert()]) == set() + + def test_tampered_metadata_is_rejected(self): + assert self._load_via_xrd(self._tampered_url(), [self.signer_cert]) == set() + + def test_any_link_certificate_verifies(self): + """A key rollover lists both the old and the new certificate""" + entity_ids = self._load_via_xrd(self._signed_url(), [self._other_cert(), self.signer_cert]) + assert 'https://idp.aco.net/idp/shibboleth' in entity_ids + + def test_link_without_certificate_is_loaded_unverified(self): + """As in pyFF 2.0.0: without a certificate there is nothing to verify against""" + assert 'https://evil.example.org/idp' in self._load_via_xrd(self._tampered_url(), []) + + class SortTest(PipeLineTest): EID1 = "https://idp.aco.net/idp/shibboleth" EID2 = "https://idp.example.com/saml2/idp/metadata.php"