| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace SimpleSAML\SAML2\XML\md; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use DOMElement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SimpleSAML\SAML2\XML\SignableElementTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SimpleSAML\SAML2\XML\SignedElementTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SimpleSAML\XMLSecurity\XML\SignableElementInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SimpleSAML\XMLSecurity\XML\SignedElementInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use function method_exists; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Abstract class that represents a signed metadata element. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @package simplesamlphp/saml2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | abstract class AbstractSignedMdElement extends AbstractMdElement implements | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     SignableElementInterface, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     SignedElementInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     use SignableElementTrait; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     use SignedElementTrait { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         SignedElementTrait::getBlacklistedAlgorithms insteadof SignableElementTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * The original signed XML | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var \DOMElement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected DOMElement $xml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * Get the XML element. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @return \DOMElement | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     public function getXML(): DOMElement | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         return $this->xml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * Set the XML element. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param \DOMElement $xml | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     protected function setXML(DOMElement $xml): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->xml = $xml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param \DOMElement|null $parent The EntityDescriptor we should append this SPSSODescriptor to. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return \DOMElement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function toXML(?DOMElement $parent = null): DOMElement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if ($this->isSigned() === true && $this->signer === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             // We already have a signed document and no signer was set to re-sign it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             if ($parent === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 return $this->getXML(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $node = $parent->ownerDocument?->importNode($this->getXML(), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $parent->appendChild($node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             return $parent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $e = $this->toUnsignedXML($parent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         // This is a dirty hack, but if we add the xsi-type on AbstractRoleDescriptor we cannot | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         // get the tests to pass because the attribute-order is messed up. This has something | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         // to do with the fact that toUnsignedXML's recursive nature. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         if (method_exists(static::class, 'getXsiTypePrefix')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $e->setAttributeNS( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 'http://www.w3.org/2000/xmlns/', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 'xmlns:' . static::getXsiTypePrefix(), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 static::getXsiTypeNamespaceURI(), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         if ($this->signer !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $signedXML = $this->doSign($e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $signedXML->insertBefore($this->signature?->toXML($signedXML), $signedXML->firstChild); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             return $signedXML; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         return $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param  \DOMElement|null $parent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @return \DOMElement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     abstract public function toUnsignedXML(?DOMElement $parent = null): DOMElement; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 105 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |  |