473,810 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

I'm trying to call CertFindCertifi cateInStore 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.Diagnost ics;
using System.Text;
using System.Runtime. InteropServices ;
using System.Security .Cryptography.X 509Certificates ;
using System.Componen tModel;

public class WinCapi
{

/*
HCERTSTORE WINAPI CertOpenSystemS tore(HCRYPTPROV hprov, LPTCSTR
szSubsystemProt ocol);
BOOL WINAPI CertCloseStore( HCERTSTORE hCertStore, DWORD dwFlags);

PCCERT_CONTEXT WINAPI CertFindCertifi cateInStore(
HCERTSTORE hCertStore,
DWORD dwCertEncodingT ype,
DWORD dwFindFlags,
DWORD dwFindType,
const void* pvFindPara,
PCCERT_CONTEXT pPrevCertContex t);

BOOL WINAPI CertFreeCertifi cateContext(
PCCERT_CONTEXT pCertContext
);

typedef struct _CTL_USAGE {
DWORD cUsageIdentifie r;
LPSTR *rgpszUsageIden tifier; // array of pszObjId
} CTL_USAGE, *PCTL_USAGE, CERT_ENHKEY_USA GE, *PCERT_ENHKEY_U SAGE;

*/
[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Auto)]
public struct CRYPT_OID_INFO
{
public uint cbSize;
[MarshalAs(Unman agedType.LPStr)] public String pszOID;
[MarshalAs(Unman agedType.LPWStr )]public String pwszName;
public uint dwGroupID;
public uint dwValue;
public int cbData; //ExtraInfo blob
public IntPtr pbData;
}

[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Auto)]
public struct _CTL_USAGE
{
public int cUsageIdentifie r;
[MarshalAs(Unman agedType.ByValA rray)]
public string[] rgpszUseageIden tifier;
}

[DllImport("cryp t32.dll", CharSet=CharSet .Auto, SetLastError=tr ue)]
public static extern IntPtr CertOpenSystemS tore(
IntPtr hCryptProv,
string storename) ;

[DllImport("cryp t32.dll", SetLastError=tr ue)]
public static extern bool CertCloseStore(
IntPtr hCertStore,
uint dwFlags) ;

[DllImport("cryp t32.dll", SetLastError=tr ue)]
public static extern IntPtr CertFindCertifi cateInStore(
IntPtr hCertStore,
uint dwCertEncodingT ype,
uint dwFindFlags,
uint dwFindType,
ref WinCapi._CTL_US AGE pvFindPara,
IntPtr pPrevCertCntxt) ;
[DllImport("cryp t32.dll", SetLastError=tr ue)]
public static extern bool CertFreeCertifi cateContext(
IntPtr hCertStore) ;

}

