wmixvideo / nfe

Nota Fiscal Eletrônica em Java.
Apache License 2.0
654 stars 377 forks source link

Erros na geração das cadeias CACERT - unable to find valid certification path to requested target #552

Closed DanielChrome closed 5 years ago

DanielChrome commented 5 years ago

Boa tarde pessoal, estou tentando gerar o arquivo cacert com as cadeias, seguindo o helper disponibilizado. Não sei se estou fazendo algo de errado, mas está dando erro para todos os Estados, como se faltasse um certificado válido. PS: Não tenho muita experiencia em Java....

Segue Log: 2019-03-22 14:56:36 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [nfe.sefaz.am.gov.br] javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 2019-03-22 14:56:37 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [nfce.sefaz.am.gov.br] javax.net.ssl.SSLHandshakeException: Received fatal alert: bad_certificate 2019-03-22 14:56:37 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [nfe.sefaz.ba.gov.br] javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 2019-03-22 14:56:38 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [nfe.sefaz.ce.gov.br] javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 2019-03-22 14:56:38 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [nfe.sefaz.go.gov.br] javax.net.ssl.SSLHandshakeException: Received fatal alert: certificate_unknown 2019-03-22 14:56:38 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [nfe.sefaz.go.gov.br] javax.net.ssl.SSLHandshakeException: Received fatal alert: certificate_unknown 2019-03-22 14:56:39 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [nfe.fazenda.mg.gov.br] javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure 2019-03-22 14:56:40 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [nfe.sefaz.mt.gov.br] javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure 2019-03-22 14:56:40 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [nfce.sefaz.mt.gov.br] javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure 2019-03-22 14:56:43 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [nfe.sefa.pr.gov.br] javax.net.ssl.SSLHandshakeException: Received fatal alert: bad_certificate 2019-03-22 14:56:43 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [nfce.sefa.pr.gov.br] javax.net.ssl.SSLHandshakeException: Received fatal alert: bad_certificate 2019-03-22 14:56:46 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [cte.sefaz.mt.gov.br] javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure 2019-03-22 14:56:47 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [cte.fazenda.mg.gov.br] javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure 2019-03-22 14:56:47 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [cte.fazenda.pr.gov.br] javax.net.ssl.SSLHandshakeException: Received fatal alert: bad_certificate 2019-03-22 14:56:48 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [homnfe.sefaz.am.gov.br] javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 2019-03-22 14:56:49 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [homnfce.sefaz.am.gov.br] javax.net.ssl.SSLHandshakeException: Received fatal alert: bad_certificate 2019-03-22 14:56:49 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [hnfe.sefaz.ba.gov.br] javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 2019-03-22 14:56:49 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [nfce-homologacao.svrs.rs.gov.br] javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 2019-03-22 14:56:49 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [nfeh.sefaz.ce.gov.br] javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 2019-03-22 14:56:49 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [nfceh.sefaz.ce.gov.br] javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure 2019-03-22 14:56:50 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [homolog.sefaz.go.gov.br] javax.net.ssl.SSLHandshakeException: Received fatal alert: certificate_unknown 2019-03-22 14:56:50 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [homolog.sefaz.go.gov.br] javax.net.ssl.SSLHandshakeException: Received fatal alert: certificate_unknown 2019-03-22 14:56:51 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [hnfe.fazenda.mg.gov.br] javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure 2019-03-22 14:56:52 [ERROR] [main] com.fincatto.documentofiscal.utils.GeraCadeiaCertificados.get:103 - [homologacao.sefaz.mt.gov.br] javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Tem algo mais além de executar o helper?? public static void main(String args[]){ try { FileUtils.writeByteArrayToFile(new File("/tmp/producao.cacerts"), GeraCadeiaCertificados.geraCadeiaCertificados(DFAmbiente.PRODUCAO, "senha")); FileUtils.writeByteArrayToFile(new File("/tmp/homologacao.cacerts"), GeraCadeiaCertificados.geraCadeiaCertificados(DFAmbiente.HOMOLOGACAO, "senha")); } catch (Exception e) { e.printStackTrace(); } }

rodrigocode4 commented 5 years ago

Tente usar o ponto como primeiro elemento da string, no construtor do File. E apenas a extensão .certs.

