Search code examples
c#excelcertificatedigital-signatureepplus

EPPlus - How to Remove Digital Signature


I would like to remove a digital signature from a VBA signed excel macro file. However when I look at EPPlus's library I see that the "Signature" property is read-only, and setting the Certificate as null doesn't seem to remove it, only invalidates the signature in the file:

using (ExcelPackage xlPackage = new ExcelPackage(fiNew))
{
    xlPackage.Workbook.VbaProject.Signature.Certificate = null;
    xlPackage.Save();
}

Calling the dispose method doesn't work either, errors out on the save. Does anybody know how to do this in EPPlus?


Solution

  • Looking at the source just provide a certificate without a private key - see line 137.

            internal void Save(ExcelVbaProject proj)
            {
                if (Certificate == null)
                {
                    return;
                }
    
                if (Certificate.HasPrivateKey==false)    //No signature. Remove any Signature part
    

    You could for example just use the first certificate in the Trusted Root Certificate Authorities, which have no keys, as long as you are not running on a root certificate authority or somebody improted a PFX by accident... so we filter for that too:

    enter image description here

    Here is some code to read from the Trusted Root Certificate Authorities store:

    using (var store = new X509Store(StoreName.Root, StoreLocation.CurrentUser)) {
      store.Open(OpenFlags.ReadOnly);
      var someCertWithoutPrivateKey = 
        store.Certificates
          .Cast<X509Certificate2>()
          .Where(c => !c.HasPrivateKey)
          .FirstOrDefault();
    }