コード例 #1
0
ファイル: x509ui.cs プロジェクト: dox0/DotNet471RS3
        private static unsafe SafeCertStoreHandle SelectFromStore(SafeCertStoreHandle safeSourceStoreHandle, string title, string message, X509SelectionFlag selectionFlags, IntPtr hwndParent)
        {
            int dwErrorCode = CAPI.ERROR_SUCCESS;

            // First, create a memory store
            SafeCertStoreHandle safeCertStoreHandle = CAPI.CertOpenStore((IntPtr)CAPI.CERT_STORE_PROV_MEMORY,
                                                                         CAPI.X509_ASN_ENCODING | CAPI.PKCS_7_ASN_ENCODING,
                                                                         IntPtr.Zero,
                                                                         0,
                                                                         null);

            if (safeCertStoreHandle == null || safeCertStoreHandle.IsInvalid)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            CAPI.CRYPTUI_SELECTCERTIFICATE_STRUCTW csc = new CAPI.CRYPTUI_SELECTCERTIFICATE_STRUCTW();
            // Older versions of CRYPTUI do not check the size correctly,
            // so always force it to the oldest version of the structure.
            csc.dwSize           = (uint)Marshal.OffsetOf(typeof(CAPI.CRYPTUI_SELECTCERTIFICATE_STRUCTW), "hSelectedCertStore");
            csc.hwndParent       = hwndParent;
            csc.dwFlags          = (uint)selectionFlags;
            csc.szTitle          = title;
            csc.dwDontUseColumn  = 0;
            csc.szDisplayString  = message;
            csc.pFilterCallback  = IntPtr.Zero;
            csc.pDisplayCallback = IntPtr.Zero;
            csc.pvCallbackData   = IntPtr.Zero;
            csc.cDisplayStores   = 1;
            IntPtr hSourceCertStore = safeSourceStoreHandle.DangerousGetHandle();

            csc.rghDisplayStores   = new IntPtr(&hSourceCertStore);
            csc.cStores            = 0;
            csc.rghStores          = IntPtr.Zero;
            csc.cPropSheetPages    = 0;
            csc.rgPropSheetPages   = IntPtr.Zero;
            csc.hSelectedCertStore = safeCertStoreHandle.DangerousGetHandle();

            SafeCertContextHandle safeCertContextHandle = CAPI.CryptUIDlgSelectCertificateW(csc);

            if (safeCertContextHandle != null && !safeCertContextHandle.IsInvalid)
            {
                // Single select, so add it to our hCertStore
                SafeCertContextHandle ppStoreContext = SafeCertContextHandle.InvalidHandle;
                if (!CAPI.CertAddCertificateContextToStore(safeCertStoreHandle,
                                                           safeCertContextHandle,
                                                           CAPI.CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES,
                                                           ppStoreContext))
                {
                    dwErrorCode = Marshal.GetLastWin32Error();
                }
            }

            if (dwErrorCode != CAPI.ERROR_SUCCESS)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            return(safeCertStoreHandle);
        }
コード例 #2
0
        internal static Cryptography.SafeCertStoreHandle ExportToMemoryStore(X509Certificate2Collection collection)
        {
            //
            // We need to Assert all StorePermission flags since this is a memory store and we want
            // semi-trusted code to be able to export certificates to a memory store.
            //

#if !FEATURE_CORESYSTEM
            StorePermission sp = new StorePermission(StorePermissionFlags.AllFlags);
            sp.Assert();
#endif

            Cryptography.SafeCertStoreHandle safeCertStoreHandle = Cryptography.SafeCertStoreHandle.InvalidHandle;

            // we always want to use CERT_STORE_ENUM_ARCHIVED_FLAG since we want to preserve the collection in this operation.
            // By default, Archived certificates will not be included.

            safeCertStoreHandle = CAPI.CertOpenStore(new IntPtr(CAPI.CERT_STORE_PROV_MEMORY),
                                                     CAPI.X509_ASN_ENCODING | CAPI.PKCS_7_ASN_ENCODING,
                                                     IntPtr.Zero,
                                                     CAPI.CERT_STORE_ENUM_ARCHIVED_FLAG | CAPI.CERT_STORE_CREATE_NEW_FLAG,
                                                     null);

            if (safeCertStoreHandle == null || safeCertStoreHandle.IsInvalid)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            //
            // We use CertAddCertificateLinkToStore to keep a link to the original store, so any property changes get
            // applied to the original store. This has a limit of 99 links per cert context however.
            //

            foreach (X509Certificate2 x509 in collection)
            {
                if (!CAPI.CertAddCertificateLinkToStore(safeCertStoreHandle,
                                                        x509.CertContext,
                                                        CAPI.CERT_STORE_ADD_ALWAYS,
                                                        Cryptography.SafeCertContextHandle.InvalidHandle))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
            }

            return(safeCertStoreHandle);
        }
