Fix forcefully encrypted Sent items
authorThomas
Fri, 02 Mar 2018 09:36:11 +0100
changeset 20267fd3cf4ac21b
parent 2025 16d165ee211e
child 2027 bd9dd65f8c6b
child 2028 d7ee86104b9f
Fix forcefully encrypted Sent items
FPPMessage.cs
UI/FormRegionPrivacyStatus.cs
     1.1 --- a/FPPMessage.cs	Thu Mar 01 10:07:49 2018 +0100
     1.2 +++ b/FPPMessage.cs	Fri Mar 02 09:36:11 2018 +0100
     1.3 @@ -199,7 +199,7 @@
     1.4                  string[] keyList;
     1.5                  pEpDecryptFlags flags;
     1.6  
     1.7 -                // Decryt incoming message
     1.8 +                // Decrypt incoming message
     1.9                  MsgProcessor msgProcessor = new MsgProcessor();
    1.10                  if (msgProcessor.Decrypt(this.CurrentMessage, out processedMessage, out keyList, out flags, out processedRating) == false)
    1.11                  {
    1.12 @@ -214,7 +214,7 @@
    1.13                          this.CurrentMessage = processedMessage;
    1.14                      }
    1.15  
    1.16 -                    // Set rating to reliable if processed rating is the exptected one (unencrypted)
    1.17 +                    // Set rating to reliable if processed rating is the expected one (unencrypted)
    1.18                      this.CurrentMessage.Rating = (processedRating == pEpRating.pEpRatingUnencrypted) ? pEpRating.pEpRatingReliable : processedRating;
    1.19  
    1.20                      return Globals.ReturnStatus.Success;
    1.21 @@ -577,9 +577,11 @@
    1.22          /// <returns>The status of this method.</returns>
    1.23          public Globals.ReturnStatus DecryptMessage(string entryId, string key)
    1.24          {
    1.25 +            bool isSecurelyStored = false;
    1.26              string filePath = null;
    1.27              string decryptedFilePath = null;
    1.28              Outlook.MailItem omi = null;
    1.29 +            Outlook.MailItem mirror = null;
    1.30              Outlook.Attachments attachments = null;
    1.31              Outlook.Attachment attachment = null;
    1.32              PEPMessage decryptedMessage = null;
    1.33 @@ -627,93 +629,116 @@
    1.34                          throw new Exception("Encrypted attachment not found.");
    1.35                      }
    1.36  
    1.37 -                    // Save attachment on disk
    1.38 -                    filePath = Path.GetTempPath() + attachment.FileName;
    1.39 -                    attachment.SaveAsFile(filePath);
    1.40 +                    // Check if message is in secure store or never unsecure
    1.41 +                    isSecurelyStored = (omi.GetNeverUnsecure() || omi.GetIsInSecureStore());
    1.42  
    1.43 -                    // Create file name for decrypted message and make sure it doesn't exist yet
    1.44 -                    decryptedFilePath = Path.ChangeExtension(filePath, "eml");
    1.45 -                    int counter = 1;
    1.46 -                    while (File.Exists(decryptedFilePath))
    1.47 +                    // If securely stored, look up mirror
    1.48 +                    if (isSecurelyStored)
    1.49                      {
    1.50 -                        decryptedFilePath = decryptedFilePath.Replace(".eml", (counter++ + ".eml"));
    1.51 +                        mirror = omi.GetMirror();
    1.52                      }
    1.53  
    1.54 -                    // Decrypt message
    1.55 -                    if (FPPMessage.DecryptFile(filePath, decryptedFilePath, key) != Globals.ReturnStatus.Success)
    1.56 +                    // If not securely stored or no mirror has been found, decrypt
    1.57 +                    if (mirror == null)
    1.58                      {
    1.59 -                        throw new Exception("DecryptMessage: Error decrypting message.");
    1.60 -                    }
    1.61 +                        // Save attachment on disk
    1.62 +                        filePath = Path.GetTempPath() + attachment.FileName;
    1.63 +                        attachment.SaveAsFile(filePath);
    1.64  
    1.65 -                    // Load decrypted eml file and apply it to Outlook mail item
    1.66 -                    MimeMessage message = MimeMessage.Load(decryptedFilePath);
    1.67 +                        // Create file name for decrypted message and make sure it doesn't exist yet
    1.68 +                        decryptedFilePath = Path.ChangeExtension(filePath, "eml");
    1.69 +                        int counter = 1;
    1.70 +                        while (File.Exists(decryptedFilePath))
    1.71 +                        {
    1.72 +                            decryptedFilePath = decryptedFilePath.Replace(".eml", (counter++ + ".eml"));
    1.73 +                        }
    1.74  
    1.75 -                    if (PEPMessage.Create(message, out decryptedMessage) == Globals.ReturnStatus.Success)
    1.76 -                    {
    1.77 -                        // Delete the Force Protection property
    1.78 -                        decryptedMessage.ForceProtectionId = null;
    1.79 +                        // Decrypt message
    1.80 +                        if (FPPMessage.DecryptFile(filePath, decryptedFilePath, key) != Globals.ReturnStatus.Success)
    1.81 +                        {
    1.82 +                            throw new Exception("DecryptMessage: Error decrypting message.");
    1.83 +                        }
    1.84  
    1.85 -                        // Set rating to reliable
    1.86 -                        decryptedMessage.Rating = pEpRating.pEpRatingReliable;
    1.87 +                        // Load decrypted eml file and apply it to Outlook mail item
    1.88 +                        MimeMessage message = MimeMessage.Load(decryptedFilePath);
    1.89  
    1.90 -                        // Add the sender's fingerprint to the message's key list
    1.91 -                        try
    1.92 +                        if (PEPMessage.Create(message, out decryptedMessage) == Globals.ReturnStatus.Success)
    1.93                          {
    1.94 -                            var ident = ThisAddIn.PEPEngine.UpdateIdentity(decryptedMessage.From.ToCOMType());
    1.95 -                            var keyList = decryptedMessage.KeyList;
    1.96 -                            if (string.IsNullOrEmpty(keyList))
    1.97 +                            // Delete the Force Protection property
    1.98 +                            decryptedMessage.ForceProtectionId = null;
    1.99 +
   1.100 +                            // Set rating to reliable
   1.101 +                            decryptedMessage.Rating = pEpRating.pEpRatingReliable;
   1.102 +
   1.103 +                            // Add the sender's fingerprint to the message's key list
   1.104 +                            try
   1.105                              {
   1.106 -                                keyList = ident.fpr;
   1.107 +                                var ident = ThisAddIn.PEPEngine.UpdateIdentity(decryptedMessage.From.ToCOMType());
   1.108 +                                var keyList = decryptedMessage.KeyList;
   1.109 +                                if (string.IsNullOrEmpty(keyList))
   1.110 +                                {
   1.111 +                                    keyList = ident.fpr;
   1.112 +                                }
   1.113 +                                else
   1.114 +                                {
   1.115 +                                    keyList = keyList.Insert(0, (ident.fpr + ","));
   1.116 +                                }
   1.117 +                                decryptedMessage.KeyList = keyList;
   1.118 +                            }
   1.119 +                            catch (Exception ex)
   1.120 +                            {
   1.121 +                                Log.Error("DecryptMessage: Error adding key list to message. " + ex.ToString());
   1.122 +                            }
   1.123 +
   1.124 +                            // If message was created correctly, apply it
   1.125 +                            if (isSecurelyStored)
   1.126 +                            {
   1.127 +                                // For untrusted server, set rating
   1.128 +                                omi.SetPEPProperty(MailItemExtensions.PEPProperty.Rating, pEpRating.pEpRatingReliable);
   1.129 +                                omi.Save();
   1.130 +
   1.131 +                                // Create a mirror and apply the decrypted message
   1.132 +                                Outlook.MailItem item = omi.CreateMirrorOMI();
   1.133 +                                decryptedMessage.ApplyTo(item, true, false);
   1.134 +                                item.Save();
   1.135 +                                item = null;
   1.136                              }
   1.137                              else
   1.138                              {
   1.139 -                                keyList = keyList.Insert(0, (ident.fpr + ","));
   1.140 +                                // For trusted server, apply decrypted message to original
   1.141 +                                decryptedMessage.ApplyTo(omi, true, false);
   1.142 +                                omi.Save();
   1.143                              }
   1.144 -                            decryptedMessage.KeyList = keyList;
   1.145                          }
   1.146 -                        catch (Exception ex)
   1.147 +                    }
   1.148 +
   1.149 +                    if (isSecurelyStored)
   1.150 +                    {
   1.151 +                        // Update the UI with the decrypted message
   1.152 +                        WindowFormRegionCollection formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveWindow()];
   1.153 +
   1.154 +                        if (formRegions?.FormRegionPreviewUnencrypted?.Visible == true)
   1.155                          {
   1.156 -                            Log.Error("DecryptMessage: Error adding key list to message. " + ex.ToString());
   1.157 -                        }
   1.158 -
   1.159 -                        // If message was created correctly, apply it
   1.160 -                        if (omi.GetIsSecurelyStored())
   1.161 -                        {
   1.162 -                            // For untrusted server / never unsecure, delete FPP property from original and set rating
   1.163 -                            omi.SetPEPProperty(MailItemExtensions.PEPProperty.ForceProtection, null);
   1.164 -                            omi.SetPEPProperty(MailItemExtensions.PEPProperty.Rating, pEpRating.pEpRatingReliable);
   1.165 -                            omi.Save();
   1.166 -
   1.167 -                            // Create a mirror and apply the decrypted message
   1.168 -                            Outlook.MailItem item = omi.CreateMirrorOMI();
   1.169 -                            decryptedMessage.ApplyTo(item, true, false);
   1.170 -                            item.Save();
   1.171 -                            item = null;
   1.172 -
   1.173 -                            // Update the UI with the decrypted message
   1.174 -                            WindowFormRegionCollection formRegions = Globals.FormRegions[Globals.ThisAddIn.Application.ActiveWindow()];
   1.175 -
   1.176 -                            if (formRegions?.FormRegionPreviewUnencrypted?.Visible == true)
   1.177 +                            // If we don't have a decrypted PEPMessage yet, create it
   1.178 +                            if ((decryptedMessage == null) &&
   1.179 +                                PEPMessage.Create(mirror, out decryptedMessage) != Globals.ReturnStatus.Success)
   1.180                              {
   1.181 -                                formRegions.FormRegionPreviewUnencrypted.DisplayState.SetMessage(decryptedMessage);
   1.182 -                                Log.Verbose("DecryptMessage: Mirror created and displayed.");
   1.183 -                            }
   1.184 -                            else
   1.185 -                            {
   1.186 -                                Log.Verbose("DecryptMessage: Error displaying mirror.");
   1.187 +                                throw new Exception("Error creating PEPMessage from mirror");
   1.188                              }
   1.189  
   1.190 -                            formRegions.FormRegionPrivacyStatus?.UpdateFormRegion(true);
   1.191 +                            // Show mirror
   1.192 +                            formRegions.FormRegionPreviewUnencrypted.DisplayState.SetMessage(decryptedMessage);
   1.193 +                            Log.Verbose("DecryptMessage: Mirror created and displayed.");
   1.194                          }
   1.195                          else
   1.196                          {
   1.197 -                            // For trusted server, apply decrypted message to original
   1.198 -                            decryptedMessage.ApplyTo(omi, true, false);
   1.199 -                            omi.Save();
   1.200 +                            Log.Verbose("DecryptMessage: FormRegionPreviewUnencrypted not visible.");
   1.201                          }
   1.202  
   1.203 -                        status = Globals.ReturnStatus.Success;
   1.204 +                        formRegions.FormRegionPrivacyStatus?.UpdateFormRegion(true);
   1.205                      }
   1.206 +
   1.207 +                    status = Globals.ReturnStatus.Success;
   1.208                  }
   1.209                  catch (Exception ex)
   1.210                  {
   1.211 @@ -724,6 +749,7 @@
   1.212                      attachment = null;
   1.213                      attachments = null;
   1.214                      omi = null;
   1.215 +                    mirror = null;
   1.216                  }
   1.217  
   1.218                  // Delete the attachment file
     2.1 --- a/UI/FormRegionPrivacyStatus.cs	Thu Mar 01 10:07:49 2018 +0100
     2.2 +++ b/UI/FormRegionPrivacyStatus.cs	Fri Mar 02 09:36:11 2018 +0100
     2.3 @@ -870,8 +870,7 @@
     2.4                  if (this.isEnabled)
     2.5                  {
     2.6                      // If forcefully protected, run dedicated decryption
     2.7 -                    if (omi.GetIsForceFullyProtected() &&
     2.8 -                        omi.GetIsIncoming())
     2.9 +                    if (omi.GetIsForceFullyProtected())
    2.10                      {
    2.11                          var fppMessage = new FPPMessage(omi);
    2.12                          if (fppMessage.GetMessageType() != null)