Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calling CryptImportKey with PInvoke

186 views
Skip to first unread message

mita patel

unread,
Jan 11, 2003, 7:22:20 PM1/11/03
to
I'm having trouble calling the native CryptImportKey API in .NET.

This is my C# signature:

[DllImport ("Advapi32.dll")] //or [DllImport ("coredll.dll")] for Compact
Framework

public static extern bool CryptImportKey(IntPtr hProv, byte[] pbKeyData,
UInt32 dwDataLen, IntPtr hPubKey, UInt32 dwFlags, ref IntPtr hKey);

My code is as follows:

pbPublicKey = ... (valid byte[]array representing a public key blob. This
was obtained from a CERT_INFO structure)

IntPtr hPublicKey = IntPtr.Zero;

result = CryptImportKey(hProv, pbPublicKey, (UInt32) pbPublicKey.Length,
IntPtr.Zero, 0, ref hPublicKey);

if( result == false)

{

lastError = GetLastError();

MessageBox.Show("CryptImportKey failed, last error " +
lastError.ToString());

}

The error I get is 126, Module not found. I am pretty sure that is a memory
issue, but I can't see what the problem is. Through debugging, I have
verified that all the parameters passed to CryptImportKey are correct. hProv
is a valid handle and pbPublicKey is a byte array with length 140. Is there
anything I'm missing??? I have been struggling with this for awhile, so I'd
really appreciate any suggestions!!!!

Thanks, Mita

Alex Feinman

unread,
Jan 11, 2003, 7:49:34 PM1/11/03
to
How do you obtain hProv?

"mita patel" <m2p...@engmail.uwaterloo.ca> wrote in message
news:eyHQmDduCHA.2568@TK2MSFTNGP12...

mita patel

unread,
Jan 11, 2003, 7:46:07 PM1/11/03
to
Actually when using the compact framework, I get NTE_BAD_VER.
I only get ERROR_MOD_NOT_FOUND on windows.


"mita patel" <m2p...@engmail.uwaterloo.ca> wrote in message
news:eyHQmDduCHA.2568@TK2MSFTNGP12...

mita patel

unread,
Jan 12, 2003, 4:16:48 PM1/12/03
to
I obtain hProv by calling CryptAcquireContext, setting the provider name and
container name to null.

My goal is to write a client/server app which allows the client to sign data
which is verified on the server side.

On the client side (which uses the Compact Framework), I call the following
functions:

1.CertOpenStore - which returns a handle to the "MY" certificate store
2.CertEnumCertificatesInStore - this is how i obtain the desired certificate
3.CryptAcquireCertificatePrivateKey
4.CryptCreateHash
5.CryptHashData
6.CryptSignHash
7.SendData(data, signature, publicKeyBlob) - this is the web service API

On the server side, SendData(data, signature, publicKeyBlob) calls the
following:

1.CryptAcquireContext - this gets the default CSP
2.CryptImportKey - *** this is where i'm stuck *****
3.CryptCreateHash
4.CryptHashData
5.CryptVerifySignature

Right now, I do not have a server side in place. I am just trying to get
all the functions to work on the client side. So SendData() is called
locally on the client. I am trying to import the key obtained from the
certificate into the default CSP, since that is what will be done on the
server. For some reason, CryptImportKey fails even though i'm passing it a
valid public key blob that was obtained from a certificate (returns error
NTE_BAD_VER). I tried running the code in a .NET Windows App (as opposed to
the CF) and the error that is returned is ERROR_MOD_NOT_FOUND.

Please let me know if you have any more questions. Thanks!

Mita


"Alex Feinman" <publi...@alexfeinman.com> wrote in message
news:#Hp4wRduCHA.2596@TK2MSFTNGP12...

David Stucki [MS]

unread,
Jan 13, 2003, 1:19:28 PM1/13/03
to
How exactly are you calling GetLastError? If you need to obtain the Win32
error code resulting from an API call you must set the SetLastError=true
property in the DllImport definition and then call
Marshal.GetLastWin32Error. Any other way will give you wrong error codes
since the Framework uses the Win32 APIs all over in it's implementation.
So basically the 126 "Module not found" error is likely a result of the
framework's calls not the error code you're looking for.

David Stucki
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Joe G

unread,
Feb 27, 2003, 9:38:00 AM2/27/03
to
test

"mita patel" <m2p...@engmail.uwaterloo.ca> wrote in message
news:eyHQmDduCHA.2568@TK2MSFTNGP12...
0 new messages