Estou enviando o seguinte xml, e está me retornando E172-Arquivo enviado com erro na assinatura. - Acerte a assinatura do arquivo.
Segue o xml em anexo e o codigo que estou gerando para assinar
requestSoap = String com o certificado sem a assinatura digital.
Codigo de Assinatura:
Empresa empresa = notaFiscalList.get(0).getId().getEmpresa();
String senhaCertificado = CryptUtil.decrypt(Coalesce.asString(empresa.getCertificadoServicoPass()));
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream fis = new FileInputStream(empresa.getCertificadoServico());
keyStore.load(fis, senhaCertificado.toCharArray());
String dataValidadeCertificado = null;
Enumeration<String> e = keyStore.aliases();
X509Certificate cert;
KeyStore.PrivateKeyEntry privateKeyEntry = null;
PrivateKey privateKey = null;
Enumeration aliasesEnum = keyStore.aliases();
while (aliasesEnum.hasMoreElements()) {
String alias = (String) aliasesEnum.nextElement();
System.out.println(alias);
if (keyStore.isKeyEntry(alias)) {
privateKeyEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(alias, new KeyStore.PasswordProtection(senhaCertificado.toCharArray()));
privateKey = privateKeyEntry.getPrivateKey();
break;
}
}
cert = (X509Certificate) privateKeyEntry.getCertificate();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = factory.newDocumentBuilder().parse(new ByteArrayInputStream(requestSoap.getBytes("UTF-8")));
XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
ArrayList transformList = new ArrayList();
TransformParameterSpec tps = null;
Transform envelopedTransform = fac.newTransform(Transform.ENVELOPED, tps);
Transform c14NTransform = fac.newTransform("http://www.w3.org/TR/2001/REC-xml-c14n-20010315", tps);
transformList.add(envelopedTransform);
transformList.add(c14NTransform);
KeyInfoFactory kif = fac.getKeyInfoFactory();
List x509Content = new ArrayList();
x509Content.add(cert);
X509Data xd = kif.newX509Data(x509Content);
KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd));
NodeList elementRps = doc.getElementsByTagName("InfDeclaracaoPrestacaoServico");
Element elRps = (Element) elementRps.item(0);
String idRPs = elRps.getAttribute("Id");
elRps.setIdAttribute("Id", true);
((Element) doc.getDocumentElement()).setAttribute("xmlns", "http://www.betha.com.br/e-nota-contribuinte-ws");
Reference refRps = fac.newReference("#" + idRPs, fac.newDigestMethod(DigestMethod.SHA256, null), transformList, null, null);
SignedInfo siRps = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(refRps));
XMLSignature signatureRps = fac.newXMLSignature(siRps, ki);
DOMSignContext dscRps = new DOMSignContext(privateKey, doc.getDocumentElement().getElementsByTagName("Rps").item(0));
signatureRps.sign(dscRps);
KeyInfo ki2 = kif.newKeyInfo(Collections.singletonList(xd));
NodeList elementRps2 = doc.getElementsByTagName("InfDeclaracaoPrestacaoServico");
Element elRps2 = (Element) elementRps2.item(1);
String idRPs2 = elRps2.getAttribute("Id");
elRps2.setIdAttribute("Id", true);
((Element) doc.getDocumentElement()).setAttribute("xmlns", "http://www.betha.com.br/e-nota-contribuinte-ws");
Reference refRps2 = fac.newReference("#" + idRPs, fac.newDigestMethod(DigestMethod.SHA256, null), transformList, null, null);
SignedInfo siRps2 = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(refRps2));
XMLSignature signatureRps2 = fac.newXMLSignature(siRps2, ki2);
DOMSignContext dscRps2 = new DOMSignContext(privateKey, doc.getDocumentElement().getElementsByTagName("Rps").item(2));
// signatureRps.sign(dscRps2);
ByteArrayOutputStream os = new ByteArrayOutputStream();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.transform(new DOMSource(doc), new StreamResult(os));
DOMSignContext dscRps3 = new DOMSignContext(privateKey, doc.getDocumentElement().getElementsByTagName("Rps").item(1));
signatureRps.sign(dscRps3);
//
// TransformerFactory tf1 = TransformerFactory.newInstance();
// Transformer trans2 = tf1.newTransformer();
// trans2.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
// trans2.transform(new DOMSource(doc), new StreamResult(os));
// return os.toString();
return os.toString();
testenota.xml