Example usage for com.lowagie.text.pdf PdfName PdfName

List of usage examples for com.lowagie.text.pdf PdfName PdfName

Introduction

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

Prototype

public PdfName(byte bytes[]) 

Source Link

Document

Constructs a PdfName.

Usage

From source file:eu.europa.ec.markt.dss.signature.pades.PAdESProfileLTV.java

License:Open Source License

private void integrateCRL(LTVSignatureValidationCallback callback, PdfStamper stamper,
        PdfDictionary dssDictionary, PdfDictionary sigVriDictionary, PdfDictionary vriDictionary)
        throws IOException {
    if (callback.getCrlsArray().size() > 0) {
        // Reference in the DSS dictionary
        PdfIndirectReference crlsRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(callback.getCrlsArray(), crlsRef, false);
        dssDictionary.put(new PdfName("CRLs"), crlsRef);

        // Array in the signature's VRI dictionary
        PdfIndirectReference sigVriCrlRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(callback.getCrlsArray(), sigVriCrlRef, false);
        sigVriDictionary.put(new PdfName("CRL"), sigVriCrlRef);

        // Build and reference a VRI dictionary for each CRL
        for (X509CRL crl : crlRefs.keySet()) {
            try {
                PdfIndirectReference vriRef = buildVRIDict(stamper, crl);
                MessageDigest md = MessageDigest.getInstance(DigestAlgorithm.SHA1.getName());
                String hexHash = Hex.encodeHexString(md.digest(crl.getSignature())).toUpperCase();
                vriDictionary.put(new PdfName(hexHash), vriRef);
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException();
            }//from w ww .j ava 2 s. c o m
        }
    }
}

From source file:eu.europa.ec.markt.dss.signature.pades.PAdESProfileLTV.java

License:Open Source License

private void integrateOCSP(LTVSignatureValidationCallback callback, PdfStamper stamper,
        PdfDictionary dssDictionary, PdfDictionary sigVriDictionary, PdfDictionary vriDictionary)
        throws IOException {
    if (callback.getOcspsArray().size() > 0) {
        // Reference in the DSS dictionary
        PdfIndirectReference ocspsRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(callback.getOcspsArray(), ocspsRef, false);
        dssDictionary.put(new PdfName("OCSPs"), ocspsRef);

        // Array in the signature's VRI dictionary
        PdfIndirectReference sigVriOcspRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(callback.getOcspsArray(), sigVriOcspRef, false);
        sigVriDictionary.put(new PdfName("OCSP"), sigVriOcspRef);

        // Build and reference a VRI dictionary for each OCSP response
        for (BasicOCSPResp ocsp : ocspRefs.keySet()) {
            try {
                PdfIndirectReference vriRef = buildVRIDict(stamper, ocsp);
                MessageDigest md = MessageDigest.getInstance(DigestAlgorithm.SHA1.getName());
                String hexHash = Hex.encodeHexString(md.digest(ocsp.getSignature())).toUpperCase();
                vriDictionary.put(new PdfName(hexHash), vriRef);
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException();
            }/*from   w  w w. j  a  va2 s .  c  o  m*/
        }
    }
}

From source file:eu.europa.ec.markt.dss.signature.pades.PAdESProfileLTV.java

License:Open Source License

@Override
public Document extendSignatures(Document document, Document originalData, SignatureParameters parameters)
        throws IOException {

    try {/*from  ww w  .j  av a 2 s  .  c om*/
        final PdfReader reader = new PdfReader(document.openStream());
        final ByteArrayOutputStream output = new ByteArrayOutputStream();
        final PdfStamper stamper = new PdfStamper(reader, output, '\0', true);

        LTVSignatureValidationCallback callback = new LTVSignatureValidationCallback(stamper);
        pdfSignatureService.validateSignatures(document.openStream(), callback);

        PdfIndirectReference certsRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(callback.getCertsArray(), certsRef, false);

        PdfDictionary dssDictionary = new PdfDictionary(new PdfName("DSS"));
        PdfDictionary vriDictionary = new PdfDictionary(new PdfName("VRI"));

        PdfDictionary sigVriDictionary = new PdfDictionary();

        integrateCRL(callback, stamper, dssDictionary, sigVriDictionary, sigVriDictionary);

        integrateOCSP(callback, stamper, dssDictionary, sigVriDictionary, sigVriDictionary);

        // Add the signature's VRI dictionary, hashing the signature block from the callback method
        MessageDigest _md = MessageDigest.getInstance(DigestAlgorithm.SHA1.getName());
        String hexHash = Hex.encodeHexString(_md.digest(callback.getSignatureBlock())).toUpperCase();

        PdfIndirectReference sigVriRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(sigVriDictionary, sigVriRef, false);
        vriDictionary.put(new PdfName(hexHash), sigVriRef);
        PdfIndirectReference vriRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(vriDictionary, vriRef, false);

        // Add final objects to DSS dictionary
        dssDictionary.put(new PdfName("VRI"), vriRef);
        dssDictionary.put(new PdfName("Certs"), certsRef);

        PdfIndirectReference dssRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(dssDictionary, dssRef, false);
        reader.getCatalog().put(new PdfName("DSS"), dssRef);

        // /Extensions<</ADBE<</BaseVersion/1.7/ExtensionLevel 5>>>>
        PdfDeveloperExtension etsiExtension = new PdfDeveloperExtension(PdfName.ADBE, new PdfName("1.7"), 5);
        stamper.getWriter().addDeveloperExtension(etsiExtension);
        stamper.getWriter().addToBody(reader.getCatalog(), reader.getCatalog().getIndRef(), false);

        stamper.close();
        output.close();

        Document extendedDocument = new InMemoryDocument(output.toByteArray());

        ByteArrayOutputStream ltvDoc = new ByteArrayOutputStream();

        ITextPDFDocTimeSampService service = new ITextPDFDocTimeSampService();
        byte[] digest = service.digest(extendedDocument.openStream(), parameters);
        TimeStampResponse tsToken = tspSource.getTimeStampResponse(parameters.getDigestAlgorithm(), digest);
        service.sign(extendedDocument.openStream(), tsToken.getTimeStampToken().getEncoded(), ltvDoc,
                parameters);

        return new InMemoryDocument(ltvDoc.toByteArray());

    } catch (DocumentException ex) {
        throw new RuntimeException(ex);
    } catch (SignatureException e) {
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }

}

