예제 #1
0
        /// <summary>
        /// Load X509Certificate2 certificate from X509Store.
        /// </summary>
        /// <param name="arguments"></param>
        /// <returns></returns>
        private static X509Certificate2 FindCertificateInX509Store(CertificateArguments arguments)
        {
            X509Certificate2 result = null;
            X509Store        store  = null;

            try
            {
                store = new X509Store(arguments.CertificateStoreName, arguments.CertificateStoreLocation);
                store.Open(OpenFlags.MaxAllowed);
                X509Certificate2Collection certs = store.Certificates;

                foreach (X509Certificate cert in certs)
                {
                    if (cert.Subject == arguments.CertificateStoreSubject)  // String.equals ?
                    {
                        result = new X509Certificate2(cert);
                        break;
                    }
                }
            }
            finally
            {
                store.Close();
            }
            return(result);
        }
예제 #2
0
        /// <summary>
        /// Load certificate by specified arguments.
        /// </summary>
        /// <param name="arguments"></param>
        /// <returns></returns>
        public static X509Certificate2 LoadCertificate(CertificateArguments arguments)
        {
            X509Certificate2 returnX509 = null;

            try
            {
                if (arguments.Validate())
                {
                    if (!String.IsNullOrEmpty(arguments.CertificateStoreSubject))
                    {
                        returnX509 = FindCertificateInX509Store(arguments);
                    }
                    else if (!String.IsNullOrEmpty(arguments.CertificateFilePath))
                    {
                        returnX509 = FindCertificateInFilePath(arguments);
                    }
                }
                if (returnX509 == null)
                {
                    throw new Exception(String.Format("Failed to load certificate with arguments {0} ", arguments.ToString()));
                }
            }
            catch (Exception ex)
            {
                Tracing.ErrorSecurity("Failed to load certificate. {0}", ex.ToString());
                throw;
            }

            return(returnX509);
        }
예제 #3
0
        /// <summary>
        /// Load X509Certificate2 certificate from file path.
        /// </summary>
        /// <param name="arguments"></param>
        /// <returns></returns>
        private static X509Certificate2 FindCertificateInFilePath(CertificateArguments arguments)
        {
            X509Certificate2 returnX509 = null;

            try
            {
                returnX509 = new X509Certificate2(arguments.CertificateFilePath,
                                                  arguments.CertificatePassword,
                                                  X509KeyStorageFlags.Exportable);
            }
            catch (Exception ex)
            {
                Tracing.ErrorSecurity(String.Format("Failed to obtain certificate. {0}", ex));
            }

            return(returnX509);
        }
        public static void Run(CommandArguments commandArguments, CertificateArguments encyptCertArgs, CertificateArguments signCertArgs)
        {
            X509Certificate2 encyptCert = CmsCryptoUtilities.LoadCertificate(encyptCertArgs);
            X509Certificate2 signCert   = CmsCryptoUtilities.LoadCertificate(signCertArgs);

            if (commandArguments.Action == ActionTypes.EncryptSign)
            {
                CmsCryptoUtilities.PerformEncryptAndSign(commandArguments, encyptCert, signCert);
            }
            else if (commandArguments.Action == ActionTypes.VerifyDescrypt)
            {
                CmsCryptoUtilities.PerformDecryptAndVerifySignature(commandArguments, encyptCert);
            }
            else
            {
                throw new ArgumentNullException("Please specify command");
            }
        }
        static void Main(string[] args)
        {
            CommandArguments commandArguments = Parse();

            if (commandArguments != null)
            {
                CertificateArguments encyptCertArgs = new CertificateArguments()
                {
                    CertificateStoreSubject = encryptCertificateSubject,
                    CertificatePassword     = encryptCertificatePassword
                };
                CertificateArguments signCertArgs = new CertificateArguments()
                {
                    CertificateStoreSubject = signCertificateSubject,
                    CertificatePassword     = signCertificatePassword
                };
                Run(commandArguments, encyptCertArgs, signCertArgs);
            }
            Console.ReadLine();
        }