d2i_RSAPublicKey and DER format

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

d2i_RSAPublicKey and DER format

Robin Hughes
Hello,

I'm would like to generate a key pair using the
openssl command line utilities and store them in
files.  The private key will be used by a Java program
that will do the signing.  The public key will be used
by a C++ program (using openssl lib) to do the
verification.

I am having trouble reconstituting the public key from
a DER format file.  I generated the file as follows:

> openssl genrsa prikey.dat 2048

> openssl rsa -in pri_key.dat -out pub_key.der \
  -outform DER -pubout

In my C++ program I read pub_key.der and attempt to
instantiate an RSA object:

    char filename[] = "pub_key.der";
    ifstream file1;
   
   
    file1.open(filename, std::ios::binary);
    char c;
    while (file1.get(c)) {
        bytes[i++] = (unsigned char) c;
    }
    file1.clear();
    file1.close();
    cout <<"Done reading public key - length is " << i
<< endl;
 
    const unsigned char *p = (const unsigned char *)
bytes;

    RSA *rsakey = NULL;
    rsakey = d2i_RSAPublicKey(NULL, &p, i);
    // alternatively:  d2i_RSAPublicKey(&rsakey, &p,
i);
       
    if (rsakey == NULL) {
        cout << "error reconstituting public key" << endl;
        return false;
    }

//...continue


I get:
Done reading public key - length is 294
error reconstituting public key


Am I misunderstanding something about the
compatibility of "-outform DER" with
d2i_RSAPublicKey??

Thanks for any help,

Robin

PS


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: d2i_RSAPublicKey and DER format

Dr. Stephen Henson
On Fri, Nov 18, 2005, Robin Hughes wrote:

>
>
> Am I misunderstanding something about the
> compatibility of "-outform DER" with
> d2i_RSAPublicKey??
>

Yes, the formats are incompatible. You need d2i_RSA_PUBKEY() instead.

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                    [hidden email]
Automated List Manager                           [hidden email]