Skip to content

Commit b49a929

Browse files
committed
Support multiple decryption keys
1 parent 8b5033f commit b49a929

File tree

9 files changed

+32
-22
lines changed

9 files changed

+32
-22
lines changed

src/XML/EncryptedElementInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ public function hasDecryptionKey(): bool;
3333

3434

3535
/**
36-
* Get the encrypted key used to encrypt the current element.
36+
* Get the encrypted keys used to encrypt the current element.
3737
*
38-
* @return \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey|null
38+
* @return \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey[]
3939
*/
40-
public function getEncryptedKey(): ?EncryptedKey;
40+
public function getEncryptedKeys(): array;
4141

4242
/**
4343
* Get the EncryptedData object.

src/XML/EncryptedElementTrait.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
use SimpleSAML\XMLSecurity\Alg\Encryption\{EncryptionAlgorithmFactory, EncryptionAlgorithmInterface};
1212
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
1313
use SimpleSAML\XMLSecurity\Constants as C;
14-
use SimpleSAML\XMLSecurity\Exception\{InvalidArgumentException, NoEncryptedDataException, RuntimeException};
14+
use SimpleSAML\XMLSecurity\Exception\{
15+
InvalidArgumentException,
16+
NoEncryptedDataException,
17+
OpenSSLException,
18+
RuntimeException,
19+
};
1520
use SimpleSAML\XMLSecurity\Key\SymmetricKey;
1621
use SimpleSAML\XMLSecurity\XML\xenc\{EncryptedData, EncryptedKey};
1722

@@ -24,8 +29,8 @@
2429
*/
2530
trait EncryptedElementTrait
2631
{
27-
/** @var \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey|null */
28-
protected ?EncryptedKey $encryptedKey = null;
32+
/** @var \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey[] */
33+
protected array $encryptedKey = [];
2934

3035

3136
/**
@@ -43,7 +48,7 @@ public function __construct(
4348

4449
foreach ($keyInfo->getInfo() as $info) {
4550
if ($info instanceof EncryptedKey) {
46-
$this->encryptedKey = $info;
51+
$this->encryptedKey = [$info];
4752
break;
4853
}
4954
}
@@ -57,16 +62,16 @@ public function __construct(
5762
*/
5863
public function hasDecryptionKey(): bool
5964
{
60-
return $this->encryptedKey !== null;
65+
return !empty($this->encryptedKey);
6166
}
6267

6368

6469
/**
6570
* Get the encrypted key used to encrypt the current element.
6671
*
67-
* @return \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey|null
72+
* @return \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey[]
6873
*/
69-
public function getEncryptedKey(): ?EncryptedKey
74+
public function getEncryptedKeys(): array
7075
{
7176
return $this->encryptedKey;
7277
}
@@ -86,7 +91,7 @@ public function getEncryptedData(): EncryptedData
8691
/**
8792
* Decrypt the data in any given element.
8893
*
89-
* Use this method to decrypt an EncryptedData XML elemento into a string. If the resulting plaintext represents
94+
* Use this method to decrypt an EncryptedData XML element into a string. If the resulting plaintext represents
9095
* an XML document which has a corresponding implementation extending \SimpleSAML\XML\ElementInterface, you
9196
* can call this method to build an object from the resulting plaintext:
9297
*
@@ -125,12 +130,23 @@ protected function decryptData(EncryptionAlgorithmInterface $decryptor): string
125130
throw new RuntimeException('Cannot decrypt data with a session key and no EncryptionMethod.');
126131
}
127132

128-
$encryptedKey = $this->getEncryptedKey();
129-
$decryptionKey = $encryptedKey->decrypt($decryptor);
130-
131133
$factory = new EncryptionAlgorithmFactory(
132134
$this->getBlacklistedAlgorithms() ?? EncryptionAlgorithmFactory::DEFAULT_BLACKLIST,
133135
);
136+
137+
$decryptionKey = null;
138+
foreach ($this->getEncryptedKeys() as $encryptedKey) {
139+
try {
140+
$decryptionKey = $encryptedKey->decrypt($decryptor);
141+
} catch (OpenSSLException $e) {
142+
continue;
143+
}
144+
}
145+
146+
if ($decryptionKey === null) {
147+
throw new RuntimeException('Cannot decrypt the session key with any of the provided decryption keys.');
148+
}
149+
134150
$decryptor = $factory->getAlgorithm(
135151
$encMethod->getAlgorithm()->getValue(),
136152
new SymmetricKey($decryptionKey),

tests/XML/EncryptedCustom.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ public function decryptWithSessionKey(EncryptionAlgorithmInterface $decryptor):
154154
}
155155

156156
// decrypt the encryption key with the decryptor we were provided
157-
$encryptedKey = $this->getEncryptedKey();
158-
$decryptionKey = $encryptedKey->decrypt($decryptor);
157+
$encryptedKey = $this->getEncryptedKeys();
158+
$decryptionKey = $encryptedKey[0]->decrypt($decryptor);
159159

160160
/*
161161
* Instantiate a new decryptor with the blacklisted algorithms and encryption backend given. This decryptor

tests/XML/ds/TransformsTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use SimpleSAML\XML\DOMDocumentFactory;
1010
use SimpleSAML\XML\TestUtils\{SchemaValidationTestTrait, SerializableElementTestTrait};
1111
use SimpleSAML\XMLSchema\Type\{AnyURIValue, StringValue};
12-
use SimpleSAML\XMLSecurity\Constants as C;
1312
use SimpleSAML\XMLSecurity\XML\ds\{AbstractDsElement, Transform, Transforms, XPath};
1413
use SimpleSAML\XPath\Constants as XPATH_C;
1514

tests/XML/xenc/CipherReferenceTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use SimpleSAML\XML\DOMDocumentFactory;
1010
use SimpleSAML\XML\TestUtils\{SchemaValidationTestTrait, SerializableElementTestTrait};
1111
use SimpleSAML\XMLSchema\Type\{AnyURIValue, StringValue};
12-
use SimpleSAML\XMLSecurity\Constants as C;
1312
use SimpleSAML\XMLSecurity\XML\ds\{Transform, XPath};
1413
use SimpleSAML\XMLSecurity\XML\xenc\{AbstractReference, AbstractXencElement, CipherReference, Transforms};
1514
use SimpleSAML\XPath\Constants as XPATH_C;

tests/XML/xenc/DataReferenceTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use SimpleSAML\XML\DOMDocumentFactory;
1010
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
1111
use SimpleSAML\XMLSchema\Type\{AnyURIValue, StringValue};
12-
use SimpleSAML\XMLSecurity\Constants as C;
1312
use SimpleSAML\XMLSecurity\XML\ds\{Transform, Transforms, XPath};
1413
use SimpleSAML\XMLSecurity\XML\xenc\{AbstractReference, AbstractXencElement, DataReference};
1514
use SimpleSAML\XPath\Constants as XPATH_C;

tests/XML/xenc/KeyReferenceTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use SimpleSAML\XML\DOMDocumentFactory;
1010
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
1111
use SimpleSAML\XMLSchema\Type\{AnyURIValue, StringValue};
12-
use SimpleSAML\XMLSecurity\Constants as C;
1312
use SimpleSAML\XMLSecurity\XML\ds\{Transform, Transforms, XPath};
1413
use SimpleSAML\XMLSecurity\XML\xenc\{AbstractReference, AbstractXencElement, KeyReference};
1514
use SimpleSAML\XPath\Constants as XPATH_C;

tests/XML/xenc/ReferenceListTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use SimpleSAML\XML\DOMDocumentFactory;
1010
use SimpleSAML\XML\TestUtils\{SchemaValidationTestTrait, SerializableElementTestTrait};
1111
use SimpleSAML\XMLSchema\Type\{AnyURIValue, StringValue};
12-
use SimpleSAML\XMLSecurity\Constants as C;
1312
use SimpleSAML\XMLSecurity\XML\ds\{Transform, Transforms, XPath};
1413
use SimpleSAML\XMLSecurity\XML\xenc\{AbstractXencElement, DataReference, KeyReference, ReferenceList};
1514
use SimpleSAML\XPath\Constants as XPATH_C;

tests/XML/xenc/TransformsTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use SimpleSAML\XML\DOMDocumentFactory;
1010
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
1111
use SimpleSAML\XMLSchema\Type\{AnyURIValue, StringValue};
12-
use SimpleSAML\XMLSecurity\Constants as C;
1312
use SimpleSAML\XMLSecurity\XML\ds\{Transform, XPath};
1413
use SimpleSAML\XMLSecurity\XML\xenc\{AbstractXencElement, Transforms};
1514
use SimpleSAML\XPath\Constants as XPATH_C;

0 commit comments

Comments
 (0)