public static void Encode_CERT_INFO(CERT_INFO info, out IntPtr pEncoded, out int encodedSize)
{
pEncoded = IntPtr.Zero;
encodedSize = 0;
IntPtr pData = IntPtr.Zero;
int dwDataSize = 0;
GCHandle hData = GCHandle.Alloc(info);
try
{
// calculate amount of memory required.
int bResult = Win32.CryptEncodeObjectEx(
Win32.X509_ASN_ENCODING,
(IntPtr)Win32.X509_CERT_CRL_TO_BE_SIGNED,
hData.AddrOfPinnedObject(),
0,
IntPtr.Zero,
IntPtr.Zero,
ref dwDataSize);
if (bResult == 0)
{
throw GetLastError(StatusCodes.BadEncodingError, "Could not get size for CRL_INFO.");
}
// allocate memory.
pData = Marshal.AllocHGlobal(dwDataSize);
// decode blob.
bResult = Win32.CryptEncodeObjectEx(
Win32.X509_ASN_ENCODING | Win32.PKCS_7_ASN_ENCODING,
(IntPtr)Win32.X509_CERT_CRL_TO_BE_SIGNED,
hData.AddrOfPinnedObject(),
0,
IntPtr.Zero,
pData,
ref dwDataSize);
if (bResult == 0)
{
throw GetLastError(StatusCodes.BadEncodingError, "Could not encoder CRL_INFO.");
}
// return results.
pEncoded = pData;
encodedSize = dwDataSize;
pData = IntPtr.Zero;
}
finally
{
if (hData.IsAllocated)
{
hData.Free();
}
if (pData != IntPtr.Zero)
{
Marshal.FreeHGlobal(pData);
}
}
}