@@ -34,6 +34,9 @@ final class SignedElementTest extends TestCase
3434 /** @var \SimpleSAML\XMLSecurity\CryptoEncoding\PEM */
3535 private PEM $ certificate ;
3636
37+ /** @var \SimpleSAML\XMLSecurity\CryptoEncoding\PEM */
38+ private PEM $ wrong_certificate ;
39+
3740 /** @var \DOMElement */
3841 private DOMElement $ signedDocumentWithComments ;
3942
@@ -63,6 +66,10 @@ public function setUp(): void
6366 $ this ->certificate = PEM ::fromString (
6467 PEMCertificatesMock::getPlainCertificate (PEMCertificatesMock::SELFSIGNED_CERTIFICATE ),
6568 );
69+
70+ $ this ->wrong_certificate = PEM ::fromString (
71+ PEMCertificatesMock::getPlainCertificate (PEMCertificatesMock::OTHER_CERTIFICATE ),
72+ );
6673 }
6774
6875
@@ -108,6 +115,44 @@ public function testSuccessfulVerifyingWithGivenKey(): void
108115 }
109116
110117
118+ /**
119+ * Test the verification of a signature with the wrong key first, and the right one second.
120+ */
121+ public function testSuccessfulVerifyingWithWrongKeyFirstRightOneSecond (): void
122+ {
123+ $ customSigned = CustomSignable::fromXML ($ this ->signedDocument );
124+
125+ $ this ->assertTrue ($ customSigned ->isSigned ());
126+ $ signature = $ customSigned ->getSignature ();
127+ $ this ->assertInstanceOf (Signature::class, $ signature );
128+ $ sigAlg = $ signature ->getSignedInfo ()->getSignatureMethod ()->getAlgorithm ();
129+ $ this ->assertEquals (C::SIG_RSA_SHA256 , $ sigAlg );
130+
131+ $ verified = null ;
132+ foreach ([$ this ->wrong_certificate , $ this ->certificate ] as $ i => $ key ) {
133+ $ factory = new SignatureAlgorithmFactory ();
134+ $ certificate = new X509Certificate ($ key );
135+ $ verifier = $ factory ->getAlgorithm ($ sigAlg , $ certificate ->getPublicKey ());
136+
137+ try {
138+ $ verified = $ customSigned ->verify ($ verifier );
139+ break 1 ;
140+ } catch (\SimpleSAML \XMLSecurity \Exception \SignatureVerificationFailedException $ e ) {
141+ continue ;
142+ }
143+ }
144+
145+ $ this ->assertInstanceOf (CustomSignable::class, $ verified );
146+ $ this ->assertFalse ($ verified ->isSigned ());
147+ $ this ->assertEquals (
148+ '<ssp:CustomSignable xmlns:ssp="urn:x-simplesamlphp:namespace"><ssp:Chunk>Some ' .
149+ '</ssp:Chunk></ssp:CustomSignable> ' ,
150+ strval ($ verified ),
151+ );
152+ $ this ->assertEquals ($ certificate ->getPublicKey (), $ verified ->getVerifyingKey ());
153+ }
154+
155+
111156 /**
112157 * Test the verification of a signature without passing a key, just what's in KeyInfo
113158 */
0 commit comments