public class SimpleCert
{
const string szOID_PKIX_KP_C ODE_SIGNING = "1.3.6.1.5.5.7. 3.3";
const string MY = "MY";
const string OTHERS = "AddressBoo k";
const uint PKCS_7_ASN_ENCO DING = 0x00010000;
const uint X509_ASN_ENCODI NG = 0x00000001;
const uint CERT_FIND_SUBJE CT_STR = 0x00080007;
const uint CERT_FIND_ENHKE Y_USAGE = 0x000A0000;

static uint MY_ENCODING_TYP E = PKCS_7_ASN_ENCO DING | X509_ASN_ENCODI NG ;

public static void Main()
{
IntPtr hSysStore = IntPtr.Zero;
IntPtr hCertCntxt = IntPtr.Zero;
IntPtr hStructure = IntPtr.Zero;

WinCapi._CTL_US AGE test = new WinCapi._CTL_US AGE();

string[] strTest = new string[1];
strTest[0] = szOID_PKIX_KP_C ODE_SIGNING;

hSysStore = WinCapi.CertOpe nSystemStore(In tPtr.Zero, MY) ;
Console.WriteLi ne("Store Handle:\t0x{0:X }", hSysStore.ToInt 32());

test.cUsageIden tifier = 1;
test.rgpszUseag eIdentifier = new string[1];
test.rgpszUseag eIdentifier[0] = szOID_PKIX_KP_C ODE_SIGNING;
if(hSysStore != IntPtr.Zero)
{
hCertCntxt=WinC api.CertFindCer tificateInStore (
hSysStore,
MY_ENCODING_TYP E,
0,
CERT_FIND_ENHKE Y_USAGE,
ref test,
IntPtr.Zero) ;

Debug.WriteLine (Marshal.GetLas tWin32Error().T oString());

while(hCertCntx t != IntPtr.Zero)
{
Console.WriteLi ne("CertContext :\t0x{0:X}", hCertCntxt.ToIn t32()) ;
X509Certificate foundcert = new X509Certificate (hCertCntxt);
Console.WriteLi ne("\nFound certificate with SubjectName string
\"{0}\"",lpszCe rtSubject);
Console.WriteLi ne("SubjectName :\t{0}", foundcert.GetNa me());
Console.WriteLi ne("Serial No:\t{0}", foundcert.GetSe rialNumberStrin g());
Console.WriteLi ne("HashString: \t{0}" , foundcert.GetCe rtHashString()) ;
Console.WriteLi ne("PublicKey:\ t{0}",foundcert .GetPublicKey() .ToString());
Console.WriteLi ne("Issuer:\t {0}",foundcert. GetIssuerName() );

hCertCntxt=WinC api.CertFindCer tificateInStore (
hSysStore,
MY_ENCODING_TYP E,
0,
CERT_FIND_ENHKE Y_USAGE,
ref test,
hCertCntxt) ;
}
}

//------- Clean Up -----------
if(hCertCntxt != IntPtr.Zero)
WinCapi.CertFre eCertificateCon text(hCertCntxt );
if(hSysStore != IntPtr.Zero)
WinCapi.CertClo seStore(hSysSto re, 0) ;
}

}
Nov 17 '05 #1
3 5507
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**********@d iscussions.micr osoft.com> wrote in message
news:6C******** *************** ***********@mic rosoft.com...
I'm trying to call CertFindCertifi cateInStore 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.Diagnost ics;
using System.Text;
using System.Runtime. InteropServices ;
using System.Security .Cryptography.X 509Certificates ;
using System.Componen tModel;

public class WinCapi
{

/*
HCERTSTORE WINAPI CertOpenSystemS tore(HCRYPTPROV hprov, LPTCSTR
szSubsystemProt ocol);
BOOL WINAPI CertCloseStore( HCERTSTORE hCertStore, DWORD dwFlags);

PCCERT_CONTEXT WINAPI CertFindCertifi cateInStore(
HCERTSTORE hCertStore,
DWORD dwCertEncodingT ype,
DWORD dwFindFlags,
DWORD dwFindType,
const void* pvFindPara,
PCCERT_CONTEXT pPrevCertContex t);

BOOL WINAPI CertFreeCertifi cateContext(
PCCERT_CONTEXT pCertContext
);

typedef struct _CTL_USAGE {
DWORD cUsageIdentifie r;
LPSTR *rgpszUsageIden tifier; // array of pszObjId
} CTL_USAGE, *PCTL_USAGE, CERT_ENHKEY_USA GE, *PCERT_ENHKEY_U SAGE;

*/
[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Auto)]
public struct CRYPT_OID_INFO
{
public uint cbSize;
[MarshalAs(Unman agedType.LPStr)] public String pszOID;
[MarshalAs(Unman agedType.LPWStr )]public String pwszName;
public uint dwGroupID;
public uint dwValue;
public int cbData; //ExtraInfo blob
public IntPtr pbData;
}

[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Auto)]
public struct _CTL_USAGE
{
public int cUsageIdentifie r;
[MarshalAs(Unman agedType.ByValA rray)]
public string[] rgpszUseageIden tifier;
}

[DllImport("cryp t32.dll", CharSet=CharSet .Auto, SetLastError=tr ue)]
public static extern IntPtr CertOpenSystemS tore(
IntPtr hCryptProv,
string storename) ;