コード例 #3
0
        public void Open(OpenFlags openFlags)
        {
            DiagnosticUtility.DebugAssert(this.certStoreHandle.IsInvalid, "");

            uint dwOpenFlags = MapX509StoreFlags(this.storeLocation, openFlags);
            SafeCertStoreHandle certStoreHandle = CAPI.CertOpenStore(new IntPtr(CAPI.CERT_STORE_PROV_SYSTEM),
                                                                     CAPI.X509_ASN_ENCODING | CAPI.PKCS_7_ASN_ENCODING,
                                                                     IntPtr.Zero,
                                                                     dwOpenFlags,
                                                                     this.storeName);

            if (certStoreHandle == null || certStoreHandle.IsInvalid)
            {
                int error = Marshal.GetLastWin32Error();
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(error));
            }
            this.certStoreHandle = certStoreHandle;
        }
コード例 #4
0
        internal static SafeCertStoreHandle ExportToMemoryStore(X509Certificate2Collection collection)
        {
            new StorePermission(StorePermissionFlags.CreateStore | StorePermissionFlags.DeleteStore | StorePermissionFlags.EnumerateStores | StorePermissionFlags.OpenStore | StorePermissionFlags.AddToStore | StorePermissionFlags.RemoveFromStore | StorePermissionFlags.EnumerateCertificates).Assert();
            SafeCertStoreHandle invalidHandle = SafeCertStoreHandle.InvalidHandle;
            SafeCertStoreHandle hCertStore    = CAPI.CertOpenStore(new IntPtr(2L), 65537U, IntPtr.Zero, 8704U, (string)null);

            if (hCertStore == null || hCertStore.IsInvalid)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            foreach (X509Certificate2 certificate in collection)
            {
                if (!CAPI.CertAddCertificateLinkToStore(hCertStore, X509Utils.GetCertContext(certificate), 4U, System.Security.Cryptography.SafeCertContextHandle.InvalidHandle))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
            }
            return(hCertStore);
        }
コード例 #5
0
        public void Open(OpenFlags flags)
        {
            if ((this.m_location != StoreLocation.CurrentUser) && (this.m_location != StoreLocation.LocalMachine))
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, SR.GetString("Arg_EnumIllegalVal"), new object[] { "m_location" }));
            }
            uint dwFlags = System.Security.Cryptography.X509Certificates.X509Utils.MapX509StoreFlags(this.m_location, flags);

            if (!this.m_safeCertStoreHandle.IsInvalid)
            {
                this.m_safeCertStoreHandle.Dispose();
            }
            this.m_safeCertStoreHandle = CAPI.CertOpenStore(new IntPtr(10L), 0x10001, IntPtr.Zero, dwFlags, this.m_storeName);
            if ((this.m_safeCertStoreHandle == null) || this.m_safeCertStoreHandle.IsInvalid)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            CAPISafe.CertControlStore(this.m_safeCertStoreHandle, 0, 4, IntPtr.Zero);
        }
コード例 #6
0
        internal static System.Security.Cryptography.SafeCertStoreHandle ExportToMemoryStore(X509Certificate2Collection collection)
        {
            new StorePermission(StorePermissionFlags.AllFlags).Assert();
            System.Security.Cryptography.SafeCertStoreHandle invalidHandle = System.Security.Cryptography.SafeCertStoreHandle.InvalidHandle;
            invalidHandle = CAPI.CertOpenStore(new IntPtr(2L), 0x10001, IntPtr.Zero, 0x2200, null);
            if ((invalidHandle == null) || invalidHandle.IsInvalid)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            X509Certificate2Enumerator enumerator = collection.GetEnumerator();

            while (enumerator.MoveNext())
            {
                X509Certificate2 current = enumerator.Current;
                if (!CAPI.CertAddCertificateLinkToStore(invalidHandle, current.CertContext, 4, System.Security.Cryptography.SafeCertContextHandle.InvalidHandle))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
            }
            return(invalidHandle);
        }
