Пример #1
0
        public void AddIssuerSerial(string issuerName, string serialNumber)
        {
            BigInt h = new BigInt();

            h.FromHexadecimal(serialNumber);
            if (_issuerSerials == null)
            {
                _issuerSerials = new ArrayList();
            }
            _issuerSerials.Add(new X509IssuerSerial(issuerName, h.ToDecimal()));
        }
Пример #2
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);
        }