[DllImport("cryp t32.dll", SetLastError=tr ue)]
public static extern bool CertCloseStore(
IntPtr hCertStore,
uint dwFlags) ;

[DllImport("cryp t32.dll", SetLastError=tr ue)]
public static extern IntPtr CertFindCertifi cateInStore(
IntPtr hCertStore,
uint dwCertEncodingT ype,
uint dwFindFlags,
uint dwFindType,
ref WinCapi._CTL_US AGE pvFindPara,
IntPtr pPrevCertCntxt) ;
[DllImport("cryp t32.dll", SetLastError=tr ue)]
public static extern bool CertFreeCertifi cateContext(
IntPtr hCertStore) ;

}

public class SimpleCert
{
const string szOID_PKIX_KP_C ODE_SIGNING = "1.3.6.1.5.5.7. 3.3";
const string MY = "MY";
const string OTHERS = "AddressBoo k";
const uint PKCS_7_ASN_ENCO DING = 0x00010000;
const uint X509_ASN_ENCODI NG = 0x00000001;
const uint CERT_FIND_SUBJE CT_STR = 0x00080007;
const uint CERT_FIND_ENHKE Y_USAGE = 0x000A0000;

static uint MY_ENCODING_TYP E = PKCS_7_ASN_ENCO DING | X509_ASN_ENCODI NG
;

public static void Main()
{
IntPtr hSysStore = IntPtr.Zero;
IntPtr hCertCntxt = IntPtr.Zero;
IntPtr hStructure = IntPtr.Zero;

WinCapi._CTL_US AGE test = new WinCapi._CTL_US AGE();

string[] strTest = new string[1];
strTest[0] = szOID_PKIX_KP_C ODE_SIGNING;

hSysStore = WinCapi.CertOpe nSystemStore(In tPtr.Zero, MY) ;
Console.WriteLi ne("Store Handle:\t0x{0:X }", hSysStore.ToInt 32());

test.cUsageIden tifier = 1;
test.rgpszUseag eIdentifier = new string[1];
test.rgpszUseag eIdentifier[0] = szOID_PKIX_KP_C ODE_SIGNING;
if(hSysStore != IntPtr.Zero)
{
hCertCntxt=WinC api.CertFindCer tificateInStore (
hSysStore,
MY_ENCODING_TYP E,
0,
CERT_FIND_ENHKE Y_USAGE,
ref test,
IntPtr.Zero) ;

Debug.WriteLine (Marshal.GetLas tWin32Error().T oString());

while(hCertCntx t != IntPtr.Zero)
{
Console.WriteLi ne("CertContext :\t0x{0:X}", hCertCntxt.ToIn t32()) ;
X509Certificate foundcert = new X509Certificate (hCertCntxt);
Console.WriteLi ne("\nFound certificate with SubjectName string
\"{0}\"",lpszCe rtSubject);
Console.WriteLi ne("SubjectName :\t{0}", foundcert.GetNa me());
Console.WriteLi ne("Serial No:\t{0}", foundcert.GetSe rialNumberStrin g());
Console.WriteLi ne("HashString: \t{0}" , foundcert.GetCe rtHashString()) ;
Console.WriteLi ne("PublicKey:\ t{0}",foundcert .GetPublicKey() .ToString());
Console.WriteLi ne("Issuer:\t {0}",foundcert. GetIssuerName() );

hCertCntxt=WinC api.CertFindCer tificateInStore (
hSysStore,
MY_ENCODING_TYP E,
0,
CERT_FIND_ENHKE Y_USAGE,
ref test,
hCertCntxt) ;
}
}

//------- Clean Up -----------
if(hCertCntxt != IntPtr.Zero)
WinCapi.CertFre eCertificateCon text(hCertCntxt );
if(hSysStore != IntPtr.Zero)
WinCapi.CertClo seStore(hSysSto re, 0) ;
}

}

