Add key-import header OUT-428
authorThomas
Wed, 11 Apr 2018 15:10:34 +0200
branchOUT-428
changeset 2106 7ee0a280030d
parent 2105 ecd44ebb48e0
child 2107 a5d7688eb17c
Add key-import header
Extensions/MailItemExtensions.cs
PEPMessage.cs
--- a/Extensions/MailItemExtensions.cs	Tue Apr 10 11:47:55 2018 +0200
+++ b/Extensions/MailItemExtensions.cs	Wed Apr 11 15:10:34 2018 +0200
@@ -34,7 +34,8 @@
             PEPProtocolVersion,
             Rating,
             EnableProtection,
-            ForceProtection
+            ForceProtection,
+            KeyImport
         }
 
         /**************************************************************
@@ -2402,6 +2403,44 @@
                                 success = true;
                                 break;
                             }
+                        // Key import fpr MAPI property
+                        case PEPProperty.KeyImport:
+                            {
+                                /* Note that KeyImport is defined as:
+                                 *   ON : KeyImport property exists with a value of a key fingerprint
+                                 *  OFF : KeyImport property does not exist or has a value of null
+                                 */
+                                outValue = MailItemExtensions.GetPEPPropertyDefault(property);
+                                propertyValue = MapiHelper.GetProperty(omi, PEPMessage.PidNamePEPKeyImport, null);
+
+                                if (propertyValue != null)
+                                {
+                                    outValue = propertyValue as string;
+                                }
+                                else
+                                {
+                                    // As a backup search the original internet message headers
+                                    if (messageHeaders == null)
+                                    {
+                                        messageHeaders = omi.GetParsedTransportMessageHeaders();
+                                    }
+                                    if (messageHeaders != null)
+                                    {
+                                        foreach (Header header in messageHeaders)
+                                        {
+                                            if ((header.Field != null) &&
+                                                (header.Value != null) &&
+                                                (string.Equals(header.Field.Trim(), PEPMessage.PR_PEP_KEY_IMPORT_NAME, StringComparison.OrdinalIgnoreCase)))
+                                            {
+                                                outValue = header.Value.ToString();
+                                            }
+                                        }
+                                    }
+                                }
+
+                                success = true;
+                                break;
+                            }
                         // Key list MAPI property
                         case PEPProperty.KeyList:
                             {
@@ -2625,6 +2664,22 @@
 
                                 break;
                             }
