|
| 1 | +package es.keensoft.test; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.InputStream; |
| 6 | +import java.io.Serializable; |
| 7 | +import java.security.KeyStore; |
| 8 | +import java.security.Provider; |
| 9 | +import java.security.cert.X509Certificate; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +import org.alfresco.service.namespace.QName; |
| 15 | + |
| 16 | +import com.itextpdf.text.pdf.AcroFields; |
| 17 | +import com.itextpdf.text.pdf.PdfPKCS7; |
| 18 | +import com.itextpdf.text.pdf.PdfReader; |
| 19 | + |
| 20 | +import es.keensoft.alfresco.model.SignModel; |
| 21 | + |
| 22 | +public class PAdESLTV { |
| 23 | + |
| 24 | + public static void main(String... args) throws Exception { |
| 25 | + |
| 26 | + |
| 27 | + ClassLoader classLoader = PAdESLTV.class.getClassLoader(); |
| 28 | + File file = new File(classLoader.getResource("PADESLTV.pdf").getFile()); |
| 29 | + |
| 30 | + Provider provider = null; |
| 31 | + try { |
| 32 | + Class c = |
| 33 | + Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider"); |
| 34 | + java.security.Security.insertProviderAt((java.security.Provider)c.newInstance(), 2000); |
| 35 | + //provider = "BC"; |
| 36 | + provider = (Provider)c.newInstance(); |
| 37 | + |
| 38 | + } catch(Exception e) { |
| 39 | + provider = null; |
| 40 | + // provider is not available } |
| 41 | + } |
| 42 | + |
| 43 | + InputStream is = new FileInputStream(file); |
| 44 | + |
| 45 | + PdfReader reader = new PdfReader(is); |
| 46 | + AcroFields af = reader.getAcroFields(); |
| 47 | + ArrayList<String> names = af.getSignatureNames(); |
| 48 | + KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 49 | + ks.load(null, null); |
| 50 | + ArrayList<Map<QName, Serializable>> aspects = new ArrayList<Map<QName, Serializable>>(); |
| 51 | + for (String name : names) { |
| 52 | + PdfPKCS7 pk = af.verifySignature(name); |
| 53 | + X509Certificate certificate = pk.getSigningCertificate(); |
| 54 | + |
| 55 | + //Set aspect properties for each signature |
| 56 | + Map<QName, Serializable> aspectSignatureProperties = new HashMap<QName, Serializable>(); |
| 57 | + if (pk.getSignDate() != null) aspectSignatureProperties.put(SignModel.PROP_DATE, pk.getSignDate().getTime()); |
| 58 | + aspectSignatureProperties.put(SignModel.PROP_CERTIFICATE_PRINCIPAL, certificate.getSubjectX500Principal().toString()); |
| 59 | + aspectSignatureProperties.put(SignModel.PROP_CERTIFICATE_SERIAL_NUMBER, certificate.getSerialNumber().toString()); |
| 60 | + aspectSignatureProperties.put(SignModel.PROP_CERTIFICATE_NOT_AFTER, certificate.getNotAfter()); |
| 61 | + aspectSignatureProperties.put(SignModel.PROP_CERTIFICATE_ISSUER, certificate.getIssuerX500Principal().toString()); |
| 62 | + aspects.add(aspectSignatureProperties); |
| 63 | + } |
| 64 | + |
| 65 | + System.out.println(aspects); |
| 66 | + |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments