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
|
|
|
use SimpleSAML\XML\XMLStringElementTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class representing a ds:X509Digest element. |
16
|
|
|
* |
17
|
|
|
* @package simplesaml/xml-security |
18
|
|
|
*/ |
19
|
|
|
final class X509Digest extends AbstractDsElement |
20
|
|
|
{ |
21
|
|
|
use XMLStringElementTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The digest algorithm. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected string $algorithm; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Initialize a X509Digest element. |
33
|
|
|
* |
34
|
|
|
* @param string $digest |
35
|
|
|
* @param string $algorithm |
36
|
|
|
*/ |
37
|
|
|
public function __construct(string $digest, string $algorithm) |
38
|
|
|
{ |
39
|
|
|
$this->setContent($digest); |
40
|
|
|
$this->setAlgorithm($algorithm); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Collect the value of the algorithm-property |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getAlgorithm(): string |
50
|
|
|
{ |
51
|
|
|
return $this->algorithm; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set the value of the algorithm-property |
57
|
|
|
* |
58
|
|
|
* @param string $algorithm |
59
|
|
|
*/ |
60
|
|
|
private function setAlgorithm(string $algorithm): void |
61
|
|
|
{ |
62
|
|
|
Assert::oneOf( |
63
|
|
|
$algorithm, |
64
|
|
|
[ |
65
|
|
|
Constants::DIGEST_SHA1, |
66
|
|
|
Constants::DIGEST_SHA224, |
67
|
|
|
Constants::DIGEST_SHA256, |
68
|
|
|
Constants::DIGEST_SHA384, |
69
|
|
|
Constants::DIGEST_SHA512, |
70
|
|
|
Constants::DIGEST_RIPEMD160, |
71
|
|
|
], |
72
|
|
|
'Invalid digest method', |
73
|
|
|
InvalidArgumentException::class |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$this->algorithm = $algorithm; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Validate the content of the element. |
82
|
|
|
* |
83
|
|
|
* @param string $content The value to go in the XML textContent |
84
|
|
|
* @throws \Exception on failure |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
private function validateContent(string $content): void |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
Assert::notEmpty($content, 'X509Digest cannot be empty'); |
90
|
|
|
Assert::stringPlausibleBase64($content, 'ds:X509Digest is not a valid Base64 encoded string'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Convert XML into a X509Digest |
96
|
|
|
* |
97
|
|
|
* @param \DOMElement $xml The XML element we should load |
98
|
|
|
* @return self |
99
|
|
|
* |
100
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
101
|
|
|
* If the qualified name of the supplied element is wrong |
102
|
|
|
*/ |
103
|
|
|
public static function fromXML(DOMElement $xml): object |
104
|
|
|
{ |
105
|
|
|
Assert::same($xml->localName, 'X509Digest', InvalidDOMElementException::class); |
106
|
|
|
Assert::same($xml->namespaceURI, X509Digest::NS, InvalidDOMElementException::class); |
107
|
|
|
|
108
|
|
|
$algorithm = self::getAttribute($xml, 'Algorithm'); |
109
|
|
|
|
110
|
|
|
return new self($xml->textContent, $algorithm); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Convert this X509Digest element to XML. |
116
|
|
|
* |
117
|
|
|
* @param \DOMElement|null $parent The element we should append this X509Digest element to. |
118
|
|
|
* @return \DOMElement |
119
|
|
|
*/ |
120
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
121
|
|
|
{ |
122
|
|
|
$e = $this->instantiateParentElement($parent); |
123
|
|
|
$e->textContent = $this->content; |
124
|
|
|
$e->setAttribute('Algorithm', $this->algorithm); |
125
|
|
|
|
126
|
|
|
return $e; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.