[openssl-users] base64 decode in C

Dave Thompson dthompson at prinpay.com
Wed Mar 18 11:34:41 UTC 2015

> From: openssl-users On Behalf Of Prashant Bapat
> Sent: Wednesday, March 18, 2015 03:37

> I'm trying to use the base64 decode function in C. <snip>

> This works well for simple b64 encoded strings like "hello world!" etc. 
> But when I want to b64 decode the contents of a SSH public key, it fails. Returns nothing. 

It returns pointer to a buffer containing the pubkey, but no indication of 
the length. You don't show the caller, but if the caller tries to treat it as a 
C string with strlen()fputs() etc that won't work because it isn't a C string.
A C string can't contain any zero-bits aka null byte, and SSH pubkey contains 
many of them; it even starts with *three* consecutive nulls, which 
makes it appear to be empty if treated as a C string.

Use the length returned from (successful) BIO_read (b64bio,...).

> This decodes fine. 
> "dGhpcyBpcyBhd2Vzb21lCg=="  : "this is awesome"

Actually that decodes as "this is awesome" PLUS A NEWLINE, or in 
C source notation "this is awesome\n". Yes that matters. 

More information about the openssl-users mailing list