In message <[EMAIL PROTECTED]> on Wed, 17 Sep 2003 15:36:12 -0500, Ashutosh Jaiswal 
<[EMAIL PROTECTED]> said:

ashutosh> I seem to be working on a similar problem. Here's my small function
ashutosh> (with the help from the man pages). Please correct me if I'm wrong:
ashutosh> 
ashutosh> int PKCS2DER(PKCS7 *p7, uchar *DER){
ashutosh> 
ashutosh>     int len;
ashutosh> 
ashutosh>     uchar *buf = NULL;
ashutosh> 
ashutosh>     len = i2d_PKCS7(p7, NULL);
ashutosh>     buf = OPENSSL_malloc(len);
ashutosh> 
ashutosh>     if (buf == NULL)
ashutosh>       return(0);
ashutosh> 
ashutosh>     DER = buf;
ashutosh>     i2d_PKCS7(p7, &DER);
ashutosh> 
ashutosh>     return(len);
ashutosh> 
ashutosh> }

As has been said before, that won't give you the result you want.  I'm
assuming that you want to be able to have code like this:

        PKCS7 *p7; /* Actual pointer given somewhere else */
        uchar *der = NULL;
        int derlen = PKCS2DER(p7, &der);

The reason that won't work is that the second argument is a 'uchar **',
not a 'uchar *'.  If you did a call like this: PKCS2DER(p7, der), you
would just pass a NULL to the function, not a pointer to the pointer
you want to have changed.

Somehwere, you mentioned using the example for using i2d_X509() (in
d2i_X509.pod) as inspiration, but you didn't realise that it was an
example to put directly in your code, and that building a function
around it required a bit of extra thinking on your part.

So, if your function looks like this, it'll work as expected:

int PKCS2DER(PKCS7 *p7, uchar **DER)
{
    int len;
    uchar *buf = NULL;

    len = i2d_PKCS7(p7, NULL);
    buf = OPENSSL_malloc(len);

    if (buf == NULL)
        return(0);

    *DER = buf;
    i2d_PKCS7(p7, buf); /* Don't give DER here, because i2d_ functions
                           move the given pointer.  We don't need buf
                           below this point, so that's a better
                           candidate */

    return(len);
}


Note: I haven't actually verified that the modified code is clear of
bugs, I've only changed what I could directly see wouldn't work.
You're welcome to use my code in your program and to work at it as you
see fit.  You're NOT welcome to come screaming at me for errors I
haven't caught.

-- 
Richard Levitte   \ Tunnlandsv�gen 3  \ [EMAIL PROTECTED]
[EMAIL PROTECTED]  \ S-168 36  BROMMA  \ T: +46-8-26 52 47
                    \      SWEDEN       \ or +46-708-26 53 44
Procurator Odiosus Ex Infernis                -- [EMAIL PROTECTED]
Member of the OpenSSL development team: http://www.openssl.org/

Unsolicited commercial email is subject to an archival fee of $400.
See <http://www.stacken.kth.se/~levitte/mail/> for more info.
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [EMAIL PROTECTED]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to