|
| 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; |
| 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\{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 SerializableElementTestTrait; |
| 35 | + |
| 36 | + /** @var string */ |
| 37 | + private static string $certificate; |
| 38 | + |
| 39 | + /** @var string[] */ |
| 40 | + private static array $certData; |
| 41 | + |
| 42 | + |
| 43 | + /** |
| 44 | + */ |
| 45 | + public function setUp(): void |
| 46 | + { |
| 47 | + self::$testedClass = AgreementMethod::class; |
| 48 | + |
| 49 | + self::$xmlRepresentation = DOMDocumentFactory::fromFile( |
| 50 | + dirname(__FILE__, 3) . '/resources/xml/xenc_AgreementMethod.xml', |
| 51 | + ); |
| 52 | + |
| 53 | + self::$certificate = str_replace( |
| 54 | + [ |
| 55 | + '-----BEGIN CERTIFICATE-----', |
| 56 | + '-----END CERTIFICATE-----', |
| 57 | + '-----BEGIN RSA PUBLIC KEY-----', |
| 58 | + '-----END RSA PUBLIC KEY-----', |
| 59 | + "\r\n", |
| 60 | + "\n", |
| 61 | + ], |
| 62 | + [ |
| 63 | + '', |
| 64 | + '', |
| 65 | + '', |
| 66 | + '', |
| 67 | + "\n", |
| 68 | + '', |
| 69 | + ], |
| 70 | + PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE), |
| 71 | + ); |
| 72 | + |
| 73 | + self::$certData = openssl_x509_parse( |
| 74 | + PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE), |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + /** |
| 80 | + */ |
| 81 | + public function testMarshalling(): void |
| 82 | + { |
| 83 | + $kaNonce = new KANonce('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI='); |
| 84 | + |
| 85 | + $someChunk = new Chunk(DOMDocumentFactory::fromString( |
| 86 | + '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>', |
| 87 | + )->documentElement); |
| 88 | + |
| 89 | + $originatorKeyInfo = new OriginatorKeyInfo( |
| 90 | + [ |
| 91 | + new KeyName('testkey'), |
| 92 | + new X509Data( |
| 93 | + [ |
| 94 | + new X509Certificate(self::$certificate), |
| 95 | + new X509SubjectName(self::$certData['name']), |
| 96 | + ], |
| 97 | + ), |
| 98 | + new Chunk(DOMDocumentFactory::fromString( |
| 99 | + '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">originator</ssp:Chunk>', |
| 100 | + )->documentElement), |
| 101 | + ], |
| 102 | + 'fed123', |
| 103 | + ); |
| 104 | + |
| 105 | + $recipientKeyInfo = new RecipientKeyInfo( |
| 106 | + [ |
| 107 | + new KeyName('testkey'), |
| 108 | + new X509Data( |
| 109 | + [ |
| 110 | + new X509Certificate(self::$certificate), |
| 111 | + new X509SubjectName(self::$certData['name']), |
| 112 | + ], |
| 113 | + ), |
| 114 | + new Chunk(DOMDocumentFactory::fromString( |
| 115 | + '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">recipient</ssp:Chunk>', |
| 116 | + )->documentElement), |
| 117 | + ], |
| 118 | + 'fed654', |
| 119 | + ); |
| 120 | + |
| 121 | + $agreementMethod = new AgreementMethod( |
| 122 | + C::XMLENC11_ECDH_ES, |
| 123 | + $kaNonce, |
| 124 | + $originatorKeyInfo, |
| 125 | + $recipientKeyInfo, |
| 126 | + [$someChunk], |
| 127 | + ); |
| 128 | + |
| 129 | + $this->assertEquals( |
| 130 | + self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), |
| 131 | + strval($agreementMethod), |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + public function testMarshallingElementOrdering(): void |
| 137 | + { |
| 138 | + $kaNonce = new KANonce('/CTj03d1DB5e2t7CTo9BEzCf5S9NRzwnBgZRlm32REI='); |
| 139 | + |
| 140 | + $someChunk = new Chunk(DOMDocumentFactory::fromString( |
| 141 | + '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>', |
| 142 | + )->documentElement); |
| 143 | + |
| 144 | + $originatorKeyInfo = new OriginatorKeyInfo( |
| 145 | + [ |
| 146 | + new KeyName('testkey'), |
| 147 | + new X509Data( |
| 148 | + [ |
| 149 | + new X509Certificate(self::$certificate), |
| 150 | + new X509SubjectName(self::$certData['name']), |
| 151 | + ], |
| 152 | + ), |
| 153 | + new Chunk(DOMDocumentFactory::fromString( |
| 154 | + '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">originator</ssp:Chunk>', |
| 155 | + )->documentElement), |
| 156 | + ], |
| 157 | + 'fed321', |
| 158 | + ); |
| 159 | + |
| 160 | + $recipientKeyInfo = new RecipientKeyInfo( |
| 161 | + [ |
| 162 | + new KeyName('testkey'), |
| 163 | + new X509Data( |
| 164 | + [ |
| 165 | + new X509Certificate(self::$certificate), |
| 166 | + new X509SubjectName(self::$certData['name']), |
| 167 | + ], |
| 168 | + ), |
| 169 | + new Chunk(DOMDocumentFactory::fromString( |
| 170 | + '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">recipient</ssp:Chunk>', |
| 171 | + )->documentElement), |
| 172 | + ], |
| 173 | + 'fed654', |
| 174 | + ); |
| 175 | + |
| 176 | + $agreementMethod = new AgreementMethod( |
| 177 | + C::XMLENC11_ECDH_ES, |
| 178 | + $kaNonce, |
| 179 | + $originatorKeyInfo, |
| 180 | + $recipientKeyInfo, |
| 181 | + [$someChunk], |
| 182 | + ); |
| 183 | + |
| 184 | + // Marshall it to a \DOMElement |
| 185 | + $agreementMethodElement = $agreementMethod->toXML(); |
| 186 | + |
| 187 | + $xpCache = XPath::getXPath($agreementMethodElement); |
| 188 | + |
| 189 | + // Test for an KA-Nonce |
| 190 | + /** @var \DOMElement[] $kaNonceElements */ |
| 191 | + $kaNonceElements = XPath::xpQuery($agreementMethodElement, './xenc:KA-Nonce', $xpCache); |
| 192 | + $this->assertCount(1, $kaNonceElements); |
| 193 | + |
| 194 | + // Test ordering of AgreementMethod contents |
| 195 | + /** @var \DOMElement[] $agreementMethodElements */ |
| 196 | + $agreementMethodElements = XPath::xpQuery( |
| 197 | + $agreementMethodElement, |
| 198 | + './xenc:KA-Nonce/following-sibling::*', |
| 199 | + $xpCache, |
| 200 | + ); |
| 201 | + |
| 202 | + $this->assertCount(3, $agreementMethodElements); |
| 203 | + $this->assertEquals('ssp:Chunk', $agreementMethodElements[0]->tagName); |
| 204 | + $this->assertEquals('xenc:OriginatorKeyInfo', $agreementMethodElements[1]->tagName); |
| 205 | + $this->assertEquals('xenc:RecipientKeyInfo', $agreementMethodElements[2]->tagName); |
| 206 | + } |
| 207 | +} |
0 commit comments