Passed
Pull Request — master (#3)
by Tim
02:21
created

X509Digest::setDigest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XMLSecurity\Constants;
11
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
12
13
/**
14
 * Class representing a ds:X509Digest element.
15
 *
16
 * @package simplesaml/xml-security
17
 */
18
final class X509Digest extends AbstractDsElement
19
{
20
    /**
21
     * The digest.
22
     *
23
     * @var string
24
     */
25
    protected string $digest;
26
27
28
    /**
29
     * The digest algorithm.
30
     *
31
     * @var string
32
     */
33
    protected string $algorithm;
34
35
36
    /**
37
     * Initialize a X509Digest element.
38
     *
39
     * @param string $digest
40
     * @param string $algorithm
41
     */
42
    public function __construct(string $digest, string $algorithm)
43
    {
44
        $this->setDigest($digest);
45
        $this->setAlgorithm($algorithm);
46
    }
47
48
49
    /**
50
     * Collect the value of the digest-property
51
     *
52
     * @return string
53
     */
54
    public function getDigest(): string
55
    {
56
        return $this->digest;
57
    }
58
59
60
    /**
61
     * Set the value of the digest-property
62
     *
63
     * @param string $digest
64
     */
65
    private function setDigest(string $digest): void
66
    {
67
        Assert::notEmpty($digest, 'X509Digest cannot be empty');
68
        $this->digest = $digest;
69
    }
70
71
72
    /**
73
     * Collect the value of the algorithm-property
74
     *
75
     * @return string
76
     */
77
    public function getAlgorithm(): string
78
    {
79
        return $this->algorithm;
80
    }
81
82
83
    /**
84
     * Set the value of the algorithm-property
85
     *
86
     * @param string $algorithm
87
     */
88
    private function setAlgorithm(string $algorithm): void
89
    {
90
        Assert::oneOf(
91
            $algorithm,
92
            [
93
                Constants::DIGEST_SHA1,
94
                Constants::DIGEST_SHA224,
95
                Constants::DIGEST_SHA256,
96
                Constants::DIGEST_SHA384,
97
                Constants::DIGEST_SHA512,
98
                Constants::DIGEST_RIPEMD160,
99
            ],
100
            'Invalid digest method',
101
            InvalidArgumentException::class
102
        );
103
104
        $this->algorithm = $algorithm;
105
    }
106
107
108
    /**
109
     * Convert XML into a X509Digest
110
     *
111
     * @param \DOMElement $xml The XML element we should load
112
     * @return self
113
     *
114
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
115
     *   If the qualified name of the supplied element is wrong
116
     */
117
    public static function fromXML(DOMElement $xml): object
118
    {
119
        Assert::same($xml->localName, 'X509Digest', InvalidDOMElementException::class);
120
        Assert::same($xml->namespaceURI, X509Digest::NS, InvalidDOMElementException::class);
121
122
        $digest = $xml->textContent;
123
        $algorithm = self::getAttribute($xml, 'Algorithm');
124
125
        Assert::stringNotEmpty($xml->textContent, 'Missing value digest.');
126
        /**
127
         * Note: This test is not watertight but prevents a string containing illegal characters
128
         * from being passed and ensures the string roughly follows the correct format for a Base64 encoded string
129
         */
130
        Assert::string(
131
            filter_var(
132
                $digest,
133
                FILTER_VALIDATE_REGEXP,
134
                [
135
                    'options' => [
136
                        'regexp' => '/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/'
137
                    ]
138
                ]
139
            ),
140
            'Digest is not a valid Base64 encoded string'
141
        );
142
        return new self($digest, $algorithm);
143
    }
144
145
146
    /**
147
     * Convert this X509Digest element to XML.
148
     *
149
     * @param \DOMElement|null $parent The element we should append this X509Digest element to.
150
     * @return \DOMElement
151
     */
152
    public function toXML(DOMElement $parent = null): DOMElement
153
    {
154
        $e = $this->instantiateParentElement($parent);
155
156
        $e->textContent = $this->digest;
157
        $e->setAttribute('Algorithm', $this->algorithm);
158
159
        return $e;
160
    }
161
}
162