|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\XMLSecurity\XML\xenc; |
| 6 | + |
| 7 | +use DOMElement; |
| 8 | +use SimpleSAML\Assert\Assert; |
| 9 | +use SimpleSAML\XML\Exception\InvalidDOMElementException; |
| 10 | +use SimpleSAML\XML\Exception\SchemaViolationException; |
| 11 | +use SimpleSAML\XML\Exception\TooManyElementsException; |
| 12 | +use SimpleSAML\XML\ExtendableElementTrait; |
| 13 | +use SimpleSAML\XML\XsNamespace as NS; |
| 14 | + |
| 15 | +use function array_pop; |
| 16 | + |
| 17 | +/** |
| 18 | + * A class implementing the xenc:AbstractAgreementMethodType element. |
| 19 | + * |
| 20 | + * @package simplesamlphp/xml-security |
| 21 | + */ |
| 22 | +abstract class AbstractAgreementMethodType extends AbstractXencElement |
| 23 | +{ |
| 24 | + use ExtendableElementTrait; |
| 25 | + |
| 26 | + /** The namespace-attribute for the xs:any element */ |
| 27 | + public const XS_ANY_ELT_NAMESPACE = NS::OTHER; |
| 28 | + |
| 29 | + |
| 30 | + /** |
| 31 | + * AgreementMethodType constructor. |
| 32 | + * |
| 33 | + * @param string $algorithm |
| 34 | + * @param \SimpleSAML\XMLSecurity\XML\xenc\KANonce|null $kaNonce |
| 35 | + * @param \SimpleSAML\XMLSecurity\XML\xenc\OriginatorKeyInfo|null $originatorKeyInfo |
| 36 | + * @param \SimpleSAML\XMLSecurity\XML\xenc\RecipientKeyInfo|null $recipientKeyInfo |
| 37 | + * @param list<\SimpleSAML\XML\SerializableElementInterface> $children |
| 38 | + */ |
| 39 | + final public function __construct( |
| 40 | + protected string $algorithm, |
| 41 | + protected ?KANonce $kaNonce = null, |
| 42 | + protected ?OriginatorKeyInfo $originatorKeyInfo = null, |
| 43 | + protected ?RecipientKeyInfo $recipientKeyInfo = null, |
| 44 | + protected array $children = [], |
| 45 | + ) { |
| 46 | + Assert::validURI($algorithm, SchemaViolationException::class); // Covers the empty string |
| 47 | + |
| 48 | + $this->setElements($children); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + /** |
| 53 | + * Get the URI identifying the algorithm used by this agreement method. |
| 54 | + * |
| 55 | + * @return string |
| 56 | + */ |
| 57 | + public function getAlgorithm(): string |
| 58 | + { |
| 59 | + return $this->algorithm; |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + /** |
| 64 | + * Get the KA-Nonce. |
| 65 | + * |
| 66 | + * @return \SimpleSAML\XMLSecurity\XML\xenc\KANonce|null |
| 67 | + */ |
| 68 | + public function getKANonce(): ?KANonce |
| 69 | + { |
| 70 | + return $this->kaNonce; |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + /** |
| 75 | + * Get the Originator KeyInfo. |
| 76 | + * |
| 77 | + * @return \SimpleSAML\XMLSecurity\XML\xenc\OriginatorKeyInfo|null |
| 78 | + */ |
| 79 | + public function getOriginatorKeyInfo(): ?OriginatorKeyInfo |
| 80 | + { |
| 81 | + return $this->originatorKeyInfo; |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + /** |
| 86 | + * Get the Recipient KeyInfo. |
| 87 | + * |
| 88 | + * @return \SimpleSAML\XMLSecurity\XML\xenc\RecipientKeyInfo|null |
| 89 | + */ |
| 90 | + public function getRecipientKeyInfo(): ?RecipientKeyInfo |
| 91 | + { |
| 92 | + return $this->recipientKeyInfo; |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + /** |
| 97 | + * Initialize an AgreementMethod object from an existing XML. |
| 98 | + * |
| 99 | + * @param \DOMElement $xml |
| 100 | + * @return static |
| 101 | + * |
| 102 | + * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
| 103 | + * if the qualified name of the supplied element is wrong |
| 104 | + * @throws \SimpleSAML\XML\Exception\MissingAttributeException |
| 105 | + * if the supplied element is missing one of the mandatory attributes |
| 106 | + * @throws \SimpleSAML\XML\Exception\TooManyElementsException |
| 107 | + * if too many child-elements of a type are specified |
| 108 | + */ |
| 109 | + public static function fromXML(DOMElement $xml): static |
| 110 | + { |
| 111 | + Assert::same($xml->localName, 'AgreementMethod', InvalidDOMElementException::class); |
| 112 | + Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
| 113 | + |
| 114 | + $algorithm = self::getAttribute($xml, 'Algorithm'); |
| 115 | + |
| 116 | + $kaNonce = KANonce::getChildrenOfClass($xml); |
| 117 | + Assert::maxCount($kaNonce, 1, TooManyElementsException::class); |
| 118 | + |
| 119 | + $originatorKeyInfo = OriginatorKeyInfo::getChildrenOfClass($xml); |
| 120 | + Assert::maxCount($originatorKeyInfo, 1, TooManyElementsException::class); |
| 121 | + |
| 122 | + $recipientKeyInfo = RecipientKeyInfo::getChildrenOfClass($xml); |
| 123 | + Assert::maxCount($recipientKeyInfo, 1, TooManyElementsException::class); |
| 124 | + |
| 125 | + $children = self::getChildElementsFromXML($xml); |
| 126 | + |
| 127 | + return new static( |
| 128 | + $algorithm, |
| 129 | + array_pop($kaNonce), |
| 130 | + array_pop($originatorKeyInfo), |
| 131 | + array_pop($recipientKeyInfo), |
| 132 | + $children, |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + /** |
| 138 | + * Convert this AgreementMethod object to XML. |
| 139 | + * |
| 140 | + * @param \DOMElement|null $parent The element we should append this AgreementMethod to. |
| 141 | + * @return \DOMElement |
| 142 | + */ |
| 143 | + public function toXML(DOMElement $parent = null): DOMElement |
| 144 | + { |
| 145 | + $e = $this->instantiateParentElement($parent); |
| 146 | + $e->setAttribute('Algorithm', $this->getAlgorithm()); |
| 147 | + |
| 148 | + $this->getKANonce()?->toXML($e); |
| 149 | + |
| 150 | + foreach ($this->getElements() as $child) { |
| 151 | + $child->toXML($e); |
| 152 | + } |
| 153 | + |
| 154 | + $this->getOriginatorKeyInfo()?->toXML($e); |
| 155 | + $this->getRecipientKeyInfo()?->toXML($e); |
| 156 | + |
| 157 | + return $e; |
| 158 | + } |
| 159 | +} |
0 commit comments