+                        // Key import fpr MAPI property
+                        case PEPProperty.KeyImport:
+                            {
+                                // Note: cannot set the property value to null in PropertyAccessor.SetProperty()
+                                if (value != null)
+                                {
+                                    MapiHelper.SetProperty(omi, PEPMessage.PidNamePEPKeyImport, value.ToString());
+                                }
+                                else
+                                {
+                                    MapiHelper.DeleteProperty(omi, PEPMessage.PidNamePEPKeyImport);
+                                }
+
+                                success = true;
+                                break;
+                            }
                         // Key list MAPI property
                         case PEPProperty.KeyList:
                             {
--- a/PEPMessage.cs	Tue Apr 10 11:47:55 2018 +0200
+++ b/PEPMessage.cs	Wed Apr 11 15:10:34 2018 +0200
@@ -24,21 +24,23 @@
         public event PropertyChangedEventHandler PropertyChanged;
 
         // pEp named MAPI properties
-        public const string PR_ENC_STATUS_NAME           = "X-EncStatus";
-        public const string PR_KEY_LIST_NAME             = "X-KeyList";
-        public const string PR_PEP_AUTO_CONSUME_NAME     = "pEp-auto-consume";
-        public const string PR_PEP_FORCE_PROTECTION_NAME = "X-pEp-force-protection";
-        public const string PR_PEP_NEVER_UNSECURE_NAME   = "X-pEp-Never-Unsecure";
-        public const string PR_PEP_PROTOCOL_VERSION_NAME = "X-pEp-Version";
-
-        public const string PR_PEP_NEVER_UNSECURE_VALUE  = "yes";
-
-        public static readonly MapiProperty.MapiProp PidNameEncStatus          = new MapiProperty.MapiProp(PR_ENC_STATUS_NAME,           MapiProperty.PS_INTERNET_HEADERS);
-        public static readonly MapiProperty.MapiProp PidNameKeyList            = new MapiProperty.MapiProp(PR_KEY_LIST_NAME,             MapiProperty.PS_INTERNET_HEADERS);
-        public static readonly MapiProperty.MapiProp PidNamePEPAutoConsume     = new MapiProperty.MapiProp(PR_PEP_AUTO_CONSUME_NAME,     MapiProperty.PS_INTERNET_HEADERS);
-        public static readonly MapiProperty.MapiProp PidNamePEPForceProtection = new MapiProperty.MapiProp(PR_PEP_FORCE_PROTECTION_NAME, MapiProperty.PS_INTERNET_HEADERS);
-        public static readonly MapiProperty.MapiProp PidNamePEPNeverUnsecure   = new MapiProperty.MapiProp(PR_PEP_NEVER_UNSECURE_NAME,   MapiProperty.PS_INTERNET_HEADERS);
-        public static readonly MapiProperty.MapiProp PidNamePEPProtocolVersion = new MapiProperty.MapiProp(PR_PEP_PROTOCOL_VERSION_NAME, MapiProperty.PS_INTERNET_HEADERS);
+        public const string PR_ENC_STATUS_NAME              = "X-EncStatus";
+        public const string PR_KEY_LIST_NAME                = "X-KeyList";
+        public const string PR_PEP_AUTO_CONSUME_NAME        = "pEp-auto-consume";
+        public const string PR_PEP_KEY_IMPORT_NAME          = "pEp-key-import";
+        public const string PR_PEP_FORCE_PROTECTION_NAME    = "X-pEp-force-protection";
+        public const string PR_PEP_NEVER_UNSECURE_NAME      = "X-pEp-Never-Unsecure";
+        public const string PR_PEP_PROTOCOL_VERSION_NAME    = "X-pEp-Version";
+
+        public const string PR_PEP_NEVER_UNSECURE_VALUE     = "yes";
+
+        public static readonly MapiProperty.MapiProp PidNameEncStatus          = new MapiProperty.MapiProp(PR_ENC_STATUS_NAME,              MapiProperty.PS_INTERNET_HEADERS);
+        public static readonly MapiProperty.MapiProp PidNameKeyList            = new MapiProperty.MapiProp(PR_KEY_LIST_NAME,                MapiProperty.PS_INTERNET_HEADERS);
+        public static readonly MapiProperty.MapiProp PidNamePEPAutoConsume     = new MapiProperty.MapiProp(PR_PEP_AUTO_CONSUME_NAME,        MapiProperty.PS_INTERNET_HEADERS);
+        public static readonly MapiProperty.MapiProp PidNamePEPForceProtection = new MapiProperty.MapiProp(PR_PEP_FORCE_PROTECTION_NAME,    MapiProperty.PS_INTERNET_HEADERS);
+        public static readonly MapiProperty.MapiProp PidNamePEPNeverUnsecure   = new MapiProperty.MapiProp(PR_PEP_NEVER_UNSECURE_NAME,      MapiProperty.PS_INTERNET_HEADERS);
+        public static readonly MapiProperty.MapiProp PidNamePEPProtocolVersion = new MapiProperty.MapiProp(PR_PEP_PROTOCOL_VERSION_NAME,    MapiProperty.PS_INTERNET_HEADERS);
+        public static readonly MapiProperty.MapiProp PidNamePEPKeyImport       = new MapiProperty.MapiProp(PR_PEP_KEY_IMPORT_NAME,          MapiProperty.PS_INTERNET_HEADERS);
 
         private static object                     mutexMirror           = new object();
         private static Dictionary<string, string> mirrorCache           = new Dictionary<string, string>();
@@ -56,6 +58,7 @@
         private bool                _ForceUnencrypted;
         private PEPIdentity         _From;
         private string              _Id;
+        private string              _KeyImport;
         private string              _KeyList;
         private List<string>        _Keywords;
         private string              _LongMsg;
@@ -329,6 +332,19 @@
         }
 
         /// <summary>
+        /// Gets or sets the partner fingerprint of a key import message.
+        /// </summary>
+        public string KeyImport
+        {
+            get { return (this._KeyImport); }
+            set
+            {
+                this._KeyImport = value;
+                this.RaisePropertyChangedEvent(nameof(this.KeyImport));
+            }
+        }
+
+        /// <summary>
         /// Gets or sets the list of keys associated with this message.
         /// Commonly this contains the list of decryption keys.
         /// Warning: Since this is stored as a header field, care must be taken not to apply this to a MailItem on an 
@@ -694,6 +710,15 @@
             }
 
             if ((alwaysAddOptFields) ||
+                (string.IsNullOrEmpty(this._KeyImport) == false))
+            {
+                field = new StringPair();
+                field.Name = PEPMessage.PR_PEP_KEY_IMPORT_NAME;
+                field.Value = this._KeyImport;
+                optionalFields.Add(field);
+            }
+
+            if ((alwaysAddOptFields) ||
                 (string.IsNullOrEmpty(this._KeyList) == false))
             {
                 field = new StringPair();
@@ -777,6 +802,7 @@
              *   • Rating
              *   • ForceProtection
              *   • NeverUnsecure
+             *   • KeyImportFpr
              *   • KeyList
              *   • PEPProtocolVersion
              * 
@@ -957,6 +983,20 @@
                 }
             }
 
