all groups > dotnet security > february 2006 >
You're in the

dotnet security

group:

CertGetCertificateContextProperty problem on multiple calls



CertGetCertificateContextProperty problem on multiple calls Mitch Gallant
2/28/2006 4:26:56 PM
dotnet security: There is a problem in this KeyPal .NET application:
http://www.jensign.com/JavaScience/dotnet/keypal

This application uses Pinvoke to CryptoAPI to enumerate
capi keycontainers and detect which certs in the MY store
have an associated private key. SOurce code is at:
http://www.jensign.com/JavaScience/dotnet/keypal/source/KeyPal.txt

The problem seems to be in the following method:
private static Hashtable GetCertContainernames()

When this method is invoked a *second* time (from the R or Refresh
keyboard command in the app), the function called during the
certificate store enumeration:

Win32.CertGetCertificateContextProperty
(hCertCntxt, CERT_KEY_PROV_INFO_PROP_ID, IntPtr.Zero, ref provinfosize))

does not return the correct result for one cert that DOES have a private key.
The initial display however does return the correct result.

I think all the proper memory is being freed. Can anyone spot some code-problem?
It is reproducible (correct result on initial display of key/cert results, but misses
one certificate with private key on successive Refresh calls to that function.

- Mitch Gallant
MVP Security



Re: CertGetCertificateContextProperty problem on multiple calls Mitch Gallant
2/28/2006 4:54:49 PM
Forgot to add that the native error on 2nd (failed) invocation of
CertGetCertificateContextProperty(..)
is
Error code: 0x80004005
Error message: The data is invalid

- Mitch

[quoted text, click to view]

Re: CertGetCertificateContextProperty problem on multiple calls Mitch Gallant
2/28/2006 8:21:33 PM
OK, I have narrowed it down to the memory-freeing call:
Marshal.FreeHGlobal(pProvInfo);
in the GetCertContainernames() method.
If I comment out that line, successive invocations (within the program
loop) work properly.
So, WHY does Marshal.FreeHGlobal cause a problem in this method?

- Mitch

[quoted text, click to view]

AddThis Social Bookmark Button