d2i_RSAPublicKey doesn't work

2005-07-18 Thread Edward Chan
Title: d2i_RSAPublicKey doesn't work






What am I doing wrong here?  I generate an RSA key.  Then I DER encode it.  Then try to decode it, but the decode fails.  The error says, "error:0D07207B:asn1 encoding routines:ASN1_get_object:header too long"

RSA* rsa = RSA_generate_key(2048, RSA_F4, 0, 0);

if (rsa)

{

    if (RSA_check_key(rsa) > 0)

    {

        int len = i2d_RSAPublicKey(rsa, 0);


        U8* buf = new U8[len];

        memset(buf, 0, len);


        i2d_RSAPublicKey(rsa, &buf);


        // everything looks good up to here; I can see buf gets filled with len number of bytes

        // but then I try to get the public key back by doing the following, and it fails.


        RSA* public_key = d2i_RSAPublicKey(0, (const U8**)&buf, len); // public_key is NULL; why???

        if (!public_key)

        {

            char err[1024];

            ERR_error_string(ERR_get_error(), err);

            fprintf(stderr, "Error : %s\n", err);

        }

    }

}

    





Re: d2i_RSAPublicKey doesn't work

2005-07-18 Thread Nils Larsch

Edward Chan wrote:
What am I doing wrong here?  I generate an RSA key.  Then I DER encode 
it.  Then try to decode it, but the decode fails.  The error says, 
"error:0D07207B:asn1 encoding routines:ASN1_get_object:header too long"


RSA* rsa = RSA_generate_key(2048, RSA_F4, 0, 0);
if (rsa)
{
if (RSA_check_key(rsa) > 0)
{
int len = i2d_RSAPublicKey(rsa, 0);

U8* buf = new U8[len];
memset(buf, 0, len);

i2d_RSAPublicKey(rsa, &buf);

// everything looks good up to here; I can see buf gets 
filled with len number of bytes
// but then I try to get the public key back by doing 
the following, and it fails.


RSA* public_key = d2i_RSAPublicKey(0, (const U8**)&buf, 
len); // public_key is NULL; why???

if (!public_key)
{
char err[1024];
ERR_error_string(ERR_get_error(), err);
fprintf(stderr, "Error : %s\n", err);
}
}
}


please read the FAQ

Nils

__
OpenSSL Project http://www.openssl.org
User Support Mailing Listopenssl-users@openssl.org
Automated List Manager   [EMAIL PROTECTED]


RE: d2i_RSAPublicKey doesn't work

2005-07-18 Thread Edward Chan
If you're referring to http://www.openssl.org/support/faq.html

3. How do I read or write a DER encoded buffer using the ASN1 functions?

...

The opposite assumes we already have len bytes in buf:

 unsigned char *p;
 p = buf;
 p7 = d2i_PKCS7(NULL, &p, len);

At this point p7 contains a valid PKCS7 structure of NULL if an error
occurred. If an error occurred ERR_print_errors(bio) should give more
information.

The reason for the temporary variable 'p' is that the ASN1 functions
increment the passed pointer so it is ready to read or write the next
structure. This is often a cause of problems: without the temporary
variable the buffer pointer is changed to point just after the data that
has been read or written. This may well be uninitialized data and
attempts to free the buffer will have unpredictable results because it
no longer points to the same address. 

--

I see where it says you need to create the temp var.  So changed my code
to do that, but I still get a null ptr returned.

Code is now:

U8* tmp = buf;
RSA* pub = d2i_RSAPublicKey(0, (const U8**)&tmp, *len);
 

> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Nils Larsch
> Sent: Monday, July 18, 2005 1:10 PM
> To: openssl-users@openssl.org
> Subject: Re: d2i_RSAPublicKey doesn't work
> 
> Edward Chan wrote:
> > What am I doing wrong here?  I generate an RSA key.  Then I 
> DER encode 
> > it.  Then try to decode it, but the decode fails.  The error says,
> > "error:0D07207B:asn1 encoding 
> routines:ASN1_get_object:header too long"
> > 
> > RSA* rsa = RSA_generate_key(2048, RSA_F4, 0, 0); if (rsa) {
> > if (RSA_check_key(rsa) > 0)
> > {
> > int len = i2d_RSAPublicKey(rsa, 0);
> > 
> > U8* buf = new U8[len];
> > memset(buf, 0, len);
> > 
> > i2d_RSAPublicKey(rsa, &buf);
> > 
> > // everything looks good up to here; I can see buf 
> > gets filled with len number of bytes
> > // but then I try to get the public key 
> back by doing 
> > the following, and it fails.
> > 
> > RSA* public_key = d2i_RSAPublicKey(0, (const 
> > U8**)&buf, len); // public_key is NULL; why???
> > if (!public_key)
> > {
> > char err[1024];
> > ERR_error_string(ERR_get_error(), err);
> > fprintf(stderr, "Error : %s\n", err);
> > }
> > }
> > }
> 
> please read the FAQ
> 
> Nils
> 
> __
> OpenSSL Project http://www.openssl.org
> User Support Mailing Listopenssl-users@openssl.org
> Automated List Manager   [EMAIL PROTECTED]
> 
__
OpenSSL Project http://www.openssl.org
User Support Mailing Listopenssl-users@openssl.org
Automated List Manager   [EMAIL PROTECTED]


Re: d2i_RSAPublicKey doesn't work

2005-07-19 Thread Nils Larsch

Edward Chan wrote:

If you're referring to http://www.openssl.org/support/faq.html

3. How do I read or write a DER encoded buffer using the ASN1 functions?

...

The opposite assumes we already have len bytes in buf:

 unsigned char *p;
 p = buf;
 p7 = d2i_PKCS7(NULL, &p, len);

At this point p7 contains a valid PKCS7 structure of NULL if an error
occurred. If an error occurred ERR_print_errors(bio) should give more
information.

The reason for the temporary variable 'p' is that the ASN1 functions
increment the passed pointer so it is ready to read or write the next
structure. This is often a cause of problems: without the temporary
variable the buffer pointer is changed to point just after the data that
has been read or written. This may well be uninitialized data and
attempts to free the buffer will have unpredictable results because it
no longer points to the same address. 


--

I see where it says you need to create the temp var.  So changed my code
to do that, but I still get a null ptr returned.

Code is now:

U8* tmp = buf;
RSA* pub = d2i_RSAPublicKey(0, (const U8**)&tmp, *len);


and what about i2d_RSAPublicKey ?

Nils
__
OpenSSL Project http://www.openssl.org
User Support Mailing Listopenssl-users@openssl.org
Automated List Manager   [EMAIL PROTECTED]


RE: d2i_RSAPublicKey doesn't work

2005-07-19 Thread Edward Chan
That part of the code remains the same, and continues to work fine.  By
the way, my last code snippet should have been:

U8* tmp = buf;
RSA* pub = d2i_RSAPublicKey(0, (const U8**)&tmp, len); 

I mistakenly typed *len instead of len.

Anyways, after I call i2d_RSAPublicKey, calling d2i_RSAPublicKey does
not seem to work. It always returns me a null ptr.  But I know the RSA
object is fine.  I can use it in RSA_public_encrypt() and
RSA_private_decrypt().

Any ideas?  Anybody else experiencing this problem?


> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Nils Larsch
> Sent: Tuesday, July 19, 2005 12:36 AM
> To: openssl-users@openssl.org
> Subject: Re: d2i_RSAPublicKey doesn't work
> 
> Edward Chan wrote:
> > If you're referring to http://www.openssl.org/support/faq.html
> > 
> > 3. How do I read or write a DER encoded buffer using the 
> ASN1 functions?
> > 
> > ...
> > 
> > The opposite assumes we already have len bytes in buf:
> > 
> >  unsigned char *p;
> >  p = buf;
> >  p7 = d2i_PKCS7(NULL, &p, len);
> > 
> > At this point p7 contains a valid PKCS7 structure of NULL 
> if an error 
> > occurred. If an error occurred ERR_print_errors(bio) should 
> give more 
> > information.
> > 
> > The reason for the temporary variable 'p' is that the ASN1 
> functions 
> > increment the passed pointer so it is ready to read or 
> write the next 
> > structure. This is often a cause of problems: without the temporary 
> > variable the buffer pointer is changed to point just after the data 
> > that has been read or written. This may well be 
> uninitialized data and 
> > attempts to free the buffer will have unpredictable results 
> because it 
> > no longer points to the same address.
> > 
> > --
> > 
> > I see where it says you need to create the temp var.  So changed my 
> > code to do that, but I still get a null ptr returned.
> > 
> > Code is now:
> > 
> > U8* tmp = buf;
> > RSA* pub = d2i_RSAPublicKey(0, (const U8**)&tmp, *len);
> 
> and what about i2d_RSAPublicKey ?
> 
> Nils
> __
> OpenSSL Project http://www.openssl.org
> User Support Mailing Listopenssl-users@openssl.org
> Automated List Manager   [EMAIL PROTECTED]
> 
__
OpenSSL Project http://www.openssl.org
User Support Mailing Listopenssl-users@openssl.org
Automated List Manager   [EMAIL PROTECTED]


Re: d2i_RSAPublicKey doesn't work

2005-07-20 Thread Dr. Stephen Henson
On Tue, Jul 19, 2005, Edward Chan wrote:

> That part of the code remains the same, and continues to work fine.  By
> the way, my last code snippet should have been:
> 
> U8* tmp = buf;
> RSA* pub = d2i_RSAPublicKey(0, (const U8**)&tmp, len); 
> 
> I mistakenly typed *len instead of len.
> 
> Anyways, after I call i2d_RSAPublicKey, calling d2i_RSAPublicKey does
> not seem to work. It always returns me a null ptr.  But I know the RSA
> object is fine.  I can use it in RSA_public_encrypt() and
> RSA_private_decrypt().
> 

What makes you think the way i2d_RSAPublicKey is being called is producing
valid data? Try dumping the data in 'tmp' to a file and using:

openssl asn1parse -inform DER -in whatever

if the result is an error message or what looks like garbage then its a
problem with the usage of i2d_RSAPublicKey. If it looks like the public key
components then it is OK.

BTW is there some reason you are using the RSAPublicKey functions? If you use
the RSA_PUBKEY versions instead the OpenSSL utilities will be able to check
the format directly.

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 Listopenssl-users@openssl.org
Automated List Manager   [EMAIL PROTECTED]


RE: d2i_RSAPublicKey doesn't work

2005-07-20 Thread Edward Chan
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?

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?

I am using the RSA_public_encrypt and RSA_private_decrypt functions
because that is what I found in the OpenSSL book I've been using as a
reference.  Should I be using something else? Is there an alternative to
the i2d* methods for converting the public and private portions of the
RSA key to some serializable form?

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?

Thanks,
Ed 

> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Dr. 
> Stephen Henson
> Sent: Wednesday, July 20, 2005 5:48 PM
> To: openssl-users@openssl.org
> Subject: Re: d2i_RSAPublicKey doesn't work
> 
> On Tue, Jul 19, 2005, Edward Chan wrote:
> 
> > That part of the code remains the same, and continues to 
> work fine.  
> > By the way, my last code snippet should have been:
> > 
> > U8* tmp = buf;
> > RSA* pub = d2i_RSAPublicKey(0, (const U8**)&tmp, len);
> > 
> > I mistakenly typed *len instead of len.
> > 
> > Anyways, after I call i2d_RSAPublicKey, calling 
> d2i_RSAPublicKey does 
> > not seem to work. It always returns me a null ptr.  But I 
> know the RSA 
> > object is fine.  I can use it in RSA_public_encrypt() and 
> > RSA_private_decrypt().
> > 
> 
> What makes you think the way i2d_RSAPublicKey is being called 
> is producing valid data? Try dumping the data in 'tmp' to a 
> file and using:
> 
> openssl asn1parse -inform DER -in whatever
> 
> if the result is an error message or what looks like garbage 
> then its a problem with the usage of i2d_RSAPublicKey. If it 
> looks like the public key components then it is OK.
> 
> BTW is there some reason you are using the RSAPublicKey 
> functions? If you use the RSA_PUBKEY versions instead the 
> OpenSSL utilities will be able to check the format directly.
> 
> 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 Listopenssl-users@openssl.org
> Automated List Manager   [EMAIL PROTECTED]
> 
__
OpenSSL Project http://www.openssl.org
User Support Mailing Listopenssl-users@openssl.org
Automated List Manager   [EMAIL PROTECTED]


