public static string SignFile(string fileName, PdfSignatureParameters parameters, IDigitalSignatureCertificateSelector certificateSelector) { if (string.IsNullOrWhiteSpace(fileName)) { throw new ArgumentException("Filename must be given", nameof(fileName)); } if (!File.Exists(fileName)) { throw new ArgumentException($"File {fileName} not found."); } if (parameters == null) { throw new ArgumentNullException(nameof(parameters)); } if (certificateSelector == null) { throw new ArgumentNullException(nameof(certificateSelector)); } string tempPath = string.Empty; try { tempPath = GetTempPath(parameters.TempFolderPath); string targetFilePath = GetTargetFilePath(parameters.TempFolderPath, parameters.TargetFilePath); var signingCertificates = CertificateHelper.GetSigningCertificates(certificateSelector); // Two clients for checking certificate revocation // * Online Certificate Status Protocol (OCSP) client // * Certificate Revocation Lists (CRL) client with online checking // Certificate will be checked when the signature is made OcspClientBouncyCastle oscpClient = new OcspClientBouncyCastle(null); List<ICrlClient> clrClients = new List<ICrlClient> { new CrlClientOnline(signingCertificates.FinalChain) }; using (FileStream targetFileStream = new FileStream(targetFilePath, FileMode.Create)) using (PdfReader reader = new PdfReader(fileName)) { PdfStamper stamper = PdfStamper.CreateSignature(reader, targetFileStream, '0', tempPath, true); PdfSignatureAppearance appearance = GetPdfSignatureAppearance(signingCertificates, stamper, reader, parameters); CreateSignature(signingCertificates, appearance, clrClients, oscpClient); } return targetFilePath; } finally { if (!string.IsNullOrWhiteSpace(tempPath) && File.Exists(tempPath)) { File.Delete(tempPath); } } }
public static SigningCertificates GetSigningCertificates(IDigitalSignatureCertificateSelector certificateSelector) { SigningCertificates signingCertificates = new SigningCertificates(); X509CertificateParser parser = new X509CertificateParser(); X509Store x509Store = new X509Store(StoreLocation.CurrentUser); x509Store.Open(OpenFlags.ReadOnly); X509Certificate2Collection validCertificates = FindDigitalSignatureCertificates(x509Store); signingCertificates.X509Certificate2 = certificateSelector.SelectCertificate(validCertificates.Cast<X509Certificate2>()); signingCertificates.X509Certificate = parser.ReadCertificate(signingCertificates.X509Certificate2.Export(X509ContentType.Cert)); signingCertificates.FinalChain = CreateChain(signingCertificates.X509Certificate, signingCertificates.X509Certificate2, parser); return signingCertificates; }
public static SigningCertificates GetSigningCertificates(IDigitalSignatureCertificateSelector certificateSelector) { SigningCertificates signingCertificates = new SigningCertificates(); X509CertificateParser parser = new X509CertificateParser(); X509Store x509Store = new X509Store(StoreLocation.CurrentUser); x509Store.Open(OpenFlags.ReadOnly); X509Certificate2Collection validCertificates = FindDigitalSignatureCertificates(x509Store); signingCertificates.X509Certificate2 = certificateSelector.SelectCertificate(validCertificates.Cast <X509Certificate2>()); signingCertificates.X509Certificate = parser.ReadCertificate(signingCertificates.X509Certificate2.Export(X509ContentType.Cert)); signingCertificates.FinalChain = CreateChain(signingCertificates.X509Certificate, signingCertificates.X509Certificate2, parser); return(signingCertificates); }
public void Setup() { certificateSelector = new FirstCertificateSelector(); }
public static string SignFile(string fileName, PdfSignatureParameters parameters, IDigitalSignatureCertificateSelector certificateSelector) { if (string.IsNullOrWhiteSpace(fileName)) { throw new ArgumentException("Filename must be given", nameof(fileName)); } if (!File.Exists(fileName)) { throw new ArgumentException($"File {fileName} not found."); } if (parameters == null) { throw new ArgumentNullException(nameof(parameters)); } if (certificateSelector == null) { throw new ArgumentNullException(nameof(certificateSelector)); } string tempPath = string.Empty; try { tempPath = GetTempPath(parameters.TempFolderPath); string targetFilePath = GetTargetFilePath(parameters.TempFolderPath, parameters.TargetFilePath); var signingCertificates = CertificateHelper.GetSigningCertificates(certificateSelector); // Two clients for checking certificate revocation // * Online Certificate Status Protocol (OCSP) client // * Certificate Revocation Lists (CRL) client with online checking // Certificate will be checked when the signature is made OcspClientBouncyCastle oscpClient = new OcspClientBouncyCastle(null); List <ICrlClient> clrClients = new List <ICrlClient> { new CrlClientOnline(signingCertificates.FinalChain) }; using (FileStream targetFileStream = new FileStream(targetFilePath, FileMode.Create)) using (PdfReader reader = new PdfReader(fileName)) using (PdfStamper stamper = PdfStamper.CreateSignature(reader, targetFileStream, '0', tempPath, true)) { PdfSignatureAppearance appearance = GetPdfSignatureAppearance(signingCertificates, stamper, reader, parameters); CreateSignature(signingCertificates, appearance, clrClients, oscpClient); } return(targetFilePath); } finally { try { if (!string.IsNullOrWhiteSpace(tempPath) && File.Exists(tempPath)) { File.Delete(tempPath); } } catch (Exception) { } } }