/// <summary>
        /// Initializes various settings based on configuration.
        /// </summary>
        private void Initialize()
        {
            // Load the signing algorithm.
            try
            {
                this.algorithm = (DkimAlgorithmKind)Enum.Parse(
                    typeof(DkimAlgorithmKind),
                    Properties.Settings.Default.Algorithm,
                    true);
            }
            catch (Exception ex)
            {
                throw new ConfigurationErrorsException(
                          Resources.DkimSigningRoutingAgentFactory_BadAlgorithmConfig,
                          ex);
            }

            // Load the list of headers to sign in each message.
            var unparsedHeaders = Properties.Settings.Default.HeadersToSign;

            if (unparsedHeaders != null)
            {
                this.headersToSign = unparsedHeaders
                                     .Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            }

            try
            {
                XmlDocument xmlDoc = new XmlDocument();                    //* create an xml document object.
                xmlDoc.Load(this.GetType().Assembly.Location + ".config"); //* load the XML document from the specified file.
                XmlNodeList domainInfoList = xmlDoc.GetElementsByTagName("domainInfo");
                if (domainInfoList == null || domainInfoList.Count != 1)
                {
                    Logger.LogError("There is an error in your configuration file. domainInfo couldn't be initialized properly.");
                    return;
                }
                XmlNode domainInfo = domainInfoList.Item(0);

                domainSettings = new List <DomainElement>();

                foreach (XmlNode n in domainInfo.ChildNodes)
                {
                    DomainElement e = new DomainElement();
                    e.Domain         = n.Attributes["Domain"].Value;
                    e.Selector       = n.Attributes["Selector"].Value;
                    e.PrivateKeyFile = n.Attributes["PrivateKeyFile"].Value;
                    if (e.initElement(Path.GetDirectoryName(this.GetType().Assembly.Location)))
                    {
                        domainSettings.Add(e);
                    }
                }
                if (domainSettings.Count == 0)
                {
                    Logger.LogWarning("No domain configuration found. DKIM will do nothing.");
                }
            }
            catch (Exception e)
            {
                Logger.LogError("Couldn't load config: " + e.ToString());
            }
            Logger.LogInformation("Exchange DKIM started. Algorithm: " + algorithm.ToString() + " Number of domains: " + domainSettings.Count);
        }
Example #2
0
        /// <summary>
        /// Initializes various settings based on configuration.
        /// </summary>
        private void Initialize()
        {
            if (RegistryHelper.Open(@"Exchange DkimSigner") != null)
            {
                DkimAlgorithmKind        signingAlgorithm       = DkimAlgorithmKind.RsaSha1;
                DkimCanonicalizationKind headerCanonicalization = DkimCanonicalizationKind.Simple;
                DkimCanonicalizationKind bodyCanonicalization   = DkimCanonicalizationKind.Simple;
                IEnumerable <string>     headersToSign          = null;

                // Load the log level.
                DkimSigningRoutingAgentFactory.logLevel = 0;
                try
                {
                    string temp = RegistryHelper.Read("LogLevel", @"Exchange DkimSigner");

                    if (temp != null)
                    {
                        DkimSigningRoutingAgentFactory.logLevel = Convert.ToInt32(temp);
                    }
                }
                catch (FormatException) { }
                catch (OverflowException) { }

                if (logLevel == 0)
                {
                    throw new ConfigurationErrorsException(Resources.DkimSigningRoutingAgentFactory_BadLogLevel);
                }

                // Load the signing algorithm.
                try
                {
                    signingAlgorithm = (DkimAlgorithmKind)Enum.Parse(typeof(DkimAlgorithmKind), RegistryHelper.Read("Algorithm", @"Exchange DkimSigner\DKIM"), true);
                }
                catch (Exception ex)
                {
                    throw new ConfigurationErrorsException(Resources.DkimSigningRoutingAgentFactory_BadDkimAlgorithmConfig, ex);
                }

                // Load the header canonicalization algorithm.
                try
                {
                    headerCanonicalization = (DkimCanonicalizationKind)Enum.Parse(typeof(DkimCanonicalizationKind), RegistryHelper.Read("HeaderCanonicalization", @"Exchange DkimSigner\DKIM"), true);
                }
                catch (Exception ex)
                {
                    throw new ConfigurationErrorsException(Resources.DkimSigningRoutingAgentFactory_BadDkimCanonicalizationHeaderConfig, ex);
                }

                // Load the body canonicalization algorithm.
                try
                {
                    bodyCanonicalization = (DkimCanonicalizationKind)Enum.Parse(typeof(DkimCanonicalizationKind), RegistryHelper.Read("BodyCanonicalization", @"Exchange DkimSigner\DKIM"), true);
                }
                catch (Exception ex)
                {
                    throw new ConfigurationErrorsException(Resources.DkimSigningRoutingAgentFactory_BadDkimCanonicalizationBodyConfig, ex);
                }

                // Load the list of headers to sign in each message.
                string unparsedHeaders = RegistryHelper.Read("HeadersToSign", @"Exchange DkimSigner\DKIM");
                if (unparsedHeaders != null)
                {
                    headersToSign = unparsedHeaders.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                }

                // Load the list of domains
                domainSettings = new List <DomainElement>();
                string[] domainNames = RegistryHelper.GetSubKeyName(@"Exchange DkimSigner\Domain");
                if (domainNames != null)
                {
                    foreach (string domainName in domainNames)
                    {
                        string selector       = RegistryHelper.Read("Selector", @"Exchange DkimSigner\Domain\" + domainName);
                        string privateKeyFile = RegistryHelper.Read("PrivateKeyFile", @"Exchange DkimSigner\Domain\" + domainName);

                        DomainElement domainElement = new DomainElement(domainName,
                                                                        selector,
                                                                        privateKeyFile);

                        if (domainElement.initElement(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)))
                        {
                            domainSettings.Add(domainElement);
                        }
                    }
                }

                this.dkimSigner = new DkimSigner(signingAlgorithm,
                                                 headerCanonicalization,
                                                 bodyCanonicalization,
                                                 headersToSign);

                Logger.LogInformation("Exchange DKIM started. Signing Algorithm: " + signingAlgorithm.ToString() + ", Canonicalization Header Algorithm: " + headerCanonicalization.ToString() + ", Canonicalization Header Algorithm: " + bodyCanonicalization.ToString() + ", Number of domains: " + domainSettings.Count);
            }
            else
            {
                throw new ConfigurationErrorsException(Resources.DkimSigningRoutingAgentFactory_BadDkimConfig);
            }
        }