Skip to content

Commit 5b9324f

Browse files
committed
Fix xenc:KeySize
1 parent c6f3423 commit 5b9324f

File tree

3 files changed

+56
-17
lines changed

3 files changed

+56
-17
lines changed

src/XML/xenc/KeySize.php

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
namespace SimpleSAML\XMLSecurity\XML\xenc;
66

7+
use DOMElement;
78
use SimpleSAML\Assert\Assert;
9+
use SimpleSAML\XML\Exception\InvalidDOMElementException;
810
use SimpleSAML\XML\Exception\SchemaViolationException;
9-
use SimpleSAML\XML\StringElementTrait;
1011

1112
/**
1213
* Class representing a xenc:KeySize element.
@@ -16,30 +17,68 @@
1617
*/
1718
final class KeySize extends AbstractXencElement
1819
{
19-
use StringElementTrait;
20+
/** @var int */
21+
protected int $keySize;
2022

2123

2224
/**
23-
* @param string $content
25+
* @param int $keySize
2426
*/
25-
public function __construct(string $content)
27+
public function __construct(int $keySize)
2628
{
27-
$this->setContent($content);
29+
$this->setKeySize($keySize);
2830
}
2931

3032

3133
/**
32-
* Validate the content of the element.
34+
* @param int $keySize
35+
*/
36+
protected function setKeySize(int $keySize): void
37+
{
38+
Assert::positiveInteger($keySize, SchemaViolationException::class);
39+
$this->keySize = $keySize;
40+
}
41+
42+
43+
/**
44+
* @return int
45+
*/
46+
public function getKeySize(): int
47+
{
48+
return $this->keySize;
49+
}
50+
51+
52+
/**
53+
* Convert XML into a class instance
54+
*
55+
* @param \DOMElement $xml The XML element we should load
56+
* @return static
3357
*
34-
* @param string $content The value to go in the XML textContent
35-
* @throws \Exception on failure
36-
* @return void
58+
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
59+
* If the qualified name of the supplied element is wrong
3760
*/
38-
protected function validateContent(string $content): void
61+
public static function fromXML(DOMElement $xml): static
3962
{
40-
Assert::positiveInteger(
41-
intval($content),
42-
SchemaViolationException::class
43-
);
63+
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
64+
Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
65+
Assert::numeric($xml->textContent);
66+
67+
return new static(intval($xml->textContent));
68+
}
69+
70+
71+
/**
72+
* Convert this element to XML.
73+
*
74+
* @param \DOMElement|null $parent The element we should append this element to.
75+
* @return \DOMElement
76+
*/
77+
public function toXML(DOMElement $parent = null): DOMElement
78+
{
79+
$e = $this->instantiateParentElement($parent);
80+
$e->textContent = strval($this->getKeySize());
81+
82+
return $e;
4483
}
4584
}

tests/XML/xenc/EncryptionMethodTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testMarshalling(): void
5454
$chunkXml = DOMDocumentFactory::fromString('<other:Element xmlns:other="urn:other:enc">Value</other:Element>');
5555
$chunk = Chunk::fromXML($chunkXml->documentElement);
5656

57-
$em = new EncryptionMethod($alg, new KeySize('10'), new OAEPparams('9lWu3Q=='), [$chunk]);
57+
$em = new EncryptionMethod($alg, new KeySize(10), new OAEPparams('9lWu3Q=='), [$chunk]);
5858

5959
$this->assertEquals(
6060
$this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement),
@@ -90,7 +90,7 @@ public function testMarshallingElementOrdering(): void
9090
$chunkXml = DOMDocumentFactory::fromString('<other:Element xmlns:other="urn:other:enc">Value</other:Element>');
9191
$chunk = Chunk::fromXML($chunkXml->documentElement);
9292

93-
$em = new EncryptionMethod($alg, new KeySize('10'), new OAEPparams('9lWu3Q=='), [$chunk]);
93+
$em = new EncryptionMethod($alg, new KeySize(10), new OAEPparams('9lWu3Q=='), [$chunk]);
9494

9595
// Marshall it to a \DOMElement
9696
$emElement = $em->toXML();

tests/XML/xenc/KeySizeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected function setUp(): void
4040
*/
4141
public function testMarshalling(): void
4242
{
43-
$keySize = new KeySize('10');
43+
$keySize = new KeySize(10);
4444

4545
$this->assertEquals(
4646
$this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement),

0 commit comments

Comments
 (0)