Skip to content

Commit 198ec0d

Browse files
committed
Add toUnsignedXML to the SignableElementTrait
1 parent 5179e8f commit 198ec0d

File tree

2 files changed

+23
-117
lines changed

2 files changed

+23
-117
lines changed

src/XML/SignableElementInterface.php

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SimpleSAML\SAML2\XML;
44

5+
use SimpleSAML\XMLSecurity\SignedElementInterface;
56
use SimpleSAML\XMLSecurity\XMLSecurityKey;
67

78
/**
@@ -12,38 +13,11 @@
1213
interface SignableElementInterface
1314
{
1415
/**
15-
* Retrieve the certificates that are included in the message.
16+
* Sign the 'Element' and return a 'SignedElement'
1617
*
17-
* @return string[] An array of certificates
18+
* @param \SimpleSAML\XMLSecurity\XMLSecurityKey $signingKey The private key we should use to sign the message
19+
* @param string[] $certificates The certificates should be strings with the PEM encoded data
20+
* @return \SimpleSAML\XMLSecurity\SignedElementInterface
1821
*/
19-
public function getCertificates(): array;
20-
21-
22-
/**
23-
* Set the certificates that should be included in the element.
24-
* The certificates should be strings with the PEM encoded data.
25-
*
26-
* @param string[] $certificates An array of certificates.
27-
*/
28-
public function setCertificates(array $certificates): void;
29-
30-
31-
/**
32-
* Get the private key we should use to sign the message.
33-
*
34-
* If the key is null, the message will be sent unsigned.
35-
*
36-
* @return \SimpleSAML\XMLSecurity\XMLSecurityKey|null
37-
*/
38-
public function getSigningKey(): ?XMLSecurityKey;
39-
40-
41-
/**
42-
* Set the private key we should use to sign the message.
43-
*
44-
* If the key is null, the message will be sent unsigned.
45-
*
46-
* @param \SimpleSAML\XMLSecurity\XMLSecurityKey|null $signingKey
47-
*/
48-
public function setSigningKey(XMLSecurityKey $signingKey = null): void;
22+
public function sign(XMLSecurityKey $signingKey, array $certificates): SignedElementInterface;
4923
}

src/XML/SignableElementTrait.php

Lines changed: 17 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
namespace SimpleSAML\XMLSecurity\XML;
66

7-
//use DOMElement;
8-
//use DOMNode;
9-
//use Exception;
7+
use DOMElement;
8+
use DOMNode;
109
use SimpleSAML\Assert\Assert;
11-
//use SimpleSAML\XMLSecurity\Utils\Security as XMLSecurityUtils;
10+
use SimpleSAML\XMLSecurity\Utils\Security as XMLSecurityUtils;
1211
use SimpleSAML\XMLSecurity\XML\ds\Signature;
1312
use SimpleSAML\XMLSecurity\XMLSecurityKey;
1413
use SimpleSAML\XML\Utils as XMLUtils;
@@ -20,94 +19,27 @@
2019
*/
2120
trait SignableElementTrait
2221
{
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-
9122
/**
9223
* Sign the given XML element.
9324
*
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.
9528
* @return \DOMElement The signed element.
9629
* @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
9832
{
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);
10941
}
42+
11043
return $root;
11144
}
112-
*/
11345
}

0 commit comments

Comments
 (0)