Skip to content

Commit e908dd6

Browse files
committed
Improve unit test
1 parent bf8e73e commit e908dd6

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

tests/XML/EncryptedCustomTest.php

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
use SimpleSAML\XML\DOMDocumentFactory;
1111
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmFactory;
1212
use SimpleSAML\XMLSecurity\Alg\KeyTransport\KeyTransportAlgorithmFactory;
13+
use SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmFactory;
1314
use SimpleSAML\XMLSecurity\Constants as C;
1415
use SimpleSAML\XMLSecurity\Key\PrivateKey;
1516
use SimpleSAML\XMLSecurity\Key\PublicKey;
1617
use SimpleSAML\XMLSecurity\Key\SymmetricKey;
18+
use SimpleSAML\XMLSecurity\Test\XML\CustomSigned;
1719
use SimpleSAML\XMLSecurity\Test\XML\EncryptedCustom;
1820
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
1921
use SimpleSAML\XMLSecurity\XML\EncryptableElementTrait;
@@ -32,7 +34,7 @@
3234
class 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

Comments
 (0)