Issues (81)

src/XML/xenc/EncryptedData.php (1 issue)

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