> I am trying to get the certificate into the "My" store. From what I
> understand, PFXImportCertStore imports the certificates in the pfx
[quoted text clipped - 35 lines]
> > >
> > > Larry
Under this project's constraints, I can't use CAPICOM.
So, yes, I could use CertAddCertificateContextToStore(),
but that requires a context, which I would need to use
then CertCreateCertificateContext, which takes me back to
my original posting. I need help with
CertCreateCertificateContext in C# (MSDN examples are in
C++). Here is some of the code I am currently using:
// DllImport declaration
[DllImport("crypt32.dll", SetLastError=true)]
public static extern IntPtr CertCreateCertificateContext(
int dwCertEncodingType,
IntPtr pbCertEncoded,
int cbCertEncoded);
// at this point, the "My" store is already opened
// read certificate
string fileName = @"C:\TestCert.pfx";
FileInfo fileinfo = new FileInfo(fileName);
BinaryReader br = new BinaryReader(fileinfo.OpenRead());
byte[] Bytes = new byte[fileinfo.Length];
br.Read(Bytes,0,(int)fileinfo.Length);
br.Close();
// We need to marshal the byte array Bytes into a pointer
IntPtr buffer = Marshal.AllocHGlobal(Bytes.Length);
Marshal.Copy(Bytes,0,buffer,Bytes.Length);
hCertCntxt = CertCreateCertificateContext
(X509_ASN_ENCODING, buffer, Bytes.Length);
if(hCertCntxt.ToInt32() != 0)
{
System.Windows.Forms.MessageBox.Show("got
context");
}
else
{
int iErrorCode = Marshal.GetLastWin32Error();
Win32Exception ex = new Win32Exception
(iErrorCode);
string errMsg = ex.Message;
System.Windows.Forms.MessageBox.Show
(errMsg, "Error Message");
}
Thanks for your time and help.
>-----Original Message-----
>You can use the capi CertAddCertificateContextToStore() to copy
[quoted text clipped - 55 lines]
>
>.
Michel Gallant - 24 Oct 2003 19:40 GMT
I don't think you can use CertCreateCertificateContext() directly
with a **pfx** blob.
What you need to do is:
(1) Use PFXImportCertStore() to get the certs into a transient memory store (which creates a
keycontainer for pfk automatically)
(2) Do a search on that memory store (using the returned memstore handle) by
enumerating the certs therein using CertEnumCertificatesInStore(..)
(3) Check if the enumerated cert has a matching private key (the pfx might contain several
root-subroot certs):
CertGetCertificateContextProperty(hCertCntxt, CERT_KEY_PROV_INFO_PROP_ID ...)
(4) Add THAT cert context to the MY store using CertAddCertificateContextToStore(..)
- Michel Gallant
> Under this project's constraints, I can't use CAPICOM.
> So, yes, I could use CertAddCertificateContextToStore(),
[quoted text clipped - 122 lines]
> >
> >.
Larry - 29 Oct 2003 21:44 GMT
This is what I figured I would have to do and it works great! Thanks
a lot for your time and help Michel!
Larry
> I don't think you can use CertCreateCertificateContext() directly
> with a **pfx** blob.
[quoted text clipped - 135 lines]
> > >
> > >.
Michel Gallant - 24 Oct 2003 19:49 GMT
An alternate easier declaration is to simply use a byte[] instead of a IntPtr.
Less code; no memory cleanup required.
[DllImport("crypt32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern uint CertCreateCertificateContext(
uint dwCertEncodingType,
byte[] pbCertEncoded,
uint cbCertEncoded) ;
- Mitch
> Under this project's constraints, I can't use CAPICOM.
> So, yes, I could use CertAddCertificateContextToStore(),
[quoted text clipped - 10 lines]
> IntPtr pbCertEncoded,
> int cbCertEncoded);