-
Notifications
You must be signed in to change notification settings - Fork 20
/
WsSecurityFilterClientServer.php
352 lines (314 loc) · 11.5 KB
/
WsSecurityFilterClientServer.php
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/*
* This file is part of the BeSimpleSoapCommon.
*
* (c) Christian Kerl <[email protected]>
* (c) Francis Besset <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapCommon;
use ass\XmlSecurity\DSig as XmlSecurityDSig;
use ass\XmlSecurity\Enc as XmlSecurityEnc;
use ass\XmlSecurity\Key as XmlSecurityKey;
use ass\XmlSecurity\Pem as XmlSecurityPem;
use BeSimple\SoapCommon\FilterHelper;
use BeSimple\SoapCommon\Helper;
use BeSimple\SoapCommon\WsSecurityKey;
/**
* WS-Security common code for client & server.
*
* @author Andreas Schamberger <[email protected]>
*/
abstract class WsSecurityFilterClientServer
{
/**
* The date format to be used with {@link \DateTime}
*/
const DATETIME_FORMAT = 'Y-m-d\TH:i:s.000\Z';
/**
* (X509 3.2.1) Reference to a Subject Key Identifier
*/
const TOKEN_REFERENCE_SUBJECT_KEY_IDENTIFIER = 0;
/**
* (X509 3.2.1) Reference to a Security Token
*/
const TOKEN_REFERENCE_SECURITY_TOKEN = 1;
/**
* (SMS_1.1 7.3) Key Identifiers
*/
const TOKEN_REFERENCE_THUMBPRINT_SHA1 = 2;
/**
* Actor.
*
* @var string
*/
protected $actor;
/**
* (SMS 10) Add security timestamp.
*
* @var boolean
*/
protected $addTimestamp;
/**
* Encrypt the signature?
*
* @var boolean
*/
protected $encryptSignature;
/**
* (SMS 10) Security timestamp expires time in seconds.
*
* @var int
*/
protected $expires;
/**
* Sign all headers.
*
* @var boolean
*/
protected $signAllHeaders;
/**
* (X509 3.2) Token reference type for encryption.
*
* @var int
*/
protected $tokenReferenceEncryption = null;
/**
* (X509 3.2) Token reference type for signature.
*
* @var int
*/
protected $tokenReferenceSignature = null;
/**
* Service WsSecurityKey.
*
* @var \BeSimple\SoapCommon\WsSecurityKey
*/
protected $serviceSecurityKey;
/**
* User WsSecurityKey.
*
* @var \BeSimple\SoapCommon\WsSecurityKey
*/
protected $userSecurityKey;
/**
* Constructor.
*
* @param boolean $addTimestamp (SMS 10) Add security timestamp.
* @param int $expires (SMS 10) Security timestamp expires time in seconds.
* @param string $actor SOAP actor
*/
public function __construct($addTimestamp = true, $expires = 300, $actor = null)
{
$this->addTimestamp = $addTimestamp;
$this->expires = $expires;
$this->actor = $actor;
}
/**
* Reset all properties to default values.
*/
public function resetFilter()
{
$this->actor = null;
$this->addTimestamp = null;
$this->encryptSignature = null;
$this->expires = null;
$this->serviceSecurityKey = null;
$this->signAllHeaders = null;
$this->tokenReferenceEncryption = null;
$this->tokenReferenceSignature = null;
$this->userSecurityKey = null;
}
/**
* Set service security key.
*
* @param \BeSimple\SoapCommon\WsSecurityKey $serviceSecurityKey Service security key
*
* @return void
*/
public function setServiceSecurityKeyObject(WsSecurityKey $serviceSecurityKey)
{
$this->serviceSecurityKey = $serviceSecurityKey;
}
/**
* Set user security key.
*
* @param \BeSimple\SoapCommon\WsSecurityKey $userSecurityKey User security key
*
* @return void
*/
public function setUserSecurityKeyObject(WsSecurityKey $userSecurityKey)
{
$this->userSecurityKey = $userSecurityKey;
}
/**
* Set security options.
*
* @param int $tokenReference self::TOKEN_REFERENCE_SUBJECT_KEY_IDENTIFIER | self::TOKEN_REFERENCE_SECURITY_TOKEN | self::TOKEN_REFERENCE_THUMBPRINT_SHA1
* @param boolean $encryptSignature Encrypt signature
*
* @return void
*/
public function setSecurityOptionsEncryption($tokenReference, $encryptSignature = false)
{
$this->tokenReferenceEncryption = $tokenReference;
$this->encryptSignature = $encryptSignature;
}
/**
* Set security options.
*
* @param int $tokenReference self::TOKEN_REFERENCE_SUBJECT_KEY_IDENTIFIER | self::TOKEN_REFERENCE_SECURITY_TOKEN | self::TOKEN_REFERENCE_THUMBPRINT_SHA1
* @param boolean $signAllHeaders Sign all headers?
*
* @return void
*/
public function setSecurityOptionsSignature($tokenReference, $signAllHeaders = false)
{
$this->tokenReferenceSignature = $tokenReference;
$this->signAllHeaders = $signAllHeaders;
}
/**
* Adds the configured KeyInfo to the parentNode.
*
* @param FilterHelper $filterHelper Filter helper object
* @param int $tokenReference Token reference type
* @param string $guid Unique ID
* @param \ass\XmlSecurity\Key $xmlSecurityKey XML security key
*
* @return \DOMElement
*/
protected function createKeyInfo(FilterHelper $filterHelper, $tokenReference, $guid, XmlSecurityKey $xmlSecurityKey = null)
{
$keyInfo = $filterHelper->createElement(XmlSecurityDSig::NS_XMLDSIG, 'KeyInfo');
$securityTokenReference = $filterHelper->createElement(Helper::NS_WSS, 'SecurityTokenReference');
$keyInfo->appendChild($securityTokenReference);
// security token
if (self::TOKEN_REFERENCE_SECURITY_TOKEN === $tokenReference) {
$reference = $filterHelper->createElement(Helper::NS_WSS, 'Reference');
$filterHelper->setAttribute($reference, null, 'URI', '#' . $guid);
if (null !== $xmlSecurityKey) {
$filterHelper->setAttribute($reference, null, 'ValueType', Helper::NAME_WSS_X509 . '#X509v3');
}
$securityTokenReference->appendChild($reference);
// subject key identifier
} elseif (self::TOKEN_REFERENCE_SUBJECT_KEY_IDENTIFIER === $tokenReference && null !== $xmlSecurityKey) {
$keyIdentifier = $filterHelper->createElement(Helper::NS_WSS, 'KeyIdentifier');
$filterHelper->setAttribute($keyIdentifier, null, 'EncodingType', Helper::NAME_WSS_SMS . '#Base64Binary');
$filterHelper->setAttribute($keyIdentifier, null, 'ValueType', Helper::NAME_WSS_X509 . '#509SubjectKeyIdentifier');
$securityTokenReference->appendChild($keyIdentifier);
$certificate = $xmlSecurityKey->getX509SubjectKeyIdentifier();
$dataNode = new \DOMText($certificate);
$keyIdentifier->appendChild($dataNode);
// thumbprint sha1
} elseif (self::TOKEN_REFERENCE_THUMBPRINT_SHA1 === $tokenReference && null !== $xmlSecurityKey) {
$keyIdentifier = $filterHelper->createElement(Helper::NS_WSS, 'KeyIdentifier');
$filterHelper->setAttribute($keyIdentifier, null, 'EncodingType', Helper::NAME_WSS_SMS . '#Base64Binary');
$filterHelper->setAttribute($keyIdentifier, null, 'ValueType', Helper::NAME_WSS_SMS_1_1 . '#ThumbprintSHA1');
$securityTokenReference->appendChild($keyIdentifier);
$thumbprintSha1 = base64_encode(sha1(base64_decode($xmlSecurityKey->getX509Certificate(true)), true));
$dataNode = new \DOMText($thumbprintSha1);
$keyIdentifier->appendChild($dataNode);
}
return $keyInfo;
}
/**
* Create a list of \DOMNodes that should be encrypted.
*
* @param \DOMDocument $dom DOMDocument to query
*
* @return \DOMNodeList
*/
protected function createNodeListForEncryption(\DOMDocument $dom)
{
$xpath = new \DOMXPath($dom);
$xpath->registerNamespace('SOAP-ENV', $dom->documentElement->namespaceURI);
$xpath->registerNamespace('ds', XmlSecurityDSig::NS_XMLDSIG);
if ($this->encryptSignature === true) {
$query = '//ds:Signature | //SOAP-ENV:Body';
} else {
$query = '//SOAP-ENV:Body';
}
return $xpath->query($query);
}
/**
* Create a list of \DOMNodes that should be signed.
*
* @param \DOMDocument $dom DOMDocument to query
* @param \DOMElement $security Security element
*
* @return array(\DOMNode)
*/
protected function createNodeListForSigning(\DOMDocument $dom, \DOMElement $security)
{
$nodes = array();
$body = $dom->getElementsByTagNameNS($dom->documentElement->namespaceURI, 'Body')->item(0);
if (null !== $body) {
$nodes[] = $body;
}
foreach ($security->childNodes as $node) {
if (XML_ELEMENT_NODE === $node->nodeType) {
$nodes[] = $node;
}
}
if ($this->signAllHeaders) {
foreach ($security->parentNode->childNodes as $node) {
if (XML_ELEMENT_NODE === $node->nodeType &&
Helper::NS_WSS !== $node->namespaceURI) {
$nodes[] = $node;
}
}
}
return $nodes;
}
/**
* Gets the referenced node for the given URI.
*
* @param \DOMElement $node Node
* @param string $uri URI
*
* @return \DOMElement
*/
protected function getReferenceNodeForUri(\DOMElement $node, $uri)
{
$url = parse_url($uri);
$referenceId = $url['fragment'];
$query = '//*[@'.Helper::PFX_WSU.':Id="'.$referenceId.'" or @Id="'.$referenceId.'"]';
$xpath = new \DOMXPath($node->ownerDocument);
$xpath->registerNamespace(Helper::PFX_WSU, Helper::NS_WSU);
return $xpath->query($query)->item(0);
}
/**
* Tries to resolve a key from the given \DOMElement.
*
* @param \DOMElement $node Node where to resolve the key
* @param string $algorithm XML security key algorithm
*
* @return \ass\XmlSecurity\Key|null
*/
public function keyInfoSecurityTokenReferenceResolver(\DOMElement $node, $algorithm)
{
foreach ($node->childNodes as $key) {
if (Helper::NS_WSS === $key->namespaceURI) {
switch ($key->localName) {
case 'KeyIdentifier':
return $this->serviceSecurityKey->getPublicKey();
case 'Reference':
$uri = $key->getAttribute('URI');
$referencedNode = $this->getReferenceNodeForUri($node, $uri);
if (XmlSecurityEnc::NS_XMLENC === $referencedNode->namespaceURI
&& 'EncryptedKey' == $referencedNode->localName) {
$key = XmlSecurityEnc::decryptEncryptedKey($referencedNode, $this->userSecurityKey->getPrivateKey());
return XmlSecurityKey::factory($algorithm, $key, false, XmlSecurityKey::TYPE_PRIVATE);
} elseif (Helper::NS_WSS === $referencedNode->namespaceURI
&& 'BinarySecurityToken' == $referencedNode->localName) {
$key = XmlSecurityPem::formatKeyInPemFormat($referencedNode->textContent);
return XmlSecurityKey::factory(XmlSecurityKey::RSA_SHA1, $key, false, XmlSecurityKey::TYPE_PUBLIC);
}
}
}
}
return null;
}
}