Nov 17 '05 #2
Thanks Ollie, however that doesn't really help me as I'm having to pass the
CTL_USAGE structure to pzFindPara of the CertFindCertifi cateInStore. 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**********@d iscussions.micr osoft.com> wrote in message
news:6C******** *************** ***********@mic rosoft.com...
I'm trying to call CertFindCertifi cateInStore 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.Diagnost ics;
using System.Text;
using System.Runtime. InteropServices ;
using System.Security .Cryptography.X 509Certificates ;
using System.Componen tModel;

public class WinCapi
{

/*
HCERTSTORE WINAPI CertOpenSystemS tore(HCRYPTPROV hprov, LPTCSTR
szSubsystemProt ocol);
BOOL WINAPI CertCloseStore( HCERTSTORE hCertStore, DWORD dwFlags);

PCCERT_CONTEXT WINAPI CertFindCertifi cateInStore(
HCERTSTORE hCertStore,
DWORD dwCertEncodingT ype,
DWORD dwFindFlags,
DWORD dwFindType,
const void* pvFindPara,
PCCERT_CONTEXT pPrevCertContex t);

BOOL WINAPI CertFreeCertifi cateContext(
PCCERT_CONTEXT pCertContext
);

typedef struct _CTL_USAGE {
DWORD cUsageIdentifie r;
LPSTR *rgpszUsageIden tifier; // array of pszObjId
} CTL_USAGE, *PCTL_USAGE, CERT_ENHKEY_USA GE, *PCERT_ENHKEY_U SAGE;

*/
[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Auto)]
public struct CRYPT_OID_INFO
{
public uint cbSize;
[MarshalAs(Unman agedType.LPStr)] public String pszOID;
[MarshalAs(Unman agedType.LPWStr )]public String pwszName;
public uint dwGroupID;
public uint dwValue;
public int cbData; //ExtraInfo blob
public IntPtr pbData;
}

[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Auto)]
public struct _CTL_USAGE
{
public int cUsageIdentifie r;
[MarshalAs(Unman agedType.ByValA rray)]
public string[] rgpszUseageIden tifier;
}

[DllImport("cryp t32.dll", CharSet=CharSet .Auto, SetLastError=tr ue)]
public static extern IntPtr CertOpenSystemS tore(
IntPtr hCryptProv,
string storename) ;

[DllImport("cryp t32.dll", SetLastError=tr ue)]
public static extern bool CertCloseStore(
IntPtr hCertStore,
uint dwFlags) ;

[DllImport("cryp t32.dll", SetLastError=tr ue)]
public static extern IntPtr CertFindCertifi cateInStore(
IntPtr hCertStore,
uint dwCertEncodingT ype,
uint dwFindFlags,
uint dwFindType,
ref WinCapi._CTL_US AGE pvFindPara,
IntPtr pPrevCertCntxt) ;
[DllImport("cryp t32.dll", SetLastError=tr ue)]
public static extern bool CertFreeCertifi cateContext(
IntPtr hCertStore) ;

}

