Exemplo n.º 1
0
        private async Task <int> issueBatchAsync(string folderPath, TaskProgressDialog progressDialog)
        {
            var issuedCount = 0;

            for (int i = 0; i < BatchSize && !progressDialog.CancellationToken.IsCancellationRequested; i++)
            {
                // Update dialog message
                progressDialog.Message = string.Format("Issuing certificate {0}/{1} ...", i + 1, BatchSize);

                // Give control back to UI briefly for message to be updated
                await Task.Delay(TimeSpan.FromMilliseconds(50));

                // Derive name and filename
                var name     = string.Format("{0} {1:D2}", Name, i + 1);
                var fileName = string.Format("{0:D2}.ac", i + 1);

                // Issue certificate
                var certContent = issue(name);

                // Save to file
                File.WriteAllBytes(Path.Combine(folderPath, fileName), certContent);

                // Update progress bar
                ++issuedCount;
                progressDialog.Progress = 100.0 * issuedCount / BatchSize;
            }

            return(issuedCount);
        }
        public static void Run(Func <TaskProgressDialog, Task> taskInvoker, Window owner = null)
        {
            var progressDialog = new TaskProgressDialog(taskInvoker, owner);

            progressDialog.ShowDialog();
            if (progressDialog.Exception != null)
            {
                throw progressDialog.Exception;
            }
        }
Exemplo n.º 3
0
        public void Issue()
        {
            if (SelectedCertificate == null)
            {
                MessageBox.Show("Please choose a certificate");
                return;
            }
            if (string.IsNullOrWhiteSpace(Name))
            {
                MessageBox.Show("Please fill the name");
                return;
            }

            try {
                if (Batch)
                {
                    var folderDialog = new System.Windows.Forms.FolderBrowserDialog()
                    {
                        Description = "Choose the folder on which to save the issued certificates:"
                    };
                    if (folderDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    {
                        return;
                    }
                    var folderPath = folderDialog.SelectedPath;

                    var sw = System.Diagnostics.Stopwatch.StartNew();

                    // Run batch in background
                    var issuedCount = TaskProgressDialog.Run(d => issueBatchAsync(folderPath, d), this.owner);

                    MessageBox.Show(string.Format("Certificates saved on {0}\n\nCertificates issued: {1}\nElapsed time: {2:F1} s ({3:F1}/min)", folderPath, issuedCount, sw.Elapsed.TotalSeconds, issuedCount / sw.Elapsed.TotalMinutes), "Batch completed");
                }
                else
                {
                    var certContent = issue(Name);

                    var saveFileDialog = new SaveFileDialog()
                    {
                        FileName = $"cert.ac",
                    };

                    if (saveFileDialog.ShowDialog() != true)
                    {
                        return;
                    }

                    File.WriteAllBytes(saveFileDialog.FileName, certContent);
                    MessageBox.Show($"Certificate saved on {saveFileDialog.FileName}");
                }
            } catch (Exception ex) {
                MessageBox.Show(ex.ToString(), "An error has occurred");
            }
        }
        public static T Run <T>(Func <TaskProgressDialog, Task <T> > taskInvoker, Window owner = null)
        {
            var progressDialog = new TaskProgressDialog(taskInvoker, owner);

            progressDialog.ShowDialog();
            if (progressDialog.Exception != null)
            {
                throw progressDialog.Exception;
            }
            return(((Task <T>)progressDialog.Task).Result);
        }
Exemplo n.º 5
0
        public void Sign()
        {
            if (!checkParameters())
            {
                return;
            }

            try {
                var success = TaskProgressDialog.Run(d => sign(d), owner);
                if (success)
                {
                    MessageBox.Show("Process completed successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            } catch (TaskCanceledException) {
                MessageBox.Show("Operation cancelled", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }
Exemplo 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);
            }
        }