I am trying to verify digitally signed PDF document in Java.
I'm using Apache PDFBox 2.0.6 to get the signature and the original PDF that was signed, then I'm using Bouncy Castle to verify detached signature(calculate the hash of the original file, verify the signature using signer's public key and compare the results).
I read this article and tried to get the signature bytes and the original PDF bytes using this code:
PDDocument doc = PDDocument.load(signedPDF);
byte[] origPDF = doc.getSignatureDictionaries().get(0).getSignedContent(signedPDF);
byte[] signature = doc.getSignatureDictionaries().get(0).getContents(signedPDF);
But, when I save the origPDF to a file I notice that it still has the signature field that the original PDF that was signed didn't have. Also, the size of the save origPDF is 21 kb, while the size of the original PDF was 15 kb. That's probably because of the signature fields.
However, when I try to strip signature fields from the origPDF like this:
public byte[] stripCryptoSig(byte[] signedPDF) throws IOException {
PDDocument pdDoc = PDDocument.load(signedPDF);
PDDocumentCatalog catalog = pdDoc.getDocumentCatalog();
PDAcroForm form = catalog.getAcroForm();
List<PDField> acroFormFields = form.getFields();
for (PDField field: acroFormFields) {
if (field.getFieldType().equalsIgnoreCase("Sig")) {
System.out.println("START removing Sign Flags");
field.setReadOnly(true);
field.setRequired(false);
field.setNoExport(true);
System.out.println("END removing Sign Flags");
/*System.out.println("START flattenning field");
field.getAcroForm().flatten();
field.getAcroForm().refreshAppearances();
System.out.println("END flattenning field");
*/
field.getAcroForm().refreshAppearances();
}
}
I get the following warrnings:
WARNING: Invalid dictionary, found: '[' but expected: '/' at offset 15756
WARNING: Appearance generation for signature fields not yet implemented - you need to generate/update that manually
And, when I open the PDF in Acrobat the signature field is gone, but I see an image of the signature where the signature used to be as part of the PDF page. This is weird since I thought I removed the signature completely by using byte[] origPDF = doc.getSignatureDictionaries().get(0).getSignedContent(signedPDF);
Btw, I call stripCryptoSig(byte[] signedPDF) function on origPDF, so that's not a mistake.
When I try to verify the signature using bouncy castle I get an exception with the message: message-digest attribute value does not match calculated value
I guess this is because the original PDF that was signed and the PDF I get from PDFBox using doc.getSignatureDictionaries().get(0).getSignedContent(signedPDF);
isn't the same.
Here is my bouncy castle verification code:
private SignatureInfo verifySig(byte[] signedData, boolean attached) throws OperatorCreationException, CertificateException, CMSException, IOException {
SignatureInfo signatureInfo = new SignatureInfo();
CMSSignedData cmsSignedData;
if (attached) {
cmsSignedData = new CMSSignedData(signedData);
}
else {
PDFUtils pdfUtils = new PDFUtils();
pdfUtils.init(signedData);
signedData = pdfUtils.getSignature(signedData);
byte[] sig = pdfUtils.getSignedContent(signedData);
cmsSignedData = new CMSSignedData(new CMSProcessableByteArray(signedData), sig);
}
SignerInformationStore sis = cmsSignedData.getSignerInfos();
Collection signers = sis.getSigners();
Store certStore = cmsSignedData.getCertificates();
Iterator it = signers.iterator();
signatureInfo.setValid(false);
while (it.hasNext()) {
SignerInformation signer = (SignerInformation) it.next();
Collection certCollection = certStore.getMatches(signer.getSID());
Iterator certIt = certCollection.iterator();
X509CertificateHolder cert = (X509CertificateHolder) certIt.next();
if(signer.verify(new JcaSimpleSignerInfoVerifierBuilder().build(cert))){
signatureInfo.setValid(true);
if (attached) {
CMSProcessableByteArray userData = (CMSProcessableByteArray) cmsSignedData.getSignedContent();
signatureInfo.setSignedDoc((byte[]) userData.getContent());
}
else {
signatureInfo.setSignedDoc(signedData);
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String signedOnDate = "null";
String validFromDate = "null";
String validToDate = "null";
Date signedOn = this.getSignatureDate(signer);
Date validFrom = cert.getNotBefore();
Date validTo = cert.getNotAfter();
if(signedOn != null) {
signedOnDate = sdf.format(signedOn);
}
if(validFrom != null) {
validFromDate = sdf.format(validFrom);
}
if(validTo != null) {
validToDate = sdf.format(validTo);
}
DefaultAlgorithmNameFinder algNameFinder = new DefaultAlgorithmNameFinder();
signatureInfo.setSignedBy(IETFUtils.valueToString(cert.getSubject().getRDNs(BCStyle.CN)[0].getFirst().getValue()));
signatureInfo.setSignedOn(signedOn);
signatureInfo.setIssuer(IETFUtils.valueToString(cert.getIssuer().getRDNs(BCStyle.CN)[0].getFirst().getValue()));
signatureInfo.setValidFrom(validFrom);
signatureInfo.setValidTo(validTo);
signatureInfo.setVersion(String.valueOf(cert.getVersion()));
signatureInfo.setSignatureAlg(algNameFinder.getAlgorithmName(signer.getDigestAlgorithmID()) + " WTIH " + algNameFinder.getAlgorithmName(cert.getSubjectPublicKeyInfo().getAlgorithmId()));
/*signatureInfo.put("Signed by", IETFUtils.valueToString(cert.getSubject().getRDNs(BCStyle.CN)[0].getFirst().getValue()));
signatureInfo.put("Signed on", signedOnDate);
signatureInfo.put("Issuer", IETFUtils.valueToString(cert.getIssuer().getRDNs(BCStyle.CN)[0].getFirst().getValue()));
signatureInfo.put("Valid from", validFromDate);
signatureInfo.put("Valid to", validToDate);
signatureInfo.put("Version", "V" + String.valueOf(cert.getVersion()));
signatureInfo.put("Signature algorithm", algNameFinder.getAlgorithmName(signer.getDigestAlgorithmID()) + " WTIH " + algNameFinder.getAlgorithmName(cert.getSubjectPublicKeyInfo().getAlgorithmId()));*/
break;
}
}
return signatureInfo;
}
getSignedContent()
returns the PDF without the signature content string. This isn't a real PDF. See the ShowSignature.java example from the source code download on how to verify a signature. If this doesn't help, please edit your question. – Tilman Hausherr