public static byte[] GetCertificateProperty( SafeCertContextHandle certificateContext, NativeMethods.Crypt32.CertificateProperty property) { var pcbData = 0; if (!NativeMethods.Crypt32.CertGetCertificateContextProperty( certificateContext, property, null, ref pcbData) ) { var code = (ErrorCode)Marshal.GetLastWin32Error(); if (code != ErrorCode.MoreData) { throw new CryptographicException((int)code); } } var pvData = new byte[pcbData]; if (!NativeMethods.Crypt32.CertGetCertificateContextProperty( certificateContext, property, pvData, ref pcbData) ) { throw new CryptographicException(Marshal.GetLastWin32Error()); } return(pvData); }
internal static unsafe T GetCertificateProperty <T>( SafeCertContextHandle certificateContext, NativeMethods.Crypt32.CertificateProperty property ) where T : struct { fixed(byte *numRef = GetCertificateProperty(certificateContext, property)) { return((T)Marshal.PtrToStructure(new IntPtr(numRef), typeof(T))); } }
internal static bool HasCertificateProperty( SafeCertContextHandle certificateContext, NativeMethods.Crypt32.CertificateProperty property) { var pcbData = 0; if (!NativeMethods.Crypt32.CertGetCertificateContextProperty( certificateContext, property, null, ref pcbData) ) { return(Marshal.GetLastWin32Error() == (int)ErrorCode.MoreData); } return(true); }