+            if (string.IsNullOrEmpty(pEpMessage.KeyImport) == false)
+            {
+                try
+                {
+                    Header header = new Header(Encoding.Unicode, PEPMessage.PR_PEP_KEY_IMPORT_NAME, pEpMessage.KeyImport);
+                    message.Headers.Add(header);
+                }
+                catch (Exception ex)
+                {
+                    status = Globals.ReturnStatus.Failure;
+                    Log.Error("ToMIMEMessage: Error adding KeyImportFpr header. " + ex.ToString());
+                }
+            }
+
             if (string.IsNullOrEmpty(pEpMessage.KeyList) == false)
             {
                 try
@@ -1086,6 +1126,7 @@
                 Comparisons.Equals(this.ForceUnencrypted, obj.ForceUnencrypted) &&
                 Comparisons.Equals(this.From, obj.From) &&
                 Comparisons.Equals(this.Id, obj.Id) &&
+                Comparisons.Equals(this.KeyImport, obj.KeyImport) &&
                 Comparisons.Equals(this.KeyList, obj.KeyList) &&
                 Comparisons.Equals(this.Keywords, obj.Keywords) &&
                 Comparisons.Equals(this.LongMsg, obj.LongMsg) &&
@@ -1186,6 +1227,7 @@
             copy.ForceUnencrypted = this._ForceUnencrypted;
             copy.From = (this._From == null ? null : this._From.Copy());
             copy.Id = (this._Id == null ? null : string.Copy(this._Id));
+            copy.KeyImport = (this._KeyImport == null ? null : string.Copy(this._KeyImport));
             copy.KeyList = (this._KeyList == null ? null : string.Copy(this._KeyList));
 
             // Keywords
@@ -1263,6 +1305,7 @@
             this._ForceUnencrypted = false;
             this._From = null;
             this._Id = null;
+            this._KeyImport = null;
             this._KeyList = null;
             this._Keywords = new List<string>();
             this._LongMsg = null;
@@ -1289,6 +1332,7 @@
             this.RaisePropertyChangedEvent(nameof(this.ForceUnencrypted));
             this.RaisePropertyChangedEvent(nameof(this.From));
             this.RaisePropertyChangedEvent(nameof(this.Id));
+            this.RaisePropertyChangedEvent(nameof(this.KeyImport));
             this.RaisePropertyChangedEvent(nameof(this.KeyList));
             this.RaisePropertyChangedEvent(nameof(this.Keywords));
             this.RaisePropertyChangedEvent(nameof(this.LongMsg));
@@ -1301,8 +1345,6 @@
             this.RaisePropertyChangedEvent(nameof(this.SentOn));
             this.RaisePropertyChangedEvent(nameof(this.ShortMsg));
             this.RaisePropertyChangedEvent(nameof(this.To));
-
-            return;
         }
 
         /// <summary>
@@ -1383,6 +1425,10 @@
             omi.GetPEPProperty(MailItemExtensions.PEPProperty.Rating, out propValue);
             this.Rating = (pEpRating)propValue;
 
+            // KeyImportFpr
+            omi.GetPEPProperty(MailItemExtensions.PEPProperty.KeyImport, out propValue);
+            this.KeyImport = propValue as string;
+
             // KeyList
             omi.GetPEPProperty(MailItemExtensions.PEPProperty.KeyList, out propValue);
             this.KeyList = propValue as string;
@@ -1927,6 +1973,7 @@
                 }
 
                 // Note: ignore return status
+                omi.SetPEPProperty(MailItemExtensions.PEPProperty.KeyImport, this._KeyImport);
                 omi.SetPEPProperty(MailItemExtensions.PEPProperty.AutoConsume, this._AutoConsume);
                 omi.SetPEPProperty(MailItemExtensions.PEPProperty.ForceProtection, this._ForceProtectionId);
                 omi.SetPEPProperty(MailItemExtensions.PEPProperty.NeverUnsecure, this._NeverUnsecure);
@@ -2307,6 +2354,11 @@
                                         newMessage.ForceProtectionId = optField.Value;
                                         break;
                                     }
+                                case PEPMessage.PR_PEP_KEY_IMPORT_NAME:
+                                    {
+                                        newMessage.KeyImport = optField.Value;
+                                        break;
+                                    }
                                 case PEPMessage.PR_KEY_LIST_NAME:
                                     {
                                         newMessage.KeyList = optField.Value;
@@ -2841,6 +2893,10 @@
                             {
                                 pEpMessage.KeyList = header.Value;
                             }
+                            else if (string.Equals(header.Field.Trim(), PEPMessage.PR_PEP_KEY_IMPORT_NAME, StringComparison.OrdinalIgnoreCase))
+                            {
+                                pEpMessage.KeyImport = header.Value;
+                            }
                             else if (string.Equals(header.Field.Trim(), PEPMessage.PR_PEP_AUTO_CONSUME_NAME, StringComparison.OrdinalIgnoreCase))
                             {
                                 pEpMessage.AutoConsume = header.Value;