By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,269 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,269 IT Pros & Developers. It's quick & easy.

Using P/Invoke to find Certificates with specific Enhanced Key Usa

P: n/a
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().ToStri ng());

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.GetP ublicKey().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) ;
}

}
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Have you been to pinvoke.net they have an example for exactly what you are
trying to do:

http://pinvoke.net/default.aspx/cryp...ificateInStore

In fact it is the best place for an interop related API issues IMHO.

HTH

Ollie Riches

"Charles Denny" <Ch**********@discussions.microsoft.com> wrote in message
news:6C**********************************@microsof t.com...
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().ToStri ng());

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.GetP ublicKey().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) ;
}

}

Nov 17 '05 #2

P: n/a
Thanks Ollie, however that doesn't really help me as I'm having to pass the
CTL_USAGE structure to pzFindPara of the CertFindCertificateInStore. This
structure has an array of strings. I need to be able to successfully marshal
the CTL_USAGE structure so that I can search for Enhanced Key Usage OIDs
rather than the subject name that is given in the example.

The problem with the website you just gave me is that pszFindPara can be one
of a number of types, and they only have an example for one of those types.
:) I suppose if I could work out how to get a IntPtr assigned to the
structure, I could try just passing that.
Regards,
Charles
"Ollie Riches" wrote:
Have you been to pinvoke.net they have an example for exactly what you are
trying to do:

http://pinvoke.net/default.aspx/cryp...ificateInStore

In fact it is the best place for an interop related API issues IMHO.

HTH

Ollie Riches

"Charles Denny" <Ch**********@discussions.microsoft.com> wrote in message
news:6C**********************************@microsof t.com...
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().ToStri ng());

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.GetP ublicKey().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) ;
}

}


Nov 17 '05 #3

P: n/a
Charles,

Have you read this page on MSDN:

http://msdn.microsoft.com/library/de...gForArrays.asp

HTH

Ollie Riches

"Charles Denny" <Ch**********@discussions.microsoft.com> wrote in message
news:33**********************************@microsof t.com...
Thanks Ollie, however that doesn't really help me as I'm having to pass
the
CTL_USAGE structure to pzFindPara of the CertFindCertificateInStore. This
structure has an array of strings. I need to be able to successfully
marshal
the CTL_USAGE structure so that I can search for Enhanced Key Usage OIDs
rather than the subject name that is given in the example.

The problem with the website you just gave me is that pszFindPara can be
one
of a number of types, and they only have an example for one of those
types.
:) I suppose if I could work out how to get a IntPtr assigned to the
structure, I could try just passing that.
Regards,
Charles
"Ollie Riches" wrote:
Have you been to pinvoke.net they have an example for exactly what you
are
trying to do:

http://pinvoke.net/default.aspx/cryp...ificateInStore

In fact it is the best place for an interop related API issues IMHO.

HTH

Ollie Riches

"Charles Denny" <Ch**********@discussions.microsoft.com> wrote in message
news:6C**********************************@microsof t.com...
> 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().ToStri ng());
>
> 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.GetP ublicKey().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) ;
> }
>
> }


Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.