Use of two different certificates to issue nfse abrasf

2

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

Only when I submit with one certificate and then another with another taxpayer, I refuse to report this error.

Error code: GOV14. Message: Issuer is not authorized to issue notes to this taxpayer. Possible Correction: The issuer of the note must be the taxpayer or the accountant.

It considers the previous certificate that is doing communication with the web service.

But if I take down tomcat and reopen the application it accepts to use the second one, but it does not let it use the first, obtaining the same error. In other words, just by closing the java, the information of the first certificate that connected with this web service disappears.

The interesting thing that for the web service of the Nota Fiscal Eletrônica Model 55, works normally, with as many certificates as it is necessary. Has anyone ever had this problem?

Using the dynamic keystore link , I get the error handshake failure.

Follow the code below:

 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());
    }
}
    
asked by anonymous 23.07.2015 / 16:14

0 answers