CreateStore() public static method

Returns an object that can be used to access the store.
public static CreateStore ( string storeType ) : ICertificateStore
storeType string
return ICertificateStore
        /// <summary>
        /// Returns an object that can be used to access the store containing the certificate.
        /// </summary>
        /// <returns>An instance of the <see cref="ICertificateStore"/> poined out by the current value of </returns>
        public ICertificateStore OpenStore()
        {
            ICertificateStore store = CertificateStoreIdentifier.CreateStore(this.StoreType);

            store.Open(this.StorePath);
            return(store);
        }
Example #2
0
        /// <summary>
        /// Opens the store.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>The store.</returns>
        public static ICertificateStore OpenStore(string path)
        {
            ICertificateStore store = CertificateStoreIdentifier.CreateStore(CertificateStoreIdentifier.DetermineStoreType(path));

            store.Open(path);
            return(store);
        }
        /// <summary>
        /// Finds a certificate in a store.
        /// </summary>
        /// <param name="needPrivateKey">if set to <c>true</c> the returned certificate must contain the private key.</param>
        /// <returns>An instance of the <see cref="X509Certificate2"/> that is emebeded by this instance or find it in
        /// the selected strore pointed out by the <see cref="StorePath"/> using selected <see cref="SubjectName"/>.</returns>
        public async Task <X509Certificate2> Find(bool needPrivateKey)
        {
            X509Certificate2 certificate = null;

            // check if the entire certificate has been specified.
            if (m_certificate != null && (!needPrivateKey || m_certificate.HasPrivateKey))
            {
                certificate = m_certificate;
            }
            else
            {
                // open store.
                using (ICertificateStore store = CertificateStoreIdentifier.CreateStore(StoreType))
                {
                    store.Open(StorePath);

                    X509Certificate2Collection collection = await store.Enumerate();

                    certificate = Find(collection, m_thumbprint, m_subjectName, needPrivateKey);

                    if (certificate != null)
                    {
                        m_certificate = certificate;
                    }
                }
            }

            // use the single instance in the certificate cache.
            if (needPrivateKey)
            {
                certificate = m_certificate = CertificateFactory.Load(certificate, true);
            }

            return(certificate);
        }
        /// <summary>
        /// Returns an object that can be used to access the store containing the certificate.
        /// </summary>
        /// <returns>An instance of the <see cref="ICertificateStore"/> poined out by the current value of </returns>
        public ICertificateStore OpenStore()
        {
            ICertificateStore store = CertificateStoreIdentifier.CreateStore(this.StoreType);
            Task t = Task.Run(() => store.Open(this.StorePath));

            t.Wait();
            return(store);
        }
 /// <summary>
 /// Add to certificate store
 /// </summary>
 /// <param name="identifier"></param>
 /// <param name="certificate"></param>
 /// <param name="noCopy"></param>
 /// <returns></returns>
 public static void AddToStore(this CertificateIdentifier identifier,
                               X509Certificate2 certificate, bool noCopy = false)
 {
     if (certificate == null)
     {
         throw new ArgumentNullException(nameof(certificate));
     }
     using (var store = CertificateStoreIdentifier.CreateStore(identifier.StoreType)) {
         store.Open(identifier.StorePath);
         store.Add(certificate.YieldReturn(), noCopy);
     }
 }
 /// <summary>
 /// Remove from certificate store
 /// </summary>
 /// <param name="identifier"></param>
 /// <param name="certificate"></param>
 public static void RemoveFromStore(this CertificateIdentifier identifier,
                                    X509Certificate2 certificate)
 {
     if (certificate == null)
     {
         throw new ArgumentNullException(nameof(certificate));
     }
     using (var store = CertificateStoreIdentifier.CreateStore(identifier.StoreType)) {
         store.Open(identifier.StorePath);
         store.Remove(certificate.YieldReturn());
     }
 }
Example #7
0
        /// <summary>
        /// Loads the private key for the certificate with an optional password.
        /// </summary>
        public async Task <X509Certificate2> LoadPrivateKeyEx(ICertificatePasswordProvider passwordProvider)
        {
            if (this.StoreType != CertificateStoreType.X509Store)
            {
                using (ICertificateStore store = CertificateStoreIdentifier.CreateStore(StoreType))
                {
                    if (store.SupportsLoadPrivateKey)
                    {
                        store.Open(this.StorePath, false);
                        string password = passwordProvider?.GetPassword(this);
                        m_certificate = await store.LoadPrivateKey(this.Thumbprint, this.SubjectName, password).ConfigureAwait(false);

                        return(m_certificate);
                    }
                }
            }
            return(await Find(true).ConfigureAwait(false));
        }
Example #8
0
        /// <summary>
        /// Gets the private key file path.
        /// </summary>
        public string GetPrivateKeyFilePath()
        {
            X509Certificate2 certificate = Find(false);

            if (certificate == null)
            {
                return(null);
            }

            ICertificateStore store = CertificateStoreIdentifier.CreateStore(this.StoreType);

            try {
                store.Open(this.StorePath);
                return(store.GetPrivateKeyFilePath(certificate.Thumbprint));
            } finally {
                store.Close();
            }
        }
Example #9
0
        /// <summary>
        /// Finds a certificate in a store.
        /// </summary>
        /// <param name="needPrivateKey">if set to <c>true</c> the returned certificate must contain the private key.</param>
        /// <returns>An instance of the <see cref="X509Certificate2"/> that is embedded by this instance or find it in
        /// the selected store pointed out by the <see cref="StorePath"/> using selected <see cref="SubjectName"/>.</returns>
        public async Task <X509Certificate2> Find(bool needPrivateKey)
        {
            X509Certificate2 certificate = null;

            // check if the entire certificate has been specified.
            if (m_certificate != null && (!needPrivateKey || m_certificate.HasPrivateKey))
            {
                certificate = m_certificate;
            }
            else
            {
                // open store.
                using (ICertificateStore store = CertificateStoreIdentifier.CreateStore(StoreType))
                {
                    store.Open(StorePath, false);

                    X509Certificate2Collection collection = await store.Enumerate().ConfigureAwait(false);

                    certificate = Find(collection, m_thumbprint, m_subjectName, needPrivateKey);

                    if (certificate != null)
                    {
                        if (needPrivateKey && store.SupportsLoadPrivateKey)
                        {
                            var message = new StringBuilder();
                            message.AppendLine("Loaded a certificate with private key from store {0}.");
                            message.AppendLine("Ensure to call LoadPrivateKeyEx with password provider before calling Find(true).");
                            Utils.LogWarning(message.ToString(), StoreType);
                        }

                        m_certificate = certificate;
                    }
                }
            }

            // use the single instance in the certificate cache.
            if (needPrivateKey)
            {
                certificate = m_certificate = CertificateFactory.Load(certificate, true);
            }

            return(certificate);
        }