Skip to content

Commit

Permalink
Revert changes
Browse files Browse the repository at this point in the history
  • Loading branch information
odan committed May 22, 2021
1 parent 23f4794 commit a613cf7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 169 deletions.
21 changes: 0 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

* Sign XML Documents with Digital Signatures ([XMLDSIG](https://www.w3.org/TR/xmldsig-core/))
* Verify the Digital Signatures of XML Documents
* Sign SOAP documents

## Requirements

Expand Down Expand Up @@ -121,26 +120,6 @@ if ($isValid) {
}
```

### Sign SOAP messages

```php
use Selective\XmlDSig\DigestAlgorithmType;
use Selective\XmlDSig\XmlSigner;

$xmlSigner = new XmlSigner();

// load pfx
$xmlSigner->loadPfx('pfx content', 'password');

// optional
$xmlSigner->setReferenceUri('');

// Define signature target
$xmlSigner->setSignatureXPath('/SOAP-ENV:Envelope/SOAP-ENV:Body/MyTargetElement');

$xmlSigner->signXml('the soap message xml', DigestAlgorithmType::SHA512);
```

### Online XML Digital Signature Verifier

Try these excellent online tools to verify XML signatures:
Expand Down
16 changes: 0 additions & 16 deletions src/XmlReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,4 @@ public function queryDomNode(DOMXPath $xpath, string $expression, DOMNode $conte

return $item;
}

/**
* Add all namespaces automatically.
*
* @param DOMXPath $xpath The xpath
*
* @return void
*/
public function registerAllNamespaces(DOMXPath $xpath)
{
foreach ($xpath->query('//namespace::*') ?: [] as $namespaceNode) {
$prefix = str_replace('xmlns:', '', $namespaceNode->nodeName);
$namespaceUri = $namespaceNode->nodeValue;
$xpath->registerNamespace($prefix, $namespaceUri);
}
}
}
7 changes: 4 additions & 3 deletions src/XmlSignatureValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function verifyXml(string $xmlContent): bool
$digestAlgorithm = $this->getDigestAlgorithm($xml);
$signatureValue = $this->getSignatureValue($xml);
$xpath = new DOMXPath($xml);
$this->xmlReader->registerAllNamespaces($xpath);
$xpath->registerNamespace('xmlns', 'http://www.w3.org/2000/09/xmldsig#');

/** @var DOMElement $signedInfoNode */
foreach ($xpath->evaluate('//xmlns:Signature/xmlns:SignedInfo') as $signedInfoNode) {
Expand Down Expand Up @@ -250,7 +250,8 @@ public function verifyXml(string $xmlContent): bool
private function getDigestAlgorithm(DOMDocument $xml): int
{
$xpath = new DOMXPath($xml);
$this->xmlReader->registerAllNamespaces($xpath);
$xpath->registerNamespace('xmlns', 'http://www.w3.org/2000/09/xmldsig#');
$xpath->registerNamespace('Algorithm', 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315');

$signatureMethodNodes = $xpath->query('//xmlns:Signature/xmlns:SignedInfo/xmlns:SignatureMethod');

Expand Down Expand Up @@ -305,7 +306,7 @@ private function getDigestAlgorithm(DOMDocument $xml): int
private function getSignatureValue(DOMDocument $xml): string
{
$xpath = new DOMXPath($xml);
$this->xmlReader->registerAllNamespaces($xpath);
$xpath->registerNamespace('xmlns', 'http://www.w3.org/2000/09/xmldsig#');

// Find the SignatureValue node
$signatureNodes = $xpath->query('//xmlns:Signature/xmlns:SignatureValue');
Expand Down
59 changes: 3 additions & 56 deletions src/XmlSigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Selective\XmlDSig;

use DOMDocument;
use DOMElement;
use DOMXPath;
use Selective\XmlDSig\Exception\XmlSignatureValidatorException;
use Selective\XmlDSig\Exception\XmlSignerException;
Expand Down Expand Up @@ -64,11 +63,6 @@ final class XmlSigner
*/
private $referenceUri = '';

/**
* @var string|null
*/
private $signatureXpath = null;

/**
* @var string
*/
Expand Down Expand Up @@ -368,7 +362,7 @@ private function setAlgorithm(string $algorithm): void
private function appendSignature(DOMDocument $xml, string $digestValue)
{
$signatureElement = $xml->createElement('Signature');
$signatureElement->setAttribute('xmlns:dsig', 'http://www.w3.org/2000/09/xmldsig#');
$signatureElement->setAttribute('xmlns', 'http://www.w3.org/2000/09/xmldsig#');

// Append the element to the XML document.
// We insert the new element as root (child of the document)
Expand All @@ -377,10 +371,7 @@ private function appendSignature(DOMDocument $xml, string $digestValue)
throw new UnexpectedValueException('Undefined document element');
}

$xpath = new DOMXpath($xml);
$this->xmlReader->registerAllNamespaces($xpath);

$this->addSignatureElement($xml->documentElement, $xpath, $signatureElement);
$xml->documentElement->appendChild($signatureElement);

$signedInfoElement = $xml->createElement('SignedInfo');
$signatureElement->appendChild($signedInfoElement);
Expand Down Expand Up @@ -444,6 +435,7 @@ private function appendSignature(DOMDocument $xml, string $digestValue)
throw new XmlSignerException('Computing of the signature failed');
}

$xpath = new DOMXpath($xml);
$signatureValueElement = $this->xmlReader->queryDomNode($xpath, '//SignatureValue', $signatureElement);
$signatureValueElement->nodeValue = base64_encode($signatureValue);
}
Expand All @@ -460,51 +452,6 @@ public function setReferenceUri(string $referenceUri)
$this->referenceUri = $referenceUri;
}

/**
* Set reference URI.
*
* @param string $xpath The signature destination xpath
*
* @return void
*/
public function setSignatureXPath(string $xpath)
{
$this->signatureXpath = $xpath;
}

/**
* Add signature element to DOM object.
*
* @param DOMElement $xml The dom
* @param DOMXPath $xpath The xpath
* @param DOMElement $signatureElement The signature element
*
* @throws XmlSignerException
*
* @return void
*/
private function addSignatureElement(DOMElement $xml, DOMXPath $xpath, DOMElement $signatureElement)
{
if (!$this->signatureXpath) {
$xml->appendChild($signatureElement);

return;
}

$element = $xpath->query($this->signatureXpath);

if (!$element || !$element->length) {
throw new XmlSignerException(sprintf('XML element for signature not found: %s', $this->signatureXpath));
}

$item = $element->item(0);
if ($item === null) {
throw new XmlSignerException(sprintf('XML element for signature not found: %s', $this->signatureXpath));
}

$item->appendChild($signatureElement);
}

/**
* Destructor.
*/
Expand Down
80 changes: 7 additions & 73 deletions tests/XmlSignatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@
*/
class XmlSignatureTest extends TestCase
{
/**
* @var array
*/
private $algos = [
DigestAlgorithmType::SHA1,
DigestAlgorithmType::SHA224,
DigestAlgorithmType::SHA256,
DigestAlgorithmType::SHA384,
DigestAlgorithmType::SHA512,
];

/**
* Test create object.
*
Expand Down Expand Up @@ -57,72 +46,18 @@ public function testSignAndVerify(string $privateKeyFile, string $publicKeyFile,

$outputFilename = __DIR__ . '/signed-example.xml';

foreach ($files as $filename) {
$this->assertFileExists($filename);

foreach ($this->algos as $algo) {
if (file_exists($outputFilename)) {
unlink($outputFilename);
}

if (method_exists($this, 'assertFileDoesNotExist')) {
$this->assertFileDoesNotExist($outputFilename);
} else {
$this->assertFileNotExists($outputFilename);
}

$signedXml = new XmlSigner();

if (pathinfo($privateKeyFile, PATHINFO_EXTENSION) === 'pfx') {
$signedXml->loadPfxFile($privateKeyFile, $password);
} else {
$signedXml->loadPrivateKeyFile($privateKeyFile, $password);
}

$signedXml->setReferenceUri('');
$signedXml->signXmlFile($filename, $outputFilename, $algo);

$this->assertFileExists($outputFilename);

// verify
$verifyXml = new XmlSignatureValidator();

if (pathinfo($publicKeyFile, PATHINFO_EXTENSION) === 'pfx') {
$verifyXml->loadPfxFile($publicKeyFile, $password);
} else {
$verifyXml->loadPublicKeyFile($publicKeyFile);
}

$isValid = $verifyXml->verifyXmlFile($outputFilename);

$this->assertTrue($isValid);
}
}
}

/**
* Test.
*
* @dataProvider providerTestSignAndVerify
*
* @param string $privateKeyFile The key file
* @param string $publicKeyFile The key file
* @param string $password The file password
*
* @return void
*/
public function testSignAndVerifySoap(string $privateKeyFile, string $publicKeyFile, string $password)
{
$files = [
__DIR__ . '/example-soap.xml',
$algos = [
DigestAlgorithmType::SHA1,
DigestAlgorithmType::SHA224,
DigestAlgorithmType::SHA256,
DigestAlgorithmType::SHA384,
DigestAlgorithmType::SHA512,
];

$outputFilename = __DIR__ . '/signed-example.xml';

foreach ($files as $filename) {
$this->assertFileExists($filename);

foreach ($this->algos as $algo) {
foreach ($algos as $algo) {
if (file_exists($outputFilename)) {
unlink($outputFilename);
}
Expand All @@ -142,7 +77,6 @@ public function testSignAndVerifySoap(string $privateKeyFile, string $publicKeyF
}

$signedXml->setReferenceUri('');
$signedXml->setSignatureXPath('/SOAP-ENV:Envelope/SOAP-ENV:Body/xmlns:RegisterTCRRequest');
$signedXml->signXmlFile($filename, $outputFilename, $algo);

$this->assertFileExists($outputFilename);
Expand Down

0 comments on commit a613cf7

Please sign in to comment.