Sicos1977 / MSGReader

C# Outlook MSG file reader without the need for Outlook
http://sicos1977.github.io/MSGReader
MIT License
493 stars 168 forks source link

it failes to load encrypted (S/MIME) msg file #415

Closed kenjiuno closed 3 weeks ago

kenjiuno commented 3 months ago

Describe the bug It throws an exception when I try to load the encrypted msg file by using new MsgReader.Outlook.Storage.Message(...).

To Reproduce As one repro sample, I have confirmed this behavior on Windows 10 with C# .NET 8.0 code:

ConsoleApp116.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="MsgReader" Version="5.6.4" />
  </ItemGroup>
</Project>

Program.cs

using System.Text;

namespace ConsoleApp116
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            using (var msg = new MsgReader.Outlook.Storage.Message(@"Message from smime1 to smime2 (encrypted).msg"))
            {
                var from = msg.Sender;
                var sentOn = msg.SentOn;
                var recipientsTo = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.To, false, false);
                var recipientsCc = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.Cc, false, false);
                var subject = msg.Subject;
                var htmlBody = msg.BodyHtml;
                // etc...
            }
        }
    }
}

Expected behavior

It can be read without exception thrown if there is an applicable situation including having a corresponding private key.

Screenshots

2024-08-19_09h52_11

Unhandled exception. System.Security.Cryptography.CryptographicException: Invalid cryptographic message type.
   at System.Security.Cryptography.Pkcs.SignedCms.<Decode>g__CopyContent|26_0(ReadOnlySpan`1 encodedMessage)
   at System.Security.Cryptography.Pkcs.SignedCms.Decode(ReadOnlySpan`1 encodedMessage)
   at System.Security.Cryptography.Pkcs.SignedCms.Decode(Byte[] encodedMessage)
   at MsgReader.Outlook.Storage.Message.ProcessSignedContent(Byte[] data)
   at MsgReader.Outlook.Storage.Message.LoadEncryptedAndPossibleSignedMessage(CFStorage storage)
   at MsgReader.Outlook.Storage.Message.LoadStorage(CFStorage storage)
   at MsgReader.Outlook.Storage..ctor(String storageFilePath, FileAccess fileAccess)
   at MsgReader.Outlook.Storage.Message..ctor(String msgfile, FileAccess fileAccess)
   at ConsoleApp116.Program.Main(String[] args) in H:\Proj\ConsoleApp116\ConsoleApp116\Program.cs:line 11

H:\Proj\ConsoleApp116\ConsoleApp116\bin\Debug\net8.0\ConsoleApp116.exe (プロセス 32748) は、コード -532462766 で終了しました。
このウィンドウを閉じるには、任意のキーを押してください...

Desktop (please complete the following information):

Additional context

The other day, in my GitHub repository, I received a question about how to deal with smime.p7m file. At that time, I didn't know how to decode it. So I studied about it. And then I created a complete set of S/MIME sample data at my repository for future research. Because this MSGReader project seems to contain decryption code, I have tested it.

Sicos1977 commented 1 month ago

I dont understand what the problem is and what I should do to fix it?