1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SAML2\XML; |
6
|
|
|
|
7
|
|
|
use DOMElement; |
8
|
|
|
use RobRichards\XMLSecLibs\XMLSecEnc; |
9
|
|
|
use RobRichards\XMLSecLibs\XMLSecurityKey; |
10
|
|
|
use SAML2\XML\xenc\EncryptedData; |
11
|
|
|
use SAML2\XML\xenc\EncryptedKey; |
12
|
|
|
use Webmozart\Assert\Assert; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait aggregating functionality for encrypted elements. |
16
|
|
|
* |
17
|
|
|
* @package simplesamlphp/saml2 |
18
|
|
|
*/ |
19
|
|
|
trait EncryptedElementTrait |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The current encrypted ID. |
24
|
|
|
* |
25
|
|
|
* @var \SAML2\XML\xenc\EncryptedData |
26
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
27
|
|
|
*/ |
28
|
|
|
protected $encryptedData; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* A list of encrypted keys. |
32
|
|
|
* |
33
|
|
|
* @var \SAML2\XML\xenc\EncryptedKey[] |
34
|
|
|
*/ |
35
|
|
|
protected $encryptedKeys = []; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor for encrypted elements. |
40
|
|
|
* |
41
|
|
|
* @param \SAML2\XML\xenc\EncryptedData $encryptedData The EncryptedData object. |
42
|
|
|
* @param \SAML2\XML\xenc\EncryptedKey[] $encryptedKeys An array of zero or more EncryptedKey objects. |
43
|
|
|
*/ |
44
|
|
|
public function __construct(EncryptedData $encryptedData, array $encryptedKeys) |
45
|
|
|
{ |
46
|
|
|
$this->setEncryptedData($encryptedData); |
47
|
|
|
$this->setEncryptedKeys($encryptedKeys); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the EncryptedData object. |
53
|
|
|
* |
54
|
|
|
* @return \SAML2\XML\xenc\EncryptedData |
55
|
|
|
*/ |
56
|
|
|
public function getEncryptedData(): EncryptedData |
57
|
|
|
{ |
58
|
|
|
return $this->encryptedData; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param \SAML2\XML\xenc\EncryptedData $encryptedData |
64
|
|
|
*/ |
65
|
|
|
protected function setEncryptedData(EncryptedData $encryptedData): void |
66
|
|
|
{ |
67
|
|
|
$this->encryptedData = $encryptedData; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the array of EncryptedKey objects |
73
|
|
|
* |
74
|
|
|
* @return \SAML2\XML\xenc\EncryptedKey[] |
75
|
|
|
*/ |
76
|
|
|
public function getEncryptedKeys(): array |
77
|
|
|
{ |
78
|
|
|
return $this->encryptedKeys; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param \SAML2\XML\xenc\EncryptedKey[] $encryptedKeys |
84
|
|
|
*/ |
85
|
|
|
protected function setEncryptedKeys(array $encryptedKeys): void |
86
|
|
|
{ |
87
|
|
|
Assert::allIsInstanceOf( |
88
|
|
|
$encryptedKeys, |
89
|
|
|
EncryptedKey::class, |
90
|
|
|
'All encrypted keys in <' . $this->getQualifiedName() . '> must be an instance of EncryptedKey.' |
|
|
|
|
91
|
|
|
); |
92
|
|
|
$this->encryptedKeys = $encryptedKeys; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Create an encrypted element from a given unencrypted element and a key. |
98
|
|
|
* |
99
|
|
|
* @param \SAML2\XML\AbstractXMLElement $element |
100
|
|
|
* @param \RobRichards\XMLSecLibs\XMLSecurityKey $key |
101
|
|
|
* |
102
|
|
|
* @return \SAML2\EncryptedElementInterface |
|
|
|
|
103
|
|
|
* @throws \Exception |
104
|
|
|
*/ |
105
|
|
|
public static function fromUnencryptedElement( |
106
|
|
|
AbstractXMLElement $element, |
107
|
|
|
XMLSecurityKey $key |
108
|
|
|
): EncryptedElementInterface { |
109
|
|
|
$xml = $element->toXML(); |
110
|
|
|
|
111
|
|
|
Utils::getContainer()->debugMessage($xml, 'encrypt'); |
|
|
|
|
112
|
|
|
|
113
|
|
|
$enc = new XMLSecEnc(); |
114
|
|
|
$enc->setNode($xml); |
115
|
|
|
$enc->type = XMLSecEnc::Element; |
116
|
|
|
|
117
|
|
|
switch ($key->type) { |
118
|
|
|
case XMLSecurityKey::TRIPLEDES_CBC: |
119
|
|
|
case XMLSecurityKey::AES128_CBC: |
120
|
|
|
case XMLSecurityKey::AES192_CBC: |
121
|
|
|
case XMLSecurityKey::AES256_CBC: |
122
|
|
|
$symmetricKey = $key; |
123
|
|
|
break; |
124
|
|
|
|
125
|
|
|
case XMLSecurityKey::RSA_1_5: |
126
|
|
|
case XMLSecurityKey::RSA_OAEP_MGF1P: |
127
|
|
|
$symmetricKey = new XMLSecurityKey(XMLSecurityKey::AES128_CBC); |
128
|
|
|
$symmetricKey->generateSessionKey(); |
129
|
|
|
|
130
|
|
|
$enc->encryptKey($key, $symmetricKey); |
131
|
|
|
|
132
|
|
|
break; |
133
|
|
|
|
134
|
|
|
default: |
135
|
|
|
throw new \Exception('Unknown key type for encryption: ' . $key->type); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$dom = $enc->encryptNode($symmetricKey); |
139
|
|
|
/** @var \SAML2\XML\xenc\EncryptedData $encData */ |
140
|
|
|
$encData = EncryptedData::fromXML($dom); |
141
|
|
|
return new static($encData, []); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @inheritDoc |
147
|
|
|
* @return \SAML2\XML\AbstractXMLElement |
148
|
|
|
*/ |
149
|
|
|
public static function fromXML(DOMElement $xml): object |
150
|
|
|
{ |
151
|
|
|
Assert::same($xml->localName, self::getClassName(static::class)); |
152
|
|
|
Assert::same($xml->namespaceURI, static::NS); |
|
|
|
|
153
|
|
|
|
154
|
|
|
$ed = EncryptedData::getChildrenOfClass($xml); |
155
|
|
|
Assert::count($ed, 1, 'No more or less than one EncryptedData element allowed in ' . |
156
|
|
|
AbstractXMLElement::getClassName(static::class) . '.'); |
157
|
|
|
|
158
|
|
|
$ek = EncryptedKey::getChildrenOfClass($xml); |
159
|
|
|
|
160
|
|
|
return new static($ed[0], $ek); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @inheritDoc |
166
|
|
|
*/ |
167
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
168
|
|
|
{ |
169
|
|
|
$e = $this->instantiateParentElement($parent); |
|
|
|
|
170
|
|
|
|
171
|
|
|
$this->encryptedData->toXML($e); |
172
|
|
|
|
173
|
|
|
foreach ($this->encryptedKeys as $key) { |
174
|
|
|
$key->toXML($e); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $e; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|