Example usage for com.itextpdf.text.pdf AcroFields getSignatureNames

List of usage examples for com.itextpdf.text.pdf AcroFields getSignatureNames

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf AcroFields getSignatureNames.

Prototype

public ArrayList<String> getSignatureNames() 

Source Link

Document

Gets the field names that have signatures and are signed.

Usage

From source file:org.roda.common.certification.PDFSignatureUtils.java

public static String runDigitalSignatureVerify(Path input) throws IOException, GeneralSecurityException {
    Security.addProvider(new BouncyCastleProvider());

    PdfReader reader = new PdfReader(input.toString());
    AcroFields fields = reader.getAcroFields();
    ArrayList<String> names = fields.getSignatureNames();
    String result = "Passed";

    for (int i = 0; i < names.size(); i++) {
        String name = names.get(i);

        try {/*from   w  w  w  .  j  av  a2s .com*/
            PdfPKCS7 pk = fields.verifySignature(name);
            X509Certificate certificate = pk.getSigningCertificate();
            certificate.checkValidity();

            if (!SignatureUtils.isCertificateSelfSigned(certificate)) {

                Set<Certificate> trustedRootCerts = new HashSet<Certificate>();
                Set<Certificate> intermediateCerts = new HashSet<Certificate>();

                for (Certificate c : pk.getSignCertificateChain()) {
                    X509Certificate cert = (X509Certificate) c;
                    cert.checkValidity();

                    if (SignatureUtils.isCertificateSelfSigned(c))
                        trustedRootCerts.add(c);
                    else
                        intermediateCerts.add(c);
                }

                SignatureUtils.verifyCertificateChain(trustedRootCerts, intermediateCerts, certificate);
                if (pk.getCRLs() != null) {
                    for (CRL crl : pk.getCRLs()) {
                        if (crl.isRevoked(certificate)) {
                            result = "Signing certificate is included on a Certificate Revocation List";
                        }
                    }
                }
            }
        } catch (NoSuchFieldError e) {
            result = "Missing signature timestamp field";
        } catch (CertificateExpiredException e) {
            result = "Contains expired certificates";
        } catch (CertificateNotYetValidException e) {
            result = "Contains certificates not yet valid";
        }
    }

    reader.close();
    return result;
}

From source file:org.roda.common.certification.PDFSignatureUtils.java

public static List<Path> runDigitalSignatureExtract(Path input) throws SignatureException, IOException {
    Security.addProvider(new BouncyCastleProvider());

    List<Path> paths = new ArrayList<Path>();
    Path output = Files.createTempFile("extraction", ".xml");
    Path outputContents = Files.createTempFile("contents", ".pkcs7");
    PdfReader reader = new PdfReader(input.toString());
    AcroFields fields = reader.getAcroFields();
    ArrayList<?> names = fields.getSignatureNames();
    String filename = input.getFileName().toString();
    filename = filename.substring(0, filename.lastIndexOf('.'));

    if (names.isEmpty())
        return paths;

    StringBuilder sb = getExtractionInformation(fields, names, outputContents, filename);

    FileOutputStream fos = new FileOutputStream(output.toString());
    OutputStreamWriter osw = new OutputStreamWriter(fos);
    PrintWriter out = new PrintWriter(osw, true);

    out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    out.println("<signatures>");
    out.println(sb.toString());/*from   w  w  w.ja v a 2s  .co m*/
    out.println("</signatures>");

    IOUtils.closeQuietly(out);
    IOUtils.closeQuietly(osw);
    IOUtils.closeQuietly(fos);
    reader.close();

    paths.add(output);
    paths.add(outputContents);
    return paths;
}

From source file:org.roda.common.certification.PDFSignatureUtils.java

public static int countSignaturesPDF(Path file) {
    int counter = -1;
    try {/*from www.  j a va  2 s . c  o m*/
        PdfReader reader = new PdfReader(file.toAbsolutePath().toString());
        AcroFields af = reader.getAcroFields();
        ArrayList<String> names = af.getSignatureNames();
        counter = names.size();
    } catch (IOException e) {
        LOGGER.error("Error getting path of file {}", e.getMessage());
    }
    return counter;
}

From source file:org.roda.core.plugins.plugins.characterization.PDFSignatureUtils.java

public static String runDigitalSignatureVerify(Path input) throws IOException, GeneralSecurityException {
    Security.addProvider(new BouncyCastleProvider());

    PdfReader reader = new PdfReader(input.toString());
    AcroFields fields = reader.getAcroFields();
    ArrayList<String> names = fields.getSignatureNames();
    String result = "Passed";

    for (int i = 0; i < names.size(); i++) {
        String name = names.get(i);

        try {// w w  w  .  j a  v a 2 s . c  o m
            PdfPKCS7 pk = fields.verifySignature(name);
            X509Certificate certificate = pk.getSigningCertificate();
            certificate.checkValidity();

            if (!SignatureUtils.isCertificateSelfSigned(certificate)) {

                Set<Certificate> trustedRootCerts = new HashSet<>();
                Set<Certificate> intermediateCerts = new HashSet<>();

                for (Certificate c : pk.getSignCertificateChain()) {
                    X509Certificate cert = (X509Certificate) c;
                    cert.checkValidity();

                    if (SignatureUtils.isCertificateSelfSigned(c))
                        trustedRootCerts.add(c);
                    else
                        intermediateCerts.add(c);
                }

                SignatureUtils.verifyCertificateChain(trustedRootCerts, intermediateCerts, certificate);
                if (pk.getCRLs() != null) {
                    for (CRL crl : pk.getCRLs()) {
                        if (crl.isRevoked(certificate)) {
                            result = "Signing certificate is included on a Certificate Revocation List";
                        }
                    }
                }
            }
        } catch (NoSuchFieldError e) {
            result = "Missing signature timestamp field";
        } catch (CertificateExpiredException e) {
            result = "Contains expired certificates";
        } catch (CertificateNotYetValidException e) {
            result = "Contains certificates not yet valid";
        }
    }

    reader.close();
    return result;
}

From source file:org.roda.core.plugins.plugins.characterization.PDFSignatureUtils.java

public static List<Path> runDigitalSignatureExtract(Path input) throws SignatureException, IOException {
    Security.addProvider(new BouncyCastleProvider());

    List<Path> paths = new ArrayList<>();
    Path output = Files.createTempFile("extraction", ".xml");
    Path outputContents = Files.createTempFile("contents", ".pkcs7");
    PdfReader reader = new PdfReader(input.toString());
    AcroFields fields = reader.getAcroFields();
    ArrayList<?> names = fields.getSignatureNames();
    String filename = input.getFileName().toString();
    filename = filename.substring(0, filename.lastIndexOf('.'));

    if (names.isEmpty())
        return paths;

    StringBuilder sb = getExtractionInformation(fields, names, outputContents, filename);

    FileOutputStream fos = new FileOutputStream(output.toString());
    OutputStreamWriter osw = new OutputStreamWriter(fos);
    try (PrintWriter out = new PrintWriter(osw, true)) {
        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        out.println("<signatures>");
        out.println(sb.toString());//from   w w  w . j  a v a2s .c om
        out.println("</signatures>");
    }

    reader.close();

    paths.add(output);
    paths.add(outputContents);
    return paths;
}

From source file:org.sinekartads.core.pdf.PDFTools.java

License:Open Source License

/**
 * metodo di utilita' che verifica se il pdf in input e' gia' firmato
 * /*  www .j  a v a2 s.  c  o  m*/
 * @param reader
 * @return
 * @throws SignatureException 
 */
public static boolean isPdfSigned(PdfReader reader) throws SignatureException {
    if (tracer.isDebugEnabled())
        tracer.debug("chacking if PDF/A is signed");
    try {
        AcroFields af = reader.getAcroFields();

        // Search of the whole signature
        ArrayList<String> names = af.getSignatureNames();

        // For every signature :
        if (names.size() > 0) {
            if (tracer.isDebugEnabled())
                tracer.debug("yes, it is");
            return true;
        } else {
            if (tracer.isDebugEnabled())
                tracer.debug("no, it isn't");
            return false;
        }
    } catch (Exception e) {
        tracer.error("Unable to read PDF. Unable to check if the pdf is signed.", e);
        throw new SignatureException("Unable to read PDF. Unable to check if the pdf is signed.", e);
    }
}

From source file:org.sinekartapdfa.alfresco.utils.PDFTools.java

License:Open Source License

/**
 * metodo di utilita' che verifica se il pdf in input e' gia' firmato
 * //w  ww  .  ja  v  a 2s.  com
 * @param reader
 * @return
 */
public static boolean isPdfSigned(PdfReader reader) {
    if (tracer.isDebugEnabled())
        tracer.debug("chacking if PDF/A is signed");
    try {
        AcroFields af = reader.getAcroFields();

        // Search of the whole signature
        ArrayList<String> names = af.getSignatureNames();

        // For every signature :
        if (names.size() > 0) {
            if (tracer.isDebugEnabled())
                tracer.debug("yes, it is");
            return true;
        } else {
            if (tracer.isDebugEnabled())
                tracer.debug("no, it isn't");
            return false;
        }
    } catch (Exception e) {
        tracer.error("Unable to read PDF. Unable to check if the pdf is signed.", e);
        throw new PDFException("Unable to read PDF. Unable to check if the pdf is signed.", e);
    }
}