Skip to content

Commit 5005c40

Browse files
committed
Ensure encryption type is set to the required value
1 parent d81bf3a commit 5005c40

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

src/XML/EncryptableElementTrait.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,21 @@
55
namespace SimpleSAML\SAML2\XML;
66

77
use SimpleSAML\SAML2\Compat\ContainerSingleton;
8+
use SimpleSAML\XMLSchema\Type\AnyURIValue;
9+
use SimpleSAML\XMLSchema\Type\Base64BinaryValue;
10+
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmFactory;
11+
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmInterface;
12+
use SimpleSAML\XMLSecurity\Constants as C;
13+
use SimpleSAML\XMLSecurity\Key\SymmetricKey;
14+
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo;
815
use SimpleSAML\XMLSecurity\XML\EncryptableElementTrait as ParentEncryptableElementTrait;
16+
use SimpleSAML\XMLSecurity\XML\xenc\CipherData;
17+
use SimpleSAML\XMLSecurity\XML\xenc\CipherValue;
18+
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedData;
19+
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey;
20+
use SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod;
21+
22+
use function in_array;
923

1024
/**
1125
* Trait aggregating functionality for elements that are encrypted.
@@ -17,6 +31,63 @@ trait EncryptableElementTrait
1731
use ParentEncryptableElementTrait;
1832

1933

34+
/**
35+
* Encryt this object.
36+
*
37+
* @param \SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmInterface $encryptor The encryptor to use,
38+
* either to encrypt the object itself, or to encrypt a session key (if the encryptor implements a key transport
39+
* algorithm).
40+
*
41+
* @return \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData
42+
*/
43+
public function encrypt(EncryptionAlgorithmInterface $encryptor): EncryptedData
44+
{
45+
$keyInfo = null;
46+
if (in_array($encryptor->getAlgorithmId(), C::$KEY_TRANSPORT_ALGORITHMS)) {
47+
// the encryptor uses a key transport algorithm, use that to generate a session key
48+
$sessionKey = SymmetricKey::generate($this->sessionKeyLen);
49+
50+
$encryptedKey = EncryptedKey::fromKey(
51+
$sessionKey,
52+
$encryptor,
53+
new EncryptionMethod(
54+
AnyURIValue::fromString($encryptor->getAlgorithmId()),
55+
),
56+
);
57+
58+
$keyInfo = new KeyInfo([$encryptedKey]);
59+
60+
$factory = new EncryptionAlgorithmFactory(
61+
$this->getBlacklistedAlgorithms() ?? EncryptionAlgorithmFactory::DEFAULT_BLACKLIST,
62+
);
63+
$encryptor = $factory->getAlgorithm($this->blockCipherAlgId, $sessionKey);
64+
$encryptor->setBackend($this->getEncryptionBackend());
65+
}
66+
67+
$xmlRepresentation = $this->toXML();
68+
69+
return new EncryptedData(
70+
new CipherData(
71+
new CipherValue(
72+
Base64BinaryValue::fromString(
73+
base64_encode($encryptor->encrypt(
74+
$xmlRepresentation->ownerDocument->saveXML($xmlRepresentation),
75+
)),
76+
),
77+
),
78+
),
79+
null,
80+
AnyURIValue::fromString(C::XMLENC_ELEMENT),
81+
null,
82+
null,
83+
new EncryptionMethod(
84+
AnyURIValue::fromString($encryptor->getAlgorithmId()),
85+
),
86+
$keyInfo,
87+
);
88+
}
89+
90+
2091
/**
2192
* @return array|null
2293
*/

src/XML/EncryptedElementTrait.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ final public function __construct(
4242
/**
4343
* 6.2: The <EncryptedData> element's Type attribute SHOULD be used and, if it is
4444
* present, MUST have the value http://www.w3.org/2001/04/xmlenc#Element.
45-
*
4645
*/
4746
Assert::nullOrSame($encryptedData->getType()->getValue(), C::XMLENC_ELEMENT);
4847

src/XML/saml/Attribute.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use DOMElement;
88
use SimpleSAML\SAML2\Assert\Assert;
99
use SimpleSAML\SAML2\Constants as C;
10+
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
1011
use SimpleSAML\SAML2\Type\SAMLAnyURIValue;
1112
use SimpleSAML\SAML2\Type\SAMLStringValue;
1213
use SimpleSAML\SAML2\XML\EncryptableElementTrait;
@@ -85,7 +86,7 @@ public function __construct(
8586
}
8687

8788
$types = array_map(
88-
function(AttributeValue $av) {
89+
function (AttributeValue $av) {
8990
return $av->getXsiType();
9091
},
9192
$attributeValue,

tests/SAML2/XML/saml/AttributeTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
2222
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
2323
use SimpleSAML\XMLSchema\Exception\MissingAttributeException;
24-
use SimpleSAML\XMLSchema\Type\DateTimeValue;
25-
use SimpleSAML\XMLSchema\Type\IntegerValue;
2624
use SimpleSAML\XMLSchema\Type\StringValue;
2725
use SimpleSAML\XMLSecurity\Alg\KeyTransport\KeyTransportAlgorithmFactory;
2826
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;

0 commit comments

Comments
 (0)