Class AddValidationInformation
- java.lang.Object
-
- org.apache.pdfbox.examples.signature.validation.AddValidationInformation
-
public class AddValidationInformation extends java.lang.ObjectAn example for adding Validation Information to a signed PDF, inspired by ETSI TS 102 778-4 V1.1.2 (2009-12), Part 4: PAdES Long Term - PAdES-LTV Profile. This procedure appends the Validation Information of the last signature (more precise its signer(s)) to a copy of the document. The signature and the signed data will not be touched and stay valid.
-
-
Field Summary
Fields Modifier and Type Field Description private CertInformationCollectorcertInformationHelperprivate java.util.Map<java.security.cert.X509Certificate,COSStream>certMapprivate COSArraycertsprivate COSArraycorrespondingCRLsprivate COSArraycorrespondingOCSPsprivate COSArraycrlsprivate PDDocumentdocumentprivate java.util.Set<java.security.cert.X509Certificate>foundRevocationInformationprivate static org.apache.commons.logging.LogLOGprivate java.util.Set<java.security.cert.X509Certificate>ocspCheckedprivate COSArrayocspsprivate java.util.CalendarsignDateprivate COSDictionaryvriBase
-
Constructor Summary
Constructors Constructor Description AddValidationInformation()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidaddAllCertsToCertArray()Adds all certs to the certs-array.private voidaddCrlRevocationInfo(CertInformationCollector.CertSignatureInformation certInfo)Fetches and adds CRL data to storage for the given Certificate.private voidaddExtensions(PDDocumentCatalog catalog)Adds Extensions to the document catalog.private voidaddOcspData(java.security.cert.X509Certificate certificate, java.security.cert.X509Certificate issuerCertificate, java.lang.String ocspURL)private voidaddOcspData(CertInformationCollector.CertSignatureInformation certInfo)Fetches and adds OCSP data to storage for the given Certificate.private voidaddRevocationData(CertInformationCollector.CertSignatureInformation certInfo)Fetches and adds revocation information based on the certInfo to the DSS.private voidaddRevocationDataRecursive(CertInformationCollector.CertSignatureInformation certInfo)Tries to get Revocation Data (first OCSP, else CRL) from the given Certificate Chain.private voiddoValidation(java.lang.String filename, java.io.OutputStream output)Fetches certificate information from the last signature of the document and appends a DSS with the validation information to the document.private voidfetchCrlData(CertInformationCollector.CertSignatureInformation certInfo)Tries to fetch and add CRL Data to its containers.private booleanfetchOcspData(CertInformationCollector.CertSignatureInformation certInfo)Tries to fetch and add OCSP Data to its containers.private static <T extends COSBase & COSUpdateInfo>
TgetOrCreateDictionaryEntry(java.lang.Class<T> clazz, COSDictionary parent, java.lang.String name)Gets or creates a dictionary entry.static voidmain(java.lang.String[] args)private voidupdateVRI(CertInformationCollector.CertSignatureInformation certInfo, COSDictionary vri)private static voidusage()voidvalidateSignature(java.io.File inFile, java.io.File outFile)Signs the given PDF file.private COSStreamwriteDataToStream(byte[] data)Creates a Flate encodedCOSStreamobject with the given data.
-
-
-
Field Detail
-
LOG
private static final org.apache.commons.logging.Log LOG
-
certInformationHelper
private CertInformationCollector certInformationHelper
-
correspondingOCSPs
private COSArray correspondingOCSPs
-
correspondingCRLs
private COSArray correspondingCRLs
-
vriBase
private COSDictionary vriBase
-
ocsps
private COSArray ocsps
-
crls
private COSArray crls
-
certs
private COSArray certs
-
certMap
private final java.util.Map<java.security.cert.X509Certificate,COSStream> certMap
-
document
private PDDocument document
-
foundRevocationInformation
private final java.util.Set<java.security.cert.X509Certificate> foundRevocationInformation
-
signDate
private java.util.Calendar signDate
-
ocspChecked
private final java.util.Set<java.security.cert.X509Certificate> ocspChecked
-
-
Method Detail
-
validateSignature
public void validateSignature(java.io.File inFile, java.io.File outFile) throws java.io.IOExceptionSigns the given PDF file.- Parameters:
inFile- input PDF fileoutFile- output PDF file- Throws:
java.io.IOException- if the input file could not be read
-
doValidation
private void doValidation(java.lang.String filename, java.io.OutputStream output) throws java.io.IOExceptionFetches certificate information from the last signature of the document and appends a DSS with the validation information to the document.- Parameters:
filename- in file to extract signatureoutput- where to write the changed document- Throws:
java.io.IOException
-
getOrCreateDictionaryEntry
private static <T extends COSBase & COSUpdateInfo> T getOrCreateDictionaryEntry(java.lang.Class<T> clazz, COSDictionary parent, java.lang.String name) throws java.io.IOException
Gets or creates a dictionary entry. If existing checks for the type and sets need to be updated.- Parameters:
clazz- the class of the dictionary entry, must implement COSUpdateInfoparent- where to find the elementname- of the element- Returns:
- a Element of given class, new or existing
- Throws:
java.io.IOException- when the type of the element is wrong
-
addRevocationData
private void addRevocationData(CertInformationCollector.CertSignatureInformation certInfo) throws java.io.IOException
Fetches and adds revocation information based on the certInfo to the DSS.- Parameters:
certInfo- Certificate information from CertInformationHelper containing certificate chains.- Throws:
java.io.IOException
-
addRevocationDataRecursive
private void addRevocationDataRecursive(CertInformationCollector.CertSignatureInformation certInfo) throws java.io.IOException
Tries to get Revocation Data (first OCSP, else CRL) from the given Certificate Chain.- Parameters:
certInfo- from which to fetch revocation data. Will work recursively through its chains.- Throws:
java.io.IOException- when failed to fetch an revocation data.
-
fetchOcspData
private boolean fetchOcspData(CertInformationCollector.CertSignatureInformation certInfo) throws java.io.IOException
Tries to fetch and add OCSP Data to its containers.- Parameters:
certInfo- the certificate info, for it to check OCSP data.- Returns:
- true when the OCSP data has successfully been fetched and added
- Throws:
java.io.IOException- when Certificate is revoked.
-
fetchCrlData
private void fetchCrlData(CertInformationCollector.CertSignatureInformation certInfo) throws java.io.IOException
Tries to fetch and add CRL Data to its containers.- Parameters:
certInfo- the certificate info, for it to check CRL data.- Throws:
java.io.IOException- when failed to fetch, because no validation data could be fetched for data.
-
addOcspData
private void addOcspData(CertInformationCollector.CertSignatureInformation certInfo) throws java.io.IOException, org.bouncycastle.cert.ocsp.OCSPException, CertificateProccessingException, RevokedCertificateException, java.net.URISyntaxException
Fetches and adds OCSP data to storage for the given Certificate.- Parameters:
certInfo- the certificate info, for it to check OCSP data.- Throws:
java.io.IOExceptionorg.bouncycastle.cert.ocsp.OCSPExceptionCertificateProccessingExceptionRevokedCertificateExceptionjava.net.URISyntaxException
-
addOcspData
private void addOcspData(java.security.cert.X509Certificate certificate, java.security.cert.X509Certificate issuerCertificate, java.lang.String ocspURL) throws java.io.IOException, org.bouncycastle.cert.ocsp.OCSPException, CertificateProccessingException, RevokedCertificateException, java.net.URISyntaxException- Throws:
java.io.IOExceptionorg.bouncycastle.cert.ocsp.OCSPExceptionCertificateProccessingExceptionRevokedCertificateExceptionjava.net.URISyntaxException
-
addCrlRevocationInfo
private void addCrlRevocationInfo(CertInformationCollector.CertSignatureInformation certInfo) throws java.io.IOException, RevokedCertificateException, java.security.GeneralSecurityException, CertificateVerificationException, java.net.URISyntaxException
Fetches and adds CRL data to storage for the given Certificate.- Parameters:
certInfo- the certificate info, for it to check CRL data.- Throws:
java.io.IOExceptionjava.net.URISyntaxExceptionRevokedCertificateExceptionjava.security.GeneralSecurityExceptionCertificateVerificationException
-
updateVRI
private void updateVRI(CertInformationCollector.CertSignatureInformation certInfo, COSDictionary vri) throws java.io.IOException
- Throws:
java.io.IOException
-
addAllCertsToCertArray
private void addAllCertsToCertArray() throws java.io.IOExceptionAdds all certs to the certs-array. Make sure that all certificates are inside the certificateStore of certInformationHelper. This should be the only call to fill certs.- Throws:
java.io.IOException
-
writeDataToStream
private COSStream writeDataToStream(byte[] data) throws java.io.IOException
Creates a Flate encodedCOSStreamobject with the given data.- Parameters:
data- to write into the COSStream- Returns:
- COSStream a COSStream object that can be added to the document
- Throws:
java.io.IOException
-
addExtensions
private void addExtensions(PDDocumentCatalog catalog)
Adds Extensions to the document catalog. So that the use of DSS is identified. Described in PAdES Part 4, Chapter 4.4.- Parameters:
catalog- to add Extensions into
-
main
public static void main(java.lang.String[] args) throws java.io.IOException- Throws:
java.io.IOException
-
usage
private static void usage()
-
-