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 classe para assinar o mesmo xml várias vezes em tags diferentes. #307

Closed
wants to merge 4 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
28 changes: 14 additions & 14 deletions src/Signer.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public static function sign(
if (empty($node) || empty($root)) {
throw SignerException::tagNotFound($tagname);
}
if (!self::existsSignature($content)) {
$dom = self::createSignature(
if (!static::existsSignature($content)) {
$dom = static::createSignature(
$certificate,
$dom,
$root,
Expand All @@ -100,7 +100,7 @@ public static function sign(
* @param array $canonical parameters to format node for signature (opcional)
* @return \DOMDocument
*/
private static function createSignature(
protected static function createSignature(
Certificate $certificate,
DOMDocument $dom,
DOMNode $root,
Expand All @@ -123,7 +123,7 @@ private static function createSignature(
$nsTransformMethod1 = 'http://www.w3.org/2000/09/xmldsig#enveloped-signature';
$nsTransformMethod2 = 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315';
$idSigned = trim($node->getAttribute($mark));
$digestValue = self::makeDigest($node, $digestAlgorithm, $canonical);
$digestValue = static::makeDigest($node, $digestAlgorithm, $canonical);
$signatureNode = $dom->createElementNS($nsDSIG, 'Signature');
$root->appendChild($signatureNode);
$signedInfoNode = $dom->createElement('SignedInfo');
Expand Down Expand Up @@ -153,7 +153,7 @@ private static function createSignature(
$digestMethodNode->setAttribute('Algorithm', $nsDigestMethod);
$digestValueNode = $dom->createElement('DigestValue', $digestValue);
$referenceNode->appendChild($digestValueNode);
$c14n = self::canonize($signedInfoNode, $canonical);
$c14n = static::canonize($signedInfoNode, $canonical);
$signature = $certificate->sign($c14n, $algorithm);
$signatureValue = base64_encode($signature);
$signatureValueNode = $dom->createElement('SignatureValue', $signatureValue);
Expand Down Expand Up @@ -181,7 +181,7 @@ private static function createSignature(
*/
public static function removeSignature($content)
{
if (!self::existsSignature($content)) {
if (!static::existsSignature($content)) {
return $content;
}
$dom = new \DOMDocument('1.0', 'utf-8');
Expand All @@ -206,13 +206,13 @@ public static function removeSignature($content)
*/
public static function isSigned($content, $tagname = '', $canonical = self::CANONICAL)
{
if (!self::existsSignature($content)) {
if (!static::existsSignature($content)) {
return false;
}
if (!self::digestCheck($content, $tagname, $canonical)) {
if (!static::digestCheck($content, $tagname, $canonical)) {
return false;
}
return self::signatureCheck($content, $canonical);
return static::signatureCheck($content, $canonical);
}

/**
Expand Down Expand Up @@ -254,7 +254,7 @@ public static function signatureCheck($xml, $canonical = self::CANONICAL)
}
$certificateContent = $signature->getElementsByTagName('X509Certificate')->item(0)->nodeValue;
$publicKey = PublicKey::createFromContent($certificateContent);
$signInfoNode = self::canonize($signature->getElementsByTagName('SignedInfo')->item(0), $canonical);
$signInfoNode = static::canonize($signature->getElementsByTagName('SignedInfo')->item(0), $canonical);
$signatureValue = $signature->getElementsByTagName('SignatureValue')->item(0)->nodeValue;
$decodedSignature = base64_decode(str_replace(array("\r", "\n"), '', $signatureValue));
if (!$publicKey->verify($signInfoNode, $decodedSignature, $algorithm)) {
Expand Down Expand Up @@ -305,7 +305,7 @@ public static function digestCheck($xml, $tagname = '', $canonical = self::CANON
if ($sigURI == '') {
$node->removeChild($signature);
}
$calculatedDigest = self::makeDigest($node, $algorithm, $canonical);
$calculatedDigest = static::makeDigest($node, $algorithm, $canonical);
$informedDigest = $signature->getElementsByTagName('DigestValue')->item(0)->nodeValue;
if ($calculatedDigest != $informedDigest) {
throw SignerException::digestComparisonFailed();
Expand All @@ -320,10 +320,10 @@ public static function digestCheck($xml, $tagname = '', $canonical = self::CANON
* @param array $canonical
* @return string
*/
private static function makeDigest(DOMNode $node, $algorithm, $canonical = self::CANONICAL)
protected static function makeDigest(DOMNode $node, $algorithm, $canonical = self::CANONICAL)
{
//calcular o hash dos dados
$c14n = self::canonize($node, $canonical);
$c14n = static::canonize($node, $canonical);
$hashValue = hash($algorithm, $c14n, true);
return base64_encode($hashValue);
}
Expand All @@ -334,7 +334,7 @@ private static function makeDigest(DOMNode $node, $algorithm, $canonical = self:
* @param array $canonical
* @return string
*/
private static function canonize(DOMNode $node, $canonical = self::CANONICAL)
protected static function canonize(DOMNode $node, $canonical = self::CANONICAL)
{
return $node->C14N(
$canonical[0],
Expand Down
19 changes: 19 additions & 0 deletions src/SignerMulti.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* Class to sign multiple times the same xml
*/

namespace NFePHP\Common;

class SignerMulti extends Signer
{
/**
* @param string $content
* @return false
*/
public static function existsSignature($content)
{
return false;
}
}
31 changes: 31 additions & 0 deletions tests/SignerMultiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace NFePHP\Common\Tests;

use NFePHP\Common\Certificate;
use NFePHP\Common\SignerMulti;
use PHPUnit\Framework\TestCase;

class SignerMultiTest extends TestCase
{
public function testSign(): void
{
$pfx = file_get_contents(__DIR__ . '/fixtures/certs/certificado_teste.pfx');
$certificate = Certificate::readPfx($pfx, 'associacao');

$xml = '<a>';
$xml .= '<b>';
$xml .= '<c>Teste Assinar mesmo documento 2x</c>';
$xml .= '</b>';
$xml .= '</a>';

$xmlSignTagB = SignerMulti::sign($certificate, $xml, 'b', 'Id', OPENSSL_ALGO_SHA1, SignerMulti::CANONICAL, 'b');
$xmlsignTagA = SignerMulti::sign($certificate, $xmlSignTagB, 'a', 'Id', OPENSSL_ALGO_SHA1, SignerMulti::CANONICAL, 'a');

$dom = new \DOMDocument('1.0', 'utf-8');
$dom->loadXML($xmlsignTagA);
$this->assertEquals(2, $dom->getElementsByTagName('Signature')->count());
$this->assertEquals('Signature', $dom->getElementsByTagName('a')->item(0)->childNodes->item(1)->nodeName);
$this->assertEquals('Signature', $dom->getElementsByTagName('b')->item(0)->childNodes->item(1)->nodeName);
}
}