コード例 #7
0
        internal static SafeCertStoreHandle ExportToMemoryStore(X509Certificate2Collection collection,
                                                                X509Certificate2Collection collection2 = null)
        {
            //
            // We need to Assert all StorePermission flags since this is a memory store and we want
            // semi-trusted code to be able to export certificates to a memory store.
            //

            StorePermission sp = new StorePermission(StorePermissionFlags.AllFlags);

            sp.Assert();

            SafeCertStoreHandle safeCertStoreHandle;

            // we always want to use CERT_STORE_ENUM_ARCHIVED_FLAG since we want to preserve the collection in this operation.
            // By default, Archived certificates will not be included.

            safeCertStoreHandle = CAPI.CertOpenStore(new IntPtr(CAPI.CERT_STORE_PROV_MEMORY),
                                                     CAPI.X509_ASN_ENCODING | CAPI.PKCS_7_ASN_ENCODING,
                                                     IntPtr.Zero,
                                                     CAPI.CERT_STORE_ENUM_ARCHIVED_FLAG | CAPI.CERT_STORE_CREATE_NEW_FLAG,
                                                     null);

            if (safeCertStoreHandle == null || safeCertStoreHandle.IsInvalid)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            AddToStore(safeCertStoreHandle, collection);

            if (collection2 != null)
            {
                AddToStore(safeCertStoreHandle, collection2);
            }

            return(safeCertStoreHandle);
        }
コード例 #8
0
        public void Open(OpenFlags flags)
        {
            if (m_location != StoreLocation.CurrentUser && m_location != StoreLocation.LocalMachine)
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.Arg_EnumIllegalVal), "m_location"));
            }

            uint storeFlags = X509Utils.MapX509StoreFlags(m_location, flags);

            if (!m_safeCertStoreHandle.IsInvalid)
            {
                // Free the current store handle
                m_safeCertStoreHandle.Dispose();
            }

            m_safeCertStoreHandle = CAPI.CertOpenStore(new IntPtr(CAPI.CERT_STORE_PROV_SYSTEM),
                                                       CAPI.X509_ASN_ENCODING | CAPI.PKCS_7_ASN_ENCODING,
                                                       IntPtr.Zero,
                                                       storeFlags,
                                                       m_storeName);

            if (m_safeCertStoreHandle == null || m_safeCertStoreHandle.IsInvalid)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            //
            // We want the store to auto-resync when requesting a snapshot so that
            // updates to the store will be taken into account.
            //

            CAPI.CertControlStore(m_safeCertStoreHandle,
                                  0,
                                  CAPI.CERT_STORE_CTRL_AUTO_RESYNC,
                                  IntPtr.Zero);
        }
