I'm using this to sign a PDF document, as the related question captures my scenario. However, I'm unable to generate the LTV (I'm using this as a guide) because I'm not able to instantiate valid OscpClient and CrlClient objects to use in the addLtv function. I'm using iTextSharp 5.5.10.
Is there some documentation that can point to the right direction?
Here is what I have at the moment:
try {
// Getting the certificate
X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2 x509 = selectCert(store.Certificates, "<<some descriptor>>");
// Initialise the various objects
PdfReader pdfReader = new PdfReader("<<path to source file>>");
FileStream signedPdf = new FileStream("<<path to dest file>>", FileMode.Create);
PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, signedPdf, '\0', null, true);
pdfStamper.MoreInfo = pdfReader.Info;
PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance;
X509Certificate2Signature externalSignature = new X509Certificate2Signature(x509, "SHA-256");
Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser();
Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] {
cp.ReadCertificate(x509.RawData)
};
// Initialise the IOcspClient implementor
// https://itextsupport.com/apidocs/itext5/latest/com/itextpdf/text/pdf/security/OcspClientBouncyCastle.html
OcspClientBouncyCastle ocsp = new OcspClientBouncyCastle(
// The first point that I get stuck on the documentation
new OCSPVerifier()
);
// Initialise the ICrlClient implementor
// https://itextsupport.com/apidocs/itext5/latest/com/itextpdf/text/pdf/security/CrlClientOnline.html
CrlClientOnline crl = new CrlClientOnline(
// https://stackoverflow.com/a/40894818
GetCrlDistributionPointURI(x509)
);
List<String> names = pdfStamper.AcroFields.GetSignatureNames();
String sigName = names[names.Capacity - 1];
PdfPKCS7 pkcs7 = pdfStamper.AcroFields.VerifySignature(sigName);
// The long term validation
if (pkcs7.IsTsp) {
pdfStamper.LtvVerification.AddVerification(
sigName, ocsp, crl,
LtvVerification.CertificateOption.SIGNING_CERTIFICATE,
LtvVerification.Level.OCSP_CRL,
LtvVerification.CertificateInclusion.NO
);
}
else {
foreach (String name in names) {
pdfStamper.LtvVerification.AddVerification(
name, ocsp, crl,
LtvVerification.CertificateOption.WHOLE_CHAIN,
LtvVerification.Level.OCSP_CRL,
LtvVerification.CertificateInclusion.NO
);
}
}
// Sign the doc and
MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS);
pdfStamper.Close();
return "Done";
}
catch (Exception ex) {
return ex.Message;
}