public class SimpleCert
{
const string szOID_PKIX_KP_C ODE_SIGNING = "1.3.6.1.5.5.7. 3.3";
const string MY = "MY";
const string OTHERS = "AddressBoo k";
const uint PKCS_7_ASN_ENCO DING = 0x00010000;
const uint X509_ASN_ENCODI NG = 0x00000001;
const uint CERT_FIND_SUBJE CT_STR = 0x00080007;
const uint CERT_FIND_ENHKE Y_USAGE = 0x000A0000;

static uint MY_ENCODING_TYP E = PKCS_7_ASN_ENCO DING | X509_ASN_ENCODI NG
;

public static void Main()
{
IntPtr hSysStore = IntPtr.Zero;
IntPtr hCertCntxt = IntPtr.Zero;
IntPtr hStructure = IntPtr.Zero;

WinCapi._CTL_US AGE test = new WinCapi._CTL_US AGE();

string[] strTest = new string[1];
strTest[0] = szOID_PKIX_KP_C ODE_SIGNING;

hSysStore = WinCapi.CertOpe nSystemStore(In tPtr.Zero, MY) ;
Console.WriteLi ne("Store Handle:\t0x{0:X }", hSysStore.ToInt 32());

test.cUsageIden tifier = 1;
test.rgpszUseag eIdentifier = new string[1];
test.rgpszUseag eIdentifier[0] = szOID_PKIX_KP_C ODE_SIGNING;
if(hSysStore != IntPtr.Zero)
{
hCertCntxt=WinC api.CertFindCer tificateInStore (
hSysStore,
MY_ENCODING_TYP E,
0,
CERT_FIND_ENHKE Y_USAGE,
ref test,
IntPtr.Zero) ;

Debug.WriteLine (Marshal.GetLas tWin32Error().T oString());

while(hCertCntx t != IntPtr.Zero)
{
Console.WriteLi ne("CertContext :\t0x{0:X}", hCertCntxt.ToIn t32()) ;
X509Certificate foundcert = new X509Certificate (hCertCntxt);
Console.WriteLi ne("\nFound certificate with SubjectName string
\"{0}\"",lpszCe rtSubject);
Console.WriteLi ne("SubjectName :\t{0}", foundcert.GetNa me());
Console.WriteLi ne("Serial No:\t{0}", foundcert.GetSe rialNumberStrin g());
Console.WriteLi ne("HashString: \t{0}" , foundcert.GetCe rtHashString()) ;
Console.WriteLi ne("PublicKey:\ t{0}",foundcert .GetPublicKey() .ToString());
Console.WriteLi ne("Issuer:\t {0}",foundcert. GetIssuerName() );

hCertCntxt=WinC api.CertFindCer tificateInStore (
hSysStore,
MY_ENCODING_TYP E,
0,
CERT_FIND_ENHKE Y_USAGE,
ref test,
hCertCntxt) ;
}
}

//------- Clean Up -----------
if(hCertCntxt != IntPtr.Zero)
WinCapi.CertFre eCertificateCon text(hCertCntxt );
if(hSysStore != IntPtr.Zero)
WinCapi.CertClo seStore(hSysSto re, 0) ;
}

}


Nov 17 '05 #3
Charles,

Have you read this page on MSDN:

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

HTH

Ollie Riches

