Hi,

There are two bugs in your code: First, you only need calls to BIO_read not BIO_write, and in order to avoid the crash you have to pass a pointer equal to &inbuf instead of &inbuf directly because d2i_PKCS7 increment the pointer internally.
So, a more correct version of the code will be :

    BIO *bio, *b64;
    char inbuf[4096];
    int inlen = strlen(a);
    char* ptr = &inbuf[0];
    b64 = BIO_new(BIO_f_base64());
    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
    bio=BIO_new_mem_buf(a, -1);
    bio = BIO_push(b64, bio);

    inlen=BIO_read(bio, inbuf, 4096);

    p7 = d2i_PKCS7(NULL, &ptr, inlen);

I hope this will help.
--
Mounir IDRASSI
IDRIX
http://www.idrix.fr


On 4/3/2010 3:06 AM, sean wang wrote:

  Hello,

I have a pkcs7 encoded cert which i want to load. the following code block 
works fine: ( I am doing a base64 decoding first, will explain the reason in 
the second half):

  BIO *bio, *b64;
  PKCS7 * p7;
  b64 = BIO_new(BIO_f_base64());
  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);

  bio=BIO_new_mem_buf(a, -1);

  bio_out= BIO_new_fp(stderr, BIO_NOCLOSE);
  bio = BIO_push(b64, bio);

  p7 = d2i_PKCS7_bio(bio, NULL);

However, for some interface reasons, I need to pass pass in a base64 decoded 
blob to a legacy function,  so i tried the following code:

BIO *bio, *b64, *bio_out;
char inbuf[4096];
int inlen = strlen(a);
b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
bio=BIO_new_mem_buf(a, -1);
bio_out= BIO_new_fp(stderr, BIO_NOCLOSE);
bio = BIO_push(b64, bio);

while((inlen=BIO_read(bio, inbuf, 4096))>0)
BIO_write(bio_out, inbuf, inlen);

p7 = d2i_PKCS7(NULL,&inbuf, inlen);

now this code fails with a segment fault.

So my question is, if i really need to get the based64 decoded blob of a pkcs 7 
cert, what is the right way?
(because the first code worked, I assume the base64 decoding worked fine, but I 
can't pull the data out of 'bio' variable, appears there the data is still 
base64 encoded. how can i get the correct decoded blob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H+w0ZGna\
OUP4KRvWhjC/AKEAMQA=";



                                        
_________________________________________________________________
The New Busy think 9 to 5 is a cute idea. Combine multiple calendars with 
Hotmail.
http://www.windowslive.com/campaign/thenewbusy?tile=multicalendar&ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_5

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

Reply via email to