Hello,

I'm working on an application where I'd like to have the public key stored in a local variable and compiled with the app, but I'm getting a runtime error when I try to read the public key after it has been stored in a BIO_mem_buf. I've tried two different ways, but I get the same error each time. If I read the same public key from a file using PEM_read_RSA_PUBKEY(), it works fine. I generated a private/ public key pair to test with. Here is an example I've tried:

static char *pubKey = "-----BEGIN PUBLIC KEY----- \nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvbB1yh71tGgX5wQ5PbYR+2V9K \nbmCqcVjbKdcsR9u91dfsXEeH+17kr6TPy2HQcAn1wk5jos8B78bSRLV8STs54Teq \nPa6Z4JrLim89+zgaxbS5k9W87oP06BZ2suf6fBB2M296UJbzBqSYCVTsxbEHQDbP\n/ hSGRtueV5668F8qzwIDAQAB\n-----END PUBLIC KEY-----\n";

BIO *pub_bio = BIO_new_mem_buf(pubKey, sizeof(pubKey);
if(pub_bio == NULL)
{
        ERR_print_errors_fp(stdout);
        return -1;
}

// rsaPubKey was previously initialized with RSA_new()
rsaPubKey = PEM_read_bio_RSAPublicKey(pub_bio, &rsaPubKey, NULL, NULL);
if(rsaPubKey == NULL)   // always fails here
{
        ERR_print_errors_fp(stdout);
        return -1;
}

The error is always: PEM_read_bio:no start line:pem_lib.c: 642:Expecting: RSA PUBLIC KEY I've added the \n newline characters to the pubKey variable, but I always get the same error. Is there something I'm missing in this process? I'm sure there is otherwise I wouldn't need to be posting to the list. :-)

Thanks for any help that anyone can provide,
Dan

Reply via email to