Esempio n. 1
0
        private static unsafe int FindTemplateNameCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData)
        {
            IntPtr zero = IntPtr.Zero;
            IntPtr ptr  = IntPtr.Zero;

            CAPIBase.CERT_CONTEXT cert_context = *((CAPIBase.CERT_CONTEXT *)safeCertContextHandle.DangerousGetHandle());
            CAPIBase.CERT_INFO    cert_info    = (CAPIBase.CERT_INFO)Marshal.PtrToStructure(cert_context.pCertInfo, typeof(CAPIBase.CERT_INFO));
            zero = CAPISafe.CertFindExtension("1.3.6.1.4.1.311.20.2", cert_info.cExtension, cert_info.rgExtension);
            ptr  = CAPISafe.CertFindExtension("1.3.6.1.4.1.311.21.7", cert_info.cExtension, cert_info.rgExtension);
            if ((zero != IntPtr.Zero) || (ptr != IntPtr.Zero))
            {
                if (zero != IntPtr.Zero)
                {
                    CAPIBase.CERT_EXTENSION cert_extension = (CAPIBase.CERT_EXTENSION)Marshal.PtrToStructure(zero, typeof(CAPIBase.CERT_EXTENSION));
                    byte[] destination = new byte[cert_extension.Value.cbData];
                    Marshal.Copy(cert_extension.Value.pbData, destination, 0, destination.Length);
                    uint cbDecodedValue = 0;
                    SafeLocalAllocHandle decodedValue = null;
                    if (CAPI.DecodeObject(new IntPtr(0x18L), destination, out decodedValue, out cbDecodedValue))
                    {
                        CAPIBase.CERT_NAME_VALUE cert_name_value = (CAPIBase.CERT_NAME_VALUE)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPIBase.CERT_NAME_VALUE));
                        if (string.Compare(Marshal.PtrToStringUni(cert_name_value.Value.pbData), (string)pvCallbackData, StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            return(0);
                        }
                    }
                }
                if (ptr != IntPtr.Zero)
                {
                    CAPIBase.CERT_EXTENSION cert_extension2 = (CAPIBase.CERT_EXTENSION)Marshal.PtrToStructure(ptr, typeof(CAPIBase.CERT_EXTENSION));
                    byte[] buffer2 = new byte[cert_extension2.Value.cbData];
                    Marshal.Copy(cert_extension2.Value.pbData, buffer2, 0, buffer2.Length);
                    uint num2 = 0;
                    SafeLocalAllocHandle handle2 = null;
                    if (CAPI.DecodeObject(new IntPtr(0x40L), buffer2, out handle2, out num2))
                    {
                        CAPIBase.CERT_TEMPLATE_EXT cert_template_ext = (CAPIBase.CERT_TEMPLATE_EXT)Marshal.PtrToStructure(handle2.DangerousGetHandle(), typeof(CAPIBase.CERT_TEMPLATE_EXT));
                        string strB = System.Security.Cryptography.X509Certificates.X509Utils.FindOidInfo(2, (string)pvCallbackData, System.Security.Cryptography.OidGroup.Template);
                        if (strB == null)
                        {
                            strB = (string)pvCallbackData;
                        }
                        if (string.Compare(cert_template_ext.pszObjId, strB, StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            return(0);
                        }
                    }
                }
            }
            return(1);
        }
        public unsafe string GetNameInfo(X509NameType nameType, bool forIssuer)
        {
            uint dwFlags       = forIssuer ? 1 : 0;
            uint dwDisplayType = System.Security.Cryptography.X509Certificates.X509Utils.MapNameType(nameType);

            switch (dwDisplayType)
            {
            case 1:
                return(CAPI.GetCertNameInfo(this.m_safeCertContext, dwFlags, dwDisplayType));

            case 4:
                return(CAPI.GetCertNameInfo(this.m_safeCertContext, dwFlags, dwDisplayType));
            }
            string str = string.Empty;

            CAPIBase.CERT_CONTEXT cert_context = *((CAPIBase.CERT_CONTEXT *) this.m_safeCertContext.DangerousGetHandle());
            CAPIBase.CERT_INFO    cert_info    = (CAPIBase.CERT_INFO)Marshal.PtrToStructure(cert_context.pCertInfo, typeof(CAPIBase.CERT_INFO));
            IntPtr[] ptrArray = new IntPtr[] { CAPISafe.CertFindExtension(forIssuer ? "2.5.29.8" : "2.5.29.7", cert_info.cExtension, cert_info.rgExtension), CAPISafe.CertFindExtension(forIssuer ? "2.5.29.18" : "2.5.29.17", cert_info.cExtension, cert_info.rgExtension) };
            for (int i = 0; i < ptrArray.Length; i++)
            {
                if (ptrArray[i] != IntPtr.Zero)
                {
                    CAPIBase.CERT_EXTENSION cert_extension = (CAPIBase.CERT_EXTENSION)Marshal.PtrToStructure(ptrArray[i], typeof(CAPIBase.CERT_EXTENSION));
                    byte[] destination = new byte[cert_extension.Value.cbData];
                    Marshal.Copy(cert_extension.Value.pbData, destination, 0, destination.Length);
                    uint cbDecodedValue = 0;
                    SafeLocalAllocHandle decodedValue = null;
                    SafeLocalAllocHandle handle2      = System.Security.Cryptography.X509Certificates.X509Utils.StringToAnsiPtr(cert_extension.pszObjId);
                    bool flag = CAPI.DecodeObject(handle2.DangerousGetHandle(), destination, out decodedValue, out cbDecodedValue);
                    handle2.Dispose();
                    if (flag)
                    {
                        CAPIBase.CERT_ALT_NAME_INFO cert_alt_name_info = (CAPIBase.CERT_ALT_NAME_INFO)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPIBase.CERT_ALT_NAME_INFO));
                        for (int j = 0; j < cert_alt_name_info.cAltEntry; j++)
                        {
                            IntPtr ptr = new IntPtr(((long)cert_alt_name_info.rgAltEntry) + (j * Marshal.SizeOf(typeof(CAPIBase.CERT_ALT_NAME_ENTRY))));
                            CAPIBase.CERT_ALT_NAME_ENTRY cert_alt_name_entry = (CAPIBase.CERT_ALT_NAME_ENTRY)Marshal.PtrToStructure(ptr, typeof(CAPIBase.CERT_ALT_NAME_ENTRY));
                            switch (dwDisplayType)
                            {
                            case 6:
                                if (cert_alt_name_entry.dwAltNameChoice == 3)
                                {
                                    str = Marshal.PtrToStringUni(cert_alt_name_entry.Value.pwszDNSName);
                                }
                                break;

                            case 7:
                                if (cert_alt_name_entry.dwAltNameChoice == 7)
                                {
                                    str = Marshal.PtrToStringUni(cert_alt_name_entry.Value.pwszURL);
                                }
                                break;

                            case 8:
                                if (cert_alt_name_entry.dwAltNameChoice == 1)
                                {
                                    CAPIBase.CERT_OTHER_NAME cert_other_name = (CAPIBase.CERT_OTHER_NAME)Marshal.PtrToStructure(cert_alt_name_entry.Value.pOtherName, typeof(CAPIBase.CERT_OTHER_NAME));
                                    if (cert_other_name.pszObjId == "1.3.6.1.4.1.311.20.2.3")
                                    {
                                        uint num6 = 0;
                                        SafeLocalAllocHandle handle3 = null;
                                        if (CAPI.DecodeObject(new IntPtr(0x18L), System.Security.Cryptography.X509Certificates.X509Utils.PtrToByte(cert_other_name.Value.pbData, cert_other_name.Value.cbData), out handle3, out num6))
                                        {
                                            CAPIBase.CERT_NAME_VALUE cert_name_value = (CAPIBase.CERT_NAME_VALUE)Marshal.PtrToStructure(handle3.DangerousGetHandle(), typeof(CAPIBase.CERT_NAME_VALUE));
                                            if (System.Security.Cryptography.X509Certificates.X509Utils.IsCertRdnCharString(cert_name_value.dwValueType))
                                            {
                                                str = Marshal.PtrToStringUni(cert_name_value.Value.pbData);
                                            }
                                            handle3.Dispose();
                                        }
                                    }
                                }
                                break;
                            }
                        }
                        decodedValue.Dispose();
                    }
                }
            }
            if ((nameType != X509NameType.DnsName) || ((str != null) && (str.Length != 0)))
            {
                return(str);
            }
            return(CAPI.GetCertNameInfo(this.m_safeCertContext, dwFlags, 3));
        }