OUT-477: Check in Registry if S/MIME is enabled.
authorThomas
Mon, 16 Jul 2018 12:25:29 +0200
changeset 2295e64498e9af92
parent 2291 d20c91b79b47
child 2296 8be60b97b838
OUT-477: Check in Registry if S/MIME is enabled.
Extensions/MailItemExtensions.cs
OutlookOptions.cs
     1.1 --- a/Extensions/MailItemExtensions.cs	Fri Jul 13 10:22:35 2018 +0200
     1.2 +++ b/Extensions/MailItemExtensions.cs	Mon Jul 16 12:25:29 2018 +0200
     1.3 @@ -255,7 +255,8 @@
     1.4              /* If S/MIME is enabled by default, the mirror creation by new mail item fails during the
     1.5               * Save() method. 
     1.6               */
     1.7 -            if (omi.GetIsSMIMEEnabled() == false)
     1.8 +            if ((Globals.ThisAddIn.OutlookOptions.IsSMIMEEnabled == false) &&
     1.9 +                (omi.GetIsSMIMEEnabled() == false))
    1.10              {
    1.11                  try
    1.12                  {
     2.1 --- a/OutlookOptions.cs	Fri Jul 13 10:22:35 2018 +0200
     2.2 +++ b/OutlookOptions.cs	Mon Jul 16 12:25:29 2018 +0200
     2.3 @@ -19,6 +19,13 @@
     2.4          private readonly string officeVersion = Globals.ThisAddIn.Application.Version.Substring(0, 2) + ".0";
     2.5  
     2.6          /// <summary>
     2.7 +        /// Gets whether S/MIME encryption or signatures are enabled.
     2.8 +        /// Note: The registry value that is set when enabling S/MIME won't get unset or changed
     2.9 +        ///       again after disabling it. So this might yield a false positive.
    2.10 +        /// </summary>
    2.11 +        public bool IsSMIMEEnabled { get; set; } = false;
    2.12 +
    2.13 +        /// <summary>
    2.14          /// Read email messages in plain text
    2.15          /// See: https://support.office.com/en-us/article/Read-email-messages-in-plain-text-2443DA66-4AB8-451E-98A8-489A58D00A40
    2.16          /// </summary>
    2.17 @@ -29,8 +36,14 @@
    2.18          /// </summary>
    2.19          public void ReadOptionsFromRegistry()
    2.20          {
    2.21 +            // Path to Outlook
    2.22 +            string registryPathOutlook = Path.Combine("Software", "Microsoft", "Office", officeVersion, "Outlook");
    2.23 +
    2.24 +            // Path to Security settings
    2.25 +            string registryPathSecurity = Path.Combine(registryPathOutlook, "Security");
    2.26 +
    2.27              // Path to Outlook options
    2.28 -            string registryPathOptions = Path.Combine("Software", "Microsoft", "Office", officeVersion, "Outlook", "Options");
    2.29 +            string registryPathOptions = Path.Combine(registryPathOutlook, "Options");
    2.30  
    2.31              // Path to Mail options
    2.32              string registryPathMail = Path.Combine(registryPathOptions, "Mail");
    2.33 @@ -40,7 +53,7 @@
    2.34              {
    2.35                  using (RegistryKey mailOptions = Registry.CurrentUser.OpenSubKey(registryPathMail))
    2.36                  {
    2.37 -                    this.ReadAsPlain = mailOptions?.GetValue(nameof(ReadAsPlain)) as int? == 1;
    2.38 +                    this.ReadAsPlain = ((mailOptions?.GetValue(nameof(ReadAsPlain)) as int?) == 1);
    2.39                  }
    2.40              }
    2.41              catch (Exception ex)
    2.42 @@ -48,6 +61,28 @@
    2.43                  this.ReadAsPlain = false;
    2.44                  Log.Error("OutlookOptions.ReadOptionsFromRegistry: Error getting ReadAsPlain value. " + ex.ToString());
    2.45              }
    2.46 +
    2.47 +            // Get whether an S/MIME option is enabled
    2.48 +            try
    2.49 +            {
    2.50 +                using (RegistryKey securityOptions = Registry.CurrentUser.OpenSubKey(registryPathSecurity))
    2.51 +                {
    2.52 +                    /* If S/MIME encryption or signature is enabled by default through the Outlook UI,
    2.53 +                     * the Registry values "InitEncrypt" and/or InitSign" are being added to 
    2.54 +                     * HKCU\Software\Microsoft\Office\<version no.>\Outlook\Security and set to 2 (enabled).
    2.55 +                     * However, if they are being disabled again through the UI, those values remain with
    2.56 +                     * the same value (at least in some environments). The following condition is therefore prone
    2.57 +                     * to yield false positives. Until a better solution is found, we will use this approximation.
    2.58 +                     */ 
    2.59 +                    this.IsSMIMEEnabled = (((securityOptions?.GetValue("InitEncrypt") as int?) == 2) ||
    2.60 +                                           ((securityOptions?.GetValue("InitSign") as int?) == 2));
    2.61 +                }
    2.62 +            }
    2.63 +            catch (Exception ex)
    2.64 +            {
    2.65 +                this.IsSMIMEEnabled = false;
    2.66 +                Log.Error("OutlookOptions.ReadOptionsFromRegistry: Error getting IsSMIMEEnabled value. " + ex.ToString());
    2.67 +            }
    2.68          }
    2.69  
    2.70          /// <summary>