Use of two different certificates for nfse abrasf issuance

Asked

Viewed 277 times

2

I am sending notes to abrasf using the code below to load keystrore certificate information and tals.

Only when sending with a certificate and then with another from another taxpayer refuses to inform this error.

Error code: GOV14. Message: Issuer is not authorized to issue notes to that contributor.. Possible Correction: The note issuer must be the contributor himself or the counter.

He considers that the previous certificate that is making the communication with the web service.

But if I drop Tomcat and open the application again it accepts to use the second one, but does not let use the first one, getting the same error. That is only by closing the java, the information of the first certificate that connected with this web service disappears.

Interestingly, for the web service of Nota Fiscal Eletrônica Modelo 55, it works normally, with as many certificates as necessary. Someone’s been through this trouble?

Using the dynamic Keystore http://www.javac.com.br/jc/posts/list/222-resolven...-a3-protocolsocketfactory.page, get Handshake Failure error.

Follows code:

 public String enviarNFse(String xmlCabecalho, String xmlEnvNFse) throws ExceptionServicoAbrasf {
    try {
        AssinarXMLNFse assinarXML = new AssinarXMLNFse(this.keyStoreService.getKeystore(), this.keyStoreService.getAliasCert(), this.keyStoreService.getSenhaPfx());
        xmlEnvNFse = assinarXML.assinaNfseEnvio(xmlEnvNFse);
        loadInfoCertificado();
        return executarServicoEnvioNFse(xmlCabecalho, xmlEnvNFse);
    } catch (Exception ex) {
        throw new ExceptionServicoAbrasf("Erro em assinatura do xml. " + ex.getMessage());
    }

}
//add xml to sign
  public String assinaNfseEnvio(String xml) throws Exception {
      //sign
    return assinar(xml, "InfDeclaracaoPrestacaoServico", "Rps");
}

//Sign

   private String assinar(String xml, String tag, String tagPaiSignature) throws Exception {
    Document document = documentFactory(xml);

    XMLSignatureFactory signatureFactory = XMLSignatureFactory
            .getInstance("DOM");
    ArrayList<Transform> transformList = signatureFactory(signatureFactory);
    loadCertificates(signatureFactory);

    NodeList elements = document.getElementsByTagName(tag);
    org.w3c.dom.Element el = (org.w3c.dom.Element) elements.item(0);
    el.setIdAttribute("Id", true);
    String id = el.getAttribute("Id");

    Reference ref = signatureFactory.newReference("#" + id,
            signatureFactory.newDigestMethod(DigestMethod.SHA1, null),
            transformList, null, null);

    SignedInfo si = signatureFactory.newSignedInfo(signatureFactory
            .newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE,
                    (C14NMethodParameterSpec) null), signatureFactory
            .newSignatureMethod(SignatureMethod.RSA_SHA1, null),
            Collections.singletonList(ref));

    XMLSignature signature = signatureFactory.newXMLSignature(si, keyInfo);

    DOMSignContext dsc = new DOMSignContext(privateKey, document.getElementsByTagName(tagPaiSignature).item(0));
    signature.sign(dsc);

    return outputXML(document);
}  
//load certificate
 protected void loadCertificates(XMLSignatureFactory signatureFactory) throws Exception {
    if (keyStore != null) {
        KeyStore.PrivateKeyEntry pkEntry = null;
        if (keyStore.isKeyEntry(alias)) {
            char[] pin = (senha == null ? "" : senha).toCharArray();
            pkEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(alias,
                    new KeyStore.PasswordProtection(pin));
            privateKey = pkEntry.getPrivateKey();
            X509Certificate cert = (X509Certificate) pkEntry.getCertificate();

            KeyInfoFactory keyInfoFactory = signatureFactory.getKeyInfoFactory();
            List<X509Certificate> x509Content = new ArrayList<>();

            x509Content.add(cert);
            X509Data x509Data = keyInfoFactory.newX509Data(x509Content);
            keyInfo = keyInfoFactory.newKeyInfo(Collections.singletonList(x509Data));
        } else {
            throw new Exception("Alias do certificado inv�lido.");
        }
    } else {
        throw new Exception("Informa��es do Certificado inv�lidas.");
    }
}
///normalize xml
 protected String outputXML(Document doc) throws TransformerException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer trans = tf.newTransformer();
    trans.transform(new DOMSource(doc), new StreamResult(os));
    String xml = os.toString();
    if ((xml != null) && (!"".equals(xml))) {
        xml = xml.replaceAll("\\r\\n", "");
        xml = xml.replaceAll(" standalone=\"no\"", "");
    }
    return xml;
}
//load info certificate
     private void loadInfoCertificado() {
    System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

    System.setProperty("javax.net.ssl.keyStoreType", "PKCS12");

    System.clearProperty("javax.net.ssl.keyStore");
    System.clearProperty("javax.net.ssl.keyStorePassword");
    System.clearProperty("javax.net.ssl.trustStore");
    System.setProperty("javax.net.ssl.keyStore", this.keyStoreService.getPathPfx());
    System.setProperty("javax.net.ssl.keyStorePassword", this.keyStoreService.getSenhaPfx());

    System.setProperty("javax.net.ssl.trustStoreType", "JKS");
    System.setProperty("javax.net.ssl.trustStore", this.pathCacerts);

}
//send web service
    private String executarServicoEnvioNFse(String xmlCabecalho, String xmlCorpo) throws ExceptionServicoAbrasf {
    Input input = new Input(xmlCabecalho, xmlCorpo);
    Output output = gerarNfse(input);
    return output.getOutputXML();

}
//generate conexao web service
    private Output gerarNfse(ws.Input parameters) throws ExceptionServicoAbrasf {
    ws.Nfse port = loadNfseDivPort();
    return port.gerarNfse(parameters);
}
//return web service
    private ws.Nfse loadNfseDivPort() throws ExceptionServicoAbrasf {
    try {
        String urlStr = this.tpAmb.equals(TpAmbiente.PRODUCAO) ? this.wsdlLocation : this.wsdlLocationHomolog;
        URL url = new URL(urlStr);
        ws.NfseServiceImplDivService service = new ws.NfseServiceImplDivService(url);
        ws.Nfse port = service.getNfseDivPort();
        return port;
    } catch (MalformedURLException ex) {
        throw new ExceptionServicoAbrasf("URL do serviço gerarNfse mal formada. " + ex.getMessage());
    }
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.