|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\XMLSecurity\XML; |
| 6 | + |
| 7 | +use DOMElement; |
| 8 | +use SimpleSAML\XMLSecurity\Assert\Assert; |
| 9 | +use SimpleSAML\XMLSecurity\Constants as C; |
| 10 | +use SimpleSAML\XMLSecurity\Exception\CanonicalizationFailedException; |
| 11 | +use SimpleSAML\XMLSecurity\XML\CanonicalizableElementTrait as BaseCanonicalizableElementTrait; |
| 12 | +use SimpleSAML\XMLSecurity\XML\ds\Transforms; |
| 13 | + |
| 14 | +/** |
| 15 | + * A trait implementing the CanonicalizableElementInterface. |
| 16 | + * |
| 17 | + * @package simplesamlphp/xml-security |
| 18 | + */ |
| 19 | +trait CanonicalizableElementTrait |
| 20 | +{ |
| 21 | + use BaseCanonicalizableElementTrait; |
| 22 | + |
| 23 | + |
| 24 | + /** |
| 25 | + * Process all transforms specified by a given Reference element. |
| 26 | + * |
| 27 | + * @param \SimpleSAML\XMLSecurity\XML\ds\Transforms $transforms The transforms to apply. |
| 28 | + * @param \DOMElement $data The data referenced. |
| 29 | + * |
| 30 | + * @return string The canonicalized data after applying all transforms specified by $ref. |
| 31 | + * |
| 32 | + * @see http://www.w3.org/TR/xmldsig-core/#sec-ReferenceProcessingModel |
| 33 | + */ |
| 34 | + #[Override] |
| 35 | + public function processTransforms( |
| 36 | + Transforms $transforms, |
| 37 | + DOMElement $data, |
| 38 | + ): string { |
| 39 | + Assert::maxCount( |
| 40 | + $transforms->getTransform(), |
| 41 | + C::MAX_TRANSFORMS, |
| 42 | + ReferenceValidationFailedException::class, |
| 43 | + 'Too many transforms.', |
| 44 | + ); |
| 45 | + |
| 46 | + $canonicalMethod = C::C14N_EXCLUSIVE_WITHOUT_COMMENTS; |
| 47 | + $arXPath = null; |
| 48 | + $prefixList = null; |
| 49 | + |
| 50 | + foreach ($transforms->getTransform() as $transform) { |
| 51 | + $canonicalMethod = $transform->getAlgorithm()->getValue(); |
| 52 | + switch ($canonicalMethod) { |
| 53 | + case C::C14N_EXCLUSIVE_WITHOUT_COMMENTS: |
| 54 | + case C::C14N_EXCLUSIVE_WITH_COMMENTS: |
| 55 | + $inclusiveNamespaces = $transform->getInclusiveNamespaces(); |
| 56 | + if ($inclusiveNamespaces !== null) { |
| 57 | + $prefixes = $inclusiveNamespaces->getPrefixes(); |
| 58 | + if ($prefixes !== null) { |
| 59 | + $prefixList = array_map('strval', $prefixes->toArray()); |
| 60 | + } |
| 61 | + } |
| 62 | + break; |
| 63 | + default: |
| 64 | + throw new CanonicalizationFailedException( |
| 65 | + 'Message rejected due to unsupported canonicalization transform.', |
| 66 | + ); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return $this->canonicalizeData($data, $canonicalMethod, $arXPath, $prefixList); |
| 71 | + } |
| 72 | +} |
0 commit comments