コード例 #1
0
        private static void ImportAlias()
        {
            try
            {
                string      aliasName         = Parameter.AliasToImport.IsDefined ? SelectedMode.GetString(Parameter.AliasToImport.ID) : string.Empty;
                string      inFile            = SelectedMode.GetString(Parameter.InImportAlias.ID);
                bool        overwriteExisting = SelectedMode.GetBool(Parameter.OverWriteExistingAlias.ID);
                X509Context Context           = SelectedMode.GetContext(Parameter.Context.ID);

                X509Alias AliasToImport = X509Alias.Import(inFile, Context, aliasName);
                if (!overwriteExisting && X509Alias.AliasExists(AliasToImport))
                {
                    throw new X509AliasAlreadyExistsException(AliasToImport);
                }
                AliasToImport.Commit();
                ConsoleMessage($"{nameof(X509Alias)} {AliasToImport.Name.InQuotes()} has been successfully imported into the {Context.Name} {nameof(X509Context)} from the file {inFile.InQuotes()}");

                if (!X509CryptoAgent.CertificateExists(AliasToImport))
                {
                    ConsoleWarning($"An encryption certificate with thumbprint {AliasToImport.Thumbprint.InQuotes()} could not be found in the {Context.Name} {nameof(X509Context)}. Ensure this certificate is installed on the system before using this alias.");
                }
            }
            catch (Exception ex)
            {
                if (ex is X509AliasAlreadyExistsException)
                {
                    throw;
                }
                else
                {
                    throw new X509CryptoException(@"Unable to import the specified alias", ex);
                }
            }
        }
コード例 #2
0
        private static void UpdateAlias()
        {
            try
            {
                string      aliasName     = SelectedMode.GetString(Parameter.AliasToUpdate.ID);
                string      newThumbprint = SelectedMode.GetString(Parameter.Thumbprint.ID);
                X509Context OldContext    = SelectedMode.GetContext(Parameter.OldContext.ID);
                X509Context NewContext    = SelectedMode.GetContext(Parameter.NewContext.ID, OldContext);

                if (!X509CryptoAgent.CertificateExists(newThumbprint, NewContext))
                {
                    throw new X509CryptoCertificateNotFoundException(newThumbprint, NewContext);
                }

                X509Alias Alias = new X509Alias(aliasName, OldContext);
                Alias.ReEncrypt(newThumbprint, NewContext);
                Alias.Commit();
                ConsoleMessage($"{nameof(X509Alias)} {aliasName} successfully updated. Now using encryption certificate with thumbprint {newThumbprint} from the {NewContext.Name} {nameof(X509Context)}");
            }
            catch (Exception ex)
            {
                if (ex is X509CryptoCertificateNotFoundException)
                {
                    throw;
                }
                else
                {
                    throw new X509CryptoException(@"Unable to update the specified alias", ex);
                }
            }
        }
コード例 #3
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;
        }
コード例 #4
0
        private void DoWork()
        {
            context = X509Context.Select(Location, true);
            if (string.IsNullOrEmpty(Thumbprint))
            {
                Thumbprint = MakeCert();
            }

            X509Alias Alias = new X509Alias(Name, Thumbprint, context, true);

            Alias.Commit();
            Result = Alias;
            Console.WriteLine($"New alias {Name.InQuotes()} committed to {context.Name.InQuotes()} {nameof(X509Context)}\r\nThumbprint: {Alias.Thumbprint}");
        }
コード例 #5
0
 private static void AddAlias()
 {
     try
     {
         string      thumbprint = SelectedMode.GetString(Parameter.Thumbprint.ID);
         string      aliasName  = SelectedMode.GetString(Parameter.AliasToAdd.ID);
         X509Context Context    = SelectedMode.GetContext(Parameter.Context.ID);
         X509Alias   NewAlias   = new X509Alias(aliasName, thumbprint, Context, AllowExistingAlias.No);
         NewAlias.Commit();
         ConsoleMessage($"New {nameof(X509Alias)} {aliasName.InQuotes()} was created in the {Context.Name} {nameof(X509Context)} using certificate with thumbprint {thumbprint.InQuotes()}");
     }
     catch (Exception ex)
     {
         throw new X509CryptoException(@"An exception occurred. The new alias could not be created.", ex);
     }
 }
コード例 #6
0
        private static bool CreateAlias(string aliasName, string thumbprint, X509Context Context)
        {
            X509Alias Alias = null;

            try
            {
                Alias = new X509Alias(aliasName, thumbprint, Context, true);
                Alias.Commit();
                return(true);
            }
            catch (X509AliasAlreadyExistsException)
            {
                if (Util.WarnConfirm($"{nameof(X509Alias)} {aliasName.InQuotes()} already exists. Do you wish to overwrite it?", Constants.Affirm))
                {
                    Alias = new X509Alias(aliasName, thumbprint, Context, false);
                    Alias.Commit();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }