|
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\XmlDSig; |
|
13
|
|
|
|
|
14
|
|
|
use LightSaml\Credential\X509Certificate; |
|
15
|
|
|
use LightSaml\Meta\SigningOptions; |
|
16
|
|
|
use LightSaml\Model\Context\DeserializationContext; |
|
17
|
|
|
use LightSaml\Model\Context\SerializationContext; |
|
18
|
|
|
use LightSaml\SamlConstants; |
|
19
|
|
|
use RobRichards\XMLSecLibs\XMLSecurityDSig; |
|
20
|
|
|
use RobRichards\XMLSecLibs\XMLSecurityKey; |
|
21
|
|
|
|
|
22
|
|
|
class SignatureWriter extends Signature |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
protected $canonicalMethod = XMLSecurityDSig::EXC_C14N; |
|
26
|
|
|
|
|
27
|
|
|
/** @var XMLSecurityKey */ |
|
28
|
|
|
protected $xmlSecurityKey; |
|
29
|
|
|
|
|
30
|
|
|
/** @var X509Certificate */ |
|
31
|
|
|
protected $certificate; |
|
32
|
|
|
|
|
33
|
|
|
protected $digestAlgorithm = XMLSecurityDSig::SHA1; |
|
34
|
|
|
|
|
35
|
|
|
/** @var SigningOptions */ |
|
36
|
|
|
protected $signingOptions; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return SignatureWriter |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function create(SigningOptions $options) |
|
42
|
|
|
{ |
|
43
|
|
|
$writer = new self($options->getCertificate(), $options->getPrivateKey()); |
|
44
|
|
|
$writer->signingOptions = $options; |
|
45
|
|
|
|
|
46
|
|
|
return $writer; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return SignatureWriter |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function createByKeyAndCertificate(X509Certificate $certificate, XMLSecurityKey $xmlSecurityKey) |
|
53
|
|
|
{ |
|
54
|
|
|
$signingOptions = new SigningOptions($xmlSecurityKey, $certificate); |
|
55
|
|
|
|
|
56
|
|
|
return self::create($signingOptions); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $digestAlgorithm |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct(X509Certificate $certificate = null, XMLSecurityKey $xmlSecurityKey = null, $digestAlgorithm = XMLSecurityDSig::SHA1) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->certificate = $certificate; |
|
65
|
|
|
$this->xmlSecurityKey = $xmlSecurityKey; |
|
66
|
|
|
$this->digestAlgorithm = $digestAlgorithm; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getDigestAlgorithm() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->digestAlgorithm; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $digestAlgorithm |
|
79
|
|
|
* |
|
80
|
|
|
* @return SignatureWriter |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setDigestAlgorithm($digestAlgorithm) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->digestAlgorithm = $digestAlgorithm; |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return SigningOptions |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getSigningOptions() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->signingOptions; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return SignatureWriter |
|
99
|
|
|
*/ |
|
100
|
|
|
public function setSigningOptions(SigningOptions $signingOptions) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->signingOptions = $signingOptions; |
|
103
|
|
|
|
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getCanonicalMethod() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->canonicalMethod; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param string $canonicalMethod |
|
117
|
|
|
* |
|
118
|
|
|
* @return SignatureWriter |
|
119
|
|
|
*/ |
|
120
|
|
|
public function setCanonicalMethod($canonicalMethod) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->canonicalMethod = $canonicalMethod; |
|
123
|
|
|
|
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return SignatureWriter |
|
129
|
|
|
*/ |
|
130
|
|
|
public function setXmlSecurityKey(XMLSecurityKey $key) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->xmlSecurityKey = $key; |
|
133
|
|
|
|
|
134
|
|
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return XMLSecurityKey |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getXmlSecurityKey() |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->xmlSecurityKey; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return SignatureWriter |
|
147
|
|
|
*/ |
|
148
|
|
|
public function setCertificate(X509Certificate $certificate) |
|
149
|
|
|
{ |
|
150
|
|
|
$this->certificate = $certificate; |
|
151
|
|
|
|
|
152
|
|
|
return $this; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return X509Certificate |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getCertificate() |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->certificate; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function serialize(\DOMNode $parent, SerializationContext $context) |
|
164
|
|
|
{ |
|
165
|
|
|
if ($this->signingOptions && false === $this->signingOptions->isEnabled()) { |
|
166
|
|
|
return; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$objXMLSecDSig = new XMLSecurityDSig(); |
|
170
|
|
|
$objXMLSecDSig->setCanonicalMethod($this->getCanonicalMethod()); |
|
171
|
|
|
$key = $this->getXmlSecurityKey(); |
|
172
|
|
|
|
|
173
|
|
|
$objXMLSecDSig->addReferenceList( |
|
174
|
|
|
[$parent], |
|
175
|
|
|
$this->digestAlgorithm, |
|
176
|
|
|
[SamlConstants::XMLSEC_TRANSFORM_ALGORITHM_ENVELOPED_SIGNATURE, XMLSecurityDSig::EXC_C14N], |
|
177
|
|
|
['id_name' => $this->getIDName(), 'overwrite' => false] |
|
178
|
|
|
); |
|
179
|
|
|
|
|
180
|
|
|
$objXMLSecDSig->sign($key); |
|
181
|
|
|
|
|
182
|
|
|
$objXMLSecDSig->add509Cert( |
|
183
|
|
|
$this->getCertificate()->getData(), |
|
184
|
|
|
false, |
|
185
|
|
|
false, |
|
186
|
|
|
$this->signingOptions ? $this->signingOptions->getCertificateOptions()->all() : null |
|
187
|
|
|
); |
|
188
|
|
|
|
|
189
|
|
|
$firstChild = $parent->hasChildNodes() ? $parent->firstChild : null; |
|
190
|
|
|
if ($firstChild && 'Issuer' == $firstChild->localName) { |
|
191
|
|
|
// The signature node should come after the issuer node |
|
192
|
|
|
$firstChild = $firstChild->nextSibling; |
|
193
|
|
|
} |
|
194
|
|
|
$objXMLSecDSig->insertSignature($parent, $firstChild); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
public function deserialize(\DOMNode $node, DeserializationContext $context) |
|
198
|
|
|
{ |
|
199
|
|
|
throw new \LogicException('SignatureWriter can not be deserialized'); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|