Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added x509 cert token to be attached to signature and/or encryption #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions src/WSSESoap.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ public function addUserToken($userName, $password = null, $passwordDigest = fals
$token->appendChild($created);
}

public function buildX509Token($cert, $isPEMFormat = true)
{
$data = XMLSecurityDSig::get509XCert($cert, $isPEMFormat);

$token = $this->soapDoc->createElementNS(self::WSSENS, self::WSSEPFX.':KeyIdentifier', $data);

$token->setAttribute('EncodingType', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary');
$token->setAttributeNS(self::WSUNS, self::WSUPFX.':Id', XMLSecurityDSig::generateGUID());
$token->setAttribute('ValueType', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3');

return $token;
}

public function addBinaryToken($cert, $isPEMFormat = true, $isDSig = true)
{
$security = $this->locateSecurityHeader();
Expand All @@ -193,7 +206,7 @@ public function addBinaryToken($cert, $isPEMFormat = true, $isDSig = true)
public function attachTokentoSig($token)
{
if (!($token instanceof DOMElement)) {
throw new Exception('Invalid parameter: BinarySecurityToken element expected');
throw new Exception('Invalid parameter: DOMElement expected');
}
$objXMLSecDSig = new XMLSecurityDSig();
if ($objDSig = $objXMLSecDSig->locateSignature($this->soapDoc)) {
Expand All @@ -209,9 +222,13 @@ public function attachTokentoSig($token)

$tokenRef = $this->soapDoc->createElementNS(self::WSSENS, self::WSSEPFX.':SecurityTokenReference');
$keyInfo->appendChild($tokenRef);
$reference = $this->soapDoc->createElementNS(self::WSSENS, self::WSSEPFX.':Reference');
$reference->setAttribute('ValueType', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3');
$reference->setAttribute('URI', $tokenURI);
if ('KeyIdentifier' == $token->localName) {
$reference = $token;
} else {
$reference = $this->soapDoc->createElementNS(self::WSSENS, self::WSSEPFX.':Reference');
$reference->setAttribute('ValueType', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3');
$reference->setAttribute('URI', $tokenURI);
}
$tokenRef->appendChild($reference);
} else {
throw new Exception('Unable to locate digital signature');
Expand Down Expand Up @@ -337,15 +354,17 @@ public function addEncryptedKey($node, $key, $token, $options = null)
return true;
}

/** @var DOMElement|null $lastToken */
$lastToken = null;
$findTokens = $security->firstChild;
while ($findTokens) {
if ($findTokens->localName == 'BinarySecurityToken') {
if (in_array($findTokens->localName, ['BinarySecurityToken', 'Signature'])) {
$lastToken = $findTokens;
break;
}
$findTokens = $findTokens->nextSibling;
}
if ($lastToken) {
if ('BinarySecurityToken' == $lastToken->localName) {
$lastToken = $lastToken->nextSibling;
}

Expand Down Expand Up @@ -387,9 +406,13 @@ public function addEncryptedKey($node, $key, $token, $options = null)
}
}

$tokenURI = '#'.$token->getAttributeNS(self::WSUNS, 'Id');
$reference = $objDoc->createElementNS(self::WSSENS, self::WSSEPFX.':Reference');
$reference->setAttribute('URI', $tokenURI);
if ('KeyIdentifier' == $token->localName) {
$reference = $token;
} else {
$tokenURI = '#'.$token->getAttributeNS(self::WSUNS, 'Id');
$reference = $objDoc->createElementNS(self::WSSENS, self::WSSEPFX.':Reference');
$reference->setAttribute('URI', $tokenURI);
}
$tokenRef->appendChild($reference);

return true;
Expand Down