|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Test\Module\saml; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use SAML2\AuthnRequest; |
| 9 | +use SimpleSAML\Configuration; |
| 10 | +use SimpleSAML\Error as SSP_Error; |
| 11 | +use SimpleSAML\Error\ErrorCodes; |
| 12 | +use SimpleSAML\Module\saml\Message; |
| 13 | +use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock; |
| 14 | + |
| 15 | +/** |
| 16 | + * Test for SAML Message handling |
| 17 | + * |
| 18 | + * @package SimpleSAML\Test\Module\saml |
| 19 | + */ |
| 20 | +class MessageTest extends TestCase |
| 21 | +{ |
| 22 | + /** @var string */ |
| 23 | + protected string $acmeeEntityId; |
| 24 | + |
| 25 | + /** @var string */ |
| 26 | + protected string $acmeeCertificate; |
| 27 | + |
| 28 | + /** @var array */ |
| 29 | + protected array $acmeeMetadata; |
| 30 | + |
| 31 | + /** @var string */ |
| 32 | + protected string $spEntityId; |
| 33 | + |
| 34 | + /** @var array */ |
| 35 | + protected array $spMetadata; |
| 36 | + |
| 37 | + /** @var string */ |
| 38 | + protected string $acmeeCertificateWrong; |
| 39 | + |
| 40 | + /** @var string */ |
| 41 | + protected string $acmeeCertificateMismatch; |
| 42 | + |
| 43 | + /** |
| 44 | + * Set up for each test. |
| 45 | + */ |
| 46 | + protected function setUp(): void |
| 47 | + { |
| 48 | + $this->acmeeEntityId = 'https://idp.acmee.com/example'; |
| 49 | + |
| 50 | + $this->acmeeCertificate = PEMCertificatesMock::getPlainCertificateContents(); |
| 51 | + |
| 52 | + $this->acmeeCertificateMismatch = PEMCertificatesMock::getPlainCertificateContents( |
| 53 | + PEMCertificatesMock::OTHER_CERTIFICATE, |
| 54 | + ); |
| 55 | + |
| 56 | + $this->acmeeCertificateWrong = PEMCertificatesMock::getPlainCertificateContents( |
| 57 | + PEMCertificatesMock::CORRUPTED_CERTIFICATE, |
| 58 | + ); |
| 59 | + |
| 60 | + // Metadata array, just like you'd see in saml20-idp-remote.php |
| 61 | + $this->acmeeMetadata = [ |
| 62 | + 'entityid' => $this->acmeeEntityId, |
| 63 | + 'description' => [ |
| 64 | + 'en' => 'Acmee IdP for testing', |
| 65 | + ], |
| 66 | + 'OrganizationName' => [ |
| 67 | + 'en' => 'Acmee', |
| 68 | + ], |
| 69 | + 'name' => [ |
| 70 | + 'en' => 'Acmee Example Identity Provider', |
| 71 | + ], |
| 72 | + 'OrganizationDisplayName' => [ |
| 73 | + 'en' => 'Acmee', |
| 74 | + ], |
| 75 | + 'url' => [ |
| 76 | + 'en' => 'https://www.acmee.com/', |
| 77 | + ], |
| 78 | + 'OrganizationURL' => [ |
| 79 | + 'en' => 'https://www.acmee.com/', |
| 80 | + ], |
| 81 | + 'contacts' => [ |
| 82 | + [ |
| 83 | + 'contactType' => 'technical', |
| 84 | + 'givenName' => 'Test Admin', |
| 85 | + 'emailAddress' => [ |
| 86 | + 'admin@acmee.com', |
| 87 | + ], |
| 88 | + ], |
| 89 | + ], |
| 90 | + 'metadata-set' => 'saml20-idp-remote', |
| 91 | + 'SingleSignOnService' => [ |
| 92 | + [ |
| 93 | + 'Binding' => 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect', |
| 94 | + 'Location' => 'https://idp.acmee.com/example/sso', |
| 95 | + ], |
| 96 | + [ |
| 97 | + 'Binding' => 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST', |
| 98 | + 'Location' => 'https://idp.acmee.com/example/sso', |
| 99 | + ], |
| 100 | + ], |
| 101 | + 'SingleLogoutService' => [ |
| 102 | + [ |
| 103 | + 'Binding' => 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect', |
| 104 | + 'Location' => 'https://idp.acmee.com/example/logout', |
| 105 | + ], |
| 106 | + ], |
| 107 | + 'ArtifactResolutionService' => [], |
| 108 | + 'NameIDFormats' => [ |
| 109 | + 'urn:oasis:names:tc:SAML:2.0:nameid-format:persistent', |
| 110 | + 'urn:oasis:names:tc:SAML:2.0:nameid-format:transient', |
| 111 | + ], |
| 112 | + 'keys' => [ |
| 113 | + [ |
| 114 | + 'encryption' => false, |
| 115 | + 'signing' => true, |
| 116 | + 'type' => 'X509Certificate', |
| 117 | + 'X509Certificate' => $this->acmeeCertificate, |
| 118 | + ], |
| 119 | + ], |
| 120 | + 'scope' => [ |
| 121 | + 'acmee.com', |
| 122 | + ], |
| 123 | + 'UIInfo' => [ |
| 124 | + 'DisplayName' => [ |
| 125 | + 'en' => 'Acmee Identity Provider', |
| 126 | + ], |
| 127 | + 'Description' => [], |
| 128 | + 'InformationURL' => [], |
| 129 | + 'PrivacyStatementURL' => [], |
| 130 | + ], |
| 131 | + ]; |
| 132 | + |
| 133 | + // Build minimal SP metadata: |
| 134 | + $this->spEntityId = 'https://sp.acmee.com/demo'; |
| 135 | + $this->spMetadata = [ |
| 136 | + 'entityID' => $this->spEntityId, |
| 137 | + 'AssertionConsumerService' => [ |
| 138 | + [ |
| 139 | + 'Binding' => 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST', |
| 140 | + 'Location' => 'https://sp.acmee.com/demo/acs', |
| 141 | + ], |
| 142 | + ], |
| 143 | + ]; |
| 144 | + |
| 145 | + parent::setUp(); |
| 146 | + } |
| 147 | + |
| 148 | + public function testCheckSignThrowsWhenBadCertificate(): void |
| 149 | + { |
| 150 | + $this->expectException(\Exception::class); |
| 151 | + |
| 152 | + $localMetadata = $this->acmeeMetadata; |
| 153 | + foreach ($localMetadata['keys'] as $index => $cert) { |
| 154 | + $localMetadata['keys'][$index]['X509Certificate'] = $this->acmeeCertificateWrong; |
| 155 | + } |
| 156 | + $idpConfig = Configuration::loadFromArray( |
| 157 | + $localMetadata, |
| 158 | + $localMetadata['entityid'], |
| 159 | + ); |
| 160 | + |
| 161 | + $spConfig = Configuration::loadFromArray( |
| 162 | + $this->spMetadata, |
| 163 | + $this->spMetadata['entityID'], |
| 164 | + ); |
| 165 | + |
| 166 | + // Build the AuthnRequest using the Message helper: |
| 167 | + $authnRequest = Message::buildAuthnRequest($spConfig, $idpConfig); |
| 168 | + |
| 169 | + // You may now use $authnRequest with checkSign: |
| 170 | + Message::checkSign($idpConfig, $authnRequest); |
| 171 | + } |
| 172 | + |
| 173 | + public function testCheckSignThrowsWhenMissingCertificate(): void |
| 174 | + { |
| 175 | + $this->expectException(SSP_Error\Exception::class); |
| 176 | + $this->expectExceptionMessage('Missing certificate in metadata for \'https://idp.acmee.com/example\''); |
| 177 | + |
| 178 | + $localMetadata = $this->acmeeMetadata; |
| 179 | + unset($localMetadata['keys']); |
| 180 | + $idpConfig = Configuration::loadFromArray( |
| 181 | + $localMetadata, |
| 182 | + $localMetadata['entityid'], |
| 183 | + ); |
| 184 | + |
| 185 | + $spConfig = Configuration::loadFromArray( |
| 186 | + $this->spMetadata, |
| 187 | + $this->spMetadata['entityID'], |
| 188 | + ); |
| 189 | + |
| 190 | + // Build the AuthnRequest using the Message helper: |
| 191 | + $authnRequest = Message::buildAuthnRequest($spConfig, $idpConfig); |
| 192 | + |
| 193 | + // You may now use $authnRequest with checkSign: |
| 194 | + Message::checkSign($idpConfig, $authnRequest); |
| 195 | + } |
| 196 | + |
| 197 | + public function testCheckSignThrowsWhenCertificateMismatch(): void |
| 198 | + { |
| 199 | + $this->expectException(SSP_Error\Error::class); |
| 200 | + $expectedMessage = [ |
| 201 | + 'errorCode' => ErrorCodes::NOTVALIDCERTSIGNATURE, |
| 202 | + 'message' => (new ErrorCodes())->getMessage(ErrorCodes::NOTVALIDCERTSIGNATURE), |
| 203 | + 'element' => 'SAML2\AuthnRequest', |
| 204 | + 'issuer' => 'https://sp.acmee.com/demo', |
| 205 | + 'entityid' => 'https://idp.acmee.com/example', |
| 206 | + ]; |
| 207 | + |
| 208 | + $this->expectExceptionMessage(json_encode($expectedMessage)); |
| 209 | + |
| 210 | + $localMetadata = $this->acmeeMetadata; |
| 211 | + $localMetadata['signature.privatekey'] = PEMCertificatesMock::buildKeysPath( |
| 212 | + PEMCertificatesMock::PRIVATE_KEY, |
| 213 | + ); |
| 214 | + $localMetadata['signature.privatekey_pass'] = PEMCertificatesMock::PASSPHRASE; |
| 215 | + $localMetadata['sign.authnrequest'] = true; |
| 216 | + foreach ($localMetadata['keys'] as $index => $cert) { |
| 217 | + $localMetadata['keys'][$index]['X509Certificate'] = $this->acmeeCertificateMismatch; |
| 218 | + } |
| 219 | + $idpConfig = Configuration::loadFromArray( |
| 220 | + $localMetadata, |
| 221 | + $localMetadata['entityid'], |
| 222 | + ); |
| 223 | + |
| 224 | + $spConfig = Configuration::loadFromArray( |
| 225 | + $this->spMetadata, |
| 226 | + $this->spMetadata['entityID'], |
| 227 | + ); |
| 228 | + |
| 229 | + // Build the AuthnRequest using the Message helper: |
| 230 | + $authnRequest = Message::buildAuthnRequest($spConfig, $idpConfig); |
| 231 | + // Extract to signed xml and then parse again to validate |
| 232 | + $signedDomElement = $authnRequest->toSignedXML(); |
| 233 | + $parsed = new AuthnRequest($signedDomElement); |
| 234 | + Message::checkSign($idpConfig, $parsed); |
| 235 | + } |
| 236 | + |
| 237 | + public function testCheckSignSucceeds(): void |
| 238 | + { |
| 239 | + $localMetadata = $this->acmeeMetadata; |
| 240 | + $localMetadata['signature.privatekey'] = PEMCertificatesMock::buildKeysPath( |
| 241 | + PEMCertificatesMock::PRIVATE_KEY, |
| 242 | + ); |
| 243 | + $localMetadata['signature.privatekey_pass'] = PEMCertificatesMock::PASSPHRASE; |
| 244 | + $localMetadata['sign.authnrequest'] = true; |
| 245 | + |
| 246 | + $idpConfig = Configuration::loadFromArray( |
| 247 | + $localMetadata, |
| 248 | + $localMetadata['entityid'], |
| 249 | + ); |
| 250 | + |
| 251 | + $spConfig = Configuration::loadFromArray( |
| 252 | + $this->spMetadata, |
| 253 | + $this->spMetadata['entityID'], |
| 254 | + ); |
| 255 | + |
| 256 | + // Build the AuthnRequest using the Message helper: |
| 257 | + $authnRequest = Message::buildAuthnRequest($spConfig, $idpConfig); |
| 258 | + // Extract to signed xml and then parse again to validate |
| 259 | + $signedDomElement = $authnRequest->toSignedXML(); |
| 260 | + $parsed = new AuthnRequest($signedDomElement); |
| 261 | + $isValid = Message::checkSign($idpConfig, $parsed); |
| 262 | + $this->assertTrue($isValid); |
| 263 | + } |
| 264 | +} |
0 commit comments