O Brother e que vc deve assinar cada nfe no xml envNFe.Tenho um metodo aqui para
isto,mas comigo ta dando um erro.
IOException: toDerInputStream rejects tag type 60
at sun.security.util.DerValue.toDerInputStream(DerValue.java:806)
at com.sun.net.ssl.internal.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:1201)
Testa ai que sabe vc ate me ajuda, a logica deste metodo e no xml envNFe ele faz um loop em todas as NFe e assina.
public static String assinarEnviNFe(String xml, String senha) 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 = builder.parse(new File(xml));
// 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(xml);
//load the keystore
ks.load(new FileInputStream(xml), senha.toCharArray());
String alias = ks.aliases().nextElement();
KeyStore.PrivateKeyEntry keyEntry = (KeyStore.PrivateKeyEntry) ks.getEntry(alias, new KeyStore.PasswordProtection(senha.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);
}
Este codigo eu peguei aqui mesmo .