OpenSSL library works with base64-encoded strings as a part of its public keys infrastructure, for example. That means, it has base64 encoding and decoding. The other thing is, if is it possible and if is it easy to utilize that encoding. We found it is possible, but it has some unexpected issue.
The thing is (and it was determined while debugging under Microsoft Visual C++), when static function b64_read() in bio_b64.c works, it looks for a ‘\n’ character in text and does decoding only if this symbol exists.
As base64 decoding function in any application is usually called many times, we propose to implement it as a function:
vector<uint8_t> Base64toBin(const char* strBase64)
{
BIO *bio, *b64;
int inlen;
uint8_t inbuf[512];
b64 = BIO_new(BIO_f_base64());
string tmpString = strBase64;
if (tmpString.find('\n') == string::npos)
tmpString = tmpString + "\n";
bio = BIO_new_mem_buf(tmpString.c_str(), tmpString.size());
BIO_push(b64, bio);
vector<uint8_t> res;
while ((inlen = BIO_read(b64, inbuf, 512)) > 0)
{
for (int i = 0; i < inlen; i++)
{
vector<uint8_t> tmp(1, inbuf[i] );
res = res + tmp;
}
}
BIO_free_all(b64);
return res;
}