On Wed, Aug 15, 2007, Dan Spirlock wrote:

> 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,

Two problems, the sizeof is wrong: you'll end up passing the size of a pointer
which isn't what you want. Pass -1 instead and it automatically does a
strlen() on the buffer.

Second problem, wrong public key format. Use PEM_read_bio_RSA_PUBKEY()
instead.

Steve.
--
Dr Stephen N. Henson. Email, S/MIME and PGP keys: see homepage
OpenSSL project core developer and freelance consultant.
Funding needed! Details on homepage.
Homepage: http://www.drh-consultancy.demon.co.uk
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [email protected]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to