Passed
Pull Request — master (#374)
by Tim
02:29
created

EncryptedElementTrait::getEncryptedKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Compat\ContainerSingleton;
10
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
11
use SimpleSAML\XML\AbstractElement;
12
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
13
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
14
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
15
use SimpleSAML\XMLSecurity\Constants as C;
16
use SimpleSAML\XMLSecurity\XML\EncryptedElementTrait as ParentEncryptedElementTrait;
17
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedData;
18
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey;
19
20
/**
21
 * Trait aggregating functionality for elements that are encrypted.
22
 *
23
 * @package simplesamlphp/saml2
24
 */
25
trait EncryptedElementTrait
26
{
27
    use ParentEncryptedElementTrait;
28
29
30
    /**
31
     * Constructor for encrypted elements.
32
     *
33
     * @param \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData $encryptedData The EncryptedData object.
34
     * @param \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey[] $encryptedKey The EncryptedKey objects.
35
     */
36
    final public function __construct(
37
        protected EncryptedData $encryptedData,
38
        array $encryptedKey = [],
39
    ) {
40
        Assert::allIsInstanceOf($encryptedKey, EncryptedKey::class, ProtocolViolationException::class);
41
        $this->encryptedKey = $encryptedKey;
42
43
        /**
44
         * 6.2: The <EncryptedData> element's Type attribute SHOULD be used and, if it is
45
         * present, MUST have the value http://www.w3.org/2001/04/xmlenc#Element.
46
         */
47
        Assert::nullOrSame($encryptedData->getType()->getValue(), C::XMLENC_ELEMENT);
48
49
        $keyInfo = $this->encryptedData->getKeyInfo();
50
        if ($keyInfo === null) {
51
            return;
52
        }
53
54
        foreach ($keyInfo->getInfo() as $info) {
55
            if ($info instanceof EncryptedKey) {
56
                $this->encryptedKey = [$info];
57
                break;
58
            }
59
        }
60
    }
61
62
63
    public function getBlacklistedAlgorithms(): ?array
64
    {
65
        $container = ContainerSingleton::getInstance();
66
        return $container->getBlacklistedEncryptionAlgorithms();
67
    }
68
69
70
    public function getEncryptionBackend(): ?EncryptionBackend
71
    {
72
        // return the encryption backend you want to use,
73
        // or null if you are fine with the default
74
        return null;
75
    }
76
77
78
    public function getEncryptedKeys(): array
79
    {
80
        return $this->encryptedKey;
81
    }
82
83
84
    /**
85
     * @inheritDoc
86
     *
87
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
88
     *   If the qualified name of the supplied element is wrong
89
     */
90
    public static function fromXML(DOMElement $xml): static
91
    {
92
        Assert::same(
93
            $xml->localName,
94
            AbstractElement::getClassName(static::class),
95
            InvalidDOMElementException::class,
96
        );
97
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
0 ignored issues
show
Bug introduced by
The constant SimpleSAML\SAML2\XML\EncryptedElementTrait::NS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
98
99
        $ed = EncryptedData::getChildrenOfClass($xml);
100
        Assert::count(
101
            $ed,
102
            1,
103
            sprintf(
104
                'No more or less than one EncryptedData element allowed in %s.',
105
                AbstractElement::getClassName(static::class),
106
            ),
107
            TooManyElementsException::class,
108
        );
109
110
        $ek = EncryptedKey::getChildrenOfClass($xml);
111
        return new static($ed[0], $ek);
112
    }
113
114
115
    /**
116
     * @inheritDoc
117
     */
118
    public function toXML(?DOMElement $parent = null): DOMElement
119
    {
120
        $e = $this->instantiateParentElement($parent);
121
        $this->encryptedData->toXML($e);
122
        foreach ($this->getEncryptedKeys() as $key) {
123
            $key->toXML($e);
124
        }
125
        return $e;
126
    }
127
}
128