I'm trying to read content of digitally signed email using EWS. Unfortunately, when I use an approach with EnvelopeCMS I get an exception:
System.Security.Cryptography.CryptographicException: ASN1 - bad tag value met.
in System.Security.Cryptography.Pkcs.EnvelopedCms.OpenToDecode(Byte[] encodedMessage)
in System.Security.Cryptography.Pkcs.EnvelopedCms.Decode(Byte[] encodedMessage) in myExchange.Email.DecryptToFile(Byte[] data)
(encodedMessage is smime.p7m attachment of an e-mail).
EDIT: this is a key code fragment:
foreach (Attachment attachment in emailMessage.Attachments)
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
if (fileAttachment.Name == "smime.p7m")
{
byte[] content = fileAttachment.Content;
MemoryStream stream = new MemoryStream();
fileAttachment.Load(stream);
StreamReader stReader = new StreamReader(stream);
stream.Seek(0, SeekOrigin.Begin);
content = stream.GetBuffer();
var encrypted = new System.Security.Cryptography.Pkcs.EnvelopedCms();
encrypted.Decode(content); // <==== Here occurs exception
encrypted.Decrypt();
byte[] unencryptedButRawMimeEntity = encrypted.ContentInfo.Content;
}
}
}
More about an e-mail - EWS output console says, that it has one attachment of "mutipart/signed" content-type
<m:ResponseCode>NoError</m:ResponseCode>
<m:Attachments>
<t:FileAttachment>
<t:AttachmentId Id="AAMkADNi(... CUT ...)T5PWd/bDM=" />
<t:Name>smime.p7m</t:Name>
<t:ContentType>multipart/signed</t:ContentType>