private void validateXmlSignature(string filePath)
        {
            var policy        = XmlPolicySpec.GetXmlDSigBasic(App.GetTrustArbitrator());
            var xmlSigLocator = new XmlSignatureLocator(File.ReadAllBytes(filePath));

            Signers.Clear();
            foreach (var signature in xmlSigLocator.GetSignatures())
            {
                var vr = signature.Validate(policy);
                Signers.Add(new SignerItem(getSignerDescription(signature, vr), vr));
            }
        }
        private void validatePdfSignature(string filePath)
        {
            var policy         = PadesPoliciesForValidation.GetPadesBasic(App.GetTrustArbitrator());
            var padesSignature = PadesSignature.Open(filePath);

            Signers.Clear();
            foreach (var signer in padesSignature.Signers)
            {
                var vr = padesSignature.ValidateSignature(signer, policy);
                Signers.Add(new SignerItem(getSignerDescription(signer, vr), vr));
            }
        }
 public void Validate()
 {
     try {
         var cert = PKCertificate.Decode(System.IO.File.ReadAllBytes(CertificatePath));
         var vr   = cert.Validate(App.GetTrustArbitrator());
         var msg  = (vr.IsValid ? "Certificate is valid!" : "Certificate is not valid!");
         new ValidationResultsDialog(msg, vr).ShowDialog();
     } catch (Exception ex) {
         logger.Error(ex, "Error validating certificate");
         MessageBox.Show(ex.Message);
     }
 }
        public void Sign()
        {
            if (string.IsNullOrEmpty(PdfPath))
            {
                MessageBox.Show("Please choose a PDF file sign");
                return;
            }
            if (!File.Exists(PdfPath))
            {
                MessageBox.Show("File not found: " + PdfPath);
                return;
            }
            if (SelectedCertificate == null)
            {
                MessageBox.Show("Please choose a certificate to sign the PDF");
                return;
            }

            try {
                var signer = new PadesSigner();                                                                              // Instantiate the PDF signer
                signer.SetSigningCertificate(selectedCertificate.CertificateWithKey);                                        // certificate with private key associated
                signer.SetPdfToSign(PdfPath);                                                                                // PDF file path
                signer.SetPolicy(PadesPoliciesForGeneration.GetPadesBasic(App.GetTrustArbitrator()));                        // Basic signature policy with the selected trust arbitrator
                signer.SetVisualRepresentation(getVisualRepresentation(selectedCertificate.CertificateWithKey.Certificate)); // Signature visual representation
                signer.ComputeSignature();                                                                                   // computes the signature
                byte[] signedPdf = signer.GetPadesSignature();                                                               // return the signed PDF bytes

                // saving signed PDF file
                var savePath = getSaveFilePath();
                if (!string.IsNullOrEmpty(savePath))
                {
                    File.WriteAllBytes(savePath, signedPdf);
                    Process.Start(savePath);
                }
            } catch (ValidationException ex) {
                new ValidationResultsDialog("Validation failed", ex.ValidationResults).ShowDialog();
            } catch (Exception ex) {
                logger.Error(ex, "Error while signing PDF");
                MessageBox.Show(ex.Message);
            }
        }
        private void validateCadesSignature(string filePath)
        {
            var policy         = CadesPoliciesForValidation.GetCadesBasic(App.GetTrustArbitrator());
            var cadesSignature = CadesSignature.Open(filePath);

            if (!cadesSignature.HasEncapsulatedContent)
            {
                MessageBox.Show("This CAdES signature does not have an encapsulated content (\"detached signature\"). Please provide the data file to continue with the validation.", "Data file needed");
                var dataFileDialog = new OpenFileDialog()
                {
                };
                if (dataFileDialog.ShowDialog() != true)
                {
                    return;
                }
                cadesSignature.SetExternalData(File.ReadAllBytes(dataFileDialog.FileName));
            }
            Signers.Clear();
            foreach (var signer in cadesSignature.Signers)
            {
                var vr = cadesSignature.ValidateSignature(signer, policy);
                Signers.Add(new SignerItem(getSignerDescription(signer, vr), vr));
            }
        }
