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