The code is basically like this:

RSA* rsa = RSA_generate_key(2048, RSA_F4, 0, 0);
// check if RSA key is valid
if (rsa && RSA_check_key(rsa) > 0)
{
        // find size of buffere required to encode public key
        int len = i2d_RSAPublicKey(rsa, 0);

        // allocate buffer
        unsigned char* buf = new unsigned char[len];

        // now call again to DER encode the public key
        if (i2d_RSAPublicKey(rsa, &buf) == len)
        {
                // now try to decode the buffer
                unsigned char* tmp = buf;
                RSA* public_key = d2i_RSAPublicKey(0, (const unsigned
char**)&tmp, len);
                if (public_key)
                {
                        printf("yeah, we successfully DER decoded the
public key.\n");
                }
                else
                {
                        char err[1024];
                        ERR_error_string(ERR_get_error(), err);
                        printf("Failed to DER decode public key : %s\n",
err);
                }
        }
} 

I've also tried creating the RSA object first, and passing it into
d2i_RSAPublicKey() for it to fill in.  It also returns me null back.
Does the code look right?

Thanks,
Ed

> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Dr. 
> Stephen Henson
> Sent: Thursday, July 21, 2005 4:12 AM
> To: openssl-users@openssl.org
> Subject: Re: d2i_RSAPublicKey doesn't work
> 
> On Wed, Jul 20, 2005, Edward Chan wrote:
> 
> > Hmm, well, I guess I assumed i2d_RSAPublicKey() was ok 
> since the RSA 
> > key seems fine (I ran RSA_check_key() on it and it says it 
> is ok).  Is 
> > there any reason why i2d_RSAPublicKey() would not be 
> returning me valid data?
> > 
> 
> Normally only if it is not called correctly.
> 
> > int len = i2d_RSAPublicKey(rsa, 0);
> > 
> > returns me something > 0, so I assumed that it is fine.  
> Wouldn't this 
> > return -1 if it failed?
> > 
> 
> Currently most i2d functions will never return -1 though that 
> may change in future.
> 
> > As for the usage of i2d_RSAPublicKey(), it is as the book says.  I 
> > call it once to find the size of the buffer required.  Then 
> I allocate 
> > the buffer.  Then call it again to actually fill the 
> buffer.  Is this 
> > not correct?
> > 
> 
> What code are you using to fill the buffer?
> 
> Steve.
> --
> Dr Stephen N. Henson. Email, S/MIME and PGP keys: see 
> homepage OpenSSL project core developer and freelance consultant.
> Funding needed! Details on homepage.
> Homepage: http://www.drh-consultancy.demon.co.uk
> ______________________________________________________________________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    openssl-users@openssl.org
> Automated List Manager                           [EMAIL PROTECTED]
> 
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to