Esempio n. 6
0
        private async Task <bool> sign(TaskProgressDialog progressDialog)
        {
            try {
                var signer = new CadesSigner();

                if (CoSign)
                {
                    progressDialog.Message = "Reading existing CAdES signature ...";
                }
                else
                {
                    progressDialog.Message = "Reading file ...";
                }
                await Task.Delay(TimeSpan.FromMilliseconds(100));

                if (CoSign)
                {
                    var cmsBytes = await readAllBytesAsync(CmsPath, progressDialog.CancellationToken);

                    signer.SetSignatureToCoSign(cmsBytes);
                }
                else
                {
                    var fileBytes = await readAllBytesAsync(FilePath, progressDialog.CancellationToken);

                    signer.SetDataToSign(fileBytes);
                }

                if (progressDialog.CancellationToken.IsCancellationRequested)
                {
                    return(false);
                }

                progressDialog.Progress = 33;
                progressDialog.Message  = "Signing ...";
                await Task.Delay(TimeSpan.FromMilliseconds(100));

                signer.SetSigningCertificate(SelectedCertificate.CertificateWithKey);
                signer.SetPolicy(CadesPoliciesForGeneration.GetCadesBasic(App.GetTrustArbitrator()));
                signer.SetEncapsulatedContent(this.EncapsulateContent);
                signer.ComputeSignature();
                var signature = signer.GetSignature();

                if (progressDialog.CancellationToken.IsCancellationRequested)
                {
                    return(false);
                }

                progressDialog.Progress = 66;
                progressDialog.Message  = "Saving signature ...";
                await Task.Delay(TimeSpan.FromMilliseconds(100));

                var saveFileDialog = new SaveFileDialog()
                {
                    Filter      = "CAdES signature files (.p7s)|*.p7s",
                    FilterIndex = 1,
                    FileName    = CoSign ? string.Format("{0}-{1:yyyy-MM-dd-HHmmss}.p7s", Path.GetFileNameWithoutExtension(CmsPath), DateTime.Now) : FilePath + ".p7s"
                };
                if (saveFileDialog.ShowDialog() != true)
                {
                    return(false);
                }

                var outFilePath = saveFileDialog.FileName;
                await writeAllBytesAsync(outFilePath, signature, progressDialog.CancellationToken);

                if (progressDialog.CancellationToken.IsCancellationRequested)
                {
                    return(false);
                }

                progressDialog.Progress = 100;
                progressDialog.Message  = "Completed!";
                return(true);
            } catch (ValidationException ex) {
                new ValidationResultsDialog("Validation failed", ex.ValidationResults).ShowDialog();
                return(false);
            } catch (Exception ex) {
                logger.Error(ex, "Error while performing CAdES signature");
                MessageBox.Show(ex.Message);
                return(false);
            }
        }
Esempio n. 7
0
        public void ValidateAttributeCert()
        {
            if (!checkLicenseLoaded())
            {
                return;
            }

            try {
                var certFileDialog = new OpenFileDialog()
                {
                    DefaultExt = ".ac",
                    Filter     = "X.509 attribute certificate (.ac)|*.ac"
                };
                if (certFileDialog.ShowDialog() != true)
                {
                    return;
                }

                // Read and decode the attribute certificate
                var certContent = File.ReadAllBytes(certFileDialog.FileName);
                var cert        = AttributeCertificate.Decode(certContent);

                // If the certificate is issued without a link to its issuer (AIA extension), the validation will fail because the issuer will not be found. In this
                // case, have to provide the issuer certificate when decoding the attribute certificate.
                if (cert.IssuerNotFound)
                {
                    MessageBox.Show("Could not find the issuer of the certificate. This usually happens with certificates that do not have a valid Authority Information Access (AIA) extension.\n\nTo continue, you will need to provide the .cer file of the issuer.", "Issuer not found");
                    var issuerFileDialog = new OpenFileDialog()
                    {
                        DefaultExt = ".cer",
                        Filter     = "X.509 certificate|*.cer;*.crt"
                    };
                    if (issuerFileDialog.ShowDialog() != true)
                    {
                        return;
                    }

                    // Read and decode the issuer certificate
                    var issuerContent = File.ReadAllBytes(issuerFileDialog.FileName);
                    var issuerCert    = PKCertificate.Decode(issuerContent);

                    // Re-open the attribute certificate providing the issuer certificate
                    cert = AttributeCertificate.Decode(certContent, new MemoryCertificateStore(new[] { issuerCert }));
                }

                CieStudentIdentity cieStudentIdentity = null;
                if (cert.Attributes.GetOids().Contains(CieStudentIdentity.Oid))
                {
                    cieStudentIdentity = CieStudentIdentity.Decode(cert.Attributes);
                }

                CieStudentData cieStudentData = null;
                if (cert.Attributes.GetOids().Contains(CieStudentData.Oid))
                {
                    cieStudentData = CieStudentData.Decode(cert.Attributes);
                }

                // Validate the certificate
                var vr = cert.Validate(App.GetTrustArbitrator());

                // Show the validation results
                new ValidationResultsDialog("Attribute certificate validation results", vr).ShowDialog();
            } catch (Exception ex) {
                MessageBox.Show(ex.ToString(), "An error has occurred");
            }
        }