-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathSecurityUtility.php
More file actions
141 lines (116 loc) · 5.32 KB
/
SecurityUtility.php
File metadata and controls
141 lines (116 loc) · 5.32 KB
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
<?php
class SecurityUtility
{
// namespaces defined by standard
const WSU_NS = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd';
const WSSE_NS = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
const SOAP_NS = 'http://schemas.xmlsoap.org/soap/envelope/';
const DS_NS = 'http://www.w3.org/2000/09/xmldsig#';
public function generateSecurityToken($xmlDom, $certificateFilePath, $keyPass, &$privateKeyId)
{
$certificateInfo = pathinfo($certificateFilePath);
$certificate = file_get_contents($certificateFilePath);
if (in_array(strtolower($certificateInfo['extension']), array('p12', 'pfx')))
{
// for PKCS12 files
openssl_pkcs12_read($certificate, $certs, $keyPass);
$privateKeyId = openssl_pkey_get_private($certs['pkey']);
$pubcert = $certs['cert'];
unset($certs);
}
else
{
// for PEM files
$privateKeyId = openssl_pkey_get_private($certificate);
$tempcert = openssl_x509_read($certificate);
openssl_x509_export($tempcert, $pubcert);
}
// trim
$pubcert = explode("\n", $pubcert);
array_shift($pubcert);
while (!trim(array_pop($pubcert))) { /* Empty while loop */ }
array_walk($pubcert, 'trim');
$pubcert = implode('', $pubcert);
// add public key reference to the token
$tokenElement = $xmlDom->createElementNS(self::WSSE_NS, 'wsse:BinarySecurityToken', $pubcert);
$tokenElement->setAttribute('ValueType', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3');
$tokenElement->setAttribute('EncodingType', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary');
$tokenElement->setAttributeNS(self::WSU_NS, 'wsu:Id', "X509Token");
return $tokenElement;
}
/**
* XML canonicalization
*
* @param string $data
* @return string
*/
function canonicalizeXML($data)
{
$result = '';
$fname = tempnam(sys_get_temp_dir(), 'temporaryBinarySecurityToken');
$f = fopen($fname, 'w+');
fwrite($f, $data);
fclose($f);
$tempFile = new DOMDocument('1.0', 'utf-8');
$tempFile->load($fname);
unlink($fname);
$result = $tempFile->C14N(true, true);
return $result;
}
/**
* Canonicalize DOMNode instance and return result as string
*
* @param DOMNode $domNode
* @return string
*/
function canonicalizeNode($domNode)
{
$domDocument = new DOMDocument('1.0', 'utf-8');
$domDocument->appendChild($domDocument->importNode($domNode, true));
return $this->canonicalizeXML($domDocument->saveXML($domDocument->documentElement));
}
/**
* Prepares SignedInfo DOMElement with required data
*
* @param DOMDocument $domDocument
* @param array $ids
* @return DOMNode
*/
function buildSignedInfo($domDocument, $ids)
{
$domXPath = new DOMXPath($domDocument);
$domXPath->registerNamespace('SOAP-ENV', self::SOAP_NS);
$domXPath->registerNamespace('wsu', self::WSU_NS);
$domXPath->registerNamespace('wsse', self::WSSE_NS);
$domXPath->registerNamespace('ds', self::DS_NS);
$signedInfo = $domDocument->createElementNS(self::DS_NS, 'ds:SignedInfo');
// Canonicalization algorithm
$method = $signedInfo->appendChild($domDocument->createElementNS(self::DS_NS, 'ds:CanonicalizationMethod'));
$method->setAttribute('Algorithm', 'http://www.w3.org/2001/10/xml-exc-c14n#');
// Signature algorithm
$method = $signedInfo->appendChild($domDocument->createElementNS(self::DS_NS, 'ds:SignatureMethod'));
$method->setAttribute('Algorithm', 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256');
foreach ($ids as $id)
{
// find a node and canonicalize it
$nodes = $domXPath->query("//*[(@wsu:Id='$id')]");
if ($nodes->length == 0) { continue; }
$canonicalized = $this->canonicalizeNode($nodes->item(0));
// Create Reference Element
$referenceElement = $signedInfo->appendChild($domDocument->createElementNS(self::DS_NS, 'ds:Reference'));
$referenceElement->setAttribute('URI', "#$id");
// Create Transform Element
$transforms = $referenceElement->appendChild($domDocument->createElementNS(self::DS_NS, 'ds:Transforms'));
$transformElement = $transforms->appendChild($domDocument->createElementNS(self::DS_NS, 'ds:Transform'));
// Mark node as Canonicalized
$transformElement->setAttribute('Algorithm', 'http://www.w3.org/2001/10/xml-exc-c14n#');
// Add a SHA256 digest
$digestValue = hash("sha256", $canonicalized, true);
$method = $referenceElement->appendChild($domDocument->createElementNS(self::DS_NS, 'ds:DigestMethod'));
$method->setAttribute('Algorithm', 'http://www.w3.org/2001/04/xmlenc#sha256');
$referenceElement->appendChild($domDocument->createElementNS(self::DS_NS, 'ds:DigestValue', base64_encode($digestValue)));
}
return $signedInfo;
}
}
?>