Example usage for com.lowagie.text.pdf PdfPKCS7 loadCacertsKeyStore

List of usage examples for com.lowagie.text.pdf PdfPKCS7 loadCacertsKeyStore

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfPKCS7 loadCacertsKeyStore.

Prototype

public static KeyStore loadCacertsKeyStore() 

Source Link

Document

Loads the default root certificates at <java.home>/lib/security/cacerts with the default provider.

Usage

From source file:ec.gov.informatica.firmadigital.FirmaDigital.java

License:Open Source License

public List<String> verificar(String direccionPDF) throws SignatureVerificationException {
    try {/*from  ww  w .  j  a v  a2  s .  c  o m*/
        List<String> firmantes = new ArrayList<>();
        if (direccionPDF == null || direccionPDF.isEmpty()) {
            System.out.print("Necesito el nombre del PDF a comprobar");
            System.exit(1);
        }

        Random rnd = new Random();
        KeyStore kall = PdfPKCS7.loadCacertsKeyStore();
        PdfReader reader = new PdfReader(direccionPDF);
        AcroFields af = reader.getAcroFields();
        ArrayList names = af.getSignatureNames();
        for (int k = 0; k < names.size(); ++k) {

            String name = (String) names.get(k);
            //            System.out.println(name);
            int random = rnd.nextInt();
            FileOutputStream out = new FileOutputStream(
                    "revision_" + random + "_" + af.getRevision(name) + ".pdf");

            byte bb[] = new byte[8192];
            InputStream ip = af.extractRevision(name);
            int n = 0;
            while ((n = ip.read(bb)) > 0)
                out.write(bb, 0, n);
            out.close();
            ip.close();

            PdfPKCS7 pk = af.verifySignature(name);
            Calendar cal = pk.getSignDate();
            Certificate pkc[] = pk.getCertificates();
            Object fails[] = PdfPKCS7.verifyCertificates(pkc, kall, null, cal);
            String firmante = pk.getSignName() + " (" + name + ") - ";
            if (fails == null) {
                firmante += "Firma Verificada";
            } else {
                firmante += "Firma No Vlida";
            }
            File f = new File("revision_" + random + "_" + af.getRevision(name) + ".pdf");
            f.delete();
            firmantes.add(firmante);
        }
        return firmantes;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}