simplesamlphp /
xml-security
| 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\ExtendableElementTrait; |
||
| 10 | use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException; |
||
| 11 | use SimpleSAML\XMLSchema\Exception\TooManyElementsException; |
||
| 12 | use SimpleSAML\XMLSchema\Type\AnyURIValue; |
||
| 13 | use SimpleSAML\XMLSchema\XML\Constants\NS; |
||
| 14 | |||
| 15 | use function array_pop; |
||
| 16 | use function strval; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * A class implementing the xenc:AbstractEncryptionMethod element. |
||
| 20 | * |
||
| 21 | * @package simplesamlphp/xml-security |
||
| 22 | */ |
||
| 23 | abstract class AbstractEncryptionMethod extends AbstractXencElement |
||
| 24 | { |
||
| 25 | use ExtendableElementTrait; |
||
| 26 | |||
| 27 | |||
| 28 | /** The namespace-attribute for the xs:any element */ |
||
| 29 | public const string XS_ANY_ELT_NAMESPACE = NS::OTHER; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * EncryptionMethod constructor. |
||
| 34 | * |
||
| 35 | * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $algorithm |
||
| 36 | * @param \SimpleSAML\XMLSecurity\XML\xenc\KeySize|null $keySize |
||
| 37 | * @param \SimpleSAML\XMLSecurity\XML\xenc\OAEPparams|null $oaepParams |
||
| 38 | * @param list<\SimpleSAML\XML\SerializableElementInterface> $children |
||
| 39 | */ |
||
| 40 | final public function __construct( |
||
| 41 | protected AnyURIValue $algorithm, |
||
| 42 | protected ?KeySize $keySize = null, |
||
| 43 | protected ?OAEPparams $oaepParams = null, |
||
| 44 | array $children = [], |
||
| 45 | ) { |
||
| 46 | $this->setElements($children); |
||
| 47 | } |
||
| 48 | |||
| 49 | |||
| 50 | /** |
||
| 51 | * Get the URI identifying the algorithm used by this encryption method. |
||
| 52 | * |
||
| 53 | * @return \SimpleSAML\XMLSchema\Type\AnyURIValue |
||
| 54 | */ |
||
| 55 | public function getAlgorithm(): AnyURIValue |
||
| 56 | { |
||
| 57 | return $this->algorithm; |
||
| 58 | } |
||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * Get the size of the key used by this encryption method. |
||
| 63 | * |
||
| 64 | * @return \SimpleSAML\XMLSecurity\XML\xenc\KeySize|null |
||
| 65 | */ |
||
| 66 | public function getKeySize(): ?KeySize |
||
| 67 | { |
||
| 68 | return $this->keySize; |
||
| 69 | } |
||
| 70 | |||
| 71 | |||
| 72 | /** |
||
| 73 | * Get the OAEP parameters. |
||
| 74 | * |
||
| 75 | * @return \SimpleSAML\XMLSecurity\XML\xenc\OAEPparams|null |
||
| 76 | */ |
||
| 77 | public function getOAEPParams(): ?OAEPparams |
||
| 78 | { |
||
| 79 | return $this->oaepParams; |
||
| 80 | } |
||
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * Initialize an EncryptionMethod object from an existing XML. |
||
| 85 | * |
||
| 86 | * @param \DOMElement $xml |
||
| 87 | * |
||
| 88 | * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException |
||
| 89 | * if the qualified name of the supplied element is wrong |
||
| 90 | * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException |
||
| 91 | * if the supplied element is missing one of the mandatory attributes |
||
| 92 | * @throws \SimpleSAML\XMLSchema\Exception\TooManyElementsException |
||
| 93 | * if too many child-elements of a type are specified |
||
| 94 | */ |
||
| 95 | public static function fromXML(DOMElement $xml): static |
||
| 96 | { |
||
| 97 | Assert::same($xml->localName, 'EncryptionMethod', InvalidDOMElementException::class); |
||
| 98 | Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class); |
||
| 99 | |||
| 100 | $keySize = KeySize::getChildrenOfClass($xml); |
||
| 101 | Assert::maxCount($keySize, 1, TooManyElementsException::class); |
||
| 102 | |||
| 103 | $oaepParams = OAEPparams::getChildrenOfClass($xml); |
||
| 104 | Assert::maxCount($oaepParams, 1, TooManyElementsException::class); |
||
| 105 | |||
| 106 | return new static( |
||
| 107 | self::getAttribute($xml, 'Algorithm', AnyURIValue::class), |
||
| 108 | array_pop($keySize), |
||
| 109 | array_pop($oaepParams), |
||
| 110 | self::getChildElementsFromXML($xml), |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * Convert this EncryptionMethod object to XML. |
||
| 117 | * |
||
| 118 | * @param \DOMElement|null $parent The element we should append this EncryptionMethod to. |
||
| 119 | */ |
||
| 120 | public function toXML(?DOMElement $parent = null): DOMElement |
||
| 121 | { |
||
| 122 | $e = $this->instantiateParentElement($parent); |
||
| 123 | $e->setAttribute('Algorithm', strval($this->getAlgorithm())); |
||
| 124 | |||
| 125 | $this->getKeySize()?->toXML($e); |
||
| 126 | $this->getOAEPparams()?->toXML($e); |
||
| 127 | |||
| 128 | foreach ($this->getElements() as $child) { |
||
| 129 | $child->toXML($e); |
||
| 130 | } |
||
| 131 | |||
| 132 | return $e; |
||
| 133 | } |
||
| 134 | } |
||
| 135 |