I need to load a PEM encoded X.509 certificate into a Windows Crypto API context to use with C++. They are the ones that have -----BEGIN RSA XXX KEY----- and -----END RSA XXX KEY-----. I found examples for Python and .NET but they use specific functions I can’t relate to the plain Windows Crypto API.
I understand how to encrypt/decrypt once I’ve got a HCRYPTKEY.
BUT, I just don’t get how to import the Base64 blob in the .PEM file(s) and get a HCRYPTKEY that I can use out of it.
I have that strange feeling that there is more to it than simply calling CryptDecodeObject().
Any pointers that can put me on track? I’ve already lost two days doing “trial & error” programming and getting nowhere.
KJKHyperion said in his answer:
This sequence really helped me understand what’s going on, but it didn’t work for me as-is. The second call to
CryptDecodeObjectExgave me an error:“ASN.1 bad tag value met”.
After many attempts at understanding Microsoft documentation, I finally realized that the output of the fist decode cannot be decoded as ASN again, and that it is actually ready for import. With this understanding I found the answer in the following link:
http://www.ms-news.net/f2748/problem-importing-public-key-4052577.html
Following is my own program that imports a public key from a .pem file to a CryptApi context: