Exemplo n.º 1
0
        private static void InstallCert()
        {
            string      infile     = string.Empty;
            string      thumbprint = string.Empty;
            string      aliasName  = string.Empty;
            X509Context Context    = null;

            try
            {
                infile  = SelectedMode.GetString(Parameter.InInstallCert.ID);
                Context = SelectedMode.GetContext(Parameter.InstallCertContext.ID);
                SecureString PfxPassword = Util.GetPassword($"Enter the password to unlock {Path.GetFileName(infile).InQuotes()}", 0);
                thumbprint = X509Utils.InstallCert(infile, PfxPassword, Context);
                StringBuilder Expression = new StringBuilder($"Added encryption certificate to the {Context.Name} {nameof(X509Context)}. \r\nCertificate Thumbprint: {thumbprint}");

                if (SelectedMode.IsParameterDefined(Parameter.AliasToInstall.ID))
                {
                    aliasName = SelectedMode.GetString(Parameter.AliasToInstall.ID);
                    if (CreateAlias(aliasName, thumbprint, Context))
                    {
                        Expression.Append($"\r\n             {nameof(X509Alias)}: {aliasName}");
                    }
                }
                ConsoleMessage(Expression.ToString());
            }
            catch (Exception ex)
            {
                throw new X509CryptoException(@"Unable to install the specified certificate", ex);
            }
        }
Exemplo n.º 2
0
        private void DoWork()
        {
            Console.WriteLine($"Path: {Path}");
            var Context = X509Context.Select(Location, true);
            var Alias   = Context.GetAliases(true).FirstOrDefault(p => p.Name.Matches(Name));

            if (null != Alias)
            {
                if (!Overwrite || !Util.WarnConfirm($"An existing {nameof(X509Alias)} with the name {Name.InQuotes()} exists in the {Context.Name} {nameof(X509Context)}. OK to overwrite?", Constants.Affirm))
                {
                    throw new X509CryptoException($"Could not import the certificate. An {nameof(X509Alias)} with the name {Name.InQuotes()} exists in the {Context.Name} {nameof(X509Context)}");
                }
            }

            var           PfxPassword = Util.GetPassword($"Enter the password to unlock {System.IO.Path.GetFileName(Path).InQuotes()}", 0);
            var           thumbprint  = X509Utils.InstallCert(Path, PfxPassword, Context);
            StringBuilder Expression  = new StringBuilder($"Added encryption certificate to the {Context.Name} {nameof(X509Context)}. \r\nCertificate Thumbprint: {thumbprint}");

            if (null != Alias && Alias.HasCert(Context))
            {
                Alias.ReEncrypt(thumbprint, Context);
                Expression.AppendLine($"\r\nAll secrets contained in the existing {nameof(X509Alias)} {Alias.Name.InQuotes()} have been re-encrypted using the new certificate.");
            }
            else
            {
                Alias = new X509Alias(Name, thumbprint, Context, false);
                Alias.Commit();
                Expression.Append($"\r\n             {nameof(X509Alias)}: {Name}");
            }

            Util.ConsoleMessage(Expression.ToString());
            Result = Alias;
        }