void FillPrivKeyCertData(X509Certificate2 certificate)
        {
            if (this.CertHasPrivateKey)
            {
                try
                {
                    RSACryptoServiceProvider privateKey = PrivateKey(certificate);

                    this.privateKeyIsExportable = CertData.Exportable(privateKey);
                    this._privateKeyFileName    = CertData.PrivateKeyFilenameForCertificate(privateKey);
                }
                catch (System.Security.Cryptography.CryptographicException ce)
                {
                    this.privateKeyIsExportable = false;
                    this._privateKeyFileName    = ce.Message;
                }
            }
            else
            {
                this.privateKeyIsExportable = false;
                this._privateKeyFileName    = String.Empty;
            }

            foreach (X509Extension ext in certificate.Extensions)
            {
                X509BasicConstraintsExtension constraintExt = ext as X509BasicConstraintsExtension;
                if (constraintExt != null)
                {
                    this._extensionIsCACert = constraintExt.CertificateAuthority;
                    break;
                }
            }

            this._privKeyDataFilled = true;
        }
        private static void FillCache(
            StoreLocation storeLocation,
            string storeNameAsString,
            bool computeKeyIdentifiersImmediately,
            bool computePrivateKeyDataImmediately)
        {
            if (_cache == null)
            {
                _cache = new Dictionary <StoreLocation, Dictionary <string, Collection <CertData> > >();
            }

            if (!_cache.ContainsKey(storeLocation))
            {
                _cache[storeLocation] = new Dictionary <string, Collection <CertData> >();
            }
            Dictionary <string, Collection <CertData> > cacheLevel2 = _cache[storeLocation];

            if (!cacheLevel2.ContainsKey(storeNameAsString))
            {
                cacheLevel2[storeNameAsString] = new Collection <CertData>();
            }
            Collection <CertData> cacheLevel3 = cacheLevel2[storeNameAsString];

            X509Store store = new X509Store(storeNameAsString, storeLocation);

            try
            {
                store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);

                // Only do the expensive operation when the number of certs changes
                if (store.Certificates.Count != cacheLevel3.Count)
                {
                    cacheLevel3.Clear();

                    foreach (X509Certificate2 cert in store.Certificates)
                    {
                        cacheLevel3.Add(CertData.FromCert(
                                            storeLocation, storeNameAsString, cert,
                                            computeKeyIdentifiersImmediately,
                                            computePrivateKeyDataImmediately));

                        cert.Reset();
                    }
                }
            }
            catch (CryptographicException)
            {
            }
            finally
            {
                if (store != null)
                {
                    store.Close();
                }
            }
        }
 void FillKeyIndentifierCertData(X509Certificate2 certificate)
 {
     CertData.GetKeyIdentifiers(certificate,
                                out this._keyIdentifierCAPI,
                                out this._keyIdentifierThumbprintSHA1,
                                out this._keyIdentifierRFC3280,
                                out this._keyIdentifierIssuerSerial,
                                out this._onlyIssuerSerialIsDefined,
                                out this._PublicKey);
 }
        public override bool Equals(object obj)
        {
            CertData other = obj as CertData;

            if (other == null)
            {
                return(false);
            }
            return
                (this.CertSubject.Equals(other.CertSubject) &&
                 this.CertIssuer.Equals(other.CertIssuer) &&
                 this.CertThumbprint.Equals(other.CertThumbprint) &&
                 this.CertSerialNumber.Equals(other.CertSerialNumber) &&
                 this.StoreLocation.Equals(other.StoreLocation) &&
                 this.StoreNameAsString.Equals(other.StoreNameAsString) &&
                 this.CertNotBefore.Equals(other.CertNotBefore) &&
                 this.CertNotAfter.Equals(other.CertNotAfter) &&
                 this.CertHasPrivateKey.Equals(other.CertHasPrivateKey));
        }
        /// <summary>
        /// Instantiates a CertData object from a certificate.
        /// </summary>
        /// <param storeName="storeLocation">The store storeLocation.</param>
        /// <param storeName="storeName">Name of the store.</param>
        /// <param storeName="certificate">The certificate.</param>
        /// <param storeName="computeKeyIdentifiersImmediately">if set to <see langword="true"/>, it computes the key identifier values immediately.</param>
        /// <param storeName="computePrivateKeyDataImmediately">if set to <see langword="true"/>, it computes the data related to the private key data immediately.</param>
        /// <returns></returns>
        internal static CertData FromCert(
            StoreLocation storeLocation, string storeName,
            X509Certificate2 certificate,
            bool computeKeyIdentifiersImmediately,
            bool computePrivateKeyDataImmediately)
        {
            CertData certData = new CertData();

            certData.FillCheapCertData(storeLocation, storeName, certificate);

            if (computeKeyIdentifiersImmediately)
            {
                certData.FillKeyIndentifierCertData(certificate);
            }
            if (computePrivateKeyDataImmediately)
            {
                certData.FillPrivKeyCertData(certificate);
            }

            return(certData);
        }