Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decrypting encrypted content without headers #443

Closed
Peperud opened this issue Oct 23, 2018 · 5 comments
Closed

Decrypting encrypted content without headers #443

Peperud opened this issue Oct 23, 2018 · 5 comments
Labels
wontfix This will not be worked on

Comments

@Peperud
Copy link

Peperud commented Oct 23, 2018

I'm getting a (test) encrypted file.
MimeKit decrypts it, but the content comes out empty.
OpenSSL decrypts it and does produce content.

I might be wrong, but from what I can tell, it looks to me that the decrypted message does not have headers and this throws off MimeKit. After decryption, it goes on to scan for headers, doesn't find any, defaults to text/plain, but then doesn't rewind the stream to account for the missing headers and comes up with empty content.

Decrypted message without headers is probably not very compliant.

However, silently "losing" the content also seems wrong.
Perhaps it would make sense (based on ParserOptions being on the strict/loose side or unconditionally), that MimeKit either:

  1. Throws an appropriate compliance/format error.
  2. Defaults to text/plain and loads the content.
@jstedfast
Copy link
Owner

I would recommend using the SecureMimeContext directly.

http://www.mimekit.net/docs/html/M_MimeKit_Cryptography_SecureMimeContext_DecryptTo.htm

@jstedfast jstedfast added the wontfix This will not be worked on label Oct 23, 2018
@jstedfast
Copy link
Owner

jstedfast commented Oct 23, 2018

Here's how you can use this:

public MimeEntity Decrypt (SecureMimeContext ctx, ApplicationPkcs7Mime pkcs7)
{
    using (var decryptedData = new MemoryStream ()) {
        using (var encryptedData = new MemoryStream ()) {
            pkcs7.Content.DecodeTo (encryptedData);
            encryptedData.Position = 0;

            ctx.DecryptTo (encryptedData, decryptedData);
            decryptedData.Position = 0;
        }

        // now figure out of it has headers or not...
        if (dataHasHeaders)
            return MimeEntity.Load (decryptedData);

        var content = new MemoryStream ();
        decryptedData.CopyTo (content);
        content.Position = 0;

        return new MimePart ("application", "octet-stream") {
            Content = new MimeContent (content)
        };
    }
}

@Peperud
Copy link
Author

Peperud commented Oct 23, 2018

MimeKit throws in a couple of other places when it fails to parse headers.
Doesn't it make sense to throw in this case too?

Thanks for the sample.

@jstedfast
Copy link
Owner

You haven't given me an example so I can't tell you why it isn't.

@Peperud
Copy link
Author

Peperud commented Oct 24, 2018

Can't share the original, but I think this should show it. Password is "nsoft".
MimeKit443.zip

So, expecting a compliant message, I go like this:

...
    var message = MimeMessage.Load(inputFileName);
    var pkcs7 = message.Body as ApplicationPkcs7Mime;
    using (var ctxDecrypt = CustomSecureMimeContext.Create(EncryptionCertificateStream, EncryptionCertificatePassword))
    {
        // this doesn't throw
        var decrypted = pkcs7.Decrypt(ctxDecrypt);

        // but the content is null
        IMimeContent content = (decrypted as MimePart).Content;
...

jstedfast added a commit that referenced this issue Oct 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants