Some restructuring OUT-428
authorThomas
Wed, 11 Apr 2018 18:02:42 +0200
branchOUT-428
changeset 210841eb44080146
parent 2107 a5d7688eb17c
child 2114 03544e99ff67
Some restructuring
UI/KeySyncWizard.xaml.cs
     1.1 --- a/UI/KeySyncWizard.xaml.cs	Wed Apr 11 15:25:48 2018 +0200
     1.2 +++ b/UI/KeySyncWizard.xaml.cs	Wed Apr 11 18:02:42 2018 +0200
     1.3 @@ -52,15 +52,14 @@
     1.4          /// </summary>
     1.5          public event PropertyChangedEventHandler PropertyChanged;
     1.6  
     1.7 -        private ObservableCollection<string>            _KeysToExport;
     1.8 -        private ObservableCollection<string>            _KeysToImport;
     1.9 +        private string                                  _ExplanationText;
    1.10 +        private ObservableCollection<HandshakeItem>     _Items;
    1.11 +        private PEPIdentity                             _Myself;
    1.12          private string                                  _NextButtonText;
    1.13 -        private string                                  _ExplanationText;
    1.14 -        private WizardType                              _Type;
    1.15 +        private PEPIdentity                             _Partner;
    1.16          private WizardState                             _State;
    1.17          private bool                                    _Success;
    1.18 -        private PEPIdentity                             _SyncPartner;
    1.19 -        private ObservableCollection<HandshakeItem>     _Items;
    1.20 +        private WizardType                              _Type;
    1.21  
    1.22          public static KeySyncWizard                     Wizard = null;
    1.23  
    1.24 @@ -94,13 +93,13 @@
    1.25              this.Reset();
    1.26  
    1.27              // Initialize the wizard
    1.28 -            this.InitializeWizard(type);
    1.29 +            this.InitializeWizard(type, myself, partnerFpr);
    1.30  
    1.31              // Attach event handler for new sync messages
    1.32              CryptableMailItem.SyncMessageReceived += MsgProcessor_SyncMessageReceived;
    1.33  
    1.34              // Send initial message
    1.35 -            this.SendSyncMessage(myself, partnerFpr);
    1.36 +            this.SendSyncMessage(MessageTypes.InitialMessage);
    1.37  
    1.38              // Set current wizard
    1.39              KeySyncWizard.Wizard = this;
    1.40 @@ -127,28 +126,28 @@
    1.41          }
    1.42  
    1.43          /// <summary>
    1.44 -        /// Gets or sets the list of keys to export.
    1.45 +        /// Gets or sets the Items collection.
    1.46          /// </summary>
    1.47 -        public ObservableCollection<string> KeysToExport
    1.48 +        public ObservableCollection<HandshakeItem> Items
    1.49          {
    1.50 -            get { return this._KeysToExport; }
    1.51 +            get { return this._Items; }
    1.52              set
    1.53              {
    1.54 -                this._KeysToExport = value;
    1.55 -                this.RaisePropertyChangedEvent(nameof(this.KeysToExport));
    1.56 +                this._Items = value;
    1.57 +                this.RaisePropertyChangedEvent(nameof(this.Items));
    1.58              }
    1.59          }
    1.60  
    1.61          /// <summary>
    1.62 -        /// Gets or sets the list of keys to import.
    1.63 +        /// Gets or sets the own identity.
    1.64          /// </summary>
    1.65 -        public ObservableCollection<string> KeysToImport
    1.66 +        public PEPIdentity Myself
    1.67          {
    1.68 -            get { return this._KeysToImport; }
    1.69 +            get { return this._Myself; }
    1.70              set
    1.71              {
    1.72 -                this._KeysToImport = value;
    1.73 -                this.RaisePropertyChangedEvent(nameof(this.KeysToImport));
    1.74 +                this._Myself = value;
    1.75 +                this.RaisePropertyChangedEvent(nameof(this.Myself));
    1.76              }
    1.77          }
    1.78  
    1.79 @@ -166,19 +165,6 @@
    1.80          }
    1.81  
    1.82          /// <summary>
    1.83 -        /// Gets or sets the Items collection.
    1.84 -        /// </summary>
    1.85 -        public ObservableCollection<HandshakeItem> Items
    1.86 -        {
    1.87 -            get { return this._Items; }
    1.88 -            set
    1.89 -            {
    1.90 -                this._Items = value;
    1.91 -                this.RaisePropertyChangedEvent(nameof(this.Items));
    1.92 -            }
    1.93 -        }
    1.94 -
    1.95 -        /// <summary>
    1.96          /// Gets or sets the wizard state.
    1.97          /// </summary>
    1.98          public WizardState State
    1.99 @@ -192,6 +178,19 @@
   1.100          }
   1.101  
   1.102          /// <summary>
   1.103 +        /// Gets or sets the sync partner.
   1.104 +        /// </summary>
   1.105 +        public PEPIdentity Partner
   1.106 +        {
   1.107 +            get { return this._Partner; }
   1.108 +            set
   1.109 +            {
   1.110 +                this._Partner = value;
   1.111 +                this.RaisePropertyChangedEvent(nameof(this.Partner));
   1.112 +            }
   1.113 +        }
   1.114 +
   1.115 +        /// <summary>
   1.116          /// Gets or sets the success state.
   1.117          /// </summary>
   1.118          public bool Success
   1.119 @@ -205,19 +204,6 @@
   1.120          }
   1.121  
   1.122          /// <summary>
   1.123 -        /// Gets or sets the sync partner.
   1.124 -        /// </summary>
   1.125 -        public PEPIdentity SyncPartner
   1.126 -        {
   1.127 -            get { return this._SyncPartner; }
   1.128 -            set
   1.129 -            {
   1.130 -                this._SyncPartner = value;
   1.131 -                this.RaisePropertyChangedEvent(nameof(this.SyncPartner));
   1.132 -            }
   1.133 -        }
   1.134 -
   1.135 -        /// <summary>
   1.136          /// Gets or sets the wizard type.
   1.137          /// </summary>
   1.138          public WizardType Type
   1.139 @@ -306,29 +292,28 @@
   1.140                  this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(() =>
   1.141                  {
   1.142                      // Public key message received. Show trustwords.
   1.143 -                    PEPIdentity myself = new PEPIdentity(ThisAddIn.PEPEngine.Myself(e.Message.To[0].ToCOMType()));
   1.144 -                    PEPIdentity partner = myself.Copy();
   1.145 -                    string partnerKey  = e.Message?.KeyImport;
   1.146 +                    string partnerFpr = e.Message?.KeyImport;
   1.147  
   1.148                      // In case of PGP import, we have no KeyImport header. Try to get from KeyList.
   1.149 -                    if (partnerKey == null)
   1.150 +                    if (partnerFpr == null)
   1.151                      {
   1.152                          string[] keys = e.Message?.KeyList.Split(',');
   1.153                          foreach (var key in keys)
   1.154                          {
   1.155 -                            if (key?.Equals(myself?.Fingerprint) == false)
   1.156 +                            if (key?.Equals(this.Myself?.Fingerprint) == false)
   1.157                              {
   1.158 -                                partnerKey = key;
   1.159 +                                partnerFpr = key;
   1.160                                  break;
   1.161                              }
   1.162                          }
   1.163                      }
   1.164  
   1.165 -                    partner.Fingerprint = partnerKey;
   1.166 -                    this.KeysToImport.Add(partnerKey);
   1.167 +                    // Set the partner's fingerprint
   1.168 +                    this.Partner.Fingerprint = partnerFpr;
   1.169  
   1.170 +                    // Create the handshake item 
   1.171                      HandshakeItem item;
   1.172 -                    if (HandshakeItem.Create(myself, partner, false, out item) == Globals.ReturnStatus.Success)
   1.173 +                    if (HandshakeItem.Create(this.Myself, this.Partner, false, out item) == Globals.ReturnStatus.Success)
   1.174                      {
   1.175                          item.AreTabControlsVisible = (this.Type == WizardType.PGP);
   1.176                          item.ActiveTab = (this.Type == WizardType.pEp ? HandshakeItem.Tabs.Trustwords : HandshakeItem.Tabs.Fingerprint);
   1.177 @@ -353,7 +338,7 @@
   1.178                      // Private key message received. Set key as default.
   1.179                      try
   1.180                      {
   1.181 -                        ThisAddIn.PEPEngine.SetOwnKey(this._SyncPartner.ToCOMType(), this.KeysToImport[0]);
   1.182 +                        ThisAddIn.PEPEngine.SetOwnKey(this._Partner.ToCOMType(), this.Partner.Fingerprint);
   1.183                          this.GoToLastStep(true);
   1.184                      }
   1.185                      catch (Exception ex)
   1.186 @@ -419,23 +404,37 @@
   1.187          /// <summary>
   1.188          /// Initializes the wizard.
   1.189          /// </summary>
   1.190 -        private void InitializeWizard(WizardType type)
   1.191 +        private void InitializeWizard(WizardType type,
   1.192 +                                      PEPIdentity myself,
   1.193 +                                      string partnerFpr)
   1.194          {
   1.195              // Set initial state
   1.196 +            this.DataContext = this;
   1.197 +            this.Myself = myself;
   1.198 +            this.State = WizardState.Step1;
   1.199              this.Type = type;
   1.200 -            this.DataContext = this;
   1.201 -            this.State = WizardState.Step1;
   1.202 +
   1.203 +            // If we have a partner fpr, set identity
   1.204 +            if ((this.Myself != null) &&
   1.205 +                (partnerFpr != null))
   1.206 +            {
   1.207 +                this.Partner = this.Myself.Copy();
   1.208 +                this.Partner.Fingerprint = partnerFpr;
   1.209 +            }
   1.210 +
   1.211 +            // Update dialog content
   1.212              this.UpdateContent();
   1.213          }
   1.214  
   1.215          /// <summary>
   1.216          /// Sends the next sync message.
   1.217          /// </summary>
   1.218 -        private void SendSyncMessage(PEPIdentity ownIdentity, string partnerFpr)
   1.219 +        private void SendSyncMessage(MessageTypes messageType)
   1.220          {
   1.221              // If own identity is null, get default account
   1.222 -            if (ownIdentity == null)
   1.223 +            if (this.Myself == null)
   1.224              {
   1.225 +                PEPIdentity ownIdentity = null;
   1.226                  Outlook.Account account = null;
   1.227                  Outlook.Accounts accounts = null;
   1.228                  Outlook.Store deliveryStore = null;
   1.229 @@ -475,15 +474,22 @@
   1.230                                  ownIdentity = null;
   1.231                                  Log.Error("SendSyncMessage: Error getting own identity. ReturnStatus != Success.");
   1.232                              }
   1.233 +                            else
   1.234 +                            {
   1.235 +                                ownIdentity = new PEPIdentity(ThisAddIn.PEPEngine.Myself(ownIdentity.ToCOMType()));
   1.236 +                            }
   1.237                              break;
   1.238                          }
   1.239  
   1.240                          account = null;
   1.241                          deliveryStore = null;
   1.242                      }
   1.243 +
   1.244 +                    this.Myself = ownIdentity;
   1.245                  }
   1.246                  catch (Exception ex)
   1.247                  {
   1.248 +                    ownIdentity = null;
   1.249                      Log.Error("SendSyncMessage: Error getting default account. " + ex.ToString());
   1.250                  }
   1.251                  finally
   1.252 @@ -498,36 +504,60 @@
   1.253              }
   1.254  
   1.255              // If we have an own identity, send message
   1.256 -            if (ownIdentity != null)
   1.257 +            if (this.Myself != null)
   1.258              {
   1.259 -                PEPMessage message = new PEPMessage
   1.260 +                try
   1.261                  {
   1.262 -                    From = ownIdentity,
   1.263 -                    ShortMsg = "pEp",
   1.264 -                    ForceUnencrypted = (string.IsNullOrEmpty(partnerFpr) ? true : false),
   1.265 -                    KeyImport = (this.Type == WizardType.pEp ? ownIdentity.Fingerprint : null)
   1.266 -                };
   1.267 -                message.To.Add(ownIdentity);
   1.268 +                    // Make sure the myself identity is up-to-date
   1.269 +                    if (this.Myself?.Fingerprint == null)
   1.270 +                    {
   1.271 +                        this.Myself = new PEPIdentity(ThisAddIn.PEPEngine.Myself(this.Myself.ToCOMType()));
   1.272 +                    }
   1.273  
   1.274 -                // If we have a partner fpr, encrypt for partner and add own key
   1.275 -                PEPMessage msg;
   1.276 -                if (string.IsNullOrEmpty(partnerFpr) == false)                    
   1.277 -                {
   1.278 -                    // TODO: Use engine method ThisAddIn.PEPEngine.EncryptAndAddKey();
   1.279 -                    if (this.EncryptAndAddPrivateKey(message, new string[] { partnerFpr }, out msg))
   1.280 +                    // If we have no partner at this point, use a copy of myself
   1.281 +                    if (this.Partner == null)
   1.282                      {
   1.283 -                        message = msg;
   1.284 +                        this.Partner = this.Myself.Copy();              
   1.285 +                    }
   1.286 +
   1.287 +                    // Create basic message
   1.288 +                    PEPMessage message = new PEPMessage
   1.289 +                    {
   1.290 +                        From = this.Myself,
   1.291 +                        ShortMsg = "pEp",
   1.292 +                        ForceUnencrypted = (messageType == MessageTypes.InitialMessage ? true : false),
   1.293 +                        KeyImport = (this.Type == WizardType.pEp ? this.Myself.Fingerprint : null)
   1.294 +                    };
   1.295 +                    message.To.Add(this.Partner);
   1.296 +
   1.297 +                    // If we have a partner fpr, encrypt for partner and add own key
   1.298 +                    PEPMessage msg;
   1.299 +                    if (messageType == MessageTypes.PrivateKeyMessage)
   1.300 +                    {
   1.301 +                        // TODO: Use engine method ThisAddIn.PEPEngine.EncryptAndAddKey();
   1.302 +                        if (this.AddPrivateKey(message, out msg))
   1.303 +                        {
   1.304 +                            message = msg;
   1.305 +                        }
   1.306 +                        else
   1.307 +                        {
   1.308 +                            Log.Error("SendSyncMessage: Error encrypting private key message.");
   1.309 +                            message = null;
   1.310 +                        }
   1.311 +                    }
   1.312 +
   1.313 +                    if (message != null)
   1.314 +                    {
   1.315 +                        Globals.ThisAddIn.CreateAndSendMessage(message, true, true, true);
   1.316                      }
   1.317                      else
   1.318                      {
   1.319 -                        Log.Error("SendSyncMessage: Error encrypting private key message.");
   1.320 -                        message = null;
   1.321 +                        Log.Error("SendSyncMessage: Myself identity is null.");
   1.322                      }
   1.323                  }
   1.324 -
   1.325 -                if (message != null)
   1.326 +                catch (Exception ex)
   1.327                  {
   1.328 -                    Globals.ThisAddIn.CreateAndSendMessage(message, true, true, true);
   1.329 +                    Log.Error("SendSyncMessage: Error sending sync message. " + ex.ToString());
   1.330                  }
   1.331              }
   1.332          }
   1.333 @@ -535,14 +565,15 @@
   1.334          /// <summary>
   1.335          /// Temp method. Has to be replaced by engine function.
   1.336          /// </summary>
   1.337 -        private bool EncryptAndAddPrivateKey(PEPMessage src, string[] extraKeys, out PEPMessage msg)
   1.338 +        private bool AddPrivateKey(PEPMessage src, out PEPMessage msg)
   1.339          {
   1.340              bool success = false;
   1.341 +            string keyFileName = null;
   1.342              PEPMessage message = null;
   1.343  
   1.344              try
   1.345              {
   1.346 -                string keyFileName = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "pEp", "private_key.asc");
   1.347 +                keyFileName = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "pEp", "private_key.asc");
   1.348                  string keyId = src.From.Fingerprint.Substring(48);
   1.349  
   1.350                  // Create GPG ProcessStartInfo
   1.351 @@ -573,14 +604,25 @@
   1.352                  else
   1.353                  {
   1.354                      message = src.Copy();
   1.355 +                    message.Attachments.Clear();
   1.356                      message.Attachments.Add(new PEPAttachment { Data = System.IO.File.ReadAllBytes(keyFileName), FileName = "secret_key.asc" });
   1.357 -                    message.To[0].Fingerprint = extraKeys[0];
   1.358                  }
   1.359              }
   1.360              catch (Exception ex)
   1.361              {
   1.362                  Log.Error("EncryptAndAddPrivateKey: Error encrypting private key message. " + ex.ToString());
   1.363              }
   1.364 +            finally
   1.365 +            {
   1.366 +                if (System.IO.File.Exists(keyFileName))
   1.367 +                {
   1.368 +                    try
   1.369 +                    {
   1.370 +                        System.IO.File.Delete(keyFileName);
   1.371 +                    }
   1.372 +                    catch { }
   1.373 +                }
   1.374 +            }
   1.375  
   1.376              msg = message;
   1.377              return success;
   1.378 @@ -595,8 +637,8 @@
   1.379              {
   1.380                  try
   1.381                  {
   1.382 -                    this._SyncPartner = this.Items[0].Partner;
   1.383 -                    pEpIdentity partner = this._SyncPartner.ToCOMType();
   1.384 +                    this._Partner = this.Items[0].Partner;
   1.385 +                    pEpIdentity partner = this._Partner.ToCOMType();
   1.386                      ThisAddIn.PEPEngine.TrustPersonalKey(partner);
   1.387  
   1.388                      this.GoToNextStep();
   1.389 @@ -651,13 +693,14 @@
   1.390          /// </summary>
   1.391          public void Reset()
   1.392          {
   1.393 -            this._KeysToExport = new ObservableCollection<string>();
   1.394 -            this._KeysToImport = new ObservableCollection<string>();
   1.395 +            this._ExplanationText = null;
   1.396              this._Items = new ObservableCollection<HandshakeItem>();
   1.397 +            this._Myself = null;
   1.398 +            this._NextButtonText = null;
   1.399 +            this._Partner = null;
   1.400              this._Type = WizardType.Undefined;
   1.401              this._State = WizardState.Undefined;
   1.402              this._Success = false;
   1.403 -            this._SyncPartner = null;
   1.404          }
   1.405  
   1.406          #endregion