1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\Test\XML\xenc; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\Attributes\CoversClass; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use SimpleSAML\XML\Chunk; |
10
|
|
|
use SimpleSAML\XML\DOMDocumentFactory; |
11
|
|
|
use SimpleSAML\XML\TestUtils\{SerializableElementTestTrait, SchemaValidationTestTrait}; |
12
|
|
|
use SimpleSAML\XMLSecurity\Constants as C; |
13
|
|
|
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock; |
14
|
|
|
use SimpleSAML\XMLSecurity\Utils\XPath; |
15
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\{DigestMethod, KeyName, X509Certificate, X509Data, X509SubjectName}; |
16
|
|
|
use SimpleSAML\XMLSecurity\XML\xenc\{AbstractAgreementMethodType, AbstractXencElement}; |
17
|
|
|
use SimpleSAML\XMLSecurity\XML\xenc\{AgreementMethod, KANonce, OriginatorKeyInfo, RecipientKeyInfo}; |
18
|
|
|
|
19
|
|
|
use function dirname; |
20
|
|
|
use function openssl_x509_parse; |
21
|
|
|
use function str_replace; |
22
|
|
|
use function strval; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class \SimpleSAML\XMLSecurity\Test\XML\xenc\AgreementMethodTest |
26
|
|
|
* |
27
|
|
|
* @package simplesamlphp/xml-security |
28
|
|
|
*/ |
29
|
|
|
#[CoversClass(AbstractXencElement::class)] |
30
|
|
|
#[CoversClass(AbstractAgreementMethodType::class)] |
31
|
|
|
#[CoversClass(AgreementMethod::class)] |
32
|
|
|
final class AgreementMethodTest extends TestCase |
33
|
|
|
{ |
34
|
|
|
use SchemaValidationTestTrait; |
35
|
|
|
use SerializableElementTestTrait; |
36
|
|
|
|
37
|
|
|
/** @var string */ |
38
|
|
|
private static string $certificate; |
39
|
|
|
|
40
|
|
|
/** @var string[] */ |
41
|
|
|
private static array $certData; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
*/ |
46
|
|
|
public function setUp(): void |
47
|
|
|
{ |
48
|
|
|
self::$testedClass = AgreementMethod::class; |
49
|
|
|
|
50
|
|
|
self::$xmlRepresentation = DOMDocumentFactory::fromFile( |
51
|
|
|
dirname(__FILE__, 3) . '/resources/xml/xenc_AgreementMethod.xml', |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
self::$certificate = str_replace( |
55
|
|
|
[ |
56
|
|
|
'-----BEGIN CERTIFICATE-----', |
57
|
|
|
'-----END CERTIFICATE-----', |
58
|
|
|
'-----BEGIN RSA PUBLIC KEY-----', |
59
|
|
|
'-----END RSA PUBLIC KEY-----', |
60
|
|
|
"\r\n", |
61
|
|
|
"\n", |
62
|
|
|
], |
63
|
|
|
[ |
64
|
|
|
'', |
65
|
|
|
'', |
66
|
|
|
'', |
67
|
|
|
'', |
68
|
|
|
"\n", |
69
|
|
|
'', |
70
|
|
|
], |
71
|
|
|
PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE), |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
self::$certData = openssl_x509_parse( |
75
|
|
|
PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE), |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
*/ |
82
|
|
|
public function testMarshalling(): void |
83
|
|
|
{ |
84
|
|
|
$kaNonce = new KANonce('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI='); |
85
|
|
|
|
86
|
|
|
$digestMethod = new DigestMethod( |
87
|
|
|
C::DIGEST_SHA256, |
88
|
|
|
[ |
89
|
|
|
new Chunk(DOMDocumentFactory::fromString( |
90
|
|
|
'<some:Chunk xmlns:some="urn:x-simplesamlphp:namespace">some</some:Chunk>', |
91
|
|
|
)->documentElement), |
92
|
|
|
], |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$originatorKeyInfo = new OriginatorKeyInfo( |
96
|
|
|
[ |
97
|
|
|
new KeyName('testkey'), |
98
|
|
|
new X509Data( |
99
|
|
|
[ |
100
|
|
|
new X509Certificate(self::$certificate), |
101
|
|
|
new X509SubjectName(self::$certData['name']), |
102
|
|
|
], |
103
|
|
|
), |
104
|
|
|
new Chunk(DOMDocumentFactory::fromString( |
105
|
|
|
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">originator</ssp:Chunk>', |
106
|
|
|
)->documentElement), |
107
|
|
|
], |
108
|
|
|
'fed123', |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$recipientKeyInfo = new RecipientKeyInfo( |
112
|
|
|
[ |
113
|
|
|
new KeyName('testkey'), |
114
|
|
|
new X509Data( |
115
|
|
|
[ |
116
|
|
|
new X509Certificate(self::$certificate), |
117
|
|
|
new X509SubjectName(self::$certData['name']), |
118
|
|
|
], |
119
|
|
|
), |
120
|
|
|
new Chunk(DOMDocumentFactory::fromString( |
121
|
|
|
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">recipient</ssp:Chunk>', |
122
|
|
|
)->documentElement), |
123
|
|
|
], |
124
|
|
|
'fed654', |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$agreementMethod = new AgreementMethod( |
128
|
|
|
C::XMLENC11_ECDH_ES, |
129
|
|
|
$kaNonce, |
130
|
|
|
$originatorKeyInfo, |
131
|
|
|
$recipientKeyInfo, |
132
|
|
|
[$digestMethod], |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
$this->assertEquals( |
136
|
|
|
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), |
137
|
|
|
strval($agreementMethod), |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
public function testMarshallingElementOrdering(): void |
143
|
|
|
{ |
144
|
|
|
$kaNonce = new KANonce('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI='); |
145
|
|
|
|
146
|
|
|
$digestMethod = new DigestMethod( |
147
|
|
|
C::DIGEST_SHA256, |
148
|
|
|
[ |
149
|
|
|
new Chunk(DOMDocumentFactory::fromString( |
150
|
|
|
'<some:Chunk xmlns:some="urn:x-simplesamlphp:namespace">some</some:Chunk>', |
151
|
|
|
)->documentElement), |
152
|
|
|
], |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
$originatorKeyInfo = new OriginatorKeyInfo( |
156
|
|
|
[ |
157
|
|
|
new KeyName('testkey'), |
158
|
|
|
new X509Data( |
159
|
|
|
[ |
160
|
|
|
new X509Certificate(self::$certificate), |
161
|
|
|
new X509SubjectName(self::$certData['name']), |
162
|
|
|
], |
163
|
|
|
), |
164
|
|
|
new Chunk(DOMDocumentFactory::fromString( |
165
|
|
|
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">originator</ssp:Chunk>', |
166
|
|
|
)->documentElement), |
167
|
|
|
], |
168
|
|
|
'fed321', |
169
|
|
|
); |
170
|
|
|
|
171
|
|
|
$recipientKeyInfo = new RecipientKeyInfo( |
172
|
|
|
[ |
173
|
|
|
new KeyName('testkey'), |
174
|
|
|
new X509Data( |
175
|
|
|
[ |
176
|
|
|
new X509Certificate(self::$certificate), |
177
|
|
|
new X509SubjectName(self::$certData['name']), |
178
|
|
|
], |
179
|
|
|
), |
180
|
|
|
new Chunk(DOMDocumentFactory::fromString( |
181
|
|
|
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">recipient</ssp:Chunk>', |
182
|
|
|
)->documentElement), |
183
|
|
|
], |
184
|
|
|
'fed654', |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
$agreementMethod = new AgreementMethod( |
188
|
|
|
C::XMLENC11_ECDH_ES, |
189
|
|
|
$kaNonce, |
190
|
|
|
$originatorKeyInfo, |
191
|
|
|
$recipientKeyInfo, |
192
|
|
|
[$digestMethod], |
193
|
|
|
); |
194
|
|
|
|
195
|
|
|
// Marshall it to a \DOMElement |
196
|
|
|
$agreementMethodElement = $agreementMethod->toXML(); |
197
|
|
|
|
198
|
|
|
$xpCache = XPath::getXPath($agreementMethodElement); |
199
|
|
|
|
200
|
|
|
// Test for an KA-Nonce |
201
|
|
|
/** @var \DOMElement[] $kaNonceElements */ |
202
|
|
|
$kaNonceElements = XPath::xpQuery($agreementMethodElement, './xenc:KA-Nonce', $xpCache); |
203
|
|
|
$this->assertCount(1, $kaNonceElements); |
204
|
|
|
|
205
|
|
|
// Test ordering of AgreementMethod contents |
206
|
|
|
/** @var \DOMElement[] $agreementMethodElements */ |
207
|
|
|
$agreementMethodElements = XPath::xpQuery( |
208
|
|
|
$agreementMethodElement, |
209
|
|
|
'./xenc:KA-Nonce/following-sibling::*', |
210
|
|
|
$xpCache, |
211
|
|
|
); |
212
|
|
|
|
213
|
|
|
$this->assertCount(3, $agreementMethodElements); |
214
|
|
|
$this->assertEquals('ds:DigestMethod', $agreementMethodElements[0]->tagName); |
215
|
|
|
$this->assertEquals('xenc:OriginatorKeyInfo', $agreementMethodElements[1]->tagName); |
216
|
|
|
$this->assertEquals('xenc:RecipientKeyInfo', $agreementMethodElements[2]->tagName); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|