| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace SimpleSAML\XMLSecurity\Utils; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use DOMElement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SimpleSAML\XMLSecurity\Constants as C; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SimpleSAML\XMLSecurity\XML\ds\Transforms; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use function count; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use function is_null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Class with utility methods for XML manipulation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @package simplesamlphp/xml-security | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | class XML | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * Canonicalize any given node. | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @param \DOMElement $element The DOM element that needs canonicalization. | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * @param string $c14nMethod The identifier of the canonicalization algorithm to use. | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * See \SimpleSAML\XMLSecurity\Constants. | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * @param array|null $xpaths An array of xpaths to filter the nodes by. Defaults to null (no filters). | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @param array|null $prefixes An array of namespace prefixes to filter the nodes by. Defaults to null (no filters). | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @return string The canonical representation of the given DOM node, according to the algorithm requested. | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     public static function canonicalizeData( | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         DOMElement $element, | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         string $c14nMethod, | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         array $xpaths = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         array $prefixes = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     ): string { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $exclusive = false; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $withComments = match ($c14nMethod) { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |             C::C14N_EXCLUSIVE_WITH_COMMENTS, C::C14N_INCLUSIVE_WITH_COMMENTS => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             default => false, | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         }; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $exclusive = match ($c14nMethod) { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             C::C14N_EXCLUSIVE_WITH_COMMENTS, C::C14N_EXCLUSIVE_WITHOUT_COMMENTS => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             default => false, | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         }; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         if ( | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             is_null($xpaths) | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             && ($element->ownerDocument !== null) | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             && ($element->ownerDocument->documentElement !== null) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             && $element->isSameNode($element->ownerDocument->documentElement) | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             // check for any PI or comments as they would have been excluded | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             $current = $element; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             while ($refNode = $current->previousSibling) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                 if ( | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                     (($refNode->nodeType === XML_COMMENT_NODE) && $withComments) | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                     || $refNode->nodeType === XML_PI_NODE | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |                 ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                     break; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                 $current = $refNode; | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             if ($refNode === null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |                 $element = $element->ownerDocument; | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         return $element->C14N($exclusive, $withComments, $xpaths, $prefixes); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * Process all transforms specified by a given Reference element. | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * @param \SimpleSAML\XMLSecurity\XML\ds\Transforms $transforms The transforms to apply. | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * @param \DOMElement $data The data referenced. | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * @param bool $includeCommentNodes Whether to allow canonicalization with comments or not. | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * @return string The canonicalized data after applying all transforms specified by $ref. | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * @see http://www.w3.org/TR/xmldsig-core/#sec-ReferenceProcessingModel | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public static function processTransforms( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         Transforms $transforms, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         DOMElement $data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     ): string { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $canonicalMethod = C::C14N_EXCLUSIVE_WITHOUT_COMMENTS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $arXPath = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $prefixList = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         foreach ($transforms->getTransform() as $transform) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             $canonicalMethod = $transform->getAlgorithm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             switch ($canonicalMethod) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 case C::C14N_EXCLUSIVE_WITHOUT_COMMENTS: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 case C::C14N_EXCLUSIVE_WITH_COMMENTS: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                     $inclusiveNamespaces = $transform->getInclusiveNamespaces(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                     if ($inclusiveNamespaces !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                         $prefixes = $inclusiveNamespaces->getPrefixes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                         if (count($prefixes) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                             $prefixList = $prefixes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 case C::XPATH_URI: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                     $xpath = $transform->getXPath(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                     if ($xpath !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                         $arXPath = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                         $arXPath['query'] = '(.//. | .//@* | .//namespace::*)[' . $xpath->getExpression() . ']'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                         $arXpath['namespaces'] = $xpath->getNamespaces(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                         // TODO: review if $nsnode->localName is equivalent to the keys in getNamespaces() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | //                        $nslist = $xp->query('./namespace::*', $node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | //                        foreach ($nslist as $nsnode) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | //                            if ($nsnode->localName != "xml") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | //                                $arXPath['namespaces'][$nsnode->localName] = $nsnode->nodeValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | //                            } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | //                        } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  |         return self::canonicalizeData($data, $canonicalMethod, $arXPath, $prefixList); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |  |