CryptAcquireContextA fails in C++ CLR for "Aloaha Cryptographic Provider"
Asked Answered
E

1

0

I have C++ CLR project with both managed and unmanaged code. In this project I loop through list of cryptoproviders and search for suitable.

So I use functions
CryptEnumProvidersA
CryptAcquireContextA

I have C# project which uses C++ project. When I call the method to loop through list of cryptoproviders twice, my application crashes. And it crashes on CryptAcquireContextA. And only on "Aloaha Cryptographic Provider".

When i use debug mode, it also crashes. But when i use debug mode, set breakpoint and pass through code with F10, it works fine.

When i carry out C++ code to separate project, it works fine in all cases.

So, I have two questions here:
1) How such a crash possible? And why it happens?
2) How can I call CryptAcquireContextA to handle such a crash?

My considerations.
1) Some troubles with memory. I was searching for memory leaks, but I didn't find any.
2) I tried to use try-catch, but with no result, CryptAcquireContextA crashes anyway. I would like to use function like "CryptIsValidProvider" or "CryptPingProvider", but I didn't find it.

Evocator answered 29/12, 2011 at 11:10 Comment(0)
P
0

If it works when single-stepping, it's probably a race condition (you're paused in the debugger long enough for some other process or driver to make progress).

Does it matter how much code you single-step through? Does putting a Sleep instead of a breakpoint right before the CryptAcquireContextA call also work?

Proximity answered 29/12, 2011 at 18:52 Comment(3)
I put 'Sleep' after provider is found, and code works fine. Looks like it's really race condition. Now, is there more elegant decision to handle that? I use 'CryptReleaseContext' after 'CryptAcquireContextA'. But it seems that 'CryptReleaseContext' doesn't free handle or something else immediately, so 'CryptAcquireContextA' fails during next call.Evocator
You can complain to the developer of that CSP, but even if they pay attention, you'll most likely have to ship with a Sleep call unless you can ensure that all your users will have the latest patched version. Be sure to leave a detailed comment why the Sleep call is there, so someone doesn't end up removing it during maintenance.Proximity
I discovered that in case of “Aloaha Cryptographic Provider” CryptAcquireContextA returns NULL handle to cryptoprovider. So my decision is to call Sleep if handle is NULL. Thanks.Evocator

© 2022 - 2024 — McMap. All rights reserved.