I have partially read itextpdf's paper about digital signatures and from it, I started developing my code:
public class ServerSignature implements ExternalSignature {
public String getHashAlgorithm() {
return DigestAlgorithms.SHA256;
}
public String getEncryptionAlgorithm() {
return "RSA";
}
public byte[] sign(byte[] message) throws GeneralSecurityException {
byte[] s = Base64.getDecoder().decode(SPMService.this.signature);
return s;
}
}
public void sign(Certificate[] chain, CryptoStandard subfilter, String reason,
String location) throws GeneralSecurityException, DocumentException, IOException {
// Creating the reader and the stamper
System.out.println("SRC: " + this.SRC);
PdfReader reader = new PdfReader(this.SRC);
FileOutputStream os = new FileOutputStream(this.DEST);
PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0', null, true);
// Creating the appearance
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setReason(reason);
appearance.setLocation(location);
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
// Creating the signature
ExternalDigest digest = new BouncyCastleDigest();
ExternalSignature signature = new ServerSignature();
MakeSignature.signDetached(appearance, digest, signature, chain, null, null, null, 0, subfilter);
this.verifySignatures(this.DEST);
}
private void verifySignature(AcroFields fields, String name) throws GeneralSecurityException, IOException {
// Returns true
System.out.println("Signature covers whole document: " + fields.signatureCoversWholeDocument(name));
// Returns the right revisions, e.g. 1 of 1
System.out.println("Document revision: " + fields.getRevision(name) + " of " + fields.getTotalRevisions());
PdfPKCS7 pkcs7 = fields.verifySignature(name);
// Returns SHA256
System.out.println("Digest algorithm: " + pkcs7.getHashAlgorithm());
//Returns RSA
System.out.println("Encryption algorithm: " + pkcs7.getEncryptionAlgorithm());
// Return /adbe.pkcs7.detached
System.out.println("Filter subtype: " + pkcs7.getFilterSubtype());
// Get the signing certificate to find out the name of the signer.
X509Certificate cert = (X509Certificate) pkcs7.getSigningCertificate();
System.out.println("Name of the signer: " + CertificateInfo.getSubjectFields(cert).getField("CN"));
if (pkcs7.getSignName() != null) {
System.out.println("Alternative name of the signer: " + pkcs7.getSignName());
}
// The following log is returning false always. Why?
System.out.println("Integrity check OK? " + pkcs7.verify());
}
private void verifySignatures(String src) throws IOException, GeneralSecurityException {
Security.addProvider(new BouncyCastleProvider());
PdfReader reader = new PdfReader(src);
AcroFields fields = reader.getAcroFields();
ArrayList<String> names = fields.getSignatureNames();
for (String name : names) {
System.out.println("===== " + name + " =====");
verifySignature(fields, name);
}
System.out.println();
}
public void start() throws GeneralSecurityException, DocumentException, IOException {
CertificateFactory factory = CertificateFactory.getInstance("X.509");
// certificate I received in the Controller
InputStream targetStream = new ByteArrayInputStream(this.certificate.getBytes());
Certificate[] chain = new Certificate[1];
chain[0] = factory.generateCertificate(targetStream);
this.sign(chain, CryptoStandard.CMS, "Reason", "Location");
}
With this code, when I open a signed document with Adobe Acrobat Reader, the signature is visible and is also embedded in the document. The problem is that it's with a red mark stating that the "Document has been altered or corrupted since it was signed".
Can someone explain to me what's missing or if my logic is wrong?
ServerSignature
: In the embedded signature container the signature bytes sign the wrong hash value (not the hash of the signed attributes but some other value). – mkl