|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\XML\xenc; |
|
6
|
|
|
|
|
7
|
|
|
use DOMElement; |
|
8
|
|
|
use SimpleSAML\Assert\Assert; |
|
9
|
|
|
use SimpleSAML\XML\SchemaValidatableElementInterface; |
|
10
|
|
|
use SimpleSAML\XML\SchemaValidatableElementTrait; |
|
11
|
|
|
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException; |
|
12
|
|
|
use SimpleSAML\XMLSchema\Exception\MissingElementException; |
|
13
|
|
|
use SimpleSAML\XMLSchema\Exception\TooManyElementsException; |
|
14
|
|
|
use SimpleSAML\XMLSchema\Type\AnyURIValue; |
|
15
|
|
|
use SimpleSAML\XMLSchema\Type\IDValue; |
|
16
|
|
|
use SimpleSAML\XMLSchema\Type\StringValue; |
|
17
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo; |
|
18
|
|
|
|
|
19
|
|
|
use function array_pop; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class containing encrypted data. |
|
23
|
|
|
* |
|
24
|
|
|
* Note: <xenc:EncryptionProperties> elements are not supported. |
|
25
|
|
|
* |
|
26
|
|
|
* @package simplesamlphp/xml-security |
|
27
|
|
|
*/ |
|
28
|
|
|
final class EncryptedData extends AbstractEncryptedType implements SchemaValidatableElementInterface |
|
29
|
|
|
{ |
|
30
|
|
|
use SchemaValidatableElementTrait; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @inheritDoc |
|
35
|
|
|
* |
|
36
|
|
|
* @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException |
|
37
|
|
|
* If the qualified name of the supplied element is wrong |
|
38
|
|
|
* @throws \SimpleSAML\XMLSchema\Exception\MissingElementException |
|
39
|
|
|
* If one of the mandatory child-elements is missing |
|
40
|
|
|
* @throws \SimpleSAML\XMLSchema\Exception\TooManyElementsException |
|
41
|
|
|
* If too many child-elements of a type are specified |
|
42
|
|
|
*/ |
|
43
|
|
|
final public static function fromXML(DOMElement $xml): static |
|
44
|
|
|
{ |
|
45
|
|
|
Assert::same($xml->localName, 'EncryptedData', InvalidDOMElementException::class); |
|
46
|
|
|
Assert::same($xml->namespaceURI, EncryptedData::NS, InvalidDOMElementException::class); |
|
47
|
|
|
|
|
48
|
|
|
$cipherData = CipherData::getChildrenOfClass($xml); |
|
49
|
|
|
Assert::minCount( |
|
50
|
|
|
$cipherData, |
|
51
|
|
|
1, |
|
52
|
|
|
'At least one CipherData element found in <xenc:EncryptedData>.', |
|
53
|
|
|
MissingElementException::class, |
|
54
|
|
|
); |
|
55
|
|
|
Assert::maxCount( |
|
56
|
|
|
$cipherData, |
|
57
|
|
|
1, |
|
58
|
|
|
'No or more than one CipherData element found in <xenc:EncryptedData>.', |
|
59
|
|
|
TooManyElementsException::class, |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
$encryptionMethod = EncryptionMethod::getChildrenOfClass($xml); |
|
63
|
|
|
Assert::maxCount( |
|
64
|
|
|
$encryptionMethod, |
|
65
|
|
|
1, |
|
66
|
|
|
'No more than one EncryptionMethod element allowed in <xenc:EncryptedData>.', |
|
67
|
|
|
TooManyElementsException::class, |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
$keyInfo = KeyInfo::getChildrenOfClass($xml); |
|
71
|
|
|
Assert::maxCount( |
|
72
|
|
|
$keyInfo, |
|
73
|
|
|
1, |
|
74
|
|
|
'No more than one KeyInfo element allowed in <xenc:EncryptedData>.', |
|
75
|
|
|
TooManyElementsException::class, |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
return new static( |
|
79
|
|
|
$cipherData[0], |
|
80
|
|
|
self::getOptionalAttribute($xml, 'Id', IDValue::class, null), |
|
81
|
|
|
self::getOptionalAttribute($xml, 'Type', AnyURIValue::class, null), |
|
82
|
|
|
self::getOptionalAttribute($xml, 'MimeType', StringValue::class, null), |
|
83
|
|
|
self::getOptionalAttribute($xml, 'Encoding', AnyURIValue::class, null), |
|
84
|
|
|
array_pop($encryptionMethod), |
|
85
|
|
|
array_pop($keyInfo), |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|