G
Guest
I'm trying to call CertFindCertificateInStore to find all certificates in the
store that have the Code Signing enhanced key usage. I'm running into
problems marshalling the array of OIDs in _CTL_USAGE. I keep getting a "This
type can not be marshalled as a structure field."
Does anyone have any ideas as to what I'm doing wrong? Here's the code:
namespace CertSignTest
{
using System;
using System.Diagnostics;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.ComponentModel;
public class WinCapi
{
/*
HCERTSTORE WINAPI CertOpenSystemStore(HCRYPTPROV hprov, LPTCSTR
szSubsystemProtocol);
BOOL WINAPI CertCloseStore(HCERTSTORE hCertStore, DWORD dwFlags);
PCCERT_CONTEXT WINAPI CertFindCertificateInStore(
HCERTSTORE hCertStore,
DWORD dwCertEncodingType,
DWORD dwFindFlags,
DWORD dwFindType,
const void* pvFindPara,
PCCERT_CONTEXT pPrevCertContext);
BOOL WINAPI CertFreeCertificateContext(
PCCERT_CONTEXT pCertContext
);
typedef struct _CTL_USAGE {
DWORD cUsageIdentifier;
LPSTR *rgpszUsageIdentifier; // array of pszObjId
} CTL_USAGE, *PCTL_USAGE, CERT_ENHKEY_USAGE, *PCERT_ENHKEY_USAGE;
*/
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct CRYPT_OID_INFO
{
public uint cbSize;
[MarshalAs(UnmanagedType.LPStr)] public String pszOID;
[MarshalAs(UnmanagedType.LPWStr)]public String pwszName;
public uint dwGroupID;
public uint dwValue;
public int cbData; //ExtraInfo blob
public IntPtr pbData;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct _CTL_USAGE
{
public int cUsageIdentifier;
[MarshalAs(UnmanagedType.ByValArray)]
public string[] rgpszUseageIdentifier;
}
[DllImport("crypt32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern IntPtr CertOpenSystemStore(
IntPtr hCryptProv,
string storename) ;
[DllImport("crypt32.dll", SetLastError=true)]
public static extern bool CertCloseStore(
IntPtr hCertStore,
uint dwFlags) ;
[DllImport("crypt32.dll", SetLastError=true)]
public static extern IntPtr CertFindCertificateInStore(
IntPtr hCertStore,
uint dwCertEncodingType,
uint dwFindFlags,
uint dwFindType,
ref WinCapi._CTL_USAGE pvFindPara,
IntPtr pPrevCertCntxt) ;
[DllImport("crypt32.dll", SetLastError=true)]
public static extern bool CertFreeCertificateContext(
IntPtr hCertStore) ;
}
public class SimpleCert
{
const string szOID_PKIX_KP_CODE_SIGNING = "1.3.6.1.5.5.7.3.3";
const string MY = "MY";
const string OTHERS = "AddressBook";
const uint PKCS_7_ASN_ENCODING = 0x00010000;
const uint X509_ASN_ENCODING = 0x00000001;
const uint CERT_FIND_SUBJECT_STR = 0x00080007;
const uint CERT_FIND_ENHKEY_USAGE = 0x000A0000;
static uint MY_ENCODING_TYPE = PKCS_7_ASN_ENCODING | X509_ASN_ENCODING ;
public static void Main()
{
IntPtr hSysStore = IntPtr.Zero;
IntPtr hCertCntxt = IntPtr.Zero;
IntPtr hStructure = IntPtr.Zero;
WinCapi._CTL_USAGE test = new WinCapi._CTL_USAGE();
string[] strTest = new string[1];
strTest[0] = szOID_PKIX_KP_CODE_SIGNING;
hSysStore = WinCapi.CertOpenSystemStore(IntPtr.Zero, MY) ;
Console.WriteLine("Store Handle:\t0x{0:X}", hSysStore.ToInt32());
test.cUsageIdentifier = 1;
test.rgpszUseageIdentifier = new string[1];
test.rgpszUseageIdentifier[0] = szOID_PKIX_KP_CODE_SIGNING;
if(hSysStore != IntPtr.Zero)
{
hCertCntxt=WinCapi.CertFindCertificateInStore(
hSysStore,
MY_ENCODING_TYPE,
0,
CERT_FIND_ENHKEY_USAGE,
ref test,
IntPtr.Zero) ;
Debug.WriteLine(Marshal.GetLastWin32Error().ToString());
while(hCertCntxt != IntPtr.Zero)
{
Console.WriteLine("CertContext:\t0x{0:X}", hCertCntxt.ToInt32()) ;
X509Certificate foundcert = new X509Certificate(hCertCntxt);
Console.WriteLine("\nFound certificate with SubjectName string
\"{0}\"",lpszCertSubject);
Console.WriteLine("SubjectName:\t{0}", foundcert.GetName());
Console.WriteLine("Serial No:\t{0}", foundcert.GetSerialNumberString());
Console.WriteLine("HashString:\t{0}" , foundcert.GetCertHashString());
Console.WriteLine("PublicKey:\t{0}",foundcert.GetPublicKey().ToString());
Console.WriteLine("Issuer:\t {0}",foundcert.GetIssuerName());
hCertCntxt=WinCapi.CertFindCertificateInStore(
hSysStore,
MY_ENCODING_TYPE,
0,
CERT_FIND_ENHKEY_USAGE,
ref test,
hCertCntxt) ;
}
}
//------- Clean Up -----------
if(hCertCntxt != IntPtr.Zero)
WinCapi.CertFreeCertificateContext(hCertCntxt);
if(hSysStore != IntPtr.Zero)
WinCapi.CertCloseStore(hSysStore, 0) ;
}
}
store that have the Code Signing enhanced key usage. I'm running into
problems marshalling the array of OIDs in _CTL_USAGE. I keep getting a "This
type can not be marshalled as a structure field."
Does anyone have any ideas as to what I'm doing wrong? Here's the code:
namespace CertSignTest
{
using System;
using System.Diagnostics;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.ComponentModel;
public class WinCapi
{
/*
HCERTSTORE WINAPI CertOpenSystemStore(HCRYPTPROV hprov, LPTCSTR
szSubsystemProtocol);
BOOL WINAPI CertCloseStore(HCERTSTORE hCertStore, DWORD dwFlags);
PCCERT_CONTEXT WINAPI CertFindCertificateInStore(
HCERTSTORE hCertStore,
DWORD dwCertEncodingType,
DWORD dwFindFlags,
DWORD dwFindType,
const void* pvFindPara,
PCCERT_CONTEXT pPrevCertContext);
BOOL WINAPI CertFreeCertificateContext(
PCCERT_CONTEXT pCertContext
);
typedef struct _CTL_USAGE {
DWORD cUsageIdentifier;
LPSTR *rgpszUsageIdentifier; // array of pszObjId
} CTL_USAGE, *PCTL_USAGE, CERT_ENHKEY_USAGE, *PCERT_ENHKEY_USAGE;
*/
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct CRYPT_OID_INFO
{
public uint cbSize;
[MarshalAs(UnmanagedType.LPStr)] public String pszOID;
[MarshalAs(UnmanagedType.LPWStr)]public String pwszName;
public uint dwGroupID;
public uint dwValue;
public int cbData; //ExtraInfo blob
public IntPtr pbData;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct _CTL_USAGE
{
public int cUsageIdentifier;
[MarshalAs(UnmanagedType.ByValArray)]
public string[] rgpszUseageIdentifier;
}
[DllImport("crypt32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern IntPtr CertOpenSystemStore(
IntPtr hCryptProv,
string storename) ;
[DllImport("crypt32.dll", SetLastError=true)]
public static extern bool CertCloseStore(
IntPtr hCertStore,
uint dwFlags) ;
[DllImport("crypt32.dll", SetLastError=true)]
public static extern IntPtr CertFindCertificateInStore(
IntPtr hCertStore,
uint dwCertEncodingType,
uint dwFindFlags,
uint dwFindType,
ref WinCapi._CTL_USAGE pvFindPara,
IntPtr pPrevCertCntxt) ;
[DllImport("crypt32.dll", SetLastError=true)]
public static extern bool CertFreeCertificateContext(
IntPtr hCertStore) ;
}
public class SimpleCert
{
const string szOID_PKIX_KP_CODE_SIGNING = "1.3.6.1.5.5.7.3.3";
const string MY = "MY";
const string OTHERS = "AddressBook";
const uint PKCS_7_ASN_ENCODING = 0x00010000;
const uint X509_ASN_ENCODING = 0x00000001;
const uint CERT_FIND_SUBJECT_STR = 0x00080007;
const uint CERT_FIND_ENHKEY_USAGE = 0x000A0000;
static uint MY_ENCODING_TYPE = PKCS_7_ASN_ENCODING | X509_ASN_ENCODING ;
public static void Main()
{
IntPtr hSysStore = IntPtr.Zero;
IntPtr hCertCntxt = IntPtr.Zero;
IntPtr hStructure = IntPtr.Zero;
WinCapi._CTL_USAGE test = new WinCapi._CTL_USAGE();
string[] strTest = new string[1];
strTest[0] = szOID_PKIX_KP_CODE_SIGNING;
hSysStore = WinCapi.CertOpenSystemStore(IntPtr.Zero, MY) ;
Console.WriteLine("Store Handle:\t0x{0:X}", hSysStore.ToInt32());
test.cUsageIdentifier = 1;
test.rgpszUseageIdentifier = new string[1];
test.rgpszUseageIdentifier[0] = szOID_PKIX_KP_CODE_SIGNING;
if(hSysStore != IntPtr.Zero)
{
hCertCntxt=WinCapi.CertFindCertificateInStore(
hSysStore,
MY_ENCODING_TYPE,
0,
CERT_FIND_ENHKEY_USAGE,
ref test,
IntPtr.Zero) ;
Debug.WriteLine(Marshal.GetLastWin32Error().ToString());
while(hCertCntxt != IntPtr.Zero)
{
Console.WriteLine("CertContext:\t0x{0:X}", hCertCntxt.ToInt32()) ;
X509Certificate foundcert = new X509Certificate(hCertCntxt);
Console.WriteLine("\nFound certificate with SubjectName string
\"{0}\"",lpszCertSubject);
Console.WriteLine("SubjectName:\t{0}", foundcert.GetName());
Console.WriteLine("Serial No:\t{0}", foundcert.GetSerialNumberString());
Console.WriteLine("HashString:\t{0}" , foundcert.GetCertHashString());
Console.WriteLine("PublicKey:\t{0}",foundcert.GetPublicKey().ToString());
Console.WriteLine("Issuer:\t {0}",foundcert.GetIssuerName());
hCertCntxt=WinCapi.CertFindCertificateInStore(
hSysStore,
MY_ENCODING_TYPE,
0,
CERT_FIND_ENHKEY_USAGE,
ref test,
hCertCntxt) ;
}
}
//------- Clean Up -----------
if(hCertCntxt != IntPtr.Zero)
WinCapi.CertFreeCertificateContext(hCertCntxt);
if(hSysStore != IntPtr.Zero)
WinCapi.CertCloseStore(hSysStore, 0) ;
}
}