コード例 #9
0
        static SafeCertStoreHandle ExportToMemoryStore(X509Certificate2Collection collection, IntPtr pCertContext)
        {
            CAPI.CERT_CONTEXT certContext = (CAPI.CERT_CONTEXT)Marshal.PtrToStructure(pCertContext, typeof(CAPI.CERT_CONTEXT));

            // No extra store nor intermediate certificates
            if ((collection == null || collection.Count <= 0) && certContext.hCertStore == IntPtr.Zero)
            {
                return(SafeCertStoreHandle.InvalidHandle);
            }

            // we always want to use CERT_STORE_ENUM_ARCHIVED_FLAG since we want to preserve the collection in this operation.
            // By default, Archived certificates will not be included.
            SafeCertStoreHandle certStoreHandle = CAPI.CertOpenStore(
                new IntPtr(CAPI.CERT_STORE_PROV_MEMORY),
                CAPI.X509_ASN_ENCODING | CAPI.PKCS_7_ASN_ENCODING,
                IntPtr.Zero,
                CAPI.CERT_STORE_ENUM_ARCHIVED_FLAG | CAPI.CERT_STORE_CREATE_NEW_FLAG,
                null);

            if (certStoreHandle == null || certStoreHandle.IsInvalid)
            {
                int error = Marshal.GetLastWin32Error();
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(error));
            }

            //
            // We use CertAddCertificateLinkToStore to keep a link to the original store, so any property changes get
            // applied to the original store. This has a limit of 99 links per cert context however.
            //

            // Add extra store
            if (collection != null && collection.Count > 0)
            {
                foreach (X509Certificate2 x509 in collection)
                {
                    if (!CAPI.CertAddCertificateLinkToStore(certStoreHandle,
                                                            x509.Handle,
                                                            CAPI.CERT_STORE_ADD_ALWAYS,
                                                            SafeCertContextHandle.InvalidHandle))
                    {
                        int error = Marshal.GetLastWin32Error();
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(error));
                    }
                }
            }

            // Add intermediates
            // The hCertStore needs to be acquired from an X509Certificate2 object
            // constructed using a fresh cert context handle. If we simply refer to the hCertStore
            // property of the certContext local variable directly, there is a risk that we are accessing
            // a closed store. This is because if the X509Certificate2(rawdata) constructor closes the store handle (hCertStore).
            // There is no way to know which constructor was used at this point.
            //
            using (SafeCertContextHandle safeCertContext
                       = CAPI.CertCreateCertificateContext(certContext.dwCertEncodingType,
                                                           certContext.pbCertEncoded,
                                                           certContext.cbCertEncoded))
            {
                //
                // Create an X509Certificate2 using the new cert context that dup's the provided certificate.
                //
                X509Certificate2 intermediatesCert = new X509Certificate2(safeCertContext.DangerousGetHandle());

                //
                // Dereference the handle to this intermediate cert and use it to access the handle
                // of this certificate's cert store. Then, call CAPI.CertAddCertificateLinkToStore
                // on each cert in this store by wrapping this cert store handle with an X509Store
                // object.
                //
                CAPI.CERT_CONTEXT intermediatesCertContext = (CAPI.CERT_CONTEXT)Marshal.PtrToStructure(intermediatesCert.Handle, typeof(CAPI.CERT_CONTEXT));
                if (intermediatesCertContext.hCertStore != IntPtr.Zero)
                {
                    X509Certificate2Collection intermediates = null;
                    X509Store store = new X509Store(intermediatesCertContext.hCertStore);

                    try
                    {
                        intermediates = store.Certificates;
                        foreach (X509Certificate2 x509 in intermediates)
                        {
                            if (!CAPI.CertAddCertificateLinkToStore(certStoreHandle,
                                                                    x509.Handle,
                                                                    CAPI.CERT_STORE_ADD_ALWAYS,
                                                                    SafeCertContextHandle.InvalidHandle))
                            {
                                int error = Marshal.GetLastWin32Error();
                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(error));
                            }
                        }
                    }
                    finally
                    {
                        SecurityUtils.ResetAllCertificates(intermediates);
                        store.Close();
                    }
                }
            }

            return(certStoreHandle);
        }
