I was testing loading public key and failed.
At first, I create a private key and public key using openssl as follow.
$ openssl genrsa -out test-priv.pem 4096
$ openssl req -subj '/C=US/ST=Texas/L=Fort Worth/O=ABC Corporation, Inc./OU=Software/CN=abc_corp.com' -x509 -nodes -key test-priv.pem -out test-public.pem -days 365 -addext keyUsage=digitalSignature -addext extendedKeyUsage=codeSigning,emailProtection
The test code is as follow.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <openssl/pem.h>
EVP_PKEY *load_pubkey(const char *file)
{
BIO *key = NULL;
EVP_PKEY *pkey = NULL;
if (file == NULL)
{
printf("no keyfile specifiedn");
goto end;
}
key = BIO_new(BIO_s_file());
if (key == NULL)
{
goto end;
}
if (BIO_read_filename(key, file) <= 0)
{
printf("Error opening : %sn", file);
goto end;
}
pkey = PEM_read_bio_PUBKEY(key, NULL, NULL, NULL);
end:
if (key != NULL)
BIO_free(key);
if (pkey == NULL)
printf("Unable to load key filename : %sn", file);
return (pkey);
}
int main(int argc, char *argv[])
{
load_pubkey((const char *)argv[1]);
return 0;
}
I compiled this test code like below.
$ gcc key_test.c -l ssl -l crypto -o key_test
Then run the program like below.
$ key_test test-public.pem
And the error message I got is “Unable to load key filename : test-public.pem”.
Do you see anything I did something wrong?
2