MimeKit extract Signed attachment

364 Views Asked by At

I have an attachment stored in a database that has an attachment name of smime.p7m and it has a mimetype of multipart/signed. I now need to extract the attachment out of the signed p7m file and I was hoping I could use Mimekit to achieve this. I've tried the following:

    CryptographyContext.Register(typeof(WindowsSecureMimeContext));
    try
    {
        using (MemoryStream ms = new MemoryStream(buf))
        {
            ApplicationPkcs7Mime p7m = new ApplicationPkcs7Mime(SecureMimeType.EnvelopedData, ms);
            p7m.Verify(out attachment);
            builder.Attachments.Add(attachment);
        }
    }
    catch (Exception ex)
    {
        log.Error("Exception in smime", ex);
    }

But it fails with a System.InvalidOperationException on the Verify line.

Any thoughts?

1

There are 1 best solutions below

0
On

SecureMimeType.EnvelopedData is for encrypted data, not signed data. You need to use SecureMimeTYpe.SignedData.