Example #1
0
        private void DoWork()
        {
            if (aliasSet)
            {
                if (contextSet || thumbprintSet)
                {
                    throw new ParameterBindingException($"Either the {nameof(Alias).InQuotes()} parameter or the {nameof(Location).InQuotes()} and {nameof(Thumbprint).InQuotes()} parameters must be set.");
                }
            }
            else
            {
                if (!(contextSet && thumbprintSet))
                {
                    throw new ParameterBindingException($"Either the {nameof(Alias).InQuotes()} parameter or the {nameof(Location).InQuotes()} and {nameof(Thumbprint).InQuotes()} parameters must be set.");
                }
            }

            if (!aliasSet)
            {
                Alias = new X509Alias(string.Empty, Thumbprint, Context, false);
            }

            if (!System.IO.Path.GetExtension(Path).Matches(FileExtensions.Pfx))
            {
                path = $"{path}{FileExtensions.Pfx}";
            }

            if (File.Exists(Path))
            {
                if (Overwrite || Util.WarnConfirm($"The specified file {Path.InQuotes()} already exists. Do you wish to overwrite it?", Constants.Affirm))
                {
                    X509Utils.DeleteFile(Path, confirmDelete: true);
                }
                else
                {
                    throw new X509CryptoException($"The specified file {Path.InQuotes()} already exists.");
                }
            }

            var Password = Util.GetPassword(@"Enter a strong password (needed to unlock the .pfx file)", Constants.MinimumPasswordLength, true);

            X509CryptoAgent.ExportPFX(Alias.Thumbprint, Alias.Context, Path, Password.Plaintext());
            Util.ConsoleMessage($"Encryption certificate with thumbprint {Alias.Thumbprint} from the {Alias.Context.Name} {nameof(X509Context)} has been exported to the file {Path.InQuotes()}");
            Result = new FileInfo(Path);
        }
Example #2
0
        private static void ExportCert()
        {
            string       outfile    = string.Empty;
            string       thumbprint = string.Empty;
            string       aliasName  = string.Empty;
            SecureString Password   = null;
            X509Context  Context    = null;
            X509Alias    Alias      = null;

            try
            {
                if (!(SelectedMode.IsParameterDefined(Parameter.AliasExportCert.ID) ^ SelectedMode.IsParameterDefined(Parameter.ThumbprintToExport.ID)))
                {
                    throw new ArgumentException($"Either {Parameter.AliasExportCert.Name} or {Parameter.ThumbprintToExport.Name} must be defined, but not both");
                }

                outfile = SelectedMode.GetString(Parameter.OutExportCert.ID);
                try
                {
                    Path.GetFullPath(outfile);
                }
                catch
                {
                    throw new IOException($"Not a valid NTFS path: {outfile}");
                }
                if (!Path.GetExtension(outfile).Matches(FileExtensions.Pfx))
                {
                    outfile = $"{outfile}{FileExtensions.Pfx}";
                }
                if (File.Exists(outfile))
                {
                    if (Util.WarnConfirm($"The specified file {outfile} already exists. Do you wish to overwrite it?", Constants.Affirm))
                    {
                        X509Utils.DeleteFile(outfile, confirmDelete: true);
                    }
                    else
                    {
                        return;
                    }
                }

                Context = SelectedMode.GetContext(Parameter.Context.ID);

                if (SelectedMode.IsParameterDefined(Parameter.AliasExportCert.ID))
                {
                    aliasName  = SelectedMode.GetString(Parameter.AliasExportCert.ID);
                    Alias      = new X509Alias(aliasName, Context);
                    thumbprint = Alias.Thumbprint;
                }
                else
                {
                    thumbprint = SelectedMode.GetString(Parameter.ThumbprintToExport.ID);
                }

                Password = Util.GetPassword(@"Enter a strong password: "******"Encryption certificate with thumbprint {thumbprint} from the {Context.Name} {nameof(X509Context)} has been exported to the file {outfile.InQuotes()}");
            }
            catch (Exception ex)
            {
                throw new X509CryptoException(@"Unable to export the specified certificate and key pair", ex);
            }
        }