I try to load a public RSA key to my program. I use openssl lib in C. The key is defined in header crypt.h file:
#define PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n" \
"MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAuDcT2OiToi9edqil/3ha\n" \
"1MMrgqYvdYObRkb8C5SZtf8iv4LynuHi3hvEtybqFW84Uf2l3Dxuz3IWVp2v1C2f\n" \
"cUxwf+hFqL4Nt2Ww/XOKLtFXTSX/2fPYoelmLja2grHPSuFx6N4gtDUXRIGSYXIz\n" \
"c2GBx8lOiOP+qUguFmWZ9E0GiMLvh4XUQUzTGr4ZNNoc5LODdO0g7beFFqhntt9w\n" \
"EKdceGQtA7Zyt5l13x0lj+RONdPJkDFZrNGdqDwNUSog9mufpvR1P2UW09pC+lzy\n" \
"z32P+w0U3Za4zv4Btis9ky16vaqdN/KlDWJRt+4W9TQSAL0x9w708OQr0Sf2CXNq\n" \
"EobSLZ/aybe75yQmzFqmn10X+NuwTtJkArIKK7JONCBSxjohQmTZw0U497TCEHia\n" \
"itzgsHNLf1of31G/3GK5rCkm9fl39nnrg0yJi1cONTDjSHzKlrPhA584jFRD0CIO\n" \
"VNHYgsVLuFQuJ0WkZON8uEZELXN1ZyWnmPXH8wSwxjud65JD4JSQornCXorBMfxd\n" \
"DFEeeNk8tjgPMaCTLFNP/gfVDzkvcct9RoC8uZxHk8zgrGOwuxOZ/ZyihE7M8v0i\n" \
"+VwG9iMrPl8W3+KqunIt/FB4Le1vJ0yYorK64DRNdeMAIsYq2iFWO3GXNsX631/K\n" \
"EuZJS8tGNhK9dF5umo0GceUCAwEAAQ==\n" \
"-----END PUBLIC KEY-----\n"
The RSA key is supposed to be saved in RSA * structure. I use a global variable for that.
RSA *rsaPubkey;
The main() calls a function that will take PUBLIC_KEY and pass it to rsaPubkey. The function is shown bellow:
#include "crypt.h"
int EVP_PKEY_get_type(EVP_PKEY *pkey) {
if (!pkey)
return EVP_PKEY_NONE;
return EVP_PKEY_type(pkey->type);
}
int PublicRSAKeyToStruct(const char *pubKey) {
BIO* bio;
bio = BIO_new_mem_buf(pubKey, (int)sizeof(pubKey));
if (bio == NULL) {
printf("bio");
return -1;
}
EVP_PKEY* pkey;
PEM_read_bio_PUBKEY(bio, &pkey, NULL, NULL);
if (pkey == NULL) {
printf("pkey");
return -1;
}
int type = EVP_PKEY_get_type(pkey);
if (type != EVP_PKEY_RSA && type != EVP_PKEY_RSA2) {
printf("get type");
return -1;
}
rsaPubkey = EVP_PKEY_get1_RSA(pkey);
if (rsaPubkey == NULL) {
printf("pubkey");
return -1;
}
EVP_PKEY_free(pkey);
BIO_free(bio);
return 0;
}
We call the function like that:
status = PublicRSAKeyToStruct(PUBLIC_KEY);
if (status == -1) {
exit(EXIT_FAILURE);
}
My problem is that the PublicRSAKeyToStruct() function throws me the bellow user defined error:
get type
This means that we failed to pass EVP_PKEY_get_type(). I can't understand why this happen though. I have also no clue if the problem is on the previous steps.
typewhen the error occurs?