1111use SimpleSAML \XMLSecurity \Alg \Encryption \{EncryptionAlgorithmFactory , EncryptionAlgorithmInterface };
1212use SimpleSAML \XMLSecurity \Backend \EncryptionBackend ;
1313use 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+ };
1520use SimpleSAML \XMLSecurity \Key \SymmetricKey ;
1621use SimpleSAML \XMLSecurity \XML \xenc \{EncryptedData , EncryptedKey };
1722
2429 */
2530trait 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 ),
0 commit comments