@@ -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+
283384class SortTest (PipeLineTest ):
284385 EID1 = "https://idp.aco.net/idp/shibboleth"
285386 EID2 = "https://idp.example.com/saml2/idp/metadata.php"
0 commit comments