> From: owner-openssl-us...@openssl.org On Behalf Of Zach
> Sent: Tuesday, 16 April, 2013 15:55

> I'm still getting an error when trying to read this key using the BIO
> interface:
> 
> Error: error:0906D064:PEM routines:PEM_read_bio:bad base64 decode
> 
> More info below:
> 
> My pubkey looks like this (this is just a test key):
> (pubkey.h):
> static const char* pubkey = (char*)"\
> -----BEGIN PUBLIC KEY-----\n\
> MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAo+0cqJqlbJ7IoauOQzS\
> wm43nMeM1wgapDxhgeNxBFz8Y8WdC56iHu7ICQhrIybzK1Zv1a9dmExyeGhGPRM\
> vXYssNoOhphLFiN5mUwA3BNkxY2QwECESqPnlThXGiJ4bhBwvdXJ8ixtjGIh84P\
> BV70Hf1F+FfVQPbi7GctLWSx6JD5xLb9h5D0sdfierup0TfNDMgrVDwvIlG4iKe\
> GfB8npCUcicQ1E8pqx1axX3OxHIRr0dLIPrsPWKVj24jdeDZn0H+jhKxqus2/Yv\
> fdoPAnlKgltmlnon23C06hziIOwbvECDho9zrw+nQSWQIQvs1TXaSZjYgVM45Uk\
> zFNYn2Smv0efCUPEJa6gNawR/HFw8hIpBmtl6Jhm+du9AgLGU0j4pgAcw0xfj5F\
> vsjeZfQDHm9FIbhY9dOoqcCwoIV5gzsb224T2uIHc+glAPjCOS+3rEP1+YwcGIK\
> ObtIbzq2/rxS1HEx5z4NacLToOFZSKStgshXFQIjWCJ+2dCS8I4z5rkn1cP4bNR\
> RIB7J5gdOsq+NJuLjC42QfTW7+rq/9ivjAUPwbnytqfWITbJZB5RurumCnaURqb\
> 18v6kzvjO0A3Hxk2a1zjbpsO1+w9G3dW/F0fWqfn2JQoCTXKf1FJnzN+NaRMa5a\
> vt8ohOwbObEDRoEjaC/OqiERaX4pHrHhU8CAwEAAQ==\n\
> -----END PUBLIC KEY-----\n\
> ";
> 
The body part of your string (between BEGIN line and END line) must 
have newlines -- actual newlines in the data \n not discarded source 
linebreaks \(EOL) -- at intervals of no more than 76 characters. 
(The normal output from PEM_write and thus most commandline utilities 
is intervals of 64 characters, which is usually convenient. I observe 
you've broken your lines above at 63 for some reason.)

> My code to read the pubkey looks like this:
> 
> BIO* bio = BIO_new_mem_buf((void*)pubkey, -1);
> RSA* x = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL);
> 
Asides: you don't need the cast to void* there; if you've 
#include'd the declaration from pem.h as required it's 
protototyped and any C compiler since 1989 will convert.

You also don't need the cast to char* on your declaration 
above; any C compiler since about 1975 will "decay" a 
string literal which is char[] to char*. (But since 1980 
or so, not explicitly 'signed char*' or 'unsigned char*'.)

Finally, it's more portable and arguably clearer to write 
long string literals as a series of adjacent pieces, which 
any compiler since 1989 must concatenate, rather than one 
long logical line (after preprocessor eliminates \(EOL)) 
which compilers aren't required to support and some don't.
Although this only needs to be portable to compilers on 
systems that support OpenSSL, somewhat more restrictive.

char* foo = "abc\1" "def\2"
/*white space or comment ignored*/ "ghi\3"
                                        "jkl\4";

produces always exactly the same result as 
char *foo = "abc\1def\2ghi\3jkl\4";

but with shorter source lines.


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to