1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the LightSAML-Core package. |
5
|
|
|
* |
6
|
|
|
* (c) Milos Tomic <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LightSaml\Model\Metadata; |
13
|
|
|
|
14
|
|
|
use LightSaml\Error\LightSamlXmlException; |
15
|
|
|
use LightSaml\Model\Context\DeserializationContext; |
16
|
|
|
use LightSaml\Model\Context\SerializationContext; |
17
|
|
|
use LightSaml\Model\AbstractSamlModel; |
18
|
|
|
use LightSaml\SamlConstants; |
19
|
|
|
use LightSaml\Credential\X509Certificate; |
20
|
|
|
|
21
|
|
|
class KeyDescriptor extends AbstractSamlModel |
22
|
|
|
{ |
23
|
|
|
const USE_SIGNING = 'signing'; |
24
|
|
|
const USE_ENCRYPTION = 'encryption'; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
protected $use; |
28
|
|
|
|
29
|
|
|
/** @var X509Certificate */ |
30
|
|
|
private $certificate; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string|null $use |
34
|
|
|
* @param X509Certificate|null $certificate |
35
|
|
|
*/ |
36
|
44 |
|
public function __construct($use = null, X509Certificate $certificate = null) |
37
|
|
|
{ |
38
|
44 |
|
$this->use = $use; |
39
|
44 |
|
$this->certificate = $certificate; |
40
|
44 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $use |
44
|
|
|
* |
45
|
|
|
* @return KeyDescriptor |
46
|
|
|
* |
47
|
|
|
* @throws \InvalidArgumentException |
48
|
|
|
*/ |
49
|
33 |
|
public function setUse($use) |
50
|
|
|
{ |
51
|
33 |
|
$use = trim($use); |
52
|
33 |
|
if (false != $use && self::USE_ENCRYPTION != $use && self::USE_SIGNING != $use) { |
|
|
|
|
53
|
|
|
throw new \InvalidArgumentException(sprintf("Invalid use value '%s'", $use)); |
54
|
|
|
} |
55
|
33 |
|
$this->use = $use; |
56
|
|
|
|
57
|
33 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
13 |
|
public function getUse() |
64
|
|
|
{ |
65
|
13 |
|
return $this->use; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param X509Certificate $certificate |
70
|
|
|
* |
71
|
|
|
* @return KeyDescriptor |
72
|
|
|
*/ |
73
|
5 |
|
public function setCertificate(X509Certificate $certificate) |
74
|
|
|
{ |
75
|
5 |
|
$this->certificate = $certificate; |
76
|
|
|
|
77
|
5 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return X509Certificate |
82
|
|
|
*/ |
83
|
14 |
|
public function getCertificate() |
84
|
|
|
{ |
85
|
14 |
|
return $this->certificate; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param \DOMNode $parent |
90
|
|
|
* @param SerializationContext $context |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
3 |
|
public function serialize(\DOMNode $parent, SerializationContext $context) |
95
|
|
|
{ |
96
|
3 |
|
$result = $this->createElement('KeyDescriptor', SamlConstants::NS_METADATA, $parent, $context); |
97
|
|
|
|
98
|
3 |
|
$this->attributesToXml(array('use'), $result); |
99
|
|
|
|
100
|
3 |
|
$keyInfo = $this->createElement('ds:KeyInfo', SamlConstants::NS_XMLDSIG, $result, $context); |
101
|
3 |
|
$xData = $this->createElement('ds:X509Data', SamlConstants::NS_XMLDSIG, $keyInfo, $context); |
102
|
3 |
|
$xCert = $this->createElement('ds:X509Certificate', SamlConstants::NS_XMLDSIG, $xData, $context); |
103
|
|
|
|
104
|
3 |
|
$xCert->nodeValue = $this->getCertificate()->getData(); |
105
|
3 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param \DOMNode $node |
109
|
|
|
* @param DeserializationContext $context |
110
|
|
|
*/ |
111
|
40 |
|
public function deserialize(\DOMNode $node, DeserializationContext $context) |
112
|
|
|
{ |
113
|
40 |
|
$this->checkXmlNodeName($node, 'KeyDescriptor', SamlConstants::NS_METADATA); |
114
|
|
|
|
115
|
40 |
|
$this->attributesFromXml($node, array('use')); |
|
|
|
|
116
|
|
|
|
117
|
40 |
|
$list = $context->getXpath()->query('./ds:KeyInfo/ds:X509Data/ds:X509Certificate', $node); |
118
|
40 |
|
if (1 != $list->length) { |
119
|
|
|
throw new LightSamlXmlException('Missing X509Certificate node'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** @var $x509CertificateNode \DOMElement */ |
123
|
40 |
|
$x509CertificateNode = $list->item(0); |
124
|
40 |
|
$certificateData = trim($x509CertificateNode->textContent); |
125
|
40 |
|
if (false == $certificateData) { |
|
|
|
|
126
|
|
|
throw new LightSamlXmlException('Missing certificate data'); |
127
|
|
|
} |
128
|
|
|
|
129
|
40 |
|
$this->certificate = new X509Certificate(); |
130
|
40 |
|
$this->certificate->setData($certificateData); |
131
|
40 |
|
} |
132
|
|
|
} |
133
|
|
|
|