1010use SimpleSAML \XML \DOMDocumentFactory ;
1111use SimpleSAML \XMLSecurity \Alg \Encryption \EncryptionAlgorithmFactory ;
1212use SimpleSAML \XMLSecurity \Alg \KeyTransport \KeyTransportAlgorithmFactory ;
13+ use SimpleSAML \XMLSecurity \Alg \Signature \SignatureAlgorithmFactory ;
1314use SimpleSAML \XMLSecurity \Constants as C ;
1415use SimpleSAML \XMLSecurity \Key \PrivateKey ;
1516use SimpleSAML \XMLSecurity \Key \PublicKey ;
1617use SimpleSAML \XMLSecurity \Key \SymmetricKey ;
18+ use SimpleSAML \XMLSecurity \Test \XML \CustomSigned ;
1719use SimpleSAML \XMLSecurity \Test \XML \EncryptedCustom ;
1820use SimpleSAML \XMLSecurity \TestUtils \PEMCertificatesMock ;
1921use SimpleSAML \XMLSecurity \XML \EncryptableElementTrait ;
3234class EncryptedCustomTest extends TestCase
3335{
3436 /** @var \DOMElement */
35- private DOMElement $ signedDocument ;
37+ private DOMElement $ signableDocument ;
3638
3739 /** @var PrivateKey */
3840 protected PrivateKey $ privKey ;
@@ -45,8 +47,8 @@ class EncryptedCustomTest extends TestCase
4547 */
4648 public function setUp (): void
4749 {
48- $ this ->signedDocument = DOMDocumentFactory::fromFile (
49- dirname (__FILE__ , 2 ) . '/resources/xml/custom_CustomSignableSigned .xml ' ,
50+ $ this ->signableDocument = DOMDocumentFactory::fromFile (
51+ dirname (__FILE__ , 2 ) . '/resources/xml/custom_CustomSignable .xml ' ,
5052 )->documentElement ;
5153
5254 $ this ->privKey = PEMCertificatesMock::getPrivateKey (PEMCertificatesMock::PRIVATE_KEY );
@@ -60,7 +62,7 @@ public function setUp(): void
6062 public function testEncryptAndDecryptSharedSecret (): void
6163 {
6264 // instantiate
63- $ customSigned = CustomSignable::fromXML ($ this ->signedDocument );
65+ $ customSigned = CustomSignable::fromXML ($ this ->signableDocument );
6466 $ sharedKey = SymmetricKey::generate (16 );
6567
6668 // encrypt
@@ -81,7 +83,7 @@ public function testEncryptAndDecryptSharedSecret(): void
8183 public function testEncryptAndDecryptSessionKey (): void
8284 {
8385 // instantiate
84- $ customSigned = CustomSignable::fromXML ($ this ->signedDocument );
86+ $ customSigned = CustomSignable::fromXML ($ this ->signableDocument );
8587
8688 // encrypt
8789 $ factory = new KeyTransportAlgorithmFactory ();
@@ -94,4 +96,42 @@ public function testEncryptAndDecryptSessionKey(): void
9496
9597 $ this ->assertEquals ($ customSigned , $ decryptedCustom );
9698 }
99+
100+
101+ /**
102+ * Test that a signature isn't mangled after encrypting/decrypting a signed object.
103+ */
104+ public function testSignatureVerifiesAfterEncryptionAndDecryption (): void
105+ {
106+ // instantiate
107+ $ customSigned = CustomSignable::fromXML ($ this ->signableDocument );
108+
109+ // sign
110+ $ privateKey = PEMCertificatesMock::getPrivateKey (PEMCertificatesMock::SELFSIGNED_PRIVATE_KEY );
111+ $ signer = (new SignatureAlgorithmFactory ())->getAlgorithm (
112+ C::SIG_RSA_SHA256 ,
113+ $ privateKey
114+ );
115+ $ customSigned ->sign ($ signer );
116+ $ customSigned = CustomSignable::fromXML ($ customSigned ->toXML ());
117+
118+ // encrypt
119+ $ factory = new KeyTransportAlgorithmFactory ();
120+ $ encryptor = $ factory ->getAlgorithm (C::KEY_TRANSPORT_OAEP_MGF1P , $ this ->pubKey );
121+ $ encryptedCustom = new EncryptedCustom ($ customSigned ->encrypt ($ encryptor ));
122+
123+ // decrypt
124+ $ decryptor = $ factory ->getAlgorithm (C::KEY_TRANSPORT_OAEP_MGF1P , $ this ->privKey );
125+ $ decryptedCustom = $ encryptedCustom ->decrypt ($ decryptor );
126+
127+ // verify signature
128+ $ publicKey = PEMCertificatesMock::getPublicKey (PEMCertificatesMock::SELFSIGNED_PUBLIC_KEY );
129+ $ verifier = (new SignatureAlgorithmFactory ())->getAlgorithm (
130+ $ decryptedCustom ->getSignature ()->getSignedInfo ()->getSignatureMethod ()->getAlgorithm (),
131+ $ publicKey ,
132+ );
133+
134+ $ verified = $ decryptedCustom ->verify ($ verifier );
135+ $ this ->assertInstanceOf (CustomSignable::class, $ verified );
136+ }
97137}
0 commit comments