internal static Collection <string> GetCertEKU(X509Certificate2 cert) { using (SecuritySupport._tracer.TraceMethod()) { Collection <string> collection = new Collection <string>(); IntPtr handle = cert.Handle; int pcbUsage = 0; IntPtr zero = IntPtr.Zero; if (!System.Management.Automation.Security.NativeMethods.CertGetEnhancedKeyUsage(handle, 0U, zero, out pcbUsage)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } if (pcbUsage > 0) { IntPtr num = Marshal.AllocHGlobal(pcbUsage); try { System.Management.Automation.Security.NativeMethods.CERT_ENHKEY_USAGE certEnhkeyUsage = System.Management.Automation.Security.NativeMethods.CertGetEnhancedKeyUsage(handle, 0U, num, out pcbUsage) ? (System.Management.Automation.Security.NativeMethods.CERT_ENHKEY_USAGE)Marshal.PtrToStructure(num, typeof(System.Management.Automation.Security.NativeMethods.CERT_ENHKEY_USAGE)) : throw new Win32Exception(Marshal.GetLastWin32Error()); IntPtr rgpszUsageIdentifier = certEnhkeyUsage.rgpszUsageIdentifier; for (int index = 0; (long)index < (long)certEnhkeyUsage.cUsageIdentifier; ++index) { string stringAnsi = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(rgpszUsageIdentifier, index * Marshal.SizeOf((object)rgpszUsageIdentifier))); collection.Add(stringAnsi); } } finally { Marshal.FreeHGlobal(num); } } return(collection); } }
internal static Collection <string> GetCertEKU(X509Certificate2 cert) { Collection <string> collection = new Collection <string>(); IntPtr handle = cert.Handle; int pcbUsage = 0; IntPtr zero = IntPtr.Zero; if (System.Management.Automation.Security.NativeMethods.CertGetEnhancedKeyUsage(handle, 0, zero, out pcbUsage)) { if (pcbUsage <= 0) { return(collection); } IntPtr pUsage = Marshal.AllocHGlobal(pcbUsage); try { if (!System.Management.Automation.Security.NativeMethods.CertGetEnhancedKeyUsage(handle, 0, pUsage, out pcbUsage)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } System.Management.Automation.Security.NativeMethods.CERT_ENHKEY_USAGE cert_enhkey_usage = (System.Management.Automation.Security.NativeMethods.CERT_ENHKEY_USAGE)Marshal.PtrToStructure(pUsage, typeof(System.Management.Automation.Security.NativeMethods.CERT_ENHKEY_USAGE)); IntPtr rgpszUsageIdentifier = cert_enhkey_usage.rgpszUsageIdentifier; for (int i = 0; i < cert_enhkey_usage.cUsageIdentifier; i++) { string item = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(rgpszUsageIdentifier, i * Marshal.SizeOf(rgpszUsageIdentifier))); collection.Add(item); } return(collection); } finally { Marshal.FreeHGlobal(pUsage); } } throw new Win32Exception(Marshal.GetLastWin32Error()); }