|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\SAML2\XML; |
|
6
|
|
|
|
|
7
|
|
|
use DOMElement; |
|
8
|
|
|
use SimpleSAML\Assert\Assert; |
|
9
|
|
|
use SimpleSAML\SAML2\Compat\ContainerSingleton; |
|
10
|
|
|
use SimpleSAML\SAML2\Exception\ProtocolViolationException; |
|
11
|
|
|
use SimpleSAML\XML\AbstractElement; |
|
12
|
|
|
use SimpleSAML\XML\Exception\InvalidDOMElementException; |
|
13
|
|
|
use SimpleSAML\XML\Exception\TooManyElementsException; |
|
14
|
|
|
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend; |
|
15
|
|
|
use SimpleSAML\XMLSecurity\XML\EncryptedElementTrait as ParentEncryptedElementTrait; |
|
16
|
|
|
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedData; |
|
17
|
|
|
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Trait aggregating functionality for elements that are encrypted. |
|
21
|
|
|
* |
|
22
|
|
|
* @package simplesamlphp/saml2 |
|
23
|
|
|
*/ |
|
24
|
|
|
trait EncryptedElementTrait |
|
25
|
|
|
{ |
|
26
|
|
|
use ParentEncryptedElementTrait; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Constructor for encrypted elements. |
|
31
|
|
|
* |
|
32
|
|
|
* @param \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData $encryptedData The EncryptedData object. |
|
33
|
|
|
* @param \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey[] $decryptionKeys The EncryptedKey objects. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct( |
|
36
|
|
|
protected EncryptedData $encryptedData, |
|
37
|
|
|
protected array $decryptionKeys = [], |
|
38
|
|
|
) { |
|
39
|
|
|
Assert::allIsInstanceOf($decryptionKeys, EncryptedKey::class, ProtocolViolationException::class); |
|
40
|
|
|
|
|
41
|
|
|
$keyInfo = $this->encryptedData->getKeyInfo(); |
|
42
|
|
|
if ($keyInfo === null) { |
|
43
|
|
|
return; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
foreach ($keyInfo->getInfo() as $info) { |
|
47
|
|
|
if ($info instanceof EncryptedKey) { |
|
48
|
|
|
$this->encryptedKey = $info; |
|
49
|
|
|
break; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
public function getBlacklistedAlgorithms(): ?array |
|
56
|
|
|
{ |
|
57
|
|
|
$container = ContainerSingleton::getInstance(); |
|
58
|
|
|
return $container->getBlacklistedEncryptionAlgorithms(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
public function getEncryptionBackend(): ?EncryptionBackend |
|
63
|
|
|
{ |
|
64
|
|
|
// return the encryption backend you want to use, |
|
65
|
|
|
// or null if you are fine with the default |
|
66
|
|
|
return null; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
public function getDecryptionKeys(): array |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->decryptionKeys; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @inheritDoc |
|
78
|
|
|
* |
|
79
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
|
80
|
|
|
* If the qualified name of the supplied element is wrong |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function fromXML(DOMElement $xml): static |
|
83
|
|
|
{ |
|
84
|
|
|
Assert::same( |
|
85
|
|
|
$xml->localName, |
|
86
|
|
|
AbstractElement::getClassName(static::class), |
|
87
|
|
|
InvalidDOMElementException::class, |
|
88
|
|
|
); |
|
89
|
|
|
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
$ed = EncryptedData::getChildrenOfClass($xml); |
|
92
|
|
|
Assert::count( |
|
93
|
|
|
$ed, |
|
94
|
|
|
1, |
|
95
|
|
|
sprintf( |
|
96
|
|
|
'No more or less than one EncryptedData element allowed in %s.', |
|
97
|
|
|
AbstractElement::getClassName(static::class), |
|
98
|
|
|
), |
|
99
|
|
|
TooManyElementsException::class, |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
$ek = EncryptedKey::getChildrenOfClass($xml); |
|
103
|
|
|
|
|
104
|
|
|
return new static($ed[0], $ek); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @inheritDoc |
|
110
|
|
|
*/ |
|
111
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
|
112
|
|
|
{ |
|
113
|
|
|
/** @psalm-var \DOMDocument $e->ownerDocument */ |
|
114
|
|
|
$e = $this->instantiateParentElement($parent); |
|
115
|
|
|
$this->encryptedData->toXML($e); |
|
116
|
|
|
foreach ($this->getDecryptionKeys() as $key) { |
|
117
|
|
|
$key->toXML($e); |
|
118
|
|
|
} |
|
119
|
|
|
return $e; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|