summaryrefslogtreecommitdiffstats
path: root/lib/SAML2/XML/ds/KeyInfo.php
blob: 44b4b0d0ed3af3e48392a45d0066400a1387388a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php

/**
 * Class representing a ds:KeyInfo element.
 *
 * @package simpleSAMLphp
 * @version $Id$
 */
class SAML2_XML_ds_KeyInfo {

	/**
	 * The Id attribute on this element.
	 *
	 * @var string|NULL
	 */
	public $Id = NULL;


	/**
	 * The various key information elements.
	 *
	 * Array with various elements describing this key.
	 * Unknown elements will be represented by SAML2_XML_Chunk.
	 *
	 * @var array
	 */
	public $info = array();


	/**
	 * Initialize a KeyInfo element.
	 *
	 * @param DOMElement|NULL $xml  The XML element we should load.
	 */
	public function __construct(DOMElement $xml = NULL) {

		if ($xml === NULL) {
			return;
		}

		if ($xml->hasAttribute('Id')) {
			$this->Id = $xml->getAttribute('Id');
		}

		for ($n = $xml->firstChild; $n !== NULL; $n = $n->nextSibling) {
			if (!($n instanceof DOMElement)) {
				continue;
			}

			if ($n->namespaceURI !== XMLSecurityDSig::XMLDSIGNS) {
				$this->info[] = new SAML2_XML_Chunk($n);
				continue;
			}
			switch ($n->localName) {
			case 'KeyName':
				$this->info[] = new SAML2_XML_ds_KeyName($n);
				break;
			case 'X509Data':
				$this->info[] = new SAML2_XML_ds_X509Data($n);
				break;
			default:
				$this->info[] = new SAML2_XML_Chunk($n);
				break;
			}
		}
	}


	/**
	 * Convert this KeyInfo to XML.
	 *
	 * @param DOMElement $parent  The element we should append this KeyInfo to.
	 */
	public function toXML(DOMElement $parent) {
		assert('is_null($this->Id) || is_string($this->Id)');
		assert('is_array($this->info)');

		$doc = $parent->ownerDocument;

		$e = $doc->createElementNS(XMLSecurityDSig::XMLDSIGNS, 'ds:KeyInfo');
		$parent->appendChild($e);

		if (isset($this->Id)) {
			$e->setAttribute('Id', $this->Id);
		}

		foreach ($this->info as $n) {
			$n->toXML($e);
		}

		return $e;
	}

}