vals.put("name", sig.getName()); vals.put("contactInfo", sig.getContactInfo()); vals.put("location", sig.getLocation()); vals.put("reason", sig.getReason()); Calendar cal = sig.getSignDate(); if (cal != null) { dateFormat.setTimeZone(cal.getTimeZone());
PDSignature signature = new PDSignature(); signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE); signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED); signature.setName("Example User"); signature.setLocation("Los Angeles, CA"); signature.setReason("Testing"); signature.setSignDate(Calendar.getInstance());
COSDictionary sigDict = sig.getCOSObject(); COSString contents = (COSString) sigDict.getDictionaryObject(COSName.CONTENTS); buf = sig.getSignedContent(fis); int[] byteRange = sig.getByteRange(); if (byteRange.length != 4) if (sig.getName() != null) System.out.println("Name: " + sig.getName()); if (sig.getSignDate() != null) System.out.println("Modified: " + sdf.format(sig.getSignDate().getTime())); String subFilter = sig.getSubFilter(); if (subFilter != null) if (sig.getSignDate() != null) cert.checkValidity(sig.getSignDate().getTime()); System.out.println("Certificate valid at signing time"); if (sig.getSignDate() != null) sig.getSignDate().getTime());
/** * Will return the embedded signature between the byterange gap. * * @param pdfFile The signed pdf file as byte array * @return a byte array containing the signature * @throws IOException if the pdfFile can't be read */ public byte[] getContents(byte[] pdfFile) throws IOException { int[] byteRange = getByteRange(); int begin = byteRange[0]+byteRange[1]+1; int len = byteRange[2]-begin; return getContents(new COSFilterInputStream(pdfFile,new int[] {begin,len})); }
/** * Extracts last Document-Signature from the document. The signature will be set on the signature-field. * * @param document to get the Signature from * @throws IOException */ private void getRelevantSignature(PDDocument document) throws IOException { // we can't use getLastSignatureDictionary() because this will fail (see PDFBOX-3978) // if a signature is assigned to a pre-defined empty signature field that isn't the last. // we get the last in time by looking at the offset in the PDF file. SortedMap<Integer, PDSignature> sortedMap = new TreeMap<>(); for (PDSignature sig : document.getSignatureDictionaries()) { int sigOffset = sig.getByteRange()[1]; sortedMap.put(sigOffset, sig); } if (sortedMap.size() > 0) { PDSignature lastSignature = sortedMap.get(sortedMap.lastKey()); COSBase type = lastSignature.getCOSObject().getItem(COSName.TYPE); if (type.equals(COSName.SIG)) { signature = lastSignature; } } }
/** * Prepares the TimeStamp-Signature and starts the saving-process. * * @param document given Pdf * @param output Where the file will be written * @throws IOException */ public void signDetached(PDDocument document, OutputStream output) throws IOException { int accessPermissions = SigUtils.getMDPPermission(document); if (accessPermissions == 1) { throw new IllegalStateException( "No changes to the document are permitted due to DocMDP transform parameters dictionary"); } // create signature dictionary PDSignature signature = new PDSignature(); signature.setType(COSName.DOC_TIME_STAMP); signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE); signature.setSubFilter(COSName.getPDFName("ETSI.RFC3161")); // No certification allowed because /Reference not allowed in signature directory // see ETSI EN 319 142-1 Part 1 and ETSI TS 102 778-4 // http://www.etsi.org/deliver/etsi_en%5C319100_319199%5C31914201%5C01.01.00_30%5Cen_31914201v010100v.pdf // http://www.etsi.org/deliver/etsi_ts/102700_102799/10277804/01.01.01_60/ts_10277804v010101p.pdf // register signature dictionary and sign interface document.addSignature(signature, this); // write incremental (only for signing purpose) document.saveIncremental(output); }
PDSignature signature = new PDSignature(); signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE); signature.setSubFilter(COSName.getPDFName("ETSI.RFC3161")); signature.setSignDate(Calendar.getInstance());
/** * @param validationCertPool * @param outerCatalog the PDF Dict of the outer document, if the PDFDocument in a enclosed revision. Can be null. * @param document the signed PDFDocument * @param cms the CMS bytes (CAdES signature) * @param inputStream the stream of the whole signed document * @throws IOException */ PdfBoxCMSInfo(CertificatePool validationCertPool, PdfDict outerCatalog, PDDocument document, PDSignature signature, byte[] cms, InputStream inputStream) throws DSSException, IOException { this.validationCertPool = validationCertPool; this.outerCatalog = PdfDssDict.build(outerCatalog); this.cms = cms; this.location = signature.getLocation(); this.signingDate = signature.getSignDate() != null ? signature.getSignDate().getTime() : null; this.signatureByteRange = signature.getByteRange(); final COSDictionary cosDictionary = document.getDocumentCatalog().getCOSDictionary(); final PdfBoxDict documentDict = new PdfBoxDict(cosDictionary, document); documentDictionary = PdfDssDict.build(documentDict); try { if (cms == null) { // due to not very good revision extracting throw new DSSPadesNoSignatureFound(); } signedBytes = signature.getSignedContent(inputStream); } catch (IOException e) { throw new DSSException(e); } }
/** * This will get the signature or null if there is none. * * @return The signature. */ public PDSignature getSignature() { PDSignature signature = null; COSDictionary sig = (COSDictionary) catalog.getDictionaryObject(COSName.SIG); if (sig != null) { signature = new PDSignature(sig); } return signature; }
@Override public void createSignature(PDSignatureField pdSignatureField, PDPage page, String signerName) throws IOException { PDSignature pdSignature = new PDSignature(); PDAnnotationWidget widget = pdSignatureField.getWidgets().get(0); pdSignatureField.setValue(pdSignature); widget.setPage(page); page.getAnnotations().add(widget); if (!signerName.isEmpty()) { pdSignature.setName(signerName); } pdfStructure.setPdSignature(pdSignature); LOG.info("PDSignature has been created"); }
/** * Embeds the new signature into the document, by copying the rest of the document * * @param docBytes byte array of the document * @param output target, where the file will be written * @throws IOException */ private void embedNewSignatureIntoDocument(byte[] docBytes, OutputStream output) throws IOException { int[] byteRange = signature.getByteRange(); output.write(docBytes, byteRange[0], byteRange[1] + 1); output.write(changedEncodedSignature); int addingLength = byteRange[2] - byteRange[1] - 2 - changedEncodedSignature.length; byte[] zeroes = Hex.getBytes(new byte[(addingLength + 1) / 2]); output.write(zeroes); output.write(docBytes, byteRange[2] - 1, byteRange[3] + 1); }
if (preferredSignatureSize > 0) sigObject.setContents(new byte[preferredSignatureSize]); sigObject.setContents(new byte[SignatureOptions.DEFAULT_SIGNATURE_SIZE]); sigObject.setByteRange(RESERVE_BYTE_RANGE); sigObject.getCOSObject().setNeedToBeUpdated(true);
if (sig.getSignDate() != null) certFromSignedData.checkValidity(sig.getSignDate().getTime()); System.out.println("Certificate valid at signing time"); if (sig.getSignDate() != null) verifyCertificateChain(certificatesStore, certFromSignedData, sig.getSignDate().getTime());
/** * Gets the certificate information of a signature. * * @param signature the signature of the document. * @param fileName of the document. * @return the CertSignatureInformation containing all certificate information * @throws CertificateProccessingException when there is an error processing the certificates * @throws IOException on a data processing error */ public CertSignatureInformation getLastCertInfo(PDSignature signature, String fileName) throws CertificateProccessingException, IOException { try (FileInputStream documentInput = new FileInputStream(fileName)) { byte[] signatureContent = signature.getContents(documentInput); return getCertInfo(signatureContent); } }
/** * Search acroform fields for signature field with specific signature dictionary. * * @param fieldIterator iterator on all fields. * @param sigObject signature object (the /V part). * @return a signature field if found, or null if none was found. */ private PDSignatureField findSignatureField(Iterator<PDField> fieldIterator, PDSignature sigObject) { PDSignatureField signatureField = null; while (fieldIterator.hasNext()) { PDField pdField = fieldIterator.next(); if (pdField instanceof PDSignatureField) { PDSignature signature = ((PDSignatureField) pdField).getSignature(); if (signature != null && signature.getCOSObject().equals(sigObject.getCOSObject())) { signatureField = (PDSignatureField) pdField; } } } return signatureField; }
final String subFilter = signature.getSubFilter(); if (DSSUtils.isBlank(subFilter)) { continue; byte[] cms = new PdfBoxDict(signature.getDictionary(), doc).get("Contents"); final String byteRange = Arrays.toString(signature.getByteRange()); Map<PdfSignatureOrDocTimestampInfo, Boolean> innerSignaturesFound = byteRangeMap.get(byteRange); if (innerSignaturesFound == null) {
@Override public String getLocation() { if (this.signature != null) { return this.signature.getLocation(); } return null; }
@Override public String getName() { if (this.signature != null) { return this.signature.getName(); } return null; }
@Override public String getContactInfo() { if (this.signature != null) { return this.signature.getContactInfo(); } return null; }
@Override public String getReason() { if (this.signature != null) { return this.signature.getReason(); } return null; }