MailModel HandlePgpMime(Outlook.MailItem mailItem, Outlook.Attachment encryptedMime, Outlook.Attachment sigMime, string sigHash = "sha1") { Logger.Trace("> HandlePgpMime"); CryptoContext context = null; var cleartext = mailItem.Body; // 1. Decrypt attachement if (encryptedMime != null) { if (DecryptMime(mailItem, encryptedMime, ref context, ref cleartext)) { return null; } } // 2. Verify signature Signature signature = null; if (sigMime != null) { context = new CryptoContext(Passphrase); signature = VerifySignature(mailItem, sigMime, sigHash, ref context); } if (context == null) return null; // Extract files from MIME data MailModel mailModel = null; var msg = new SharpMessage(cleartext); string body = mailItem.Body; if (mailItem.BodyFormat == Outlook.OlBodyFormat.olFormatPlain) { mailModel = new PlainMailModel { Body = msg.Body }; } else if (mailItem.BodyFormat == Outlook.OlBodyFormat.olFormatHTML) { if (!msg.Body.TrimStart().ToLower().StartsWith("<html")) { body = msg.Body; body = System.Net.WebUtility.HtmlEncode(body); body = body.Replace("\n", "<br />"); mailModel = new HtmlMailModel { Body = "<html><head></head><body>" + body + "</body></html>" }; } else { mailModel = new HtmlMailModel { Body = msg.Body }; } } else { // May cause mail item not to open correctly mailModel = new PlainMailModel { Body = msg.Body, Signature = signature }; } foreach (SharpAttachment mimeAttachment in msg.Attachments) { mimeAttachment.Stream.Position = 0; var fileName = mimeAttachment.Name; var tempFile = Path.Combine(Path.GetTempPath(), fileName); using (var fout = File.OpenWrite(tempFile)) { mimeAttachment.Stream.CopyTo(fout); } if (fileName == "signature.asc") { var detachedsig = File.ReadAllText(tempFile); var clearsig = CreateClearSignatureFromDetachedSignature(mailItem, sigHash, detachedsig); var crypto = new PgpCrypto(context); signature = VerifyClearSignature(ref context, crypto, clearsig); } mailModel.Attachments.Add(new Attachment { TempFile = tempFile, AttachmentType = Outlook.OlAttachmentType.olByValue, FileName = fileName }); } mailModel.Body = mailModel.Body; mailModel.Signature = signature; return mailModel; }
public MailModel DecryptEmail(Outlook.MailItem mailItem) { if (mailItem.Body == null || Regex.IsMatch(mailItem.Body, PgpEncryptedHeader) == false) { return HandleMailWithoutPgpBody(mailItem); } MailModel mailModel; // Sometimes messages could contain multiple message blocks. In that case just use the // very first one. var firstPgpBlock = GetFirstPgpBlock(mailItem); var encoding = GetEncoding(firstPgpBlock); CryptoContext context; var cleardata = DecryptAndVerify(mailItem.To, Encoding.ASCII.GetBytes(firstPgpBlock), out context); if (cleardata == null) return null; if (mailItem.BodyFormat == Outlook.OlBodyFormat.olFormatHTML) { // Don't HMTL encode or we will encode emails already in HTML format. // Office has a safe html module they use to prevent security issues. // Not encoding here should be no worse then reading a standard HTML // email. var html = encoding.GetString(cleardata); html = html.Replace("\n", "<br/>"); html = "<html><body>" + html + "</body></html>"; mailModel = new HtmlMailModel { Body = html }; } else { var mailText = encoding.GetString(cleardata); mailModel = new PlainMailModel { Body = mailText }; } // Decrypt all attachments var mailAttachments = CreateAttachmentListFromMailItem(mailItem); var attachments = new List<Attachment>(); foreach (var attachment in mailAttachments) { var tempAttachment = new Attachment(); // content id if (attachment.FileName.StartsWith("Attachment") && attachment.FileName.EndsWith(".pgp")) { var property = attachment.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F"); tempAttachment.FileName = property.ToString(); if (tempAttachment.FileName.Contains('@')) { tempAttachment.FileName = tempAttachment.FileName.Substring(0, tempAttachment.FileName.IndexOf('@')); } tempAttachment.TempFile = Path.GetTempPath(); tempAttachment.AttachmentType = attachment.Type; tempAttachment.TempFile = Path.Combine(tempAttachment.TempFile, tempAttachment.FileName); attachment.SaveAsFile(tempAttachment.TempFile); TryDecryptAndAddAttachment(mailItem, tempAttachment, attachments); } //else if (attachment.FileName == "PGPexch.htm.pgp") //{ // // This is the HTML email message. // var TempFile = Path.GetTempFileName(); // attachment.SaveAsFile(TempFile); // // Decrypt file // var cyphertext = File.ReadAllBytes(TempFile); // File.Delete(TempFile); // try // { // var plaintext = DecryptAndVerify(mailItem.To, cyphertext); // mailItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML; // mailItem.HTMLBody = _encoding.GetString(plaintext); // } // catch // { // // Odd! // } //} else { tempAttachment.FileName = Regex.Replace(attachment.FileName, EncryptionExtension, ""); tempAttachment.DisplayName = Regex.Replace(attachment.DisplayName, EncryptionExtension, ""); ; tempAttachment.TempFile = Path.GetTempPath(); tempAttachment.AttachmentType = attachment.Type; tempAttachment.TempFile = Path.Combine(tempAttachment.TempFile, tempAttachment.FileName); attachment.SaveAsFile(tempAttachment.TempFile); TryDecryptAndAddAttachment(mailItem, tempAttachment, attachments); } } mailModel.Attachments = attachments; return mailModel; }