Esempio n. 1
0
 function it_appends_html_node(\DOMElement $element, \DOMElement $node, \DeForm\Node\HtmlNode $htmlNode)
 {
     $node->C14N()->willReturn('<input />');
     $element->appendChild($node)->shouldBeCalled();
     $htmlNode->getDomElement()->willReturn($node);
     $this->appendChild($htmlNode);
 }
Esempio n. 2
0
function two()
{
    $root = new DOMElement('root');
    $child = new DOMElement('child');
    $root->appendChild($child);
    var_dump($root->C14N());
}
 protected static function canonicalize(DOMElement $node)
 {
     $canonicalized = $node->C14N(true, false, null);
     return $canonicalized;
 }
Esempio n. 4
0
 /**
  * zSignXML
  * Método que provê a assinatura do xml conforme padrão SEFAZ
  * @param DOMDocument $xmldoc
  * @param DOMElement $root
  * @param DOMElement $node
  * @param resource $objSSLPriKey
  * @return string xml assinado
  * @internal param DOMDocument $xmlDoc
  */
 private function zSignXML($xmldoc, $root, $node, $objSSLPriKey)
 {
     $nsDSIG = 'http://www.w3.org/2000/09/xmldsig#';
     $nsCannonMethod = 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315';
     $nsSignatureMethod = 'http://www.w3.org/2000/09/xmldsig#rsa-sha1';
     $nsTransformMethod1 = 'http://www.w3.org/2000/09/xmldsig#enveloped-signature';
     $nsTransformMethod2 = 'http://www.w3.org/TR/2001/REC-xml-c14n-20010315';
     $nsDigestMethod = 'http://www.w3.org/2000/09/xmldsig#sha1';
     //pega o atributo id do node a ser assinado
     $idSigned = trim($node->getAttribute("Id"));
     //extrai os dados da tag para uma string na forma canonica
     $dados = $node->C14N(true, false, null, null);
     //calcular o hash dos dados
     $hashValue = hash('sha1', $dados, true);
     //converter o hash para base64
     $digValue = base64_encode($hashValue);
     //cria o node <Signature>
     $signatureNode = $xmldoc->createElementNS($nsDSIG, 'Signature');
     //adiciona a tag <Signature> ao node raiz
     $root->appendChild($signatureNode);
     //cria o node <SignedInfo>
     $signedInfoNode = $xmldoc->createElement('SignedInfo');
     //adiciona o node <SignedInfo> ao <Signature>
     $signatureNode->appendChild($signedInfoNode);
     //cria no node com o método de canonização dos dados
     $canonicalNode = $xmldoc->createElement('CanonicalizationMethod');
     //adiona o <CanonicalizationMethod> ao node <SignedInfo>
     $signedInfoNode->appendChild($canonicalNode);
     //seta o atributo ao node <CanonicalizationMethod>
     $canonicalNode->setAttribute('Algorithm', $nsCannonMethod);
     //cria o node <SignatureMethod>
     $signatureMethodNode = $xmldoc->createElement('SignatureMethod');
     //adiciona o node <SignatureMethod> ao node <SignedInfo>
     $signedInfoNode->appendChild($signatureMethodNode);
     //seta o atributo Algorithm ao node <SignatureMethod>
     $signatureMethodNode->setAttribute('Algorithm', $nsSignatureMethod);
     //cria o node <Reference>
     $referenceNode = $xmldoc->createElement('Reference');
     //adiciona o node <Reference> ao node <SignedInfo>
     $signedInfoNode->appendChild($referenceNode);
     //seta o atributo URI a node <Reference>
     $referenceNode->setAttribute('URI', '#' . $idSigned);
     //cria o node <Transforms>
     $transformsNode = $xmldoc->createElement('Transforms');
     //adiciona o node <Transforms> ao node <Reference>
     $referenceNode->appendChild($transformsNode);
     //cria o primeiro node <Transform> OBS: no singular
     $transfNode1 = $xmldoc->createElement('Transform');
     //adiciona o primeiro node <Transform> ao node <Transforms>
     $transformsNode->appendChild($transfNode1);
     //set o atributo Algorithm ao primeiro node <Transform>
     $transfNode1->setAttribute('Algorithm', $nsTransformMethod1);
     //cria outro node <Transform> OBS: no singular
     $transfNode2 = $xmldoc->createElement('Transform');
     //adiciona o segundo node <Transform> ao node <Transforms>
     $transformsNode->appendChild($transfNode2);
     //set o atributo Algorithm ao segundo node <Transform>
     $transfNode2->setAttribute('Algorithm', $nsTransformMethod2);
     //cria o node <DigestMethod>
     $digestMethodNode = $xmldoc->createElement('DigestMethod');
     //adiciona o node <DigestMethod> ao node <Reference>
     $referenceNode->appendChild($digestMethodNode);
     //seta o atributo Algorithm ao node <DigestMethod>
     $digestMethodNode->setAttribute('Algorithm', $nsDigestMethod);
     //cria o node <DigestValue>
     $digestValueNode = $xmldoc->createElement('DigestValue', $digValue);
     //adiciona o node <DigestValue> ao node <Reference>
     $referenceNode->appendChild($digestValueNode);
     //extrai node <SignedInfo> para uma string na sua forma canonica
     $cnSignedInfoNode = $signedInfoNode->C14N(true, false, null, null);
     //cria uma variavel vasia que receberá a assinatura
     $signature = '';
     //calcula a assinatura do node canonizado <SignedInfo>
     //usando a chave privada em formato PEM
     if (!openssl_sign($cnSignedInfoNode, $signature, $objSSLPriKey)) {
         $msg = "Houve erro durante a assinatura digital.\n";
         $this->zGetOpenSSLError($msg);
         //while ($erro = openssl_error_string()) {
         //    $msg .= $erro . "\n";
         //}
         //throw new Exception\RuntimeException($msg);
     }
     //converte a assinatura em base64
     $signatureValue = base64_encode($signature);
     //cria o node <SignatureValue>
     $signatureValueNode = $xmldoc->createElement('SignatureValue', $signatureValue);
     //adiciona o node <SignatureValue> ao node <Signature>
     $signatureNode->appendChild($signatureValueNode);
     //cria o node <KeyInfo>
     $keyInfoNode = $xmldoc->createElement('KeyInfo');
     //adiciona o node <KeyInfo> ao node <Signature>
     $signatureNode->appendChild($keyInfoNode);
     //cria o node <X509Data>
     $x509DataNode = $xmldoc->createElement('X509Data');
     //adiciona o node <X509Data> ao node <KeyInfo>
     $keyInfoNode->appendChild($x509DataNode);
     //remove linhas desnecessárias do certificado
     $pubKeyClean = $this->zCleanPubKey();
     //cria o node <X509Certificate>
     $x509CertificateNode = $xmldoc->createElement('X509Certificate', $pubKeyClean);
     //adiciona o node <X509Certificate> ao node <X509Data>
     $x509DataNode->appendChild($x509CertificateNode);
     //salva o xml completo em uma string
     $xmlResp = $xmldoc->saveXML();
     //retorna o documento assinado
     return $xmlResp;
 }