|
4 | 4 |
|
5 | 5 | namespace SimpleSAML\XMLSecurity\XML; |
6 | 6 |
|
7 | | -//use DOMElement; |
8 | | -//use DOMNode; |
9 | | -//use Exception; |
| 7 | +use DOMElement; |
| 8 | +use DOMNode; |
10 | 9 | use SimpleSAML\Assert\Assert; |
11 | | -//use SimpleSAML\XMLSecurity\Utils\Security as XMLSecurityUtils; |
| 10 | +use SimpleSAML\XMLSecurity\Utils\Security as XMLSecurityUtils; |
12 | 11 | use SimpleSAML\XMLSecurity\XML\ds\Signature; |
13 | 12 | use SimpleSAML\XMLSecurity\XMLSecurityKey; |
14 | 13 | use SimpleSAML\XML\Utils as XMLUtils; |
|
20 | 19 | */ |
21 | 20 | trait SignableElementTrait |
22 | 21 | { |
23 | | - /** |
24 | | - * List of certificates that should be included in the message. |
25 | | - * |
26 | | - * @var string[] |
27 | | - */ |
28 | | - protected array $certificates = []; |
29 | | - |
30 | | - /** |
31 | | - * The private key we should use to sign an unsigned message. |
32 | | - * |
33 | | - * The private key can be null, in which case we can only validate an already signed message. |
34 | | - * |
35 | | - * @var \SimpleSAML\XMLSecurity\XMLSecurityKey|null |
36 | | - */ |
37 | | - protected ?XMLSecurityKey $signingKey = null; |
38 | | - |
39 | | - |
40 | | - /** |
41 | | - * Retrieve the certificates that are included in the message. |
42 | | - * |
43 | | - * @return string[] An array of certificates |
44 | | - */ |
45 | | - public function getCertificates(): array |
46 | | - { |
47 | | - return $this->certificates; |
48 | | - } |
49 | | - |
50 | | - |
51 | | - /** |
52 | | - * Set the certificates that should be included in the element. |
53 | | - * The certificates should be strings with the PEM encoded data. |
54 | | - * |
55 | | - * @param string[] $certificates An array of certificates. |
56 | | - */ |
57 | | - public function setCertificates(array $certificates): void |
58 | | - { |
59 | | - Assert::allStringNotEmpty($certificates); |
60 | | - |
61 | | - $this->certificates = $certificates; |
62 | | - } |
63 | | - |
64 | | - |
65 | | - /** |
66 | | - * Get the private key we should use to sign the message. |
67 | | - * |
68 | | - * If the key is null, the message will be sent unsigned. |
69 | | - * |
70 | | - * @return \SimpleSAML\XMLSecurity\XMLSecurityKey|null |
71 | | - */ |
72 | | - public function getSigningKey(): ?XMLSecurityKey |
73 | | - { |
74 | | - return $this->signingKey; |
75 | | - } |
76 | | - |
77 | | - |
78 | | - /** |
79 | | - * Set the private key we should use to sign the message. |
80 | | - * |
81 | | - * If the key is null, the message will be sent unsigned. |
82 | | - * |
83 | | - * @param \SimpleSAML\XMLSecurity\XMLSecurityKey|null $signingKey |
84 | | - */ |
85 | | - public function setSigningKey(XMLSecurityKey $signingKey = null): void |
86 | | - { |
87 | | - $this->signingKey = $signingKey; |
88 | | - } |
89 | | - |
90 | | - |
91 | 22 | /** |
92 | 23 | * Sign the given XML element. |
93 | 24 | * |
94 | | - * @param \DOMElement $root The element we should sign. |
| 25 | + * @param \SimpleSAML\XMLSecurity\XMLSecurityKey $signKey The private key used for signing. |
| 26 | + * @param array $certificates Any public key to be added to the ds:Signature |
| 27 | + * @param \DOMNode|null $insertBefore A specific node in the DOM structure where the ds:Signature should be put in front. |
95 | 28 | * @return \DOMElement The signed element. |
96 | 29 | * @throws \Exception If an error occurs while trying to sign. |
97 | | - protected function signElement(DOMElement $root, DOMNode $insertBefore = null): DOMElement |
| 30 | + */ |
| 31 | + private function toSignedXML(XMLSecurityKey $signKey, array $certificates, DOMNode $insertBefore = null): DOMElement |
98 | 32 | { |
99 | | - if ($this->signingKey instanceof XMLSecurityKey) { |
100 | | - if ($insertBefore !== null) { |
101 | | - XMLSecurityUtils::insertSignature($this->signingKey, $this->certificates, $root, $insertBefore); |
102 | | -
|
103 | | - $doc = clone $root->ownerDocument; |
104 | | - $this->signature = Signature::fromXML(XMLUtils::xpQuery($doc->documentElement, './ds:Signature')[0]); |
105 | | - } else { |
106 | | - $this->signature = new Signature($this->signingKey->getAlgorithm(), $this->certificates, $this->signingKey); |
107 | | - $this->signature->toXML($root); |
108 | | - } |
| 33 | + $root = $this->toXML(); |
| 34 | + |
| 35 | + if ($insertBefore !== null) { |
| 36 | + XMLSecurityUtils::insertSignature($this->signingKey, $this->certificates, $root, $insertBefore); |
| 37 | + $doc = clone $root->ownerDocument; |
| 38 | + } else { |
| 39 | + $signature = new Signature($this->signingKey->getAlgorithm(), $this->certificates, $this->signingKey); |
| 40 | + $signature->toXML($root); |
109 | 41 | } |
| 42 | + |
110 | 43 | return $root; |
111 | 44 | } |
112 | | - */ |
113 | 45 | } |
0 commit comments