OUT-486: Implement workaround until engine returns flag when source was modified
authorThomas
Wed, 27 Mar 2019 08:47:21 +0100
changeset 26116c532dd283a9
parent 2607 8a9eddf5347a
child 2612 d46de99a6317
OUT-486: Implement workaround until engine returns flag when source was modified
MsgProcessor.cs
     1.1 --- a/MsgProcessor.cs	Fri Mar 22 08:42:12 2019 +0100
     1.2 +++ b/MsgProcessor.cs	Wed Mar 27 08:47:21 2019 +0100
     1.3 @@ -83,7 +83,7 @@
     1.4                                  }));
     1.5                              }
     1.6                              else
     1.7 -                            {                                
     1.8 +                            {
     1.9                                  Log.Verbose("CheckForSyncMessage: Wizard not opened. Wizard was {0} open. Own fpr is {1}", ((KeySyncWizard.Wizard?.IsVisible == true) ? "already" : "not"), (myself?.Fingerprint ?? "<null>"));
    1.10                              }
    1.11                          }
    1.12 @@ -305,50 +305,18 @@
    1.13                              {
    1.14                                  /* Decrypt message. This is done to check if it's a beacon
    1.15                                   * message and to remove public keys attached to the message.
    1.16 -                                 * The processed message itself can be ignored.
    1.17 +                                 * If the source message has been changed during
    1.18 +                                 * decryption, this is possibly due to the removal of a key.
    1.19 +                                 * In this case, return the source message as processed message if
    1.20 +                                 * it hasn't been sent to myself (do not remove keys from unsecure 
    1.21 +                                 * messages sent to myself).
    1.22                                  */
    1.23 -                                this.Decrypt(ref message,
    1.24 -                                             out _,
    1.25 -                                             out decryptionKeyList,
    1.26 -                                             ref decryptionFlags);
    1.27 -                            }
    1.28 -
    1.29 -                            /* Check all unsecure messages that are not sent to myself
    1.30 -                             * for attached keys and remove them.
    1.31 -                             * Although this is also done in the engine during decryption,
    1.32 -                             * it seems safer to only apply the round-trip Outlook mail item
    1.33 -                             * => PEPMessage => Outlook mail item if really needed.
    1.34 -                             */
    1.35 -                            bool keyFound = false;
    1.36 -                            bool sentToMyself = false;
    1.37 -
    1.38 -                            for (int i = 0; i < message.Attachments?.Count; i++)
    1.39 -                            {
    1.40 -                                if (message.Attachments[i].IsKey)
    1.41 +                                if (this.Decrypt(ref message, out _, out _, ref decryptionFlags, out _) &&
    1.42 +                                    decryptionFlags.HasFlag(pEpDecryptFlags.pEpDecryptFlagSrcModified))
    1.43                                  {
    1.44 -                                    message.Attachments.Remove(message.Attachments[i]);
    1.45 -                                    keyFound = true;
    1.46 -                                    i--;
    1.47 +                                    processedMessage = message;
    1.48                                  }
    1.49                              }
    1.50 -
    1.51 -                            if (keyFound)
    1.52 -                            {
    1.53 -                                for (int i = 0; i < message.Recipients?.Length; i++)
    1.54 -                                {
    1.55 -                                    if (message.Recipients[i].EqualsByAddress(message.From))
    1.56 -                                    {
    1.57 -                                        sentToMyself = true;
    1.58 -                                        break;
    1.59 -                                    }
    1.60 -                                }
    1.61 -                            }
    1.62 -
    1.63 -                            // If a key was found and detached, return updated message
    1.64 -                            if (keyFound && (sentToMyself == false))
    1.65 -                            {
    1.66 -                                processedMessage = message;
    1.67 -                            }
    1.68                          }
    1.69                      }
    1.70                  }
    1.71 @@ -507,7 +475,6 @@
    1.72          /// <returns>True if decryption was considered successful, otherwise false.</returns>
    1.73          public bool Decrypt(PEPMessage sourceMessage)
    1.74          {
    1.75 -
    1.76              return this.Decrypt(sourceMessage, out _);
    1.77          }
    1.78  
    1.79 @@ -629,7 +596,33 @@
    1.80                      keyList = dstKeyList;
    1.81                  }
    1.82  
    1.83 -                // Replace original if needed
    1.84 +                /* If the source message's attachments count changes during decryption and it's not because
    1.85 +                 * the source has been reencrypted, there has probably been removed a key. In this case,
    1.86 +                 * check if message is sent to myself and if not, set modified source flag so that the
    1.87 +                 * modified source is returned.
    1.88 +                 */
    1.89 +                bool attachmentsCountChanged = (src.Attachments?.Length != sourceMessage.Attachments?.Count);
    1.90 +                if ((flags.HasFlag(pEpDecryptFlags.pEpDecryptFlagSrcModified) == false) &&
    1.91 +                    attachmentsCountChanged)
    1.92 +                {
    1.93 +                    bool sentToMyself = false;
    1.94 +                    for (int i = 0; i < sourceMessage.Recipients?.Length; i++)
    1.95 +                    {
    1.96 +                        if (sourceMessage.Recipients[i].EqualsByAddress(sourceMessage.From))
    1.97 +                        {
    1.98 +                            sentToMyself = true;
    1.99 +                            break;
   1.100 +                        }
   1.101 +                    }
   1.102 +
   1.103 +                    // If a key was found and detached, return updated message
   1.104 +                    if (sentToMyself == false)
   1.105 +                    {
   1.106 +                        flags |= pEpDecryptFlags.pEpDecryptFlagSrcModified;
   1.107 +                    }
   1.108 +                }
   1.109 +
   1.110 +                // Return modified source if necessary
   1.111                  if (flags.HasFlag(pEpDecryptFlags.pEpDecryptFlagSrcModified))
   1.112                  {
   1.113                      sts = PEPMessage.Create(src, out PEPMessage reencryptedMessage);