Add support for old and new Key Import header names 1.0.204 Patch Release
authorThomas
Mon, 04 Mar 2019 12:11:59 +0100
branch1.0.204 Patch Release
changeset 25906f029d803429
parent 2589 4196bf6148c2
child 2591 78699bc300f7
Add support for old and new Key Import header names
Extensions/MailItemExtensions.cs
PEPMessage.cs
     1.1 --- a/Extensions/MailItemExtensions.cs	Mon Mar 04 11:56:21 2019 +0100
     1.2 +++ b/Extensions/MailItemExtensions.cs	Mon Mar 04 12:11:59 2019 +0100
     1.3 @@ -2525,7 +2525,6 @@
     1.4                                            HeaderList messageHeaders)
     1.5          {
     1.6              bool success = false;
     1.7 -            object propertyValue;
     1.8              object outValue = defaultValue;
     1.9  
    1.10              try
    1.11 @@ -2537,17 +2536,19 @@
    1.12                          // Auto consume MAPI property
    1.13                          case PEPProperty.AutoConsume:
    1.14                              {
    1.15 -                                // Note: this value is passed through completely as a string
    1.16 -                                propertyValue = MapiHelper.GetProperty(omi, PEPMessage.PidNamePEPAutoConsume, null);
    1.17 +                                // Note: this value is passed through completely as a string. As a backup search the original internet message headers   
    1.18 +                                string autoConsume = (MapiHelper.GetProperty(omi, PEPMessage.PidNamePEPAutoConsume, null) ?? omi.GetValueFromMessageHeaders(PEPMessage.PR_PEP_AUTO_CONSUME_NAME, ref messageHeaders)) as string;
    1.19  
    1.20 -                                if (propertyValue != null)
    1.21 +                                // Check for legacy header name
    1.22 +                                if (string.IsNullOrEmpty(autoConsume))
    1.23                                  {
    1.24 -                                    outValue = propertyValue;
    1.25 +                                    autoConsume = (MapiHelper.GetProperty(omi, PEPMessage.PidNamePEPAutoConsumeOld, null) ?? omi.GetValueFromMessageHeaders(PEPMessage.PR_PEP_AUTO_CONSUME_NAME_OLD, ref messageHeaders)) as string;
    1.26                                  }
    1.27 -                                else
    1.28 +
    1.29 +                                // Set value if it has been retrieved
    1.30 +                                if (string.IsNullOrEmpty(autoConsume) == false)
    1.31                                  {
    1.32 -                                    // As a backup search the original internet message headers   
    1.33 -                                    outValue = omi.GetValueFromMessageHeaders(PEPMessage.PR_PEP_AUTO_CONSUME_NAME, messageHeaders);
    1.34 +                                    outValue = autoConsume;
    1.35                                  }
    1.36  
    1.37                                  success = true;
    1.38 @@ -2559,17 +2560,14 @@
    1.39                                  /* Note that ForceProtection is defined as:
    1.40                                   *   ON : ForceProtection property exists with a value of a message GUID
    1.41                                   *  OFF : ForceProtection property does not exist or has a value of null
    1.42 +                                 *  As a backup search the original internet message headers
    1.43                                   */
    1.44 -                                propertyValue = MapiHelper.GetProperty(omi, PEPMessage.PidNamePEPForceProtection, null);
    1.45 +                                string forceProtection = (MapiHelper.GetProperty(omi, PEPMessage.PidNamePEPForceProtection, null) ?? omi.GetValueFromMessageHeaders(PEPMessage.PR_PEP_FORCE_PROTECTION_NAME, messageHeaders)) as string;
    1.46  
    1.47 -                                if (propertyValue != null)
    1.48 +                                // Set value if it has been retrieved
    1.49 +                                if (string.IsNullOrEmpty(forceProtection) == false)
    1.50                                  {
    1.51 -                                    outValue = propertyValue as string;
    1.52 -                                }
    1.53 -                                else
    1.54 -                                {
    1.55 -                                    // As a backup search the original internet message headers
    1.56 -                                    outValue = omi.GetValueFromMessageHeaders(PEPMessage.PR_PEP_FORCE_PROTECTION_NAME, messageHeaders);
    1.57 +                                    outValue = forceProtection;
    1.58                                  }
    1.59  
    1.60                                  success = true;
    1.61 @@ -2582,11 +2580,10 @@
    1.62                                   *   ON : ForceUnencrypted property exists with a value of 'True'
    1.63                                   *  OFF : ForceUnencrypted property does not exist or has a value of 'False' or null
    1.64                                   */
    1.65 -                                propertyValue = omi.GetUserProperty(CryptableMailItem.USER_PROPERTY_KEY_FORCE_UNENCRYPTED);
    1.66 +                                bool? forceUnencrypted = omi.GetUserProperty(CryptableMailItem.USER_PROPERTY_KEY_FORCE_UNENCRYPTED) as bool?;
    1.67  
    1.68 -                                if ((propertyValue != null) &&
    1.69 -                                    (propertyValue is bool) &&
    1.70 -                                    ((bool)propertyValue == true))
    1.71 +                                // Set value if it has been retrieved
    1.72 +                                if (forceUnencrypted == true)
    1.73                                  {
    1.74                                      outValue = true;
    1.75                                  }
    1.76 @@ -2600,18 +2597,21 @@
    1.77                                  /* Note that KeyImport is defined as:
    1.78                                   *   ON : KeyImport property exists with a value of a key fingerprint
    1.79                                   *  OFF : KeyImport property does not exist or has a value of null
    1.80 +                                 *  As a backup search the original internet message headers
    1.81                                   */
    1.82 -                                propertyValue = MapiHelper.GetProperty(omi, PEPMessage.PidNamePEPKeyImport, null);
    1.83 +                                string keyImport = (MapiHelper.GetProperty(omi, PEPMessage.PidNamePEPKeyImport, null) ?? omi.GetValueFromMessageHeaders(PEPMessage.PR_PEP_KEY_IMPORT_NAME, ref messageHeaders)) as string;
    1.84  
    1.85 -                                if (propertyValue != null)
    1.86 +                                // Check for legacy header name
    1.87 +                                if (string.IsNullOrEmpty(keyImport))
    1.88                                  {
    1.89 -                                    outValue = propertyValue as string;
    1.90 +                                    keyImport = (MapiHelper.GetProperty(omi, PEPMessage.PidNamePEPKeyImportOld, null) ?? omi.GetValueFromMessageHeaders(PEPMessage.PR_PEP_KEY_IMPORT_NAME_OLD, ref messageHeaders)) as string;
    1.91                                  }
    1.92 -                                else
    1.93 +
    1.94 +                                // Set value if it has been retrieved
    1.95 +                                if (string.IsNullOrEmpty(keyImport) == false)
    1.96                                  {
    1.97 -                                    // As a backup search the original internet message headers
    1.98 -                                    outValue = omi.GetValueFromMessageHeaders(PEPMessage.PR_PEP_KEY_IMPORT_NAME, messageHeaders);
    1.99 -                                }
   1.100 +                                    outValue = keyImport;
   1.101 +                                }                                
   1.102  
   1.103                                  success = true;
   1.104                                  break;
   1.105 @@ -2620,7 +2620,14 @@
   1.106                          case PEPProperty.KeyList:
   1.107                              {
   1.108                                  // Note: this value is passed through completely as a string
   1.109 -                                outValue = MapiHelper.GetProperty(omi, PEPMessage.PidNameKeyList, MailItemExtensions.GetPEPPropertyDefault(property));
   1.110 +                                string keyList = MapiHelper.GetProperty(omi, PEPMessage.PidNameKeyList, MailItemExtensions.GetPEPPropertyDefault(property)) as string;
   1.111 +
   1.112 +                                // Set value if it has been retrieved
   1.113 +                                if (string.IsNullOrEmpty(keyList) == false)
   1.114 +                                {
   1.115 +                                    outValue = keyList;
   1.116 +                                }
   1.117 +
   1.118                                  success = true;
   1.119                                  break;
   1.120                              }
   1.121 @@ -2630,22 +2637,12 @@
   1.122                                  /* Note that NeverUnsecure is defined as:
   1.123                                   *   ON : NeverUnsecure property exists with ANY value ('1' is the default)
   1.124                                   *  OFF : NeverUnsecure property does not exist or has a null value
   1.125 +                                 *  As a backup search the original internet message headers
   1.126                                   */
   1.127 -                                propertyValue = MapiHelper.GetProperty(omi, PEPMessage.PidNamePEPNeverUnsecure, null);
   1.128 -
   1.129 -                                // Only check that the NeverUnsecure property exists, the value is ignored
   1.130 -                                if (propertyValue != null)
   1.131 +                                if ((MapiHelper.GetProperty(omi, PEPMessage.PidNamePEPNeverUnsecure, null) ?? omi.GetValueFromMessageHeaders(PEPMessage.PR_PEP_NEVER_UNSECURE_NAME, messageHeaders)) != null)
   1.132                                  {
   1.133                                      outValue = true;
   1.134                                  }
   1.135 -                                else
   1.136 -                                {
   1.137 -                                    // Fallback solution for when the property doesn't get set correctly as MAPI property in an Exchange
   1.138 -                                    // server environment and therefore has to be retrieved directly from the message's internet headers.
   1.139 -                                    // This is not ideal, as the fallback will only be needed for some edge cases, but the logic is run 
   1.140 -                                    // several times for each mail item.
   1.141 -                                    outValue = (omi.GetValueFromMessageHeaders(PEPMessage.PR_PEP_NEVER_UNSECURE_NAME, messageHeaders) != null);
   1.142 -                                }
   1.143  
   1.144                                  success = true;
   1.145                                  break;
   1.146 @@ -2654,15 +2651,14 @@
   1.147                          case PEPProperty.PEPProtocolVersion:
   1.148                              {
   1.149                                  // Note: this value is passed through completely as a string
   1.150 -                                outValue = MapiHelper.GetProperty(omi, PEPMessage.PidNamePEPProtocolVersion);
   1.151 +                                string pEpProtocolVersion = (MapiHelper.GetProperty(omi, PEPMessage.PidNamePEPProtocolVersion) ?? omi.GetValueFromMessageHeaders(PEPMessage.PR_PEP_PROTOCOL_VERSION_NAME, messageHeaders)) as string;
   1.152  
   1.153 -                                // OUT-259. In case of clean account sometimes can not get PidNamePEPProtocolVersion property("The property is unknown or cannot be found" exception)
   1.154 -                                // workaround: getting all Transport Message Headers and taking needed PR_PEP_PROTOCOL_VERSION_NAME from there.    
   1.155 -                                if (outValue == null)
   1.156 +                                // Set value if it has been retrieved
   1.157 +                                if (string.IsNullOrEmpty(pEpProtocolVersion) == false)
   1.158                                  {
   1.159 -                                    // As a backup search the original internet message headers
   1.160 -                                    outValue = omi.GetValueFromMessageHeaders(PEPMessage.PR_PEP_PROTOCOL_VERSION_NAME, messageHeaders);
   1.161 +                                    outValue = pEpProtocolVersion;
   1.162                                  }
   1.163 +
   1.164                                  success = true;
   1.165                                  break;
   1.166                              }
   1.167 @@ -2673,14 +2669,14 @@
   1.168                                   *          pEpRatingX : Rating property exists with a valid string value (parsable to pEpRating)
   1.169                                   *  pEpRatingUndefined : Rating property does not exist or has an invalid value (including null)
   1.170                                   */
   1.171 -                                propertyValue = MapiHelper.GetProperty(omi, PEPMessage.PidNameEncStatus, null);
   1.172 +                                string encStatus = (MapiHelper.GetProperty(omi, PEPMessage.PidNameEncStatus, null)) as string;
   1.173  
   1.174 -                                if ((propertyValue != null) &&
   1.175 -                                    (propertyValue is string))
   1.176 +                                // Set value if it has been retrieved
   1.177 +                                if (string.IsNullOrEmpty(encStatus) == false)
   1.178                                  {
   1.179                                      try
   1.180                                      {
   1.181 -                                        outValue = AdapterExtensions.ParseRatingString((string)propertyValue);
   1.182 +                                        outValue = AdapterExtensions.ParseRatingString(encStatus);
   1.183                                      }
   1.184                                      catch
   1.185                                      {
   1.186 @@ -2698,11 +2694,10 @@
   1.187                                   *   ON : EnableProtection property exists with a value of 'True'
   1.188                                   *  OFF : EnableProtection property does not exist or has a value of 'False' or null
   1.189                                   */
   1.190 -                                propertyValue = omi.GetUserProperty(CryptableMailItem.USER_PROPERTY_KEY_ENABLE_PROTECTION);
   1.191 +                                bool? enableProtection = (omi.GetUserProperty(CryptableMailItem.USER_PROPERTY_KEY_ENABLE_PROTECTION)) as bool?;
   1.192  
   1.193 -                                if ((propertyValue != null) &&
   1.194 -                                    (propertyValue is bool) &&
   1.195 -                                    ((bool)propertyValue == true))
   1.196 +                                // Set value if it has been retrieved
   1.197 +                                if (enableProtection == true)
   1.198                                  {
   1.199                                      outValue = true;
   1.200                                  }
   1.201 @@ -2714,7 +2709,7 @@
   1.202                  }
   1.203                  else
   1.204                  {
   1.205 -                    Log.Error("GetPEPProperty: Can't set for null MailItem or propertyName.");
   1.206 +                    Log.Error("GetPEPProperty: Can't get for null MailItem or propertyName.");
   1.207                  }
   1.208              }
   1.209              catch (Exception ex)
   1.210 @@ -2734,7 +2729,7 @@
   1.211          /// <param name="key">The key to look its value for.</param>
   1.212          /// <param name="messageHeaders">The message headers to search.</param>
   1.213          /// <returns>The found value or null if no value has been found.</returns>
   1.214 -        public static object GetValueFromMessageHeaders(this Outlook.MailItem omi, string key, HeaderList messageHeaders = null)
   1.215 +        public static object GetValueFromMessageHeaders(this Outlook.MailItem omi, string key, ref HeaderList messageHeaders)
   1.216          {
   1.217              object value = null;
   1.218  
     2.1 --- a/PEPMessage.cs	Mon Mar 04 11:56:21 2019 +0100
     2.2 +++ b/PEPMessage.cs	Mon Mar 04 12:11:59 2019 +0100
     2.3 @@ -27,8 +27,10 @@
     2.4          // pEp named MAPI properties
     2.5          public const string PR_ENC_STATUS_NAME              = "X-EncStatus";
     2.6          public const string PR_KEY_LIST_NAME                = "X-KeyList";
     2.7 -        public const string PR_PEP_AUTO_CONSUME_NAME        = "pEp-auto-consume";
     2.8 -        public const string PR_PEP_KEY_IMPORT_NAME          = "pEp-key-import";
     2.9 +        public const string PR_PEP_AUTO_CONSUME_NAME        = "X-pEp-auto-consume";
    2.10 +        public const string PR_PEP_AUTO_CONSUME_NAME_OLD    = "pEp-auto-consume"; // legacy - see OUT-522
    2.11 +        public const string PR_PEP_KEY_IMPORT_NAME          = "X-pEp-key-import";
    2.12 +        public const string PR_PEP_KEY_IMPORT_NAME_OLD      = "pEp-key-import"; // legacy - see OUT-522
    2.13          public const string PR_PEP_FORCE_PROTECTION_NAME    = "X-pEp-force-protection";
    2.14          public const string PR_PEP_NEVER_UNSECURE_NAME      = "X-pEp-Never-Unsecure";
    2.15          public const string PR_PEP_PROTOCOL_VERSION_NAME    = "X-pEp-Version";
    2.16 @@ -38,10 +40,12 @@
    2.17          public static readonly MapiProperty.MapiProp PidNameEncStatus          = new MapiProperty.MapiProp(PR_ENC_STATUS_NAME,              MapiProperty.PS_INTERNET_HEADERS);
    2.18          public static readonly MapiProperty.MapiProp PidNameKeyList            = new MapiProperty.MapiProp(PR_KEY_LIST_NAME,                MapiProperty.PS_INTERNET_HEADERS);
    2.19          public static readonly MapiProperty.MapiProp PidNamePEPAutoConsume     = new MapiProperty.MapiProp(PR_PEP_AUTO_CONSUME_NAME,        MapiProperty.PS_INTERNET_HEADERS);
    2.20 +        public static readonly MapiProperty.MapiProp PidNamePEPAutoConsumeOld  = new MapiProperty.MapiProp(PR_PEP_AUTO_CONSUME_NAME_OLD,    MapiProperty.PS_INTERNET_HEADERS);
    2.21          public static readonly MapiProperty.MapiProp PidNamePEPForceProtection = new MapiProperty.MapiProp(PR_PEP_FORCE_PROTECTION_NAME,    MapiProperty.PS_INTERNET_HEADERS);
    2.22          public static readonly MapiProperty.MapiProp PidNamePEPNeverUnsecure   = new MapiProperty.MapiProp(PR_PEP_NEVER_UNSECURE_NAME,      MapiProperty.PS_INTERNET_HEADERS);
    2.23          public static readonly MapiProperty.MapiProp PidNamePEPProtocolVersion = new MapiProperty.MapiProp(PR_PEP_PROTOCOL_VERSION_NAME,    MapiProperty.PS_INTERNET_HEADERS);
    2.24          public static readonly MapiProperty.MapiProp PidNamePEPKeyImport       = new MapiProperty.MapiProp(PR_PEP_KEY_IMPORT_NAME,          MapiProperty.PS_INTERNET_HEADERS);
    2.25 +        public static readonly MapiProperty.MapiProp PidNamePEPKeyImportOld    = new MapiProperty.MapiProp(PR_PEP_KEY_IMPORT_NAME_OLD,      MapiProperty.PS_INTERNET_HEADERS);
    2.26  
    2.27          public const string PEP_HIDDEN_SYNC_MESSAGE_BODY                 = "This message is part of p≡p's concept to synchronize. \n\nYou can safely ignore it. It will be deleted automatically.";
    2.28          public const string PEP_HIDDEN_SYNC_MESSAGE_SUBJECT              = "p≡p synchronization message - please ignore";