pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Osdeni José Sadzinski <osd...@virtualiza.com.br>
Subject Verifier digital signatures
Date Mon, 06 Jun 2016 20:59:44 GMT
I am creating a system where I need to validate a PDF document, and or
"the" digital signatures contained therein are valid.

Following my understanding created the following script with the intention
to open the PDF, list the gifts signatures, retrieve the signed content and
the public key of these signatures and tried to compare with the verify
method.

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
import org.bouncycastle.jcajce.provider.asymmetric.x509.CertificateFactory;

public class Verificador4
{
public static void main (String [] args) throws FileNotFoundException {
Pathfile String = "/var/www/html/digital-signatures/pdf/b1.pdf";

PDDocument doc = null;

List <PDSignature> signatures = null;

try {
File file = new File (pathfile);


VERIFIED boolean = false;
doc = doc.load (new File (pathfile));

doc.getSignatureDictionaries signatures = ();
Iterator i = signatures.iterator ();

while (i.hasNext ()) {
PDSignature p = (PDSignature) i.next ();

byte [] = signatureAsBytes p.getContents (new FileInputStream (file));
byte [] = signedContentAsBytes p.getSignedContent (new FileInputStream
(file));

CertificateFactory cf = new CertificateFactory ();
Collection <Object> c = cf.engineGenerateCertificates (new
ByteArrayInputStream (signatureAsBytes));
Iterator it = c.iterator ();
PublicKey pubkey = null;
X509Certificate cert509 = null;
while (it.hasNext ())
{
cert509 = (X509Certificate) it.next ();
pubkey cert509.getPublicKey = ();
}

System.out.println (cert509.getSigAlgName ());

Signature signer = Signature.getInstance (cert509.getSigAlgName ());

signer.initVerify (pubkey);
signer.update (signedContentAsBytes);
VERIFIED = signer.verify (signatureAsBytes);
}

} Catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace ();
} Catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace ();
} Catch (CertificateException e) {
// TODO Auto-generated catch block
e.printStackTrace ();
} Catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace ();
} Catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace ();
} Catch (SignatureException e) {
// TODO Auto-generated catch block
e.printStackTrace ();
}
}

}



But of the following error: Signature length not correct: got 15000 but was
expecting 256

The Hash of this document is: SHA256withRSA

I believe it may be the public key that is unable to decode, or is missing
something to interpret this key: Something between these steps.

byte [] = signedContentAsBytes p.getSignedContent (new FileInputStream
(file));
VERIFIED = signer.verify (signatureAsBytes);

Can someone help me?

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message