"Charles Denny" <Ch**********@d iscussions.micr osoft.com> wrote in message
news:33******** *************** ***********@mic rosoft.com...
Thanks Ollie, however that doesn't really help me as I'm having to pass
the
CTL_USAGE structure to pzFindPara of the CertFindCertifi cateInStore. 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**********@d iscussions.micr osoft.com> wrote in message
news:6C******** *************** ***********@mic rosoft.com...
> I'm trying to call CertFindCertifi cateInStore 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.Diagnost ics;
> using System.Text;
> using System.Runtime. InteropServices ;
> using System.Security .Cryptography.X 509Certificates ;
> using System.Componen tModel;
>
> public class WinCapi
> {
>
> /*
> HCERTSTORE WINAPI CertOpenSystemS tore(HCRYPTPROV hprov, LPTCSTR
> szSubsystemProt ocol);
> BOOL WINAPI CertCloseStore( HCERTSTORE hCertStore, DWORD dwFlags);
>
> PCCERT_CONTEXT WINAPI CertFindCertifi cateInStore(
> HCERTSTORE hCertStore,
> DWORD dwCertEncodingT ype,
> DWORD dwFindFlags,
> DWORD dwFindType,
> const void* pvFindPara,
> PCCERT_CONTEXT pPrevCertContex t);
>
> BOOL WINAPI CertFreeCertifi cateContext(
> PCCERT_CONTEXT pCertContext
> );
>
> typedef struct _CTL_USAGE {
> DWORD cUsageIdentifie r;
> LPSTR *rgpszUsageIden tifier; // array of
> pszObjId
> } CTL_USAGE, *PCTL_USAGE, CERT_ENHKEY_USA GE, *PCERT_ENHKEY_U SAGE;
>
> */
>
>
> [StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Auto)]
> public struct CRYPT_OID_INFO
> {
> public uint cbSize;
> [MarshalAs(Unman agedType.LPStr)] public String pszOID;
> [MarshalAs(Unman agedType.LPWStr )]public String pwszName;
> public uint dwGroupID;
> public uint dwValue;
> public int cbData; //ExtraInfo blob
> public IntPtr pbData;
> }
>
> [StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Auto)]
> public struct _CTL_USAGE
> {
> public int cUsageIdentifie r;
> [MarshalAs(Unman agedType.ByValA rray)]
> public string[] rgpszUseageIden tifier;
> }
>
> [DllImport("cryp t32.dll", CharSet=CharSet .Auto, SetLastError=tr ue)]
> public static extern IntPtr CertOpenSystemS tore(
> IntPtr hCryptProv,
> string storename) ;
>
> [DllImport("cryp t32.dll", SetLastError=tr ue)]
> public static extern bool CertCloseStore(
> IntPtr hCertStore,
> uint dwFlags) ;
>
> [DllImport("cryp t32.dll", SetLastError=tr ue)]
> public static extern IntPtr CertFindCertifi cateInStore(
> IntPtr hCertStore,
> uint dwCertEncodingT ype,
> uint dwFindFlags,
> uint dwFindType,
> ref WinCapi._CTL_US AGE pvFindPara,
> IntPtr pPrevCertCntxt) ;
>
>
> [DllImport("cryp t32.dll", SetLastError=tr ue)]
> public static extern bool CertFreeCertifi cateContext(
> IntPtr hCertStore) ;
>
> }
>
> public class SimpleCert
> {
> const string szOID_PKIX_KP_C ODE_SIGNING = "1.3.6.1.5.5.7. 3.3";
> const string MY = "MY";
> const string OTHERS = "AddressBoo k";
> const uint PKCS_7_ASN_ENCO DING = 0x00010000;
> const uint X509_ASN_ENCODI NG = 0x00000001;
> const uint CERT_FIND_SUBJE CT_STR = 0x00080007;
> const uint CERT_FIND_ENHKE Y_USAGE = 0x000A0000;
>
> static uint MY_ENCODING_TYP E = PKCS_7_ASN_ENCO DING |
> X509_ASN_ENCODI NG
> ;
>
> public static void Main()
> {
> IntPtr hSysStore = IntPtr.Zero;
> IntPtr hCertCntxt = IntPtr.Zero;
> IntPtr hStructure = IntPtr.Zero;
>
> WinCapi._CTL_US AGE test = new WinCapi._CTL_US AGE();
>
> string[] strTest = new string[1];
> strTest[0] = szOID_PKIX_KP_C ODE_SIGNING;
>
> hSysStore = WinCapi.CertOpe nSystemStore(In tPtr.Zero, MY) ;
> Console.WriteLi ne("Store Handle:\t0x{0:X }", hSysStore.ToInt 32());
>
> test.cUsageIden tifier = 1;
> test.rgpszUseag eIdentifier = new string[1];
> test.rgpszUseag eIdentifier[0] = szOID_PKIX_KP_C ODE_SIGNING;
> if(hSysStore != IntPtr.Zero)
> {
> hCertCntxt=WinC api.CertFindCer tificateInStore (
> hSysStore,
> MY_ENCODING_TYP E,
> 0,
> CERT_FIND_ENHKE Y_USAGE,
> ref test,
> IntPtr.Zero) ;
>
> Debug.WriteLine (Marshal.GetLas tWin32Error().T oString());
>
> while(hCertCntx t != IntPtr.Zero)
> {
> Console.WriteLi ne("CertContext :\t0x{0:X}", hCertCntxt.ToIn t32()) ;
> X509Certificate foundcert = new X509Certificate (hCertCntxt);
> Console.WriteLi ne("\nFound certificate with SubjectName string
> \"{0}\"",lpszCe rtSubject);
> Console.WriteLi ne("SubjectName :\t{0}", foundcert.GetNa me());
> Console.WriteLi ne("Serial No:\t{0}",
> foundcert.GetSe rialNumberStrin g());
> Console.WriteLi ne("HashString: \t{0}" , foundcert.GetCe rtHashString()) ;
> Console.WriteLi ne("PublicKey:\ t{0}",foundcert .GetPublicKey() .ToString());
> Console.WriteLi ne("Issuer:\t {0}",foundcert. GetIssuerName() );
>
> hCertCntxt=WinC api.CertFindCer tificateInStore (
> hSysStore,
> MY_ENCODING_TYP E,
> 0,
> CERT_FIND_ENHKE Y_USAGE,
> ref test,
> hCertCntxt) ;
> }
> }
>
> //------- Clean Up -----------
> if(hCertCntxt != IntPtr.Zero)
> WinCapi.CertFre eCertificateCon text(hCertCntxt );
> if(hSysStore != IntPtr.Zero)
> WinCapi.CertClo seStore(hSysSto re, 0) ;
> }
>
> }


Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
13570
by: JPRoot | last post by:
Hi I wish to set/get a property using System.ComponentModel.ISynchronizeInvoke.Invoke but I cannot find the exact syntax.. Any clue how to do this (right now I am making GetEnabled/SetEnabled methods when I wish to get/set Form.Enabled property. Thank JPRoo
5
2138
by: RobbieK | last post by:
I am hoping someone can help with a strange problem - I am not very savvy with certificates, so bear with my ignorance in that area. I have an ASP.NET (1.1) application that calls a web service (non .net) over SSL. The owner of the web service asked for a certificate request that he would sign and return back (I used OpenSSL to create the CR). He provided the certificate and I imported it into my Local Machine\Personal certs. Things...
5
4455
by: | last post by:
Hi all, HttpWebRequest, and SoapHttpClientProtocol both expose a ClientCertificates property, which can hold multiple client certificates, but on the service side, it can only receive one client certificate, since it derives System.Web.Services.WebService class, and it's Context.Request.ClientCertificate is a single HttpClientCertificate object, is there a way to receive all the client certificates that is sent in the request? or does IIS...
0
2802
by: Rave | last post by:
This is a long shot, but I thought I'd try it. I am currently using excel as an inventory tool. I currently have a hand-held scanner plugged into a laptop for reading barcodes. Using the "Find and Replace" fuction, I scan the merchandise which then searches the spreadsheet for the matching inventory number. When it is found, I highlight that cell in yellow. After scanning and coloring all of my inventory I can then see exactly what is...
2
2664
by: b.fokke | last post by:
I'd like to connect to a webservice using TLS/SSL. I have two separate client certificates: 1. A certificate for digital verification 2. A certificate for encryption. When I use the first one (i.e. add it to the ClientCertificates collection of the web request), I can establish a connection but I get a WebException: "Underlying connection closed: Could not establish trust relationship". When I use the second one I can't even establish a...
23
2648
by: Thomas Due | last post by:
Hi, I have a class which monitors a TCP socket. This will on occasion raise an event which can be handled by a GUI. Now, I am aware of the if(InvokeRequire) { EventHandler d = new EventHandler(); Invoke(d, new object{sender, e}); } else {
3
5274
by: =?Utf-8?B?Sm9l?= | last post by:
I know that I have posted this question before, but it is still unresolved and I don't know where to turn to next. I have code that is creating a user (works fine), then sets the account flags (works fine) and then sets the password (fails). Here is the pertinent information: Ex.InnerException.Message: Logon failure: unknown user name or bad password. Ex.Message: Exception has been thrown by the target of an invocation.
5
1722
by: iLL | last post by:
So why is it that we need to use the Invoke method when updating the GUI? The following is the way we are suppose to update GUI components: delegate void textIt(object o); public partial class Form1 : Form { public Form1() {
4
1744
by: James | last post by:
Hello everyone, While loading a page (http://www.edmonton.ca/portal/server.pt?space=CommunityPage&control=SetCommunity&CommunityID=239) into a webbrowser control I use invokemember on the 'onclick' event from 'cmdBusStopScheduleSubmit' element - see below. ONCLICK ========= <input type="submit" name="cmdBusStopScheduleSubmit" value="Get Bus Stop
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10644
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4334
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.