10
10
use SimpleSAML \XML \DOMDocumentFactory ;
11
11
use SimpleSAML \XMLSecurity \Alg \Encryption \EncryptionAlgorithmFactory ;
12
12
use SimpleSAML \XMLSecurity \Alg \KeyTransport \KeyTransportAlgorithmFactory ;
13
+ use SimpleSAML \XMLSecurity \Alg \Signature \SignatureAlgorithmFactory ;
13
14
use SimpleSAML \XMLSecurity \Constants as C ;
14
15
use SimpleSAML \XMLSecurity \Key \PrivateKey ;
15
16
use SimpleSAML \XMLSecurity \Key \PublicKey ;
16
17
use SimpleSAML \XMLSecurity \Key \SymmetricKey ;
18
+ use SimpleSAML \XMLSecurity \Test \XML \CustomSigned ;
17
19
use SimpleSAML \XMLSecurity \Test \XML \EncryptedCustom ;
18
20
use SimpleSAML \XMLSecurity \TestUtils \PEMCertificatesMock ;
19
21
use SimpleSAML \XMLSecurity \XML \EncryptableElementTrait ;
32
34
class EncryptedCustomTest extends TestCase
33
35
{
34
36
/** @var \DOMElement */
35
- private DOMElement $ signedDocument ;
37
+ private DOMElement $ signableDocument ;
36
38
37
39
/** @var PrivateKey */
38
40
protected PrivateKey $ privKey ;
@@ -45,8 +47,8 @@ class EncryptedCustomTest extends TestCase
45
47
*/
46
48
public function setUp (): void
47
49
{
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 ' ,
50
52
)->documentElement ;
51
53
52
54
$ this ->privKey = PEMCertificatesMock::getPrivateKey (PEMCertificatesMock::PRIVATE_KEY );
@@ -60,7 +62,7 @@ public function setUp(): void
60
62
public function testEncryptAndDecryptSharedSecret (): void
61
63
{
62
64
// instantiate
63
- $ customSigned = CustomSignable::fromXML ($ this ->signedDocument );
65
+ $ customSigned = CustomSignable::fromXML ($ this ->signableDocument );
64
66
$ sharedKey = SymmetricKey::generate (16 );
65
67
66
68
// encrypt
@@ -81,7 +83,7 @@ public function testEncryptAndDecryptSharedSecret(): void
81
83
public function testEncryptAndDecryptSessionKey (): void
82
84
{
83
85
// instantiate
84
- $ customSigned = CustomSignable::fromXML ($ this ->signedDocument );
86
+ $ customSigned = CustomSignable::fromXML ($ this ->signableDocument );
85
87
86
88
// encrypt
87
89
$ factory = new KeyTransportAlgorithmFactory ();
@@ -94,4 +96,42 @@ public function testEncryptAndDecryptSessionKey(): void
94
96
95
97
$ this ->assertEquals ($ customSigned , $ decryptedCustom );
96
98
}
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
+ }
97
137
}
0 commit comments