Import GnuPG keys during startup if necessary sync
authorThomas
Mon, 20 May 2019 09:33:57 +0200
branchsync
changeset 262805efdbd7c21d
parent 2627 47eb50143f2a
child 2629 ce9d82a16cea
Import GnuPG keys during startup if necessary
ThisAddIn.cs
     1.1 --- a/ThisAddIn.cs	Fri May 17 08:58:33 2019 +0200
     1.2 +++ b/ThisAddIn.cs	Mon May 20 09:33:57 2019 +0200
     1.3 @@ -1,4 +1,5 @@
     1.4 -´╗┐using pEp.Extensions;
     1.5 +´╗┐using Microsoft.Win32;
     1.6 +using pEp.Extensions;
     1.7  using pEp.UI;
     1.8  using pEpCOMServerAdapterLib;
     1.9  using System;
    1.10 @@ -2693,9 +2694,6 @@
    1.11              this._Settings.LoadFromRegistry();
    1.12              this.SyncAccountsList();
    1.13  
    1.14 -            // Set Cipher Suite
    1.15 -            ThisAddIn.PEPEngine.ConfigCipherSuite();
    1.16 -
    1.17  #if !READER_RELEASE_MODE
    1.18              // Connect callbacks, use PEPEngine property accessor in case the engine is not already initialized
    1.19              Log.Info("ThisAddIn_Startup: Register callbacks");
    1.20 @@ -2703,6 +2701,12 @@
    1.21              ThisAddIn.PEPEngine.RegisterCallbacks(this.adapterCallbacks);
    1.22  #endif
    1.23  
    1.24 +            // Import GPG keys if necessary
    1.25 +            this.ImportGPGKeys();
    1.26 +
    1.27 +            // Set Cipher Suite
    1.28 +            ThisAddIn.PEPEngine.ConfigCipherSuite();
    1.29 +
    1.30              /* Register all accounts as own identities in the engine
    1.31               * Warning: This must be done BEFORE synchronizing settings because SyncWithSettings() will use 
    1.32               * the engine's OwnIdentitiesRetrieve list to synchronize own identity flags such as IsSyncEnabled.
    1.33 @@ -2777,6 +2781,111 @@
    1.34          }
    1.35  
    1.36          /// <summary>
    1.37 +        /// Imports a file with GPG keys from disk if available.
    1.38 +        /// </summary>
    1.39 +        private void ImportGPGKeys()
    1.40 +        {
    1.41 +            string gpgKeysFilePath = null;
    1.42 +            string importKeyRegistryEntry = "ImportGPGKeys";
    1.43 +
    1.44 +            try
    1.45 +            {
    1.46 +                // Check in Registry if a key import file exists
    1.47 +                using (RegistryKey key = Registry.LocalMachine.OpenSubKey(Path.Combine("SOFTWARE", "pEp", importKeyRegistryEntry)))
    1.48 +                {
    1.49 +                    gpgKeysFilePath = key.GetValue("KeyFilePath") as string;
    1.50 +                }
    1.51 +            }
    1.52 +            catch (Exception ex)
    1.53 +            {
    1.54 +                gpgKeysFilePath = null;
    1.55 +                Log.Error("ImportGPGKeys: Error accessing LM Registry key. " + ex.ToString());
    1.56 +            }
    1.57 +
    1.58 +            // If nothing was found or an error occured, check also the CU scope
    1.59 +            if (string.IsNullOrEmpty(gpgKeysFilePath))
    1.60 +            {
    1.61 +                try
    1.62 +                {
    1.63 +                    using (RegistryKey key = Registry.CurrentUser.OpenSubKey(Path.Combine("SOFTWARE", "pEp", importKeyRegistryEntry)))
    1.64 +                    {
    1.65 +                        gpgKeysFilePath = key.GetValue("KeyFilePath") as string;
    1.66 +                    }
    1.67 +                }
    1.68 +                catch (Exception ex)
    1.69 +                {
    1.70 +                    gpgKeysFilePath = null;
    1.71 +                    Log.Error("ImportGPGKeys: Error accessing CU Registry key. " + ex.ToString());
    1.72 +                }
    1.73 +            }
    1.74 +
    1.75 +            // If key import file exists, import it
    1.76 +            if ((string.IsNullOrEmpty(gpgKeysFilePath) == false) &&
    1.77 +                (File.Exists(gpgKeysFilePath)))
    1.78 +            {
    1.79 +                Log.Verbose("ImportGPGKeys: Registry setting ImportGPGKeys found. Trying to import keys.");
    1.80 +
    1.81 +                bool keysImported = false;
    1.82 +                try
    1.83 +                {
    1.84 +                    ThisAddIn.PEPEngine.ImportKey(gpgKeysFilePath, out pEpIdentity[] privateKeys);
    1.85 +
    1.86 +                    for (int i = 0; i < privateKeys.Length; i++)
    1.87 +                    {
    1.88 +                        Log.Info($"ImportGPGKeys: Imported private key { privateKeys[i].fpr } for { privateKeys[i].Address }");
    1.89 +                    }
    1.90 +
    1.91 +                    keysImported = true;
    1.92 +                }
    1.93 +                catch (Exception ex)
    1.94 +                {
    1.95 +                    keysImported = false;
    1.96 +                    Log.Error("ImportGPGKeys: Error importing keys. " + ex.ToString());
    1.97 +                }
    1.98 +
    1.99 +                // If keys were imported, try to delete file and Registry entry
   1.100 +                if (keysImported)
   1.101 +                {
   1.102 +                    try
   1.103 +                    {
   1.104 +                        File.Delete(gpgKeysFilePath);
   1.105 +                        Log.Verbose("ImportGPGKeys: Key file deleted.");
   1.106 +                    }
   1.107 +                    catch (Exception ex)
   1.108 +                    {
   1.109 +                        Log.Error("ImportGPGKeys: Error deleting key file." + ex.ToString());
   1.110 +                    }
   1.111 +
   1.112 +                    try
   1.113 +                    {
   1.114 +                        using (RegistryKey key = Registry.LocalMachine.OpenSubKey(Path.Combine("SOFTWARE", "pEp")))
   1.115 +                        {
   1.116 +                            key.DeleteSubKey(importKeyRegistryEntry);
   1.117 +                            Log.Verbose("ImportGPGKeys: LM Registry entry deleted.");
   1.118 +                        }
   1.119 +                    }
   1.120 +                    catch (Exception ex)
   1.121 +                    {
   1.122 +                        Log.Error("ImportGPGKeys: Error deleting LM Registry entry." + ex.ToString());
   1.123 +                    }
   1.124 +
   1.125 +                    try
   1.126 +                    {
   1.127 +                        using (RegistryKey key = Registry.CurrentUser.OpenSubKey(Path.Combine("SOFTWARE", "pEp")))
   1.128 +                        {
   1.129 +                            key.DeleteSubKey(importKeyRegistryEntry);
   1.130 +                            Log.Verbose("ImportGPGKeys: CU Registry entry deleted.");
   1.131 +                        }
   1.132 +                    }
   1.133 +                    catch (Exception ex)
   1.134 +                    {
   1.135 +                        Log.Error("ImportGPGKeys: Error deleting CU Registry entry." + ex.ToString());
   1.136 +                    }
   1.137 +                }
   1.138 +            }
   1.139 +        }
   1.140 +
   1.141 +        /// <summary>
   1.142          /// Subscribes to application events or unsubscribes from them.
   1.143          /// </summary>
   1.144          /// <param name="subscribe">Whether to subscribe or unsubscribe</param>