Java Code Examples for org.apache.pdfbox.pdmodel.PDDocument#getSignatureDictionaries()

The following examples show how to use org.apache.pdfbox.pdmodel.PDDocument#getSignatureDictionaries() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CalculateDigest.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/57926872/signed-pdf-content-digest-that-was-calculated-during-verification-is-diffrent-th">
 * Signed PDF content digest that was calculated during verification is diffrent than decripted digest from signature
 * </a>
 * <br/>
 * <a href="https://drive.google.com/open?id=1UlOZOp-UYllK7Ra35dggccoWdhcb_Ntp">
 * TEST-signed-pades-baseline-b.pdf
 * </a>
 * <p>
 * The code here demonstrates how to retrieve the messageDigest
 * signed attribute value from a signed PDF. For production use
 * obviously some null checks are required.
 * </p>
 */
@Test
public void testExtractMessageDigestAttributeForUser2893427() throws IOException, CMSException {
    try (   InputStream resource = getClass().getResourceAsStream("TEST-signed-pades-baseline-b.pdf")   ) {
        byte[] bytes = IOUtils.toByteArray(resource);
        PDDocument document = Loader.loadPDF(bytes);
        List<PDSignature> signatures = document.getSignatureDictionaries();
        PDSignature sig = signatures.get(0);
        byte[] cmsBytes = sig.getContents(bytes);
        CMSSignedData cms = new CMSSignedData(cmsBytes);
        SignerInformation signerInformation = cms.getSignerInfos().iterator().next();
        Attribute attribute = signerInformation.getSignedAttributes().get(PKCSObjectIdentifiers.pkcs_9_at_messageDigest);
        ASN1Encodable value = attribute.getAttributeValues()[0];
        System.out.printf("MessageDigest attribute value: %s\n", value);
    }
}
 
Example 2
Source File: PdfBoxSignatureService.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private boolean containsFilledSignature(PDDocument pdDocument) {
	try {
		List<PDSignature> signatures = pdDocument.getSignatureDictionaries();
		for (PDSignature pdSignature : signatures) {
			if (pdSignature.getCOSObject().containsKey(COSName.BYTERANGE)) {
				return true;
			}
		}
		return false;
	} catch (IOException e) {
		LOG.warn("Cannot read the existing signature(s)", e);
		return false;
	}
}
 
Example 3
Source File: VRITest.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void vri() throws Exception {
	String path = "/validation/Signature-P-HU_MIC-3.pdf";
	String vriValue = "C41B1DBFE0E816D8A6F99A9DB98FD43960A5CF45";

	PDDocument pdDoc = PDDocument.load(getClass().getResourceAsStream(path));
	List<PDSignature> signatureDictionaries = pdDoc.getSignatureDictionaries();
	assertTrue(Utils.isCollectionNotEmpty(signatureDictionaries));
	PDSignature pdSignature = signatureDictionaries.get(0);
	byte[] contents = pdSignature.getContents(getClass().getResourceAsStream(path));
	byte[] digest = DSSUtils.digest(DigestAlgorithm.SHA1, contents);
	assertEquals(vriValue, Utils.upperCase(Utils.toHex(digest)));

	// We can't use CMSSignedData, the pdSignature content is trimmed (000000)
}
 
Example 4
Source File: PDFVerify.java    From signer with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testTimeStampOnly() {
	

		String filePath = "caminho do arquivo";
		
		PDDocument document;
		try {
			document = PDDocument.load(new File(filePath));
			Timestamp varTimeStamp = null;

		for (PDSignature sig : document.getSignatureDictionaries()) {
				COSDictionary sigDict = sig.getCOSObject();
				COSString contents = (COSString) sigDict.getDictionaryObject(COSName.CONTENTS);
				FileInputStream fis = new FileInputStream(filePath);
				byte[] buf = null;

				try {
					buf = sig.getSignedContent(fis);
				} finally {
					fis.close();
				}

				CAdESTimeStampSigner varCAdESTimeStampSigner = new CAdESTimeStampSigner();
				varTimeStamp = varCAdESTimeStampSigner.checkTimeStampPDFWithContent(contents.getBytes(), buf);
			}
		if (varTimeStamp != null){
			System.out.println("Carimbo do tempo");
			System.out.println(varTimeStamp.getTimeStampAuthorityInfo());
			System.out.println(varTimeStamp.getSerialNumber());
			System.out.println(varTimeStamp.getCertificates());
			System.out.println(varTimeStamp.getTimeStamp());				
			
		}
		
		
			
					
		assertTrue(true);
		
	} catch (IOException e) {	
		e.printStackTrace();
		assertTrue(false);
	}
}