Re: d2i_RSAPublicKey doesn't work

2005-07-21 Thread Dr. Stephen Henson
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 Listopenssl-users@openssl.org
Automated List Manager   [EMAIL PROTECTED]


RE: d2i_RSAPublicKey doesn't work

2005-07-21 Thread Edward Chan
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 Listopenssl-users@openssl.org
> Automated List Manager   [EMAIL PROTECTED]
> 
__
OpenSSL Project http://www.openssl.org
User Support Mailing Listopenssl-users@openssl.org
Automated List Manager   [EMAIL PROTECTED]


Re: d2i_RSAPublicKey doesn't work

2005-07-21 Thread Dr. Stephen Henson
On Thu, Jul 21, 2005, Edward Chan wrote:

> The code is basically like this:
> 
>   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)
>   {
> 
> 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?
> 

No, this code is wrong for the reasons mentioned in the FAQ. After this call
'buf' points to garbage and that's why d2i_RSAPublicKey() is failing.

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 Listopenssl-users@openssl.org
Automated List Manager   [EMAIL PROTECTED]


RE: d2i_RSAPublicKey doesn't work

2005-07-21 Thread Edward Chan
So I need to save buf to a tmp first like with d2i_RSAPublicKey?

Like this:

int len = i2d_RSAPublicKey(rsa, 0); 
unsigned char* buf = new unsigned char[len];
unsigned char* tmp = buf;
i2d_RSAPublicKey(rsa, &tmp);

// now use buf to decode

Is this correct?

> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Dr. 
> Stephen Henson
> Sent: Thursday, July 21, 2005 10:23 AM
> To: openssl-users@openssl.org
> Subject: Re: d2i_RSAPublicKey doesn't work
> 
> On Thu, Jul 21, 2005, Edward Chan wrote:
> 
> > The code is basically like this:
> > 
> > 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)
> > {
> > 
> > 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?
> > 
> 
> No, this code is wrong for the reasons mentioned in the FAQ. 
> After this call 'buf' points to garbage and that's why 
> d2i_RSAPublicKey() is failing.
> 
> 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 Listopenssl-users@openssl.org
> Automated List Manager   [EMAIL PROTECTED]
> 
__
OpenSSL Project http://www.openssl.org
User Support Mailing Listopenssl-users@openssl.org
Automated List Manager   [EMAIL PROTECTED]


Re: d2i_RSAPublicKey doesn't work

2005-07-21 Thread Dr. Stephen Henson
On Thu, Jul 21, 2005, Edward Chan wrote:

> So I need to save buf to a tmp first like with d2i_RSAPublicKey?
> 
> Like this:
> 
> int len = i2d_RSAPublicKey(rsa, 0); 
> unsigned char* buf = new unsigned char[len];
> unsigned char* tmp = buf;
> i2d_RSAPublicKey(rsa, &tmp);
> 
> // now use buf to decode
> 
> Is this correct?
> 

Yes, that's correct.

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 Listopenssl-users@openssl.org
Automated List Manager   [EMAIL PROTECTED]


RE: d2i_RSAPublicKey doesn't work

2005-07-21 Thread Edward Chan
Horray...that is it.  Thanks!!! 

> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Edward Chan
> Sent: Thursday, July 21, 2005 11:00 AM
> To: openssl-users@openssl.org
> Subject: RE: d2i_RSAPublicKey doesn't work
> 
> So I need to save buf to a tmp first like with d2i_RSAPublicKey?
> 
> Like this:
> 
> int len = i2d_RSAPublicKey(rsa, 0);
> unsigned char* buf = new unsigned char[len]; unsigned char* 
> tmp = buf; i2d_RSAPublicKey(rsa, &tmp);
> 
> // now use buf to decode
> 
> Is this correct?
> 
> > -Original Message-
> > From: [EMAIL PROTECTED] 
> > [mailto:[EMAIL PROTECTED] On Behalf Of Dr.
> > Stephen Henson
> > Sent: Thursday, July 21, 2005 10:23 AM
> > To: openssl-users@openssl.org
> > Subject: Re: d2i_RSAPublicKey doesn't work
> > 
> > On Thu, Jul 21, 2005, Edward Chan wrote:
> > 
> > > The code is basically like this:
> > > 
> > >   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)
> > >   {
> > > 
> > > 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?
> > > 
> > 
> > No, this code is wrong for the reasons mentioned in the FAQ. 
> > After this call 'buf' points to garbage and that's why
> > d2i_RSAPublicKey() is failing.
> > 
> > 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 Listopenssl-users@openssl.org
> Automated List Manager   [EMAIL PROTECTED]
> 
__
OpenSSL Project http://www.openssl.org
User Support Mailing Listopenssl-users@openssl.org
Automated List Manager   [EMAIL PROTECTED]


RE: d2i_RSAPublicKey doesn't work

2005-07-22 Thread Frédéric Donnat
Hi,

According to the man page, d2i_xx method is able to allocate memory for you.
d2i_PUBKEY_xxx

If you are using OpenSSL 0.9.7 or later then this can be simplified to:
int len;
unsigned char *buf;

buf = NULL;
len = i2d_X509(x, &buf);
if (len < 0)
   /* error */

But i think you have to free the memory using OPENSSSL_free().

Regards,

fred


-Original Message-
From:   Edward Chan [mailto:[EMAIL PROTECTED]
Sent:   Thu 7/21/2005 8:08 PM
To: openssl-users@openssl.org
Cc: 
Subject:    RE: d2i_RSAPublicKey doesn't work
Horray...that is it.  Thanks!!! 

> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Edward Chan
> Sent: Thursday, July 21, 2005 11:00 AM
> To: openssl-users@openssl.org
> Subject: RE: d2i_RSAPublicKey doesn't work
> 
> So I need to save buf to a tmp first like with d2i_RSAPublicKey?
> 
> Like this:
> 
> int len = i2d_RSAPublicKey(rsa, 0);
> unsigned char* buf = new unsigned char[len]; unsigned char* 
> tmp = buf; i2d_RSAPublicKey(rsa, &tmp);
> 
> // now use buf to decode
> 
> Is this correct?
> 
> > -Original Message-
> > From: [EMAIL PROTECTED] 
> > [mailto:[EMAIL PROTECTED] On Behalf Of Dr.
> > Stephen Henson
> > Sent: Thursday, July 21, 2005 10:23 AM
> > To: openssl-users@openssl.org
> > Subject: Re: d2i_RSAPublicKey doesn't work
> > 
> > On Thu, Jul 21, 2005, Edward Chan wrote:
> > 
> > > The code is basically like this:
> > > 
> > >   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)
> > >   {
> > > 
> > > 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?
> > > 
> > 
> > No, this code is wrong for the reasons mentioned in the FAQ. 
> > After this call 'buf' points to garbage and that's why
> > d2i_RSAPublicKey() is failing.
> > 
> > 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 Listopenssl-users@openssl.org
> Automated List Manager   [EMAIL PROTECTED]
> 
__
OpenSSL Project http://www.openssl.org
User Support Mailing Listopenssl-users@openssl.org
Automated List Manager   [EMAIL PROTECTED]



<>