コード例 #10
0
        private static unsafe System.Security.Cryptography.SafeCertStoreHandle FindCertInStore(System.Security.Cryptography.SafeCertStoreHandle safeSourceStoreHandle, X509FindType findType, object findValue, bool validOnly)
        {
            string str;
            string str2;

            System.Security.Cryptography.SafeCertStoreHandle handle2;
            if (findValue == null)
            {
                throw new ArgumentNullException("findValue");
            }
            IntPtr           zero          = IntPtr.Zero;
            object           dwKeyUsageBit = null;
            object           obj3          = null;
            FindProcDelegate delegate2     = null;
            FindProcDelegate delegate3     = null;
            uint             dwFindType    = 0;

            CAPIBase.CRYPTOAPI_BLOB cryptoapi_blob = new CAPIBase.CRYPTOAPI_BLOB();
            SafeLocalAllocHandle    invalidHandle  = SafeLocalAllocHandle.InvalidHandle;

            System.Runtime.InteropServices.ComTypes.FILETIME filetime = new System.Runtime.InteropServices.ComTypes.FILETIME();
            string keyValue = null;

            switch (findType)
            {
            case X509FindType.FindByThumbprint:
            {
                if (findValue.GetType() != typeof(string))
                {
                    throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
                }
                byte[] managed = System.Security.Cryptography.X509Certificates.X509Utils.DecodeHexString((string)findValue);
                cryptoapi_blob.pbData = System.Security.Cryptography.X509Certificates.X509Utils.ByteToPtr(managed).DangerousGetHandle();
                cryptoapi_blob.cbData = (uint)managed.Length;
                dwFindType            = 0x10000;
                zero = new IntPtr((void *)&cryptoapi_blob);
                goto Label_0703;
            }

            case X509FindType.FindBySubjectName:
                if (findValue.GetType() != typeof(string))
                {
                    throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
                }
                str        = (string)findValue;
                dwFindType = 0x80007;
                zero       = System.Security.Cryptography.X509Certificates.X509Utils.StringToUniPtr(str).DangerousGetHandle();
                goto Label_0703;

            case X509FindType.FindBySubjectDistinguishedName:
                if (findValue.GetType() != typeof(string))
                {
                    throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
                }
                str           = (string)findValue;
                delegate2     = new FindProcDelegate(X509Certificate2Collection.FindSubjectDistinguishedNameCallback);
                dwKeyUsageBit = str;
                goto Label_0703;

            case X509FindType.FindByIssuerName:
                if (findValue.GetType() != typeof(string))
                {
                    throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
                }
                str2          = (string)findValue;
                dwFindType    = 0x80004;
                invalidHandle = System.Security.Cryptography.X509Certificates.X509Utils.StringToUniPtr(str2);
                zero          = invalidHandle.DangerousGetHandle();
                goto Label_0703;

            case X509FindType.FindByIssuerDistinguishedName:
                if (findValue.GetType() != typeof(string))
                {
                    throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
                }
                str2          = (string)findValue;
                delegate2     = new FindProcDelegate(X509Certificate2Collection.FindIssuerDistinguishedNameCallback);
                dwKeyUsageBit = str2;
                goto Label_0703;

            case X509FindType.FindBySerialNumber:
            {
                if (findValue.GetType() != typeof(string))
                {
                    throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
                }
                delegate2 = new FindProcDelegate(X509Certificate2Collection.FindSerialNumberCallback);
                delegate3 = new FindProcDelegate(X509Certificate2Collection.FindSerialNumberCallback);
                BigInt num2 = new BigInt();
                num2.FromHexadecimal((string)findValue);
                dwKeyUsageBit = num2.ToByteArray();
                num2.FromDecimal((string)findValue);
                obj3 = num2.ToByteArray();
                goto Label_0703;
            }

            case X509FindType.FindByTimeValid:
                if (findValue.GetType() != typeof(DateTime))
                {
                    throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
                }
                *((long *)&filetime) = ((DateTime)findValue).ToFileTime();
                delegate2            = new FindProcDelegate(X509Certificate2Collection.FindTimeValidCallback);
                dwKeyUsageBit        = filetime;
                goto Label_0703;

            case X509FindType.FindByTimeNotYetValid:
                if (findValue.GetType() != typeof(DateTime))
                {
                    throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
                }
                *((long *)&filetime) = ((DateTime)findValue).ToFileTime();
                delegate2            = new FindProcDelegate(X509Certificate2Collection.FindTimeNotBeforeCallback);
                dwKeyUsageBit        = filetime;
                goto Label_0703;

            case X509FindType.FindByTimeExpired:
                if (findValue.GetType() != typeof(DateTime))
                {
                    throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
                }
                *((long *)&filetime) = ((DateTime)findValue).ToFileTime();
                delegate2            = new FindProcDelegate(X509Certificate2Collection.FindTimeNotAfterCallback);
                dwKeyUsageBit        = filetime;
                goto Label_0703;

            case X509FindType.FindByTemplateName:
                if (findValue.GetType() != typeof(string))
                {
                    throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
                }
                dwKeyUsageBit = (string)findValue;
                delegate2     = new FindProcDelegate(X509Certificate2Collection.FindTemplateNameCallback);
                goto Label_0703;

            case X509FindType.FindByApplicationPolicy:
                if (findValue.GetType() != typeof(string))
                {
                    throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
                }
                keyValue = System.Security.Cryptography.X509Certificates.X509Utils.FindOidInfo(2, (string)findValue, System.Security.Cryptography.OidGroup.Policy);
                if (keyValue == null)
                {
                    keyValue = (string)findValue;
                    System.Security.Cryptography.X509Certificates.X509Utils.ValidateOidValue(keyValue);
                }
                dwKeyUsageBit = keyValue;
                delegate2     = new FindProcDelegate(X509Certificate2Collection.FindApplicationPolicyCallback);
                goto Label_0703;

            case X509FindType.FindByCertificatePolicy:
                if (findValue.GetType() != typeof(string))
                {
                    throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
                }
                keyValue = System.Security.Cryptography.X509Certificates.X509Utils.FindOidInfo(2, (string)findValue, System.Security.Cryptography.OidGroup.Policy);
                if (keyValue == null)
                {
                    keyValue = (string)findValue;
                    System.Security.Cryptography.X509Certificates.X509Utils.ValidateOidValue(keyValue);
                }
                dwKeyUsageBit = keyValue;
                delegate2     = new FindProcDelegate(X509Certificate2Collection.FindCertificatePolicyCallback);
                goto Label_0703;

            case X509FindType.FindByExtension:
                if (findValue.GetType() != typeof(string))
                {
                    throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
                }
                keyValue = System.Security.Cryptography.X509Certificates.X509Utils.FindOidInfo(2, (string)findValue, System.Security.Cryptography.OidGroup.ExtensionOrAttribute);
                if (keyValue == null)
                {
                    keyValue = (string)findValue;
                    System.Security.Cryptography.X509Certificates.X509Utils.ValidateOidValue(keyValue);
                }
                dwKeyUsageBit = keyValue;
                delegate2     = new FindProcDelegate(X509Certificate2Collection.FindExtensionCallback);
                goto Label_0703;

            case X509FindType.FindByKeyUsage:
            {
                if (!(findValue.GetType() == typeof(string)))
                {
                    if (findValue.GetType() == typeof(X509KeyUsageFlags))
                    {
                        dwKeyUsageBit = findValue;
                    }
                    else
                    {
                        if (!(findValue.GetType() == typeof(uint)) && !(findValue.GetType() == typeof(int)))
                        {
                            throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindType"));
                        }
                        dwKeyUsageBit = findValue;
                    }
                    goto Label_06A2;
                }
                CAPIBase.KEY_USAGE_STRUCT[] key_usage_structArray = new CAPIBase.KEY_USAGE_STRUCT[] { new CAPIBase.KEY_USAGE_STRUCT("DigitalSignature", 0x80), new CAPIBase.KEY_USAGE_STRUCT("NonRepudiation", 0x40), new CAPIBase.KEY_USAGE_STRUCT("KeyEncipherment", 0x20), new CAPIBase.KEY_USAGE_STRUCT("DataEncipherment", 0x10), new CAPIBase.KEY_USAGE_STRUCT("KeyAgreement", 8), new CAPIBase.KEY_USAGE_STRUCT("KeyCertSign", 4), new CAPIBase.KEY_USAGE_STRUCT("CrlSign", 2), new CAPIBase.KEY_USAGE_STRUCT("EncipherOnly", 1), new CAPIBase.KEY_USAGE_STRUCT("DecipherOnly", 0x8000) };
                for (uint i = 0; i < key_usage_structArray.Length; i++)
                {
                    if (string.Compare(key_usage_structArray[i].pwszKeyUsage, (string)findValue, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        dwKeyUsageBit = key_usage_structArray[i].dwKeyUsageBit;
                        break;
                    }
                }
                break;
            }

            case X509FindType.FindBySubjectKeyIdentifier:
                if (findValue.GetType() != typeof(string))
                {
                    throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindValue"));
                }
                dwKeyUsageBit = System.Security.Cryptography.X509Certificates.X509Utils.DecodeHexString((string)findValue);
                delegate2     = new FindProcDelegate(X509Certificate2Collection.FindSubjectKeyIdentifierCallback);
                goto Label_0703;

            default:
                throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindType"));
            }
            if (dwKeyUsageBit == null)
            {
                throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidFindType"));
            }
Label_06A2:
            delegate2 = new FindProcDelegate(X509Certificate2Collection.FindKeyUsageCallback);
Label_0703:
            handle2 = CAPI.CertOpenStore(new IntPtr(2L), 0x10001, IntPtr.Zero, 0x2200, null);
            if ((handle2 == null) || handle2.IsInvalid)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            FindByCert(safeSourceStoreHandle, dwFindType, zero, validOnly, delegate2, delegate3, dwKeyUsageBit, obj3, handle2);
            invalidHandle.Dispose();
            return(handle2);
        }