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