I am trying to sign pdf file programmatically with IText in java with my own provider. When I sign with p12 file all is fine. But when I try to sign with token, i dont have any exception. But when I try to open signed pdf I get this error 0x2726.
Here my signed file: https://drive.google.com/file/d/0Bz1LIekMx5F8WHFDRHpTTUU2SVU/view?usp=sharing pls, Help!
public static void main(String[] args) throws IOException, GeneralSecurityException, DocumentException {
char[] pass = PASSWORD.toCharArray();
MyProvider provider = new MyProvider();
Security.addProvider(provider);
KeyStore ks = null;
try {
ks = KeyStoreUtil.getKeyStore(Storage.MYTOKEN, "TOEKN_PATH", pass, provider);
} catch (KeyStoreException | NoSuchProviderException | FileNotFoundException | NoSuchAlgorithmException | CertificateException | PrivilegedActionException ex) {
Logger.getLogger(PDFSIGN.class.getName()).log(Level.SEVERE, null, ex);
}
PDFSIGN app = new PDFSIGN();
PrivateKey pk = null;
Certificate[] chain = new Certificate[1];
Enumeration<String> als = ks.aliases();
String alias = (String) als.nextElement();
System.err.println(alias);
Certificate cert = ks.getCertificate(alias);
ASN1InputStream ais = new ASN1InputStream(cert.getEncoded());
X509CertificateStructure x509Struct = new X509CertificateStructure((ASN1Sequence) ais.readObject());
ais.close();
X509CertificateObject certificateObject = new X509CertificateObject(x509Struct);
chain[0] = certificateObject;
passw = "123456";
pk = (PrivateKey) ks.getKey(alias, passw.toCharArray());
app.sign(SRC, DEST, chain, pk, DigestAlgorithms.SHA1, provider.getName(), CryptoStandard.CMS, "Test", "Ghent", crlList, null, null, 0);
}
public void sign(String src, String dest, Certificate[] chain, PrivateKey pk, String digestAlgorithm, String provider, CryptoStandard subfilter, String reason, String location, Collection<CrlClient> crlList,
OcspClient ocspClient, TSAClient tsaClient, int estimatedSize)
throws GeneralSecurityException, IOException, DocumentException {
PdfReader reader = new PdfReader(src);
FileOutputStream os = new FileOutputStream(dest);
PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setReason(reason);
appearance.setLocation(location);
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
ExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, provider);
ExternalDigest digest = new ProviderDigest(provider);
MakeSignature.signDetached(appearance, digest, pks, chain, crlList, ocspClient, tsaClient, estimatedSize, subfilter);
}