|
| 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\MissingElementException; |
| 11 | +use SimpleSAML\XML\Exception\SchemaViolationException; |
| 12 | +use SimpleSAML\XML\Exception\TooManyElementsException; |
| 13 | + |
| 14 | +use function array_pop; |
| 15 | + |
| 16 | +/** |
| 17 | + * A class implementing the xenc:AbstractDHKeyValueType element. |
| 18 | + * |
| 19 | + * @package simplesamlphp/xml-security |
| 20 | + */ |
| 21 | +abstract class AbstractDHKeyValueType extends AbstractXencElement |
| 22 | +{ |
| 23 | + /** |
| 24 | + * DHKeyValueType constructor. |
| 25 | + * |
| 26 | + * @param \SimpleSAML\XMLSecurity\XML\xenc\XencPublic $xencPublic |
| 27 | + * @param \SimpleSAML\XMLSecurity\XML\xenc\P|null $p |
| 28 | + * @param \SimpleSAML\XMLSecurity\XML\xenc\Q|null $q |
| 29 | + * @param \SimpleSAML\XMLSecurity\XML\xenc\Generator|null $generator |
| 30 | + * @param \SimpleSAML\XMLSecurity\XML\xenc\Seed|null $seed |
| 31 | + * @param \SimpleSAML\XMLSecurity\XML\xenc\PgenCounter|null $pgenCounter |
| 32 | + */ |
| 33 | + final public function __construct( |
| 34 | + protected XencPublic $xencPublic, |
| 35 | + protected ?P $p = null, |
| 36 | + protected ?Q $q = null, |
| 37 | + protected ?Generator $generator = null, |
| 38 | + protected ?Seed $seed = null, |
| 39 | + protected ?PgenCounter $pgenCounter = null, |
| 40 | + ) { |
| 41 | + if ($p !== null || $q !== null || $generator !== null) { |
| 42 | + Assert::allNotNull([$p, $q, $generator], SchemaViolationException::class); |
| 43 | + } else { |
| 44 | + Assert::allNull([$p, $q, $generator], SchemaViolationException::class); |
| 45 | + } |
| 46 | + |
| 47 | + if ($seed !== null || $pgenCounter !== null) { |
| 48 | + Assert::allNotNull([$seed, $pgenCounter], SchemaViolationException::class); |
| 49 | + } else { |
| 50 | + Assert::allNull([$seed, $pgenCounter], SchemaViolationException::class); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + /** |
| 56 | + * Get the Public. |
| 57 | + * |
| 58 | + * @return \SimpleSAML\XMLSecurity\XML\xenc\XencPublic |
| 59 | + */ |
| 60 | + public function getPublic(): XencPublic |
| 61 | + { |
| 62 | + return $this->xencPublic; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + /** |
| 67 | + * Get the P. |
| 68 | + * |
| 69 | + * @return \SimpleSAML\XMLSecurity\XML\xenc\P|null |
| 70 | + */ |
| 71 | + public function getP(): ?P |
| 72 | + { |
| 73 | + return $this->p; |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * Get the Q. |
| 79 | + * |
| 80 | + * @return \SimpleSAML\XMLSecurity\XML\xenc\Q|null |
| 81 | + */ |
| 82 | + public function getQ(): ?Q |
| 83 | + { |
| 84 | + return $this->q; |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + /** |
| 89 | + * Get the Generator. |
| 90 | + * |
| 91 | + * @return \SimpleSAML\XMLSecurity\XML\xenc\Generator|null |
| 92 | + */ |
| 93 | + public function getGenerator(): ?Generator |
| 94 | + { |
| 95 | + return $this->generator; |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + /** |
| 100 | + * Get the Seed. |
| 101 | + * |
| 102 | + * @return \SimpleSAML\XMLSecurity\XML\xenc\Seed|null |
| 103 | + */ |
| 104 | + public function getSeed(): ?Seed |
| 105 | + { |
| 106 | + return $this->seed; |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + /** |
| 111 | + * Get the PgenCounter. |
| 112 | + * |
| 113 | + * @return \SimpleSAML\XMLSecurity\XML\xenc\PgenCounter|null |
| 114 | + */ |
| 115 | + public function getPgenCounter(): ?PgenCounter |
| 116 | + { |
| 117 | + return $this->pgenCounter; |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + /** |
| 122 | + * Initialize an DHKeyValue object from an existing XML. |
| 123 | + * |
| 124 | + * @param \DOMElement $xml |
| 125 | + * @return static |
| 126 | + * |
| 127 | + * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
| 128 | + * if the qualified name of the supplied element is wrong |
| 129 | + * @throws \SimpleSAML\XML\Exception\MissingAttributeException |
| 130 | + * if the supplied element is missing one of the mandatory attributes |
| 131 | + * @throws \SimpleSAML\XML\Exception\TooManyElementsException |
| 132 | + * if too many child-elements of a type are specified |
| 133 | + */ |
| 134 | + public static function fromXML(DOMElement $xml): static |
| 135 | + { |
| 136 | + Assert::same($xml->localName, 'DHKeyValue', InvalidDOMElementException::class); |
| 137 | + Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
| 138 | + |
| 139 | + $xencPublic = XencPublic::getChildrenOfClass($xml); |
| 140 | + Assert::minCount($xencPublic, 1, MissingElementException::class); |
| 141 | + Assert::maxCount($xencPublic, 1, TooManyElementsException::class); |
| 142 | + |
| 143 | + $p = P::getChildrenOfClass($xml); |
| 144 | + Assert::maxCount($p, 1, TooManyElementsException::class); |
| 145 | + |
| 146 | + $q = Q::getChildrenOfClass($xml); |
| 147 | + Assert::maxCount($q, 1, TooManyElementsException::class); |
| 148 | + |
| 149 | + $generator = Generator::getChildrenOfClass($xml); |
| 150 | + Assert::maxCount($generator, 1, TooManyElementsException::class); |
| 151 | + |
| 152 | + $seed = Seed::getChildrenOfClass($xml); |
| 153 | + Assert::maxCount($seed, 1, TooManyElementsException::class); |
| 154 | + |
| 155 | + $pgenCounter = PgenCounter::getChildrenOfClass($xml); |
| 156 | + Assert::maxCount($pgenCounter, 1, TooManyElementsException::class); |
| 157 | + |
| 158 | + return new static( |
| 159 | + array_pop($xencPublic), |
| 160 | + array_pop($p), |
| 161 | + array_pop($q), |
| 162 | + array_pop($generator), |
| 163 | + array_pop($seed), |
| 164 | + array_pop($pgenCounter), |
| 165 | + ); |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | + /** |
| 170 | + * Convert this DHKeyValue object to XML. |
| 171 | + * |
| 172 | + * @param \DOMElement|null $parent The element we should append this DHKeyValue to. |
| 173 | + * @return \DOMElement |
| 174 | + */ |
| 175 | + public function toXML(DOMElement $parent = null): DOMElement |
| 176 | + { |
| 177 | + $e = $this->instantiateParentElement($parent); |
| 178 | + |
| 179 | + $this->getP()?->toXML($e); |
| 180 | + $this->getQ()?->toXML($e); |
| 181 | + $this->getGenerator()?->toXML($e); |
| 182 | + $this->getPublic()->toXML($e); |
| 183 | + $this->getSeed()?->toXML($e); |
| 184 | + $this->getPgenCounter()?->toXML($e); |
| 185 | + |
| 186 | + return $e; |
| 187 | + } |
| 188 | +} |
0 commit comments