Esempio n. 1
0
        private string MakeCert()
        {
            string commonName = string.Empty;
            string thumbprint = string.Empty;

            if (context == X509Context.UserFull)
            {
                commonName = Environment.UserName;
            }
            else
            {
                commonName = Environment.MachineName;
            }

            context.MakeCertWorker(commonName, 2048, 10, out thumbprint);
            return(thumbprint);
        }
Esempio n. 2
0
        private static void MakeCert()
        {
            string      subject    = string.Empty;
            string      keySize    = string.Empty;
            string      thumbprint = string.Empty;
            string      aliasName  = string.Empty;
            int         keyLength  = Constants.DefaultKeyLength;
            int         yearsValid = Constants.DefaultYearsValid;
            X509Context Context    = null;

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

                if (SelectedMode.IsParameterDefined(Parameter.MakeCertSubject.ID))
                {
                    subject = SelectedMode.GetString(Parameter.MakeCertSubject.ID);
                }
                else
                {
                    subject = Context.Name.Matches(X509Context.UserReadOnly.Name) ? Environment.UserName : Environment.MachineName;
                }

                if (SelectedMode.IsParameterDefined(Parameter.MakeCertKeySize.ID))
                {
                    keySize = SelectedMode.GetString(Parameter.MakeCertKeySize.ID);
                    switch (keySize)
                    {
                    case KeySize.Small:
                        keyLength = KeyLength.Small;
                        break;

                    case KeySize.Medium:
                        keyLength = KeyLength.Medium;
                        break;

                    case KeySize.Large:
                        keyLength = KeyLength.Large;
                        break;

                    default:
                        throw new InvalidArgumentsException(Parameter.MakeCertKeySize.Name, keySize);
                    }
                }

                if (SelectedMode.IsParameterDefined(Parameter.MakeCertYearsValid.ID))
                {
                    yearsValid = SelectedMode.GetInt(Parameter.MakeCertYearsValid.ID);
                }

                Context.MakeCertWorker(subject, keyLength, yearsValid, out thumbprint);
                StringBuilder Expression = new StringBuilder($"Certificate was created and added 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)
            {
                ConsoleError(new X509CryptoException(@"An exception occurred attempting to generate a new encryption certificate", ex));
            }
        }