Example usage for com.itextpdf.text.pdf PdfReader getAcroFields

List of usage examples for com.itextpdf.text.pdf PdfReader getAcroFields

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfReader getAcroFields.

Prototype

public AcroFields getAcroFields() 

Source Link

Document

Gets a read-only version of AcroFields.

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 {/*  w ww  .j  a  v a  2 s  . c  om*/
            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  ww. j a va 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 {// ww w  . j  a  va2s  .  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  .  ja  v a  2 s .c  om*/
            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());/* w ww  . ja va 2 s. 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
 * /*from   www.j  a  v  a2s  . 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
 * //from   www .j  a va 2  s. c  o m
 * @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);
    }
}

From source file:pdfupdate.PdfUpdate.java

public void readXml(String src, String dest) throws IOException, DocumentException, TransformerException {
    PdfReader reader = new PdfReader(src);
    AcroFields form = reader.getAcroFields();
    XfaForm xfa = form.getXfa();//from  www .  j ava 2s.c o m
    Node node = xfa.getDatasetsNode();
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        if ("data".equals(list.item(i).getLocalName())) {
            node = list.item(i);
            break;
        }
    }
    list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        if ("movies".equals(list.item(i).getLocalName())) {
            node = list.item(i);
            break;
        }
    }
    Transformer tf = TransformerFactory.newInstance().newTransformer();
    tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    tf.setOutputProperty(OutputKeys.INDENT, "yes");
    FileOutputStream os = new FileOutputStream(dest);
    tf.transform(new DOMSource(node), new StreamResult(os));
    reader.close();
}