Aproveitando o topico aqui, bom aqui estou eu tentando assinar uma NFe, peguei uns codigo aqui do forum, mexi pouca coisa só pra fazer um teste e to recebendo um erro:
javax.xml.crypto.dsig.XMLSignatureException: javax.xml.crypto.URIReferenceException: java.lang.NullPointerException
at org.jcp.xml.dsig.internal.dom.DOMReference.dereference(DOMReference.java:352)
at org.jcp.xml.dsig.internal.dom.DOMReference.digest(DOMReference.java:278)
at org.jcp.xml.dsig.internal.dom.DOMXMLSignature.digestReference(DOMXMLSignature.java:447)
at org.jcp.xml.dsig.internal.dom.DOMXMLSignature.sign(DOMXMLSignature.java:343)
O erro acontece nesta linha:
DOMSignContext dsc = new DOMSignContext(privateKey, doc.getDocumentElement().getElementsByTagName("NFe").item(i));
signature.sign(dsc); //<=== Erro acontece nesta linha
e Eis todo o codigo que peguei aqui mesmo no GUJ.
public static String assinarEnviNFe(String xml, String certificado, String keyStorePass) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
// Document docs = builder.parse(new File(
// "c:/xml/430802017886010001735500000000010000030371-nfe.xml"));
Document doc = factory.newDocumentBuilder().parse(
new ByteArrayInputStream(xml.getBytes()));
// Create a DOM XMLSignatureFactory that will be used to
// generate the enveloped signature.
XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
// Create a Reference to the enveloped document (in this case,
// you are signing the whole document, so a URI of "" signifies
// that, and also specify the SHA1 digest algorithm and
// the ENVELOPED Transform.
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);
// Load the KeyStore and get the signing key and certificate.
KeyStore ks = KeyStore.getInstance("PKCS12"); //MUDEI AQUI PRA PEGAR DIRETO O PFX
FileInputStream fis = new FileInputStream(certificado);
//load the keystore
ks.load(fis, keyStorePass.toCharArray());
String alias = ks.aliases().nextElement();
KeyStore.PrivateKeyEntry keyEntry = (KeyStore.PrivateKeyEntry) ks.getEntry(alias, new KeyStore.PasswordProtection(keyStorePass.toCharArray()));
X509Certificate cert = (X509Certificate) keyEntry.getCertificate();
// Create the KeyInfo containing the X509Data.
KeyInfoFactory kif = fac.getKeyInfoFactory();
List<Serializable> x509Content = new ArrayList<Serializable>();
x509Content.add(cert.getSubjectX500Principal().getName());
x509Content.add(cert);
X509Data xd = kif.newX509Data(x509Content);
KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd));
for (int i = 0; i < doc.getDocumentElement().getElementsByTagName("NFe").getLength(); i++) {
assinarNFE(fac, transformList, keyEntry.getPrivateKey(), ki, doc, i);
}
// Output the resulting document.
ByteArrayOutputStream os = new ByteArrayOutputStream();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
trans.transform(new DOMSource(doc), new StreamResult(os));
return os.toString();
}
private static void assinarNFE(XMLSignatureFactory fac,
ArrayList transformList, PrivateKey privateKey, KeyInfo ki,
Document doc, int i) throws Exception {
// Obtem elemento do documento a ser assinado, será criado uma
// REFERENCE para o mesmo
NodeList elements = doc.getElementsByTagName("infNFe");
Element el = (Element) elements.item(i);
String id = el.getAttribute("Id");
// doc.getDocumentElement().removeAttribute("xmlns:ns2");
// ((Element)
// doc.getDocumentElement().getElementsByTagName("NFe").item(0))
// .setAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe");
// Create a DOM XMLSignatureFactory that will be used to
// generate the enveloped signature.
Reference ref = fac.newReference("" + id, fac.newDigestMethod(
DigestMethod.SHA1, null), transformList, null, null);
// Create the SignedInfo.
SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(
CanonicalizationMethod.INCLUSIVE,
(C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null),
Collections.singletonList(ref));
// Create the XMLSignature, but don't sign it yet.
XMLSignature signature = fac.newXMLSignature(si, ki);
// Marshal, generate, and sign the enveloped signature.
// Create a DOMSignContext and specify the RSA PrivateKey and
// location of the resulting XMLSignature's parent element.
DOMSignContext dsc = new DOMSignContext(privateKey, doc.getDocumentElement().getElementsByTagName("NFe").item(i));
signature.sign(dsc);
}
Uma coisa que mudei foi pegar direto o PFX, ao inves do JKS, to tentando fazer assim, sem criar JKS, seguindo o que o thingol disse aqui: http://www.guj.com.br/posts/list/15/87934.java
To meio perdido aqui com essa assinatura, alguem passou por isso?