Passed
Pull Request — master (#17)
by Jaime Pérez
01:57
created

Signature::setCertificates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A Signature::setSignatureValue() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use Exception;
9
use SimpleSAML\Assert\Assert;
10
use SimpleSAML\XML\Chunk;
11
use SimpleSAML\XML\Constants;
12
use SimpleSAML\XML\Exception\InvalidDOMElementException;
13
use SimpleSAML\XML\Utils as XMLUtils;
14
15
/**
16
 * Class representing a ds:Signature element.
17
 *
18
 * @package simplesamlphp/xml-security
19
 */
20
final class Signature extends AbstractDsElement
21
{
22
    /** @var \SimpleSAML\XMLSecurity\XML\ds\SignedInfo */
23
    protected SignedInfo $signedInfo;
24
25
    /** @var \SimpleSAML\XMLSecurity\XML\ds\SignatureValue */
26
    protected SignatureValue $signatureValue;
27
28
    /** @var \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo */
29
    protected ?KeyInfo $keyInfo;
30
31
    /** @var \SimpleSAML\XML\Chunk[] */
32
    protected array $objects;
33
34
    /** @var string|null */
35
    protected ?string $Id;
36
37
38
    /**
39
     * Signature constructor.
40
     *
41
     * @param \SimpleSAML\XMLSecurity\XML\ds\SignedInfo $signedInfo
42
     * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureValue $signatureValue
43
     * @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo
44
     * @param \SimpleSAML\XML\Chunk[] $objects
45
     * @param string|null $Id
46
     */
47
    public function __construct(
48
        SignedInfo $signedInfo,
49
        SignatureValue $signatureValue,
50
        ?KeyInfo $keyInfo,
51
        array $objects = [],
52
        ?string $Id = null
53
    ) {
54
        $this->setSignedInfo($signedInfo);
55
        $this->setSignatureValue($signatureValue);
56
        $this->setKeyInfo($keyInfo);
57
        $this->setObjects($objects);
58
        $this->setId($Id);
59
    }
60
61
62
    /**
63
     * Get the Id used for this signature.
64
     *
65
     * @return string|null
66
     */
67
    public function getId(): ?string
68
    {
69
        return $this->Id;
70
    }
71
72
73
    /**
74
     * Set the Id used for this signature.
75
     *
76
     * @param string|null $Id
77
     */
78
    protected function setId(?string $Id): void
79
    {
80
        $this->Id = $Id;
81
    }
82
83
84
    /**
85
     * @param \SimpleSAML\XMLSecurity\XML\ds\SignedInfo
86
     */
87
    protected function setSignedInfo(SignedInfo $signedInfo): void
88
    {
89
        $this->signedInfo = $signedInfo;
90
    }
91
92
93
    /**
94
     * @return \SimpleSAML\XMLSecurity\XML\ds\SignedInfo
95
     */
96
    public function getSignedInfo(): SignedInfo
97
    {
98
        return $this->signedInfo;
99
    }
100
101
102
    /**
103
     * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureValue
104
     */
105
    protected function setSignatureValue(SignatureValue $signatureValue): void
106
    {
107
        $this->signatureValue = $signatureValue;
108
    }
109
110
111
    /**
112
     * @return \SimpleSAML\XMLSecurity\XML\ds\SignatureValue
113
     */
114
    public function getSignatureValue(): SignatureValue
115
    {
116
        return $this->signatureValue;
117
    }
118
119
120
    /**
121
     * @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null
122
     */
123
    protected function setKeyInfo(?KeyInfo $keyInfo): void
124
    {
125
        $this->keyInfo = $keyInfo;
126
    }
127
128
129
    /**
130
     * @return \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null
131
     */
132
    public function getKeyInfo(): ?KeyInfo
133
    {
134
        return $this->keyInfo;
135
    }
136
137
138
    /**
139
     * Get the array of ds:Object elements attached to this signature.
140
     *
141
     * @return \SimpleSAML\XML\Chunk[]
142
     */
143
    public function getObjects(): array
144
    {
145
        return $this->objects;
146
    }
147
148
149
    /**
150
     * Set the array of ds:Object elements attached to this signature.
151
     *
152
     * @param \SimpleSAML\XML\Chunk[] $objects
153
     */
154
    protected function setObjects(array $objects): void
155
    {
156
        Assert::allIsInstanceOf($objects, Chunk::class);
157
158
        foreach ($objects as $o) {
159
            Assert::true(
160
                $o->getNamespaceURI() === Constants::NS_XDSIG
161
                && $o->getLocalName() === 'Object',
162
                'Only elements of type ds:Object are allowed.'
163
            );
164
        }
165
166
        $this->objects = $objects;
167
    }
168
169
170
    /**
171
     * Convert XML into a Signature element
172
     *
173
     * @param \DOMElement $xml
174
     * @return \SimpleSAML\XML\AbstractXMLElement
175
     *
176
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
177
     *   If the qualified name of the supplied element is wrong
178
     */
179
    public static function fromXML(DOMElement $xml): object
180
    {
181
        Assert::same($xml->localName, 'Signature', InvalidDOMElementException::class);
182
        Assert::same($xml->namespaceURI, Signature::NS, InvalidDOMElementException::class);
183
184
        $Id = self::getAttribute($xml, 'Id', null);
185
186
        $signedInfo = SignedInfo::getChildrenOfClass($xml);
187
        Assert::count($signedInfo, 1, 'ds:Signature needs exactly one ds:SignedInfo element.');
188
189
        $signatureValue = SignatureValue::getChildrenOfClass($xml);
190
        Assert::count($signatureValue, 1, 'ds:Signature needs exactly one ds:SignatureValue element.');
191
192
        $keyInfo = KeyInfo::getChildrenOfClass($xml);
193
        Assert::maxCount($keyInfo, 1, 'ds:Signature can hold a maximum of one ds:KeyInfo element.');
194
195
        $objects = [];
196
        foreach ($xml->childNodes as $o) {
197
            if (
198
                $o instanceof DOMElement
199
                && $o->namespaceURI === Constants::NS_XDSIG
200
                && $o->localName === 'Object'
201
            ) {
202
                $objects[] = Chunk::fromXML($o);
203
            }
204
        }
205
206
        return new self(
207
            array_pop($signedInfo),
208
            array_pop($signatureValue),
209
            empty($keyInfo) ? null : array_pop($keyInfo),
210
            $objects,
211
            $Id
212
        );
213
    }
214
215
216
    /**
217
     * Convert this Signature element to XML.
218
     *
219
     * @param \DOMElement|null $parent The element we should append this Signature element to.
220
     * @return \DOMElement
221
     */
222
    public function toXML(DOMElement $parent = null): DOMElement
223
    {
224
        $e = $this->instantiateParentElement($parent);
225
226
        if ($this->Id !== null) {
227
            $e->setAttribute('Id', $this->Id);
228
        }
229
230
        $this->signedInfo->toXML($e);
231
        $this->signatureValue->toXML($e);
232
233
        if ($this->keyInfo !== null) {
234
            $this->keyInfo->toXML($e);
235
        }
236
237
        foreach ($this->objects as $o) {
238
            $o->toXML($e);
239
        }
240
241
        return $e;
242
    }
243
}
244