2

I‘m trying to sign a existing PDF with a certificate that’s located in my local PCs store. Based on this article I identified he correct certificate of type X509Certificate2.

For signing I want to use EvoPdf (unfortnately I can't use iTextSharp here; this would make things easier) and the demo for signing uses a DigitalCertificate which doesn’t provide the information (especially the subject property which is almost totally different in the two classes) I need to identify the correct certificate (I could manually identify the correct certificate from DigitalCertificatesStore.GetCertificates(CertSystemStore.PersonalCertificates) but that doesn’t help me much since the certificate might change and I need a robust and lasting solution. What I did right now is 1st: identify the correct certificate from X509Store(StoreName.My) and keep the SerialNumber (of type String) and compare it to the SerialNumber (of type Byte[]) of the DigitalCertificate which is the best change to get a match:

            foreach (DigitalCertificate cert in DigitalCertificatesStore.GetCertificates(CertSystemStore.PersonalCertificates))
            {
                var serialNumber = String.Empty;
                foreach (var b in cert.SerialNumber)
                {
                    serialNumber = $"{b:X2}{serialNumber}";
                }

                if (serialNumber != x509Cert.SerialNumber)
                {
                    continue;
                }

                this.pdfCertificate = cert;
                break;
            }

Anyone got a better solution for this? (How) can I use the x509 cert with EvoPdf directly?

1 Answers1

0

In .NET 5/6 I'm using code like this:

// Get Personal Certificates with EvoPDF API
var evoCerts = DigitalCertificatesStore.GetCertificates(); 
byte[] evoSerNum = Convert.FromHexString(x509Cert.SerialNumber).Reverse().ToArray();
// Match x509Cert and evoCert by SerialNumber
this.pdfCertificate = evoCerts.GetCertBySerialNumber(evoSerNum); 
Leonidius
  • 648
  • 1
  • 9
  • 10