simplesamlphp /
xml-security
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace SimpleSAML\XMLSecurity\XML\ds; |
||
| 6 | |||
| 7 | use DOMElement; |
||
| 8 | use SimpleSAML\Assert\Assert; |
||
| 9 | use SimpleSAML\XML\ExtendableElementTrait; |
||
| 10 | use SimpleSAML\XML\SchemaValidatableElementInterface; |
||
| 11 | use SimpleSAML\XML\SchemaValidatableElementTrait; |
||
| 12 | use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException; |
||
| 13 | use SimpleSAML\XMLSchema\XML\Constants\NS; |
||
| 14 | use SimpleSAML\XMLSecurity\Constants as C; |
||
| 15 | use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException; |
||
| 16 | use SimpleSAML\XMLSecurity\Exception\ProtocolViolationException; |
||
| 17 | use SimpleSAML\XMLSecurity\XML\dsig11\X509Digest; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Class representing a ds:X509Data element. |
||
| 21 | * |
||
| 22 | * @package simplesamlphp/xml-security |
||
| 23 | */ |
||
| 24 | final class X509Data extends AbstractDsElement implements SchemaValidatableElementInterface |
||
| 25 | { |
||
| 26 | use ExtendableElementTrait; |
||
| 27 | use SchemaValidatableElementTrait; |
||
| 28 | |||
| 29 | |||
| 30 | /** The namespace-attribute for the xs:any element */ |
||
| 31 | public const string XS_ANY_ELT_NAMESPACE = NS::OTHER; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 32 | |||
| 33 | /** The exclusions for the xs:any element */ |
||
| 34 | public const array XS_ANY_ELT_EXCLUSIONS = [ |
||
| 35 | [X509Digest::NS, 'X509Digest'], |
||
| 36 | ]; |
||
| 37 | |||
| 38 | |||
| 39 | /** |
||
| 40 | * Initialize a X509Data. |
||
| 41 | * |
||
| 42 | * @param ( |
||
| 43 | * \SimpleSAML\XMLSecurity\XML\ds\X509Certificate| |
||
| 44 | * \SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial| |
||
| 45 | * \SimpleSAML\XMLSecurity\XML\ds\X509SubjectName| |
||
| 46 | * \SimpleSAML\XMLSecurity\XML\ds\X509SKI| |
||
| 47 | * \SimpleSAML\XMLSecurity\XML\ds\X509CRL| |
||
| 48 | * \SimpleSAML\XMLSecurity\XML\dsig11\X509Digest |
||
| 49 | * )[] $data |
||
| 50 | * @param \SimpleSAML\XML\SerializableElementInterface[] $children |
||
| 51 | */ |
||
| 52 | public function __construct( |
||
| 53 | protected array $data, |
||
| 54 | protected array $children = [], |
||
| 55 | ) { |
||
| 56 | /** |
||
| 57 | * At least one element from the dsig namespaces should be present and |
||
| 58 | * additional elements from an external namespace to accompany/complement them. |
||
| 59 | */ |
||
| 60 | Assert::minCount($data, 1, ProtocolViolationException::class); |
||
| 61 | Assert::maxCount($data, C::UNBOUNDED_LIMIT); |
||
| 62 | Assert::allIsInstanceOfAny( |
||
| 63 | $data, |
||
| 64 | [ |
||
| 65 | X509Certificate::class, |
||
| 66 | X509IssuerSerial::class, |
||
| 67 | X509SubjectName::class, |
||
| 68 | X509Digest::class, |
||
| 69 | X509SKI::class, |
||
| 70 | X509CRL::class, |
||
| 71 | ], |
||
| 72 | InvalidArgumentException::class, |
||
| 73 | ); |
||
| 74 | |||
| 75 | $this->setElements($children); |
||
| 76 | } |
||
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * Collect the value of the data-property |
||
| 81 | * |
||
| 82 | * @return ( |
||
| 83 | * \SimpleSAML\XMLSecurity\XML\ds\X509Certificate| |
||
| 84 | * \SimpleSAML\XMLSecurity\XML\ds\X509IssuerSerial| |
||
| 85 | * \SimpleSAML\XMLSecurity\XML\ds\X509SubjectName| |
||
| 86 | * \SimpleSAML\XMLSecurity\XML\ds\X509SKI| |
||
| 87 | * \SimpleSAML\XMLSecurity\XML\ds\X509CRL| |
||
| 88 | * \SimpleSAML\XMLSecurity\XML\dsig11\X509Digest |
||
| 89 | * )[] |
||
| 90 | */ |
||
| 91 | public function getData(): array |
||
| 92 | { |
||
| 93 | return $this->data; |
||
| 94 | } |
||
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * Convert XML into a X509Data |
||
| 99 | * |
||
| 100 | * @param \DOMElement $xml The XML element we should load |
||
| 101 | * |
||
| 102 | * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException |
||
| 103 | * If the qualified name of the supplied element is wrong |
||
| 104 | */ |
||
| 105 | public static function fromXML(DOMElement $xml): static |
||
| 106 | { |
||
| 107 | Assert::same($xml->localName, 'X509Data', InvalidDOMElementException::class); |
||
| 108 | Assert::same($xml->namespaceURI, X509Data::NS, InvalidDOMElementException::class); |
||
| 109 | |||
| 110 | $x509Certificate = X509Certificate::getChildrenOfClass($xml); |
||
| 111 | $x509IssuerSerial = X509IssuerSerial::getChildrenOfClass($xml); |
||
| 112 | $x509SubjectName = X509SubjectName::getChildrenOfClass($xml); |
||
| 113 | $x509SKI = X509SKI::getChildrenOfClass($xml); |
||
| 114 | $x509CRL = X509CRL::getChildrenOfClass($xml); |
||
| 115 | $x509Digest = X509Digest::getChildrenOfClass($xml); |
||
| 116 | |||
| 117 | $data = array_merge($x509Certificate, $x509IssuerSerial, $x509SubjectName, $x509SKI, $x509CRL, $x509Digest); |
||
| 118 | $children = self::getChildElementsFromXML($xml); |
||
| 119 | |||
| 120 | return new static($data, $children); |
||
| 121 | } |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * Convert this X509Data element to XML. |
||
| 126 | * |
||
| 127 | * @param \DOMElement|null $parent The element we should append this X509Data element to. |
||
| 128 | */ |
||
| 129 | public function toXML(?DOMElement $parent = null): DOMElement |
||
| 130 | { |
||
| 131 | $e = $this->instantiateParentElement($parent); |
||
| 132 | |||
| 133 | foreach ($this->getData() as $d) { |
||
| 134 | $d->toXML($e); |
||
| 135 | } |
||
| 136 | |||
| 137 | foreach ($this->getElements() as $c) { |
||
| 138 | $c->toXML($e); |
||
| 139 | } |
||
| 140 | |||
| 141 | return $e; |
||
| 142 | } |
||
| 143 | } |
||
| 144 |