Passed
Pull Request — master (#225)
by Jaime Pérez
02:23
created

EncryptedKey   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 49
c 3
b 0
f 0
dl 0
loc 169
rs 10
wmc 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setRecipient() 0 3 1
A getRecipient() 0 3 1
A setReferenceList() 0 3 1
A getReferenceList() 0 3 1
A setCarriedKeyName() 0 3 1
A getCarriedKeyName() 0 3 1
A fromXML() 0 35 5
A __construct() 0 16 1
A toXML() 0 19 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2\XML\xenc;
6
7
use DOMElement;
8
use SAML2\Utils;
9
use SAML2\XML\ds\KeyInfo;
10
use Webmozart\Assert\Assert;
11
12
/**
13
 * Class representing an encrypted key.
14
 *
15
 * @package simplesamlphp/saml2
16
 */
17
class EncryptedKey extends EncryptedData
18
{
19
    /** @var string */
20
    protected $carriedKeyName;
21
22
    /** @var string */
23
    protected $recipient;
24
25
    /** @var ReferenceList */
26
    protected $referenceList;
27
28
29
    /**
30
     * EncryptedKey constructor.
31
     *
32
     * @param CipherData $cipherData The CipherData object of this EncryptedData.
33
     * @param string|null $id The Id attribute of this object. Optional.
34
     * @param string|null $type The Type attribute of this object. Optional.
35
     * @param string|null $mimeType The MimeType attribute of this object. Optional.
36
     * @param string|null $encoding The Encoding attribute of this object. Optional.
37
     * @param string|null $recipient The Recipient attribute of this object. Optional.
38
     * @param string|null $carriedKeyName The value of the CarriedKeyName element of this EncryptedData.
39
     * @param EncryptionMethod|null $encryptionMethod The EncryptionMethod object of this EncryptedData. Optional.
40
     * @param KeyInfo|null $keyInfo The KeyInfo object of this EncryptedData. Optional.
41
     * @param ReferenceList|null $referenceList The ReferenceList object of this EncryptedData. Optional.
42
     */
43
    public function __construct(
44
        CipherData $cipherData,
45
        ?string $id = null,
46
        ?string $type = null,
47
        ?string $mimeType = null,
48
        ?string $encoding = null,
49
        ?string $recipient = null,
50
        ?string $carriedKeyName = null,
51
        ?EncryptionMethod $encryptionMethod = null,
52
        ?KeyInfo $keyInfo = null,
53
        ?ReferenceList $referenceList = null
54
    ) {
55
        parent::__construct($cipherData, $id, $type, $mimeType, $encoding, $encryptionMethod, $keyInfo);
56
        $this->setRecipient($recipient);
57
        $this->setReferenceList($referenceList);
58
        $this->setCarriedKeyName($carriedKeyName);
59
    }
60
61
62
    /**
63
     * Get the value of the CarriedKeyName element.
64
     *
65
     * @return string|null
66
     */
67
    public function getCarriedKeyName(): ?string
68
    {
69
        return $this->carriedKeyName;
70
    }
71
72
73
    /**
74
     * @param string|null $carriedKeyName
75
     */
76
    protected function setCarriedKeyName(?string $carriedKeyName): void
77
    {
78
        $this->carriedKeyName = $carriedKeyName;
79
    }
80
81
82
    /**
83
     * Get the value of the Recipient attribute.
84
     *
85
     * @return string|null
86
     */
87
    public function getRecipient(): ?string
88
    {
89
        return $this->recipient;
90
    }
91
92
93
    /**
94
     * @param string|null $recipient
95
     */
96
    protected function setRecipient(?string $recipient): void
97
    {
98
        $this->recipient = $recipient;
99
    }
100
101
102
    /**
103
     * Get the ReferenceList object.
104
     *
105
     * @return ReferenceList|null
106
     */
107
    public function getReferenceList(): ?ReferenceList
108
    {
109
        return $this->referenceList;
110
    }
111
112
113
    /**
114
     * @param ReferenceList|null $referenceList
115
     */
116
    protected function setReferenceList(?ReferenceList $referenceList): void
117
    {
118
        $this->referenceList = $referenceList;
119
    }
120
121
122
    /**
123
     * @inheritDoc
124
     */
125
    public static function fromXML(DOMElement $xml): object
126
    {
127
        Assert::same($xml->localName, 'EncryptedKey');
128
        Assert::same($xml->namespaceURI, EncryptedKey::NS);
129
130
        $cipherData = CipherData::getChildrenOfClass($xml);
131
        Assert::count($cipherData, 1, 'No or more than one CipherData element found in <xenc:EncryptedKey>.');
132
133
        $encryptionMethod = EncryptionMethod::getChildrenOfClass($xml);
134
        Assert::maxCount(
135
            $encryptionMethod,
136
            1,
137
            'No more than one EncryptionMethod element allowed in <xenc:EncryptedKey>.'
138
        );
139
140
        $keyInfo = KeyInfo::getChildrenOfClass($xml);
141
        Assert::maxCount($keyInfo, 1, 'No more than one KeyInfo element allowed in <xenc:EncryptedKey>.');
142
143
        $referenceLists = ReferenceList::getChildrenOfClass($xml);
144
        Assert::maxCount($keyInfo, 1, 'Only one ReferenceList element allowed in <xenc:EncryptedKey>.');
145
146
        $carriedKeyNames = Utils::xpQuery($xml, './xenc:CarriedKeyName');
147
        Assert::maxCount($carriedKeyNames, 1, 'Only one CarriedKeyName element allowed in <xenc:EncryptedKey>.');
148
149
        return new self(
150
            $cipherData[0],
151
            self::getAttribute($xml, 'Id', null),
152
            self::getAttribute($xml, 'Type', null),
153
            self::getAttribute($xml, 'MimeType', null),
154
            self::getAttribute($xml, 'Type', null),
155
            self::getAttribute($xml, 'Recipient', null),
156
            count($carriedKeyNames) === 1 ? $carriedKeyNames[0]->textContent : null,
157
            count($encryptionMethod) === 1 ? $encryptionMethod[0] : null,
158
            count($keyInfo) === 1 ? $keyInfo[0] : null,
159
            count($referenceLists) === 1 ? $referenceLists[0] : null
160
        );
161
    }
162
163
164
    /**
165
     * @inheritDoc
166
     */
167
    public function toXML(DOMElement $parent = null): DOMElement
168
    {
169
        $e = parent::toXML($parent);
170
171
        if ($this->referenceList !== null) {
172
            $this->referenceList->toXML($e);
173
        }
174
175
        if ($this->carriedKeyName !== null) {
176
            $ckn = $e->ownerDocument->createElementNS(self::NS, self::NS_PREFIX . ':CarriedKeyName');
177
            $ckn->textContent = $this->carriedKeyName;
178
            $e->appendChild($ckn);
179
        }
180
181
        if ($this->recipient !== null) {
182
            $e->setAttribute('Recipient', $this->recipient);
183
        }
184
185
        return $e;
186
    }
187
}
188