|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\Test\XML\ds; |
|
6
|
|
|
|
|
7
|
|
|
use DOMElement; |
|
8
|
|
|
use PHPUnit\Framework\Attributes\CoversClass; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use SimpleSAML\XML\DOMDocumentFactory; |
|
11
|
|
|
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait; |
|
12
|
|
|
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait; |
|
13
|
|
|
use SimpleSAML\XMLSecurity\Constants as C; |
|
14
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement; |
|
15
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethod; |
|
16
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\Reference; |
|
17
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\SignatureMethod; |
|
18
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\SignedInfo; |
|
19
|
|
|
|
|
20
|
|
|
use function dirname; |
|
21
|
|
|
use function strval; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class \SimpleSAML\XMLSecurity\Test\XML\ds\SignedInfoTest |
|
25
|
|
|
* |
|
26
|
|
|
* @package simplesamlphp/xml-security |
|
27
|
|
|
*/ |
|
28
|
|
|
#[CoversClass(AbstractDsElement::class)] |
|
29
|
|
|
#[CoversClass(SignedInfo::class)] |
|
30
|
|
|
final class SignedInfoTest extends TestCase |
|
31
|
|
|
{ |
|
32
|
|
|
use SchemaValidationTestTrait; |
|
33
|
|
|
use SerializableElementTestTrait; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function setUpBeforeClass(): void |
|
38
|
|
|
{ |
|
39
|
|
|
self::$testedClass = SignedInfo::class; |
|
40
|
|
|
|
|
41
|
|
|
self::$xmlRepresentation = DOMDocumentFactory::fromFile( |
|
42
|
|
|
dirname(__FILE__, 3) . '/resources/xml/ds_SignedInfo.xml', |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
*/ |
|
49
|
|
|
public function testMarshalling(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$signedInfo = new SignedInfo( |
|
52
|
|
|
new CanonicalizationMethod(C::C14N_EXCLUSIVE_WITHOUT_COMMENTS), |
|
53
|
|
|
new SignatureMethod(C::SIG_RSA_SHA256), |
|
54
|
|
|
[ |
|
55
|
|
|
Reference::fromXML( |
|
56
|
|
|
DOMDocumentFactory::fromFile( |
|
57
|
|
|
dirname(__FILE__, 3) . '/resources/xml/ds_Reference.xml', |
|
58
|
|
|
)->documentElement, |
|
59
|
|
|
), |
|
60
|
|
|
], |
|
61
|
|
|
'cba321', |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertEquals( |
|
65
|
|
|
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), |
|
66
|
|
|
strval($signedInfo), |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* |
|
73
|
|
|
*/ |
|
74
|
|
|
private function canonicalization(DOMElement $xml, SignedInfo $signedInfo): void |
|
75
|
|
|
{ |
|
76
|
|
|
$this->assertEquals( |
|
77
|
|
|
$xml->C14N(true, false), |
|
78
|
|
|
$signedInfo->canonicalize(C::C14N_EXCLUSIVE_WITHOUT_COMMENTS), |
|
79
|
|
|
); |
|
80
|
|
|
$this->assertEquals( |
|
81
|
|
|
$xml->C14N(false, false), |
|
82
|
|
|
$signedInfo->canonicalize(C::C14N_INCLUSIVE_WITHOUT_COMMENTS), |
|
83
|
|
|
); |
|
84
|
|
|
$this->assertEquals( |
|
85
|
|
|
$xml->C14N(true, true), |
|
86
|
|
|
$signedInfo->canonicalize(C::C14N_EXCLUSIVE_WITH_COMMENTS), |
|
87
|
|
|
); |
|
88
|
|
|
$this->assertEquals( |
|
89
|
|
|
$xml->C14N(false, true), |
|
90
|
|
|
$signedInfo->canonicalize(C::C14N_INCLUSIVE_WITH_COMMENTS), |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Test that canonicalization works fine. |
|
97
|
|
|
*/ |
|
98
|
|
|
public function testCanonicalization(): void |
|
99
|
|
|
{ |
|
100
|
|
|
$xml = DOMDocumentFactory::fromFile( |
|
101
|
|
|
dirname(__FILE__, 3) . '/resources/xml/ds_SignedInfoWithComments.xml', |
|
102
|
|
|
)->documentElement; |
|
103
|
|
|
$signedInfo = SignedInfo::fromXML($xml); |
|
104
|
|
|
$this->canonicalization($xml, $signedInfo); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Test that canonicalization works fine even after serializing and unserializing |
|
110
|
|
|
*/ |
|
111
|
|
|
public function testCanonicalizationAfterSerialization(): void |
|
112
|
|
|
{ |
|
113
|
|
|
$xml = DOMDocumentFactory::fromFile( |
|
114
|
|
|
dirname(__FILE__, 3) . '/resources/xml/ds_SignedInfoWithComments.xml', |
|
115
|
|
|
)->documentElement; |
|
116
|
|
|
$signedInfo = unserialize(serialize(SignedInfo::fromXML($xml))); |
|
117
|
|
|
$this->canonicalization($xml, $signedInfo); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|