From source file:eu.europa.ec.markt.dss.signature.pdf.itext.ITextPdfDict.java

License:Open Source License

ITextPdfDict(String dictionaryType) {
    if (dictionaryType != null) {
        wrapped = new PdfDictionary(new PdfName(dictionaryType));
    } else {//from w ww.  j  a v a 2  s  .com
        wrapped = new PdfDictionary();
    }
}

From source file:eu.europa.ec.markt.dss.signature.pdf.itext.ITextPdfDict.java

License:Open Source License

@Override
public PdfDict getAsDict(String name) {
    PdfDictionary asDict = wrapped.getAsDict(new PdfName(name));
    if (asDict == null) {
        return null;
    } else {/* ww w. j  a  v  a 2  s  . c o m*/
        return new ITextPdfDict(asDict);
    }
}

From source file:eu.europa.ec.markt.dss.signature.pdf.itext.ITextPdfDict.java

License:Open Source License

@Override
public PdfArray getAsArray(String name) {
    com.lowagie.text.pdf.PdfArray asArray = wrapped.getAsArray(new PdfName(name));
    if (asArray == null) {
        return null;
    } else {/*from w w  w .ja  v  a 2s  . co  m*/
        return new ITextPdfArray(asArray);
    }
}

From source file:eu.europa.ec.markt.dss.signature.pdf.itext.ITextPdfDict.java

License:Open Source License

@Override
public boolean hasANameWithValue(String name, String value) {
    PdfName asName = wrapped.getAsName(new PdfName(name));
    if (asName == null) {
        LOGGER.info("No value with name " + name);
        return false;
    }/*from  w w w  .ja v  a 2  s  .  c  om*/

    PdfName asValue = new PdfName(value);
    boolean r = asName.equals(asValue);
    LOGGER.info("Comparison of " + asName + "(" + asName.getClass() + ")" + " and " + asValue + " : " + r);
    return r;
}

From source file:eu.europa.ec.markt.dss.signature.pdf.itext.ITextPdfDict.java

License:Open Source License

@Override
public byte[] get(String name) {
    PdfObject val = wrapped.get(new PdfName(name));
    if (val == null) {
        return null;
    }//from w  ww. j a v  a 2 s .  com
    return val.getBytes();
}

From source file:eu.europa.ec.markt.dss.signature.pdf.itext.ITextPDFDocTimeSampService.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
private PdfStamper prepareStamper(InputStream pdfData, OutputStream output, SignatureParameters parameters)
        throws IOException {

    try {/*www. j a  v a  2s  . c o m*/
        PdfReader reader = new PdfReader(pdfData);
        PdfStamper stamper = PdfStamper.createSignature(reader, output, '\0', null, true);

        PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
        appearance.setAcro6Layers(true);
        appearance.setRender(PdfSignatureAppearance.SignatureRenderDescription);

        PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, new PdfName("ETSI.RFC3161"));
        // defined in ETSI TS 102 778-4 A.2
        dic.put(PdfName.TYPE, new PdfName("DocTimeStamp"));

        Calendar cal = Calendar.getInstance();
        if (parameters.getSigningDate() != null) {
            cal.setTime(parameters.getSigningDate());
            appearance.setSignDate(cal);
        }
        dic.setDate(new PdfDate(cal));

        appearance.setCryptoDictionary(dic);

        int csize = getSignatureSize();
        HashMap exc = new HashMap();
        exc.put(PdfName.CONTENTS, new Integer(csize * 2 + 2));

        appearance.preClose(exc);

        return stamper;
    } catch (DocumentException e) {
        throw new IOException(e);
    }

}

From source file:eu.europa.ec.markt.dss.signature.pdf.itext.ITextPDFSignatureService.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
private PdfStamper prepareStamper(InputStream pdfData, OutputStream output, SignatureParameters parameters)
        throws IOException, DocumentException {

    PdfReader reader = new PdfReader(pdfData);
    PdfStamper stp = PdfStamper.createSignature(reader, output, '\0', null, true);

    PdfSignatureAppearance sap = stp.getSignatureAppearance();
    sap.setAcro6Layers(true);//w  w  w.  j  av  a 2 s .c o  m
    sap.setRender(PdfSignatureAppearance.SignatureRenderDescription);

    PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, new PdfName("ETSI.CAdES.detached"));
    Calendar cal = Calendar.getInstance();
    cal.setTime(parameters.getSigningDate());
    sap.setSignDate(cal);
    dic.setDate(new PdfDate(cal));

    if (parameters.getReason() != null) {
        dic.setReason(parameters.getReason());
    }
    if (parameters.getLocation() != null) {
        dic.setLocation(parameters.getLocation());
    }
    if (parameters.getContactInfo() != null) {
        dic.setContact(parameters.getContactInfo());
    }

    sap.setCryptoDictionary(dic);

    int csize = getSignatureSize();
    HashMap exc = new HashMap();
    exc.put(PdfName.CONTENTS, new Integer(csize * 2 + 2));

    sap.preClose(exc);

    return stp;
}