Пример #1
0
        public X509Certificate2Collection Find(X509FindType findType, object findValue, bool validOnly)
        {
            SafeHGlobalHandle invalidHandle = SafeHGlobalHandle.InvalidHandle;

            System.IdentityModel.SafeCertContextHandle pPrevCertContext = System.IdentityModel.SafeCertContextHandle.InvalidHandle;
            X509Certificate2Collection certificates = new X509Certificate2Collection();
            SafeHGlobalHandle          handle3      = SafeHGlobalHandle.InvalidHandle;

            try
            {
                uint   num;
                string str;
                byte[] buffer;
                System.IdentityModel.CAPI.CRYPTOAPI_BLOB cryptoapi_blob;
                switch (findType)
                {
                case X509FindType.FindByThumbprint:
                    buffer = findValue as byte[];
                    if (buffer == null)
                    {
                        str = findValue as string;
                        if (str == null)
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(System.IdentityModel.SR.GetString("X509FindValueMismatchMulti", new object[] { findType, typeof(string), typeof(byte[]), findValue.GetType() })));
                        }
                        goto Label_011A;
                    }
                    goto Label_0123;

                case X509FindType.FindBySubjectName:
                    str = findValue as string;
                    if (str == null)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(System.IdentityModel.SR.GetString("X509FindValueMismatch", new object[] { findType, typeof(string), findValue.GetType() })));
                    }
                    break;

                case X509FindType.FindBySubjectDistinguishedName:
                    if (!(findValue is string))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(System.IdentityModel.SR.GetString("X509FindValueMismatch", new object[] { findType, typeof(string), findValue.GetType() })));
                    }
                    goto Label_01C4;

                case X509FindType.FindByIssuerName:
                    str = findValue as string;
                    if (str == null)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(System.IdentityModel.SR.GetString("X509FindValueMismatch", new object[] { findType, typeof(string), findValue.GetType() })));
                    }
                    goto Label_021D;

                case X509FindType.FindByIssuerDistinguishedName:
                    if (!(findValue is string))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(System.IdentityModel.SR.GetString("X509FindValueMismatch", new object[] { findType, typeof(string), findValue.GetType() })));
                    }
                    goto Label_027E;

                case X509FindType.FindBySerialNumber:
                    buffer = findValue as byte[];
                    if (buffer == null)
                    {
                        str = findValue as string;
                        if (str == null)
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(System.IdentityModel.SR.GetString("X509FindValueMismatchMulti", new object[] { findType, typeof(string), typeof(byte[]), findValue.GetType() })));
                        }
                        goto Label_02F4;
                    }
                    goto Label_033C;

                case X509FindType.FindBySubjectKeyIdentifier:
                    buffer = findValue as byte[];
                    if (buffer == null)
                    {
                        str = findValue as string;
                        if (str == null)
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(System.IdentityModel.SR.GetString("X509FindValueMismatchMulti", new object[] { findType, typeof(string), typeof(byte[]), findValue.GetType() })));
                        }
                        buffer = System.IdentityModel.SecurityUtils.DecodeHexString(str);
                    }
                    findValue = buffer;
                    num       = 0;
                    goto Label_03F4;

                default:
                {
                    X509Store store = new X509Store(this.certStoreHandle.DangerousGetHandle());
                    try
                    {
                        return(store.Certificates.Find(findType, findValue, validOnly));
                    }
                    finally
                    {
                        store.Close();
                    }
                    goto Label_03F4;
                }
                }
                num           = 0x80007;
                invalidHandle = SafeHGlobalHandle.AllocHGlobal(str);
                goto Label_03F4;
Label_011A:
                buffer = System.IdentityModel.SecurityUtils.DecodeHexString(str);
Label_0123:
                cryptoapi_blob        = new System.IdentityModel.CAPI.CRYPTOAPI_BLOB();
                handle3               = SafeHGlobalHandle.AllocHGlobal(buffer);
                cryptoapi_blob.pbData = handle3.DangerousGetHandle();
                cryptoapi_blob.cbData = (uint)buffer.Length;
                num = 0x10000;
                Marshal.StructureToPtr(cryptoapi_blob, SafeHGlobalHandle.AllocHGlobal(System.IdentityModel.CAPI.CRYPTOAPI_BLOB.Size).DangerousGetHandle(), false);
                goto Label_03F4;
Label_01C4:
                num = 0;
                goto Label_03F4;
Label_021D:
                num           = 0x80004;
                invalidHandle = SafeHGlobalHandle.AllocHGlobal(str);
                goto Label_03F4;
Label_027E:
                num = 0;
                goto Label_03F4;
Label_02F4:
                buffer = System.IdentityModel.SecurityUtils.DecodeHexString(str);
                int length = buffer.Length;
                int index  = 0;
                for (int i = length - 1; index < (buffer.Length / 2); i--)
                {
                    byte num5 = buffer[index];
                    buffer[index] = buffer[i];
                    buffer[i]     = num5;
                    index++;
                }
Label_033C:
                findValue = buffer;
                num       = 0;
Label_03F4:
                pPrevCertContext = System.IdentityModel.CAPI.CertFindCertificateInStore(this.certStoreHandle, 0x10001, 0, num, invalidHandle, pPrevCertContext);
                while ((pPrevCertContext != null) && !pPrevCertContext.IsInvalid)
                {
                    X509Certificate2 certificate;
                    if (this.TryGetMatchingX509Certificate(pPrevCertContext.DangerousGetHandle(), findType, num, findValue, validOnly, out certificate))
                    {
                        certificates.Add(certificate);
                    }
                    RuntimeHelpers.PrepareConstrainedRegions();
                    try
                    {
                        continue;
                    }
                    finally
                    {
                        GC.SuppressFinalize(pPrevCertContext);
                        pPrevCertContext = System.IdentityModel.CAPI.CertFindCertificateInStore(this.certStoreHandle, 0x10001, 0, num, invalidHandle, pPrevCertContext);
                    }
                }
            }
            finally
            {
                if (pPrevCertContext != null)
                {
                    pPrevCertContext.Close();
                }
                invalidHandle.Close();
                handle3.Close();
            }
            return(certificates);
        }