Skip to content

Commit

Permalink
Adicionando parâmetro para verificar existência de assinatura
Browse files Browse the repository at this point in the history
  • Loading branch information
gersonfs committed Jul 1, 2024
1 parent 32e23b0 commit 64bcbe1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
28 changes: 23 additions & 5 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 @@ -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
24 changes: 24 additions & 0 deletions tests/SignerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,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

0 comments on commit 64bcbe1

Please sign in to comment.