1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\XMLSecurity\Test\XML\ds; |
6
|
|
|
|
7
|
|
|
use DOMDocument; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use SimpleSAML\Assert\AssertionFailedException; |
10
|
|
|
use SimpleSAML\XML\Chunk; |
11
|
|
|
use SimpleSAML\XML\DOMDocumentFactory; |
12
|
|
|
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait; |
13
|
|
|
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait; |
14
|
|
|
use SimpleSAML\XML\Utils as XMLUtils; |
15
|
|
|
use SimpleSAML\XMLSecurity\Constants as C; |
16
|
|
|
use SimpleSAML\XMLSecurity\CryptoEncoding\PEM; |
17
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\X509Certificate; |
18
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\X509Data; |
19
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\X509IssuerName; |
20
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial; |
21
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\X509SerialNumber; |
22
|
|
|
use SimpleSAML\XMLSecurity\XML\ds\X509SubjectName; |
23
|
|
|
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock; |
24
|
|
|
|
25
|
|
|
use function base64_encode; |
26
|
|
|
use function dirname; |
27
|
|
|
use function hex2bin; |
28
|
|
|
use function openssl_x509_parse; |
29
|
|
|
use function str_replace; |
30
|
|
|
use function strval; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class \SimpleSAML\XMLSecurity\Test\XML\ds\X509DataTest |
34
|
|
|
* |
35
|
|
|
* @covers \SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement |
36
|
|
|
* @covers \SimpleSAML\XMLSecurity\XML\ds\X509Data |
37
|
|
|
* |
38
|
|
|
* @package simplesamlphp/xml-security |
39
|
|
|
*/ |
40
|
|
|
final class X509DataTest extends TestCase |
41
|
|
|
{ |
42
|
|
|
use SchemaValidationTestTrait; |
|
|
|
|
43
|
|
|
use SerializableElementTestTrait; |
|
|
|
|
44
|
|
|
|
45
|
|
|
/** @var string */ |
46
|
|
|
private static string $certificate; |
47
|
|
|
|
48
|
|
|
/** @var string[] */ |
49
|
|
|
private static array $certData; |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
*/ |
54
|
|
|
public static function setUpBeforeClass(): void |
55
|
|
|
{ |
56
|
|
|
self::$testedClass = X509Data::class; |
57
|
|
|
|
58
|
|
|
self::$schemaFile = dirname(__FILE__, 4) . '/resources/schemas/xmldsig1-schema.xsd'; |
59
|
|
|
|
60
|
|
|
self::$xmlRepresentation = DOMDocumentFactory::fromFile( |
61
|
|
|
dirname(__FILE__, 3) . '/resources/xml/ds_X509Data.xml', |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
self::$certificate = str_replace( |
65
|
|
|
[ |
66
|
|
|
'-----BEGIN CERTIFICATE-----', |
67
|
|
|
'-----END CERTIFICATE-----', |
68
|
|
|
'-----BEGIN RSA PUBLIC KEY-----', |
69
|
|
|
'-----END RSA PUBLIC KEY-----', |
70
|
|
|
"\r\n", |
71
|
|
|
"\n", |
72
|
|
|
], |
73
|
|
|
[ |
74
|
|
|
'', |
75
|
|
|
'', |
76
|
|
|
'', |
77
|
|
|
'', |
78
|
|
|
"\n", |
79
|
|
|
'' |
80
|
|
|
], |
81
|
|
|
PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE), |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
self::$certData = openssl_x509_parse( |
85
|
|
|
PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE), |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
*/ |
92
|
|
|
public function testMarshalling(): void |
93
|
|
|
{ |
94
|
|
|
$x509data = new X509Data( |
95
|
|
|
[ |
96
|
|
|
new Chunk( |
97
|
|
|
DOMDocumentFactory::fromString( |
98
|
|
|
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>' |
99
|
|
|
)->documentElement, |
100
|
|
|
), |
101
|
|
|
new X509Certificate(self::$certificate), |
102
|
|
|
new X509IssuerSerial( |
103
|
|
|
new X509IssuerName(sprintf( |
104
|
|
|
'C=US,ST=Hawaii,L=Honolulu,O=SimpleSAMLphp HQ,CN=SimpleSAMLphp Testing CA,emailAddress=%s', |
105
|
|
|
'[email protected]' |
106
|
|
|
)), |
107
|
|
|
new X509SerialNumber('2'), |
108
|
|
|
), |
109
|
|
|
new X509SubjectName(self::$certData['name']), |
110
|
|
|
new Chunk(DOMDocumentFactory::fromString( |
111
|
|
|
'<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">other</ssp:Chunk>' |
112
|
|
|
)->documentElement) |
113
|
|
|
], |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
$this->assertEquals( |
117
|
|
|
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), |
118
|
|
|
strval($x509data), |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
*/ |
125
|
|
|
public function testUnmarshalling(): void |
126
|
|
|
{ |
127
|
|
|
$x509data = X509Data::fromXML(self::$xmlRepresentation->documentElement); |
128
|
|
|
|
129
|
|
|
$this->assertEquals( |
130
|
|
|
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), |
131
|
|
|
strval($x509data), |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|