I have some base64 encoded data in my own buffer (a character array).
I want to decode this.
From the man pages it appeared to me that I should
1) create a memory bio,
2) populate it with my base64 encoded data.
3) Create a base64 filter bio
4) Create a chain like this:
[base64_bio]-----[mem_bio]
5) Read from the chain
Sadly this does not work. Read always signals EOF/EOD. Read always
returns -1. BIO_should_retry() or BIO_should_read() is true after
every subsequent attempt to read from the chain.
Here is the program:
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<openssl/bio.h>
#include<openssl/evp.h>
int main()
{
int n;
char encodedCertData[] = "SGVsbG8gV29ybGQhCg==";
char certData[500];
BIO *bio = BIO_new(BIO_s_mem());
/* Here, I have also tried using the BIO_new_mem_buf(), as my data is
fixed and available in memory to me all at once. But that too did not
work */
n = BIO_write(bio, encodedCertData, strlen(encodedCertData));
BIO *b64 = BIO_new(BIO_f_base64());
BIO *bioChain = BIO_push(b64, bio);
while(1)
{
n = BIO_read(bioChain, certData, 450);
if(n <= 0)
{
printf("Something happened. Some kireek for sure n = %d\n", n);
if(!BIO_should_retry(bioChain) || !BIO_should_read(bioChain))
{
printf("The End\n");
break;
}
sleep(2);
continue;
}
certData[n] = '\0';
printf("Decoded data = %s\n", certData);
}
BIO_free_all(bioChain);
return 0;
}
If I replace the [mem_bio] with a [fp_bio], it works fine, i.e if I
place the base64 encoded data in a file and create a chain like this:
[base64_bio]-----[fp_bio]
and then read from the chain, the data is properly decoded, all in one
go.
Here is the program:
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<openssl/bio.h>
#include<openssl/evp.h>
int main()
{
int n;
char certData[500];
BIO *bio = BIO_new_file("/home/srirang/crap/openssl/b64-encoded-
data", "r");
BIO *b64 = BIO_new(BIO_f_base64());
BIO *bioChain = BIO_push(b64, bio);
while(1)
{
n = BIO_read(bioChain, certData, 450);
if(n <= 0)
{
printf("Something happened. Some kireek for sure n = %d\n", n);
if(!BIO_should_retry(bioChain) || !BIO_should_read(bioChain))
{
printf("The End\n");
break;
}
sleep(2);
}
certData[n] = '\0';
printf("Decoded data chunk = %s\n", certData);
}
BIO_free_all(bioChain);
return 0;
}
I read this posting : http://marc.info/?l=openssl-users&m=123171064303018&w=2
and this : http://markmail.org/message/cdndl7pofs7maixq#query:+page:1+mid:hts7qlqkz3yzsmz2+state:results
but they did not solve my problem. The first posting is large and
elaborate and I did understand parts of it. Probably its the parts
that I did not understand that are needed here.
Either ways, any help is really appreciated.
Just to give the complete picture, I have the contents of a PKCS#12
certificate in base64 encoded form in a char array. I need to base64
decode it and then parse it (and later use it as the client
certificate). I want to avoid writing this to a file and processing it
and hence I am using memory buffer. If there is a better way to
achieve this without the the exercise that I am trying to do, kindly
point that out too.
Thank you.
Regards,
Brahmana
(Srirang G Doddihal)
Just bumping this up the thread. Hope some one can help me, either
identifying where I am going wrong or what is the right way to do
this..
Thanks.
Regards,
Brahmana