FileUtils.writeByteArrayToFile(new File("./tmp/producao.certs"), 
GeraCadeiaCertificados.geraCadeiaCertificados(DFAmbiente.PRODUCAO, "senha"));

FileUtils.writeByteArrayToFile(new File("./tmp/homologacao.certs"), 
GeraCadeiaCertificados.geraCadeiaCertificados(DFAmbiente.HOMOLOGACAO, "senha"))
DanielChrome commented 5 years ago

Obrigado pela resposta Rodrigo, mas deu o mesmo problema. Dá vários erros como: Received fatal alert: bad_certificate Received fatal alert: handshake_failure Received fatal alert: certificate_unknown unable to find valid certification path to requested target

É como se faltasse algum certificado válido pra processar as requisições.

Segue o meu código: `import java.io.File;

import org.apache.commons.io.FileUtils;

import com.fincatto.documentofiscal.DFAmbiente; import com.fincatto.documentofiscal.utils.GeraCadeiaCertificados;

public class Generate {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
        FileUtils.writeByteArrayToFile(new File("./tmp/producao.certs"), GeraCadeiaCertificados.geraCadeiaCertificados(DFAmbiente.PRODUCAO, "senha"));
        FileUtils.writeByteArrayToFile(new File("./tmp/homologacao.certs"), GeraCadeiaCertificados.geraCadeiaCertificados(DFAmbiente.HOMOLOGACAO, "senha"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}`

fincatto commented 5 years ago

Alguns erros ocorrem pois nem todos os certificados dos endpoints estão ok para importação, mas, no geral, a importação ocorre e os arquivos são gerados. Testei inclusive com o seu codigo e ele gerou as cadeias sem problema. Tem um teste unitario (ignorado por padrao, pois é lento para rodar no CI) chamado GeraCadeiaCertificadosTest.java. Verifique se você consegue rodar ele sem problemas. Se ainda estiver com problema, passe pra gente qual a versao do java você esta usando.

fincatto commented 5 years ago

Alterei o nome dessa classe recentemente no master, então se você atualizar, vai ficar assim:

public static void main(String args[]){
    try {
        FileUtils.writeByteArrayToFile(new File("/tmp/producao.cacerts"), DFCadeiaCertificados.geraCadeiaCertificados(DFAmbiente.PRODUCAO, "senha"));
        FileUtils.writeByteArrayToFile(new File("/tmp/homologacao.cacerts"), DFCadeiaCertificados.geraCadeiaCertificados(DFAmbiente.HOMOLOGACAO, "senha"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
DanielChrome commented 5 years ago

Opa. desculpe a demora pra responder. Consegui resolver olhando outros exemplos na Net. Tive que fazer alguns ajustes. Segue meu código:

` import java.io.File; import org.apache.commons.io.FileUtils; import com.fincatto.documentofiscal.DFAmbiente; import com.fincatto.documentofiscal.utils.GeraCadeiaCertificados;

public class Generate {

private static final String CACERTS_NAME = "Cacert";
private static final String CACERTS_PATH = System.getProperty("user.dir") + "/src/main/resources/";
private static final char SEPARATOR = File.separatorChar;

public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {       
        String file = CACERTS_PATH + SEPARATOR + CACERTS_NAME;
        info("| Loading KeyStore " + file + "...");
        FileUtils.writeByteArrayToFile(new File(file), 
                GeraCadeiaCertificados.geraCadeiaCertificados(DFAmbiente.PRODUCAO, "changeit", file));
        FileUtils.writeByteArrayToFile(new File(file), 
                GeraCadeiaCertificados.geraCadeiaCertificados(DFAmbiente.HOMOLOGACAO, "changeit", file));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static void info(String log) {  
    System.out.println("INFO: " + log);  
}  

private static void error(String log) {  
    System.out.println("ERROR: " + log);  
}  

} `

E alterei o método geraCadeiaCertificados para receber o arquivo cacert, e passei ele no load do keystore (tava null)
Não sei se foi o correto, ou o pq não funcionava antes. Mas consegui resolver assim. Obrigado pela ajuda.

public static byte[] geraCadeiaCertificados(final DFAmbiente ambiente, final String senha, final String file) throws Exception { InputStream in = new FileInputStream(file); KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(in, senha.toCharArray()); in.close();