Passed
Push — master ( 84e061...59fb84 )
by Tim
02:20
created

AbstractEncryptedType::getCipherData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\MissingElementException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo;
13
14
/**
15
 * Abstract class representing encrypted data.
16
 *
17
 * Note: <xenc:EncryptionProperties> elements are not supported.
18
 *
19
 * @package simplesamlphp/xml-security
20
 */
21
class AbstractEncryptedType extends AbstractXencElement
22
{
23
    /** @var \SimpleSAML\XMLSecurity\XML\xenc\CipherData */
24
    protected CipherData $cipherData;
25
26
    /** @var string|null */
27
    protected ?string $encoding;
28
29
    /** @var \SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod|null */
30
    protected ?EncryptionMethod $encryptionMethod;
31
32
    /** @var string|null */
33
    protected ?string $id;
34
35
    /** @var \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null */
36
    protected ?KeyInfo $keyInfo;
37
38
    /** @var string|null */
39
    protected ?string $mimeType;
40
41
    /** @var string|null */
42
    protected ?string $type;
43
44
45
    /**
46
     * EncryptedData constructor.
47
     *
48
     * @param \SimpleSAML\XMLSecurity\XML\xenc\CipherData $cipherData The CipherData object of this EncryptedData.
49
     * @param string|null $id The Id attribute of this object. Optional.
50
     * @param string|null $type The Type attribute of this object. Optional.
51
     * @param string|null $mimeType The MimeType attribute of this object. Optional.
52
     * @param string|null $encoding The Encoding attribute of this object. Optional.
53
     * @param \SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod|null $encryptionMethod
54
     *   The EncryptionMethod object of this EncryptedData. Optional.
55
     * @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo The KeyInfo object of this EncryptedData. Optional.
56
     */
57
    protected function __construct(
58
        CipherData $cipherData,
59
        ?string $id = null,
60
        ?string $type = null,
61
        ?string $mimeType = null,
62
        ?string $encoding = null,
63
        ?EncryptionMethod $encryptionMethod = null,
64
        ?KeyInfo $keyInfo = null
65
    ) {
66
        $this->setCipherData($cipherData);
67
        $this->setEncoding($encoding);
68
        $this->setID($id);
69
        $this->setMimeType($mimeType);
70
        $this->setType($type);
71
        $this->setEncryptionMethod($encryptionMethod);
72
        $this->setKeyInfo($keyInfo);
73
    }
74
75
76
    /**
77
     * Get the CipherData object.
78
     *
79
     * @return \SimpleSAML\XMLSecurity\XML\xenc\CipherData
80
     */
81
    public function getCipherData(): CipherData
82
    {
83
        return $this->cipherData;
84
    }
85
86
87
    /**
88
     * @param \SimpleSAML\XMLSecurity\XML\xenc\CipherData $cipherData
89
     */
90
    protected function setCipherData(CipherData $cipherData): void
91
    {
92
        $this->cipherData = $cipherData;
93
    }
94
95
96
    /**
97
     * Get the value of the Encoding attribute.
98
     *
99
     * @return string|null
100
     */
101
    public function getEncoding(): ?string
102
    {
103
        return $this->encoding;
104
    }
105
106
107
    /**
108
     * @param string|null $encoding
109
     */
110
    protected function setEncoding(?string $encoding): void
111
    {
112
        Assert::nullOrNotEmpty($encoding, 'Encoding in <xenc:EncryptedData> cannot be empty.');
113
        $this->encoding = $encoding;
114
    }
115
116
117
    /**
118
     * Get the EncryptionMethod object.
119
     *
120
     * @return \SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod|null
121
     */
122
    public function getEncryptionMethod(): ?EncryptionMethod
123
    {
124
        return $this->encryptionMethod;
125
    }
126
127
128
    /**
129
     * @param \SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod|null $encryptionMethod
130
     */
131
    protected function setEncryptionMethod(?EncryptionMethod $encryptionMethod): void
132
    {
133
        $this->encryptionMethod = $encryptionMethod;
134
    }
135
136
137
    /**
138
     * Get the value of the Id attribute.
139
     *
140
     * @return string
141
     */
142
    public function getID(): ?string
143
    {
144
        return $this->id;
145
    }
146
147
148
    /**
149
     * @param string|null $id
150
     */
151
    protected function setID(?string $id): void
152
    {
153
        Assert::nullOrNotEmpty($id, 'Id in <xenc:EncryptedData> cannot be empty.');
154
        $this->id = $id;
155
    }
156
157
158
    /**
159
     * Get the KeyInfo object.
160
     *
161
     * @return \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null
162
     */
163
    public function getKeyInfo(): ?KeyInfo
164
    {
165
        return $this->keyInfo;
166
    }
167
168
169
    /**
170
     * @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo
171
     */
172
    protected function setKeyInfo(?KeyInfo $keyInfo): void
173
    {
174
        $this->keyInfo = $keyInfo;
175
    }
176
177
178
    /**
179
     * Get the value of the MimeType attribute.
180
     *
181
     * @return string
182
     */
183
    public function getMimeType(): ?string
184
    {
185
        return $this->mimeType;
186
    }
187
188
189
    /**
190
     * @param string|null $mimeType
191
     */
192
    protected function setMimeType(?string $mimeType): void
193
    {
194
        $this->mimeType = $mimeType;
195
    }
196
197
198
    /**
199
     * Get the value of the Type attribute.
200
     *
201
     * @return string|null
202
     */
203
    public function getType(): ?string
204
    {
205
        return $this->type;
206
    }
207
208
209
    /**
210
     * @param string|null $type
211
     */
212
    protected function setType(?string $type): void
213
    {
214
        Assert::nullOrNotEmpty($type, 'Type in <xenc:EncryptedData> cannot be empty.');
215
        $this->type = $type;
216
    }
217
218
219
    /**
220
     * @inheritDoc
221
     *
222
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
223
     *   If the qualified name of the supplied element is wrong
224
     * @throws \SimpleSAML\XML\Exception\MissingElementException
225
     *   If one of the mandatory child-elements is missing
226
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
227
     *   If too many child-elements of a type are specified
228
     */
229
    public static function fromXML(DOMElement $xml): object
230
    {
231
        Assert::same($xml->localName, 'EncryptedData', InvalidDOMElementException::class);
232
        Assert::same($xml->namespaceURI, EncryptedData::NS, InvalidDOMElementException::class);
233
234
        $cipherData = CipherData::getChildrenOfClass($xml);
235
        Assert::minCount(
236
            $cipherData,
237
            1,
238
            'At least one CipherData element found in <xenc:EncryptedData>.',
239
            MissingElementException::class
240
        );
241
        Assert::maxCount(
242
            $cipherData,
243
            1,
244
            'No or more than one CipherData element found in <xenc:EncryptedData>.',
245
            TooManyElementsException::class
246
        );
247
248
        $encryptionMethod = EncryptionMethod::getChildrenOfClass($xml);
249
        Assert::maxCount(
250
            $encryptionMethod,
251
            1,
252
            'No more than one EncryptionMethod element allowed in <xenc:EncryptedData>.',
253
            TooManyElementsException::class
254
        );
255
256
        $keyInfo = KeyInfo::getChildrenOfClass($xml);
257
        Assert::maxCount(
258
            $keyInfo,
259
            1,
260
            'No more than one KeyInfo element allowed in <xenc:EncryptedData>.',
261
            TooManyElementsException::class
262
        );
263
264
        return new static(
265
            $cipherData[0],
266
            self::getAttribute($xml, 'Id', null),
267
            self::getAttribute($xml, 'Type', null),
268
            self::getAttribute($xml, 'MimeType', null),
269
            self::getAttribute($xml, 'Encoding', null),
270
            count($encryptionMethod) === 1 ? $encryptionMethod[0] : null,
271
            count($keyInfo) === 1 ? $keyInfo[0] : null
272
        );
273
    }
274
275
276
    /**
277
     * @inheritDoc
278
     */
279
    public function toXML(DOMElement $parent = null): DOMElement
280
    {
281
        $e = $this->instantiateParentElement($parent);
282
283
        if ($this->id !== null) {
284
            $e->setAttribute('Id', $this->id);
285
        }
286
287
        if ($this->type !== null) {
288
            $e->setAttribute('Type', $this->type);
289
        }
290
291
        if ($this->mimeType !== null) {
292
            $e->setAttribute('MimeType', $this->mimeType);
293
        }
294
295
        if ($this->encoding !== null) {
296
            $e->setAttribute('Encoding', $this->encoding);
297
        }
298
299
        if ($this->encryptionMethod !== null) {
300
            $this->encryptionMethod->toXML($e);
301
        }
302
303
        if ($this->keyInfo !== null) {
304
            $this->keyInfo->toXML($e);
305
        }
306
307
        $this->cipherData->toXML($e);
308
309
        return $e;
310
    }
311
}
312