Example #1
0
        private void ValidateCertButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(CertificateLocation.Text))
            {
                return;
            }
            var ms = new MemoryStream();

            var textRange = new TextRange(PolicyRichTextBox.Document.ContentStart,
                                          PolicyRichTextBox.Document.ContentEnd);

            textRange.Save(ms, DataFormats.Text);
            ms.Position = 0;
            var       cert     = new X509Certificate2(CertificateLocation.Text);
            ICompiler compiler = new StackMachineCompiler();

            compiler.ReportModeEnabled = true;
            IPolicyFilter filter = new PolicyFilter(compiler, new StackMachine(),
                                                    new SimpleTextV1LexiconPolicyParser());
            var sb = new StringBuilder();

            sb.Append("Validation run at ")
            .AppendLine(DateTime.Now.ToString("ddd, MMM d yyyy HH:mm:ss"))
            .AppendLine();
            try
            {
                if (filter.IsCompliant(cert, ms))
                {
                    sb.Append("Certificate is compliant with the provided policy.");
                }
                else
                {
                    sb.AppendLine("Certificate is NOT compliant with the provided policy.").AppendLine();
                    foreach (string item in compiler.CompiliationReport)
                    {
                        sb.AppendLine(item);
                    }
                }
            }
            catch (PolicyRequiredException ex)
            {
                sb.AppendLine("Validation Successful")
                .AppendLine("Certificate is missing a required field \t")
                .AppendLine(ex.Message);
            }
            catch (PolicyGrammarException ex)
            {
                sb.AppendLine("Validation Failed:")
                .AppendLine("Error compiling policy\t")
                .AppendLine(ex.Message);
            }
            catch (Exception ex)
            {
                sb.AppendLine("Validation Failed:")
                .AppendLine("Error compiling or proccessing policy\t" + ex.Message)
                .AppendLine(ex.StackTrace);
            }

            ValidationResults.Text = sb.ToString();
        }
Example #2
0
        private void ValidateCertButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(CertificateLocation.Text))
            {
                return;
            }
            var ms = new MemoryStream();

            var textRange = new TextRange(PolicyRichTextBox.Document.ContentStart,
                PolicyRichTextBox.Document.ContentEnd);
            textRange.Save(ms, DataFormats.Text);
            ms.Position = 0;
            var cert = new X509Certificate2(CertificateLocation.Text);
            ICompiler compiler = new StackMachineCompiler();
            compiler.ReportModeEnabled = true;
            IPolicyFilter filter = new PolicyFilter(compiler, new StackMachine(),
                new SimpleTextV1LexiconPolicyParser());
            var sb = new StringBuilder();
            sb.Append("Validation run at ")
                .AppendLine(DateTime.Now.ToString("ddd, MMM d yyyy HH:mm:ss"))
                .AppendLine();
            try
            {
                if (filter.IsCompliant(cert, ms))
                {
                    sb.Append("Certificate is compliant with the provided policy.");
                }
                else
                {
                    sb.AppendLine("Certificate is NOT compliant with the provided policy.").AppendLine();
                    foreach (string item in compiler.CompiliationReport)
                    {
                        sb.AppendLine(item);
                    }
                }
            }
            catch (PolicyRequiredException ex)
            {
                sb.AppendLine("Validation Successful")
                    .AppendLine("Certificate is missing a required field \t")
                    .AppendLine(ex.Message);
            }
            catch (PolicyGrammarException ex)
            {
                sb.AppendLine("Validation Failed:")
                    .AppendLine("Error compiling policy\t")
                    .AppendLine(ex.Message);
            }
            catch (Exception ex)
            {
                sb.AppendLine("Validation Failed:")
                .AppendLine("Error compiling or proccessing policy\t" + ex.Message)
                .AppendLine(ex.StackTrace);
            }

            ValidationResults.Text = sb.ToString();
        }