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

Adicionando parâmetro para verificar existência de assinatura dentro de determinada tag #305

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
extensions: mbstring, intl, mysql, zlib, dom, openssl, soap, json, simplexml, libxml

- name: checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Composer Install
run: |
Expand Down
32 changes: 26 additions & 6 deletions src/Signer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

class Signer
{
const CANONICAL = [true,false,null,null];
const CANONICAL = [true, false, null, null];

/**
* Make Signature tag
Expand Down Expand Up @@ -74,7 +74,7 @@ public static function sign(
if (empty($node) || empty($root)) {
throw SignerException::tagNotFound($tagname);
}
if (!self::existsSignature($content)) {
if (!self::existsSignature($content, $rootname)) {
$dom = self::createSignature(
$certificate,
$dom,
Expand Down Expand Up @@ -174,6 +174,7 @@ private static function createSignature(
$x509DataNode->appendChild($x509CertificateNode);
return $dom;
}

/**
* Remove old signature from document to replace it
* @param string $content
Expand All @@ -196,6 +197,7 @@ public static function removeSignature($content)
}
return $dom->saveXML();
}

/**
* Verify if xml signature is valid
* @param string $content
Expand All @@ -218,9 +220,10 @@ public static function isSigned($content, $tagname = '', $canonical = self::CANO
/**
* Check if Signature tag already exists
* @param string $content
* @param string|null $rootname
* @return boolean
*/
public static function existsSignature($content)
public static function existsSignature($content, $rootname = null)
{
if (!Validator::isXML($content)) {
throw SignerException::isNotXml();
Expand All @@ -229,8 +232,23 @@ public static function existsSignature($content)
$dom->formatOutput = false;
$dom->preserveWhiteSpace = false;
$dom->loadXML($content);
$signature = $dom->getElementsByTagName('Signature')->item(0);
return !empty($signature);

if (empty($rootname)) {
return !empty($dom->getElementsByTagName('Signature')->item(0));
}

$root = $dom->documentElement->getElementsByTagName($rootname)->item(0);
if ($dom->documentElement->tagName == $rootname) {
$root = $dom->documentElement;
}

foreach ($root->childNodes as $child) {
if ($child->nodeName == 'Signature') {
return true;
}
}

return false;
}

/**
Expand All @@ -256,7 +274,7 @@ public static function signatureCheck($xml, $canonical = self::CANONICAL)
$publicKey = PublicKey::createFromContent($certificateContent);
$signInfoNode = self::canonize($signature->getElementsByTagName('SignedInfo')->item(0), $canonical);
$signatureValue = $signature->getElementsByTagName('SignatureValue')->item(0)->nodeValue;
$decodedSignature = base64_decode(str_replace(array("\r", "\n"), '', $signatureValue));
$decodedSignature = base64_decode(str_replace(["\r", "\n"], '', $signatureValue));
if (!$publicKey->verify($signInfoNode, $decodedSignature, $algorithm)) {
throw SignerException::signatureComparisonFailed();
}
Expand Down Expand Up @@ -285,7 +303,9 @@ public static function digestCheck($xml, $tagname = '', $canonical = self::CANON
$tagname = $root->nodeName;
} else {
$xpath = new \DOMXPath($dom);
/** @var \DOMNodeList $entries */
$entries = $xpath->query('//@Id');
/** @var \DOMAttr $entry */
foreach ($entries as $entry) {
$tagname = $entry->ownerElement->nodeName;
break;
Expand Down
34 changes: 34 additions & 0 deletions tests/SignerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ public function testSign()
$xmlsign = Signer::sign($certificate, $content, 'infNFe', 'Id');
$actual = Signer::isSigned($xmlsign);
$this->assertTrue($actual);

$xmlsign = Signer::sign($certificate, '<a><b><c>Teste Assinar mesmo documento 2x</c></b></a>', 'b', 'Id', OPENSSL_ALGO_SHA1, Signer::CANONICAL, 'b');
$this->assertTrue(Signer::existsSignature($xmlsign, 'b'));

$xmlsign2x = Signer::sign($certificate, $xmlsign, 'b', 'Id', OPENSSL_ALGO_SHA1, Signer::CANONICAL, 'a');
$this->assertTrue(Signer::existsSignature($xmlsign2x, 'a'));

$dom = new \DOMDocument('1.0', 'utf-8');
$dom->loadXML($xmlsign2x);
$this->assertEquals(2, $dom->getElementsByTagName('Signature')->count());
}

/**
Expand Down Expand Up @@ -79,6 +89,30 @@ public function testIsSignedFailDigest()
Signer::isSigned($xml);
}

public function testExistsSignatureRootnode(): void
{
$content = '<a><b><c></c><Signature></Signature></b></a>';
$this->assertTrue(Signer::existsSignature($content));

$content = '<a><b><c></c></b><Signature></Signature></a>';
$this->assertTrue(Signer::existsSignature($content));

$content = '<a><b><c></c><Signature></Signature></b></a>';
$this->assertFalse(Signer::existsSignature($content, 'a'));

$content = '<a><b><c></c></b><Signature></Signature></a>';
$this->assertTrue(Signer::existsSignature($content, 'a'));

$content = '<a><b><c></c><Signature></Signature></b></a>';
$this->assertFalse(Signer::existsSignature($content, 'c'));

$content = '<a><b><c><Signature></Signature></c></b></a>';
$this->assertTrue(Signer::existsSignature($content, 'c'));

$content = '<a><b><c></c><Signature></Signature></b></a>';
$this->assertTrue(Signer::existsSignature($content, 'b'));
}

/**
* @covers Signer::existsSignature
* @covers Signer::digestCheck
Expand Down