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);
                }
        }
}
       

Reply via email to